eZ Publish  [4.0]
ezwebincommon.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Created on: <26-Jun-2007 15:00:00 dl>
00004 //
00005 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00006 // SOFTWARE NAME: eZ Publish
00007 // SOFTWARE RELEASE: 4.0.x
00008 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00009 // SOFTWARE LICENSE: GNU General Public License v2.0
00010 // NOTICE: >
00011 //   This program is free software; you can redistribute it and/or
00012 //   modify it under the terms of version 2.0  of the GNU General
00013 //   Public License as published by the Free Software Foundation.
00014 //
00015 //   This program is distributed in the hope that it will be useful,
00016 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //   GNU General Public License for more details.
00019 //
00020 //   You should have received a copy of version 2.0 of the GNU General
00021 //   Public License along with this program; if not, write to the Free
00022 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00023 //   MA 02110-1301, USA.
00024 //
00025 //
00026 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00027 //
00028 
00029 // eZWebin install/updagrade helper routines.
00030 // file  bin/php/ezwebincommon.php
00031 
00032 
00033 /*!
00034  define constans
00035 */
00036 define( "EZ_INSTALL_PACKAGE_EXTRA_ACTION_QUIT", 'q' );
00037 define( "EZ_INSTALL_PACKAGE_EXTRA_ACTION_SKIP_PACKAGE", 's' );
00038 
00039 /*!
00040  define global vars
00041 */
00042 global $cli;
00043 global $script;
00044 
00045 
00046 /*!
00047  includes
00048 */
00049 require_once 'autoload.php';
00050 
00051 //include_once( 'kernel/classes/ezscript.php' );
00052 require_once( 'kernel/common/i18n.php' );
00053 //include_once( 'kernel/classes/ezpackage.php' );
00054 
00055 
00056 /**************************************************************
00057 * 'cli->output' wrappers                                      *
00058 ***************************************************************/
00059 function showError( $message, $addEOL = true, $bailOut = true )
00060 {
00061     global $cli;
00062     global $script;
00063 
00064     $cli->output( $cli->stylize( 'error', "Error: " .  $message ), $addEOL );
00065 
00066     if( $bailOut )
00067     {
00068         $script->shutdown( 1 );
00069     }
00070 }
00071 
00072 function showWarning( $message, $addEOL = true )
00073 {
00074     global $cli;
00075     $cli->output( $cli->stylize( 'warning', "Warning: " . $message ), $addEOL );
00076 }
00077 
00078 function showNotice( $message, $addEOL = true )
00079 {
00080     global $cli;
00081     $cli->output( $cli->stylize( 'notice', "Notice: " ) .  $message, $addEOL );
00082 }
00083 
00084 function showMessage( $message, $addEOL = true )
00085 {
00086     global $cli;
00087     $cli->output( $cli->stylize( 'blue', $message ), $addEOL );
00088 }
00089 
00090 function showMessage2( $message, $addEOL = true )
00091 {
00092     global $cli;
00093     $cli->output( $cli->stylize( 'red', $message ), $addEOL );
00094 }
00095 
00096 /*!
00097  Show available actions to user
00098 */
00099 function showPackageActions( $actionList )
00100 {
00101     foreach( $actionList as $action => $actionDescription )
00102     {
00103         showMessage( "    [ $action ]: " . $actionDescription );
00104     }
00105 }
00106 
00107 /*!
00108  add extra actions to default package item's actions
00109 */
00110 function getExtraActions( $actionList )
00111 {
00112     $actionList[EZ_INSTALL_PACKAGE_EXTRA_ACTION_SKIP_PACKAGE] = "Skipt rest of the package";
00113     $actionList[EZ_INSTALL_PACKAGE_EXTRA_ACTION_QUIT] = "Quit";
00114     return $actionList;
00115 }
00116 
00117 /*!
00118  prompt user to choose what to do next
00119 */
00120 function getUserInput( $prompt )
00121 {
00122     $stdin = fopen( "php://stdin", "r+" );
00123 
00124     fwrite( $stdin, $prompt );
00125 
00126     $userInput = fgets( $stdin );
00127     $userInput = trim( $userInput, "\n" );
00128 
00129     fclose( $stdin );
00130 
00131     return $userInput;
00132 }
00133 
00134 /*!
00135  handle installation error:
00136      - show error message;
00137      - ask user what to do next;
00138 */
00139 function handlePackageError( $error )
00140 {
00141     showWarning( $error['description'] );
00142 
00143     $actionList = $error['actions'];
00144     $actionList = getExtraActions( $actionList );
00145 
00146     showPackageActions( $actionList );
00147 
00148     $actions = array_keys( $actionList );
00149     $actions = '[' . implode( '], [', $actions ) . ']';
00150 
00151     $userInput = getUserInput( "    Plese choose one of the following actions( $actions ): " );
00152 
00153     return $userInput;
00154 }
00155 
00156 /*!
00157  Check if dir $dirName exists. If not, ask user to create it.
00158 */
00159 function checkDir( $dirName )
00160 {
00161     if ( !file_exists( $dirName ) )
00162     {
00163         global $autoMode;
00164         if( $autoMode == 'on' )
00165         {
00166             $action = 'y';
00167         }
00168         else
00169         {
00170             $action = getUserInput( "Directory '$dirName' doesn't exist. Create? [y/n]: ");
00171         }
00172 
00173         if( strpos( $action, 'n' ) === 0 )
00174             showError( "Unable to continue. Aborting..." );
00175 
00176         if( !eZDir::mkdir( $dirName, false, true ) )
00177             showError( "Unable to create dir '$dirName'. Aborting..." );
00178     }
00179 
00180     return true;
00181 }
00182 
00183 function installScriptDir( $packageRepository )
00184 {
00185     return ( eZPackage::repositoryPath() . "/$packageRepository/ezwebin_site" );
00186 }
00187 
00188 function defaultVendor()
00189 {
00190     // $vendor is taken from 'ezwebin_site' package.
00191     $vendor = 'eZ systems';
00192     return $vendor;
00193 }
00194 
00195 function repositoryByVendor( $vendor )
00196 {
00197     // it's copy/paste from eZPackage
00198     $trans = eZCharTransform::instance();
00199     $repository = $trans->transformByGroup( $vendor, 'urlalias' );
00200     return $repository;
00201 }
00202 
00203 /*!
00204  download packages if neede.
00205     1. check if packages specified in $packageList exist in $packageRepository(means already downloaded and imported).
00206        if yes - ask user to do download or not. If not - go out
00207     2. check $packgesList exists in $packageDir(means packages downloaded but not imported)
00208        if yes - ask user to import or not. If not - go out
00209     3. download and import.
00210 */
00211 function downloadPackages( $packageList, $packageURL, $packageDir, $packageRepository )
00212 {
00213     global $cli;
00214 
00215     showMessage2( "Configuring..." );
00216 
00217     if ( !is_array( $packageList ) || count( $packageList ) == 0 )
00218         showError( "Package list is empty. Aborting..." );
00219 
00220     // 1. check if packages specified in $packageList exist in $packageRepository(means already downloaded and imported).
00221     //    if yes - ask user to do download or not. If not - go out
00222     foreach( array_keys( $packageList ) as $k )
00223     {
00224         $packageName = $packageList[$k];
00225         $package = eZPackage::fetch( $packageName );
00226 
00227         if( is_object( $package ) )
00228         {
00229             global $autoMode;
00230             if( $autoMode == 'on' )
00231             {
00232                 $action = 'y';
00233             }
00234             else
00235             {
00236                 $action = getUserInput( "Package '$packageName' already imported. Import it anyway? [y/n]: " );
00237             }
00238 
00239             if ( strpos( $action, 'n' ) === 0 )
00240                 unset( $packageList[$k] );
00241             else
00242             {
00243                 eZDir::recursiveDelete( eZPackage::repositoryPath() . "/$packageRepository/$packageName" );
00244             }
00245         }
00246     }
00247 
00248     if( count( $packageList ) == 0 )
00249     {
00250         // all packages are imported.
00251         return true;
00252     }
00253 
00254     // 2. check $packgesList exists in $packageDir(means packages downloaded but not imported)
00255     //    if yes - ask user to import or not. If not - go out
00256     if( !checkDir( $packageDir ) )
00257         return false;
00258 
00259     $downloadPackageList = array();
00260     foreach( $packageList as $packageName )
00261     {
00262         if( file_exists( "$packageDir/$packageName.ezpkg" ) )
00263         {
00264             global $autoMode;
00265             if( $autoMode == 'on' )
00266             {
00267                 $action = 'y';
00268             }
00269             else
00270             {
00271                 $action = getUserInput( "Package '$packageName' already downloaded. Download it anyway? [y/n]: " );
00272             }
00273 
00274             if ( strpos( $action, 'n' ) === 0 )
00275                 continue;
00276         }
00277 
00278         $downloadPackageList[] = $packageName;
00279     }
00280 
00281     //
00282     // download
00283     //
00284     showMessage2( "Downloading..." );
00285     if( count( $downloadPackageList ) > 0 )
00286     {
00287         // TODO: using 'eZStepSiteTypes' is hack.
00288         //       need to exclude 'downloadFile' from that class.
00289         //include_once( 'kernel/setup/steps/ezstep_site_types.php' );
00290 
00291         $tpl = false;
00292         $http = false;
00293         $ini = false;
00294         $persistenceList = false;
00295 
00296         $downloader = new eZStepSiteTypes( $tpl, $http, $ini, $persistenceList );
00297 
00298         foreach( $downloadPackageList as $packageName )
00299         {
00300             showMessage( "$packageName" );
00301 
00302             $archiveName = $downloader->downloadFile( "$packageURL/$packageName.ezpkg", $packageDir );
00303             if ( $archiveName === false )
00304                 showError( "download error - " . $downloader->ErrorMsg );
00305         }
00306     }
00307 
00308     //
00309     // import
00310     //
00311     showMessage2( "Importing..." );
00312     foreach( $packageList as $packageName )
00313     {
00314         showMessage( "$packageName" );
00315 
00316         $package = eZPackage::import( "$packageDir/$packageName.ezpkg", $packageName, false, $packageRepository );
00317         if( !is_object( $package ) )
00318             showError( "Faild to import '$packageName' package: err = $package" );
00319     }
00320 
00321     return true;
00322 }
00323 
00324 /*!
00325  install packages
00326 */
00327 function installPackages( $packageList, $params = array() )
00328 {
00329     global $cli;
00330 
00331     showMessage2( "Installing..." );
00332 
00333     // copy/paste from eZPackage
00334     if ( !isset( $params['path'] ) )
00335         $params['path'] = false;
00336 
00337     // process packages
00338     $action = false;
00339     while( ( list( , $packageName ) = each( $packageList ) ) && $action != EZ_INSTALL_PACKAGE_EXTRA_ACTION_QUIT )
00340     {
00341         $action = false;
00342 
00343         $cli->output( $cli->stylize( 'emphasize', "Installing package '$packageName'" ), true );
00344 
00345         $package = eZPackage::fetch( $packageName );
00346         if ( !is_object( $package ) )
00347         {
00348             showError( "can't fetch package '$packageName'. Aborting..." );
00349         }
00350 
00351         // skip package which can not be installed(e.g. which can be imported only, like 'design' and 'site' types)
00352         if ( $package->attribute( 'install_type' ) != 'install' )
00353         {
00354             continue;
00355         }
00356 
00357         $packageType = $package->attribute( 'type' );
00358         $packageItems = $package->installItemsList();
00359 
00360         while( ( list( , $item ) = each( $packageItems ) ) && $action != EZ_INSTALL_PACKAGE_EXTRA_ACTION_QUIT
00361                                                            && $action != EZ_INSTALL_PACKAGE_EXTRA_ACTION_SKIP_PACKAGE )
00362         {
00363             $itemInstalled = false;
00364             do
00365             {
00366                 $action = false;
00367                 $package->installItem( $item, $params );
00368 
00369                 if ( isset( $params['error'] ) && is_array( $params['error'] ) && count( $params['error'] ) > 0 )
00370                 {
00371                     global $autoMode;
00372                     if( $autoMode == 'on' )
00373                     {
00374                         switch( $packageType )
00375                         {
00376                             case 'contentclass':
00377                                 $action = 2;
00378                                 break;
00379                             case 'extension':
00380                                 $action = 1;
00381                                 break;
00382                             default:
00383                                 $action = handlePackageError( $params['error'] );
00384                                 break;
00385                         }
00386                     }
00387                     else
00388                     {
00389                         $action = handlePackageError( $params['error'] );
00390                     }
00391 
00392                     $params['error']['choosen_action'] = $action;
00393                 }
00394                 else
00395                 {
00396                     $itemInstalled = true;
00397                 }
00398             }
00399             while( !$itemInstalled && $action != EZ_INSTALL_PACKAGE_EXTRA_ACTION_QUIT
00400                                    && $action != EZ_INSTALL_PACKAGE_EXTRA_ACTION_SKIP_PACKAGE );
00401         }
00402     }
00403 }
00404 
00405 function siteAccessMap( $siteAccessNameArray )
00406 {
00407     if( is_array( $siteAccessNameArray ) )
00408     {
00409         //Build array map of checked siteaccesses.
00410         //Siteaccess name as key, points to root dir, to be used in eZINI methods.
00411         $siteAccessMap = array();
00412         foreach( $siteAccessNameArray as $siteAccessName )
00413         {
00414             $mapEntry = checkSiteaccess( $siteAccessName );
00415             $siteAccessMap[] = $mapEntry;
00416         }
00417         return $siteAccessMap;
00418     }
00419     else
00420     {
00421         return false;
00422     }
00423 }
00424 
00425 function checkSiteaccess( $siteAccess, $bailOutOnError = false )
00426 {
00427     //include_once( 'lib/ezutils/classes/ezextension.php' );
00428     $extensionBaseDir = eZExtension::baseDirectory();
00429     $extensionNameArray = eZExtension::activeExtensions();
00430     $siteAccessSettingsDir = '/settings/siteaccess/';
00431     $siteAccessExists = false;
00432 
00433     if( file_exists( 'settings/siteaccess/' . $siteAccess ) )
00434     {
00435         $siteAccessExists = true;
00436     }
00437     else
00438     {
00439         // Not found, check if it exists in extensions
00440         foreach( $extensionNameArray as $extensionName )
00441         {
00442             $extensionSiteaccessPath = $extensionBaseDir . '/' . $extensionName . $siteAccessSettingsDir . $siteAccess;
00443             if( file_exists( $extensionSiteaccessPath ) )
00444             {
00445                 $siteAccessExists = true;
00446                 break;
00447             }
00448         }
00449     }
00450 
00451     if( !$siteAccessExists && $bailOutOnError )
00452     {
00453         showError( "Siteaccess '" . $siteAccess . "' does not exist. Exiting..." );
00454     }
00455 
00456     return $siteAccessExists;
00457 }
00458 
00459 //
00460 // For BC with eZWebin 1.2
00461 //
00462 function postInstallAdminSiteaccessINIUpdate( $params )
00463 {
00464     $siteINI = eZINI::instance( "site.ini.append.php", "settings/siteaccess/" . $params['admin_siteaccess'], null, false, null, true );
00465     $siteINI->setVariable( "DesignSettings", "SiteDesign", $params['admin_siteaccess'] );
00466     $siteINI->setVariable( "DesignSettings", "AdditionalSiteDesignList", array( "admin" ) );
00467     $siteINI->setVariable( "SiteAccessSettings", "RelatedSiteAccessList", $params['all_siteaccess_list'] );
00468     $siteINI->setVariable( "FileSettings", "VarDir", "var/ezwebin_site" );
00469     $siteINI->save();
00470 }
00471 
00472 function postInstallUserSiteaccessINIUpdate( $params )
00473 {
00474     $siteINI = eZINI::instance( "site.ini.append.php", "settings/siteaccess/" . $params['user_siteaccess'], null, false, null, true );
00475     $siteINI->setVariable( "DesignSettings", "SiteDesign", $params['main_site_design'] );
00476     $siteINI->setVariable( "SiteAccessSettings", "RelatedSiteAccessList", $params['all_siteaccess_list'] );
00477     $siteINI->setVariable( "FileSettings", "VarDir", "var/ezwebin_site" );
00478     $siteINI->save( false, false, false, false, true, true );
00479 }
00480 
00481 function createTranslationSiteAccesses( $params )
00482 {
00483     foreach( $params['locales'] as $locale )
00484     {
00485         // Prepare 'SiteLanguageList':
00486         // make $locale as 'top priority language'
00487         // and append 'primary language' as fallback language.
00488         $primaryLanguage = $params['primary_language'];
00489         $languageList = array( $locale );
00490         if ( $locale != $primaryLanguage )
00491         {
00492             $languageList[] = $primaryLanguage;
00493         }
00494 
00495         eZSiteInstaller::createSiteAccess( array( 'src' => array( 'siteaccess' => $params['user_siteaccess'] ),
00496                                                   'dst' => array( 'siteaccess' => eZSiteInstaller::languageNameFromLocale( $locale ),
00497                                                                   'settings' => array( 'site.ini' => array( 'RegionalSettings' => array( 'Locale' => $locale,
00498                                                                                                                                          'ContentObjectLocale' => $locale,
00499                                                                                                                                          'TextTranslation' => $locale != 'eng-GB' ? 'enabled' : 'disabled',
00500                                                                                                                                          'SiteLanguageList' => $languageList ) ) ) ) ) );
00501     }
00502 }
00503 
00504 function resetINI( $settingsGroups, $iniToReset )
00505 {
00506     foreach( $settingsGroups['groups'] as $settingsGroup )
00507     {
00508         if( $settingsGroup['name'] === $iniToReset )
00509         {
00510             $iniFilename = $settingsGroup['name'] . '.append.php';
00511             $ini = eZINI::instance( $iniFilename, $settingsGroups['settings_dir'] );
00512             $ini->reset();
00513         }
00514     }
00515 }
00516 
00517 function languageMatrixDefinition()
00518 {
00519     $matrixDefinition = new eZMatrixDefinition();
00520     $matrixDefinition->addColumn( "Site URL", "site_url" );
00521     $matrixDefinition->addColumn( "Siteaccess", "siteaccess" );
00522     $matrixDefinition->addColumn( "Language name", "language_name" );
00523     $matrixDefinition->decodeClassAttribute( $matrixDefinition->xmlString() );
00524 
00525     return $matrixDefinition;
00526 }
00527 
00528 function templateLookObjectData( $params )
00529 {
00530     $languageSettingsMatrixDefinition = languageMatrixDefinition();
00531 
00532     $siteaccessUrls = $params['siteaccess_urls'];
00533 
00534     // set 'language settings' matrix data
00535     $siteaccessAliasTable = array();
00536     foreach( $siteaccessUrls['translation'] as $name => $urlInfo )
00537     {
00538         $siteaccessAliasTable[] = $urlInfo['url'];
00539         $siteaccessAliasTable[] = $name;
00540         $siteaccessAliasTable[] = ucfirst( $name );
00541     }
00542 
00543     //create data array
00544     $templateLookData = array( "site_map_url" => array( "DataText" => "Site map",
00545                                                         "Content" => "/content/view/sitemap/2" ),
00546                                 "tag_cloud_url" => array( "DataText" => "Tag cloud",
00547                                                           "Content" => "/content/view/tagcloud/2" ),
00548                                 "login_label" => array( "DataText" => "Login" ),
00549                                 "logout_label" => array( "DataText" => "Logout" ),
00550                                 "my_profile_label" => array( "DataText" => "My profile" ),
00551                                 "register_user_label" => array( "DataText" => "Register" ),
00552                                 "rss_feed" => array( "DataText" => "/rss/feed/my_feed" ),
00553                                 "shopping_basket_label" => array( "DataText" => "Shopping basket" ),
00554                                 "site_settings_label" => array( "DataText" => "Site settings" ),
00555                                 "language_settings" => array( "MatrixTitle" => "Language settings",
00556                                                               "MatrixDefinition" => $languageSettingsMatrixDefinition,
00557                                                               "MatrixCells" => $siteaccessAliasTable ),
00558                                 "footer_text" => array( "DataText" => "Copyright &#169; 2007 eZ Systems AS. All rights reserved." ),
00559                                 "hide_powered_by" => array( "DataInt" => 0 ),
00560                                 "footer_script" => array( "DataText" => "" ) );
00561 
00562     return $templateLookData;
00563 }
00564 
00565 function updateINIAccessType( $accessType, $params )
00566 {
00567     if( $accessType === 'url' || $accessType === 'url' )
00568         return;
00569 
00570     // avoid double check of 'hostname' and 'host'
00571     if( $accessType === 'hostname' || $accessType === 'host' )
00572         $accessType = 'hostname';
00573 
00574     $portMatch = array();
00575     $hostMatch = array();
00576 
00577     $siteINI = eZINI::instance( "site.ini.append.php", "settings/override", null, false, null, true );
00578 
00579     $siteaccessTypes = $params['siteaccess_urls'];
00580 
00581     // append webin's hosts to existing info.
00582     if( $accessType === 'hostname' || $accessType === 'host' )
00583         $hostMatch = $siteINI->variable( 'SiteAccessSettings', 'HostMatchMapItems' );
00584 
00585     foreach( $siteaccessTypes as $siteaccessList )
00586     {
00587         foreach( $siteaccessList as $siteaccessName => $urlInfo )
00588         {
00589             switch( $accessType )
00590             {
00591                 case 'port':
00592                     {
00593                         $port = $urlInfo['port'];
00594                         $siteINI->setVariable( 'PortAccessSettings', $port, $siteaccessName );
00595                     } break;
00596 
00597                 case 'hostname':
00598                     {
00599                         $host = $urlInfo['host'];
00600                         $hostMatch[] = $host . ';' . $siteaccessName;
00601                     }
00602             }
00603         }
00604     }
00605 
00606     if( $accessType === 'hostname' )
00607         $siteINI->setVariable( 'SiteAccessSettings', 'HostMatchMapItems', $hostMatch );
00608 
00609     $siteINI->save( false, false, false, false, true, true );
00610 }
00611 
00612 ?>