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

/modules/PNphpBB2/includes/auth.php

https://gitlab.com/bulwye/reliquerunt
PHP | 334 lines | 222 code | 40 blank | 72 comment | 26 complexity | fa965d739beb61f972c509cdb090da5e MD5 | raw file
  1. <?php
  2. /***************************************************************************
  3. * auth.php
  4. * -------------------
  5. * begin : Saturday, Feb 13, 2001
  6. * copyright : (C) 2001 The phpBB Group
  7. * email : support@phpbb.com
  8. *
  9. * $Id: auth.php,v 1.2 2006/04/28 17:49:44 adrianc602 Exp $
  10. *
  11. *
  12. ***************************************************************************/
  13. /***************************************************************************
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. ***************************************************************************/
  21. /*
  22. $type's accepted (pre-pend with AUTH_):
  23. VIEW, READ, POST, REPLY, EDIT, DELETE, STICKY, ANNOUNCE, VOTE, POLLCREATE
  24. Possible options ($type/forum_id combinations):
  25. * If you include a type and forum_id then a specific lookup will be done and
  26. the single result returned
  27. * If you set type to AUTH_ALL and specify a forum_id an array of all auth types
  28. will be returned
  29. * If you provide a forum_id a specific lookup on that forum will be done
  30. * If you set forum_id to AUTH_LIST_ALL and specify a type an array listing the
  31. results for all forums will be returned
  32. * If you set forum_id to AUTH_LIST_ALL and type to AUTH_ALL a multidimensional
  33. array containing the auth permissions for all types and all forums for that
  34. user is returned
  35. All results are returned as associative arrays, even when a single auth type is
  36. specified.
  37. If available you can send an array (either one or two dimensional) containing the
  38. forum auth levels, this will prevent the auth function having to do its own
  39. lookup
  40. */
  41. function auth($type, $forum_id, $userdata, $f_access = '')
  42. {
  43. global $db, $lang;
  44. switch( $type )
  45. {
  46. case AUTH_ALL:
  47. $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_vote, a.auth_pollcreate';
  48. $auth_fields = array('auth_view', 'auth_read', 'auth_post', 'auth_reply', 'auth_edit', 'auth_delete', 'auth_sticky', 'auth_announce', 'auth_vote', 'auth_pollcreate');
  49. break;
  50. case AUTH_VIEW:
  51. $a_sql = 'a.auth_view';
  52. $auth_fields = array('auth_view');
  53. break;
  54. case AUTH_READ:
  55. $a_sql = 'a.auth_read';
  56. $auth_fields = array('auth_read');
  57. break;
  58. case AUTH_POST:
  59. $a_sql = 'a.auth_post';
  60. $auth_fields = array('auth_post');
  61. break;
  62. case AUTH_REPLY:
  63. $a_sql = 'a.auth_reply';
  64. $auth_fields = array('auth_reply');
  65. break;
  66. case AUTH_EDIT:
  67. $a_sql = 'a.auth_edit';
  68. $auth_fields = array('auth_edit');
  69. break;
  70. case AUTH_DELETE:
  71. $a_sql = 'a.auth_delete';
  72. $auth_fields = array('auth_delete');
  73. break;
  74. case AUTH_ANNOUNCE:
  75. $a_sql = 'a.auth_announce';
  76. $auth_fields = array('auth_announce');
  77. break;
  78. case AUTH_STICKY:
  79. $a_sql = 'a.auth_sticky';
  80. $auth_fields = array('auth_sticky');
  81. break;
  82. case AUTH_POLLCREATE:
  83. $a_sql = 'a.auth_pollcreate';
  84. $auth_fields = array('auth_pollcreate');
  85. break;
  86. case AUTH_VOTE:
  87. $a_sql = 'a.auth_vote';
  88. $auth_fields = array('auth_vote');
  89. break;
  90. case AUTH_ATTACH:
  91. break;
  92. default:
  93. break;
  94. }
  95. /* -- mod : File Attachment Mod v2 Version 2.4.3 ---------------------------------------------------- */
  96. if (!intval($attach_config['disable_mod']))
  97. {
  98. attach_setup_basic_auth($type, $auth_fields, $a_sql);
  99. }
  100. /* -- fin : File Attachment Mod v2 Version 2.4.3 ---------------------------------------------------- */
  101. //
  102. // If f_access has been passed, or auth is needed to return an array of forums
  103. // then we need to pull the auth information on the given forum (or all forums)
  104. //
  105. if ( empty($f_access) )
  106. {
  107. $forum_match_sql = ( $forum_id != AUTH_LIST_ALL ) ? "WHERE a.forum_id = $forum_id" : '';
  108. $sql = "SELECT a.forum_id, $a_sql
  109. FROM " . FORUMS_TABLE . " a
  110. $forum_match_sql";
  111. if ( !($result = $db->sql_query($sql)) )
  112. {
  113. message_die(GENERAL_ERROR, 'Failed obtaining forum access control lists', '', __LINE__, __FILE__, $sql);
  114. }
  115. $sql_fetchrow = ( $forum_id != AUTH_LIST_ALL ) ? 'sql_fetchrow' : 'sql_fetchrowset';
  116. if ( !($f_access = $db->$sql_fetchrow($result)) )
  117. {
  118. $db->sql_freeresult($result);
  119. return array();
  120. }
  121. $db->sql_freeresult($result);
  122. }
  123. //
  124. // If the user isn't logged on then all we need do is check if the forum
  125. // has the type set to ALL, if yes they are good to go, if not then they
  126. // are denied access
  127. //
  128. $u_access = array();
  129. if ( $userdata['session_logged_in'] )
  130. {
  131. $forum_match_sql = ( $forum_id != AUTH_LIST_ALL ) ? "AND a.forum_id = $forum_id" : '';
  132. $sql = "SELECT a.forum_id, $a_sql, a.auth_mod
  133. FROM " . AUTH_ACCESS_TABLE . " a, " . USER_GROUP_TABLE . " ug
  134. WHERE ug.user_id = ".$userdata['user_id']. "
  135. AND ug.user_pending = 0
  136. AND a.group_id = ug.group_id
  137. $forum_match_sql";
  138. if ( !($result = $db->sql_query($sql)) )
  139. {
  140. message_die(GENERAL_ERROR, 'Failed obtaining forum access control lists', '', __LINE__, __FILE__, $sql);
  141. }
  142. if ( $row = $db->sql_fetchrow($result) )
  143. {
  144. do
  145. {
  146. if ( $forum_id != AUTH_LIST_ALL)
  147. {
  148. $u_access[] = $row;
  149. }
  150. else
  151. {
  152. $u_access[$row['forum_id']][] = $row;
  153. }
  154. }
  155. while( $row = $db->sql_fetchrow($result) );
  156. }
  157. $db->sql_freeresult($result);
  158. }
  159. $is_admin = ( $userdata['user_level'] == ADMIN && $userdata['session_logged_in'] ) ? TRUE : 0;
  160. $auth_user = array();
  161. for($i = 0; $i < count($auth_fields); $i++)
  162. {
  163. $key = $auth_fields[$i];
  164. //
  165. // If the user is logged on and the forum type is either ALL or REG then the user has access
  166. //
  167. // If the type if ACL, MOD or ADMIN then we need to see if the user has specific permissions
  168. // to do whatever it is they want to do ... to do this we pull relevant information for the
  169. // user (and any groups they belong to)
  170. //
  171. // Now we compare the users access level against the forums. We assume here that a moderator
  172. // and admin automatically have access to an ACL forum, similarly we assume admins meet an
  173. // auth requirement of MOD
  174. //
  175. if ( $forum_id != AUTH_LIST_ALL )
  176. {
  177. $value = $f_access[$key];
  178. switch( $value )
  179. {
  180. case AUTH_ALL:
  181. $auth_user[$key] = TRUE;
  182. $auth_user[$key . '_type'] = $lang['Auth_Anonymous_Users'];
  183. break;
  184. case AUTH_REG:
  185. $auth_user[$key] = ( $userdata['session_logged_in'] ) ? TRUE : 0;
  186. $auth_user[$key . '_type'] = $lang['Auth_Registered_Users'];
  187. break;
  188. case AUTH_ACL:
  189. $auth_user[$key] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_ACL, $key, $u_access, $is_admin) : 0;
  190. $auth_user[$key . '_type'] = $lang['Auth_Users_granted_access'];
  191. break;
  192. case AUTH_MOD:
  193. $auth_user[$key] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;
  194. $auth_user[$key . '_type'] = $lang['Auth_Moderators'];
  195. break;
  196. case AUTH_ADMIN:
  197. $auth_user[$key] = $is_admin;
  198. $auth_user[$key . '_type'] = $lang['Auth_Administrators'];
  199. break;
  200. default:
  201. $auth_user[$key] = 0;
  202. break;
  203. }
  204. }
  205. else
  206. {
  207. for($k = 0; $k < count($f_access); $k++)
  208. {
  209. $value = $f_access[$k][$key];
  210. $f_forum_id = $f_access[$k]['forum_id'];
  211. $u_access[$f_forum_id] = isset($u_access[$f_forum_id]) ? $u_access[$f_forum_id] : array();
  212. switch( $value )
  213. {
  214. case AUTH_ALL:
  215. $auth_user[$f_forum_id][$key] = TRUE;
  216. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Anonymous_Users'];
  217. break;
  218. case AUTH_REG:
  219. $auth_user[$f_forum_id][$key] = ( $userdata['session_logged_in'] ) ? TRUE : 0;
  220. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Registered_Users'];
  221. break;
  222. case AUTH_ACL:
  223. $auth_user[$f_forum_id][$key] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_ACL, $key, $u_access[$f_forum_id], $is_admin) : 0;
  224. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Users_granted_access'];
  225. break;
  226. case AUTH_MOD:
  227. $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;
  228. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Moderators'];
  229. break;
  230. case AUTH_ADMIN:
  231. $auth_user[$f_forum_id][$key] = $is_admin;
  232. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Administrators'];
  233. break;
  234. default:
  235. $auth_user[$f_forum_id][$key] = 0;
  236. break;
  237. }
  238. }
  239. }
  240. }
  241. //
  242. // Is user a moderator?
  243. //
  244. if ( $forum_id != AUTH_LIST_ALL )
  245. {
  246. $auth_user['auth_mod'] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;
  247. }
  248. else
  249. {
  250. for($k = 0; $k < count($f_access); $k++)
  251. {
  252. $f_forum_id = $f_access[$k]['forum_id'];
  253. $u_access[$f_forum_id] = isset($u_access[$f_forum_id]) ? $u_access[$f_forum_id] : array();
  254. $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;
  255. }
  256. }
  257. return $auth_user;
  258. }
  259. function auth_check_user($type, $key, $u_access, $is_admin)
  260. {
  261. $auth_user = 0;
  262. if ( count($u_access) )
  263. {
  264. for($j = 0; $j < count($u_access); $j++)
  265. {
  266. $result = 0;
  267. switch($type)
  268. {
  269. case AUTH_ACL:
  270. $result = $u_access[$j][$key];
  271. case AUTH_MOD:
  272. $result = $result || $u_access[$j]['auth_mod'];
  273. case AUTH_ADMIN:
  274. $result = $result || $is_admin;
  275. break;
  276. }
  277. $auth_user = $auth_user || $result;
  278. }
  279. }
  280. else
  281. {
  282. $auth_user = $is_admin;
  283. }
  284. return $auth_user;
  285. }
  286. ?>