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