PageRenderTime 62ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/application/main.php

https://bitbucket.org/jlc08k/cci-directory
PHP | 422 lines | 253 code | 100 blank | 69 comment | 23 complexity | 5369833cb1bb1074e5593d76540966a3 MD5 | raw file
  1. <?php
  2. class Main extends Controller {
  3. function Main()
  4. {
  5. parent::Controller();
  6. $this->load->library('session');
  7. $this->load->model('directory_model');
  8. $this->load->helper(array('form', 'url'));
  9. $this->load->library('form_validation');
  10. }
  11. function index()
  12. {
  13. $this->load->model('directory_model');
  14. // get all faculty data to display
  15. $data = array();
  16. $data['directory'] = $this->directory_model->get_faculty();
  17. // get unique user names
  18. $users = $this->directory_model->get_users();
  19. // extract user names into numbered array
  20. $user = array();
  21. $i = 0;
  22. foreach ($users as $un){
  23. foreach ($un as $key => $val){
  24. $user[$i] = $val;
  25. $i = $i + 1;
  26. }
  27. }
  28. $data['users'] = $user;
  29. // load main directory page
  30. $this->load->view('header');
  31. $this->load->view('directory', $data);
  32. $this->load->view('footer');
  33. }
  34. function profile($id)
  35. {
  36. // get all data to be displayed on profile page
  37. $data = array();
  38. $data['profile'] = $this->directory_model->get_profile($id);
  39. // load main profile page
  40. $this->load->view('header');
  41. $this->load->view('profile', $data);
  42. $this->load->view('footer');
  43. }
  44. function login()
  45. {
  46. // run if login data has been submitted
  47. if (isset($_POST['un']) && isset($_POST['pw']))
  48. {
  49. // validate password
  50. $this->form_validation->set_rules('un', 'Username', 'strip_tags|required|max_length[25]|min_length[2]|alpha_dash');
  51. $this->form_validation->set_rules('pw', 'The Password', 'strip_tags|required|max_length[100]|min_length[2]');
  52. if ($this->form_validation->run() === TRUE)
  53. {
  54. // check login against database
  55. $result = $this->directory_model->check_login($_POST['un'], $_POST['pw']);
  56. if ($result === TRUE)
  57. {
  58. // set session data
  59. $this->session->set_userdata('loggedIn', TRUE);
  60. $this->session->set_userdata('user', $_POST['un']);
  61. // if this is users first login, make them change password and username
  62. if ($_POST['un'] === $_POST['pw'])
  63. $this->update_password();
  64. // if not first login load user's profile
  65. else
  66. $this->profile($_POST['un']);
  67. }
  68. // if incorrect login, reload website
  69. else
  70. {
  71. $this->load->view('header');
  72. $this->load->view('login_fail');
  73. $this->load->view('login');
  74. $this->load->view('footer');
  75. }
  76. }
  77. // if form doesn't validate, display error messages
  78. else
  79. {
  80. $this->load->view('header');
  81. $this->load->view('login');
  82. $this->load->view('footer');
  83. }
  84. }
  85. // if form has not been submitted, load main login page
  86. else
  87. {
  88. $this->load->view('header');
  89. $this->load->view('login');
  90. $this->load->view('footer');
  91. }
  92. }
  93. function logout()
  94. {
  95. // kill all user data and session, then load main directory page
  96. $this->session->unset_userdata('loggedIn');
  97. $this->session->unset_userdata('user');
  98. $this->session->sess_destroy();
  99. $this->index();
  100. }
  101. function register()
  102. {
  103. // run if form has been submitted
  104. if (isset($_POST['fname']) && isset($_POST['lname']))
  105. {
  106. // validation rules. makes sure good values are submitted
  107. $this->form_validation->set_rules('fname', 'First name', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean');
  108. $this->form_validation->set_rules('lname', 'Last name', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean');
  109. $this->form_validation->set_rules('title', 'Title', 'strip_tags|trim|required|max_length[100]|min_length[2]|xxs_clean');
  110. $this->form_validation->set_rules('phone', 'Phone Number', 'strip_tags|trim|required|max_length[15]|min_length[10]|xxs_clean');
  111. $this->form_validation->set_rules('email', 'Email', 'strip_tags|callback_good_email|required|max_length[50]|min_length[2]|valid_email');
  112. $this->form_validation->set_rules('location', 'Location', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean');
  113. $this->form_validation->set_rules('department', 'Department', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean');
  114. $this->form_validation->set_rules('bio', 'Biography', 'strip_tags|trim|max_length[1000]|xxs_clean');
  115. $this->form_validation->set_rules('cv', 'CV', 'strip_tags|trim|max_length[100]|min_length[2]|xxs_clean');
  116. $this->form_validation->set_rules('homepage', 'Home Page', 'strip_tags|trim|max_length[100]|min_length[2]|xxs_clean');
  117. $this->form_validation->set_rules('username', 'Username', 'strip_tags|callback_username_unique|trim|max_length[50]|min_length[2]|xxs_clean');
  118. $this->form_validation->set_rules('pw1', 'Password', 'strip_tags|required|max_length[50]|min_length[2]|matches[pw2]');
  119. $this->form_validation->set_rules('pw2', 'Confirm Password', 'strip_tags|required|max_length[50]|min_length[2]');
  120. // if form passes validation
  121. if ($this->form_validation->run() === TRUE)
  122. {
  123. // extracts post info into array for simple insertion to database
  124. $memberInfo = array(
  125. "fname" => $_POST['fname'],
  126. "lname" => $_POST['lname'],
  127. "title" => $_POST['title'],
  128. "phone" => $_POST['phone'],
  129. "email" => $_POST['email'],
  130. "location" => $_POST['location'],
  131. "department" => $_POST['department'],
  132. "bio" => $_POST['bio'],
  133. "homepage" => $_POST['homepage'],
  134. "cv" => $_POST['cv'],
  135. "user" => $_POST['username'],
  136. "password" => md5($_POST['pw1']));
  137. // attempt to insert values into database
  138. $result = $this->directory_model->register_new($memberInfo);
  139. // if insert is succesful, log user in and load profile
  140. if ($result === TRUE)
  141. {
  142. $this->session->set_userdata('user', $_POST['username']);
  143. $this->session->set_userdata('loggedIn', TRUE);
  144. $this->profile($_POST['username']);
  145. }
  146. // if there is a problem inserting load main directory page again
  147. else
  148. $this->index ();
  149. }
  150. // if the form does not validate, reload with validation errors
  151. else
  152. {
  153. $this->load->view('header');
  154. $this->load->view('register');
  155. $this->load->view('footer');
  156. }
  157. // end of post if
  158. }
  159. // if no form has been submitted, load form
  160. else
  161. {
  162. $this->load->view('header');
  163. $this->load->view('register');
  164. $this->load->view('footer');
  165. }
  166. }
  167. function update_password ()
  168. {
  169. // if there is no user logged in, load main directory page
  170. if (($this->session->userdata('loggedIn') != TRUE))
  171. $this->index ();
  172. else{
  173. // if there has been attempt to change password
  174. if (isset($_POST['pw']) && isset($_POST['pw2']))
  175. {
  176. // validate password
  177. $this->form_validation->set_rules('pw', 'Password', 'strip_tags|required|max_length[50]|min_length[2]|matches[pw2]');
  178. $this->form_validation->set_rules('pw2', 'Confirm Password', 'strip_tags|required|max_length[50]|min_length[2]');
  179. if ($this->form_validation->run() === TRUE)
  180. {
  181. // encrypt password and send to database
  182. $pw = md5($_POST['pw']);
  183. $result = $this->directory_model->update_password($this->session->userdata('user'), $pw);
  184. // if password update is succesful, load user's profile
  185. if ($result === TRUE)
  186. $this->profile($this->session->userdata('user'));
  187. // if there is a problem updating password load main directory page
  188. else
  189. $this->index();
  190. }
  191. // if form doesn't validate, reload form with validation errors
  192. else
  193. {
  194. $this->load->view('header');
  195. $this->load->view('update_password');
  196. $this->load->view('footer');
  197. }
  198. }
  199. // if the form hasn't been submitted, load form
  200. else
  201. {
  202. $this->load->view('header');
  203. $this->load->view('update_password');
  204. $this->load->view('footer');
  205. }
  206. }
  207. }
  208. function update_profile ()
  209. {
  210. // if there is no user logged in, load main directory page
  211. if (($this->session->userdata('loggedIn') != TRUE))
  212. $this->index ();
  213. else{
  214. // if form data has been submitted
  215. if (isset($_POST['fname']) && isset($_POST['lname']))
  216. {
  217. // validate form info
  218. $this->form_validation->set_rules('fname', 'First name', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean');
  219. $this->form_validation->set_rules('lname', 'Last name', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean|');
  220. $this->form_validation->set_rules('title', 'Title', 'strip_tags|trim|required|max_length[100]|min_length[2]|xxs_clean');
  221. $this->form_validation->set_rules('phone', 'Phone Number', 'strip_tags|trim|required|max_length[15]|min_length[10]|xxs_clean');
  222. $this->form_validation->set_rules('email', 'Email', 'strip_tags|required|max_length[100]|min_length[2]|valid_email');
  223. $this->form_validation->set_rules('location', 'Location', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean');
  224. $this->form_validation->set_rules('department', 'Department', 'strip_tags|trim|required|max_length[100]|min_length[2]|xxs_clean');
  225. $this->form_validation->set_rules('bio', 'Biography', 'strip_tags|trim|max_length[1000]|xxs_clean');
  226. $this->form_validation->set_rules('cv', 'CV', 'strip_tags|trim|max_length[75]|min_length[2]|xxs_clean');
  227. $this->form_validation->set_rules('homepage', 'Home Page', 'strip_tags|trim|max_length[75]|min_length[2]|xxs_clean');
  228. if ($this->form_validation->run() === TRUE)
  229. {
  230. // send data to database if validation is succesful
  231. $result = $this->directory_model->update_profile($_POST);
  232. // if query is succesful load user's profile
  233. if ($result === TRUE)
  234. $this->profile($_POST['user']);
  235. // if there is a database problem load main directory page
  236. else
  237. $this->index();
  238. }
  239. // if validation is not successful reload form with validation errors
  240. else
  241. {
  242. // convert post data to array to refill form values
  243. // the profile index name cannot change (matches same name when data is gotten from database
  244. $data = array();
  245. $profile = $_POST;
  246. $data['profile'] = $profile;
  247. $this->load->view('header');
  248. $this->load->view('update_profile', $data);
  249. $this->load->view('footer');
  250. }
  251. }
  252. // this will be ran if the form has not been submitted
  253. else
  254. {
  255. // get profile data and convert to array (loads here to be in scope throughout function)
  256. $rawProfile = $this->directory_model->get_profile($this->session->userdata('user'));
  257. $profile = array();
  258. foreach ($rawProfile as $rp)
  259. {
  260. foreach ($rp as $key => $val)
  261. {
  262. $profile[$key] = $val;
  263. }
  264. }
  265. $data['profile'] = $profile;
  266. // load form with database info as form values
  267. $this->load->view('header');
  268. $this->load->view('update_profile', $data);
  269. $this->load->view('footer');
  270. }
  271. }
  272. }
  273. /**
  274. *
  275. * Checks to see if the string passed to it is a unique value in the
  276. * database. If it is unique it returns true, otherwise it will
  277. * set the form_validation error message and return false
  278. *
  279. * @param string $username
  280. * @return bool
  281. */
  282. function username_unique($username)
  283. {
  284. $users = $this->directory_model->get_users();
  285. // Is there a row with this email?
  286. foreach ($users as $user)
  287. {
  288. foreach ($user as $key => $value)
  289. {
  290. // if this username exists return false and set error message
  291. if ($username === $value)
  292. {
  293. $this->form_validation->set_message('username_unique', 'That User Name is already used.');
  294. return FALSE;
  295. }
  296. }
  297. }
  298. // usename is unique, don't return an error.
  299. return TRUE;
  300. }
  301. /**
  302. * Checks that email address ends in @cci.fsu.edu. If it does it will
  303. * return true, if not it will set form_validation error message and return
  304. * false.
  305. *
  306. * @param string $email
  307. * @return bool
  308. */
  309. function good_email($email)
  310. {
  311. // if email does not end in @cci.fsu.edu set error and return false
  312. if (substr($email, -12, 12) !== "@cci.fsu.edu")
  313. {
  314. $this->form_validation->set_message('good_email', 'You must use a valid @cci.fsu.edu email address.');
  315. return FALSE;
  316. }
  317. else
  318. return TRUE;
  319. }
  320. }
  321. /* End of file welcome.php */
  322. /* Location: ./system/application/controllers/welcome.php */