id; } // Clean incomming variables $user_id = Dba::escape($user_id); $timestamp = intval($timestamp); $ip = ip2int($ip); // Log this attempt debug_event('API','Login Attempt, IP:' . int2ip($ip) . ' Time:' . $timestamp . ' User:' . $user_id . ' Auth:' . $passphrase,'1'); // Run the query and return the passphrases as we'll have to mangle them // to figure out if they match what we've got $sql = "SELECT * FROM `access_list` WHERE `user`='$user_id' AND `start` <= '$ip' AND `end` >= '$ip'"; $db_results = Dba::query($sql); while ($row = Dba::fetch_assoc($db_results)) { // Combine and MD5 this mofo $md5pass = md5($timestamp . $row['key']); if ($md5pass === $passphrase) { // Create the Session, in this class for now needs to be moved $token = self::create_session($row['level'],$ip,$user_id); debug_event('API','Login Success, passphrase matched','1'); return array('auth'=>$token,'api'=>self::$version); } // match } // end while debug_event('API','Login Failed, unable to match passphrase','1'); } // handhsake /** * create_session * This actually creates the new session it takes the level, ip and user * and figures out the agent and expire then returns the token */ public static function create_session($level,$ip,$user_id) { // Generate the token $token = md5(uniqid(rand(), true)); $level = Dba::escape($level); $agent = Dba::escape($_SERVER['HTTP_USER_AGENT']); $expire = time() + 3600; $sql = "REPLACE INTO `session_api` (`id`,`user`,`agent`,`level`,`expire`,`ip`) " . "VALUES ('$token','$user_id','$agent','$level','$expire','$ip')"; $db_results = Dba::query($sql); if ($db_results) { return $token; } return false; } // create_session } // API class ?>