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

Revision 18777, 17.7 KB checked in by eccuore, 14 years ago (diff)

#792(ダウンロード販売機能) 機能追加

  • 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() {
140        $objErr = new SC_CheckError();
141        $cnt = 0;
142        $arrKeyname = array_flip($this->keyname);
143
144        if(!($_FILES['down_file']['size'] > 0)) {
145            $objErr->arrErr['down_file'] = "※ " . $this->disp_name[$arrKeyname['down_file']] . "がアップロードされていません。<br />";
146        } else {
147            foreach($this->keyname as $val) {
148               // 一致したキーのファイルに情報を保存する。
149                if ($val == 'down_file') {
150                    // 拡張子チェック
151                    $objErr->doFunc(array($this->disp_name[$cnt], 'down_file', $this->arrExt[$cnt]), array("FILE_EXT_CHECK"));
152                    // ファイルサイズチェック
153                    $objErr->doFunc(array($this->disp_name[$cnt], 'down_file', $this->size[$cnt]), array("FILE_SIZE_CHECK"));
154                    // エラーがない場合
155                   if(!isset($objErr->arrErr['down_file'])) {
156                        // 一意なファイル名を作成する。
157                        $uniqname = date("mdHi") . "_" . uniqid("").".";
158                        $this->temp_file[$cnt] = ereg_replace("^.*\.",$uniqname, $_FILES['down_file']['name']);
159                        set_time_limit(0);
160                        $result  = copy($_FILES['down_file']['tmp_name'], $this->temp_dir . $this->temp_file[$cnt]);
161                        GC_Utils_Ex::gfPrintLog($result." -> ". $this->temp_dir . $this->temp_file[$cnt]);
162                    }
163                }
164                $cnt++;
165            }
166        }
167        return $objErr->arrErr['down_file'];
168    }
169
170    // 画像を削除する。
171    function deleteFile($keyname) {
172        $objImage = new SC_Image($this->temp_dir);
173        $cnt = 0;
174        foreach($this->keyname as $val) {
175            if ($val == $keyname) {
176                // 一時ファイルの場合削除する。
177                if($this->temp_file[$cnt] != "") {
178                    $objImage->deleteImage($this->temp_file[$cnt], $this->temp_dir);
179                }
180                $this->temp_file[$cnt] = "";
181                $this->save_file[$cnt] = "";
182            }
183            $cnt++;
184        }
185    }
186
187    // 一時ファイルパスを取得する。
188    function getTempFilePath($keyname) {
189        $cnt = 0;
190        $filepath = "";
191        foreach($this->keyname as $val) {
192            if ($val == $keyname) {
193                if($this->temp_file[$cnt] != "") {
194                    $filepath = $this->temp_dir . $this->temp_file[$cnt];
195                }
196            }
197            $cnt++;
198        }
199        return $filepath;
200    }
201
202    // 一時ファイルを保存ディレクトリに移す
203    function moveTempFile() {
204        $cnt = 0;
205        $objImage = new SC_Image($this->temp_dir);
206
207        foreach($this->keyname as $val) {
208            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
209
210                $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
211
212                // すでに保存ファイルがあった場合は削除する。
213                if(isset($this->save_file[$cnt])
214                   && $this->save_file[$cnt] != ""
215                   && !ereg("^sub/", $this->save_file[$cnt])) {
216
217                    $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
218                }
219            }
220            $cnt++;
221        }
222    }
223
224    // ダウンロード一時ファイルを保存ディレクトリに移す
225    function moveTempDownFile() {
226        $cnt = 0;
227        $objImage = new SC_Image($this->temp_dir);
228        foreach($this->keyname as $val) {
229            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
230                $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
231                // すでに保存ファイルがあった場合は削除する。
232                if(isset($this->save_file[$cnt])
233                   && $this->save_file[$cnt] != ""
234                   && !ereg("^sub/", $this->save_file[$cnt])) {
235                    $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
236                }
237            }
238            $cnt++;
239        }
240    }
241
242    // HIDDEN用のファイル名配列を返す
243    function getHiddenFileList() {
244        $cnt = 0;
245        $arrRet = array();
246        foreach($this->keyname as $val) {
247            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
248                $arrRet["temp_" . $val] = $this->temp_file[$cnt];
249            }
250            if(isset($this->save_file[$cnt]) && $this->save_file[$cnt] != "") {
251                $arrRet["save_" . $val] = $this->save_file[$cnt];
252            }
253            $cnt++;
254        }
255        return $arrRet;
256    }
257
258    // HIDDENで送られてきたファイル名を取得する
259    function setHiddenFileList($arrPOST) {
260        $cnt = 0;
261        foreach($this->keyname as $val) {
262            $key = "temp_" . $val;
263            if(isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
264                $this->temp_file[$cnt] = $arrPOST[$key];
265            }
266            $key = "save_" . $val;
267            if(isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
268                $this->save_file[$cnt] = $arrPOST[$key];
269            }
270            $cnt++;
271        }
272    }
273
274    // フォームに渡す用のファイル情報配列を返す
275    function getFormFileList($temp_url, $save_url, $real_size = false) {
276        $arrRet = array();
277        $cnt = 0;
278        foreach($this->keyname as $val) {
279            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
280                // ファイルパスチェック(パスのスラッシュ/が連続しないようにする。)
281                if(ereg("/$", $temp_url)) {
282                    $arrRet[$val]['filepath'] = $temp_url . $this->temp_file[$cnt];
283                } else {
284                    $arrRet[$val]['filepath'] = $temp_url . "/" . $this->temp_file[$cnt];
285                }
286                $arrRet[$val]['real_filepath'] = $this->temp_dir . $this->temp_file[$cnt];
287            } elseif (isset($this->save_file[$cnt]) && $this->save_file[$cnt] != "") {
288                // ファイルパスチェック(パスのスラッシュ/が連続しないようにする。)
289                if(ereg("/$", $save_url)) {
290                    $arrRet[$val]['filepath'] = $save_url . $this->save_file[$cnt];
291                } else {
292                    $arrRet[$val]['filepath'] = $save_url . "/" . $this->save_file[$cnt];
293                }
294                $arrRet[$val]['real_filepath'] = $this->save_dir . $this->save_file[$cnt];
295            }
296            if(isset($arrRet[$val]['filepath']) && !empty($arrRet[$val]['filepath'])) {
297                if($real_size){
298                    if(is_file($arrRet[$val]['real_filepath'])) {
299                        list($width, $height) = getimagesize($arrRet[$val]['real_filepath']);
300                    }
301                    // ファイル横幅
302                    $arrRet[$val]['width'] = $width;
303                    // ファイル縦幅
304                    $arrRet[$val]['height'] = $height;
305                }else{
306                    // ファイル横幅
307                    $arrRet[$val]['width'] = $this->width[$cnt];
308                    // ファイル縦幅
309                    $arrRet[$val]['height'] = $this->height[$cnt];
310                }
311                // 表示名
312                $arrRet[$val]['disp_name'] = $this->disp_name[$cnt];
313            }
314            $cnt++;
315        }
316        return $arrRet;
317    }
318
319    // フォームに渡す用のダウンロードファイル情報を返す
320    function getFormDownFile() {
321        $arrRet = "";
322        $cnt = 0;
323GC_Utils::gfDebugLog($this->keyname);
324        foreach($this->keyname as $val) {
325            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
326                $arrRet = $this->temp_file[$cnt];
327            } elseif (isset($this->save_file[$cnt]) && $this->save_file[$cnt] != "") {
328                $arrRet = $this->save_file[$cnt];
329            }
330            $cnt++;
331        }
332GC_Utils::gfPrintLog("1111111111111111111111");
333GC_Utils::gfDebugLog($arrRet);
334        return $arrRet;
335    }
336
337    // DB保存用のファイル名配列を返す
338    function getDBFileList() {
339        $cnt = 0;
340        foreach($this->keyname as $val) {
341            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
342                $arrRet[$val] = $this->temp_file[$cnt];
343            } else  {
344                $arrRet[$val] = isset($this->save_file[$cnt]) ? $this->save_file[$cnt] : "";
345            }
346            $cnt++;
347        }
348        return $arrRet;
349    }
350
351    // DBで保存されたファイル名配列をセットする
352    function setDBFileList($arrVal) {
353        $cnt = 0;
354        foreach($this->keyname as $val) {
355            if(isset($arrVal[$val]) && $arrVal[$val] != "") {
356                $this->save_file[$cnt] = $arrVal[$val];
357            }
358            $cnt++;
359        }
360    }
361
362    // DBで保存されたダウンロードファイル名をセットする
363    function setDBDownFile($arrVal) {
364GC_Utils::gfPrintLog("setDBDownFile");
365GC_Utils::gfDebugLog($arrVal);
366        if(isset($arrVal['down_realfilename']) && $arrVal['down_realfilename'] != "") {
367            $this->save_file[0] = $arrVal['down_realfilename'];
368        }
369    }
370
371    // 画像をセットする
372    function setDBImageList($arrVal) {
373        $cnt = 0;
374        foreach($this->keyname as $val) {
375            if($arrVal[$val] != "" && $val == 'tv_products_image') {
376                $this->save_file[$cnt] = $arrVal[$val];
377            }
378            $cnt++;
379        }
380    }
381
382    // DB上のファイルの内削除要求があったファイルを削除する。
383    function deleteDBFile($arrVal) {
384        $objImage = new SC_Image($this->temp_dir);
385        $cnt = 0;
386        foreach($this->keyname as $val) {
387            if($arrVal[$val] != "") {
388                if($this->save_file[$cnt] == "" && !ereg("^sub/", $arrVal[$val])) {
389                    $objImage->deleteImage($arrVal[$val], $this->save_dir);
390                }
391            }
392            $cnt++;
393        }
394    }
395
396    // DB上のダウンロードファイルの内削除要求があったファイルを削除する。
397    function deleteDBDownFile($arrVal) {
398        $objImage = new SC_Image($this->temp_dir);
399        $cnt = 0;
400        if($arrVal['down_realfilename'] != "") {
401            if($this->save_file[$cnt] == "" && !ereg("^sub/", $arrVal['down_realfilename'])) {
402                $objImage->deleteImage($arrVal['down_realfilename'], $this->save_dir);
403            }
404        }
405    }
406
407    // 必須判定
408    function checkEXISTS($keyname = "") {
409        $cnt = 0;
410        $arrRet = array();
411        foreach($this->keyname as $val) {
412            if($val == $keyname || $keyname == "") {
413                // 必須であればエラーチェック
414                if ($this->necessary[$cnt] == true) {
415                    if (!isset($this->save_file[$cnt])) $this->save_file[$cnt] = "";
416                    if (!isset($this->temp_file[$cnt])) $this->temp_file[$cnt] = "";
417                    if($this->save_file[$cnt] == ""
418                            &&  $this->temp_file[$cnt] == "") {
419                        $arrRet[$val] = "※ " . $this->disp_name[$cnt] . "がアップロードされていません。<br>";
420                    }
421                }
422            }
423            $cnt++;
424        }
425        return $arrRet;
426    }
427
428    // 拡大率を指定して画像保存
429    function saveResizeImage($keyname, $to_w, $to_h) {
430        $path = "";
431
432        // keynameの添付ファイルを取得
433        $arrImageKey = array_flip($this->keyname);
434        $file = $this->temp_file[$arrImageKey[$keyname]];
435        $filepath = $this->temp_dir . $file;
436
437        $path = $this->makeThumb($filepath, $to_w, $to_h);
438
439        // ファイル名だけ返す
440        return basename($path);
441    }
442
443    /**
444     * 一時保存用のファイル名を生成する
445     *
446     * @param string $rename
447     * @param int $keyname
448     * @return string
449     */
450    function lfGetTmpImageName($rename, $keyname = "", $uploadfile = ""){
451
452        if( $rename === true ){
453            // 一意なIDを取得し、画像名をリネームし保存
454            $uniqname = date("mdHi") . "_" . uniqid("");
455        } else {
456            // アップロードした画像名で保存
457            $uploadfile = strlen($uploadfile) > 0 ? $uploadfile : $_FILES[$keyname]['name'];
458            $uniqname =  preg_replace('/(.+)\.(.+?)$/','$1', $uploadfile);
459        }
460        $dst_file = $this->temp_dir . $uniqname;
461        return $dst_file;
462    }
463}
464?>
Note: See TracBrowser for help on using the repository browser.