/** * Creates an HTTP cookie to be sent along with an HTTP response. * * Internally, the value of a cookie is always stored as a string. If the cookie's value was provided as an array * or map, it's encoded into JSON and is stored as a JSON string. Boolean values are stored as "1" for `true` and * as "0" for `false`. * * @param string $cookieName The cookie's name. * @param mixed $value The cookie's value. This can be a string, a `bool`, an `int`, a `float`, an array, or a * map. */ public function __construct($cookieName, $value) { assert('is_cstring($cookieName) && (is_cstring($value) || is_bool($value) || is_int($value) || ' . 'is_float($value) || is_collection($value))', vs(isset($this), get_defined_vars())); $this->m_name = $cookieName; if (is_cstring($value)) { $this->m_value = $value; } else { if (is_bool($value)) { $this->m_value = CString::fromBool10($value); } else { if (is_int($value)) { $this->m_value = CString::fromInt($value); } else { if (is_float($value)) { $this->m_value = CString::fromFloat($value); } else { $json = new CJson($value); $this->m_value = $json->encode(); } } } } }
/** * Joins the elements of an array into a string. * * The elements in the source array are not required to be all strings and, in addition to types that know how to * become a string, an element can be `int`, `float`, or `bool`. In the resulting string, a boolean value of * `true` becomes "1" and `false` becomes "0". * * As a special case, the array is allowed to be empty. * * @param array $array The array containing the elements to be joined. * @param string $binder The string to be put between any two elements in the resulting string, such as ", ". * Can be empty. * * @return string The resulting string. */ public static function join($array, $binder) { assert('is_carray($array) && is_cstring($binder)', vs(isset($this), get_defined_vars())); $array = splarray($array); $array = self::makeCopy($array); for ($i = 0; $i < $array->getSize(); $i++) { if (!is_cstring($array[$i])) { if (is_bool($array[$i])) { $array[$i] = CString::fromBool10($array[$i]); } else { if (is_int($array[$i])) { $array[$i] = CString::fromInt($array[$i]); } else { if (is_float($array[$i])) { $array[$i] = CString::fromFloat($array[$i]); } } } } } return implode($binder, self::toPArray($array)); }
protected static function shiftTimeInTimeZone(CTime $time, $timeUnit, $quantity, $timeZone) { $units; switch ($timeUnit) { case self::SECOND: $units = "seconds"; break; case self::MINUTE: $units = "minutes"; break; case self::HOUR: $units = "hours"; break; case self::DAY: $units = "days"; break; case self::WEEK: $units = "weeks"; break; case self::MONTH: $units = "months"; break; case self::YEAR: $units = "years"; break; default: assert('false', vs(isset($this), get_defined_vars())); break; } $dt = new DateTime(); $dt->setTimestamp($time->UTime()); $dt->setTimezone(is_cstring($timeZone) ? new DateTimeZone($timeZone) : $timeZone->DTimeZone()); $sign = $quantity < 0 ? "-" : "+"; $absQty = CString::fromInt(CMathi::abs($quantity)); $dt->modify("{$sign}{$absQty} {$units}"); $UTime = $dt->getTimestamp(); $MTime = $time->MTime(); if ($UTime != 0 && $MTime != 0 && CMathi::sign($UTime) != CMathi::sign($MTime)) { if ($UTime < 0) { // $MTime > 0 $UTime++; $MTime -= 1000; } else { // $MTime < 0 $UTime--; $MTime += 1000; } } return new self($UTime, $MTime); }
/** * @ignore */ public static function maybeUpdateThirdParty() { if (!self::isInCliMode()) { // This method can be run in CLI mode only. assert('false', vs(isset($this), get_defined_vars())); return false; } $updates = CConfiguration::option("updates"); $updatesAreEnabled = $updates["enable"]; if ($updatesAreEnabled) { $minTimeBetweenDoUpdatesDays = $updates["minTimeBetweenDoUpdatesDays"]; $components = $updates["components"]; assert('is_int($minTimeBetweenDoUpdatesDays)', vs(isset($this), get_defined_vars())); // Logging. $logging = $updates["logging"]; $loggingIsEnabled = $logging["enable"]; $logFp = $logging["logFilePath"]; if ($loggingIsEnabled) { assert('!CString::isEmpty($logFp)', vs(isset($this), get_defined_vars())); $logFp = CFilePath::frameworkPath($logFp); CShell::setLogging($logFp); } // Mailing. $mailing = $updates["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())); $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())); } } CShell::setMailing($mail); } $thirdPartyDp = $GLOBALS["PHRED_PATH_TO_THIRD_PARTY"]; $lastUpdateTimeFp = CFilePath::add($thirdPartyDp, self::$ms_thirdPartyLastUpdateTimeFn); // Read the file containing the Unix seconds of the last update time stamp (if exists) and compare that // time with the current time. $numDaysSinceLastUpdate; if (CFile::exists($lastUpdateTimeFp)) { $lastUpdateTime = new CTime(CString::toInt(CFile::read($lastUpdateTimeFp))); $currTime = CTime::now(); if ($lastUpdateTime->isBefore($currTime)) { $numDaysSinceLastUpdate = $currTime->diffInDays($lastUpdateTime); if ($numDaysSinceLastUpdate < $minTimeBetweenDoUpdatesDays) { // It is too early for updates yet. return false; } } else { assert('false', vs(isset($this), get_defined_vars())); } } $date = CShell::currentDate(); CShell::say("Started on {$date}."); if (isset($numDaysSinceLastUpdate)) { CShell::say("It has been {$numDaysSinceLastUpdate} day(s) since last successful update."); } $concurrLockFp = CFilePath::add($thirdPartyDp, self::$ms_thirdPartyConcurrLockFn); // Try locking the operation. if (!self::setLock($concurrLockFp, false)) { assert('false', vs(isset($this), get_defined_vars())); CShell::onError(false, "Could not obtain a lock on the operation."); CShell::writeToLog("\n"); return false; } $phpConfigNeedsReload = false; $totalNumComponents = CMap::length($components); $numComponentsUpdated = 0; // The Browser Capabilities Project (BrowsCap). if (CMap::hasKey($components, "browsCap")) { $browsCap = $components["browsCap"]; $skip = $browsCap["skip"]; if (!$skip) { CShell::say("Updating the Browser Capabilities Project (BrowsCap) ..."); $lookupFileUrl = $browsCap["lookupFileUrl"]; assert('!CString::isEmpty($lookupFileUrl)', vs(isset($this), get_defined_vars())); // Component-related constants. static $s_configOptName = "browscap"; static $s_lookupFileDownloadTimeoutSeconds = 120; if (self::hasConfigOption($s_configOptName)) { $browsCapLookupFp = CString::trim(self::configOption($s_configOptName)); if (!CString::isEmpty($browsCapLookupFp)) { $browsCapDp = CFilePath::directory($browsCapLookupFp); CShell::say("Downloading a BrowsCap lookup file from '{$lookupFileUrl}' ..."); $temporaryFp = CFile::createTemporary($browsCapDp); $downloadRes = CInetRequest::downloadFile($lookupFileUrl, $temporaryFp, $s_lookupFileDownloadTimeoutSeconds); if ($downloadRes) { // After the file is downloaded into a temporary one, move it to the destination, // safely replacing the existing file, if any. CFile::move($temporaryFp, $browsCapLookupFp); $numComponentsUpdated++; $phpConfigNeedsReload = true; $downloadedFileSizeKB = CUUnit::convertStoragef((double) CFile::size($browsCapLookupFp), CUUnit::BYTE, CUUnit::KILOBYTE); $downloadedFileSizeKB = CMathf::round($downloadedFileSizeKB, 2); CShell::say("Done. The downloaded file is {$downloadedFileSizeKB} KB in size."); } else { CShell::onError(false, "Could not download a BrowsCap lookup file from '{$lookupFileUrl}'."); } // Just in case, check for any temporary files that could have been left by any previous // operations in the directory. $leftoverFiles = CFile::findFiles(CFilePath::add($browsCapDp, CFile::DEFAULT_TEMPORARY_FILE_PREFIX . "*")); if (!CArray::isEmpty($leftoverFiles)) { // Cleanup the directory from the temporary files. $len = CArray::length($leftoverFiles); for ($i = 0; $i < $len; $i++) { CFile::delete($leftoverFiles[$i]); } } } else { CShell::onError(false, "Could not read the value of '{$s_configOptName}' option " . "in the PHP CLI configuration file."); } } else { CShell::onError(false, "Could not find '{$s_configOptName}' option in the PHP CLI configuration file."); } } else { CShell::say("Skipping the Browser Capabilities Project (BrowsCap)."); } } // All the components have been processed. Unlock the operation. self::unsetLock($concurrLockFp); $date = CShell::currentDate(); if ($numComponentsUpdated != 0) { // One or more third-party components have been updated. Put a time stamp on the directory where the // components are located. CFile::write($lastUpdateTimeFp, CString::fromInt(CTime::currentUTime())); if ($numComponentsUpdated == $totalNumComponents) { CShell::speak("Success. All {$totalNumComponents} third-party component(s)"); } else { CShell::speak("Partial success. {$numComponentsUpdated} out of {$totalNumComponents} third-party component(s)"); } CShell::say("have been updated. Completed on {$date}."); } else { CShell::say("No third-party components have been updated. Completed on {$date}."); } return $phpConfigNeedsReload; } else { return false; } }
/** * Returns a character by its code point specified as an integer. * * @param int $code The Unicode code point. * * @return string The Unicode character with the code point specified. */ public static function fromCharCode($code) { assert('is_int($code)', vs(isset($this), get_defined_vars())); assert('0 <= $code && $code < 0xFFFE', vs(isset($this), get_defined_vars())); return json_decode("\"\\u" . CString::padStart(CString::decToHex(CString::fromInt($code)), "0", 4) . "\""); }
/** * Returns the result of the verification of the SSL certificate of the remote server. * * @return bool `true` if the SSL certificate was verified successfully, `false` otherwise. */ public function requestSslVerificationResult() { assert('$this->m_done && !$this->m_hasError', vs(isset($this), get_defined_vars())); $key = "ssl_verify_result"; if (CMap::hasKey($this->m_requestSummary, $key)) { $value = $this->m_requestSummary[$key]; if (!is_bool($value)) { if (!is_cstring($value)) { $value = CString::fromInt($value); } return CString::toBool($value); } else { return $value; } } else { return false; } }
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; }
protected static function collectionElementToString($elementValue, &$success) { if (is_cstring($elementValue)) { return $elementValue; } if (is_bool($elementValue)) { return CString::fromBool10($elementValue); } if (is_int($elementValue)) { return CString::fromInt($elementValue); } if (is_float($elementValue)) { return CString::fromFloat($elementValue); } $success = false; }