/** * Get the message of a status code * @param string $code The status code * @return string */ public static function get_message($code) { $code = (int) $code; if (array_key_exists($code, self::$status_code_messages)) { $message = self::$status_code_messages[$code]; } else { $message = sprintf(self::ERROR_UNKNOWN_STATUS_CODE, $code); Error::set($message, false); } return $message; }
/** * Gets the iteration number used for bcrypt hashing * @param int $iteration_number * @return int */ private static function get_iteration_number($iteration_number) { // get the iteration number if set if (!empty($iteration_number)) { $iteration_number = (int) $iteration_number; if (4 > $iteration_number || 31 < $iteration_number) { Error::set(self::ERROR_INVALID_ITERATION_NUMBER); } } else { $iteration_number = self::DEFAULT_ITERATION_NUMBER; } return $iteration_number; }
/** * Get an instance of the required cache system * @param string $type the cache type * @param array $options the config options set for the cache system * @return ICache */ public static function get_instance($type = null, array $options = []) { if (empty($options)) { $options = App::get_instance()->get_config()->cache; } // return the null object if cache is not enabled if (empty($options[self::ENABLED])) { return new Void($options); } switch ($type) { case self::DEFAULT_KEY: case '': case null: if (isset($options[self::DEFAULT_KEY])) { return self::get_instance($options[self::DEFAULT_KEY], $options); } else { Error::set(self::ERROR_DEFAULT_NOT_SET, false); } break; case self::APC: return new Apc($options); break; case self::FILE: return new File($options); break; case self::MEMCACHED: return new Memcached($options); break; case self::REDIS: return new Redis($options); break; case self::VOID: return new Void($options); break; default: Error::set(sprintf(self::ERROR_INVALID_CACHE_SYSTEM, $type)); break; } }
/** * Construct * @param array $options the config options */ public function __construct(array $options = []) { if (!extension_loaded(self::APC_EXTENSION)) { Error::set(self::ERROR_NO_APC); } }
/** * Prepares query limit * @param int $limit * @param int $start * @return string */ public function prepare_limit(array &$binds, $limit = 0, $start = 0) { if ($start >= self::MAX_INTEGER_VALUE or $limit >= self::MAX_INTEGER_VALUE) { Error::set(self::ERROR_LIMIT); } if (!empty($limit)) { $bind_key_start = ':start'; $bind_key_limit = ':limit'; $binds[$bind_key_start] = $start; $binds[$bind_key_limit] = $limit; } $result = empty($limit) ? '' : ' LIMIT ' . $bind_key_start . ', ' . $bind_key_limit; return $result; }
/** * Set the model adapter * @return mixed */ private function set_model_adapter() { // get the database model instance switch ($this->db_engine) { case 'mysql': $this->adapter = new Mysql(); break; case 'mongo': $this->adapter = null; break; default: Error::set(sprintf(self::ERROR_INVALID_DB_MODEL, $this->db_engine)); break; } }
/** * Gets the Database instance * @param string $db_engine The database engine * @return mixed */ public static function get_instance($db_engine) { Error::set('Not implemented'); }
/** * Sets the redis data serializer * * @param int $serializer */ private function set_serializer($serializer) { if (!in_array($serializer, array_keys($this->serializers))) { Error::set(sprintf(self::ERROR_INVALID_SERIALIZER, $serializer)); } $this->setOption(Redis::OPT_SERIALIZER, $this->serializers[$serializer]); return $this; }
/** * Parse a "in" and "not in" operators * @param string $operator * @param string $key * @param mixed $value * @param array $binds */ private function parse_in_operator($operator, $key, $value, &$binds) { $values = []; if (!is_array($value)) { Error::set(sprintf(self::ERROR_QUERY, '"$in" operator expects an array as value')); } // parse and bind every value of the "in" filter foreach ($value as $v) { if (isset($binds)) { $values[] = ':' . $this->i; $binds[':' . $this->i] = $v; $this->i++; } else { $values[] = $v; } } return $key . ' ' . sprintf($operator, implode(',', $values)); }
/** * Sets the cache path * @param string $folder_path * @return ICache */ public function set_folder_path($folder_path) { $folder_path = (string) $folder_path; // if the folder path doesn't exists, try to create it if (!is_dir($folder_path)) { if (@(!mkdir($folder_path, 0755, true))) { Error::set(sprintf(self::ERROR_FOLDER_NOT_EXISTS, $folder_path)); } } // make sure the path is writable if (!is_writeable($folder_path)) { Error::set(sprintf(self::ERROR_FOLDER_PERMISSION, $folder_path)); } $this->folder_path = $folder_path; return $this; }
/** * Writes and image in a desired destination * @param Imagick $image The Imagick resource * @param string $destination The destination * @return boolean */ private function save_image(Imagick $image, $destination) { // saves the image to disk $image_path = dirname($destination); if (!is_writable($image_path)) { Error::set(sprintf(self::ERROR_CANNOT_WRITE_FILE, $image_path)); } return $image->writeImage($destination); }
/** * Uploads all files to the destination folder * @param string $folder * @param string $name * @return $boolean */ public function upload_all($folder, $name = '') { // make sure the file input exists if (empty($_FILES)) { Error::set(self::ERROR_NO_FILES); } // stores whether all files were uploaded successfully or not $all_succeed = true; // set name key if required if (!empty($name) && count($_FILES) > 1) { $this->name_key = 1; } // upload all files foreach ($_FILES as $input => $file) { if (!$this->upload($input, $folder, $name)) { $all_succeed = false; } } return $all_succeed; }
/** * Outputs json content * @param mixed $content */ public function json_output($content) { if (isset($this->view['view']) && $this->auto_display) { Error::set(self::ERROR_AUTO_DISPLAY_INCOMPATIBLE); } header('Content-Type: application/json'); echo json_encode($content); }
/** * Sets the model name and table used * * @param string $model * * @return Model */ public function set_model($model) { // set the model $model = (string) $model; $this->model = $model; // set the table for this model if (!defined($model . '::TABLE')) { Error::set(sprintf(self::ERROR_NO_TABLE, $model)); } $table = constant($model . '::TABLE'); $this->table($table); return $this; }
/** * Gets the strings paths for the current language * @param string $language * @return array */ private static function get_strings_paths($language) { // set the strings paths using if exists the array of l10n paths else the default application path $strings_paths = empty(self::$l10n_paths) ? [App::get_instance()->get_l10n_folder()] : self::$l10n_paths; foreach ($strings_paths as &$strings_path) { // add file name to the string path $strings_path .= $language . '.ini'; // validate the string path if (!is_readable($strings_path)) { Error::set(sprintf(self::ERROR_INVALID_STRINGS_PATH, $strings_path)); } } return $strings_paths; }
/** * Add javascripts to the html type templates * @access private * @param string @template */ private function add_scripts($template) { // add lazy scripts if (!empty($this->lazy_scripts)) { $lazy_scripts = implode(' ', $this->lazy_scripts); $this->scripts[] = sprintf(self::LAZY_LOAD_SCRIPT, $lazy_scripts); } // include scripts if (!empty($this->scripts)) { // set the scripts as text $scripts = implode(' ', $this->scripts); // and now add the scripts at the end if (strpos($this->parsed_blocks[$template], '</body>') > 0) { $this->parsed_blocks[$template] = str_replace('</body>', $scripts . '</body>', $this->parsed_blocks[$template]); } else { Error::set(sprintf(self::ERROR_INVALID_HTML_BODY, 'scripts'), Error::WARNING); } } }
/** * Binds values using PDO prepare statements * @param PDOStatement $statement * @param array $binds */ public function bind_values(PDOStatement &$statement, array $binds) { foreach ($binds as $key => $bind) { switch (true) { case is_int($bind): $type = PdoDriver::PARAM_INT; break; case is_bool($bind): $type = PdoDriver::PARAM_BOOL; break; case is_null($bind): $type = PdoDriver::PARAM_NULL; break; case is_object($bind): case is_array($bind): Error::set(sprintf(self::ERROR_INVALID_BIND_VALUE, print_r($bind, true))); default: $type = PdoDriver::PARAM_STR; break; } $statement->bindValue($key, $bind, $type); } }
/** * Optimize the Solr index */ public function optimize() { $connection = $this->get_connection(); try { $response = $connection->optimize(); } catch (SolrClientException $e) { Error::set(sprintf(self::ERROR_SOLR_CLIENT, $e->getMessage())); } return $response; }
/** * Sets the global config values * @return Config */ private function set_global_config() { $path = $this->get_config_path(); if (!is_readable($path)) { Error::set(sprintf(self::ERROR_CONFIG_PATH, $path)); } $this->config = parse_ini_file($path); return $this; }
/** * Executes an operation * * @param array $options The executions options * * @return mixed */ public function execute(array $options) { if (!empty($options['debug'])) { var_dump($options); } $connection = $this->get_connection(); $db_name = !empty($options['query']['database']) ? $options['query']['database'] : $this->database; if (empty($options['query']['table'])) { Error::set(self::ERROR_NO_COLLECTION); } $collection_name = !empty($options['query']['prefix']) ? $options['query']['prefix'] . $options['query']['table'] : $options['query']['table']; $collection = $connection->{$db_name}->{$collection_name}; return $collection; }
/** * Gets the controller instance * @return Controller */ private function get_controller_instance() { $default_path = 'Controller\\' . str_replace(DIRECTORY_SEPARATOR, '\\', $this->controller); $module = App::get_instance()->get_module(); if (isset($module)) { $module_path = 'Module\\' . ucfirst(strtolower($module)) . '\\' . $default_path; if (class_exists($module_path)) { return new $module_path(); } } if (!class_exists($default_path)) { Error::set(sprintf(self::ERROR_NO_CONTROLLER_FILE, $this->controller, $default_path)); } return new $default_path(); }
/** * Convert HTML/CSS to HTML inline style * @return string */ public function convert() { if (empty($this->html)) { Error::set(self::ERROR_NO_HTML); } // process css $this->process_css(); // create new DOMDocument $document = new DOMDocument('1.0', $this->get_encoding()); // set error level libxml_use_internal_errors(true); // load HTML and create new XPath $document->loadHTML($this->html); $xpath = new DOMXPath($document); if (!empty($this->css_rules)) { // apply every rule foreach ($this->css_rules as $rule) { // get query $query = $this->build_xpath_query($rule['selector']); if (empty($query)) { continue; } // search elements $elements = $xpath->query($query); if (empty($elements)) { continue; } // loop found elements foreach ($elements as $element) { $this->set_original_styles($element); // get the properties $styles_attribute = $element->attributes->getNamedItem('style'); $properties = $this->get_properties($styles_attribute); // set the properties $this->set_properties($rule['properties'], $properties, $element); } } // reapply original styles $query = $this->build_xpath_query('*[@' . self::ORIGINAL_STYLES . ']'); if (false === $query) { return; } // search elements $elements = $xpath->query($query); // loop found elements foreach ($elements as $element) { // get the original styles $original_style = $element->attributes->getNamedItem(self::ORIGINAL_STYLES); $original_properties = $this->get_properties($original_style); if (!empty($original_properties)) { // get current styles $styles_attribute = $element->attributes->getNamedItem('style'); $properties = $this->get_properties($styles_attribute); // set the properties $this->set_properties($original_properties, $properties, $element); } // remove placeholder $element->removeAttribute(self::ORIGINAL_STYLES); } } // get the HTML $html = $document->saveHTML(); // cleanup the HTML if we need to if ($this->cleanup) { $html = $this->cleanup_html($html); } return $html; }