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

Revision 23124, 19.7 KB checked in by kimoto, 13 years ago (diff)

#2043 typo修正・ソース整形・ソースコメントの改善 for 2.13.0
PHP4的な書き方の修正

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