/application/libraries/Quickauth.php

https://bitbucket.org/earnest-tekkies/sample-php-ci · PHP · 345 lines · 213 code · 39 blank · 93 comment · 32 complexity · 7da86d4f866cc7c2582ca8857dca35c0 MD5 · raw file

  1. <?php
  2. /*
  3. * @Package Quick Authentication Library
  4. * @author David Blencowe
  5. * @link http://www.syntaxmonster.net
  6. * @version 1.0.0
  7. * @since Version 1.0.0
  8. */
  9. class Quickauth
  10. {
  11. var $CI;
  12. var $_username;
  13. var $_language=1;// global variable for handling language
  14. var $_table = array(
  15. 'users' => 'sm_users',
  16. 'groups' => 'sm_user_groups'
  17. );
  18. function __construct()
  19. {
  20. $this->CI =& get_instance();
  21. $this->CI->load->helper('url');
  22. $this->CI->load->helper('email');
  23. $this->CI->load->helper('string');
  24. }
  25. function is_maintain(){
  26. $setting = $this->CI->db->get_where('tbl_config', array('maintenance_enabled' => 1))->row_object();
  27. if(is_object($setting) && $setting->maintenance_enabled == 1){
  28. redirect('maintenance');
  29. }
  30. }
  31. function is_maintain_mode(){
  32. $setting = $this->CI->db->get_where('sm_config', array('maintenance_enabled' => 1))->row_object();
  33. if(is_object($setting) && $setting->maintenance_enabled == 1){
  34. return true;
  35. }else{
  36. return false;
  37. }
  38. }
  39. function Quickauth()
  40. {
  41. self::__construct();
  42. }
  43. function has_role($only_for_administrator = true, $only_for_user = false){
  44. if ($this->CI->session->userdata('logged_in') == FALSE)
  45. {
  46. redirect('login');
  47. }
  48. else
  49. {
  50. if($this->CI->session->userdata('group_id') != 3){
  51. if($only_for_administrator){
  52. redirect('membros');
  53. }
  54. }else if($only_for_user){
  55. redirect('admin/pages');
  56. }
  57. }
  58. }
  59. function is_admin(){
  60. if($this->CI->session->userdata('group_id') == 1){
  61. return true;
  62. }else{
  63. return false;
  64. }
  65. }
  66. function is_supportor(){
  67. if($this->CI->session->userdata('group_id') == 2){
  68. return true;
  69. }else{
  70. return false;
  71. }
  72. }
  73. function redirect_out_admin(){
  74. redirect(base_url().'user/login');
  75. }
  76. // -----------------------------------------------------------------------------------------
  77. /**
  78. * Check the database too see if the user email that is passed to the function exists.
  79. * @param String $email
  80. * @param Integer $user_id , by default it will be zero
  81. * @return TRUE/FALSE
  82. */
  83. function _useremail_exists($email,$user_id=0){
  84. $this->CI->db->where('email', $email);
  85. //if user id provided
  86. if($user_id > 0){
  87. $this->CI->db->where('id <>', $user_id);
  88. }
  89. $this->CI->db->limit(1);
  90. $query = $this->CI->db->get($this->_table['users']);
  91. if ($query->num_rows() !== 1){
  92. return FALSE;
  93. }else{
  94. return TRUE;
  95. }
  96. }
  97. // -----------------------------------------------------------------------------------------
  98. /**
  99. * Check the config file to see if the current language.
  100. * If it does then it is set to the global $_username variable for later use
  101. *
  102. * @return $lan_id
  103. */
  104. function _get_language_code(){
  105. $lan_id = 0;
  106. $cur_lanhuage=$this->CI->config->item('admin_language');
  107. if($cur_lanhuage == 'english'){
  108. $lan_id = 1;
  109. $this->_language = $lan_id;
  110. }else if($cur_lanhuage == 'portuguese'){
  111. $lan_id = 2;
  112. $this->_language = $lan_id;
  113. }
  114. return $lan_id;
  115. }
  116. // -----------------------------------------------------------------------------------------
  117. /**
  118. * Used for restricting users to certain controllers and functions
  119. * by their user level.
  120. * @param String $restrict_to Name of the group
  121. * @return TRUE/Error
  122. */
  123. function restrict( $restrict_to = NULL, $redirect_to_login = FALSE )
  124. {
  125. if ( $restrict_to !== NULL)
  126. {
  127. if ($this->CI->session->userdata('logged_in') == TRUE)
  128. {
  129. $this->CI->db->where('id', $restrict_to);
  130. $query = $this->CI->db->get($this->_table['groups']);
  131. $level = $query->row_array();
  132. $users_level = $this->CI->session->userdata('group_id');
  133. if ($users_level >= $level['id'])
  134. {
  135. return TRUE;
  136. }
  137. else
  138. {
  139. redirect('/admin/');
  140. }
  141. }
  142. else
  143. {
  144. redirect("/admin/");
  145. }
  146. }
  147. else
  148. {
  149. // Page locked to everyone
  150. show_error($this->CI->lang->line('access_denied'));
  151. }
  152. }
  153. // -----------------------------------------------------------------------------------------
  154. /**
  155. * Check the database too see if the username that is passed to the function exists.
  156. * If it does then it is set to the global $_username variable for later use
  157. * @param String $username
  158. * @return TRUE/FALSE
  159. */
  160. function _username_exists( $username )
  161. {
  162. $this->CI->db->where('username', $username);
  163. $this->CI->db->limit(1);
  164. $query = $this->CI->db->get($this->_table['users']);
  165. if ($query->num_rows() !== 1)
  166. {
  167. return FALSE;
  168. }
  169. else
  170. {
  171. $this->_username = $username;
  172. return TRUE;
  173. }
  174. }
  175. // -----------------------------------------------------------------------------------------
  176. /**
  177. * Encrypts the submitted password and then checks it in the database
  178. * using the value of the global $_username variable. If True is returned
  179. * then the username and password submitted by the user are correct and they
  180. * should then get logged in (See login function)
  181. * @param String $password
  182. * @return TRUE/FALSE
  183. */
  184. function _check_correct_password( $password )
  185. {
  186. $this->CI->db->select('password');
  187. $this->CI->db->where('username', $this->_username);
  188. $this->CI->db->limit(1);
  189. $query = $this->CI->db->get($this->_table['users']);
  190. $result = $query->row();
  191. if ($result->password == $this->encrypt($password))
  192. {
  193. return TRUE;
  194. }
  195. else
  196. {
  197. return FALSE;
  198. }
  199. }
  200. // -----------------------------------------------------------------------------------------
  201. /**
  202. * Returns the number of characters in a string after it has been trimemd for
  203. * whitespace.
  204. * @param String $string
  205. * @return Int
  206. */
  207. function check_string_length( $string )
  208. {
  209. $string = trim($string);
  210. return strlen($string);
  211. }
  212. // -----------------------------------------------------------------------------------------
  213. /**
  214. * This function will encrypt any data passed to it.
  215. * It is primarily used for encrypting passwords before
  216. * querying the database.
  217. * @param String $data
  218. * @return String
  219. */
  220. function encrypt( $data )
  221. {
  222. if ($this->CI->config->item('encryption_key') !== NULL)
  223. {
  224. return sha1($this->CI->config->item('encryption_key').$data);
  225. }
  226. else
  227. {
  228. show_error('Please set an encryption key in your config file. <a href="javascript:history.back();">back</a>');
  229. }
  230. }
  231. // -----------------------------------------------------------------------------------------
  232. /**
  233. * Check if a user is logged in
  234. *
  235. * @access public
  236. * @param string
  237. * @return bool
  238. */
  239. function logged_in()
  240. {
  241. return $this->CI->session->userdata('logged_in');
  242. }
  243. // -----------------------------------------------------------------------------------------
  244. /**
  245. * Log a user out (destroy all session variables)
  246. *
  247. * @access public
  248. */
  249. function logout()
  250. {
  251. $this->CI->session->sess_destroy();
  252. }
  253. // -----------------------------------------------------------------------------------------
  254. /**
  255. * This function will get the currently selected template theme
  256. *
  257. * @access public
  258. * @return string template name
  259. */
  260. function get_current_template_css(){
  261. $template_id = 0;
  262. $this->CI->db->select('template');
  263. $query = $this->CI->db->get('sm_config');
  264. $result = $query->row();
  265. // check the sm_config for seleted template if template set the get template id from there else set template is as 1
  266. if ($query->num_rows() !== 1){
  267. $template_id = 1;
  268. }else{
  269. $template_id = $result->template;
  270. }
  271. // if template id is zero then make it as one for getting default template
  272. if($template_id < 1){
  273. $template_id = 1;
  274. }
  275. //get the template name from table sm_template_css
  276. $this->CI->db->select('template_css');
  277. $this->CI->db->where('id', $template_id);
  278. $query = $this->CI->db->get('sm_template_css');
  279. $result = $query->row();
  280. if ($query->num_rows() !== 1){
  281. return 'none';
  282. }else{
  283. return $result->template_css;
  284. }
  285. }
  286. /**
  287. * This function will get the currently copy right text
  288. *
  289. * @access public
  290. * @return string copy right text
  291. */
  292. function get_copy_right_text(){
  293. $language_id = $this->_get_language_code();
  294. $this->CI->db->select('copy_right_text');
  295. $this->CI->db->where('language', $language_id);
  296. $query = $this->CI->db->get('sm_config');
  297. $result = $query->row();
  298. if ($query->num_rows() !== 1){
  299. return 'none';
  300. }else{
  301. return $result->copy_right_text;
  302. }
  303. }
  304. }
  305. /* End of file Quickauth.php */
  306. /* Location: ./application/libraries/Quickauth.php */