Esempio n. 1
0
 protected static function recurseValueBeforeFiltering($value, $inputFilterOrFilterCollection, &$success, $currDepth)
 {
     assert('$inputFilterOrFilterCollection instanceof CInputFilter || ' . 'is_collection($inputFilterOrFilterCollection)', vs(isset($this), get_defined_vars()));
     if ($currDepth == self::$ms_maxRecursionDepth) {
         $success = false;
         return;
     }
     $currDepth++;
     if (!is_cmap($value)) {
         // Only interested in PHP arrays.
         return $value;
     }
     if (is_carray($inputFilterOrFilterCollection)) {
         // The output value is expected to be a CArray; the keys in the arrived PHP array should be sequential.
         if (!CMap::areKeysSequential($value)) {
             $success = false;
             return;
         }
         $value = CArray::fromPArray($value);
         $len = CArray::length($value);
         if ($len != CArray::length($inputFilterOrFilterCollection)) {
             $success = false;
             return;
         }
         for ($i = 0; $i < $len; $i++) {
             $inputValue = $value[$i];
             $inputFilterElement = $inputFilterOrFilterCollection[$i];
             $inputValue = self::recurseValueBeforeFiltering($inputValue, $inputFilterElement, $success, $currDepth);
             if (!$success) {
                 return;
             }
             $value[$i] = $inputValue;
         }
     } else {
         if (is_cmap($inputFilterOrFilterCollection)) {
             // The output value is expected to be a CMap; already got one.
             foreach ($value as $inputKey => &$inputValue) {
                 if (!CMap::hasKey($inputFilterOrFilterCollection, $inputKey)) {
                     $success = false;
                     return;
                 }
                 $inputFilterElement = $inputFilterOrFilterCollection[$inputKey];
                 $inputValue = self::recurseValueBeforeFiltering($inputValue, $inputFilterElement, $success, $currDepth);
                 if (!$success) {
                     return;
                 }
             }
             unset($inputValue);
         } else {
             $success = false;
             return;
         }
     }
     return $value;
 }
Esempio n. 2
0
 /**
  * Determines the order in which two values should appear in a place where it matters, assuming the ascending
  * order.
  *
  * If the values are objects of your custom class, the class should conform to the
  * [IEqualityAndOrder](IEqualityAndOrder.html) interface.
  *
  * @param  mixed $value0 The first value for comparison.
  * @param  mixed $value1 The second value for comparison.
  *
  * @return int A negative value (typically `-1`) if the first value should go before the second value, a positive
  * value (typically `1`) if the other way around, and `0` if the two values are equal.
  *
  * @link   IEqualityAndOrder.html IEqualityAndOrder
  */
 public static function orderAsc($value0, $value1)
 {
     if (CDebug::isDebugModeOn()) {
         if (!(is_cstring($value0) && is_cstring($value1) || is_carray($value0) && is_carray($value1) || is_cmap($value0) && is_cmap($value1))) {
             // With the above exceptions, the two values should be both either scalars or objects of the same
             // class.
             assert('is_object($value0) == is_object($value1)', vs(isset($this), get_defined_vars()));
             assert('!is_object($value0) || CString::equals(get_class($value0), get_class($value1))', vs(isset($this), get_defined_vars()));
         }
     }
     $className;
     if (!phred_classify_duo($value0, $value1, $className)) {
         // Compare the values as scalars.
         assert('(is_scalar($value0) || is_null($value0)) && (is_scalar($value1) || is_null($value1))', vs(isset($this), get_defined_vars()));
         return $value0 === $value1 ? 0 : ($value0 < $value1 ? -1 : 1);
     } else {
         // Compare the values as objects that may conform to one of the comparison interfaces.
         $reflClass = new ReflectionClass($className);
         if ($reflClass->implementsInterface("IEqualityAndOrderStatic")) {
             $res = call_user_func([$className, "compare"], $value0, $value1);
             assert('is_int($res)', vs(isset($this), get_defined_vars()));
             return $res;
         }
         if ($reflClass->implementsInterface("IEqualityAndOrder")) {
             $res = call_user_func([$value0, "compare"], $value1);
             assert('is_int($res)', vs(isset($this), get_defined_vars()));
             return $res;
         }
         // The class of the objects being compared does not implement any applicable comparison interfaces.
         assert('false', vs(isset($this), get_defined_vars()));
     }
 }
Esempio n. 3
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);
     }
 }
Esempio n. 4
0
 /**
  * Sets the byte range(s) of interest in the requested entity.
  *
  * @param  array $byteRangeOrRanges The byte range(s) of interest. This is either an array with two integer values
  * specifying the byte range to be retrieved or, for HTTP requests only, an array of such arrays specifying
  * multiple byte ranges. The second value in a pair is allowed to be `null`, in which case the range is considered
  * to be open-ended and the server is expected to respond with data starting from the byte indicated by the first
  * value in the pair and up to the very last byte available.
  *
  * @return void
  */
 public function setRequestedByteRange($byteRangeOrRanges)
 {
     assert('is_carray($byteRangeOrRanges) && !CArray::isEmpty($byteRangeOrRanges)', vs(isset($this), get_defined_vars()));
     if (!is_carray(CArray::first($byteRangeOrRanges))) {
         assert('CArray::length($byteRangeOrRanges) == 2', vs(isset($this), get_defined_vars()));
         $rangeBPosLow = $byteRangeOrRanges[0];
         $rangeBPosHigh = $byteRangeOrRanges[1];
         assert('is_int($rangeBPosLow) && (is_null($rangeBPosHigh) || is_int($rangeBPosHigh))', vs(isset($this), get_defined_vars()));
         assert('$rangeBPosLow >= 0 && (is_null($rangeBPosHigh) || $rangeBPosHigh >= 0)', vs(isset($this), get_defined_vars()));
         assert('is_null($rangeBPosHigh) || $rangeBPosHigh >= $rangeBPosLow', vs(isset($this), get_defined_vars()));
         $this->m_requestedByteRange = !is_null($rangeBPosHigh) ? "{$rangeBPosLow}-{$rangeBPosHigh}" : "{$rangeBPosLow}-";
     } else {
         assert('$this->isHttp()', vs(isset($this), get_defined_vars()));
         $preOutString = CArray::make();
         $len = CArray::length($byteRangeOrRanges);
         $lenMinusOne = $len - 1;
         for ($i = 0; $i < $len; $i++) {
             $range = $byteRangeOrRanges[$i];
             assert('is_carray($range)', vs(isset($this), get_defined_vars()));
             assert('CArray::length($range) == 2', vs(isset($this), get_defined_vars()));
             $rangeBPosLow = $range[0];
             $rangeBPosHigh = $range[1];
             if (CDebug::isDebugModeOn()) {
                 // The high byte position can be specified as `null` for the last range only.
                 if ($i != $lenMinusOne) {
                     assert('is_int($rangeBPosLow) && is_int($rangeBPosHigh)', vs(isset($this), get_defined_vars()));
                     assert('$rangeBPosLow >= 0 && $rangeBPosHigh >= 0', vs(isset($this), get_defined_vars()));
                     assert('$rangeBPosHigh >= $rangeBPosLow', vs(isset($this), get_defined_vars()));
                 } else {
                     assert('is_int($rangeBPosLow) && (is_null($rangeBPosHigh) || is_int($rangeBPosHigh))', vs(isset($this), get_defined_vars()));
                     assert('$rangeBPosLow >= 0 && (is_null($rangeBPosHigh) || $rangeBPosHigh >= 0)', vs(isset($this), get_defined_vars()));
                     assert('is_null($rangeBPosHigh) || $rangeBPosHigh >= $rangeBPosLow', vs(isset($this), get_defined_vars()));
                 }
             }
             $rbr = !is_null($rangeBPosHigh) ? "{$rangeBPosLow}-{$rangeBPosHigh}" : "{$rangeBPosLow}-";
             CArray::push($preOutString, $rbr);
         }
         $this->m_requestedByteRange = CArray::join($preOutString, ",");
     }
 }
Esempio n. 5
0
/**
 * @ignore
 */
function _from_oop_tp($value)
{
    // Only used with OOP wrapping for third-party components.
    if (is_carray($value)) {
        $value = splarray($value);
        $len = CArray::length($value);
        for ($i = 0; $i < $len; $i++) {
            $value[$i] = _from_oop_tp($value[$i]);
        }
        return $value->toArray();
    }
    if (is_cmap($value)) {
        $value = parray($value);
        foreach ($value as &$mapValue) {
            $mapValue = _from_oop_tp($mapValue);
        }
        unset($mapValue);
        return $value;
    }
    return $value;
}
Esempio n. 6
0
 protected static function recurseQueryValueBeforeComposingQs($value, $currDepth)
 {
     if ($currDepth == self::$ms_maxRecursionDepth) {
         return $value;
     }
     $currDepth++;
     if (!is_collection($value)) {
         if (!is_cstring($value)) {
             if (is_bool($value)) {
                 $value = CString::fromBool10($value);
             } else {
                 if (is_int($value)) {
                     $value = CString::fromInt($value);
                 } else {
                     if (is_float($value)) {
                         $value = CString::fromFloat($value);
                     } else {
                         assert('false', vs(isset($this), get_defined_vars()));
                     }
                 }
             }
         }
         return $value;
     }
     if (is_carray($value)) {
         $value = splarray($value)->toArray();
     } else {
         $value = parray($value);
     }
     foreach ($value as &$mapValue) {
         $mapValue = self::recurseQueryValueBeforeComposingQs($mapValue, $currDepth);
     }
     unset($mapValue);
     return $value;
 }
Esempio n. 7
0
 protected static function recurseCollectionFiltering($inputCollection, $filterOrFilterCollection, &$success, $currDepth)
 {
     assert('is_a($filterOrFilterCollection, get_called_class()) || is_collection($filterOrFilterCollection)', vs(isset($this), get_defined_vars()));
     if ($currDepth == self::$ms_maxRecursionDepth) {
         $success = false;
         return;
     }
     $currDepth++;
     if (is_carray($inputCollection)) {
         if (!is_carray($filterOrFilterCollection)) {
             $success = false;
             return;
         }
         $len = CArray::length($inputCollection);
         if ($len != CArray::length($filterOrFilterCollection)) {
             $success = false;
             return;
         }
         for ($i = 0; $i < $len; $i++) {
             $inputValue = $inputCollection[$i];
             $filterElement = $filterOrFilterCollection[$i];
             if (!is_collection($inputValue)) {
                 $strInputValue = self::collectionElementToString($inputValue, $success);
                 if (!$success) {
                     return;
                 }
                 if (!is_a($filterElement, get_called_class())) {
                     $success = false;
                     return;
                 }
                 $inputValue = $filterElement->filter($strInputValue, $success);
                 if (!$success) {
                     return;
                 }
             } else {
                 $inputValue = self::recurseCollectionFiltering($inputValue, $filterElement, $success, $currDepth);
                 if (!$success) {
                     return;
                 }
             }
             $inputCollection[$i] = $inputValue;
         }
     } else {
         if (!is_cmap($filterOrFilterCollection)) {
             $success = false;
             return;
         }
         foreach ($inputCollection as $inputKey => &$inputValue) {
             if (!CMap::hasKey($filterOrFilterCollection, $inputKey)) {
                 $success = false;
                 return;
             }
             $filterElement = $filterOrFilterCollection[$inputKey];
             if (!is_collection($inputValue)) {
                 $strInputValue = self::collectionElementToString($inputValue, $success);
                 if (!$success) {
                     return;
                 }
                 if (!is_a($filterElement, get_called_class())) {
                     $success = false;
                     return;
                 }
                 $inputValue = $filterElement->filter($strInputValue, $success);
                 if (!$success) {
                     return;
                 }
             } else {
                 $inputValue = self::recurseCollectionFiltering($inputValue, $filterElement, $success, $currDepth);
                 if (!$success) {
                     return;
                 }
             }
         }
         unset($inputValue);
     }
     return $inputCollection;
 }
Esempio n. 8
0
 protected static function recurseValueBeforeEncoding($value, $currDepth)
 {
     if ($currDepth == self::$ms_maxRecursionDepth) {
         return $value;
     }
     $currDepth++;
     if (is_cstring($value)) {
         return $value;
     }
     if (is_cmap($value)) {
         $value = parray($value);
         foreach ($value as &$valueInMap) {
             $valueInMap = self::recurseValueBeforeEncoding($valueInMap, $currDepth);
         }
         unset($valueInMap);
         $value = (object) $value;
     } else {
         if (is_carray($value)) {
             $value = splarray($value);
             $len = CArray::length($value);
             for ($i = 0; $i < $len; $i++) {
                 $value[$i] = self::recurseValueBeforeEncoding($value[$i], $currDepth);
             }
             $value = CArray::toPArray($value);
         }
     }
     return $value;
 }