Advertisement
Guest User

Untitled

a guest
Apr 17th, 2011
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.00 KB | None | 0 0
  1. <?php
  2. /*
  3. +I.D.E ENGINE+
  4. Controller of Account for Modern AAC - Powered by IDE Engine.
  5. A lot of new functionality and variables can be hard-coded here.
  6. If you do NOT understand the code, do NOT change anything in here.
  7. */
  8.  
  9. class Account extends Controller {
  10.  
  11. /* Main index of Account controllers, also work as a __construct(); It is called by engine as a default. */
  12. function index($action = 0) {
  13. if($action == 1) success("Your new character has been created!");
  14. if($action == 2) success("Your nickname has been set! Thank you!");
  15. if($action == 3) success("You have exceded the maximum amount of characters per account.");
  16. if($action == 4) success("Your profile has been updated.");
  17. if($action == 5) success("Your avatar has been updated!");
  18. $this->load->model("Account_model");
  19. if(empty($_SESSION['account_id'])) $_SESSION['account_id'] = $this->Account_model->getAccountID();
  20. $ide = new IDE;
  21. $ide->requireLogin();
  22. if(empty($_SESSION['nickname'])) $ide->redirect(WEBSITE."/index.php/account/setNickname");
  23. $data = array();
  24. $data['loggedUser'] = $_SESSION['name'];
  25. $data['characters'] = $this->Account_model->getCharacters();
  26. $data['messages'] = $this->Account_model->checkMessages();
  27. $ots = POT::getInstance();
  28. $ots->connect(POT::DB_MYSQL, connection());
  29. $account = $ots->createObject('Account');
  30. try { $account->find($_SESSION['name']); } catch(Exception $e) {show_error('There was a problem during loading account. Err code: 220212072010 Futher details: '.$e->getMessage());}
  31. $data['account'] = $account;
  32. $data['acc'] = $this->Account_model->load($_SESSION['account_id']);
  33. $recovery_key = $this->Account_model->getRecoveryKey($_SESSION['name']);
  34. if($recovery_key === "") alert("You don't have recovery key set up. Click <a href='".WEBSITE."/index.php/account/generate_recovery_key'><b>here</b></a> to create one. We strongly recommend to create one now for security reasons.");
  35. /* Load view of account page and send data to it. */
  36. $this->load->view('account', $data);
  37. }
  38.  
  39. /*
  40. Function to check if account with this name already exists, it is used by create controller as a callaback in form validation.
  41. It should be made as an abstract class of database in Model, but I don't think there is point of it.
  42. */
  43. function _account_exists($name) {
  44. $ots = POT::getInstance();
  45. $ots->connect(POT::DB_MYSQL, connection());
  46. $account = new OTS_Account();
  47. try { $account->find($name); } catch(Exception $e) {show_error('There was a problem during loading account. Err code: 220512072010 Futher details: '.$e->getMessage());}
  48. if($account->isLoaded()) { $this->form_validation->set_message('_account_exists', 'Account with this name already exists.');return false;} else return true;
  49. }
  50.  
  51.  
  52. function _nicknameExists($name) {
  53. $this->load->model("account_model");
  54. if($this->account_model->nicknameExists($name)) {
  55. $this->form_validation->set_message('_nicknameExists', 'This nickname already exists!.');
  56. return false;
  57. }
  58. else
  59. return true;
  60. }
  61. function _emailExists($email) {
  62. $this->load->model("account_model");
  63. if($this->account_model->emailExists($email)) {
  64. $this->form_validation->set_message('_emailExists', 'This email is already used by another account already exists!');
  65. return false;
  66. }
  67. else
  68. return true;
  69. }
  70.  
  71. function _characterExists($name) {
  72. $this->load->model("character_model");
  73. if($this->character_model->characterExists($name)) {
  74. $this->form_validation->set_message('_characterExists', 'This character name already exists, please choose another one!');
  75. return false;
  76. }
  77. else
  78. return true;
  79. }
  80.  
  81. function _checkDelay() {
  82. global $config;
  83. if(!isset($_SESSION['accountDelay'])) $_SESSION['accountDelay'] = 0;
  84. if($config['accountDelay']) {
  85. if(@(time()-$_SESSION['accountDelay']) > 240) {
  86. return true;
  87. }
  88. else {
  89. $this->form_validation->set_message('_checkDelay', 'You cannot create another account just after another. Please wait few minutes.');
  90. return false;
  91. }
  92. }
  93. else
  94. return true;
  95. }
  96.  
  97. function _checkCity($id) {
  98. $this->config->load('create_character.php');
  99. if(!array_key_exists($id, $this->config->item('cities'))) {
  100. $this->form_validation->set_message('_checkCity', 'Unknown City');
  101. return false;
  102. }
  103. else
  104. return true;
  105. }
  106.  
  107. function _checkWorld($id) {
  108. $this->config->load('create_character.php');
  109. if(!array_key_exists($id, $this->config->item('worlds'))) {
  110. $this->form_validation->set_message('_checkWorld', 'Unknown World');
  111. return false;
  112. }
  113. else
  114. return true;
  115. }
  116.  
  117. function _checkVocation($id) {
  118. $this->config->load('create_character.php');
  119. if(!array_key_exists($id, $this->config->item('vocations'))) {
  120. $this->form_validation->set_message('_checkVocation', 'Unknown Vocation');
  121. return false;
  122. }
  123. else
  124. return true;
  125. }
  126.  
  127. function _checkSex($id) {
  128. if($id != 0 and $id != 1) {
  129. $this->form_validation->set_message('_checkSex', 'Unknown Sex');
  130. return false;
  131. }
  132. else
  133. return true;
  134. }
  135.  
  136. function _validName($name) {
  137. require("config.php");
  138. $name = explode(" ", $name);
  139. foreach($name as $unit) {
  140. if(in_array(strtolower($unit), $config['invalidNameTags'])) {
  141. $this->form_validation->set_message('_validName', 'Invalid Name');
  142. return false;
  143. }
  144. else if(strlen($unit) == 1) {
  145. $this->form_validation->set_message('_validName', 'Invalid Name');
  146. return false;
  147. }
  148. else
  149. continue;
  150. }
  151. }
  152.  
  153. // Function which make the player more real by tatu hunter
  154. // Eg: elder'Druid = Elder'Druid
  155. // elder'druid = Elder'druid
  156. // druid theMaster = Druid themaster
  157. function strFirst($name) {
  158. $name = explode(' ', trim($name));
  159. for($i=0, $t = sizeof($name); $i<$t; ++$i)
  160. for($j=0, $l=strlen($name[$i]); $j<$l; ++$j)
  161. !$j ?
  162. ($name[$i][$j] = !$i ? ($name[$i][$j] == strtoupper($name[$i][$j]) ? $name[$i][$j] : strtoupper($name[$i][$j])): $name[$i][$j]) :
  163. ($name[$i][$j] = ($name[$i][$j-1] == '\'' ? $name[$i][$j] :
  164. strtolower($name[$i][$j])));
  165.  
  166. $ret = '';
  167. foreach($name as $k)
  168. $ret .= $k . ' ';
  169.  
  170. return trim($ret);
  171. }
  172.  
  173. /* Controller of creating new account. New values can be hard-coded here. (only experienced users) */
  174. function create($ajax = 0) {
  175. require_once("system/application/config/create_character.php");
  176. $ide = new IDE;
  177. global $config;
  178. if($ajax == 1 && $ide->isLogged()) exit;
  179. if($ide->isLogged()) $ide->redirect(WEBSITE.'/index.php/account');
  180. $this->load->helper('form');
  181.  
  182. $vals = array(
  183. 'font_path' => WEBSITE.'/system/fonts/texb.ttf',
  184. 'img_width' => '156',
  185. 'img_height' => 30,
  186. 'expiration' => 120
  187. );
  188. if($_POST) {
  189. $this->load->library('form_validation');
  190. $_POST['nickname'] = ucfirst(strtolower($_POST['nickname']));
  191. $this->form_validation->set_rules('name', 'Account Nasme', 'required|min_length[4]|max_length[32]|callback__account_exists|alpha_numeric|callback__checkDelay');
  192. $this->form_validation->set_rules('nickname', 'Nickname', 'required|min_length[4]|max_length[32]|callback__nicknameExists');
  193. $this->form_validation->set_rules('password', 'Password', 'required|matches[repeat]|min_length[4]|max_length[255]');
  194. $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback__emailExists');
  195. if($this->form_validation->run() == TRUE) {
  196. require(APPPATH.'config/ide_default.php');
  197. $ots = POT::getInstance();
  198. $ots->connect(POT::DB_MYSQL, connection());
  199. $account = new OTS_Account();
  200. $name = $account->createNamed($_POST['name']);
  201. $account->setPassword(sha1($_POST['password']));
  202. $account->setEmail($_POST['email']);
  203. $account->setCustomField('nickname', $_POST['nickname']);
  204. $account->setCustomField('premdays', PREMDAYS);
  205. $account->setCustomField('lastday', $_SERVER['REQUEST_TIME']);
  206. try {
  207. $account->save();
  208. unset($account);
  209. $_SESSION['logged'] = 1;
  210. $_SESSION['name'] = $_POST['name'];
  211. $_SESSION['nickname'] = $_POST['nickname'];
  212. $_SESSION['accountDelay'] = time();
  213.  
  214. $account = $ots->createObject('Account');
  215. $account->find($_POST['name']);
  216.  
  217.  
  218.  
  219. // Create new character
  220.  
  221. if($ajax == 0)
  222. $ide->redirect(WEBSITE.'/index.php/account');
  223. else
  224. $ide->criticalRedirect(WEBSITE.'/index.php/account');
  225. }
  226. catch(Exception $e) {
  227. error($e->getMessage());
  228. }
  229. }
  230. }
  231. #Load view of creating account
  232. if($ajax == 1) {
  233. echo error(validation_errors());
  234. $ide->system_stop();
  235. }
  236. else {
  237. $this->load->view('create', $data);
  238. }
  239. }
  240.  
  241. /* Function to check if passed login and password are correct, it uses abstract database model. */
  242. function _check_login() {
  243. $this->load->model("Account_model");
  244. if($this->Account_model->check_login() == false) {
  245. $this->form_validation->set_message("_check_login", "Account name or password are incorrect.");
  246. return false;
  247. }
  248. else
  249. return true;
  250. }
  251.  
  252. /* Login controller */
  253. function login($action = 0) {
  254. if((int) $action == 1) success("You have been logged out.");
  255. if((int) $action == 2) success("Your account has been recovered. You may login now.");
  256. $ide = new IDE;
  257. $this->load->helper("form");
  258. $this->load->library("form_validation");
  259. if($_POST) {
  260. $this->form_validation->set_rules('name', 'Account Name', 'required|callback__check_login');
  261. $this->form_validation->set_rules('pass', 'Password', 'required');
  262. if(in_array($_POST['name'], $GLOBALS['config']['restrictedAccounts']))
  263. error("The account you try to access is restricted!");
  264. else {
  265. if($this->form_validation->run() == true) {
  266. $_SESSION['logged'] = 1;
  267. $_SESSION['name'] = $_POST['name'];
  268. if(!empty($_SESSION['forward'])) {
  269. $forward = $_SESSION['forward'];
  270. $_SESSION['forward'] = "";
  271. $ide->redirect($forward);
  272. }
  273. else
  274. $ide->redirect(WEBSITE.'/index.php/account');
  275.  
  276. }
  277. }
  278. }
  279. /* Load view of login page. */
  280. $this->load->view("login");
  281.  
  282. }
  283. /* Function to logout from account. */
  284. function logout() {
  285. $ide = new IDE;
  286. $_SESSION['logged'] = '';
  287. $_SESSION['account_id'] = '';
  288. $_SESSION['name'] = '';
  289. $_SESSION['admin'] = 0;
  290. $_SESSION['forward'] = "";
  291. $ide->redirect('login/1');
  292. }
  293.  
  294. /* Controller to generate random recovery key and save it, accessed by user, only once per account. */
  295. function generate_recovery_key() {
  296. $this->load->helper("form");
  297. $ide = new IDE;
  298. $ide->requireLogin();
  299. $this->load->model("Account_model");
  300. if($_POST) {
  301. $data['info'] = '';
  302. $key = $this->Account_model->generateKey($_SESSION['name']);
  303. success("<center><font size='4'>$key</font></center>");
  304. alert("<b>Save this recovery key, you see this key only once! You will never see it again, don't refresh or move away from this website until you save it!</b>");
  305. }
  306. else
  307. $data['info'] = '<center id=\'info\'><b>Press this button to generate your unique recovery key. <br>Remember! You can do this only once! Your recovery key will be shown only once! Write it down, for security reasons we recommend to not save it on computers hard drive!</b></center><br><center><input type=\'submit\' value=\'Generate\' name=\'submit\'></center>';
  308. /* Load view of generating new recovery key. */
  309. $this->load->view('generate_recovery_key', $data);
  310.  
  311. }
  312.  
  313. function _checkCurrentPassword($pass) {
  314. $this->load->model("account_model");
  315. if($this->account_model->checkPassword($pass))
  316. return true;
  317. else {
  318. $this->form_validation->set_message("_checkCurrentPassword", "Current password is incorrect.");
  319. return false;
  320. }
  321. }
  322.  
  323. function changepassword() {
  324. $ide = new IDE;
  325. $ide->requireLogin();
  326. $this->load->helper("form_helper");
  327. if($_POST) {
  328. $this->load->library("form_validation");
  329. $this->form_validation->set_rules('current', 'Current Password', 'required|callback__checkCurrentPassword');
  330. $this->form_validation->set_rules('password', 'Password', 'required|matches[repeat]|min_length[4]|max_length[255]');
  331. if($this->form_validation->run() == true) {
  332. $this->load->model("account_model");
  333. $this->account_model->changePassword($_POST['password'], $_SESSION['name']);
  334. success("Your password has been changed.");
  335. $ide->redirect(WEBSITE."/index.php/account", 2);
  336. }
  337. }
  338. $this->load->view("changepassword");
  339. }
  340.  
  341. function editcomment($id) {
  342. $ide = new IDE;
  343. $ide->requireLogin();
  344. if(empty($id)) $ide->redirect(WEBSITE."/index.php/account");
  345. $this->load->model("account_model");
  346. if(!$this->account_model->isUserPlayer($id)) $ide->redirect(WEBSITE."/index.php/account");
  347. $data['id'] = $id;
  348. if($_POST) {
  349. $this->load->library("form_validation");
  350. $this->form_validation->set_rules('comment', 'Comment', 'max_length[255]|alpha_ide');
  351. if($this->form_validation->run() == true) {
  352. if(@$_POST['hide'] == 1)
  353. $this->account_model->changeComment($id, $_POST['comment'], true);
  354. else
  355. $this->account_model->changeComment($id, $_POST['comment'], false);
  356. success("Your comment has been changed.");
  357. $ide->redirect(WEBSITE."/index.php/account", 2);
  358. }
  359. }
  360. $data['comment'] = $this->account_model->getPlayerComment($id);
  361. $this->load->helper("form_helper");
  362. $this->load->view("edit_comment", $data);
  363. }
  364.  
  365. function deletePlayer($id) {
  366. $ide = new IDE;
  367. $ide->requireLogin();
  368. $id = (int)$id;
  369. if(empty($id)) $ide->redirect(WEBSITE."/index.php/account");
  370. $this->load->model("account_model");
  371. if(!$this->account_model->isUserPlayer($id)) $ide->redirect(WEBSITE."/index.php/account");
  372. $this->account_model->deletePlayer($id);
  373. $ide->redirect(WEBSITE."/index.php/account");
  374. }
  375.  
  376. public function setNickname() {
  377. $ide = new IDE;
  378. $ide->requireLogin();
  379. if(!empty($_SESSION['nickname'])) $ide->goPrevious();
  380. $this->load->helper("form_helper");
  381. if($_POST) {
  382. $_POST['nickname'] = ucfirst(strtolower($_POST['nickname']));
  383. $this->load->library("form_validation");
  384. $this->form_validation->set_rules('nickname', 'Nickname', 'required|min_length[4]|max_length[32]|nickname|callback__nicknameExists');
  385. $this->form_validation->set_rules('rules', 'Rules', 'required');
  386. if($this->form_validation->run()) {
  387. $this->load->model("account_model");
  388. $this->account_model->setNickname($ide->loggedAccountId(), $_POST['nickname']);
  389. $_SESSION['nickname'] = $_POST['nickname'];
  390. $ide->redirect(WEBSITE."/index.php/account/index/2");
  391. }
  392. }
  393. $this->load->view("setNickname");
  394.  
  395. }
  396.  
  397. function _validKey($key) {
  398. $this->load->model("account_model");
  399. $_POST['key'] = str_replace("-", "", $_POST['key']);
  400. if($this->account_model->checkKey($_POST['key'], $_POST['email'])) {
  401. return true;
  402. }
  403. else {
  404. $this->form_validation->set_message("_validKey", "Could not change password. Make sure email and recovery key are valid.");
  405. return false;
  406. }
  407. }
  408.  
  409. public function lost() {
  410. $ide = new IDE;
  411. $this->load->helper("form_helper");
  412. if($_POST) {
  413. $_POST['key'] = str_replace("-", "", $_POST['key']);
  414.  
  415. $this->load->library("form_validation");
  416. $this->form_validation->set_rules('password', 'Password', 'required|matches[repeat]|min_length[4]|max_length[255]');
  417. $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
  418. $this->form_validation->set_rules('key', 'Recovery Key', 'required|callback__validKey');
  419.  
  420. if($this->form_validation->run()) {
  421. $this->load->model("account_model");
  422. $this->account_model->recoveryAccount($_POST['key'], $_POST['email'], $_POST['password']);
  423. $ide->redirect(WEBSITE."/index.php/account/login/2");
  424. }
  425. }
  426. $this->load->view("account_lost");
  427. }
  428.  
  429. }
  430.  
  431. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement