]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Merge branch 'testing'
authorEvan Prodromou <evan@status.net>
Wed, 23 Dec 2009 00:44:19 +0000 (16:44 -0800)
committerEvan Prodromou <evan@status.net>
Wed, 23 Dec 2009 00:44:19 +0000 (16:44 -0800)
1  2 
classes/Memcached_DataObject.php
lib/util.php
plugins/TwitterBridge/twitter.php

index 644b84d5cf64317dfea648edfa09d4dd6dd86578,70e9e351d5e8855f814d168b2505091f8b427bf1..d8b0db5a69b7e1363a5487e6272050175d57f97d
@@@ -23,6 -23,20 +23,20 @@@ require_once INSTALLDIR.'/classes/Memca
  
  class Memcached_DataObject extends DB_DataObject
  {
+     /**
+      * Destructor to free global memory resources associated with
+      * this data object when it's unset or goes out of scope.
+      * DB_DataObject doesn't do this yet by itself.
+      */
+     function __destruct()
+     {
+         $this->free();
+         if (method_exists('DB_DataObject', '__destruct')) {
+             parent::__destruct();
+         }
+     }
      function &staticGet($cls, $k, $v=null)
      {
          if (is_null($v)) {
      }
  
      static function cacheKey($cls, $k, $v) {
 +        if (is_object($cls) || is_object($j) || is_object($v)) {
 +            $e = new Exception();
 +            common_log(LOG_ERR, __METHOD__ . ' object in param: ' .
 +                str_replace("\n", " ", $e->getTraceAsString()));
 +        }
          return common_cache_key(strtolower($cls).':'.$k.':'.$v);
      }
  
diff --combined lib/util.php
index 5d20ed82df8ba0395d418305368edabb61f8b005,af4885f40f779bb8fa8ee1cc5080dc744f85681f..ed81aeba16ceffde2a25c84c1229f7c26dca7358
@@@ -91,16 -91,8 +91,16 @@@ function common_language(
      if (_have_config() && common_logged_in()) {
          $user = common_current_user();
          $user_language = $user->language;
 -        if ($user_language)
 -          return $user_language;
 +
 +        if ($user->language) {
 +            // Validate -- we don't want to end up with a bogus code
 +            // left over from some old junk.
 +            foreach (common_config('site', 'languages') as $code => $info) {
 +                if ($info['lang'] == $user_language) {
 +                    return $user_language;
 +                }
 +            }
 +        }
      }
  
      // Otherwise, find the best match for the languages requested by the
@@@ -135,7 -127,7 +135,7 @@@ function common_check_user($nickname, $
                  if (0 == strcmp(common_munge_password($password, $user->id),
                                  $user->password)) {
                      //internal checking passed
-                     $authenticatedUser =& $user;
+                     $authenticatedUser = $user;
                  }
              }
          }
@@@ -531,19 -523,23 +531,23 @@@ function callback_helper($matches, $cal
      return substr($matches[0],0,$left) . $result . substr($matches[0],$right);
  }
  
- function curry($fn) {
-     //TODO switch to a PHP 5.3 function closure based approach if PHP 5.3 is used
-     $args = func_get_args();
-     array_shift($args);
-     $id = uniqid('_partial');
-     $GLOBALS[$id] = array($fn, $args);
-     return create_function('',
-                            '$args = func_get_args(); '.
-                            'return call_user_func_array('.
-                            '$GLOBALS["'.$id.'"][0],'.
-                            'array_merge('.
-                            '$args,'.
-                            '$GLOBALS["'.$id.'"][1]));');
+ if (version_compare(PHP_VERSION, '5.3.0', 'ge')) {
+     // lambda implementation in a separate file; PHP 5.2 won't parse it.
+     require_once INSTALLDIR . "/lib/curry.php";
+ } else {
+     function curry($fn) {
+         $args = func_get_args();
+         array_shift($args);
+         $id = uniqid('_partial');
+         $GLOBALS[$id] = array($fn, $args);
+         return create_function('',
+                                '$args = func_get_args(); '.
+                                'return call_user_func_array('.
+                                '$GLOBALS["'.$id.'"][0],'.
+                                'array_merge('.
+                                '$args,'.
+                                '$GLOBALS["'.$id.'"][1]));');
+     }
  }
  
  function common_linkify($url) {
@@@ -1078,18 -1074,21 +1082,21 @@@ function common_request_id(
  
  function common_log($priority, $msg, $filename=null)
  {
-     $msg = '[' . common_request_id() . '] ' . $msg;
-     $logfile = common_config('site', 'logfile');
-     if ($logfile) {
-         $log = fopen($logfile, "a");
-         if ($log) {
-             $output = common_log_line($priority, $msg);
-             fwrite($log, $output);
-             fclose($log);
+     if(Event::handle('StartLog', array(&$priority, &$msg, &$filename))){
+         $msg = '[' . common_request_id() . '] ' . $msg;
+         $logfile = common_config('site', 'logfile');
+         if ($logfile) {
+             $log = fopen($logfile, "a");
+             if ($log) {
+                 $output = common_log_line($priority, $msg);
+                 fwrite($log, $output);
+                 fclose($log);
+             }
+         } else {
+             common_ensure_syslog();
+             syslog($priority, $msg);
          }
-     } else {
-         common_ensure_syslog();
-         syslog($priority, $msg);
+         Event::handle('EndLog', array($priority, $msg, $filename));
      }
  }
  
@@@ -1245,8 -1244,12 +1252,12 @@@ function common_copy_args($from
      return $to;
  }
  
- // Neutralise the evil effects of magic_quotes_gpc in the current request.
- // This is used before handing a request off to OAuthRequest::from_request.
+ /**
+  * Neutralise the evil effects of magic_quotes_gpc in the current request.
+  * This is used before handing a request off to OAuthRequest::from_request.
+  * @fixme Doesn't consider vars other than _POST and _GET?
+  * @fixme Can't be undone and could corrupt data if run twice.
+  */
  function common_remove_magic_from_request()
  {
      if(get_magic_quotes_gpc()) {
@@@ -1448,6 -1451,17 +1459,17 @@@ function common_database_tablename($tab
    return $tablename;
  }
  
+ /**
+  * Shorten a URL with the current user's configured shortening service,
+  * or ur1.ca if configured, or not at all if no shortening is set up.
+  * Length is not considered.
+  *
+  * @param string $long_url
+  * @return string may return the original URL if shortening failed
+  *
+  * @fixme provide a way to specify a particular shortener
+  * @fixme provide a way to specify to use a given user's shortening preferences
+  */
  function common_shorten_url($long_url)
  {
      $user = common_current_user();
      }
  }
  
+ /**
+  * @return mixed array($proxy, $ip) for web requests; proxy may be null
+  *               null if not a web request
+  *
+  * @fixme X-Forwarded-For can be chained by multiple proxies;
+           we should parse the list and provide a cleaner array
+  * @fixme X-Forwarded-For can be forged by clients; only use them if trusted
+  * @fixme X_Forwarded_For headers will override X-Forwarded-For read through $_SERVER;
+  *        use function to get exact request headers from Apache if possible.
+  */
  function common_client_ip()
  {
      if (!isset($_SERVER) || !array_key_exists('REQUEST_METHOD', $_SERVER)) {
index 2b9cde1aa895c832274de2bf3f84939ccae43410,003b52682f910bc3f3f65b1a70d07b2434849483..e133ce6f745c1a8c5e9e30ef67d367e91b6f1bbc
@@@ -170,6 -170,8 +170,6 @@@ function broadcast_twitter($notice
  function broadcast_oauth($notice, $flink) {
      $user = $flink->getUser();
      $statustxt = format_status($notice);
 -    // Convert !groups to #hashes
 -    $statustxt = preg_replace('/(^|\s)!([A-Za-z0-9]{1,64})/', "\\1#\\2", $statustxt);
      $token = TwitterOAuthClient::unpackToken($flink->credentials);
      $client = new TwitterOAuthClient($token->key, $token->secret);
      $status = null;
      try {
          $status = $client->statusesUpdate($statustxt);
      } catch (OAuthClientException $e) {
-         return process_error($e, $flink);
+         return process_error($e, $flink, $notice);
      }
  
      if (empty($status)) {
          // or the Twitter API might just be behaving flakey.
  
          $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);
+                           'trying to post notice %d for User %s (user id %d).',
+                           $notice->id,
+                           $user->nickname,
+                           $user->id);
          common_log(LOG_WARNING, $errmsg);
  
          return false;
  
      // Notice crossed the great divide
  
-     $msg = sprintf('Twitter bridge - posted notice %s to Twitter using OAuth.',
-                    $notice->id);
+     $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' .
+                    'OAuth for User %s (user id %d).',
+                    $notice->id,
+                    $user->nickname,
+                    $user->id);
      common_log(LOG_INFO, $msg);
  
      return true;
@@@ -213,73 -222,75 +220,80 @@@ function broadcast_basicauth($notice, $
  
      try {
          $status = $client->statusesUpdate($statustxt);
-     } catch (HTTP_Request2_Exception $e) {
-         return process_error($e, $flink);
+     } catch (BasicAuthException $e) {
+         return process_error($e, $flink, $notice);
      }
  
      if (empty($status)) {
  
          $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);
+                           'trying to post notice %d for %s (user id %d).',
+                           $notice->id,
+                           $user->nickname,
+                           $user->id);
          common_log(LOG_WARNING, $errmsg);
  
-             $errmsg = sprintf('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);
+         $errmsg = sprintf('No data returned by Twitter API when ' .
+                           'trying to post notice %d for %s (user id %d).',
+                           $notice->id,
+                           $user->nickname,
+                           $user->id);
+         common_log(LOG_WARNING, $errmsg);
          return false;
      }
  
-     $msg = sprintf('Twitter bridge - posted notice %s to Twitter using basic auth.',
-                    $notice->id);
+     $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' .
+                    'HTTP basic auth for User %s (user id %d).',
+                    $notice->id,
+                    $user->nickname,
+                    $user->id);
      common_log(LOG_INFO, $msg);
  
      return true;
  }
  
- function process_error($e, $flink)
+ function process_error($e, $flink, $notice)
  {
-     $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;
-     }
+     $user = $flink->getUser();
+     $code = $e->getCode();
+     $logmsg = sprintf('Twitter bridge - %d posting notice %d for ' .
+                       'User %s (user id: %d): %s.',
+                       $code,
+                       $notice->id,
+                       $user->nickname,
+                       $user->id,
+                       $e->getMessage());
  
      common_log(LOG_WARNING, $logmsg);
  
-     return $delivered;
+     if ($code == 401) {
+         // Probably a revoked or otherwise bad access token - nuke!
+         remove_twitter_link($flink);
+         return true;
+     } else {
+         // For every other case, it's probably some flakiness so try
+         // sending the notice again later (requeue).
+         return false;
+     }
  }
  
  function format_status($notice)
  {
      // XXX: Hack to get around PHP cURL's use of @ being a a meta character
 -    return preg_replace('/^@/', ' @', $notice->content);
 +    $statustxt = preg_replace('/^@/', ' @', $notice->content);
 +
 +    // Convert !groups to #hashes
 +    $statustxt = preg_replace('/(^|\s)!([A-Za-z0-9]{1,64})/', "\\1#\\2", $statustxt);
 +
 +    return $statustxt;
  }
  
  function remove_twitter_link($flink)
@@@ -333,11 -344,11 +347,11 @@@ function mail_twitter_bridge_removed($u
  
      $profile = $user->getProfile();
  
-     $subject = sprintf(_('Your Twitter bridge has been disabled.'));
+     $subject = sprintf(_m('Your Twitter bridge has been disabled.'));
  
      $site_name = common_config('site', 'name');
  
-     $body = sprintf(_('Hi, %1$s. We\'re sorry to inform you that your ' .
+     $body = sprintf(_m('Hi, %1$s. We\'re sorry to inform you that your ' .
          'link to Twitter has been disabled. We no longer seem to have ' .
      'permission to update your Twitter status. (Did you revoke ' .
      '%3$s\'s access?)' . "\n\n" .