Esempio n. 1
0
 public function __construct(array $options = null)
 {
     $options = Helper::getOptions($options, ["dir" => "/tmp/query_cache", "sql" => null, "query" => null, "params" => null, "timeout" => self::DAY, "limit" => 10000, "directories" => 3, "permissions" => 0777]);
     $this->sql = $options["sql"];
     # Store the query for other methods to use
     $this->query = $options["query"];
     $this->params = $options["params"];
     # Create the hash of the query to use as an identifier
     $this->hash = sha1($this->query . print_r($this->params, true));
     /**
      * Create the path to the cache directory
      * Adding the number of directories specified in the options
      * This is because most filesystems place a limit on how many links you can have within a directory,
      * so this reduces that problem by spliting the cache directories into subdirectories
      */
     $this->dir = $options["dir"] . "/";
     $directories = [];
     for ($i = 0; $i < $options["directories"]; $i++) {
         if (!($dir = substr($this->hash, $i, 1))) {
             break;
         }
         $this->dir .= $dir . "/";
         $directories[] = $dir;
     }
     $this->dir .= $this->hash;
     $directories[] = $this->hash;
     $this->timeout = $options["timeout"];
     $this->rowLimit = round($options["limit"]);
     # Ensure a cache directory exists for this query
     if (!is_dir($this->dir)) {
         # Ensure the base directory exists
         if (!is_dir($options["dir"])) {
             mkdir($options["dir"], 0777, true);
             chmod($options["dir"], $options["permissions"]);
         }
         /**
          * We can't use the recursive feature of mkdir(), as it's permissions handling is affected by umask().
          * So we create each directory individually and set the permissions using chmod().
          */
         $path = $options["dir"];
         foreach ($directories as $dir) {
             $path .= "/" . $dir;
             if (!is_dir($path)) {
                 mkdir($path);
                 chmod($path, $options["permissions"]);
             }
         }
     }
     # If cache doesn't exist for this query then create it now
     if (!$this->isCached()) {
         $this->createCache();
     }
     $data = Json::decodeFromFile($this->dir . "/.data");
     $this->totalRows = $data["totalRows"];
     $this->columnCount = $data["columnCount"];
     $this->position = 0;
     $this->indexMap = false;
 }
Esempio n. 2
0
 /**
  * Convert the passed variable between array and the serial format (depending on the type passed).
  *
  * {@inheritDoc}
  */
 public static function convert($data, $options = null)
 {
     $options = Helper::getOptions($options, ["cleanup" => false]);
     # If the data is an array the assume we are encoding it
     if (is_array($data)) {
         if ($options["cleanup"]) {
             $data = Helper::cleanupArray($data);
         }
         return static::encode($data);
         # If the data isn't an array then assume we decoding it
     } else {
         $array = static::decode($data);
         if ($options["cleanup"]) {
             $array = Helper::cleanupArray($array);
         }
         return $array;
     }
 }
Esempio n. 3
0
 public static function img($options)
 {
     if (!is_array($options)) {
         $options = array("src" => $options);
     }
     $options = Helper::getOptions($options, ["id" => "", "src" => "", "default" => "", "class" => "", "alt" => "", "title" => "", "getSize" => false]);
     $path = Env::getPath($options["src"]);
     /**
      * If a default image has been specifed then check if the image requested exists
      * If the image doesn't exist, then use the default image instead
      */
     if ($options["default"]) {
         if (!file_exists($path)) {
             $options["src"] = $options["default"];
         }
     }
     /**
      * If no alt text was specified then use any title text that may have been specified
      */
     if (!$options["alt"]) {
         $options["alt"] = $options["title"];
     }
     $img = "<img ";
     $img .= "src='" . $options["src"] . "' ";
     if ($options["id"]) {
         $img .= "id='" . $options["id"] . "' ";
     }
     if ($options["class"]) {
         $img .= "class='" . $options["class"] . "' ";
     }
     if ($options["alt"]) {
         $img .= "alt='" . Html::entities($options["alt"]) . "' ";
     }
     if ($options["title"]) {
         $img .= "title='" . Html::entities($options["title"]) . "' ";
     }
     if ($options["getSize"]) {
         list($width, $height) = getimagesize($path);
         if ($width > 0 && $height > 0) {
             $img .= "style='width:" . $width . "px;height:" . $height . "px;' ";
         }
     }
     $img .= ">";
     return $img;
 }
Esempio n. 4
0
 public function __construct(array $options = null)
 {
     $options = Helper::getOptions($options, ["mode" => "mysql", "hostname" => "", "username" => "", "password" => "", "database" => false, "charset" => "utf8", "timezone" => false, "definitions" => []]);
     $this->options = $options;
     $this->mode = $options["mode"];
     $this->quoteChars = ["mysql" => "`", "postgres" => '"', "redshift" => '"', "odbc" => '"', "sqlite" => "`", "mssql" => ["[", "]"]];
     if (!array_key_exists($this->mode, $this->quoteChars)) {
         throw new \Exception("Unsupported mode (" . $this->mode . ")");
     }
     $this->output = false;
     $this->htmlMode = false;
     # Create the empty triggers array, with each acceptable type
     $this->triggers = [self::TRIGGER_INSERT => [], self::TRIGGER_UPDATE => [], self::TRIGGER_DELETE => []];
     # Don't allow nulls by default
     $this->allowNulls = false;
     # Don't log by default
     $this->log = false;
     $this->logDir = "/tmp/sql-class-logs";
     $this->attached = [];
     $this->tables = [];
     if ($options["definitions"]) {
         $this->definitions($options["definitions"]);
     }
     $this->cacheOptions = [];
     $this->cacheNext = false;
 }