/** * @param $string * * @return mixed */ public function addAliases($string) { $string = mb_strtoupper(str_ireplace('ё', 'е', $this->modx->stripTags($string)), 'UTF-8'); $string = preg_replace('#\\[.*\\]#isU', '', $string); $pcre = $this->config['split_words']; $words = preg_split($pcre, $string, -1, PREG_SPLIT_NO_EMPTY); $words = array_unique($words); $forms = $this->getBaseForms($words, false); $q = $this->modx->newQuery('mseAlias', array('word:IN' => array_merge($words, array_keys($forms)))); $q->select('word,alias,replace'); $tstart = microtime(true); if ($q->prepare() && $q->stmt->execute()) { $this->modx->queryTime += microtime(true) - $tstart; $this->modx->executedQueries++; while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) { if ($row['replace']) { $all = current($this->getAllForms($row['word'])); $all[] = $row['word']; foreach ($all as $word) { $key = array_search(mb_strtolower($word), $words); if ($key !== false) { $words[$key] = $row['alias']; } } } else { $words[] = $row['alias']; } } } $words = array_unique($words); return implode(' ', $words); }
/** * @param string $content * @param array $tags */ public function findTags($content, &$tags) { $parser = $this->modx->getParser(); $collectedTags = array(); $parser->collectElementTags($content, $collectedTags); foreach ($collectedTags as $tag) { $tagName = $tag[1]; if (substr($tagName, 0, 1) == '!') { $tagName = substr($tagName, 1); } $token = substr($tagName, 0, 1); $tagParts = xPDO::escSplit('?', $tagName, '`', 2); $tagName = trim($tagParts[0]); $tagPropString = null; $tagName = trim($this->modx->stripTags($tagName)); if (in_array($token, array('$', '+', '~', '#', '%', '-', '*'))) { $tagName = substr($tagName, 1); } switch ($token) { case '$': $class = 'modChunk'; break; case '+': case '~': case '#': case '%': case '-': case '*': continue 2; break; default: $class = 'modSnippet'; break; } if (isset($tagParts[1])) { $tagPropString = trim($tagParts[1]); $this->findTags($tagPropString, $tags); $element = $parser->getElement($class, $tagName); if ($element) { $properties = $element->getProperties($tagPropString); } else { $properties = array(); } } else { $properties = array(); } $this->debug('Found ' . $class . ' ' . $tagName . ' with properties ' . print_r($properties, 1)); $tagName = $parser->realname($tagName); if (empty($tagName)) { continue; } $tags[$tagName] = array('name' => $tagName, 'class' => $class); foreach ($properties as $property) { $prop = trim($property); if (!empty($prop) && !is_numeric($prop) && is_string($prop)) { $tags[$prop] = array('name' => $prop, 'class' => 'modChunk', 'isProperty' => true); } } } }
/** * Search and return array with resources that matched for LIKE search * * @param $query * * @return array */ public function simpleSearch($query) { $string = preg_replace('/[^_-а-яёa-z0-9\\s\\.\\/]+/iu', ' ', $this->modx->stripTags($query)); $result = array(); $q = $this->modx->newQuery('mseIntro'); $q->select('`resource`'); $q->where(array('intro:LIKE' => '%' . $string . '%')); if ($q->prepare() && $q->stmt->execute()) { $result = $q->stmt->fetchAll(PDO::FETCH_COLUMN); } return $result; }
/** * Sanitize a string * * @param string $text The text to sanitize * @return string The sanitized text */ public function sanitize($text) { $text = strip_tags($text); $text = preg_replace('/(\\[\\[\\+.*?\\]\\])/i', '', $text); return $this->modx->stripTags($text); }
/** * Transforms a string to form a valid URL representation. * * @param string $alias A string to transform into a valid URL representation. * @param array $options Options to append to or override configuration settings. * @return string The transformed string. */ public function cleanAlias($alias, array $options = array()) { /* setup the various options */ $iconv = function_exists('iconv'); $mbext = function_exists('mb_strlen') && (bool) $this->xpdo->getOption('use_multibyte', false); $charset = strtoupper((string) $this->xpdo->getOption('modx_charset', $options, 'UTF-8')); $delimiter = $this->xpdo->getOption('friendly_alias_word_delimiter', $options, '-'); $delimiters = $this->xpdo->getOption('friendly_alias_word_delimiters', $options, '-_'); $maxlength = (int) $this->xpdo->getOption('friendly_alias_max_length', $options, 0); $stripElementTags = (bool) $this->xpdo->getOption('friendly_alias_strip_element_tags', $options, true); $trimchars = $this->xpdo->getOption('friendly_alias_trim_chars', $options, '/.' . $delimiters); $restrictchars = $this->xpdo->getOption('friendly_alias_restrict_chars', $options, 'pattern'); $restrictcharspattern = $this->xpdo->getOption('friendly_alias_restrict_chars_pattern', $options, '/[\\0\\x0B\\t\\n\\r\\f\\a&=+%#<>"~`@\\?\\[\\]\\{\\}\\|\\^\'\\\\]/'); $lowercase = (bool) $this->xpdo->getOption('friendly_alias_lowercase_only', $options, true); $translit = $this->xpdo->getOption('friendly_alias_translit', $options, $iconv ? 'iconv' : 'none'); $translitClass = $this->xpdo->getOption('friendly_alias_translit_class', $options, 'translit.modTransliterate'); /* strip html and optionally MODX element tags (stripped by default) */ if ($this->xpdo instanceof modX) { $alias = $this->xpdo->stripTags($alias, '', $stripElementTags ? array() : null); } /* replace with the specified word delimiter */ $alias = str_replace(' ', $delimiter, $alias); /* decode named entities to the appropriate character for the character set */ $alias = html_entity_decode($alias, ENT_QUOTES, $charset); /* replace any remaining & with a lexicon value if available */ if ($this->xpdo instanceof modX && $this->xpdo->getService('lexicon', 'modLexicon')) { $alias = str_replace('&', $this->xpdo->lexicon('and') ? ' ' . $this->xpdo->lexicon('and') . ' ' : ' and ', $alias); } /* apply transliteration as configured */ switch ($translit) { case '': case 'none': /* no transliteration */ break; case 'iconv': /* if iconv is available, use the built-in transliteration it provides */ $alias = iconv($mbext ? mb_detect_encoding($alias) : $charset, $charset . '//TRANSLIT//IGNORE', $alias); break; default: /* otherwise look for a transliteration service class that will accept named transliteration tables */ if ($this->xpdo instanceof modX) { $translitClassPath = $this->xpdo->getOption('friendly_alias_translit_class_path', $options, $this->xpdo->getOption('core_path', $options, MODX_CORE_PATH) . 'components/'); if ($this->xpdo->getService('translit', $translitClass, $translitClassPath, $options)) { $alias = $this->xpdo->translit->translate($alias, $translit); } } break; } /* restrict characters as configured */ switch ($restrictchars) { case 'alphanumeric': /* restrict alias to alphanumeric characters only */ $alias = preg_replace('/[^\\.%A-Za-z0-9 _-]/', '', $alias); break; case 'alpha': /* restrict alias to alpha characters only */ $alias = preg_replace('/[^\\.%A-Za-z _-]/', '', $alias); break; case 'legal': /* restrict alias to legal URL characters only */ $alias = preg_replace('/[\\0\\x0B\\t\\n\\r\\f\\a&=+%#<>"~`@\\?\\[\\]\\{\\}\\|\\^\'\\\\]/', '', $alias); break; case 'pattern': default: /* restrict alias using regular expression pattern configured (same as legal by default) */ if (!empty($restrictcharspattern)) { $alias = preg_replace($restrictcharspattern, '', $alias); } } /* replace one or more space characters with word delimiter */ $alias = preg_replace('/\\s+/u', $delimiter, $alias); /* replace one or more instances of word delimiters with word delimiter */ $delimiterTokens = array(); for ($d = 0; $d < strlen($delimiters); $d++) { $delimiterTokens[] = $delimiters[$d]; } $delimiterPattern = '/[' . implode('|', $delimiterTokens) . ']+/'; $alias = preg_replace($delimiterPattern, $delimiter, $alias); /* unless lowercase_only preference is explicitly off, change case to lowercase */ if ($lowercase) { if ($mbext) { /* if the mb extension is available use it to protect multi-byte chars */ $alias = mb_convert_case($alias, MB_CASE_LOWER, $charset); } else { /* otherwise, just use strtolower */ $alias = strtolower($alias); } } /* trim specified chars from both ends of the alias */ $alias = trim($alias, $trimchars); /* get the strlen of the alias (use mb extension if available) */ $length = $mbext ? mb_strlen($alias, $charset) : strlen($alias); /* if maxlength is specified and exceeded, return substr with additional trim applied */ if ($maxlength > 0 && $length > $maxlength) { $alias = substr($alias, 0, $maxlength); $alias = trim($alias, $trimchars); $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "alias after maxlength applied = {$alias}"); } return $alias; }