Source for file Multisites.php
Documentation is available at Multisites.php
* Multisites is a package that allows multi-homing for bitweaver and restriction of content to certain sites
* @version $Header: /cvsroot/bitweaver/_bit_multisites/Multisites.php,v 1.14 2006/08/05 16:54:41 squareing Exp $
* @author xing <xing@synapse.plus.com>
* id of the currently active domain
* Initialisation of this class
* Load the currently active domain data from the db
$query = "SELECT * FROM `". BIT_DB_PREFIX. "multisites` WHERE `server_name`=?";
$result = $this->mDb->query( $query, array( $_SERVER['SERVER_NAME'] ) );
if( !empty( $result ) ) {
$res = $result->fetchRow();
$query = "SELECT * FROM `". BIT_DB_PREFIX. "multisite_preferences` WHERE `multisite_id`=?";
if( !empty( $result ) ) {
while( $res = $result->fetchRow() ) {
$this->mPrefs[$res['name']] = $res['pref_value'];
* Get the list of servers and their preferences
* @param if $pMultisiteId is set, it only gets specified server
* @param if $pContentId is set, it selects multisites selected for the specfied content
function getMultisites( $pMultisiteId= NULL , $pContentId= NULL ) {
$where = " WHERE ms.`multisite_id`=?";
$bindvals[] = $pMultisiteId;
$result = $this->mDb->query( $query, $bindvals );
while( $res = $result->fetchRow() ) {
$ret[$res['multisite_id']] = $res;
$join = " LEFT JOIN `". BIT_DB_PREFIX. "multisite_preferences` mp ON (ms.multisite_id=mp.multisite_id) ". $join;
$query = $select. $join. $where;
$result = $this->mDb->query( $query, $bindvals );
while( $res = $result->fetchRow() ) {
$ret[$res['multisite_id']]['prefs'][$res['name']] = $res['pref_value'];
if( !empty( $pContentId ) ) {
$join = " LEFT JOIN `". BIT_DB_PREFIX. "multisite_content` mc ON (ms.multisite_id=mc.multisite_id)";
$where = $where. " AND mc.content_id=?";
$where = " WHERE mc.content_id=?";
$bindvals[] = $pContentId;
$query = $select. $join. $where;
$result = $this->mDb->query( $query, $bindvals );
while( $res = $result->fetchRow() ) {
if ( !empty($res['content_id']) ) {
$ret[$res['multisite_id']][0]['selected'] = TRUE;
return( empty( $ret ) ? NULL : $ret );
* Store / Update server data
* @param array pParams hash of values that will be used to store the page
* @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why
function store( &$pParamHash ) {
if( $this->verify($pParamHash ) ) {
$this->mDb->StartTrans();
$result = $this->mDb->associateUpdate( BIT_DB_PREFIX. "multisites", $pParamHash['server_store'], array( "multisite_id" => $pParamHash['multisite_id'] ) );
$this->expungePreferences( $pParamHash['multisite_id'] );
foreach( $pParamHash['prefs_store'] as $pref ) {
$result = $this->mDb->associateInsert( BIT_DB_PREFIX. "multisite_preferences", $pref );
$result = $this->mDb->associateInsert( BIT_DB_PREFIX. "multisites", $pParamHash['server_store'] );
$msId = $this->mDb->getOne( "SELECT MAX(`multisite_id`) FROM `". BIT_DB_PREFIX. "multisites`" );
foreach( $pParamHash['prefs_store'] as $pref ) {
$pref['multisite_id'] = $msId;
$result = $this->mDb->associateInsert( BIT_DB_PREFIX. "multisite_preferences", $pref );
$this->mDb->CompleteTrans();
$this->mErrors[] = "There was a problem trying to save the settings.";
* Make sure the data is safe to store
* @param array pParams reference to hash of values that will be used to store the page, they will be modified where necessary
* @return bool TRUE on success, FALSE if verify failed. If FALSE, $this->mErrors will have reason why
function verify( &$pParamHash ) {
$pParamHash['server_store']['multisite_id'] = $pParamHash['multisite_id'];
if( !empty( $pParamHash['server_name'] ) ) {
$query = "SELECT * FROM `". BIT_DB_PREFIX. "multisites` WHERE `server_name`=?";
$result = $this->mDb->query( $query, array( trim( $pParamHash['server_name'] ) ) );
$site = $result->fetchRow();
if( empty( $site ) || ( $site['multisite_id'] == $pParamHash['multisite_id'] ) ) {
$pParamHash['server_store']['server_name'] = trim( $pParamHash['server_name'] );
$this->mErrors[] = 'There already is a server in the database with this value.';
$this->mErrors[] = "A Server Name is required to save this setting.";
if( !empty( $pParamHash['server_prefs'] ) ) {
foreach( $pParamHash['server_prefs'] as $pref => $value ) {
$pParamHash['prefs_store'][] = array(
'multisite_id' => $pParamHash['multisite_id'],
$pParamHash['prefs_store'] = NULL;
if( !empty( $pParamHash['description'] ) ) {
$pParamHash['server_store']['description'] = $pParamHash['description'];
$pParamHash['server_store']['description'] = NULL;
* @param $pMultisiteId is the id of the server we need to delete
function expunge( $pMultisiteId= NULL ) {
$this->expungePreferences( $pMultisiteId );
$query = "DELETE FROM `". BIT_DB_PREFIX. "multisites` WHERE `multisite_id` = ?";
$ret = $this->mDb->query( $query, array( $pMultisiteId ) );
* remove restrictions by multisite_id from db
$query = "DELETE FROM `". BIT_DB_PREFIX. "multisite_content` WHERE multisite_id = ?";
$ret = $this->mDb->query( $query, array( $pMultisiteId ) );
$query = "DELETE FROM `". BIT_DB_PREFIX. "multisite_content` WHERE content_id =?";
$ret = $this->mDb->query( $query, array( $pContentId ) );
* remove all preferences associated with a given server
* @param $pMultisiteId is the id of the server we need to delete
function expungePreferences( $pMultisiteId= NULL ) {
$query = "DELETE FROM `". BIT_DB_PREFIX. "multisite_preferences` WHERE `multisite_id` = ?";
$ret = $this->mDb->query( $query, array( $pMultisiteId ) );
* Store content restriction
* @param $pParamHash an array of restrictions to be stored.
* @param $pParamHash[multisite_id] The id of the site to restrict to
* @param $pParamHash[content_id] The id of the content to restrict
* @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why
if( $this->verifyRestrictions($pParamHash) ) {
$this->mDb->StartTrans();
foreach( $pParamHash['member_store'] as $item ) {
$result = $this->mDb->associateInsert( BIT_DB_PREFIX. "multisite_content", $item );
$this->mDb->CompleteTrans();
* verify, clean up and prepare data to be stored
* @param $pParamHash all information that is being stored. will update $pParamHash by reference with fixed array of items
* @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why
function verifyRestrictions( &$pParamHash ) {
foreach( $pParamHash as $key => $item ) {
if( isset ( $item['multisite_id'] ) && @BitBase::verifyId( $item['multisite_id'] ) ) {
$tmp['member_store'][$key]['multisite_id'] = $item['multisite_id'];
$this->mErrors['store_members'] = tra( 'The multisite_id is missing.' );
if( isset ( $item['content_id'] ) && @BitBase::verifyId( $item['content_id'] ) ) {
$tmp['member_store'][$key]['content_id'] = $item['content_id'];
$this->mErrors['store_members'] = 'The content_id is not valid.';
// ============= SERVICE FUNCTIONS =============
// TODO: Add a feature to display sites the content is shown on to permissioned users
global $gBitSmarty, $gBitUser, $gBitSystem;
$multisitesList = array();
if( $gBitSystem->isFeatureActive('multisites_per_site_content') && $gBitUser->hasPermission( 'p_multisites_restrict_content' )) {
if ($multisitesList = $multisites->getMultisites( NULL, !empty( $pObject->mContentId ) ? $pObject->mContentId : NULL )) {
$gBitSmarty->assign( 'multisitesList', $multisitesList);
global $gBitSmarty, $gBitUser, $gBitSystem;
if( $gBitSystem->isFeatureActive('multisites_per_site_content') && $gBitUser->hasPermission( 'p_multisites_restrict_content' )) {
foreach( $multisitesList as $key => $site ) {
if (!empty( $_REQUEST['multisites']['multisite'] ) && in_array( $key, $_REQUEST['multisites']['multisite'] ) ) {
$multisitesList[$key][0]['selected'] = TRUE;
$multisitesList[$key][0]['selected'] = FALSE;
$gBitSmarty->assign( 'multisitesList', $multisitesList );
global $gBitSmarty, $gBitUser, $gBitSystem;
if( $gBitSystem->isFeatureActive('multisites_per_site_content') && $gBitUser->hasPermission( 'p_multisites_restrict_content' )) {
if( is_object( $pObject ) && empty( $pParamHash['content_id'] ) ) {
$pParamHash['content_id'] = $pObject->mContentId;
if( !empty( $pParamHash['content_id'] ) ) {
$multisitesList = $multisites->getMultisites( NULL, $pParamHash['content_id'] );
// Now we need to work out if we need to save at all
if( !empty( $multisitesList )) {
foreach( $multisitesList as $site ) {
if( !empty( $site[0]['selected'] )) {
$selectedItem[] = $site['multisite_id'];
// Quick and Dirty check to start of with
if( empty( $_REQUEST['multisites'] ) || count( $_REQUEST['multisites']['multisite']) != count( $selectedItem ) ) {
foreach( $selectedItem as $item ) {
if( !in_array( $item, $_REQUEST['multisites']['multisite'] ) ) {
if( !empty( $modified ) ) {
// first remove all entries with this content_id
if ($multisites->expungeRestrictions( NULL, $pParamHash['content_id'] ) && !empty( $_REQUEST['multisites'] ) ) {
// insert the content restrictions
foreach( $_REQUEST['multisites']['multisite'] as $m_id ) {
'content_id' => $pParamHash['content_id']
$gBitSmarty->assign( 'msg', tra( "There was a problem setting the site restriction." ) );
$gBitSmarty->display( 'error.tpl' );
// Limits the list of content to those with an entry that matches the server
// name or an entry that has no multistes restrictions if the user can't view everything.
global $gBitSystem, $gBitUser;
// We only limit content if the user has activated this feature and they are not an administrator
if( $gBitSystem->isFeatureActive('multisites_per_site_content') && !$gBitUser->hasPermission('p_multisites_view_restricted') ) {
$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`) ";
$ret['where_sql'] = " AND ( ms.server_name IS NULL OR ms.`server_name` =? ) ";
$ret['bind_vars'][] = $_SERVER['SERVER_NAME'];
|