Просмотр файла includes/auth.php

Размер файла: 6.79Kb
  1. <?php
  2. /***************************************************************************
  3. * mides.ru
  4. * -------------------
  5. ***************************************************************************/
  6. function auth($type, $forum_id, $userdata, $f_access = '')
  7. {
  8. global $db, $lang;
  9.  
  10. switch( $type )
  11. {
  12. case AUTH_ALL:
  13. $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';
  14. $auth_fields = array('auth_view', 'auth_read', 'auth_post', 'auth_reply', 'auth_edit', 'auth_delete', 'auth_sticky', 'auth_announce', 'auth_vote', 'auth_pollcreate');
  15. break;
  16.  
  17. case AUTH_VIEW:
  18. $a_sql = 'a.auth_view';
  19. $auth_fields = array('auth_view');
  20. break;
  21.  
  22. case AUTH_READ:
  23. $a_sql = 'a.auth_read';
  24. $auth_fields = array('auth_read');
  25. break;
  26. case AUTH_POST:
  27. $a_sql = 'a.auth_post';
  28. $auth_fields = array('auth_post');
  29. break;
  30. case AUTH_REPLY:
  31. $a_sql = 'a.auth_reply';
  32. $auth_fields = array('auth_reply');
  33. break;
  34. case AUTH_EDIT:
  35. $a_sql = 'a.auth_edit';
  36. $auth_fields = array('auth_edit');
  37. break;
  38. case AUTH_DELETE:
  39. $a_sql = 'a.auth_delete';
  40. $auth_fields = array('auth_delete');
  41. break;
  42.  
  43. case AUTH_ANNOUNCE:
  44. $a_sql = 'a.auth_announce';
  45. $auth_fields = array('auth_announce');
  46. break;
  47. case AUTH_STICKY:
  48. $a_sql = 'a.auth_sticky';
  49. $auth_fields = array('auth_sticky');
  50. break;
  51.  
  52. case AUTH_POLLCREATE:
  53. $a_sql = 'a.auth_pollcreate';
  54. $auth_fields = array('auth_pollcreate');
  55. break;
  56. case AUTH_VOTE:
  57. $a_sql = 'a.auth_vote';
  58. $auth_fields = array('auth_vote');
  59. break;
  60. case AUTH_ATTACH:
  61. break;
  62.  
  63. default:
  64. break;
  65. }
  66. attach_setup_basic_auth($type, $auth_fields, $a_sql);
  67.  
  68. if ( empty($f_access) )
  69. {
  70. $forum_match_sql = ( $forum_id != AUTH_LIST_ALL ) ? "WHERE a.forum_id = $forum_id" : "";
  71.  
  72. $sql = "SELECT a.forum_id, $a_sql
  73. FROM " . FORUMS_TABLE . " a
  74. $forum_match_sql";
  75. if ( !($result = $db->sql_query($sql)) )
  76. {
  77. message_die(GENERAL_ERROR, 'Failed obtaining forum access control lists', '', __LINE__, __FILE__, $sql);
  78. }
  79.  
  80. $sql_fetchrow = ( $forum_id != AUTH_LIST_ALL ) ? 'sql_fetchrow' : 'sql_fetchrowset';
  81.  
  82. if ( !($f_access = $db->$sql_fetchrow($result)) )
  83. {
  84. $db->sql_freeresult($result);
  85. return array();
  86. }
  87. $db->sql_freeresult($result);
  88. }
  89.  
  90. $u_access = array();
  91. if ( $userdata['session_logged_in'] )
  92. {
  93. $forum_match_sql = ( $forum_id != AUTH_LIST_ALL ) ? "AND a.forum_id = $forum_id" : '';
  94.  
  95. $sql = "SELECT a.forum_id, $a_sql, a.auth_mod
  96. FROM " . AUTH_ACCESS_TABLE . " a, " . USER_GROUP_TABLE . " ug
  97. WHERE ug.user_id = ".$userdata['user_id']. "
  98. AND ug.user_pending = 0
  99. AND a.group_id = ug.group_id
  100. $forum_match_sql";
  101. if ( !($result = $db->sql_query($sql)) )
  102. {
  103. message_die(GENERAL_ERROR, 'Failed obtaining forum access control lists', '', __LINE__, __FILE__, $sql);
  104. }
  105.  
  106. if ( $row = $db->sql_fetchrow($result) )
  107. {
  108. do
  109. {
  110. if ( $forum_id != AUTH_LIST_ALL)
  111. {
  112. $u_access[] = $row;
  113. }
  114. else
  115. {
  116. $u_access[$row['forum_id']][] = $row;
  117. }
  118. }
  119. while( $row = $db->sql_fetchrow($result) );
  120. }
  121. $db->sql_freeresult($result);
  122. }
  123.  
  124. $is_admin = ( $userdata['user_level'] == ADMIN && $userdata['session_logged_in'] ) ? TRUE : 0;
  125.  
  126. $auth_user = array();
  127. for($i = 0; $i < count($auth_fields); $i++)
  128. {
  129. $key = $auth_fields[$i];
  130.  
  131. if ( $forum_id != AUTH_LIST_ALL )
  132. {
  133. $value = $f_access[$key];
  134.  
  135. switch( $value )
  136. {
  137. case AUTH_ALL:
  138. $auth_user[$key] = TRUE;
  139. $auth_user[$key . '_type'] = $lang['Auth_Anonymous_Users'];
  140. break;
  141.  
  142. case AUTH_REG:
  143. $auth_user[$key] = ( $userdata['session_logged_in'] ) ? TRUE : 0;
  144. $auth_user[$key . '_type'] = $lang['Auth_Registered_Users'];
  145. break;
  146.  
  147. case AUTH_ACL:
  148. $auth_user[$key] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_ACL, $key, $u_access, $is_admin) : 0;
  149. $auth_user[$key . '_type'] = $lang['Auth_Users_granted_access'];
  150. break;
  151.  
  152. case AUTH_MOD:
  153. $auth_user[$key] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;
  154. $auth_user[$key . '_type'] = $lang['Auth_Moderators'];
  155. break;
  156.  
  157. case AUTH_ADMIN:
  158. $auth_user[$key] = $is_admin;
  159. $auth_user[$key . '_type'] = $lang['Auth_Administrators'];
  160. break;
  161.  
  162. default:
  163. $auth_user[$key] = 0;
  164. break;
  165. }
  166. }
  167. else
  168. {
  169. for($k = 0; $k < count($f_access); $k++)
  170. {
  171. $value = $f_access[$k][$key];
  172. $f_forum_id = $f_access[$k]['forum_id'];
  173. $u_access[$f_forum_id] = isset($u_access[$f_forum_id]) ? $u_access[$f_forum_id] : array();
  174.  
  175. switch( $value )
  176. {
  177. case AUTH_ALL:
  178. $auth_user[$f_forum_id][$key] = TRUE;
  179. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Anonymous_Users'];
  180. break;
  181.  
  182. case AUTH_REG:
  183. $auth_user[$f_forum_id][$key] = ( $userdata['session_logged_in'] ) ? TRUE : 0;
  184. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Registered_Users'];
  185. break;
  186.  
  187. case AUTH_ACL:
  188. $auth_user[$f_forum_id][$key] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_ACL, $key, $u_access[$f_forum_id], $is_admin) : 0;
  189. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Users_granted_access'];
  190. break;
  191.  
  192. case AUTH_MOD:
  193. $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;
  194. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Moderators'];
  195. break;
  196.  
  197. case AUTH_ADMIN:
  198. $auth_user[$f_forum_id][$key] = $is_admin;
  199. $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Administrators'];
  200. break;
  201.  
  202. default:
  203. $auth_user[$f_forum_id][$key] = 0;
  204. break;
  205. }
  206. }
  207. }
  208. }
  209.  
  210. if ( $forum_id != AUTH_LIST_ALL )
  211. {
  212. $auth_user['auth_mod'] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;
  213. }
  214. else
  215. {
  216. for($k = 0; $k < count($f_access); $k++)
  217. {
  218. $f_forum_id = $f_access[$k]['forum_id'];
  219. $u_access[$f_forum_id] = isset($u_access[$f_forum_id]) ? $u_access[$f_forum_id] : array();
  220.  
  221. $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;
  222. }
  223. }
  224.  
  225. return $auth_user;
  226. }
  227.  
  228. function auth_check_user($type, $key, $u_access, $is_admin)
  229. {
  230. $auth_user = 0;
  231.  
  232. if ( count($u_access) )
  233. {
  234. for($j = 0; $j < count($u_access); $j++)
  235. {
  236. $result = 0;
  237. switch($type)
  238. {
  239. case AUTH_ACL:
  240. $result = $u_access[$j][$key];
  241.  
  242. case AUTH_MOD:
  243. $result = $result || $u_access[$j]['auth_mod'];
  244.  
  245. case AUTH_ADMIN:
  246. $result = $result || $is_admin;
  247. break;
  248. }
  249.  
  250. $auth_user = $auth_user || $result;
  251. }
  252. }
  253. else
  254. {
  255. $auth_user = $is_admin;
  256. }
  257.  
  258. return $auth_user;
  259. }
  260.  
  261. ?>