addEntity (self::ENTITY_DICTIONARY, 'dictionaries.json'); $this->addEntity (self::ENTITY_TEMPLATE, 'templates.json'); $this->addEntity (self::ENTITY_LOCALE, 'locales.json'); $this->setDecompressor (new Compression_LZW ()); } protected function getWorkingDir ($file) { return DATADIR . '/tmp/' . $file; } protected function getUpdateUrl () { $result = Kohana::config ('core.update_server_url') or $result = 'http://downloads.templatemonster.com/json/'; return $result; } function download ($entity_name, $force = false) { $entity = $this->getEntity ($entity_name); $local = $file = $this->getWorkingDir ($entity); $url = $this->getUpdateUrl() . $entity; if ($this->hasDecompressor ()) { $url = $this->getDecompressor ()->getCompressedFilename ($url); $local = $this->getDecompressor ()->getCompressedFilename ($local); } if ($force && file_exists ($local)) { return $file; } $this->downloadFile ($url, $local); if ($this->hasDecompressor ()) { $this->getDecompressor ()->decompress ($local); $decompressed_file = new File ($this->getDecompressor ()->getDecompressedFilename ($local)); $file_size = new String ($decompressed_file->getFileSize ()); $this->notice (sprintf ('Decompressed: %s bytes (%s).', (string) $file_size, (string) $file_size->humanFileSize ())); } return $file; } private function isFileUptoDate ($filename) { if (file_exists ($filename)) { $stat = stat ($filename); $hours_to_live = 5; return (time () - $stat['ctime']) < $hours_to_live * 60 * 60; } } private function downloadFile ($url, $file) { $this->notice ("Downloading $url"); $remote_file = new File ($url); $remote_file->open (File::OPEN_MODE_READ); $remote_file->setBufferSize (512 * 1024); $local_file = new File ($file); $local_file->open (File::OPEN_MODE_WRITE | File::OPEN_MODE_UNLINK_BEFORE_OPEN); while (strlen ($string = $remote_file->read ())) { $local_file->write ($string); } $remote_file->close (); $local_file->close (); $file_size = new String ($local_file->getFileSize ()); $this->notice (sprintf ("Downloaded: %s bytes (%s).", $local_file->getFileSize (), (string) $file_size->humanFileSize ())); } private function gunzipFile ($gzip_file_name) { $this->notice ("Decompressing $gzip_file_name"); $compressed_file = new File ($gzip_file_name); $compressed_file->setWrapper ('compress.zlib://'); $compressed_file->setBufferSize (2 * 1024 * 1024); $compressed_file->open (File::OPEN_MODE_READ); $file = substr ($gzip_file_name, 0, -3); $decompressed_file = new File ($file); $decompressed_file->open (File::OPEN_MODE_WRITE | File::OPEN_MODE_UNLINK_BEFORE_OPEN); while (strlen ($string = $compressed_file->read ())) { $decompressed_file->write ($string); } $decompressed_file->close (); $compressed_file->delete (); $file_size = new String ($decompressed_file->getFileSize ()); $this->notice (sprintf ('%s bytes (%s) decompressed.', $decompressed_file->getFileSize (), (string) $file_size->humanFileSize ())); } function addEntity ($name, $entity) { $this->entities[$name] = $entity; } function getEntity ($name) { return $this->entities[$name]; } function logger (Zend_Log $logger = null) { if (isset ($logger)) { $this->logger = $logger; } else { return $this->logger; } } function __call ($name, $arguments) { $logger = $this->logger (); if (isset ($logger)) { return call_user_func_array (array ($logger, $name), $arguments); } } function setDecompressor (Compression_Decompressor $decompressor) { $this->decompressor = $decompressor; } function getDecompressor () { return $this->decompressor; } function hasDecompressor () { return isset($this->decompressor); } }