1+ <?php 
2+ //七牛的phpSDK用的我脑袋都抽筋,看完代码我都想骂妈。 
3+ class  SDK  implements  ArrayAccess {
4+ 
5+ 	const  QINIU_UP_HOST 	= 'http://up.qiniu.com ' ;
6+ 	const  QINIU_RS_HOST 	= 'http://rs.qbox.me ' ;
7+ 	const  QINIU_RSF_HOST = 'http://rsf.qbox.me ' ;
8+ 	//查看  
9+ 	//删除  
10+ 	//复制 x 
11+ 	//移动 x 
12+ 	//上传 x 
13+ 	protected  $ access_token  ;
14+ 	protected  $ secret_token  ;
15+ 	protected  $ bucket ;
16+ 	protected  $ cache  = array ();
17+ 	protected  $ aliases  = array (); //文件别名, 针对文件名比较长的文件 
18+ 	//curl  
19+ 	protected  $ ch ;
20+ 	protected  $ headers ;
21+ 	protected  $ options  = array ();
22+ 	protected  $ response ;
23+ 	protected  $ info ;
24+ 	protected  $ errno ; 
25+ 	protected  $ error ;
26+ 
27+ 	public  function  __construct ($ access_token , $ secret_token , $ bucket  = null )
28+ 	{
29+ 		$ this  ->access_token  = $ access_token ;
30+ 		$ this  ->secret_token  = $ secret_token ;
31+ 		$ this  ->bucket  = $ bucket ;
32+ 	}
33+ 
34+ 	public  function  getBucket ()
35+ 	{
36+ 		return  $ this  ->bucket ;
37+ 	}
38+ 
39+ 	public  function  setBucket ($ bucket )
40+ 	{
41+ 		$ this  ->bucket  = $ bucket ;
42+ 	}
43+ 	/** 
44+ 	 * 查看指定文件信息。 
45+ 	 * @param  string $key  	文件名或者目录+文件名 
46+ 	 * @return Array|boolean 	成功返回文件内容,否会返回false. 
47+ 	 */ 
48+ 	public  function  stat ($ key )
49+ 	{
50+ 		list ($ bucket , $ key ) = $ this  ->parseKey ($ key );
51+ 		if  ( is_null ($ bucket ) ) 
52+ 		{
53+ 			die ('error ' );
54+ 		}
55+ 		$ url  = self ::QINIU_RS_HOST  .'/stat/ '  . $ this  ->encode ("$ bucket: $ key " );
56+ 		return  $ this  ->get ($ url );
57+ 	}
58+ 
59+ 	public  function  delete ($ key )
60+ 	{
61+ 		list ($ bucket , $ key ) = $ this  ->parseKey ($ key );
62+ 		if  ( is_null ($ bucket ) ) 
63+ 		{
64+ 			die ('error ' );
65+ 		}
66+ 		$ url  = self ::QINIU_RS_HOST  .'/delete/ '  . $ this  ->encode ("$ bucket: $ key " );
67+ 
68+ 		return  $ this  ->get ($ url );
69+ 	}
70+ 
71+ 	protected  function  parseKey ($ key )
72+ 	{
73+ 		$ key  = $ this  ->getAlias ($ key );
74+ 		if  ( isset ($ this  ->cache [$ key ]) ) 
75+ 		{
76+ 			return  $ this  ->cache [$ key ];
77+ 		}
78+ 		$ segments  = explode ("| " , $ key );
79+ 		if  ( count ($ segments ) === 1  ) 
80+ 		{
81+ 			$ this  ->cache [$ key ] = array ($ this  ->bucket , $ segments [0 ]);
82+ 		}
83+ 		else 
84+ 		{
85+ 			$ key  = implode ('| ' , array_slice ($ segments , 1 ));
86+ 			$ this  ->cache [$ key ] = array ($ segments [0 ], $ key );
87+ 		}
88+ 		return  $ this  ->cache [$ key ];
89+ 	}
90+ 
91+ 	public  function  getAlias ($ key )
92+ 	{
93+ 		return  isset ($ this  ->aliases [$ key ]) ? $ this  ->aliases [$ key ] : $ key ;
94+ 	}
95+ 
96+ 	public  function  accessToken ($ url , $ body  = false )
97+ 	{
98+ 		$ url  = parse_url ($ url );
99+ 		$ result  = '' ;
100+ 		if  (isset ($ url ['path ' ])) {
101+ 			$ result  = $ url ['path ' ];
102+ 		}
103+ 		if  (isset ($ url ['query ' ])) {
104+ 			$ result  .= '? '  . $ url ['query ' ];
105+ 		}
106+ 		$ result  .= "\n" ;
107+ 		if  ($ body ) {
108+ 			$ result  .= $ body ;
109+ 		}
110+ 		$ sign  = hash_hmac ('sha1 ' , $ result , $ this  ->secret_token , true );
111+ 		return  $ this  ->access_token  . ': '  . $ this  ->encode ($ sign );
112+ 	}
113+ 
114+ 	public  function  get ($ url , $ options  = array ())
115+ 	{
116+ 		$ this  ->ch  = curl_init ();
117+ 		$ token  = $ this  ->accessToken ($ url );
118+ 		$ options [CURLOPT_HTTPHEADER ] = array ('Authorization: QBox  ' . $ token );
119+ 		$ this  ->options [CURLOPT_URL ] = $ url ;
120+ 		$ this  ->options  = $ options  + $ this  ->options ;
121+ 		//临时处理逻辑 
122+ 		
123+ 		return  $ this  ->execute ();
124+ 	}
125+ 
126+ 	protected  function  execute () 
127+ 	{
128+ 		if  ( !$ this  ->option (CURLOPT_RETURNTRANSFER ) ) 
129+ 		{
130+ 			$ this  ->option (CURLOPT_RETURNTRANSFER , true );
131+ 		}
132+ 		if  ( !$ this  ->option (CURLOPT_SSL_VERIFYPEER ) ) 
133+ 		{
134+ 			$ this  ->option (CURLOPT_SSL_VERIFYPEER , false );
135+ 		}
136+ 		if  ( !$ this  ->option (CURLOPT_SSL_VERIFYHOST ) ) 
137+ 		{
138+ 			$ this  ->option (CURLOPT_SSL_VERIFYHOST , false );
139+ 		}
140+ 		if  ( !$ this  ->option (CURLOPT_CUSTOMREQUEST ) ) 
141+ 		{
142+ 			$ this  ->option (CURLOPT_CUSTOMREQUEST , 'POST ' );
143+ 		}
144+ 		if  ( $ this  ->headers  ) 
145+ 		{
146+ 			$ this  ->option (CURLOPT_HTTPHEADER , $ this  ->headers );
147+ 		}
148+ 		$ this  ->setupCurlOptions ();
149+ 
150+ 		$ this  ->response  = curl_exec ($ this  ->ch );
151+ 		$ this  ->info  = curl_getinfo ($ this  ->ch );
152+ 
153+ 		if  ( $ this  ->response  === false  ) 
154+ 		{
155+ 			$ this  ->error  = curl_error ($ this  ->ch );
156+ 			$ this  ->errno  = curl_errno ($ this  ->ch );
157+ 			curl_close ($ this  ->ch );
158+ 			return  false ;
159+ 		}
160+ 		else 
161+ 		{
162+ 			curl_close ($ this  ->ch );
163+ 			//未处理http_code。 
164+ 			if  ( $ this  ->info ['content_type ' ] == 'application/json '  ) 
165+ 			{
166+ 				$ this  ->response  = json_decode ($ this  ->response , true );
167+ 			}
168+ 			return  $ this  ->response ;
169+ 		}
170+ 	}
171+ 	public  function  setupCurlOptions ()
172+ 	{
173+ 		curl_setopt_array ($ this  ->ch , $ this  ->options );
174+ 	}
175+ 
176+ 	public  function  option ($ key , $ value  = NULL )
177+ 	{
178+ 		if  ( is_null ($ value ) ) 
179+ 		{
180+ 			return  !isset ($ this  ->options [$ key ]) ? null : $ this  ->options [$ key ];
181+ 		}
182+ 		else 
183+ 		{
184+ 			$ this  ->options [$ key ] = $ value ;
185+ 			return  $ this  ;
186+ 		}
187+ 	}
188+ 
189+ 	public  function  alias ($ key , $ value )
190+ 	{
191+ 		$ this  ->alias [$ key ] = $ value ;
192+ 	}
193+ 
194+ 	protected  function  encode ($ str )
195+ 	{
196+         $ trans  = array ("+ "  => "- " , "/ "  => "_ " );
197+         return  strtr (base64_encode ($ str ), $ trans );
198+ 	}
199+ 
200+ 	public  function  __get ($ key )
201+ 	{
202+ 		return  $ this  ->$ key ;
203+ 	}
204+ 
205+ 	public  function  offsetExists ($ key )
206+ 	{
207+ 		//check response; 
208+ 	}
209+ 
210+ 	public  function  offsetGet ($ key )
211+ 	{
212+ 		return  $ this  ->stat ($ key );
213+ 	}
214+ 
215+ 	public  function  offsetSet ($ key , $ value ) 
216+ 	{
217+ 		//move or copy 
218+ 	}
219+ 
220+ 	public  function  offsetUnset ($ key )
221+ 	{
222+ 		return  $ this  ->delete ();
223+ 	}
224+ }
225+ 
226+ //demo 
227+ $ accessKey  = '' ;
228+ $ secretKey  = '' ;
229+ $ bucket  = "phpsdk " ;
230+ $ sdk  = new  SDK ($ accessKey , $ secretKey , $ bucket );
231+ $ test  = $ sdk ['test.jpg ' ]; // $sdk->stat('test.jpg'); or $sdk->stat('your bucket|your file name'); 
0 commit comments