X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Fapiaction.php;h=c0e5095fd5030153ed71275f34e477bba47f64db;hb=c44a94e8f59d879b5e829f990e4d2818f34ccbd3;hp=8a7be31502f29e89ca02f5370a22b4d89f0351ac;hpb=407663fb402f0384cced2b63a2f901d19ab71d19;p=quix0rs-gnu-social.git diff --git a/lib/apiaction.php b/lib/apiaction.php index 8a7be31502..c0e5095fd5 100644 --- a/lib/apiaction.php +++ b/lib/apiaction.php @@ -98,6 +98,8 @@ if (!defined('STATUSNET')) { exit(1); } +class ApiValidationException extends Exception { } + /** * Contains most of the Twitter-compatible API output functions. * @@ -200,6 +202,8 @@ class ApiAction extends Action { $twitter_user = array(); + $user = $profile->getUser(); + $twitter_user['id'] = intval($profile->id); $twitter_user['name'] = $profile->getBestName(); $twitter_user['screen_name'] = $profile->nickname; @@ -211,11 +215,10 @@ class ApiAction extends Action Avatar::defaultImage(AVATAR_STREAM_SIZE); $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null; - $twitter_user['protected'] = false; # not supported by StatusNet yet + $twitter_user['protected'] = ($user->private_stream) ? true : false; $twitter_user['followers_count'] = $profile->subscriberCount(); - $design = null; - $user = $profile->getUser(); + $design = null; // Note: some profiles don't have an associated user @@ -263,8 +266,7 @@ class ApiAction extends Action ? Design::url($design->backgroundimage) : ''; $twitter_user['profile_background_tile'] - = empty($design->disposition) - ? '' : ($design->disposition & BACKGROUND_TILE) ? 'true' : 'false'; + = (bool)($design->disposition & BACKGROUND_TILE); $twitter_user['statuses_count'] = $profile->noticeCount(); @@ -291,7 +293,7 @@ class ApiAction extends Action if ($get_notice) { $notice = $profile->getCurrentNotice(); if ($notice) { - # don't get user! + // don't get user! $twitter_user['status'] = $this->twitterStatusArray($notice, false); } } @@ -396,7 +398,7 @@ class ApiAction extends Action } if ($include_user && $profile) { - # Don't get notice (recursive!) + // Don't get notice (recursive!) $twitter_user = $this->twitterUserArray($profile, false); $twitter_status['user'] = $twitter_user; } @@ -412,7 +414,7 @@ class ApiAction extends Action { $twitter_group = array(); - $twitter_group['id'] = $group->id; + $twitter_group['id'] = intval($group->id); $twitter_group['url'] = $group->permalink(); $twitter_group['nickname'] = $group->nickname; $twitter_group['fullname'] = $group->fullname; @@ -457,6 +459,32 @@ class ApiAction extends Action return $entry; } + function twitterListArray($list) + { + $profile = Profile::staticGet('id', $list->tagger); + + $twitter_list = array(); + $twitter_list['id'] = $list->id; + $twitter_list['name'] = $list->tag; + $twitter_list['full_name'] = '@'.$profile->nickname.'/'.$list->tag;; + $twitter_list['slug'] = $list->tag; + $twitter_list['description'] = $list->description; + $twitter_list['subscriber_count'] = $list->subscriberCount(); + $twitter_list['member_count'] = $list->taggedCount(); + $twitter_list['uri'] = $list->getUri(); + + if (isset($this->auth_user)) { + $twitter_list['following'] = $list->hasSubscriber($this->auth_user); + } else { + $twitter_list['following'] = false; + } + + $twitter_list['mode'] = ($list->private) ? 'private' : 'public'; + $twitter_list['user'] = $this->twitterUserArray($profile, false); + + return $twitter_list; + } + function twitterRssEntryArray($notice) { $entry = array(); @@ -561,7 +589,7 @@ class ApiAction extends Action $details['notifications_enabled'] = $notifications; $details['blocking'] = $source->hasBlocked($target); - $details['id'] = $source->id; + $details['id'] = intval($source->id); return $details; } @@ -632,6 +660,20 @@ class ApiAction extends Action $this->elementEnd('group'); } + function showTwitterXmlList($twitter_list) + { + $this->elementStart('list'); + foreach($twitter_list as $element => $value) { + if($element == 'user') { + $this->showTwitterXmlUser($value, 'user'); + } + else { + $this->element($element, null, $value); + } + } + $this->elementEnd('list'); + } + function showTwitterXmlUser($twitter_user, $role='user', $namespaces=false) { $attrs = array(); @@ -697,7 +739,7 @@ class ApiAction extends Action $this->element('guid', null, $entry['guid']); $this->element('link', null, $entry['link']); - # RSS only supports 1 enclosure per item + // RSS only supports 1 enclosure per item if(array_key_exists('enclosures', $entry) and !empty($entry['enclosures'])){ $enclosure = $entry['enclosures'][0]; $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null); @@ -832,7 +874,7 @@ class ApiAction extends Action } if (!is_null($suplink)) { - # For FriendFeed's SUP protocol + // For FriendFeed's SUP protocol $this->element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup', 'href' => $suplink, 'type' => 'application/json')); @@ -945,10 +987,10 @@ class ApiAction extends Action $from_profile = $message->getFrom(); $to_profile = $message->getTo(); - $dmsg['id'] = $message->id; - $dmsg['sender_id'] = $message->from_profile; + $dmsg['id'] = intval($message->id); + $dmsg['sender_id'] = intval($from_profile); $dmsg['text'] = trim($message->content); - $dmsg['recipient_id'] = $message->to_profile; + $dmsg['recipient_id'] = intval($to_profile); $dmsg['created_at'] = $this->dateTwitter($message->created); $dmsg['sender_screen_name'] = $from_profile->nickname; $dmsg['recipient_screen_name'] = $to_profile->nickname; @@ -1109,6 +1151,65 @@ class ApiAction extends Action $this->endDocument('xml'); } + function showXmlLists($list, $next_cursor=0, $prev_cursor=0) + { + + $this->initDocument('xml'); + $this->elementStart('lists_list'); + $this->elementStart('lists', array('type' => 'array')); + + if (is_array($list)) { + foreach ($list as $l) { + $twitter_list = $this->twitterListArray($l); + $this->showTwitterXmlList($twitter_list); + } + } else { + while ($list->fetch()) { + $twitter_list = $this->twitterListArray($list); + $this->showTwitterXmlList($twitter_list); + } + } + + $this->elementEnd('lists'); + + $this->element('next_cursor', null, $next_cursor); + $this->element('previous_cursor', null, $prev_cursor); + + $this->elementEnd('lists_list'); + $this->endDocument('xml'); + } + + function showJsonLists($list, $next_cursor=0, $prev_cursor=0) + { + $this->initDocument('json'); + + $lists = array(); + + if (is_array($list)) { + foreach ($list as $l) { + $twitter_list = $this->twitterListArray($l); + array_push($lists, $twitter_list); + } + } else { + while ($list->fetch()) { + $twitter_list = $this->twitterListArray($list); + array_push($lists, $twitter_list); + } + } + + $lists_list = array( + 'lists' => $lists, + 'next_cursor' => $next_cursor, + 'next_cursor_str' => strval($next_cursor), + 'previous_cursor' => $prev_cursor, + 'previous_cursor_str' => strval($prev_cursor) + ); + + $this->showJsonObjects($lists_list); + + $this->endDocument('json'); + } + function showTwitterXmlUsers($user) { $this->initDocument('xml'); @@ -1170,6 +1271,22 @@ class ApiAction extends Action $this->endDocument('xml'); } + function showSingleJsonList($list) + { + $this->initDocument('json'); + $twitter_list = $this->twitterListArray($list); + $this->showJsonObjects($twitter_list); + $this->endDocument('json'); + } + + function showSingleXmlList($list) + { + $this->initDocument('xml'); + $twitter_list = $this->twitterListArray($list); + $this->showTwitterXmlList($twitter_list); + $this->endDocument('xml'); + } + function dateTwitter($dt) { $dateStr = date('d F Y H:i:s', strtotime($dt)); @@ -1236,9 +1353,12 @@ class ApiAction extends Action return; } - function clientError($msg, $code = 400, $format = 'xml') + function clientError($msg, $code = 400, $format = null) { $action = $this->trimmed('action'); + if ($format === null) { + $format = $this->format; + } common_debug("User error '$code' on '$action': $msg", __FILE__); @@ -1278,9 +1398,12 @@ class ApiAction extends Action } } - function serverError($msg, $code = 500, $content_type = 'xml') + function serverError($msg, $code = 500, $content_type = null) { $action = $this->trimmed('action'); + if ($content_type === null) { + $content_type = $this->format; + } common_debug("Server error '$code' on '$action': $msg", __FILE__); @@ -1365,11 +1488,16 @@ class ApiAction extends Action return; } + private static function is_decimal($str) + { + return preg_match('/^[0-9]+$/', $str); + } + function getTargetUser($id) { if (empty($id)) { // Twitter supports these other ways of passing the user ID - if (is_numeric($this->arg('id'))) { + if (self::is_decimal($this->arg('id'))) { return User::staticGet($this->arg('id')); } else if ($this->arg('id')) { $nickname = common_canonical_nickname($this->arg('id')); @@ -1377,7 +1505,7 @@ class ApiAction extends Action } else if ($this->arg('user_id')) { // This is to ensure that a non-numeric user_id still // overrides screen_name even if it doesn't get used - if (is_numeric($this->arg('user_id'))) { + if (self::is_decimal($this->arg('user_id'))) { return User::staticGet('id', $this->arg('user_id')); } } else if ($this->arg('screen_name')) { @@ -1388,7 +1516,7 @@ class ApiAction extends Action return $this->auth_user; } - } else if (is_numeric($id)) { + } else if (self::is_decimal($id)) { return User::staticGet($id); } else { $nickname = common_canonical_nickname($id); @@ -1401,7 +1529,7 @@ class ApiAction extends Action if (empty($id)) { // Twitter supports these other ways of passing the user ID - if (is_numeric($this->arg('id'))) { + if (self::is_decimal($this->arg('id'))) { return Profile::staticGet($this->arg('id')); } else if ($this->arg('id')) { // Screen names currently can only uniquely identify a local user. @@ -1411,7 +1539,7 @@ class ApiAction extends Action } else if ($this->arg('user_id')) { // This is to ensure that a non-numeric user_id still // overrides screen_name even if it doesn't get used - if (is_numeric($this->arg('user_id'))) { + if (self::is_decimal($this->arg('user_id'))) { return Profile::staticGet('id', $this->arg('user_id')); } } else if ($this->arg('screen_name')) { @@ -1419,7 +1547,7 @@ class ApiAction extends Action $user = User::staticGet('nickname', $nickname); return $user ? $user->getProfile() : null; } - } else if (is_numeric($id)) { + } else if (self::is_decimal($id)) { return Profile::staticGet($id); } else { $nickname = common_canonical_nickname($id); @@ -1431,43 +1559,59 @@ class ApiAction extends Action function getTargetGroup($id) { if (empty($id)) { - if (is_numeric($this->arg('id'))) { - return User_group::staticGet($this->arg('id')); + if (self::is_decimal($this->arg('id'))) { + return User_group::staticGet('id', $this->arg('id')); } else if ($this->arg('id')) { - $nickname = common_canonical_nickname($this->arg('id')); - $local = Local_group::staticGet('nickname', $nickname); - if (empty($local)) { - return null; - } else { - return User_group::staticGet('id', $local->id); - } + return User_group::getForNickname($this->arg('id')); } else if ($this->arg('group_id')) { - // This is to ensure that a non-numeric user_id still - // overrides screen_name even if it doesn't get used - if (is_numeric($this->arg('group_id'))) { + // This is to ensure that a non-numeric group_id still + // overrides group_name even if it doesn't get used + if (self::is_decimal($this->arg('group_id'))) { return User_group::staticGet('id', $this->arg('group_id')); } } else if ($this->arg('group_name')) { - $nickname = common_canonical_nickname($this->arg('group_name')); - $local = Local_group::staticGet('nickname', $nickname); - if (empty($local)) { - return null; - } else { - return User_group::staticGet('id', $local->group_id); - } + return User_group::getForNickname($this->arg('group_name')); } - } else if (is_numeric($id)) { - return User_group::staticGet($id); + } else if (self::is_decimal($id)) { + return User_group::staticGet('id', $id); } else { - $nickname = common_canonical_nickname($id); - $local = Local_group::staticGet('nickname', $nickname); - if (empty($local)) { - return null; + return User_group::getForNickname($id); + } + } + + function getTargetList($user=null, $id=null) + { + $tagger = $this->getTargetUser($user); + $list = null; + + if (empty($id)) { + $id = $this->arg('id'); + } + + if($id) { + if (is_numeric($id)) { + $list = Profile_list::staticGet('id', $id); + + // only if the list with the id belongs to the tagger + if(empty($list) || $list->tagger != $tagger->id) { + $list = null; + } + } + if (empty($list)) { + $tag = common_canonical_tag($id); + $list = Profile_list::getByTaggerAndTag($tagger->id, $tag); + } + + if (!empty($list) && $list->private) { + if ($this->auth_user->id == $list->tagger) { + return $list; + } } else { - return User_group::staticGet('id', $local->group_id); + return $list; } } + return null; } /**