Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.93 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'] . "'") == 1)
  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. final public function ref($post){
  255. if(!$this->nameTaken($post)){
  256. echo'The user you reffered does not exist';
  257. }else{
  258. global $engine;
  259. $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));
  260. $engine->query("UPDATE users SET credits=(`credits`+1000) WHERE username='".$post."'");
  261. $engine->query("UPDATE users SET ref=(`ref`+1) WHERE username='".$post."'");
  262. }
  263. }
  264. final public function loginHK()
  265. {
  266. global $template, $_CONFIG, $core;
  267.  
  268. if(isset($_POST['login']))
  269. {
  270. $template->form->setData();
  271. unset($template->form->error);
  272.  
  273. if(isset($template->form->username) && isset($template->form->password))
  274. {
  275. if($this->nameTaken($template->form->username))
  276. {
  277. if($this->userValidation($template->form->username, $core->hashed($template->form->password)))
  278. {
  279. if(($this->getInfo($_SESSION['user']['id'], 'rank')) >= 4)
  280. {
  281. $_SESSION["in_hk"] = true;
  282. header("Location:".$_CONFIG['hotel']['url']."/ase/dash");
  283. exit;
  284. }
  285. else
  286. {
  287. $template->form->error = 'Incorrect access level.';
  288. return;
  289. }
  290. }
  291. else
  292. {
  293. $template->form->error = 'Incorrect password.';
  294. return;
  295. }
  296. }
  297. else
  298. {
  299. $template->form->error = 'User does not exist.';
  300. return;
  301. }
  302. }
  303.  
  304. $template->form->unsetData();
  305. }
  306. }
  307.  
  308. final public function help()
  309. {
  310. global $template, $_CONFIG;
  311. $template->form->setData();
  312.  
  313. if(isset($template->form->help))
  314. {
  315. $to = $_CONFIG['hotel']['email'];
  316. $subject = "Help from RevCMS user - " . $this->getInfo($_SESSION['user']['id'], 'username');
  317. $body = $template->form->question;
  318.  
  319. if (mail($to, $subject, $body))
  320. {
  321. $template->form->error = 'Message successfully sent! We will answer you shortly!';
  322. }
  323. else
  324. {
  325. $template->form->error = 'Message delivery failed.';
  326. }
  327. }
  328. }
  329.  
  330. /*-------------------------------Account settings-------------------------------------*/
  331.  
  332. final public function updateAccount()
  333. {
  334. global $template, $_CONFIG, $core, $engine;
  335.  
  336. if(isset($_POST['account']))
  337. {
  338.  
  339. if(isset($_POST['acc_motto']) && strlen($_POST['acc_motto']) < 30 && $_POST['acc_motto'] != $this->getInfo($_SESSION['user']['id'], 'motto'))
  340. {
  341. $this->updateUser($_SESSION['user']['id'], 'motto', $engine->secure($_POST['acc_motto']));
  342. header('Location: '.$_CONFIG['hotel']['url'].'/account');
  343. exit;
  344. }
  345. else
  346. {
  347. $template->form->error = 'Motto is invalid.';
  348. }
  349.  
  350. if(isset($_POST['acc_email']) && $_POST['acc_email'] != $this->getInfo($_SESSION['user']['id'], 'mail'))
  351. {
  352. if($this->validEmail($_POST['acc_email']))
  353. {
  354. $this->updateUser($_SESSION['user']['id'], 'mail', $engine->secure($_POST['acc_email']));
  355. header('Location: '.$_CONFIG['hotel']['url'].'/account');
  356. exit;
  357. }
  358. else
  359. {
  360. $template->form->error = 'Email is not valid';
  361. return;
  362. }
  363. }
  364.  
  365. if(!empty($_POST['acc_old_password']) && !empty($_POST['acc_new_password']))
  366. {
  367. if($this->userValidation($this->getInfo($_SESSION['user']['id'], 'username'), $core->hashed($_POST['acc_old_password'])))
  368. {
  369. if(strlen($_POST['acc_new_password']) >= 8)
  370. {
  371. $this->updateUser($_SESSION['user']['id'], 'password', $core->hashed($_POST['acc_new_password']));
  372. header('Location: '.$_CONFIG['hotel']['url'].'/me');
  373. exit;
  374. }
  375. else
  376. {
  377. $template->form->error = 'New password is too short';
  378. return;
  379. }
  380. }
  381. else
  382. {
  383. $template->form->error = 'Current password is wrong';
  384. return;
  385. }
  386. }
  387. }
  388. }
  389.  
  390.  
  391. final public function turnOn($k)
  392. {
  393. $j = $this->getID($k);
  394. $this->createSSO($j);
  395. $_SESSION['user']['id'] = $j;
  396. $this->cacheUser($j);
  397. unset($j);
  398. }
  399.  
  400. /*-------------------------------Loggin forgotten-------------------------------------*/
  401.  
  402. final public function forgotten()
  403. {
  404. global $template, $_CONFIG, $core;
  405.  
  406. if(isset($_POST ))
  407. {
  408.  
  409. $template->form->setData();
  410. unset($template->form->error);
  411.  
  412. if($this->nameTaken($template->form->for_username))
  413. {
  414. if(strlen($template->form->for_password) > 6)
  415. {
  416. if($this->getInfo($this->getID($template->form->for_username), 'seckey') == $core->hashed($template->form->for_key))
  417. {
  418. $this->updateUser($this->getID($template->form->for_username), 'password', $core->hashed($template->form->for_password));
  419. $template->form->error = 'Account recovered! Go <b><a href="index">here</a></b> to login!';
  420. return;
  421. }
  422. else
  423. {
  424. $template->form->error = 'Secret key is incorrect';
  425. return;
  426. }
  427. }
  428. else
  429. {
  430. $template->form->error = 'Password must have more than 6 characters.';
  431. return;
  432. }
  433. }
  434. else
  435. {
  436. $template->form->error = 'Username does not exist';
  437. return;
  438. }
  439. }
  440. }
  441.  
  442. /*-------------------------------Create SSO auth_ticket-------------------------------------*/
  443.  
  444. final public function createSSO($k)
  445. {
  446. $sessionKey = 'RevCMS-'.rand(9,999).'/'.substr(sha1(time()).'/'.rand(9,9999999).'/'.rand(9,9999999).'/'.rand(9,9999999),0,33);
  447.  
  448. $this->updateUser($k, 'auth_ticket', $sessionKey);
  449.  
  450. unset($sessionKey);
  451. }
  452.  
  453. /*-------------------------------Adding/Updating/Deleting users-------------------------------------*/
  454.  
  455. final public function addUser($username, $password, $email, $motto, $credits, $pixels, $rank, $figure, $gender, $seckey)
  456. {
  457.  
  458. if($_POST['ref']){
  459. $ref = $_POST['ref'];
  460. $this->ref($ref);
  461. }else{
  462.  
  463. global $engine;
  464. $sessionKey = 'RevCMS-'.rand(9,999).'/'.substr(sha1(time()).'/'.rand(9,9999999).'/'.rand(9,9999999).'/'.rand(9,9999999),0,33);
  465. $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 . "')");
  466. $user_id = $this->getID($username);
  467. $engine->query("INSERT INTO user_info (user_id, bans, cautions, reg_timestamp, login_timestamp, cfhs, cfhs_abusive) VALUES ('". $user_id ."', '0', '0', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), '0', '0')");
  468. unset($sessionKey);
  469. }
  470. }
  471.  
  472. final public function deleteUser($k)
  473. {
  474. global $engine;
  475. $engine->query("DELETE FROM users WHERE id = '" . $k . "' LIMIT 1");
  476. $engine->query("DELETE FROM items WHERE userid = '" . $k . "' LIMIT 1");
  477. $engine->query("DELETE FROM rooms WHERE ownerid = '" . $k . "' LIMIT 1");
  478. }
  479.  
  480. final public function updateUser($k, $key, $value)
  481. {
  482. global $engine;
  483. $engine->query("UPDATE users SET " . $key . " = '" . $engine->secure($value) . "' WHERE id = '" . $k . "' LIMIT 1");
  484. $_SESSION['user'][$key] = $engine->secure($value);
  485. }
  486.  
  487. /*-------------------------------Handling user information-------------------------------------*/
  488.  
  489. final public function cacheUser($k)
  490. {
  491. global $engine;
  492. $userInfo = $engine->fetch_assoc("SELECT username, rank, motto, mail, credits, activity_points, look, auth_ticket, ip_last FROM users WHERE id = '" . $k . "' LIMIT 1");
  493.  
  494. foreach($userInfo as $key => $value)
  495. {
  496. $this->setInfo($key, $value);
  497. }
  498. }
  499.  
  500. final public function setInfo($key, $value)
  501. {
  502. global $engine;
  503. $_SESSION['user'][$key] = $engine->secure($value);
  504. }
  505.  
  506. final public function getInfo($k, $key)
  507. {
  508. global $engine;
  509. if(!isset($_SESSION['user'][$key]))
  510. {
  511. $value = $engine->result("SELECT $key FROM users WHERE id = '" . $engine->secure($k) . "' LIMIT 1");
  512. if($value != null)
  513. {
  514. $this->setInfo($key, $value);
  515. }
  516. }
  517.  
  518. return $_SESSION['user'][$key];
  519. }
  520.  
  521.  
  522.  
  523. /*-------------------------------Get user ID or Username-------------------------------------*/
  524.  
  525. final public function getID($k)
  526. {
  527. global $engine;
  528. return $engine->result("SELECT id FROM users WHERE username = '" . $engine->secure($k) . "' LIMIT 1");
  529. }
  530.  
  531. final public function getUsername($k)
  532. {
  533. global $engine;
  534. return $this->getInfo($_SESSION['user']['id'], 'username');
  535. }
  536.  
  537. }
  538. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement