]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/twitter.php
move scripts to just before </body>, add event for scripts that need to be in <head>
[quix0rs-gnu-social.git] / lib / twitter.php
index d63384fc9328f52795fc36e1b3ca210b5ee0eb85..676c9b20a2522f37fafbcd0092a7c88dd1e1f778 100644 (file)
@@ -160,6 +160,8 @@ function broadcast_twitter($notice)
             return broadcast_basicauth($notice, $flink);
         }
     }
+
+    return true;
 }
 
 function broadcast_oauth($notice, $flink) {
@@ -173,34 +175,7 @@ function broadcast_oauth($notice, $flink) {
     try {
         $status = $client->statusesUpdate($statustxt);
     } catch (OAuthClientCurlException $e) {
-
-        if ($e->getMessage() == 'The requested URL returned error: 401') {
-
-            $errmsg = sprintf('User %1$s (user id: %2$s) has an invalid ' .
-                              'Twitter OAuth access token.',
-                              $user->nickname, $user->id);
-            common_log(LOG_WARNING, $errmsg);
-
-            // Bad auth token! We need to delete the foreign_link
-            // to Twitter and inform the user.
-
-            remove_twitter_link($flink);
-            return true;
-
-        } else {
-
-            // Some other error happened, so we should probably
-            // try to send again later.
-
-            $errmsg = sprintf('cURL error trying to send notice to Twitter ' .
-                              'for user %1$s (user id: %2$s) - ' .
-                              'code: %3$s message: $4$s.',
-                              $user->nickname, $user->id,
-                              $e->getCode(), $e->getMessage());
-            common_log(LOG_WARNING, $errmsg);
-
-            return false;
-        }
+        return process_error($e, $flink);
     }
 
     if (empty($status)) {
@@ -208,9 +183,9 @@ function broadcast_oauth($notice, $flink) {
         // This could represent a failure posting,
         // or the Twitter API might just be behaving flakey.
 
-        $errmsg = sprintf('No data returned by Twitter API when ' .
-                         'trying to send update for %1$s (user id %2$s).',
-                         $user->nickname, $user->id);
+        $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' .
+                          'trying to send update for %1$s (user id %2$s).',
+                          $user->nickname, $user->id);
         common_log(LOG_WARNING, $errmsg);
 
         return false;
@@ -218,7 +193,7 @@ function broadcast_oauth($notice, $flink) {
 
     // Notice crossed the great divide
 
-    $msg = sprintf('Twitter bridge posted notice %s to Twitter.',
+    $msg = sprintf('Twitter bridge - posted notice %s to Twitter using OAuth.',
                    $notice->id);
     common_log(LOG_INFO, $msg);
 
@@ -228,82 +203,68 @@ function broadcast_oauth($notice, $flink) {
 function broadcast_basicauth($notice, $flink)
 {
     $user = $flink->getUser();
-    $fuser = $flink->getForeignUser();
-    $twitter_user = $fuser->nickname;
-    $twitter_password = $flink->credentials;
-    $uri = 'http://www.twitter.com/statuses/update.json';
-    $statustxt = format_status($notice);
 
-    $options = array(CURLOPT_USERPWD        => "$twitter_user:$twitter_password",
-                     CURLOPT_POST           => true,
-                     CURLOPT_POSTFIELDS     =>
-                     array(
-                           'status' => $statustxt,
-                           'source' => common_config('integration', 'source')
-                           ),
-                     CURLOPT_RETURNTRANSFER => true,
-                     CURLOPT_FAILONERROR    => true,
-                     CURLOPT_HEADER         => false,
-                     CURLOPT_FOLLOWLOCATION => true,
-                     CURLOPT_USERAGENT      => "StatusNet",
-                     CURLOPT_CONNECTTIMEOUT => 120,
-                     CURLOPT_TIMEOUT        => 120,
-
-                     # Twitter is strict about accepting invalid "Expect" headers
-                     CURLOPT_HTTPHEADER => array('Expect:'));
-
-    $ch = curl_init($uri);
-    curl_setopt_array($ch, $options);
-    $data = curl_exec($ch);
-    $errmsg = curl_error($ch);
-
-    if ($errmsg == 'The requested URL returned error: 401') {
-
-        $errmsg = sprintf('User %1$s (user id: %2$s) has an invalid ' .
-                          'Twitter basic auth username/password.',
-                          $user->nickname, $user->id);
-        common_log(LOG_WARNING, $errmsg);
-
-        // Bad credentials. We need to delete the foreign_link
-        // to Twitter and inform the user.
-
-        remove_twitter_link($flink);
-        return true;
-
-    } elseif (!empty($errmsg)) {
-
-        $code = curl_errno($ch);
-
-        $msg = "cURL error: $code, '$errmsg' - trying to send notice $notice->id " .
-          "to Twitter using basic auth.";
+    $statustxt = format_status($notice);
 
-        common_log(LOG_WARNING, $msg);
+    $client = new TwitterBasicAuthClient($flink);
+    $status = null;
 
-        return false;
+    try {
+        $status = $client->statusesUpdate($statustxt);
+    } catch (BasicAuthCurlException $e) {
+        return process_error($e, $flink);
     }
 
-    curl_close($ch);
-
-    $status = json_decode($data);
-
     if (empty($status)) {
 
-        $errmsg = sprintf('No data returned by Twitter API when ' .
-                          'trying to send update for %1$s (user id %2$s) ' .
-                          'using basic auth.',
+        $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' .
+                          'trying to send update for %1$s (user id %2$s).',
                           $user->nickname, $user->id);
         common_log(LOG_WARNING, $errmsg);
 
         return false;
     }
 
-    $msg = sprintf('Twitter bridge posted notice %s to Twitter using basic auth.',
+    $msg = sprintf('Twitter bridge posted notice %s to Twitter using basic auth.',
                    $notice->id);
     common_log(LOG_INFO, $msg);
 
     return true;
 }
 
+function process_error($e, $flink)
+{
+    $user        = $flink->getUser();
+    $errmsg      = $e->getMessage();
+    $delivered   = false;
+
+    switch($errmsg) {
+     case 'The requested URL returned error: 401':
+        $logmsg = sprintf('Twiter bridge - User %1$s (user id: %2$s) has an invalid ' .
+                          'Twitter screen_name/password combo or an invalid acesss token.',
+                          $user->nickname, $user->id);
+        $delivered = true;
+        remove_twitter_link($flink);
+        break;
+     case 'The requested URL returned error: 403':
+        $logmsg = sprintf('Twitter bridge - User %1$s (user id: %2$s) has exceeded ' .
+                          'his/her Twitter request limit.',
+                          $user->nickname, $user->id);
+        break;
+     default:
+        $logmsg = sprintf('Twitter bridge - cURL error trying to send notice to Twitter ' .
+                          'for user %1$s (user id: %2$s) - ' .
+                          'code: %3$s message: %4$s.',
+                          $user->nickname, $user->id,
+                          $e->getCode(), $e->getMessage());
+        break;
+    }
+
+    common_log(LOG_WARNING, $logmsg);
+
+    return $delivered;
+}
+
 function format_status($notice)
 {
     // XXX: Hack to get around PHP cURL's use of @ being a a meta character