]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/twitterimport.php
More info for a proper, fancy-url lighttpd setup
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitterimport.php
index 0eacfcd62c57cf10dc05350dc0a52ff88347d726..cdfb8f8316b86686ee20b9be69cb02151c46d1c3 100644 (file)
@@ -90,7 +90,7 @@ class TwitterImport
         $statusUri = $this->makeStatusURI($status->user->screen_name, $statusId);
 
         // check to see if we've already imported the status
-        $n2s = Notice_to_status::staticGet('status_id', $statusId);
+        $n2s = Notice_to_status::getKV('status_id', $statusId);
 
         if (!empty($n2s)) {
             common_log(
@@ -98,7 +98,7 @@ class TwitterImport
                 $this->name() .
                 " - Ignoring duplicate import: {$statusId}"
             );
-            return Notice::staticGet('id', $n2s->notice_id);
+            return Notice::getKV('id', $n2s->notice_id);
         }
 
         // If it's a retweet, save it as a repeat!
@@ -149,11 +149,11 @@ class TwitterImport
         $replyTo = twitter_id($status, 'in_reply_to_status_id');
         if (!empty($replyTo)) {
             common_log(LOG_INFO, "Status {$statusId} is a reply to status {$replyTo}");
-            $n2s = Notice_to_status::staticGet('status_id', $replyTo);
+            $n2s = Notice_to_status::getKV('status_id', $replyTo);
             if (empty($n2s)) {
                 common_log(LOG_INFO, "Couldn't find local notice for status {$replyTo}");
             } else {
-                $reply = Notice::staticGet('id', $n2s->notice_id);
+                $reply = Notice::getKV('id', $n2s->notice_id);
                 if (empty($reply)) {
                     common_log(LOG_INFO, "Couldn't find local notice for status {$replyTo}");
                 } else {
@@ -172,8 +172,8 @@ class TwitterImport
 
         $notice->is_local   = Notice::GATEWAY;
 
-        $notice->content  = html_entity_decode($status->text, ENT_QUOTES, 'UTF-8');
-        $notice->rendered = $this->linkify($status);
+        $notice->content  = html_entity_decode($this->linkify($status, FALSE), ENT_QUOTES, 'UTF-8');
+        $notice->rendered = $this->linkify($status, TRUE);
 
         if (Event::handle('StartNoticeSave', array(&$notice))) {
 
@@ -215,7 +215,7 @@ class TwitterImport
 
 
     /**
-     * Look up a Profile by profileurl field.  Profile::staticGet() was
+     * Look up a Profile by profileurl field.  Profile::getKV() was
      * not working consistently.
      *
      * @param string $nickname   local nickname of the Twitter user
@@ -305,7 +305,7 @@ class TwitterImport
 
             // check for remote profile
 
-            $remote_pro = Remote_profile::staticGet('uri', $profileurl);
+            $remote_pro = Remote_profile::getKV('uri', $profileurl);
 
             if (empty($remote_pro)) {
                 $remote_pro = new Remote_profile();
@@ -339,10 +339,7 @@ class TwitterImport
     {
         global $config;
 
-        $path_parts = pathinfo($twitter_user->profile_image_url);
-
-        $newname = 'Twitter_' . $twitter_user->id . '_' .
-            $path_parts['basename'];
+        $newname = 'Twitter_' . $twitter_user->id . '_' . basename($twitter_user->profile_image_url);
 
         $oldname = $profile->getAvatar(48)->filename;
 
@@ -370,15 +367,15 @@ class TwitterImport
 
         $path_parts = pathinfo($twitter_user->profile_image_url);
 
-        $img_root = substr($path_parts['basename'], 0, -11);
-        $ext = $path_parts['extension'];
-        $mediatype = $this->getMediatype($ext);
+        $ext = (isset($path_parts['extension']) ? '.'.$path_parts['extension'] : '');  // some lack extension
+        $img_root = basename($path_parts['basename'], '_normal'.$ext); // cut off extension
+        $mediatype = $this->getMediatype(substr($ext, 1));
 
         foreach (array('mini', 'normal', 'bigger') as $size) {
             $url = $path_parts['dirname'] . '/' .
-                $img_root . '_' . $size . ".$ext";
+                $img_root . '_' . $size . $ext;
             $filename = 'Twitter_' . $twitter_user->id . '_' .
-                $img_root . "_$size.$ext";
+                $img_root . '_' . $size . $ext;
 
             $this->updateAvatar($profile->id, $size, $mediatype, $filename);
             $this->fetchAvatar($url, $filename);
@@ -401,8 +398,9 @@ class TwitterImport
         $mediatype = null;
 
         switch (strtolower($ext)) {
+        case 'jpeg':
         case 'jpg':
-            $mediatype = 'image/jpg';
+            $mediatype = 'image/jpeg';
             break;
         case 'gif':
             $mediatype = 'image/gif';
@@ -419,16 +417,15 @@ class TwitterImport
         global $config;
 
         $path_parts = pathinfo($user->profile_image_url);
-        $ext = $path_parts['extension'];
-        $end = strlen('_normal' . $ext);
-        $img_root = substr($path_parts['basename'], 0, -($end+1));
-        $mediatype = $this->getMediatype($ext);
+        $ext = (isset($path_parts['extension']) ? '.'.$path_parts['extension'] : '');
+        $img_root = basename($path_parts['basename'], '_normal'.$ext);
+        $mediatype = $this->getMediatype(substr($ext, 1));
 
         foreach (array('mini', 'normal', 'bigger') as $size) {
             $url = $path_parts['dirname'] . '/' .
-                $img_root . '_' . $size . ".$ext";
+                $img_root . '_' . $size . $ext;
             $filename = 'Twitter_' . $user->id . '_' .
-                $img_root . "_$size.$ext";
+                $img_root . '_' . $size . $ext;
 
             if ($this->fetchAvatar($url, $filename)) {
                 $this->newAvatar($id, $size, $mediatype, $filename);
@@ -443,7 +440,7 @@ class TwitterImport
 
         common_debug($this->name() . " - Updating avatar: $size");
 
-        $profile = Profile::staticGet($profile_id);
+        $profile = Profile::getKV($profile_id);
 
         if (empty($profile)) {
             common_debug($this->name() . " - Couldn't get profile: $profile_id!");
@@ -540,7 +537,7 @@ class TwitterImport
     const HASHTAG = 2;
     const MENTION = 3;
 
-    function linkify($status)
+    function linkify($status, $html = FALSE)
     {
         $text = $status->text;
 
@@ -596,13 +593,21 @@ class TwitterImport
             $orig = $this->twitEscape(mb_substr($text, $start, $end - $start));
             switch($type) {
             case self::URL:
-                $linkText = $this->makeUrlLink($object, $orig);
+                $linkText = $this->makeUrlLink($object, $orig, $html);
                 break;
             case self::HASHTAG:
-                $linkText = $this->makeHashtagLink($object, $orig);
+                if ($html) {
+                    $linkText = $this->makeHashtagLink($object, $orig);
+                }else{
+                    $linkText = $orig;
+                }
                 break;
             case self::MENTION:
-                $linkText = $this->makeMentionLink($object, $orig);
+                if ($html) {
+                    $linkText = $this->makeMentionLink($object, $orig);
+                }else{
+                    $linkText = $orig;
+                }
                 break;
             default:
                 $linkText = $orig;
@@ -630,9 +635,13 @@ class TwitterImport
         return htmlspecialchars(html_entity_decode($str, ENT_COMPAT, 'UTF-8'));
     }
 
-    function makeUrlLink($object, $orig)
+    function makeUrlLink($object, $orig, $html)
     {
-        return "<a href='{$object->url}' class='extlink'>{$orig}</a>";
+        if ($html) {
+            return '<a href="'.htmlspecialchars($object->expanded_url).'" class="extlink">'.htmlspecialchars($object->display_url).'</a>';
+        }else{
+            return htmlspecialchars($object->expanded_url);
+        }
     }
 
     function makeHashtagLink($object, $orig)
@@ -670,7 +679,7 @@ class TwitterImport
         foreach ($status->entities->user_mentions as $mention) {
             $flink = Foreign_link::getByForeignID($mention->id, TWITTER_SERVICE);
             if (!empty($flink)) {
-                $user = User::staticGet('id', $flink->user_id);
+                $user = User::getKV('id', $flink->user_id);
                 if (!empty($user)) {
                     $reply = new Reply();
                     $reply->notice_id  = $notice->id;
@@ -700,4 +709,4 @@ class TwitterImport
             }
         }
     }
-}
\ No newline at end of file
+}