Ticket #306: phpconnector-diff-v241-v1-tallyce.txt

File phpconnector-diff-v241-v1-tallyce.txt, 12.1 KB (added by Thomas Tallyce, 13 years ago)

Diff for FCKeditor2.4.1 to implement subdirectory,regexp,overwriting configuration

Line 
1diff -ru --exclude='fckconfig*' FCKeditor_2.4.1-virgin/editor/filemanager/browser/default/connectors/php/commands.php FCKeditor_2.4.1-patched/editor/filemanager/browser/default/connectors/php/commands.php
2--- FCKeditor_2.4.1-virgin/editor/filemanager/browser/default/connectors/php/commands.php       2007-03-01 22:55:20.000000000 +0000
3+++ FCKeditor_2.4.1-patched/editor/filemanager/browser/default/connectors/php/commands.php      2007-03-27 12:56:52.000000000 +0100
4@@ -177,33 +177,70 @@
5 
6                $arAllowed      = $Config['AllowedExtensions'][$resourceType] ;
7                $arDenied       = $Config['DeniedExtensions'][$resourceType] ;
8+               $arRegexp       = (isSet ($Config['Regexp']) && array_key_exists ($resourceType, $Config['Regexp']) ? $Config['Regexp'][$resourceType] : '');
9 
10-               if ( ( count($arAllowed) == 0 || in_array( $sExtension, $arAllowed ) ) && ( count($arDenied) == 0 || !in_array( $sExtension, $arDenied ) ) )
11+               if ( ( count($arAllowed) == 0 || in_array( $sExtension, $arAllowed ) ) && ( count($arDenied) == 0 || !in_array( $sExtension, $arDenied ) ) && ( $arRegexp === '' || ereg( $arRegexp, RemoveExtension( $sOriginalFileName ) ) ) )
12                {
13-                       $iCounter = 0 ;
14-
15-                       while ( true )
16-                       {
17-                               $sFilePath = $sServerDir . $sFileName ;
18-
19+                       // Assign the new file's name
20+                       $sFilePath = $sServerDir . $sFileName ;
21+                       $doUpload = true;
22+                       
23+                       // If the file already exists, select what behaviour should be adopted
24+                       if ( is_file( $sFilePath ) ) {
25+                               $sFilenameClashBehaviour = (isSet ($Config['filenameClashBehaviour']) ? $Config['filenameClashBehaviour'] : 'newname');
26+                               switch ($sFilenameClashBehaviour) {
27+                                       
28+                                       // overwrites the version on the server with the same name
29+                                       case 'overwrite':
30+                                               $sErrorNumber = '204' ;
31+                                               // Do nothing - move_uploaded_file will just overwrite naturally
32+                                               break;
33+                                               
34+                                       // generate an error so that the file uploading fails
35+                                       case false:
36+                                       case 'false':   // String version in case someone quotes the boolean text equivalent
37+                                               $sErrorNumber = '205' ;
38+                                               $doUpload = false;
39+                                               break;
40+                                       
41+                                       // give the uploaded file a new name (this was the (unconfigurable) behaviour in FCKeditor2.2) - named as: originalName(number).extension
42+                                       case 'newname':
43+                                               $iCounter = 0 ;
44+                                               while ( true )
45+                                               {
46+                                                       if ( is_file( $sFilePath ) )
47+                                                       {
48+                                                               $iCounter++ ;
49+                                                               $sFileName = RemoveExtension( $sOriginalFileName ) . '(' . $iCounter . ').' . $sExtension ;
50+                                                               $sErrorNumber = '201' ;
51+                                                               $sFilePath = $sServerDir . $sFileName ;
52+                                                       }
53+                                                       else
54+                                                       {
55+                                                               break ;
56+                                                       }
57+                                               }
58+                                               break;
59+                                               
60+                                       // (default behaviour) back up the version on the server to the same name + timestamp appended to the filename (after the extension)
61+                                       case 'renameold':
62+                                       default:
63+                                               $timestamp = '.' . date ('Ymd-His');
64+                                               copy ($sFilePath, $sFilePath . $timestamp);
65+                                               $sFileName = $sFileName . $timestamp;
66+                                               $sErrorNumber = '206' ;
67+                                               break;
68+                               }       // End of switch statement
69+                       }
70+                       
71+                       // Now its name has been assigned, move the uploaded file into position
72+                       if ($doUpload) {
73+                               move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;
74                                if ( is_file( $sFilePath ) )
75                                {
76-                                       $iCounter++ ;
77-                                       $sFileName = RemoveExtension( $sOriginalFileName ) . '(' . $iCounter . ').' . $sExtension ;
78-                                       $sErrorNumber = '201' ;
79-                               }
80-                               else
81-                               {
82-                                       move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;
83-
84-                                       if ( is_file( $sFilePath ) )
85-                                       {
86-                                               $oldumask = umask(0) ;
87-                                               chmod( $sFilePath, 0777 ) ;
88-                                               umask( $oldumask ) ;
89-                                       }
90-
91-                                       break ;
92+                                       $oldumask = umask(0) ;
93+                                       chmod( $sFilePath, 0777 ) ;
94+                                       umask( $oldumask ) ;
95                                }
96                        }
97                }
98diff -ru --exclude='fckconfig*' FCKeditor_2.4.1-virgin/editor/filemanager/browser/default/connectors/php/config.php FCKeditor_2.4.1-patched/editor/filemanager/browser/default/connectors/php/config.php
99--- FCKeditor_2.4.1-virgin/editor/filemanager/browser/default/connectors/php/config.php 2007-03-06 11:21:08.000000000 +0000
100+++ FCKeditor_2.4.1-patched/editor/filemanager/browser/default/connectors/php/config.php        2007-03-27 13:22:09.000000000 +0100
101@@ -24,33 +24,57 @@
102 
103 global $Config ;
104 
105-// SECURITY: You must explicitelly enable this "connector". (Set it to "true").
106+// SECURITY: You must explicitly enable this "connector". (Set it to "true").
107 $Config['Enabled'] = false ;
108 
109 
110 // Path to user files relative to the document root.
111-$Config['UserFilesPath'] = '/userfiles/' ;
112+$Config['UserFilesPath'] = '/userfiles/' ;     // Set to / if you want filebrowsing across the whole site directory
113 
114 // Fill the following value it you prefer to specify the absolute path for the
115-// user files directory. Usefull if you are using a virtual directory, symbolic
116+// user files directory. Useful if you are using a virtual directory, symbolic
117 // link or alias. Examples: 'C:\\MySite\\userfiles\\' or '/root/mysite/userfiles/'.
118 // Attention: The above 'UserFilesPath' must point to the same directory.
119-$Config['UserFilesAbsolutePath'] = '' ;
120+$Config['UserFilesAbsolutePath'] = '' ;        // Set to $_SERVER['DOCUMENT_ROOT'] if you want filebrowsing across the whole site
121 
122 // Due to security issues with Apache modules, it is reccomended to leave the
123 // following setting enabled.
124 $Config['ForceSingleExtension'] = true ;
125 
126+// What to do if a file being uploaded has the same name as an existing file on the server
127+//     'renameold' - (default behaviour) backs up the version on the server to the same name + timestamp appended to the filename (after the extension)
128+//     'overwrite' - overwrites the version on the server with the same name
129+//     'newname' - gives the uploaded file a new name (this was the (unconfigurable) behaviour in FCKeditor2.2)
130+//     false - generates an error so that the file uploading fails
131+$Config['filenameClashBehaviour'] = 'renameold';
132+
133+// In the following groupings:
134+//             'Subdirectory' is the subdirectory under the main 'UserFilesPath'
135+//                     e.g. 'File/'
136+//                     or leave it blank as '' to use the main UserFilesPath directory (i.e. the user can add files across the whole site)
137+//             'Regexp' ereg-style regexp which the name must validate to
138+//                     This regexp applies to the part BEFORE the dot + file extension
139+//                     e.g. '^([-_a-zA-Z0-9]{1,25})$'   (which would be sensible for best practice)
140+//                     or leave it blank as '' for no checking
141+
142+$Config['Subdirectory']['File']        = 'file/' ;
143 $Config['AllowedExtensions']['File']   = array() ;
144 $Config['DeniedExtensions']['File']            = array('html','htm','php','php2','php3','php4','php5','phtml','pwml','inc','asp','aspx','ascx','jsp','cfm','cfc','pl','bat','exe','com','dll','vbs','js','reg','cgi','htaccess','asis') ;
145+$Config['Regexp']['File']      = '' ;
146 
147+$Config['Subdirectory']['Image']       = 'image/' ;
148 $Config['AllowedExtensions']['Image']  = array('jpg','gif','jpeg','png') ;
149 $Config['DeniedExtensions']['Image']   = array() ;
150+$Config['Regexp']['Image']     = '' ;
151 
152+$Config['Subdirectory']['Flash']       = 'flash/' ;
153 $Config['AllowedExtensions']['Flash']  = array('swf','fla') ;
154 $Config['DeniedExtensions']['Flash']   = array() ;
155+$Config['Regexp']['Flash']     = '' ;
156 
157+$Config['Subdirectory']['Media']       = 'media/' ;
158 $Config['AllowedExtensions']['Media']  = array('swf','fla','jpg','gif','jpeg','png','avi','mpg','mpeg') ;
159 $Config['DeniedExtensions']['Media']   = array() ;
160+$Config['Regexp']['Media']     = '' ;
161 
162 ?>
163\ No newline at end of file
164diff -ru --exclude='fckconfig*' FCKeditor_2.4.1-virgin/editor/filemanager/browser/default/connectors/php/connector.php FCKeditor_2.4.1-patched/editor/filemanager/browser/default/connectors/php/connector.php
165--- FCKeditor_2.4.1-virgin/editor/filemanager/browser/default/connectors/php/connector.php      2007-03-01 22:55:20.000000000 +0000
166+++ FCKeditor_2.4.1-patched/editor/filemanager/browser/default/connectors/php/connector.php     2007-03-27 13:04:42.000000000 +0100
167@@ -36,6 +36,11 @@
168 // Get the "UserFiles" path.
169 $GLOBALS["UserFilesPath"] = '' ;
170 
171+// Global the subdirectories for the media types
172+if (isSet ($Config['Subdirectory'])) {
173+       $GLOBALS['Subdirectory'] = $Config['Subdirectory'] ;
174+}
175+
176 if ( isset( $Config['UserFilesPath'] ) )
177        $GLOBALS["UserFilesPath"] = $Config['UserFilesPath'] ;
178 else if ( isset( $_GET['ServerPath'] ) )
179diff -ru --exclude='fckconfig*' FCKeditor_2.4.1-virgin/editor/filemanager/browser/default/connectors/php/io.php FCKeditor_2.4.1-patched/editor/filemanager/browser/default/connectors/php/io.php
180--- FCKeditor_2.4.1-virgin/editor/filemanager/browser/default/connectors/php/io.php     2007-03-01 22:55:20.000000000 +0000
181+++ FCKeditor_2.4.1-patched/editor/filemanager/browser/default/connectors/php/io.php    2007-03-27 13:13:19.000000000 +0100
182@@ -24,10 +24,7 @@
183 
184 function GetUrlFromPath( $resourceType, $folderPath )
185 {
186-       if ( $resourceType == '' )
187-               return RemoveFromEnd( $GLOBALS["UserFilesPath"], '/' ) . $folderPath ;
188-       else
189-               return $GLOBALS["UserFilesPath"] . strtolower( $resourceType ) . $folderPath ;
190+       return $GLOBALS["UserFilesPath"] . GetResourceTypeSubdirectory ( $resourceType ) . RemoveFromStart ( $folderPath , '/' );
191 }
192 
193 function RemoveExtension( $fileName )
194@@ -38,7 +35,7 @@
195 function ServerMapFolder( $resourceType, $folderPath )
196 {
197        // Get the resource type directory.
198-       $sResourceTypePath = $GLOBALS["UserFilesDirectory"] . strtolower( $resourceType ) . '/' ;
199+       $sResourceTypePath = $GLOBALS["UserFilesDirectory"] . GetResourceTypeSubdirectory ( $resourceType );
200 
201        // Ensure that the directory exists.
202        CreateServerFolder( $sResourceTypePath ) ;
203@@ -47,6 +44,27 @@
204        return $sResourceTypePath . RemoveFromStart( $folderPath, '/' ) ;
205 }
206 
207+
208+// Function to determine the directory where the files for this resource type are located
209+function GetResourceTypeSubdirectory ( $resourceType )
210+{
211+       // Return the empty string if no resource type specified, i.e. don't go down into any subdirectory
212+       if ($resourceType == '') {return '';}
213+       
214+       // Use the configured value if it exists; NB array_key_exists is used rather than isSet to allow empty values
215+       if (isSet ($GLOBALS['Subdirectory']) && array_key_exists ($resourceType, $GLOBALS['Subdirectory'])) {
216+               
217+               // If the value is empty, don't add a slash to the empty string, and return that
218+               if ($GLOBALS['Subdirectory'][$resourceType] == '') {return '';}
219+               
220+               // Otherwise ensure the subdirectory is slash-terminated, and return that
221+               return RemoveFromEnd( $GLOBALS['Subdirectory'][$resourceType], '/' ) . '/';
222+       }
223+       
224+       // Otherwise default to the resource type name itself as the directory name
225+       return strtolower ( $resourceType ) . '/';
226+}
227+
228 function GetParentFolder( $folderPath )
229 {
230        $sPattern = "-[/\\\\][^/\\\\]+[/\\\\]?$-" ;
231@@ -55,6 +73,12 @@
232 
233 function CreateServerFolder( $folderPath )
234 {
235+       // Ensure the folder path has no double-slashes, or mkdir may fail on certain platforms
236+       while (strpos ($folderPath, '//') !== false)
237+       {
238+               $folderPath = str_replace( '//', '/', $folderPath ) ;
239+       }
240+       
241        $sParent = GetParentFolder( $folderPath ) ;
242 
243        // Check if the parent exists, or create it.
244diff -ru --exclude='fckconfig*' FCKeditor_2.4.1-virgin/editor/filemanager/browser/default/frmupload.html FCKeditor_2.4.1-patched/editor/filemanager/browser/default/frmupload.html
245--- FCKeditor_2.4.1-virgin/editor/filemanager/browser/default/frmupload.html    2007-03-01 22:55:20.000000000 +0000
246+++ FCKeditor_2.4.1-patched/editor/filemanager/browser/default/frmupload.html   2007-03-27 13:16:46.000000000 +0100
247@@ -81,6 +81,17 @@
248                case 202 :
249                        alert( 'Invalid file' ) ;
250                        break ;
251+               case 204 :
252+                       window.parent.frames['frmResourcesList'].Refresh() ;
253+                       alert( 'Your file has been successfully uploaded, and replaced the existing file with the same name.' ) ;
254+                       break ;
255+               case 205 :
256+                       alert( 'A file of that name already exists, so the upload failed.' ) ;
257+                       break ;
258+               case 206 :
259+                       window.parent.frames['frmResourcesList'].Refresh() ;
260+                       alert( 'Your file has been successfully uploaded. There was already a file of that name, so that previous file has been backed up by being renamed to "' + data + '".' ) ;
261+                       break ;
262                default :
263                        alert( 'Error on file upload. Error number: ' + errorNumber ) ;
264                        break ;
© 2003 – 2019 CKSource – Frederico Knabben. All rights reserved. | Terms of use | Privacy policy