Ejemplo n.º 1
0
 /**
  * @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;
     }
 }
Ejemplo n.º 2
0
 /**
  * Enables the logging of messages issued with `say` and `speak` methods and error messages into a file located at
  * a specified path.
  *
  * @param  string $logFp The path to the log file.
  *
  * @return void
  */
 public static function setLogging($logFp)
 {
     assert('is_cstring($logFp)', vs(isset($this), get_defined_vars()));
     $logFp = CFilePath::frameworkPath($logFp);
     self::$ms_logFp = $logFp;
 }
Ejemplo n.º 3
0
 /**
  * Generates a CID for a file by which the file can be embedded into the body of a message.
  *
  * The same CID can be used to embed a file in more than one place.
  *
  * For example, an image for which a CID was generated and put into `$cid` variable can be embedded into a message
  * with HTML type of body by `<img src="' . $cid . '" alt="Title" />`.
  *
  * @param  string $embedFp The path to the file to be embedded.
  *
  * @return CUStringObject The embeddable CID of the file.
  */
 public function embeddableCidForFile($embedFp)
 {
     assert('is_cstring($embedFp)', vs(isset($this), get_defined_vars()));
     assert('isset($this->m_swiftMessage)', vs(isset($this), get_defined_vars()));
     $embedFp = CFilePath::frameworkPath($embedFp);
     $embeddedFile = Swift_EmbeddedFile::fromPath($embedFp);
     return $this->m_swiftMessage->embed($embeddedFile);
 }
Ejemplo n.º 4
0
 /**
  * Sets the SSL options for a request.
  *
  * @param  string $certificateFp The path to the certificate's file.
  * @param  string $privateKeyFp **OPTIONAL. Default is** *none*. The path to the private key's file.
  * @param  string $certificateFormat **OPTIONAL. Default is** "PEM". The format used by the certificate.
  * @param  string $privateKeyFormat **OPTIONAL. Default is** "PEM". The format used by the private key.
  * @param  string $certificatePassphrase **OPTIONAL. Default is** *none*. The passphrase used by the certificate.
  * @param  string $privateKeyPassphrase **OPTIONAL. Default is** *none*. The passphrase used by the private key.
  * @param  int $sslVersion **OPTIONAL. Default is** *cURL's default*. The SSL version to be used in the
  * communication.
  * @param  string $cipherList **OPTIONAL. Default is** *cURL's default*. The ciphers to be used.
  * @param  string $engine **OPTIONAL. Default is** *cURL's default*. The SSL engine identifier.
  * @param  string $defaultEngine **OPTIONAL. Default is** *cURL's default*. The identifier of the actual crypto
  * engine to be used as the default engine for (asymmetric) crypto operations.
  *
  * @return void
  */
 public function setSslOptions($certificateFp, $privateKeyFp = null, $certificateFormat = "PEM", $privateKeyFormat = "PEM", $certificatePassphrase = null, $privateKeyPassphrase = null, $sslVersion = null, $cipherList = null, $engine = null, $defaultEngine = null)
 {
     assert('is_cstring($certificateFp) && (!isset($privateKeyFp) || is_cstring($privateKeyFp)) && ' . 'is_cstring($certificateFormat) && is_cstring($privateKeyFormat) && ' . '(!isset($certificatePassphrase) || is_cstring($certificatePassphrase)) && ' . '(!isset($privateKeyPassphrase) || is_cstring($privateKeyPassphrase)) && ' . '(!isset($sslVersion) || is_int($sslVersion)) && ' . '(!isset($cipherList) || is_cstring($cipherList)) && (!isset($engine) || is_cstring($engine)) && ' . '(!isset($defaultEngine) || is_cstring($defaultEngine))', vs(isset($this), get_defined_vars()));
     assert('CFilePath::isAbsolute($certificateFp)', vs(isset($this), get_defined_vars()));
     assert('!isset($privateKeyFp) || CFilePath::isAbsolute($privateKeyFp)', vs(isset($this), get_defined_vars()));
     $this->m_sslCertificateFp = CFilePath::frameworkPath($certificateFp);
     $this->m_sslPrivateKeyFp = CFilePath::frameworkPath($privateKeyFp);
     $this->m_sslCertificateFormat = $certificateFormat;
     $this->m_sslPrivateKeyFormat = $privateKeyFormat;
     $this->m_sslCertificatePassphrase = $certificatePassphrase;
     $this->m_sslPrivateKeyPassphrase = $privateKeyPassphrase;
     $this->m_sslVersion = $sslVersion;
     $this->m_sslCipherList = $cipherList;
     $this->m_sslEngine = $engine;
     $this->m_sslDefaultEngine = $defaultEngine;
 }
Ejemplo n.º 5
0
 /**
  * Computes more of the hash from a file.
  *
  * @param  string $dataFp The path to the source file.
  *
  * @return void
  */
 public function computeMoreFromFile($dataFp)
 {
     assert('is_cstring($dataFp)', vs(isset($this), get_defined_vars()));
     $dataFp = CFilePath::frameworkPath($dataFp);
     hash_update_file($this->m_hashingContext, $dataFp);
 }
Ejemplo n.º 6
0
 /**
  * Creates a file accessor for performing sequential read/write operations on a file.
  *
  * The purpose of this constructor is to open the file in the specified access mode. If the access mode is `READ`
  * or `READ_WRITE`, the file should already exist when calling the constructor. With the rest of the modes, the
  * file will be created if doesn't exist.
  *
  * The opened file will be closed and the file system's resources will be freed automatically when the object is no
  * longer needed and gets destructed by the runtime, or when `done` method is called on the object to close the
  * file manually.
  *
  * @param  string $filePath The path to the file to be opened for access.
  * @param  enum $mode The access mode to be used for the file. Can be `READ`, `READ_WRITE`, `WRITE_NEW`,
  * `READ_WRITE_NEW`, `WRITE_APPEND`, and `READ_WRITE_APPEND`. See [Summary](#summary) for the description of a
  * mode.
  * @param  enum $wrapper **OPTIONAL. Default is** `NONE`. The wrapper to be used for accessing the file.
  * Currently, only `ZLIB` wrapper is supported.
  */
 public function __construct($filePath, $mode, $wrapper = self::NONE)
 {
     assert('is_cstring($filePath) && is_enum($mode) && is_enum($wrapper)', vs(isset($this), get_defined_vars()));
     $filePath = CFilePath::frameworkPath($filePath);
     $this->m_filePath = $filePath;
     $this->m_mode = $mode;
     $strMode;
     switch ($mode) {
         case self::READ:
             $strMode = "rb";
             break;
         case self::READ_WRITE:
             $strMode = "r+b";
             break;
         case self::WRITE_NEW:
             $strMode = "wb";
             break;
         case self::READ_WRITE_NEW:
             $strMode = "w+b";
             break;
         case self::WRITE_APPEND:
             $strMode = "ab";
             break;
         case self::READ_WRITE_APPEND:
             $strMode = "a+b";
             break;
         default:
             assert('false', vs(isset($this), get_defined_vars()));
             break;
     }
     $strWrapper;
     switch ($wrapper) {
         case self::NONE:
             $strWrapper = "";
             break;
         case self::ZLIB:
             $strWrapper = "compress.zlib://";
             break;
         default:
             assert('false', vs(isset($this), get_defined_vars()));
             break;
     }
     $filePath = $strWrapper . $filePath;
     $this->m_file = fopen($filePath, $strMode);
     assert('is_resource($this->m_file)', vs(isset($this), get_defined_vars()));
 }
Ejemplo n.º 7
0
<?php

require __DIR__ . "/Bootstrap/Phred.php";
echo passthru(CFilePath::frameworkPath("cd {{PHRED_PATH_TO_FRAMEWORK_ROOT}}; php ThirdParty/bin/phpunit --colors"));
Ejemplo n.º 8
0
 public function testFrameworkPath()
 {
     $this->assertTrue(CFilePath::absolute(CFilePath::frameworkPath("{{PHRED_PATH_TO_APP}}/Tests"))->equals(CFilePath::absolute(CFilePath::add($GLOBALS["PHRED_PATH_TO_APP"], "/Tests"))));
     $this->assertTrue(CFilePath::absolute(CFilePath::frameworkPath("{{PHRED_PATH_TO_FRAMEWORK_ROOT}}/Application/Tests"))->equals(CFilePath::absolute(CFilePath::add($GLOBALS["PHRED_PATH_TO_FRAMEWORK_ROOT"], "/Application/Tests"))));
     $this->assertTrue(CFilePath::absolute(CFilePath::frameworkPath("{{PHRED_PATH_TO_APP}}Tests"))->equals(CFilePath::absolute(CFilePath::add($GLOBALS["PHRED_PATH_TO_APP"], "/Tests"))));
 }