PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/twitterapi.php

https://github.com/Br3nda/laconica
PHP | 901 lines | 676 code | 145 blank | 80 comment | 91 complexity | 62ebc572f2fe87f17408aa9d7774f331 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /*
  3. * Laconica - a distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, Control Yourself, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('LACONICA')) {
  20. exit(1);
  21. }
  22. class TwitterapiAction extends Action
  23. {
  24. var $auth_user;
  25. /**
  26. * Initialization.
  27. *
  28. * @param array $args Web and URL arguments
  29. *
  30. * @return boolean false if user doesn't exist
  31. */
  32. function prepare($args)
  33. {
  34. parent::prepare($args);
  35. return true;
  36. }
  37. /**
  38. * Handle a request
  39. *
  40. * @param array $args Arguments from $_REQUEST
  41. *
  42. * @return void
  43. */
  44. function handle($args)
  45. {
  46. parent::handle($args);
  47. }
  48. /**
  49. * Overrides XMLOutputter::element to write booleans as strings (true|false).
  50. * See that method's documentation for more info.
  51. *
  52. * @param string $tag Element type or tagname
  53. * @param array $attrs Array of element attributes, as
  54. * key-value pairs
  55. * @param string $content string content of the element
  56. *
  57. * @return void
  58. */
  59. function element($tag, $attrs=null, $content=null)
  60. {
  61. if (is_bool($content)) {
  62. $content = ($content ? 'true' : 'false');
  63. }
  64. return parent::element($tag, $attrs, $content);
  65. }
  66. function twitter_user_array($profile, $get_notice=false)
  67. {
  68. $twitter_user = array();
  69. $twitter_user['id'] = intval($profile->id);
  70. $twitter_user['name'] = $profile->getBestName();
  71. $twitter_user['screen_name'] = $profile->nickname;
  72. $twitter_user['location'] = ($profile->location) ? $profile->location : null;
  73. $twitter_user['description'] = ($profile->bio) ? $profile->bio : null;
  74. $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
  75. $twitter_user['profile_image_url'] = ($avatar) ? $avatar->displayUrl() :
  76. Avatar::defaultImage(AVATAR_STREAM_SIZE);
  77. $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
  78. $twitter_user['protected'] = false; # not supported by Laconica yet
  79. $twitter_user['followers_count'] = $profile->subscriberCount();
  80. // To be supported soon...
  81. $twitter_user['profile_background_color'] = '';
  82. $twitter_user['profile_text_color'] = '';
  83. $twitter_user['profile_link_color'] = '';
  84. $twitter_user['profile_sidebar_fill_color'] = '';
  85. $twitter_user['profile_sidebar_border_color'] = '';
  86. $twitter_user['friends_count'] = $profile->subscriptionCount();
  87. $twitter_user['created_at'] = $this->date_twitter($profile->created);
  88. $twitter_user['favourites_count'] = $profile->faveCount(); // British spelling!
  89. // Need to pull up the user for some of this
  90. $user = User::staticGet($profile->id);
  91. $timezone = 'UTC';
  92. if ($user->timezone) {
  93. $timezone = $user->timezone;
  94. }
  95. $t = new DateTime;
  96. $t->setTimezone(new DateTimeZone($timezone));
  97. $twitter_user['utc_offset'] = $t->format('Z');
  98. $twitter_user['time_zone'] = $timezone;
  99. // To be supported some day, perhaps
  100. $twitter_user['profile_background_image_url'] = '';
  101. $twitter_user['profile_background_tile'] = false;
  102. $twitter_user['statuses_count'] = $profile->noticeCount();
  103. // Is the requesting user following this user?
  104. $twitter_user['following'] = false;
  105. $twitter_user['notifications'] = false;
  106. if (isset($apidata['user'])) {
  107. $twitter_user['following'] = $apidata['user']->isSubscribed($profile);
  108. // Notifications on?
  109. $sub = Subscription::pkeyGet(array('subscriber' =>
  110. $apidata['user']->id, 'subscribed' => $profile->id));
  111. if ($sub) {
  112. $twitter_user['notifications'] = ($sub->jabber || $sub->sms);
  113. }
  114. }
  115. if ($get_notice) {
  116. $notice = $profile->getCurrentNotice();
  117. if ($notice) {
  118. # don't get user!
  119. $twitter_user['status'] = $this->twitter_status_array($notice, false);
  120. }
  121. }
  122. return $twitter_user;
  123. }
  124. function twitter_status_array($notice, $include_user=true)
  125. {
  126. $profile = $notice->getProfile();
  127. $twitter_status = array();
  128. $twitter_status['text'] = $notice->content;
  129. $twitter_status['truncated'] = false; # Not possible on Laconica
  130. $twitter_status['created_at'] = $this->date_twitter($notice->created);
  131. $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ?
  132. intval($notice->reply_to) : null;
  133. $twitter_status['source'] = $this->source_link($notice->source);
  134. $twitter_status['id'] = intval($notice->id);
  135. $replier_profile = null;
  136. if ($notice->reply_to) {
  137. $reply = Notice::staticGet(intval($notice->reply_to));
  138. if ($reply) {
  139. $replier_profile = $reply->getProfile();
  140. }
  141. }
  142. $twitter_status['in_reply_to_user_id'] =
  143. ($replier_profile) ? intval($replier_profile->id) : null;
  144. $twitter_status['in_reply_to_screen_name'] =
  145. ($replier_profile) ? $replier_profile->nickname : null;
  146. if (isset($this->auth_user)) {
  147. $twitter_status['favorited'] = $this->auth_user->hasFave($notice);
  148. } else {
  149. $twitter_status['favorited'] = false;
  150. }
  151. # Enclosures
  152. $attachments = $notice->attachments();
  153. $twitter_status['attachments']=array();
  154. if($attachments){
  155. foreach($attachments as $attachment){
  156. if ($attachment->isEnclosure()) {
  157. $enclosure=array();
  158. $enclosure['url']=$attachment->url;
  159. $enclosure['mimetype']=$attachment->mimetype;
  160. $enclosure['size']=$attachment->size;
  161. $twitter_status['attachments'][]=$enclosure;
  162. }
  163. }
  164. }
  165. if ($include_user) {
  166. # Don't get notice (recursive!)
  167. $twitter_user = $this->twitter_user_array($profile, false);
  168. $twitter_status['user'] = $twitter_user;
  169. }
  170. return $twitter_status;
  171. }
  172. function twitter_rss_entry_array($notice)
  173. {
  174. $profile = $notice->getProfile();
  175. $entry = array();
  176. # We trim() to avoid extraneous whitespace in the output
  177. $entry['content'] = common_xml_safe_str(trim($notice->rendered));
  178. $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
  179. $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
  180. $entry['published'] = common_date_iso8601($notice->created);
  181. $taguribase = common_config('integration', 'taguri');
  182. $entry['id'] = "tag:$taguribase:$entry[link]";
  183. $entry['updated'] = $entry['published'];
  184. $entry['author'] = $profile->getBestName();
  185. # Enclosure
  186. $attachments = $notice->attachments();
  187. if($attachments){
  188. $entry['enclosures']=array();
  189. foreach($attachments as $attachment){
  190. if ($attachment->isEnclosure()) {
  191. $enclosure=array();
  192. $enclosure['url']=$attachment->url;
  193. $enclosure['mimetype']=$attachment->mimetype;
  194. $enclosure['size']=$attachment->size;
  195. $entry['enclosures'][]=$enclosure;
  196. }
  197. }
  198. }
  199. # RSS Item specific
  200. $entry['description'] = $entry['content'];
  201. $entry['pubDate'] = common_date_rfc2822($notice->created);
  202. $entry['guid'] = $entry['link'];
  203. return $entry;
  204. }
  205. function twitter_rss_dmsg_array($message)
  206. {
  207. $entry = array();
  208. $entry['title'] = sprintf('Message from %s to %s',
  209. $message->getFrom()->nickname, $message->getTo()->nickname);
  210. $entry['content'] = common_xml_safe_str(trim($message->content));
  211. $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
  212. $entry['published'] = common_date_iso8601($message->created);
  213. $taguribase = common_config('integration', 'taguri');
  214. $entry['id'] = "tag:$taguribase,:$entry[link]";
  215. $entry['updated'] = $entry['published'];
  216. $entry['author'] = $message->getFrom()->getBestName();
  217. # RSS Item specific
  218. $entry['description'] = $entry['content'];
  219. $entry['pubDate'] = common_date_rfc2822($message->created);
  220. $entry['guid'] = $entry['link'];
  221. return $entry;
  222. }
  223. function twitter_dmsg_array($message)
  224. {
  225. $twitter_dm = array();
  226. $from_profile = $message->getFrom();
  227. $to_profile = $message->getTo();
  228. $twitter_dm['id'] = $message->id;
  229. $twitter_dm['sender_id'] = $message->from_profile;
  230. $twitter_dm['text'] = trim($message->content);
  231. $twitter_dm['recipient_id'] = $message->to_profile;
  232. $twitter_dm['created_at'] = $this->date_twitter($message->created);
  233. $twitter_dm['sender_screen_name'] = $from_profile->nickname;
  234. $twitter_dm['recipient_screen_name'] = $to_profile->nickname;
  235. $twitter_dm['sender'] = $this->twitter_user_array($from_profile, false);
  236. $twitter_dm['recipient'] = $this->twitter_user_array($to_profile, false);
  237. return $twitter_dm;
  238. }
  239. function twitter_relationship_array($source, $target)
  240. {
  241. $relationship = array();
  242. $relationship['source'] =
  243. $this->relationship_details_array($source, $target);
  244. $relationship['target'] =
  245. $this->relationship_details_array($target, $source);
  246. return array('relationship' => $relationship);
  247. }
  248. function relationship_details_array($source, $target)
  249. {
  250. $details = array();
  251. $details['screen_name'] = $source->nickname;
  252. $details['followed_by'] = $target->isSubscribed($source);
  253. $details['following'] = $source->isSubscribed($target);
  254. $notifications = false;
  255. if ($source->isSubscribed($target)) {
  256. $sub = Subscription::pkeyGet(array('subscriber' =>
  257. $source->id, 'subscribed' => $target->id));
  258. if (!empty($sub)) {
  259. $notifications = ($sub->jabber || $sub->sms);
  260. }
  261. }
  262. $details['notifications_enabled'] = $notifications;
  263. $details['blocking'] = $source->hasBlocked($target);
  264. $details['id'] = $source->id;
  265. return $details;
  266. }
  267. function show_twitter_xml_relationship($relationship)
  268. {
  269. $this->elementStart('relationship');
  270. foreach($relationship as $element => $value) {
  271. if ($element == 'source' || $element == 'target') {
  272. $this->elementStart($element);
  273. $this->show_xml_relationship_details($value);
  274. $this->elementEnd($element);
  275. }
  276. }
  277. $this->elementEnd('relationship');
  278. }
  279. function show_xml_relationship_details($details)
  280. {
  281. foreach($details as $element => $value) {
  282. $this->element($element, null, $value);
  283. }
  284. }
  285. function show_twitter_xml_status($twitter_status)
  286. {
  287. $this->elementStart('status');
  288. foreach($twitter_status as $element => $value) {
  289. switch ($element) {
  290. case 'user':
  291. $this->show_twitter_xml_user($twitter_status['user']);
  292. break;
  293. case 'text':
  294. $this->element($element, null, common_xml_safe_str($value));
  295. break;
  296. default:
  297. $this->element($element, null, $value);
  298. }
  299. }
  300. $this->elementEnd('status');
  301. }
  302. function show_twitter_xml_user($twitter_user, $role='user')
  303. {
  304. $this->elementStart($role);
  305. foreach($twitter_user as $element => $value) {
  306. if ($element == 'status') {
  307. $this->show_twitter_xml_status($twitter_user['status']);
  308. } else {
  309. $this->element($element, null, $value);
  310. }
  311. }
  312. $this->elementEnd($role);
  313. }
  314. function show_twitter_rss_item($entry)
  315. {
  316. $this->elementStart('item');
  317. $this->element('title', null, $entry['title']);
  318. $this->element('description', null, $entry['description']);
  319. $this->element('pubDate', null, $entry['pubDate']);
  320. $this->element('guid', null, $entry['guid']);
  321. $this->element('link', null, $entry['link']);
  322. # RSS only supports 1 enclosure per item
  323. if($entry['enclosures']){
  324. $enclosure = $entry['enclosures'][0];
  325. $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null);
  326. }
  327. $this->elementEnd('item');
  328. }
  329. function show_json_objects($objects)
  330. {
  331. print(json_encode($objects));
  332. }
  333. function show_single_xml_status($notice)
  334. {
  335. $this->init_document('xml');
  336. $twitter_status = $this->twitter_status_array($notice);
  337. $this->show_twitter_xml_status($twitter_status);
  338. $this->end_document('xml');
  339. }
  340. function show_single_json_status($notice)
  341. {
  342. $this->init_document('json');
  343. $status = $this->twitter_status_array($notice);
  344. $this->show_json_objects($status);
  345. $this->end_document('json');
  346. }
  347. function show_single_xml_dmsg($message)
  348. {
  349. $this->init_document('xml');
  350. $dmsg = $this->twitter_dmsg_array($message);
  351. $this->show_twitter_xml_dmsg($dmsg);
  352. $this->end_document('xml');
  353. }
  354. function show_single_json_dmsg($message)
  355. {
  356. $this->init_document('json');
  357. $dmsg = $this->twitter_dmsg_array($message);
  358. $this->show_json_objects($dmsg);
  359. $this->end_document('json');
  360. }
  361. function show_twitter_xml_dmsg($twitter_dm)
  362. {
  363. $this->elementStart('direct_message');
  364. foreach($twitter_dm as $element => $value) {
  365. switch ($element) {
  366. case 'sender':
  367. case 'recipient':
  368. $this->show_twitter_xml_user($value, $element);
  369. break;
  370. case 'text':
  371. $this->element($element, null, common_xml_safe_str($value));
  372. break;
  373. default:
  374. $this->element($element, null, $value);
  375. }
  376. }
  377. $this->elementEnd('direct_message');
  378. }
  379. function show_xml_timeline($notice)
  380. {
  381. $this->init_document('xml');
  382. $this->elementStart('statuses', array('type' => 'array'));
  383. if (is_array($notice)) {
  384. foreach ($notice as $n) {
  385. $twitter_status = $this->twitter_status_array($n);
  386. $this->show_twitter_xml_status($twitter_status);
  387. }
  388. } else {
  389. while ($notice->fetch()) {
  390. $twitter_status = $this->twitter_status_array($notice);
  391. $this->show_twitter_xml_status($twitter_status);
  392. }
  393. }
  394. $this->elementEnd('statuses');
  395. $this->end_document('xml');
  396. }
  397. function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=null)
  398. {
  399. $this->init_document('rss');
  400. $this->elementStart('channel');
  401. $this->element('title', null, $title);
  402. $this->element('link', null, $link);
  403. if (!is_null($suplink)) {
  404. # For FriendFeed's SUP protocol
  405. $this->element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
  406. 'rel' => 'http://api.friendfeed.com/2008/03#sup',
  407. 'href' => $suplink,
  408. 'type' => 'application/json'));
  409. }
  410. $this->element('description', null, $subtitle);
  411. $this->element('language', null, 'en-us');
  412. $this->element('ttl', null, '40');
  413. if (is_array($notice)) {
  414. foreach ($notice as $n) {
  415. $entry = $this->twitter_rss_entry_array($n);
  416. $this->show_twitter_rss_item($entry);
  417. }
  418. } else {
  419. while ($notice->fetch()) {
  420. $entry = $this->twitter_rss_entry_array($notice);
  421. $this->show_twitter_rss_item($entry);
  422. }
  423. }
  424. $this->elementEnd('channel');
  425. $this->end_twitter_rss();
  426. }
  427. function show_atom_timeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null)
  428. {
  429. $this->init_document('atom');
  430. $this->element('title', null, $title);
  431. $this->element('id', null, $id);
  432. $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
  433. if (!is_null($suplink)) {
  434. # For FriendFeed's SUP protocol
  435. $this->element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
  436. 'href' => $suplink,
  437. 'type' => 'application/json'));
  438. }
  439. if (!is_null($selfuri)) {
  440. $this->element('link', array('href' => $selfuri,
  441. 'rel' => 'self', 'type' => 'application/atom+xml'), null);
  442. }
  443. $this->element('updated', null, common_date_iso8601('now'));
  444. $this->element('subtitle', null, $subtitle);
  445. if (is_array($notice)) {
  446. foreach ($notice as $n) {
  447. $this->raw($n->asAtomEntry());
  448. }
  449. } else {
  450. while ($notice->fetch()) {
  451. $this->raw($notice->asAtomEntry());
  452. }
  453. }
  454. $this->end_document('atom');
  455. }
  456. function show_json_timeline($notice)
  457. {
  458. $this->init_document('json');
  459. $statuses = array();
  460. if (is_array($notice)) {
  461. foreach ($notice as $n) {
  462. $twitter_status = $this->twitter_status_array($n);
  463. array_push($statuses, $twitter_status);
  464. }
  465. } else {
  466. while ($notice->fetch()) {
  467. $twitter_status = $this->twitter_status_array($notice);
  468. array_push($statuses, $twitter_status);
  469. }
  470. }
  471. $this->show_json_objects($statuses);
  472. $this->end_document('json');
  473. }
  474. // Anyone know what date format this is?
  475. // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
  476. function date_twitter($dt)
  477. {
  478. $t = strtotime($dt);
  479. return date("D M d H:i:s O Y", $t);
  480. }
  481. // XXX: Candidate for a general utility method somewhere?
  482. function count_subscriptions($profile)
  483. {
  484. $count = 0;
  485. $sub = new Subscription();
  486. $sub->subscribed = $profile->id;
  487. $count = $sub->find();
  488. if ($count > 0) {
  489. return $count - 1;
  490. } else {
  491. return 0;
  492. }
  493. }
  494. function init_document($type='xml')
  495. {
  496. switch ($type) {
  497. case 'xml':
  498. header('Content-Type: application/xml; charset=utf-8');
  499. $this->startXML();
  500. break;
  501. case 'json':
  502. header('Content-Type: application/json; charset=utf-8');
  503. // Check for JSONP callback
  504. $callback = $this->arg('callback');
  505. if ($callback) {
  506. print $callback . '(';
  507. }
  508. break;
  509. case 'rss':
  510. header("Content-Type: application/rss+xml; charset=utf-8");
  511. $this->init_twitter_rss();
  512. break;
  513. case 'atom':
  514. header('Content-Type: application/atom+xml; charset=utf-8');
  515. $this->init_twitter_atom();
  516. break;
  517. default:
  518. $this->clientError(_('Not a supported data format.'));
  519. break;
  520. }
  521. return;
  522. }
  523. function end_document($type='xml')
  524. {
  525. switch ($type) {
  526. case 'xml':
  527. $this->endXML();
  528. break;
  529. case 'json':
  530. // Check for JSONP callback
  531. $callback = $this->arg('callback');
  532. if ($callback) {
  533. print ')';
  534. }
  535. break;
  536. case 'rss':
  537. $this->end_twitter_rss();
  538. break;
  539. case 'atom':
  540. $this->end_twitter_rss();
  541. break;
  542. default:
  543. $this->clientError(_('Not a supported data format.'));
  544. break;
  545. }
  546. return;
  547. }
  548. function clientError($msg, $code = 400, $content_type = 'json')
  549. {
  550. static $status = array(400 => 'Bad Request',
  551. 401 => 'Unauthorized',
  552. 402 => 'Payment Required',
  553. 403 => 'Forbidden',
  554. 404 => 'Not Found',
  555. 405 => 'Method Not Allowed',
  556. 406 => 'Not Acceptable',
  557. 407 => 'Proxy Authentication Required',
  558. 408 => 'Request Timeout',
  559. 409 => 'Conflict',
  560. 410 => 'Gone',
  561. 411 => 'Length Required',
  562. 412 => 'Precondition Failed',
  563. 413 => 'Request Entity Too Large',
  564. 414 => 'Request-URI Too Long',
  565. 415 => 'Unsupported Media Type',
  566. 416 => 'Requested Range Not Satisfiable',
  567. 417 => 'Expectation Failed');
  568. $action = $this->trimmed('action');
  569. common_debug("User error '$code' on '$action': $msg", __FILE__);
  570. if (!array_key_exists($code, $status)) {
  571. $code = 400;
  572. }
  573. $status_string = $status[$code];
  574. header('HTTP/1.1 '.$code.' '.$status_string);
  575. if ($content_type == 'xml') {
  576. $this->init_document('xml');
  577. $this->elementStart('hash');
  578. $this->element('error', null, $msg);
  579. $this->element('request', null, $_SERVER['REQUEST_URI']);
  580. $this->elementEnd('hash');
  581. $this->end_document('xml');
  582. } else {
  583. $this->init_document('json');
  584. $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
  585. print(json_encode($error_array));
  586. $this->end_document('json');
  587. }
  588. }
  589. function init_twitter_rss()
  590. {
  591. $this->startXML();
  592. $this->elementStart('rss', array('version' => '2.0'));
  593. }
  594. function end_twitter_rss()
  595. {
  596. $this->elementEnd('rss');
  597. $this->endXML();
  598. }
  599. function init_twitter_atom()
  600. {
  601. $this->startXML();
  602. // FIXME: don't hardcode the language here!
  603. $this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom',
  604. 'xml:lang' => 'en-US',
  605. 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0'));
  606. }
  607. function end_twitter_atom()
  608. {
  609. $this->elementEnd('feed');
  610. $this->endXML();
  611. }
  612. function show_profile($profile, $content_type='xml', $notice=null)
  613. {
  614. $profile_array = $this->twitter_user_array($profile, true);
  615. switch ($content_type) {
  616. case 'xml':
  617. $this->show_twitter_xml_user($profile_array);
  618. break;
  619. case 'json':
  620. $this->show_json_objects($profile_array);
  621. break;
  622. default:
  623. $this->clientError(_('Not a supported data format.'));
  624. return;
  625. }
  626. return;
  627. }
  628. function get_user($id, $apidata=null)
  629. {
  630. if (empty($id)) {
  631. // Twitter supports these other ways of passing the user ID
  632. if (is_numeric($this->arg('id'))) {
  633. return User::staticGet($this->arg('id'));
  634. } else if ($this->arg('id')) {
  635. $nickname = common_canonical_nickname($this->arg('id'));
  636. return User::staticGet('nickname', $nickname);
  637. } else if ($this->arg('user_id')) {
  638. // This is to ensure that a non-numeric user_id still
  639. // overrides screen_name even if it doesn't get used
  640. if (is_numeric($this->arg('user_id'))) {
  641. return User::staticGet('id', $this->arg('user_id'));
  642. }
  643. } else if ($this->arg('screen_name')) {
  644. $nickname = common_canonical_nickname($this->arg('screen_name'));
  645. return User::staticGet('nickname', $nickname);
  646. } else {
  647. // Fall back to trying the currently authenticated user
  648. return $apidata['user'];
  649. }
  650. } else if (is_numeric($id)) {
  651. return User::staticGet($id);
  652. } else {
  653. $nickname = common_canonical_nickname($id);
  654. return User::staticGet('nickname', $nickname);
  655. }
  656. }
  657. function get_group($id, $apidata=null)
  658. {
  659. if (empty($id)) {
  660. if (is_numeric($this->arg('id'))) {
  661. return User_group::staticGet($this->arg('id'));
  662. } else if ($this->arg('id')) {
  663. $nickname = common_canonical_nickname($this->arg('id'));
  664. return User_group::staticGet('nickname', $nickname);
  665. } else if ($this->arg('group_id')) {
  666. // This is to ensure that a non-numeric user_id still
  667. // overrides screen_name even if it doesn't get used
  668. if (is_numeric($this->arg('group_id'))) {
  669. return User_group::staticGet('id', $this->arg('group_id'));
  670. }
  671. } else if ($this->arg('group_name')) {
  672. $nickname = common_canonical_nickname($this->arg('group_name'));
  673. return User_group::staticGet('nickname', $nickname);
  674. }
  675. } else if (is_numeric($id)) {
  676. return User_group::staticGet($id);
  677. } else {
  678. $nickname = common_canonical_nickname($id);
  679. return User_group::staticGet('nickname', $nickname);
  680. }
  681. }
  682. function get_profile($id)
  683. {
  684. if (is_numeric($id)) {
  685. return Profile::staticGet($id);
  686. } else {
  687. $user = User::staticGet('nickname', $id);
  688. if ($user) {
  689. return $user->getProfile();
  690. } else {
  691. return null;
  692. }
  693. }
  694. }
  695. function source_link($source)
  696. {
  697. $source_name = _($source);
  698. switch ($source) {
  699. case 'web':
  700. case 'xmpp':
  701. case 'mail':
  702. case 'omb':
  703. case 'api':
  704. break;
  705. default:
  706. $ns = Notice_source::staticGet($source);
  707. if ($ns) {
  708. $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
  709. }
  710. break;
  711. }
  712. return $source_name;
  713. }
  714. /**
  715. * Returns query argument or default value if not found. Certain
  716. * parameters used throughout the API are lightly scrubbed and
  717. * bounds checked. This overrides Action::arg().
  718. *
  719. * @param string $key requested argument
  720. * @param string $def default value to return if $key is not provided
  721. *
  722. * @return var $var
  723. */
  724. function arg($key, $def=null)
  725. {
  726. // XXX: Do even more input validation/scrubbing?
  727. if (array_key_exists($key, $this->args)) {
  728. switch($key) {
  729. case 'page':
  730. $page = (int)$this->args['page'];
  731. return ($page < 1) ? 1 : $page;
  732. case 'count':
  733. $count = (int)$this->args['count'];
  734. if ($count < 1) {
  735. return 20;
  736. } elseif ($count > 200) {
  737. return 200;
  738. } else {
  739. return $count;
  740. }
  741. case 'since_id':
  742. $since_id = (int)$this->args['since_id'];
  743. return ($since_id < 1) ? 0 : $since_id;
  744. case 'max_id':
  745. $max_id = (int)$this->args['max_id'];
  746. return ($max_id < 1) ? 0 : $max_id;
  747. case 'since':
  748. return strtotime($this->args['since']);
  749. default:
  750. return parent::arg($key, $def);
  751. }
  752. } else {
  753. return $def;
  754. }
  755. }
  756. }