source: branches/version-2_12-dev/data/class/SC_UploadFile.php @ 22567

Revision 22567, 19.5 KB checked in by shutta, 13 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

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