/** * add identifier * * @author Matthias Pfefferle * @param User $pUser * @param AuthToken $pAuthToken * @return OnlineIdentity */ public function addIdentifier($pUser, $pCode) { $lAccessToken = $this->getAccessToken($pCode); // get params $lParamsArray = array(); // extract params $lAccessToken = json_decode($lAccessToken, true); $lJsonObject = json_decode(UrlUtils::sendGetRequest("https://api.flattr.com/rest/v2/user", array("Authorization: Bearer " . $lAccessToken["access_token"]))); // facebook identifier $lIdentifier = $lJsonObject->link; // ask for online identity $lOnlineIdentity = OnlineIdentityTable::retrieveByAuthIdentifier($lIdentifier); // check if user already exists if ($lOnlineIdentity) { if ($lOnlineIdentity->getUserId() && $pUser->getId() == $lOnlineIdentity->getUserId()) { if (!$lOnlineIdentity->getActive()) { $lOnlineIdentity->setActive(true); } else { throw new sfException("online identity already added", 1); } } elseif ($lOnlineIdentity->getUserId() && $pUser->getId() != $lOnlineIdentity->getUserId()) { throw new sfException("online identity already added by someone else", 2); } } else { // check online identity $lOnlineIdentity = OnlineIdentityTable::addOnlineIdentity($lJsonObject->link, $lJsonObject->username, $this->aCommunityId, $lIdentifier); } // use api complete informations $this->completeOnlineIdentity($lOnlineIdentity, $lJsonObject, $pUser, $lIdentifier); // signup,add new AuthTokenTable::saveToken($pUser->getId(), $lOnlineIdentity->getId(), $lAccessToken["access_token"], null, true); // signup,add new return $lOnlineIdentity; }
/** * Imports all likes from a facebook account to store it in the mongodb * * @param int $online_identity */ public static function importInterests($online_identity) { $token = AuthTokenTable::getByUserAndOnlineIdentity($online_identity->getUserId(), $online_identity->getId()); if (!$token) { $online_identity->deactivate(); throw new Exception('damn theres no token!', '666'); } // get likes $response = UrlUtils::sendGetRequest("https://graph.facebook.com/me/likes?access_token=" . $token->getTokenKey()); // mongo manager $dm = MongoManager::getDM(); // check likes if ($response && ($objects = json_decode($response, true))) { if (!isset($objects['data'])) { return false; } // iterate and save in mongodb foreach ($objects["data"] as $object) { $interest = $dm->getRepository('Documents\\Interest')->upsert($object); // check interest if ($interest) { $user_interest = $dm->getRepository('Documents\\UserInterest')->upsert($online_identity, $interest); } } } return; }
/** * Enter description here... * * @param string $url */ public static function requestCoupon($url) { $json = UrlUtils::sendGetRequest($url); // check json if ($coupon = json_decode($json, true)) { // check coupon-code if (array_key_exists("coupon", $coupon) && array_key_exists("code", $coupon["coupon"])) { return $coupon["coupon"]["code"]; } } return null; }
/** * returns the klout rank of a user * * @return string */ public function getKloutRank() { if ($this->getCommunity()->getName() == "twitter") { $profile = str_replace("http://twitter.com/", "", $this->getProfileUri()); $result = UrlUtils::sendGetRequest("http://api.klout.com/1/klout.json?key=n7mb4vu5ka7um88remypp2bw&users=" . $profile); if ($kloutobj = json_decode($result, true)) { if (array_key_exists("status", $kloutobj) && $kloutobj["status"] == 200 && array_key_exists("users", $kloutobj)) { return $kloutobj["users"][0]["kscore"]; } } } return "NaN"; }
/** * do a get * * @param string $url * @param array $header http header * @return string get-body */ private static function doGet($url, $header = null) { return UrlUtils::sendGetRequest($url, $header); }
/** * retrieve the access token * * @author Matthias Pfefferle * @param string $pCode */ public function getAccessToken($pCode) { $lConsumer = $this->getConsumer(); $lAccessUrl = "https://graph.facebook.com/oauth/access_token?client_id=" . $lConsumer->key . "&redirect_uri=" . $this->getCallbackUri() . "&client_secret=" . $lConsumer->secret . "&code=" . $pCode; //&grant_type=client_credentials $lAccessToken = UrlUtils::sendGetRequest($lAccessUrl); return $lAccessToken; }