Ejemplo n.º 1
0
 /**
  * @ignore
  */
 public static function initializeFramework()
 {
     $currEnv;
     if (!$GLOBALS["PHRED_TESTS"]) {
         // Try to get the name of the environment in which the application is currently running without the risk of
         // encountering an error or triggering an assertion, which could otherwise reveal sensitive debugging
         // information if "display_errors" happens to be enabled in php.ini.
         $appConfigFp = $GLOBALS["PHRED_PATH_TO_APP"] . "/Configuration" . "/Application.json";
         if (file_exists($appConfigFp)) {
             $appConfig = file_get_contents($appConfigFp);
             if (is_string($appConfig)) {
                 $matches;
                 $res = preg_match("/^\\h*\"environment\"\\s*:\\s*\"(\\w+)\"/m", $appConfig, $matches);
                 if ($res === 1) {
                     $currEnv = $matches[1];
                 }
             }
         }
     } else {
         $currEnv = "tst";
     }
     if (isset($currEnv)) {
         // Based on the current environment, set some debugging options to temporary values for the time while the
         // configuration is not yet read.
         if (strcasecmp($currEnv, "dev") == 0) {
             // Development.
             ini_set("display_errors", "On");
             CDebug::setAssertionsLevel1(true);
             CDebug::setAssertionsLevel2(false);
         } else {
             if (strcasecmp($currEnv, "pro") == 0) {
                 // Production.
                 ini_set("display_errors", "Off");
                 CDebug::setAssertionsLevel1(false);
                 CDebug::setAssertionsLevel2(false);
             } else {
                 if (strcasecmp($currEnv, "tst") == 0) {
                     // Testing.
                     ini_set("display_errors", "On");
                     CDebug::setAssertionsLevel1(true);
                     CDebug::setAssertionsLevel2(true);
                 } else {
                     // Unknown environment.
                     error_reporting(E_ALL);
                     ini_set("display_errors", "On");
                 }
             }
         }
     } else {
         error_reporting(E_ALL);
         ini_set("display_errors", "On");
         trigger_error("Could not read the name of the current environment.", E_USER_ERROR);
     }
     // Read all the configuration options.
     CConfiguration::initialize();
     // Set whether error messages should be shown is the output.
     if (CConfiguration::option("debug.displayErrors")) {
         ini_set("display_errors", "On");
     } else {
         ini_set("display_errors", "Off");
     }
     // Set the error reporting level.
     $errorReportingLevel = constant(CConfiguration::option("debug.errorReportingLevel"));
     error_reporting($errorReportingLevel);
     // Process the configuration options that are related to debugging.
     $debug = CConfiguration::option("debug");
     // Assertions and the conditions on which they get active.
     $assertionsAreActive = false;
     $assertionsAreEnabled = $debug["enableAssertions"];
     if ($assertionsAreEnabled) {
         $assertionsAreActiveBasedOn = $debug["assertionsAreActiveBasedOn"];
         if (CString::equalsCi($assertionsAreActiveBasedOn, "always")) {
             // Always.
             $assertionsAreActive = true;
         } else {
             // Reference time zone.
             $refTimeZone;
             $timeZoneName = $debug["referenceTimeZone"];
             if (!CString::isEmpty($timeZoneName)) {
                 $refTimeZone = new CTimeZone($timeZoneName);
             } else {
                 $refTimeZone = CTimeZone::makeUtc();
             }
             if (CString::equalsCi($assertionsAreActiveBasedOn, "hour")) {
                 // Current time.
                 $currTime = CTime::now();
                 // Hour.
                 $hourRanges = $debug["assertionsAreActiveWithinHourRange"];
                 $currHour = $currTime->hourInTimeZone($refTimeZone);
                 if (!is_carray($hourRanges[0])) {
                     $hourRanges = CArray::fromElements($hourRanges);
                 }
                 $len = CArray::length($hourRanges);
                 for ($i = 0; $i < $len; $i++) {
                     $range = $hourRanges[$i];
                     assert('is_int($range[0]) && is_int($range[1])', vs(isset($this), get_defined_vars()));
                     if ($range[0] <= $currHour && $currHour <= $range[1]) {
                         $assertionsAreActive = true;
                         break;
                     }
                 }
             } else {
                 if (CString::equalsCi($assertionsAreActiveBasedOn, "dayOfWeek")) {
                     // Current time.
                     $currTime = CTime::now();
                     // Day of week.
                     $daysOfWeek = $debug["assertionsAreActiveOnDaysOfWeek"];
                     if (!is_carray($daysOfWeek)) {
                         $daysOfWeek = CArray::fromElements($daysOfWeek);
                     }
                     $currDayOfWeek = $currTime->dayOfWeekInTimeZone($refTimeZone);
                     $currDayOfWeekShort;
                     switch ($currDayOfWeek) {
                         case CTime::SUNDAY:
                             $currDayOfWeekShort = "sun";
                             break;
                         case CTime::MONDAY:
                             $currDayOfWeekShort = "mon";
                             break;
                         case CTime::TUESDAY:
                             $currDayOfWeekShort = "tue";
                             break;
                         case CTime::WEDNESDAY:
                             $currDayOfWeekShort = "wed";
                             break;
                         case CTime::THURSDAY:
                             $currDayOfWeekShort = "thu";
                             break;
                         case CTime::FRIDAY:
                             $currDayOfWeekShort = "fri";
                             break;
                         case CTime::SATURDAY:
                             $currDayOfWeekShort = "sat";
                             break;
                     }
                     $len = CArray::length($daysOfWeek);
                     for ($i = 0; $i < $len; $i++) {
                         $dow = $daysOfWeek[$i];
                         assert('!CString::isEmpty($dow)', vs(isset($this), get_defined_vars()));
                         if (CString::equalsCi($currDayOfWeekShort, $dow)) {
                             $assertionsAreActive = true;
                             break;
                         }
                     }
                 } else {
                     if (CString::equalsCi($assertionsAreActiveBasedOn, "dayOfYear")) {
                         // Day of year.
                         $multiplier = $debug["assertionsAreActiveOnEveryDayOfYearMultipleOf"];
                         assert('is_int($multiplier)', vs(isset($this), get_defined_vars()));
                         $currDayOfYear = CTime::currentDayOfYearInTimeZone($refTimeZone);
                         if ($currDayOfYear % $multiplier == 0) {
                             $assertionsAreActive = true;
                         }
                     } else {
                         assert('false', vs(isset($this), get_defined_vars()));
                     }
                 }
             }
         }
     }
     if ($assertionsAreActive) {
         // Enable level 1 assertions.
         CDebug::setAssertionsLevel1(true);
         $enableAssertionsLevel2 = $debug["enableAssertionsLevel2"];
         if ($enableAssertionsLevel2) {
             // Enable level 2 assertions.
             CDebug::setAssertionsLevel2(true);
         }
     } else {
         CDebug::setAssertionsLevel1(false);
         CDebug::setAssertionsLevel2(false);
     }
     // Logging.
     $logging = $debug["logging"];
     $loggingIsEnabled = $logging["enable"];
     if ($loggingIsEnabled) {
         $logFp = $logging["logFilePath"];
         assert('!CString::isEmpty($logFp)', vs(isset($this), get_defined_vars()));
         $logFp = CFilePath::frameworkPath($logFp);
         CDebug::setLogging($logFp);
     }
     // Mailing.
     $mailing = $debug["mailing"];
     $mailingIsEnabled = $mailing["enable"];
     if ($mailingIsEnabled) {
         $adminMail = CConfiguration::option("admin.mail");
         $to = $adminMail["to"];
         $from = $adminMail["from"];
         $transport = $adminMail["transport"];
         assert('!CString::isEmpty($to) && !CString::isEmpty($from) && !CString::isEmpty($transport)', vs(isset($this), get_defined_vars()));
         $minTimeBetweenSendMailHours = $mailing["minTimeBetweenSendMailHours"];
         assert('is_int($minTimeBetweenSendMailHours)', vs(isset($this), get_defined_vars()));
         $mail;
         if (CString::equalsCi($transport, "smtp")) {
             $smtpOutgoingServer = $adminMail["smtpOutgoingServer"];
             $smtpUsername = $adminMail["smtpUsername"];
             $smtpPassword = $adminMail["smtpPassword"];
             assert('!CString::isEmpty($smtpOutgoingServer) && !CString::isEmpty($smtpUsername) && ' . '!CString::isEmpty($smtpPassword)', vs(isset($this), get_defined_vars()));
             $mail = CMail::makeSmtp($smtpOutgoingServer, $smtpUsername, $smtpPassword, $from, $to);
         } else {
             if (CString::equalsCi($transport, "system")) {
                 $mail = CMail::makeSystem($from, $to);
             } else {
                 assert('false', vs(isset($this), get_defined_vars()));
             }
         }
         CDebug::setMailing($mail, $minTimeBetweenSendMailHours);
     }
 }