Show
Ignore:
Timestamp:
07/15/08 13:36:59 (4 months ago)
Author:
sidtheduck
Message:

+ Fix for ticket #169 - no duplicate album names within the same collection
+ Extended the duplicate collection name verification
+ Fixed issues with move_picture to behave more like add_picture (adds a (#) at the end of the filename if duplicate filenames)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/plog-admin/plog-admin-functions.php

    r557 r558  
    4242        $result = array( 
    4343                'output' => '', 
     44                'errors' => '', 
    4445                'picture_id' => false, 
    4546        ); 
     
    198199        } else { 
    199200                return array('errors' => plog_tr('Could not modify selected picture')); 
    200         }; 
     201        } 
    201202 
    202203} 
     
    208209        $pic_id = intval($pic_id); 
    209210 
    210         $query = "SELECT * FROM ".TABLE_PREFIX."albums WHERE `id` = '$to_album'"; 
     211        $query = "SELECT * FROM ".TABLE_PREFIX."albums WHERE `id` = '".$to_album."'"; 
    211212        $result = run_query($query); 
    212213        $row = mysql_fetch_assoc($result); 
     
    214215        if (!is_array($row)) { 
    215216                return array('errors' => sprintf(plog_tr('There is no album with id %d'),$to_album)); 
    216         }; 
     217        } 
    217218 
    218219        $new_collection = $row['parent_id']; 
    219          
    220220 
    221221        // move picture to new location 
     
    226226 
    227227        $filename = SmartStripSlashes(basename($picture['path'])); 
    228         $directory = SmartStripSlashes($album['collection_path'])."/".SmartStripSlashes($album['album_path'])."/"; 
    229         $new_path = $directory.$filename; 
    230  
    231         if (!rename($config['basedir']."plog-content/images/".SmartStripSlashes($picture['path']), $config['basedir']."plog-content/images/".$new_path)) { 
    232                 return array('errors' => sprintf(plog_tr("Could not move file! %s to %s"),$picture["path"],$new_path)); 
    233         }; 
    234  
    235         $new_path = mysql_real_escape_string($new_path); 
     228        $target_path = SmartStripSlashes($album['collection_path'])."/".SmartStripSlashes($album['album_path']); 
     229 
     230        $filename_parts = explode(".",strrev($filename),2); 
     231        $filename_base = strrev($filename_parts[1]); 
     232        $filename_ext = strrev($filename_parts[0]); 
     233        $unique_filename_base = strtolower(SmartStripSlashes($filename_base)); 
     234 
     235        $i = 0; 
     236        while ($to_album != $picture['parent_album'] && is_file($config['basedir'].'plog-content/images/'.$target_path."/".$unique_filename_base.".".$filename_ext)){ 
     237                $unique_filename_base = $filename_base." (" . ++$i . ")"; 
     238        } 
     239 
     240        // final fully qualified file name 
     241        $picture_path = $target_path.'/'.$unique_filename_base.".".$filename_ext; 
     242        $final_fqfn = $config['basedir'].'plog-content/images/'.$picture_path; 
     243 
     244        if (!rename($config['basedir']."plog-content/images/".SmartStripSlashes($picture['path']), $final_fqfn)) { 
     245                return array('errors' => sprintf(plog_tr("Could not move file! %s to %s"),$picture['path'],$final_fqfn)); 
     246        } 
    236247 
    237248        // update database 
    238249        $sql = "UPDATE ".TABLE_PREFIX."pictures SET 
    239                         path = '$new_path', 
    240                         parent_album = '$to_album', 
    241                         parent_collection = '$new_collection' 
    242                 WHERE id = '$pic_id'"; 
     250                        path = '".mysql_real_escape_string($picture_path)."', 
     251                        parent_album = '".$to_album."', 
     252                        parent_collection = '".$new_collection."' 
     253                WHERE id = '".$pic_id."'"; 
    243254        if (!mysql_query($sql)) { 
    244255                return array('errors' => mysql_error()); 
    245         }; 
     256        } 
    246257        return array('output' => plog_tr('Success')); 
    247258} 
     
    297308        if (empty($collection_name)) { 
    298309                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)); 
    306310        } 
    307311 
     
    311315        // anyway 
    312316        $create_path = $config["basedir"] . "plog-content/images/".$collection_folder; 
     317 
     318        // do not allow collections with duplicate names, otherwise mod_rewritten links will start 
     319        // to behave weird. 
     320        if (is_dir($create_path)) { 
     321                // if there is already a directory, check to see if it's in the database 
     322                $collection_data = get_collection_by_name($collection_name); 
     323                if ($collection_data){ 
     324                        // it's in the database, so throw duplicate collection error 
     325                        return array("errors" => sprintf(plog_tr('New collection could not be created, because there already is one named `%s`!'),$collection_name)); 
     326                } else{ 
     327                        // it's not in the database so attempt to delete the directory 
     328                        if (!@rmdir($target_path)){ 
     329                                // could not delete the directory, so prompt the user to delete it manually 
     330                                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)); 
     331                        } 
     332                } 
     333        } 
    313334 
    314335        // create directory 
     
    324345 
    325346                $output .= sprintf(plog_tr('You have successfully created the collection <strong>%s</strong>'),$collection_name);     
    326         }; 
     347        } 
    327348 
    328349        // caller can check the value of id, if it is zero, then collection creation failed 
     
    346367        if (empty($name)) { 
    347368                return array("errors" => plog_tr("Please enter a valid name for the collection")); 
    348         }; 
     369        } 
    349370 
    350371        $target_name = strtolower(sanitize_filename($name)); 
     
    367388        $row = mysql_fetch_assoc($result); 
    368389 
    369         // do not allow collections with duplicate names, otherwise mod_rewritten links will start 
    370         // 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)); 
    374         } 
    375  
    376390        $source_collection_name = SmartStripSlashes($row["collection_path"]); 
    377391        $source_path = $config["basedir"] . "plog-content/images/".$source_collection_name; 
    378392        $target_path = $config["basedir"] . "plog-content/images/".$target_name; 
    379393 
     394        // do not allow collections with duplicate names, otherwise mod_rewritten links will start 
     395        // to behave weird. 
     396        if ($row["name"] != $name && is_dir($target_path)) { 
     397                // if there is already a directory, check to see if it's in the database 
     398                $collection_data = get_collection_by_name($name); 
     399                if ($collection_data){ 
     400                        // it's in the database, so throw duplicate collection error 
     401                        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)); 
     402                } else{ 
     403                        // it's not in the database so attempt to delete the directory 
     404                        if (!@rmdir($target_path)){ 
     405                                // could not delete the directory, so prompt the user to delete it manually 
     406                                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)); 
     407                        } 
     408                } 
     409        } 
     410 
    380411        // perform the rename on the directory 
    381412        if (!rename($source_path, $target_path)) { 
    382413                return array("errors" => sprintf(plog_tr("Error renaming directory! (%s to %s)"),$source_path,$target_path)); 
    383         }; 
     414        } 
    384415 
    385416        $target_name = mysql_real_escape_string($target_name); 
     
    389420        if (!$result) { 
    390421                return array("errors" => mysql_error()); 
    391         }; 
     422        } 
    392423 
    393424        $output = plog_tr('You have successfully modified the selected collection.'); 
     
    494525        if (empty($album_name)) { 
    495526                return array("errors" => plog_tr("Please enter a valid name for the album")); 
    496         }; 
     527        } 
    497528        // get the parent collection name 
    498529        $query = "SELECT c.path as collection_path FROM ". TABLE_PREFIX."collections c WHERE id = '$pid'"; 
     
    504535        if (empty($row)) { 
    505536                return array("errors" => plog_tr("No such collection")); 
    506         }; 
     537        } 
    507538 
    508539        $album_folder = strtolower(sanitize_filename($album_name)); 
     
    511542        // will be unusable anyway 
    512543        $create_path = $config["basedir"] . "plog-content/images/".SmartStripSlashes($row["collection_path"])."/".$album_folder; 
     544 
     545        // check path so we are not creating duplicate albums within the same collection 
     546        if (is_dir($create_path)){ 
     547                // if there is already a directory, check to see if it's in the database 
     548                $album_data = get_album_by_name($album_name, $pid); 
     549                if ($album_data) { 
     550                        // it's in the database, so throw duplicate album error 
     551                        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']))); 
     552                } else { 
     553                        // it's not in the database so attempt to delete the directory 
     554                        if (!@rmdir($create_path)){ 
     555                                // could not delete the directory, so prompt the user to delete it manually 
     556                                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)); 
     557                        } 
     558                } 
     559        } 
    513560 
    514561        if (!makeDirs($create_path, 0777)) { 
     
    523570 
    524571                $output .= sprintf(plog_tr('You have successfully created the album <strong>%s</strong>'),$album_name); 
    525         }; 
     572        } 
    526573        // caller can check the value of id, if it is zero, then album creation failed 
    527574        // errors and output are separate, because this way the caller can format the return value 
     
    561608        $target_path = $config['basedir'] . "plog-content/images/".$source_collection_name."/".$target_name; 
    562609 
     610        // check path so we are not creating duplicate albums within the same collection 
     611        if (is_dir($target_path)){ 
     612                // if there is already a directory, check to see if it's in the database 
     613                $album_data = get_album_by_name($album_name, $to_collection); 
     614                if ($album_data) { 
     615                        // it's in the database, so throw duplicate album error 
     616                        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)); 
     617                } else { 
     618                        // it's not in the database so attempt to delete the directory 
     619                        if (!@rmdir($target_path)){ 
     620                                // could not delete the directory, so prompt the user to delete it manually 
     621                                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)); 
     622                        } 
     623                } 
     624        } 
     625 
    563626        // perform the rename on the directory 
    564627        if (!rename($source_path, $target_path)) 
     
    566629                return array( 
    567630                        "errors" => sprintf(plog_tr("Error renaming directory! (%s to %s)"),$source_path,$target_path)); 
    568         }; 
     631        } 
    569632 
    570633        $target_name = mysql_real_escape_string($target_name); 
     
    581644        if (!$result) { 
    582645                return array("errors" => mysql_error()); 
    583         }; 
     646        } 
    584647 
    585648        $output .= plog_tr('You have successfully modified the selected album.'); 
     
    632695} 
    633696 
    634 function move_album($album_id,$to_collection) { 
     697function move_album($album_id, $to_collection) { 
    635698        global $config; 
    636699 
     
    665728        $target_path = $config['basedir']."plog-content/images/".$target_collection_name."/".$source_album_name; 
    666729 
     730        // check path so we are not creating duplicate albums within the same collection 
     731        if (is_dir($target_path)){ 
     732                // if there is already a directory, check to see if it's in the database 
     733                $album_data = get_album_by_name($source_album_name, $to_collection); 
     734                if ($album_data) { 
     735                        // it's in the database, so throw duplicate album error 
     736                        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)); 
     737                } else { 
     738                        // it's not in the database so attempt to delete the directory 
     739                        if (!@rmdir($target_path)){ 
     740                                // could not delete the directory, so prompt the user to delete it manually 
     741                                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)); 
     742                        } 
     743                } 
     744        } 
     745 
    667746        // attempt to make new album directory in target collection 
    668         @mkdir($target_path, 0775); 
     747        if (!makeDirs($target_path, 0777)) { 
     748                return array("errors" => sprintf(plog_tr('Could not create directory `%s`!'),$target_path)); 
     749        } 
    669750 
    670751        //if (!rename($source_path, $target_path)) 
     
    685766 
    686767                if (!rename($old_path, $new_path)) 
    687                         $res['errors'] .=  sprintf(plog_tr("Could not move file! %s to %s"),$old_path,$new_path); 
     768                        $res['errors'] .=  sprintf(plog_tr("Could not move file! `%s` to `%s`"),$old_path,$new_path); 
    688769 
    689770                $path_insert = mysql_real_escape_string($target_collection_name."/".$source_album_name."/".$filename); 
     
    699780        $query = "UPDATE ".TABLE_PREFIX."albums SET `parent_id` = '$to_collection' WHERE `id`='$album_id'"; 
    700781        $result = run_query($query); 
     782 
     783        // attempt to delete the old folder if there were no errors moving the files 
     784        if ($res['errors'] == ''){ 
     785                @chmod($source_path,0777); 
     786                if (!rmdir($source_path)) { 
     787                        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)); 
     788                } 
     789        } 
    701790 
    702791        return $res; 
     
    10681157        $parent_collection = intval($parent_collection); 
    10691158        $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') . '" />'; 
    1070         $output .= "\n\t\t" . '<form action="'.$_SERVER["PHP_SELF"].'" method="post"> 
     1159        $output .= "\n\t\t" . '<form action="'.$_SERVER["REQUEST_URI"].'" method="post"> 
    10711160                        <div id="create-album" class="cssbox-green" style="width: 385px !important; display: none;"> 
    10721161                                <div class="cssbox_head-green"><h2>' . plog_tr('Create an Album') . '</h2></div> 
     
    10891178        $output = ''; 
    10901179        $collection_id = intval($collection_id); 
    1091         $output .= "\n\t\t" . '<form class="edit" action="'.$_SERVER["PHP_SELF"].'" method="post">'; 
     1180        $output .= "\n\t\t" . '<form class="edit" action="'.$_SERVER["REQUEST_URI"].'" method="post">'; 
    10921181        $collection = get_collection_by_id($collection_id); 
    10931182