/** * Returns a securely generated random *bytes* using the supplied providers. * Providers will be queried in the same order as the were given to constructor * and first which will return random bytes will be used. * * @param int $count number of random bytes to be generated * * @returns bytes generated bytes * @throws RuntimeException if any error occurs (or no provider returns random bytes) * Warning: Never catch and ignore exceptions from this function! */ public function randomBytes($count) { Preconditions::check(is_int($count)); Preconditions::check($count > 0); // TODO(ppershing): verify following claim // Try the OpenSSL method first. This is the strongest. $output = false; foreach ($this->providers as $provider) { $output = $provider->randomBytes($count); if ($output !== false) { break; } } // No method was able to generate random bytes. // Do not ignore this and throw security error as // caller may forgot to check return value. if ($output == false) { throw new RuntimeException("Unable to generate random bytes."); } // internal check on sanity if (strlen($output) != $count) { throw new Exception("Wrong number random bytes were generated. " . "This may be a severe internal error in secure random."); } return $output; }
public function runInformacnyList(Trace $trace, Context $context) { $request = $context->getRequest(); $response = $context->getResponse(); $code = $request->getParameter('code'); Preconditions::check(!empty($code), "Nezadaný kód predmetu!"); $content = $this->registerPredmetovScreen->getInformacnyList($trace, $code); $content = iconv("Windows-1250", "UTF-8", $content); $response->setTemplate('predmety/informacnyList'); $response->set('content', $content); }
/** * Returns a securely generated random bytes from windows cryptoapi. * * Warning: use SecureRandom::random() instead * * @param int $count number of random bytes to be generated * * @returns bytes|false generated bytes or false on error */ public function randomBytes($count) { Preconditions::check(is_int($count)); Preconditions::check($count > 0); if (!self::isAvailable()) { return false; } $util = new COM(self::CRYPTOAPI_COM); $base64 = $util->GetRandom($count, 0); // default encoding is base64 $output = base64_decode($base64, true); // be strict return $output; }
/** * Returns a securely generated random bytes from linux random. * * Warning: use SecureRandom::random() instead * * @param int $count number of random bytes to be generated * * @returns bytes|false generated bytes or false on error */ public function randomBytes($count) { Preconditions::check(is_int($count)); Preconditions::check($count > 0); if (!self::isAvailable()) { return false; } $handle = @fopen(self::RANDOM_FILE, 'rb'); $output = @fread($handle, $count); @fclose($handle); if (strlen($output) !== $count) { return false; // oops, some problem reading required number of bytes } return $output; }
/** * Returns a securely generated random bytes from open SSL. * * Warning: use SecureRandom::random() instead * * @param int $count number of random bytes to be generated * * @returns bytes|false generated bytes or false on error */ public function randomBytes($count) { Preconditions::check(is_int($count)); Preconditions::check($count > 0); if (!self::isAvailable()) { return false; } // TODO(ppershing): check this on windows: from user comments in manual page: // FYI, openssl_random_pseudo_bytes() can be incredibly slow under Windows, to the point of being // unusable. $output = openssl_random_pseudo_bytes($count, $strong); if ($strong === true) { // openssl claim the random is strong return $output; } return false; }
public function getFullPath(array $options, $tableName) { Preconditions::checkIsString($tableName); Preconditions::check($tableName != '', "Table name must be non-empty"); $path = ''; $options = array_merge($this->options, $options); foreach ($options as $key => $value) { $pathSegment = $key . $value; if (!preg_match(self::ALLOWED_CHARS_REGEX, $pathSegment)) { throw IllegalArgumentException('Invalid characters in options'); } $path .= '/' . $pathSegment; } if (!preg_match(self::ALLOWED_CHARS_REGEX, $tableName)) { throw IllegalArgumentException('Invalid characters in tableName'); } return $path .= '/' . $tableName; }
/** * Initialize this storage instance. * * Available options: * * permanent_storage: sfStorage, required * * temporary_storage: sfStorage, required * * @param array $options An associative array of options * * @returns bool true, if initialization completes successfully */ public function initialize($options = array()) { parent::initialize($options); // $options are now available as $this->options if (!isset($this->options[self::PERMANENT])) { throw new sfInitializationException('You must provide a "' . self::PERMANENT . '" option to FileStorage.'); } Preconditions::check($this->options[self::PERMANENT] instanceof sfStorage, 'Permanent storage must be instance of sfStorage'); if (!isset($this->options[self::TEMPORARY])) { throw new sfInitializationException('You must provide a "' . self::TEMPORARY . '" option to FileStorage.'); } Preconditions::check($this->options[self::TEMPORARY] instanceof sfStorage, 'Temporary storage must be instance of sfStorage'); if (($initialKeys = $this->options[self::TEMPORARY]->read(self::KEY)) !== null) { $this->changedKeys = $initialKeys; } else { $this->changedKeys = array(); } }
/** * Constructs settings object. Omitting $settingsStorage will * result in "unmodifiable and default" settings. * * @param FajrConfig $config fajr configuration * @param sfStorage $settingsStorage user settings storage */ public function __construct(FajrConfig $config, sfStorage $settingsStorage = null) { $this->settingsStorage = $settingsStorage; $allSkins = $config->get(FajrConfigOptions::TEMPLATE_SKINS); foreach ($allSkins as $skin) { Preconditions::check($skin instanceof SkinConfig); } $skins = array(); foreach ($allSkins as $key => $skin) { if (!$skin->isInternal()) { $skins[$key] = $skin; } } $this->skins = $skins; $default = $config->get(FajrConfigOptions::TEMPLATE_DEFAULT_SKIN); if (!in_array($default, array_keys($this->skins))) { throw new RuntimeException("Default skin is not available!"); } $this->defaultSkinName = $default; }
public function add($castRoka, $znamka, $kredity) { Preconditions::check(in_array($castRoka, array(self::SEMESTER_LETNY, self::SEMESTER_ZIMNY, self::AKADEMICKY_ROK)), "Neplatná časť študijného roka."); $this->obdobia[$castRoka]->add($znamka, $kredity); $this->obdobia[self::AKADEMICKY_ROK]->add($znamka, $kredity); }
public function testCheckFail() { $this->setExpectedException("InvalidArgumentException"); Preconditions::check(2 > 4, "Plainly wrong"); }
/** * Prida predmet s danou znamkou, zarata do neohodnotenych ak * sa znamku nepodarilo rozpoznat alebo nie je vyplnena * @param string $castRoka do ktorej casti roka sa ma znamka zaratat * @param string $znamkaText nazov znamky (A, B, ...) * @param int $kredity pocet kreditov pre danu znamku */ public function add($castRoka, $znamkaText, $kredity) { Preconditions::check(in_array($castRoka, array(self::SEMESTER_LETNY, self::SEMESTER_ZIMNY, self::AKADEMICKY_ROK)), "Neplatná časť študijného roka."); $znamka = null; if ($znamkaText !== '') { $znamka = Znamka::fromString($znamkaText); } $this->obdobia[$castRoka]->add($kredity, $znamka); // Ak pridavame do akademickeho roka, tak hodnotu nechceme zaratat dvakrat if ($castRoka !== self::AKADEMICKY_ROK) { $this->obdobia[self::AKADEMICKY_ROK]->add($kredity, $znamka); } }
/** * Compute the p-value of null-hypothesis holds. * * Warning: please read * http://en.wikipedia.org/wiki/P-value#Frequent_misunderstandings * or consult statistician how to interpret results. * * @param int $degreesOfFreedom If you have 1-D histogram * analysis, $degreesOfFreedom should be number of bins-1. * For other scenarios, please consult statistician. * @param double $chisqr result of chi-square test * * @returns double p-value. */ static function pvalue($degreesOfFreedom, $chisqr) { Preconditions::check(is_int($degreesOfFreedom)); Preconditions::check($degreesOfFreedom > 0); Preconditions::checkIsNumber($chisqr); Preconditions::check($chisqr >= 0); return Gamma::regularizedGammaQ($degreesOfFreedom / 2.0, $chisqr / 2.0); }
/** * Compute square of the argument * * @param numeric $value * * @returns numeric $value * $value */ public static function sqr($value) { Preconditions::check(is_numeric($value)); return $value * $value; }
/** * Sends redirect headers. * * Note that this will not end script execution! * * @param array|string $target array query params or absolute url as string * @param string $file file to which redirect * @todo set http response code to 302/303. * * @returns void */ public function redirect($target = array(), $file = 'fajr.php') { Preconditions::check(is_array($target) || is_string($target), '$target needs to be array or string'); if (is_array($target)) { $url = FajrUtils::buildUrl($target, $file); } else { if (is_string($target)) { $url = $target; } else { assert(false); } } // Note: It is tempting to end script execution here. // However, it is not wise. Calling exit() will start // php shutdown phase and according to manual // there is unpredictable object destruction order // in this phase header('Location: ' . $url); $this->set('redirectUrl', $url); $this->setTemplate('redirect'); }