PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/auth.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 612 lines | 438 code | 71 blank | 103 comment | 107 complexity | 0d7ac583c3c4115d7421a93ddfeb2975 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. *
  12. * @Icy Phoenix is based on phpBB
  13. * @copyright (c) 2008 phpBB Group
  14. *
  15. */
  16. if (!defined('IN_ICYPHOENIX'))
  17. {
  18. die('Hacking attempt');
  19. }
  20. /*
  21. * $type's accepted (pre-pend with AUTH_):
  22. *
  23. * 01 View,
  24. * 02 Read,
  25. * 03 Post,
  26. * 04 Reply,
  27. * 05 Edit,
  28. * 06 Delete,
  29. * 07 Sticky,
  30. * 08 Announce,
  31. * 09 Global Ann,
  32. * 10 News,
  33. * 11 Calendar,
  34. * 12 Vote,
  35. * 13 Poll,
  36. * 14 Attach,
  37. * 15 Download,
  38. * 16 Warn,
  39. * 17 Unban,
  40. * 18 Report,
  41. * 19 Rate
  42. *
  43. * Possible options ($type/forum_id combinations):
  44. *
  45. * - If you include a type and forum_id then a specific lookup will be done and the single result returned
  46. * - If you set type to AUTH_ALL and specify a forum_id an array of all auth types will be returned
  47. * - If you provide a forum_id a specific lookup on that forum will be done
  48. * - If you set forum_id to AUTH_LIST_ALL and specify a type an array listing the results for all forums will be returned
  49. * - If you set forum_id to AUTH_LIST_ALL and type to AUTH_ALL a multidimensional array containing the auth permissions
  50. * for all types and all forums for that user is returned
  51. *
  52. * All results are returned as associative arrays, even when a single auth type is specified.
  53. *
  54. * If available you can send an array (either one or two dimensional) containing the
  55. * forum auth levels, this will prevent the auth function having to do its own lookup
  56. */
  57. function auth($type, $forum_id, &$user_data, $f_access = '')
  58. {
  59. global $db, $config, $user, $lang, $tree;
  60. if (!empty($tree['data']))
  61. {
  62. $f_access = array();
  63. if (!empty($forum_id))
  64. {
  65. $idx = $tree['keys'][POST_FORUM_URL . $forum_id];
  66. $f_access = $tree['data'][$idx];
  67. }
  68. else
  69. {
  70. for ($i = 0; $i < sizeof($tree['data']); $i++)
  71. {
  72. if ($tree['type'][$i] == POST_FORUM_URL)
  73. {
  74. $f_access[] = $tree['data'][$i];
  75. }
  76. }
  77. }
  78. }
  79. switch($type)
  80. {
  81. case AUTH_ALL:
  82. $a_sql = 'a.auth_view, a.auth_read, a.auth_post, a.auth_reply, a.auth_edit, a.auth_delete, a.auth_sticky, a.auth_announce, a.auth_globalannounce, a.auth_news, a.auth_cal, a.auth_vote, a.auth_pollcreate, a.auth_attachments, a.auth_download, a.auth_ban, a.auth_greencard, a.auth_bluecard, a.auth_rate';
  83. $auth_fields = array('auth_view', 'auth_read', 'auth_post', 'auth_reply', 'auth_edit', 'auth_delete', 'auth_sticky', 'auth_announce', 'auth_globalannounce', 'auth_news', 'auth_cal', 'auth_vote', 'auth_pollcreate', 'auth_attachments', 'auth_download', 'auth_ban', 'auth_greencard', 'auth_bluecard', 'auth_rate');
  84. break;
  85. case AUTH_VIEW:
  86. $a_sql = 'a.auth_view';
  87. $auth_fields = array('auth_view');
  88. break;
  89. case AUTH_READ:
  90. $a_sql = 'a.auth_read';
  91. $auth_fields = array('auth_read');
  92. break;
  93. case AUTH_POST:
  94. $a_sql = 'a.auth_post';
  95. $auth_fields = array('auth_post');
  96. break;
  97. case AUTH_REPLY:
  98. $a_sql = 'a.auth_reply';
  99. $auth_fields = array('auth_reply');
  100. break;
  101. case AUTH_EDIT:
  102. $a_sql = 'a.auth_edit';
  103. $auth_fields = array('auth_edit');
  104. break;
  105. case AUTH_DELETE:
  106. $a_sql = 'a.auth_delete';
  107. $auth_fields = array('auth_delete');
  108. break;
  109. case AUTH_STICKY:
  110. $a_sql = 'a.auth_sticky';
  111. $auth_fields = array('auth_sticky');
  112. break;
  113. case AUTH_ANNOUNCE:
  114. $a_sql = 'a.auth_announce';
  115. $auth_fields = array('auth_announce');
  116. break;
  117. case AUTH_GLOBALANNOUNCE:
  118. $a_sql = 'a.auth_globalannounce';
  119. $auth_fields = array('auth_globalannounce');
  120. break;
  121. case AUTH_NEWS:
  122. $a_sql = 'a.auth_news';
  123. $auth_fields = array('auth_news');
  124. break;
  125. case AUTH_CAL:
  126. $a_sql = 'a.auth_cal';
  127. $auth_fields = array('auth_cal');
  128. break;
  129. case AUTH_VOTE:
  130. $a_sql = 'a.auth_vote';
  131. $auth_fields = array('auth_vote');
  132. break;
  133. case AUTH_POLLCREATE:
  134. $a_sql = 'a.auth_pollcreate';
  135. $auth_fields = array('auth_pollcreate');
  136. break;
  137. case AUTH_ATTACHMENTS:
  138. $a_sql = 'a.auth_attachments';
  139. $auth_fields = array('auth_attachments');
  140. break;
  141. case AUTH_DOWNLOAD:
  142. $a_sql = 'a.auth_download';
  143. $auth_fields = array('auth_download');
  144. break;
  145. case AUTH_BAN:
  146. $a_sql = 'a.auth_ban';
  147. $auth_fields = array('auth_ban');
  148. break;
  149. case AUTH_GREENCARD:
  150. $a_sql = 'a.auth_greencard';
  151. $auth_fields = array('auth_greencard');
  152. break;
  153. case AUTH_BLUECARD:
  154. $a_sql = 'a.auth_bluecard';
  155. $auth_fields = array('auth_bluecard');
  156. break;
  157. case AUTH_RATE:
  158. $a_sql = 'a.auth_rate';
  159. $auth_fields = array('auth_rate');
  160. break;
  161. default:
  162. break;
  163. }
  164. //
  165. // If f_access has been passed, or auth is needed to return an array of forums
  166. // then we need to pull the auth information on the given forum (or all forums)
  167. //
  168. if (empty($f_access))
  169. {
  170. $forum_match_sql = ($forum_id != AUTH_LIST_ALL) ? (" WHERE a.forum_id = " . $forum_id) : '';
  171. $sql = "SELECT a.forum_id, $a_sql
  172. FROM " . FORUMS_TABLE . " a
  173. $forum_match_sql";
  174. $result = $db->sql_query($sql);
  175. $sql_fetchrow = ($forum_id != AUTH_LIST_ALL) ? 'sql_fetchrow' : 'sql_fetchrowset';
  176. if (!($f_access = $db->$sql_fetchrow($result)))
  177. {
  178. $db->sql_freeresult($result);
  179. return array();
  180. }
  181. $db->sql_freeresult($result);
  182. }
  183. //
  184. // If the user isn't logged on then all we need do is check if the forum
  185. // has the type set to ALL, if yes they are good to go, if not then they are denied access
  186. //
  187. $u_access = array();
  188. if ($user_data['session_logged_in'])
  189. {
  190. $forum_match_sql = ($forum_id != AUTH_LIST_ALL) ? "AND a.forum_id = $forum_id" : '';
  191. $sql = "SELECT a.forum_id, $a_sql, a.auth_mod
  192. FROM " . AUTH_ACCESS_TABLE . " a, " . USER_GROUP_TABLE . " ug
  193. WHERE ug.user_id = " . $user_data['user_id'] . "
  194. AND ug.user_pending = 0
  195. AND a.group_id = ug.group_id
  196. $forum_match_sql";
  197. $result = $db->sql_query($sql, 0, 'auth_');
  198. if ($row = $db->sql_fetchrow($result))
  199. {
  200. do
  201. {
  202. if ($forum_id != AUTH_LIST_ALL)
  203. {
  204. $u_access[] = $row;
  205. }
  206. else
  207. {
  208. $u_access[$row['forum_id']][] = $row;
  209. }
  210. }
  211. while($row = $db->sql_fetchrow($result));
  212. }
  213. $db->sql_freeresult($result);
  214. }
  215. $is_admin = (($user_data['user_level'] == ADMIN) && $user_data['session_logged_in']) ? true : 0;
  216. //$is_admin = ((($user_data['user_level'] == ADMIN) || ($user_data['user_level'] == JUNIOR_ADMIN)) && $user_data['session_logged_in']) ? true : 0;
  217. $auth_user = array();
  218. for($i = 0; $i < sizeof($auth_fields); $i++)
  219. {
  220. $key = $auth_fields[$i];
  221. //
  222. // If the user is logged on and the forum type is either ALL or REG then the user has access
  223. //
  224. // If the type if ACL, MOD or ADMIN then we need to see if the user has specific permissions
  225. // to do whatever it is they want to do ... to do this we pull relevant information for the
  226. // user (and any groups they belong to)
  227. //
  228. // Now we compare the users access level against the forums. We assume here that a moderator
  229. // and admin automatically have access to an ACL forum, similarly we assume admins meet an
  230. // auth requirement of MOD
  231. //
  232. if ($forum_id != AUTH_LIST_ALL)
  233. {
  234. $value = $f_access[$key];
  235. switch($value)
  236. {
  237. case AUTH_ALL:
  238. $auth_user[$key] = true;
  239. $auth_user[$key . '_type'] = $lang['Auth_Anonymous_Users'];
  240. break;
  241. case AUTH_REG:
  242. $auth_user[$key] = ($user_data['session_logged_in'] || (!empty($config['bots_reg_auth']) && $user_data['is_bot'])) ? true : 0;
  243. $auth_user[$key . '_type'] = $lang['Auth_Registered_Users'];
  244. break;
  245. // Self AUTH - BEGIN
  246. case AUTH_SELF:
  247. $auth_user[$key] = ($user_data['session_logged_in']) ? ((auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin)) ? true : AUTH_SELF) : 0;
  248. $auth_user[$key . '_type'] = $lang['Auth_Self_Users'];
  249. break;
  250. // Self AUTH - END
  251. case AUTH_ACL:
  252. $auth_user[$key] = ($user_data['session_logged_in']) ? auth_check_user(AUTH_ACL, $key, $u_access, $is_admin) : 0;
  253. $auth_user[$key . '_type'] = $lang['Auth_Users_granted_access'];
  254. break;
  255. case AUTH_MOD:
  256. $auth_user[$key] = ($user_data['session_logged_in']) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;
  257. $auth_user[$key . '_type'] = $lang['Auth_Moderators'];
  258. break;
  259. case AUTH_ADMIN:
  260. $auth_user[$key] = $is_admin;
  261. $auth_user[$key . '_type'] = $lang['Auth_Administrators'];
  262. break;
  263. case AUTH_NONE:
  264. $auth_user[$key] = 0;
  265. break;
  266. default:
  267. $auth_user[$key] = 0;
  268. break;
  269. }
  270. }
  271. else
  272. {
  273. for($k = 0; $k < sizeof($f_access); $k++)
  274. {
  275. $value = $f_access[$k][$key];
  276. $f_forum_id = $f_access[$k]['forum_id'];
  277. $u_access[$f_forum_id] = isset($u_access[$f_forum_id]) ? $u_access[$f_forum_id] : array();
  278. switch($value)
  279. {
  280. case AUTH_ALL:
  281. $auth_user[$f_forum_id][$key] = true;
  282. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Anonymous_Users'];
  283. break;
  284. case AUTH_REG:
  285. $auth_user[$f_forum_id][$key] = ($user_data['session_logged_in'] || (!empty($config['bots_reg_auth']) && $user_data['is_bot'])) ? true : 0;
  286. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Registered_Users'];
  287. break;
  288. // Self AUTH - BEGIN
  289. case AUTH_SELF:
  290. $auth_user[$f_forum_id][$key] = ($user_data['session_logged_in']) ? ((auth_check_user(AUTH_MOD, 'auth_mod', $u_access[$f_forum_id], $is_admin)) ? true : AUTH_SELF) : 0;
  291. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Self_Users'];
  292. break;
  293. // Self AUTH - END
  294. case AUTH_ACL:
  295. $auth_user[$f_forum_id][$key] = ($user_data['session_logged_in']) ? auth_check_user(AUTH_ACL, $key, $u_access[$f_forum_id], $is_admin) : 0;
  296. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Users_granted_access'];
  297. break;
  298. case AUTH_MOD:
  299. $auth_user[$f_forum_id][$key] = ($user_data['session_logged_in']) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access[$f_forum_id], $is_admin) : 0;
  300. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Moderators'];
  301. break;
  302. case AUTH_ADMIN:
  303. $auth_user[$f_forum_id][$key] = $is_admin;
  304. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Administrators'];
  305. break;
  306. case AUTH_NONE:
  307. $auth_user[$f_forum_id][$key] = 0;
  308. break;
  309. default:
  310. $auth_user[$f_forum_id][$key] = 0;
  311. break;
  312. }
  313. }
  314. }
  315. }
  316. // Is user a moderator?
  317. if ($forum_id != AUTH_LIST_ALL)
  318. {
  319. $auth_user['auth_mod'] = ($user_data['session_logged_in']) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;
  320. }
  321. else
  322. {
  323. for($k = 0; $k < sizeof($f_access); $k++)
  324. {
  325. $f_forum_id = $f_access[$k]['forum_id'];
  326. $u_access[$f_forum_id] = isset($u_access[$f_forum_id]) ? $u_access[$f_forum_id] : array();
  327. $auth_user[$f_forum_id]['auth_mod'] = ($user_data['session_logged_in']) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access[$f_forum_id], $is_admin) : 0;
  328. }
  329. }
  330. return $auth_user;
  331. }
  332. /*
  333. * Check user auth
  334. */
  335. function auth_check_user($type, $key, $u_access, $is_admin)
  336. {
  337. $auth_user = 0;
  338. if (sizeof($u_access))
  339. {
  340. for($j = 0; $j < sizeof($u_access); $j++)
  341. {
  342. $result = 0;
  343. switch($type)
  344. {
  345. case AUTH_ACL:
  346. $result = $u_access[$j][$key];
  347. case AUTH_MOD:
  348. $result = $result || $u_access[$j]['auth_mod'];
  349. case AUTH_ADMIN:
  350. $result = $result || $is_admin;
  351. break;
  352. }
  353. $auth_user = $auth_user || $result;
  354. }
  355. }
  356. else
  357. {
  358. $auth_user = $is_admin;
  359. }
  360. return $auth_user;
  361. }
  362. /*
  363. * Check auth level
  364. * Returns true in case the user has the requested level
  365. */
  366. function check_auth_level($level_required)
  367. {
  368. global $user, $config;
  369. if ($level_required == AUTH_ALL)
  370. {
  371. return true;
  372. }
  373. if ($user->data['user_level'] == ADMIN)
  374. {
  375. if (($level_required == AUTH_ADMIN) || ($level_required == AUTH_GUEST_ONLY))
  376. {
  377. return true;
  378. }
  379. if ($level_required == AUTH_FOUNDER)
  380. {
  381. $founder_id = (defined('FOUNDER_ID') ? FOUNDER_ID : get_founder_id());
  382. return ($user->data['user_id'] == $founder_id) ? true : false;
  383. }
  384. elseif ($level_required == AUTH_MAIN_ADMIN)
  385. {
  386. if (defined('MAIN_ADMINS_ID'))
  387. {
  388. $allowed_admins = explode(',', MAIN_ADMINS_ID);
  389. return (in_array($user->data['user_id'], $allowed_admins)) ? true : false;
  390. }
  391. }
  392. }
  393. // Before going on... if level_required is for Guests only then check if the user is a guest but not a bot...
  394. if ($level_required == AUTH_GUEST_ONLY)
  395. {
  396. return (!$user->data['is_bot'] && !$user->data['session_logged_in']) ? true : false;
  397. }
  398. // Force to AUTH_ADMIN since we already checked all cases for founder or main admins
  399. if (($level_required == AUTH_FOUNDER) || ($level_required == AUTH_MAIN_ADMIN))
  400. {
  401. $level_required = AUTH_ADMIN;
  402. }
  403. // Access level required is at least REG and user is not an admin!
  404. // Remember that Junior Admin has the ADMIN level while not in CMS or ACP
  405. $not_auth = false;
  406. // Check if the user is REG or a BOT
  407. $is_reg = ((!empty($config['bots_reg_auth']) && $user->data['is_bot']) || $user->data['session_logged_in']) ? true : false;
  408. if ($level_required == AUTH_OWNER)
  409. {
  410. return (($is_reg && !empty($user->data['user_id_plugin_owner']) && ($user->data['user_id'] == $user->data['user_id_plugin_owner'])) || ($user->data['user_level'] == ADMIN)) ? true : false;
  411. }
  412. $not_auth = (!$not_auth && ($level_required == AUTH_REG) && !$is_reg) ? true : $not_auth;
  413. $not_auth = (!$not_auth && ($level_required == AUTH_MOD) && ($user->data['user_level'] != MOD) && ($user->data['user_level'] != ADMIN)) ? true : $not_auth;
  414. $not_auth = (!$not_auth && ($level_required == AUTH_ADMIN)) ? true : $not_auth;
  415. if ($not_auth)
  416. {
  417. return false;
  418. }
  419. return true;
  420. }
  421. /**
  422. * Check if the user is allowed to access a page
  423. */
  424. function check_page_auth($cms_page_id, $cms_auth_level, $return = false)
  425. {
  426. global $user, $lang;
  427. $is_auth = check_auth_level($cms_auth_level);
  428. if (!$is_auth)
  429. {
  430. if ($return)
  431. {
  432. return false;
  433. }
  434. else
  435. {
  436. if (!$user->data['is_bot'] && !$user->data['session_logged_in'])
  437. {
  438. $page_array = array();
  439. $page_array = extract_current_page(IP_ROOT_PATH);
  440. redirect(append_sid(CMS_PAGE_LOGIN . '?redirect=' . str_replace(('.' . PHP_EXT . '?'), ('.' . PHP_EXT . '&'), $page_array['page']), true));
  441. }
  442. else
  443. {
  444. message_die(GENERAL_MESSAGE, $lang['Not_Auth_View']);
  445. }
  446. }
  447. }
  448. return true;
  449. }
  450. /**
  451. * Builds a list of forums with no read access
  452. */
  453. function build_exclusion_forums_list($only_auth_view = true)
  454. {
  455. global $db, $config, $user, $lang, $tree;
  456. $sql_auth = "SELECT forum_id, parent_id, forum_name, auth_view, auth_read, auth_post FROM " . FORUMS_TABLE;
  457. $result_auth = $db->sql_query($sql_auth, 0, 'forums_excluded_list_', FORUMS_CACHE_FOLDER);
  458. $forum_data = array();
  459. while($row_auth = $db->sql_fetchrow($result_auth))
  460. {
  461. $forum_data[] = $row_auth;
  462. }
  463. $db->sql_freeresult($result_auth);
  464. $is_auth_ary = array();
  465. $is_auth_ary = auth(AUTH_ALL, AUTH_LIST_ALL, $user->data);
  466. $except_forums = '\'start\'';
  467. for($f = 0; $f < sizeof($forum_data); $f++)
  468. {
  469. $exclude_this = false;
  470. if((!$is_auth_ary[$forum_data[$f]['forum_id']]['auth_read']) || ((!$is_auth_ary[$forum_data[$f]['forum_id']]['auth_view']) && $only_auth_view))
  471. {
  472. $exclude_this = true;
  473. }
  474. // SELF AUTH - BEGIN
  475. // Comment the lines below if you wish to show RESERVED topics for AUTH_SELF.
  476. if(((($user->data['user_level'] != ADMIN) && ($user->data['user_level'] != MOD)) || (($user->data['user_level'] == MOD) && !$config['allow_mods_view_self'])) && (intval($is_auth_ary[$forum_data[$f]['forum_id']]['auth_read']) == AUTH_SELF))
  477. {
  478. $exclude_this = true;
  479. }
  480. // SELF AUTH - END
  481. if ($exclude_this)
  482. {
  483. if($except_forums == '\'start\'')
  484. {
  485. $except_forums = $forum_data[$f]['forum_id'];
  486. }
  487. else
  488. {
  489. $except_forums .= ',' . $forum_data[$f]['forum_id'];
  490. }
  491. }
  492. }
  493. return $except_forums;
  494. }
  495. /**
  496. * Builds a list of forums with read access
  497. */
  498. function build_allowed_forums_list($return_array = false)
  499. {
  500. global $db, $config, $user, $lang, $tree;
  501. $sql_auth = "SELECT forum_id, parent_id, forum_name, auth_view, auth_read, auth_post FROM " . FORUMS_TABLE;
  502. $result_auth = $db->sql_query($sql_auth, 0, 'forums_allowed_list_', FORUMS_CACHE_FOLDER);
  503. $forum_data = array();
  504. while($row_auth = $db->sql_fetchrow($result_auth))
  505. {
  506. $forum_data[] = $row_auth;
  507. }
  508. $db->sql_freeresult($result_auth);
  509. $is_auth_ary = array();
  510. $is_auth_ary = auth(AUTH_ALL, AUTH_LIST_ALL, $user->data);
  511. $allowed_forums_array = array();
  512. for($f = 0; $f < sizeof($forum_data); $f++)
  513. {
  514. $include_this = false;
  515. if($is_auth_ary[$forum_data[$f]['forum_id']]['auth_read'])
  516. {
  517. $include_this = true;
  518. }
  519. // SELF AUTH - BEGIN
  520. // Comment the lines below if you wish to show RESERVED topics for AUTH_SELF.
  521. if(((($user->data['user_level'] != ADMIN) && ($user->data['user_level'] != MOD)) || (($user->data['user_level'] == MOD) && !$config['allow_mods_view_self'])) && (intval($is_auth_ary[$forum_data[$f]['forum_id']]['auth_read']) == AUTH_SELF))
  522. {
  523. $include_this = false;
  524. }
  525. // SELF AUTH - END
  526. if ($include_this)
  527. {
  528. $allowed_forums_array[] = $forum_data[$f]['forum_id'];
  529. }
  530. }
  531. if ($return_array)
  532. {
  533. $allowed_forums = $allowed_forums_array;
  534. }
  535. else
  536. {
  537. $allowed_forums = !empty($allowed_forums_array) ? implode(',', $allowed_forums_array) : 0;
  538. }
  539. return $allowed_forums;
  540. }
  541. ?>