<?php\n\ndefined('BASEPATH') or exit('No direct script access allowed');\n\n$dir = FCPATH . 'forum';\nrequire $dir . '/src/XF.php';\n\nclass Forum_engine extends MY_Controller\n{\n\n    private $xf;\n\n    public function __construct()\n    {\n        parent::__construct();\n\n        if (empty($this->session->logged) && $this->router->fetch_method() != 'auth') {\n            $this->session->set_flashdata('show_login_form', true);\n            redirect('engine/error?http_code=403');\n        } else {\n            XF::start($dir);\n            $this->xf = XF::setupApp('XF\\Pub\\App');\n        }\n    }\n\n    private function _login()\n    {\n        $ip      = $this->xf->request->getIp();\n        $service = $this->xf->service('XF:User\\Login', null, $ip);\n\n        $user = $service->getUserFromID($this->session->forum_id);\n        $this->xf->session->changeUser($user);\n        XF::setVisitor($user);\n        $this->xf->repository('XF:SessionActivity')->clearUserActivity(0, $ip);\n        $this->xf->repository('XF:Ip')->logIp(\n            $user->user_id, $ip,\n            'user', $user->user_id, 'login'\n        );\n\n        $rememberRepo = $this->xf->repository('XF:UserRemember');\n        $key          = $rememberRepo->createRememberRecord($user->user_id);\n        $value        = $rememberRepo->getCookieValue($user->user_id, $key);\n\n        $this->load->helper('cookie');\n\n        $cookie = array('name' => 'user', 'value' => $value, 'expire' =>  time() + (365 * 86400), 'prefix' => 'yggxf_');\n        $this->input->set_cookie($cookie);\n    }\n\n    private function _register()\n    {\n        $registration = XF::service('XF:User\\Registration');\n        $data         = array\n            (\n            'username' => $this->session->nickname,\n            'email'    => $this->session->email,\n            'password' => bin2hex(random_bytes(10)),\n        );\n\n        $registration->setFromInput($data);\n\n        $user = (array) $registration->save();\n        $user = array_values($user)[4]['user_id'];\n        $this->db->where('id', $this->session->id)->set('forum_id', $user)->update('users');\n        $this->session->forum_id = $user;\n        $this->_login();\n    }\n\n    public function auth()\n    {\n        $this->load->library('user_agent');\n        $redir_path = $this->input->get('redir_path');\n\n        /**if ($redir_path == '') {\n            $ref = $this->agent->referrer();\n\n            if (parse_url($ref, PHP_URL_HOST) == parse_url(base_url(), PHP_URL_HOST)) {\n                $path          = parse_url($ref, PHP_URL_PATH);\n                $path_exploded = explode('/', parse_url($ref, PHP_URL_PATH));\n                // On vérifie si le visiteur vient du forum\n                if ($path_exploded[2] == 'ww2_forum') {\n                    $redirect = true;\n                }\n            }\n        } else {\n            $redir_path_exploded = explode('/', $redir_path);\n\n            if ($redir_path_exploded[2] == 'ww2_forum') {\n                $path_response = $redir_path;\n            }\n        }/***/\n\n        if ($this->session->logged) {\n            if ($this->session->forum_id == 0) {\n                $this->_register();\n            } else {\n                $this->_login();\n            }\n            // Redirect forum\n           redirect('forum');\n\n        } else {\n\t\t\t// User is no connected\n            $this->session->set_flashdata('show_login_form', true);\n            redirect('engine/error?http_code=403');\n        }\n\n/**        if (isset($path_response)) {\n            $base = 'http://yggtorrent.is';\n            redirect($base . $path_response);\n        } else if ($redirect == true) {\n            $path = $path;\n            $path .= parse_url($ref, PHP_URL_QUERY);\n            //echo $path;\n            $this->session->logged ? redirect($ref) : redirect(base_url() . '?action=signin&target=forum&redir_path=' . $path);\n        } else {\n            redirect('ww2_forum');\n        }**/\n    }\n\n}\n\n