public function __invoke($uri, $query = null, $configKey = 'default') { if ($query) { if (true === is_array($query)) { $query = implode(',', $query); } if (false !== ($pos = strrpos($uri, '.'))) { $uri = explode('/', $uri); $fileName = array_pop($uri); $nameArray = explode('.', $fileName); $nameExt = array_pop($nameArray); $nameFinal = array_pop($nameArray); $nameFinal .= ',' . $query; array_push($nameArray, $nameFinal, $nameExt); $fileName = implode('.', $nameArray); array_push($uri, $fileName); $uri = implode('/', $uri); } } if (\Phalcon\Text::startsWith($uri, 'http://', false) || \Phalcon\Text::startsWith($uri, 'https://', false)) { return str_replace('http://api.wallstreetcn.com/', 'http://thumbnail.wallstreetcn.com/thumb/', $uri); } $config = self::getDI()->getConfig(); if (isset($config->thumbnail->{$configKey}->baseUri) && ($baseUrl = $config->thumbnail->{$configKey}->baseUri)) { return $baseUrl . $uri; } return $uri; }
/** * Gets the value of an environment variable. Supports boolean, empty and null. * * @param string $key * @param mixed $default * @return mixed */ function env($key, $default = null) { $value = getenv($key); if ($value === false) { return value($default); } switch (strtolower($value)) { case 'true': case '(true)': return true; case 'false': case '(false)': return false; case 'empty': case '(empty)': return ''; case 'null': case '(null)': return; } if (Text::startsWith($value, '"') && Text::endsWith($value, '"')) { return substr($value, 1, -1); } return $value; }
private function addFunction($jsCode) { if (!Text::startsWith($jsCode, "function")) { $jsCode = "%function(){" . $jsCode . "}%"; } return $jsCode; }
public function translate($idElement, $key, $default) { $this->message = ""; if (Text::startsWith($this->language, "en", true)) { return $default; } $trans = $this->translations->filter(function ($object) use($idElement, $key) { if (Text::startsWith($this->language, $object->getLang(), true) && $object->getIdElement() == $idElement && $object->getName() == $key) { return $object; } }); if (is_array($trans)) { if (sizeof($trans) > 0) { $trans = $trans[0]; } else { $this->message = $this->translate(1, "translate.info", ""); return $default; } } if (is_a($trans, "Translation")) { return $trans->getText(); } else { $this->message = $this->translate(1, "translate.info", ""); return $default; } }
/** * Icons to display, with or without text (see text option). * By default, the primary icon is displayed on the left of the label text and the secondary is displayed on the right. * The positioning can be controlled via CSS. * The value for the primary and secondary properties must match an icon class name, e.g., "ui-icon-gear". * For using only one icon: icons: { primary: "ui-icon-locked" }. For using two icons: icons: { primary: "ui-icon-gear", secondary: "ui-icon-triangle-1-s" }. * @param String $value default : { primary: null, secondary: null } * @return $this */ public function setIcons($value) { if (is_string($value)) { if (Text::startsWith($value, "{")) { } $value = "%" . $value . "%"; } return $this->setParam("icons", $value); }
/** * Define source property with an ajax request based on $url * $url must return a JSON array of values * @param String $url * @return $this */ public function setAjaxSource($url) { if (Text::startsWith($url, "/")) { $u = $this->js->getDi()->get("url"); $url = $u->getBaseUri() . $url; } $ajax = "%function (request, response) {\n\t\t\t\$.ajax({\n\t\t\t\turl: '{$url}',\n\t\t\t\tdataType: 'jsonp',\n\t\t\t\tdata: {q : request.term},\n\t\t\t\tsuccess: function(data) {response(data);}\n\t\t\t});\n\t\t}%"; return $this->setParam("source", $ajax); }
private function parseDefinitionArgument($argument) { if (Text::startsWith($argument, '!')) { return ['type' => 'parameter', 'value' => Arr::path($this->config->toArray(), substr($argument, 1))]; } elseif (Text::startsWith($argument, '@')) { return ['type' => 'service', 'name' => substr($argument, 1)]; } else { return ['type' => 'parameter', 'value' => $argument]; } }
public function testStartsWith() { $this->assertFalse(PhText::startsWith("", "")); $this->assertFalse(PhText::startsWith("", "hello")); $this->assertTrue(PhText::startsWith("Hello", "H")); $this->assertTrue(PhText::startsWith("Hello", "He")); $this->assertTrue(PhText::startsWith("Hello", "Hello")); $this->assertFalse(PhText::startsWith("Hello", "hel")); $this->assertFalse(PhText::startsWith("Hello", "hello")); $this->assertFalse(PhText::startsWith("Hello", "hello", true)); $this->assertTrue(PhText::startsWith("Hello", "hello", false)); $this->assertTrue(PhText::startsWith("Hello", "h", false)); }
/** * Query the existing cached keys * * @param string|null $prefix * @return array */ public function queryKeys($prefix = null) { if (is_null($prefix) === false) { $prefix = (string) $prefix; } if (is_array($this->_data) === true) { if (is_null($prefix) === true) { return array_keys($this->_data); } $result = array(); foreach ($this->_data as $key => $value) { if (Text::startsWith($key, $prefix) === true) { $result[] = $key; } } return $result; } //@note The default implementation returns NULL return array(); }
public function isLocal() { return !Text::startsWith($this->getUrl(), 'http'); }
public static function startsWith($str, $start, $ignoreCase = null) { return Text::startsWith($str, $start, $ignoreCase); }
/** * Query the existing cached keys * * @param string|null $prefix * @return array * @throws Exception */ public function queryKeys($prefix = null) { if (is_string($prefix) === false && is_null($prefix) === false) { throw new Exception('Invalid parameter type.'); } $keys = array(); //We use a directory iterator to traverse the cache dir directory $ce = new DirectoryIterator($this->_options['cacheDir']); if (is_null($prefix) === false) { //Prefix is set foreach ($ce as $item) { if (is_dir($item) === false) { $key = $item->getFileName(); if (Text::startsWith($key, $prefix) === false) { continue; } $keys[] = $key; } } } else { //Without using a prefix foreach ($ce as $item) { if (is_dir($item) === false) { $keys[] = $item->getFileName(); } } } return $keys; }
/** * Makes the work of autoload registered classes * * @param string $className * @return boolean */ public function autoLoad($className) { $eventsManager = $this->_eventsManager; if (is_object($eventsManager) === true) { $eventsManager->fire('loader:beforeCheckClass', $this, $className); } /* First we check for static paths */ if (is_array($this->_classes) === true && isset($this->_classes[$className]) === true) { $filePath = $this->_classes[$className]; if (is_object($eventsManager) === true) { $this->_foundPath = $filePath; $eventsManager->fire('loader:pathFound', $this, $filePath); } require_once $filePath; return true; } $extensions = $this->_extensions; /* Checking in namespaces */ if (is_array($this->_namespaces) === true) { foreach ($this->_namespaces as $nsPrefix => $directory) { //The class name must start with the current namespace if (Text::startsWith($className, $nsPrefix) === true) { //Get the possible file path $fileName = self::possibleAutoloadFilePath($nsPrefix, $className, \DIRECTORY_SEPARATOR, null); if ($fileName !== false) { //Add a trailing directory separator is the user forgot to do that $fixedDirectory = self::fixPath($directory, \DIRECTORY_SEPARATOR); foreach ($extensions as $extension) { $filePath = $fixedDirectory . $fileName . '.' . $extension; //Check if an events manager is available if (is_object($eventsManager) === true) { $this->_checkedPath = $filePath; $eventsManager->fire('loader:beforeCheckPath', $this); } //This is probably a good path, let's check if the file exists if (file_exists($filePath) === true) { if (is_object($eventsManager) === true) { $this->_foundPath = $filePath; $eventsManager->fire('loader:pathFound', $this, $filePath); } require_once $filePath; //Return true means success return true; } } } } } } /* Checking in prefixes */ $prefixes = $this->_prefixes; if (is_array($prefixes) === true) { foreach ($prefixes as $prefix => $directory) { //The class name starts with the prefix? if (Text::startsWith($className, $prefix) === true) { //Get the possible file path $fileName = self::possibleAutoloadFilePath($prefix, $className, \DIRECTORY_SEPARATOR, '_'); if ($fileName !== false) { //Add a trailing directory separator is the user forgot to do that $fixedDirectory = self::fixPath($directory, \DIRECTORY_SEPARATOR); foreach ($extensions as $extension) { $filePath = $fixedDirectory . $fileName . '.' . $extension; if (is_object($eventsManager) === true) { $this->_checkedPath = $filePath; $eventsManager->fire('loader:beforeCheckPath', $this, $filePath); } if (file_exists($filePath) === true) { //Call 'pathFound' event if (is_object($eventsManager) === true) { $this->_foundPath = $filePath; $eventsManager->fire('loader:pathFound', $this, $filePath); } require_once $filePath; return true; } } } } } } //Change the pseudo-separator by the directory separator in the class name $dsClassName = str_replace('_', \DIRECTORY_SEPARATOR, $className); //And change the namespace separator by directory separator too $nsClassName = str_replace('\\', \DIRECTORY_SEPARATOR, $dsClassName); /* Checking in directories */ $directories = $this->_directories; if (is_array($directories) === true) { foreach ($directories as $directory) { //Add a trailing directory separator if the user forgot to do that $fixedDirectory = self::fixPath($directory, \DIRECTORY_SEPARATOR); foreach ($extensions as $extension) { //Create a possible path for the file $filePath = $fixedDirectory . $nsClassName . '.' . $extension; if (is_object($eventsManager) === true) { $this->_checkedPath = $filePath; $eventsManager->fire('loader:beforeCheckPath', $this, $filePath); } //Check in every directory if the class exists here if (file_exists($filePath) === true) { //Call 'pathFound' event if (is_object($eventsManager) === true) { $this->_foundPath = $filePath; $eventsManager->fire('loader:pathFound', $this, $filePath); } require_once $filePath; //Returning true means success return true; } } } } //Call 'afterCheckClass' event if (is_object($eventsManager) === true) { $eventsManager->fire('loader:afterCheckClass', $this, $className); } //Cannot find the class - return false return false; }
public static function startsWith($str, $start, $ignoreCase = true) { return parent::startsWith($str, $start, $ignoreCase); }
public function assetType($src) { if (Text::startsWith($src, 'http')) { return 'remote'; } if (Text::startsWith($src, 'files/')) { return 'local'; } return 'inline'; }
/** * Query the existing cached keys * * @param string|null $prefix * @return array * @throws Exception */ public function queryKeys($prefix = null) { if (is_string($prefix) === false && is_null($prefix) === false) { throw new Exception('Invalid parameter type.'); } if (is_object($this->_memcache) === false) { $this->_connect(); } //Get the key from memcached $keys = $this->_memcache->get($this->_options['statsKey']); if (is_array($keys) === true) { if (isset($prefix) === true) { //Use prefix $prefixedKeys = array(); foreach ($keys as $key => $ttl) { if (Text::startsWith($key, $prefix) === false) { continue; } $prefixedKeys[] = $key; } } else { //Don't use prefix $prefixedKeys = array_keys($keys); } return $prefixedKeys; } return array(); }
/** * Checks if a password hash is a valid bcrypt's hash * * @param string $passwordHash * @return boolean * @throws FlashException */ public function isLegacyHash($passwordHash) { if (is_string($passwordHash) === false) { throw new FlashException('Invalid parameter type.'); } return Text::startsWith($passwordHash, '$2a$'); }
protected function getTypeName($type) { static $types; if (!$types) { $refl = new \ReflectionClass(Column::CLASS); foreach ($refl->getConstants() as $name => $val) { if (Text::startsWith($name, 'TYPE_')) { $types[$val] = strtolower(substr($name, 5)); } } } $name = isset($types[$type]) ? $types[$type] : self::TYPE_UNKNOWN; return isset(self::$TYPE_ALIASES[$name]) ? self::$TYPE_ALIASES[$name] : $name; }
function url($path, $queries = [], $secure = null) { if (isHttpUrl($path)) { return $path; } $path = trim($path, '/'); if (Config::get('app.enable_https')) { Text::startsWith($path, 'admin', false) and $secure = true; $secure === null && null !== ($configValue = Config::get('app.secure_routes.' . $path)) and $secure = $configValue; // TODO Detection https via proxy $secure === null and $secure = Di::getDefault()['request']->getScheme() === 'https'; } else { $secure = false; } $protocol = $secure ? 'https://' : 'http://'; $host = fnGet($_SERVER, 'HTTP_HOST') ?: parse_url(Config::get('app.url'), PHP_URL_HOST); $base = $_SERVER['SCRIPT_NAME']; $base = trim(dirname($base), '/'); $base and $base .= '/'; $url = $protocol . $host . '/' . $base; $url .= $path; if ($queries && is_array($queries)) { $queries = http_build_query($queries); } $queries && is_string($queries) and $url .= '?' . str_replace('?', '', $queries); return $url; }
/** * Reconfigure the route adding a new pattern and a set of paths * * @param string $pattern * @param array|null|string $paths * @throws Exception */ public function reConfigure($pattern, $paths = null) { if (is_string($pattern) === false) { throw new Exception('The pattern must be string'); } $originalPattern = $pattern; if (is_string($paths) === true) { $moduleName = null; $controllerName = null; $actionName = null; //Explode the short paths using the :: separator $parts = explode('::', $paths); $numberParts = count($parts); //Create the array paths dynamically switch ($numberParts) { case 3: $moduleName = $parts[0]; $controllerName = $parts[1]; $actionName = $parts[2]; break; case 2: $controllerName = $parts[0]; $actionName = $parts[1]; break; case 1: $controllerName = $parts[0]; break; //@note no default } $routePaths = array(); //Process module name if (is_null($moduleName) === false) { $routePaths['module'] = $moduleName; } //Process controller name if (is_null($controllerName) === false) { //Check if we need to obtain the namespace if (strpos($controllerName, '\\') !== false) { $classWithNamespace = get_class($controllerName); //Extract the real class name from the namespaced class //Extract the namespace from the namespaced class $pos = strrpos($classWithNamespace, '\\'); if ($pos !== false) { $namespaceName = substr($classWithNamespace, 0, $pos); $realClassName = substr($classWithNamespace, $pos); } else { $realClassName = $classWithNamespace; } //Update the namespace if (isset($namespaceName) === true) { $routePaths['namespace'] = $namespaceName; } } else { $realClassName = $controllerName; } //Always pass the controller to lowercase $realClassName = Text::uncamelize($realClassName); //Update the controller path $routePaths['controller'] = $realClassName; } //Process action name if (is_null($actionName) === false) { $routePaths['action'] = $actionName; } } elseif (is_array($paths) === true) { $routePaths = $paths; } elseif (is_null($paths) === true) { $routePaths = array(); } else { throw new Exception('The route contains invalid paths'); } //If the route starts with '#' we assume that it is a regular expression if (Text::startsWith($pattern, '#') === false) { if (strpos($pattern, '{') !== false) { //The route has named parameters so we need to extract them $pattern = self::extractNamedParameters($pattern, $routePaths); } //Transform the route's pattern to a regular expression $pattern = $this->compilePattern($pattern); } //Update member variables $this->_pattern = $originalPattern; $this->_compiledPattern = $pattern; $this->_paths = $routePaths; }
public function afterExecuteRoute($dispatcher) { return; //$this->response->setContent($this->htmlDoc); //$this->htmlDoc->getBody()->setContent($this->response->getContent()); //$this->response->setContent($this->htmlDoc); //return $this->response->send(); //$item = new Doc(); $item = new HtmlModel(array('tag' => new Doc())); //var_dump ($doctag->getChild('html')->setAttribute('lang', 'gb')); echo '<pre><code>' . htmlentities($item) . '</code></pre>'; return; $test = HtmlTag::factory(array('tagname' => 'doc', 'children' => array('doctype' => array('tagname' => 'doctype')))); echo '<pre><code>'; var_dump($test); echo '</code></pre>'; return; // Set up the assets manager to not output directly $this->assets->useImplicitOutput(false); // Compile everything into the assets manager foreach ($this->htmldoc['css'] as $css) { if (Text::startsWith($css, 'http')) { $this->assets->addCss($css, false); } elseif (Text::startsWith($css, 'files/')) { $this->assets->addCss($css); } else { $this->assets->addInlineCss($css); } } foreach ($this->htmldoc['js'] as $js) { if (Text::startsWith($js, 'http')) { $this->assets->addJs($js, false); } elseif (Text::startsWith($js, 'files/')) { $this->assets->addJs($js); } else { $this->assets->addInlineJs($js); } } $this->assets->collection('jsfooter'); foreach ($this->htmldoc['jsfooter'] as $js) { if (Text::startsWith($js, 'http')) { $this->assets->collection('jsfooter')->addJs($js, false); } elseif (Text::startsWith($js, 'files/')) { $this->assets->collection('jsfooter')->addJs($js); } else { $this->assets->addInlineJs($js); } } $this->htmldoc['meta-src'] = ''; foreach ($this->htmldoc['meta'] as $meta) { $this->htmldoc['meta-src'] .= $this->tag->tagHtml('meta', $meta, true, true, true); } // Create the css view variable $this->htmldoc['css-src'] = $this->assets->outputCss(); $this->htmldoc['css-inline-src'] = $this->assets->outputInlineCss(); // Create the javascript view variables // Inline scripts are separated $this->htmldoc['js-src'] = $this->assets->outputJs(); $this->htmldoc['js-inline-src'] = $this->assets->outputInlineJs(); // Create the footer javascript view variable $this->htmldoc['jsfooter-src'] = $this->assets->outputJs('jsfooter'); // Create the import view variable $this->htmldoc['import-src'] = ''; foreach ($this->htmldoc['import'] as $import) { $this->htmldoc['import-src'] .= $this->tag->tagHtml('link', array('rel' => 'import', 'href' => $this->url->get($import)), true, true, true); } // Create the menu items $this->htmldoc['menu-items'] = $this->_parse_menu_items($this->htmldoc['menu']); // Add the variables to the view $this->view->setVar('htmldoc', $this->htmldoc); $this->view->setVar('menu', $this->view->getPartial('shared/menu')); }
/** * Query the existing cached keys * * @param string|null $prefix * @return array * @throws Exception */ public function queryKeys($prefix = null) { if (is_null($prefix) === true) { $prefix = '_PHCX'; } elseif (is_string($prefix) === true) { $prefix = '_PHCX' . $prefix; } else { throw new Exception('Invalid parameter type.'); } /* *Get the key from XCache (we cannot use xcache_list() as it is available only to * the administrator) */ $keys = xcache_get($this->_options['statsKey']); if (is_array($keys) === true) { $prefixedKeys = array(); foreach ($keys as $key => $ttl) { if (Text::startsWith($key, $prefix) === false) { continue; } $prefixedKeys[] = substr($key, 5); } return $prefixedKeys; } return array(); }
/** * @param $dmmId * @return string */ public static function dmmIdToBanngo($dmmId) { //Normal banngo with prefix if (preg_match('/^[a-z]*_*\\d+([a-z]+)(\\d+)[a-z]*$/', $dmmId, $matches) || preg_match('/^([a-z]+)(\\d+)$/', $dmmId, $matches)) { $digits = $matches[1]; $number = $matches[2]; if (strlen($number) > 3 && Text::startsWith($number, '00')) { return $digits . substr($number, 2); } return $digits . $number; } return $dmmId; }
/** * Produce the routing parameters from the rewrite information * * @param string|null $uri * @throws Exception */ public function handle($uri = null) { if (is_null($uri) === true) { $uri = $this->getRewriteUri(); } elseif (is_string($uri) === false) { throw new Exception('Invalid parameter type.'); } $annotationsService = null; if ($this->_processed === false) { if (is_array($this->_handlers) === true) { foreach ($this->_handlers as $scope) { if (is_array($scope) === true) { //A prefix (if any) must be in position 0 if (is_string($scope[0]) === true) { if (Text::startsWith($uri, $scope[0]) === false) { continue; } } if (is_object($annotationsService) === false) { if (is_object($this->_dependencyInjector) === false) { throw new Exception("A dependency injection container is required to access the 'annotations' service"); } $annotationsService = $this->_dependencyInjector->getShared('annotations'); //@note no interface validation } //The controller must be in position 1 if (strpos($scope[1], '\\') !== false) { //Extract the real class name from the namespaced class $classWithNamespace = get_class($handler); //Extract the real class name from the namespaced class //Extract the namespace from the namespaced class $pos = strrpos($classWithNamespace, '\\'); if ($pos !== false) { $namespaceName = substr($classWithNamespace, 0, $pos); $controllerName = substr($classWithNamespace, $pos); } else { $controllerName = $classWithNamespace; $namespaceName = null; } $this->_routePrefix = null; //Check if the scope has a module associated if (isset($scope[2]) === true) { $moduleName = $scope[2]; } else { $moduleName = null; } //Get the annotations from the class $handlerAnnotations = $annotationsService->get($handler . $this->_controllerSuffix); //Process class annotations $classAnnotations = $handlerAnnotations->getClassAnnotations(); if (is_object($classAnnotations) === true) { //Process class annotaitons $annotations = $classAnnotations->getAnnotations(); if (is_array($annotations) === true) { foreach ($annotations as $annotation) { $this->processControllerAnnotation($annotation); } } } //Process method annotations $methodAnnotations = $handlerAnnotations->getMethodsAnnotations(); if (is_array($methodAnnotations) === true) { foreach ($methodAnnotations as $method => $collection) { if (is_object($collection) === true) { $annotations = $collection->getAnnotations(); foreach ($annotations as $annotation) { $this->processActionAnnotation($moduleName, $namespaceName, $controllerName, $method, $annotation); } } } } } } } } $this->_processed = true; } parent::handle($uri); }
public function searchAction() { $text = $_POST["text"]; if (Text::startsWith($this->translateEngine->getLanguage(), "en", true)) { $domaines = Domaine::find("libelle LIKE '%" . $text . "%'"); $rubriques = Rubrique::find("titre LIKE '%" . $text . "%' OR description LIKE '%" . $text . "%'"); $exemples = Exemple::find("titre LIKE '%" . $text . "%' OR description LIKE '%" . $text . "%'"); } else { $domaines = array(); $rubriques = array(); $exemples = array(); $translations = $this->translateEngine->getTranslations(); if ($text != "") { $arrayTranslations = $translations->filter(function ($object) use($text) { if ($object->getName() == "domaine.libelle" && stristr($object->getText(), $text) !== false) { return $object; } }); if (sizeof($arrayTranslations) > 0) { $domaines = Domaine::find($this->_getCondition($arrayTranslations)); } $arrayRubriques = $translations->filter(function ($object) use($text) { if (Text::startsWith($object->getName(), "rubrique" && stristr($object->getText(), $text) !== false)) { return $object; } }); if (sizeof($arrayRubriques) > 0) { $rubriques = Rubrique::find($this->_getCondition($arrayRubriques)); } $arrayExemples = $translations->filter(function ($object) use($text) { if (Text::startsWith($object->getName(), "exemple")) { if (stristr($object->getText(), $text) !== false) { return $object; } } }); if (sizeof($arrayExemples) > 0) { $exemples = Exemple::find($this->_getCondition($arrayExemples)); } } } $this->_searchResults($text, $domaines, $rubriques, $exemples); }
public function testStartsWithException() { $this->setExpectedException('\\Phalcon\\Exception'); \Phalcon\Text::startsWith(false, 'he'); }
/** * Returns the available headers in the request * * @return array */ public function getHeaders() { if (is_array($_SERVER) === false) { return; } $result = array(); foreach ($_SERVER as $key => $value) { if (Text::startsWith($key, 'HTTP_') === true) { $result[] = substr($key, 5); } } return $result; }