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

Revision 23126, 19.7 KB checked in by m_uehara, 11 years ago (diff)

#2348 r23116 - r23125 をマージ

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