source: branches/version-2_12-multilang/data/class/SC_UploadFile.php @ 22058

Revision 22058, 19.2 KB checked in by h_yoshimoto, 11 years ago (diff)

#1955 r21998~r22057間のコミットをmerge

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