/** * Action to handle the ask from another site * * Handled via HTTP Response code * * TODO: Handle Network replies as Exceptions * * @return void */ function askAction() { if ($this->options['acceptFriends'] == 0) { Network::reply(403, HERISSON_EXIT); } $signature = post('signature'); $f = new WpHerissonFriends(); $f->url = post('url'); $f->reloadPublicKey(); if (Encryption::i()->publicDecrypt($signature, $f->public_key) == $f->url) { $f->getInfo(); if ($this->options['acceptFriends'] == 2) { // Friend automatically accepted, so it's a 202 Accepted for further process response Network::reply(202); $f->is_active = 1; } else { // Friend request need to be manually processed, so it's a 200 Ok response Network::reply(200); $f->b_wantsyou = 1; $f->is_active = 0; } $f->save(); } else { Network::reply(417, HERISSON_EXIT); } exit; }
/** * Test validating a friend with the wrong key * * @return void */ public function testValidateFriendWaitingError() { // create a fake request from sample site $f = new WpHerissonFriends(); $e = Encryption::i(); $e->generateKeyPairs(); $f->public_key = $e->public; $f->url = $this->herissonUrl; //$f->setUrl($this->herissonUrl); $f->b_wantsyou = 1; $f->save(); // Check the request is pending $friends = WpHerissonFriendsTable::getWhere('url=? and b_wantsyou=? and is_active=?', array($f->url, 1, 0)); $this->assertEquals(1, sizeof($friends)); $friend = $friends[0]; $friend->validateFriend(); $msgs = Message::i()->getErrors(); $msgs = array_reverse($msgs); $this->assertEquals(1, preg_match("/417/", $msgs[0])); }