PageRenderTime 77ms CodeModel.GetById 48ms RepoModel.GetById 2ms app.codeStats 0ms

/application/helpers/users_helper.php

https://bitbucket.org/matyhaty/senses-designertravelv3
PHP | 555 lines | 340 code | 85 blank | 130 comment | 73 complexity | 6d1b4a6947fb61815ba5cda0f42d8551 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * User helper function for seeing if the user is logged in
  4. */
  5. if (!function_exists('isLoggedIn'))
  6. {
  7. function isLoggedIn($session)
  8. {
  9. $user = $session -> all_userdata();
  10. if (!isset($user['user_data']['username']) || !isset($user['user_data']['email']))
  11. return false;
  12. return $user['user_data'];
  13. }
  14. }
  15. /* Function checks whether the user has permission to access a function
  16. */
  17. function check_permissions(DataMapper $user = NULL, $uri = null)
  18. {
  19. static $allowedURIS;
  20. if (isset($allowedURIS[$uri]))
  21. {
  22. // this uri has allowed been allowed! cool trick aye
  23. return true;
  24. }
  25. // 0 non registered
  26. // 1 registered
  27. // 2 special guest
  28. // 3 some other account
  29. // 9 admin account
  30. //$perms['user/profile/index'] = array( 'user_type' => array( 1=>1 , 3=>3 ) , 'fail_hook'=>$failHook = array(), 'success'=>$successHook = array() , 'logged_in' => 1 );
  31. //$perms['user/account/index'] = array( 'user_type' => array( 1=>1 , 3=>3 ) , 'fail_hook'=>$failHook = array(), 'success'=>$successHook = array() , 'logged_in' => 1 );
  32. //$perms['event/events/create'] = array( 'user_type' => array( 1=>1 , 3=>3 ) , 'fail_hook'=>$failHook = array(), 'success'=>$successHook = array() , 'logged_in' => 1 );
  33. $perms['admin/login'] = array(
  34. 'user_type' => array(0 => 0),
  35. 'fail_hook' => $failHook = array(),
  36. 'success' => $successHook = array(),
  37. 'logged_in' => 2
  38. );
  39. $perms['admin/home'] = array(
  40. 'user_type' => array(1 => 1),
  41. 'fail_hook' => $failHook = array(),
  42. 'success' => $successHook = array(),
  43. 'logged_in' => 1
  44. );
  45. $perms['admin/destinations/list'] = array(
  46. 'user_type' => array(1 => 1),
  47. 'fail_hook' => $failHook = array(),
  48. 'success' => $successHook = array(),
  49. 'logged_in' => 1
  50. );
  51. $perms['admin/itinerary/list'] = array(
  52. 'user_type' => array(1 => 1),
  53. 'fail_hook' => $failHook = array(),
  54. 'success' => $successHook = array(),
  55. 'logged_in' => 1
  56. );
  57. $perms['admin/user/view'] = array(
  58. 'user_type' => array(1 => 1),
  59. 'fail_hook' => $failHook = array(),
  60. 'success' => $successHook = array(),
  61. 'logged_in' => 1
  62. );
  63. $perms['admin/communications/list'] = array(
  64. 'user_type' => array(1 => 1),
  65. 'fail_hook' => $failHook = array(),
  66. 'success' => $successHook = array(),
  67. 'logged_in' => 1
  68. );
  69. //$perms['user/account'] = array( 'user_type' => array( 1=>1 ) , 'fail_hook'=>$failHook = array(), 'success'=>$successHook = array() , 'logged_in' => 1 );
  70. // could probably be moved to its own seperate config file!
  71. // perm usertype, key and val should always be the the same
  72. $uri = $uri ? $uri : uri_string(current_url());
  73. $uriArr = explode('/', $uri, 4);
  74. unset($uriArr[3]);
  75. // we are only interested in the first 3 segments
  76. $uri = implode('/', $uriArr);
  77. if (isset($perms[$uri]))
  78. {
  79. // they must be logged in first
  80. if ($perms[$uri]['logged_in'] == 1 && !$user -> id)
  81. {
  82. return 'must_be_logged_in';
  83. }
  84. // they cant be logged in to view this page, useful for the login page for example
  85. if ($perms[$uri]['logged_in'] == 2 && $user -> id)
  86. {
  87. return 'must_be_logged_in_out';
  88. }
  89. // check if the current uri has a permission, use isset for speed
  90. // admin overrules all
  91. if (isset($perms[$uri]['user_type'][(int)$user -> user_type]) || $user -> user_type == ADMIN_USER_TYPE)
  92. {
  93. // looks complicated but quite simple
  94. // check the $perms array against user_type array against the key of the actual users user_type
  95. // if it return true the have permission
  96. // run success hook
  97. $allowedURIS[$uri] = 1;
  98. return true;
  99. }
  100. else
  101. {
  102. //print_r($perms[$uri]);
  103. //echo '<hr><pre>';
  104. //print_r( $user -> all_to_array());
  105. return false;
  106. //run fail hook
  107. }// no acces
  108. }
  109. else
  110. {
  111. // we are only interested in the first 2 segments and wildcards
  112. unset($uriArr[2]);
  113. $uriWildcard = implode('/', $uriArr);
  114. $uriWildcard .= '/any';
  115. if (isset($perms[$uriWildcard]))
  116. {
  117. //$CI = &get_instance();
  118. //$CI -> firephp -> error("Wildcard match: " . $uriWildcard);
  119. // they must be logged in first
  120. if ($perms[$uriWildcard]['logged_in'] == 1 && !$user -> id)
  121. {
  122. return 'must_be_logged_in';
  123. }
  124. // they cant be logged in to view this page, useful for the login page for example
  125. if ($perms[$uriWildcard]['logged_in'] == 2 && $user -> id)
  126. {
  127. return 'must_be_logged_in_out';
  128. }
  129. // check if the current uri has a permission, use isset for speed
  130. // admin overrules all
  131. if (isset($perms[$uriWildcard]['user_type'][(int)$user -> user_type]) || $user -> user_type == ADMIN_USER_TYPE)
  132. {
  133. // looks complicated but quite simple
  134. // check the $perms array against user_type array against the key of the actual users user_type
  135. // if it return true the have permission
  136. // run success hook
  137. $allowedURIS[$uriWildcard] = 1;
  138. return true;
  139. }
  140. else
  141. {
  142. return false;
  143. //run fail hook
  144. }// no acces
  145. }
  146. else
  147. {
  148. $CI = &get_instance();
  149. $CI -> firephp -> error("No Permission settings for URL: " . $uri . " or " . $uriWildcard);
  150. $allowedURIS[$uri] = 1;
  151. // if not set has no perm association so return true
  152. //return true;
  153. return true;
  154. }
  155. }
  156. }
  157. /**
  158. * All links should use this function to utilise the basic ACL
  159. *
  160. * @param DataMapper $user
  161. * @paramString $uri
  162. * @param Boolean $returnURI
  163. * @param String $permURI
  164. * @return varies
  165. */
  166. function checkURIPerm(DataMapper $user, $uri, $returnURI = true, $permURI = '')
  167. {
  168. //if(!$uri) throw new Exception('URI must be sent to this checkURIPerm function');
  169. $uri = $permURI != '' ? $permURI : $uri;
  170. // add flexibilty for uri which have ids in them
  171. //$CI = &get_instance();
  172. // $CI -> firephp -> error("not allowed: ".$uri);
  173. if (check_permissions($user, $uri))
  174. {
  175. if ($returnURI)
  176. {
  177. return site_url($uri);
  178. }
  179. else
  180. {
  181. return true;
  182. }
  183. }
  184. if ($returnURI)
  185. {
  186. // return site_url('noaccess');
  187. }
  188. else
  189. {
  190. return false;
  191. }
  192. }
  193. function slir($path, $image, $width, $height, array $crop = array(), $attri = '', $uriOnly = false)
  194. {
  195. $slircode = '';
  196. $slircode .= 'w' . $width . '-';
  197. $slircode .= 'h' . $height;
  198. if (isset($crop['width']) && isset($crop['height']))
  199. $slircode .= '-c' . $crop['width'] . ':' . $crop['height'];
  200. $imageRel = $path . $image;
  201. $imageAbs = FRONT_END_PATH . '/uploads/' . $path . $image;
  202. $imageAbsBackup = FRONT_END_PATH . '/uploads/' . $path . 'profile-image.png';
  203. $imageAbsFullback = FRONT_END_PATH . '/uploads/fallback-image.png';
  204. // the absolute path
  205. if (file_exists($imageAbs) && $image != "")
  206. {
  207. if ($uriOnly)
  208. return SLIR_URL . $slircode . $imageRel;
  209. $base = '<img src="' . SLIR_URL . $slircode . '/uploads' . $imageRel . '" ' . $attri . ' />';
  210. }
  211. else
  212. if (file_exists($imageAbsBackup) && $image != "")
  213. {
  214. //echo $imageAbs;
  215. if ($uriOnly)
  216. return SLIR_URL . $slircode . $path . 'profile-image.png';
  217. $base = '<img src="' . SLIR_URL . $slircode . '/uploads' . $path . 'profile-image.png" alt="File not found: ' . $path . $image . '" />';
  218. }
  219. else
  220. {
  221. if ($uriOnly)
  222. return SLIR_URL . $slircode . 'fallback-image.png';
  223. $base = '<img src="' . SLIR_URL . $slircode . '/uploads/fallback-image.png" alt="File not found: ' . $path . $image . '" />';
  224. }
  225. return $base;
  226. }
  227. function slirPath($image, $slircode, $data = NULL)
  228. {
  229. $imageAbs = FRONT_END_PATH . '/uploads/' . $image;
  230. //echo SLIR_URL . '?' . $slircode . '&i=' . UPLOAD_URL_ . $image;
  231. if (file_exists($imageAbs) && !is_dir($imageAbs))
  232. {
  233. $base = SLIR_URL . '?' . $slircode . '&i=' . UPLOAD_URL . $image;
  234. }
  235. else
  236. {
  237. $base = SLIR_URL . '?' . $slircode . '&i=' . UPLOAD_URL . 'placeholder.jpg';
  238. }
  239. return $base;
  240. }
  241. /**
  242. * All links should pass through this function
  243. *
  244. * @param DataMapper $user
  245. * @param unknown_type $uri
  246. * @param unknown_type $text
  247. * @param unknown_type $attribs
  248. * @return unknown
  249. */
  250. function permAnchor(DataMapper $user, $uri, $text, $attribs = array())
  251. {
  252. return anchor(checkURIPerm($user, $uri), $text, $attribs);
  253. }
  254. /**
  255. * Truncate string
  256. *
  257. * @param String $string
  258. * @param Int $your_desired_width
  259. * @return String
  260. */
  261. function tokenTruncate($string, $your_desired_width)
  262. {
  263. $parts = preg_split('/([\s\n\r]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE);
  264. $parts_count = count($parts);
  265. $length = 0;
  266. $last_part = 0;
  267. for (; $last_part < $parts_count; ++$last_part)
  268. {
  269. $length += strlen($parts[$last_part]);
  270. if ($length > $your_desired_width)
  271. {
  272. break;
  273. }
  274. }
  275. return implode(array_slice($parts, 0, $last_part));
  276. }
  277. /**
  278. * Get a language string
  279. *
  280. * @param String $langStr
  281. * @return String
  282. */
  283. function l($langStr)
  284. {
  285. static $CI;
  286. if ($CI)
  287. return $CI -> lang -> line($langStr);
  288. $CI = get_instance();
  289. return $CI -> lang -> line($langStr);
  290. }
  291. /**
  292. * Returns the number of images for the supplied user
  293. *
  294. */
  295. function get_user_image_count($userid = 0)
  296. {
  297. if ($userid == 0)
  298. return 0;
  299. }
  300. /**
  301. * Returns the number of classes for the supplied user
  302. *
  303. */
  304. function get_user_class_count($userid = 0)
  305. {
  306. if ($userid == 0)
  307. return 0;
  308. $user = new User();
  309. $user -> where("id", $userid) -> get();
  310. $user -> lecture -> get();
  311. return ($user -> lecture -> count() != 0 ? $user -> lecture -> count() : 0);
  312. }
  313. /**
  314. * Returns the number of events for the supplied user
  315. *
  316. */
  317. function get_user_event_count($userid = 0)
  318. {
  319. if ($userid == 0)
  320. return 0;
  321. $user = new User();
  322. $user -> where("id", $userid) -> get();
  323. $user -> event -> get();
  324. return ($user -> event -> count() != 0 ? $user -> event -> count() : 0);
  325. }
  326. /**
  327. * Returns the number of jobs for the supplied user
  328. *
  329. */
  330. function get_user_job_count($userid = 0)
  331. {
  332. if ($userid == 0)
  333. return 0;
  334. $user = new User();
  335. $user -> where("id", $userid) -> get();
  336. $user -> job -> get();
  337. return ($user -> job -> count() != 0 ? $user -> job -> count() : 0);
  338. }
  339. /**
  340. * Returns the number of groups for the supplied user
  341. *
  342. */
  343. function get_user_group_count($userid = 0)
  344. {
  345. if ($userid == 0)
  346. return 0;
  347. $user = new User();
  348. $user -> where("id", $userid) -> get();
  349. $user -> group -> get();
  350. return ($user -> group -> count() != 0 ? $user -> group -> count() : 0);
  351. }
  352. function isAdmin($id)
  353. {
  354. $u = new User();
  355. $obj = $u -> getObject('user', $id, null, false, array('state' => 'Active'), '', '') -> dm_object;
  356. if ($obj -> user_admin)
  357. {
  358. return true;
  359. }
  360. else
  361. {
  362. return false;
  363. }
  364. }
  365. /**
  366. * Returns the number of questions for the supplied user
  367. *
  368. */
  369. function get_user_question_count($userid = 0)
  370. {
  371. if ($userid == 0)
  372. return 0;
  373. $user = new User();
  374. $user -> where("id", $userid) -> get();
  375. $user -> group -> get();
  376. return ($user -> question -> count() != 0 ? $user -> question -> count() : 0);
  377. }
  378. /**
  379. * Get the user's fullname
  380. *
  381. */
  382. function get_user_fullname($userid = 0)
  383. {
  384. $user = new User();
  385. $user -> where("id", $userid) -> get();
  386. return $user -> firstname . " " . $user -> surname;
  387. }
  388. /**
  389. * The time since something was done by a user, or object
  390. *
  391. * @param UNIX time stamp
  392. * @return STRING
  393. */
  394. function time_since($original)
  395. {
  396. // array of time period chunks
  397. $chunks = array(
  398. array(
  399. 60 * 60 * 24 * 365,
  400. 'year'
  401. ),
  402. array(
  403. 60 * 60 * 24 * 30,
  404. 'month'
  405. ),
  406. array(
  407. 60 * 60 * 24 * 7,
  408. 'week'
  409. ),
  410. array(
  411. 60 * 60 * 24,
  412. 'day'
  413. ),
  414. array(
  415. 60 * 60,
  416. 'hour'
  417. ),
  418. array(
  419. 60,
  420. 'minute'
  421. ),
  422. );
  423. $today = time();
  424. /* Current unix time */
  425. $since = $today - $original;
  426. if ($since > 604800)
  427. {
  428. $print = date("M jS", $original);
  429. if ($since > 31536000)
  430. {
  431. $print .= ", " . date("Y", $original);
  432. }
  433. return $print;
  434. }
  435. // $j saves performing the count function each time around the loop
  436. for ($i = 0, $j = count($chunks); $i < $j; $i++)
  437. {
  438. $seconds = $chunks[$i][0];
  439. $name = $chunks[$i][1];
  440. // finding the biggest chunk (if the chunk fits, break)
  441. if (($count = floor($since / $seconds)) != 0)
  442. {
  443. // DEBUG print "<!-- It's $name -->\n";
  444. break;
  445. }
  446. }
  447. $print = ($count == 1) ? '1 ' . $name : "$count {$name}s";
  448. return $print . " ago";
  449. }
  450. /**
  451. * Get the number of inbox messages for the user
  452. *
  453. */
  454. function get_user_pms_count($uid, $state = "active", $created_by = null, $userid_to = null)
  455. {
  456. $user = new User();
  457. // Get the users messages
  458. $user -> where("id", $uid);
  459. $user -> get();
  460. // Get the messages
  461. $messages = new Message();
  462. $messages -> where('state', $state);
  463. // handle user id to field
  464. if ($userid_to != 0)
  465. $messages -> where('userid_to', $userid_to);
  466. // handle created by field
  467. if ($created_by != 0)
  468. $messages -> where('created_by', $created_by);
  469. return $messages -> count();
  470. }