/** * Prepares a new stream. * * @param string $path Virtual path which should describe the content (e.g. "template/1"), only relevant for error messages * @param string $content Content which will be included * * @throws InvalidArgumentException * * @return string Full path with protocol (e.g. "rex://template/1") */ public static function factory($path, $content) { if (!is_string($path) || empty($path)) { throw new InvalidArgumentException('Expecting $path to be a string and not empty!'); } if (!is_string($content)) { throw new InvalidArgumentException('Expecting $content to be a string!'); } if (null === self::$useRealFiles) { self::$useRealFiles = extension_loaded('suhosin') && !preg_match('/(?:^|,)rex(?::|,|$)/', ini_get('suhosin.executor.include.whitelist')); } if (self::$useRealFiles) { $hash = substr(sha1($content), 0, 7); $path = rex_path::coreCache('stream/' . $path . '/' . $hash); if (!file_exists($path)) { rex_file::put($path, $content); } return $path; } if (!self::$registered) { stream_wrapper_register('rex', __CLASS__); self::$registered = true; } $path = 'rex://' . $path; self::$nextContent[$path] = $content; return $path; }
/** * Prepares a new stream. * * @param string $path Virtual path which should describe the content (e.g. "template/1"), only relevant for error messages * @param string $content Content which will be included * * @throws InvalidArgumentException * * @return string Full path with protocol (e.g. "rex:///template/1") */ public static function factory($path, $content) { if (!is_string($path) || empty($path)) { throw new InvalidArgumentException('Expecting $path to be a string and not empty!'); } if (!is_string($content)) { throw new InvalidArgumentException('Expecting $content to be a string!'); } if (null === self::$useRealFiles) { self::$useRealFiles = extension_loaded('suhosin') && !preg_match('/(?:^|,)rex(?::|,|$)/', ini_get('suhosin.executor.include.whitelist')); } if (self::$useRealFiles) { $hash = substr(sha1($content), 0, 7); $path = rex_path::coreCache('stream/' . $path . '/' . $hash); if (!file_exists($path)) { rex_file::put($path, $content); } return $path; } if (!self::$registered) { stream_wrapper_register('rex', __CLASS__); self::$registered = true; } // 3 slashes needed to sidestep some server url include protections // example: https://www.strato.de/faq/article/622/Warum-erhalte-ich-über-PHP-die-Fehlermeldung-%22Warning:-main()-…:-include(….).html $path = 'rex:///' . $path; self::$nextContent[$path] = $content; return $path; }
private function updateVersion() { $file = rex_path::core('boot.php'); $content = rex_file::get($file); $content = preg_replace('/(?<=^rex::setProperty\\(\'version\', \').*?(?=\')/m', $this->version, $content); rex_file::put($file, $content); }
public function testDelete() { $path = $this->getPath('delete.log'); $path2 = $path . '.2'; rex_file::put($path, ''); rex_file::put($path2, ''); rex_log_file::delete($path); $this->assertFileNotExists($path); $this->assertFileNotExists($path2); }
/** * Compile a fragment. * * @param string $value */ public function compile($file) { if ($this->isExpired($file)) { $content = rex_file::get($file); if (rex_file::put($this->getCompiledFile($file), $this->compileString($content)) === false) { throw new rex_exception('Unable to generate fragment ' . $file . '!'); } } return $this->getCompiledFile($file); }
/** * Constructor. * * @param string $path File path * @param int|null $maxFileSize Maximum file size */ public function __construct($path, $maxFileSize = null) { $this->path = $path; if (!file_exists($path)) { rex_file::put($path, ''); } if ($maxFileSize && file_exists($path) && filesize($path) > $maxFileSize) { rename($path, $path . '.2'); } $this->file = fopen($path, 'a+b'); }
public function setUp() { parent::setUp(); rex_file::put($this->getPath('file1.txt'), ''); rex_file::put($this->getPath('file2.yml'), ''); rex_file::put($this->getPath('dir1/file3.txt'), ''); rex_file::put($this->getPath('dir2/file4.yml'), ''); rex_file::put($this->getPath('dir2/dir/file5.yml'), ''); rex_dir::create($this->getPath('dir1/dir')); rex_dir::create($this->getPath('dir2/dir1')); rex_dir::create($this->getPath('dir')); rex_file::put($this->getPath('.DS_Store'), ''); rex_file::put($this->getPath('dir1/Thumbs.db'), ''); }
private function backup() { $content = '<!-- ' . PHP_EOL . date('d.m.Y H:i:s') . PHP_EOL; $content .= 'From : ' . $this->From . PHP_EOL; $content .= 'To : ' . implode(', ', array_column($this->getToAddresses(), 0)) . PHP_EOL; $content .= 'Subject : ' . $this->Subject . PHP_EOL; $content .= ' -->' . PHP_EOL; $content .= $this->Body; $dir = rex_path::addonData('phpmailer', 'mail_backup/' . date('Y') . '/' . date('m')); $count = 1; $backupFile = $dir . '/' . date('Y-m-d_H_i_s') . '.html'; while (file_exists($backupFile)) { $backupFile = $dir . '/' . date('Y-m-d_H_i_s') . '_' . ++$count . '.html'; } rex_file::put($backupFile, $content); }
public function generate() { $template_id = $this->getId(); if ($template_id < 1) { return false; } $sql = rex_sql::factory(); $qry = 'SELECT * FROM ' . rex::getTablePrefix() . 'template WHERE id = ' . $template_id; $sql->setQuery($qry); if ($sql->getRows() == 1) { $templateFile = self::getFilePath($template_id); $content = $sql->getValue('content'); $content = rex_var::parse($content, rex_var::ENV_FRONTEND, 'template'); if (rex_file::put($templateFile, $content) !== false) { return true; } else { throw new rex_exception('Unable to generate template ' . $template_id . '!'); } } else { throw new rex_exception('Template with id "' . $template_id . '" does not exist!'); } }
} $filename = $filename . '_' . $i; } if ($exporttype == 'sql') { // ------------------------------ FUNC EXPORT SQL $header = 'plain/text'; $hasContent = rex_backup::exportDb($export_path . $filename . $ext, $EXPTABLES); // ------------------------------ /FUNC EXPORT SQL } elseif ($exporttype == 'files') { // ------------------------------ FUNC EXPORT FILES $header = 'tar/gzip'; if (empty($EXPDIR)) { $error = rex_i18n::msg('backup_please_choose_folder'); } else { $content = rex_backup::exportFiles($EXPDIR); $hasContent = rex_file::put($export_path . $filename . $ext, $content); } // ------------------------------ /FUNC EXPORT FILES } if ($hasContent) { if ($exportdl) { $filename = $filename . $ext; rex_response::sendFile($export_path . $filename, $header, 'attachment'); rex_file::delete($export_path . $filename); exit; } else { $success = rex_i18n::msg('backup_file_generated_in') . ' ' . strtr($filename . $ext, '\\', '/'); } } else { $error = rex_i18n::msg('backup_file_could_not_be_generated') . ' ' . rex_i18n::msg('backup_check_rights_in_directory') . ' ' . $export_path; }
/** * Generiert den Artikel-Cache des Artikelinhalts. * * @param int $article_id Id des zu generierenden Artikels * @param int $clang ClangId des Artikels * * @return bool TRUE bei Erfolg, FALSE wenn eine ungütlige article_id übergeben wird, sonst eine Fehlermeldung */ public static function generateArticleContent($article_id, $clang = null) { foreach (rex_clang::getAllIds() as $_clang) { if ($clang !== null && $clang != $_clang) { continue; } $CONT = new rex_article_content_base(); $CONT->setCLang($_clang); $CONT->setEval(false); // Content nicht ausführen, damit in Cachedatei gespeichert werden kann if (!$CONT->setArticleId($article_id)) { return false; } // --------------------------------------------------- Artikelcontent speichern $article_content_file = rex_path::addonCache('structure', "{$article_id}.{$_clang}.content"); $article_content = $CONT->getArticle(); // ----- EXTENSION POINT $article_content = rex_extension::registerPoint(new rex_extension_point('GENERATE_FILTER', $article_content, ['id' => $article_id, 'clang' => $_clang, 'article' => $CONT])); if (rex_file::put($article_content_file, $article_content) === false) { return rex_i18n::msg('article_could_not_be_generated') . ' ' . rex_i18n::msg('check_rights_in_directory') . rex_path::addonCache('structure'); } } return true; }
<?php /** @var i18n $I18N */ $content = ''; $settings = rex_post('settings', array(array('backups', 'bool', false), array('api_login', 'string'), array('api_key', 'string')), null); if (is_array($settings)) { $settingsContent = "<?php\n\n"; foreach ($settings as $key => $value) { $settingsContent .= "\$REX['ADDON'][" . var_export($key, true) . "]['install'] = " . var_export($value, true) . ";\n"; OOAddon::setProperty('install', $key, $value); } if (rex_file::put(rex_path::addonData('install', 'settings.inc.php'), $settingsContent)) { echo rex_info($I18N->msg('install_settings_saved')); } else { echo rex_warning($I18N->msg('install_settings_not_saved')); } rex_install_webservice::deleteCache(); } $content .= ' <div class="rex-addon-output"> <h2 class="rex-hl2">' . $I18N->msg('install_subpage_settings') . '</h2> <div class="rex-form"> <form action="index.php?page=install&subpage=settings" method="post"> <fieldset class="rex-form-col-1"> <legend>' . $I18N->msg('install_settings_general') . '</legend> <div class="rex-form-wrapper">'; $content .= ' <div class="rex-form-row"> <p class="rex-form-col-a rex-form-checkbox rex-form-label-right">
public static function generateConfig() { $filecontent = '<?php ' . "\n"; $gc = rex_sql::factory(); $domains = $gc->getArray('select * from rex_yrewrite_domain order by alias_domain, mount_id, clangs'); foreach ($domains as $domain) { if ($domain['domain'] != '') { if ($domain['alias_domain'] != '') { $filecontent .= "\n" . 'rex_yrewrite::addAliasDomain("' . $domain['domain'] . '", "' . $domain['alias_domain'] . '", ' . $domain['clang_start'] . ');'; } elseif ($domain['start_id'] > 0 && $domain['notfound_id'] > 0) { $filecontent .= "\n" . 'rex_yrewrite::addDomain(new rex_yrewrite_domain(' . '"' . $domain['domain'] . '", ' . $domain['mount_id'] . ', ' . $domain['start_id'] . ', ' . $domain['notfound_id'] . ', ' . (strlen(trim($domain['clangs'])) ? 'array(' . $domain['clangs'] . ')' : 'null') . ', ' . $domain['clang_start'] . ', ' . '"' . htmlspecialchars($domain['title_scheme']) . '", ' . '"' . htmlspecialchars($domain['description']) . '", ' . '"' . htmlspecialchars($domain['robots']) . '"' . '));'; } } } rex_file::put(self::$configfile, $filecontent); }
static function save($name, $success, $message = '', $id = null) { global $REX; $year = date('Y'); $month = date('m'); // in den Log-Dateien festes Datumsformat verwenden // wird bei der Ausgabe entsprechend der lokalen Einstellungen umgewandelt // rex_formatter nicht verwenden, da im Frontend nicht verfuegbar $newline = date('Y-m-d H:i'); if ($success) { $newline .= ' | SUCCESS | '; } else { $newline .= ' | ERROR | '; } if (!$id) { $id = '--'; } else { $id = str_pad($id, 2, ' ', STR_PAD_LEFT); } $newline .= $id . ' | ' . $name; if ($message) { $newline .= ' | ' . str_replace(array("\r\n", "\n"), ' | ', trim(strip_tags($message))); } $dir = REX_CRONJOB_LOG_FOLDER . $year; $content = ''; $file = $dir . '/' . $year . '-' . $month . '.log'; if (file_exists($file)) { $content = rex_file::get($file); } $content = $newline . "\n" . $content; return rex_file::put($file, $content); }
public function testGetOutput() { $file = $this->getPath('test.php'); rex_file::put($file, 'a<?php echo "b";'); $this->assertEquals('ab', rex_file::getOutput($file), 'getOutput() returns the executed content'); }
public static function generateConfig() { $content = '<?php ' . "\n"; $gc = rex_sql::factory(); $domains = $gc->getArray('select * from ' . rex::getTable('yrewrite_domain') . ' order by alias_domain, mount_id, clangs'); foreach ($domains as $domain) { if (!$domain['domain']) { continue; } $name = $domain['domain']; if (false === strpos($name, '//')) { $name = '//' . $name; } $parts = parse_url($name); $name = $parts['host']; if (isset($parts['port'])) { $name .= ':' . $parts['port']; } $path = '/'; if (isset($parts['path'])) { $path = rtrim($parts['path'], '/') . '/'; } if ($domain['alias_domain'] != '') { $content .= "\n" . 'rex_yrewrite::addAliasDomain("' . $name . '", "' . $domain['alias_domain'] . '", ' . $domain['clang_start'] . ');'; } elseif ($domain['start_id'] > 0 && $domain['notfound_id'] > 0) { $content .= "\n" . 'rex_yrewrite::addDomain(new rex_yrewrite_domain(' . '"' . $name . '", ' . (isset($parts['scheme']) ? '"' . $parts['scheme'] . '"' : 'null') . ', ' . '"' . $path . '", ' . $domain['mount_id'] . ', ' . $domain['start_id'] . ', ' . $domain['notfound_id'] . ', ' . (strlen(trim($domain['clangs'])) ? 'array(' . $domain['clangs'] . ')' : 'null') . ', ' . $domain['clang_start'] . ', ' . '"' . htmlspecialchars($domain['title_scheme']) . '", ' . '"' . htmlspecialchars($domain['description']) . '", ' . '"' . htmlspecialchars($domain['robots']) . '"' . '));'; } } rex_file::put(self::$configfile, $content); }
/** * Generiert den TemplateCache im Filesystem * * @param $template_id Id des zu generierenden Templates * * @return TRUE bei Erfolg, sonst FALSE */ function rex_generateTemplate($template_id) { global $REX; $sql = rex_sql::factory(); $qry = 'SELECT * FROM ' . $REX['TABLE_PREFIX'] . 'template WHERE id = ' . $template_id; $sql->setQuery($qry); if ($sql->getRows() == 1) { $templatesDir = rex_template::getTemplatesDir(); $templateFile = rex_template::getFilePath($template_id); $content = $sql->getValue('content'); foreach ($REX['VARIABLES'] as $var) { $content = $var->getTemplate($content); } if (rex_file::put($templateFile, $content) !== false) { return true; } else { trigger_error('Unable to generate template ' . $template_id . '!', E_USER_ERROR); if (!is_writable()) { trigger_error('directory "' . $templatesDir . '" is not writable!', E_USER_ERROR); } } } else { trigger_error('Template with id "' . $template_id . '" does not exist!', E_USER_ERROR); } return false; }
/** * Erstellt einen SQL Dump, der die aktuellen Datebankstruktur darstellt. * Dieser wird in der Datei $filename gespeichert. * * @param string $filename * @param array $tables * * @return bool TRUE wenn ein Dump erstellt wurde, sonst FALSE */ public static function exportDb($filename, array $tables = null) { $fp = @fopen($filename, 'w'); if (!$fp) { return false; } $sql = rex_sql::factory(); $nl = "\n"; $insertSize = 5000; // ----- EXTENSION POINT rex_extension::registerPoint(new rex_extension_point('BACKUP_BEFORE_DB_EXPORT')); // Versionsstempel hinzufügen fwrite($fp, '## Redaxo Database Dump Version ' . rex::getVersion('%s') . $nl); fwrite($fp, '## Prefix ' . rex::getTablePrefix() . $nl); //fwrite($fp, '## charset '.rex_i18n::msg('htmlcharset').$nl.$nl); fwrite($fp, '## charset utf-8' . $nl . $nl); // fwrite($fp, '/*!40110 START TRANSACTION; */'.$nl); if (is_null($tables)) { $tables = []; foreach (rex_sql::showTables(1, rex::getTablePrefix()) as $table) { if ($table != rex::getTable('user') && substr($table, 0, strlen(rex::getTablePrefix() . rex::getTempPrefix())) != rex::getTablePrefix() . rex::getTempPrefix()) { // Tabellen die mit rex_tmp_ beginnne, werden nicht exportiert! $tables[] = $table; } } } foreach ($tables as $table) { //---- export metadata $create = rex_sql::showCreateTable($table); fwrite($fp, 'DROP TABLE IF EXISTS `' . $table . '`;' . $nl); fwrite($fp, $create . ';' . $nl); $fields = $sql->getArray('SHOW FIELDS FROM `' . $table . '`'); foreach ($fields as &$field) { if (preg_match('#^(bigint|int|smallint|mediumint|tinyint|timestamp)#i', $field['Type'])) { $field = 'int'; } elseif (preg_match('#^(float|double|decimal)#', $field['Type'])) { $field = 'double'; } elseif (preg_match('#^(char|varchar|text|longtext|mediumtext|tinytext)#', $field['Type'])) { $field = 'string'; } // else ? } //---- export tabledata $start = 0; $max = $insertSize; do { $array = $sql->getArray('SELECT * FROM `' . $table . '` LIMIT ' . $start . ',' . $max, [], PDO::FETCH_NUM); $count = $sql->getRows(); if ($count > 0 && $start == 0) { fwrite($fp, $nl . 'LOCK TABLES `' . $table . '` WRITE;'); fwrite($fp, $nl . '/*!40000 ALTER TABLE `' . $table . '` DISABLE KEYS */;'); } elseif ($count == 0) { break; } $start += $max; $values = []; foreach ($array as $row) { $record = []; foreach ($fields as $idx => $type) { $column = $row[$idx]; switch ($type) { case 'int': $record[] = intval($column); break; case 'double': $record[] = sprintf('%.10F', (double) $column); break; case 'string': default: $record[] = $sql->escape($column, "'"); break; } } $values[] = $nl . ' (' . implode(',', $record) . ')'; } if (!empty($values)) { fwrite($fp, $nl . 'INSERT INTO `' . $table . '` VALUES ' . implode(',', $values) . ';'); unset($values); } } while ($count >= $max); if ($start > 0) { fwrite($fp, $nl . '/*!40000 ALTER TABLE `' . $table . '` ENABLE KEYS */;'); fwrite($fp, $nl . 'UNLOCK TABLES;' . $nl . $nl); } } fclose($fp); $hasContent = true; // Den Dateiinhalt geben wir nur dann weiter, wenn es unbedingt notwendig ist. if (rex_extension::isRegistered('BACKUP_AFTER_DB_EXPORT')) { $content = rex_file::get($filename); $hashBefore = md5($content); // ----- EXTENSION POINT $content = rex_extension::registerPoint(new rex_extension_point('BACKUP_AFTER_DB_EXPORT', $content)); $hashAfter = md5($content); if ($hashAfter != $hashBefore) { rex_file::put($filename, $content); $hasContent = !empty($content); unset($content); } } return $hasContent; }
$this->FromName = ' . var_export($fromname, true) . '; $this->ConfirmReadingTo = ' . var_export($confirmto, true) . '; $this->AdminBcc = ' . var_export($AdminBcc, true) . '; $this->Mailer = ' . var_export($mailer, true) . '; $this->Host = ' . var_export($host, true) . '; $this->Port = ' . var_export($port, true) . '; $this->CharSet = ' . var_export($charset, true) . '; $this->WordWrap = ' . var_export($wordwrap, true) . '; $this->Encoding = ' . var_export($encoding, true) . '; $this->Priority = ' . var_export($priority, true) . '; $this->SMTPSecure = ' . var_export($smtpsecure, true) . '; $this->SMTPAuth = ' . var_export($smtpauth, true) . '; $this->Username = '******'; $this->Password = '******'; '; if (rex_file::put($file, $content) !== false) { $message = $I18N->msg('phpmailer_config_saved_successful'); } } $sel_mailer = new rex_select(); $sel_mailer->setId('mailer'); $sel_mailer->setName('mailer'); $sel_mailer->setSize(1); $sel_mailer->setSelected($mailer); foreach (array('mail', 'sendmail', 'smtp') as $type) { $sel_mailer->addOption($type, $type); } $sel_smtpsecure = new rex_select(); $sel_smtpsecure->setId('smtpsecure'); $sel_smtpsecure->setName('smtpsecure'); $sel_smtpsecure->setSize(1);
public function testDeleteFilesRecursive() { $dir = $this->getPath('deleteFilesRecursive'); $file1 = $this->getPath('deleteFilesRecursive/file.txt'); $file2 = $this->getPath('deleteFilesRecursive/subdir/file.txt'); rex_file::put($file1, ''); rex_file::put($file2, ''); $this->assertTrue(is_file($file1), 'file exists after put()'); $this->assertTrue(is_file($file2), 'file exists after put()'); $this->assertTrue(rex_dir::deleteFiles($dir), 'deleteFiles() returns true on success'); $this->assertFalse(is_file($file1), 'file in main dir does not exist after deleteFiles()'); $this->assertFalse(is_file($file2), 'file in subdir does not exist after recursive deleteFiles()'); $this->assertTrue(is_dir($dir . '/subdir'), 'subdir still exists after deleteFiles()'); }
public function toTar($filename, $useGzip) { // Encode processed files into TAR file format $this->__generateTar(); // GZ Compress the data if we need to if ($useGzip) { // Make sure we have gzip support if (!function_exists('gzencode')) { return false; } $file = gzencode($this->tar_file); } else { $file = $this->tar_file; } // Write the TAR file // $fp = fopen($filename,"wb"); // fwrite($fp,$file); // fclose($fp); // kein Filename gegeben => Inhalt zurueckgeben if (!$filename) { return $file; } // STM: hier mit put_file_contents ist viel schneller return rex_file::put($filename, $file) !== false; }
public function sendMedia($sourceCacheFilename, $headerCacheFilename, $save = false) { if ($this->asImage) { $src = $this->getImageSource(); } else { $src = rex_file::get($this->getMediapath()); } $this->setHeader('Content-Length', rex_string::size($src)); $header = $this->getHeader(); if (!array_key_exists('Content-Type', $header)) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $content_type = finfo_file($finfo, $this->getMediapath()); if ($content_type != '') { $this->setHeader('Content-Type', $content_type); } } if (!array_key_exists('Content-Disposition', $header)) { $this->setHeader('Content-Disposition', 'inline; filename="' . $this->getMediaFilename() . '";'); } if (!array_key_exists('Last-Modified', $header)) { $this->setHeader('Last-Modified', gmdate('D, d M Y H:i:s T')); } rex_response::cleanOutputBuffers(); foreach ($this->header as $t => $c) { header($t . ': ' . $c); } echo $src; if ($save) { rex_file::putCache($headerCacheFilename, $this->header); rex_file::put($sourceCacheFilename, $src); } }
function saveNextTime($nexttime = null) { global $REX; if ($nexttime === null) { $sql_manager = rex_cronjob_manager_sql::factory($this); $nexttime = $sql_manager->getMinNextTime(); } if ($nexttime === null) { $nexttime = 0; } else { $nexttime = max(1, $nexttime); } if ($nexttime != $REX['ADDON']['nexttime']['cronjob'] && rex_file::put(REX_CRONJOB_NEXTTIME_FILE, $nexttime)) { $REX['ADDON']['nexttime']['cronjob'] = $nexttime; return true; } return false; }
public static function generatePathFile() { $gc = rex_sql::factory(); $content = $gc->getArray('select * from rex_yrewrite_forward'); rex_file::put(self::$pathfile, json_encode($content)); }