apiaction.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Base API action
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category API
  23. * @package StatusNet
  24. * @author Craig Andrews <candrews@integralblue.com>
  25. * @author Dan Moore <dan@moore.cx>
  26. * @author Evan Prodromou <evan@status.net>
  27. * @author Jeffery To <jeffery.to@gmail.com>
  28. * @author Toby Inkster <mail@tobyinkster.co.uk>
  29. * @author Zach Copley <zach@status.net>
  30. * @copyright 2009-2010 StatusNet, Inc.
  31. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  32. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  33. * @link http://status.net/
  34. */
  35. /* External API usage documentation. Please update when you change how the API works. */
  36. /*! @mainpage StatusNet REST API
  37. @section Introduction
  38. Some explanatory text about the API would be nice.
  39. @section API Methods
  40. @subsection timelinesmethods_sec Timeline Methods
  41. @li @ref publictimeline
  42. @li @ref friendstimeline
  43. @subsection statusmethods_sec Status Methods
  44. @li @ref statusesupdate
  45. @subsection usermethods_sec User Methods
  46. @subsection directmessagemethods_sec Direct Message Methods (now a plugin)
  47. @subsection friendshipmethods_sec Friendship Methods
  48. @subsection socialgraphmethods_sec Social Graph Methods
  49. @subsection accountmethods_sec Account Methods
  50. @subsection favoritesmethods_sec Favorites Methods
  51. @subsection blockmethods_sec Block Methods
  52. @subsection oauthmethods_sec OAuth Methods
  53. @subsection helpmethods_sec Help Methods
  54. @subsection groupmethods_sec Group Methods
  55. @page apiroot API Root
  56. The URLs for methods referred to in this API documentation are
  57. relative to the StatusNet API root. The API root is determined by the
  58. site's @b server and @b path variables, which are generally specified
  59. in config.php. For example:
  60. @code
  61. $config['site']['server'] = 'example.org';
  62. $config['site']['path'] = 'statusnet'
  63. @endcode
  64. The pattern for a site's API root is: @c protocol://server/path/api E.g:
  65. @c http://example.org/statusnet/api
  66. The @b path can be empty. In that case the API root would simply be:
  67. @c http://example.org/api
  68. */
  69. if (!defined('STATUSNET')) {
  70. exit(1);
  71. }
  72. class ApiValidationException extends Exception { }
  73. /**
  74. * Contains most of the Twitter-compatible API output functions.
  75. *
  76. * @category API
  77. * @package StatusNet
  78. * @author Craig Andrews <candrews@integralblue.com>
  79. * @author Dan Moore <dan@moore.cx>
  80. * @author Evan Prodromou <evan@status.net>
  81. * @author Jeffery To <jeffery.to@gmail.com>
  82. * @author Toby Inkster <mail@tobyinkster.co.uk>
  83. * @author Zach Copley <zach@status.net>
  84. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  85. * @link http://status.net/
  86. */
  87. class ApiAction extends Action
  88. {
  89. const READ_ONLY = 1;
  90. const READ_WRITE = 2;
  91. var $user = null;
  92. var $auth_user = null;
  93. var $page = null;
  94. var $count = null;
  95. var $offset = null;
  96. var $limit = null;
  97. var $max_id = null;
  98. var $since_id = null;
  99. var $source = null;
  100. var $callback = null;
  101. var $format = null;
  102. var $access = self::READ_ONLY; // read (default) or read-write
  103. static $reserved_sources = array('web', 'omb', 'ostatus', 'mail', 'xmpp', 'api');
  104. /**
  105. * Initialization.
  106. *
  107. * @param array $args Web and URL arguments
  108. *
  109. * @return boolean false if user doesn't exist
  110. */
  111. protected function prepare(array $args=array())
  112. {
  113. GNUsocial::setApi(true); // reduce exception reports to aid in debugging
  114. parent::prepare($args);
  115. $this->format = $this->arg('format');
  116. $this->callback = $this->arg('callback');
  117. $this->page = (int)$this->arg('page', 1);
  118. $this->count = (int)$this->arg('count', 20);
  119. $this->max_id = (int)$this->arg('max_id', 0);
  120. $this->since_id = (int)$this->arg('since_id', 0);
  121. // These two are not used everywhere, mainly just AtompubAction extensions
  122. $this->offset = ($this->page-1) * $this->count;
  123. $this->limit = $this->count + 1;
  124. if ($this->arg('since')) {
  125. header('X-GNUsocial-Warning: since parameter is disabled; use since_id');
  126. }
  127. $this->source = $this->trimmed('source');
  128. if (empty($this->source) || in_array($this->source, self::$reserved_sources)) {
  129. $this->source = 'api';
  130. }
  131. return true;
  132. }
  133. /**
  134. * Handle a request
  135. *
  136. * @param array $args Arguments from $_REQUEST
  137. *
  138. * @return void
  139. */
  140. protected function handle()
  141. {
  142. header('Access-Control-Allow-Origin: *');
  143. parent::handle();
  144. }
  145. /**
  146. * Overrides XMLOutputter::element to write booleans as strings (true|false).
  147. * See that method's documentation for more info.
  148. *
  149. * @param string $tag Element type or tagname
  150. * @param array $attrs Array of element attributes, as
  151. * key-value pairs
  152. * @param string $content string content of the element
  153. *
  154. * @return void
  155. */
  156. function element($tag, $attrs=null, $content=null)
  157. {
  158. if (is_bool($content)) {
  159. $content = ($content ? 'true' : 'false');
  160. }
  161. return parent::element($tag, $attrs, $content);
  162. }
  163. function twitterUserArray($profile, $get_notice=false)
  164. {
  165. $twitter_user = array();
  166. try {
  167. $user = $profile->getUser();
  168. } catch (NoSuchUserException $e) {
  169. $user = null;
  170. }
  171. $twitter_user['id'] = $profile->getID();
  172. $twitter_user['name'] = $profile->getBestName();
  173. $twitter_user['screen_name'] = $profile->getNickname();
  174. $twitter_user['location'] = $profile->location;
  175. $twitter_user['description'] = $profile->getDescription();
  176. // TODO: avatar url template (example.com/user/avatar?size={x}x{y})
  177. $twitter_user['profile_image_url'] = Avatar::urlByProfile($profile, AVATAR_STREAM_SIZE);
  178. $twitter_user['profile_image_url_https'] = $twitter_user['profile_image_url'];
  179. // START introduced by qvitter API, not necessary for StatusNet API
  180. $twitter_user['profile_image_url_profile_size'] = Avatar::urlByProfile($profile, AVATAR_PROFILE_SIZE);
  181. try {
  182. $avatar = Avatar::getUploaded($profile);
  183. $origurl = $avatar->displayUrl();
  184. } catch (Exception $e) {
  185. $origurl = $twitter_user['profile_image_url_profile_size'];
  186. }
  187. $twitter_user['profile_image_url_original'] = $origurl;
  188. $twitter_user['groups_count'] = $profile->getGroupCount();
  189. foreach (array('linkcolor', 'backgroundcolor') as $key) {
  190. $twitter_user[$key] = Profile_prefs::getConfigData($profile, 'theme', $key);
  191. }
  192. // END introduced by qvitter API, not necessary for StatusNet API
  193. $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
  194. $twitter_user['protected'] = (!empty($user) && $user->private_stream) ? true : false;
  195. $twitter_user['followers_count'] = $profile->subscriberCount();
  196. // Note: some profiles don't have an associated user
  197. $twitter_user['friends_count'] = $profile->subscriptionCount();
  198. $twitter_user['created_at'] = self::dateTwitter($profile->created);
  199. $timezone = 'UTC';
  200. if (!empty($user) && $user->timezone) {
  201. $timezone = $user->timezone;
  202. }
  203. $t = new DateTime;
  204. $t->setTimezone(new DateTimeZone($timezone));
  205. $twitter_user['utc_offset'] = $t->format('Z');
  206. $twitter_user['time_zone'] = $timezone;
  207. $twitter_user['statuses_count'] = $profile->noticeCount();
  208. // Is the requesting user following this user?
  209. // These values might actually also mean "unknown". Ambiguity issues?
  210. $twitter_user['following'] = false;
  211. $twitter_user['statusnet_blocking'] = false;
  212. $twitter_user['notifications'] = false;
  213. if ($this->scoped instanceof Profile) {
  214. try {
  215. $sub = Subscription::getSubscription($this->scoped, $profile);
  216. // Notifications on?
  217. $twitter_user['following'] = true;
  218. $twitter_user['notifications'] = ($sub->jabber || $sub->sms);
  219. } catch (NoResultException $e) {
  220. // well, the values are already false...
  221. }
  222. $twitter_user['statusnet_blocking'] = $this->scoped->hasBlocked($profile);
  223. }
  224. if ($get_notice) {
  225. $notice = $profile->getCurrentNotice();
  226. if ($notice instanceof Notice) {
  227. // don't get user!
  228. $twitter_user['status'] = $this->twitterStatusArray($notice, false);
  229. }
  230. }
  231. // StatusNet-specific
  232. $twitter_user['statusnet_profile_url'] = $profile->profileurl;
  233. // The event call to handle NoticeSimpleStatusArray lets plugins add data to the output array
  234. Event::handle('TwitterUserArray', array($profile, &$twitter_user, $this->scoped, array()));
  235. return $twitter_user;
  236. }
  237. function twitterStatusArray($notice, $include_user=true)
  238. {
  239. $base = $this->twitterSimpleStatusArray($notice, $include_user);
  240. // FIXME: MOVE TO SHARE PLUGIN
  241. if (!empty($notice->repeat_of)) {
  242. $original = Notice::getKV('id', $notice->repeat_of);
  243. if ($original instanceof Notice) {
  244. $orig_array = $this->twitterSimpleStatusArray($original, $include_user);
  245. $base['retweeted_status'] = $orig_array;
  246. }
  247. }
  248. return $base;
  249. }
  250. function twitterSimpleStatusArray($notice, $include_user=true)
  251. {
  252. $profile = $notice->getProfile();
  253. $twitter_status = array();
  254. $twitter_status['text'] = $notice->content;
  255. $twitter_status['truncated'] = false; # Not possible on StatusNet
  256. $twitter_status['created_at'] = self::dateTwitter($notice->created);
  257. try {
  258. // We could just do $notice->reply_to but maybe the future holds a
  259. // different story for parenting.
  260. $parent = $notice->getParent();
  261. $in_reply_to = $parent->id;
  262. } catch (NoParentNoticeException $e) {
  263. $in_reply_to = null;
  264. } catch (NoResultException $e) {
  265. // the in_reply_to message has probably been deleted
  266. $in_reply_to = null;
  267. }
  268. $twitter_status['in_reply_to_status_id'] = $in_reply_to;
  269. $source = null;
  270. $source_link = null;
  271. $ns = $notice->getSource();
  272. if ($ns instanceof Notice_source) {
  273. $source = $ns->code;
  274. if (!empty($ns->url)) {
  275. $source_link = $ns->url;
  276. if (!empty($ns->name)) {
  277. $source = $ns->name;
  278. }
  279. }
  280. }
  281. $twitter_status['uri'] = $notice->getUri();
  282. $twitter_status['source'] = $source;
  283. $twitter_status['source_link'] = $source_link;
  284. $twitter_status['id'] = intval($notice->id);
  285. $replier_profile = null;
  286. if ($notice->reply_to) {
  287. $reply = Notice::getKV(intval($notice->reply_to));
  288. if ($reply) {
  289. $replier_profile = $reply->getProfile();
  290. }
  291. }
  292. $twitter_status['in_reply_to_user_id'] =
  293. ($replier_profile) ? intval($replier_profile->id) : null;
  294. $twitter_status['in_reply_to_screen_name'] =
  295. ($replier_profile) ? $replier_profile->nickname : null;
  296. try {
  297. $notloc = Notice_location::locFromStored($notice);
  298. // This is the format that GeoJSON expects stuff to be in
  299. $twitter_status['geo'] = array('type' => 'Point',
  300. 'coordinates' => array((float) $notloc->lat,
  301. (float) $notloc->lon));
  302. } catch (ServerException $e) {
  303. $twitter_status['geo'] = null;
  304. }
  305. // Enclosures
  306. $attachments = $notice->attachments();
  307. if (!empty($attachments)) {
  308. $twitter_status['attachments'] = array();
  309. foreach ($attachments as $attachment) {
  310. try {
  311. $enclosure_o = $attachment->getEnclosure();
  312. $enclosure = array();
  313. $enclosure['url'] = $enclosure_o->url;
  314. $enclosure['mimetype'] = $enclosure_o->mimetype;
  315. $enclosure['size'] = $enclosure_o->size;
  316. $twitter_status['attachments'][] = $enclosure;
  317. } catch (ServerException $e) {
  318. // There was not enough metadata available
  319. }
  320. }
  321. }
  322. if ($include_user && $profile) {
  323. // Don't get notice (recursive!)
  324. $twitter_user = $this->twitterUserArray($profile, false);
  325. $twitter_status['user'] = $twitter_user;
  326. }
  327. // StatusNet-specific
  328. $twitter_status['statusnet_html'] = $notice->getRendered();
  329. $twitter_status['statusnet_conversation_id'] = intval($notice->conversation);
  330. // The event call to handle NoticeSimpleStatusArray lets plugins add data to the output array
  331. Event::handle('NoticeSimpleStatusArray', array($notice, &$twitter_status, $this->scoped,
  332. array('include_user'=>$include_user)));
  333. return $twitter_status;
  334. }
  335. function twitterGroupArray($group)
  336. {
  337. $twitter_group = array();
  338. $twitter_group['id'] = intval($group->id);
  339. $twitter_group['url'] = $group->permalink();
  340. $twitter_group['nickname'] = $group->nickname;
  341. $twitter_group['fullname'] = $group->fullname;
  342. if ($this->scoped instanceof Profile) {
  343. $twitter_group['member'] = $this->scoped->isMember($group);
  344. $twitter_group['blocked'] = Group_block::isBlocked(
  345. $group,
  346. $this->scoped
  347. );
  348. }
  349. $twitter_group['admin_count'] = $group->getAdminCount();
  350. $twitter_group['member_count'] = $group->getMemberCount();
  351. $twitter_group['original_logo'] = $group->original_logo;
  352. $twitter_group['homepage_logo'] = $group->homepage_logo;
  353. $twitter_group['stream_logo'] = $group->stream_logo;
  354. $twitter_group['mini_logo'] = $group->mini_logo;
  355. $twitter_group['homepage'] = $group->homepage;
  356. $twitter_group['description'] = $group->description;
  357. $twitter_group['location'] = $group->location;
  358. $twitter_group['created'] = self::dateTwitter($group->created);
  359. $twitter_group['modified'] = self::dateTwitter($group->modified);
  360. return $twitter_group;
  361. }
  362. function twitterRssGroupArray($group)
  363. {
  364. $entry = array();
  365. $entry['content']=$group->description;
  366. $entry['title']=$group->nickname;
  367. $entry['link']=$group->permalink();
  368. $entry['published']=common_date_iso8601($group->created);
  369. $entry['updated']==common_date_iso8601($group->modified);
  370. $taguribase = common_config('integration', 'groupuri');
  371. $entry['id'] = "group:$groupuribase:$entry[link]";
  372. $entry['description'] = $entry['content'];
  373. $entry['pubDate'] = common_date_rfc2822($group->created);
  374. $entry['guid'] = $entry['link'];
  375. return $entry;
  376. }
  377. function twitterListArray($list)
  378. {
  379. $profile = Profile::getKV('id', $list->tagger);
  380. $twitter_list = array();
  381. $twitter_list['id'] = $list->id;
  382. $twitter_list['name'] = $list->tag;
  383. $twitter_list['full_name'] = '@'.$profile->nickname.'/'.$list->tag;;
  384. $twitter_list['slug'] = $list->tag;
  385. $twitter_list['description'] = $list->description;
  386. $twitter_list['subscriber_count'] = $list->subscriberCount();
  387. $twitter_list['member_count'] = $list->taggedCount();
  388. $twitter_list['uri'] = $list->getUri();
  389. if ($this->scoped instanceof Profile) {
  390. $twitter_list['following'] = $list->hasSubscriber($this->scoped);
  391. } else {
  392. $twitter_list['following'] = false;
  393. }
  394. $twitter_list['mode'] = ($list->private) ? 'private' : 'public';
  395. $twitter_list['user'] = $this->twitterUserArray($profile, false);
  396. return $twitter_list;
  397. }
  398. function twitterRssEntryArray($notice)
  399. {
  400. $entry = array();
  401. if (Event::handle('StartRssEntryArray', array($notice, &$entry))) {
  402. $profile = $notice->getProfile();
  403. // We trim() to avoid extraneous whitespace in the output
  404. $entry['content'] = common_xml_safe_str(trim($notice->getRendered()));
  405. $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
  406. $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
  407. $entry['published'] = common_date_iso8601($notice->created);
  408. $taguribase = TagURI::base();
  409. $entry['id'] = "tag:$taguribase:$entry[link]";
  410. $entry['updated'] = $entry['published'];
  411. $entry['author'] = $profile->getBestName();
  412. // Enclosures
  413. $attachments = $notice->attachments();
  414. $enclosures = array();
  415. foreach ($attachments as $attachment) {
  416. try {
  417. $enclosure_o = $attachment->getEnclosure();
  418. $enclosure = array();
  419. $enclosure['url'] = $enclosure_o->url;
  420. $enclosure['mimetype'] = $enclosure_o->mimetype;
  421. $enclosure['size'] = $enclosure_o->size;
  422. $enclosures[] = $enclosure;
  423. } catch (ServerException $e) {
  424. // There was not enough metadata available
  425. }
  426. }
  427. if (!empty($enclosures)) {
  428. $entry['enclosures'] = $enclosures;
  429. }
  430. // Tags/Categories
  431. $tag = new Notice_tag();
  432. $tag->notice_id = $notice->id;
  433. if ($tag->find()) {
  434. $entry['tags']=array();
  435. while ($tag->fetch()) {
  436. $entry['tags'][]=$tag->tag;
  437. }
  438. }
  439. $tag->free();
  440. // RSS Item specific
  441. $entry['description'] = $entry['content'];
  442. $entry['pubDate'] = common_date_rfc2822($notice->created);
  443. $entry['guid'] = $entry['link'];
  444. try {
  445. $notloc = Notice_location::locFromStored($notice);
  446. // This is the format that GeoJSON expects stuff to be in.
  447. // showGeoRSS() below uses it for XML output, so we reuse it
  448. $entry['geo'] = array('type' => 'Point',
  449. 'coordinates' => array((float) $notloc->lat,
  450. (float) $notloc->lon));
  451. } catch (ServerException $e) {
  452. $entry['geo'] = null;
  453. }
  454. Event::handle('EndRssEntryArray', array($notice, &$entry));
  455. }
  456. return $entry;
  457. }
  458. function twitterRelationshipArray($source, $target)
  459. {
  460. $relationship = array();
  461. $relationship['source'] =
  462. $this->relationshipDetailsArray($source->getProfile(), $target->getProfile());
  463. $relationship['target'] =
  464. $this->relationshipDetailsArray($target->getProfile(), $source->getProfile());
  465. return array('relationship' => $relationship);
  466. }
  467. function relationshipDetailsArray(Profile $source, Profile $target)
  468. {
  469. $details = array();
  470. $details['screen_name'] = $source->getNickname();
  471. $details['followed_by'] = $target->isSubscribed($source);
  472. try {
  473. $sub = Subscription::getSubscription($source, $target);
  474. $details['following'] = true;
  475. $details['notifications_enabled'] = ($sub->jabber || $sub->sms);
  476. } catch (NoResultException $e) {
  477. $details['following'] = false;
  478. $details['notifications_enabled'] = false;
  479. }
  480. $details['blocking'] = $source->hasBlocked($target);
  481. $details['id'] = intval($source->id);
  482. return $details;
  483. }
  484. function showTwitterXmlRelationship($relationship)
  485. {
  486. $this->elementStart('relationship');
  487. foreach($relationship as $element => $value) {
  488. if ($element == 'source' || $element == 'target') {
  489. $this->elementStart($element);
  490. $this->showXmlRelationshipDetails($value);
  491. $this->elementEnd($element);
  492. }
  493. }
  494. $this->elementEnd('relationship');
  495. }
  496. function showXmlRelationshipDetails($details)
  497. {
  498. foreach($details as $element => $value) {
  499. $this->element($element, null, $value);
  500. }
  501. }
  502. function showTwitterXmlStatus($twitter_status, $tag='status', $namespaces=false)
  503. {
  504. $attrs = array();
  505. if ($namespaces) {
  506. $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
  507. }
  508. $this->elementStart($tag, $attrs);
  509. foreach($twitter_status as $element => $value) {
  510. switch ($element) {
  511. case 'user':
  512. $this->showTwitterXmlUser($twitter_status['user']);
  513. break;
  514. case 'text':
  515. $this->element($element, null, common_xml_safe_str($value));
  516. break;
  517. case 'attachments':
  518. $this->showXmlAttachments($twitter_status['attachments']);
  519. break;
  520. case 'geo':
  521. $this->showGeoXML($value);
  522. break;
  523. case 'retweeted_status':
  524. // FIXME: MOVE TO SHARE PLUGIN
  525. $this->showTwitterXmlStatus($value, 'retweeted_status');
  526. break;
  527. default:
  528. if (strncmp($element, 'statusnet_', 10) == 0) {
  529. if ($element === 'statusnet_in_groups' && is_array($value)) {
  530. // QVITTERFIX because it would cause an array to be sent as $value
  531. // THIS IS UNDOCUMENTED AND SHOULD NEVER BE RELIED UPON (qvitter uses json output)
  532. $value = json_encode($value);
  533. }
  534. $this->element('statusnet:'.substr($element, 10), null, $value);
  535. } else {
  536. $this->element($element, null, $value);
  537. }
  538. }
  539. }
  540. $this->elementEnd($tag);
  541. }
  542. function showTwitterXmlGroup($twitter_group)
  543. {
  544. $this->elementStart('group');
  545. foreach($twitter_group as $element => $value) {
  546. $this->element($element, null, $value);
  547. }
  548. $this->elementEnd('group');
  549. }
  550. function showTwitterXmlList($twitter_list)
  551. {
  552. $this->elementStart('list');
  553. foreach($twitter_list as $element => $value) {
  554. if($element == 'user') {
  555. $this->showTwitterXmlUser($value, 'user');
  556. }
  557. else {
  558. $this->element($element, null, $value);
  559. }
  560. }
  561. $this->elementEnd('list');
  562. }
  563. function showTwitterXmlUser($twitter_user, $role='user', $namespaces=false)
  564. {
  565. $attrs = array();
  566. if ($namespaces) {
  567. $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
  568. }
  569. $this->elementStart($role, $attrs);
  570. foreach($twitter_user as $element => $value) {
  571. if ($element == 'status') {
  572. $this->showTwitterXmlStatus($twitter_user['status']);
  573. } else if (strncmp($element, 'statusnet_', 10) == 0) {
  574. $this->element('statusnet:'.substr($element, 10), null, $value);
  575. } else {
  576. $this->element($element, null, $value);
  577. }
  578. }
  579. $this->elementEnd($role);
  580. }
  581. function showXmlAttachments($attachments) {
  582. if (!empty($attachments)) {
  583. $this->elementStart('attachments', array('type' => 'array'));
  584. foreach ($attachments as $attachment) {
  585. $attrs = array();
  586. $attrs['url'] = $attachment['url'];
  587. $attrs['mimetype'] = $attachment['mimetype'];
  588. $attrs['size'] = $attachment['size'];
  589. $this->element('enclosure', $attrs, '');
  590. }
  591. $this->elementEnd('attachments');
  592. }
  593. }
  594. function showGeoXML($geo)
  595. {
  596. if (empty($geo)) {
  597. // empty geo element
  598. $this->element('geo');
  599. } else {
  600. $this->elementStart('geo', array('xmlns:georss' => 'http://www.georss.org/georss'));
  601. $this->element('georss:point', null, $geo['coordinates'][0] . ' ' . $geo['coordinates'][1]);
  602. $this->elementEnd('geo');
  603. }
  604. }
  605. function showGeoRSS($geo)
  606. {
  607. if (!empty($geo)) {
  608. $this->element(
  609. 'georss:point',
  610. null,
  611. $geo['coordinates'][0] . ' ' . $geo['coordinates'][1]
  612. );
  613. }
  614. }
  615. function showTwitterRssItem($entry)
  616. {
  617. $this->elementStart('item');
  618. $this->element('title', null, $entry['title']);
  619. $this->element('description', null, $entry['description']);
  620. $this->element('pubDate', null, $entry['pubDate']);
  621. $this->element('guid', null, $entry['guid']);
  622. $this->element('link', null, $entry['link']);
  623. // RSS only supports 1 enclosure per item
  624. if(array_key_exists('enclosures', $entry) and !empty($entry['enclosures'])){
  625. $enclosure = $entry['enclosures'][0];
  626. $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null);
  627. }
  628. if(array_key_exists('tags', $entry)){
  629. foreach($entry['tags'] as $tag){
  630. $this->element('category', null,$tag);
  631. }
  632. }
  633. $this->showGeoRSS($entry['geo']);
  634. $this->elementEnd('item');
  635. }
  636. function showJsonObjects($objects)
  637. {
  638. $json_objects = json_encode($objects);
  639. if($json_objects === false) {
  640. $this->clientError(_('JSON encoding failed. Error: ').json_last_error_msg());
  641. } else {
  642. print $json_objects;
  643. }
  644. }
  645. function showSingleXmlStatus($notice)
  646. {
  647. $this->initDocument('xml');
  648. $twitter_status = $this->twitterStatusArray($notice);
  649. $this->showTwitterXmlStatus($twitter_status, 'status', true);
  650. $this->endDocument('xml');
  651. }
  652. function showSingleAtomStatus($notice)
  653. {
  654. header('Content-Type: application/atom+xml;type=entry;charset="utf-8"');
  655. print '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  656. print $notice->asAtomEntry(true, true, true, $this->scoped);
  657. }
  658. function show_single_json_status($notice)
  659. {
  660. $this->initDocument('json');
  661. $status = $this->twitterStatusArray($notice);
  662. $this->showJsonObjects($status);
  663. $this->endDocument('json');
  664. }
  665. function showXmlTimeline($notice)
  666. {
  667. $this->initDocument('xml');
  668. $this->elementStart('statuses', array('type' => 'array',
  669. 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
  670. if (is_array($notice)) {
  671. //FIXME: make everything calling showJsonTimeline use only Notice objects
  672. $ids = array();
  673. foreach ($notice as $n) {
  674. $ids[] = $n->getID();
  675. }
  676. $notice = Notice::multiGet('id', $ids);
  677. }
  678. while ($notice->fetch()) {
  679. try {
  680. $twitter_status = $this->twitterStatusArray($notice);
  681. $this->showTwitterXmlStatus($twitter_status);
  682. } catch (Exception $e) {
  683. common_log(LOG_ERR, $e->getMessage());
  684. continue;
  685. }
  686. }
  687. $this->elementEnd('statuses');
  688. $this->endDocument('xml');
  689. }
  690. function showRssTimeline($notice, $title, $link, $subtitle, $suplink = null, $logo = null, $self = null)
  691. {
  692. $this->initDocument('rss');
  693. $this->element('title', null, $title);
  694. $this->element('link', null, $link);
  695. if (!is_null($self)) {
  696. $this->element(
  697. 'atom:link',
  698. array(
  699. 'type' => 'application/rss+xml',
  700. 'href' => $self,
  701. 'rel' => 'self'
  702. )
  703. );
  704. }
  705. if (!is_null($suplink)) {
  706. // For FriendFeed's SUP protocol
  707. $this->element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
  708. 'rel' => 'http://api.friendfeed.com/2008/03#sup',
  709. 'href' => $suplink,
  710. 'type' => 'application/json'));
  711. }
  712. if (!is_null($logo)) {
  713. $this->elementStart('image');
  714. $this->element('link', null, $link);
  715. $this->element('title', null, $title);
  716. $this->element('url', null, $logo);
  717. $this->elementEnd('image');
  718. }
  719. $this->element('description', null, $subtitle);
  720. $this->element('language', null, 'en-us');
  721. $this->element('ttl', null, '40');
  722. if (is_array($notice)) {
  723. //FIXME: make everything calling showJsonTimeline use only Notice objects
  724. $ids = array();
  725. foreach ($notice as $n) {
  726. $ids[] = $n->getID();
  727. }
  728. $notice = Notice::multiGet('id', $ids);
  729. }
  730. while ($notice->fetch()) {
  731. try {
  732. $entry = $this->twitterRssEntryArray($notice);
  733. $this->showTwitterRssItem($entry);
  734. } catch (Exception $e) {
  735. common_log(LOG_ERR, $e->getMessage());
  736. // continue on exceptions
  737. }
  738. }
  739. $this->endTwitterRss();
  740. }
  741. function showAtomTimeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null, $logo=null)
  742. {
  743. $this->initDocument('atom');
  744. $this->element('title', null, $title);
  745. $this->element('id', null, $id);
  746. $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
  747. if (!is_null($logo)) {
  748. $this->element('logo',null,$logo);
  749. }
  750. if (!is_null($suplink)) {
  751. // For FriendFeed's SUP protocol
  752. $this->element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
  753. 'href' => $suplink,
  754. 'type' => 'application/json'));
  755. }
  756. if (!is_null($selfuri)) {
  757. $this->element('link', array('href' => $selfuri,
  758. 'rel' => 'self', 'type' => 'application/atom+xml'), null);
  759. }
  760. $this->element('updated', null, common_date_iso8601('now'));
  761. $this->element('subtitle', null, $subtitle);
  762. if (is_array($notice)) {
  763. //FIXME: make everything calling showJsonTimeline use only Notice objects
  764. $ids = array();
  765. foreach ($notice as $n) {
  766. $ids[] = $n->getID();
  767. }
  768. $notice = Notice::multiGet('id', $ids);
  769. }
  770. while ($notice->fetch()) {
  771. try {
  772. $this->raw($notice->asAtomEntry());
  773. } catch (Exception $e) {
  774. common_log(LOG_ERR, $e->getMessage());
  775. continue;
  776. }
  777. }
  778. $this->endDocument('atom');
  779. }
  780. function showRssGroups($group, $title, $link, $subtitle)
  781. {
  782. $this->initDocument('rss');
  783. $this->element('title', null, $title);
  784. $this->element('link', null, $link);
  785. $this->element('description', null, $subtitle);
  786. $this->element('language', null, 'en-us');
  787. $this->element('ttl', null, '40');
  788. if (is_array($group)) {
  789. foreach ($group as $g) {
  790. $twitter_group = $this->twitterRssGroupArray($g);
  791. $this->showTwitterRssItem($twitter_group);
  792. }
  793. } else {
  794. while ($group->fetch()) {
  795. $twitter_group = $this->twitterRssGroupArray($group);
  796. $this->showTwitterRssItem($twitter_group);
  797. }
  798. }
  799. $this->endTwitterRss();
  800. }
  801. function showTwitterAtomEntry($entry)
  802. {
  803. $this->elementStart('entry');
  804. $this->element('title', null, common_xml_safe_str($entry['title']));
  805. $this->element(
  806. 'content',
  807. array('type' => 'html'),
  808. common_xml_safe_str($entry['content'])
  809. );
  810. $this->element('id', null, $entry['id']);
  811. $this->element('published', null, $entry['published']);
  812. $this->element('updated', null, $entry['updated']);
  813. $this->element('link', array('type' => 'text/html',
  814. 'href' => $entry['link'],
  815. 'rel' => 'alternate'));
  816. $this->element('link', array('type' => $entry['avatar-type'],
  817. 'href' => $entry['avatar'],
  818. 'rel' => 'image'));
  819. $this->elementStart('author');
  820. $this->element('name', null, $entry['author-name']);
  821. $this->element('uri', null, $entry['author-uri']);
  822. $this->elementEnd('author');
  823. $this->elementEnd('entry');
  824. }
  825. function showAtomGroups($group, $title, $id, $link, $subtitle=null, $selfuri=null)
  826. {
  827. $this->initDocument('atom');
  828. $this->element('title', null, common_xml_safe_str($title));
  829. $this->element('id', null, $id);
  830. $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
  831. if (!is_null($selfuri)) {
  832. $this->element('link', array('href' => $selfuri,
  833. 'rel' => 'self', 'type' => 'application/atom+xml'), null);
  834. }
  835. $this->element('updated', null, common_date_iso8601('now'));
  836. $this->element('subtitle', null, common_xml_safe_str($subtitle));
  837. if (is_array($group)) {
  838. foreach ($group as $g) {
  839. $this->raw($g->asAtomEntry());
  840. }
  841. } else {
  842. while ($group->fetch()) {
  843. $this->raw($group->asAtomEntry());
  844. }
  845. }
  846. $this->endDocument('atom');
  847. }
  848. function showJsonTimeline($notice)
  849. {
  850. $this->initDocument('json');
  851. $statuses = array();
  852. if (is_array($notice)) {
  853. //FIXME: make everything calling showJsonTimeline use only Notice objects
  854. $ids = array();
  855. foreach ($notice as $n) {
  856. $ids[] = $n->getID();
  857. }
  858. $notice = Notice::multiGet('id', $ids);
  859. }
  860. while ($notice->fetch()) {
  861. try {
  862. $twitter_status = $this->twitterStatusArray($notice);
  863. array_push($statuses, $twitter_status);
  864. } catch (Exception $e) {
  865. common_log(LOG_ERR, $e->getMessage());
  866. continue;
  867. }
  868. }
  869. $this->showJsonObjects($statuses);
  870. $this->endDocument('json');
  871. }
  872. function showJsonGroups($group)
  873. {
  874. $this->initDocument('json');
  875. $groups = array();
  876. if (is_array($group)) {
  877. foreach ($group as $g) {
  878. $twitter_group = $this->twitterGroupArray($g);
  879. array_push($groups, $twitter_group);
  880. }
  881. } else {
  882. while ($group->fetch()) {
  883. $twitter_group = $this->twitterGroupArray($group);
  884. array_push($groups, $twitter_group);
  885. }
  886. }
  887. $this->showJsonObjects($groups);
  888. $this->endDocument('json');
  889. }
  890. function showXmlGroups($group)
  891. {
  892. $this->initDocument('xml');
  893. $this->elementStart('groups', array('type' => 'array'));
  894. if (is_array($group)) {
  895. foreach ($group as $g) {
  896. $twitter_group = $this->twitterGroupArray($g);
  897. $this->showTwitterXmlGroup($twitter_group);
  898. }
  899. } else {
  900. while ($group->fetch()) {
  901. $twitter_group = $this->twitterGroupArray($group);
  902. $this->showTwitterXmlGroup($twitter_group);
  903. }
  904. }
  905. $this->elementEnd('groups');
  906. $this->endDocument('xml');
  907. }
  908. function showXmlLists($list, $next_cursor=0, $prev_cursor=0)
  909. {
  910. $this->initDocument('xml');
  911. $this->elementStart('lists_list');
  912. $this->elementStart('lists', array('type' => 'array'));
  913. if (is_array($list)) {
  914. foreach ($list as $l) {
  915. $twitter_list = $this->twitterListArray($l);
  916. $this->showTwitterXmlList($twitter_list);
  917. }
  918. } else {
  919. while ($list->fetch()) {
  920. $twitter_list = $this->twitterListArray($list);
  921. $this->showTwitterXmlList($twitter_list);
  922. }
  923. }
  924. $this->elementEnd('lists');
  925. $this->element('next_cursor', null, $next_cursor);
  926. $this->element('previous_cursor', null, $prev_cursor);
  927. $this->elementEnd('lists_list');
  928. $this->endDocument('xml');
  929. }
  930. function showJsonLists($list, $next_cursor=0, $prev_cursor=0)
  931. {
  932. $this->initDocument('json');
  933. $lists = array();
  934. if (is_array($list)) {
  935. foreach ($list as $l) {
  936. $twitter_list = $this->twitterListArray($l);
  937. array_push($lists, $twitter_list);
  938. }
  939. } else {
  940. while ($list->fetch()) {
  941. $twitter_list = $this->twitterListArray($list);
  942. array_push($lists, $twitter_list);
  943. }
  944. }
  945. $lists_list = array(
  946. 'lists' => $lists,
  947. 'next_cursor' => $next_cursor,
  948. 'next_cursor_str' => strval($next_cursor),
  949. 'previous_cursor' => $prev_cursor,
  950. 'previous_cursor_str' => strval($prev_cursor)
  951. );
  952. $this->showJsonObjects($lists_list);
  953. $this->endDocument('json');
  954. }
  955. function showTwitterXmlUsers($user)
  956. {
  957. $this->initDocument('xml');
  958. $this->elementStart('users', array('type' => 'array',
  959. 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
  960. if (is_array($user)) {
  961. foreach ($user as $u) {
  962. $twitter_user = $this->twitterUserArray($u);
  963. $this->showTwitterXmlUser($twitter_user);
  964. }
  965. } else {
  966. while ($user->fetch()) {
  967. $twitter_user = $this->twitterUserArray($user);
  968. $this->showTwitterXmlUser($twitter_user);
  969. }
  970. }
  971. $this->elementEnd('users');
  972. $this->endDocument('xml');
  973. }
  974. function showJsonUsers($user)
  975. {
  976. $this->initDocument('json');
  977. $users = array();
  978. if (is_array($user)) {
  979. foreach ($user as $u) {
  980. $twitter_user = $this->twitterUserArray($u);
  981. array_push($users, $twitter_user);
  982. }
  983. } else {
  984. while ($user->fetch()) {
  985. $twitter_user = $this->twitterUserArray($user);
  986. array_push($users, $twitter_user);
  987. }
  988. }
  989. $this->showJsonObjects($users);
  990. $this->endDocument('json');
  991. }
  992. function showSingleJsonGroup($group)
  993. {
  994. $this->initDocument('json');
  995. $twitter_group = $this->twitterGroupArray($group);
  996. $this->showJsonObjects($twitter_group);
  997. $this->endDocument('json');
  998. }
  999. function showSingleXmlGroup($group)
  1000. {
  1001. $this->initDocument('xml');
  1002. $twitter_group = $this->twitterGroupArray($group);
  1003. $this->showTwitterXmlGroup($twitter_group);
  1004. $this->endDocument('xml');
  1005. }
  1006. function showSingleJsonList($list)
  1007. {
  1008. $this->initDocument('json');
  1009. $twitter_list = $this->twitterListArray($list);
  1010. $this->showJsonObjects($twitter_list);
  1011. $this->endDocument('json');
  1012. }
  1013. function showSingleXmlList($list)
  1014. {
  1015. $this->initDocument('xml');
  1016. $twitter_list = $this->twitterListArray($list);
  1017. $this->showTwitterXmlList($twitter_list);
  1018. $this->endDocument('xml');
  1019. }
  1020. static function dateTwitter($dt)
  1021. {
  1022. $dateStr = date('d F Y H:i:s', strtotime($dt));
  1023. $d = new DateTime($dateStr, new DateTimeZone('UTC'));
  1024. $d->setTimezone(new DateTimeZone(common_timezone()));
  1025. return $d->format('D M d H:i:s O Y');
  1026. }
  1027. function initDocument($type='xml')
  1028. {
  1029. switch ($type) {
  1030. case 'xml':
  1031. header('Content-Type: application/xml; charset=utf-8');
  1032. $this->startXML();
  1033. break;
  1034. case 'json':
  1035. header('Content-Type: application/json; charset=utf-8');
  1036. // Check for JSONP callback
  1037. if (isset($this->callback)) {
  1038. print $this->callback . '(';
  1039. }
  1040. break;
  1041. case 'rss':
  1042. header("Content-Type: application/rss+xml; charset=utf-8");
  1043. $this->initTwitterRss();
  1044. break;
  1045. case 'atom':
  1046. header('Content-Type: application/atom+xml; charset=utf-8');
  1047. $this->initTwitterAtom();
  1048. break;
  1049. default:
  1050. // TRANS: Client error on an API request with an unsupported data format.
  1051. $this->clientError(_('Not a supported data format.'));
  1052. }
  1053. return;
  1054. }
  1055. function endDocument($type='xml')
  1056. {
  1057. switch ($type) {
  1058. case 'xml':
  1059. $this->endXML();
  1060. break;
  1061. case 'json':
  1062. // Check for JSONP callback
  1063. if (isset($this->callback)) {
  1064. print ')';
  1065. }
  1066. break;
  1067. case 'rss':
  1068. $this->endTwitterRss();
  1069. break;
  1070. case 'atom':
  1071. $this->endTwitterRss();
  1072. break;
  1073. default:
  1074. // TRANS: Client error on an API request with an unsupported data format.
  1075. $this->clientError(_('Not a supported data format.'));
  1076. }
  1077. return;
  1078. }
  1079. function initTwitterRss()
  1080. {
  1081. $this->startXML();
  1082. $this->elementStart(
  1083. 'rss',
  1084. array(
  1085. 'version' => '2.0',
  1086. 'xmlns:atom' => 'http://www.w3.org/2005/Atom',
  1087. 'xmlns:georss' => 'http://www.georss.org/georss'
  1088. )
  1089. );
  1090. $this->elementStart('channel');
  1091. Event::handle('StartApiRss', array($this));
  1092. }
  1093. function endTwitterRss()
  1094. {
  1095. $this->elementEnd('channel');
  1096. $this->elementEnd('rss');
  1097. $this->endXML();
  1098. }
  1099. function initTwitterAtom()
  1100. {
  1101. $this->startXML();
  1102. // FIXME: don't hardcode the language here!
  1103. $this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom',
  1104. 'xml:lang' => 'en-US',
  1105. 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0'));
  1106. }
  1107. function endTwitterAtom()
  1108. {
  1109. $this->elementEnd('feed');
  1110. $this->endXML();
  1111. }
  1112. function showProfile($profile, $content_type='xml', $notice=null, $includeStatuses=true)
  1113. {
  1114. $profile_array = $this->twitterUserArray($profile, $includeStatuses);
  1115. switch ($content_type) {
  1116. case 'xml':
  1117. $this->showTwitterXmlUser($profile_array);
  1118. break;
  1119. case 'json':
  1120. $this->showJsonObjects($profile_array);
  1121. break;
  1122. default:
  1123. // TRANS: Client error on an API request with an unsupported data format.
  1124. $this->clientError(_('Not a supported data format.'));
  1125. }
  1126. return;
  1127. }
  1128. private static function is_decimal($str)
  1129. {
  1130. return preg_match('/^[0-9]+$/', $str);
  1131. }
  1132. function getTargetUser($id)
  1133. {
  1134. if (empty($id)) {
  1135. // Twitter supports these other ways of passing the user ID
  1136. if (self::is_decimal($this->arg('id'))) {
  1137. return User::getKV($this->arg('id'));
  1138. } else if ($this->arg('id')) {
  1139. $nickname = common_canonical_nickname($this->arg('id'));
  1140. return User::getKV('nickname', $nickname);
  1141. } else if ($this->arg('user_id')) {
  1142. // This is to ensure that a non-numeric user_id still
  1143. // overrides screen_name even if it doesn't get used
  1144. if (self::is_decimal($this->arg('user_id'))) {
  1145. return User::getKV('id', $this->arg('user_id'));
  1146. }
  1147. } else if ($this->arg('screen_name')) {
  1148. $nickname = common_canonical_nickname($this->arg('screen_name'));
  1149. return User::getKV('nickname', $nickname);
  1150. } else {
  1151. // Fall back to trying the currently authenticated user
  1152. return $this->scoped->getUser();
  1153. }
  1154. } else if (self::is_decimal($id)) {
  1155. return User::getKV($id);
  1156. } else {
  1157. $nickname = common_canonical_nickname($id);
  1158. return User::getKV('nickname', $nickname);
  1159. }
  1160. }
  1161. function getTargetProfile($id)
  1162. {
  1163. if (empty($id)) {
  1164. // Twitter supports these other ways of passing the user ID
  1165. if (self::is_decimal($this->arg('id'))) {
  1166. return Profile::getKV($this->arg('id'));
  1167. } else if ($this->arg('id')) {
  1168. // Screen names currently can only uniquely identify a local user.
  1169. $nickname = common_canonical_nickname($this->arg('id'));
  1170. $user = User::getKV('nickname', $nickname);
  1171. return $user ? $user->getProfile() : null;
  1172. } else if ($this->arg('user_id')) {
  1173. // This is to ensure that a non-numeric user_id still
  1174. // overrides screen_name even if it doesn't get used
  1175. if (self::is_decimal($this->arg('user_id'))) {
  1176. return Profile::getKV('id', $this->arg('user_id'));
  1177. }
  1178. } elseif (mb_strlen($this->arg('screen_name')) > 0) {
  1179. $nickname = common_canonical_nickname($this->arg('screen_name'));
  1180. $user = User::getByNickname($nickname);
  1181. return $user->getProfile();
  1182. } else {
  1183. // Fall back to trying the currently authenticated user
  1184. return $this->scoped;
  1185. }
  1186. } else if (self::is_decimal($id) && intval($id) > 0) {
  1187. return Profile::getByID($id);
  1188. } else {
  1189. // FIXME: check if isAcct to identify remote profiles and not just local nicknames
  1190. $nickname = common_canonical_nickname($id);
  1191. $user = User::getByNickname($nickname);
  1192. return $user->getProfile();
  1193. }
  1194. }
  1195. function getTargetGroup($id)
  1196. {
  1197. if (empty($id)) {
  1198. if (self::is_decimal($this->arg('id'))) {
  1199. return User_group::getKV('id', $this->arg('id'));
  1200. } else if ($this->arg('id')) {
  1201. return User_group::getForNickname($this->arg('id'));
  1202. } else if ($this->arg('group_id')) {
  1203. // This is to ensure that a non-numeric group_id still
  1204. // overrides group_name even if it doesn't get used
  1205. if (self::is_decimal($this->arg('group_id'))) {
  1206. return User_group::getKV('id', $this->arg('group_id'));
  1207. }
  1208. } else if ($this->arg('group_name')) {
  1209. return User_group::getForNickname($this->arg('group_name'));
  1210. }
  1211. } else if (self::is_decimal($id)) {
  1212. return User_group::getKV('id', $id);
  1213. } else if ($this->arg('uri')) { // FIXME: move this into empty($id) check?
  1214. return User_group::getKV('uri', urldecode($this->arg('uri')));
  1215. } else {
  1216. return User_group::getForNickname($id);
  1217. }
  1218. }
  1219. function getTargetList($user=null, $id=null)
  1220. {
  1221. $tagger = $this->getTargetUser($user);
  1222. $list = null;
  1223. if (empty($id)) {
  1224. $id = $this->arg('id');
  1225. }
  1226. if($id) {
  1227. if (is_numeric($id)) {
  1228. $list = Profile_list::getKV('id', $id);
  1229. // only if the list with the id belongs to the tagger
  1230. if(empty($list) || $list->tagger != $tagger->id) {
  1231. $list = null;
  1232. }
  1233. }
  1234. if (empty($list)) {
  1235. $tag = common_canonical_tag($id);
  1236. $list = Profile_list::getByTaggerAndTag($tagger->id, $tag);
  1237. }
  1238. if (!empty($list) && $list->private) {
  1239. if ($this->scoped->id == $list->tagger) {
  1240. return $list;
  1241. }
  1242. } else {
  1243. return $list;
  1244. }
  1245. }
  1246. return null;
  1247. }
  1248. /**
  1249. * Returns query argument or default value if not found. Certain
  1250. * parameters used throughout the API are lightly scrubbed and
  1251. * bounds checked. This overrides Action::arg().
  1252. *
  1253. * @param string $key requested argument
  1254. * @param string $def default value to return if $key is not provided
  1255. *
  1256. * @return var $var
  1257. */
  1258. function arg($key, $def=null)
  1259. {
  1260. // XXX: Do even more input validation/scrubbing?
  1261. if (array_key_exists($key, $this->args)) {
  1262. switch($key) {
  1263. case 'page':
  1264. $page = (int)$this->args['page'];
  1265. return ($page < 1) ? 1 : $page;
  1266. case 'count':
  1267. $count = (int)$this->args['count'];
  1268. if ($count < 1) {
  1269. return 20;
  1270. } elseif ($count > 200) {
  1271. return 200;
  1272. } else {
  1273. return $count;
  1274. }
  1275. case 'since_id':
  1276. $since_id = (int)$this->args['since_id'];
  1277. return ($since_id < 1) ? 0 : $since_id;
  1278. case 'max_id':
  1279. $max_id = (int)$this->args['max_id'];
  1280. return ($max_id < 1) ? 0 : $max_id;
  1281. default:
  1282. return parent::arg($key, $def);
  1283. }
  1284. } else {
  1285. return $def;
  1286. }
  1287. }
  1288. /**
  1289. * Calculate the complete URI that called up this action. Used for
  1290. * Atom rel="self" links. Warning: this is funky.
  1291. *
  1292. * @return string URL a URL suitable for rel="self" Atom links
  1293. */
  1294. function getSelfUri()
  1295. {
  1296. $action = mb_substr(get_class($this), 0, -6); // remove 'Action'
  1297. $id = $this->arg('id');
  1298. $aargs = array('format' => $this->format);
  1299. if (!empty($id)) {
  1300. $aargs['id'] = $id;
  1301. }
  1302. $user = $this->arg('user');
  1303. if (!empty($user)) {
  1304. $aargs['user'] = $user;
  1305. }
  1306. $tag = $this->arg('tag');
  1307. if (!empty($tag)) {
  1308. $aargs['tag'] = $tag;
  1309. }
  1310. parse_str($_SERVER['QUERY_STRING'], $params);
  1311. $pstring = '';
  1312. if (!empty($params)) {
  1313. unset($params['p']);
  1314. $pstring = http_build_query($params);
  1315. }
  1316. $uri = common_local_url($action, $aargs);
  1317. if (!empty($pstring)) {
  1318. $uri .= '?' . $pstring;
  1319. }
  1320. return $uri;
  1321. }
  1322. }