Example #1
0
 /**
  * Action to handle validation of a pending request for friendship.
  *
  * Handled via HTTP Response code
  *
  * @return void
  */
 function validateAction()
 {
     $signature = post('signature');
     $url = post('url');
     $f = WpHerissonFriendsTable::getOneWhere("url=? AND b_youwant=1", array($url));
     try {
         if (Encryption::i()->publicDecrypt($signature, $f->public_key) == $url) {
             $f->b_youwant = 0;
             $f->is_active = 1;
             $f->save();
             Network::reply(200);
             echo "1";
             exit;
         } else {
             Network::reply(417, HERISSON_EXIT);
         }
     } catch (Encryption\Exception $e) {
         Network::reply(417, HERISSON_EXIT);
     }
 }
 /**
  * Test inserting a new friend that need validation, and try to validate it with the wrong private key for cipher
  *
  * @return void
  */
 public function testValidateFriendFrontErrorEncoding()
 {
     // create a fake request from sample site
     $f = new WpHerissonFriends();
     $e = Encryption::i();
     $e->generateKeyPairs();
     $f->public_key = $e->public;
     $f->url = $this->sampleUrl;
     $f->b_youwant = 1;
     $f->save();
     // Change the key pairs, to create a cipher error
     $e->generateKeyPairs();
     // Check the request is pending
     $friends = WpHerissonFriendsTable::getWhere('url=? and b_youwant=? and is_active=?', array($f->url, 1, 0));
     $this->assertEquals(1, sizeof($friends));
     // encrypt sample url, with sample private key
     $network = new Network();
     $signature = Encryption::i()->privateEncrypt($f->url, $e->private);
     $postData = array('url' => $f->url, 'signature' => $signature);
     // request our installation to validate sample site
     try {
         $content = $network->download(HERISSON_LOCAL_URL . "/validate", $postData);
     } catch (Network\Exception $e) {
         $this->assertEquals(417, $e->getCode());
     }
     // check it's not pending anymore
     $friends = WpHerissonFriendsTable::getWhere('url=? and b_youwant=? and is_active=?', array($f->url, 1, 0));
     $this->assertEquals(1, sizeof($friends));
     // Delete it and check it's not here anymore
     foreach ($friends as $f) {
         $f->delete();
     }
     $friends = WpHerissonFriendsTable::getWhere('url=? and b_youwant=?', array($f->url, 1));
     $this->assertEquals(0, sizeof($friends));
 }
 /**
  * Download backup data from this friend
  *
  * Do network hit to the friend's url
  * We decipher our bookmarks data with our private key, because only we can read our bookmarks
  *
  * @return true if backup was succesful, false otherwise
  */
 public function downloadBackup()
 {
     $signature = Encryption::i()->privateEncrypt(HERISSON_LOCAL_URL);
     $postData = array('url' => HERISSON_LOCAL_URL, 'signature' => $signature);
     $network = new Network();
     try {
         $content = $network->download($this->url . "/downloadbackup", $postData);
         // FIXME We should not have to use stripslashes here !!
         $encryptionData = unserialize(stripslashes($content['data']));
         $data = Encryption::i()->privateDecryptLongData($encryptionData['data'], $encryptionData['hash'], $encryptionData['iv']);
         return $data;
     } catch (Network\Exception $e) {
         switch ($e->getCode()) {
             case 417:
                 return 0;
                 break;
         }
         return $e->getCode();
     }
 }
 /**
  * Get HTML content from URL
  *
  * Retrieve content via Curl simple HTTP request
  * Do nothing if the bookmark has $this->error=1
  *
  * @param boolean $verbose flag to set mode verbose (default true)
  *
  * @return true if the content is retrieve succesfully
  */
 public function getContentFromUrl($verbose = true)
 {
     $options = get_option('HerissonOptions');
     if (!$options['spiderOptionTextOnly']) {
         return false;
     }
     if ($this->error) {
         return false;
     }
     if (!$this->content) {
         $network = new Network();
         try {
             $content = $network->download($this->url);
             $this->_set('content_type', $content['type']);
             if (preg_match('#^text#', $content['type'])) {
                 $this->_set('content', $content['data']);
                 if ($verbose) {
                     Message::i()->addSucces(__("Setting content from URL", HERISSON_TD));
                 }
             } else {
                 $this->saveBinary($content);
             }
             return true;
             //$this->save();
         } catch (Network\Exception $e) {
             Message::i()->addError($e->getMessage());
             return false;
         }
     }
     return false;
 }