getAvatarUrl(); $userdata['url'] = $mininaute->getUrl(); $userdata['gender'] = intval($mininaute->getSex()); // sinon ça peut retourner "false" si le compte n'existe plus $userdata['is_online'] = $mininaute->isOnline(); if($from_skynaute_obj instanceof skyFriends) $userdata['is_best_friend'] = $from_skynaute_obj->isBestFriend($id_skynaute); return $userdata; } /** * Returns informations about a user * * If authenticated and called without any argument, will return current authenticated user details. * * @param string $username The username to request informations about (optional) * @param bool $delete Either delete * or delete not ? * * @return array Detailed informations : * int id * bool is_online * string avatar_url * string url * string username * string gender * int nb_friends * bool has_blog * bool has_profile */ public static function get_infos($data, $request_data) { // si on a ça c'est qu'on s'est loggé avec OAuth $is_logged = (isset($request_data['user_token']) && $request_data['user_token'] !== FALSE); if (!$is_logged || isset($data['get']['username'])) { list($id_skynaute, $pseudo) = self::_checkPseudoAndIdSkynaute($data['get']); } else { list($id_skynaute, $pseudo) = self::_checkPseudoAndIdSkynauteFromRequest($request_data); } $userdata = self::_getUserData($id_skynaute, $pseudo); if ($userdata === FALSE) return array(); skyObjectLoader::loadObject('skyFriends'); $friends_obj = skyFriends::getInstance($id_skynaute, $pseudo); $userdata['nb_friends'] = $friends_obj->getFriendsCount(); $mininaute = skyMiniNaute::getInstance($id_skynaute, $pseudo); $userdata['has_blog'] = $mininaute->hasBlog(); $userdata['has_profile'] = ($mininaute->hasProfil() && $mininaute->hasValidProfil()); unset($friends_obj); skyFriends::deleteInstance(); if ($is_logged) { // TODO : champs additionnels } return $userdata; } protected static function _checkFriendsListOrder($data) { if (empty($data['get']['order'])) return DEFAULT_FRIENDS_ORDER; $order = strtoupper(trim($data['get']['order'])); switch ($order) { case 'ALPHA': $order = ALPHA_FRIENDS_ORDER; break; case 'OLDEST_TO_NEWEST': $order = OLDEST_TO_NEWEST_FRIENDS_ORDER; break; case 'NEWEST_TO_OLDEST': $order = NEWEST_TO_OLDEST_FRIENDS_ORDER; break; case 'BEST_FIRST': $order = BEST_FIRST_FRIENDS_ORDER; break; case 'BEST_LAST': $order = BEST_LAST_FRIENDS_ORDER; break; default: $order = DEFAULT_FRIENDS_ORDER; break; } return $order; } /** * Returns a user's friends list, paginated. * */ public static function list_friends($data) { list($id_skynaute, $pseudo) = self::_checkPseudoAndIdSkynaute($data['get']); $page = 1; if(isset($data['get']['page'])) { $page = intval($data['get']['page']); } skyObjectLoader::loadObject('skyFriends'); $order = self::_checkFriendsListOrder($data); $friends_obj = skyFriends::getInstance($id_skynaute, $pseudo); $skynaute_friends = $friends_obj->getFriends($page - 1, $order); $page_max = $friends_obj->getFriendsPageMax() + 1; $ret = array(); foreach($skynaute_friends as $friend) { $friend_data = self::_getUserData($friend['id_skynaute'], $friend['pseudo'], $friends_obj); if(!empty($friend_data)) $ret[] = $friend_data; } return array('max_page' => $page_max, 'friends' => $ret); } /** * Returns a user's best friends list, paginated. * */ public static function list_best_friends($data, $request_data) { list($id_skynaute, $pseudo) = self::_checkPseudoAndIdSkynauteFromRequest($request_data); $page = 1; if(isset($data['get']['page'])) { $page = intval($data['get']['page']); } skyObjectLoader::loadObject('skyFriends'); $order = self::_checkFriendsListOrder($data); $friends_obj = skyFriends::getInstance($id_skynaute, $pseudo); $skynaute_friends = $friends_obj->getBestFriends($page - 1); $page_max = $friends_obj->getBestFriendsPageMax() + 1; $ret = array(); foreach($skynaute_friends as $friend) { $friend_data = self::_getUserData($friend['id_skynaute'], $friend['pseudo'], $friends_obj); if(!empty($friend_data)) $ret[] = $friend_data; } return array('max_page' => $page_max, 'friends' => $ret); } public static function get_mood($data) { list($id_skynaute, $pseudo) = self::_checkPseudoAndIdSkynaute($data['get']); skyObjectLoader::loadObject('skyMood'); $mood = new skyMood($id_skynaute); $r = $mood->load(); if(empty($r)) { $ret = array('message' => '', 'update' => 0); return $ret; } return array('message' => utf8_encode($mood->getMessage()), 'update' => $mood->getModificationTS()); } public static function set_mood($data, $request_data) { $id_skynaute = $request_data['id_skynaute']; if($id_skynaute <= 0) throw new skyException('401 Missing or wrong user token.', self::E_MISSING_OR_MALFORMED_USER_TOKEN); self::_expectParams($data['post'], array('message')); if (empty($data['post']['message'])) throw new skyException('400 Message can not be empty.', self::E_MISSING_ARGUMENT); skyObjectLoader::loadObject('skyMood'); $mood = new skyMood($id_skynaute); $mood->load(); $message = (string) $data['post']['message']; if (!utils::is_utf8($message)) throw new skyException('400 Message must be a UTF8 string.', self::E_WRONG_ENCODING); $message = trim($message); $message = str_replace(array("\r", "\n"), array("", ""), $message); $message = mb_substr($message, 0, 140); $message = utils::utf8_decode($message); $mood->setMessage($message); $ret = (bool) $mood->save(); return $ret; } private static function send_alert_or_event($mode, $data, $request_data) { $id_skynaute = $request_data['id_skynaute']; if($id_skynaute <= 0) throw new skyException('401 Missing or wrong user token.', self::E_MISSING_OR_MALFORMED_USER_TOKEN); self::_expectParams($data['post'], array('event_hash')); // params et url son optionnels if (!isset($data['post']['params'])) $data['post']['params'] = array(); if (!isset($data['post']['url'])) $data['post']['url'] = ''; $event = skyApiEvents::getEventByHashes($request_data['hex_id_application'], $data['post']['event_hash']); if(empty($event)) throw new skyException('400 Unknown event or alert.', self::E_UNKNOWN_EVENT_OR_ALERT); if (isset($event['type']) && $event['type'] != $mode) { $wrong_type = ($mode == skyApiEvents::TYPE_EVENT) ? 'an event.' : 'an alert.'; throw new skyException('400 This event_hash is not '.$wrong_type, self::E_UNKNOWN_EVENT_OR_ALERT); } // support array serialisée en php if (is_string($data['post']['params'])) $data['post']['params'] = unserialize($data['post']['params']); $params = $data['post']['params']; // verification du bon nombre de params $placeholders = preg_match_all('/(\%[0-9]+)/', $event['raw_event_string_fr'], $matches, PREG_SET_ORDER); $required_placeholders = array(); foreach ($matches as $m) { $required_placeholders[] = substr($m[0], 1); } $required_placeholders = array_unique($required_placeholders); if (count($required_placeholders) != count($params)) throw new skyException('400 Wrong number of params.', self::E_MISSING_ARGUMENT); foreach ($required_placeholders as $p) { if (!isset($params[$p])) throw new skyException('400 Wrong params. Expected: '.implode(', ', $required_placeholders), self::E_MISSING_ARGUMENT); } foreach($params as $index => $replacement) { if (!utils::is_utf8($replacement)) throw new skyException('400 Params must be UTF8 strings.', self::E_WRONG_ENCODING); // pas de HTML dans les placeholders $replacement = strip_tags($replacement); // max 100 caractères $replacement = mb_substr($replacement, 0, 100); $params[$index] = $replacement; } $params['url'] = $data['post']['url']; skyObjectLoader::loadLib('utils/url'); if (!skyUrlUtils::isUrlValid($params['url'])) throw new skyException('403 Bad URL.'); $text = skyApiEvents::translateMessage($request_data['hex_id_application'], $data['post']['event_hash'], $params, 'fr'); // filtrage des URL autres que skyrock et domaine de l'application $urls = skyUrlUtils::extractURLS($text); skyObjectLoader::loadObject('api/skyApiApplications'); $app = skyApiApplications::getInstanceByRealId($request_data['id_application']); $app_url_bits = parse_url($app->_application_website); $app_host = $app_url_bits['host']; foreach($urls as $u) { $u_bits = parse_url($u); $host = $u_bits['host']; // [/|\.] est là pour dire : soit c'est un sous-domaine/www soit c'est sans www // et éviter que des URL comme machintrucskyrock.com ne passent // (j'aurais pu écrire "merde à celui qui lira", mais cette regex est une manière plus subtile de le faire) $sky_hosts = '[/|\.]skyrock.com|[/|\.]skyrock.mobi|\.nakama.fr|\.biki.fr'; if (!skyUrlUtils::isUrlValid($u) || !preg_match('#('.$sky_hosts.'|[/|\.]'.$app_host.')$#', '.'.$host)) { throw new skyException('403 Bad URL in message.'); } } // fin du filtrage $event_id = hexdec($data['post']['event_hash']); switch($mode) { case skyApiEvents::TYPE_ALERT: skyObjectLoader::loadObject('newsfeed/skyNewsfeedNotifications'); $nf_alerts = skyNewsfeedNotifications::getInstance(); // le $from_skynaute est le skynaute même. $nf_alerts->generic_api_new($id_skynaute, (int) $request_data['id_application'], $event_id, $params, $id_skynaute); break; case skyApiEvents::TYPE_EVENT: skyObjectLoader::loadObject('newsfeed/skyNewsfeedEvents'); $nf_events = skyNewsfeedEvents::getInstance(); $nf_events->generic_api_new($id_skynaute, (int) $request_data['id_application'], $event_id, $params); break; } return TRUE; } public static function send_alert($data, $request_data) { skyObjectLoader::loadObject('api/skyApiEvents'); return self::send_alert_or_event(skyApiEvents::TYPE_ALERT, $data, $request_data); } public static function send_event($data, $request_data) { skyObjectLoader::loadObject('api/skyApiEvents'); return self::send_alert_or_event(skyApiEvents::TYPE_EVENT, $data, $request_data); } } ?>