source: branches/feature-module-update/data/class/SC_UploadFile.php @ 16741

Revision 16741, 12.8 KB checked in by adachi, 16 years ago (diff)

set eol-style:LF

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