Guest User

Untitled

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