Advertisement
Guest User

Untitled

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