Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // insert into tofcore_login_handlers (login_key, login_settings, login_order) values ('TankiOnline', '{}', 10);
- namespace IPS\Login;
- class _TankiOnline extends LoginAbstract {
- public static $loginKey = 'TankiOnline';
- const HOST = 'i1.eu.tankionline.com';
- const PATH = '/auth';
- const PORT = 7080;
- protected $host;
- protected $path;
- protected $method;
- public $error = '';
- /**
- * @brief Authentication types
- */
- public $authTypes = \IPS\Login::AUTH_TYPE_USERNAME;
- public function init(){
- $this->host = isset($this->settings['to_url']) ? $this->settings['to_url'] : self::HOST;
- $this->path = isset($this->settings['to_path']) ? $this->settings['to_path'] : self::PATH;
- $this->port = isset($this->settings['to_port']) ? $this->settings['to_port'] : self::PORT;
- }
- public function loginForm($url, $ucp = false){
- $standardForm = new \IPS\Helpers\Form("login_to", 'login', $url);
- $standardForm->class = 'ipsForm_vertical ipsPad';
- $standardForm->add(new \IPS\Helpers\Form\Text('auth', NULL, TRUE, array(), NULL, NULL, NULL, 'auth'));
- $standardForm->add(new \IPS\Helpers\Form\Password('password', NULL, TRUE, array(), NULL, NULL, NULL, 'password'));
- $standardForm->add(new \IPS\Helpers\Form\Captcha('', NULL, TRUE, array(), NULL, NULL, NULL, 'captcha'));
- $standardForm->add(new \IPS\Helpers\Form\Checkbox('remember_me', TRUE));
- return $standardForm;
- }
- public function getData($url){
- $curlres = curl_init();
- curl_setopt($curlres, CURLOPT_URL, $url);
- curl_setopt($curlres, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curlres, CURLOPT_HEADER, false);
- curl_setopt($curlres, CURLOPT_NOSIGNAL, 1);
- curl_setopt($curlres, CURLOPT_CONNECTTIMEOUT, 30);
- curl_setopt($curlres, CURLOPT_TIMEOUT, 30); //in sec
- $ans = curl_exec($curlres);
- $curl_errno = curl_errno($curlres);
- $curl_error = curl_error($curlres);
- curl_close($curlres);
- if ($curl_errno > 0) {
- $this->error = "Error $curl_errno: $curl_error; URL: $url";
- }
- return json_decode($ans,true);
- }
- /**
- * Authenticate
- *
- * @param array $values Values from from
- * @return \IPS\Member
- * @throws \IPS\Login\Exception
- */
- public function authenticate($values){
- /* Init */
- $username = $values['auth'];
- $password = $values['password'];
- $userLower = mb_strtolower($username);
- $url = 'http://' . $this->host . ':' . $this->port . $this->path;
- $userinfo = $this->getData($url . '?type=userinfo&user=' . $userLower);
- if (!$userinfo) {
- date_default_timezone_set('Europe/Moscow');
- $f = fopen(__DIR__ . '/' . "curlerror.log", "a");
- fwrite($f, date('Y-m-d H:i:s ') . $this->error ."\n");
- fclose($f);
- throw new \IPS\Login\Exception("Сервер временно недоступен.", \IPS\Login\Exception::INTERNAL_ERROR);
- }
- if ($userinfo['responseType'] == 'UNKNOWN_USER') {
- throw new \IPS\Login\Exception("Пользователя с таким именем не существует.", \IPS\Login\Exception::INTERNAL_ERROR);
- }
- $user = $userinfo['users'][0];
- $auth = $this->getData($url . '?type=auth&user=' . $userLower . '&pass=' . urlencode($password));
- if ($auth['responseType'] != 'AUTH_SUCCESS') {
- throw new \IPS\Login\Exception("Неверный пароль.", \IPS\Login\Exception::BAD_PASSWORD);
- }
- if ($user['rank'] < 11) {
- throw new \IPS\Login\Exception("Для входа на форум необходимо звание не ниже Уорэнт-офицер 1.", \IPS\Login\Exception::INTERNAL_ERROR);
- }
- $rows = iterator_to_array(\IPS\Db::i()->select('name', 'core_members', 'to_id=' . $user['id']));
- $member = \IPS\Member::load($rows[0], 'name');
- if (!$member->member_id) {
- $member = new \IPS\Member;
- $member->member_group_id = \IPS\Settings::i()->member_group;
- $member->name = $user['uid'];
- if ($user['email']) {
- $member->email = $user['email'];
- }
- $member->to_id = $user['id'];
- //$member->rank = $user['rank'];
- $member->save();
- } else {
- $member->name = $user['uid'];
- if ($user['email']) {
- $member->email = $user['email'];
- }
- $member->to_id = $user['id'];
- $member->save();
- }
- /* Return member */
- return $member;
- }
- /**
- * ACP Settings Form
- *
- * @param string $url URL to redirect user to after successful submission
- * @return array List of settings to save - settings will be stored to core_login_handlers.login_settings DB field
- * @code
- return array( 'savekey' => new \IPS\Helpers\Form\[Type]( ... ), ... );
- * @endcode
- */
- public function acpForm(){
- return array(
- 'to_url' => new \IPS\Helpers\Form\Text('URL', (isset($this->settings['to_url'])) ? $this->settings['to_url'] : self::HOST, true),
- 'to_path' => new \IPS\Helpers\Form\Text('PATH', (isset($this->settings['to_path'])) ? $this->settings['to_path'] : self::PATH, true),
- 'to_port' => new \IPS\Helpers\Form\Text('PORT', (isset($this->settings['to_port'])) ? $this->settings['to_port'] : self::PORT, true)
- );
- }
- /**
- * Can a member change their email/password with this login handler?
- *
- * @param string $type 'username' or 'email' or 'password'
- * @param \IPS\Member $member The member
- * @return bool
- */
- public function canChange($type, \IPS\Member $member){
- return FALSE;
- }
- public function canProcess(\IPS\Member $member){
- return TRUE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement