]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/shownotice.php
Improve ShownoticeAction remote redirect code
[quix0rs-gnu-social.git] / actions / shownotice.php
index 6055184fcdf2ced037e4cf8bde2dee2be15ebad2..b72f39513ed8e11db4c463e5e0ca928bd5b97948 100644 (file)
@@ -70,7 +70,7 @@ class ShownoticeAction extends Action
      *
      * @return success flag
      */
-    function prepare($args)
+    protected function prepare(array $args=array())
     {
         parent::prepare($args);
         if ($this->boolean('ajax')) {
@@ -97,12 +97,15 @@ class ShownoticeAction extends Action
         if (empty($this->profile)) {
             // TRANS: Server error displayed trying to show a notice without a connected profile.
             $this->serverError(_('Notice has no profile.'), 500);
-            return false;
         }
 
-        $this->user = User::staticGet('id', $this->profile->id);
+        $this->user = User::getKV('id', $this->profile->id);
 
-        $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
+        try {
+            $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
+        } catch (Exception $e) {
+            $this->avatar = null;
+        }
 
         return true;
     }
@@ -113,16 +116,16 @@ class ShownoticeAction extends Action
      *
      * @return Notice
      */
-    function getNotice()
+    protected function getNotice()
     {
         $id = $this->arg('notice');
 
-        $notice = Notice::staticGet('id', $id);
+        $notice = Notice::getKV('id', $id);
 
-        if (empty($notice)) {
+        if (!$notice instanceof Notice) {
             // Did we used to have it, and it got deleted?
-            $deleted = Deleted_notice::staticGet($id);
-            if (!empty($deleted)) {
+            $deleted = Deleted_notice::getKV($id);
+            if ($deleted instanceof Deleted_notice) {
                 // TRANS: Client error displayed trying to show a deleted notice.
                 $this->clientError(_('Notice deleted.'), 410);
             } else {
@@ -207,26 +210,21 @@ class ShownoticeAction extends Action
      *
      * @return void
      */
-    function handle($args)
+    protected function handle()
     {
-        parent::handle($args);
+        parent::handle();
 
         if ($this->boolean('ajax')) {
             $this->showAjax();
         } else {
             if ($this->notice->is_local == Notice::REMOTE) {
-                if (!empty($this->notice->url)) {
-                    $target = $this->notice->url;
-                } else if (!empty($this->notice->uri) && preg_match('/^https?:/', $this->notice->uri)) {
-                    // Old OMB posts saved the remote URL only into the URI field.
-                    $target = $this->notice->uri;
-                } else {
-                    // Shouldn't happen.
-                    $target = false;
-                }
-                if ($target && $target != $this->selfUrl()) {
-                    common_redirect($target, 301);
-                    return false;
+                try {
+                    $target = $this->notice->getUrl()
+                    if ($target != $this->selfUrl()) {
+                        common_redirect($target, 301);
+                    }
+                } catch (InvalidUrlException $e) {
+                    common_debug('ShownoticeAction could not redirect to remote notice with id='.$this->notice->id . '. Falling back to showPage().');
                 }
             }
             $this->showPage();
@@ -250,9 +248,7 @@ class ShownoticeAction extends Action
 
     function showAjax()
     {
-        header('Content-Type: text/xml;charset=utf-8');
-        $this->xw->startDocument('1.0', 'UTF-8');
-        $this->elementStart('html');
+        $this->startHTML('text/xml;charset=utf-8');
         $this->elementStart('head');
         // TRANS: Title for page that shows a notice.
         $this->element('title', null, _m('TITLE','Notice'));
@@ -261,7 +257,7 @@ class ShownoticeAction extends Action
         $nli = new NoticeListItem($this->notice, $this);
         $nli->show();
         $this->elementEnd('body');
-        $this->elementEnd('html');
+        $this->endHTML();
     }
 
     /**
@@ -290,7 +286,7 @@ class ShownoticeAction extends Action
      */
     function extraHead()
     {
-        $user = User::staticGet($this->profile->id);
+        $user = User::getKV($this->profile->id);
 
         if (!$user) {
             return;
@@ -319,10 +315,7 @@ class ShownoticeAction extends Action
             'title'=>'oEmbed'),null);
 
         // Extras to aid in sharing notices to Facebook
-        $avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
-        $avatarUrl = ($avatar) ?
-                     $avatar->displayUrl() :
-                     Avatar::defaultImage(AVATAR_PROFILE_SIZE);
+        $avatarUrl = $this->profile->avatarUrl(AVATAR_PROFILE_SIZE);
         $this->element('meta', array('property' => 'og:image',
                                      'content' => $avatarUrl));
         $this->element('meta', array('property' => 'og:description',