/pigeoncms/Plugins/fckeditor/editor/filemanager/connectors/perl/commands.pl

http://pigeoncms.googlecode.com/ · Perl · 200 lines · 140 code · 28 blank · 32 comment · 28 complexity · 7bed26c5bee093677bfdeafd7fc26fe7 MD5 · raw file

  1. #####
  2. # FCKeditor - The text editor for Internet - http://www.fckeditor.net
  3. # Copyright (C) 2003-2009 Frederico Caldeira Knabben
  4. #
  5. # == BEGIN LICENSE ==
  6. #
  7. # Licensed under the terms of any of the following licenses at your
  8. # choice:
  9. #
  10. # - GNU General Public License Version 2 or later (the "GPL")
  11. # http://www.gnu.org/licenses/gpl.html
  12. #
  13. # - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
  14. # http://www.gnu.org/licenses/lgpl.html
  15. #
  16. # - Mozilla Public License Version 1.1 or later (the "MPL")
  17. # http://www.mozilla.org/MPL/MPL-1.1.html
  18. #
  19. # == END LICENSE ==
  20. #
  21. # This is the File Manager Connector for Perl.
  22. #####
  23. sub GetFolders
  24. {
  25. local($resourceType, $currentFolder) = @_;
  26. # Map the virtual path to the local server path.
  27. $sServerDir = &ServerMapFolder($resourceType, $currentFolder);
  28. print "<Folders>"; # Open the "Folders" node.
  29. opendir(DIR,"$sServerDir");
  30. @files = grep(!/^\.\.?$/,readdir(DIR));
  31. closedir(DIR);
  32. foreach $sFile (@files) {
  33. if($sFile != '.' && $sFile != '..' && (-d "$sServerDir$sFile")) {
  34. $cnv_filename = &ConvertToXmlAttribute($sFile);
  35. print '<Folder name="' . $cnv_filename . '" />';
  36. }
  37. }
  38. print "</Folders>"; # Close the "Folders" node.
  39. }
  40. sub GetFoldersAndFiles
  41. {
  42. local($resourceType, $currentFolder) = @_;
  43. # Map the virtual path to the local server path.
  44. $sServerDir = &ServerMapFolder($resourceType,$currentFolder);
  45. # Initialize the output buffers for "Folders" and "Files".
  46. $sFolders = '<Folders>';
  47. $sFiles = '<Files>';
  48. opendir(DIR,"$sServerDir");
  49. @files = grep(!/^\.\.?$/,readdir(DIR));
  50. closedir(DIR);
  51. foreach $sFile (@files) {
  52. if($sFile ne '.' && $sFile ne '..') {
  53. if(-d "$sServerDir$sFile") {
  54. $cnv_filename = &ConvertToXmlAttribute($sFile);
  55. $sFolders .= '<Folder name="' . $cnv_filename . '" />' ;
  56. } else {
  57. ($iFileSize,$refdate,$filedate,$fileperm) = (stat("$sServerDir$sFile"))[7,8,9,2];
  58. if($iFileSize > 0) {
  59. $iFileSize = int($iFileSize / 1024);
  60. if($iFileSize < 1) {
  61. $iFileSize = 1;
  62. }
  63. }
  64. $cnv_filename = &ConvertToXmlAttribute($sFile);
  65. $sFiles .= '<File name="' . $cnv_filename . '" size="' . $iFileSize . '" />' ;
  66. }
  67. }
  68. }
  69. print $sFolders ;
  70. print '</Folders>'; # Close the "Folders" node.
  71. print $sFiles ;
  72. print '</Files>'; # Close the "Files" node.
  73. }
  74. sub CreateFolder
  75. {
  76. local($resourceType, $currentFolder) = @_;
  77. $sErrorNumber = '0' ;
  78. $sErrorMsg = '' ;
  79. if($FORM{'NewFolderName'} ne "") {
  80. $sNewFolderName = $FORM{'NewFolderName'};
  81. $sNewFolderName =~ s/\.|\\|\/|\||\:|\?|\*|\"|<|>|[[:cntrl:]]/_/g;
  82. # Map the virtual path to the local server path of the current folder.
  83. $sServerDir = &ServerMapFolder($resourceType, $currentFolder);
  84. if(-w $sServerDir) {
  85. $sServerDir .= $sNewFolderName;
  86. $sErrorMsg = &CreateServerFolder($sServerDir);
  87. if($sErrorMsg == 0) {
  88. $sErrorNumber = '0';
  89. } elsif($sErrorMsg eq 'Invalid argument' || $sErrorMsg eq 'No such file or directory') {
  90. $sErrorNumber = '102'; #// Path too long.
  91. } else {
  92. $sErrorNumber = '110';
  93. }
  94. } else {
  95. $sErrorNumber = '103';
  96. }
  97. } else {
  98. $sErrorNumber = '102' ;
  99. }
  100. # Create the "Error" node.
  101. $cnv_errmsg = &ConvertToXmlAttribute($sErrorMsg);
  102. print '<Error number="' . $sErrorNumber . '" />';
  103. }
  104. sub FileUpload
  105. {
  106. eval("use File::Copy;");
  107. local($resourceType, $currentFolder) = @_;
  108. $allowedExtensions = $allowedExtensions{$resourceType};
  109. $sErrorNumber = '0' ;
  110. $sFileName = '' ;
  111. if($new_fname) {
  112. # Map the virtual path to the local server path.
  113. $sServerDir = &ServerMapFolder($resourceType,$currentFolder);
  114. # Get the uploaded file name.
  115. $sFileName = $new_fname;
  116. $sFileName =~ s/\\|\/|\||\:|\?|\*|\"|<|>|[[:cntrl:]]/_/g;
  117. $sFileName =~ s/\.(?![^.]*$)/_/g;
  118. $ext = '';
  119. if($sFileName =~ /([^\\\/]*)\.(.*)$/) {
  120. $ext = $2;
  121. }
  122. $allowedRegex = qr/^($allowedExtensions)$/i;
  123. if (!($ext =~ $allowedRegex)) {
  124. SendUploadResults('202', '', '', '');
  125. }
  126. $sOriginalFileName = $sFileName;
  127. $iCounter = 0;
  128. while(1) {
  129. $sFilePath = $sServerDir . $sFileName;
  130. if(-e $sFilePath) {
  131. $iCounter++ ;
  132. ($path,$BaseName,$ext) = &RemoveExtension($sOriginalFileName);
  133. $sFileName = $BaseName . '(' . $iCounter . ').' . $ext;
  134. $sErrorNumber = '201';
  135. } else {
  136. copy("$img_dir/$new_fname","$sFilePath");
  137. if (defined $CHMOD_ON_UPLOAD) {
  138. if ($CHMOD_ON_UPLOAD) {
  139. umask(000);
  140. chmod($CHMOD_ON_UPLOAD,$sFilePath);
  141. }
  142. }
  143. else {
  144. umask(000);
  145. chmod(0777,$sFilePath);
  146. }
  147. unlink("$img_dir/$new_fname");
  148. last;
  149. }
  150. }
  151. } else {
  152. $sErrorNumber = '202' ;
  153. }
  154. $sFileName =~ s/"/\\"/g;
  155. SendUploadResults($sErrorNumber, $GLOBALS{'UserFilesPath'}.$resourceType.$currentFolder.$sFileName, $sFileName, '');
  156. }
  157. sub SendUploadResults
  158. {
  159. local($sErrorNumber, $sFileUrl, $sFileName, $customMsg) = @_;
  160. # Minified version of the document.domain automatic fix script (#1919).
  161. # The original script can be found at _dev/domain_fix_template.js
  162. # Note: in Perl replace \ with \\ and $ with \$
  163. print <<EOF;
  164. Content-type: text/html
  165. <script type="text/javascript">
  166. (function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\\.|\$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();
  167. EOF
  168. print 'window.parent.OnUploadCompleted(' . $sErrorNumber . ',"' . JS_cnv($sFileUrl) . '","' . JS_cnv($sFileName) . '","' . JS_cnv($customMsg) . '") ;';
  169. print '</script>';
  170. exit ;
  171. }
  172. 1;