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
41 41 42 42 $result = array( 43 43 'output' => '', 44 'errors' => '', 44 45 'picture_id' => false, 45 46 ); 46 47 … … 296 297 $collection_name = trim(SmartStripSlashes($collection_name)); 297 298 if (empty($collection_name)) { 298 299 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 start302 // 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));306 300 } 307 301 308 302 $collection_folder = strtolower(sanitize_filename($collection_name)); … … 311 305 // anyway 312 306 $create_path = $config["basedir"] . "plog-content/images/".$collection_folder; 313 307 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 314 325 // create directory 315 326 if (!makeDirs($create_path, 0777)) { 316 327 $errors .= sprintf(plog_tr("Could not create directory %s!"),$create_path); … … 323 334 $id = mysql_insert_id(); 324 335 325 336 $output .= sprintf(plog_tr('You have successfully created the collection <strong>%s</strong>'),$collection_name); 326 } ;337 } 327 338 328 339 // caller can check the value of id, if it is zero, then collection creation failed 329 340 // errors and output are separate, because this way the caller can format the return value … … 345 356 $name = trim(SmartStripSlashes($name)); 346 357 if (empty($name)) { 347 358 return array("errors" => plog_tr("Please enter a valid name for the collection")); 348 } ;359 } 349 360 350 361 $target_name = strtolower(sanitize_filename($name)); 351 362 … … 366 377 $result = run_query($sql); 367 378 $row = mysql_fetch_assoc($result); 368 379 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 369 384 // do not allow collections with duplicate names, otherwise mod_rewritten links will start 370 385 // 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 } 374 399 } 375 400 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 380 401 // perform the rename on the directory 381 402 if (!rename($source_path, $target_path)) { 382 403 return array("errors" => sprintf(plog_tr("Error renaming directory! (%s to %s)"),$source_path,$target_path)); 383 } ;404 } 384 405 385 406 $target_name = mysql_real_escape_string($target_name); 386 407 … … 388 409 $result = mysql_query($query); 389 410 if (!$result) { 390 411 return array("errors" => mysql_error()); 391 } ;412 } 392 413 393 414 $output = plog_tr('You have successfully modified the selected collection.'); 394 415 … … 493 514 $album_name = trim(SmartStripSlashes($album_name)); 494 515 if (empty($album_name)) { 495 516 return array("errors" => plog_tr("Please enter a valid name for the album")); 496 } ;517 } 497 518 // get the parent collection name 498 519 $query = "SELECT c.path as collection_path FROM ". TABLE_PREFIX."collections c WHERE id = '$pid'"; 499 520 … … 503 524 // this shouldn't happen 504 525 if (empty($row)) { 505 526 return array("errors" => plog_tr("No such collection")); 506 } ;527 } 507 528 508 529 $album_folder = strtolower(sanitize_filename($album_name)); 509 530 … … 511 532 // will be unusable anyway 512 533 $create_path = $config["basedir"] . "plog-content/images/".SmartStripSlashes($row["collection_path"])."/".$album_folder; 513 534 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 514 551 if (!makeDirs($create_path, 0777)) { 515 552 $errors .= sprintf(plog_tr("Could not create directory %s!"),$path); 516 553 } else { … … 522 559 $id = mysql_insert_id(); 523 560 524 561 $output .= sprintf(plog_tr('You have successfully created the album <strong>%s</strong>'),$album_name); 525 } ;562 } 526 563 // caller can check the value of id, if it is zero, then album creation failed 527 564 // errors and output are separate, because this way the caller can format the return value 528 565 // as it needs … … 561 598 $source_path = $config['basedir'] . "plog-content/images/".$source_collection_name."/".$source_album_name; 562 599 $target_path = $config['basedir'] . "plog-content/images/".$source_collection_name."/".$target_name; 563 600 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 564 617 // perform the rename on the directory 565 618 if (!rename($source_path, $target_path)) 566 619 { 567 620 return array( 568 621 "errors" => sprintf(plog_tr("Error renaming directory! (%s to %s)"),$source_path,$target_path)); 569 } ;622 } 570 623 571 624 $target_name = mysql_real_escape_string($target_name); 572 625 … … 581 634 $result = mysql_query($query); 582 635 if (!$result) { 583 636 return array("errors" => mysql_error()); 584 } ;637 } 585 638 586 639 $output .= plog_tr('You have successfully modified the selected album.'); 587 640 … … 632 685 }; 633 686 } 634 687 635 function move_album($album_id, $to_collection) {688 function move_album($album_id, $to_collection) { 636 689 global $config; 637 690 638 691 $res = array( … … 665 718 $source_path = $config['basedir']."plog-content/images/".$source_collection_name."/".$source_album_name; 666 719 $target_path = $config['basedir']."plog-content/images/".$target_collection_name."/".$source_album_name; 667 720 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 668 737 // 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 } 670 741 671 742 //if (!rename($source_path, $target_path)) 672 743 // $output .= '<p class="errors">Could not rename directory!</p>'; … … 685 756 $new_path = $target_path."/".$filename; 686 757 687 758 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); 689 760 690 761 $path_insert = mysql_real_escape_string($target_collection_name."/".$source_album_name."/".$filename); 691 762 … … 700 771 $query = "UPDATE ".TABLE_PREFIX."albums SET `parent_id` = '$to_collection' WHERE `id`='$album_id'"; 701 772 $result = run_query($query); 702 773 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 703 782 return $res; 704 783 } 705 784 … … 1069 1148 function plog_add_album_form($parent_collection) { 1070 1149 $parent_collection = intval($parent_collection); 1071 1150 $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"> 1073 1152 <div id="create-album" class="cssbox-green" style="width: 385px !important; display: none;"> 1074 1153 <div class="cssbox_head-green"><h2>' . plog_tr('Create an Album') . '</h2></div> 1075 1154 <div class="cssbox_body-green"> … … 1090 1169 global $config, $thumbnail_config; 1091 1170 $output = ''; 1092 1171 $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">'; 1094 1173 $collection = get_collection_by_id($collection_id); 1095 1174 1096 1175 $auto_graphic = $config['gallery_url']."plog-admin/images/auto.gif"; -
plog-admin/plog-import.php
46 46 // Check if update has been clicked, handle erroneous conditions, or upload 47 47 //print_r($_POST); 48 48 49 if (isset($_POST['upload'])) {49 if (isset($_POST['upload'])) { 50 50 51 51 $destinations = isset($_POST['destinations']) ? $_POST['destinations'] : ''; 52 52 $captions = $_POST['captions']; … … 58 58 59 59 $files = get_files($config['basedir'] . 'plog-content/uploads'); 60 60 61 if ($_POST['destination_radio'] == "new" && $_POST["new_album_name"] == "") {61 if ($_POST['destination_radio'] == "new" && $_POST["new_album_name"] == "") { 62 62 $output .= "\n\t" . '<p class="errors">' . plog_tr('New album name not specified!') . '</p>' . "\n"; 63 } 64 else { 63 } else { 65 64 66 65 if ($_POST["destination_radio"] == "new"){ 67 66 // 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 73 90 $album_id = $_POST["destination"]; 74 91 } 75 92 … … 92 109 $imported++; 93 110 // delete thumbnail file if it exists 94 111 $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)) { 97 113 unlink($thumbpath); 98 } ;99 } ;114 } 115 } 100 116 } 101 117 102 118 $counter++; 103 } ;119 } 104 120 105 121 } 106 122 107 123 // get album name for display 108 124 $sql = "SELECT name FROM ".TABLE_PREFIX."albums WHERE id = $album_id"; 109 125 $result = run_query($sql); 110 111 126 $row = mysql_fetch_assoc($result); 112 127 113 128 $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"; 114 129 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 }*/ 117 137 } 118 else119 $output .= "\n\t" . '<p class="errors">'.$result['output'].'</p>' . "\n";120 121 138 } 122 139 123 140 // read the list again, so any newly created directories show up -
plog-admin/plog-upload.php
106 106 $output .= "\n\t" . '<p class="errors">' . plog_tr('New album name not specified!') . '</p>' . "\n"; 107 107 } else { 108 108 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 } 112 131 } else { 132 // use an existing album 113 133 $album_id = $_REQUEST["albums_menu"]; 114 134 } 115 135 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 } 118 146 119 147 } 120 148 } -
plog-includes/plog-functions.php
728 728 return $album; 729 729 } 730 730 731 function get_album_by_name($name ) {731 function get_album_by_name($name, $parent_id = 0) { 732 732 $sql = "SELECT * 733 FROM `".TABLE_PREFIX."albums s`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)."'"; 735 735 $result = run_query($sql); 736 736 737 737 if (mysql_num_rows($result) > 0){ … … 781 781 return $collection; 782 782 } 783 783 784 function get_collection_by_name($name ) {784 function get_collection_by_name($name, $parent_id = 0) { 785 785 $sql = "SELECT * 786 786 FROM `".TABLE_PREFIX."collections` 787 787 WHERE name = '".mysql_real_escape_string($name)."'";
