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

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

#2545 ファイルアップロード時のエラーメッセージが不適切
makeTempDownFileメソッドにも、何もファイルを指定せずにアップロードした場合のエラーメッセージを追加。
また、 #2495 と同様にエラーメッセージを調整。

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