gatekeeper
[ class tree: gatekeeper ] [ index: gatekeeper ] [ all elements ]

Source for file LibertyGatekeeper.php

Documentation is available at LibertyGatekeeper.php

  1. <?php
  2. /**
  3.  * @version $Header: /cvsroot/bitweaver/_bit_gatekeeper/LibertyGatekeeper.php,v 1.18 2006/11/16 23:15:01 spiderr Exp $
  4.  *
  5.  *  Copyright (c) 2004 bitweaver.org
  6.  *  Copyright (c) 2003 tikwiki.org
  7.  *  Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
  8.  *  All Rights Reserved. See copyright.txt for details and a complete list of authors.
  9.  *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details
  10.  *
  11.  *  $Id: LibertyGatekeeper.php,v 1.18 2006/11/16 23:15:01 spiderr Exp $
  12.  * @package gatekeeper
  13.  */
  14.  
  15. /**
  16.  * required setup
  17.  */
  18. require_onceLIBERTY_PKG_PATH.'LibertyBase.php' );
  19.  
  20. /**
  21.  * Gatekeeper class to illustrate best practices when creating a new bitweaver package that
  22.  * builds on core bitweaver functionality, such as the Liberty CMS engine
  23.  *
  24.  * @package gatekeeper
  25.  * @subpackage LibertyGatekeeper
  26.  *
  27.  *  created 2004/8/15
  28.  *
  29.  * @author spider <spider@steelsun.com>
  30.  *
  31.  * @version $Revision: 1.18 $ $Date: 2006/11/16 23:15:01 $ $Author: spiderr $
  32.  */
  33. class LibertyGatekeeper extends LibertyBase {
  34.     /**
  35.     * During initialisation, be sure to call our base constructors
  36.     **/
  37.  
  38.     function LibertyGatekeeper$pContentId=NULL {
  39.         $this->mContentId $pContentId;
  40.         LibertyBase::LibertyBase();
  41.     }
  42.  
  43.     function isValid({
  44.         return$this->verifyId$this->mContentId ) );
  45.     }
  46.  
  47.     function verifySecurity&$pParamHash {
  48.         if( ($pParamHash['security_id'!= 'public'&& !empty$pParamHash['access_level') ) {
  49.             // if we have an access level, we know we are trying to save/update,
  50.             // else perhaps we are just assigning security_id to content_id
  51.             ifempty$pParamHash['security_description'&& empty$pParamHash['security_id'|| $pParamHash['security_id'== 'new' ) ) {
  52.                 // default name to security access level instead of throwing an error
  53.                 $pParamHash['security_store']['security_description'$pParamHash['access_level'];
  54.             elseif!empty$pParamHash['security_description') ) {
  55.                 // we need to load the existing security_id to verify we user owns the security_id & if anything has changed
  56.                 $pParamHash['security_store']['security_description'substr$pParamHash['security_description']0160 );
  57.             }
  58.             if!empty$pParamHash['access_level') ) {
  59.                 $pParamHash['security_store']['is_hidden'($pParamHash['access_level'== 'hidden' 'y' NULL);
  60.                 $pParamHash['security_store']['is_private'($pParamHash['access_level'== 'private' 'y' NULL);
  61.                 // If we have an answer, store the question.
  62.                 if$pParamHash['access_level'== 'protected' && empty$pParamHash['access_answer') ) {
  63.                     $this->mErrors['security'tra"You must enter an answer for your security question." );
  64.                 else {
  65.                     $pParamHash['security_store']['access_question'!empty$pParamHash['access_answer'$pParamHash['access_question'NULL;
  66.                     $pParamHash['security_store']['access_answer'!empty$pParamHash['access_answer'trim$pParamHash['access_answer'NULL;
  67.                 }
  68.     //                $pParamHash['security_store']['group_id'] = !empty( $pParamHash['access_group_id'] ) ? $pParamHash['access_group_id'] : NULL;
  69.             }
  70.         }
  71.         returncount$this->mErrors == );
  72.     }
  73.  
  74.     function storeSecurity&$pParamHash {
  75.         if@$this->verifyId$pParamHash['content_id') ) {
  76.             // We'll first nuke any security mappings for this content_id
  77.             $sql "DELETE FROM `".BIT_DB_PREFIX."gatekeeper_security_map`
  78.                     WHERE `content_id` = ?";
  79.             $rs $this->mDb->query$sqlarray$pParamHash['content_id') );
  80.         }
  81.         if!empty$pParamHash['access_level'|| @$this->verifyId$pParamHash['security_id'&& $pParamHash['security_id'!= 'public') ) {
  82.             if$this->verifySecurity$pParamHash && !empty$pParamHash['security_store') ) {
  83.                 trim_array$pParamHash );
  84.                 if!empty$pParamHash['security_store') ) {
  85.                     global $gBitUser;
  86.                     $table BIT_DB_PREFIX."gatekeeper_security";
  87.                     if!(@$this->verifyId$pParamHash['security_id')) ) {
  88.                         $pParamHash['security_store']['user_id'$gBitUser->mUserId;
  89.                         $pParamHash['security_id'$this->mDb->GenID'gatekeeper_security_id_seq' );
  90.                         $pParamHash['security_store']['security_id'$pParamHash['security_id'];
  91.                         $result $this->mDb->associateInsert$table$pParamHash['security_store');
  92.                     else {
  93.                         $result $this->mDb->associateUpdate$table$pParamHash['security_store']array"security_id" => $pParamHash['security_id']) );
  94.                     }
  95.                 }
  96.             }
  97.  
  98.             if@$this->verifyId$pParamHash['content_id'&& @$this->verifyId$pParamHash['security_id') ) {
  99.                 $sql "INSERT INTO `".BIT_DB_PREFIX."gatekeeper_security_map` ( `content_id`, `security_id` ) VALUES (?,?)";
  100.                 $rs $this->mDb->query$sqlarray$pParamHash['content_id']$pParamHash['security_id') );
  101.             }
  102.         }
  103.         returncount$this->mErrors == );
  104.     }
  105.  
  106.     function getSecurityList$pUserId=NULL$pSecurityId=NULL$pSecurityDesc=NULL {
  107.         ifempty$pUserId ) ) {
  108.             global $gBitUser;
  109.             $pUserId $gBitUser->mUserId;
  110.         }
  111.         $whereSql NULL;
  112.         $bindVars array$pUserId );
  113.         if@$this->verifyId$pSecurityId ) ) {
  114.             $whereSql ' AND `security_id`=? ';
  115.             array_push$bindVars$pSecurityId );
  116.         }
  117.  
  118.         if$pSecurityDesc {
  119.             $whereSql .= ' AND `security_description`=? ';
  120.             array_push$bindVars$pSecurityDesc );
  121.         }
  122.  
  123.         $query "SELECT `security_id` AS `hash_id`, `security_id`, `user_id`, `security_description`, `is_private`, `is_hidden`, `access_question`, `access_answer` FROM `".BIT_DB_PREFIX."gatekeeper_securityWHERE `user_id`=? $whereSql";
  124.         return $this->mDb->getAssoc$query$bindVars );
  125.     }
  126.  
  127.     // guaranteeing pSecurityId is owned by someone else better happen upstream!
  128.     
  129.     function expungeSecurity$pSecurityId {
  130.         $ret FALSE;
  131.         if@$this->verifyId$pSecurityId ) ) {
  132.             $this->mDb->StartTrans();
  133.  
  134.             $sql "DELETE FROM `".BIT_DB_PREFIX."gatekeeper_security_map` WHERE security_id=?";
  135.             $rs $this->mDb->query$sqlarray$pSecurityId ) );
  136.  
  137.             $sql "DELETE FROM `".BIT_DB_PREFIX."gatekeeper_security` WHERE security_id=?";
  138.             $rs $this->mDb->query$sqlarray$pSecurityId ) );
  139.  
  140.             $this->mDb->CompleteTrans();
  141.             $ret TRUE;
  142.         }
  143.         return $ret;
  144.     }
  145. }
  146.  
  147.     $ret array(
  148.         'select_sql' => ' ,gs.`security_id` AS `has_access_control`, gs.`security_id`, gs.`security_description`, gs.`is_private`, gs.`is_hidden`, gs.`access_question`, gs.`access_answer`  ',
  149.         'join_sql' => " LEFT OUTER JOIN `".BIT_DB_PREFIX."gatekeeper_security_map` gsm ON ( lc.`content_id`=gsm.`content_id` )  LEFT OUTER JOIN `".BIT_DB_PREFIX."gatekeeper_security` gs ON ( gsm.`security_id`=gs.`security_id` ) ",
  150.     );
  151.     return $ret;
  152. }
  153.  
  154. function gatekeeper_content_store&$pObject&$pParamHash {
  155.     global $gBitSystem$gGatekeeper;
  156.     $errors NULL;
  157.     // If a content access system is active, let's call it
  158.     if$gBitSystem->isPackageActive'gatekeeper' ) ) {
  159.         if!$gGatekeeper->storeSecurity$pParamHash ) ) {
  160.             $errors['security'$gGatekeeper->mErrors['security'];
  161.         }
  162.     }
  163.     return$errors );
  164. }
  165.  
  166. function gatekeeper_content_display&$pContent&$pParamHash {
  167.     global $gBitSystem$gBitSmarty;
  168.     $pContent->hasUserPermission$pParamHash['perm_name');
  169. }
  170.  
  171. function gatekeeper_content_verify_access&$pContent&$pHash {
  172.     global $gBitUser$gBitSystem;
  173.  
  174.     if!count$pHash ) ) {
  175.         $pHash &$pContent->mInfo;
  176.     }
  177.     $error NULL;
  178.     if!$gBitUser->isRegistered(|| !($pHash['user_id'== $gBitUser->mUserId) ) {
  179.         if!($gBitUser->isAdmin()) ) {
  180.             if$pContent->mDb->isAdvancedPostgresEnabled() ) {
  181.                 global $gBitDb$gBitSmarty;
  182.                 // This code makes use of the badass /usr/share/pgsql/contrib/tablefunc.sql
  183.                 // contribution that you have to install like: psql foo < /usr/share/pgsql/contrib/tablefunc.sql
  184.                 // This code pulls all branches for the current node and determines if there is a path from this content to the root
  185.                 // without hitting a security_id. If there is clear path it returns TRUE. If there is a security_id, then
  186.                 // it determines if the current user has permission
  187.                 $query "SELECT branch,level,cb_item_content_id,cb_gallery_content_id,gs.*
  188.                         FROM connectby('`".BIT_DB_PREFIX."fisheye_gallery_image_map`', '`gallery_content_id`', '`item_content_id`', ?, 0, '/') AS t(`cb_gallery_content_id` int,`cb_item_content_id` int, `level` int, `branch` text)
  189.                             LEFT OUTER JOIN `".BIT_DB_PREFIX."gatekeeper_security_map` gsm ON (`cb_gallery_content_id`=gsm.`content_id`)
  190.                             LEFT OUTER JOIN `".BIT_DB_PREFIX."gatekeeper_security` gs ON (gs.`security_id`=gsm.`security_id`)
  191.                         ORDER BY branch
  192.                         ";
  193.         $gBitDb->setFatalActiveFALSE );
  194.                 $tree $pContent->mDb->getAssoc$queryarray$pHash['content_id') );
  195.         $gBitDb->setFatalActiveTRUE );
  196.                 if$tree {
  197.                     // we will assume true here since the prevention cases can repeatedly flag FALSE
  198.                     $lastLevel = -1;
  199.                     foreach$tree AS $branch => $node {
  200.                         if$node['level'<= $lastLevel {
  201.                             // we have moved followed a branch to the end and there is no security!
  202.                             unset$errorMessage );
  203.                             break;
  204.                         }
  205.                         if$node['security_id'{
  206.                             $ret FALSE;
  207.                             if$node['is_hidden'{
  208.                                 if!empty$pHash['no_fatal') ) {
  209.                                     // We are on a listing, so we should hide this with an empty error message
  210.                                     $errorMessage '';
  211.                                 }
  212.                             }
  213.                             if$node['is_private'{
  214.                                 if!empty$pHash['no_fatal') ) {
  215.                                     // We are on a listing, so we should hide this with an empty error message
  216.                                     $errorMessage '';
  217.                                 else {
  218.                                     $errorMessage tra'You cannot view this' ).' '.strtolowertra$pHash['content_type']['content_description') );
  219.                                 }
  220.                             }
  221.                             if!empty$node['access_answer') ) {
  222.                                 $pContent->mInfo array_merge$pHash$node );
  223.                                 if$valError gatekeeper_authenticate$nodeempty$pHash['no_fatal') ) ) {
  224.                                     $errorMessage $valError;
  225.                                 }
  226.                             }
  227.                         }
  228.                         $lastLevel $node['level'];
  229.                     }
  230.  
  231.                     ifisset$errorMessage ) ) {
  232.                         ifempty$pHash['no_fatal') ) {
  233.                             $gBitSystem->fatalError$errorMessage );
  234.                         else {
  235.                             $error['access_control'$errorMessage;
  236.                         }
  237.                     }
  238.  
  239.                 elseif!empty$gBitDb->mDb->_errorMsg ) ) {
  240.                     if$gBitUser->isOwner() ) {
  241.                         $gBitSmarty->assign'feedback'array'warning' => $gBitDb->mDb->_errorMsg.'<br/>'.tra'Please check the galleries to which this '.$pHash['content_description'].' belongs' ) ) );
  242.                     }
  243.                 }
  244.             elseif!empty$pHash['security_id') ) {
  245.                 // order matters here!
  246.                 if$pHash['is_hidden'== 'y' {
  247.                     $ret TRUE;
  248.                 }
  249.                 if$pHash['is_private'== 'y' {
  250.                     $errorMessage tra'You cannot view this' ).' '.strtolowertra$pHash['content_type']['content_description') );
  251.                     ifempty$pHash['no_fatal') ) {
  252.                         $gBitSystem->fatalError$errorMessage );
  253.                     else {
  254.                         $error['access_control'$errorMessage;
  255.                     }
  256.                 }
  257.                 if!empty$pHash['access_answer') ) {
  258.                     if!($valError gatekeeper_authenticate$pHashempty$pHash['no_fatal') ) ) ) {
  259.                         $error['access_control'$valError;
  260.                     }
  261.                 }
  262.             }
  263.         }
  264.     }
  265.     return $error;
  266. }
  267.  
  268.  
  269. function gatekeeper_authenticate&$pInfo$pFatalOnError TRUE {
  270.     global $gBitSystem$gBitSmarty;
  271.     $ret FALSE;
  272.  
  273.     ifempty$_SESSION['gatekeeper_security'][$pInfo['security_id']] || $_SESSION['gatekeeper_security'][$pInfo['security_id']] != md5$pInfo['access_answer') ) ) {
  274.         if!empty$_REQUEST['try_access_answer'&& strtouppertrim$_REQUEST['try_access_answer') ) == strtouppertrim($pInfo['access_answer']) ) ) {
  275.             // we have a successful password entry. Set the session so we don't ask for it again
  276.             $_SESSION['gatekeeper_security'][$pInfo['security_id']] md5$pInfo['access_answer');
  277.         else {
  278.             if$pFatalOnError {
  279.                 $gBitSystem->display("bitpackage:gatekeeper/authenticate.tpl""Password Required" );
  280.                 die;
  281.             else {
  282.                 $ret '<h2>'.tra"Password Required" ).'</h2>'.$gBitSmarty->fetch"bitpackage:gatekeeper/authenticate.tpl" );
  283.             }
  284.         }
  285.     }
  286.     return $ret;
  287. }
  288.  
  289.  
  290.  
  291.  
  292. function gatekeeper_content_edit&$pContent {
  293.     global $gGatekeeper$gBitUser$gBitSmarty;
  294.     $gBitSmarty->assign'securities'$gGatekeeper->getSecurityList$gBitUser->mUserId ) );
  295. }
  296.  
  297. global $gGatekeeper;
  298. $gGatekeeper new LibertyGatekeeper();
  299.  
  300. ?>

Documentation generated on Thu, 15 Feb 2007 20:42:26 +0000 by phpDocumentor 1.3.0