Project

General

Profile

1
<?php
2
/*
3
 * CKeditor - The text editor for Internet - http://www.ckeditor.net
4
 * Copyright (C) 2003-2010 Frederico Caldeira Knabben
5
 *
6
 * http://www.mixedwaves.com/2010/02/integrating-fckeditor-filemanager-in-ckeditor/
7
 *
8
 * == BEGIN LICENSE ==
9
 *
10
 * Licensed under the terms of any of the following licenses at your
11
 * choice:
12
 *
13
 *  - GNU General Public License Version 2 or later (the "GPL")
14
 *    http://www.gnu.org/licenses/gpl.html
15
 *
16
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
17
 *    http://www.gnu.org/licenses/lgpl.html
18
 *
19
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
20
 *    http://www.mozilla.org/MPL/MPL-1.1.html
21
 *
22
 * == END LICENSE ==
23
 *
24
 * This is the File Manager Connector for PHP.
25
 */
26

    
27
function GetFolders( $resourceType, $currentFolder )
28
{
29
    // Map the virtual path to the local server path.
30
    $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'GetFolders' ) ;
31

    
32
    // Array that will hold the folders names.
33
    $aFolders    = array() ;
34

    
35
    $oCurrentFolder = @opendir( $sServerDir ) ;
36

    
37
    if ($oCurrentFolder !== false)
38
    {
39
        while ( $sFile = readdir( $oCurrentFolder ) )
40
        {
41
            if ( $sFile != '.' && $sFile != '..' && is_dir( $sServerDir . $sFile ) )
42
                $aFolders[] = '<Folder name="' . ConvertToXmlAttribute( $sFile ) . '" />' ;
43
        }
44
        closedir( $oCurrentFolder ) ;
45
    }
46

    
47
    // Open the "Folders" node.
48
    echo "<Folders>" ;
49

    
50
    natcasesort( $aFolders ) ;
51
    foreach ( $aFolders as $sFolder )
52
        echo $sFolder ;
53

    
54
    // Close the "Folders" node.
55
    echo "</Folders>" ;
56
}
57

    
58
function GetFoldersAndFiles( $resourceType, $currentFolder )
59
{
60
    // Map the virtual path to the local server path.
61
    $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'GetFoldersAndFiles' ) ;
62

    
63
    // Arrays that will hold the folders and files names.
64
    $aFolders    = array() ;
65
    $aFiles        = array() ;
66

    
67
    $oCurrentFolder = @opendir( $sServerDir ) ;
68

    
69
    if ($oCurrentFolder !== false)
70
    {
71
        while ( $sFile = readdir( $oCurrentFolder ) )
72
        {
73
            if ( $sFile != '.' && $sFile != '..' )
74
            {
75
                if ( is_dir( $sServerDir . $sFile ) )
76
                    $aFolders[] = '<Folder name="' . ConvertToXmlAttribute( $sFile ) . '" />' ;
77
                else
78
                {
79
                    $iFileSize = @filesize( $sServerDir . $sFile ) ;
80
                    if ( !$iFileSize ) {
81
                        $iFileSize = 0 ;
82
                    }
83
                    if ( $iFileSize > 0 )
84
                    {
85
                        $iFileSize = round( $iFileSize / 1024 ) ;
86
                        if ( $iFileSize < 1 )
87
                            $iFileSize = 1 ;
88
                    }
89

    
90
                    $aFiles[] = '<File name="' . ConvertToXmlAttribute( $sFile ) . '" size="' . $iFileSize . '" />' ;
91
                }
92
            }
93
        }
94
        closedir( $oCurrentFolder ) ;
95
    }
96

    
97
    // Send the folders
98
    natcasesort( $aFolders ) ;
99
    echo '<Folders>' ;
100

    
101
    foreach ( $aFolders as $sFolder )
102
        echo $sFolder ;
103

    
104
    echo '</Folders>' ;
105

    
106
    // Send the files
107
    natcasesort( $aFiles ) ;
108
    echo '<Files>' ;
109

    
110
    foreach ( $aFiles as $sFiles )
111
        echo $sFiles ;
112

    
113
    echo '</Files>' ;
114
}
115

    
116
function CreateFolder( $resourceType, $currentFolder )
117
{
118
    if (!isset($_GET)) {
119
        global $_GET;
120
    }
121
    $sErrorNumber    = '0' ;
122
    $sErrorMsg        = '' ;
123

    
124
    if ( isset( $_GET['NewFolderName'] ) )
125
    {
126
        $sNewFolderName = $_GET['NewFolderName'] ;
127
        $sNewFolderName = SanitizeFolderName( $sNewFolderName ) ;
128

    
129
        if ( strpos( $sNewFolderName, '..' ) !== FALSE )
130
            $sErrorNumber = '102' ;        // Invalid folder name.
131
        else
132
        {
133
            // Map the virtual path to the local server path of the current folder.
134
            $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'CreateFolder' ) ;
135

    
136
            if ( is_writable( $sServerDir ) )
137
            {
138
                $sServerDir .= $sNewFolderName ;
139

    
140
                $sErrorMsg = CreateServerFolder( $sServerDir ) ;
141

    
142
                switch ( $sErrorMsg )
143
                {
144
                    case '' :
145
                        $sErrorNumber = '0' ;
146
                        break ;
147
                    case 'Invalid argument' :
148
                    case 'No such file or directory' :
149
                        $sErrorNumber = '102' ;        // Path too long.
150
                        break ;
151
                    default :
152
                        $sErrorNumber = '110' ;
153
                        break ;
154
                }
155
            }
156
            else
157
                $sErrorNumber = '103' ;
158
        }
159
    }
160
    else
161
        $sErrorNumber = '102' ;
162

    
163
    // Create the "Error" node.
164
    echo '<Error number="' . $sErrorNumber . '" />' ;
165
}
166

    
167
// Notice the last paramter added to pass the CKEditor callback function
168
function FileUpload( $resourceType, $currentFolder, $sCommand, $CKEcallback = '' )
169
{
170
    if (!isset($_FILES)) {
171
        global $_FILES;
172
    }
173
    $sErrorNumber = '0' ;
174
    $sFileName = '' ;
175
 
176
        //PATCH to detect a quick file upload.
177
    if (( isset( $_FILES['NewFile'] ) && !is_null( $_FILES['NewFile']['tmp_name'] ) ) || (isset( $_FILES['upload'] ) && !is_null( $_FILES['upload']['tmp_name'] ) ))
178
    {
179
        global $Config ;
180
 
181
                 //PATCH to detect a quick file upload.
182
        $oFile = isset($_FILES['NewFile']) ? $_FILES['NewFile'] : $_FILES['upload'];
183
 
184
        // Map the virtual path to the local server path.
185
        $sServerDir = ServerMapFolder( $resourceType, $currentFolder, $sCommand ) ;
186
 
187
        // Get the uploaded file name.
188
        $sFileName = $oFile['name'] ;
189
        $sFileName = SanitizeFileName( $sFileName ) ;
190
 
191
        $sOriginalFileName = $sFileName ;
192
 
193
        // Get the extension.
194
        $sExtension = substr( $sFileName, ( strrpos($sFileName, '.') + 1 ) ) ;
195
        $sExtension = strtolower( $sExtension ) ;
196
 
197
        if ( isset( $Config['SecureImageUploads'] ) )
198
        {
199
            if ( ( $isImageValid = IsImageValid( $oFile['tmp_name'], $sExtension ) ) === false )
200
            {
201
                $sErrorNumber = '202' ;
202
            }
203
        }
204
 
205
        if ( isset( $Config['HtmlExtensions'] ) )
206
        {
207
            if ( !IsHtmlExtension( $sExtension, $Config['HtmlExtensions'] ) &&
208
                ( $detectHtml = DetectHtml( $oFile['tmp_name'] ) ) === true )
209
            {
210
                $sErrorNumber = '202' ;
211
            }
212
        }
213
 
214
        // Check if it is an allowed extension.
215
        if ( !$sErrorNumber && IsAllowedExt( $sExtension, $resourceType ) )
216
        {
217
            $iCounter = 0 ;
218
 
219
            while ( true )
220
            {
221
                $sFilePath = $sServerDir . $sFileName ;
222
 
223
                if ( is_file( $sFilePath ) )
224
                {
225
                    $iCounter++ ;
226
                    $sFileName = RemoveExtension( $sOriginalFileName ) . '(' . $iCounter . ').' . $sExtension ;
227
                    $sErrorNumber = '201' ;
228
                }
229
                else
230
                {
231
                    move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;
232
 
233
                    if ( is_file( $sFilePath ) )
234
                    {
235
                        if ( isset( $Config['ChmodOnUpload'] ) && !$Config['ChmodOnUpload'] )
236
                        {
237
                            break ;
238
                        }
239
 
240
                        $permissions = 0777;
241
 
242
                        if ( isset( $Config['ChmodOnUpload'] ) && $Config['ChmodOnUpload'] )
243
                        {
244
                            $permissions = $Config['ChmodOnUpload'] ;
245
                        }
246
 
247
                        $oldumask = umask(0) ;
248
                        chmod( $sFilePath, $permissions ) ;
249
                        umask( $oldumask ) ;
250
                    }
251
 
252
                    break ;
253
                }
254
            }
255
 
256
            if ( file_exists( $sFilePath ) )
257
            {
258
                //previous checks failed, try once again
259
                if ( isset( $isImageValid ) && $isImageValid === -1 && IsImageValid( $sFilePath, $sExtension ) === false )
260
                {
261
                    @unlink( $sFilePath ) ;
262
                    $sErrorNumber = '202' ;
263
                }
264
                else if ( isset( $detectHtml ) && $detectHtml === -1 && DetectHtml( $sFilePath ) === true )
265
                {
266
                    @unlink( $sFilePath ) ;
267
                    $sErrorNumber = '202' ;
268
                }
269
            }
270
        }
271
        else
272
            $sErrorNumber = '202' ;
273
    }
274
    else
275
        $sErrorNumber = '202' ;
276
 
277
    $sFileUrl = CombinePaths( GetResourceTypePath( $resourceType, $sCommand ) , $currentFolder ) ;
278
    $sFileUrl = CombinePaths( $sFileUrl, $sFileName ) ;
279
 
280
    if($CKEcallback == '')
281
    {
282
        SendUploadResults( $sErrorNumber, $sFileUrl, $sFileName ) ;
283
    }
284
    else
285
    {
286
        //issue the CKEditor Callback
287
        SendCKEditorResults ($sErrorNumber, $CKEcallback, $sFileUrl, $sFileName);
288
    }
289
    exit ;
290
}
(2-2/8)