source: branches/version-2/data/class/SC_UploadFile.php @ 18007

Revision 18007, 13.6 KB checked in by kajiwara, 15 years ago (diff)

2.4.0 正式版のコミット。コミット内容の詳細はこちら(http://svn.ec-cube.net/open_trac/query?status=closed&milestone=EC-CUBE2.4.0

  • 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-2007 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    var $keyname;   // ファイルinputタグのname
32    var $width;     // 横サイズ
33    var $height;    // 縦サイズ
34    var $arrExt;    // 指定する拡張子
35    var $temp_file; // 保存されたファイル名
36    var $save_file; // DBから読み出したファイル名
37    var $disp_name; // 項目名
38    var $size;      // 制限サイズ
39    var $necessary; // 必須の場合:true
40    var $image;     // 画像の場合:true
41
42    // ファイル管理クラス
43    function SC_UploadFile($temp_dir, $save_dir) {
44        $this->temp_dir = (preg_match("|/$|", $temp_dir) == 0) ? $temp_dir. "/" : $temp_dir;
45        $this->save_dir = (preg_match("|/$|", $save_dir) == 0) ? $save_dir. "/" : $save_dir;
46        $this->file_max = 0;
47    }
48
49    // ファイル情報追加
50    function addFile($disp_name, $keyname, $arrExt, $size, $necessary=false, $width=0, $height=0, $image=true) {
51        $this->disp_name[] = $disp_name;
52        $this->keyname[] = $keyname;
53        $this->width[] = $width;
54        $this->height[] = $height;
55        $this->arrExt[] = $arrExt;
56        $this->size[] = $size;
57        $this->necessary[] = $necessary;
58        $this->image[] = $image;
59    }
60    // サムネイル画像の作成
61    function makeThumb($src_file, $width, $height, $dst_file) {
62        $objThumb = new gdthumb();
63        $ret = $objThumb->Main($src_file, $width, $height, $dst_file);
64
65        if($ret[0] != 1) {
66            // エラーメッセージの表示
67            print($ret[1]);
68            exit;
69        }
70
71        return basename($ret[1]);
72    }
73
74    // アップロードされたファイルを保存する。
75    // FIXME see. http://www.php.net/manual/en/features.file-upload.php
76    function makeTempFile($keyname, $rename = IMAGE_RENAME) {
77        $objErr = new SC_CheckError();
78        $cnt = 0;
79        $arrKeyname = array_flip($this->keyname);
80
81        if(!($_FILES[$keyname]['size'] > 0)) {
82            $objErr->arrErr[$keyname] = "※ " . $this->disp_name[$arrKeyname[$keyname]] . "がアップロードされていません。<br />";
83        } else {
84            foreach($this->keyname as $val) {
85                // 一致したキーのファイルに情報を保存する。
86                if ($val == $keyname) {
87                    // 拡張子チェック
88                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->arrExt[$cnt]), array("FILE_EXT_CHECK"));
89                    // ファイルサイズチェック
90                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->size[$cnt]), array("FILE_SIZE_CHECK"));
91                    // エラーがない場合
92                    if(!isset($objErr->arrErr[$keyname])) {
93                        // 画像ファイルの場合
94                        if($this->image[$cnt]) {
95                            // 保存用の画像名を取得する
96                            $dst_file = $this->lfGetTmpImageName($rename, $keyname);
97                            $this->temp_file[$cnt] = $this->makeThumb($_FILES[$keyname]['tmp_name'], $this->width[$cnt], $this->height[$cnt], $dst_file);
98                        // 画像ファイル以外の場合
99                        } else {
100                            // 一意なファイル名を作成する。
101                            if($rename) {
102                                $uniqname = date("mdHi") . "_" . uniqid("").".";
103                                $this->temp_file[$cnt] = ereg_replace("^.*\.",$uniqname, $_FILES[$keyname]['name']);
104                            } else {
105                                $this->temp_file[$cnt] = $_FILES[$keyname]['name'];
106                            }
107                            $result  = copy($_FILES[$keyname]['tmp_name'], $this->temp_dir . $this->temp_file[$cnt]);
108                            GC_Utils_Ex::gfPrintLog($_FILES[$keyname]['name']." -> ". $this->temp_dir . $this->temp_file[$cnt]);
109                        }
110                    }
111                }
112                $cnt++;
113            }
114        }
115        return $objErr->arrErr[$keyname];
116    }
117
118    // 画像を削除する。
119    function deleteFile($keyname) {
120        $objImage = new SC_Image($this->temp_dir);
121        $cnt = 0;
122        foreach($this->keyname as $val) {
123            if ($val == $keyname) {
124                // 一時ファイルの場合削除する。
125                if($this->temp_file[$cnt] != "") {
126                    $objImage->deleteImage($this->temp_file[$cnt], $this->temp_dir);
127                }
128                $this->temp_file[$cnt] = "";
129                $this->save_file[$cnt] = "";
130            }
131            $cnt++;
132        }
133    }
134
135    // 一時ファイルパスを取得する。
136    function getTempFilePath($keyname) {
137        $cnt = 0;
138        $filepath = "";
139        foreach($this->keyname as $val) {
140            if ($val == $keyname) {
141                if($this->temp_file[$cnt] != "") {
142                    $filepath = $this->temp_dir . $this->temp_file[$cnt];
143                }
144            }
145            $cnt++;
146        }
147        return $filepath;
148    }
149
150    // 一時ファイルを保存ディレクトリに移す
151    function moveTempFile() {
152        $cnt = 0;
153        $objImage = new SC_Image($this->temp_dir);
154
155        foreach($this->keyname as $val) {
156            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
157
158                $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
159
160                // すでに保存ファイルがあった場合は削除する。
161                if(isset($this->save_file[$cnt])
162                   && $this->save_file[$cnt] != ""
163                   && !ereg("^sub/", $this->save_file[$cnt])) {
164
165                    $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
166                }
167            }
168            $cnt++;
169        }
170    }
171
172    // HIDDEN用のファイル名配列を返す
173    function getHiddenFileList() {
174        $cnt = 0;
175        $arrRet = array();
176        foreach($this->keyname as $val) {
177            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
178                $arrRet["temp_" . $val] = $this->temp_file[$cnt];
179            }
180            if(isset($this->save_file[$cnt]) && $this->save_file[$cnt] != "") {
181                $arrRet["save_" . $val] = $this->save_file[$cnt];
182            }
183            $cnt++;
184        }
185        return $arrRet;
186    }
187
188    // HIDDENで送られてきたファイル名を取得する
189    function setHiddenFileList($arrPOST) {
190        $cnt = 0;
191        foreach($this->keyname as $val) {
192            $key = "temp_" . $val;
193            if(isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
194                $this->temp_file[$cnt] = $arrPOST[$key];
195            }
196            $key = "save_" . $val;
197            if(isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
198                $this->save_file[$cnt] = $arrPOST[$key];
199            }
200            $cnt++;
201        }
202    }
203
204    // フォームに渡す用のファイル情報配列を返す
205    function getFormFileList($temp_url, $save_url, $real_size = false) {
206        $arrRet = array();
207        $cnt = 0;
208        foreach($this->keyname as $val) {
209            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
210                // ファイルパスチェック(パスのスラッシュ/が連続しないようにする。)
211                if(ereg("/$", $temp_url)) {
212                    $arrRet[$val]['filepath'] = $temp_url . $this->temp_file[$cnt];
213                } else {
214                    $arrRet[$val]['filepath'] = $temp_url . "/" . $this->temp_file[$cnt];
215                }
216                $arrRet[$val]['real_filepath'] = $this->temp_dir . $this->temp_file[$cnt];
217            } elseif (isset($this->save_file[$cnt]) && $this->save_file[$cnt] != "") {
218                // ファイルパスチェック(パスのスラッシュ/が連続しないようにする。)
219                if(ereg("/$", $save_url)) {
220                    $arrRet[$val]['filepath'] = $save_url . $this->save_file[$cnt];
221                } else {
222                    $arrRet[$val]['filepath'] = $save_url . "/" . $this->save_file[$cnt];
223                }
224                $arrRet[$val]['real_filepath'] = $this->save_dir . $this->save_file[$cnt];
225            }
226            if(isset($arrRet[$val]['filepath']) && !empty($arrRet[$val]['filepath'])) {
227                if($real_size){
228                    if(is_file($arrRet[$val]['real_filepath'])) {
229                        list($width, $height) = getimagesize($arrRet[$val]['real_filepath']);
230                    }
231                    // ファイル横幅
232                    $arrRet[$val]['width'] = $width;
233                    // ファイル縦幅
234                    $arrRet[$val]['height'] = $height;
235                }else{
236                    // ファイル横幅
237                    $arrRet[$val]['width'] = $this->width[$cnt];
238                    // ファイル縦幅
239                    $arrRet[$val]['height'] = $this->height[$cnt];
240                }
241                // 表示名
242                $arrRet[$val]['disp_name'] = $this->disp_name[$cnt];
243            }
244            $cnt++;
245        }
246        return $arrRet;
247    }
248
249    // DB保存用のファイル名配列を返す
250    function getDBFileList() {
251        $cnt = 0;
252        foreach($this->keyname as $val) {
253            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
254                $arrRet[$val] = $this->temp_file[$cnt];
255            } else  {
256                $arrRet[$val] = isset($this->save_file[$cnt]) ? $this->save_file[$cnt] : "";
257            }
258            $cnt++;
259        }
260        return $arrRet;
261    }
262
263    // DBで保存されたファイル名配列をセットする
264    function setDBFileList($arrVal) {
265        $cnt = 0;
266        foreach($this->keyname as $val) {
267            if(isset($arrVal[$val]) && $arrVal[$val] != "") {
268                $this->save_file[$cnt] = $arrVal[$val];
269            }
270            $cnt++;
271        }
272    }
273
274    // 画像をセットする
275    function setDBImageList($arrVal) {
276        $cnt = 0;
277        foreach($this->keyname as $val) {
278            if($arrVal[$val] != "" && $val == 'tv_products_image') {
279                $this->save_file[$cnt] = $arrVal[$val];
280            }
281            $cnt++;
282        }
283    }
284
285    // DB上のファイルの内削除要求があったファイルを削除する。
286    function deleteDBFile($arrVal) {
287        $objImage = new SC_Image($this->temp_dir);
288        $cnt = 0;
289        foreach($this->keyname as $val) {
290            if($arrVal[$val] != "") {
291                if($this->save_file[$cnt] == "" && !ereg("^sub/", $arrVal[$val])) {
292                    $objImage->deleteImage($arrVal[$val], $this->save_dir);
293                }
294            }
295            $cnt++;
296        }
297    }
298
299    // 必須判定
300    function checkEXISTS($keyname = "") {
301        $cnt = 0;
302        $arrRet = array();
303        foreach($this->keyname as $val) {
304            if($val == $keyname || $keyname == "") {
305                // 必須であればエラーチェック
306                if ($this->necessary[$cnt] == true) {
307                    if (!isset($this->save_file[$cnt])) $this->save_file[$cnt] = "";
308                    if (!isset($this->temp_file[$cnt])) $this->temp_file[$cnt] = "";
309                    if($this->save_file[$cnt] == ""
310                            &&  $this->temp_file[$cnt] == "") {
311                        $arrRet[$val] = "※ " . $this->disp_name[$cnt] . "がアップロードされていません。<br>";
312                    }
313                }
314            }
315            $cnt++;
316        }
317        return $arrRet;
318    }
319
320    // 拡大率を指定して画像保存
321    function saveResizeImage($keyname, $to_w, $to_h) {
322        $path = "";
323
324        // keynameの添付ファイルを取得
325        $arrImageKey = array_flip($this->keyname);
326        $file = $this->temp_file[$arrImageKey[$keyname]];
327        $filepath = $this->temp_dir . $file;
328
329        $path = $this->makeThumb($filepath, $to_w, $to_h);
330
331        // ファイル名だけ返す
332        return basename($path);
333    }
334
335    /**
336     * 一時保存用のファイル名を生成する
337     *
338     * @param string $rename
339     * @param int $keyname
340     * @return strgin $dst_file
341     */
342    function lfGetTmpImageName($rename, $keyname = "", $uploadfile = ""){
343
344        if( $rename === true ){
345            // 一意なIDを取得し、画像名をリネームし保存
346            $uniqname = date("mdHi") . "_" . uniqid("");
347        } else {
348            // アップロードした画像名で保存
349            $uploadfile = strlen($uploadfile) > 0 ? $uploadfile : $_FILES[$keyname]['name'];
350            $uniqname =  preg_replace('/(.+)\.(.+?)$/','$1', $uploadfile);
351        }
352        $dst_file = $this->temp_dir . $uniqname;
353        return $dst_file;
354    }
355}
356?>
Note: See TracBrowser for help on using the repository browser.