public function compute($password, User $user) { $fields = ['name', 'email', 'familyName', 'firstName']; $parts = []; $index = 1; foreach ($fields as $field) { foreach (preg_split('~[@._\\s+]~', $user->{$field}) as $part) { $parts[$part] = $index++; $parts[Strings::toAscii($part)] = $index++; } } try { $result = $this->computer->passwordStrength($password, $parts); } catch (\Exception $e) { return NULL; } return number_format($result['entropy'], 2); }
private function filterData() { foreach ($this->data as $id => $item) { $value = array(); $match = true; foreach ($this->filter[1] as $collumn) { $value[$collumn] = strtolower(Strings::toAscii($item[$collumn])); } foreach ($this->filter[0] as $word) { $found = false; foreach ($value as $val) { if (strstr($val, $word) !== false) { $found = true; break; } } if (!$found) { $match = false; break; } } if (!$match) { unset($this->data[$id]); } } }
/** * @param string $actual * @param string $condition * @param mixed $expected * @throws Exception * @return bool */ public function compare($actual, $condition, $expected) { $expected = (array) $expected; $expected = current($expected); $cond = str_replace(' ?', '', $condition); if ($cond === 'LIKE') { $actual = Strings::toAscii($actual); $expected = Strings::toAscii($expected); $pattern = str_replace('%', '(.|\\s)*', preg_quote($expected, '/')); return (bool) preg_match("/^{$pattern}\$/i", $actual); } elseif ($cond === '=') { return $actual == $expected; } elseif ($cond === '<>') { return $actual != $expected; } elseif ($cond === 'IS NULL') { return $actual === NULL; } elseif ($cond === 'IS NOT NULL') { return $actual !== NULL; } elseif ($cond === '<') { return (int) $actual < $expected; } elseif ($cond === '<=') { return (int) $actual <= $expected; } elseif ($cond === '>') { return (int) $actual > $expected; } elseif ($cond === '>=') { return (int) $actual >= $expected; } else { throw new Exception("Condition '{$condition}' not implemented yet."); } }
/** * @param type $name * @param \User $user * @return \Project * @throws \ExistingProjectException */ public function createProject($values, \User $user) { $project = new \Project(); $key = $this->keyGenerator->generateKey(); $name = Strings::webalize(Strings::lower(Strings::toAscii($values->caption))); $name = str_replace('-', '_', $name); $project->setCaption($values->caption)->setName($name)->setSourceLanguage($values->sourceLang)->setLink($values->link)->setKey($key); $project->setOwner($user); $this->dm->persist($project); $this->dm->flush(); return $project; }
public function search($query, $excludeIds = null) { $query = \Nette\Utils\Strings::toAscii($query); $regex = new \MongoRegex('/.*' . $query . '.*/i'); $qb = $this->dm->getRepository('User')->createQueryBuilder()->field('nick')->equals($regex); //->field('email')->equals($regex); //$qb->addOr($qb->field('nick')->equals($regex)); $qb->addOr($qb->expr()->field('email')->equals($regex)); if ($excludeIds !== null) { $qb->field('id')->notIn($excludeIds); } $qb->sort('nick', 'desc'); return $qb->getQuery()->execute(); }
public function save($sourceFile, $originalName, $path, $thumbs = array()) { if (!file_exists($this->root)) { throw new \Nette\IOException("FileBackend root doesn't exists '{$this->root}'"); } if (!file_exists($sourceFile)) { throw new \Nette\IOException("Source file '{$sourceFile}' doesn't exists "); } FileSystem::createDir($this->root . $path); $originalName = Strings::toAscii($originalName); $targetFile = $path . DIRECTORY_SEPARATOR . $originalName; $targetFullPath = $this->root . $targetFile; FileSystem::copy($sourceFile, $targetFullPath); foreach ($thumbs as $thumb => $thumbPath) { if (!file_exists($thumbPath)) { throw new \Nette\IOException("Thumb doesn't exists '{$thumbPath}'"); } $targetThumbPath = $this->root . $path . DIRECTORY_SEPARATOR . $thumb . '_' . $originalName; FileSystem::copy($thumbPath, $targetThumbPath); } return $path . DIRECTORY_SEPARATOR . $originalName; }
/** * Czech helper translate to morse code. * * @param string Message to be translated * @param bool Do you want to change numbers to words * @return string Encoded message */ public static function morse($text, $number = TRUE) { //prepare text for translation $morseText = \Nette\Utils\Strings::normalize($text); $morseText = \Nette\Utils\Strings::toAscii($morseText); $morseText = \Nette\Utils\Strings::lower($morseText); if ($number) { foreach (self::$NUMBERS as $original => $translation) { //translate numbers to text $morseText = str_replace($original, $translation, $morseText); } //remove double spaces $morseText = \Nette\Utils\Strings::replace($morseText, '/ {2,}/', ' '); $morseText = \Nette\Utils\Strings::normalize($morseText); } foreach (self::$TRANSLATOR as $original => $translation) { //translate text to morse code $morseText = str_replace($original, $translation, $morseText); } $morseText = '///' . $morseText . '//'; //start & end of text return $morseText; }
public function renderDefault($search) { //FIXME tagy ::: 'publish_date <=' => new \DateTime() $string = Strings::lower(Strings::normalize($search)); $string = Strings::replace($string, '/[^\\d\\w]/u', ' '); $words = Strings::split(Strings::trim($string), '/\\s+/u'); $words = array_unique(array_filter($words, function ($word) { return Strings::length($word) > 1; })); $words = array_map(function ($word) { return Strings::toAscii($word); }, $words); $string = implode(' ', $words); $this->template->tag = $this->tags->findOneBy(['name' => $string]); $result = $this->posts->fulltextSearch($string); if (count($result) == 0) { $this->template->search = $search; $this->template->error = 'Nic nebylo nalezeno'; } else { $this->template->search = $search; $this->template->result = $result; } }
/** * Processing of organization member editation form * * @Privilege("edit", "create") * * @param Form $form */ public function organizationFormSucceded(Form $form) { $values = $form->getValues(TRUE); unset($values['send']); //handle file upload $file = $values['file']; unset($values['file']); $params = $this->context->parameters; $path = $params['wwwDir'] . $params['memberPhotosStorage'] . '/'; if ($file->isOk()) { if ($file->isImage()) { //make sure the file will be JPEG $image = $file->toImage(); $filename = \Nette\Utils\Strings::lower(\Nette\Utils\Strings::toAscii($values['nickname'])) . ".jpg"; $image->save($path . $filename, 100, \Nette\Image::JPEG); } } $item = $this->members->get($values['nickname']); if ($item) { $item->update($values); $this->flashMessage('Záznam byl úspěšně aktualizován'); } else { $this->members->insert($values); $this->flashMessage('Záznam byl úspěšně vytvořen'); } $this->redirect('default'); }
/** * Get safe file name * * @param string $name File name * * @return string */ public function safeFilename($name) { $except = array("\\", "/", ":", "*", "?", '"', "<", ">", "|"); $name = str_replace($except, "", $name); return Strings::toAscii($name); }
private function processStorePath($storePath, $tmpFileName) { $replace = array(':year' => date('Y'), ':month' => date('m'), ':day' => date('d'), ':hash' => md5($tmpFileName . time() . Random::generate(32)), ':filename' => Strings::toAscii($tmpFileName)); return str_replace(array_keys($replace), array_values($replace), $storePath); }
public static function hash($input) { $hash = Strings::toAscii($input); $hash = Str_Replace(' ', '', $hash); $hash = md5($hash); return $hash . '-' . base64_encode($input); }
public function getWordVariations($word) { $sNoBrackets = Strings::replace($word, "/[\\[\\](){}]/", ""); $keywords = array_merge(array($word, $sNoBrackets), explode("-", $sNoBrackets), explode("_", $sNoBrackets), explode(" ", $sNoBrackets), Strings::split($sNoBrackets, "[ _-]")); foreach ($keywords as $index => $kw) { $keywords[$index] = Strings::trim($kw); $keywords[$index] = Strings::replace($keywords[$index], '/^\\+/', ''); // remove + operator if (Strings::length($keywords[$index]) < 3) { unset($keywords[$index]); } else { $keywords[] = Strings::toAscii($keywords[$index]); } } $keywords = array_unique($keywords); $keywords = array_values($keywords); return $keywords; }
/** * Apply fitler and tell whether row passes conditions or not * @param mixed $row * @param Filter $filter * @return mixed */ protected function applyFilter($row, Filter $filter) { if (is_array($row) || $row instanceof \Traversable) { if ($filter instanceof FilterDate) { return $this->applyFilterDate($row, $filter); } else { if ($filter instanceof FilterMultiSelect) { return $this->applyFilterMultiSelect($row, $filter); } else { if ($filter instanceof FilterDateRange) { return $this->applyFilterDateRange($row, $filter); } else { if ($filter instanceof FilterRange) { return $this->applyFilterRange($row, $filter); } } } } $condition = $filter->getCondition(); foreach ($condition as $column => $value) { if ($filter instanceof FilterText && $filter->isExactSearch()) { return $row[$column] == $value; } if ($filter instanceof FilterText && $filter->hasSplitWordsSearch() === FALSE) { $words = [$value]; } else { $words = explode(' ', $value); } $row_value = strtolower(Strings::toAscii($row[$column])); foreach ($words as $word) { if (FALSE !== strpos($row_value, strtolower(Strings::toAscii($word)))) { return $row; } } } } return FALSE; }
/** * Apply fitler and tell whether row passes conditions or not * @param mixed $row * @param Filter $filter * @return mixed */ protected function applyFilter($row, Filter $filter) { if (is_array($row) || $row instanceof \Traversable) { if ($filter instanceof FilterDate) { return $this->applyFilterDate($row, $filter); } $condition = $filter->getCondition(); foreach ($condition as $column => $value) { $words = explode(' ', $value); $row_value = strtolower(Strings::toAscii($row[$column])); foreach ($words as $word) { if (FALSE !== strpos($row_value, strtolower(Strings::toAscii($value)))) { return $row; } } } } return FALSE; }
/** * Remove big letters, diacritics, translate numbers and y = i * @param string $answer * @return string */ private function cutAnswer($answer) { return $this->translateNumber($this->normalizeY(Strings::toAscii(Strings::lower(Strings::trim(Strings::normalize($answer)))))); }
/** * Converts to ASCII. * @param string $s UTF-8 encoding * @return string ASCII */ public static function toAscii($s) { return Nette\Utils\Strings::toAscii($s); }
/** * Apply fitler and tell whether row passes conditions or not * @param mixed $row * @param Filter $filter * @return mixed */ protected function applyFilter($row, Filter $filter) { if (is_array($row) || $row instanceof \Traversable) { foreach ($row as $key => $value) { if (FALSE !== strpos(Strings::toAscii($value), Strings::toAscii($filter->getValue()))) { return $row; } } } return FALSE; }
/** * @param int $type * @param mixed $actual * @param mixed $expected * @param array $options * @return bool */ private function compare($type, $actual, $expected = null, array $options = []) { if (isset($options[Condition::CASE_INSENSITIVE]) && $options[Condition::CASE_INSENSITIVE]) { $actual = Strings::lower($actual); $expected = Strings::lower($expected); } if ($type === Condition::SAME) { return $actual === $expected; } elseif ($type === Condition::NOT_SAME) { return $actual !== $expected; } elseif ($type === Condition::IS_NULL) { return $actual === null; } elseif ($type === Condition::IS_NOT_NULL) { return $actual !== null; } elseif ($type === Condition::LIKE) { $actual = Strings::toAscii($actual); $expected = Strings::toAscii($expected); $pattern = str_replace('%', '(.|\\s)*', preg_quote($expected, '/')); return (bool) preg_match("/^{$pattern}\$/i", $actual); } else { throw new NotImplementedException('Filtering condition is not implemented.'); } }
public static function parse($phrase) { $normalized = Strings::lower(Strings::normalize(Strings::toAscii($phrase))); $words = preg_split('~\\s+~', $normalized, -1, PREG_SPLIT_NO_EMPTY); $number = 0; $buffer = 0; while ($word = array_shift($words)) { $compound = static::parseCompoundWord($word); if ($compound) { $buffer += $compound; continue; } switch ($word) { case 'a': // ignore // ignore case 'nula': // keep $number 0 break; case 'jedno': case 'jedna': case 'jeden': $buffer += 1; break; case 'dva': case 'dve': $buffer += 2; break; case 'tri': $buffer += 3; break; case 'ctyri': $buffer += 4; break; case 'pet': $buffer += 5; break; case 'sest': $buffer += 6; break; case 'sedm': case 'sedum': $buffer += 7; break; case 'osm': case 'osum': $buffer += 8; break; case 'devet': $buffer += 9; break; case 'deset': $buffer += 10; break; case 'jedenact': $buffer += 11; break; case 'dvanact': $buffer += 12; break; case 'trinact': $buffer += 13; break; case 'ctrnact': $buffer += 14; break; case 'patnact': $buffer += 15; break; case 'sestnact': $buffer += 16; break; case 'sedmnact': case 'sedumnact': $buffer += 17; break; case 'osmnact': case 'osumnact': $buffer += 18; break; case 'devatenact': $buffer += 19; break; case 'dvacet': $buffer += 20; break; case 'tricet': $buffer += 30; break; case 'ctyricet': $buffer += 40; break; case 'padesat': $buffer += 50; break; case 'sedesat': $buffer += 60; break; case 'sedmdesat': case 'sedumdesat': $buffer += 70; break; case 'osmdesat': case 'osumdesat': $buffer += 80; break; case 'devadesat': $buffer += 90; break; case 'sto': if ($buffer === 1) { // jedno sto $buffer = 100; break; } else { $buffer += 100; break; } case 'ste': case 'sta': case 'set': if ($buffer >= 11 && $buffer <= 19) { // devatenact set $number += $buffer * 100; $buffer = 0; } else { $buffer *= 100; } break; case 'tisic': case 'tisice': case 'tisicu': $number += ($buffer ?: 1) * 1000.0; $buffer = 0; break; case 'milion': case 'miliony': case 'milionu': $number += ($buffer ?: 1) * 1000000.0; $buffer = 0; break; default: throw new InvalidArgumentException("Failed to parse '{$word}'"); } } return (int) $number + $buffer; var_dump($number, $buffer); }
/** * @param string $specialClass * @return \Illagrenan\Navigation\NavigationNode */ public function addSpecialClass($specialClass) { if (Validators::isUnicode($specialClass) === FALSE) { throw new Exceptions\InvalidSpecialClassException("Given \"" . $specialClass . "\" is not valid special class."); } $specialClass = Strings::trim($specialClass); $specialClass = Strings::normalize($specialClass); $specialClass = Strings::toAscii($specialClass); $this->specialClass[] = $specialClass; return $this; }
/** * Filter by keyword * @param Filter\FilterText $filter * @return void */ public function applyFilterText(Filter\FilterText $filter) { $condition = $filter->getCondition(); $or = []; foreach ($condition as $column => $value) { $words = explode(' ', $value); foreach ($words as $word) { $escaped = $this->data_source->getConnection()->getDriver()->escapeLike($word, 0); if (preg_match("/[€-ÿ]/", $escaped)) { $or[] = "{$column} LIKE {$escaped} COLLATE utf8_bin"; } else { $escaped = Strings::toAscii($escaped); $or[] = "{$column} LIKE {$escaped} COLLATE utf8_general_ci"; } } } if (sizeof($or) > 1) { $this->data_source->where('(%or)', $or); } else { $this->data_source->where($or); } }
protected function generateKey($key) { return Strings::toAscii($key) . '.srt'; }