Ticket #169: duplicate_album_fix.patch

File duplicate_album_fix.patch, 20.0 KB (added by sidtheduck, 5 months ago)
  • plog-admin/plog-admin-functions.php

     
    4141 
    4242        $result = array( 
    4343                'output' => '', 
     44                'errors' => '', 
    4445                'picture_id' => false, 
    4546        ); 
    4647 
     
    296297        $collection_name = trim(SmartStripSlashes($collection_name)); 
    297298        if (empty($collection_name)) { 
    298299                return array("errors" => plog_tr("Please enter a valid name for the collection")); 
    299         }; 
    300  
    301         // do not allow collections with duplicate names, otherwise mod_rewritten links will start 
    302         // to behave weird. 
    303         $collection_exists = get_collection_by_name($collection_name); 
    304         if ($collection_exists) { 
    305                 return array("errors" => sprintf(plog_tr('New collection could not be created, because there already is one named `%s`!'),$collection_exists)); 
    306300        } 
    307301 
    308302        $collection_folder = strtolower(sanitize_filename($collection_name)); 
     
    311305        // anyway 
    312306        $create_path = $config["basedir"] . "plog-content/images/".$collection_folder; 
    313307 
     308        // do not allow collections with duplicate names, otherwise mod_rewritten links will start 
     309        // to behave weird. 
     310        if (is_dir($create_path)) { 
     311                // if there is already a directory, check to see if it's in the database 
     312                $collection_data = get_collection_by_name($collection_name); 
     313                if ($collection_data){ 
     314                        // it's in the database, so throw duplicate collection error 
     315                        return array("errors" => sprintf(plog_tr('New collection could not be created, because there already is one named `%s`!'),$collection_name)); 
     316                } else{ 
     317                        // it's not in the database so attempt to delete the directory 
     318                        if (!@rmdir($target_path)){ 
     319                                // could not delete the directory, so prompt the user to delete it manually 
     320                                return array("errors" => sprintf(plog_tr('Collection directory `%s` exists, but no collection exists in the database. Attempt to delete automatically failed. Please delete folder via FTP manually and try again.'), $target_path)); 
     321                        } 
     322                } 
     323        } 
     324 
    314325        // create directory 
    315326        if (!makeDirs($create_path, 0777)) { 
    316327                $errors .= sprintf(plog_tr("Could not create directory %s!"),$create_path); 
     
    323334                $id = mysql_insert_id(); 
    324335 
    325336                $output .= sprintf(plog_tr('You have successfully created the collection <strong>%s</strong>'),$collection_name);     
    326         }; 
     337        } 
    327338 
    328339        // caller can check the value of id, if it is zero, then collection creation failed 
    329340        // errors and output are separate, because this way the caller can format the return value 
     
    345356        $name = trim(SmartStripSlashes($name)); 
    346357        if (empty($name)) { 
    347358                return array("errors" => plog_tr("Please enter a valid name for the collection")); 
    348         }; 
     359        } 
    349360 
    350361        $target_name = strtolower(sanitize_filename($name)); 
    351362 
     
    366377        $result = run_query($sql); 
    367378        $row = mysql_fetch_assoc($result); 
    368379 
     380        $source_collection_name = SmartStripSlashes($row["collection_path"]); 
     381        $source_path = $config["basedir"] . "plog-content/images/".$source_collection_name; 
     382        $target_path = $config["basedir"] . "plog-content/images/".$target_name; 
     383 
    369384        // do not allow collections with duplicate names, otherwise mod_rewritten links will start 
    370385        // to behave weird. 
    371         $collection_exists = get_collection_by_name($name); 
    372         if ($row["name"] != $name && $collection_exists) { 
    373                 return array("errors" => sprintf(plog_tr('Collection `%s could not be renamed to `%s, because there is another collection with that name'),$row['name'],$name)); 
     386        if ($row["name"] != $name && is_dir($target_path)) { 
     387                // if there is already a directory, check to see if it's in the database 
     388                $collection_data = get_collection_by_name($name); 
     389                if ($collection_data){ 
     390                        // it's in the database, so throw duplicate collection error 
     391                        return array("errors" => sprintf(plog_tr('Collection `%s` could not be renamed to `%s`, because there is another collection with that name'),$row['name'],$name)); 
     392                } else{ 
     393                        // it's not in the database so attempt to delete the directory 
     394                        if (!@rmdir($target_path)){ 
     395                                // could not delete the directory, so prompt the user to delete it manually 
     396                                return array("errors" => sprintf(plog_tr('Collection directory `%s` exists, but no collection exists in the database. Attempt to delete automatically failed. Please delete folder via FTP manually and try again.'), $target_path)); 
     397                        } 
     398                } 
    374399        } 
    375400 
    376         $source_collection_name = SmartStripSlashes($row["collection_path"]); 
    377         $source_path = $config["basedir"] . "plog-content/images/".$source_collection_name; 
    378         $target_path = $config["basedir"] . "plog-content/images/".$target_name; 
    379  
    380401        // perform the rename on the directory 
    381402        if (!rename($source_path, $target_path)) { 
    382403                return array("errors" => sprintf(plog_tr("Error renaming directory! (%s to %s)"),$source_path,$target_path)); 
    383         }; 
     404        } 
    384405 
    385406        $target_name = mysql_real_escape_string($target_name); 
    386407 
     
    388409        $result = mysql_query($query); 
    389410        if (!$result) { 
    390411                return array("errors" => mysql_error()); 
    391         }; 
     412        } 
    392413 
    393414        $output = plog_tr('You have successfully modified the selected collection.'); 
    394415 
     
    493514        $album_name = trim(SmartStripSlashes($album_name)); 
    494515        if (empty($album_name)) { 
    495516                return array("errors" => plog_tr("Please enter a valid name for the album")); 
    496         }; 
     517        } 
    497518        // get the parent collection name 
    498519        $query = "SELECT c.path as collection_path FROM ". TABLE_PREFIX."collections c WHERE id = '$pid'"; 
    499520 
     
    503524        // this shouldn't happen 
    504525        if (empty($row)) { 
    505526                return array("errors" => plog_tr("No such collection")); 
    506         }; 
     527        } 
    507528 
    508529        $album_folder = strtolower(sanitize_filename($album_name)); 
    509530 
     
    511532        // will be unusable anyway 
    512533        $create_path = $config["basedir"] . "plog-content/images/".SmartStripSlashes($row["collection_path"])."/".$album_folder; 
    513534 
     535        // check path so we are not creating duplicate albums within the same collection 
     536        if (is_dir($create_path)){ 
     537                // if there is already a directory, check to see if it's in the database 
     538                $album_data = get_album_by_name($album_name, $pid); 
     539                if ($album_data) { 
     540                        // it's in the database, so throw duplicate album error 
     541                        return array("output" => "existing", "id" => $album_data['id'], "errors" => sprintf(plog_tr('New album could not be created, because there already is one named `%s` in the collection `%s`!'), $album_folder, SmartStripSlashes($row['collection_path']))); 
     542                } else { 
     543                        // it's not in the database so attempt to delete the directory 
     544                        if (!@rmdir($create_path)){ 
     545                                // could not delete the directory, so prompt the user to delete it manually 
     546                                return array("errors" => sprintf(plog_tr('Album directory `%s` exists, but no album exists in the database. Attempt to delete automatically failed. Please delete folder via FTP manually and try again.'), $create_path)); 
     547                        } 
     548                } 
     549        } 
     550 
    514551        if (!makeDirs($create_path, 0777)) { 
    515552                $errors .= sprintf(plog_tr("Could not create directory %s!"),$path); 
    516553        } else { 
     
    522559                $id = mysql_insert_id(); 
    523560 
    524561                $output .= sprintf(plog_tr('You have successfully created the album <strong>%s</strong>'),$album_name); 
    525         }; 
     562        } 
    526563        // caller can check the value of id, if it is zero, then album creation failed 
    527564        // errors and output are separate, because this way the caller can format the return value 
    528565        // as it needs 
     
    561598        $source_path = $config['basedir'] . "plog-content/images/".$source_collection_name."/".$source_album_name; 
    562599        $target_path = $config['basedir'] . "plog-content/images/".$source_collection_name."/".$target_name; 
    563600 
     601        // check path so we are not creating duplicate albums within the same collection 
     602        if (is_dir($target_path)){ 
     603                // if there is already a directory, check to see if it's in the database 
     604                $album_data = get_album_by_name($album_name, $to_collection); 
     605                if ($album_data) { 
     606                        // it's in the database, so throw duplicate album error 
     607                        return array("errors" => sprintf(plog_tr('New album could not be created, because there already is one named `%s` in the collection `%s`!'), $target_name, $source_collection_name)); 
     608                } else { 
     609                        // it's not in the database so attempt to delete the directory 
     610                        if (!@rmdir($target_path)){ 
     611                                // could not delete the directory, so prompt the user to delete it manually 
     612                                return array("errors" => sprintf(plog_tr('Album directory `%s` exists, but no album exists in the database. Attempt to delete automatically failed. Please delete folder via FTP manually and try again.'), $target_path)); 
     613                        } 
     614                } 
     615        } 
     616 
    564617        // perform the rename on the directory 
    565618        if (!rename($source_path, $target_path)) 
    566619        { 
    567620                return array( 
    568621                        "errors" => sprintf(plog_tr("Error renaming directory! (%s to %s)"),$source_path,$target_path)); 
    569         }; 
     622        } 
    570623 
    571624        $target_name = mysql_real_escape_string($target_name); 
    572625 
     
    581634        $result = mysql_query($query); 
    582635        if (!$result) { 
    583636                return array("errors" => mysql_error()); 
    584         }; 
     637        } 
    585638 
    586639        $output .= plog_tr('You have successfully modified the selected album.'); 
    587640 
     
    632685        }; 
    633686} 
    634687 
    635 function move_album($album_id,$to_collection) { 
     688function move_album($album_id, $to_collection) { 
    636689        global $config; 
    637690 
    638691        $res = array( 
     
    665718        $source_path = $config['basedir']."plog-content/images/".$source_collection_name."/".$source_album_name; 
    666719        $target_path = $config['basedir']."plog-content/images/".$target_collection_name."/".$source_album_name; 
    667720 
     721        // check path so we are not creating duplicate albums within the same collection 
     722        if (is_dir($target_path)){ 
     723                // if there is already a directory, check to see if it's in the database 
     724                $album_data = get_album_by_name($source_album_name, $to_collection); 
     725                if ($album_data) { 
     726                        // it's in the database, so throw duplicate album error 
     727                        return array("errors" => sprintf(plog_tr('New album could not be created, because there already is one named `%s` in the collection `%s`!'), $source_album_name, $target_collection_name)); 
     728                } else { 
     729                        // it's not in the database so attempt to delete the directory 
     730                        if (!@rmdir($target_path)){ 
     731                                // could not delete the directory, so prompt the user to delete it manually 
     732                                return array("errors" => sprintf(plog_tr('Album directory `%s` exists, but no album exists in the database. Attempt to delete automatically failed. Please delete folder via FTP manually and try again.'), $target_path)); 
     733                        } 
     734                } 
     735        } 
     736 
    668737        // attempt to make new album directory in target collection 
    669         @mkdir($target_path, 0775); 
     738        if (!makeDirs($target_path, 0777)) { 
     739                return array("errors" => sprintf(plog_tr('Could not create directory `%s`!'),$target_path)); 
     740        } 
    670741 
    671742        //if (!rename($source_path, $target_path)) 
    672743        //  $output .= '<p class="errors">Could not rename directory!</p>'; 
     
    685756                $new_path = $target_path."/".$filename; 
    686757 
    687758                if (!rename($old_path, $new_path)) 
    688                         $res['errors'] .=  sprintf(plog_tr("Could not move file! %s to %s"),$old_path,$new_path); 
     759                        $res['errors'] .=  sprintf(plog_tr("Could not move file! `%s` to `%s`"),$old_path,$new_path); 
    689760 
    690761                $path_insert = mysql_real_escape_string($target_collection_name."/".$source_album_name."/".$filename); 
    691762 
     
    700771        $query = "UPDATE ".TABLE_PREFIX."albums SET `parent_id` = '$to_collection' WHERE `id`='$album_id'"; 
    701772        $result = run_query($query); 
    702773 
     774        // attempt to delete the old folder if there were no errors moving the files 
     775        if ($res['errors'] == ''){ 
     776                @chmod($source_path,0777); 
     777                if (!rmdir($source_path)) { 
     778                        return array("errors" => sprintf(plog_tr('Could not remove album from collection `%s`. Album still contains files after all pictures have been moved.'), $source_collection_name)); 
     779                } 
     780        } 
     781 
    703782        return $res; 
    704783} 
    705784 
     
    10691148function plog_add_album_form($parent_collection) { 
    10701149        $parent_collection = intval($parent_collection); 
    10711150        $output = "\n\t\t" . '<input type="button" class="submit" id="show-album" onclick="toggle(\'create-album\'); toggle(\'show-album\')" value="' . plog_tr('Create an Album') . '" />'; 
    1072         $output .= "\n\t\t" . '<form action="'.$_SERVER["PHP_SELF"].'" method="post"> 
     1151        $output .= "\n\t\t" . '<form action="'.$_SERVER["REQUEST_URI"].'" method="post"> 
    10731152                        <div id="create-album" class="cssbox-green" style="width: 385px !important; display: none;"> 
    10741153                                <div class="cssbox_head-green"><h2>' . plog_tr('Create an Album') . '</h2></div> 
    10751154                                <div class="cssbox_body-green"> 
     
    10901169        global $config, $thumbnail_config; 
    10911170        $output = ''; 
    10921171        $collection_id = intval($collection_id); 
    1093         $output .= "\n\t\t" . '<form class="edit" action="'.$_SERVER["PHP_SELF"].'" method="post">'; 
     1172        $output .= "\n\t\t" . '<form class="edit" action="'.$_SERVER["REQUEST_URI"].'" method="post">'; 
    10941173        $collection = get_collection_by_id($collection_id); 
    10951174 
    10961175        $auto_graphic = $config['gallery_url']."plog-admin/images/auto.gif"; 
  • plog-admin/plog-import.php

     
    4646// Check if update has been clicked, handle erroneous conditions, or upload 
    4747//print_r($_POST); 
    4848 
    49 if (isset($_POST['upload'])){ 
     49if (isset($_POST['upload'])) { 
    5050 
    5151        $destinations = isset($_POST['destinations']) ? $_POST['destinations'] : ''; 
    5252        $captions = $_POST['captions']; 
     
    5858 
    5959        $files = get_files($config['basedir'] . 'plog-content/uploads'); 
    6060 
    61         if ($_POST['destination_radio'] == "new" && $_POST["new_album_name"] == ""){ 
     61        if ($_POST['destination_radio'] == "new" && $_POST["new_album_name"] == "") { 
    6262                $output .= "\n\t" . '<p class="errors">' . plog_tr('New album name not specified!') . '</p>' . "\n"; 
    63         } 
    64         else { 
     63        } else { 
    6564 
    6665                if ($_POST["destination_radio"] == "new"){ 
    6766                        // Create the new album 
    68                         $result = add_album($_POST["new_album_name"], NULL, $_POST["collections_menu"]); 
    69                         $album_id = $result["id"]; 
    70                 } 
    71                 else 
    72                 { 
     67                        $result = add_album($_POST['new_album_name'], NULL, $_POST['collections_menu']); 
     68                        if (!$result['errors']) { 
     69                                // no errors, add images to new album 
     70                                $album_id = $result['id']; 
     71                        } else { 
     72                                // errors exist, let's find out what they are 
     73                                if (isset($result['output']) && $result['output'] == "existing" && isset($result['id'])) { 
     74                                        // album already exists so try insert images into the existing album 
     75                                        // and alert the user that their "new" album is already existing 
     76                                        $album_id = $result['id']; 
     77                                        // get the collection name for display 
     78                                        $sql = "SELECT `name` FROM ".TABLE_PREFIX."collections WHERE id = ".intval($_REQUEST['collections_menu']); 
     79                                        $result = run_query($sql); 
     80                                        $row = mysql_fetch_assoc($result); 
     81                                        $output .= "\n\t" . '<p class="actions">'.sprintf(plog_tr('Album already exists. Uploading file to existing album `%s` in collection `%s`'), $_REQUEST['new_album_name'], $row['name']).'</p>' . "\n"; 
     82                                } else { 
     83                                        // error has nothing to do with an existing album, show the returned error 
     84                                        $album_id = ''; 
     85                                        $output .= "\n\t" . '<p class="errors">' . $result['errors'] . '</p>' . "\n"; 
     86                                } 
     87                        } 
     88                } else { 
     89                        // use an existing album 
    7390                        $album_id = $_POST["destination"]; 
    7491                } 
    7592 
     
    92109                                                        $imported++; 
    93110                                                        // delete thumbnail file if it exists 
    94111                                                        $thumbpath = $config['basedir'] . 'plog-content/thumbs/import-'.substr($file_key,0,2).'-'.basename($file_name); 
    95                                                         if (is_file($thumbpath) && is_readable($thumbpath)) 
    96                                                         { 
     112                                                        if (is_file($thumbpath) && is_readable($thumbpath)) { 
    97113                                                                unlink($thumbpath); 
    98                                                         }; 
    99                                                 }; 
     114                                                        } 
     115                                                } 
    100116                                        } 
    101117 
    102118                                        $counter++; 
    103                                 }; 
     119                                } 
    104120 
    105121                        } 
    106122 
    107123                        // get album name for display 
    108124                        $sql = "SELECT name FROM ".TABLE_PREFIX."albums WHERE id = $album_id"; 
    109125                        $result = run_query($sql); 
    110  
    111126                        $row = mysql_fetch_assoc($result); 
    112127 
    113128                        $output .= "\n\t" . '<p class="actions">'.sprintf(plog_tr('%d picture(s) were successfully imported to album <strong>%s</strong>'),$imported,$row['name']). '</p>' . "\n"; 
    114129 
    115                         if ($imported == 0) 
    116                         $output .= "\n\t" . '<p class="errors">' . plog_tr('Make sure to CHMOD 777 your newly created folders within the \'plog-content/uploads/\' directory or else Plogger cannot access them. Plogger cannot CHMOD the directory for you while PHP is in safe mode.') . '</p>' . "\n"; 
     130                        if ($imported == 0) { 
     131                                $output .= "\n\t" . '<p class="errors">' . plog_tr('Make sure to CHMOD 777 your newly created folders within the \'plog-content/uploads/\' directory or else Plogger cannot access them. Plogger cannot CHMOD the directory for you while PHP is in safe mode.') . '</p>' . "\n"; 
     132                        } 
     133                        /* what is this for? 
     134                        else { 
     135                                $output .= "\n\t" . '<p class="errors">'.$result['output'].'</p>' . "\n"; 
     136                        }*/ 
    117137                } 
    118                 else 
    119                 $output .= "\n\t" . '<p class="errors">'.$result['output'].'</p>' . "\n"; 
    120  
    121138        } 
    122139 
    123140        // read the list again, so any newly created directories show up 
  • plog-admin/plog-upload.php

     
    106106                        $output .= "\n\t" . '<p class="errors">' . plog_tr('New album name not specified!') . '</p>' . "\n"; 
    107107                } else { 
    108108                        if ($_REQUEST["destination_radio"] == "new"){ 
    109                                 // Create the new album 
    110                                 $result = add_album(mysql_real_escape_string($_REQUEST["new_album_name"]), NULL, $_REQUEST["collections_menu"]); 
    111                                 $album_id = $result["id"]; 
     109                                // create a new album 
     110                                $result = add_album(mysql_real_escape_string($_REQUEST['new_album_name']), NULL, $_REQUEST['collections_menu']); 
     111                                if (!$result['errors']) { 
     112                                        // no errors, add uploaded image to new album 
     113                                        $album_id = $result['id']; 
     114                                } else { 
     115                                        // errors exist, let's find out what they are 
     116                                        if (isset($result['output']) && $result['output'] == "existing" && isset($result['id'])) { 
     117                                                // album already exists so try insert images into the existing album 
     118                                                // and alert the user that their "new" album is already existing 
     119                                                $album_id = $result['id']; 
     120                                                // get the collection name for display 
     121                                                $sql = "SELECT `name` FROM ".TABLE_PREFIX."collections WHERE id = ".intval($_REQUEST['collections_menu']); 
     122                                                $result = run_query($sql); 
     123                                                $row = mysql_fetch_assoc($result); 
     124                                                $output .= "\n\t" . '<p class="actions">'.sprintf(plog_tr('Album already exists. Uploading file to existing album `%s` in collection `%s`'), $_REQUEST['new_album_name'], $row['name']).'</p>' . "\n"; 
     125                                        } else { 
     126                                                // error has nothing to do with an existing album, show the returned error 
     127                                                $album_id = ''; 
     128                                                $output .= "\n\t" . '<p class="errors">' . $result['errors'] . '</p>' . "\n"; 
     129                                        } 
     130                                } 
    112131                        } else { 
     132                                // use an existing album 
    113133                                $album_id = $_REQUEST["albums_menu"]; 
    114134                        } 
    115135 
    116                         $result = add_picture($album_id,$_FILES["userfile"]["tmp_name"],$_FILES["userfile"]["name"],$_REQUEST["caption"], $_REQUEST["description"]); 
    117                         $output .= "\n\t" . '<p class="actions">'.$result["output"].'</p>' . "\n"; 
     136                        if ($album_id){ 
     137                                $result = add_picture($album_id,$_FILES["userfile"]["tmp_name"],$_FILES["userfile"]["name"],$_REQUEST["caption"], $_REQUEST["description"]); 
     138                                if (!$result['errors']) { 
     139                                        // added uploaded image successfully 
     140                                        $output .= "\n\t" . '<p class="actions">'.$result['output'].'</p>' . "\n"; 
     141                                } else { 
     142                                        // errors adding the image, show the returned error 
     143                                        $output .= "\n\t" . '<p class="errors">' . $result['errors'] . '</p>' . "\n"; 
     144                                } 
     145                        } 
    118146 
    119147                } 
    120148        } 
  • plog-includes/plog-functions.php

     
    728728                return $album; 
    729729        } 
    730730 
    731         function get_album_by_name($name) { 
     731        function get_album_by_name($name, $parent_id = 0) { 
    732732                $sql = "SELECT * 
    733                 FROM `".TABLE_PREFIX."albumss` 
    734                 WHERE `name` = '".mysql_real_escape_string($name)."'"; 
     733                FROM `".TABLE_PREFIX."albums` 
     734                WHERE `parent_id` = '".$parent_id."' AND `name` = '".mysql_real_escape_string($name)."'"; 
    735735                $result = run_query($sql); 
    736736 
    737737                if (mysql_num_rows($result) > 0){ 
     
    781781                return $collection; 
    782782        } 
    783783 
    784         function get_collection_by_name($name) { 
     784        function get_collection_by_name($name, $parent_id = 0) { 
    785785                $sql = "SELECT * 
    786786                FROM `".TABLE_PREFIX."collections` 
    787787                WHERE name = '".mysql_real_escape_string($name)."'";