Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- namespace GalaxisDark\Mvc;
- use GalaxisDark\Models\Forums\User;
- class Controller extends \Phalcon\Mvc\Controller {
- private $theme = "galaxisdark";
- protected $user = false;
- public function beforeExecuteRoute($dispatcher) {
- $this->url->setBasePath(realpath(__DIR__ . "/../.."));
- $this->url->setBaseUri("//galaxisdark.com");
- $this->url->setThemeBasePath($this->url->path("/themes/{$this->theme}"));
- $this->url->setThemeBaseUri($this->url->get("/themes/{$this->theme}"));
- $this->view->setViewsDir($this->url->getThemePath("/views/"));
- $this->view->setMainView("template");
- $this->view->setLayout("main");
- $this->login();
- }
- public function login($force = false) {
- if($this->user === false) {
- $lastvisit = time();
- if(isset($_POST['login']['form'])) {
- $this->user = $this->login_using_form();
- } else if (isset($_COOKIE['mybbuser'])) {
- $this->user = $this->login_using_cookies();
- $lastvisit = null;
- }
- if($this->user) {
- setCookie("mybbuser", "{$this->user->uid}_{$this->user->loginkey}", (time() + 3600), '/', '.galaxisdark.com');
- $this->user->lastip = $_SERVER['REMOTE_ADDR'];
- $this->user->longlastip = $this->ip2long($this->user->lastip);
- $this->user->lastactive = time();
- $this->user->lastvisit = $lastvisit ? $lastvisit : $this->user->lastvisit;
- $this->user->update();
- }
- $this->view->user = $this->user;
- }
- if($force == true && !$this->user) {
- $this->dispatcher->forward([
- 'controller' => 'rpg',
- 'action' => 'login'
- ]);
- }
- return $this->user ? true : false;
- }
- public function logout() {
- setCookie("mybbuser", null, (time() - 3600), '/', '.galaxisdark.com');
- unset($_COOKIE['mybbuser']);
- $this->user = null;
- $this->view->user = null;
- }
- private function ip2long($ip) {
- $ip_long = ip2long($ip);
- if(!$ip_long) {
- $ip_long = sprintf("%u", ip2long($ip));
- if(!$ip_long)
- return 0;
- }
- if($ip_long >= 2147483648)
- $ip_long -= 4294967296;
- return $ip_long;
- }
- private function login_using_form() {
- $username = $_POST['username'];
- $password = $_POST['password'];
- $user = User::find_first([
- 'username = :username:',
- 'bind' => ['username' => $username],
- ':cache' => false
- ]);
- if($user === false || $user->password !== md5(md5($user->salt) . md5($password)))
- $user = null;
- return $user;
- }
- private function login_using_cookies() {
- list($uid, $loginKey) = explode("_", $_COOKIE['mybbuser']);
- $user = User::get_by_id($uid, [':cache' => ['query' => 0]]);
- if($user == false || $user->loginkey !== $loginKey)
- $user = null;
- return $user;
- }
- public function start_journey() {
- if($this->user && $this->user->trainer->region_id == 0) {
- $this->response->redirect('/start_journey');
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement