/** * Installs the continents from the data directory. The data directory is defined in the orm.path.continents configuration * @param array $locales Array with locale codes * @return array Array with the continent code as key and the continent data as value */ public function installContinents(array $locales) { $path = $this->getDataPath(); $query = $this->createQuery(0); $continents = $query->query('code'); $transactionStarted = $this->startTransaction(); try { foreach ($locales as $locale) { $file = new File($path, $locale . '.ini'); if (!$file->exists()) { continue; } $continentNames = parse_ini_file($file->getPath(), false, INI_SCANNER_RAW); foreach ($continentNames as $continentCode => $continentName) { if (array_key_exists($continentCode, $continents)) { $continent = $continents[$continentCode]; } else { $continent = $this->createData(); $continent->code = $continentCode; $continents[$continentCode] = $continent; } $continent->name = $continentName; $continent->dataLocale = $locale; $this->save($continent); } } $this->commitTransaction($transactionStarted); } catch (Exception $e) { $this->rollbackTransaction($transactionStarted); throw $e; } return $continents; }
/** * Installs the countries from the data directory. The data directory is defined in the orm.path.countries configuration * @param array $locales Array with locale codes * @param array $continents Array with the installed continents * @return null */ public function installCountries(array $locales, array $continents) { $path = $this->getDataPath(); $continentCountryCodes = $this->getContinentCountryCodes(); $query = $this->createQuery(0); $countries = $query->query('code'); $transactionStarted = $this->startTransaction(); try { foreach ($locales as $locale) { $file = new File($path, $locale . '.ini'); if (!$file->exists()) { continue; } $countryNames = $this->readCountries($file); foreach ($countryNames as $countryCode => $countryName) { if (array_key_exists($countryCode, $countries)) { $country = $countries[$countryCode]; } else { $country = $this->createData(); $country->code = $countryCode; $country->continent = $this->getContinentForCountry($countryCode, $continents, $continentCountryCodes); $countries[$countryCode] = $country; } $country->name = $countryName; $country->dataLocale = $locale; $this->save($country); } } $this->commitTransaction($transactionStarted); } catch (Exception $e) { $this->rollbackTransaction($transactionStarted); throw $e; } }
/** * Constructs a new file view * @param zibo\library\filesystem\File $file File to render * @return null */ public function __construct(File $file) { if (!$file->exists() || $file->isDirectory()) { throw new ZiboException($file . ' does not exists or is a directory.'); } $this->file = $file; }
public function testClearCache() { $this->setUpWriteCache(); $type = 'type'; $values = array('id1' => 'value1', 'id2' => 'value2'); foreach ($values as $id => $value) { $object = new CacheObject($type, $id, $value); $this->cache->writeToCache($object); } $cachePath = new File($this->path, $type); $cacheFile1 = new File($cachePath, 'id1'); $cacheFile2 = new File($cachePath, 'id2'); $this->assertEquals(true, $cacheFile1->exists()); $this->assertEquals(true, $cacheFile2->exists()); $this->cache->clearCache($type, 'id1'); $this->assertEquals(false, $cacheFile1->exists()); $this->assertEquals(true, $cacheFile2->exists()); $this->cache->clearCache($type); $this->assertEquals(false, $cachePath->exists()); $this->tearDownCache(); }
/** * Read the modules from the provided path * @param zibo\library\filesystem\File $path Path to read the module definitions from * @return array Array with Module instances * @throws zibo\admin\model\exception\ModuleDefinitionNotFoundException when no module definition could be read from the provided path */ public function readModules(File $path) { $xmlFile = new File($path, self::MODULE_FILE); if (!$xmlFile->exists()) { throw new ModuleDefinitionNotFoundException($path); } $dom = new Document('1.0', 'utf-8'); $dom->preserveWhiteSpace = false; $dom->setSchemaFileFromConfig(self::CONFIG_MODULES_SCHEMA); $dom->load($xmlFile->getPath()); return $this->readModulesFromElement($dom->documentElement, self::TAG_MODULE); }
/** * Reads the cache objects from the cache file * @return null */ private function readFile() { if ($this->cache !== null) { return; } if (!$this->file->exists()) { $this->cache = array(); return; } $serializedValue = $this->file->read(); $this->cache = unserialize($serializedValue); }
/** * Finished the installation, remove the installation module and redirect * @return null */ public function finish() { $installModule = new File(__DIR__ . '/../../../../'); $installModule->delete(); $installScript = new File($installModule->getParent()->getParent(), 'install.php'); if ($installScript->exists() && $installScript->isWritable()) { $installScript->delete(); } $zibo = Zibo::getInstance(); $request = $zibo->getRequest(); $response = $zibo->getResponse(); $response->setRedirect($request->getBaseUrl()); }
/** * Connects this connection * @return null * @throws zibo\library\database\mysql\exception\MysqlException when no connection could be made with the host * @throws zibo\library\database\mysql\exception\MysqlException when the database could not be selected */ public function connect() { $file = new File($this->dsn->getPath()); if (!$file->exists()) { $parent = $file->getParent(); $parent->create(); } try { $this->connection = new SQLite3($file->getAbsolutePath()); } catch (Exception $exception) { $this->connection = null; throw new SqliteException($exception->getMessage(), 0, $exception); } }
public function read(File $file) { if (!$file->exists()) { return false; } $ini = $file->read(); $ini = parse_ini_string($ini); if (array_key_exists('current', $ini)) { $this->current = $ini['current']; } $this->visited = $ini['visited']; $this->gathered = $ini['gathered']; $this->start = $ini['start']; $this->stop = $ini['stop']; }
public function testSetTranslationWhenTranslationFileExists() { $localeCode = 'locale'; $key = 'translation5'; $translation = 'Translation 5'; $this->io->setTranslation($localeCode, $key, $translation); $fileName = Zibo::DIRECTORY_APPLICATION . File::DIRECTORY_SEPARATOR . Zibo::DIRECTORY_L10N . File::DIRECTORY_SEPARATOR . $localeCode . IniTranslationIO::EXTENSION; $translationFile = new File($fileName); $this->assertTrue($translationFile->exists(), 'No file is being written'); $iniContent = $translationFile->read(); $translations = parse_ini_string($iniContent, false); $this->assertEquals(5, count($translations)); $this->assertTrue(array_key_exists($key, $translations)); $this->assertEquals($translation, $translations[$key]); }
/** * Decorates the cell with the path of the file * @param zibo\library\html\table\Cell $cell Cell to decorate * @param zibo\library\html\table\Row $row Row of the cell to decorate * @param integer $rowNumber Number of the current row * @param array $remainingValues Array containing the values of the remaining rows of the table * @return null */ public function decorate(Cell $cell, Row $row, $rowNumber, array $remainingValues) { $file = $cell->getValue(); $absoluteFile = new File($this->root, $file); if (!$absoluteFile->exists()) { $cell->setValue('---'); return; } if ($absoluteFile->isDirectory()) { $class = self::CLASS_DIRECTORY; } else { $class = self::CLASS_FILE; } $html = $this->getNameHtml($file->getPath(), $class); $cell->setValue($html); }
/** * Gets the dependency container * @param zibo\core\Zibo $zibo Instance of zibo * @return zibo\core\di\DependencyContainer */ public function getContainer(Zibo $zibo) { $file = new File(self::FILE); if ($file->exists()) { require $file; } if (isset($container)) { return $container; } $container = $this->io->getContainer($zibo); $parent = $file->getParent(); $parent->create(); $php = $this->generatePhp($container, $file); $file->write($php); return $container; }
/** * Gets all available locales * @return array all Locale objects */ public function getLocales() { $file = new File(self::FILE); if ($file->exists()) { require $file; } if (isset($locales)) { return $locales; } $locales = $this->io->getLocales(); $parent = $file->getParent(); $parent->create(); $php = $this->generatePhp($locales); $file->write($php); return $locales; }
/** * Compresses a file into the archive * @param \Phar $archive Phar object of PHP * @param zibo\library\filesystem\File $file The file to compress in the archive * @param zibo\library\filesystem\File $prefix The path for the file in the archive * @return null */ private function compressFile(PhpPhar $archive, File $file, File $prefix = null) { if ($prefix == null) { $prefix = new File($file->getName()); } else { $prefix = new File($prefix, $file->getName()); } if ($file->exists()) { if ($file->isDirectory()) { $this->compressDirectory($archive, $file, $prefix); } else { $archive->addFile($file->getAbsolutePath(), $prefix->getPath()); } } else { throw new ArchiveException("Source does not exist: {$file}"); } }
/** * Checks if the application directory exists and is writable * @return null */ public function performCheck() { $root = Zibo::getInstance()->getRootPath(); $directory = new File($root, $this->path); $this->isMet = false; if (!$directory->exists()) { try { $directory->create(); } catch (ZiboException $exception) { $this->message = $this->messageExists; return; } } if ($this->messageWritable && !$directory->isWritable()) { $this->message = $this->messageWritable; return; } $this->isMet = true; }
/** * Sets a translation for the provided locale * @param string $localeCode Code of the locale * @param string $key Key of the translation * @param string $translation * @return null * @throws zibo\ZiboException when the locale code is empty or invalid * @throws zibo\ZiboException when the translation key is empty or invalid * @throws zibo\ZiboException when the translation is empty or invalid */ public function setTranslation($localeCode, $key, $translation) { if (!String::isString($localeCode, String::NOT_EMPTY)) { throw new ZiboException('Provided locale code is empty'); } if (!String::isString($key, String::NOT_EMPTY)) { throw new ZiboException('Provided translation key is empty'); } if (!String::isString($translation, String::NOT_EMPTY)) { throw new ZiboException('Provided translation is empty'); } $translationFile = new File(Zibo::DIRECTORY_APPLICATION . File::DIRECTORY_SEPARATOR . Zibo::DIRECTORY_L10N, $localeCode . self::EXTENSION); if ($translationFile->exists()) { $translations = $this->getTranslationsFromFiles(array($translationFile)); } else { $translations = array(); } $translations[$key] = $translation; $this->setTranslationsToFile($translationFile, $translations); }
/** * Constructs a new file view * @param zibo\library\filesystem\File $file File to render * @return null */ public function __construct(File $file) { $response = Zibo::getInstance()->getResponse(); if (!$file->exists() || $file->isDirectory()) { $response->setStatusCode(Response::STATUS_CODE_NOT_FOUND); return; } $maxAge = 60 * 60; $mime = Mime::getMimeType($file); $lastModified = gmdate('r', $file->getModificationTime()); $expires = gmdate('r', time() + $maxAge); $response->setHeader('Pragma', 'public'); $response->setHeader('Cache-Control', 'max-age=' . $maxAge); $response->setHeader('Expires', $expires); $response->setHeader('Last-Modified', $lastModified); $response->setHeader('ETag', md5($lastModified)); $response->setHeader('Content-Type', $mime); $response->setHeader('Content-Length', $file->getSize()); $this->file = $file; }
/** * Look for files by looping through the include paths * @param string $fileName relative path of a file in the Zibo filesystem structure * @param boolean $firstOnly true to get the first matched file, false to get an array * with all the matched files * @return zibo\library\filesystem\File|array Depending on the firstOnly flag, an instance or an array of zibo\library\filesystem\File * @throws zibo\ZiboException when fileName is empty or not a string */ protected function lookupFile($fileName, $firstOnly) { if (!$fileName instanceof File && !String::isString($fileName, String::NOT_EMPTY)) { throw new FileSystemException('Provided filename is empty'); } $files = array(); $includePaths = $this->getIncludePaths(); foreach ($includePaths as $includePath) { $file = new File($includePath, $fileName); if (!$file->exists()) { continue; } if ($firstOnly) { return $file; } $files[$file->getPath()] = $file; } if ($firstOnly) { return null; } ksort($files); return $files; }
/** * Reads from the filesystem with extra options * @param zibo\library\filesystem\File $path The path of the directory to read * @param array $filters Array with filters * @param boolean $recursive Set to true to read recursivly * @return array Array with the directories and files of the provided path */ public function readDirectory(File $path = null, array $filters = array(), $recursive = false) { $path = new File($this->root, $path); if (!$path->exists()) { throw new FileSystemException($path->getPath() . ' does not exist'); } if (!$path->isDirectory()) { throw new FileSystemException($path->getPath() . ' is not a directory'); } if (!$path->isReadable()) { throw new FileSystemException($path->getPath() . ' is not readable'); } $paths = array(); $files = $path->read($recursive); foreach ($files as $file) { $path = $this->getPath($file, false); $paths[$path->getPath()] = $path; } if ($filters) { $paths = $this->applyFilters($paths, $filters); } return $paths; }
/** * Imports a SQL file on this connection * @param zibo\library\filesystem\File $file SQL file * @return null */ public function import(File $file) { if (!$file->exists()) { throw new DatabaseException('Provided file does not exist'); } $content = $file->read(); $content = preg_replace('#/\\*.*?\\*/#s', '', $content); $sqls = array(); $sql = ''; $lines = explode("\n", $content); foreach ($lines as $line) { $line = trim($line); if (empty($line) || substr($line, 0, 2) == '--') { continue; } $sql .= $line; if (substr($sql, -1, 1) == ';') { $sql = substr($sql, 0, -1); $sqls[] = $sql; $sql = ''; } } $sql[] = $sql; $transactionStarted = $this->startTransaction(); try { foreach ($sqls as $sql) { $this->execute($sql); } if ($transactionStarted) { $this->commitTransaction(); } } catch (Exception $exception) { if ($transactionStarted) { $this->rollbackTransaction(); } } }
/** * Compresses a file into the archive * @param ZipArchive $archive ZipArchive object of PHP * @param zibo\library\filesystem\File $file The file to compress in the archive * @param zibo\library\filesystem\File $prefix The path for the file in the archive * @return null */ private function compressFile(ZipArchive $archive, File $file, File $prefix = null) { if ($prefix == null) { $prefix = new File($file->getName()); } else { $prefix = new File($prefix, $file->getName()); } $children = null; if ($file->exists()) { if ($file->isDirectory()) { $children = $file->read(); } else { $archive->addFile($file->getPath(), $prefix->getPath()); return; } } if (empty($children)) { $archive->addEmptyDir($prefix->getPath()); } else { foreach ($children as $file) { $this->compressFile($archive, $file, $prefix); } } }
/** * Uploads a file to the FTP server * @param zibo\library\filesystem\File $localFile The file to upload * @param string $remoteFile The destination file on the FTP server * @param integer $mode ASCII or binary (constants FTP_ASCII or FTP_BINARY) * @return null * @throws zibo\library\ftp\exception\FtpException when not connected to the FTP server * @throws zibo\library\ftp\exception\FtpException when the file could not be uploaded */ public function put(File $localFile, $remoteFile, $mode = null) { $this->checkConnected(); if (!$localFile->exists()) { throw new FtpException('Could not upload ' . $localFile->getName() . ': Source does not exist'); } elseif ($localFile->isDirectory()) { throw new FtpException('Could not upload ' . $localFile->getName() . ': Source is a directory'); } if (!$mode) { $mode = FTP_BINARY; } if (!@ftp_put($this->handle, $remoteFile, $localFile->getAbsolutePath(), $mode)) { throw new FtpException('Could not upload ' . $localFile->getName() . ': A problem occured while uploading'); } }
/** * Reads the cache object from a file * @param zibo\library\filesystem\File $cacheFile File of the cache object * @return zibo\library\cache\CacheObject|null The read cache object or null */ private function readCacheObject(File $cacheFile) { if (!$cacheFile->exists()) { return null; } try { if ($this->useLock) { $cacheFile->waitForUnlock(); } $serializedValue = $cacheFile->read(); $object = unserialize($serializedValue); } catch (Exception $exception) { Zibo::getInstance()->runEvent(Zibo::EVENT_LOG, $exception->getMessage(), $exception->getTraceAsString(), 1); $object = null; } if (!$object) { return null; } return $object; }
/** * Write the model definitions of the provided models to the provided model definition file * @param zibo\library\filesystem\File $file * @param array $models models to write to file * @return null */ public function writeModelsToFile(File $file, array $models) { if (!$models) { if ($file->exists()) { $file->delete(); } return; } $dom = new Document('1.0', 'utf-8'); $dom->formatOutput = true; $modelsElement = $dom->createElement(self::TAG_ROOT); $dom->appendChild($modelsElement); foreach ($models as $model) { $modelElement = $this->getElementFromModel($dom, $model); if ($modelElement != null) { $importedModelElement = $dom->importNode($modelElement, true); $modelsElement->appendChild($importedModelElement); } } $dom->save($file); }
/** * Action to set files and directories to the clipboard * @param array $files Array with relative paths * @return null */ public function clipboardAction(array $files = null) { $this->response->setRedirect($this->getReferer()); if ($files == null) { return; } foreach ($files as $file) { $file = new File($this->fileBrowser->getRoot(), $file); if (!$file->exists()) { continue; } $file = $this->fileBrowser->getPath($file, false); $this->clipboard[$file->getPath()] = $file; } }
/** * Starts the crawling * @param integer $delay Delay between each page in miliseconds * @param zibo\library\filesystem\File $statusFile File where the status of the crawling process is written * @param zibo\library\filesystem\File $cancelFile File which will cancel/stop the crawling process when exists * @return null */ public function crawl($delay = 1000, File $statusFile = null, File $cancelFile = null) { $prey = $this->web->resetPrey(); $start = time(); $index = 0; $isCancelled = false; while ($prey) { if ($cancelFile && $cancelFile->exists()) { $cancelFile->delete(); $isCancelled = true; break; } usleep($delay * 1000); $index++; $url = $prey->getUrl(); if ($this->shouldIgnore($url)) { $prey->addType(WebNode::TYPE_IGNORED); $prey = $this->web->getNextPrey(); continue; } if ($statusFile) { $status = new SpiderStatus($url, $index, $this->web->countNodes(), $start); $status->write($statusFile); } if (String::startsWith($url, 'mailto:')) { $prey->addType(WebNode::TYPE_MAILTO); $prey = $this->web->getNextPrey(); continue; } try { $crawl = new Crawl($url); $crawl->performCrawl(); $response = $crawl->getResponse(); $prey->setResponse($response); if ($response->isRedirect()) { $location = $response->getHeader('Location'); if (!String::looksLikeUrl($location)) { if ($location[0] == '/') { $base = $crawl->getBaseUrl(); } else { $base = $crawl->getBasePath(); } $location = rtrim($base, '/') . '/' . ltrim($location, '/'); } if ($url == $location) { throw new Exception('Redirect loop'); } $locationNode = $this->web->getNode($location); $locationNode->addReference($prey); $prey->addLink($locationNode); } if (!String::startsWith($url, $this->baseUrl)) { $prey->addType(WebNode::TYPE_EXTERNAL); if (!$this->willBiteExternalNodes) { $prey = $this->web->getNextPrey(); continue; } } $this->bite($prey, $crawl->getBaseUrl(), $crawl->getBasePath()); } catch (Exception $exception) { if ($crawl) { $response = $crawl->getResponse(); if ($response) { $prey->setResponse($response); } } $prey->setError($exception->getMessage()); } $prey = $this->web->getNextPrey(); } if (!$isCancelled) { if ($statusFile) { $status = new SpiderStatus("reports", $index, $this->web->countNodes(), $start); $status->write($statusFile); } foreach ($this->reports as $report) { $report->setWeb($this->web); } } if ($statusFile) { $status = new SpiderStatus(null, $index, $this->web->countNodes(), $start, time()); $status->write($statusFile); } }
/** * Get the names of the sections in the provided directory * @param zibo\library\filesystem\File $directory * @return array Array with the file names of all the ini files, withouth the extension */ private function getDirectorySections(File $directory) { $sections = array(); if (!$directory->exists()) { return $sections; } $files = $directory->read(); foreach ($files as $file) { if ($file->isDirectory() || $file->getExtension() != 'ini') { continue; } $sectionName = substr($file->getName(), 0, -4); $sections[$sectionName] = $sectionName; } return $sections; }
public function reportAction($id, $url = null) { $spider = null; $url = null; if (isset($_GET['url'])) { $url = $_GET['url']; } $fileSpider = new File(self::PATH_DATA, $id . self::SUFFIX_SPIDER); if ($fileSpider->exists()) { $fileSpiderContent = $fileSpider->read(); $spider = unserialize($fileSpiderContent); } if ($url) { $url = urldecode($url); if (!$spider) { $node = new WebNode($url); } else { $web = $spider->getWeb(); $node = $web->getNode($url); } $view = new ReportDetailView($node); $this->response->setView($view); return; } if ($spider) { $web = $spider->getWeb(); $reports = $spider->getReports(); foreach ($reports as $report) { $report->setWeb($web); } } else { $reports = array(); } $view = new ReportView($reports); $this->response->setView($view); }
/** * @expectedException zibo\library\archive\exception\ArchiveException */ public function testUncompressWithNonPharPathThrowsException() { $phar = new Phar($this->nonPharFile); $uncompressDirectory = new File('/tmp/zibo/'); if ($uncompressDirectory->exists()) { $uncompressDirectory->delete(); } $phar->uncompress($uncompressDirectory); }
/** * Reads the cache object from a file * @param zibo\library\filesystem\File $cacheFile File of the cache object * @return zibo\library\cache\CacheObject|null The read cache object or null */ private function readCacheObject(File $cacheFile) { if (!$cacheFile->exists()) { return null; } try { if ($this->useLock) { $cacheFile->waitForUnlock(); } $serializedValue = $cacheFile->read(); $object = unserialize($serializedValue); } catch (Exception $exception) { $object = null; } if (!$object) { return null; } return $object; }