PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/auth.php

https://github.com/igorw-forks/icy_phoenix
PHP | 599 lines | 428 code | 66 blank | 105 comment | 99 complexity | 1c93167b2579700531da804794175641 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, $userdata, $f_access = '')
  58. {
  59. global $db, $config, $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 ($userdata['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 = " . $userdata['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 = (($userdata['user_level'] == ADMIN) && $userdata['session_logged_in']) ? true : 0;
  216. //$is_admin = ((($userdata['user_level'] == ADMIN) || ($userdata['user_level'] == JUNIOR_ADMIN)) && $userdata['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] = ($userdata['session_logged_in']) ? true : 0;
  243. // Check if the user is a BOT
  244. $auth_user[$key] = (($config['bots_reg_auth'] == true) && $userdata['is_bot']) ? true : $auth_user[$key];
  245. $auth_user[$key . '_type'] = $lang['Auth_Registered_Users'];
  246. break;
  247. // Self AUTH - BEGIN
  248. case AUTH_SELF:
  249. $auth_user[$key] = ($userdata['session_logged_in']) ? ((auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin)) ? true : AUTH_SELF) : 0;
  250. $auth_user[$key . '_type'] = $lang['Auth_Self_Users'];
  251. break;
  252. // Self AUTH - END
  253. case AUTH_ACL:
  254. $auth_user[$key] = ($userdata['session_logged_in']) ? auth_check_user(AUTH_ACL, $key, $u_access, $is_admin) : 0;
  255. $auth_user[$key . '_type'] = $lang['Auth_Users_granted_access'];
  256. break;
  257. case AUTH_MOD:
  258. $auth_user[$key] = ($userdata['session_logged_in']) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;
  259. $auth_user[$key . '_type'] = $lang['Auth_Moderators'];
  260. break;
  261. case AUTH_ADMIN:
  262. $auth_user[$key] = $is_admin;
  263. $auth_user[$key . '_type'] = $lang['Auth_Administrators'];
  264. break;
  265. case AUTH_NONE:
  266. $auth_user[$key] = 0;
  267. break;
  268. default:
  269. $auth_user[$key] = 0;
  270. break;
  271. }
  272. }
  273. else
  274. {
  275. for($k = 0; $k < sizeof($f_access); $k++)
  276. {
  277. $value = $f_access[$k][$key];
  278. $f_forum_id = $f_access[$k]['forum_id'];
  279. $u_access[$f_forum_id] = isset($u_access[$f_forum_id]) ? $u_access[$f_forum_id] : array();
  280. switch($value)
  281. {
  282. case AUTH_ALL:
  283. $auth_user[$f_forum_id][$key] = true;
  284. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Anonymous_Users'];
  285. break;
  286. case AUTH_REG:
  287. $auth_user[$f_forum_id][$key] = ($userdata['session_logged_in']) ? true : 0;
  288. // Check if the user is a BOT
  289. $auth_user[$f_forum_id][$key] = (($config['bots_reg_auth'] == true) && $userdata['is_bot']) ? true : $auth_user[$f_forum_id][$key];
  290. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Registered_Users'];
  291. break;
  292. // Self AUTH - BEGIN
  293. case AUTH_SELF:
  294. $auth_user[$f_forum_id][$key] = ($userdata['session_logged_in']) ? ((auth_check_user(AUTH_MOD, 'auth_mod', $u_access[$f_forum_id], $is_admin)) ? true : AUTH_SELF) : 0;
  295. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Self_Users'];
  296. break;
  297. // Self AUTH - END
  298. case AUTH_ACL:
  299. $auth_user[$f_forum_id][$key] = ($userdata['session_logged_in']) ? auth_check_user(AUTH_ACL, $key, $u_access[$f_forum_id], $is_admin) : 0;
  300. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Users_granted_access'];
  301. break;
  302. case AUTH_MOD:
  303. $auth_user[$f_forum_id][$key] = ($userdata['session_logged_in']) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access[$f_forum_id], $is_admin) : 0;
  304. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Moderators'];
  305. break;
  306. case AUTH_ADMIN:
  307. $auth_user[$f_forum_id][$key] = $is_admin;
  308. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Administrators'];
  309. break;
  310. case AUTH_NONE:
  311. $auth_user[$f_forum_id][$key] = 0;
  312. break;
  313. default:
  314. $auth_user[$f_forum_id][$key] = 0;
  315. break;
  316. }
  317. }
  318. }
  319. }
  320. // Is user a moderator?
  321. if ($forum_id != AUTH_LIST_ALL)
  322. {
  323. $auth_user['auth_mod'] = ($userdata['session_logged_in']) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;
  324. }
  325. else
  326. {
  327. for($k = 0; $k < sizeof($f_access); $k++)
  328. {
  329. $f_forum_id = $f_access[$k]['forum_id'];
  330. $u_access[$f_forum_id] = isset($u_access[$f_forum_id]) ? $u_access[$f_forum_id] : array();
  331. $auth_user[$f_forum_id]['auth_mod'] = ($userdata['session_logged_in']) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access[$f_forum_id], $is_admin) : 0;
  332. }
  333. }
  334. return $auth_user;
  335. }
  336. /*
  337. * Check user auth
  338. */
  339. function auth_check_user($type, $key, $u_access, $is_admin)
  340. {
  341. $auth_user = 0;
  342. if (sizeof($u_access))
  343. {
  344. for($j = 0; $j < sizeof($u_access); $j++)
  345. {
  346. $result = 0;
  347. switch($type)
  348. {
  349. case AUTH_ACL:
  350. $result = $u_access[$j][$key];
  351. case AUTH_MOD:
  352. $result = $result || $u_access[$j]['auth_mod'];
  353. case AUTH_ADMIN:
  354. $result = $result || $is_admin;
  355. break;
  356. }
  357. $auth_user = $auth_user || $result;
  358. }
  359. }
  360. else
  361. {
  362. $auth_user = $is_admin;
  363. }
  364. return $auth_user;
  365. }
  366. /*
  367. * Check auth level
  368. * Returns true in case the user has the requested level
  369. */
  370. function check_auth_level($level_required)
  371. {
  372. global $userdata, $config;
  373. if ($level_required == AUTH_ALL)
  374. {
  375. return true;
  376. }
  377. if ($userdata['user_level'] == ADMIN)
  378. {
  379. if (($level_required == AUTH_ADMIN) || ($level_required == AUTH_GUEST_ONLY))
  380. {
  381. return true;
  382. }
  383. if ($level_required == AUTH_FOUNDER)
  384. {
  385. $founder_id = (defined('FOUNDER_ID') ? FOUNDER_ID : get_founder_id());
  386. return ($userdata['user_id'] == $founder_id) ? true : false;
  387. }
  388. elseif ($level_required == AUTH_MAIN_ADMIN)
  389. {
  390. if (defined('MAIN_ADMINS_ID'))
  391. {
  392. $allowed_admins = explode(',', MAIN_ADMINS_ID);
  393. return (in_array($userdata['user_id'], $allowed_admins)) ? true : false;
  394. }
  395. }
  396. }
  397. // Before going on... if level_required is for Guests only then check if the user is a guest but not a bot...
  398. if ($level_required == AUTH_GUEST_ONLY)
  399. {
  400. return (!$userdata['is_bot'] && !$userdata['session_logged_in']) ? true : false;
  401. }
  402. // Force to AUTH_ADMIN since we already checked all cases for founder or main admins
  403. if (($level_required == AUTH_FOUNDER) || ($level_required == AUTH_MAIN_ADMIN))
  404. {
  405. $level_required = AUTH_ADMIN;
  406. }
  407. // Access level required is at least REG and user is not an admin!
  408. // Remember that Junior Admin has the ADMIN level while not in CMS or ACP
  409. $not_auth = false;
  410. // Check if the user is REG or a BOT
  411. $is_reg = (($config['bots_reg_auth'] && $userdata['is_bot']) || $userdata['session_logged_in']) ? true : false;
  412. $not_auth = (!$not_auth && ($level_required == AUTH_REG) && !$is_reg) ? true : $not_auth;
  413. $not_auth = (!$not_auth && ($level_required == AUTH_MOD) && ($userdata['user_level'] != MOD) && ($userdata['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 $userdata, $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 (!$userdata['is_bot'] && !$userdata['session_logged_in'])
  437. {
  438. $page_array = array();
  439. $page_array = extract_current_page(IP_ROOT_PATH);
  440. redirect(append_sid(IP_ROOT_PATH . 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, $userdata, $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, $userdata);
  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(((($userdata['user_level'] != ADMIN) && ($userdata['user_level'] != MOD)) || (($userdata['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()
  499. {
  500. global $db, $config, $userdata, $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, $userdata);
  511. $allowed_forums = '0';
  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(((($userdata['user_level'] != ADMIN) && ($userdata['user_level'] != MOD)) || (($userdata['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 .= ', ' . $forum_data[$f]['forum_id'];
  529. }
  530. }
  531. return $allowed_forums;
  532. }
  533. ?>