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

Revision 14508, 14.6 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 = $this->makeFtpTempDir($this->temp_dir);
63            $dst_file = $ftp_temp_dir . $uniqname;
64            $ret = $objThumb->Main($src_file, $width, $height, $dst_file);
65            $this->ftpMoveFile($this->ftp_temp_dir . basename($ret[1]), $ret[1]);
66        } else {
67            $dst_file = $this->temp_dir . $uniqname;
68            $ret = $objThumb->Main($src_file, $width, $height, $dst_file);         
69        }
70       
71        if($ret[0] != 1) {
72            // ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤Îɽ¼¨
73            print($ret[1]);
74            exit;
75        }
76       
77        return basename($ret[1]);
78    }
79       
80    // ¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤òÊݸ¤¹¤ë¡£
81    function makeTempFile($keyname, $rename = true) {
82        $objErr = new SC_CheckError();
83        $cnt = 0;
84        $arrKeyname = array_flip($this->keyname);
85       
86        if(!($_FILES[$keyname]['size'] > 0)) {
87            $objErr->arrErr[$keyname] = "¢¨ " . $this->disp_name[$arrKeyname[$keyname]] . "¤¬¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤Æ¤¤¤Þ¤»¤ó¡£<br />";
88        } else {
89            foreach($this->keyname as $val) {
90                // °ìÃפ·¤¿¥­¡¼¤Î¥Õ¥¡¥¤¥ë¤Ë¾ðÊó¤òÊݸ¤¹¤ë¡£
91                if ($val == $keyname) {
92                    // ³ÈÄ¥»Ò¥Á¥§¥Ã¥¯
93                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->arrExt[$cnt]), array("FILE_EXT_CHECK"));
94                    // ¥Õ¥¡¥¤¥ë¥µ¥¤¥º¥Á¥§¥Ã¥¯
95                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->size[$cnt]), array("FILE_SIZE_CHECK"));
96                    // ¥¨¥é¡¼¤¬¤Ê¤¤¾ì¹ç
97                    if(!isset($objErr->arrErr[$keyname])) {
98                        // ²èÁü¥Õ¥¡¥¤¥ë¤Î¾ì¹ç
99                        if($this->image[$cnt]) {
100                            $this->temp_file[$cnt] = $this->makeThumb($_FILES[$keyname]['tmp_name'], $this->width[$cnt], $this->height[$cnt]);
101                        // ²èÁü¥Õ¥¡¥¤¥ë°Ê³°¤Î¾ì¹ç
102                        } else {
103                            // °ì°Õ¤Ê¥Õ¥¡¥¤¥ë̾¤òºîÀ®¤¹¤ë¡£
104                            if($rename) {
105                                $uniqname = date("mdHi") . "_" . uniqid("").".";
106                                $this->temp_file[$cnt] = ereg_replace("^.*\.",$uniqname, $_FILES[$keyname]['name']);
107                            } else {
108                                $this->temp_file[$cnt] = $_FILES[$keyname]['name'];   
109                            }
110                            $result  = copy($_FILES[$keyname]['tmp_name'], $this->temp_dir. "/". $this->temp_file[$cnt]);
111                            gfPrintLog($_FILES[$keyname]['name']." -> ".$this->temp_dir. "/". $this->temp_file[$cnt]);
112                        }
113                    }
114                }
115                $cnt++;
116            }
117        }
118        return $objErr->arrErr[$keyname];
119    }
120
121    // ²èÁü¤òºï½ü¤¹¤ë¡£
122    function deleteFile($keyname) {
123        $objImage = new SC_Image($this->temp_dir);
124        $cnt = 0;
125        foreach($this->keyname as $val) {
126            if ($val == $keyname) {
127                // °ì»þ¥Õ¥¡¥¤¥ë¤Î¾ì¹çºï½ü¤¹¤ë¡£
128                if($this->temp_file[$cnt] != "") {
129                    // Éé²Ùʬ»¶»þ¤Ï¤¹¤Ù¤Æ¤Î¥µ¡¼¥Ð¤«¤éºï½ü
130                    if($this->multi_web_server_mode === true) {
131                        $this->ftpDeleteFile($this->ftp_temp_dir . $this->temp_file[$cnt]);
132                    } else {
133                        $objImage->deleteImage($this->temp_file[$cnt], $this->save_dir);
134                    }
135                }
136                $this->temp_file[$cnt] = "";
137                $this->save_file[$cnt] = "";
138            }
139            $cnt++;
140        }
141    }
142   
143    // °ì»þ¥Õ¥¡¥¤¥ë¥Ñ¥¹¤ò¼èÆÀ¤¹¤ë¡£
144    function getTempFilePath($keyname) {
145        $cnt = 0;
146        $filepath = "";
147        foreach($this->keyname as $val) {
148            if ($val == $keyname) {
149                if($this->temp_file[$cnt] != "") {
150                    $filepath = $this->temp_dir . "/" . $this->temp_file[$cnt];
151                }
152            }
153            $cnt++;
154        }
155        return $filepath;
156    }
157   
158    // °ì»þ¥Õ¥¡¥¤¥ë¤òÊݸ¥Ç¥£¥ì¥¯¥È¥ê¤Ë°Ü¤¹
159    function moveTempFile() {
160        $cnt = 0;
161        $objImage = new SC_Image($this->temp_dir);
162       
163        foreach($this->keyname as $val) {
164            if($this->temp_file[$cnt] != "") {
165                // Éé²Ùʬ»¶»þ¤Ï¤¹¤Ù¤Æ¤Î¥µ¡¼¥Ð¤Ç°Üư¤ò¼Â¹Ô
166                if($this->multi_web_server_mode === true) {
167                    $this->ftpMoveFile($this->save_dir . $this->temp_file[$cnt]);
168                } else {
169                    $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
170                }
171               
172                // ¤¹¤Ç¤ËÊݸ¥Õ¥¡¥¤¥ë¤¬¤¢¤Ã¤¿¾ì¹ç¤Ïºï½ü¤¹¤ë¡£
173                if($this->save_file[$cnt] != "" && !ereg("^sub/", $this->save_file[$cnt])) {
174                    // Éé²Ùʬ»¶»þ¤Ï¤¹¤Ù¤Æ¤Î¥µ¡¼¥Ð¤Ç¥Õ¥¡¥¤¥ëºï½ü
175                    if($this->multi_web_server_mode === true) {
176                        $this->ftpDeleteFile($this->save_dir . $this->save_file[$cnt]);
177                    } else {
178                        $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
179                    }
180                }
181            }
182            $cnt++;
183        }
184    }
185   
186    // HIDDENÍѤΥե¡¥¤¥ë̾ÇÛÎó¤òÊÖ¤¹
187    function getHiddenFileList() {
188        $cnt = 0;
189        foreach($this->keyname as $val) {
190            if($this->temp_file[$cnt] != "") {
191                $arrRet["temp_" . $val] = $this->temp_file[$cnt];
192            }
193            if($this->save_file[$cnt] != "") {
194                $arrRet["save_" . $val] = $this->save_file[$cnt];
195            }
196            $cnt++;
197        }
198        return $arrRet;
199    }
200   
201    // HIDDEN¤ÇÁ÷¤é¤ì¤Æ¤­¤¿¥Õ¥¡¥¤¥ë̾¤ò¼èÆÀ¤¹¤ë
202    function setHiddenFileList($arrPOST) {
203        $cnt = 0;
204        foreach($this->keyname as $val) {
205            $key = "temp_" . $val;
206            if($arrPOST[$key] != "") {
207                $this->temp_file[$cnt] = $arrPOST[$key];
208            }
209            $key = "save_" . $val;
210            if($arrPOST[$key] != "") {
211                $this->save_file[$cnt] = $arrPOST[$key];
212            }
213            $cnt++;
214        }
215    }
216   
217    // ¥Õ¥©¡¼¥à¤ËÅϤ¹ÍѤΥե¡¥¤¥ë¾ðÊóÇÛÎó¤òÊÖ¤¹
218    function getFormFileList($temp_url, $save_url, $real_size = false) {
219
220        $cnt = 0;
221        foreach($this->keyname as $val) {
222            if($this->temp_file[$cnt] != "") {
223                // ¥Õ¥¡¥¤¥ë¥Ñ¥¹¥Á¥§¥Ã¥¯(¥Ñ¥¹¤Î¥¹¥é¥Ã¥·¥å/¤¬Ï¢Â³¤·¤Ê¤¤¤è¤¦¤Ë¤¹¤ë¡£)
224                if(ereg("/$", $temp_url)) {
225                    $arrRet[$val]['filepath'] = $temp_url . $this->temp_file[$cnt];
226                } else {
227                    $arrRet[$val]['filepath'] = $temp_url . "/" . $this->temp_file[$cnt];
228                }
229                $arrRet[$val]['real_filepath'] = $this->temp_dir . $this->temp_file[$cnt];
230            } elseif ($this->save_file[$cnt] != "") {
231                // ¥Õ¥¡¥¤¥ë¥Ñ¥¹¥Á¥§¥Ã¥¯(¥Ñ¥¹¤Î¥¹¥é¥Ã¥·¥å/¤¬Ï¢Â³¤·¤Ê¤¤¤è¤¦¤Ë¤¹¤ë¡£)
232                if(ereg("/$", $save_url)) {
233                    $arrRet[$val]['filepath'] = $save_url . $this->save_file[$cnt];
234                } else {
235                    $arrRet[$val]['filepath'] = $save_url . "/" . $this->save_file[$cnt];
236                }
237                $arrRet[$val]['real_filepath'] = $this->save_dir . $this->save_file[$cnt];
238            }
239            if($arrRet[$val]['filepath'] != "") {
240                if($real_size){
241                    if(is_file($arrRet[$val]['real_filepath'])) {
242                        list($width, $height) = getimagesize($arrRet[$val]['real_filepath']);
243                    }
244                    // ¥Õ¥¡¥¤¥ë²£Éý
245                    $arrRet[$val]['width'] = $width;
246                    // ¥Õ¥¡¥¤¥ë½ÄÉý
247                    $arrRet[$val]['height'] = $height;
248                }else{
249                    // ¥Õ¥¡¥¤¥ë²£Éý
250                    $arrRet[$val]['width'] = $this->width[$cnt];
251                    // ¥Õ¥¡¥¤¥ë½ÄÉý
252                    $arrRet[$val]['height'] = $this->height[$cnt];
253                }
254                // ɽ¼¨Ì¾
255                $arrRet[$val]['disp_name'] = $this->disp_name[$cnt];
256            }
257            $cnt++;
258        }
259        return $arrRet;
260    }
261   
262    // DBÊݸÍѤΥե¡¥¤¥ë̾ÇÛÎó¤òÊÖ¤¹
263    function getDBFileList() {
264        $cnt = 0;
265        foreach($this->keyname as $val) {
266            if($this->temp_file[$cnt] != "") {
267                $arrRet[$val] = $this->temp_file[$cnt];
268            } else  {
269                $arrRet[$val] = $this->save_file[$cnt];
270            }
271            $cnt++;
272        }
273        return $arrRet;
274    }
275   
276    // DB¤ÇÊݸ¤µ¤ì¤¿¥Õ¥¡¥¤¥ë̾ÇÛÎó¤ò¥»¥Ã¥È¤¹¤ë
277    function setDBFileList($arrVal) {
278        $cnt = 0;
279        foreach($this->keyname as $val) {
280            if($arrVal[$val] != "") {
281                $this->save_file[$cnt] = $arrVal[$val];
282            }
283            $cnt++;
284        }
285    }
286   
287    // ²èÁü¤ò¥»¥Ã¥È¤¹¤ë
288    function setDBImageList($arrVal) {
289        $cnt = 0;
290        foreach($this->keyname as $val) {
291            if($arrVal[$val] != "" && $val == 'tv_products_image') {
292                $this->save_file[$cnt] = $arrVal[$val];
293            }
294            $cnt++;
295        }
296    }
297   
298    // DB¾å¤Î¥Õ¥¡¥¤¥ë¤ÎÆâºï½üÍ׵᤬¤¢¤Ã¤¿¥Õ¥¡¥¤¥ë¤òºï½ü¤¹¤ë¡£
299    function deleteDBFile($arrVal) {
300        $objImage = new SC_Image($this->temp_dir);
301        $cnt = 0;
302        foreach($this->keyname as $val) {
303            if($arrVal[$val] != "") {
304                if($this->save_file[$cnt] == "" && !ereg("^sub/", $arrVal[$val])) {
305                   
306                    // Éé²Ùʬ»¶»þ¤Ï¤¹¤Ù¤Æ¤Î¥µ¡¼¥Ð¤Ç¥Õ¥¡¥¤¥ëºï½ü
307                    if($this->multi_web_server_mode === true) {
308                        $this->ftpDeleteFile($this->save_dir . $arrVal[$val]);
309                    } else {
310                        $objImage->deleteImage($arrVal[$val], $this->save_dir);
311                    }
312                }
313            }
314            $cnt++;
315        }
316    }
317   
318    // ɬ¿ÜȽÄê
319    function checkEXISTS($keyname = "") {
320        $cnt = 0;
321        $arrRet = array();
322        foreach($this->keyname as $val) {
323            if($val == $keyname || $keyname == "") {
324                // ɬ¿Ü¤Ç¤¢¤ì¤Ð¥¨¥é¡¼¥Á¥§¥Ã¥¯
325                if ($this->necessary[$cnt] == true) {
326                    if($this->save_file[$cnt] == "" && $this->temp_file[$cnt] == "") {
327                        $arrRet[$val] = "¢¨ " . $this->disp_name[$cnt] . "¤¬¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤Æ¤¤¤Þ¤»¤ó¡£<br>";
328                    }
329                }
330            }
331            $cnt++;
332        }
333        return $arrRet;
334    }
335       
336    // ³ÈÂçΨ¤ò»ØÄꤷ¤Æ²èÁüÊݸ
337    function saveResizeImage($keyname, $to_w, $to_h) {
338        $path = "";
339       
340        // keyname¤ÎźÉÕ¥Õ¥¡¥¤¥ë¤ò¼èÆÀ
341        $arrImageKey = array_flip($this->keyname);
342        $file = $this->temp_file[$arrImageKey[$keyname]];
343        $filepath = $this->temp_dir . $file;
344       
345        $path = $this->makeThumb($filepath, $to_w, $to_h);
346       
347        // ¥Õ¥¡¥¤¥ë̾¤À¤±ÊÖ¤¹
348        return basename($path);
349    }
350
351    /**         
352     * ¥Õ¥¡¥¤¥ë¤òÁ´¤Æ¤ÎWEB¥µ¡¼¥Ð¤Ø¥³¥Ô¡¼
353     *
354     * @param string $dst_path ¥³¥Ô¡¼Àè¥Õ¥¡¥¤¥ë¥Ñ¥¹(ÁêÂХѥ¹)
355     * @param string $src_path ¥³¥Ô¡¼¸µ¥Õ¥¡¥¤¥ë¥Ñ¥¹(ÀäÂХѥ¹)
356     * @return void
357     */
358    function ftpMoveFile($dst_path, $src_path) {
359        global $arrWEB_SERVERS;
360
361        // Á´¤Æ¤Î¥µ¡¼¥Ð¤Ë¥Õ¥¡¥¤¥ë¤ò¥³¥Ô¡¼¤¹¤ë
362        foreach($arrWEB_SERVERS as $array) {
363            sfFtpCopy($array['host'], $array['user'], $array['pass'], $dst_path, $src_path);
364        }
365        // °Üư¸å¤Ï¥Õ¥¡¥¤¥ë¤òºï½ü
366        @unlink($src_path);
367    }
368
369    /**         
370     * ¥Õ¥¡¥¤¥ë¤òÁ´¤Æ¤ÎWEB¥µ¡¼¥Ð¾å¤«¤éºï½ü
371     *
372     * @param string $dst_path ¥³¥Ô¡¼Àè¥Õ¥¡¥¤¥ë¥Ñ¥¹(ÁêÂХѥ¹)
373     * @return void
374     */
375    function ftpDeleteFile($dst_path) {
376        global $arrWEB_SERVERS;
377
378        // Á´¤Æ¤Î¥µ¡¼¥Ð¤Ë¥Õ¥¡¥¤¥ë¤ò¥³¥Ô¡¼¤¹¤ë
379        foreach($arrWEB_SERVERS as $array) {
380            sfFtpDelete($array['host'], $array['user'], $array['pass'], $dst_path);
381        }
382    }
383
384
385    /**
386     * FTPÍÑ¥Õ¥¡¥¤¥ë°ì»þ³ÊǼ¥Ç¥£¥ì¥¯¥È¥êºîÀ®
387     *
388     * @param string $dir ºîÀ®¥Ç¥£¥ì¥¯¥È¥ê
389     * @return string $ftp_temp_dir ºîÀ®°ì»þ³ÊǼ¥Ç¥£¥ì¥¯¥È¥ê¥Ñ¥¹
390     */
391    function makeFtpTempDir($dir) {
392        // FTPÍÑ¥Õ¥¡¥¤¥ë°ì»þ³ÊǼÍѥǥ£¥ì¥¯¥È¥ê
393        $ftp_temp_dir = $this->temp_dir . "ftp_temp/";
394        // ¥Ç¥£¥ì¥¯¥È¥ê¤¬Â¸ºß¤·¤Ê¤«¤Ã¤¿¤éºîÀ®
395        if(!file_exists($ftp_temp_dir)) {
396            mkdir($ftp_temp_dir);
397            chmod($ftp_temp_dir, 0777);
398        }
399       
400        return $ftp_temp_dir;
401    }
402}
403?>
Note: See TracBrowser for help on using the repository browser.