PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/application/third_party/community_auth/controllers/Examples.php

https://gitlab.com/CurC/ProjectW
PHP | 421 lines | 227 code | 73 blank | 121 comment | 26 complexity | 642305dee29d29d2775e2154775bc772 MD5 | raw file
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. /**
  4. * Community Auth - Examples Controller
  5. *
  6. * Community Auth is an open source authentication application for CodeIgniter 3
  7. *
  8. * @package Community Auth
  9. * @author Robert B Gottier
  10. * @copyright Copyright (c) 2011 - 2016, Robert B Gottier. (http://brianswebdesign.com/)
  11. * @license BSD - http://www.opensource.org/licenses/BSD-3-Clause
  12. * @link http://community-auth.com
  13. */
  14. class Examples extends MY_Controller
  15. {
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. // Force SSL
  20. //$this->force_ssl();
  21. }
  22. // -----------------------------------------------------------------------
  23. /**
  24. * Demonstrate being redirected to login.
  25. * If you are logged in and request this method,
  26. * you'll see the message, otherwise you will be
  27. * shown the login form. Once login is achieved,
  28. * you will be redirected back to this method.
  29. */
  30. public function index()
  31. {
  32. if( $this->require_role('admin') )
  33. {
  34. echo $this->load->view('examples/page_header', '', TRUE);
  35. echo '<p>You are logged in!</p>';
  36. echo $this->load->view('examples/page_footer', '', TRUE);
  37. }
  38. }
  39. // -----------------------------------------------------------------------
  40. /**
  41. * Demonstrate an optional login.
  42. * Remember to add "examples/optional_login_test" to the
  43. * allowed_pages_for_login array in config/authentication.php.
  44. *
  45. * Notice that we are using verify_min_level to check if
  46. * a user is already logged in.
  47. */
  48. public function optional_login_test()
  49. {
  50. if( $this->verify_min_level(1) )
  51. {
  52. $page_content = '<p>Although not required, you are logged in!</p>';
  53. }
  54. elseif( $this->tokens->match && $this->optional_login() )
  55. {
  56. // Let Community Auth handle the login attempt ...
  57. }
  58. else
  59. {
  60. // Notice parameter set to TRUE, which designates this as an optional login
  61. $this->setup_login_form(TRUE);
  62. $page_content = '<p>You are not logged in, but can still see this page.</p>';
  63. $page_content .= $this->load->view('examples/login_form', '', TRUE);
  64. }
  65. echo $this->load->view('examples/page_header', '', TRUE);
  66. echo $page_content;
  67. echo $this->load->view('examples/page_footer', '', TRUE);
  68. }
  69. // -----------------------------------------------------------------------
  70. /**
  71. * Here we simply verify if a user is logged in, but
  72. * not enforcing authentication. The presence of auth
  73. * related variables that are not empty indicates
  74. * that somebody is logged in. Also showing how to
  75. * get the contents of the HTTP user cookie.
  76. */
  77. public function simple_verification()
  78. {
  79. $this->is_logged_in();
  80. echo $this->load->view('examples/page_header', '', TRUE);
  81. echo '<p>';
  82. if( ! empty( $this->auth_role ) )
  83. {
  84. echo $this->auth_role . ' logged in!<br />
  85. User ID is ' . $this->auth_user_id . '<br />
  86. Auth level is ' . $this->auth_level . '<br />
  87. Username is ' . $this->auth_username;
  88. if( $http_user_cookie_contents = $this->input->cookie( config_item('http_user_cookie_name') ) )
  89. {
  90. $http_user_cookie_contents = unserialize( $http_user_cookie_contents );
  91. echo '<br />
  92. <pre>';
  93. print_r( $http_user_cookie_contents );
  94. echo '</pre>';
  95. }
  96. }
  97. else
  98. {
  99. echo 'Nobody logged in.';
  100. }
  101. echo '</p>';
  102. echo $this->load->view('examples/page_footer', '', TRUE);
  103. }
  104. // -----------------------------------------------------------------------
  105. /**
  106. * Most minimal user creation. You will of course make your
  107. * own interface for adding users, and you may even let users
  108. * register and create their own accounts.
  109. *
  110. * The password used in the $user_data array needs to meet the
  111. * following default strength requirements:
  112. * - Must be at least 8 characters long
  113. * - Must have at least one digit
  114. * - Must have at least one lower case letter
  115. * - Must have at least one upper case letter
  116. * - Must not have any space, tab, or other whitespace characters
  117. * - No backslash, apostrophe or quote chars are allowed
  118. */
  119. public function create_user()
  120. {
  121. // Customize this array for your user
  122. $user_data = array(
  123. 'username' => 'skunkbot',
  124. 'passwd' => 'PepeLePew7',
  125. 'email' => 'skunkbot@example.com',
  126. 'auth_level' => '1', // 9 if you want to login @ examples/index.
  127. );
  128. $this->is_logged_in();
  129. echo $this->load->view('examples/page_header', '', TRUE);
  130. // Load resources
  131. $this->load->model('examples_model');
  132. $this->load->library('form_validation');
  133. $this->form_validation->set_data( $user_data );
  134. $validation_rules = array(
  135. array(
  136. 'field' => 'username',
  137. 'label' => 'username',
  138. 'rules' => 'max_length[12]|is_unique[' . config_item('user_table') . '.username]'
  139. ),
  140. array(
  141. 'field' => 'passwd',
  142. 'label' => 'passwd',
  143. 'rules' => 'trim|required|external_callbacks[model,formval_callbacks,_check_password_strength,TRUE]',
  144. ),
  145. array(
  146. 'field' => 'email',
  147. 'label' => 'email',
  148. 'rules' => 'required|valid_email|is_unique[' . config_item('user_table') . '.email]'
  149. ),
  150. array(
  151. 'field' => 'auth_level',
  152. 'label' => 'auth_level',
  153. 'rules' => 'required|integer|in_list[1,6,9]'
  154. )
  155. );
  156. $this->form_validation->set_rules( $validation_rules );
  157. if( $this->form_validation->run() )
  158. {
  159. $user_data['passwd'] = $this->authentication->hash_passwd($user_data['passwd']);
  160. $user_data['user_id'] = $this->examples_model->get_unused_id();
  161. $user_data['created_at'] = date('Y-m-d H:i:s');
  162. // If username is not used, it must be entered into the record as NULL
  163. if( empty( $user_data['username'] ) )
  164. {
  165. $user_data['username'] = NULL;
  166. }
  167. $this->db->set($user_data)
  168. ->insert(config_item('user_table'));
  169. if( $this->db->affected_rows() == 1 )
  170. echo '<h1>Congratulations</h1>' . '<p>User ' . $user_data['username'] . ' was created.</p>';
  171. }
  172. else
  173. {
  174. echo '<h1>User Creation Error(s)</h1>' . validation_errors();
  175. }
  176. echo $this->load->view('examples/page_footer', '', TRUE);
  177. }
  178. // -----------------------------------------------------------------------
  179. /**
  180. * This login method only serves to redirect a user to a
  181. * location once they have successfully logged in. It does
  182. * not attempt to confirm that the user has permission to
  183. * be on the page they are being redirected to.
  184. */
  185. public function login()
  186. {
  187. // Method should not be directly accessible
  188. if( $this->uri->uri_string() == 'examples/login')
  189. show_404();
  190. if( strtolower( $_SERVER['REQUEST_METHOD'] ) == 'post' )
  191. $this->require_min_level(1);
  192. $this->setup_login_form();
  193. $html = $this->load->view('examples/page_header', '', TRUE);
  194. $html .= $this->load->view('examples/login_form', '', TRUE);
  195. $html .= $this->load->view('examples/page_footer', '', TRUE);
  196. echo $html;
  197. }
  198. // --------------------------------------------------------------
  199. /**
  200. * Log out
  201. */
  202. public function logout()
  203. {
  204. $this->authentication->logout();
  205. redirect( secure_site_url( LOGIN_PAGE . '?logout=1') );
  206. }
  207. // --------------------------------------------------------------
  208. /**
  209. * User recovery form
  210. */
  211. public function recover()
  212. {
  213. // Load resources
  214. $this->load->model('examples_model');
  215. /// If IP or posted email is on hold, display message
  216. if( $on_hold = $this->authentication->current_hold_status( TRUE ) )
  217. {
  218. $view_data['disabled'] = 1;
  219. }
  220. else
  221. {
  222. // If the form post looks good
  223. if( $this->tokens->match && $this->input->post('email') )
  224. {
  225. if( $user_data = $this->examples_model->get_recovery_data( $this->input->post('email') ) )
  226. {
  227. // Check if user is banned
  228. if( $user_data->banned == '1' )
  229. {
  230. // Log an error if banned
  231. $this->authentication->log_error( $this->input->post('email', TRUE ) );
  232. // Show special message for banned user
  233. $view_data['banned'] = 1;
  234. }
  235. else
  236. {
  237. /**
  238. * Use the string generator to create a random string
  239. * that will be hashed and stored as the password recovery key.
  240. */
  241. $this->load->library('generate_string');
  242. $recovery_code = $this->generate_string->set_options(
  243. array( 'exclude' => array( 'char' ) )
  244. )->random_string(64)->show();
  245. // Update user record with recovery code and time
  246. $this->examples_model->update_user_raw_data(
  247. $user_data->user_id,
  248. array(
  249. 'passwd_recovery_code' => $this->authentication->hash_passwd($recovery_code),
  250. 'passwd_recovery_date' => date('Y-m-d H:i:s')
  251. )
  252. );
  253. $view_data['special_link'] = secure_anchor(
  254. 'examples/recovery_verification/' . $user_data->user_id . '/' . $recovery_code,
  255. secure_site_url( 'examples/recovery_verification/' . $user_data->user_id . '/' . $recovery_code ),
  256. 'target ="_blank"'
  257. );
  258. $view_data['confirmation'] = 1;
  259. }
  260. }
  261. // There was no match, log an error, and display a message
  262. else
  263. {
  264. // Log the error
  265. $this->authentication->log_error( $this->input->post('email', TRUE ) );
  266. $view_data['no_match'] = 1;
  267. }
  268. }
  269. }
  270. echo $this->load->view('examples/page_header', '', TRUE);
  271. echo $this->load->view('examples/recover_form', ( isset( $view_data ) ) ? $view_data : '', TRUE );
  272. echo $this->load->view('examples/page_footer', '', TRUE);
  273. }
  274. // --------------------------------------------------------------
  275. /**
  276. * Verification of a user by email for recovery
  277. *
  278. * @param int the user ID
  279. * @param string the passwd recovery code
  280. */
  281. public function recovery_verification( $user_id = '', $recovery_code = '' )
  282. {
  283. /// If IP is on hold, display message
  284. if( $on_hold = $this->authentication->current_hold_status( TRUE ) )
  285. {
  286. $view_data['disabled'] = 1;
  287. }
  288. else
  289. {
  290. // Load resources
  291. $this->load->model('examples_model');
  292. if(
  293. /**
  294. * Make sure that $user_id is a number and less
  295. * than or equal to 10 characters long
  296. */
  297. is_numeric( $user_id ) && strlen( $user_id ) <= 10 &&
  298. /**
  299. * Make sure that $recovery code is exactly 64 characters long
  300. */
  301. strlen( $recovery_code ) == 64 &&
  302. /**
  303. * Try to get a hashed password recovery
  304. * code and user salt for the user.
  305. */
  306. $recovery_data = $this->examples_model->get_recovery_verification_data( $user_id ) )
  307. {
  308. /**
  309. * Check that the recovery code from the
  310. * email matches the hashed recovery code.
  311. */
  312. if( $recovery_data->passwd_recovery_code == $this->authentication->check_passwd( $recovery_data->passwd_recovery_code, $recovery_code ) )
  313. {
  314. $view_data['user_id'] = $user_id;
  315. $view_data['username'] = $recovery_data->username;
  316. $view_data['recovery_code'] = $recovery_data->passwd_recovery_code;
  317. }
  318. // Link is bad so show message
  319. else
  320. {
  321. $view_data['recovery_error'] = 1;
  322. // Log an error
  323. $this->authentication->log_error('');
  324. }
  325. }
  326. // Link is bad so show message
  327. else
  328. {
  329. $view_data['recovery_error'] = 1;
  330. // Log an error
  331. $this->authentication->log_error('');
  332. }
  333. /**
  334. * If form submission is attempting to change password
  335. */
  336. if( $this->tokens->match )
  337. {
  338. $this->examples_model->recovery_password_change();
  339. }
  340. }
  341. echo $this->load->view('examples/page_header', '', TRUE);
  342. echo $this->load->view( 'examples/choose_password_form', $view_data, TRUE );
  343. echo $this->load->view('examples/page_footer', '', TRUE);
  344. }
  345. // --------------------------------------------------------------
  346. }
  347. /* End of file Examples.php */
  348. /* Location: /application/controllers/Examples.php */