Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.24 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.  
  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, maximaal 2 accounts per IP/netwerk';
  166. }
  167. }
  168. else
  169. {
  170. $template->form->error = 'Je bent verbannen op Mine.<br />';
  171. $template->form->error .= 'Reden: ' . $this->getReason($_SERVER['REMOTE_ADDR']);
  172. return;
  173. }
  174. }
  175. else
  176. {
  177. $template->form->error = 'Je wachtwoorden zijn niet hetzelfde';
  178. return;
  179. }
  180.  
  181. }
  182. else
  183. {
  184. $template->form->error = 'Wachtwoord moet meer dan 6 tekens hebben.';
  185. return;
  186. }
  187. }
  188. else
  189. {
  190. $template->form->error = 'E-mail: <b>' . $template->form->reg_email . '</b> is bezet';
  191. return;
  192. }
  193. }
  194. else
  195. {
  196. $template->form->error = 'E-mail is niet geldig';
  197. return;
  198. }
  199. }
  200. else
  201. {
  202. $template->form->error = 'Naam is bezet. Kies een andere';
  203. return;
  204. }
  205. }
  206. else
  207. {
  208. $template->form->error = 'Naam is ongeldig';
  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($template->form->log_username) == false || $this->isBanned($_SERVER['REMOTE_ADDR']) == false)
  226. {
  227. if($this->userValidation($template->form->log_username, $core->hashed($template->form->log_password)))
  228. {
  229. $this->turnOn($template->form->log_username);
  230. $this->updateUser($_SESSION['user']['id'], 'ip_last', $_SERVER['REMOTE_ADDR']);
  231. $template->form->unsetData();
  232. header('Location: ' . $_CONFIG['hotel']['url'] . '/me');
  233. exit;
  234. }
  235. else
  236. {
  237. $template->form->error = 'Je naam en/of wachtwoord is fout!';
  238. return;
  239. }
  240. }
  241. else
  242. {
  243. $template->form->error = 'Je bent verbannen.<br />';
  244. $template->form->error .= 'Dit is waarom: ' . $this->getReason($template->form->log_username);
  245. return;
  246. }
  247. }
  248. else
  249. {
  250. $template->form->error = 'Naam ongeldig';
  251. return;
  252. }
  253. }
  254. }
  255.  
  256. final public function loginHK()
  257. {
  258. global $template, $_CONFIG, $core;
  259.  
  260. if(isset($_POST['login']))
  261. {
  262. $template->form->setData();
  263. unset($template->form->error);
  264.  
  265. if(isset($template->form->username) && isset($template->form->password))
  266. {
  267. if($this->nameTaken($template->form->username))
  268. {
  269. if($this->userValidation($template->form->username, $core->hashed($template->form->password)))
  270. {
  271. if(($this->getInfo($_SESSION['user']['id'], 'rank')) >= 6)
  272. {
  273. $_SESSION["in_hk"] = true;
  274. header("Location:".$_CONFIG['hotel']['url']."/ase/index.php?url=dash");
  275. exit;
  276. }
  277. else
  278. {
  279. $template->form->error = 'Incorrect access level.';
  280. return;
  281. }
  282. }
  283. else
  284. {
  285. $template->form->error = 'Incorrect password.';
  286. return;
  287. }
  288. }
  289. else
  290. {
  291. $template->form->error = 'User does not exist.';
  292. return;
  293. }
  294. }
  295.  
  296. $template->form->unsetData();
  297. }
  298. }
  299.  
  300. final public function help()
  301. {
  302. global $template, $_CONFIG;
  303. $template->form->setData();
  304.  
  305. if(isset($template->form->help))
  306. {
  307. $to = $_CONFIG['hotel']['email'];
  308. $subject = "Help from RevCMS user - " . $this->getInfo($_SESSION['user']['id'], 'username');
  309. $body = $template->form->question;
  310.  
  311. if (mail($to, $subject, $body))
  312. {
  313. $template->form->error = 'Message successfully sent! We will answer you shortly!';
  314. }
  315. else
  316. {
  317. $template->form->error = 'Message delivery failed.';
  318. }
  319. }
  320. }
  321.  
  322. /*-------------------------------Account settings-------------------------------------*/
  323.  
  324. final public function updateAccount()
  325. {
  326. global $template, $_CONFIG, $core, $engine;
  327.  
  328. if(isset($_POST['account']))
  329. {
  330.  
  331. if(isset($_POST['acc_motto']) && strlen($_POST['acc_motto']) < 30 && $_POST['acc_motto'] != $this->getInfo($_SESSION['user']['id'], 'motto'))
  332. {
  333. $this->updateUser($_SESSION['user']['id'], 'motto', $engine->secure($_POST['acc_motto']));
  334. header('Location: '.$_CONFIG['hotel']['url'].'/account');
  335. exit;
  336. }
  337. else
  338. {
  339. $template->form->error = 'Motto is invalid.';
  340. }
  341.  
  342. if(isset($_POST['acc_email']) && $_POST['acc_email'] != $this->getInfo($_SESSION['user']['id'], 'mail'))
  343. {
  344. if($this->validEmail($_POST['acc_email']))
  345. {
  346. $this->updateUser($_SESSION['user']['id'], 'mail', $engine->secure($_POST['acc_email']));
  347. header('Location: '.$_CONFIG['hotel']['url'].'/account');
  348. exit;
  349. }
  350. else
  351. {
  352. $template->form->error = 'Email is not valid';
  353. return;
  354. }
  355. }
  356.  
  357. if(!empty($_POST['acc_old_password']) && !empty($_POST['acc_new_password']))
  358. {
  359. if($this->userValidation($this->getInfo($_SESSION['user']['id'], 'username'), $core->hashed($_POST['acc_old_password'])))
  360. {
  361. if(strlen($_POST['acc_new_password']) >= 8)
  362. {
  363. $this->updateUser($_SESSION['user']['id'], 'password', $core->hashed($_POST['acc_new_password']));
  364. header('Location: '.$_CONFIG['hotel']['url'].'/me');
  365. exit;
  366. }
  367. else
  368. {
  369. $template->form->error = 'New password is too short';
  370. return;
  371. }
  372. }
  373. else
  374. {
  375. $template->form->error = 'Current password is wrong';
  376. return;
  377. }
  378. }
  379. }
  380. }
  381.  
  382.  
  383. final public function turnOn($k)
  384. {
  385. $j = $this->getID($k);
  386. $this->createSSO($j);
  387. $_SESSION['user']['id'] = $j;
  388. $this->cacheUser($j);
  389. unset($j);
  390. }
  391.  
  392. /*-------------------------------Loggin forgotten-------------------------------------*/
  393.  
  394. final public function forgotten()
  395. {
  396. }
  397.  
  398. /*-------------------------------Create SSO auth_ticket-------------------------------------*/
  399.  
  400. final public function createSSO($k)
  401. {
  402. $sessionKey = 'RevCMS-'.rand(9,999).'/'.substr(sha1(time()).'/'.rand(9,9999999).'/'.rand(9,9999999).'/'.rand(9,9999999),0,33);
  403.  
  404. $this->updateUser($k, 'auth_ticket', $sessionKey);
  405.  
  406. unset($sessionKey);
  407. }
  408.  
  409. /*-------------------------------Adding/Updating/Deleting users-------------------------------------*/
  410.  
  411. final public function addUser($username, $password, $email, $motto, $credits, $pixels, $rank, $figure, $gender, $seckey)
  412. {
  413. global $engine;
  414. $sessionKey = 'RevCMS-'.rand(9,999).'/'.substr(sha1(time()).'/'.rand(9,9999999).'/'.rand(9,9999999).'/'.rand(9,9999999),0,33);
  415. $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 . "')");
  416. unset($sessionKey);
  417.  
  418. }
  419.  
  420. final public function deleteUser($k)
  421. {
  422. global $engine;
  423. $engine->query("DELETE FROM users WHERE id = '" . $k . "' LIMIT 1");
  424. $engine->query("DELETE FROM items WHERE userid = '" . $k . "' LIMIT 1");
  425. $engine->query("DELETE FROM rooms WHERE ownerid = '" . $k . "' LIMIT 1");
  426. }
  427.  
  428. final public function updateUser($k, $key, $value)
  429. {
  430. global $engine;
  431. $engine->query("UPDATE users SET " . $key . " = '" . $engine->secure($value) . "' WHERE id = '" . $k . "' LIMIT 1");
  432. $_SESSION['user'][$key] = $engine->secure($value);
  433. }
  434.  
  435. /*-------------------------------Handling user information-------------------------------------*/
  436.  
  437. final public function cacheUser($k)
  438. {
  439. global $engine;
  440. $userInfo = $engine->fetch_assoc("SELECT username, rank, motto, mail, credits, activity_points, look, auth_ticket, ip_last FROM users WHERE id = '" . $k . "' LIMIT 1");
  441.  
  442. foreach($userInfo as $key => $value)
  443. {
  444. $this->setInfo($key, $value);
  445. }
  446. }
  447.  
  448. final public function setInfo($key, $value)
  449. {
  450. global $engine;
  451. $_SESSION['user'][$key] = $engine->secure($value);
  452. }
  453.  
  454. final public function getInfo($k, $key)
  455. {
  456. global $engine;
  457. if(!isset($_SESSION['user'][$key]))
  458. {
  459. $value = $engine->result("SELECT $key FROM users WHERE id = '" . $engine->secure($k) . "' LIMIT 1");
  460. if($value != null)
  461. {
  462. $this->setInfo($key, $value);
  463. }
  464. }
  465.  
  466. return $_SESSION['user'][$key];
  467. }
  468.  
  469.  
  470.  
  471. /*-------------------------------Get user ID or Username-------------------------------------*/
  472.  
  473. final public function getID($k)
  474. {
  475. global $engine;
  476. return $engine->result("SELECT id FROM users WHERE username = '" . $engine->secure($k) . "' LIMIT 1");
  477. }
  478.  
  479. final public function getUsername($k)
  480. {
  481. global $engine;
  482. return $this->getInfo($_SESSION['user']['id'], 'username');
  483. }
  484.  
  485. }
  486. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement