Advertisement
Guest User

upload code

a guest
Jan 21st, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.86 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. /**
  5. * User class.
  6. *
  7. * @extends CI_Controller
  8. */
  9. class User extends CI_Controller {
  10.  
  11. /**
  12. * __construct function.
  13. *
  14. * @access public
  15. * @return void
  16. */
  17. public function __construct() {
  18.  
  19. parent::__construct();
  20. $this->load->library(array('session'));
  21. $this->load->helper(array('url'));
  22. $this->load->model('user_model');
  23. }
  24.  
  25.  
  26. public function index($username = false) {
  27.  
  28. if ($username === false) {
  29. redirect(base_url());
  30. return;
  31. }
  32.  
  33. // create the data object
  34. $data = new stdClass();
  35. $data->title = "$username Profile";
  36.  
  37. // load the forum model
  38. $this->load->model('forum_model');
  39.  
  40. // get user id from username
  41. $user_id = $this->user_model->get_user_id_from_username($username);
  42.  
  43. // create the user object
  44. $user = $this->user_model->get_user($user_id);
  45. $user->count_topics = $this->user_model->count_user_topics($user_id);
  46. $user->count_posts = $this->user_model->count_user_posts($user_id);
  47. $user->latest_post = $this->user_model->get_user_last_post($user_id);
  48. if ($user->latest_post !== null) {
  49. $user->latest_post->topic = $this->forum_model->get_topic($user->latest_post->topic_id);
  50. $user->latest_post->topic->forum = $this->forum_model->get_forum($user->latest_post->topic->forum_id);
  51. $user->latest_post->topic->permalink = base_url($user->latest_post->topic->forum->slug . '/' . $user->latest_post->topic->slug);
  52. } else {
  53. $user->latest_post = new stdClass();
  54. $user->latest_post->created_at = $user->username . ' has not posted yet';
  55. }
  56. $user->latest_topic = $this->user_model->get_user_last_topic($user_id);
  57. if ($user->latest_topic !== null) {
  58. $user->latest_topic->forum = $this->forum_model->get_forum($user->latest_topic->forum_id);
  59. $user->latest_topic->permalink = base_url($user->latest_topic->forum->slug . '/' . $user->latest_topic->slug);
  60. } else {
  61. $user->latest_topic = new stdClass();
  62. $user->latest_topic->title = $user->username . ' has not started a topic yet';
  63. }
  64.  
  65. // create breadcrumb
  66. $breadcrumb = '<ol class="breadcrumb">';
  67. $breadcrumb .= '<li><a href="' . base_url() . '">Home</a></li>';
  68. $breadcrumb .= '<li class="active">' . $username . '</li>';
  69. $breadcrumb .= '</ol>';
  70.  
  71. // create a button to permit profile edition
  72. $edit_button = '<a href="' . base_url('user/' . $user->username . '/edit') . '" class="btn btn-xs btn-success">Edit your profile</a>';
  73.  
  74. // assign created objects to the data object
  75. $data->user = $user;
  76. $data->breadcrumb = $breadcrumb;
  77. if (isset($_SESSION['username']) && $_SESSION['username'] === $username) {
  78. // user is on his own profile
  79. $data->edit_button = $edit_button;
  80. } else {
  81. // user is not on his own profile
  82. $data->edit_button = null;
  83. }
  84.  
  85. $this->template->load('user/profile/profile', $data);
  86. }
  87.  
  88. /**
  89. * register function.
  90. *
  91. * @access public
  92. * @return void
  93. */
  94. public function register() {
  95.  
  96. // create the data object
  97. $data=array(
  98. 'title' => 'Register'
  99. );
  100.  
  101. // load form helper and validation library
  102. $this->load->helper('form');
  103. $this->load->library('form_validation');
  104.  
  105. // set validation rules
  106. $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[4]|is_unique[users.username]', array('is_unique' => 'This username already exists. Please choose another one.'));
  107. $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
  108. $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
  109. $this->form_validation->set_rules('password_confirm', 'Confirm Password', 'trim|required|min_length[6]|matches[password]');
  110.  
  111. if ($this->form_validation->run() === false) {
  112.  
  113. // validation not ok, send validation errors to the view
  114. $this->template->load('register',$data);
  115.  
  116. } else {
  117.  
  118. // set variables from the form
  119. $username = $this->input->post('username');
  120. $email = $this->input->post('email');
  121. $password = $this->input->post('password');
  122.  
  123. if ($this->user_model->create_user($username, $email, $password)) {
  124.  
  125. // user creation ok
  126. $this->template->load('register_success',$data);
  127.  
  128. } else {
  129.  
  130. // user creation failed, this should never happen
  131. $data['error'] = 'There was a problem creating your new account. Please try again.';
  132.  
  133. // send error to the view
  134. $this->template->load('register',$data);
  135.  
  136. }
  137.  
  138. }
  139.  
  140. }
  141.  
  142. /**
  143. * login function.
  144. *
  145. * @access public
  146. * @return void
  147. */
  148. public function login() {
  149.  
  150. // create the data object
  151. $data=array(
  152. 'title' => 'Login'
  153. );
  154.  
  155. // load form helper and validation library
  156. $this->load->helper('form');
  157. $this->load->library('form_validation');
  158.  
  159. // set validation rules
  160. $this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric');
  161. $this->form_validation->set_rules('password', 'Password', 'required');
  162.  
  163. if ($this->form_validation->run() == false) {
  164.  
  165. // validation not ok, send validation errors to the view
  166. $this->template->load('login',$data);
  167.  
  168. } else {
  169.  
  170. // set variables from the form
  171. $username = $this->input->post('username');
  172. $password = $this->input->post('password');
  173.  
  174. if ($this->user_model->resolve_user_login($username, $password)) {
  175.  
  176. $user_id = $this->user_model->get_user_id_from_username($username);
  177. $user = $this->user_model->get_user($user_id);
  178.  
  179. // set session user datas
  180. $_SESSION['user_id'] = (int)$user->id;
  181. $_SESSION['username'] = (string)$user->username;
  182. $_SESSION['logged_in'] = (bool)true;
  183.  
  184. // user login ok
  185. $this->template->load('login_success',$data);
  186.  
  187. } else {
  188.  
  189. // login failed
  190. $data['error'] = 'Wrong username or password.';
  191.  
  192. // send error to the view
  193. $this->template->load('login',$data);
  194.  
  195. }
  196.  
  197. }
  198.  
  199. }
  200.  
  201. /**
  202. * logout function.
  203. *
  204. * @access public
  205. * @return void
  206. */
  207. public function logout() {
  208.  
  209. // create the data object
  210. $data=array(
  211. 'title' => 'Logout'
  212. );
  213.  
  214. if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
  215.  
  216. // remove session datas
  217. foreach ($_SESSION as $key => $value) {
  218. unset($_SESSION[$key]);
  219. }
  220.  
  221. // user logout ok
  222. $this->template->load('logout_success',$data);
  223.  
  224. } else {
  225.  
  226. // there user was not logged in, we cannot logged him out,
  227. // redirect him to site root
  228. redirect('/');
  229.  
  230. }
  231.  
  232. }
  233. public function forget()
  234. {
  235. $data=array(
  236. 'title' => 'Forgot'
  237. );
  238. if (isset($_GET['info'])) {
  239. $data['info'] = $_GET['info'];
  240. }
  241. if (isset($_GET['error'])) {
  242. $data['error'] = $_GET['error'];
  243. }
  244. $this->template->load('login_forget',$data);
  245. }
  246.  
  247. public function doforget()
  248. {
  249. $this->load->helper('url');
  250. $email= $_POST['email'];
  251. $q = $this->db->query("select * from users where email=?",Array($email));
  252. $r = $q->result();
  253. if ($q->num_rows() > 0) {
  254. $user=$r[0];
  255. $this->resetpassword($user);
  256. $info= "Password has been reset and has been sent to email id: ". $email;
  257. redirect('/Login/forget?info=' . $info, 'refresh');
  258. } else {
  259. $error= "The email id you entered not found on our database ";
  260. redirect('/Login/forget?error=' . $error, 'refresh');
  261. }
  262. }
  263. /**
  264. * edit function.
  265. *
  266. * @access public
  267. * @param mixed $username (default: false)
  268. * @return void
  269. */
  270. public function edit($username = false) {
  271.  
  272. // a user cann only edit his own profile
  273. if ($username === false || $username !== $_SESSION['username']) {
  274. redirect(base_url());
  275. return;
  276. }
  277.  
  278. // create the data object
  279. $data = new stdClass();
  280. $data->title = "Edit Profile";
  281.  
  282. // load form helper and form validation library
  283. $this->load->helper('form');
  284. $this->load->library('form_validation');
  285.  
  286. // form validation
  287. $password_required_if = $this->input->post('password') ? '|required' : ''; // if there is something on password input, current password is required
  288. $this->form_validation->set_rules('username', 'Username', 'trim|min_length[4]|max_length[20]|alpha_numeric|is_unique[users.username]', array('is_unique' => 'This username already exists. Please choose another username.'));
  289. $this->form_validation->set_rules('email', 'Email', 'trim|valid_email|is_unique[users.email]', array('is_unique' => 'The email you entered already exists in our database.'));
  290. if($this->input->post('password') AND $this->input->post('current_password') AND $this->input->post('password_confirm')){
  291. $this->form_validation->set_rules('current_password', 'Current Password', 'trim' . $password_required_if . '|callback_verify_current_password');
  292.  
  293. $this->form_validation->set_rules('password', 'New Password', 'trim|min_length[6]|matches[password_confirm]');
  294. $this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'trim|min_length[6]');
  295. }
  296. // get the user object
  297. $user_id = $this->user_model->get_user_id_from_username($username);
  298. $user = $this->user_model->get_user($user_id);
  299.  
  300. // create breadcrumb
  301. $breadcrumb = '<ol class="breadcrumb">';
  302. $breadcrumb .= '<li><a href="' . base_url() . '">Home</a></li>';
  303. $breadcrumb .= '<li><a href="' . base_url('user/' . $username) . '">' . $username . '</a></li>';
  304. $breadcrumb .= '<li class="active">Edit</li>';
  305. $breadcrumb .= '</ol>';
  306.  
  307. // assign objects to the data object
  308. $data->user = $user;
  309. $data->breadcrumb = $breadcrumb;
  310.  
  311. if ($this->form_validation->run() === false) {
  312.  
  313. // validation not ok, send validation errors to the view
  314. $this->template->load('user/profile/edit', $data);
  315.  
  316. } else {
  317.  
  318. $user_id = $_SESSION['user_id'];
  319. $update_data = [];
  320.  
  321. if ($this->input->post('username') != '') {
  322. $update_data['username'] = $this->input->post('username');
  323. }
  324. if ($this->input->post('email') != '') {
  325. $update_data['email'] = $this->input->post('email');
  326. }
  327. if ($this->input->post('password') != '') {
  328. $update_data['password'] = $this->input->post('password');
  329. }
  330.  
  331. // avatar upload
  332. if (isset($_FILES['userfile']['name']) && !empty($_FILES['userfile']['name'])) {
  333.  
  334. // setup upload configuration and load upload library
  335. $config['upload_path'] = './uploads/avatars/';
  336. $config['allowed_types'] = 'gif|jpg|png';
  337. $config['max_size'] = 2048;
  338. $config['max_width'] = 1024;
  339. $config['max_height'] = 1024;
  340. $config['file_ext_tolower'] = true;
  341. $config['encrypt_name'] = true;
  342. //get_instance()->load->library('upload', $config);
  343. $this->load->library('upload');
  344. $this->upload->initialize($config);
  345.  
  346. if (!$this->upload->do_upload()) {
  347.  
  348. // upload NOT ok
  349. $error = array('error' => $this->upload->display_errors());
  350. $this->load->view('upload_form', $error);
  351. } else {
  352.  
  353. // Upload ok send name to $updated_data
  354. $update_data['avatar'] = $this->upload->data('file_name');
  355.  
  356. }
  357.  
  358. }
  359.  
  360. // if everything is ok
  361. if ($this->user_model->update_user($user_id, $update_data)) {
  362.  
  363. // if username change, update session
  364. if(isset($update_data['username'])) {
  365. $_SESSION['username'] = $update_data['username'];
  366. if ($this->input->post('username') != '') {
  367. // a little hook to send success message the new profil edit url if the username was updated
  368. $_SESSION['flash'] = 'Your profile has been successfully updated!';
  369. }
  370. }
  371.  
  372. // fix the fact that a new avatar was not shown until page refresh
  373. if(isset($update_data['avatar'])) {
  374. $data->user->avatar = $update_data['avatar'];
  375. }
  376.  
  377. if ($this->input->post('username') != '') {
  378.  
  379. // redirect to the new profile edit url
  380. redirect(base_url('user/' . $update_data['username'] . '/edit'));
  381.  
  382. } else {
  383.  
  384. // create a success message
  385. $data->success = 'Your profile has been successfully updated!';
  386.  
  387. // send success message to the views
  388. $this->template->load('user/profile/edit', $data);
  389.  
  390. }
  391.  
  392. } else {
  393.  
  394. // update user not ok : this should never happen
  395. $data->error = 'There was a problem updating your account. Please try again.';
  396.  
  397. //send errors to the views
  398. $this->template->load('user/profile/edit', $data);
  399.  
  400. }
  401.  
  402. }
  403.  
  404. }
  405.  
  406. /**
  407. * delete function.
  408. *
  409. * @access public
  410. * @param mixed $username (default: false)
  411. * @return void
  412. */
  413. public function delete($username = false) {
  414.  
  415. // a user cann only delete his own profile and must be logged in
  416. if ($username == false || !isset($_SESSION['username']) || $username !== $_SESSION['username']) {
  417. redirect(base_url());
  418. return;
  419. }
  420.  
  421. // create the data object
  422. $data = new stdClass();
  423. $data->title = "Delete Account";
  424.  
  425. if ($_SESSION['username'] === $username) {
  426.  
  427. // create breadcrumb
  428. $breadcrumb = '<ol class="breadcrumb">';
  429. $breadcrumb .= '<li><a href="' . base_url() . '">Home</a></li>';
  430. $breadcrumb .= '<li><a href="' . base_url('user/' . $username) . '">' . $username . '</a></li>';
  431. $breadcrumb .= '<li class="active">Delete</li>';
  432. $breadcrumb .= '</ol>';
  433.  
  434. $user_id = $this->user_model->get_user_id_from_username($username);
  435. $data->user = $this->user_model->get_user($user_id);
  436. $data->breadcrumb = $breadcrumb;
  437.  
  438. if ($this->user_model->delete_user($user_id)) {
  439.  
  440. $data->success = 'Your user account has been successfully deleted. Bye bye :(';
  441.  
  442. // user delete ok, load views
  443. $this->template->load('user/profile/delete', $data);
  444.  
  445. } else {
  446.  
  447. // user delete not ok, this should never happen
  448. $data->error = 'There was a problem deleting your user account. Please contact an administrator.';
  449.  
  450. // send errors to the views
  451. $this->template->load('user/profile/edit', $data);
  452.  
  453. }
  454.  
  455. } else {
  456.  
  457. // a user cann only delete his own profile and must be logged in
  458. redirect(base_url());
  459. return;
  460.  
  461. }
  462.  
  463. }
  464. private function resetpassword($user)
  465. {
  466. date_default_timezone_set('GMT');
  467. $this->load->helper('string');
  468. $password= random_string('alnum', 16);
  469. $this->db->where('id', $user->id);
  470. $this->db->update('users',array('password' => $this->user_model->hash_password($password)));
  471. $this->load->library('email');
  472. $this->email->from('withussocialize@gmail.com', 'Socializewithus');
  473. $this->email->to($user->email);
  474. $this->email->subject('Password reset');
  475. $this->email->message('You have requested the new password, Here is you new password:'. $password);
  476. $this->email->send();
  477. }
  478. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement