]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/apiaction.php
Ticket #3022: fix formatting output for ApiAction::clientError and ApiAction::serverE...
[quix0rs-gnu-social.git] / lib / apiaction.php
index 89a4871029d3efc208cd3fc512c777bb8f0840cc..6caf468bf2324ddc87f2714d9ab0321dcc102aad 100644 (file)
@@ -27,7 +27,7 @@
  * @author    Jeffery To <jeffery.to@gmail.com>
  * @author    Toby Inkster <mail@tobyinkster.co.uk>
  * @author    Zach Copley <zach@status.net>
- * @copyright 2009 StatusNet, Inc.
+ * @copyright 2009-2010 StatusNet, Inc.
  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link      http://status.net/
@@ -112,7 +112,6 @@ if (!defined('STATUSNET')) {
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link     http://status.net/
  */
-
 class ApiAction extends Action
 {
     const READ_ONLY  = 1;
@@ -126,6 +125,7 @@ class ApiAction extends Action
     var $max_id    = null;
     var $since_id  = null;
     var $source    = null;
+    var $callback  = null;
 
     var $access    = self::READ_ONLY;  // read (default) or read-write
 
@@ -138,13 +138,13 @@ class ApiAction extends Action
      *
      * @return boolean false if user doesn't exist
      */
-
     function prepare($args)
     {
         StatusNet::setApi(true); // reduce exception reports to aid in debugging
         parent::prepare($args);
 
         $this->format   = $this->arg('format');
+        $this->callback = $this->arg('callback');
         $this->page     = (int)$this->arg('page', 1);
         $this->count    = (int)$this->arg('count', 20);
         $this->max_id   = (int)$this->arg('max_id', 0);
@@ -170,7 +170,6 @@ class ApiAction extends Action
      *
      * @return void
      */
-
     function handle($args)
     {
         header('Access-Control-Allow-Origin: *');
@@ -299,7 +298,7 @@ class ApiAction extends Action
 
         // StatusNet-specific
 
-        $twitter_user['statusnet:profile_url'] = $profile->profileurl;
+        $twitter_user['statusnet_profile_url'] = $profile->profileurl;
 
         return $twitter_user;
     }
@@ -404,7 +403,7 @@ class ApiAction extends Action
 
         // StatusNet-specific
 
-        $twitter_status['statusnet:html'] = $notice->rendered;
+        $twitter_status['statusnet_html'] = $notice->rendered;
 
         return $twitter_status;
     }
@@ -460,65 +459,70 @@ class ApiAction extends Action
 
     function twitterRssEntryArray($notice)
     {
-        $profile = $notice->getProfile();
         $entry = array();
 
-        // We trim() to avoid extraneous whitespace in the output
+        if (Event::handle('StartRssEntryArray', array($notice, &$entry))) {
+            $profile = $notice->getProfile();
 
-        $entry['content'] = common_xml_safe_str(trim($notice->rendered));
-        $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
-        $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
-        $entry['published'] = common_date_iso8601($notice->created);
+            // We trim() to avoid extraneous whitespace in the output
 
-        $taguribase = TagURI::base();
-        $entry['id'] = "tag:$taguribase:$entry[link]";
+            $entry['content'] = common_xml_safe_str(trim($notice->rendered));
+            $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
+            $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
+            $entry['published'] = common_date_iso8601($notice->created);
 
-        $entry['updated'] = $entry['published'];
-        $entry['author'] = $profile->getBestName();
+            $taguribase = TagURI::base();
+            $entry['id'] = "tag:$taguribase:$entry[link]";
 
-        // Enclosures
-        $attachments = $notice->attachments();
-        $enclosures = array();
-
-        foreach ($attachments as $attachment) {
-            $enclosure_o=$attachment->getEnclosure();
-            if ($enclosure_o) {
-                 $enclosure = array();
-                 $enclosure['url'] = $enclosure_o->url;
-                 $enclosure['mimetype'] = $enclosure_o->mimetype;
-                 $enclosure['size'] = $enclosure_o->size;
-                 $enclosures[] = $enclosure;
-            }
-        }
+            $entry['updated'] = $entry['published'];
+            $entry['author'] = $profile->getBestName();
 
-        if (!empty($enclosures)) {
-            $entry['enclosures'] = $enclosures;
-        }
+            // Enclosures
+            $attachments = $notice->attachments();
+            $enclosures = array();
 
-        // Tags/Categories
-        $tag = new Notice_tag();
-        $tag->notice_id = $notice->id;
-        if ($tag->find()) {
-            $entry['tags']=array();
-            while ($tag->fetch()) {
-                $entry['tags'][]=$tag->tag;
+            foreach ($attachments as $attachment) {
+                $enclosure_o=$attachment->getEnclosure();
+                if ($enclosure_o) {
+                    $enclosure = array();
+                    $enclosure['url'] = $enclosure_o->url;
+                    $enclosure['mimetype'] = $enclosure_o->mimetype;
+                    $enclosure['size'] = $enclosure_o->size;
+                    $enclosures[] = $enclosure;
+                }
             }
-        }
-        $tag->free();
 
-        // RSS Item specific
-        $entry['description'] = $entry['content'];
-        $entry['pubDate'] = common_date_rfc2822($notice->created);
-        $entry['guid'] = $entry['link'];
+            if (!empty($enclosures)) {
+                $entry['enclosures'] = $enclosures;
+            }
 
-        if (isset($notice->lat) && isset($notice->lon)) {
-            // This is the format that GeoJSON expects stuff to be in.
-            // showGeoRSS() below uses it for XML output, so we reuse it
-            $entry['geo'] = array('type' => 'Point',
-                                  'coordinates' => array((float) $notice->lat,
-                                                         (float) $notice->lon));
-        } else {
-            $entry['geo'] = null;
+            // Tags/Categories
+            $tag = new Notice_tag();
+            $tag->notice_id = $notice->id;
+            if ($tag->find()) {
+                $entry['tags']=array();
+                while ($tag->fetch()) {
+                    $entry['tags'][]=$tag->tag;
+                }
+            }
+            $tag->free();
+
+            // RSS Item specific
+            $entry['description'] = $entry['content'];
+            $entry['pubDate'] = common_date_rfc2822($notice->created);
+            $entry['guid'] = $entry['link'];
+
+            if (isset($notice->lat) && isset($notice->lon)) {
+                // This is the format that GeoJSON expects stuff to be in.
+                // showGeoRSS() below uses it for XML output, so we reuse it
+                $entry['geo'] = array('type' => 'Point',
+                                      'coordinates' => array((float) $notice->lat,
+                                                             (float) $notice->lon));
+            } else {
+                $entry['geo'] = null;
+            }
+
+            Event::handle('EndRssEntryArray', array($notice, &$entry));
         }
 
         return $entry;
@@ -547,7 +551,6 @@ class ApiAction extends Action
         $notifications = false;
 
         if ($source->isSubscribed($target)) {
-
             $sub = Subscription::pkeyGet(array('subscriber' =>
                 $source->id, 'subscribed' => $target->id));
 
@@ -610,7 +613,11 @@ class ApiAction extends Action
                 $this->showTwitterXmlStatus($value, 'retweeted_status');
                 break;
             default:
-                $this->element($element, null, $value);
+                if (strncmp($element, 'statusnet_', 10) == 0) {
+                    $this->element('statusnet:'.substr($element, 10), null, $value);
+                } else {
+                    $this->element($element, null, $value);
+                }
             }
         }
         $this->elementEnd($tag);
@@ -635,6 +642,8 @@ class ApiAction extends Action
         foreach($twitter_user as $element => $value) {
             if ($element == 'status') {
                 $this->showTwitterXmlStatus($twitter_user['status']);
+            } else if (strncmp($element, 'statusnet_', 10) == 0) {
+                $this->element('statusnet:'.substr($element, 10), null, $value);
             } else {
                 $this->element($element, null, $value);
             }
@@ -717,6 +726,12 @@ class ApiAction extends Action
         $this->endDocument('xml');
     }
 
+    function showSingleAtomStatus($notice)
+    {
+        header('Content-Type: application/atom+xml; charset=utf-8');
+        print $notice->asAtomEntry(true, true, true, $this->auth_user);
+    }
+
     function show_single_json_status($notice)
     {
         $this->initDocument('json');
@@ -727,20 +742,21 @@ class ApiAction extends Action
 
     function showXmlTimeline($notice)
     {
-
         $this->initDocument('xml');
         $this->elementStart('statuses', array('type' => 'array',
                                               'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
 
         if (is_array($notice)) {
-            foreach ($notice as $n) {
-                $twitter_status = $this->twitterStatusArray($n);
-                $this->showTwitterXmlStatus($twitter_status);
-            }
-        } else {
-            while ($notice->fetch()) {
+            $notice = new ArrayWrapper($notice);
+        }
+
+        while ($notice->fetch()) {
+            try {
                 $twitter_status = $this->twitterStatusArray($notice);
                 $this->showTwitterXmlStatus($twitter_status);
+            } catch (Exception $e) {
+                common_log(LOG_ERR, $e->getMessage());
+                continue;
             }
         }
 
@@ -750,7 +766,6 @@ class ApiAction extends Action
 
     function showRssTimeline($notice, $title, $link, $subtitle, $suplink = null, $logo = null, $self = null)
     {
-
         $this->initDocument('rss');
 
         $this->element('title', null, $title);
@@ -788,14 +803,16 @@ class ApiAction extends Action
         $this->element('ttl', null, '40');
 
         if (is_array($notice)) {
-            foreach ($notice as $n) {
-                $entry = $this->twitterRssEntryArray($n);
-                $this->showTwitterRssItem($entry);
-            }
-        } else {
-            while ($notice->fetch()) {
+            $notice = new ArrayWrapper($notice);
+        }
+
+        while ($notice->fetch()) {
+            try {
                 $entry = $this->twitterRssEntryArray($notice);
                 $this->showTwitterRssItem($entry);
+            } catch (Exception $e) {
+                common_log(LOG_ERR, $e->getMessage());
+                // continue on exceptions
             }
         }
 
@@ -804,7 +821,6 @@ class ApiAction extends Action
 
     function showAtomTimeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null, $logo=null)
     {
-
         $this->initDocument('atom');
 
         $this->element('title', null, $title);
@@ -831,22 +847,23 @@ class ApiAction extends Action
         $this->element('subtitle', null, $subtitle);
 
         if (is_array($notice)) {
-            foreach ($notice as $n) {
-                $this->raw($n->asAtomEntry());
-            }
-        } else {
-            while ($notice->fetch()) {
+            $notice = new ArrayWrapper($notice);
+        }
+
+        while ($notice->fetch()) {
+            try {
                 $this->raw($notice->asAtomEntry());
+            } catch (Exception $e) {
+                common_log(LOG_ERR, $e->getMessage());
+                continue;
             }
         }
 
         $this->endDocument('atom');
-
     }
 
     function showRssGroups($group, $title, $link, $subtitle)
     {
-
         $this->initDocument('rss');
 
         $this->element('title', null, $title);
@@ -994,7 +1011,6 @@ class ApiAction extends Action
 
     function showAtomGroups($group, $title, $id, $link, $subtitle=null, $selfuri=null)
     {
-
         $this->initDocument('atom');
 
         $this->element('title', null, common_xml_safe_str($title));
@@ -1025,20 +1041,21 @@ class ApiAction extends Action
 
     function showJsonTimeline($notice)
     {
-
         $this->initDocument('json');
 
         $statuses = array();
 
         if (is_array($notice)) {
-            foreach ($notice as $n) {
-                $twitter_status = $this->twitterStatusArray($n);
-                array_push($statuses, $twitter_status);
-            }
-        } else {
-            while ($notice->fetch()) {
+            $notice = new ArrayWrapper($notice);
+        }
+
+        while ($notice->fetch()) {
+            try {
                 $twitter_status = $this->twitterStatusArray($notice);
                 array_push($statuses, $twitter_status);
+            } catch (Exception $e) {
+                common_log(LOG_ERR, $e->getMessage());
+                continue;
             }
         }
 
@@ -1049,7 +1066,6 @@ class ApiAction extends Action
 
     function showJsonGroups($group)
     {
-
         $this->initDocument('json');
 
         $groups = array();
@@ -1095,7 +1111,6 @@ class ApiAction extends Action
 
     function showTwitterXmlUsers($user)
     {
-
         $this->initDocument('xml');
         $this->elementStart('users', array('type' => 'array',
                                            'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
@@ -1118,7 +1133,6 @@ class ApiAction extends Action
 
     function showJsonUsers($user)
     {
-
         $this->initDocument('json');
 
         $users = array();
@@ -1175,9 +1189,8 @@ class ApiAction extends Action
             header('Content-Type: application/json; charset=utf-8');
 
             // Check for JSONP callback
-            $callback = $this->arg('callback');
-            if ($callback) {
-                print $callback . '(';
+            if (isset($this->callback)) {
+                print $this->callback . '(';
             }
             break;
         case 'rss':
@@ -1204,10 +1217,8 @@ class ApiAction extends Action
             $this->endXML();
             break;
         case 'json':
-
             // Check for JSONP callback
-            $callback = $this->arg('callback');
-            if ($callback) {
+            if (isset($this->callback)) {
                 print ')';
             }
             break;
@@ -1225,9 +1236,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__);
 
@@ -1237,30 +1251,42 @@ class ApiAction extends Action
 
         $status_string = ClientErrorAction::$status[$code];
 
-        header('HTTP/1.1 '.$code.' '.$status_string);
+        // Do not emit error header for JSONP
+        if (!isset($this->callback)) {
+            header('HTTP/1.1 ' . $code . ' ' . $status_string);
+        }
 
-        if ($format == 'xml') {
+        switch($format) {
+        case 'xml':
             $this->initDocument('xml');
             $this->elementStart('hash');
             $this->element('error', null, $msg);
             $this->element('request', null, $_SERVER['REQUEST_URI']);
             $this->elementEnd('hash');
             $this->endDocument('xml');
-        } elseif ($format == 'json'){
+            break;
+        case 'json':
             $this->initDocument('json');
             $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
             print(json_encode($error_array));
             $this->endDocument('json');
-        } else {
-
+            break;
+        case 'text':
+            header('Content-Type: text/plain; charset=utf-8');
+            print $msg;
+            break;
+        default:
             // If user didn't request a useful format, throw a regular client error
             throw new ClientException($msg, $code);
         }
     }
 
-    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__);
 
@@ -1270,7 +1296,10 @@ class ApiAction extends Action
 
         $status_string = ServerErrorAction::$status[$code];
 
-        header('HTTP/1.1 '.$code.' '.$status_string);
+        // Do not emit error header for JSONP
+        if (!isset($this->callback)) {
+            header('HTTP/1.1 '.$code.' '.$status_string);
+        }
 
         if ($content_type == 'xml') {
             $this->initDocument('xml');
@@ -1342,12 +1371,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'));
@@ -1355,7 +1388,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')) {
@@ -1366,7 +1399,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);
@@ -1374,45 +1407,59 @@ class ApiAction extends Action
         }
     }
 
-    function getTargetGroup($id)
+    function getTargetProfile($id)
     {
         if (empty($id)) {
-            if (is_numeric($this->arg('id'))) {
-                return User_group::staticGet($this->arg('id'));
+
+            // Twitter supports these other ways of passing the user 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.
                 $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);
-                }
-            } else if ($this->arg('group_id')) {
+                $user = User::staticGet('nickname', $nickname);
+                return $user ? $user->getProfile() : null;
+            } 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('group_id'))) {
+                if (self::is_decimal($this->arg('user_id'))) {
+                    return Profile::staticGet('id', $this->arg('user_id'));
+                }
+            } else if ($this->arg('screen_name')) {
+                $nickname = common_canonical_nickname($this->arg('screen_name'));
+                $user = User::staticGet('nickname', $nickname);
+                return $user ? $user->getProfile() : null;
+            }
+        } else if (self::is_decimal($id)) {
+            return Profile::staticGet($id);
+        } else {
+            $nickname = common_canonical_nickname($id);
+            $user = User::staticGet('nickname', $nickname);
+            return $user ? $user->getProfile() : null;
+        }
+    }
+
+    function getTargetGroup($id)
+    {
+        if (empty($id)) {
+            if (self::is_decimal($this->arg('id'))) {
+                return User_group::staticGet('id', $this->arg('id'));
+            } else if ($this->arg('id')) {
+                return User_group::getForNickname($this->arg('id'));
+            } else if ($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;
-            } else {
-                return User_group::staticGet('id', $local->group_id);
-            }
+            return User_group::getForNickname($id);
         }
     }
 
@@ -1428,7 +1475,6 @@ class ApiAction extends Action
      */
     function arg($key, $def=null)
     {
-
         // XXX: Do even more input validation/scrubbing?
 
         if (array_key_exists($key, $this->args)) {
@@ -1495,5 +1541,4 @@ class ApiAction extends Action
 
         return $uri;
     }
-
 }