Guest User

Untitled

a guest
Jun 6th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.76 KB | None | 0 0
  1. <?php
  2.  
  3. class User extends Controller {
  4.  
  5. function User()
  6. {
  7. parent::Controller();
  8. $this->output->enable_profiler(TRUE);
  9. // Load email library
  10. $this->load->library('email');
  11. }
  12.  
  13. /*
  14. ******************
  15. * PAGE FUNCTIONS *
  16. ******************
  17. */
  18.  
  19. // Login
  20. function login()
  21. {
  22. // Validation Rules
  23. $rules = array(
  24. 'email' => 'required|callback_check_login',
  25. 'password' => 'required');
  26.  
  27. // Set validation rules
  28. $this->validation->set_rules($rules);
  29.  
  30. // Validation fields
  31. $fields = array(
  32. 'email'=>'Email Address',
  33. 'password'=>'Password');
  34.  
  35. // Set error delimiters
  36. $this->validation->set_error_delimiters('<li>', '</li>');
  37.  
  38. // Set validation fields
  39. $this->validation->set_fields($fields);
  40.  
  41. if ($this->validation->run())
  42. {
  43. // Redirect logged in users to home.
  44. redirect("my/home");
  45. }
  46. else
  47. {
  48. // Write form view to template engine
  49. $this->template->write_view('content', 'forms/login');
  50. // render page
  51. $this->template->render();
  52. }
  53. }
  54.  
  55. // Register
  56. function register()
  57. {
  58. // Validation Rules
  59. $rules = array(
  60. 'first_name' => 'required',
  61. 'last_name' => 'required',
  62. 'email' => 'required|valid_email|callback_email_check',
  63. 'password' => 'required|matches[confirm_password]',
  64. 'confirm_password' => 'required',
  65. 'gender' => 'required',
  66. 'day' => 'required',
  67. 'month' => 'required',
  68. 'year' => 'required');
  69.  
  70. // Set validation rules
  71. $this->validation->set_rules($rules);
  72.  
  73. // Validation fields
  74. $fields = array(
  75. 'first_name'=>'First Name',
  76. 'last_name'=>'Last Name',
  77. 'email'=>'Email Address',
  78. 'password'=>'Password',
  79. 'confirm_password'=>'Confirm Password',
  80. 'gender'=>'Gender',
  81. 'day'=>'Day',
  82. 'month'=>'Month',
  83. 'year'=>'Year');
  84.  
  85. // Set error delimiters
  86. $this->validation->set_error_delimiters('<li>', '</li>');
  87.  
  88. // Set validation fields
  89. $this->validation->set_fields($fields);
  90.  
  91. if ($this->validation->run())
  92. {
  93. // Create birthday stamp.
  94. $birthday = mktime(0, 0, 0, $this->input->post('month'), $this->input->post('day'), $this->input->post('year'));
  95. // Database fields
  96. $data = array(
  97. 'First_Name' => $this->input->post('first_name'),
  98. 'Last_Name' => $this->input->post('last_name'),
  99. 'Email' => $this->input->post('email'),
  100. 'Password' => md5($this->input->post('password')),
  101. 'Birth' => $birthday,
  102. 'Gender' => $this->input->post('gender'),
  103. 'Stamp' => time()
  104. );
  105. // Create user.
  106. $this->User_auth->create_user($data);
  107.  
  108. // Send out Activation email
  109. $this->email->from('auto@domain.com', 'Site');
  110. $this->email->to($this->input->post('email'));
  111.  
  112. $this->email->subject('Site Account Activation');
  113.  
  114. // Email template.
  115. $message = 'You need to activate your account before you are able to login.'."\n\n";
  116. $message .= 'Click or paste the link below into your browser to activate your account.'."\n";
  117. $message .= $this->config->site_url().'/activate/'.md5($this->input->post('email')).''."\n\n";
  118. $message .= '*******************'."\n";
  119. $message .= '* ACCOUNT DETAILS *'."\n";
  120. $message .= '*******************'."\n";
  121. $message .= 'username: '.$this->input->post('email').''."\n";
  122. $message .= 'password: '.$this->input->post('password').''."\n\n";
  123. $message .= 'The [Site] Team';
  124.  
  125. $this->email->message($message);
  126.  
  127. $this->email->send();
  128.  
  129. $content_body = '<p>Your account has been successfully created.</p>';
  130. $content_body .= '<p>You will be sent out an acivation e-mail in the next 24/48 hours.</p>';
  131. $content_body .= '<p>If you don\'t recieve your acivation e-mail please contact the support team at help@domain.com</p>';
  132.  
  133. $content = array('title'=>'Congratulations','content'=>$content_body);
  134.  
  135. // Write form view to template engine
  136. $this->template->write_view('body', 'content/message', $content);
  137. // render page
  138. $this->template->render();
  139. }
  140. else
  141. {
  142. // Write form view to template engine
  143. $this->template->write_view('body', 'forms/register');
  144. // render page
  145. $this->template->render();
  146. }
  147. }
  148.  
  149. // Activation
  150. function activate()
  151. {
  152. // Grab segment 3 from URL
  153. $email = $this->uri->segment(3);
  154. // Check to see if email is not empty
  155. if (!empty($email))
  156. {
  157. // Query
  158. $query = $this->db->get_where("users", array('MD5(Email)' => $email));
  159. // Row count from results
  160. if ($query->num_rows() > 0)
  161. {
  162. // Get results
  163. $row = $query->row();
  164. // Check if user account has been activated
  165. if ($row->Activate == 0)
  166. {
  167. // update activation details for this user account
  168. $data = array('Activate' => 1);
  169. $where = "MD5(Email)='".$email."'";
  170. $str = $this->db->update_string('users', $data, $where);
  171. $this->db->query($str);
  172.  
  173. // dump complete message
  174. $content_body = '<p>Your account has been successfully activated.</p>';
  175. $content_body .= '<p><a href="#" title="">Login to your account</a></p>';
  176.  
  177. $content = array('title'=>'Activated','content'=>$content_body);
  178.  
  179. // Write form view to template engine
  180. $this->template->write_view('body', 'content/message', $content);
  181. // render page
  182. $this->template->render();
  183. }
  184. else
  185. {
  186. // dump error message (account is allready active)
  187. $content_body = '<p>Your account has allready been activated.</p>';
  188.  
  189. $content = array('title'=>'Activation Error (ACCA)','content'=>$content_body);
  190.  
  191. // Write form view to template engine
  192. $this->template->write_view('body', 'content/message', $content);
  193. // render page
  194. $this->template->render();
  195. }
  196. }
  197. else
  198. {
  199. // dump error message (no user found)
  200. $content_body = '<p>There were problems when trying to activate your account.</p>';
  201. $content_body .= '<p>if the problem persists please contact the support team at help@domain.com</p>';
  202.  
  203. $content = array('title'=>'Activation Error (NOUSR)','content'=>$content_body);
  204.  
  205. // Write form view to template engine
  206. $this->template->write_view('body', 'content/message', $content);
  207. // render page
  208. $this->template->render();
  209. }
  210. }
  211. else
  212. {
  213. // dump error message (no segments found)
  214. $content_body = '<p>There were problems when trying to activate your account.</p>';
  215. $content_body .= '<p>if the problem persists please contact the support team at help@domain.com</p>';
  216.  
  217. $content = array('title'=>'Activation Error (NOSEG)','content'=>$content_body);
  218.  
  219. // Write form view to template engine
  220. $this->template->write_view('body', 'content/message', $content);
  221. // render page
  222. $this->template->render();
  223. }
  224. }
  225.  
  226. /*
  227. ***************************
  228. * FORM CALLBACK FUNCTIONS *
  229. ***************************
  230. */
  231.  
  232. // Check to see if username is in our database.
  233. function email_check($str)
  234. {
  235. // Query
  236. $query = $this->db->get_where("users", array('Email' => $str));
  237. // Row count from results
  238. if ($query->num_rows() > 0)
  239. {
  240. // Add error and return false
  241. $this->validation->set_message('email_check', 'E-mail address you have entered is allready in our database.');
  242. return FALSE;
  243. }
  244. else
  245. {
  246. // Return true with no error
  247. return TRUE;
  248. }
  249. }
  250.  
  251. // Check to see if the user exists and attempt a login.
  252. function check_login($email)
  253. {
  254. $password = md5($this->input->post('password'));
  255. if ($this->user_auth->try_login(array('Email'=>$email, 'Password'=>$password)))
  256. {
  257. return TRUE;
  258. }
  259. else
  260. {
  261. $this->validation->set_message('check_login', 'Your Email/Password do not match our records.');
  262. return FALSE;
  263. }
  264. }
  265. }
  266.  
  267. /* End of file user.php */
  268. /* Location: ./system/application/controllers/user.php */
Add Comment
Please, Sign In to add comment