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

Revision 23546, 20.7 KB checked in by shutta, 12 years ago (diff)

#2580 Copyrightを更新
Copyrightを2014に更新

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