source: branches/version-2_13-dev/data/class/SC_UploadFile.php @ 23328

Revision 23328, 19.9 KB checked in by Seasoft, 10 years ago (diff)

#2495 (ファイルアップロード時のエラーチェックが不十分)
#2448 (typo修正・ソース整形・ソースコメントの改善 for 2.13.2)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24/* アップロードファイル管理クラス */
25class SC_UploadFile
26{
27    public $temp_dir;
28    public $save_dir;
29
30    /** ファイルinputタグのname */
31    public $keyname = array();
32
33    /** 横サイズ */
34    public $width = array();
35
36    /** 縦サイズ */
37    public $height = array();
38
39    /** 指定する拡張子 */
40    public $arrExt = array();
41
42    /** 保存されたファイル名 */
43    public $temp_file = array();
44
45    /** DBから読み出したファイル名 */
46    public $save_file = array();
47
48    /** 項目名 */
49    public $disp_name = array();
50
51    /** 制限サイズ */
52    public $size = array();
53
54    /** 必須の場合:true */
55    public $necessary = array();
56
57    /** 画像の場合:true */
58    public $image = array();
59
60    // ファイル管理クラス
61    public function __construct($temp_dir, $save_dir)
62    {
63        $this->temp_dir = rtrim($temp_dir, '/') . '/';
64        $this->save_dir = rtrim($save_dir, '/') . '/';
65        $this->file_max = 0;
66    }
67
68    // ファイル情報追加
69    public function addFile($disp_name, $keyname, $arrExt, $size, $necessary=false, $width=0, $height=0, $image=true)
70    {
71        $this->disp_name[] = $disp_name;
72        $this->keyname[] = $keyname;
73        $this->width[] = $width;
74        $this->height[] = $height;
75        $this->arrExt[] = $arrExt;
76        $this->size[] = $size;
77        $this->necessary[] = $necessary;
78        $this->image[] = $image;
79    }
80    // サムネイル画像の作成
81    public function makeThumb($src_file, $width, $height, $dst_file)
82    {
83        $objThumb = new gdthumb();
84        $ret = $objThumb->Main($src_file, $width, $height, $dst_file);
85
86        if ($ret[0] != 1) {
87            // エラーメッセージの表示
88            echo $ret[1];
89            exit;
90        }
91
92        return basename($ret[1]);
93    }
94
95    // アップロードされたファイルを保存する。
96    public function makeTempFile($keyname, $rename = IMAGE_RENAME)
97    {
98        $objErr = new SC_CheckError_Ex();
99        $cnt = 0;
100        $arrKeyname = array_flip($this->keyname);
101
102        if ($_FILES[$keyname]['error'] != 0) {
103            $objErr->arrErr[$keyname] .= '※ ' . $this->disp_name[$arrKeyname[$keyname]] . 'のアップロードに失敗しました。';
104            $objErr->arrErr[$keyname] .= 'エラーコードは[' . $_FILES[$keyname]['error'] . ']です。';
105            $objErr->arrErr[$keyname] .= '<br />';
106        } else {
107            foreach ($this->keyname as $val) {
108                // 一致したキーのファイルに情報を保存する。
109                if ($val == $keyname) {
110                    // 拡張子チェック
111                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->arrExt[$cnt]), array('FILE_EXT_CHECK'));
112                    // ファイルサイズチェック
113                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->size[$cnt]), array('FILE_SIZE_CHECK'));
114                    // エラーがない場合
115                    if (!isset($objErr->arrErr[$keyname])) {
116                        // 画像ファイルの場合
117                        if ($this->image[$cnt]) {
118                            // 保存用の画像名を取得する
119                            $dst_file = $this->lfGetTmpImageName($rename, $keyname);
120                            $this->temp_file[$cnt] = $this->makeThumb($_FILES[$keyname]['tmp_name'], $this->width[$cnt], $this->height[$cnt], $dst_file);
121                        // 画像ファイル以外の場合
122                        } else {
123                            // 一意なファイル名を作成する。
124                            if ($rename) {
125                                $uniqname = date('mdHi') . '_' . uniqid('').'.';
126                                $this->temp_file[$cnt] = preg_replace("/^.*\./", $uniqname, $_FILES[$keyname]['name']);
127                            } else {
128                                $this->temp_file[$cnt] = $_FILES[$keyname]['name'];
129                            }
130                            if (move_uploaded_file($_FILES[$keyname]['tmp_name'], $this->temp_dir . $this->temp_file[$cnt])) {
131                                GC_Utils_Ex::gfPrintLog($_FILES[$keyname]['name'].' -> '. $this->temp_dir . $this->temp_file[$cnt]);
132                            } else {
133                                $objErr->arrErr[$keyname] = '※ ファイルのアップロードに失敗しました。<br />';
134                                GC_Utils_Ex::gfPrintLog('File Upload Error!: ' . $_FILES[$keyname]['name'].' -> '. $this->temp_dir . $this->temp_file[$cnt]);
135                            }
136                        }
137                    }
138                }
139                $cnt++;
140            }
141        }
142
143        return $objErr->arrErr[$keyname];
144    }
145
146    // アップロードされたダウンロードファイルを保存する。
147    public function makeTempDownFile($keyname='down_file')
148    {
149        $objErr = new SC_CheckError_Ex();
150        $cnt = 0;
151        $arrKeyname = array_flip($this->keyname);
152        if (!($_FILES[$keyname]['size'] > 0)) {
153            $objErr->arrErr[$keyname] = '※ ' . $this->disp_name[$arrKeyname[$keyname]] . 'がアップロードされていません。(ファイルがアップロードできない場合は、.htaccessファイルのphp_value upload_max_filesizeを調整してください)<br />';
154        } else {
155            foreach ($this->keyname as $val) {
156                // 一致したキーのファイルに情報を保存する。
157                if ($val == $keyname) {
158                    // 拡張子チェック
159                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->arrExt[$cnt]), array('FILE_EXT_CHECK'));
160                    // ファイルサイズチェック
161                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->size[$cnt]), array('FILE_SIZE_CHECK'));
162                    // エラーがない場合
163                    if (!isset($objErr->arrErr[$keyname])) {
164                        // 一意なファイル名を作成する。
165                        $uniqname = date('mdHi') . '_' . uniqid('').'.';
166                        $this->temp_file[$cnt] = preg_replace("/^.*\./", $uniqname, $_FILES[$keyname]['name']);
167                        $result  = copy($_FILES[$keyname]['tmp_name'], $this->temp_dir . $this->temp_file[$cnt]);
168                        GC_Utils_Ex::gfPrintLog($result.' -> '. $this->temp_dir . $this->temp_file[$cnt]);
169                        SC_Utils_Ex::extendTimeOut();
170                    }
171                }
172                $cnt++;
173            }
174        }
175
176        return $objErr->arrErr[$keyname];
177    }
178
179    // 画像を削除する。
180    public function deleteFile($keyname)
181    {
182        $objImage = new SC_Image_Ex($this->temp_dir);
183        $cnt = 0;
184        foreach ($this->keyname as $val) {
185            if ($val == $keyname) {
186                // 一時ファイルの場合削除する。
187                if ($this->temp_file[$cnt] != '') {
188                    $objImage->deleteImage($this->temp_file[$cnt], $this->temp_dir);
189                }
190                $this->temp_file[$cnt] = '';
191                $this->save_file[$cnt] = '';
192            }
193            $cnt++;
194        }
195    }
196
197    // 画像を削除する。
198    public function deleteKikakuFile($keyname)
199    {
200        $objImage = new SC_Image_Ex($this->temp_dir);
201        $cnt = 0;
202        foreach ($this->keyname as $val) {
203            if ($val == $keyname) {
204                // 一時ファイルの場合削除する。
205                if ($this->temp_file[$cnt] != '') {
206                    $objImage->deleteImage($this->temp_file[$cnt], $this->temp_dir);
207                }
208                $this->temp_file[$cnt] = '';
209                //$this->save_file[$cnt] = '';
210            }
211            $cnt++;
212        }
213    }
214
215    // 一時ファイルパスを取得する。
216    public function getTempFilePath($keyname)
217    {
218        $cnt = 0;
219        $filepath = '';
220        foreach ($this->keyname as $val) {
221            if ($val == $keyname) {
222                if ($this->temp_file[$cnt] != '') {
223                    $filepath = $this->temp_dir . $this->temp_file[$cnt];
224                }
225            }
226            $cnt++;
227        }
228
229        return $filepath;
230    }
231
232    // 一時ファイルを保存ディレクトリに移す
233    public function moveTempFile()
234    {
235        $objImage = new SC_Image_Ex($this->temp_dir);
236
237        for ($cnt = 0; $cnt < count($this->keyname); $cnt++) {
238            if (isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != '') {
239                $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
240
241                // すでに保存ファイルがあった場合は削除する。
242                if (isset($this->save_file[$cnt])
243                    && $this->save_file[$cnt] != ''
244                    && !preg_match('|^sub/|', $this->save_file[$cnt])
245                ) {
246                    $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
247                }
248            }
249        }
250    }
251
252    // ダウンロード一時ファイルを保存ディレクトリに移す
253    public function moveTempDownFile()
254    {
255        $objImage = new SC_Image_Ex($this->temp_dir);
256        for ($cnt = 0; $cnt < count($this->keyname); $cnt++) {
257            if (isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != '') {
258                $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
259                // すでに保存ファイルがあった場合は削除する。
260                if (isset($this->save_file[$cnt])
261                    && $this->save_file[$cnt] != ''
262                    && !preg_match('|^sub/|', $this->save_file[$cnt])
263                ) {
264                    $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
265                }
266            }
267        }
268    }
269
270    // HIDDEN用のファイル名配列を返す
271    public function getHiddenFileList()
272    {
273        $cnt = 0;
274        $arrRet = array();
275        foreach ($this->keyname as $val) {
276            if (isset($this->temp_file[$cnt])) {
277                $arrRet['temp_' . $val] = $this->temp_file[$cnt];
278            }
279            if (isset($this->save_file[$cnt]) && $this->save_file[$cnt] != '') {
280                $arrRet['save_' . $val] = $this->save_file[$cnt];
281            }
282            $cnt++;
283        }
284
285        return $arrRet;
286    }
287
288    // HIDDENで送られてきたファイル名を取得する
289    public function setHiddenFileList($arrPOST)
290    {
291        $cnt = 0;
292        foreach ($this->keyname as $val) {
293            $key = 'temp_' . $val;
294            if (isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
295                $this->temp_file[$cnt] = $arrPOST[$key];
296            }
297            $key = 'save_' . $val;
298            if (isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
299                $this->save_file[$cnt] = $arrPOST[$key];
300            }
301            $cnt++;
302        }
303    }
304
305    public function setHiddenKikakuFileList($arrPOST)
306    {
307        $cnt = 0;
308        foreach ($this->keyname as $val) {
309            $key = 'temp_' . $val;
310            if (isset($arrPOST[$key])) {
311                $this->temp_file[$cnt] = $arrPOST[$key];
312            }
313            $key = 'save_' . $val;
314            if (isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
315                $this->save_file[$cnt] = $arrPOST[$key];
316            }
317            $cnt++;
318        }
319    }
320
321    // フォームに渡す用のファイル情報配列を返す
322    public function getFormFileList($temp_url, $save_url, $real_size = false)
323    {
324        $arrRet = array();
325        $cnt = 0;
326        foreach ($this->keyname as $val) {
327            if (isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != '') {
328                // パスのスラッシュ/が連続しないようにする。
329                $arrRet[$val]['filepath'] = rtrim($temp_url, '/') . '/' . $this->temp_file[$cnt];
330
331                $arrRet[$val]['real_filepath'] = $this->temp_dir . $this->temp_file[$cnt];
332            } elseif (isset($this->save_file[$cnt]) && $this->save_file[$cnt] != '') {
333                // パスのスラッシュ/が連続しないようにする。
334                $arrRet[$val]['filepath'] = rtrim($save_url, '/') . '/' . $this->save_file[$cnt];
335
336                $arrRet[$val]['real_filepath'] = $this->save_dir . $this->save_file[$cnt];
337            }
338            if (isset($arrRet[$val]['filepath']) && !empty($arrRet[$val]['filepath'])) {
339                if ($real_size) {
340                    if (is_file($arrRet[$val]['real_filepath'])) {
341                        list($width, $height) = getimagesize($arrRet[$val]['real_filepath']);
342                    }
343                    // ファイル横幅
344                    $arrRet[$val]['width'] = $width;
345                    // ファイル縦幅
346                    $arrRet[$val]['height'] = $height;
347                } else {
348                    // ファイル横幅
349                    $arrRet[$val]['width'] = $this->width[$cnt];
350                    // ファイル縦幅
351                    $arrRet[$val]['height'] = $this->height[$cnt];
352                }
353                // 表示名
354                $arrRet[$val]['disp_name'] = $this->disp_name[$cnt];
355            }
356            $cnt++;
357        }
358
359        return $arrRet;
360    }
361
362    // フォームに渡す用のダウンロードファイル情報を返す
363    public function getFormDownFile()
364    {
365        $arrRet = '';
366        for ($cnt = 0; $cnt < count($this->keyname); $cnt++) {
367            if (isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != '') {
368                $arrRet = $this->temp_file[$cnt];
369            } elseif (isset($this->save_file[$cnt]) && $this->save_file[$cnt] != '') {
370                $arrRet = $this->save_file[$cnt];
371            }
372        }
373
374        return $arrRet;
375    }
376    public function getFormKikakuDownFile()
377    {
378        $arrRet = array();
379        $cnt = 0;
380        foreach ($this->keyname as $val) {
381            if (isset($this->temp_file[$cnt])) {
382                $arrRet[$val] = $this->temp_file[$cnt];
383            } elseif (isset($this->save_file[$cnt]) && $this->save_file[$cnt] != '') {
384                $arrRet[$val] = $this->save_file[$cnt];
385            }
386            $cnt++;
387        }
388
389        return $arrRet;
390    }
391
392    // DB保存用のファイル名配列を返す
393    public function getDBFileList()
394    {
395        $cnt = 0;
396        $dbFileList = array();
397        foreach ($this->keyname as $val) {
398            if (isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != '') {
399                $dbFileList[$val] = $this->temp_file[$cnt];
400            } else {
401                $dbFileList[$val] = isset($this->save_file[$cnt]) ? $this->save_file[$cnt] : '';
402            }
403            $cnt++;
404        }
405
406        return $dbFileList;
407    }
408
409    // DBで保存されたファイル名配列をセットする
410    public function setDBFileList($arrVal)
411    {
412        $cnt = 0;
413        foreach ($this->keyname as $val) {
414            if (isset($arrVal[$val]) && $arrVal[$val] != '') {
415                $this->save_file[$cnt] = $arrVal[$val];
416            }
417            $cnt++;
418        }
419    }
420
421    // DBで保存されたダウンロードファイル名をセットする
422    public function setDBDownFile($arrVal)
423    {
424        if (isset($arrVal['down_realfilename']) && $arrVal['down_realfilename'] != '') {
425            $this->save_file[0] = $arrVal['down_realfilename'];
426        }
427    }
428
429    // DBで保存されたダウンロードファイル名をセットする(setDBDownFileと統合予定)
430    public function setPostFileList($arrPost)
431    {
432        for ($cnt = 0;$cnt < count($this->keyname); $cnt++) {
433            if (isset($arrPost['temp_down_realfilename:' . ($cnt+1)])) {
434                $this->temp_file[$cnt] = $arrPost['temp_down_realfilename:' . ($cnt+1)];
435            }
436        }
437    }
438
439    // 画像をセットする
440    public function setDBImageList($arrVal)
441    {
442        $cnt = 0;
443        foreach ($this->keyname as $val) {
444            if ($arrVal[$val] != '' && $val == 'tv_products_image') {
445                $this->save_file[$cnt] = $arrVal[$val];
446            }
447            $cnt++;
448        }
449    }
450
451    // DB上のファイルの内削除要求があったファイルを削除する。
452    public function deleteDBFile($arrVal)
453    {
454        $objImage = new SC_Image_Ex($this->temp_dir);
455        $cnt = 0;
456        foreach ($this->keyname as $val) {
457            if ($arrVal[$val] != '') {
458                if ($this->save_file[$cnt] == '' && !preg_match('|^sub/|', $arrVal[$val])) {
459                    $objImage->deleteImage($arrVal[$val], $this->save_dir);
460                }
461            }
462            $cnt++;
463        }
464    }
465
466    // DB上のダウンロードファイルの内削除要求があったファイルを削除する。
467    public function deleteDBDownFile($arrVal)
468    {
469        $objImage = new SC_Image_Ex($this->temp_dir);
470        $cnt = 0;
471        if ($arrVal['down_realfilename'] != '') {
472            if ($this->save_file[$cnt] == '' && !preg_match('|^sub/|', $arrVal['down_realfilename'])) {
473                $objImage->deleteImage($arrVal['down_realfilename'], $this->save_dir);
474            }
475        }
476    }
477
478    // 必須判定
479    public function checkExists($keyname = '')
480    {
481        $cnt = 0;
482        $arrRet = array();
483        foreach ($this->keyname as $val) {
484            if ($val == $keyname || $keyname == '') {
485                // 必須であればエラーチェック
486                if ($this->necessary[$cnt] == true) {
487                    if (!isset($this->save_file[$cnt])) $this->save_file[$cnt] = '';
488                    if (!isset($this->temp_file[$cnt])) $this->temp_file[$cnt] = '';
489                    if ($this->save_file[$cnt] == ''
490                        && $this->temp_file[$cnt] == ''
491                    ) {
492                        $arrRet[$val] = '※ ' . $this->disp_name[$cnt] . 'がアップロードされていません。<br>';
493                    }
494                }
495            }
496            $cnt++;
497        }
498
499        return $arrRet;
500    }
501
502    // 拡大率を指定して画像保存
503    public function saveResizeImage($keyname, $to_w, $to_h)
504    {
505        $path = '';
506
507        // keynameの添付ファイルを取得
508        $arrImageKey = array_flip($this->keyname);
509        $file = $this->temp_file[$arrImageKey[$keyname]];
510        $filepath = $this->temp_dir . $file;
511
512        $path = $this->makeThumb($filepath, $to_w, $to_h);
513
514        // ファイル名だけ返す
515        return basename($path);
516    }
517
518    /**
519     * 一時保存用のファイル名を生成する
520     *
521     * @param  string $rename
522     * @param  int    $keyname
523     * @return string
524     */
525    public function lfGetTmpImageName($rename, $keyname = '', $uploadfile = '')
526    {
527        if ($rename === true) {
528            // 一意なIDを取得し、画像名をリネームし保存
529            $uniqname = date('mdHi') . '_' . uniqid('');
530        } else {
531            // アップロードした画像名で保存
532            $uploadfile = strlen($uploadfile) > 0 ? $uploadfile : $_FILES[$keyname]['name'];
533            $uniqname =  preg_replace('/(.+)\.(.+?)$/','$1', $uploadfile);
534        }
535        $dst_file = $this->temp_dir . $uniqname;
536
537        return $dst_file;
538    }
539}
Note: See TracBrowser for help on using the repository browser.