[Linpha-cvs] SF.net SVN: linpha: [4549] trunk/linpha2
Status: Inactive
Brought to you by:
bzrudi
From: <fan...@us...> - 2006-04-26 21:43:24
|
Revision: 4549 Author: fangehrn Date: 2006-04-26 14:43:06 -0700 (Wed, 26 Apr 2006) ViewCVS: http://svn.sourceforge.net/linpha/?rev=4549&view=rev Log Message: ----------- 2006-04-26 flo * filemanager: implementing rename, delete, create folder Modified Paths: -------------- trunk/linpha2/ChangeLog trunk/linpha2/admin/import.php trunk/linpha2/lib/classes/linpha.filesys.class.php trunk/linpha2/lib/classes/linpha.import.class.php trunk/linpha2/lib/classes/linpha.sql.class.php trunk/linpha2/lib/modules/module.filemanager.php Modified: trunk/linpha2/ChangeLog =================================================================== --- trunk/linpha2/ChangeLog 2006-04-25 22:05:37 UTC (rev 4548) +++ trunk/linpha2/ChangeLog 2006-04-26 21:43:06 UTC (rev 4549) @@ -1,3 +1,6 @@ +2006-04-26 flo + * filemanager: implementing rename, delete, create folder + 2006-04-25 flo * filemanager: implementing read/write Modified: trunk/linpha2/admin/import.php =================================================================== --- trunk/linpha2/admin/import.php 2006-04-25 22:05:37 UTC (rev 4548) +++ trunk/linpha2/admin/import.php 2006-04-26 21:43:06 UTC (rev 4549) @@ -241,9 +241,9 @@ <?php echo i18n("Thumbnails").':'; ?> <br /> -<input type="radio" id="index_thumbnails_deletefirstreindex" name="index_thumbnails" value="recreate" /><label for="index_thumbnails_reindex"> <?php echo i18n("Re-create"); ?></label><br /> <input type="radio" id="index_thumbnails_no" name="index_thumbnails" value="no" /><label for="index_thumbnails_no"> <?php echo i18n("No Create"); ?></label><br /> <input type="radio" id="index_thumbnails_index" name="index_thumbnails" value="create" checked="checked" /><label for="index_thumbnails_index"> <?php echo i18n("Create"); ?></label><br /> +<input type="radio" id="index_thumbnails_reindex" name="index_thumbnails" value="recreate" /><label for="index_thumbnails_reindex"> <?php echo i18n("Re-create"); ?></label><br /> <br /> <?php Modified: trunk/linpha2/lib/classes/linpha.filesys.class.php =================================================================== --- trunk/linpha2/lib/classes/linpha.filesys.class.php 2006-04-25 22:05:37 UTC (rev 4548) +++ trunk/linpha2/lib/classes/linpha.filesys.class.php 2006-04-26 21:43:06 UTC (rev 4549) @@ -27,7 +27,86 @@ class LinFilesys { + /** +* create directory recursively - same as "mkdir -p" +*/ +function mkdir_p($target,$mode='0700') +{ + if(is_dir($target) || empty($target)) { // best case check first + return 1; + } + if(file_exists($target) && !is_dir($target)) { // target exists bug isn't a directory.. + return 0; + } + if( mkdir_p( substr( $target,0,strrpos( $target,'/') ) ) ) + { + return mkdir($target,intval($mode,8)); // crawl back up & create dir tree + } + return 0; +} + +/** +* copy recursive - same as "cp -R" +*/ +function copy_r($source,$dest) +{ + if( !is_dir($source) ) + { + if( is_file($source) ) + { + @copy($source,$dest); + return 1; + } + else + { + return 0; + } + } + if (!is_dir($dest)) { + mkdir_p($dest); + } + $h=@dir($source); + while (@($entry=$h->read()) !== false) + { + if (($entry!=".")&&($entry!="..")) + { + if (is_dir("$source/$entry")&&$dest!=="$source/$entry") + { + copy_r("$source/$entry","$dest/$entry"); + } + else + { + @copy("$source/$entry","$dest/$entry"); + } + } + } + $h->close(); + return 1; +} + +/** + * delete directory recurisvely - same as "rm -rf" + */ +function rm_rf($path,$followLinks=false) +{ + $dir = opendir($path) ; + while (false !== ($entry = readdir($dir))) + { + if ( is_file( "$path/$entry" ) || ((!$followLinks) && is_link("$path/$entry")) ) + { + @unlink( "$path/$entry" ); + } + elseif ( is_dir( "$path/$entry" ) && $entry!='.' && $entry!='..' ) + { + rm_rf( "$path/$entry" ); + } + } + closedir($dir) ; + return @rmdir($path); +} + +/** * realpath() doesnt work somtimes if path is absolute * linRealpath takes care of this */ Modified: trunk/linpha2/lib/classes/linpha.import.class.php =================================================================== --- trunk/linpha2/lib/classes/linpha.import.class.php 2006-04-25 22:05:37 UTC (rev 4548) +++ trunk/linpha2/lib/classes/linpha.import.class.php 2006-04-26 21:43:06 UTC (rev 4549) @@ -205,7 +205,10 @@ $d = dir( $dirname ); while( false !== ($entry = $d->read() ) ) { - LinImport::addNewEntry( $parent_id , $dirname , $entry, $recursive ); + if($entry != '.' AND $entry != '..') + { + LinImport::addNewEntry( $parent_id , $dirname , $entry, $recursive ); + } } $d->close(); } Modified: trunk/linpha2/lib/classes/linpha.sql.class.php =================================================================== --- trunk/linpha2/lib/classes/linpha.sql.class.php 2006-04-25 22:05:37 UTC (rev 4548) +++ trunk/linpha2/lib/classes/linpha.sql.class.php 2006-04-26 21:43:06 UTC (rev 4549) @@ -596,8 +596,14 @@ */ function getFullImagePath( $photo_id ) { + $album_dir = LinSql::getPath('album'); + + if($photo_id == 0) + { + return $album_dir; // return album_dir without ending slash + } + $array_path = LinSql::getFullFilenameFromId( $photo_id ); - $album_dir = LinSql::getPath('album'); return $album_dir .'/'. implode('/',$array_path); } Modified: trunk/linpha2/lib/modules/module.filemanager.php =================================================================== --- trunk/linpha2/lib/modules/module.filemanager.php 2006-04-25 22:05:37 UTC (rev 4548) +++ trunk/linpha2/lib/modules/module.filemanager.php 2006-04-26 21:43:06 UTC (rev 4549) @@ -30,7 +30,7 @@ * But he cannot rename or delete the folder itself * * - * it doesnt make sense to give write permissions to the file itself + * it doesnt make sense to give write permissions to the files itself */ if(!defined('LINPHA_DIR')) { exit(1); } @@ -93,68 +93,198 @@ */ if(isset($_POST['cmd'])) { - switch($_POST['cmd']) + try { - case 'file_action': - ?> - <form method="POST" action="<?php echo LINPHA_DIR.'/?cat=filemanager&id='.$parent_id; ?>"> - <?php - switch($_POST['file_action']) + switch($_POST['cmd']) { - case 'rename': + case 'file_action': + if(!isset($_POST['checkedfiles'])) + { + throw new Exception(i18n("No Files Selected")); + } + if(!isset($_POST['file_action'])) + { + throw new Exception(i18n("No Action Selected")); + } + ?> - <input type="text" name="do_rename" value="" /> + <form method="POST" action="<?php echo LINPHA_DIR.'/?cat=filemanager&id='.$parent_id; ?>"> <?php - break; - - case 'move': - case 'copy': + switch($_POST['file_action']) + { + case 'rename': + $data = $GLOBALS['linpha']->db->GetRow("SELECT name, parent_id FROM ".PREFIX."photos " . + "WHERE id = '".LinSql::linAddslashes($_POST['checkedfiles'][0])."'"); + + echo '<b>'.i18n("Rename File/Folder").':</b><br />'; + ?> + <input type="text" name="new_name" value="<?php echo $data['name']; ?>" style="width: 300px;" /> + <input type="hidden" name="file_id" value="<?php echo $_POST['checkedfiles'][0]; ?>" /> + <?php + break; + + case 'move': + case 'copy': + ?> + <select> + <option value=""></option> + <option value=""></option> + <option value=""></option> + </selet> + <?php + break; + case 'delete': + echo i18n("Delete All The Following Files/Folders including contensts:").'<br />'; + foreach($_POST['checkedfiles'] AS $value) + { + $data = $GLOBALS['linpha']->db->GetRow("SELECT name FROM ".PREFIX."photos " . + "WHERE id = '".LinSql::linAddslashes($value)."'"); + echo $data['name'].', '; + } + echo '<br /><br />'; + echo i18n("Really Sure?"); + ?> + <input type="hidden" name="checkedfiles" value="<?php echo implode(';',$_POST['checkedfiles']); ?>" /> + <?php + break; + } ?> - <select> - <option value=""></option> - <option value=""></option> - <option value=""></option> - </selet> + <input type="hidden" name="cmd" value="do_file_action" /> + <input type="hidden" name="file_action" value="<?php echo $_POST['file_action']; ?>" /> + <input type="submit" name="submit" value="<?php echo i18n("Submit"); ?>" /> + </form> + <hr /><br /> <?php + break; + case 'do_file_action': + switch($_POST['file_action']) + { + case 'rename': + $new_name = removeUnWantedChars( $_POST['new_name'] ); + + $full_filename = LinSql::getFullImagePath( $_POST['file_id'] ); + $parent_dirname = dirname($full_filename); + + /** + * check write permission of parent folder + */ + $data = $GLOBALS['linpha']->db->GetRow("SELECT parent_id FROM ".PREFIX."photos " . + "WHERE id = '".LinSql::linAddslashes($_POST['file_id'])."'"); + if( ! $GLOBALS['linpha']->sql->photoIsAllowed( $data['parent_id'], 'write') ) + { + throw new Exception(i18n("You don't have permissions to this folder!")); + } + + if( nameAlreadyExists( $new_name, $data['parent_id'] )) + { + throw new Exception(i18n("A File/Folder with the same name already exists!")); + } + + if( @rename( $full_filename , $parent_dirname."/".$new_name ) ) + { + /** + * rename on filesys was successful + * -> rename also in db + */ + $GLOBALS['linpha']->db->Execute("UPDATE ".PREFIX."photos SET name = '".LinSql::linAddslashes($new_name)."' " . + "WHERE id = '".LinSql::linAddslashes($_POST['file_id'])."'"); + + linSysLog(i18n("Successfully Renamed!")."<br />"); + } + else + { + linSysLog(i18n(sprintf(i18n("Error While Renaming File/Folder \"%s\" to \"%s\"!"),$full_filename,$parent_dirname.'/'.$new_name)).'<br />'); + linSysLog(i18n("Check the permissions in the linpha albums folder. (This means NOT the write permissions page in the admin section)").'<br />'); + } break; - case 'delete': - echo i18n("Really Sure?"); + + case 'move': break; - } - ?> - <input type="hidden" name="cmd" value="do_file_action" /> - <input type="hidden" name="file_action" value="<?php echo $_POST['file_action']; ?>" /> - <input type="submit" name="submit" value="<?php echo i18n("Submit"); ?>" /> - </form> - <?php - break; - case 'do_file_action': - switch($_POST['file_action']) - { - case 'rename': - break; + + case 'copy': + break; + + case 'delete': + $checkedfiles = explode(';',$_POST['checkedfiles']); + foreach($checkedfiles AS $value) + { + $data = $GLOBALS['linpha']->db->GetRow("SELECT parent_id FROM ".PREFIX."photos " . + "WHERE id = '".LinSql::linAddslashes($value)."'"); - case 'move': - break; + /** + * check write permission of parent folder + */ + if( ! $GLOBALS['linpha']->sql->photoIsAllowed( $data['parent_id'], 'write') ) + { + throw new Exception(i18n("You don't have permissions to this folder!")); + } + + $full_filename = LinSql::getFullImagePath( $value ); + + if(LinFilesys::rm_rf($full_filename)) + { + LinImport::deleteEntry( $value, $full_filename); + + linSysLog(i18n("File/Folder deleted successfully!").'<br />'); + } + else + { + linSysLog(i18n("Error While Deleting Folder")." \"".$full_filename."\"".'<br />'); + linSysLog(i18n("Check the permissions in the linpha albums folder. (This means NOT the write permissions page in the admin section)").'<br />'); + } + } + break; - case 'copy': - break; + case 'create_folder': + /** + * check write permission of parent folder + */ + if( ! $GLOBALS['linpha']->sql->photoIsAllowed( $_POST['file_id'], 'write') ) + { + throw new Exception(i18n("You don't have permissions to this folder!")); + } + + $new_name = removeUnWantedChars( $_POST['folder_name'] ); + + if( nameAlreadyExists( $new_name, $_POST['file_id'] )) + { + throw new Exception(i18n("A File/Folder with the same name already exists!")); + } + + $full_filename = LinSql::getFullImagePath( $_POST['file_id'] ); - case 'delete': + if( @mkdir( $full_filename .'/'. $new_name )) + { + linSysLog(i18n("Folder Created Successfully!").'<br />'); + linSysLog(i18n("You may now want to start the Import to make your changes appear").'<br />'); + } + else + { + linSysLog(i18n("Error While Creating Folder")." \"".$new_name."\"".'<br />'); + linSysLog(i18n("Check the permissions in the linpha albums folder. (This means NOT the write permissions page in the admin section)").'<br />'); + } + break; + } + break; + case 'upload': + + break; } - - break; - case 'create_folder': - - break; - case 'do_create_folder': - - break; - case 'upload': - - break; } + catch(Exception $error) + { + echo "Error: ".$error -> getMessage(); + } + + /** + * print Syslog + */ + if( isset($GLOBALS['linpha']->template->output['sys_log']) ) + { + echo $GLOBALS['linpha']->template->output['sys_log']; + echo '<hr />'; + } } ?> @@ -188,15 +318,16 @@ $query = $GLOBALS['linpha']->db->Execute("SELECT id, name, img_type FROM ".PREFIX."photos WHERE parent_id = '".LinSql::linAddslashes($parent_id)."' ORDER by name"); while($data = $query->FetchRow()) { + $full_filename = LinSql::getFullImagePath( $data['id'] ); + /** - * check read permission - */ - if( $GLOBALS['linpha']->sql->photoIsAllowed( $data['id'], 'read' ) ) + * show only if file exists and we have READ permissions + */ + if( file_exists( $full_filename ) && $GLOBALS['linpha']->sql->photoIsAllowed( $data['id'], 'read' ) ) { /** * set file informations */ - $full_filename = LinSql::getFullImagePath( $data['id'] ); $filestat = stat($full_filename); /** @@ -255,7 +386,7 @@ <tr> <td> <?php if($isWriteAble) { ?> - <input type="checkbox" name="checkedfiles[<?php echo $data['id']; ?>]" value="value" /> + <input type="checkbox" name="checkedfiles[]" value="<?php echo $data['id']; ?>" /> <?php } else { ?> <?php } ?> @@ -281,7 +412,7 @@ } ?> </table> -<input type="radio" id="file_rename" name="file_action" value="rename" /><label for="file_rename"> <?php echo i18n("Rename"); ?></label> +<input type="radio" id="file_rename" name="file_action" value="rename" checked="checked" /><label for="file_rename"> <?php echo i18n("Rename"); ?></label> <input type="radio" id="file_move" name="file_action" value="move" /><label for="file_move"> <?php echo i18n("Move"); ?></label> <input type="radio" id="file_copy" name="file_action" value="copy" /><label for="file_copy"> <?php echo i18n("Copy"); ?></label> <input type="radio" id="file_delete" name="file_action" value="delete" /><label for="file_delete"> <?php echo i18n("Delete"); ?></label> @@ -292,8 +423,10 @@ <br /><br /> <form method="POST" action="<?php echo LINPHA_DIR.'/?cat=filemanager&id='.$parent_id; ?>"> -<input type="text" name="createfolder" value="" /> -<input type="hidden" name="cmd" value="create_folder" /> +<input type="text" name="folder_name" value="" /> +<input type="hidden" name="file_id" value="<?php echo $parent_id; ?>" /> +<input type="hidden" name="cmd" value="do_file_action" /> +<input type="hidden" name="file_action" value="create_folder" /> <input type="submit" name="submit" value="<?php echo i18n("Create Folder"); ?>" /> </form> @@ -407,4 +540,35 @@ return false; } + +/** + * make sure to keep in current directory + * @todo is this enough? + */ +function removeUnWantedChars( $str ) +{ + $str = str_replace('/','',$str); + $str = str_replace('\\','',$str); + + return $str; +} + +/** + * checks if a file/folder with the same name already exists in the same folder + */ +function nameAlreadyExists( $name, $parent_id ) +{ + $query = $GLOBALS['linpha']->db->Execute("SELECT id FROM ".PREFIX."photos " . + "WHERE name = '".LinSql::linAddslashes($name)."' " . + "AND parent_id = '".LinSql::linAddslashes($parent_id)."'"); + + if( $query->EOF) // not a single value returned + { + return false; + } + else + { + return true; + } +} ?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |