3 * FileIO ImageShack 遠端儲存 API
\r
5 * 以 ImageShack 存圖網站作為圖檔儲存的方式,並提供一套方法供程式管理圖片
\r
7 * 使用此功能請遵守 ImageShack 網站的 Terms of Service,並注意以下條約:
\r
8 * Terms specific to the XML API:
\r
10 * - Website or software must already be developed or have a strategic plan to be developed in the near future.
\r
11 * - Website or software users must be informed that ImageShack is providing free image hosting.
\r
13 * Free implimentation support is offered to websites that have at least 500 unique visitors per day (users)
\r
14 * or expect to achieve 500 users in the near future. Otherwise, the XML API is offered as is.
\r
16 * 使用時請自律將 ImageShack 網站連結置於明顯處,並說明正使用其提供之免費圖檔存放功能。
\r
17 * (http://reg.imageshack.us/content.php?page=linkto 可選擇喜歡方式使用)
\r
19 * @package PMCLibrary
\r
20 * @version $Id: fileio.imageshack.php 393 2007-04-30 11:25:29Z scribe $
\r
21 * @date $Date: 2007-04-30 19:25:29 +0800 (星期一, 30 四月 2007) $
\r
25 var $userAgent, $parameter;
\r
28 /* private 傳檔案到 ImageShack 上面 (發送抓取請求) */
\r
29 function _transloadImageShack($imgname){
\r
30 if(!($fp = @fsockopen('www.imageshack.us', 80))) return false;
\r
32 $argument = 'xml=yes&rembar=1&url='.$this->getImageLocalURL($imgname);
\r
33 $out = "POST /transload.php HTTP/1.1\r\n";
\r
34 $out .= 'Host: www.imageshack.us'."\r\n";
\r
35 $out .= 'User-Agent: '.$this->userAgent."\r\n";
\r
36 $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
\r
37 if($this->parameter[0]) $out .= 'Cookie: myimages='.$this->parameter[0]."\r\n"; // ImageShack Registration Key Cookie
\r
38 $out .= 'Content-Length: '.strlen($argument)."\r\n\r\n";
\r
43 while(!feof($fp)){ $result .= fgets($fp, 128); }
\r
46 if(strpos($result, '<'.'?xml version="1.0" encoding="iso-8859-1"?>')===false) return false;
\r
48 $returnValue = array();
\r
49 $xmlData = explode("\n", $result);
\r
50 foreach($xmlData as $xmlDatum){
\r
51 $xmlDatum = trim($xmlDatum);
\r
52 if($xmlDatum != '' && !eregi('links', $xmlDatum) && !eregi('xml', $xmlDatum)){
\r
53 $xmlDatum = str_replace('>', '<', $xmlDatum);
\r
54 $xmlDatum = explode('<', $xmlDatum);
\r
55 if(count($xmlDatum) >= 3) $returnValue[$xmlDatum[1]] = $xmlDatum[2];
\r
58 return $returnValue;
\r
62 /* private 發出刪除圖片要求 (需填入 Registration Key) */
\r
63 function _deleteImageShack($imgURL){
\r
64 if(!$this->parameter[0]) return true; // 沒金鑰無法要求刪除故直接略過
\r
65 $imgURL = parse_url($imgURL); // 分析 URL 結構準備重組
\r
66 if(!($fp = @fsockopen($imgURL['host'], 80))) return false;
\r
68 $out = 'GET /delete.php?l='.substr($imgURL['path'], 1).'&c='.$this->parameter[0].'&page=THIS_IS_A_FLAG HTTP/1.1'."\r\n";
\r
69 $out .= 'Host: '.$imgURL['host']."\r\n";
\r
70 $out .= 'User-Agent: '.$this->userAgent."\r\n\r\n";
\r
74 while(!feof($fp)){ $result .= fgets($fp, 128); }
\r
77 return (strpos($result, 'THIS_IS_A_FLAG')!==false ? true : false); // 偷吃步,偵測page是否為設定的特殊值
\r
80 /* private 生成 ImageShack my.php 指向頁面位置 */
\r
81 function _myphpImageShack($imgurl, $ishotlink){
\r
82 if($ishotlink) return $imgurl; // 直連:直接傳回不需處理
\r
83 $imgurl = parse_url($imgurl);
\r
84 return 'http://'.$imgurl['host'].'/my.php?image='.basename($imgurl['path']);
\r
88 function _setIndex(){
\r
89 $this->IFS->saveIndex(); // 索引表更新
\r
92 function FileIO($parameter, $ENV){
\r
93 require($ENV['IFS.PATH']);
\r
94 $this->IFS = new IndexFS($ENV['IFS.LOG']); // IndexFS 物件
\r
95 $this->IFS->openIndex();
\r
96 register_shutdown_function(array($this, '_setIndex')); // 設定解構元 (PHP 結束前執行)
\r
97 set_time_limit(120); // 執行時間 120 秒 (傳輸過程可能很長)
\r
98 $this->userAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'; // Just for fun ;-)
\r
99 $this->parameter = $parameter; // 將參數重新解析
\r
101 [0] : ImageShack 註冊金鑰 (即登入頁面 setlogin.php 網址後面附帶一串編碼) * 可不填,但功能會少
\r
102 登入後亦可在 http://reg.imageshack.us/content.php?page=register 找到 Your Registration Code
\r
110 function imageExists($imgname){
\r
111 return $this->IFS->beRecord($imgname);
\r
114 function deleteImage($imgname){
\r
115 if(is_array($imgname)){
\r
116 foreach($imgname as $i){
\r
117 if(($rc = $this->IFS->getRecord($i)) && $this->_deleteImageShack($rc['imgURL'])) $this->IFS->delRecord($i); // 自索引中刪除
\r
118 else return false; // 送出刪除要求失敗
\r
123 if(($rc = $this->IFS->getRecord($imgname)) && $this->_deleteImageShack($rc['imgURL'])){ $this->IFS->delRecord($imgname); return true; }
\r
128 function uploadImage($imgname='', $imgpath='', $imgsize=0){
\r
129 if($imgname=='') return true; // 支援上傳方法
\r
130 if(substr($imgname, -5)=='s.jpg'){ unlink($imgpath); return true; } // 預覽圖不用上傳,直接刪除
\r
131 $result = $this->_transloadImageShack($imgname);
\r
133 $this->IFS->addRecord($imgname, $imgsize, $result['image_link']); // 加入索引之中
\r
134 list($w, $h) = explode('x', $result['resolution']); // 原圖解析度
\r
135 // 判斷是否自動生成預覽圖 (長寬任一大於200像素) 並儲存預覽圖位置
\r
136 $this->IFS->addRecord(substr($imgname, 0, 13).'s.jpg', ceil($imgsize / 4), (($w > 200 || $h > 200) ? $result['thumb_link'] : $result['image_link']));
\r
137 unlink($imgpath); // 確實上傳後刪除本機暫存
\r
142 function getImageFilesize($imgname){
\r
143 if($rc = $this->IFS->getRecord($imgname)) return $rc['imgSize'];
\r
147 function getImageURL($imgname, $ishotlink = false){ // 是否使用熱連結直連圖檔位置 (極有可能被 Ban 網域!請慎用)
\r
148 return ($rc = $this->IFS->getRecord($imgname)) ? (substr($imgname, -5)=='s.jpg' ? $rc['imgURL'] : $this->_myphpImageShack($rc['imgURL'], $ishotlink)) : false;
\r