Advertisement
Guest User

Untitled

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