source: branches/dev/data/class/SC_UploadFile.php @ 14430

Revision 14430, 13.2 KB checked in by uehara, 19 years ago (diff)
Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8$SC_UPLOADFILE_DIR = realpath(dirname( __FILE__));
9require_once($SC_UPLOADFILE_DIR . "/../lib/gdthumb.php");
10require_once($SC_UPLOADFILE_DIR . "/../include/ftp.php");
11
12/* ¥¢¥Ã¥×¥í¡¼¥É¥Õ¥¡¥¤¥ë´ÉÍý¥¯¥é¥¹ */
13class SC_UploadFile {
14    var $temp_dir;                  // °ì»þ¥Õ¥¡¥¤¥ëÊݸ¥Ç¥£¥ì¥¯¥È¥ê
15    var $save_dir;                  // ¥Õ¥¡¥¤¥ëÊݸ¥Ç¥£¥ì¥¯¥È
16    var $ftp_temp_dir;              // FTPÀè°ì»þ¥Ç¥£¥ì¥¯¥È¥ê(Éé²Ùʬ»¶»þ»ÈÍÑ)
17    var $ftp_save_dir;              // FTPÀè¥Õ¥¡¥¤¥ëÊݸ¥Ç¥£¥ì¥¯¥È¥ê(Éé²Ùʬ»¶»þ»ÈÍÑ)
18    var $multi_web_server_mode;     // Éé²Ùʬ»¶¥Õ¥é¥°(Éé²Ùʬ»¶»þ»ÈÍÑ)
19    var $keyname;                   // ¥Õ¥¡¥¤¥ëinput¥¿¥°¤Îname
20    var $width;                     // ²£¥µ¥¤¥º
21    var $height;                    // ½Ä¥µ¥¤¥º
22    var $arrExt;                    // »ØÄꤹ¤ë³ÈÄ¥»Ò
23    var $temp_file;                 // Êݸ¤µ¤ì¤¿¥Õ¥¡¥¤¥ë̾
24    var $save_file;                 // DB¤«¤éÆÉ¤ß½Ð¤·¤¿¥Õ¥¡¥¤¥ë̾
25    var $disp_name;                 // ¹àÌÜ̾
26    var $size;                      // À©¸Â¥µ¥¤¥º
27    var $necessary;                 // ɬ¿Ü¤Î¾ì¹ç:true
28    var $image;                     // ²èÁü¤Î¾ì¹ç:true
29   
30    // ¥Õ¥¡¥¤¥ë´ÉÍý¥¯¥é¥¹
31    function SC_UploadFile($temp_dir, $save_dir, $ftp_temp_dir = "", $ftp_save_dir = "", $multi_web_server_mode = false) {
32        $this->temp_dir = $temp_dir;
33        $this->save_dir = $save_dir;
34        $this->ftp_temp_dir = $ftp_temp_dir;
35        $this->ftp_save_dir = $ftp_save_dir;
36        $this->multi_web_server_mode = $multi_web_server_mode;
37        $this->file_max = 0;
38    }
39
40    // ¥Õ¥¡¥¤¥ë¾ðÊóÄɲÃ
41    function addFile($disp_name, $keyname, $arrExt, $size, $necessary=false, $width=0, $height=0, $image=true) {
42        $this->disp_name[] = $disp_name;
43        $this->keyname[] = $keyname;
44        $this->width[] = $width;
45        $this->height[] = $height;
46        $this->arrExt[] = $arrExt;
47        $this->size[] = $size;
48        $this->necessary[] = $necessary;
49        $this->image[] = $image;
50    }
51    // ¥µ¥à¥Í¥¤¥ë²èÁü¤ÎºîÀ®
52    function makeThumb($src_file, $width, $height) {
53        // °ì°Õ¤ÊID¤ò¼èÆÀ¤¹¤ë¡£
54        $uniqname = date("mdHi") . "_" . uniqid("");
55       
56        $objThumb = new gdthumb();
57       
58        // WEB¥µ¡¼¥ÐÉé²Ùʬ»¶´Ä¶­¤Î¾ì¹ç
59        if($this->multi_web_server_mode === true) {
60           
61            // FTPÍÑ¥Õ¥¡¥¤¥ë°ì»þ³ÊǼÍѥǥ£¥ì¥¯¥È¥êºîÀ®
62            $ftp_temp_dir = makeFtpTempDir($this->temp_dir);
63           
64            $dst_file = $ftp_temp_dir . $uniqname;
65            $ret = $objThumb->Main($src_file, $width, $height, $dst_file);
66            $this->ftpMoveFile($this->ftp_temp_dir . basename($ret[1]), $ret[1]);
67        } else {
68            $dst_file = $this->temp_dir . $uniqname;
69            $ret = $objThumb->Main($src_file, $width, $height, $dst_file);         
70        }
71       
72        if($ret[0] != 1) {
73            // ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤Îɽ¼¨
74            print($ret[1]);
75            exit;
76        }
77       
78        return basename($ret[1]);
79    }
80       
81    // ¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤òÊݸ¤¹¤ë¡£
82    function makeTempFile($keyname, $rename = true) {
83        $objErr = new SC_CheckError();
84        $cnt = 0;
85        $arrKeyname = array_flip($this->keyname);
86       
87        if(!($_FILES[$keyname]['size'] > 0)) {
88            $objErr->arrErr[$keyname] = "¢¨ " . $this->disp_name[$arrKeyname[$keyname]] . "¤¬¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤Æ¤¤¤Þ¤»¤ó¡£<br />";
89        } else {
90            foreach($this->keyname as $val) {
91                // °ìÃפ·¤¿¥­¡¼¤Î¥Õ¥¡¥¤¥ë¤Ë¾ðÊó¤òÊݸ¤¹¤ë¡£
92                if ($val == $keyname) {
93                    // ³ÈÄ¥»Ò¥Á¥§¥Ã¥¯
94                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->arrExt[$cnt]), array("FILE_EXT_CHECK"));
95                    // ¥Õ¥¡¥¤¥ë¥µ¥¤¥º¥Á¥§¥Ã¥¯
96                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->size[$cnt]), array("FILE_SIZE_CHECK"));
97                    // ¥¨¥é¡¼¤¬¤Ê¤¤¾ì¹ç
98                    if(!isset($objErr->arrErr[$keyname])) {
99                        // ²èÁü¥Õ¥¡¥¤¥ë¤Î¾ì¹ç
100                        if($this->image[$cnt]) {
101                            $this->temp_file[$cnt] = $this->makeThumb($_FILES[$keyname]['tmp_name'], $this->width[$cnt], $this->height[$cnt]);
102                        // ²èÁü¥Õ¥¡¥¤¥ë°Ê³°¤Î¾ì¹ç
103                        } else {
104                            // °ì°Õ¤Ê¥Õ¥¡¥¤¥ë̾¤òºîÀ®¤¹¤ë¡£
105                            if($rename) {
106                                $uniqname = date("mdHi") . "_" . uniqid("").".";
107                                $this->temp_file[$cnt] = ereg_replace("^.*\.",$uniqname, $_FILES[$keyname]['name']);
108                            } else {
109                                $this->temp_file[$cnt] = $_FILES[$keyname]['name'];   
110                            }
111                            $result  = copy($_FILES[$keyname]['tmp_name'], $this->temp_dir. "/". $this->temp_file[$cnt]);
112                            gfPrintLog($_FILES[$keyname]['name']." -> ".$this->temp_dir. "/". $this->temp_file[$cnt]);
113                        }
114                    }
115                }
116                $cnt++;
117            }
118        }
119        return $objErr->arrErr[$keyname];
120    }
121
122    // ²èÁü¤òºï½ü¤¹¤ë¡£
123    function deleteFile($keyname) {
124        $objImage = new SC_Image($this->temp_dir);
125        $cnt = 0;
126        foreach($this->keyname as $val) {
127            if ($val == $keyname) {
128                // °ì»þ¥Õ¥¡¥¤¥ë¤Î¾ì¹çºï½ü¤¹¤ë¡£
129                if($this->temp_file[$cnt] != "") {
130                    $objImage->deleteImage($this->temp_file[$cnt], $this->save_dir);
131                }
132                $this->temp_file[$cnt] = "";
133                $this->save_file[$cnt] = "";
134            }
135            $cnt++;
136        }
137    }
138   
139    // °ì»þ¥Õ¥¡¥¤¥ë¥Ñ¥¹¤ò¼èÆÀ¤¹¤ë¡£
140    function getTempFilePath($keyname) {
141        $cnt = 0;
142        $filepath = "";
143        foreach($this->keyname as $val) {
144            if ($val == $keyname) {
145                if($this->temp_file[$cnt] != "") {
146                    $filepath = $this->temp_dir . "/" . $this->temp_file[$cnt];
147                }
148            }
149            $cnt++;
150        }
151        return $filepath;
152    }
153   
154    // °ì»þ¥Õ¥¡¥¤¥ë¤òÊݸ¥Ç¥£¥ì¥¯¥È¥ê¤Ë°Ü¤¹
155    function moveTempFile() {
156        $cnt = 0;
157        $objImage = new SC_Image($this->temp_dir);
158       
159        foreach($this->keyname as $val) {
160            if($this->temp_file[$cnt] != "") {
161                                                   
162                $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
163                // ¤¹¤Ç¤ËÊݸ¥Õ¥¡¥¤¥ë¤¬¤¢¤Ã¤¿¾ì¹ç¤Ïºï½ü¤¹¤ë¡£
164                if($this->save_file[$cnt] != "" && !ereg("^sub/", $this->save_file[$cnt])) {
165                    $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
166                }
167            }
168            $cnt++;
169        }
170    }
171   
172    // HIDDENÍѤΥե¡¥¤¥ë̾ÇÛÎó¤òÊÖ¤¹
173    function getHiddenFileList() {
174        $cnt = 0;
175        foreach($this->keyname as $val) {
176            if($this->temp_file[$cnt] != "") {
177                $arrRet["temp_" . $val] = $this->temp_file[$cnt];
178            }
179            if($this->save_file[$cnt] != "") {
180                $arrRet["save_" . $val] = $this->save_file[$cnt];
181            }
182            $cnt++;
183        }
184        return $arrRet;
185    }
186   
187    // HIDDEN¤ÇÁ÷¤é¤ì¤Æ¤­¤¿¥Õ¥¡¥¤¥ë̾¤ò¼èÆÀ¤¹¤ë
188    function setHiddenFileList($arrPOST) {
189        $cnt = 0;
190        foreach($this->keyname as $val) {
191            $key = "temp_" . $val;
192            if($arrPOST[$key] != "") {
193                $this->temp_file[$cnt] = $arrPOST[$key];
194            }
195            $key = "save_" . $val;
196            if($arrPOST[$key] != "") {
197                $this->save_file[$cnt] = $arrPOST[$key];
198            }
199            $cnt++;
200        }
201    }
202   
203    // ¥Õ¥©¡¼¥à¤ËÅϤ¹ÍѤΥե¡¥¤¥ë¾ðÊóÇÛÎó¤òÊÖ¤¹
204    function getFormFileList($temp_url, $save_url, $real_size = false) {
205
206        $cnt = 0;
207        foreach($this->keyname as $val) {
208            if($this->temp_file[$cnt] != "") {
209                // ¥Õ¥¡¥¤¥ë¥Ñ¥¹¥Á¥§¥Ã¥¯(¥Ñ¥¹¤Î¥¹¥é¥Ã¥·¥å/¤¬Ï¢Â³¤·¤Ê¤¤¤è¤¦¤Ë¤¹¤ë¡£)
210                if(ereg("/$", $temp_url)) {
211                    $arrRet[$val]['filepath'] = $temp_url . $this->temp_file[$cnt];
212                } else {
213                    $arrRet[$val]['filepath'] = $temp_url . "/" . $this->temp_file[$cnt];
214                }
215                $arrRet[$val]['real_filepath'] = $this->temp_dir . $this->temp_file[$cnt];
216            } elseif ($this->save_file[$cnt] != "") {
217                // ¥Õ¥¡¥¤¥ë¥Ñ¥¹¥Á¥§¥Ã¥¯(¥Ñ¥¹¤Î¥¹¥é¥Ã¥·¥å/¤¬Ï¢Â³¤·¤Ê¤¤¤è¤¦¤Ë¤¹¤ë¡£)
218                if(ereg("/$", $save_url)) {
219                    $arrRet[$val]['filepath'] = $save_url . $this->save_file[$cnt];
220                } else {
221                    $arrRet[$val]['filepath'] = $save_url . "/" . $this->save_file[$cnt];
222                }
223                $arrRet[$val]['real_filepath'] = $this->save_dir . $this->save_file[$cnt];
224            }
225            if($arrRet[$val]['filepath'] != "") {
226                if($real_size){
227                    if(is_file($arrRet[$val]['real_filepath'])) {
228                        list($width, $height) = getimagesize($arrRet[$val]['real_filepath']);
229                    }
230                    // ¥Õ¥¡¥¤¥ë²£Éý
231                    $arrRet[$val]['width'] = $width;
232                    // ¥Õ¥¡¥¤¥ë½ÄÉý
233                    $arrRet[$val]['height'] = $height;
234                }else{
235                    // ¥Õ¥¡¥¤¥ë²£Éý
236                    $arrRet[$val]['width'] = $this->width[$cnt];
237                    // ¥Õ¥¡¥¤¥ë½ÄÉý
238                    $arrRet[$val]['height'] = $this->height[$cnt];
239                }
240                // ɽ¼¨Ì¾
241                $arrRet[$val]['disp_name'] = $this->disp_name[$cnt];
242            }
243            $cnt++;
244        }
245        return $arrRet;
246    }
247   
248    // DBÊݸÍѤΥե¡¥¤¥ë̾ÇÛÎó¤òÊÖ¤¹
249    function getDBFileList() {
250        $cnt = 0;
251        foreach($this->keyname as $val) {
252            if($this->temp_file[$cnt] != "") {
253                $arrRet[$val] = $this->temp_file[$cnt];
254            } else  {
255                $arrRet[$val] = $this->save_file[$cnt];
256            }
257            $cnt++;
258        }
259        return $arrRet;
260    }
261   
262    // DB¤ÇÊݸ¤µ¤ì¤¿¥Õ¥¡¥¤¥ë̾ÇÛÎó¤ò¥»¥Ã¥È¤¹¤ë
263    function setDBFileList($arrVal) {
264        $cnt = 0;
265        foreach($this->keyname as $val) {
266            if($arrVal[$val] != "") {
267                $this->save_file[$cnt] = $arrVal[$val];
268            }
269            $cnt++;
270        }
271    }
272   
273    // ²èÁü¤ò¥»¥Ã¥È¤¹¤ë
274    function setDBImageList($arrVal) {
275        $cnt = 0;
276        foreach($this->keyname as $val) {
277            if($arrVal[$val] != "" && $val == 'tv_products_image') {
278                $this->save_file[$cnt] = $arrVal[$val];
279            }
280            $cnt++;
281        }
282    }
283   
284    // DB¾å¤Î¥Õ¥¡¥¤¥ë¤ÎÆâºï½üÍ׵᤬¤¢¤Ã¤¿¥Õ¥¡¥¤¥ë¤òºï½ü¤¹¤ë¡£
285    function deleteDBFile($arrVal) {
286        $objImage = new SC_Image($this->temp_dir);
287        $cnt = 0;
288        foreach($this->keyname as $val) {
289            if($arrVal[$val] != "") {
290                if($this->save_file[$cnt] == "" && !ereg("^sub/", $arrVal[$val])) {
291                    $objImage->deleteImage($arrVal[$val], $this->save_dir);
292                }
293            }
294            $cnt++;
295        }
296    }
297   
298    // ɬ¿ÜȽÄê
299    function checkEXISTS($keyname = "") {
300        $cnt = 0;
301        $arrRet = array();
302        foreach($this->keyname as $val) {
303            if($val == $keyname || $keyname == "") {
304                // ɬ¿Ü¤Ç¤¢¤ì¤Ð¥¨¥é¡¼¥Á¥§¥Ã¥¯
305                if ($this->necessary[$cnt] == true) {
306                    if($this->save_file[$cnt] == "" && $this->temp_file[$cnt] == "") {
307                        $arrRet[$val] = "¢¨ " . $this->disp_name[$cnt] . "¤¬¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤Æ¤¤¤Þ¤»¤ó¡£<br>";
308                    }
309                }
310            }
311            $cnt++;
312        }
313        return $arrRet;
314    }
315       
316    // ³ÈÂçΨ¤ò»ØÄꤷ¤Æ²èÁüÊݸ
317    function saveResizeImage($keyname, $to_w, $to_h) {
318        $path = "";
319       
320        // keyname¤ÎźÉÕ¥Õ¥¡¥¤¥ë¤ò¼èÆÀ
321        $arrImageKey = array_flip($this->keyname);
322        $file = $this->temp_file[$arrImageKey[$keyname]];
323        $filepath = $this->temp_dir . $file;
324       
325        $path = $this->makeThumb($filepath, $to_w, $to_h);
326       
327        // ¥Õ¥¡¥¤¥ë̾¤À¤±ÊÖ¤¹
328        return basename($path);
329    }
330
331    /**         
332     * ¥Õ¥¡¥¤¥ë¤òÁ´¤Æ¤ÎWEB¥µ¡¼¥Ð¤Ø¥³¥Ô¡¼¤·¤Þ¤¹¡£
333     *
334     * @param string $dst_path ¥³¥Ô¡¼Àè¥Õ¥¡¥¤¥ë¥Ñ¥¹(ÁêÂХѥ¹)
335     * @param string $src_path ¥³¥Ô¡¼¸µ¥Õ¥¡¥¤¥ë¥Ñ¥¹(ÀäÂХѥ¹)
336     * @return void
337     */
338    function ftpMoveFile($dst_path, $src_path) {
339        global $arrWEB_SERVERS;
340
341        // Á´¤Æ¤Î¥µ¡¼¥Ð¤Ë¥Õ¥¡¥¤¥ë¤ò¥³¥Ô¡¼¤¹¤ë
342        foreach($arrWEB_SERVERS as $array) {
343            sfFtpCopy($array['host'], $array['user'], $array['pass'], $dst_path, $src_path);
344        }
345        // °Üư¸å¤Ï¥Õ¥¡¥¤¥ë¤òºï½ü
346        unlink($src_path);
347    }
348   
349    /**
350     * FTPÍÑ¥Õ¥¡¥¤¥ë°ì»þ³ÊǼ¥Ç¥£¥ì¥¯¥È¥êºîÀ®
351     *
352     * @param string $dir ºîÀ®¥Ç¥£¥ì¥¯¥È¥ê
353     * @return string $ftp_temp_dir ºîÀ®°ì»þ³ÊǼ¥Ç¥£¥ì¥¯¥È¥ê¥Ñ¥¹
354     */
355    function makeFtpTempDir($dir) {
356        // FTPÍÑ¥Õ¥¡¥¤¥ë°ì»þ³ÊǼÍѥǥ£¥ì¥¯¥È¥ê
357        $ftp_temp_dir = $this->temp_dir . "ftp_temp/";
358        // ¥Ç¥£¥ì¥¯¥È¥ê¤¬Â¸ºß¤·¤Ê¤«¤Ã¤¿¤éºîÀ®
359        if(!file_exists($ftp_temp_dir)) {
360            mkdir($ftp_temp_dir);
361            chmod($ftp_temp_dir, 0777);
362        }
363       
364        return $ftp_temp_dir;
365    }
366}
367?>
Note: See TracBrowser for help on using the repository browser.