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

Source for file LibertyProtector.php

Documentation is available at LibertyProtector.php

  1. <?php
  2. /**
  3.  * protector package limits content based on user role
  4.  *
  5.  * @copyright (c) 2004-15 bitweaver.org
  6.  * @package protector
  7.  */
  8.  
  9. /**
  10.  * required setup
  11.  */
  12. require_once( LIBERTY_PKG_PATH.'LibertyBase.php' );
  13.  
  14. /**
  15.  * Protector class to illustrate best practices when creating a new bitweaver package that
  16.  * builds on core bitweaver functionality, such as the Liberty CMS engine
  17.  *
  18.  * @package protector
  19.  */
  20. class LibertyProtector extends LibertyBase {
  21.     /**
  22.     * During initialisation, be sure to call our base constructors
  23.     **/
  24.  
  25.     function __construct( $pContentId=NULL ) {
  26.         $this->mContentId = $pContentId;
  27.         parent::__construct();
  28.     }
  29.  
  30.     /**
  31.     * Update the liberty_content_role_map table with corrected role_id(s).
  32.     * 
  33.     * In -1 for anonymouse is not stored, switching content to anonymouse will clear array
  34.     *  
  35.     * @param object $pParamHash 
  36.     */
  37.     function storeProtection( &$pParamHash ) {
  38.         global $gBitSystem;
  39.         if( @BitBase::verifyId( $pParamHash['protector']['role_id'] ) ) {
  40.             $this->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."liberty_content_role_map` WHERE `content_id`=?", array( $pParamHash['content_id'] ) );
  41.             if( $gBitSystem->isFeatureActive( 'protector_single_role' ) ) {
  42.                 if( $pParamHash['protector']['role_id'] != -1 )
  43.                     $this->mDb->query( "INSERT INTO `".BIT_DB_PREFIX."liberty_content_role_map` ( `role_id`, `content_id` ) VALUES ( ?, ? )", array( $pParamHash['protector']['role_id'], $pParamHash['content_id'] ) );
  44.             } else {
  45.                 foreach( $pParamHash['protector']['role_id'] AS $roleId ) {
  46.                     if( $roleId != -1 )
  47.                     $this->mDb->query( "INSERT INTO `".BIT_DB_PREFIX."liberty_content_role_map` ( `role_id`, `content_id` ) VALUES ( ?, ? )", array( $roleId, $pParamHash['content_id'] ) );
  48.                 }
  49.             }
  50.         }
  51.         return( count( $this->mErrors ) == 0 );
  52.     }
  53.  
  54.     /**
  55.     * Delete entry(ies) from liberty_content_role_map table with content_id.
  56.     * 
  57.     * @param object $pContent 
  58.     */
  59.     function expunge( $ContentId=NULL ) {
  60.         $ret = FALSE;
  61.         if( @BitBase::verifyId( $ContentId ) ) {
  62.             $this->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."liberty_content_role_map` WHERE `content_id`=?", array( $ContentId ) );
  63.         }
  64.         return $ret;
  65.     }
  66.  
  67.     /**
  68.     * @return liberty_content_role_map for selected content_id
  69.     *  Ret -1 for anonymouse if alternatives are not stored
  70.     ***/
  71.     function getProtectionList( $ContentId=NULL ) {
  72.         global $gBitSystem;
  73.         $ret = array( '-1' <= $ContentId );
  74.         if( isset( $ContentId ) ) {
  75.             $ret = $this->mDb->GetAssoc( "SELECT `role_id`, `content_id` FROM `".BIT_DB_PREFIX."liberty_content_role_map` WHERE `content_id`=?", array( $ContentId ) );
  76.         }
  77.         return $ret;
  78.     }
  79. }
  80.  
  81. /**
  82. * function to provide list of filtered content
  83. **/
  84.  
  85. function protector_content_list() {
  86.     global $gBitUser;
  87.     $roles = array_keys($gBitUser->mRoles);
  88.     $ret = array(
  89.         'join_sql' => " LEFT JOIN `".BIT_DB_PREFIX."liberty_content_role_map` lcrm ON ( lc.`content_id`=lcrm.`content_id` ) LEFT OUTER JOIN `".BIT_DB_PREFIX."users_roles_map` purm ON ( purm.`user_id`=".$gBitUser->mUserId." ) AND ( purm.`role_id`=lcrm.`role_id` ) ",
  90.         'where_sql' => " AND (lcrm.`content_id` IS NULL OR lcrm.`role_id` IN(". implode(',', array_fill(0, count($roles), '?')) ." ) OR purm.`user_id`=?) ",
  91.         'bind_vars' => array_merge( $roles, array( $gBitUser->mUserId ) ),
  92.     );
  93.     return $ret;
  94. }
  95.  
  96. /**
  97.  * function to load a filtered content element
  98.  * 
  99.  * @param object $pContent 
  100.  */
  101. function protector_content_load( &$pContent = NULL ) {
  102.     global $gBitUser;
  103.  
  104.     $roles = array_keys($gBitUser->mRoles);
  105.     protector_content_verify_access( $pContent, $roles );
  106.     $ret = array(
  107.         'join_sql' => " LEFT JOIN `".BIT_DB_PREFIX."liberty_content_role_map` lcrm ON ( lc.`content_id`=lcrm.`content_id` ) LEFT OUTER JOIN `".BIT_DB_PREFIX."users_roles_map` purm ON ( purm.`role_id`=lcrm.`role_id` ) ",
  108.         'where_sql' => " AND (lcrm.`content_id` IS NULL OR lcrm.`role_id` IN(". implode(',', array_fill(0, count($roles), '?')) ." ) OR purm.`user_id`=?) ",
  109.         'bind_vars' => array( $gBitUser->mUserId ),
  110.     );
  111.     $ret['bind_vars'] = array_merge( $roles, $ret['bind_vars'] );
  112.     return $ret;
  113. }
  114.  
  115. /**
  116. * function to store a filtered content element
  117. * 
  118. * @param object $pObject 
  119. * @param array $pParamHash 
  120. ***/
  121. function protector_content_store( &$pObject, &$pParamHash ) {
  122.     global $gBitSystem, $gProtector;
  123.     $errors = NULL;
  124.     // If a content access system is active, let's call it
  125.     if( $gBitSystem->isPackageActive( 'protector' ) ) {
  126.         if( !$gProtector->storeProtection( $pParamHash ) ) {
  127.             $errors['protector'] = $gProtector->mErrors['security'];
  128.         }
  129.     }
  130.     return( $errors );
  131. }
  132.  
  133. /**
  134. * function to store a filtered comment element
  135. * 
  136. * @param object $pContent 
  137. * @param array $pParamHash 
  138. ***/
  139. function protector_comment_store( &$pContent, &$pParamHash ) {
  140.     global $gBitSystem, $gProtector;
  141.     $errors = NULL;
  142.     // If a content access system is active, let's call it
  143.     if( $gBitSystem->isPackageActive( 'protector' ) ) {
  144.         if( isset( $pParamHash['comments_parent_id'] ) ) {
  145.             $pParamHash['protector']['role_id'] = $pContent->mDb->GetOne( "SELECT `role_id` FROM `".BIT_DB_PREFIX."liberty_content_role_map` WHERE `content_id`=?", array( $pParamHash['comments_parent_id'] ) );
  146.         }
  147.         if( !$gProtector->storeProtection( $pParamHash ) ) {
  148.             $errors['protector'] = $gProtector->mErrors['security'];
  149.         }
  150.     }
  151.     return( $errors );
  152. }
  153.  
  154. /**
  155. * function to delete a filtered content element
  156. * 
  157. * @param object $pContent 
  158. * @param array $pParamHash 
  159. ***/
  160. function protector_content_expunge( &$pContent = NULL ) {
  161.         if( @BitBase::verifyId( $pContent->mContentId ) ) {
  162.             $pContent->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."liberty_content_role_map` WHERE `content_id`=?", array( $pContent->mContentId ) );
  163.         }
  164. }
  165.  
  166. /**
  167. * function to display a filtered content element
  168. * 
  169. * @param object $pContent 
  170. * @param array $pParamHash 
  171. ***/
  172. function protector_content_display( &$pContent, &$pParamHash ) {
  173.     global $gBitSystem, $gBitSmarty;
  174.     $pContent->hasUserPermission( $pParamHash['perm_name'] );
  175. }
  176.  
  177. /**
  178. * function to verify access to a filtered content element
  179. * 
  180. * @param object $pContent 
  181. * @param array $pHash 
  182. ***/
  183. function protector_content_verify_access( &$pContent, &$pHash ) {
  184.     global $gBitUser, $gBitSystem;
  185.  
  186.     $error = NULL;
  187.     if( !$pContent->verifyId( $pContent->mContentId ) ) {
  188. // vd($pContent);
  189. // need to get ContentId if not set
  190.     }
  191.     if( $pContent->verifyId( $pContent->mContentId ) ) {
  192.         $query = "SELECT lc.`content_id`, lcrm.`role_id` as `is_protected`
  193.             FROM `".BIT_DB_PREFIX."liberty_content` lc 
  194.             LEFT JOIN `".BIT_DB_PREFIX."liberty_content_role_map` lcrm ON ( lc.`content_id`=lcrm.`content_id` ) LEFT OUTER JOIN `".BIT_DB_PREFIX."users_roles_map` urm ON ( urm.`user_id`=".$gBitUser->mUserId." ) AND ( urm.`role_id`=lcrm.`role_id` ) 
  195.             WHERE lc.`content_id` = ?";
  196.         $ret = $pContent->mDb->getRow( $query, array( $pContent->mContentId ) );
  197.         if( $ret and is_numeric($ret['is_protected']) and !in_array( $ret['is_protected'], $pHash ) ) {
  198.             $gBitSystem->fatalError( tra( 'You do not have permission to access this '.$pContent->getContentTypeName() ), 'error.tpl', tra( 'Permission denied.' ) );
  199.         }
  200.     }
  201.     return $error;
  202. }
  203.  
  204. /**
  205. * function to edit a filtered content element
  206. * 
  207. * @param object $pContent 
  208. ***/
  209. function protector_content_edit( &$pContent ) {
  210.     global $gProtector, $gBitUser, $gBitSmarty;
  211.     $roles = $gBitUser->getRoles();
  212.     $roles[-1]['role_name'] = "~~ System Default ~~";
  213.     ksort( $roles );
  214.     foreach( array_keys( $roles ) as $roleId ) {
  215.         if( $roleId != -1 ) {
  216.             $protectorRolesId[$roleId] = $roles[$roleId]['role_name'];
  217.         } else {
  218.             $protectorRolesId[$roleId] = "~~ System Default ~~";
  219.         }
  220.     }
  221.     if ( $pContent->mContentId ) {
  222.         $serviceHash['protector']['role'] = $gProtector->getProtectionList( $pContent->mContentId );
  223.     } else {
  224.         if ( isset( $pContent->mInfo['parent_id'] ) ) {
  225.             $serviceHash['protector']['role'] = $gProtector->getProtectionList( $pContent->mInfo['parent_id'] );
  226.         }
  227.     }    
  228.     if ( isset( $serviceHash['protector']['role'] ) ) { $prot = array_keys( $serviceHash['protector']['role'] ); }
  229.     $serviceHash['protector']['role_id'] = ( empty( $prot[0] ) ? -1 : $prot[0] );
  230.     $gBitSmarty->assign_by_ref( 'serviceHash', $serviceHash );
  231.     $gBitSmarty->assign_by_ref( 'protectorRolesId', $protectorRolesId );
  232.     $gBitSmarty->assign_by_ref( 'protectorRoles', $roles );
  233. }
  234.  
  235. global $gProtector;
  236. $gProtector = new LibertyProtector();
  237.  
  238. ?>

Documentation generated on Wed, 29 Jul 2015 13:56:56 +0000 by phpDocumentor 1.5.0-lsces