PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/_plugins_/spixplorer/action/spx_copy_move.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 263 lines | 161 code | 32 blank | 70 comment | 43 complexity | 7b5a27be968e4b65ae7fb21b19f40da6 MD5 | raw file
  1. <?php
  2. /*------------------------------------------------------------------------------
  3. The contents of this file are subject to the Mozilla Public License
  4. Version 1.1 (the "License"); you may not use this file except in
  5. compliance with the License. You may obtain a copy of the License at
  6. http://www.mozilla.org/MPL/
  7. Software distributed under the License is distributed on an "AS IS"
  8. basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  9. License for the specific language governing rights and limitations
  10. under the License.
  11. The Original Code is fun_copy_move.php, released on 2003-03-31.
  12. The Initial Developer of the Original Code is The QuiX project.
  13. Alternatively, the contents of this file may be used under the terms
  14. of the GNU General Public License Version 2 or later (the "GPL"), in
  15. which case the provisions of the GPL are applicable instead of
  16. those above. If you wish to allow use of your version of this file only
  17. under the terms of the GPL and not to allow others to use
  18. your version of this file under the MPL, indicate your decision by
  19. deleting the provisions above and replace them with the notice and
  20. other provisions required by the GPL. If you do not delete
  21. the provisions above, a recipient may use your version of this file
  22. under either the MPL or the GPL."
  23. ------------------------------------------------------------------------------*/
  24. /*------------------------------------------------------------------------------
  25. Author: The QuiX project
  26. quix@free.fr
  27. http://www.quix.tk
  28. http://quixplorer.sourceforge.net
  29. Comment:
  30. QuiXplorer Version 2.3
  31. File/Directory Copy & Move Functions
  32. Have Fun...
  33. Adaptation spip, plugin spixplorer : bertrand@toggg.com © 2007
  34. ------------------------------------------------------------------------------*/
  35. function action_spx_copy_move()
  36. {
  37. include_spip('inc/spx_init');
  38. copy_move_items($GLOBALS['spx']["dir"]);
  39. exit;
  40. }
  41. //------------------------------------------------------------------------------
  42. function dir_list($dir) { // make list of directories
  43. // this list is used to copy/move items to a specific location
  44. $handle = @opendir(get_abs_dir($dir));
  45. if($handle===false) return; // unable to open dir
  46. while(($new_item=readdir($handle))!==false) {
  47. //if(!@file_exists(get_abs_item($dir, $new_item))) continue;
  48. if(!get_show_item($dir, $new_item)) continue;
  49. if(!get_is_dir($dir,$new_item)) continue;
  50. $dir_list[$new_item] = $new_item;
  51. }
  52. // sort
  53. if(is_array($dir_list)) ksort($dir_list);
  54. return $dir_list;
  55. }
  56. //------------------------------------------------------------------------------
  57. function dir_print($dir_list, $new_dir) { // print list of directories
  58. // this list is used to copy/move items to a specific location
  59. // Link to Parent Directory
  60. $dir_up = dirname($new_dir);
  61. if($dir_up==".") $dir_up = "";
  62. echo "<TR><TD><A HREF=\"javascript:NewDir('".$dir_up;
  63. echo "');\"><IMG border=\"0\" width=\"16\" height=\"16\"";
  64. echo " align=\"ABSMIDDLE\" src=\"" . _DIR_PLUGIN_SPIXPLORER . "_img/_up.gif\" ALT=\"\">&nbsp;..</A></TD></TR>\n";
  65. // Print List Of Target Directories
  66. if(!is_array($dir_list)) return;
  67. while(list($new_item,) = each($dir_list)) {
  68. $s_item=$new_item; if(strlen($s_item)>40) $s_item=substr($s_item,0,37)."...";
  69. echo "<TR><TD><A HREF=\"javascript:NewDir('".get_rel_item($new_dir,$new_item).
  70. "');\"><IMG border=\"0\" width=\"16\" height=\"16\" align=\"ABSMIDDLE\" ".
  71. "src=\"" . _DIR_PLUGIN_SPIXPLORER . "_img/dir.gif\" ALT=\"\">&nbsp;".$s_item."</A></TD></TR>\n";
  72. }
  73. }
  74. //------------------------------------------------------------------------------
  75. function copy_move_items($dir) { // copy/move file/dir
  76. if(($GLOBALS['spx']["permissions"]&01)!=01) show_error(_T('spixplorer:accessfunc'));
  77. // Vars
  78. $first = _request('first');
  79. if($first=="y") $new_dir=$dir;
  80. else $new_dir = stripslashes(_request('new_dir'));
  81. if($new_dir==".") $new_dir="";
  82. $selitems = _request('selitems');
  83. $cnt=count($selitems);
  84. $newitems = _request('newitems');
  85. $do_action = _request('do_action');
  86. // Copy or Move?
  87. if ($do_action != "move") {
  88. $_img = _DIR_PLUGIN_SPIXPLORER . "_img/__copy.gif";
  89. } else {
  90. $_img = _DIR_PLUGIN_SPIXPLORER . "_img/__cut.gif";
  91. }
  92. // Get New Location & Names
  93. if(_request("confirm")!="true") {
  94. show_header($do_action != "move" ?
  95. _T('spixplorer:actcopyitems') :
  96. _T('spixplorer:actmoveitems')
  97. );
  98. // JavaScript for Form:
  99. // Select new target directory / execute action
  100. ?><script language="JavaScript1.2" type="text/javascript">
  101. <!--
  102. function NewDir(newdir) {
  103. document.selform.new_dir.value = newdir;
  104. document.selform.submit();
  105. }
  106. function Execute() {
  107. document.selform.confirm.value = "true";
  108. }
  109. //-->
  110. </script><?php
  111. // "Copy / Move from .. to .."
  112. $s_dir=$dir; if(strlen($s_dir)>40) $s_dir="...".substr($s_dir,-37);
  113. $s_ndir=$new_dir; if(strlen($s_ndir)>40) $s_ndir="...".substr($s_ndir,-37);
  114. echo "<br /><IMG SRC=\"".$_img."\" align=\"ABSMIDDLE\" ALT=\"\">&nbsp;";
  115. echo sprintf($do_action != "move" ?_T('spixplorer:actcopyfrom') :
  116. _T('spixplorer:actmovefrom'), $s_dir, $s_ndir);
  117. echo "<IMG SRC=\"" . _DIR_PLUGIN_SPIXPLORER . "_img/__paste.gif\" align=\"ABSMIDDLE\" ALT=\"\">\n";
  118. // Form for Target Directory & New Names
  119. echo "<br /><br /><FORM name=\"selform\" method=\"post\" action=\"spip.php\"><TABLE>\n";
  120. list($arg, $hash) = make_hash('copy_move', $dir);
  121. echo '
  122. <input type="hidden" name="action" value="spx_copy_move">
  123. <input type="hidden" name="arg" value="' . $arg . '">
  124. <input type="hidden" name="hash" value="' . $hash . '">
  125. <input type="hidden" name="dir" value="' . htmlentities($dir) .'">
  126. <input type="hidden" name="order" value="' .
  127. ($GLOBALS['spx']["srt"] == 'no' ? '-' : '') . $GLOBALS['spx']["order"] .'">
  128. ';
  129. echo "<INPUT type=\"hidden\" name=\"do_action\" value=\"" . $do_action . "\">\n";
  130. echo "<INPUT type=\"hidden\" name=\"confirm\" value=\"false\">\n";
  131. echo "<INPUT type=\"hidden\" name=\"first\" value=\"n\">\n";
  132. echo "<INPUT type=\"hidden\" name=\"new_dir\" value=\"".$new_dir."\">\n";
  133. // List Directories to select Target
  134. dir_print(dir_list($new_dir),$new_dir);
  135. echo "</TABLE><br /><TABLE>\n";
  136. // Print Text Inputs to change Names
  137. for($i=0;$i<$cnt;++$i) {
  138. $selitem=stripslashes($selitems[$i]);
  139. if(isset($newitems[$i])) {
  140. $newitem=stripslashes($newitems[$i]);
  141. if($first=="y") $newitem=$selitem;
  142. } else $newitem=$selitem;
  143. $s_item=$selitem; if(strlen($s_item)>50) $s_item=substr($s_item,0,47)."...";
  144. echo "<TR><TD><IMG SRC=\"" . _DIR_PLUGIN_SPIXPLORER . "_img/_info.gif\" align=\"ABSMIDDLE\" ALT=\"\">";
  145. // Old Name
  146. echo "<INPUT type=\"hidden\" name=\"selitems[]\" value=\"";
  147. echo $selitem."\">&nbsp;".$s_item."&nbsp;";
  148. // New Name
  149. echo "</TD><TD><INPUT type=\"text\" size=\"25\" name=\"newitems[]\" value=\"";
  150. echo $newitem."\"></TD></TR>\n";
  151. }
  152. // Submit & Cancel
  153. echo "</TABLE><br /><TABLE><TR>\n<TD>";
  154. echo "<INPUT type=\"submit\" value=\"";
  155. echo $do_action != "move" ? _T('spixplorer:btncopy') : _T('spixplorer:btnmove');
  156. echo "\" onclick=\"javascript:Execute();\"></TD>\n<TD>";
  157. echo "<input type=\"button\" value=\""._T('spixplorer:btncancel');
  158. echo "\" onClick=\"javascript:location='".make_link("list",$dir,NULL);
  159. echo "';\"></TD>\n</TR></FORM></TABLE><br />\n";
  160. show_footer();
  161. return;
  162. }
  163. // DO COPY/MOVE
  164. // ALL OK?
  165. if(!@file_exists(get_abs_dir($new_dir))) show_error($new_dir.": "._T('spixplorer:targetexist'));
  166. if(!get_show_item($new_dir,"")) show_error($new_dir.": "._T('spixplorer:accesstarget'));
  167. if(!down_home(get_abs_dir($new_dir))) show_error($new_dir.": "._T('spixplorer:targetabovehome'));
  168. // copy / move files
  169. $err=false;
  170. for($i=0;$i<$cnt;++$i) {
  171. $tmp = stripslashes($selitems[$i]);
  172. $new = basename(stripslashes($newitems[$i]));
  173. $abs_item = get_abs_item($dir,$tmp);
  174. $abs_new_item = get_abs_item($new_dir,$new);
  175. $items[$i] = $tmp;
  176. // Check
  177. if($new=="") {
  178. $error[$i]= _T('spixplorer:miscnoname');
  179. $err=true; continue;
  180. }
  181. if(!@file_exists($abs_item)) {
  182. $error[$i]= _T('spixplorer:itemexist');
  183. $err=true; continue;
  184. }
  185. if(!get_show_item($dir, $tmp)) {
  186. $error[$i]= _T('spixplorer:accessitem');
  187. $err=true; continue;
  188. }
  189. if(@file_exists($abs_new_item)) {
  190. $error[$i]= _T('spixplorer:targetdoesexist');
  191. $err=true; continue;
  192. }
  193. // Copy / Move
  194. if ($do_action == "copy") {
  195. if(@is_link($abs_item) || @is_file($abs_item)) {
  196. // check file-exists to avoid error with 0-size files (PHP 4.3.0)
  197. $ok=@copy($abs_item,$abs_new_item); //||@file_exists($abs_new_item);
  198. } elseif(@is_dir($abs_item)) {
  199. $ok=copy_dir($abs_item,$abs_new_item);
  200. }
  201. } else {
  202. $ok=@rename($abs_item,$abs_new_item);
  203. }
  204. if($ok===false) {
  205. $error[$i] = $do_action == "copy" ?
  206. _T('spixplorer:copyitem') :
  207. _T('spixplorer:moveitem');
  208. $err=true; continue;
  209. }
  210. $error[$i]=NULL;
  211. }
  212. if($err) { // there were errors
  213. $err_msg="";
  214. for($i=0;$i<$cnt;++$i) {
  215. if($error[$i]==NULL) continue;
  216. $err_msg .= $items[$i]." : ".$error[$i]."<br />\n";
  217. }
  218. show_error($err_msg);
  219. }
  220. header("Location: ".make_link("list",$dir,NULL));
  221. }
  222. //------------------------------------------------------------------------------
  223. ?>