OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / scripts / fckeditor2.6.6 / editor / plugins / FileBrowser_Thumbnail / connectors / php / Commands / FileUpload.php
1 <?php
2 /*
3 PHP Connector for the FCKEditor v2 File Manager
4 Written By Grant French, UK, Sept 2004
5 http://www.mcpuk.net
6
7 FCKEditor - By Frederico Caldeira Knabben
8 http://www.fckeditor.net
9
10 File Description:
11 Implements the FileUpload command,
12 Checks the file uploaded is allowed, 
13 then moves it to the user data area. 
14
15 2008.6.8        modified by naoki hirata
16 2009.2.10       mbstringなしでも実行できるように修正 by naoki hirata
17 */
18
19 class FileUpload extends command {
20         function run() {
21                 $disp = $this->_run();
22                 
23                 header ("content-type: text/html; charset=UTF-8");
24 ?>
25 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
26 <html>
27     <head>
28         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
29         <title>Upload Complete</title>
30     </head>
31     <body>
32         <script type="text/javascript">
33 window.parent.frames['frmUpload'].OnUploadCompleted(<?php echo $disp; ?>) ;
34         </script>
35     </body>
36 </html>
37 <?php
38         }
39         
40         function _run() {
41                 global $gOpeLogManager;
42                 
43                 if (sizeof($_FILES) == 0){              // アップロードファイルがないとき
44                         return "202";// fixed by naoki on 2008/12/9.
45                 }
46                 // エラーコード取得
47                 $errCode = $_FILES['NewFile']['error'];
48                 if($errCode !== UPLOAD_ERR_OK){
49                         $gOpeLogManager->writeError(__METHOD__, 'ファイルブラウザからのアップロードに失敗しました。エラーコード: ' . $errCode, 3001);
50                 }
51                 
52                 $typeconfig = $this->fckphp_config['ResourceAreas'][$this->type];
53                 
54                 if (! array_key_exists("NewFile", $_FILES)) {
55                         return "203";   //No parametor
56                 }
57                 
58                 if ($_FILES['NewFile']['size'] > ($typeconfig['MaxSize'] * 1024)) {
59                         return "204,'{$typeconfig['MaxSize']}'";        //Too big
60                 }
61                 
62                 $filename = $_FILES['NewFile']['name'];
63                 
64                 if ($filename == '') {
65                         return 101;
66                 }
67                 
68                 if (function_exists('mb_ereg')){
69                         if (mb_ereg($this->fckphp_config['DisableName'], $filename)) {
70                                 return 101;
71                         }
72                 } else {
73                         if (ereg($this->fckphp_config['DisableName'], $filename)) {
74                                 return 101;
75                         }
76                 }
77                 
78                 if (function_exists('mb_ereg')){
79                         if (mb_ereg($this->fckphp_config['DisableChars'], $filename)) {
80                                 return 102;
81                         }
82                 } else {
83                         if (ereg($this->fckphp_config['DisableChars'], $filename)) {
84                                 return 102;
85                         }
86                 }
87                 
88                 if (function_exists('mb_strrpos')){
89                         $lastdot = mb_strrpos($filename, ".");
90                         if ($lastdot === false) {
91                                 return "205";   //No ext
92                         }
93                 
94                         $ext = mb_substr($filename, ($lastdot + 1));
95                         $filename = mb_substr($filename, 0, $lastdot);
96                 } else {
97                         $lastdot = strrpos($filename, ".");
98                         if ($lastdot === false) {
99                                 return "205";   //No ext
100                         }
101                 
102                         $ext = substr($filename, ($lastdot + 1));
103                         $filename = substr($filename, 0, $lastdot);
104                 }
105                 
106                 if (! in_array(strtolower($ext), $typeconfig['AllowedExtensions'])) {
107                         return "206,'{$ext}'";  //Disallowed file extension
108                 }
109                 
110                 if (! $this->mapFolder($this->actual_cwd)) {
111                         return 110;     //Unknown error
112                 }
113                 
114                 $test = 0;
115                 $dirSizes = array();
116                 $globalSize = 0;
117                 
118                 if ($this->fckphp_config['DiskQuota']['Global'] != -1) {
119                         foreach ($this->fckphp_config['ResourceTypes'] as $resType) {
120                                 //$path = "{$this->fckphp_config['UserFilesPath']}/{$resType}";
121                                 $path = $this->fckphp_config['UserFilesPath'] . '/' . strtolower($resType);             // ディレクトリ名を英小文字に設定 by naoki
122                                 
123                                 if (is_dir($this->path($path))) {
124                                         $dirSizes[$resType] = $this->getDirSize($path);
125                                         
126                                         if ($dirSizes[$resType] === false) {
127                                                 return "207,'{$resType}'";      //Unable to determine the size of a folder
128                                         }
129                                 } else {
130                                         $dirSizes[$resType] = 0;
131                                 }
132                                 
133                                 $globalSize += $dirSizes[$resType];
134                         }
135                         
136                         $globalSize += $_FILES['NewFile']['size'];
137                         
138                         if ($globalSize > ($this->fckphp_config['DiskQuota']['Global'] * 1024 * 1024)) {
139                                 return "208,'{$this->fckphp_config['DiskQuota']['Global']}'";   //You are over the global disk quota
140                         }
141                 }
142                 
143                 if ($typeconfig['DiskQuota'] != -1) {
144                         if ($this->fckphp_config['DiskQuota']['Global'] == -1) {
145                                 //$path = "{$this->fckphp_config['UserFilesPath']}/{$this->type}";
146                                 $path = $this->fckphp_config['UserFilesPath'] . '/' . strtolower($this->type);          // ディレクトリ名を英小文字に設定 by naoki
147
148                                 if (is_dir($this->path($path))) {
149                                         $dirSizes[$this->type] = $this->getDirSize($path);
150                                         
151                                         if ($dirSizes[$this->type] === false) {
152                                                 return "207,'{$this->type}'";   //Unable to determine the size of a folder
153                                         }
154                                 } else {
155                                         $dirSizes[$this->type] = 0;
156                                 }
157                         }
158                         
159                         if (($dirSizes[$this->type] + $_FILES['NewFile']['size']) > ($typeconfig['DiskQuota'] * 1024 * 1024)) {
160                                 return "209,'{$typeconfig['DiskQuota']}'";      //You are over the disk quota for this resource type
161                         }
162                 }
163                 
164                 $unique = $filename;
165                 $done = false;
166                 
167                 for ($i = 1; $i < $this->fckphp_config['AvailableMax']; $i++) {
168                         if (! file_exists($this->path("{$this->actual_cwd}{$unique}.{$ext}"))) {
169                                 $done = true;
170                                 break;
171                         }
172                         
173                         $unique = "{$filename}({$i})";
174                 }
175                 
176                 if (! $done) {
177                         return "210";
178                 }
179                 
180                 //Upload file
181                 if (! is_uploaded_file($_FILES['NewFile']['tmp_name'])) {
182                         return "211";
183                 }
184                 
185                 if (! move_uploaded_file($_FILES['NewFile']['tmp_name'], $this->path("{$this->actual_cwd}{$unique}.{$ext}"))) {
186                         $path = $this->path("{$this->actual_cwd}");
187                         //$gOpeLogManager->writeError(__METHOD__, 'ファイルのアップロードに失敗しました。ディレクトリに書き込み権限がない可能性があります。ディレクトリ: ' . $path, 3001);
188                         return "212,'{$path}'";
189                 }
190                 
191                 chmod($this->path("{$this->actual_cwd}{$unique}.{$ext}"), 0777);
192
193                 return ($unique == $filename) ? "0" : "1,'". "{$unique}.{$ext}'";
194         }
195         
196         function getDirSize($dir) {
197                 if (($dh = @opendir($this->path($dir))) === false) {
198                         return false;
199                 }
200                 
201                 $dirSize=0;
202                 
203                 while ($file = @readdir($dh)) {
204                         if ($file == ".") continue;
205                         if ($file == "..") continue;
206                         
207                         if (function_exists('mb_convert_encoding')){
208                                 $file = mb_convert_encoding($file, $this->fckphp_config['FileEncoding'], 'UTF-8');
209                         }
210                         
211                         if (is_dir($this->path("{$dir}/{$file}"))) {
212                                 $tmp_dirSize = $this->getDirSize("{$dir}/{$file}");
213                                 
214                                 if ($tmp_dirSize === false) {
215                                         @closedir($dh);
216                                         return false;
217                                 }
218                                 
219                                 $dirSize += $tmp_dirSize;
220                                 
221                                 continue;
222                         }
223                         
224                         $dirSize += filesize($this->path("{$dir}/{$file}"));
225                 }
226                 
227                 @closedir($dh);
228                 
229                 return $dirSize;
230         }
231 }
232 ?>