Advertisement
Guest User

Untitled

a guest
Apr 16th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.55 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Revolution;
  4. if(!defined('IN_INDEX')) { die('Sorry, you cannot access this file.'); }
  5. class users implements iUsers
  6. {
  7.  
  8. /*-------------------------------Authenticate-------------------------------------*/
  9.  
  10. final public function isLogged()
  11. {
  12. if(isset($_SESSION['user']['id']))
  13. {
  14. return true;
  15. }
  16.  
  17. return false;
  18. }
  19.  
  20. /*-------------------------------Checking of submitted data-------------------------------------*/
  21.  
  22. final public function validName($username)
  23. {
  24. if(strlen($username) <= 25 && ctype_alnum($username))
  25. {
  26. return true;
  27. }
  28.  
  29. return false;
  30. }
  31.  
  32. final public function validEmail($email)
  33. {
  34. return preg_match("/^[a-z0-9_\.-]+@([a-z0-9]+([\-]+[a-z0-9]+)*\.)+[a-z]{2,7}$/i", $email);
  35. }
  36.  
  37. final public function validSecKey($seckey)
  38. {
  39. if(is_numeric($seckey) && strlen($seckey) == 4)
  40. {
  41. return true;
  42. }
  43.  
  44. return false;
  45. }
  46.  
  47. final public function nameTaken($username)
  48. {
  49. global $engine;
  50.  
  51. if($engine->num_rows("SELECT * FROM users WHERE username = '" . $username . "' LIMIT 1") > 0)
  52. {
  53. return true;
  54. }
  55.  
  56. return false;
  57. }
  58.  
  59. final public function emailTaken($email)
  60. {
  61. global $engine;
  62.  
  63. if($engine->num_rows("SELECT * FROM users WHERE mail = '" . $email . "' LIMIT 1") > 0)
  64. {
  65. return true;
  66. }
  67.  
  68. return false;
  69. }
  70.  
  71. final public function userValidation($username, $password)
  72. {
  73. global $engine, $core, $_CONFIG;
  74. if($_CONFIG['hotel']['bcrypt'] == 1){
  75. if($engine->num_rows("SELECT * FROM users WHERE username = '" . $username . "' LIMIT 1") > 0)
  76. {
  77. $uFetch = $engine->fetch_assoc("SELECT * FROM users WHERE username = '" . $username . "' LIMIT 1");
  78. if(password_verify($password, $uFetch['password'])){
  79. return true;
  80. }else{
  81. return false;
  82. }
  83. }else{
  84. return false;
  85. }
  86. }else{
  87. if($engine->num_rows("SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . $core->hashed($password) . "' LIMIT 1") > 0)
  88. {
  89. return true;
  90. }else{
  91. return false;
  92. }
  93. }
  94. }
  95.  
  96. /*-------------------------------Stuff related to bans-------------------------------------*/
  97.  
  98. final public function isBanned($value)
  99. {
  100. global $engine;
  101. if($engine->num_rows("SELECT * FROM bans WHERE value = '" . $value . "' LIMIT 1") > 0)
  102. {
  103. return true;
  104. }
  105.  
  106. return false;
  107. }
  108.  
  109. final public function getReason($value)
  110. {
  111. global $engine;
  112. return $engine->result("SELECT reason FROM bans WHERE value = '" . $value . "' LIMIT 1", "reason");
  113. }
  114.  
  115. final public function hasClones($ip)
  116. {
  117. global $engine;
  118. if($engine->num_rows("SELECT * FROM users WHERE ip_reg = '" . $_SERVER['REMOTE_ADDR'] . "'") == 1)
  119. {
  120. return true;
  121. }
  122.  
  123. return false;
  124. }
  125.  
  126. /*-------------------------------Login or Register user-------------------------------------*/
  127.  
  128. final public function register()
  129. {
  130. global $core, $template, $_CONFIG;
  131.  
  132. if(isset($_POST['register']))
  133. {
  134. unset($template->form->error);
  135.  
  136. $template->form->setData();
  137.  
  138. if($this->validName($template->form->reg_username))
  139. {
  140. if(!$this->nameTaken($template->form->reg_username))
  141. {
  142. if($this->validEmail($template->form->reg_email))
  143. {
  144. if(!$this->emailTaken($template->form->reg_email))
  145. {
  146. if(strlen($template->form->reg_password) > 6)
  147. {
  148. if($template->form->reg_password == $template->form->reg_rep_password)
  149. {
  150. if(isset($template->form->reg_seckey))
  151. {
  152. if($this->validSecKey($template->form->reg_seckey))
  153. {
  154. //Continue
  155. }
  156. else
  157. {
  158. $template->form->error = 'Secret key must only have 4 numbers';
  159. return;
  160. }
  161. }
  162. if($this->isBanned($_SERVER['REMOTE_ADDR']) == false)
  163. {
  164. if(!$this->hasClones($_SERVER['REMOTE_ADDR']))
  165. {
  166. if(!isset($template->form->reg_gender)) { $template->form->reg_gender = 'M'; }
  167. if(!isset($template->form->reg_figure)) { $template->form->reg_figure = $_CONFIG['hotel']['figure']; }
  168.  
  169. $this->addUser($template->form->reg_username, $core->hashed($template->form->reg_password), $template->form->reg_email, $_CONFIG['hotel']['motto'], $_CONFIG['hotel']['credits'], $_CONFIG['hotel']['pixels'], 1, $template->form->reg_figure, $template->form->reg_gender, $core->hashed($template->form->reg_key));
  170.  
  171. $this->turnOn($template->form->reg_username);
  172.  
  173. header('Location: ' . $_CONFIG['hotel']['url'] . '/me');
  174. exit;
  175. }
  176. else
  177. {
  178. $template->form->error = 'Sorry, but you cannot register twice';
  179. }
  180. }
  181. else
  182. {
  183. $template->form->error = 'Sorry, it appears you are IP banned.<br />';
  184. $template->form->error .= 'Reason: ' . $this->getReason($_SERVER['REMOTE_ADDR']);
  185. return;
  186. }
  187. }
  188. else
  189. {
  190. $template->form->error = 'Password does not match repeated password';
  191. return;
  192. }
  193.  
  194. }
  195. else
  196. {
  197. $template->form->error = 'Password must have more than 6 characters';
  198. return;
  199. }
  200. }
  201. else
  202. {
  203. $template->form->error = 'Email: <b>' . $template->form->reg_email . '</b> is already registered';
  204. return;
  205. }
  206. }
  207. else
  208. {
  209. $template->form->error = 'Email is not valid';
  210. return;
  211. }
  212. }
  213. else
  214. {
  215. $template->form->error = 'Username is already registered';
  216. return;
  217. }
  218. }
  219. else
  220. {
  221. $template->form->error = 'Username is invalid';
  222. return;
  223. }
  224. }
  225. }
  226.  
  227. final public function login()
  228. {
  229. global $template, $_CONFIG, $core;
  230.  
  231. if(isset($_POST['login']))
  232. {
  233. $template->form->setData();
  234. unset($template->form->error);
  235.  
  236. if($this->nameTaken($template->form->log_username))
  237. {
  238. if($this->isBanned($template->form->log_username) == false || $this->isBanned($_SERVER['REMOTE_ADDR']) == false)
  239. {
  240. if($this->userValidation($template->form->log_username, $template->form->log_password))
  241. {
  242. $this->turnOn($template->form->log_username);
  243. $this->updateUser($_SESSION['user']['id'], 'ip_last', $_SERVER['REMOTE_ADDR']);
  244. $template->form->unsetData();
  245. header('Location: ' . $_CONFIG['hotel']['url'] . '/me');
  246. exit;
  247. }
  248. else
  249. {
  250. $template->form->error = 'Details do not match';
  251. return;
  252. }
  253. }
  254. else
  255. {
  256. $template->form->error = 'Sorry, it appears this user is banned<br />';
  257. $template->form->error .= 'Reason: ' . $this->getReason($template->form->log_username);
  258. return;
  259. }
  260. }
  261. else
  262. {
  263. $template->form->error = 'Username does not exist';
  264. return;
  265. }
  266. }
  267. }
  268.  
  269. final public function loginHK()
  270. {
  271. global $template, $_CONFIG, $core;
  272.  
  273. if(isset($_POST['login']))
  274. {
  275. $template->form->setData();
  276. unset($template->form->error);
  277.  
  278. if(isset($template->form->username) && isset($template->form->password))
  279. {
  280. if($this->nameTaken($template->form->username))
  281. {
  282. if($this->userValidation($template->form->username, $template->form->password))
  283. {
  284. if(($this->getInfo($_SESSION['user']['id'], 'rank')) >= 4)
  285. {
  286. $_SESSION["in_hk"] = true;
  287. header("Location:".$_CONFIG['hotel']['url']."/ase/dash");
  288. exit;
  289. }
  290. else
  291. {
  292. $template->form->error = 'Incorrect access level.';
  293. return;
  294. }
  295. }
  296. else
  297. {
  298. $template->form->error = 'Incorrect password.';
  299. return;
  300. }
  301. }
  302. else
  303. {
  304. $template->form->error = 'User does not exist.';
  305. return;
  306. }
  307. }
  308.  
  309. $template->form->unsetData();
  310. }
  311. }
  312.  
  313. final public function help()
  314. {
  315. global $template, $_CONFIG;
  316. $template->form->setData();
  317.  
  318. if(isset($template->form->help))
  319. {
  320. $to = $_CONFIG['hotel']['email'];
  321. $subject = "Help from RevCMS user - " . $this->getInfo($_SESSION['user']['id'], 'username');
  322. $body = $template->form->question;
  323.  
  324. if (mail($to, $subject, $body))
  325. {
  326. $template->form->error = 'Message successfully sent! We will answer you shortly!';
  327. }
  328. else
  329. {
  330. $template->form->error = 'Message delivery failed.';
  331. }
  332. }
  333. }
  334.  
  335. /*-------------------------------Account settings-------------------------------------*/
  336.  
  337. final public function updateAccount()
  338. {
  339. global $template, $_CONFIG, $core, $engine;
  340.  
  341. if(isset($_POST['account']))
  342. {
  343.  
  344. if(isset($_POST['acc_motto']) && strlen($_POST['acc_motto']) < 30 && $_POST['acc_motto'] != $this->getInfo($_SESSION['user']['id'], 'motto'))
  345. {
  346. $this->updateUser($_SESSION['user']['id'], 'motto', $engine->secure($_POST['acc_motto']));
  347. header('Location: '.$_CONFIG['hotel']['url'].'/account');
  348. exit;
  349. }
  350. else
  351. {
  352. $template->form->error = 'Motto is invalid.';
  353. }
  354.  
  355. if(isset($_POST['acc_email']) && $_POST['acc_email'] != $this->getInfo($_SESSION['user']['id'], 'mail'))
  356. {
  357. if($this->validEmail($_POST['acc_email']))
  358. {
  359. $this->updateUser($_SESSION['user']['id'], 'mail', $engine->secure($_POST['acc_email']));
  360. header('Location: '.$_CONFIG['hotel']['url'].'/account');
  361. exit;
  362. }
  363. else
  364. {
  365. $template->form->error = 'Email is not valid';
  366. return;
  367. }
  368. }
  369.  
  370. if(!empty($_POST['acc_old_password']) && !empty($_POST['acc_new_password']))
  371. {
  372. if($this->userValidation($this->getInfo($_SESSION['user']['id'], 'username'), $_POST['acc_old_password']))
  373. {
  374. if(strlen($_POST['acc_new_password']) >= 8)
  375. {
  376. $this->updateUser($_SESSION['user']['id'], 'password', $core->hashed($_POST['acc_new_password']));
  377. header('Location: '.$_CONFIG['hotel']['url'].'/me');
  378. exit;
  379. }
  380. else
  381. {
  382. $template->form->error = 'New password is too short';
  383. return;
  384. }
  385. }
  386. else
  387. {
  388. $template->form->error = 'Current password is wrong';
  389. return;
  390. }
  391. }
  392. }
  393. }
  394.  
  395.  
  396. final public function turnOn($k)
  397. {
  398. $j = $this->getID($k);
  399. $this->createSSO($j);
  400. $_SESSION['user']['id'] = $j;
  401. $this->cacheUser($j);
  402. unset($j);
  403. }
  404.  
  405.  
  406. /*-------------------------------Create SSO auth_ticket-------------------------------------*/
  407.  
  408. final public function createSSO($k)
  409. {
  410. $sessionKey = 'RevCMS-'.rand(9,999).'/'.substr(sha1(time()).'/'.rand(9,9999999).'/'.rand(9,9999999).'/'.rand(9,9999999),0,33);
  411.  
  412. $this->updateUser($k, 'auth_ticket', $sessionKey);
  413.  
  414. unset($sessionKey);
  415. }
  416.  
  417. /*-------------------------------Adding/Updating/Deleting users-------------------------------------*/
  418.  
  419. final public function addUser($username, $password, $email, $motto, $credits, $pixels, $rank, $figure, $gender, $seckey)
  420. {
  421. global $engine;
  422. $sessionKey = 'RevCMS-'.rand(9,999).'/'.substr(sha1(time()).'/'.rand(9,9999999).'/'.rand(9,9999999).'/'.rand(9,9999999),0,33);
  423. $engine->query("INSERT INTO users (username, password, mail, motto, credits, activity_points, rank, look, gender, seckey, ip_last, ip_reg, account_created, last_online, auth_ticket) VALUES('" . $username . "', '" . $password . "', '" . $email . "', '" . $motto . "', '" . $credits . "', '" . $pixels . "', '" . $rank . "', '" . $figure . "', '" . $gender . "', '" . $seckey . "', '" . $_SERVER['REMOTE_ADDR'] . "', '" . $_SERVER['REMOTE_ADDR'] . "', '" . time() . "', '" . time() . "', '" . $sessionKey . "')");
  424. unset($sessionKey);
  425.  
  426. }
  427.  
  428. final public function deleteUser($k)
  429. {
  430. global $engine;
  431. $engine->query("DELETE FROM users WHERE id = '" . $k . "' LIMIT 1");
  432. $engine->query("DELETE FROM items WHERE userid = '" . $k . "' LIMIT 1");
  433. $engine->query("DELETE FROM rooms WHERE ownerid = '" . $k . "' LIMIT 1");
  434. }
  435.  
  436. final public function updateUser($k, $key, $value)
  437. {
  438. global $engine;
  439. $engine->query("UPDATE users SET " . $key . " = '" . $engine->secure($value) . "' WHERE id = '" . $k . "' LIMIT 1");
  440. $_SESSION['user'][$key] = $engine->secure($value);
  441. }
  442.  
  443. /*-------------------------------Handling user information-------------------------------------*/
  444.  
  445. final public function cacheUser($k)
  446. {
  447. global $engine;
  448. $userInfo = $engine->fetch_assoc("SELECT username, rank, motto, mail, credits, activity_points, look, auth_ticket, ip_last FROM users WHERE id = '" . $k . "' LIMIT 1");
  449.  
  450.  
  451.  
  452. foreach($userInfo as $key => $value)
  453. {
  454. $this->setInfo($key, $value);
  455. }
  456. }
  457.  
  458. final public function setInfo($key, $value)
  459. {
  460. global $engine;
  461. $_SESSION['user'][$key] = $engine->secure($value);
  462. }
  463.  
  464. final public function getInfo($k, $key)
  465. {
  466.  
  467. global $engine;
  468. if(!isset($_SESSION['user'][$key]))
  469. {
  470. $value = $engine->result("SELECT $key FROM users WHERE id = '" . $engine->secure($k) . "' LIMIT 1", $key);
  471. if($value != null)
  472. {
  473. $this->setInfo($key, $value);
  474. }
  475. }
  476.  
  477.  
  478. return $_SESSION['user'][$key];
  479.  
  480. }
  481.  
  482.  
  483.  
  484. /*-------------------------------Get user ID or Username-------------------------------------*/
  485.  
  486. final public function getID($k)
  487. {
  488. global $engine;
  489. return $engine->result("SELECT id FROM users WHERE username = '" . $engine->secure($k) . "' LIMIT 1", "id");
  490. }
  491.  
  492. final public function getUsername($k)
  493. {
  494. global $engine;
  495. return $this->getInfo($_SESSION['user']['id'], 'username');
  496. }
  497.  
  498. }
  499. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement