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

Source for file Multisites.php

Documentation is available at Multisites.php

  1. <?php
  2. /**
  3. * Multisites is a package that allows multi-homing for bitweaver and restriction of content to certain sites
  4. *
  5. @package  multisites
  6. @version $Header: /cvsroot/bitweaver/_bit_multisites/Multisites.php,v 1.14 2006/08/05 16:54:41 squareing Exp $
  7. @author   xing <xing@synapse.plus.com>
  8. */
  9.  
  10. /**
  11.  * Multisite Management
  12.  *
  13.  * @package  multisites
  14.  */
  15. class Multisites extends BitBase {
  16.     /**
  17.     * id of the currently active domain
  18.     */
  19.     var $mMultisiteId;
  20.     var $mPrefs;
  21.  
  22.     /**
  23.     * Initialisation of this class
  24.     */
  25.     function Multisites({
  26.         BitBase::BitBase();
  27.         $this->load();
  28.     }
  29.  
  30.     /**
  31.     * Load the currently active domain data from the db
  32.     **/
  33.  
  34.     function load({
  35.         $query "SELECT * FROM `".BIT_DB_PREFIX."multisites` WHERE `server_name`=?";
  36.         $result $this->mDb->query$queryarray$_SERVER['SERVER_NAME') );
  37.         if!empty$result ) ) {
  38.             $res $result->fetchRow();
  39.             $this->mMultisiteId = $res['multisite_id'];
  40.             $this->mInfo = $res;
  41.         
  42.         
  43.         if @BitBase::verifyId$this->mMultisiteId ) ) {
  44.             $query "SELECT * FROM `".BIT_DB_PREFIX."multisite_preferences` WHERE `multisite_id`=?";
  45.             $result $this->mDb->query$queryarray$this->mMultisiteId ) );
  46.             if!empty$result ) ) {
  47.                 while$res $result->fetchRow() ) {
  48.                     $this->mPrefs[$res['name']] $res['pref_value'];
  49.                 }
  50.             }
  51.         }
  52.         returncount$this->mInfo ) );
  53.     }
  54.  
  55.     /**
  56.     * Get the list of servers and their preferences
  57.     * @param if $pMultisiteId is set, it only gets specified server
  58.     * @param if $pContentId is set, it selects multisites selected for the specfied content
  59.     ***/
  60.     function getMultisites$pMultisiteId=NULL $pContentId=NULL {
  61.         $where '';
  62.         $join '';
  63.         $bindvals array();
  64.         if@BitBase::verifyId$pMultisiteId ) ) {
  65.             $where " WHERE ms.`multisite_id`=?";
  66.             $bindvals[$pMultisiteId;
  67.         }
  68.  
  69.         $select"SELECT * FROM `".BIT_DB_PREFIX."multisites` ms ";
  70.         $query $select.$where;
  71.         $result $this->mDb->query$query$bindvals );        
  72.         while$res $result->fetchRow() ) {
  73.             $ret[$res['multisite_id']] $res;
  74.         }
  75.  
  76.         $join " LEFT JOIN `".BIT_DB_PREFIX."multisite_preferences` mp ON (ms.multisite_id=mp.multisite_id) ".$join;
  77.         $query $select.$join.$where;
  78.         $result $this->mDb->query$query$bindvals );
  79.         while$res $result->fetchRow() ) {
  80.             $ret[$res['multisite_id']]['prefs'][$res['name']] $res['pref_value'];
  81.         }
  82.         
  83.         if!empty$pContentId ) ) {
  84.             $join " LEFT JOIN `".BIT_DB_PREFIX."multisite_content` mc ON (ms.multisite_id=mc.multisite_id)";
  85.             if@BitBase::verifyId$pContentId ) ) {
  86.                 if ($where != ''{
  87.                     $where $where." AND mc.content_id=?";
  88.                 }
  89.                 else {
  90.                     $where " WHERE mc.content_id=?";
  91.                 }
  92.                 $bindvals[$pContentId;
  93.             }
  94.             
  95.             $query $select.$join.$where;
  96.             $result $this->mDb->query$query$bindvals );
  97.             while$res $result->fetchRow() ) {
  98.                 if !empty($res['content_id']) ) {
  99.                     $ret[$res['multisite_id']][0]['selected'TRUE;
  100.                 }
  101.             }
  102.         }
  103.  
  104.         returnempty$ret NULL $ret );
  105.     }
  106.  
  107.     /**
  108.     * Store / Update server data
  109.     *
  110.     * @param array pParams hash of values that will be used to store the page
  111.     * @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why
  112.     * @access public
  113.     ***/
  114.     function store&$pParamHash {
  115.         if$this->verify($pParamHash ) ) {
  116.             $this->mDb->StartTrans();
  117.             if@BitBase::verifyId$pParamHash['multisite_id') ) {
  118.                 $result $this->mDb->associateUpdateBIT_DB_PREFIX."multisites"$pParamHash['server_store']array"multisite_id" => $pParamHash['multisite_id') );
  119.                 $this->expungePreferences$pParamHash['multisite_id');
  120.                 foreach$pParamHash['prefs_store'as $pref {
  121.                     $result $this->mDb->associateInsertBIT_DB_PREFIX."multisite_preferences"$pref );
  122.                 }
  123.             else {
  124.                 $result $this->mDb->associateInsertBIT_DB_PREFIX."multisites"$pParamHash['server_store');
  125.                 $msId $this->mDb->getOne"SELECT MAX(`multisite_id`) FROM `".BIT_DB_PREFIX."multisites`" );
  126.                 foreach$pParamHash['prefs_store'as $pref {
  127.                     $pref['multisite_id'$msId;
  128.                     $result $this->mDb->associateInsertBIT_DB_PREFIX."multisite_preferences"$pref );
  129.                 }
  130.             }
  131.             $this->load();
  132.             $this->mDb->CompleteTrans();
  133.         else {
  134.             $this->mErrors["There was a problem trying to save the settings.";
  135.         }
  136.         returncount$this->mErrors == );
  137.     }
  138.  
  139.     /**
  140.     * Make sure the data is safe to store
  141.     *
  142.     * @param array pParams reference to hash of values that will be used to store the page, they will be modified where necessary
  143.     * @return bool TRUE on success, FALSE if verify failed. If FALSE, $this->mErrors will have reason why
  144.     * @access private
  145.     ***/
  146.     function verify&$pParamHash {
  147.         if@BitBase::verifyId$pParamHash['multisite_id') ) {
  148.             $pParamHash['server_store']['multisite_id'$pParamHash['multisite_id'];
  149.         }
  150.  
  151.         if!empty$pParamHash['server_name') ) {
  152.             $query "SELECT * FROM `".BIT_DB_PREFIX."multisites` WHERE `server_name`=?";
  153.             $result $this->mDb->query$queryarraytrim$pParamHash['server_name') ) );
  154.             $site $result->fetchRow();
  155.             ifempty$site || $site['multisite_id'== $pParamHash['multisite_id') ) {
  156.                 $pParamHash['server_store']['server_name'trim$pParamHash['server_name');
  157.             else {
  158.                 $this->mErrors['There already is a server in the database with this value.';
  159.             }
  160.         else {
  161.             $this->mErrors["A Server Name is required to save this setting.";
  162.         }
  163.  
  164.         if!empty$pParamHash['server_prefs') ) {
  165.             foreach$pParamHash['server_prefs'as $pref => $value {
  166.                 $pParamHash['prefs_store'][array(
  167.                     'multisite_id' => $pParamHash['multisite_id'],
  168.                     'name' => $pref,
  169.                     'pref_value' => $value,
  170.                 );
  171.             }
  172.         else {
  173.             $pParamHash['prefs_store'NULL;
  174.         }
  175.  
  176.         if!empty$pParamHash['description') ) {
  177.             $pParamHash['server_store']['description'$pParamHash['description'];
  178.         else {
  179.             $pParamHash['server_store']['description'NULL;
  180.         }
  181.         returncount$this->mErrors == );
  182.     }
  183.  
  184.     /**
  185.     * remove server from db
  186.     * 
  187.     * @param $pMultisiteId is the id of the server we need to delete
  188.     * @access public
  189.     ***/
  190.     function expunge$pMultisiteId=NULL {
  191.         $ret FALSE;
  192.         if@BitBase::verifyId$pMultisiteId ) ) {
  193.             $this->expungeRestrictions($pMultisiteId);
  194.             $this->expungePreferences$pMultisiteId );
  195.             $query "DELETE FROM `".BIT_DB_PREFIX."multisites` WHERE `multisite_id` = ?";
  196.             $ret $this->mDb->query$queryarray$pMultisiteId ) );
  197.         }
  198.         return $ret;
  199.     }
  200.  
  201.     /**
  202.      * remove restrictions by multisite_id from db
  203.      *
  204.      * @param $pMultisiteId 
  205.      * @access public
  206.      ***/
  207.     function expungeRestrictions$pMultisiteId$pContentId NULL {
  208.         $ret FALSE;
  209.         if !empty($pMultisiteId&& @BitBase::verifyId$pMultisiteId ) ) {
  210.             $query "DELETE FROM `".BIT_DB_PREFIX."multisite_content` WHERE multisite_id = ?";
  211.             $ret $this->mDb->query$queryarray$pMultisiteId ) );
  212.         }
  213.         if !empty($pContentId&& @BitBase::verifyId$pContentId ) ) {
  214.             $query "DELETE FROM `".BIT_DB_PREFIX."multisite_content` WHERE content_id =?";
  215.             $ret $this->mDb->query$queryarray$pContentId ) );
  216.         }
  217.  
  218.         return $ret;
  219.     }
  220.  
  221.     /**
  222.     * remove all preferences associated with a given server
  223.     * 
  224.     * @param $pMultisiteId is the id of the server we need to delete
  225.     * @access private
  226.     ***/
  227.     function expungePreferences$pMultisiteId=NULL {
  228.         $ret FALSE;
  229.         if@BitBase::verifyId$pMultisiteId ) ) {
  230.             $query "DELETE FROM `".BIT_DB_PREFIX."multisite_preferences` WHERE `multisite_id` = ?";
  231.             $ret $this->mDb->query$queryarray$pMultisiteId ) );
  232.         }
  233.         return $ret;
  234.     }
  235.     
  236.     /**
  237.      * Store content restriction
  238.      * @param $pParamHash an array of restrictions to be stored.
  239.      * @param $pParamHash[multisite_id] The id of the site to restrict to
  240.      * @param $pParamHash[content_id] The id of the content to restrict
  241.      * @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why
  242.      * @access public
  243.      ***/
  244.     function insertRestriction&$pParamHash {
  245.         if$this->verifyRestrictions($pParamHash) ) {
  246.             $this->mDb->StartTrans();
  247.             foreach$pParamHash['member_store'as $item {
  248.                 $result $this->mDb->associateInsertBIT_DB_PREFIX."multisite_content"$item );
  249.             }
  250.             $this->mDb->CompleteTrans();        
  251.         else {
  252.             vd$this->mErrors );
  253.         }
  254.         returncount$this->mErrors == );
  255.     }
  256.  
  257.     /**
  258.     * verify, clean up and prepare data to be stored
  259.     * @param $pParamHash all information that is being stored. will update $pParamHash by reference with fixed array of items
  260.     * @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why
  261.     * @access private
  262.     ***/
  263.     function verifyRestrictions&$pParamHash {
  264.         foreach$pParamHash as $key => $item {
  265.             ifisset$item['multisite_id'&& @BitBase::verifyId$item['multisite_id') ) {
  266.                 $tmp['member_store'][$key]['multisite_id'$item['multisite_id'];
  267.             else {
  268.                 $this->mErrors['store_members'tra'The multisite_id is missing.' );
  269.             }
  270.             
  271.             ifisset$item['content_id'&& @BitBase::verifyId$item['content_id') ) {
  272.                 $tmp['member_store'][$key]['content_id'$item['content_id'];
  273.             else {
  274.                 $this->mErrors['store_members''The content_id is not valid.';
  275.             }
  276.         }
  277.         
  278.         $pParamHash $tmp;
  279.         returncount$this->mErrors == );
  280.     }
  281. }
  282.  
  283. // ============= SERVICE FUNCTIONS =============
  284.  
  285. function multisites_content_display&$pObject {
  286.     // TODO: Add a feature to display sites the content is shown on to permissioned users
  287. }
  288.  
  289. function multisites_content_edit$pObject=NULL {
  290.     global $gBitSmarty$gBitUser$gBitSystem;
  291.     $multisitesList array();
  292.  
  293.     if$gBitSystem->isFeatureActive('multisites_per_site_content'&& $gBitUser->hasPermission'p_multisites_restrict_content' )) {
  294.         $multisites new Multisites();
  295.         if ($multisitesList $multisites->getMultisitesNULL!empty$pObject->mContentId $pObject->mContentId NULL )) {
  296.             $gBitSmarty->assign'multisitesList'$multisitesList);
  297.         }
  298.     }
  299. }
  300.  
  301. function multisites_content_expunge$pObject=NULL {
  302.     $multisites new Multisites();
  303.     $multisites->expungeRestrictions(NULL$pObject->mContentId);
  304. }
  305.  
  306.     global $gBitSmarty$gBitUser$gBitSystem;
  307.     
  308.     if$gBitSystem->isFeatureActive('multisites_per_site_content'&& $gBitUser->hasPermission'p_multisites_restrict_content' )) {
  309.         $multisites new Multisites();
  310.  
  311.         if ($multisitesList $multisites->getMultisites()) {
  312.             foreach$multisitesList as $key => $site {
  313.                 if (!empty$_REQUEST['multisites']['multisite'&& in_array$key$_REQUEST['multisites']['multisite') ) {
  314.                     $multisitesList[$key][0]['selected'TRUE;
  315.                 else {
  316.                     $multisitesList[$key][0]['selected'FALSE;
  317.                 }
  318.             }
  319.             $gBitSmarty->assign'multisitesList'$multisitesList );
  320.         }
  321.     }
  322. }
  323.  
  324. function multisites_content_store$pObject$pParamHash {
  325.     global $gBitSmarty$gBitUser$gBitSystem;    
  326.  
  327.     if$gBitSystem->isFeatureActive('multisites_per_site_content'&& $gBitUser->hasPermission'p_multisites_restrict_content' )) {
  328.         ifis_object$pObject  && empty$pParamHash['content_id') ) {
  329.             $pParamHash['content_id'$pObject->mContentId;
  330.         }
  331.  
  332.         if!empty$pParamHash['content_id') ) {
  333.             $multisites new Multisites();
  334.             $multisitesList $multisites->getMultisitesNULL$pParamHash['content_id');
  335.  
  336.             // Now we need to work out if we need to save at all
  337.             $selectedItem array();
  338.             if!empty$multisitesList )) {
  339.                 foreach$multisitesList as $site {
  340.                     if!empty$site[0]['selected')) {
  341.                         $selectedItem[$site['multisite_id'];
  342.                     }
  343.                 }
  344.             }
  345.  
  346.             // Quick and Dirty check to start of with
  347.             ifempty$_REQUEST['multisites'|| count$_REQUEST['multisites']['multisite']!= count$selectedItem ) ) {
  348.                 $modified TRUE;
  349.             else {
  350.                 // more thorough check
  351.                 foreach$selectedItem as $item {
  352.                     if!in_array$item$_REQUEST['multisites']['multisite') ) {
  353.                         $modified TRUE;
  354.                     }
  355.                 }
  356.             }
  357.  
  358.             if!empty$modified ) ) {
  359.                 // first remove all entries with this content_id
  360.                 if ($multisites->expungeRestrictionsNULL$pParamHash['content_id'&& !empty$_REQUEST['multisites') ) {
  361.                     // insert the content restrictions
  362.                     foreach$_REQUEST['multisites']['multisite'as $m_id {
  363.                         $siteHash[array(
  364.                                     'multisite_id' => $m_id,
  365.                                     'content_id' => $pParamHash['content_id']
  366.                                     );
  367.                     }
  368.                     if!$multisites->insertRestriction$siteHash ) ) {
  369.                         $gBitSmarty->assign'msg'tra"There was a problem setting the site restriction." ) );
  370.                         $gBitSmarty->display'error.tpl' );
  371.                         die;
  372.                     }
  373.                 }
  374.             }
  375.         }
  376.     }
  377. }
  378.  
  379. // Limits the list of content to those with an entry that matches the server
  380. // name or an entry that has no multistes restrictions if the user can't view everything.
  381.  
  382. function multisites_content_sql &$pObject$pParamHash ''{
  383.     global $gBitSystem$gBitUser;
  384.     $ret array();
  385.     // We only limit content if the user has activated this feature and they are not an administrator
  386.     if$gBitSystem->isFeatureActive('multisites_per_site_content'&& !$gBitUser->hasPermission('p_multisites_view_restricted') ) {
  387.         $ret['join_sql'" LEFT OUTER JOIN `".BIT_DB_PREFIX."multisite_content` mc ON (lc.`content_id` = mc.`content_id`) LEFT JOIN `".BIT_DB_PREFIX."multisites` ms ON (mc.`multisite_id` = ms.`multisite_id`) ";
  388.         $ret['where_sql'" AND ( ms.server_name IS NULL OR ms.`server_name` =? ) ";
  389.         $ret['bind_vars'][$_SERVER['SERVER_NAME'];
  390.     }
  391.     return $ret;
  392. }
  393. ?>

Documentation generated on Thu, 15 Feb 2007 20:45:55 +0000 by phpDocumentor 1.3.0