PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/codefight-cms/codefight/app/controllers/frontend/registration/registration.php

http://cmsdamu.googlecode.com/
PHP | 447 lines | 209 code | 122 blank | 116 comment | 15 complexity | 1491abf25050a49e001d57eb5fd61062 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, AGPL-1.0, GPL-2.0
  1. <?php
  2. /**
  3. * Codefight CMS
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@codefight.org so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Codefight CMS to newer
  18. * versions in the future.
  19. *
  20. * @category Codefight CMS
  21. * @package cf_Registration
  22. * @copyright Copyright (c) 2010 Codefight CMS Team (http://codefight.org)
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. */
  25. /**
  26. * Registration Controller
  27. */
  28. class Registration extends MY_Controller
  29. {
  30. /**
  31. * Constructor method
  32. *
  33. * @access public
  34. * @return void
  35. */
  36. public function __construct()
  37. {
  38. /*
  39. | define an array $load with keys model,library etc
  40. | you can load multiple models etc separated by + sign
  41. | you can load the CI way as well though :)
  42. */
  43. $load = array(
  44. 'model' => 'cf_menu_model + blog/cf_blog_model',
  45. 'library' => 'form_validation',
  46. 'helper' => 'form'
  47. );
  48. parent::MY_Controller($load);
  49. }
  50. /**
  51. * Index | Default Method
  52. *
  53. * @access public
  54. * @return void
  55. */
  56. public function index()
  57. {
  58. $config = array(
  59. array(
  60. 'field' => 'firstname',
  61. 'label' => 'First Name',
  62. 'rules' => 'trim|required|xss_clean'
  63. ),
  64. array(
  65. 'field' => 'lastname',
  66. 'label' => 'Last Name',
  67. 'rules' => 'trim|required|xss_clean'
  68. ),
  69. array(
  70. 'field' => 'email',
  71. 'label' => 'Email',
  72. 'rules' => 'trim|required|valid_email|xss_clean'
  73. ),
  74. array(
  75. 'field' => 'password',
  76. 'label' => 'Password',
  77. 'rules' => 'trim|required|matches[password_conf]|xss_clean|md5'
  78. ),
  79. array(
  80. 'field' => 'password_conf',
  81. 'label' => 'Password Confirmation',
  82. 'rules' => 'trim|required|xss_clean'
  83. )
  84. );
  85. $this->form_validation->set_rules($config);
  86. if ($this->form_validation->run()) {
  87. $user_id = '';
  88. $group_id = '2'; //On first user creation, is added to group_id 2 which is public
  89. $active = 0;
  90. $email = set_value('email');
  91. $password = set_value('password');
  92. $firstname = set_value('firstname');
  93. $lastname = set_value('lastname');
  94. $this->db->where('email', $email);
  95. $this->db->from('user');
  96. if ($this->db->count_all_results() < 1) {
  97. $sql = array(
  98. 'user_id' => $user_id,
  99. 'active' => $active,
  100. 'email' => $email,
  101. 'password' => $password,
  102. 'firstname' => $firstname,
  103. 'lastname' => $lastname,
  104. 'group_id' => $group_id);
  105. $this->db->insert('user', $sql);
  106. //Set Success Message
  107. $msg = array('login' => '<p>Registration Successful. You will be notified once your account is activated.</p>');
  108. set_global_messages($msg, 'success');
  109. }
  110. else
  111. {
  112. //Set Error Message
  113. $msg = array('login' => '<p>User with such email is already registered.</p>');
  114. set_global_messages($msg, 'error');
  115. }
  116. }
  117. else
  118. {
  119. if (validation_errors()) {
  120. //Set Error Message
  121. $msg = array('login' => validation_errors());
  122. set_global_messages($msg, 'error');
  123. }
  124. }
  125. //load all required css
  126. $assets['css'] = array('page', 'login');
  127. //load all required js
  128. //$assets['js'] = array();
  129. $this->cf_asset_lib->load($assets);
  130. //main content block [content view]
  131. $data['content_block'] = 'registration/registration_view';
  132. /*
  133. | @process_view('data', 'master page')
  134. | @see app/core/MY_Controller.php
  135. */
  136. $this->process_view($data);
  137. }
  138. /**
  139. * Login
  140. *
  141. * @access public
  142. * @return void
  143. */
  144. public function login()
  145. {
  146. $data = '';
  147. $val = array(
  148. array(
  149. 'field' => 'password',
  150. 'label' => 'Password',
  151. 'rules' => 'trim|required|xss_clean|md5'
  152. ),
  153. array(
  154. 'field' => 'email',
  155. 'label' => 'Email',
  156. 'rules' => 'trim|required|valid_email|xss_clean'
  157. )
  158. );
  159. $this->form_validation->set_rules($val);
  160. if ($this->form_validation->run() == FALSE) {
  161. if (!validation_errors() == '') {
  162. $msg = array('login' => validation_errors());
  163. set_global_messages($msg, 'error');
  164. }
  165. }
  166. else
  167. {
  168. $email = set_value('email');
  169. $password = set_value('password');
  170. /* check login */
  171. $this->cf_login_lib->process_login($email, $password);
  172. /* ------ */
  173. if ($this->session->userdata('logged_in') == FALSE) {
  174. // display login error
  175. $msg = array('error' => '<p>Invalid Login Data, Please try again.</p>');
  176. set_global_messages($msg, 'error');
  177. }
  178. else
  179. {
  180. // display login success message
  181. $msg = array('success' => '<p>Login Successful.</p>');
  182. set_global_messages($msg, 'success');
  183. }
  184. }
  185. $assets = array();
  186. //load all required css
  187. $assets['css'] = array('page', 'login');
  188. //load all required js
  189. //$assets['js'] = array();
  190. $this->cf_asset_lib->load($assets);
  191. //main content block [content view]
  192. $data['content_block'] = 'registration/login_view';
  193. /*
  194. | @process_view('data', 'master page')
  195. | @see app/core/MY_Controller.php
  196. */
  197. $this->process_view($data);
  198. }
  199. /**
  200. * Logout
  201. *
  202. * @access public
  203. * @return void
  204. */
  205. public function logout()
  206. {
  207. //Destroy All Session Data | TODO:: improve
  208. $this->session->sess_destroy();
  209. //Set Logged In As False, In Case All Sessions Not Destroyed
  210. $this->session->set_userdata('logged_in', false);
  211. // display logout success message
  212. $msg = array('success' => '<p>Logout Successful.</p>');
  213. set_global_messages($msg, 'success');
  214. //Show login page again.
  215. $this->login();
  216. }
  217. /**
  218. * Forgotten Password
  219. *
  220. * @access public
  221. * @return void
  222. */
  223. public function forgotten_password()
  224. {
  225. $data = '';
  226. //Define Validation Rules
  227. $val = array(
  228. array(
  229. 'field' => 'email',
  230. 'label' => 'Email',
  231. 'rules' => 'trim|required|valid_email|xss_clean'
  232. )
  233. );
  234. $this->form_validation->set_rules($val);
  235. //Run Validation
  236. if ($this->form_validation->run()) {
  237. $email = set_value('email');
  238. $query = $this->db->get_where('user', array('email' => $email));
  239. $query = $query->result();
  240. //If User Found With Such Email, Reset Password And Notify
  241. if (count($query) >= 1) {
  242. $userData = $query[0];
  243. //Create A Random Password
  244. $newPass = $this->_createRandomPassword();
  245. $newPassMD5 = md5($newPass);
  246. //update database with new password
  247. $this->db->where('email', $email);
  248. $this->db->update('user', array('password' => $newPassMD5));
  249. //send new password
  250. $this->load->library('email');
  251. $this->email->initialize();
  252. $this->email->subject('| ' . $this->setting->site_name . ' | Password Reset');
  253. $this->email->from($this->setting->email_sender, $this->setting->site_name);
  254. $this->email->to($email);
  255. //Prepare Email Body
  256. $emailBody = "
  257. Hi " . $userData->firstname . " " . $userData->lastname . ",
  258. You or someone requested for a new password through forgotten password link at " . $this->setting->site_name . " and we have reset your password.
  259. Your New Password is:
  260. " . $newPass . "
  261. ";
  262. $this->email->message($emailBody);
  263. if ($this->email->send()) {
  264. //Set Success Message
  265. $msg = array('login' => '<p>New Password Sent Successfully.</p>');
  266. set_global_messages($msg, 'success');
  267. $_POST = array();
  268. }
  269. else
  270. {
  271. //Set Error Message
  272. //echo $this->email->print_debugger();
  273. $msg = array('login' => '<p>System could not send password at this time, please try again later.</p>');
  274. set_global_messages($msg, 'error');
  275. }
  276. }
  277. else
  278. {
  279. //Set Error Message
  280. $msg = array('login' => '<p>Invalid Email.</p>');
  281. set_global_messages($msg, 'error');
  282. }
  283. }
  284. if (validation_errors()) {
  285. //Set Error Message
  286. $msg = array('login' => validation_errors());
  287. set_global_messages($msg, 'error');
  288. }
  289. //load all required css
  290. $assets['css'] = array('page', 'login');
  291. //load all required js
  292. //$assets['js'] = array();
  293. $this->cf_asset_lib->load($assets);
  294. //main content block [content view]
  295. $data['content_block'] = 'registration/forgotten_password_view';
  296. /*
  297. | @process_view('data', 'master page')
  298. | @see app/core/MY_Controller.php
  299. */
  300. $this->process_view($data);
  301. }
  302. /**
  303. * Create Random Password
  304. *
  305. * @access public
  306. * @return string
  307. */
  308. public function _createRandomPassword()
  309. {
  310. $chars = "abdefghjmnpqrstuvwxyz23456789";
  311. srand((double)microtime() * 1000000);
  312. $i = 0;
  313. $pass = '';
  314. while ($i <= 7)
  315. {
  316. $num = rand() % 29;
  317. $tmp = substr($chars, $num, 1);
  318. $pass = $pass . $tmp;
  319. $i++;
  320. }
  321. return $pass;
  322. }
  323. }
  324. /* End of file registration.php */
  325. /* Location: ./app/frontend/controllers/registration/registration.php */