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

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