Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. <?php
  2.  
  3. // insert into tofcore_login_handlers (login_key, login_settings, login_order) values ('TankiOnline', '{}', 10);
  4.  
  5. namespace IPS\Login;
  6.  
  7. class _TankiOnline extends LoginAbstract {
  8.  
  9. public static $loginKey = 'TankiOnline';
  10.  
  11. const HOST = 'i1.eu.tankionline.com';
  12. const PATH = '/auth';
  13. const PORT = 7080;
  14.  
  15.  
  16. protected $host;
  17. protected $path;
  18. protected $method;
  19.  
  20. public $error = '';
  21.  
  22. /**
  23. * @brief Authentication types
  24. */
  25. public $authTypes = \IPS\Login::AUTH_TYPE_USERNAME;
  26.  
  27. public function init(){
  28. $this->host = isset($this->settings['to_url']) ? $this->settings['to_url'] : self::HOST;
  29. $this->path = isset($this->settings['to_path']) ? $this->settings['to_path'] : self::PATH;
  30. $this->port = isset($this->settings['to_port']) ? $this->settings['to_port'] : self::PORT;
  31. }
  32.  
  33. public function loginForm($url, $ucp = false){
  34. $standardForm = new \IPS\Helpers\Form("login_to", 'login', $url);
  35. $standardForm->class = 'ipsForm_vertical ipsPad';
  36. $standardForm->add(new \IPS\Helpers\Form\Text('auth', NULL, TRUE, array(), NULL, NULL, NULL, 'auth'));
  37. $standardForm->add(new \IPS\Helpers\Form\Password('password', NULL, TRUE, array(), NULL, NULL, NULL, 'password'));
  38. $standardForm->add(new \IPS\Helpers\Form\Captcha('', NULL, TRUE, array(), NULL, NULL, NULL, 'captcha'));
  39. $standardForm->add(new \IPS\Helpers\Form\Checkbox('remember_me', TRUE));
  40.  
  41. return $standardForm;
  42. }
  43.  
  44. public function getData($url){
  45. $curlres = curl_init();
  46. curl_setopt($curlres, CURLOPT_URL, $url);
  47. curl_setopt($curlres, CURLOPT_RETURNTRANSFER, true);
  48. curl_setopt($curlres, CURLOPT_HEADER, false);
  49. curl_setopt($curlres, CURLOPT_NOSIGNAL, 1);
  50. curl_setopt($curlres, CURLOPT_CONNECTTIMEOUT, 30);
  51. curl_setopt($curlres, CURLOPT_TIMEOUT, 30); //in sec
  52. $ans = curl_exec($curlres);
  53. $curl_errno = curl_errno($curlres);
  54. $curl_error = curl_error($curlres);
  55. curl_close($curlres);
  56. if ($curl_errno > 0) {
  57. $this->error = "Error $curl_errno: $curl_error; URL: $url";
  58. }
  59.  
  60. return json_decode($ans,true);
  61. }
  62.  
  63. /**
  64. * Authenticate
  65. *
  66. * @param array $values Values from from
  67. * @return \IPS\Member
  68. * @throws \IPS\Login\Exception
  69. */
  70. public function authenticate($values){
  71. /* Init */
  72. $username = $values['auth'];
  73. $password = $values['password'];
  74. $userLower = mb_strtolower($username);
  75.  
  76. $url = 'http://' . $this->host . ':' . $this->port . $this->path;
  77.  
  78. $userinfo = $this->getData($url . '?type=userinfo&user=' . $userLower);
  79. if (!$userinfo) {
  80. date_default_timezone_set('Europe/Moscow');
  81. $f = fopen(__DIR__ . '/' . "curlerror.log", "a");
  82. fwrite($f, date('Y-m-d H:i:s ') . $this->error ."\n");
  83. fclose($f);
  84.  
  85.  
  86. throw new \IPS\Login\Exception("Сервер временно недоступен.", \IPS\Login\Exception::INTERNAL_ERROR);
  87. }
  88. if ($userinfo['responseType'] == 'UNKNOWN_USER') {
  89. throw new \IPS\Login\Exception("Пользователя с таким именем не существует.", \IPS\Login\Exception::INTERNAL_ERROR);
  90. }
  91.  
  92. $user = $userinfo['users'][0];
  93.  
  94. $auth = $this->getData($url . '?type=auth&user=' . $userLower . '&pass=' . urlencode($password));
  95.  
  96. if ($auth['responseType'] != 'AUTH_SUCCESS') {
  97. throw new \IPS\Login\Exception("Неверный пароль.", \IPS\Login\Exception::BAD_PASSWORD);
  98. }
  99.  
  100. if ($user['rank'] < 11) {
  101. throw new \IPS\Login\Exception("Для входа на форум необходимо звание не ниже Уорэнт-офицер 1.", \IPS\Login\Exception::INTERNAL_ERROR);
  102. }
  103.  
  104. $rows = iterator_to_array(\IPS\Db::i()->select('name', 'core_members', 'to_id=' . $user['id']));
  105.  
  106. $member = \IPS\Member::load($rows[0], 'name');
  107. if (!$member->member_id) {
  108. $member = new \IPS\Member;
  109. $member->member_group_id = \IPS\Settings::i()->member_group;
  110. $member->name = $user['uid'];
  111. if ($user['email']) {
  112. $member->email = $user['email'];
  113. }
  114. $member->to_id = $user['id'];
  115. //$member->rank = $user['rank'];
  116. $member->save();
  117. } else {
  118. $member->name = $user['uid'];
  119. if ($user['email']) {
  120. $member->email = $user['email'];
  121. }
  122. $member->to_id = $user['id'];
  123. $member->save();
  124. }
  125. /* Return member */
  126. return $member;
  127. }
  128.  
  129.  
  130. /**
  131. * ACP Settings Form
  132. *
  133. * @param string $url URL to redirect user to after successful submission
  134. * @return array List of settings to save - settings will be stored to core_login_handlers.login_settings DB field
  135. * @code
  136. return array( 'savekey' => new \IPS\Helpers\Form\[Type]( ... ), ... );
  137. * @endcode
  138. */
  139. public function acpForm(){
  140. return array(
  141. 'to_url' => new \IPS\Helpers\Form\Text('URL', (isset($this->settings['to_url'])) ? $this->settings['to_url'] : self::HOST, true),
  142. 'to_path' => new \IPS\Helpers\Form\Text('PATH', (isset($this->settings['to_path'])) ? $this->settings['to_path'] : self::PATH, true),
  143. 'to_port' => new \IPS\Helpers\Form\Text('PORT', (isset($this->settings['to_port'])) ? $this->settings['to_port'] : self::PORT, true)
  144. );
  145. }
  146.  
  147.  
  148. /**
  149. * Can a member change their email/password with this login handler?
  150. *
  151. * @param string $type 'username' or 'email' or 'password'
  152. * @param \IPS\Member $member The member
  153. * @return bool
  154. */
  155. public function canChange($type, \IPS\Member $member){
  156. return FALSE;
  157. }
  158.  
  159. public function canProcess(\IPS\Member $member){
  160. return TRUE;
  161. }
  162.  
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement