PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jymengine.class.php

http://github.com/yahoo/messenger-sdk-php
PHP | 522 lines | 375 code | 87 blank | 60 comment | 33 complexity | fb1d8e047626d4c8d94afbb3071ef58c MD5 | raw file
  1. <?php
  2. /*
  3. *
  4. *Software Copyright License Agreement (BSD License)
  5. *
  6. *Copyright (c) 2010, Yahoo! Inc.
  7. *All rights reserved.
  8. *
  9. *Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  10. *
  11. ** Redistributions of source code must retain the above
  12. * copyright notice, this list of conditions and the
  13. * following disclaimer.
  14. *
  15. ** Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the
  17. * following disclaimer in the documentation and/or other
  18. * materials provided with the distribution.
  19. *
  20. ** Neither the name of Yahoo! Inc. nor the names of its
  21. * contributors may be used to endorse or promote products
  22. * derived from this software without specific prior
  23. * written permission of Yahoo! Inc.
  24. *
  25. *THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. class JYMEngine
  29. {
  30. const URL_OAUTH_DIRECT = 'https://login.yahoo.com/WSLogin/V1/get_auth_token';
  31. const URL_OAUTH_ACCESS_TOKEN = 'https://api.login.yahoo.com/oauth/v2/get_token';
  32. const URL_YM_SESSION = 'http://developer.messenger.yahooapis.com/v1/session';
  33. const URL_YM_PRESENCE = 'http://developer.messenger.yahooapis.com/v1/presence';
  34. const URL_YM_CONTACT = 'http://developer.messenger.yahooapis.com/v1/contacts';
  35. const URL_YM_MESSAGE = 'http://developer.messenger.yahooapis.com/v1/message/yahoo/{{USER}}';
  36. const URL_YM_NOTIFICATION = 'http://developer.messenger.yahooapis.com/v1/notifications';
  37. const URL_YM_NOTIFICATION_LONG = 'http://{{NOTIFICATION_SERVER}}/v1/pushchannel/{{USER}}';
  38. const URL_YM_BUDDYREQUEST = 'http://developer.messenger.yahooapis.com/v1/buddyrequest/yahoo/{{USER}}';
  39. const URL_YM_GROUP = 'http://developer.messenger.yahooapis.com/v1/group/{{GROUP}}/contact/yahoo/{{USER}}';
  40. protected $_oauth;
  41. protected $_token;
  42. protected $_ym;
  43. protected $_config;
  44. protected $_error;
  45. public $includeheader = false;
  46. public $debug = false;
  47. /*
  48. * constructor
  49. */
  50. public function __construct($consumer_key = '', $secret_key = '', $username = '', $password = '')
  51. {
  52. $this->_config['consumer_key'] = $consumer_key;
  53. $this->_config['secret_key'] = $secret_key;
  54. $this->_config['username'] = $username;
  55. $this->_config['password'] = $password;
  56. $this->_ym = array();
  57. $this->_error = null;
  58. }
  59. public function fetch_request_token()
  60. {
  61. //prepare url
  62. $url = $this::URL_OAUTH_DIRECT;
  63. $url .= '?login='. $this->_config['username'];
  64. $url .= '&passwd='. $this->_config['password'];
  65. $url .= '&oauth_consumer_key='. $this->_config['consumer_key'];
  66. $rs = $this->curl($url);
  67. if (stripos($rs, 'RequestToken') === false) return false;
  68. $request_token = trim(str_replace('RequestToken=', '', $rs));
  69. $this->_token['request'] = $request_token;
  70. return true;
  71. }
  72. public function fetch_access_token()
  73. {
  74. //prepare url
  75. $url = $this::URL_OAUTH_ACCESS_TOKEN;
  76. $url .= '?oauth_consumer_key='. $this->_config['consumer_key'];
  77. $url .= '&oauth_nonce='. uniqid(rand());
  78. $url .= '&oauth_signature='. $this->_config['secret_key']. '%26';
  79. $url .= '&oauth_signature_method=PLAINTEXT';
  80. $url .= '&oauth_timestamp='. time();
  81. $url .= '&oauth_token='. $this->_token['request'];
  82. $url .= '&oauth_version=1.0';
  83. $rs = $this->curl($url);
  84. if (stripos($rs, 'oauth_token') === false)
  85. {
  86. $this->_error = $rs;
  87. return false;
  88. }
  89. //parse access token
  90. $tmp = explode('&', $rs);
  91. foreach ($tmp as $row)
  92. {
  93. $col = explode('=', $row);
  94. $access_token[$col[0]] = $col[1];
  95. }
  96. $this->_token['access'] = $access_token;
  97. return true;
  98. }
  99. public function fetch_crumb()
  100. {
  101. //prepare url
  102. $url = $this::URL_YM_SESSION;
  103. $url .= '?oauth_consumer_key='. $this->_config['consumer_key'];
  104. $url .= '&oauth_nonce='. uniqid(rand());
  105. $url .= '&oauth_signature='. $this->_config['secret_key']. '%26'. $this->_token['access']['oauth_token_secret'];
  106. $url .= '&oauth_signature_method=PLAINTEXT';
  107. $url .= '&oauth_timestamp='. time();
  108. $url .= '&oauth_token='. $this->_token['access']['oauth_token'];
  109. $url .= '&oauth_version=1.0';
  110. //additional header
  111. $header[] = 'Content-type: application/json; charset=utf-8';
  112. $rs = $this->curl($url, 'get', $header);
  113. if (stripos($rs, 'crumb') === false) return false;
  114. $js = json_decode($rs, true);
  115. $this->_ym['crumb'] = $js;
  116. return true;
  117. }
  118. public function signon($status = '', $state = 0)
  119. {
  120. //prepare url
  121. $url = $this::URL_YM_SESSION;
  122. $url .= '?oauth_consumer_key='. $this->_config['consumer_key'];
  123. $url .= '&oauth_nonce='. uniqid(rand());
  124. $url .= '&oauth_signature='. $this->_config['secret_key']. '%26'. $this->_token['access']['oauth_token_secret'];
  125. $url .= '&oauth_signature_method=PLAINTEXT';
  126. $url .= '&oauth_timestamp='. time();
  127. $url .= '&oauth_token='. $this->_token['access']['oauth_token'];
  128. $url .= '&oauth_version=1.0';
  129. $url .= '&notifyServerToken=1';
  130. //additional header
  131. $header[] = 'Content-type: application/json; charset=utf-8';
  132. $postdata = '{"presenceState" : '. $state. ', "presenceMessage" : "'. $status. '"}';
  133. $this->includeheader = true;
  134. $rs = $this->curl($url, 'post', $header, $postdata);
  135. $this->includeheader = false;
  136. list($header, $body) = explode("\r\n\r\n", $rs, 2);
  137. //get IM cookie
  138. $notifytoken = '';
  139. if (preg_match('/set-cookie: IM=(.+?); expires/', $header, $m))
  140. $notifytoken = $m[1];
  141. if (stripos($body, 'sessionId') === false) return false;
  142. $js = json_decode($body, true);
  143. $js['notifytoken'] = $notifytoken;
  144. $this->_ym['signon'] = $js;
  145. return true;
  146. }
  147. public function signoff()
  148. {
  149. //prepare url
  150. $url = $this::URL_YM_SESSION;
  151. $url .= '?oauth_consumer_key='. $this->_config['consumer_key'];
  152. $url .= '&oauth_nonce='. uniqid(rand());
  153. $url .= '&oauth_signature='. $this->_config['secret_key']. '%26'. $this->_token['access']['oauth_token_secret'];
  154. $url .= '&oauth_signature_method=PLAINTEXT';
  155. $url .= '&oauth_timestamp='. time();
  156. $url .= '&oauth_token='. $this->_token['access']['oauth_token'];
  157. $url .= '&oauth_version=1.0';
  158. $url .= '&sid='. $this->_ym['signon']['sessionId'];
  159. //additional header
  160. $header[] = 'Content-type: application/json; charset=utf-8';
  161. $rs = $this->curl($url, 'delete', $header);
  162. return true;
  163. }
  164. public function change_presence($status = '', $state = 0)
  165. {
  166. //prepare url
  167. $url = $this::URL_YM_PRESENCE;
  168. $url .= '?oauth_consumer_key='. $this->_config['consumer_key'];
  169. $url .= '&oauth_nonce='. uniqid(rand());
  170. $url .= '&oauth_signature='. $this->_config['secret_key']. '%26'. $this->_token['access']['oauth_token_secret'];
  171. $url .= '&oauth_signature_method=PLAINTEXT';
  172. $url .= '&oauth_timestamp='. time();
  173. $url .= '&oauth_token='. $this->_token['access']['oauth_token'];
  174. $url .= '&oauth_version=1.0';
  175. $url .= '&sid='. $this->_ym['signon']['sessionId'];
  176. //additional header
  177. $header[] = 'Content-type: application/json; charset=utf-8';
  178. $postdata = '{"presenceState" : '. $state. ', "presenceMessage" : "'. $status. '"}';
  179. $rs = $this->curl($url, 'put', $header, $postdata);
  180. return true;
  181. }
  182. public function send_message($user, $message)
  183. {
  184. //prepare url
  185. $url = $this::URL_YM_MESSAGE;
  186. $url .= '?oauth_consumer_key='. $this->_config['consumer_key'];
  187. $url .= '&oauth_nonce='. uniqid(rand());
  188. $url .= '&oauth_signature='. $this->_config['secret_key']. '%26'. $this->_token['access']['oauth_token_secret'];
  189. $url .= '&oauth_signature_method=PLAINTEXT';
  190. $url .= '&oauth_timestamp='. time();
  191. $url .= '&oauth_token='. $this->_token['access']['oauth_token'];
  192. $url .= '&oauth_version=1.0';
  193. $url .= '&sid='. $this->_ym['signon']['sessionId'];
  194. $url = str_replace('{{USER}}', $user, $url);
  195. //additional header
  196. $header[] = 'Content-type: application/json; charset=utf-8';
  197. $postdata = '{"message" : '. $message. '}';
  198. $rs = $this->curl($url, 'post', $header, $postdata);
  199. return true;
  200. }
  201. public function fetch_contact_list()
  202. {
  203. //prepare url
  204. $url = $this::URL_YM_CONTACT;
  205. $url .= '?oauth_consumer_key='. $this->_config['consumer_key'];
  206. $url .= '&oauth_nonce='. uniqid(rand());
  207. $url .= '&oauth_signature='. $this->_config['secret_key']. '%26'. $this->_token['access']['oauth_token_secret'];
  208. $url .= '&oauth_signature_method=PLAINTEXT';
  209. $url .= '&oauth_timestamp='. time();
  210. $url .= '&oauth_token='. $this->_token['access']['oauth_token'];
  211. $url .= '&oauth_version=1.0';
  212. $url .= '&sid='. $this->_ym['signon']['sessionId'];
  213. $url .= '&fields=%2Bpresence';
  214. $url .= '&fields=%2Bgroups';
  215. //additional header
  216. $header[] = 'Content-type: application/json; charset=utf-8';
  217. $rs = $this->curl($url, 'get', $header);
  218. if (stripos($rs, 'contact') === false) return false;
  219. $js = json_decode($rs, true);
  220. return $js['contacts'];
  221. }
  222. public function add_contact($user, $group = 'Friends', $message = '')
  223. {
  224. //prepare url
  225. $url = $this::URL_YM_GROUP;
  226. $url .= '?oauth_consumer_key='. $this->_config['consumer_key'];
  227. $url .= '&oauth_nonce='. uniqid(rand());
  228. $url .= '&oauth_signature='. $this->_config['secret_key']. '%26'. $this->_token['access']['oauth_token_secret'];
  229. $url .= '&oauth_signature_method=PLAINTEXT';
  230. $url .= '&oauth_timestamp='. time();
  231. $url .= '&oauth_token='. $this->_token['access']['oauth_token'];
  232. $url .= '&oauth_version=1.0';
  233. $url .= '&sid='. $this->_ym['signon']['sessionId'];
  234. $url = str_replace('{{GROUP}}', $group, $url);
  235. $url = str_replace('{{USER}}', $user, $url);
  236. //additional header
  237. $header[] = 'Content-type: application/json; charset=utf-8';
  238. $postdata = '{"message" : "'. $message. '"}';
  239. $rs = $this->curl($url, 'put', $header, $postdata);
  240. return true;
  241. }
  242. public function delete_contact($user, $group = 'Friends')
  243. {
  244. //prepare url
  245. $url = $this::URL_YM_GROUP;
  246. $url .= '?oauth_consumer_key='. $this->_config['consumer_key'];
  247. $url .= '&oauth_nonce='. uniqid(rand());
  248. $url .= '&oauth_signature='. $this->_config['secret_key']. '%26'. $this->_token['access']['oauth_token_secret'];
  249. $url .= '&oauth_signature_method=PLAINTEXT';
  250. $url .= '&oauth_timestamp='. time();
  251. $url .= '&oauth_token='. $this->_token['access']['oauth_token'];
  252. $url .= '&oauth_version=1.0';
  253. $url .= '&sid='. $this->_ym['signon']['sessionId'];
  254. $url = str_replace('{{GROUP}}', $group, $url);
  255. $url = str_replace('{{USER}}', $user, $url);
  256. //additional header
  257. $header[] = 'Content-type: application/json; charset=utf-8';
  258. $rs = $this->curl($url, 'delete', $header);
  259. return true;
  260. }
  261. public function response_contact($user, $accept = true, $message = '')
  262. {
  263. if ($accept)
  264. {
  265. $method = 'POST';
  266. $message == '' ? $reason = 'Welcome' : $reason = $message;
  267. }
  268. else
  269. {
  270. $method = 'DELETE';
  271. $message == '' ? $reason = 'No thanks' : $reason = $message;
  272. }
  273. //prepare url
  274. $url = $this::URL_YM_BUDDYREQUEST;
  275. $url .= '?oauth_consumer_key='. $this->_config['consumer_key'];
  276. $url .= '&oauth_nonce='. uniqid(rand());
  277. $url .= '&oauth_signature='. $this->_config['secret_key']. '%26'. $this->_token['access']['oauth_token_secret'];
  278. $url .= '&oauth_signature_method=PLAINTEXT';
  279. $url .= '&oauth_timestamp='. time();
  280. $url .= '&oauth_token='. $this->_token['access']['oauth_token'];
  281. $url .= '&oauth_version=1.0';
  282. $url .= '&sid='. $this->_ym['signon']['sessionId'];
  283. $url = str_replace('{{USER}}', $user, $url);
  284. //additional header
  285. $header[] = 'Content-type: application/json; charset=utf-8';
  286. $postdata = '{"authReason" : "'. $reason. '"}';
  287. $this->includeheader = true;
  288. $rs = $this->curl($url, strtolower($method), $header, $postdata);
  289. $this->includeheader = true;
  290. if (strpos($rs, "\r\n\r\n") === false)
  291. {
  292. $this->_error = -20;
  293. return false;
  294. }
  295. list($header, $body) = explode("\r\n\r\n", $rs, 2);
  296. //get status code
  297. $code = '';
  298. if (preg_match('|HTTP/1.1 (.*?) OK|', $header, $m))
  299. $code = $m[1];
  300. if ($code != 200)
  301. {
  302. $this->_error = $code;
  303. return false;
  304. }
  305. return true;
  306. }
  307. public function fetch_notification($seq = 0)
  308. {
  309. //prepare url
  310. $url = $this::URL_YM_NOTIFICATION;
  311. $url .= '?oauth_consumer_key='. $this->_config['consumer_key'];
  312. $url .= '&oauth_nonce='. uniqid(rand());
  313. $url .= '&oauth_signature='. $this->_config['secret_key']. '%26'. $this->_token['access']['oauth_token_secret'];
  314. $url .= '&oauth_signature_method=PLAINTEXT';
  315. $url .= '&oauth_timestamp='. time();
  316. $url .= '&oauth_token='. $this->_token['access']['oauth_token'];
  317. $url .= '&oauth_version=1.0';
  318. $url .= '&sid='. $this->_ym['signon']['sessionId'];
  319. $url .= '&seq='. intval($seq);
  320. $url .= '&count=100';
  321. //additional header
  322. $header[] = 'Content-type: application/json; charset=utf-8';
  323. $rs = $this->curl($url, 'get', $header);
  324. $js = json_decode($rs, true);
  325. if (isset($js['error']))
  326. {
  327. $this->_error = $js['error'];
  328. return false;
  329. }
  330. if (stripos($rs, 'pendingMsg') === false) return false;
  331. if (count($js['responses']) < 1) return false;
  332. $this->_error = null;
  333. return $js['responses'];
  334. }
  335. public function fetch_long_notification($seq = 0)
  336. {
  337. //prepare url
  338. $url = $this::URL_YM_NOTIFICATION_LONG;
  339. $url .= '?oauth_consumer_key='. $this->_config['consumer_key'];
  340. $url .= '&oauth_nonce='. uniqid(rand());
  341. $url .= '&oauth_signature='. $this->_config['secret_key']. '%26'. $this->_token['access']['oauth_token_secret'];
  342. $url .= '&oauth_signature_method=PLAINTEXT';
  343. $url .= '&oauth_timestamp='. time();
  344. $url .= '&oauth_token='. $this->_token['access']['oauth_token'];
  345. $url .= '&oauth_version=1.0';
  346. $url .= '&sid='. $this->_ym['signon']['sessionId'];
  347. $url .= '&seq='. intval($seq);
  348. $url .= '&format=json';
  349. $url .= '&count=100';
  350. $url .= '&idle=120';
  351. $url .= '&rand='. uniqid(rand());
  352. $url .= '&IM='. $this->_ym['signon']['notifytoken'];
  353. $url = str_replace('{{NOTIFICATION_SERVER}}', $this->_ym['signon']['notifyServer'], $url);
  354. $url = str_replace('{{USER}}', $this->_ym['signon']['primaryLoginId'], $url);
  355. //additional header
  356. $header[] = 'Content-type: application/json; charset=utf-8';
  357. $header[] = 'Connection: keep-alive';
  358. $this->includeheader = true;
  359. $rs = $this->curl($url, 'get', $header, null, 160);
  360. $this->includeheader = false;
  361. if (strpos($rs, "\r\n\r\n") === false)
  362. {
  363. $this->_error = -20;
  364. return false;
  365. }
  366. list($header, $body) = explode("\r\n\r\n", $rs, 2);
  367. //get status code
  368. $code = '';
  369. if (preg_match('|HTTP/1.1 (.*?) OK|', $header, $m))
  370. $code = $m[1];
  371. if ($code != 200)
  372. {
  373. $this->_error = $code;
  374. return false;
  375. }
  376. if ($body == '')
  377. {
  378. $this->_error = -10;
  379. return false;
  380. }
  381. $js = json_decode($body, true);
  382. if (isset($js['error']))
  383. {
  384. $this->_error = $js['error'];
  385. return false;
  386. }
  387. $this->_error = null;
  388. return $js['responses'];
  389. }
  390. /*
  391. * fetch url using curl
  392. */
  393. private function curl($url, $method = 'get', $header = null, $postdata = null, $timeout = 60)
  394. {
  395. $s = curl_init();
  396. curl_setopt($s,CURLOPT_URL, $url);
  397. if ($header)
  398. curl_setopt($s,CURLOPT_HTTPHEADER, $header);
  399. if ($this->debug)
  400. curl_setopt($s,CURLOPT_VERBOSE, TRUE);
  401. curl_setopt($s,CURLOPT_TIMEOUT, $timeout);
  402. curl_setopt($s,CURLOPT_CONNECTTIMEOUT, $timeout);
  403. curl_setopt($s,CURLOPT_MAXREDIRS, 3);
  404. curl_setopt($s,CURLOPT_RETURNTRANSFER, true);
  405. curl_setopt($s,CURLOPT_FOLLOWLOCATION, 1);
  406. curl_setopt($s,CURLOPT_COOKIEJAR, 'cookie.txt');
  407. curl_setopt($s,CURLOPT_COOKIEFILE, 'cookie.txt');
  408. if(strtolower($method) == 'post')
  409. {
  410. curl_setopt($s,CURLOPT_POST, true);
  411. curl_setopt($s,CURLOPT_POSTFIELDS, $postdata);
  412. }
  413. else if(strtolower($method) == 'delete')
  414. {
  415. curl_setopt($s,CURLOPT_CUSTOMREQUEST, 'DELETE');
  416. }
  417. else if(strtolower($method) == 'put')
  418. {
  419. curl_setopt($s,CURLOPT_CUSTOMREQUEST, 'PUT');
  420. curl_setopt($s,CURLOPT_POSTFIELDS, $postdata);
  421. }
  422. curl_setopt($s,CURLOPT_HEADER, $this->includeheader);
  423. curl_setopt($s,CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1');
  424. curl_setopt($s, CURLOPT_SSL_VERIFYPEER, false);
  425. $html = curl_exec($s);
  426. $status = curl_getinfo($s, CURLINFO_HTTP_CODE);
  427. curl_close($s);
  428. return $html;
  429. }
  430. public function get_error()
  431. {
  432. return $this->_error;
  433. }
  434. }
  435. ?>