PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/plugin/PBAPI/PBAPI/Request.php

https://bitbucket.org/chamilo/chamilo-ext-repo-photobucket-dev/
PHP | 398 lines | 175 code | 43 blank | 180 comment | 14 complexity | 17e211c1fd3c6ffd6b0efffe78ba1269 MD5 | raw file
  1. <?php
  2. use common\libraries\Path;
  3. /**
  4. * Photobucket API
  5. * Fluent interface for PHP5
  6. * Request parent class
  7. *
  8. * @author jhart
  9. * @package PBAPI
  10. *
  11. * @copyright Copyright (c) 2008, Photobucket, Inc.
  12. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  13. */
  14. /**
  15. * Require OAuth Request Obj
  16. */
  17. require_once Path :: get_plugin_path() . 'pear/OAuth/Request.php';
  18. /**
  19. * Request method parent class
  20. *
  21. * @package PBAPI
  22. */
  23. abstract class PBAPI_Request
  24. {
  25. /**
  26. * Oauth consumer object
  27. *
  28. * @var OAuth_Consumer
  29. */
  30. private $oauth_consumer;
  31. /**
  32. * Oauth token object
  33. *
  34. * @var OAuth_Token
  35. */
  36. private $oauth_token;
  37. /**
  38. * Current subdomain
  39. *
  40. * @var string
  41. */
  42. protected $subdomain;
  43. /**
  44. * Default format id
  45. *
  46. * @var string
  47. */
  48. protected $default_format;
  49. /**
  50. * Full request URL (for debugging)
  51. *
  52. * @var string
  53. */
  54. public $request_url;
  55. /**
  56. * Oauth Request object (for debugging)
  57. *
  58. * @var OAuthRequest
  59. */
  60. public $oauth_request;
  61. /**
  62. * Request strategy parameters
  63. *
  64. * @var array
  65. */
  66. protected $request_params = array();
  67. /**
  68. * Photobucket Web Login url
  69. *
  70. * @static
  71. * @var string
  72. */
  73. public static $web_login_url = 'http://photobucket.com/apilogin/login';
  74. /**
  75. * Class Constructor
  76. *
  77. * @param array $request_parameters parameters send to set in this object
  78. * @param string $subdomain default subdomain
  79. * @param string $default_format default format
  80. */
  81. public function __construct($subdomain = 'api', $default_format = 'xml', $request_params = array())
  82. {
  83. $this->setSubdomain($subdomain);
  84. $this->setDefaultFormat($default_format);
  85. $this->setRequestParams($request_params);
  86. }
  87. /**
  88. * Set OAuth Consumer information
  89. *
  90. * @param string $consumer_key
  91. * @param string $consumer_secret
  92. */
  93. public function setOAuthConsumer($consumer_key, $consumer_secret)
  94. {
  95. $this->oauth_consumer = new OAuth_Consumer($consumer_key, $consumer_secret);
  96. }
  97. /**
  98. * Set OAuth Token info
  99. *
  100. * @param string $token
  101. * @param string $token_secret
  102. */
  103. public function setOAuthToken($token, $token_secret)
  104. {
  105. $this->oauth_token = new OAuth_Token($token, $token_secret);
  106. }
  107. /**
  108. * Set OAuth Token Object
  109. *
  110. * @param oAuth_Token $oauth_token
  111. */
  112. public function setOAuthTokenObject(OAuth_Token $oauth_token)
  113. {
  114. $this->oauth_token = $oauth_token;
  115. }
  116. /**
  117. * Get OAuth Token info
  118. *
  119. * return OAuth_Token
  120. */
  121. public function getOAuthToken()
  122. {
  123. return $this->oauth_token;
  124. }
  125. /**
  126. * clear oauth token
  127. *
  128. */
  129. public function resetOAuthToken()
  130. {
  131. $this->oauth_token = null;
  132. }
  133. /**
  134. * set subdomain value
  135. *
  136. * @param string $subdomain
  137. */
  138. public function setSubdomain($subdomain)
  139. {
  140. $subdomain = preg_replace('#\.photobucket\.com(/.*)?$#', '', $subdomain);
  141. $this->subdomain = $subdomain;
  142. }
  143. /**
  144. * get subdomain value
  145. *
  146. * @return string subdomain
  147. */
  148. public function getSubdomain()
  149. {
  150. return $this->subdomain;
  151. }
  152. /**
  153. * get whole subdomain url
  154. *
  155. * @param string $uri uri ending
  156. * @return string http://...
  157. */
  158. public function getSubdomainUrl($uri)
  159. {
  160. return 'http://' . $this->subdomain . '.photobucket.com/' . trim($uri, '/');
  161. }
  162. /**
  163. * set default response format
  164. *
  165. * @param string $format
  166. */
  167. public function setDefaultFormat($format)
  168. {
  169. //todo verify allowed formats
  170. $this->default_format = $format;
  171. }
  172. /**
  173. * Request params
  174. *
  175. * @param array $params request params
  176. */
  177. public function setRequestParams($params)
  178. {
  179. $this->request_params = $params;
  180. }
  181. /**
  182. * Get OAuthRequest for given items
  183. *
  184. * @param string $method HTTP method
  185. * @param string $uri URI (no http/host)
  186. * @param array $params key=>value parameters
  187. * @return OAuthRequest
  188. */
  189. protected function getSignedOAuthRequest($method, $uri, array $params)
  190. {
  191. $req = OAuth_Request :: fromConsumerAndToken($this->oauth_consumer, $this->oauth_token, $method, 'http://api.photobucket.com/' . trim($uri, '/'), $params);
  192. $req->signRequest('HMAC-SHA1', $this->oauth_consumer, $this->oauth_token);
  193. return $req;
  194. }
  195. /**
  196. * Pre-Request filter
  197. *
  198. * @param string $method (ref) HTTP method
  199. * @param string $uri URI (no http://)
  200. * @param array $params parameters
  201. * @return string finished request url
  202. */
  203. protected function preRequest(&$method, &$uri, array &$params)
  204. {
  205. //cleanup method
  206. $method = strtoupper($method);
  207. //cleanup/determine format
  208. if ($this->default_format && ! array_key_exists('format', $params))
  209. {
  210. $params['format'] = $this->default_format;
  211. }
  212. //block uploadfile from parameters
  213. $uploadfile = null;
  214. if (! empty($params['uploadfile']))
  215. {
  216. $uploadfile = $params['uploadfile'];
  217. unset($params['uploadfile']);
  218. }
  219. //get fullly signed request
  220. $req = $this->getSignedOAuthRequest($method, $uri, $params);
  221. $url = '';
  222. $params = array();
  223. //rebuild url and request with uploadfile
  224. if ($method != 'POST')
  225. {
  226. $url = $this->getSubdomainUrl($uri) . '?' . $req->toPostdata();
  227. }
  228. else
  229. {
  230. $url = $this->getSubDomainUrl($uri);
  231. if ($uploadfile)
  232. {
  233. $req->setParameter('uploadfile', $uploadfile);
  234. }
  235. $params = $req->getParameters();
  236. }
  237. $this->request_url = $url;
  238. $this->oauth_request = $req;
  239. return $url;
  240. }
  241. /**
  242. * Redirect to the login page (for given token)
  243. *
  244. * @param string $extra optional extra parameter to pass along (if you need to store a key without a cookie, for example)
  245. */
  246. public function redirectLogin($extra = null)
  247. {
  248. if (! $this->oauth_token || self :: getOAuthTokenType($this->oauth_token) != 'req')
  249. {
  250. throw new PBAPI_Exception('OAuth Token is not a request token');
  251. }
  252. $req = $this->oauth_token->getKey();
  253. $url = self :: $web_login_url . '?oauth_token=' . $req;
  254. if ($extra)
  255. $url .= '&extra=' . $extra;
  256. header('Location: ' . $url);
  257. exit();
  258. }
  259. /**#@+
  260. * Request function
  261. *
  262. * @param string $uri
  263. * @param array $params
  264. */
  265. public function get($uri, $params = array())
  266. {
  267. return $this->request('GET', $uri, $params);
  268. }
  269. public function post($uri, $params = array())
  270. {
  271. return $this->request('POST', $uri, $params);
  272. }
  273. public function put($uri, $params = array())
  274. {
  275. return $this->request('PUT', $uri, $params);
  276. }
  277. public function delete($uri, $params = array())
  278. {
  279. return $this->request('DELETE', $uri, $params);
  280. }
  281. /**#@-*/
  282. /**
  283. * Actual Request function
  284. *
  285. * @param string $method
  286. * @param string $uri
  287. * @param array $params
  288. * @return string
  289. */
  290. abstract protected function request($method, $uri, $params = array());
  291. /**
  292. * Turn parameters into multipart encoded string
  293. *
  294. * @param array $params key value pairs of parameters
  295. * @param string $bound boundary (should be relatively unique)
  296. * @return string mime type multipart-form
  297. */
  298. public static function multipartEncodeParams(array $params, $bound)
  299. {
  300. $bound = '--' . trim($bound) . "\n";
  301. $result = '';
  302. $paramStr = array();
  303. foreach ($params as $key => $val)
  304. {
  305. $file = false;
  306. if (strpos($val, '@') === 0)
  307. {
  308. $filepath = trim($val, '@');
  309. $val = file_get_contents($filepath);
  310. $disp = 'content-disposition: form-data; name="' . $key . '"; filename="' . basename($filepath) . '"' . "\n";
  311. $mimetype = 'mimetype';
  312. $disp .= 'content-type: ' . $mimetype . "\n";
  313. $disp .= 'content-transfer-encoding: binary' . "\n";
  314. }
  315. else
  316. {
  317. $disp = 'content-disposition: form-data; name="' . $key . '"' . "\n";
  318. }
  319. $paramStr[] = $disp . "\n" . $val . "\n";
  320. }
  321. $result = $bound . implode($bound, $paramStr) . $bound;
  322. return $result;
  323. }
  324. /**
  325. * See if the given array has an upload filename as a parameter
  326. *
  327. * @param array $params parameters
  328. * @return bool array has at least one upload filename parameter
  329. */
  330. public static function detectFileUploadParams($params = array())
  331. {
  332. foreach ($params as $p)
  333. {
  334. if (strpos($p, '@') === 0)
  335. return true;
  336. }
  337. return false;
  338. }
  339. /**
  340. * Determine an OAuth_Token's type
  341. *
  342. * @param OAuth_Token $token
  343. * @return string type of token [req|user]
  344. */
  345. public static function getOAuthTokenType(OAuth_Token $token)
  346. {
  347. if (! $token)
  348. return false;
  349. if (strpos($token->getKey(), 'req_') === 0)
  350. $type = 'req';
  351. else
  352. $type = 'user';
  353. return $type;
  354. }
  355. }