]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/oauthstore.php
Fix "$s"s that slipped into double-quoted translatable strings' '%1$s' pattern. Switc...
[quix0rs-gnu-social.git] / lib / oauthstore.php
index 87d8cf2137b197718d93b4d5803502e5c50f867f..1c8e7250092bdb56e644e10fd6f4184eeb3a8f63 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
- * Laconica - a distributed open-source microblogging tool
- * Copyright (C) 2008, 2009, Control Yourself, Inc.
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2008, 2009, StatusNet, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-if (!defined('LACONICA')) {
-    exit(1);
-}
+if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
 
 require_once 'libomb/datastore.php';
 
-class LaconicaDataStore extends OMB_Datastore
+class StatusNetOAuthDataStore extends OAuthDataStore
 {
 
     // We keep a record of who's contacted us
@@ -57,6 +55,17 @@ class LaconicaDataStore extends OMB_Datastore
         }
     }
 
+    function getTokenByKey($token_key)
+    {
+        $t = new Token();
+        $t->tok = $token_key;
+        if ($t->find(true)) {
+            return $t;
+        } else {
+            return null;
+        }
+    }
+
     // http://oauth.net/core/1.0/#nonce
     // "The Consumer SHALL then generate a Nonce value that is unique for
     // all requests with that timestamp."
@@ -67,7 +76,7 @@ class LaconicaDataStore extends OMB_Datastore
     {
         $n = new Nonce();
         $n->consumer_key = $consumer->key;
-        $n->ts = $timestamp;
+        $n->ts = common_sql_date($timestamp);
         $n->nonce = $nonce;
         if ($n->find(true)) {
             return true;
@@ -158,7 +167,6 @@ class LaconicaDataStore extends OMB_Datastore
         return $this->new_access_token($consumer);
     }
 
-
     /**
      * Revoke specified OAuth token
      *
@@ -283,7 +291,7 @@ class LaconicaDataStore extends OMB_Datastore
                 $profile->created = DB_DataObject_Cast::dateTime(); # current time
                 $id = $profile->insert();
                 if (!$id) {
-                    throw new Exception(_('Error inserting new profile'));
+                    throw new Exception(_('Error inserting new profile.'));
                 }
                 $remote->id = $id;
             }
@@ -291,7 +299,7 @@ class LaconicaDataStore extends OMB_Datastore
             $avatar_url = $omb_profile->getAvatarURL();
             if ($avatar_url) {
                 if (!$this->add_avatar($profile, $avatar_url)) {
-                    throw new Exception(_('Error inserting avatar'));
+                    throw new Exception(_('Error inserting avatar.'));
                 }
             } else {
                 $avatar = $profile->getOriginalAvatar();
@@ -306,12 +314,12 @@ class LaconicaDataStore extends OMB_Datastore
 
             if ($exists) {
                 if (!$remote->update($orig_remote)) {
-                    throw new Exception(_('Error updating remote profile'));
+                    throw new Exception(_('Error updating remote profile.'));
                 }
             } else {
                 $remote->created = DB_DataObject_Cast::dateTime(); # current time
                 if (!$remote->insert()) {
-                    throw new Exception(_('Error inserting remote profile'));
+                    throw new Exception(_('Error inserting remote profile.'));
                 }
             }
         }
@@ -320,13 +328,18 @@ class LaconicaDataStore extends OMB_Datastore
     function add_avatar($profile, $url)
     {
         $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
-        copy($url, $temp_filename);
-        $imagefile = new ImageFile($profile->id, $temp_filename);
-        $filename = Avatar::filename($profile->id,
-                                     image_type_to_extension($imagefile->type),
-                                     null,
-                                     common_timestamp());
-        rename($temp_filename, Avatar::path($filename));
+        try {
+            copy($url, $temp_filename);
+            $imagefile = new ImageFile($profile->id, $temp_filename);
+            $filename = Avatar::filename($profile->id,
+                                         image_type_to_extension($imagefile->type),
+                                         null,
+                                         common_timestamp());
+            rename($temp_filename, Avatar::path($filename));
+        } catch (Exception $e) {
+            unlink($temp_filename);
+            throw $e;
+        }
         return $profile->setOriginal($filename);
     }
 
@@ -345,7 +358,8 @@ class LaconicaDataStore extends OMB_Datastore
      **/
     public function saveNotice(&$omb_notice) {
         if (Notice::staticGet('uri', $omb_notice->getIdentifierURI())) {
-            throw new Exception(_('Duplicate notice'));
+            // TRANS: Exception thrown when a notice is denied because it has been sent before.
+            throw new Exception(_('Duplicate notice.'));
         }
         $author_uri = $omb_notice->getAuthor()->getIdentifierURI();
         common_log(LOG_DEBUG, $author_uri, __FILE__);
@@ -354,7 +368,7 @@ class LaconicaDataStore extends OMB_Datastore
             $author = User::staticGet('uri', $author_uri);
         }
         if (!$author) {
-            throw new Exception('No such user');
+            throw new Exception('No such user.');
         }
 
         common_log(LOG_DEBUG, print_r($author, true), __FILE__);
@@ -362,13 +376,9 @@ class LaconicaDataStore extends OMB_Datastore
         $notice = Notice::saveNew($author->id,
                                   $omb_notice->getContent(),
                                   'omb',
-                                  false,
-                                  null,
-                                  $omb_notice->getIdentifierURI());
-        if (is_string($notice)) {
-            throw new Exception($notice);
-        }
-        common_broadcast_notice($notice, true);
+                                  array('is_local' => Notice::REMOTE_OMB,
+                                        'uri' => $omb_notice->getIdentifierURI()));
+
     }
 
     /**
@@ -397,7 +407,7 @@ class LaconicaDataStore extends OMB_Datastore
         $sub->subscribed = $user->id;
 
         if (!$sub->find(true)) {
-            return 0;
+            return array();
         }
 
         /* Since we do not use OMB_Service_Provider’s action methods, there
@@ -412,7 +422,7 @@ class LaconicaDataStore extends OMB_Datastore
             $user = User::staticGet('uri', $uri);
         }
         if (!$user) {
-            throw new Exception('No such user');
+            throw new Exception('No such user.');
         }
         return $user;
     }
@@ -467,6 +477,11 @@ class LaconicaDataStore extends OMB_Datastore
         $subscribed = $this->_getAnyProfile($subscribed_user_uri);
         $subscriber = $this->_getAnyProfile($subscriber_uri);
 
+        if (!$subscriber->hasRight(Right::SUBSCRIBE)) {
+            common_log(LOG_INFO, __METHOD__ . ": remote subscriber banned ($subscriber_uri subbing to $subscribed_user_uri)");
+            return _('You have been banned from subscribing.');
+        }
+
         $sub->subscribed = $subscribed->id;
         $sub->subscriber = $subscriber->id;