source: branches/comu-ver2/data/class/SC_UploadFile.php @ 18379

Revision 18379, 13.8 KB checked in by Seasoft, 14 years ago (diff)

特定条件で PHP が Worning を発する問題を改修。

  • 変数初期化を明示的に配列とした。
  • 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-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
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 deleteFile($keyname) {
140        $objImage = new SC_Image($this->temp_dir);
141        $cnt = 0;
142        foreach($this->keyname as $val) {
143            if ($val == $keyname) {
144                // 一時ファイルの場合削除する。
145                if($this->temp_file[$cnt] != "") {
146                    $objImage->deleteImage($this->temp_file[$cnt], $this->temp_dir);
147                }
148                $this->temp_file[$cnt] = "";
149                $this->save_file[$cnt] = "";
150            }
151            $cnt++;
152        }
153    }
154
155    // 一時ファイルパスを取得する。
156    function getTempFilePath($keyname) {
157        $cnt = 0;
158        $filepath = "";
159        foreach($this->keyname as $val) {
160            if ($val == $keyname) {
161                if($this->temp_file[$cnt] != "") {
162                    $filepath = $this->temp_dir . $this->temp_file[$cnt];
163                }
164            }
165            $cnt++;
166        }
167        return $filepath;
168    }
169
170    // 一時ファイルを保存ディレクトリに移す
171    function moveTempFile() {
172        $cnt = 0;
173        $objImage = new SC_Image($this->temp_dir);
174
175        foreach($this->keyname as $val) {
176            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
177
178                $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
179
180                // すでに保存ファイルがあった場合は削除する。
181                if(isset($this->save_file[$cnt])
182                   && $this->save_file[$cnt] != ""
183                   && !ereg("^sub/", $this->save_file[$cnt])) {
184
185                    $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
186                }
187            }
188            $cnt++;
189        }
190    }
191
192    // HIDDEN用のファイル名配列を返す
193    function getHiddenFileList() {
194        $cnt = 0;
195        $arrRet = array();
196        foreach($this->keyname as $val) {
197            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
198                $arrRet["temp_" . $val] = $this->temp_file[$cnt];
199            }
200            if(isset($this->save_file[$cnt]) && $this->save_file[$cnt] != "") {
201                $arrRet["save_" . $val] = $this->save_file[$cnt];
202            }
203            $cnt++;
204        }
205        return $arrRet;
206    }
207
208    // HIDDENで送られてきたファイル名を取得する
209    function setHiddenFileList($arrPOST) {
210        $cnt = 0;
211        foreach($this->keyname as $val) {
212            $key = "temp_" . $val;
213            if(isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
214                $this->temp_file[$cnt] = $arrPOST[$key];
215            }
216            $key = "save_" . $val;
217            if(isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
218                $this->save_file[$cnt] = $arrPOST[$key];
219            }
220            $cnt++;
221        }
222    }
223
224    // フォームに渡す用のファイル情報配列を返す
225    function getFormFileList($temp_url, $save_url, $real_size = false) {
226        $arrRet = array();
227        $cnt = 0;
228        foreach($this->keyname as $val) {
229            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
230                // ファイルパスチェック(パスのスラッシュ/が連続しないようにする。)
231                if(ereg("/$", $temp_url)) {
232                    $arrRet[$val]['filepath'] = $temp_url . $this->temp_file[$cnt];
233                } else {
234                    $arrRet[$val]['filepath'] = $temp_url . "/" . $this->temp_file[$cnt];
235                }
236                $arrRet[$val]['real_filepath'] = $this->temp_dir . $this->temp_file[$cnt];
237            } elseif (isset($this->save_file[$cnt]) && $this->save_file[$cnt] != "") {
238                // ファイルパスチェック(パスのスラッシュ/が連続しないようにする。)
239                if(ereg("/$", $save_url)) {
240                    $arrRet[$val]['filepath'] = $save_url . $this->save_file[$cnt];
241                } else {
242                    $arrRet[$val]['filepath'] = $save_url . "/" . $this->save_file[$cnt];
243                }
244                $arrRet[$val]['real_filepath'] = $this->save_dir . $this->save_file[$cnt];
245            }
246            if(isset($arrRet[$val]['filepath']) && !empty($arrRet[$val]['filepath'])) {
247                if($real_size){
248                    if(is_file($arrRet[$val]['real_filepath'])) {
249                        list($width, $height) = getimagesize($arrRet[$val]['real_filepath']);
250                    }
251                    // ファイル横幅
252                    $arrRet[$val]['width'] = $width;
253                    // ファイル縦幅
254                    $arrRet[$val]['height'] = $height;
255                }else{
256                    // ファイル横幅
257                    $arrRet[$val]['width'] = $this->width[$cnt];
258                    // ファイル縦幅
259                    $arrRet[$val]['height'] = $this->height[$cnt];
260                }
261                // 表示名
262                $arrRet[$val]['disp_name'] = $this->disp_name[$cnt];
263            }
264            $cnt++;
265        }
266        return $arrRet;
267    }
268
269    // DB保存用のファイル名配列を返す
270    function getDBFileList() {
271        $cnt = 0;
272        foreach($this->keyname as $val) {
273            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
274                $arrRet[$val] = $this->temp_file[$cnt];
275            } else  {
276                $arrRet[$val] = isset($this->save_file[$cnt]) ? $this->save_file[$cnt] : "";
277            }
278            $cnt++;
279        }
280        return $arrRet;
281    }
282
283    // DBで保存されたファイル名配列をセットする
284    function setDBFileList($arrVal) {
285        $cnt = 0;
286        foreach($this->keyname as $val) {
287            if(isset($arrVal[$val]) && $arrVal[$val] != "") {
288                $this->save_file[$cnt] = $arrVal[$val];
289            }
290            $cnt++;
291        }
292    }
293
294    // 画像をセットする
295    function setDBImageList($arrVal) {
296        $cnt = 0;
297        foreach($this->keyname as $val) {
298            if($arrVal[$val] != "" && $val == 'tv_products_image') {
299                $this->save_file[$cnt] = $arrVal[$val];
300            }
301            $cnt++;
302        }
303    }
304
305    // DB上のファイルの内削除要求があったファイルを削除する。
306    function deleteDBFile($arrVal) {
307        $objImage = new SC_Image($this->temp_dir);
308        $cnt = 0;
309        foreach($this->keyname as $val) {
310            if($arrVal[$val] != "") {
311                if($this->save_file[$cnt] == "" && !ereg("^sub/", $arrVal[$val])) {
312                    $objImage->deleteImage($arrVal[$val], $this->save_dir);
313                }
314            }
315            $cnt++;
316        }
317    }
318
319    // 必須判定
320    function checkEXISTS($keyname = "") {
321        $cnt = 0;
322        $arrRet = array();
323        foreach($this->keyname as $val) {
324            if($val == $keyname || $keyname == "") {
325                // 必須であればエラーチェック
326                if ($this->necessary[$cnt] == true) {
327                    if (!isset($this->save_file[$cnt])) $this->save_file[$cnt] = "";
328                    if (!isset($this->temp_file[$cnt])) $this->temp_file[$cnt] = "";
329                    if($this->save_file[$cnt] == ""
330                            &&  $this->temp_file[$cnt] == "") {
331                        $arrRet[$val] = "※ " . $this->disp_name[$cnt] . "がアップロードされていません。<br>";
332                    }
333                }
334            }
335            $cnt++;
336        }
337        return $arrRet;
338    }
339
340    // 拡大率を指定して画像保存
341    function saveResizeImage($keyname, $to_w, $to_h) {
342        $path = "";
343
344        // keynameの添付ファイルを取得
345        $arrImageKey = array_flip($this->keyname);
346        $file = $this->temp_file[$arrImageKey[$keyname]];
347        $filepath = $this->temp_dir . $file;
348
349        $path = $this->makeThumb($filepath, $to_w, $to_h);
350
351        // ファイル名だけ返す
352        return basename($path);
353    }
354
355    /**
356     * 一時保存用のファイル名を生成する
357     *
358     * @param string $rename
359     * @param int $keyname
360     * @return strgin $dst_file
361     */
362    function lfGetTmpImageName($rename, $keyname = "", $uploadfile = ""){
363
364        if( $rename === true ){
365            // 一意なIDを取得し、画像名をリネームし保存
366            $uniqname = date("mdHi") . "_" . uniqid("");
367        } else {
368            // アップロードした画像名で保存
369            $uploadfile = strlen($uploadfile) > 0 ? $uploadfile : $_FILES[$keyname]['name'];
370            $uniqname =  preg_replace('/(.+)\.(.+?)$/','$1', $uploadfile);
371        }
372        $dst_file = $this->temp_dir . $uniqname;
373        return $dst_file;
374    }
375}
376?>
Note: See TracBrowser for help on using the repository browser.