예제 #1
0
 public static function validateRequest()
 {
     $headers = self::getHeaders();
     if (isset($headers['Authorization'])) {
         $dataAuthorisation = explode(' ', $headers['Authorization']);
         $apiData = explode(':', base64_decode($dataAuthorisation[1]));
         if (count($apiData) != 2) {
             throw new Exception(erTranslationClassLhTranslation::getInstance()->getTranslation('lhrestapi/validation', 'Authorization failed!'));
         }
         $apiKey = erLhAbstractModelRestAPIKey::findOne(array('enable_sql_cache' => true, 'filter' => array('active' => 1, 'api_key' => $apiData[1])));
         if (!$apiKey instanceof erLhAbstractModelRestAPIKey) {
             throw new Exception(erTranslationClassLhTranslation::getInstance()->getTranslation('lhrestapi/validation', 'Authorization failed!'));
         }
         if ($apiKey->user->username != $apiData[0]) {
             throw new Exception(erTranslationClassLhTranslation::getInstance()->getTranslation('lhrestapi/validation', 'Authorization failed!'));
         }
         // API Key
         self::$apiKey = $apiKey;
         if (isset($_GET['update_activity'])) {
             $db = ezcDbInstance::get();
             $stmt = $db->prepare('UPDATE lh_userdep SET last_activity = :last_activity WHERE user_id = :user_id');
             $stmt->bindValue(':last_activity', time(), PDO::PARAM_INT);
             $stmt->bindValue(':user_id', self::$apiKey->user->id, PDO::PARAM_INT);
             $stmt->execute();
         }
     } else {
         throw new Exception(erTranslationClassLhTranslation::getInstance()->getTranslation('lhrestapi/validation', 'Authorization header is missing!'));
     }
 }