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

Revision 21593, 19.8 KB checked in by Seasoft, 14 years ago (diff)

#1613 (typo修正・ソース整形・ソースコメントの改善)

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