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

Revision 23485, 20.7 KB checked in by shutta, 10 years ago (diff)

#2545 ファイルアップロード時のエラーメッセージが不適切
ファイルのアップロードのエラーチェック処理を分離。

  • 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        $check = $this->checkUploadError($keyname, $objErr);
101        if ($check) {
102            foreach ($this->keyname as $val) {
103                // 一致したキーのファイルに情報を保存する。
104                if ($val == $keyname) {
105                    // 拡張子チェック
106                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->arrExt[$cnt]), array('FILE_EXT_CHECK'));
107                    // ファイルサイズチェック
108                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->size[$cnt]), array('FILE_SIZE_CHECK'));
109                    // エラーがない場合
110                    if (!isset($objErr->arrErr[$keyname])) {
111                        // 画像ファイルの場合
112                        if ($this->image[$cnt]) {
113                            // 保存用の画像名を取得する
114                            $dst_file = $this->lfGetTmpImageName($rename, $keyname);
115                            $this->temp_file[$cnt] = $this->makeThumb($_FILES[$keyname]['tmp_name'], $this->width[$cnt], $this->height[$cnt], $dst_file);
116                        // 画像ファイル以外の場合
117                        } else {
118                            // 一意なファイル名を作成する。
119                            if ($rename) {
120                                $uniqname = date('mdHi') . '_' . uniqid('').'.';
121                                $this->temp_file[$cnt] = preg_replace("/^.*\./", $uniqname, $_FILES[$keyname]['name']);
122                            } else {
123                                $this->temp_file[$cnt] = $_FILES[$keyname]['name'];
124                            }
125                            if (move_uploaded_file($_FILES[$keyname]['tmp_name'], $this->temp_dir . $this->temp_file[$cnt])) {
126                                GC_Utils_Ex::gfPrintLog($_FILES[$keyname]['name'].' -> '. $this->temp_dir . $this->temp_file[$cnt]);
127                            } else {
128                                $objErr->arrErr[$keyname] = '※ ファイルのアップロードに失敗しました。<br />';
129                                GC_Utils_Ex::gfPrintLog('File Upload Error!: ' . $_FILES[$keyname]['name'].' -> '. $this->temp_dir . $this->temp_file[$cnt]);
130                            }
131                        }
132                    }
133                }
134                $cnt++;
135            }
136        }
137
138        return $objErr->arrErr[$keyname];
139    }
140
141    // アップロードされたダウンロードファイルを保存する。
142    public function makeTempDownFile($keyname='down_file')
143    {
144        $objErr = new SC_CheckError_Ex();
145        $cnt = 0;
146        $check = $this->checkUploadError($keyname, $objErr);
147        if ($check) {
148            foreach ($this->keyname as $val) {
149                // 一致したキーのファイルに情報を保存する。
150                if ($val == $keyname) {
151                    // 拡張子チェック
152                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->arrExt[$cnt]), array('FILE_EXT_CHECK'));
153                    // ファイルサイズチェック
154                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->size[$cnt]), array('FILE_SIZE_CHECK'));
155                    // エラーがない場合
156                    if (!isset($objErr->arrErr[$keyname])) {
157                        // 一意なファイル名を作成する。
158                        $uniqname = date('mdHi') . '_' . uniqid('').'.';
159                        $this->temp_file[$cnt] = preg_replace("/^.*\./", $uniqname, $_FILES[$keyname]['name']);
160                        $result  = copy($_FILES[$keyname]['tmp_name'], $this->temp_dir . $this->temp_file[$cnt]);
161                        GC_Utils_Ex::gfPrintLog($result.' -> '. $this->temp_dir . $this->temp_file[$cnt]);
162                        SC_Utils_Ex::extendTimeOut();
163                    }
164                }
165                $cnt++;
166            }
167        }
168
169        return $objErr->arrErr[$keyname];
170    }
171
172    // 画像を削除する。
173    public function deleteFile($keyname)
174    {
175        $objImage = new SC_Image_Ex($this->temp_dir);
176        $cnt = 0;
177        foreach ($this->keyname as $val) {
178            if ($val == $keyname) {
179                // 一時ファイルの場合削除する。
180                if ($this->temp_file[$cnt] != '') {
181                    $objImage->deleteImage($this->temp_file[$cnt], $this->temp_dir);
182                }
183                $this->temp_file[$cnt] = '';
184                $this->save_file[$cnt] = '';
185            }
186            $cnt++;
187        }
188    }
189
190    // 画像を削除する。
191    public function deleteKikakuFile($keyname)
192    {
193        $objImage = new SC_Image_Ex($this->temp_dir);
194        $cnt = 0;
195        foreach ($this->keyname as $val) {
196            if ($val == $keyname) {
197                // 一時ファイルの場合削除する。
198                if ($this->temp_file[$cnt] != '') {
199                    $objImage->deleteImage($this->temp_file[$cnt], $this->temp_dir);
200                }
201                $this->temp_file[$cnt] = '';
202                //$this->save_file[$cnt] = '';
203            }
204            $cnt++;
205        }
206    }
207
208    // 一時ファイルパスを取得する。
209    public function getTempFilePath($keyname)
210    {
211        $cnt = 0;
212        $filepath = '';
213        foreach ($this->keyname as $val) {
214            if ($val == $keyname) {
215                if ($this->temp_file[$cnt] != '') {
216                    $filepath = $this->temp_dir . $this->temp_file[$cnt];
217                }
218            }
219            $cnt++;
220        }
221
222        return $filepath;
223    }
224
225    // 一時ファイルを保存ディレクトリに移す
226    public function moveTempFile()
227    {
228        $objImage = new SC_Image_Ex($this->temp_dir);
229
230        for ($cnt = 0; $cnt < count($this->keyname); $cnt++) {
231            if (isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != '') {
232                $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
233
234                // すでに保存ファイルがあった場合は削除する。
235                if (isset($this->save_file[$cnt])
236                    && $this->save_file[$cnt] != ''
237                    && !preg_match('|^sub/|', $this->save_file[$cnt])
238                ) {
239                    $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
240                }
241            }
242        }
243    }
244
245    // ダウンロード一時ファイルを保存ディレクトリに移す
246    public function moveTempDownFile()
247    {
248        $objImage = new SC_Image_Ex($this->temp_dir);
249        for ($cnt = 0; $cnt < count($this->keyname); $cnt++) {
250            if (isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != '') {
251                $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
252                // すでに保存ファイルがあった場合は削除する。
253                if (isset($this->save_file[$cnt])
254                    && $this->save_file[$cnt] != ''
255                    && !preg_match('|^sub/|', $this->save_file[$cnt])
256                ) {
257                    $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
258                }
259            }
260        }
261    }
262
263    // HIDDEN用のファイル名配列を返す
264    public function getHiddenFileList()
265    {
266        $cnt = 0;
267        $arrRet = array();
268        foreach ($this->keyname as $val) {
269            if (isset($this->temp_file[$cnt])) {
270                $arrRet['temp_' . $val] = $this->temp_file[$cnt];
271            }
272            if (isset($this->save_file[$cnt]) && $this->save_file[$cnt] != '') {
273                $arrRet['save_' . $val] = $this->save_file[$cnt];
274            }
275            $cnt++;
276        }
277
278        return $arrRet;
279    }
280
281    // HIDDENで送られてきたファイル名を取得する
282    public function setHiddenFileList($arrPOST)
283    {
284        $cnt = 0;
285        foreach ($this->keyname as $val) {
286            $key = 'temp_' . $val;
287            if (isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
288                $this->temp_file[$cnt] = $arrPOST[$key];
289            }
290            $key = 'save_' . $val;
291            if (isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
292                $this->save_file[$cnt] = $arrPOST[$key];
293            }
294            $cnt++;
295        }
296    }
297
298    public function setHiddenKikakuFileList($arrPOST)
299    {
300        $cnt = 0;
301        foreach ($this->keyname as $val) {
302            $key = 'temp_' . $val;
303            if (isset($arrPOST[$key])) {
304                $this->temp_file[$cnt] = $arrPOST[$key];
305            }
306            $key = 'save_' . $val;
307            if (isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
308                $this->save_file[$cnt] = $arrPOST[$key];
309            }
310            $cnt++;
311        }
312    }
313
314    // フォームに渡す用のファイル情報配列を返す
315    public function getFormFileList($temp_url, $save_url, $real_size = false)
316    {
317        $arrRet = array();
318        $cnt = 0;
319        foreach ($this->keyname as $val) {
320            if (isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != '') {
321                // パスのスラッシュ/が連続しないようにする。
322                $arrRet[$val]['filepath'] = rtrim($temp_url, '/') . '/' . $this->temp_file[$cnt];
323
324                $arrRet[$val]['real_filepath'] = $this->temp_dir . $this->temp_file[$cnt];
325            } elseif (isset($this->save_file[$cnt]) && $this->save_file[$cnt] != '') {
326                // パスのスラッシュ/が連続しないようにする。
327                $arrRet[$val]['filepath'] = rtrim($save_url, '/') . '/' . $this->save_file[$cnt];
328
329                $arrRet[$val]['real_filepath'] = $this->save_dir . $this->save_file[$cnt];
330            }
331            if (isset($arrRet[$val]['filepath']) && !empty($arrRet[$val]['filepath'])) {
332                if ($real_size) {
333                    if (is_file($arrRet[$val]['real_filepath'])) {
334                        list($width, $height) = getimagesize($arrRet[$val]['real_filepath']);
335                    }
336                    // ファイル横幅
337                    $arrRet[$val]['width'] = $width;
338                    // ファイル縦幅
339                    $arrRet[$val]['height'] = $height;
340                } else {
341                    // ファイル横幅
342                    $arrRet[$val]['width'] = $this->width[$cnt];
343                    // ファイル縦幅
344                    $arrRet[$val]['height'] = $this->height[$cnt];
345                }
346                // 表示名
347                $arrRet[$val]['disp_name'] = $this->disp_name[$cnt];
348            }
349            $cnt++;
350        }
351
352        return $arrRet;
353    }
354
355    // フォームに渡す用のダウンロードファイル情報を返す
356    public function getFormDownFile()
357    {
358        $arrRet = '';
359        for ($cnt = 0; $cnt < count($this->keyname); $cnt++) {
360            if (isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != '') {
361                $arrRet = $this->temp_file[$cnt];
362            } elseif (isset($this->save_file[$cnt]) && $this->save_file[$cnt] != '') {
363                $arrRet = $this->save_file[$cnt];
364            }
365        }
366
367        return $arrRet;
368    }
369    public function getFormKikakuDownFile()
370    {
371        $arrRet = array();
372        $cnt = 0;
373        foreach ($this->keyname as $val) {
374            if (isset($this->temp_file[$cnt])) {
375                $arrRet[$val] = $this->temp_file[$cnt];
376            } elseif (isset($this->save_file[$cnt]) && $this->save_file[$cnt] != '') {
377                $arrRet[$val] = $this->save_file[$cnt];
378            }
379            $cnt++;
380        }
381
382        return $arrRet;
383    }
384
385    // DB保存用のファイル名配列を返す
386    public function getDBFileList()
387    {
388        $cnt = 0;
389        $dbFileList = array();
390        foreach ($this->keyname as $val) {
391            if (isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != '') {
392                $dbFileList[$val] = $this->temp_file[$cnt];
393            } else {
394                $dbFileList[$val] = isset($this->save_file[$cnt]) ? $this->save_file[$cnt] : '';
395            }
396            $cnt++;
397        }
398
399        return $dbFileList;
400    }
401
402    // DBで保存されたファイル名配列をセットする
403    public function setDBFileList($arrVal)
404    {
405        $cnt = 0;
406        foreach ($this->keyname as $val) {
407            if (isset($arrVal[$val]) && $arrVal[$val] != '') {
408                $this->save_file[$cnt] = $arrVal[$val];
409            }
410            $cnt++;
411        }
412    }
413
414    // DBで保存されたダウンロードファイル名をセットする
415    public function setDBDownFile($arrVal)
416    {
417        if (isset($arrVal['down_realfilename']) && $arrVal['down_realfilename'] != '') {
418            $this->save_file[0] = $arrVal['down_realfilename'];
419        }
420    }
421
422    // DBで保存されたダウンロードファイル名をセットする(setDBDownFileと統合予定)
423    public function setPostFileList($arrPost)
424    {
425        for ($cnt = 0;$cnt < count($this->keyname); $cnt++) {
426            if (isset($arrPost['temp_down_realfilename:' . ($cnt+1)])) {
427                $this->temp_file[$cnt] = $arrPost['temp_down_realfilename:' . ($cnt+1)];
428            }
429        }
430    }
431
432    // 画像をセットする
433    public function setDBImageList($arrVal)
434    {
435        $cnt = 0;
436        foreach ($this->keyname as $val) {
437            if ($arrVal[$val] != '' && $val == 'tv_products_image') {
438                $this->save_file[$cnt] = $arrVal[$val];
439            }
440            $cnt++;
441        }
442    }
443
444    // DB上のファイルの内削除要求があったファイルを削除する。
445    public function deleteDBFile($arrVal)
446    {
447        $objImage = new SC_Image_Ex($this->temp_dir);
448        $cnt = 0;
449        foreach ($this->keyname as $val) {
450            if ($arrVal[$val] != '') {
451                if ($this->save_file[$cnt] == '' && !preg_match('|^sub/|', $arrVal[$val])) {
452                    $objImage->deleteImage($arrVal[$val], $this->save_dir);
453                }
454            }
455            $cnt++;
456        }
457    }
458
459    // DB上のダウンロードファイルの内削除要求があったファイルを削除する。
460    public function deleteDBDownFile($arrVal)
461    {
462        $objImage = new SC_Image_Ex($this->temp_dir);
463        $cnt = 0;
464        if ($arrVal['down_realfilename'] != '') {
465            if ($this->save_file[$cnt] == '' && !preg_match('|^sub/|', $arrVal['down_realfilename'])) {
466                $objImage->deleteImage($arrVal['down_realfilename'], $this->save_dir);
467            }
468        }
469    }
470
471    // 必須判定
472    public function checkExists($keyname = '')
473    {
474        $cnt = 0;
475        $arrRet = array();
476        foreach ($this->keyname as $val) {
477            if ($val == $keyname || $keyname == '') {
478                // 必須であればエラーチェック
479                if ($this->necessary[$cnt] == true) {
480                    if (!isset($this->save_file[$cnt])) $this->save_file[$cnt] = '';
481                    if (!isset($this->temp_file[$cnt])) $this->temp_file[$cnt] = '';
482                    if ($this->save_file[$cnt] == ''
483                        && $this->temp_file[$cnt] == ''
484                    ) {
485                        $arrRet[$val] = '※ ' . $this->disp_name[$cnt] . 'がアップロードされていません。<br>';
486                    }
487                }
488            }
489            $cnt++;
490        }
491
492        return $arrRet;
493    }
494
495    // 拡大率を指定して画像保存
496    public function saveResizeImage($keyname, $to_w, $to_h)
497    {
498        $path = '';
499
500        // keynameの添付ファイルを取得
501        $arrImageKey = array_flip($this->keyname);
502        $file = $this->temp_file[$arrImageKey[$keyname]];
503        $filepath = $this->temp_dir . $file;
504
505        $path = $this->makeThumb($filepath, $to_w, $to_h);
506
507        // ファイル名だけ返す
508        return basename($path);
509    }
510
511    /**
512     * 一時保存用のファイル名を生成する
513     *
514     * @param  string $rename
515     * @param  int    $keyname
516     * @return string
517     */
518    public function lfGetTmpImageName($rename, $keyname = '', $uploadfile = '')
519    {
520        if ($rename === true) {
521            // 一意なIDを取得し、画像名をリネームし保存
522            $uniqname = date('mdHi') . '_' . uniqid('');
523        } else {
524            // アップロードした画像名で保存
525            $uploadfile = strlen($uploadfile) > 0 ? $uploadfile : $_FILES[$keyname]['name'];
526            $uniqname =  preg_replace('/(.+)\.(.+?)$/','$1', $uploadfile);
527        }
528        $dst_file = $this->temp_dir . $uniqname;
529
530        return $dst_file;
531    }
532
533    /**
534     * ファイルのアップロードのエラーを確認
535     *
536     * @param string $keyname ファイルinputタグのname
537     * @param object $objErr SC_CheckErrorインスタンス
538     * @return boolean
539     */
540    public function checkUploadError($keyname, SC_CheckError &$objErr)
541    {
542        $index = array_search($keyname, $this->keyname);
543
544        switch ($_FILES[$keyname]['error']) {
545            case UPLOAD_ERR_OK:
546                return true;
547                break;
548            case UPLOAD_ERR_NO_FILE:
549                $objErr->arrErr[$keyname] = '※ '
550                    . $this->disp_name[$index]
551                    . 'が選択されていません。'
552                    . '<br />';
553                break;
554            case UPLOAD_ERR_INI_SIZE:
555                $objErr->arrErr[$keyname] = '※ '
556                    . $this->disp_name[$index]
557                    . 'のアップロードに失敗しました。'
558                    . '(.htaccessファイルのphp_value upload_max_filesizeを調整してください)'
559                    . '<br />';
560                break;
561            default:
562                $objErr->arrErr[$keyname] = '※ '
563                    . $this->disp_name[$index]
564                    . 'のアップロードに失敗しました。'
565                    . 'エラーコードは[' . $_FILES[$keyname]['error'] . ']です。'
566                    . '<br />';
567                break;
568        }
569
570        return false;
571    }
572}
Note: See TracBrowser for help on using the repository browser.