source: branches/version-2_11-dev/data/class/SC_UploadFile.php @ 20863

Revision 20863, 19.8 KB checked in by nanasess, 13 years ago (diff)

#624(軽微な表示乱れを修正)

  • data/class/SC_UploadFile.php の文言修正

#972 (リファクタリング開発:[管理画面]デザイン管理)

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