Advertisement
Guest User

Untitled

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