Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezscript.php
Go to the documentation of this file.
1 <?php
48 class eZScript
49 {
50 public $CurrentOptions;
51 public $CurrentOptionConfig;
52 public $CurrentStandardOptions;
53 public $CurrentExcludeOptions;
54 public $CurrentOptionHelp;
55 public $IterationTrueString;
56 public $IterationFalseString;
57 public $IterationNumericStrings;
58 public $IterationWrapNumeric;
59 public $IterationIndex;
60 public $IterationColumn;
61 public $IterationColumnMax;
62 public $IterationMax;
63 public $AllowedDebugLevels;
64 public $UseDebugAccumulators;
65 public $UseDebugTimingPoints;
66 public $UseIncludeFiles;
67 public $Description;
68 public $MinVersion;
69 public $MaxVersion;
70 public $ArgumentConfig;
74 function __construct( $settings = array() )
75 {
76 $settings += array(
77 'debug-message' => false,
78 'debug-output' => false,
79 'debug-include' => false,
80 'debug-levels' => false,
81 'debug-accumulator' => false,
82 'debug-timing' => false,
83 'use-session' => false,
84 'use-extensions' => true,
85 'use-modules' => false,
86 'user' => false,
87 'description' => 'eZ Publish script',
88 'site-access' => false,
89 'min_version' => false,
90 'max_version' => false
91 );
92 $this->updateSettings( $settings );
93 $this->ExitCode = false;
94 $this->IsQuiet = false;
95 $this->ShowVerbose = false;
96 $this->IsInitialized = false;
97 $this->CurrentOptions = false;
98 $this->CurrentOptionConfig = false;
99 $this->CurrentStandardOptions = false;
100 $this->CurrentExcludeOptions = false;
101 $this->CurrentOptionHelp = false;
102
103 $this->IterationTrueString = '.';
104 $this->IterationFalseString = '~';
105 $this->IterationNumericStrings = false;
106 $this->IterationWrapNumeric = false;
107 $this->IterationIndex = 0;
108 $this->IterationColumn = 0;
109 $this->IterationColumnMax = 70;
110 $this->IterationMax = false;
111 $this->InitializationErrorMessage = 'unknown error';
112 }
113
135 private function updateSettings( array $settings = array() )
136 {
137 if ( isset( $settings['debug-message'] ) )
138 $this->DebugMessage = $settings['debug-message'];
139 if ( isset( $settings['debug-output'] ) )
140 $this->UseDebugOutput = $settings['debug-output'];
141 if ( isset( $settings['debug-levels'] ) )
142 $this->AllowedDebugLevels = $settings['debug-levels'];
143 if ( isset( $settings['debug-accumulator'] ) )
144 $this->UseDebugAccumulators = $settings['debug-accumulator'];
145 if ( isset( $settings['debug-timing'] ) )
146 $this->UseDebugTimingPoints = $settings['debug-timing'];
147 if ( isset( $settings['debug-include'] ) )
148 $this->UseIncludeFiles = $settings['debug-include'];
149 if ( isset( $settings['use-session'] ) )
150 $this->UseSession = $settings['use-session'];
151 if ( isset( $settings['use-modules'] ) )
152 $this->UseModules = $settings['use-modules'];
153 if ( isset( $settings['use-extensions'] ) )
154 $this->UseExtensions = $settings['use-extensions'];
155 if ( isset( $settings['user'] ) )
156 $this->User = $settings['user'];
157 if ( isset( $settings['site-access'] ) )
158 $this->SiteAccess = $settings['site-access'];
159 if ( isset( $settings['description'] ) )
160 $this->Description = $settings['description'];
161 if ( isset( $settings['min_version'] ) )
162 $this->MinVersion = $settings['min_version'];
163 if ( isset( $settings['max_version'] ) )
164 $this->MaxVersion = $settings['max_version'];
165 }
166
170 function validateVersion()
171 {
172 $versionValidated = false;
173 $ezversion = eZPublishSDK::version();
174 if ( $this->MinVersion !== false )
175 {
176 if ( $this->MaxVersion !== false )
177 {
178 if ( version_compare( $this->MinVersion, $ezversion , 'le' ) &&
179 version_compare( $this->MaxVersion, $ezversion , 'ge' ) )
180 {
181 return true;
182 }
183 return false;
184 }
185 if ( version_compare( $this->MinVersion, $ezversion , 'le' ) )
186 {
187 return true;
188 }
189 return false;
190 }
191 else
192 {
193 if ( version_compare( $this->MaxVersion, $ezversion , 'ge' ) )
194 {
195 return true;
196 }
197 return false;
198 }
199 }
200
207 function startup()
208 {
209 error_reporting( E_ALL & ~E_DEPRECATED );
210
211 eZDebug::setHandleType( eZDebug::HANDLE_TO_PHP );
212
213 if ( php_sapi_name() != 'cli' )
214 {
215 $cli = eZCLI::instance();
216 $cli->output( "PHP is currently using the '" . php_sapi_name() . "' interface. Make sure it is using the 'cli' interface." );
217 exit( 1 );
218 }
219
220 $ini = eZINI::instance();
221 $phpLocale = trim( $ini->variable( 'RegionalSettings', 'SystemLocale' ) );
222 if ( $phpLocale != '' )
223 {
224 setlocale( LC_ALL, explode( ',', $phpLocale ) );
225 }
226
227 // Set correct site timezone
228 $timezone = $ini->variable( "TimeZoneSettings", "TimeZone" );
229 if ( $timezone )
230 {
231 date_default_timezone_set( $timezone );
232 }
233
234 // Check if the current system user is allowed to execute the script
235 $fileIni = eZINI::instance( 'file.ini' );
236 $checkSystemUser = $fileIni->variable( 'FileSettings', 'SystemUserCheck' ) == 'enabled' ;
237 if ( $checkSystemUser && function_exists( 'posix_geteuid' ) )
238 {
239 $currentUser = posix_getpwuid( posix_geteuid() );
240 $requiredUser = $fileIni->variable( 'FileSettings', 'RequiredSystemUser' );
241 if ( $currentUser['name'] !== $requiredUser )
242 {
243 $cli = eZCLI::instance();
244 $cli->error( "Scripts must be executed as '{$requiredUser}'." );
245 exit( 1 );
246 }
247 }
248 }
249
257 function initialize()
258 {
259 if ( !ezpKernel::hasInstance() )
260 {
261 if ( ob_get_length() != 0 )
262 ob_end_clean();
263 }
264
265 // Initialize text codec settings
266 $this->updateTextCodecSettings();
267
268 // Initialize debug settings
269 $this->updateDebugSettings( $this->UseDebugOutput );
270
271 // Set the different permissions/settings.
272 $ini = eZINI::instance();
273 $iniFilePermission = $ini->variable( 'FileSettings', 'StorageFilePermissions' );
274 $iniDirPermission = $ini->variable( 'FileSettings', 'StorageDirPermissions' );
275 $iniVarDirectory = eZSys::cacheDirectory() ;
276
277 eZCodePage::setPermissionSetting( array( 'file_permission' => octdec( $iniFilePermission ),
278 'dir_permission' => octdec( $iniDirPermission ),
279 'var_directory' => $iniVarDirectory ) );
280
281 eZExecution::addCleanupHandler( 'eZDBCleanup' );
282 eZExecution::addFatalErrorHandler( 'eZFatalError' );
283
284 eZDebug::setHandleType( eZDebug::HANDLE_FROM_PHP );
285
286 if ( $this->UseExtensions )
287 {
288 // Check for extension
289 eZExtension::activateExtensions( 'default' );
290 // Extension check end
291 }
292 else if ( !$this->isQuiet() )
293 {
294 $cli = eZCLI::instance();
295 $cli->output( "Notice: This script uses 'use-extensions' => false, meaning extension settings are not loaded!" );
296 }
297
298 $siteaccess = $this->SiteAccess;
299 if ( $siteaccess )
300 {
301 $access = array( 'name' => $siteaccess,
302 'type' => eZSiteAccess::TYPE_STATIC );
303 }
304 else
305 {
306 $ini = eZINI::instance();
307 $siteaccess = $ini->variable( 'SiteSettings', 'DefaultAccess' );
308 $access = array( 'name' => $siteaccess,
309 'type' => eZSiteAccess::TYPE_DEFAULT );
310 }
311
312 $access = eZSiteAccess::change( $access );
313
314 if ( $this->UseExtensions )
315 {
316 // Check for siteaccess extension
317 eZExtension::activateExtensions( 'access' );
318 // Extension check end
319 }
320
321 // Now that all extensions are activated and siteaccess has been changed, reset
322 // all eZINI instances as they may not take into account siteaccess specific settings.
323 eZINI::resetAllInstances( false );
324
325 // Set the global setting which is read by the session lib
326 $GLOBALS['eZSiteBasics']['session-required'] = $this->UseSession;
327
328 if ( $this->UseSession )
329 {
330 $db = eZDB::instance();
331 if ( $db->isConnected() )
332 {
333 eZSession::start();
334 }
335 else
336 {
337 $this->setIsInitialized( false );
338 $this->InitializationErrorMessage = 'database error: ' . $db->errorMessage();
339 return;
340 }
341 }
342
343 if ( $this->User )
344 {
345 $userLogin = $this->User['login'];
346 $userPassword = $this->User['password'];
347 if ( $userLogin and $userPassword )
348 {
349 $userID = eZUser::loginUser( $userLogin, $userPassword );
350 if ( !$userID )
351 {
352 $cli = eZCLI::instance();
353 if ( $this->isLoud() )
354 $cli->warning( 'Failed to login with user ' . $userLogin );
355 eZExecution::cleanup();
356 eZExecution::setCleanExit();
357 }
358 }
359 }
360
361 // Initialize module handling
362 if ( $this->UseModules )
363 {
364 $moduleRepositories = eZModule::activeModuleRepositories( $this->UseExtensions );
365 eZModule::setGlobalPathList( $moduleRepositories );
366 }
367 $this->setIsInitialized( true );
368 }
369
370 function isInitialized()
371 {
372 return $this->IsInitialized;
373 }
374
375 public function setIsInitialized( $isInitialized )
376 {
377 $this->IsInitialized = (bool)$isInitialized;
378 }
379
380 function initializationError()
381 {
382 return $this->InitializationErrorMessage;
383 }
384
395 function shutdown( $exitCode = false, $exitText = false )
396 {
397 $cli = eZCLI::instance();
398 if ( class_exists( 'eZDB' )
399 and eZDB::hasInstance() )
400 {
401 $db = eZDB::instance( false, array( 'show_errors' => false ) );
402 // Perform transaction check
403 $transactionCounterCheck = eZDB::checkTransactionCounter();
404 if ( isset( $transactionCounterCheck['error'] ) )
405 $cli->error( $transactionCounterCheck['error'] );
406
407 if ( $this->UseSession and
408 $db->isConnected() )
409 {
410 eZUser::logoutCurrent();
411 eZSession::remove();
412 }
413 }
414
415 $webOutput = $cli->isWebOutput();
416
417 if ( $this->UseDebugOutput or
418 eZDebug::isDebugEnabled() )
419 {
420 if ( $this->DebugMessage )
421 fputs( STDERR, $this->DebugMessage );
422 fputs( STDERR, eZDebug::printReport( false, $webOutput, true,
423 $this->AllowedDebugLevels, $this->UseDebugAccumulators,
424 $this->UseDebugTimingPoints, $this->UseIncludeFiles ) );
425 }
426
427 eZExecution::cleanup();
428 eZExecution::setCleanExit();
429 eZExpiryHandler::shutdown();
430 $this->setIsInitialized( false );
431 if ( $exitCode !== false )
432 $this->ExitCode = $exitCode;
433 if ( $this->ExitCode !== false )
434 {
435 if ( $exitText !== false )
436 $cli->output( $exitText );
437 exit( $this->ExitCode );
438 }
439 }
440
447 function setDebugMessage( $message )
448 {
449 $this->DebugMessage = $message;
450 }
451
456 function setUseDebugOutput( $useDebug )
457 {
458 $this->UseDebugOutput = $useDebug;
459 }
460
466 function setUseDebugAccumulators( $useAccumulators )
467 {
468 $this->UseDebugAccumulators = $useAccumulators;
469 }
470
476 function setUseDebugTimingPoints( $useTimingPoints )
477 {
478 $this->UseDebugTimingPoints = $useTimingPoints;
479 }
480
486 function setUseIncludeFiles( $useIncludeFiles )
487 {
488 $this->UseIncludeFiles = $useIncludeFiles;
489 }
490
497 function setAllowedDebugLevels( $allowedDebugLevels )
498 {
499 $this->AllowedDebugLevels = $allowedDebugLevels;
500 }
501
507 function setUseSession( $useSession )
508 {
509 $this->UseSession = $useSession;
510 }
511
516 function setUseExtensions( $useExtensions )
517 {
518 $this->UseExtensions = $useExtensions;
519 }
520
526 function setUseSiteAccess( $siteAccess )
527 {
528 $this->SiteAccess = $siteAccess;
529 }
530
534 function usedSiteAccess()
535 {
536 return $this->SiteAccess;
537 }
538
539 function setUseModules( $useModules )
540 {
541 $this->UseModules = $useModules;
542 }
543
544 function setUser( $userLogin, $userPassword )
545 {
546 $this->User = array( 'login' => $userLogin,
547 'password' => $userPassword );
548 }
549
556 function setShowVerboseOutput( $verbose )
557 {
558 if ( $verbose === true )
559 $verbose = 1;
560 $this->ShowVerbose = $verbose;
561 }
562
566 function verboseOutputLevel()
567 {
568 return $this->ShowVerbose;
569 }
570
574 function currentOptions()
575 {
576 return $this->CurrentOptions;
577 }
578
582 function currentOptionConfig()
583 {
584 return $this->CurrentOptionConfig;
585 }
586
591 function setExitCode( $code = false )
592 {
593 $this->ExitCode = $code;
594 }
595
596 function exitCode()
597 {
598 return $this->ExitCode;
599 }
600
606 function setIsQuiet( $isQuiet )
607 {
608 $cli = eZCLI::instance();
609 $this->IsQuiet = $isQuiet;
610 $cli->setIsQuiet( $isQuiet );
611 }
612
617 function isQuiet()
618 {
619 return $this->IsQuiet;
620 }
621
626 function isLoud()
627 {
628 return !$this->IsQuiet;
629 }
630
631 function setIterationData( $trueString, $falseString,
632 $numericStrings = false, $wrapNumeric = false )
633 {
634 $this->IterationTrueString = $trueString;
635 $this->IterationFalseString = $falseString;
636 $this->IterationNumericStrings = $numericStrings;
637 $this->IterationWrapNumeric = $wrapNumeric;
638 }
639
640 function resetIteration( $iterationMax = false, $startIndex = 0 )
641 {
642 $this->IterationIndex = $startIndex;
643 $this->IterationColumn = 0;
644 $this->IterationMax = $iterationMax;
645 }
646
647 function iterate( $cli, $status, $text = false )
648 {
649 if ( !$this->IterationNumericStrings )
650 $status = (bool)$status;
651 if ( $this->verboseOutputLevel() === false or
652 $text === false )
653 {
654 if ( is_bool( $status ) )
655 {
656 $statusText = $status ? $this->IterationTrueString : $this->IterationFalseString;
657 }
658 else
659 {
660 if ( $this->IterationWrapNumeric )
661 $status = $status % count( $this->IterationNumericStrings );
662 if ( $status < count( $this->IterationNumericStrings ) )
663 $statusText = $this->IterationNumericStrings[$status];
664 else
665 $statusText = ' ';
666 }
667 $endLine = false;
668 $changeLine = false;
669 ++$this->IterationIndex;
670 ++$this->IterationColumn;
671 $iterationColumn = $this->IterationColumn;
672 if ( $this->IterationColumn >= $this->IterationColumnMax )
673 {
674 $this->IterationColumn = 0;
675 $changeLine = true;
676 }
677 if ( $this->IterationMax !== false )
678 {
679 if ( $this->IterationIndex >= $this->IterationMax )
680 {
681 $this->IterationColumn = 0;
682 $changeLine = true;
683 }
684 }
685 if ( $changeLine )
686 {
687 if ( $this->IterationMax !== false )
688 {
689 $spacing = $this->IterationColumnMax - $iterationColumn;
690 $percent = ( $this->IterationIndex * 100 ) / $this->IterationMax;
691 if ( $percent > 100.0 )
692 $percent = 100;
693 else
694 $spacing += 1;
695 $percentText = number_format( $percent, 2 ) . '%';
696 $statusText .= str_repeat( ' ', $spacing );
697 $statusText .= $percentText;
698 }
699 $endLine = true;
700 }
701 $cli->output( $statusText, $endLine );
702 }
703 else
704 {
705 $statusLevel = $status;
706 if ( is_bool( $status ) )
707 $statusLevel = $status ? 0 : 1;
708 if ( $statusLevel > 0 )
709 {
710 --$statusLevel;
711 $statusLevels = array( 'warning', 'failure' );
712 if ( $statusLevel > count( $statusLevels ) )
713 $statusLevel = count( $statusLevels ) - 1;
714 $levelText = $statusLevels[$statusLevel];
715 $cli->output( $cli->stylize( $levelText, $text ) );
716 }
717 else
718 {
719 $cli->output( $text );
720 }
721 }
722 }
723
724 function showHelp( $useStandardOptions = false, $optionConfig = false, $optionHelp = false, $argumentConfig = false, $arguments = false )
725 {
726 if ( $useStandardOptions === false )
727 {
728 $useStandardOptions = $this->CurrentStandardOptions;
729 }
730 if ( $optionConfig === false )
731 {
732 $optionConfig = $this->CurrentOptionConfig;
733 }
734 if ( $optionHelp === false )
735 {
736 $optionHelp = $this->CurrentOptionHelp;
737 }
738 if ( $argumentConfig === false )
739 {
740 $argumentConfig = $this->ArgumentConfig;
741 }
742 $optionList = array();
743 foreach ( $optionConfig['list'] as $configItem )
744 {
745 if ( in_array( $configItem['name'], $this->CurrentExcludeOptions ) )
746 continue;
747 $optionText = '-';
748 if ( $configItem['is-long-option'] )
749 $optionText .= '-';
750 $optionText .= $configItem['name'];
751 if ( $configItem['has-value'] and $configItem['is-long-option'] )
752 $optionText .= "=VALUE";
753 $hasMultipleValues = ( $configItem['quantifier']['min'] > 1 or
754 $configItem['quantifier']['max'] === false or
755 $configItem['quantifier']['max'] > 1 );
756 if ( $hasMultipleValues )
757 $optionText .= "...";
758 $optionDescription = '';
759 if ( isset( $optionHelp[$configItem['name']] ) )
760 $optionDescription = $optionHelp[$configItem['name']];
761 $optionList[] = array( $optionText, $optionDescription );
762 }
763 if ( $arguments === false )
764 {
765 $arguments = $_SERVER['argv'];
766 $program = $arguments[0];
767 }
768 $cli = eZCLI::instance();
769 $generalOptionList = array();
770 $generalOptionList = array();
771 if ( $useStandardOptions )
772 {
773 $generalOptionList[] = array( '-h,--help', 'display this help and exit');
774 $generalOptionList[] = array( '-q,--quiet', 'do not give any output except when errors occur' );
775 if ( $useStandardOptions['siteaccess'] )
776 $generalOptionList[] = array( '-s,--siteaccess', "selected siteaccess for operations,\nif not specified default siteaccess is used" );
777 if ( $useStandardOptions['debug'] )
778 $generalOptionList[] = array( '-d,--debug...', ( "display debug output at end of execution,\n" .
779 "the following debug items can be controlled: \n" .
780 "all, accumulator, include, timing, error, warning, debug, notice or strict." ) );
781 if ( $useStandardOptions['colors'] )
782 {
783 $generalOptionList[] = array( '-c,--colors', 'display output using ANSI colors (default)' );
784 $generalOptionList[] = array( '--no-colors', 'do not use ANSI coloring' );
785 }
786 if ( $useStandardOptions['user'] )
787 {
788 $generalOptionList[] = array( '-l,--login USER', 'login with USER and use it for all operations' );
789 $generalOptionList[] = array( '-p,--password PWD', 'use PWD as password for USER' );
790 }
791 if ( $useStandardOptions['log'] )
792 {
793 $generalOptionList[] = array( '--logfiles', 'create log files' );
794 $generalOptionList[] = array( '--no-logfiles', 'do not create log files (default)' );
795 }
796 if ( $useStandardOptions['verbose'] )
797 {
798 $generalOptionList[] = array( '-v,--verbose...', "display more information, \nused multiple times will increase amount of information" );
799 }
800 if( $useStandardOptions['root'] )
801 {
802 $generalOptionList[] = array( '-r,--allow-root-user', "Allows the script to be run by the root user" );
803 }
804 }
805 $description = $this->Description;
806 $helpText = "Usage: " . $program;
807 if ( count( $optionList ) > 0 or count( $generalOptionList ) > 0 )
808 {
809 $helpText .= " [OPTION]...";
810 }
811 if ( $argumentConfig && isset( $argumentConfig['list'] ) && is_array( $argumentConfig['list'] ) )
812 {
813 foreach ( $argumentConfig['list'] as $argumentItem )
814 {
815 $argumentName = strtoupper( $argumentItem['name'] );
816 $quantifier = $argumentItem['quantifier'];
817 if ( $quantifier['min'] > 1 or $quantifier['max'] === false or $quantifier['max'] > 1 )
818 $helpText .= " [$argumentName]...";
819 else
820 $helpText .= " [$argumentName]";
821 }
822 }
823 if ( $description )
824 $helpText .= "\n" . $description . "\n";
825 if ( count( $generalOptionList ) > 0 )
826 {
827 $helpText .= "\nGeneral options:\n";
828 $maxLength = 0;
829 foreach ( $generalOptionList as $optionItem )
830 {
831 $maxLength = max( strlen( $optionItem[0] ), $maxLength );
832 }
833 $spacingLength = $maxLength + 2;
834 foreach ( $generalOptionList as $optionItem )
835 {
836 $option = $optionItem[0];
837 $optionDescription = $optionItem[1];
838 $optionLines = explode( "\n", $option );
839 $optionDescriptionLines = explode( "\n", $optionDescription );
840 $count = max( count( $optionLines ), count( $optionDescriptionLines ) );
841 for ( $i = 0; $i < $count; ++$i )
842 {
843 $optionText = '';
844 if ( isset( $optionLines[$i] ) )
845 $optionText = $optionLines[$i];
846 $optionDescriptionText = '';
847 if ( isset( $optionDescriptionLines[$i] ) )
848 $optionDescriptionText = $optionDescriptionLines[$i];
849 $spacing = $spacingLength - strlen( $optionText );
850 if ( $optionText or $optionDescriptionText )
851 $helpText .= ' ';
852 $helpText .= $optionText;
853 if ( $i > 0 )
854 $spacing += 2;
855 if ( $optionDescriptionText )
856 $helpText .= str_repeat( ' ', $spacing ) . $optionDescriptionText;
857 $helpText .= "\n";
858 }
859 }
860 }
861 if ( count( $optionList ) > 0 )
862 {
863 $helpText .= "\nOptions:\n";
864 $maxLength = 0;
865 foreach ( $optionList as $optionItem )
866 {
867 $maxLength = max( strlen( $optionItem[0] ), $maxLength );
868 }
869 $spacingLength = $maxLength + 2;
870 foreach ( $optionList as $optionItem )
871 {
872 $option = $optionItem[0];
873 $optionDescription = $optionItem[1];
874 $optionLines = explode( "\n", $option );
875 $optionDescriptionLines = explode( "\n", $optionDescription );
876 $count = max( count( $optionLines ), count( $optionDescriptionLines ) );
877 for ( $i = 0; $i < $count; ++$i )
878 {
879 $optionText = '';
880 if ( isset( $optionLines[$i] ) )
881 $optionText = $optionLines[$i];
882 $optionDescriptionText = '';
883 if ( isset( $optionDescriptionLines[$i] ) )
884 $optionDescriptionText = $optionDescriptionLines[$i];
885 $spacing = $spacingLength - strlen( $optionText );
886 if ( $optionText or $optionDescriptionText )
887 $helpText .= ' ';
888 $helpText .= $optionText;
889 if ( $i > 0 )
890 $spacing += 2;
891 if ( $optionDescriptionText )
892 $helpText .= str_repeat( ' ', $spacing ) . $optionDescriptionText;
893 $helpText .= "\n";
894 }
895 }
896 }
897 $cli->output( $helpText );
898 }
899
910 function getOptions( $config = '', $argumentConfig = '', $optionHelp = false,
911 $arguments = false, $useStandardOptions = true )
912 {
913 if ( is_string( $config ) )
914 $config = eZCLI::parseOptionString( $config, $tmpConfig );
915 if ( is_string( $argumentConfig ) )
916 $argumentConfig = eZCLI::parseOptionString( $argumentConfig, $tmpArgumentConfig );
917
918 if ( $useStandardOptions )
919 {
920 if ( !is_array( $useStandardOptions ) )
921 $useStandardOptions = array();
922 $useStandardOptions = array_merge( array( 'debug' => true,
923 'colors' => true,
924 'log' => true,
925 'siteaccess' => true,
926 'verbose' => true,
927 'root' => true,
928 'user' => false ),
929 $useStandardOptions );
930 }
931
932 if ( $useStandardOptions )
933 {
934 $optionConfig = $config;
935 $excludeOptions = array();
936 $optionString = "[h|help][q|quiet]";
937 $excludeOptions[] = 'h';
938 $excludeOptions[] = 'help';
939 $excludeOptions[] = 'q';
940 $excludeOptions[] = 'quiet';
941 if ( $useStandardOptions['debug'] )
942 {
943 $optionString .= "[d;*|debug;*]";
944 $excludeOptions[] = 'd';
945 $excludeOptions[] = 'debug';
946 }
947 if ( $useStandardOptions['colors'] )
948 {
949 $optionString .= "[c|colors][no-colors]";
950 $excludeOptions[] = 'c';
951 $excludeOptions[] = 'colors';
952 $excludeOptions[] = 'no-colors';
953 }
954 if ( $useStandardOptions['log'] )
955 {
956 $optionString .= "[logfiles][no-logfiles]";
957 $excludeOptions[] = 'logfiles';
958 $excludeOptions[] = 'no-logfiles';
959 }
960 if ( $useStandardOptions['siteaccess'] )
961 {
962 $optionString .= "[s:|siteaccess:]";
963 $excludeOptions[] = 's';
964 $excludeOptions[] = 'siteaccess';
965 }
966 if ( $useStandardOptions['user'] )
967 {
968 $optionString .= "[l:|login:][p:|password:]";
969 $excludeOptions[] = 'l';
970 $excludeOptions[] = 'login';
971 $excludeOptions[] = 'p';
972 $excludeOptions[] = 'password';
973 }
974 if ( $useStandardOptions['verbose'] )
975 {
976 $optionString .= "[v*|verbose*]";
977 $excludeOptions[] = 'v';
978 $excludeOptions[] = 'verbose';
979 }
980 if( $useStandardOptions['root'] )
981 {
982 $optionString .= "[r?|allow-root-user?]";
983 $excludeOptions[] = 'r';
984 $excludeOptions[] = 'allow-root-user';
985 }
986
987 $config = eZCLI::parseOptionString( $optionString, $optionConfig );
988 }
989 $cli = eZCLI::instance();
990 $options = $cli->getOptions( $config, $argumentConfig, $arguments );
991 $this->CurrentOptionConfig = $config;
992 $this->CurrentOptions = $options;
993 $this->CurrentStandardOptions = $useStandardOptions;
994 $this->CurrentExcludeOptions = $excludeOptions;
995 $this->CurrentOptionHelp = $optionHelp;
996 $this->ArgumentConfig = $argumentConfig;
997 if ( !$options )
998 {
999 if ( !$this->IsInitialized )
1000 $this->initialize();
1001 $this->shutdown( 1 );
1002 }
1003
1004 if ( $useStandardOptions )
1005 {
1006 if ( function_exists( 'posix_getuid' ) )
1007 {
1008 if( posix_getuid() === 0 )
1009 {
1010 if( !$options['allow-root-user'] )
1011 {
1012 $cli->warning( "Running scripts as root may be dangerous." );
1013 $cli->warning( "If you think you know what you are doing, you can run this script with the " );
1014 $cli->warning( "root account by appending the parameter --allow-root-user." );
1015
1016 exit( 1 );
1017 }
1018 else
1019 {
1020 // ho no, you are not going to be quiet while
1021 // running with root priviledges
1022 $this->setIsQuiet( false );
1023
1024 $cli = eZCLI::instance();
1025
1026 $cli->warning( 'With great power comes great responsibility.' );
1027 $cli->warning( "You have 10 seconds to break the script (press Ctrl-C)." );
1028 sleep( 10 );
1029 }
1030 }
1031 }
1032
1033 if ( $options['quiet'] )
1034 $this->setIsQuiet( true );
1035 $useColors = true;
1036 if ( $options['colors'] )
1037 $useColors = true;
1038 if ( $options['no-colors'] )
1039 $useColors = false;
1040 $cli->setUseStyles( $useColors );
1041 if ( $options['debug'] )
1042 {
1043 $levels = array();
1044 foreach ( $options['debug'] as $debugOption )
1045 {
1046 $levels = array_merge( $levels, explode( ',', $debugOption ) );
1047 }
1048 $allowedDebugLevels = array();
1049 $useDebugAccumulators = false;
1050 $useDebugTimingpoints = false;
1051 $useIncludeFiles = false;
1052 foreach ( $levels as $level )
1053 {
1054 if ( $level == 'all' )
1055 {
1056 $useDebugAccumulators = true;
1057 $allowedDebugLevels = false;
1058 $useDebugTimingpoints = true;
1059 break;
1060 }
1061 if ( $level == 'accumulator' )
1062 {
1063 $useDebugAccumulators = true;
1064 continue;
1065 }
1066 if ( $level == 'timing' )
1067 {
1068 $useDebugTimingpoints = true;
1069 continue;
1070 }
1071 if ( $level == 'include' )
1072 {
1073 $useIncludeFiles = true;
1074 }
1075 if ( $level == 'strict' )
1076 $level = eZDebug::LEVEL_STRICT;
1077 else if ( $level == 'error' )
1078 $level = eZDebug::LEVEL_ERROR;
1079 else if ( $level == 'warning' )
1080 $level = eZDebug::LEVEL_WARNING;
1081 else if ( $level == 'debug' )
1082 $level = eZDebug::LEVEL_DEBUG;
1083 else if ( $level == 'notice' )
1084 $level = eZDebug::LEVEL_NOTICE;
1085 else if ( $level == 'timing' )
1086 $level = eZDebug::LEVEL_TIMING_POINT;
1087 $allowedDebugLevels[] = $level;
1088 }
1089 $this->setUseDebugOutput( true );
1090 $this->setAllowedDebugLevels( $allowedDebugLevels );
1091 $this->setUseDebugAccumulators( $useDebugAccumulators );
1092 $this->setUseDebugTimingPoints( $useDebugTimingpoints );
1093 $this->setUseIncludeFiles( $useIncludeFiles );
1094 $this->setDebugMessage( "\n\n" . str_repeat( '#', 36 ) . $cli->style( 'emphasize' ) . " DEBUG " . $cli->style( 'emphasize-end' ) . str_repeat( '#', 36 ) . "\n" );
1095 }
1096 if ( is_array( $options['verbose'] ) && count( $options['verbose'] ) > 0 )
1097 {
1098 $this->setShowVerboseOutput( count( $options['verbose'] ) );
1099 }
1100
1101 if ( isset( $options['siteaccess'] ) and $options['siteaccess'] )
1102 $this->setUseSiteAccess( $options['siteaccess'] );
1103
1104 if ( $options['help'] )
1105 {
1106 if ( !$this->IsInitialized )
1107 $this->initialize();
1108 $this->showHelp();
1109 $this->shutdown( 0 );
1110 }
1111
1112 if ( isset( $options['login'] ) and $options['login'] )
1113 $this->setUser( $options['login'], isset( $options['password'] ) ? $options['password'] : false );
1114 }
1115 return $options;
1116 }
1117
1124 static function instance( $settings = array() )
1125 {
1126 if (
1127 !isset( $GLOBALS['eZScriptInstance'] )
1128 || !( $GLOBALS['eZScriptInstance'] instanceof eZScript )
1129 )
1130 {
1131 $GLOBALS['eZScriptInstance'] = new eZScript( $settings );
1132 }
1133 else if ( !empty( $settings ) )
1134 {
1135 $GLOBALS['eZScriptInstance']->updateSettings( $settings );
1136 }
1137
1138 return $GLOBALS['eZScriptInstance'];
1139 }
1140
1145 function updateDebugSettings( $useDebug = null )
1146 {
1147 global $debugOutput;
1148 global $useLogFiles;
1149 $ini = eZINI::instance();
1150 $cli = eZCLI::instance();
1151 $debugSettings = array();
1152 $debugSettings['debug-enabled'] = ( $ini->variable( 'DebugSettings', 'DebugOutput' ) == 'enabled' and
1153 $ini->variable( 'DebugSettings', 'ScriptDebugOutput' ) == 'enabled' );
1154 if ( $useDebug !== null )
1155 $debugSettings['debug-enabled'] = $useDebug;
1156 $debugSettings['debug-by-ip'] = $ini->variable( 'DebugSettings', 'DebugByIP' ) == 'enabled';
1157 $debugSettings['debug-ip-list'] = $ini->variable( 'DebugSettings', 'DebugIPList' );
1158 if ( isset( $debugOutput ) )
1159 $debugSettings['debug-enabled'] = $debugOutput;
1160 $debugSettings['debug-log-files-enabled'] = $useLogFiles;
1161 if ( $cli->useStyles() and
1162 !$cli->isWebOutput() )
1163 {
1164 $debugSettings['debug-styles'] = $cli->terminalStyles();
1165 }
1166 $logList = $ini->variable( 'DebugSettings', 'AlwaysLog' );
1167 $logMap = array( 'notice' => eZDebug::LEVEL_NOTICE,
1168 'warning' => eZDebug::LEVEL_WARNING,
1169 'error' => eZDebug::LEVEL_ERROR,
1170 'debug' => eZDebug::LEVEL_DEBUG,
1171 'strict' => eZDebug::LEVEL_STRICT );
1172 $debugSettings['always-log'] = array();
1173 foreach ( $logMap as $name => $level )
1174 {
1175 $debugSettings['always-log'][$level] = in_array( $name, $logList );
1176 }
1177 eZDebug::updateSettings( $debugSettings );
1178 }
1179
1184 function updateTextCodecSettings()
1185 {
1186 $ini = eZINI::instance( 'i18n.ini' );
1187 $i18nSettings = array();
1188 $i18nSettings['internal-charset'] = $ini->variable( 'CharacterSettings', 'Charset' );
1189 $i18nSettings['http-charset'] = $ini->variable( 'CharacterSettings', 'HTTPCharset' );
1190 $i18nSettings['mbstring-extension'] = $ini->variable( 'CharacterSettings', 'MBStringExtension' ) == 'enabled';
1191 eZTextCodec::updateSettings( $i18nSettings );
1192 }
1193
1195 public $IsInitialized;
1196 public $InitializationErrorMessage;
1197 public $DebugMessage;
1198 public $UseDebugOutput;
1199 public $UseSession;
1200 public $UseExtensions;
1201 public $UseModules;
1202 public $User;
1203 public $SiteAccess;
1204 public $ExitCode;
1205 public $IsQuiet;
1206 public $ShowVerbose;
1207 }
1208
1209 function eZDBCleanup()
1210 {
1211 if ( class_exists( 'ezdb' )
1212 and eZDB::hasInstance() )
1213 {
1214 $db = eZDB::instance();
1215 $db->setIsSQLOutputEnabled( false );
1216 }
1217 }
1218
1219 function eZFatalError()
1220 {
1221 $cli = eZCLI::instance();
1222 $endl = $cli->endlineString();
1223 $webOutput = $cli->isWebOutput();
1224 $bold = $cli->style( 'bold' );
1225 $unbold = $cli->style( 'bold-end' );
1226 $par = $cli->style( 'paragraph' );
1227 $unpar = $cli->style( 'paragraph-end' );
1228
1229 $allowedDebugLevels = true;
1230 $useDebugAccumulators = true;
1231 $useDebugTimingpoints = true;
1232
1233 eZDebug::setHandleType( eZDebug::HANDLE_NONE );
1234 if ( !$webOutput )
1235 fputs( STDERR, $endl );
1236 fputs( STDERR, $bold . "Fatal error" . $unbold . ": eZ Publish did not finish its request$endl" );
1237 fputs( STDERR, $par . "The execution of eZ Publish was abruptly ended, the debug output is present below." . $unpar . $endl );
1238 fputs( STDERR, eZDebug::printReport( false, $webOutput, true ) );
1239 }
1240
1244 if ( !function_exists( 'eZUpdateDebugSettings' ) )
1245 {
1246 function eZUpdateDebugSettings( $useDebug = null )
1247 {
1248 }
1249 }
1250
1254 function eZUpdateTextCodecSettings()
1255 {
1256 }
1257
1258 ?>
eZCLI\instance
static instance()
Definition: ezcli.php:717
eZCLI\parseOptionString
static parseOptionString( $configString, &$optionConfig)
Definition: ezcli.php:446
eZCodePage\setPermissionSetting
static setPermissionSetting( $permissionArray)
Definition: ezcodepage.php:747
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZDB\checkTransactionCounter
static checkTransactionCounter()
Definition: ezdb.php:293
eZDB\hasInstance
static hasInstance()
Definition: ezdb.php:94
eZDebug\HANDLE_TO_PHP
const HANDLE_TO_PHP
Definition: ezdebug.php:76
eZDebug\LEVEL_ERROR
const LEVEL_ERROR
Definition: ezdebug.php:61
eZDebug\printReport
static printReport( $newWindow=false, $as_html=true, $returnReport=false, $allowedDebugLevels=false, $useAccumulators=true, $useTiming=true, $useIncludedFiles=false)
Definition: ezdebug.php:1224
eZDebug\isDebugEnabled
static isDebugEnabled()
Definition: ezdebug.php:1027
eZDebug\LEVEL_STRICT
const LEVEL_STRICT
Definition: ezdebug.php:64
eZDebug\HANDLE_FROM_PHP
const HANDLE_FROM_PHP
Definition: ezdebug.php:75
eZDebug\LEVEL_WARNING
const LEVEL_WARNING
Definition: ezdebug.php:60
eZDebug\setHandleType
static setHandleType( $type)
Definition: ezdebug.php:247
eZDebug\LEVEL_TIMING_POINT
const LEVEL_TIMING_POINT
Definition: ezdebug.php:62
eZDebug\updateSettings
static updateSettings( $settings)
Definition: ezdebug.php:1132
eZDebug\LEVEL_NOTICE
const LEVEL_NOTICE
Definition: ezdebug.php:59
eZDebug\HANDLE_NONE
const HANDLE_NONE
Definition: ezdebug.php:74
eZDebug\LEVEL_DEBUG
const LEVEL_DEBUG
Definition: ezdebug.php:63
eZExecution\addCleanupHandler
static addCleanupHandler( $handler)
Definition: ezexecution.php:54
eZExecution\setCleanExit
static setCleanExit( $hasCleanExit=true)
Definition: ezexecution.php:28
eZExecution\cleanup
static cleanup()
Definition: ezexecution.php:36
eZExecution\addFatalErrorHandler
static addFatalErrorHandler( $handler)
Definition: ezexecution.php:74
eZExtension\activateExtensions
static activateExtensions( $extensionType='default', ?eZINI $siteINI=null)
Definition: ezextension.php:187
eZModule\activeModuleRepositories
static activeModuleRepositories( $useExtensions=true)
Definition: ezmodule.php:1897
eZModule\setGlobalPathList
static setGlobalPathList( $pathList)
Definition: ezmodule.php:1951
eZScript
Handles the basics of script execution.
Definition: ezscript.php:49
eZScript\$MaxVersion
$MaxVersion
Definition: ezscript.php:69
eZScript\setUseIncludeFiles
setUseIncludeFiles( $useIncludeFiles)
Definition: ezscript.php:486
eZScript\$IterationIndex
$IterationIndex
Definition: ezscript.php:59
eZScript\isQuiet
isQuiet()
Definition: ezscript.php:617
eZScript\setUser
setUser( $userLogin, $userPassword)
Definition: ezscript.php:544
eZScript\$ArgumentConfig
$ArgumentConfig
Definition: ezscript.php:70
eZScript\$IterationTrueString
$IterationTrueString
Definition: ezscript.php:55
eZScript\$CurrentExcludeOptions
$CurrentExcludeOptions
Definition: ezscript.php:53
eZScript\$IterationMax
$IterationMax
Definition: ezscript.php:62
eZScript\$User
$User
Definition: ezscript.php:1202
eZScript\$IterationColumn
$IterationColumn
Definition: ezscript.php:60
eZScript\initialize
initialize()
Definition: ezscript.php:257
eZScript\$IterationNumericStrings
$IterationNumericStrings
Definition: ezscript.php:57
eZScript\$CurrentOptionConfig
$CurrentOptionConfig
Definition: ezscript.php:51
eZScript\setDebugMessage
setDebugMessage( $message)
Definition: ezscript.php:447
eZScript\setUseExtensions
setUseExtensions( $useExtensions)
Definition: ezscript.php:516
eZScript\$UseDebugOutput
$UseDebugOutput
Definition: ezscript.php:1198
eZScript\$IsQuiet
$IsQuiet
Definition: ezscript.php:1205
eZScript\exitCode
exitCode()
Definition: ezscript.php:596
eZScript\setShowVerboseOutput
setShowVerboseOutput( $verbose)
Definition: ezscript.php:556
eZScript\setUseDebugOutput
setUseDebugOutput( $useDebug)
Definition: ezscript.php:456
eZScript\showHelp
showHelp( $useStandardOptions=false, $optionConfig=false, $optionHelp=false, $argumentConfig=false, $arguments=false)
Definition: ezscript.php:724
eZScript\verboseOutputLevel
verboseOutputLevel()
Definition: ezscript.php:566
eZScript\setUseDebugTimingPoints
setUseDebugTimingPoints( $useTimingPoints)
Definition: ezscript.php:476
eZScript\validateVersion
validateVersion()
Definition: ezscript.php:170
eZScript\$UseExtensions
$UseExtensions
Definition: ezscript.php:1200
eZScript\setUseDebugAccumulators
setUseDebugAccumulators( $useAccumulators)
Definition: ezscript.php:466
eZScript\isLoud
isLoud()
Definition: ezscript.php:626
eZScript\isInitialized
isInitialized()
Definition: ezscript.php:370
eZScript\startup
startup()
Definition: ezscript.php:207
eZScript\$UseModules
$UseModules
Definition: ezscript.php:1201
eZScript\getOptions
getOptions( $config='', $argumentConfig='', $optionHelp=false, $arguments=false, $useStandardOptions=true)
Definition: ezscript.php:910
eZScript\$ShowVerbose
$ShowVerbose
Definition: ezscript.php:1206
eZScript\setUseSiteAccess
setUseSiteAccess( $siteAccess)
Definition: ezscript.php:526
eZScript\updateTextCodecSettings
updateTextCodecSettings()
Definition: ezscript.php:1184
eZScript\setExitCode
setExitCode( $code=false)
Definition: ezscript.php:591
eZScript\$SiteAccess
$SiteAccess
Definition: ezscript.php:1203
eZScript\setUseModules
setUseModules( $useModules)
Definition: ezscript.php:539
eZScript\$UseDebugAccumulators
$UseDebugAccumulators
Definition: ezscript.php:64
eZScript\setIsQuiet
setIsQuiet( $isQuiet)
Definition: ezscript.php:606
eZScript\$DebugMessage
$DebugMessage
Definition: ezscript.php:1197
eZScript\shutdown
shutdown( $exitCode=false, $exitText=false)
Definition: ezscript.php:395
eZScript\currentOptionConfig
currentOptionConfig()
Definition: ezscript.php:582
eZScript\$IterationWrapNumeric
$IterationWrapNumeric
Definition: ezscript.php:58
eZScript\$AllowedDebugLevels
$AllowedDebugLevels
Definition: ezscript.php:63
eZScript\$MinVersion
$MinVersion
Definition: ezscript.php:68
eZScript\$IterationColumnMax
$IterationColumnMax
Definition: ezscript.php:61
eZScript\resetIteration
resetIteration( $iterationMax=false, $startIndex=0)
Definition: ezscript.php:640
eZScript\$CurrentStandardOptions
$CurrentStandardOptions
Definition: ezscript.php:52
eZScript\$IterationFalseString
$IterationFalseString
Definition: ezscript.php:56
eZScript\updateSettings
updateSettings(array $settings=array())
Definition: ezscript.php:135
eZScript\updateDebugSettings
updateDebugSettings( $useDebug=null)
Definition: ezscript.php:1145
eZScript\setAllowedDebugLevels
setAllowedDebugLevels( $allowedDebugLevels)
Definition: ezscript.php:497
eZScript\$CurrentOptions
$CurrentOptions
Definition: ezscript.php:50
eZScript\__construct
__construct( $settings=array())
Definition: ezscript.php:74
eZScript\$CurrentOptionHelp
$CurrentOptionHelp
Definition: ezscript.php:54
eZScript\$ExitCode
$ExitCode
Definition: ezscript.php:1204
eZScript\$Description
$Description
Definition: ezscript.php:67
eZScript\instance
static instance( $settings=array())
Definition: ezscript.php:1124
eZScript\setIterationData
setIterationData( $trueString, $falseString, $numericStrings=false, $wrapNumeric=false)
Definition: ezscript.php:631
eZScript\$UseDebugTimingPoints
$UseDebugTimingPoints
Definition: ezscript.php:65
eZScript\currentOptions
currentOptions()
Definition: ezscript.php:574
eZScript\$UseSession
$UseSession
Definition: ezscript.php:1199
eZScript\$UseIncludeFiles
$UseIncludeFiles
Definition: ezscript.php:66
eZScript\iterate
iterate( $cli, $status, $text=false)
Definition: ezscript.php:647
eZScript\usedSiteAccess
usedSiteAccess()
Definition: ezscript.php:534
eZScript\$InitializationErrorMessage
$InitializationErrorMessage
Definition: ezscript.php:1196
eZScript\setIsInitialized
setIsInitialized( $isInitialized)
Definition: ezscript.php:375
eZScript\initializationError
initializationError()
Definition: ezscript.php:380
eZScript\$IsInitialized
$IsInitialized
Definition: ezscript.php:1195
eZScript\setUseSession
setUseSession( $useSession)
Definition: ezscript.php:507
eZSiteAccess\TYPE_STATIC
const TYPE_STATIC
Definition: ezsiteaccess.php:28
eZSiteAccess\change
static change(array $access, ?eZINI $siteINI=null)
Definition: ezsiteaccess.php:503
eZSiteAccess\TYPE_DEFAULT
const TYPE_DEFAULT
Definition: ezsiteaccess.php:23
eZSys\cacheDirectory
static cacheDirectory()
Definition: ezsys.php:527
eZTextCodec\updateSettings
static updateSettings( $settings)
Definition: eztextcodec.php:526
eZUser\logoutCurrent
static logoutCurrent()
Definition: ezuser.php:1106
eZUser\loginUser
static loginUser( $login, $password, $authenticationMatch=false)
Definition: ezuser.php:758
eZDBCleanup
eZDBCleanup()
Definition: ezscript.php:1209
eZUpdateTextCodecSettings
if(!function_exists( 'eZUpdateDebugSettings')) eZUpdateTextCodecSettings()
Definition: ezscript.php:1254
eZFatalError
eZFatalError()
Definition: ezscript.php:1219
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.