diff --git a/ground/app.php b/ground/app.php index b181017..a580fec 100644 --- a/ground/app.php +++ b/ground/app.php @@ -30,6 +30,7 @@ class App private static $config = array(); private static $urlParams = array(); public static $DB = null; + public static $user = null; /** * Разбивает урл на контроллер, метод и выбирает параметры. @@ -60,6 +61,7 @@ class App public static function run($config = '') { ob_start(); + session_start(); self::$config = $config ? include_once $config : array(); self::parseUrl(); @@ -69,6 +71,9 @@ class App { $confDB = self::getConfig('DB'); self::$DB = new PDO('mysql:host=' . $confDB['host'] . ';dbname=' . $confDB['dbname'].';charset=utf8;', $confDB['user'], $confDB['password']); + + // Работать с пользователями моно только при наличии БД + self::userInit(); } catch (PDOException $e) { @@ -94,7 +99,36 @@ class App die($message ? $message : '404 - Not Found'); } - /** + // Пытаемся восстановить пользователя из сессии + public static function userInit() + { + if (isset($_SESSION['user_id']) && isset($_SESSION['user_key']) && $_SESSION['user_id'] && $_SESSION['user_key'] ) + { + $user = new User(); + $user = $user->getByPK((int)$_SESSION['user_id']); + + if ( $user && md5($user->uid) == $_SESSION['user_key'] ) + { + self::$user = $user; + } + } + } + + public static function userLogin($user) + { + $_SESSION['user_id'] = $user->id; + $_SESSION['user_key'] = md5($user->uid); + return true; + } + + public static function userLogout() + { + $_SESSION['user_id'] = null; + $_SESSION['user_key'] = null; + return true; + } + + /** * Перенаправление на другой URL * @param type $url * @param type $httpCode - по умолчанию 307, временный редирект diff --git a/ground/controllers/IndexController.php b/ground/controllers/IndexController.php index a20a34b..8e47567 100644 --- a/ground/controllers/IndexController.php +++ b/ground/controllers/IndexController.php @@ -4,7 +4,7 @@ class IndexController extends Controller { /** - * Действие по умолчанию - вывоим каталог товаров. + * Действие по умолчанию */ static function actionIndex() { @@ -16,6 +16,9 @@ class IndexController extends Controller self::addStyle('/css/bootstrap.min.css'); self::addStyle('/css/bootstrap-theme.min.css'); + if (!App::$user ) + self::addScript('//ulogin.ru/js/ulogin.js'); + $category = new Category(); $res = App::$DB->query("SELECT COUNT(`id`) pointsCount FROM `points`")->fetch(PDO::FETCH_ASSOC); @@ -23,7 +26,54 @@ class IndexController extends Controller self::render('indexTemplate.php', array( 'categories' => $category->getAll('', 'ord'), + 'user' => App::$user, )); } + static function actionUser() + { + print $_SERVER['HTTP_HOST']; + var_dump(App::$user->id, App::$user->nick, App::$user->name, App::$user->network); + } + + static function actionLogout() + { + App::userLogout(); + App::redirect('/'); + } + + static function actionLogin() + { + $source = file_get_contents('http://ulogin.ru/token.php?token=' . $_POST['token'] . '&host=' . $_SERVER['HTTP_HOST']); + $uLoginUser = json_decode($source, true); + $uLoginUser['identity'] = md5($uLoginUser['uid'].$uLoginUser['network'].'=P'); + + $user = new User(); + $user->getByUID($uLoginUser['identity']); + + if( !$user->id ) + { + $res = $user->add(array( + 'uid' => $uLoginUser['identity'], + 'nick' => $uLoginUser['nickname'], + 'name' => $uLoginUser['first_name'], + 'profile' => $uLoginUser['profile'], + 'network' => $uLoginUser['network'] + )); + + if ($res) + { + $user = new User(); + $user = $user->getByPK($res); + } + else + { + return false; + } + } + + App::userLogin($user); + App::redirect('/'); + } + } diff --git a/ground/controllers/JsonController.php b/ground/controllers/JsonController.php index 20db9d2..3ca4c4c 100644 --- a/ground/controllers/JsonController.php +++ b/ground/controllers/JsonController.php @@ -81,10 +81,14 @@ class JsonController extends Controller if(!( isset($_POST['source']) )) $errors[5]='Не передано поле источкика информации. Допускается пустое значение.'; if(!( isset($_POST['lat']) && $_POST['lat'] )) $errors[6]='Ошибочное значение широты'; if(!( isset($_POST['lng']) && $_POST['lng'] )) $errors[7]='Ошибочное значение долготы'; + if(!App::$user ) $errors[8]='Вы не авторизованы! Пожалуйста, войдите.'; + if (empty($errors)) { $point = new Point(); + $_POST['author'] = App::$user->id; + $_POST['date'] = time(); $point->add($_POST); if ( $point->id ) diff --git a/ground/models/User.php b/ground/models/User.php new file mode 100644 index 0000000..25236e4 --- /dev/null +++ b/ground/models/User.php @@ -0,0 +1,26 @@ +_tableName_ = 'users'; + parent::__construct(); + } + + function getByUID($uid) + { + $res = App::$DB->query('SELECT `id` FROM ' . $this->_tableName_ . ' WHERE `uid` = "' . $uid . '"'); + if ($res) + { + $res = $res->fetch(PDO::FETCH_ASSOC); + return parent::getByPK((int)$res['id']); + } + else + { + return false; + } + } +} + diff --git a/ground/views/indexTemplate.php b/ground/views/indexTemplate.php index da6a3c6..92fc474 100644 --- a/ground/views/indexTemplate.php +++ b/ground/views/indexTemplate.php @@ -25,11 +25,32 @@