source: branches/version-2_12-dev/data/class/SC_UploadFile.php @ 22567

Revision 22567, 19.5 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

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