예제 #1
0
    };
};
$container['errorHandler'] = function ($container) {
    return function ($request, $response, $exception) use($container) {
        $error = json_encode(array('error' => true, 'error_code' => 500, 'msg' => 'something went wrong!'));
        return $container['response']->withStatus(500)->withHeader('Content-Type', 'application/json')->write($error);
    };
};
$container['notAllowedHandler'] = function ($container) {
    return function ($request, $response, $methods) use($container) {
        $error = json_encode(array('error' => true, 'error_code' => 405, 'msg' => 'HTTP request method not allowed'));
        return $container['response']->withStatus(405)->withHeader('Content-Type', 'application/json')->write($error);
    };
};
$app = new \Slim\App($container);
$app->config('debug', $debug_mode);
function parseToken($request)
{
    if (!$request) {
        return false;
    }
    if (!$request->getHeaderLine('Abouda-Token')) {
        return false;
    }
    $token = base64_decode($request->getHeaderLine('Abouda-Token'));
    $token = explode(":", $token);
    if (count($token) != 2) {
        return false;
    }
    $token = array(Users::ID_KEY => trim($token[0]), Users::TOKEN_KEY => trim($token[1]), Users::REMOTE_ADDR_KEY => $_SERVER['REMOTE_ADDR']);
    return $token;
예제 #2
0
파일: bootstrap.php 프로젝트: thecp/wkv-api
//\Slim\Slim::registerAutoloader();
$dependencyContainer = new \Slim\Container();
$dependencyContainer->dbConnection = openDb();
// todo store in session??? -> saves 2ms time
$dependencyContainer['errorHandler'] = function ($dependencyContainer) {
    return function ($request, $response, $exception) use($dependencyContainer) {
        return $dependencyContainer['response']->withStatus(500)->withHeader('Content-Type', 'application/json')->write(json_encode(array('error' => $exception->getMessage())));
    };
};
$dependencyContainer['notFoundHandler'] = function ($dependencyContainer) {
    return function ($request, $response) use($dependencyContainer) {
        return $dependencyContainer['response']->withStatus(404)->withHeader('Content-Type', 'application/json')->write(json_encode(array('error' => 'Seite nicht gefunden.')));
    };
};
$app = new \Slim\App($dependencyContainer);
$app->config('debug', false);
// Login
$app->post('/login', function ($request, $response, $args) {
    $body = json_decode($request->getBody(), true);
    $db = $this->dbConnection;
    $sql = "SELECT * FROM user WHERE login=:login AND password=:password LIMIT 1;";
    $stmt = $db->prepare($sql);
    $stmt->bindValue(':login', $body['alias']);
    $stmt->bindValue(':password', $body['password']);
    $success = $stmt->execute();
    if ($success && $stmt->rowCount()) {
        $userId = $stmt->fetch(PDO::FETCH_ASSOC)['id'];
        // Create SessionToken
        $token = hash('sha256', date('Ymdhis', time()) . $body['alias']);
        $sql = "INSERT INTO session (token, expire, userid) VALUES (:token, :expire, :user);";
        $stmt = $db->prepare($sql);