1. Alexander Shabunevich
  2. pocketbox

Commits

Alexander Shabunevich  committed 6608146

Fix login issue, add checkForm token for Pocket API.

  • Participants
  • Parent commits e3c7153
  • Branches default

Comments (0)

Files changed (1)

File app/pocket.php

View file
  • Ignore whitespace
 class PocketAPI {
 
     public $url = 'https://readitlaterlist.com/v2/';
-    public $login_page = 'http://getpocket.com/l/';
+    public $login_page = 'http://getpocket.com/login';
     public $login_action = 'http://getpocket.com/login_process/';
 
     // get ur key from http://readitlaterlist.com/api/signup/
 
     private $settings = array();
     private $cookies = 'app/cookies.txt';
+    private $formCheck = '';
 
     public function PocketAPI($username, $password, $settings) {
       $this->settings = $settings;
       }
 
       // try to get session cookies
-      if (!$this->checkCookies()) {
+      if (!$this->checkCookies() || !$this->grabFormCheckToken()) {
         throw new Exception("Error while grabbing session cookies.");
       }
       return true;
       return true;
     }
 
+    /**
+     * Get formCheck token from page after authorization.
+     */
+    private function grabFormCheckToken() {
+      $response = $this->serverCall('a/queue/', null, 'http://getpocket.com/', true, false, false);
+      preg_match("/formCheck = '([a-z0-9]{32})'/", $response, $matches);
+      if (empty($matches[1])) {
+        return false;
+      }
+
+      $this->formCheck = $matches[1];
+      return true;
+    }
+
     public function register() {
       $data = "username=".$this->username.
                "&password=".$this->password.
     public function getSingleArticle($pid) {
       $data = array(
         'itemId' => $pid,
+        'formCheck' => $this->formCheck,
       );
       $response = $this->serverCall('a/x/getArticle.php', $data, 'http://getpocket.com/', true, true);
       $ret = json_decode($response);
       return $ret;
     }
 
-    private function serverCall($method, $data, $url = NULL, $emulate = false, $ajax = false) {
+    private function serverCall($method, $data, $url = NULL, $emulate = false, $ajax = false, $post = true) {
       try {
         $link = $url ? $url . $method : $this->url . $method;
 
         curl_setopt($ch, CURLOPT_FAILONERROR, true);
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
-        curl_setopt($ch, CURLOPT_POST, true);
-        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+        if ($post) {
+          curl_setopt($ch, CURLOPT_POST, true);
+          curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+        }
         curl_setopt($ch, CURLOPT_HEADER, false);
         if ($emulate) {
           curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent: Opera/9.80 (Macintosh; Intel Mac OS X 10.7.4; U; en) Presto/2.10.289 Version/12.00");