home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / unitednuke / unitednuke.exe / html / includes / auth.php next >
PHP Script  |  2004-01-10  |  17KB  |  363 lines

  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.37.2.3 2003/02/25 16:02:59 acydburn Exp $
  10.  *
  11.  *
  12.  ***************************************************************************/
  13. /***************************************************************************
  14. * phpbb2 forums port version 2.0.5 (c) 2003 - Nuke Cops (http://nukecops.com)
  15. *
  16. * Ported by Nuke Cops to phpbb2 standalone 2.0.5 Test
  17. * and debugging completed by the Elite Nukers and site members.
  18. *
  19. * You run this package at your sole risk. Nuke Cops and affiliates cannot
  20. * be held liable if anything goes wrong. You are advised to test this
  21. * package on a development system. Backup everything before implementing
  22. * in a production environment. If something goes wrong, you can always
  23. * backout and restore your backups.
  24. *
  25. * Installing and running this also means you agree to the terms of the AUP
  26. * found at Nuke Cops.
  27. *
  28. * This is version 2.0.5 of the phpbb2 forum port for PHP-Nuke. Work is based
  29. * on Tom Nitzschner's forum port version 2.0.6. Tom's 2.0.6 port was based
  30. * on the phpbb2 standalone version 2.0.3. Our version 2.0.5 from Nuke Cops is
  31. * now reflecting phpbb2 standalone 2.0.5 that fixes some bugs and the
  32. * invalid_session error message.
  33. ***************************************************************************/
  34. /***************************************************************************
  35.  *   This file is part of the phpBB2 port to Nuke 6.0 (c) copyright 2002
  36.  *   by Tom Nitzschner (tom@toms-home.com)
  37.  *   http://bbtonuke.sourceforge.net (or http://www.toms-home.com)
  38.  *
  39.  *   As always, make a backup before messing with anything. All code
  40.  *   release by me is considered sample code only. It may be fully
  41.  *   functual, but you use it at your own risk, if you break it,
  42.  *   you get to fix it too. No waranty is given or implied.
  43.  *
  44.  *   Please post all questions/request about this port on http://bbtonuke.sourceforge.net first,
  45.  *   then on my site. All original header code and copyright messages will be maintained
  46.  *   to give credit where credit is due. If you modify this, the only requirement is
  47.  *   that you also maintain all original copyright messages. All my work is released
  48.  *   under the GNU GENERAL PUBLIC LICENSE. Please see the README for more information.
  49.  *
  50.  ***************************************************************************/
  51. /***************************************************************************
  52.  *
  53.  *   This program is free software; you can redistribute it and/or modify
  54.  *   it under the terms of the GNU General Public License as published by
  55.  *   the Free Software Foundation; either version 2 of the License, or
  56.  *   (at your option) any later version.
  57.  *
  58.  ***************************************************************************/
  59.  
  60. /*
  61.         $type's accepted (pre-pend with AUTH_):
  62.         VIEW, READ, POST, REPLY, EDIT, DELETE, STICKY, ANNOUNCE, VOTE, POLLCREATE
  63.  
  64.         Possible options ($type/forum_id combinations):
  65.  
  66.         * If you include a type and forum_id then a specific lookup will be done and
  67.         the single result returned
  68.  
  69.         * If you set type to AUTH_ALL and specify a forum_id an array of all auth types
  70.         will be returned
  71.  
  72.         * If you provide a forum_id a specific lookup on that forum will be done
  73.  
  74.         * If you set forum_id to AUTH_LIST_ALL and specify a type an array listing the
  75.         results for all forums will be returned
  76.  
  77.         * If you set forum_id to AUTH_LIST_ALL and type to AUTH_ALL a multidimensional
  78.         array containing the auth permissions for all types and all forums for that
  79.         user is returned
  80.  
  81.         All results are returned as associative arrays, even when a single auth type is
  82.         specified.
  83.  
  84.         If available you can send an array (either one or two dimensional) containing the
  85.         forum auth levels, this will prevent the auth function having to do its own
  86.         lookup
  87. */
  88. function auth($type, $forum_id, $userdata, $f_access = '')
  89. {
  90.         global $db, $lang;
  91.  
  92.         switch( $type )
  93.         {
  94.                 case AUTH_ALL:
  95.                         $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';
  96.                         $auth_fields = array('auth_view', 'auth_read', 'auth_post', 'auth_reply', 'auth_edit', 'auth_delete', 'auth_sticky', 'auth_announce', 'auth_vote', 'auth_pollcreate');
  97.                         break;
  98.  
  99.                 case AUTH_VIEW:
  100.                         $a_sql = 'a.auth_view';
  101.                         $auth_fields = array('auth_view');
  102.                         break;
  103.  
  104.                 case AUTH_READ:
  105.                         $a_sql = 'a.auth_read';
  106.                         $auth_fields = array('auth_read');
  107.                         break;
  108.                 case AUTH_POST:
  109.                         $a_sql = 'a.auth_post';
  110.                         $auth_fields = array('auth_post');
  111.                         break;
  112.                 case AUTH_REPLY:
  113.                         $a_sql = 'a.auth_reply';
  114.                         $auth_fields = array('auth_reply');
  115.                         break;
  116.                 case AUTH_EDIT:
  117.                         $a_sql = 'a.auth_edit';
  118.                         $auth_fields = array('auth_edit');
  119.                         break;
  120.                 case AUTH_DELETE:
  121.                         $a_sql = 'a.auth_delete';
  122.                         $auth_fields = array('auth_delete');
  123.                         break;
  124.  
  125.                 case AUTH_ANNOUNCE:
  126.                         $a_sql = 'a.auth_announce';
  127.                         $auth_fields = array('auth_announce');
  128.                         break;
  129.                 case AUTH_STICKY:
  130.                         $a_sql = 'a.auth_sticky';
  131.                         $auth_fields = array('auth_sticky');
  132.                         break;
  133.  
  134.                 case AUTH_POLLCREATE:
  135.                         $a_sql = 'a.auth_pollcreate';
  136.                         $auth_fields = array('auth_pollcreate');
  137.                         break;
  138.                 case AUTH_VOTE:
  139.                         $a_sql = 'a.auth_vote';
  140.                         $auth_fields = array('auth_vote');
  141.                         break;
  142.                 case AUTH_ATTACH:
  143.                         break;
  144.  
  145.                 default:
  146.                         break;
  147.         }
  148.  
  149.         //
  150.         // If f_access has been passed, or auth is needed to return an array of forums
  151.         // then we need to pull the auth information on the given forum (or all forums)
  152.         //
  153.         if ( empty($f_access) )
  154.         {
  155.                 $forum_match_sql = ( $forum_id != AUTH_LIST_ALL ) ? "WHERE a.forum_id = $forum_id" : '';
  156.  
  157.                 $sql = "SELECT a.forum_id, $a_sql
  158.                         FROM " . FORUMS_TABLE . " a
  159.                         $forum_match_sql";
  160.                 if ( !($result = $db->sql_query($sql)) )
  161.                 {
  162.                         message_die(GENERAL_ERROR, 'Failed obtaining forum access control lists', '', __LINE__, __FILE__, $sql);
  163.                 }
  164.  
  165.                 $sql_fetchrow = ( $forum_id != AUTH_LIST_ALL ) ? 'sql_fetchrow' : 'sql_fetchrowset';
  166.  
  167.                 if ( !($f_access = $db->$sql_fetchrow($result)) )
  168.                 {
  169.                         $db->sql_freeresult($result);
  170.                         return array();
  171.                 }
  172.  
  173.                 $db->sql_freeresult($result);
  174.         }
  175.  
  176.         //
  177.         // If the user isn't logged on then all we need do is check if the forum
  178.         // has the type set to ALL, if yes they are good to go, if not then they
  179.         // are denied access
  180.         //
  181.         $u_access = array();
  182.         if ( $userdata['session_logged_in'] )
  183.         {
  184.                 $forum_match_sql = ( $forum_id != AUTH_LIST_ALL ) ? "AND a.forum_id = $forum_id" : '';
  185.  
  186.                 $sql = "SELECT a.forum_id, $a_sql, a.auth_mod
  187.                         FROM " . AUTH_ACCESS_TABLE . " a, " . USER_GROUP_TABLE . " ug
  188.                         WHERE ug.user_id = ".$userdata['user_id']. "
  189.                                 AND ug.user_pending = 0
  190.                                 AND a.group_id = ug.group_id
  191.                                 $forum_match_sql";
  192.                 if ( !($result = $db->sql_query($sql)) )
  193.                 {
  194.                         message_die(GENERAL_ERROR, 'Failed obtaining forum access control lists', '', __LINE__, __FILE__, $sql);
  195.                 }
  196.  
  197.                 if ( $row = $db->sql_fetchrow($result) )
  198.                 {
  199.                         do
  200.                         {
  201.                                 if ( $forum_id != AUTH_LIST_ALL)
  202.                                 {
  203.                                         $u_access[] = $row;
  204.                                 }
  205.                                 else
  206.                                 {
  207.                                         $u_access[$row['forum_id']][] = $row;
  208.                                 }
  209.                         }
  210.                         while( $row = $db->sql_fetchrow($result) );
  211.                 }
  212.         }
  213.  
  214.         $is_admin = ( $userdata['user_level'] == ADMIN && $userdata['session_logged_in'] ) ? TRUE : 0;
  215.  
  216.         $auth_user = array();
  217.         for($i = 0; $i < count($auth_fields); $i++)
  218.         {
  219.                 $key = $auth_fields[$i];
  220.  
  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.  
  236.                         switch( $value )
  237.                         {
  238.                                 case AUTH_ALL:
  239.                                         $auth_user[$key] = TRUE;
  240.                                         $auth_user[$key . '_type'] = $lang['Auth_Anonymous_Users'];
  241.                                         break;
  242.  
  243.                                 case AUTH_REG:
  244.                                         $auth_user[$key] = ( $userdata['session_logged_in'] ) ? TRUE : 0;
  245.                                         $auth_user[$key . '_type'] = $lang['Auth_Registered_Users'];
  246.                                         break;
  247.  
  248.                                 case AUTH_ACL:
  249.                                         $auth_user[$key] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_ACL, $key, $u_access, $is_admin) : 0;
  250.                                         $auth_user[$key . '_type'] = $lang['Auth_Users_granted_access'];
  251.                                         break;
  252.  
  253.                                 case AUTH_MOD:
  254.                                         $auth_user[$key] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;
  255.                                         $auth_user[$key . '_type'] = $lang['Auth_Moderators'];
  256.                                         break;
  257.  
  258.                                 case AUTH_ADMIN:
  259.                                         $auth_user[$key] = $is_admin;
  260.                                         $auth_user[$key . '_type'] = $lang['Auth_Administrators'];
  261.                                         break;
  262.  
  263.                                 default:
  264.                                         $auth_user[$key] = 0;
  265.                                         break;
  266.                         }
  267.                 }
  268.                 else
  269.                 {
  270.                         for($k = 0; $k < count($f_access); $k++)
  271.                         {
  272.                                 $value = $f_access[$k][$key];
  273.                                 $f_forum_id = $f_access[$k]['forum_id'];
  274.  
  275.                                 switch( $value )
  276.                                 {
  277.                                         case AUTH_ALL:
  278.                                                 $auth_user[$f_forum_id][$key] = TRUE;
  279.                                                 $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Anonymous_Users'];
  280.                                                 break;
  281.  
  282.                                         case AUTH_REG:
  283.                                                 $auth_user[$f_forum_id][$key] = ( $userdata['session_logged_in'] ) ? TRUE : 0;
  284.                                                 $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Registered_Users'];
  285.                                                 break;
  286.  
  287.                                         case AUTH_ACL:
  288.                                                 $auth_user[$f_forum_id][$key] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_ACL, $key, $u_access[$f_forum_id], $is_admin) : 0;
  289.                                                 $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Users_granted_access'];
  290.                                                 break;
  291.  
  292.                                         case AUTH_MOD:
  293.                                                 $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;
  294.                                                 $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Moderators'];
  295.                                                 break;
  296.  
  297.                                         case AUTH_ADMIN:
  298.                                                 $auth_user[$f_forum_id][$key] = $is_admin;
  299.                                                 $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Administrators'];
  300.                                                 break;
  301.  
  302.                                         default:
  303.                                                 $auth_user[$f_forum_id][$key] = 0;
  304.                                                 break;
  305.                                 }
  306.                         }
  307.                 }
  308.         }
  309.  
  310.         //
  311.         // Is user a moderator?
  312.         //
  313.         if ( $forum_id != AUTH_LIST_ALL )
  314.         {
  315.                 $auth_user['auth_mod'] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;
  316.         }
  317.         else
  318.         {
  319.                 for($k = 0; $k < count($f_access); $k++)
  320.                 {
  321.                         $f_forum_id = $f_access[$k]['forum_id'];
  322.  
  323.                         $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;
  324.                 }
  325.         }
  326.  
  327.         return $auth_user;
  328. }
  329.  
  330. function auth_check_user($type, $key, $u_access, $is_admin)
  331. {
  332.         $auth_user = 0;
  333.  
  334.         if ( count($u_access) )
  335.         {
  336.                 for($j = 0; $j < count($u_access); $j++)
  337.                 {
  338.                         $result = 0;
  339.                         switch($type)
  340.                         {
  341.                                 case AUTH_ACL:
  342.                                         $result = $u_access[$j][$key];
  343.  
  344.                                 case AUTH_MOD:
  345.                                         $result = $result || $u_access[$j]['auth_mod'];
  346.  
  347.                                 case AUTH_ADMIN:
  348.                                         $result = $result || $is_admin;
  349.                                         break;
  350.                         }
  351.  
  352.                         $auth_user = $auth_user || $result;
  353.                 }
  354.         }
  355.         else
  356.         {
  357.                 $auth_user = $is_admin;
  358.         }
  359.  
  360.         return $auth_user;
  361. }
  362.  
  363. ?>