PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/application/helpers/acl_check_helper.php

https://github.com/tupkung/Behat_CI_Test
PHP | 321 lines | 229 code | 45 blank | 47 comment | 69 complexity | fe97a475ea2734fac5ea0815ed8b1fff MD5 | raw file
  1. <?php
  2. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  3. /*
  4. if ( ! function_exists('acl_check'))
  5. {
  6. function acl_check($uri = '', $user_gids = '')
  7. {
  8. $sql = "SELECT uri, groups FROM acls WHERE uri = ?";
  9. $qret = $this->dbr->query($sql, $uri);
  10. if ($qret->num_rows() <= 0) {
  11. return TRUE;
  12. }
  13. $row = $this->dbr->row();
  14. $acl_gids = $row->groups;
  15. $qret->free_result();
  16. $user_gids_array = explode(',', $user_gids);
  17. $tok = strtok($acl_gids, ';');
  18. while ( $tok !== FALSE ) {
  19. $acl_gids_array = explode(',', $tok);
  20. $result = array_diff($acl_gids_array, $user_gids_array);
  21. if ( empty($result) ) {
  22. return TRUE;
  23. }
  24. $tok = strtok(';');
  25. }
  26. return FALSE;
  27. }
  28. }
  29. */
  30. if ( ! function_exists('getCurrPath'))
  31. {
  32. function getCurrPath()
  33. {
  34. $CI =& get_instance();
  35. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  36. $CI->debug_log->write('function getCurrPath():', $CI->debug, $CI->debug_file);
  37. }
  38. /*
  39. * DPW: The following URI checks are for the testing environment.
  40. */
  41. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  42. $CI->debug_log->write('getCurrPath(): checking $CI->uri->uri_string = ' . print_r($CI->uri->uri_string, 1), $CI->debug, $CI->debug_file);
  43. $CI->debug_log->write('getCurrPath(): checking $CI->uri->segments = ' . print_r($CI->uri->segments, 1), $CI->debug, $CI->debug_file);
  44. $CI->debug_log->write('getCurrPath(): checking $CI->uri->rsegments = ' . print_r($CI->uri->rsegments, 1), $CI->debug, $CI->debug_file);
  45. }
  46. if ( $CI->uri->uri_string == '' ) {
  47. $CI->uri->uri_string = $_SERVER['REQUEST_URI'];
  48. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  49. $CI->debug_log->write('getCurrPath(): uri->uri_string was empty. fetched = ' . $CI->uri->uri_string, $CI->debug, $CI->debug_file);
  50. }
  51. }
  52. else {
  53. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  54. $CI->debug_log->write('getCurrPath(): uri->uri_string = ' . $CI->uri->uri_string, $CI->debug, $CI->debug_file);
  55. }
  56. }
  57. if ( empty($CI->uri->segments) ) {
  58. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  59. $CI->debug_log->write('getCurrPath(): uri->segments is empty. fixing...', $CI->debug, $CI->debug_file);
  60. }
  61. // DPW: because the URI segments array starts at index 1, we do the following...
  62. $ndx =1;
  63. foreach (explode('/', $_SERVER['REQUEST_URI']) as $value) {
  64. if ( $value == '' ) continue;
  65. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  66. $CI->debug_log->write("getCurrPath(): adding uri->segment[$ndx] = $value", $CI->debug, $CI->debug_file);
  67. $CI->uri->segments[$ndx] = trim($value);
  68. }
  69. $ndx++;
  70. }
  71. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  72. $CI->debug_log->write('getCurrPath(): $CI->uri->segments = ' . print_r($CI->uri->segments, 1), $CI->debug, $CI->debug_file);
  73. }
  74. }
  75. else {
  76. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  77. $CI->debug_log->write('getCurrPath(): uri->segments = ' . print_r($CI->uri->segments, 1), $CI->debug, $CI->debug_file);
  78. }
  79. }
  80. if ( empty($CI->uri->rsegments) ) {
  81. $CI->uri->rsegments = array_reverse($CI->uri->segments);
  82. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  83. $CI->debug_log->write('getCurrPath(): uri->rsegments is empty. fixing...', $CI->debug, $CI->debug_file);
  84. $CI->debug_log->write('getCurrPath(): $CI->uri->rsegments = ' . print_r($CI->uri->rsegments, 1), $CI->debug, $CI->debug_file);
  85. }
  86. }
  87. // DPW: end of URI checks are for the testing environment.
  88. $request_url = $CI->uri->segment(1) != "" ? $CI->uri->segment(1) : "/";
  89. $request_url = $CI->uri->segment(2) != "" ? $request_url."/".$CI->uri->segment(2) : $request_url;
  90. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  91. $CI->debug_log->write('getCurrPath(): returning $request_url = >' . print_r($request_url, 1) . '<', $CI->debug, $CI->debug_file);
  92. }
  93. return $request_url;
  94. }
  95. }
  96. if ( ! function_exists('getACLData'))
  97. {
  98. function getACLData()
  99. {
  100. $CI =& get_instance();
  101. $sql = "SELECT uri, groups FROM acls ORDER BY uri";
  102. $qret = $CI->db->query($sql);
  103. if ($qret->num_rows() > 0) {
  104. foreach ($qret->result() as $row) {
  105. $data[] = $row;
  106. }
  107. $qret->free_result();
  108. return $data;
  109. } else {
  110. return FALSE;
  111. }
  112. }
  113. }
  114. if ( ! function_exists('get_acl_redirect'))
  115. {
  116. function get_acl_redirect($request_url)
  117. {
  118. $CI =& get_instance();
  119. $sql = "SELECT redirect FROM acls WHERE uri = '".$request_url."'";
  120. $qret = $CI->db->query($sql);
  121. if ($qret->num_rows() > 0) {
  122. foreach ($qret->result() as $row) {
  123. $data = $row->redirect;
  124. }
  125. $qret->free_result();
  126. return $data;
  127. } else {
  128. return FALSE;
  129. }
  130. }
  131. }
  132. if ( ! function_exists('acl_check'))
  133. {
  134. function acl_check($view, $uri_data = NULL)
  135. {
  136. $CI =& get_instance();
  137. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  138. $CI->debug_log->write('acl_check(): checking $view = >' . print_r($view, 1) . '<', $CI->debug, $CI->debug_file);
  139. }
  140. //This boolean-returned function will check user against table-based access definitions when view request is made.
  141. //
  142. //Function will return TRUE if user has access rights to the page view
  143. //Function will return FALSE is user's groups are not found or that the access level is not sufficient for access
  144. //******************
  145. $ret_value = 0;
  146. if ($CI->config->item('uri_list')) {
  147. $uri_data = $CI->config->item('uri_list');
  148. } else {
  149. error_log("Config File config_custom.php Not Found!");
  150. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  151. $CI->debug_log->write('acl_check(): Config File config_custom.php Not Found!', $CI->debug, $CI->debug_file);
  152. }
  153. }
  154. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  155. $CI->debug_log->write('acl_check(): checking $uri_data = >' . print_r($uri_data, 1) . '<', $CI->debug, $CI->debug_file);
  156. }
  157. if (isset($uri_data)) {
  158. //NEW VERSION HERE!! MAKES USE OF THE CONFIG CUSTOM VAR FOR URI ACCESS
  159. //get all current groups user is assigned and put into array
  160. $curr_groups = explode(",", $CI->session->userdata('groups'));
  161. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  162. $CI->debug_log->write('acl_check(): $curr_groups = >' . print_r($curr_groups, 1) . '<', $CI->debug, $CI->debug_file);
  163. }
  164. foreach ($uri_data as $acldata) {
  165. $acl_uri = $acldata['uri'];
  166. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  167. $CI->debug_log->write('acl_check(): $acl_uri = >' . print_r($acl_uri, 1) . '<', $CI->debug, $CI->debug_file);
  168. }
  169. $acl_groups = $acldata['groups'];
  170. if ($view === $acl_uri) {
  171. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  172. $CI->debug_log->write('acl_check(): matched view and $acl_uri = >' . print_r($acl_uri, 1) . '<', $CI->debug, $CI->debug_file);
  173. }
  174. if (strpos($acl_groups, 'ALL') !== FALSE) {
  175. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  176. $CI->debug_log->write('acl_check(): matched acl group = >ALL<', $CI->debug, $CI->debug_file);
  177. $CI->debug_log->write('acl_check(): returning $ret_value = >' . print_r(TRUE, 1) . '<', $CI->debug, $CI->debug_file);
  178. }
  179. return TRUE;
  180. }
  181. $group_list = explode(";", $acl_groups);
  182. foreach ($group_list as $key=>$value) {
  183. $acl_sublist = explode(",", $value);
  184. foreach($acl_sublist as $key=>$a_data) {
  185. //echo "<br>DIR=$view ARR_SEARCH=array_search($a_data, "; print_r($curr_groups); echo " IN_ARRAY=".in_array($a_data, $curr_groups); echo "<br>";
  186. if (in_array($a_data, $curr_groups)) {
  187. $ret_value = 1;
  188. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  189. $CI->debug_log->write("acl_check(): matched acl group = >$a_data<", $CI->debug, $CI->debug_file);
  190. }
  191. break;
  192. }
  193. }
  194. if ($ret_value == 1) break;
  195. }
  196. }
  197. if ($ret_value == 1) break;
  198. }
  199. } else {
  200. //ORIGINAL VERSION HERE!!
  201. //retrieve all ACLS data
  202. $acl_data = array();
  203. $acl_data = getACLData();
  204. $CI->session->set_userdata('acl', $acl_data);
  205. //get all current groups user is assigned and put into array
  206. $curr_groups = explode(",", $CI->session->userdata('groups'));
  207. //get all acl/directory combos to search on
  208. $acl_all = $CI->session->userdata('acl');
  209. foreach ($acl_all as $acldata) {
  210. $acl_uri = $acldata->uri;
  211. $acl_groups = $acldata->groups;
  212. if ($view === $acl_uri) {
  213. if (strpos($acl_groups, 'ALL') !== FALSE) {
  214. return TRUE;
  215. }
  216. $group_list = explode(";", $acl_groups);
  217. foreach ($group_list as $key=>$value) {
  218. $acl_sublist = explode(",", $value);
  219. foreach($acl_sublist as $key=>$a_data) {
  220. //echo "DIR=$view ARR_SEARCH=array_search($a_data, "; print_r($curr_groups); echo " IN_ARRAY=".in_array($a_data, $curr_groups); echo "<br>";
  221. if (in_array($a_data, $curr_groups)) {
  222. $ret_value = 1;
  223. } else {
  224. $ret_value = 0;
  225. break;
  226. }
  227. }
  228. if ($ret_value == 1) break;
  229. }
  230. }
  231. if ($ret_value == 1) break;
  232. }
  233. }
  234. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  235. $CI->debug_log->write('acl_check(): returning $ret_value = >' . print_r($ret_value, 1) . '<', $CI->debug, $CI->debug_file);
  236. }
  237. if ($ret_value == 1) {
  238. return TRUE;
  239. } else {
  240. return FALSE;
  241. }
  242. }
  243. }
  244. if ( ! function_exists('authorize'))
  245. {
  246. function authorized()
  247. {
  248. $CI =& get_instance();
  249. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  250. $CI->debug_log->write('helper function authorized():', $CI->debug, $CI->debug_file);
  251. $CI->debug_log->write('checking session user_id = >' . print_r($CI->session->userdata, 1) . '<', $CI->debug, $CI->debug_file);
  252. }
  253. if ( $CI->session->userdata('user_id') == "" ) {
  254. $return_value = 0;
  255. }
  256. else {
  257. $pathinfo = getCurrPath();
  258. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  259. $CI->debug_log->write('authorized(): pathinfo = >' . print_r($pathinfo, 1) . '<', $CI->debug, $CI->debug_file);
  260. }
  261. if ( acl_check($pathinfo) ) {
  262. $return_value = 1;
  263. }
  264. else {
  265. $return_value = -1;
  266. }
  267. }
  268. if ( isset($CI->debug) and isset($CI->debug_file) ) {
  269. $CI->debug_log->write('authorized(): returning return_value = >' . print_r($return_value, 1) . '<', $CI->debug, $CI->debug_file);
  270. }
  271. return $return_value;
  272. }
  273. }
  274. ?>