source: branches/version-2_5-dev/data/class/SC_UploadFile.php @ 19660

Revision 19660, 19.5 KB checked in by eccuore, 13 years ago (diff)

#792(ダウンロード販売機能) .htaccessに依存する部分があるので暫定でエラーメッセージにて説明を入れました

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