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

Revision 23605, 20.8 KB checked in by kimoto, 10 years ago (diff)

#2448 typo修正・ソース整形・ソースコメントの改善 for 2.13.3

Scrutinizer Auto-Fixes

This patch was automatically generated as part of the following inspection:
 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/d8722894-69a6-4b1b-898d-43618035c60d

Enabled analysis tools:

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