]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Twitter-compatible API - checks for is_readonly() now work!
authorzach <zach@controlyourself.ca>
Fri, 24 Oct 2008 21:37:45 +0000 (17:37 -0400)
committerzach <zach@controlyourself.ca>
Fri, 24 Oct 2008 21:37:45 +0000 (17:37 -0400)
darcs-hash:20081024213745-462f3-7c26611e7f75265affafd471c3d66e02ec7e686e.gz

actions/api.php
actions/twitapiaccount.php
actions/twitapidirect_messages.php
actions/twitapifavorites.php
actions/twitapifriendships.php
actions/twitapihelp.php
actions/twitapistatuses.php
actions/twitapiusers.php

index ea4553543e144f3eaca055417db303436e412cc2..4cdda5ff644ed016120fc62a19c062eb3332c48b 100644 (file)
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
  * GNU Affero General Public License for more details.
  *
  * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * along with this program.     If not, see <http://www.gnu.org/licenses/>.
  */
 
 if (!defined('LACONICA')) { exit(1); }
@@ -40,7 +40,7 @@ class ApiAction extends Action {
                        $this->api_method = $method;
                        $this->content_type = strtolower($cmdext[1]);
                } else {
-                       
+
                        # Requested format / content-type will be an extension on the method
                        $cmdext = explode('.', $method);
                        $this->api_method = $cmdext[0];
@@ -72,13 +72,13 @@ class ApiAction extends Action {
 
                        # Caller might give us a username even if not required
                        if (isset($_SERVER['PHP_AUTH_USER'])) {
-                               $user = User::staticGet('nickname', $_SERVER['PHP_AUTH_USER']); 
+                               $user = User::staticGet('nickname', $_SERVER['PHP_AUTH_USER']);
                                if ($user) {
                                        $this->user = $user;
                                }
                                # Twitter doesn't throw an error if the user isn't found
                        }
-                       
+
                        $this->process_command();
                }
        }
@@ -109,7 +109,7 @@ class ApiAction extends Action {
 
        # Whitelist of API methods that don't need authentication
        function requires_auth() {
-               static $noauth = array( 'statuses/public_timeline',
+               static $noauth = array( 'statuses/public_timeline',
                                                                'statuses/show',
                                                                'users/show',
                                                                'help/test',
@@ -138,10 +138,10 @@ class ApiAction extends Action {
                }
        }
 
-       function show_basic_auth_error() {      
-               header('HTTP/1.1 401 Unauthorized');
-               $msg = 'Could not authenticate you.';
-       
+       function show_basic_auth_error() {
+               header('HTTP/1.1 401 Unauthorized');
+               $msg = 'Could not authenticate you.';
+
                if ($this->content_type == 'xml') {
                        header('Content-Type: application/xml; charset=utf-8');
                        common_start_xml();
@@ -151,7 +151,7 @@ class ApiAction extends Action {
                        common_element_end('hash');
                        common_end_xml();
                } else if ($this->content_type == 'json')  {
-                       header('Content-Type: application/json; charset=utf-8');                        
+                       header('Content-Type: application/json; charset=utf-8');
                        $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
                        print(json_encode($error_array));
                } else {
@@ -165,20 +165,27 @@ class ApiAction extends Action {
                $apiaction = $_REQUEST['apiaction'];
                $method = $_REQUEST['method'];
                list($cmdtext, $fmt) = explode('.', $method);
-               
-               # FIXME: probably need a table here, instead of this switch
-               
-               switch ($apiaction) {
-                case 'statuses':
-                       switch ($cmdtext) {
-                        case 'update':
-                        case 'destroy':
-                               return false;
-                        default:
+
+               static $write_methods = array(
+                       'account' => array('update_location', 'update_delivery_device', 'end_session'),
+                       'blocks' => array('create', 'destroy'),
+                       'direct_messages' => array('create', 'destroy'),
+                       'favorites' => array('create', 'destroy'),
+                       'friendships' => array('create', 'destroy'),
+                       'help' => array(),
+                       'notifications' => array('follow', 'leave'),
+                       'statuses' => array('update', 'destroy'),
+                       'users' => array()
+               );
+
+               if (array_key_exists($apiaction, $write_methods)) {
+                       common_debug("checking method");
+                       if (!in_array($cmdtext, $write_methods[$apiaction])) {
                                return true;
                        }
-                default: 
-                       return false;
                }
+
+               return false;
        }
+
 }
index 4ce61f35369fe79564adf0ed4bb52e81d670fec0..bb59005a9594a57547851a99c688d7db155c58c1 100644 (file)
@@ -23,20 +23,6 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
 
 class TwitapiaccountAction extends TwitterapiAction {
 
-       function is_readonly() {
-
-               static $write_methods = array(  'update_location',
-                                                                               'update_delivery_device');
-
-               $cmdtext = explode('.', $this->arg('method'));
-
-               if (in_array($cmdtext[0], $write_methods)) {
-                       return false;
-               }
-
-               return true;
-       }
-
        function verify_credentials($args, $apidata) {
 
                if ($apidata['content-type'] == 'xml') {
index 466e74823868f8749a6eed2fa2aea12760995e92..a31d18a5cc50684f40ced2c250af65221922010d 100644 (file)
@@ -23,20 +23,6 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
 
 class Twitapidirect_messagesAction extends TwitterapiAction {
 
-       function is_readonly() {
-
-               static $write_methods = array(  'direct_messages',
-                                                                               'sent');
-
-               $cmdtext = explode('.', $this->arg('method'));
-
-               if (in_array($cmdtext[0], $write_methods)) {
-                       return false;
-               }
-
-               return true;
-       }
-
        function direct_messages($args, $apidata) {
                parent::handle($args);
                return $this->show_messages($args, $apidata, 'received');
index 2c6c2e1d9e7cf57b2825f0ad1a81a392a1f496c2..2fdd88544336d778abb7207dc74926808bc68eb0 100644 (file)
@@ -23,19 +23,6 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
 
 class TwitapifavoritesAction extends TwitterapiAction {
 
-       function is_readonly() {
-
-               static $write_methods = array('favorites');
-
-               $cmdtext = explode('.', $this->arg('method'));
-
-               if (in_array($cmdtext[0], $write_methods)) {
-                       return false;
-               }
-
-               return true;
-       }
-
        function favorites($args, $apidata) {
                parent::handle($args);
 
index 03dd8f06358f5c0bf95659591b14d6667ff69cc2..e4b49cbe4a92529cc2c470b2779ec68dfa8b96f4 100644 (file)
@@ -23,20 +23,6 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
 
 class TwitapifriendshipsAction extends TwitterapiAction {
 
-       function is_readonly() {
-
-               static $write_methods = array(  'create',
-                                                                               'destroy');
-
-               $cmdtext = explode('.', $this->arg('method'));
-
-               if (in_array($cmdtext[0], $write_methods)) {
-                       return false;
-               }
-
-               return true;
-       }
-
        function create($args, $apidata) {
                parent::handle($args);
 
index 6e9b41f8aea6631a237c196959a134f322142a7c..c5d503e11864cadd33f23dacaa6cafe4dec17302 100644 (file)
@@ -23,10 +23,6 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
 
 class TwitapihelpAction extends TwitterapiAction {
 
-       function is_readonly() {
-               return true;
-       }
-
        /* Returns the string "ok" in the requested format with a 200 OK HTTP status code.
         * URL:http://identi.ca/api/help/test.format
         * Formats: xml, json
index f80db8899d9ce864254107a35a53b06e0319209d..5459db8a012b95c355f65853acd9f7ea0f428575 100644 (file)
@@ -23,20 +23,6 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
 
 class TwitapistatusesAction extends TwitterapiAction {
 
-       function is_readonly() {
-
-               static $write_methods = array(  'update',
-                                                                               'destroy');
-
-               $cmdtext = explode('.', $this->arg('method'));
-
-               if (in_array($cmdtext[0], $write_methods)) {
-                       return false;
-               }
-
-               return true;
-       }
-
        function public_timeline($args, $apidata) {
                parent::handle($args);
 
@@ -242,6 +228,10 @@ class TwitapistatusesAction extends TwitterapiAction {
                        return;
                }
 
+               foreach ($_POST as $p => $v) {
+                       common_debug("_POST: $p = $v");
+               }
+
                $this->auth_user = $apidata['user'];
                $user = $this->auth_user;
                $status = $this->trimmed('status');
index ec66ad1faf292d315d09acfba65ba1b1724e22c5..337ec91d1968d1af26bf93a2fe113dd9d95f36e2 100644 (file)
@@ -23,10 +23,6 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
 
 class TwitapiusersAction extends TwitterapiAction {
 
-       function is_readonly() {
-               return true;
-       }
-
        function show($args, $apidata) {
                parent::handle($args);