From dd4b6ed3f48e1556b4ca290febb8f26496e87306 Mon Sep 17 00:00:00 2001 From: Christoph Date: Thu, 4 Jun 2015 12:19:21 +0200 Subject: [PATCH] allow to configure the directory where charts are published --- CHANGELOG.md | 3 ++ config.template.yaml | 5 ++++ lib/api/charts.php | 2 +- lib/core/build/classes/datawrapper/Chart.php | 6 ++-- lib/utils/chart_publish.php | 26 ++++++++++++---- lib/utils/check_server.php | 31 +++++++++++++------- 6 files changed, 53 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b12919c524..73dfb9728a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ CHANGELOG --------- +### 1.9.x +* the chart publish directory can now be configured + ### 1.9.4 * allow setting of relative chart widths (e.g. 100%) * reload chart iframe on ctrl-r diff --git a/config.template.yaml b/config.template.yaml index da985ae391..670b33108f 100644 --- a/config.template.yaml +++ b/config.template.yaml @@ -65,6 +65,11 @@ footer_links: ## libraries such as globalize.js. # asset_domain: assets-datawrapper.s3.amazonaws.com +## This is the directory where locally published files will be stored. +## If you do not define this, the `charts` directory within datawrapper +## will be used. +# publish_directory: /full/path/to/where/charts/should/be/published/ + ## to use a custom home page in your installation ## uncomment the following setting and create a new ## Twig template (e.g. my-home.twig) in /templates/custom/ diff --git a/lib/api/charts.php b/lib/api/charts.php index 6b1591d181..b742939d6e 100644 --- a/lib/api/charts.php +++ b/lib/api/charts.php @@ -170,7 +170,7 @@ function if_chart_exists($id, $callback) { $sizeLimit = 2 * 1024 * 1024; // byte $uploader = new qqFileUploader($allowedExtensions, $sizeLimit); - $result = $uploader->handleUpload('../../charts/data/tmp/'); + $result = $uploader->handleUpload(chart_publish_directory().'data/tmp/'); // check and correct file encoding $detect_encoding = function($string) { diff --git a/lib/core/build/classes/datawrapper/Chart.php b/lib/core/build/classes/datawrapper/Chart.php index ccbdab731b..13c0c775a2 100644 --- a/lib/core/build/classes/datawrapper/Chart.php +++ b/lib/core/build/classes/datawrapper/Chart.php @@ -95,12 +95,12 @@ protected function uppercaseKeys($arr) { * get the path where this charts data file is stored */ protected function getDataPath() { - $path = ROOT_PATH . 'charts/data/' . $this->getCreatedAt('Ym'); + $path = chart_publish_directory() . 'data/' . $this->getCreatedAt('Ym'); return $path; } protected function getStaticPath() { - $path = ROOT_PATH . 'charts/static/' . $this->getID(); + $path = chart_publish_directory() . 'static/' . $this->getID(); return $path; } @@ -285,7 +285,7 @@ public function publish() { public function redirectPreviousVersions() { $current_target = $this->getCDNPath(); $redirect_html = ''; - $redirect_file = ROOT_PATH . 'charts/static/' . $this->getID() . '/redirect.html'; + $redirect_file = chart_publish_directory() . 'static/' . $this->getID() . '/redirect.html'; file_put_contents($redirect_file, $redirect_html); $files = array(); for ($v=0; $v < $this->getPublicVersion(); $v++) { diff --git a/lib/utils/chart_publish.php b/lib/utils/chart_publish.php index 3fdc2b52d6..13d4cd5f1d 100644 --- a/lib/utils/chart_publish.php +++ b/lib/utils/chart_publish.php @@ -54,7 +54,7 @@ function _setPublishStatus($chart, $status) { if (isset($_GLOBALS['dw-config']['memcache'])) { $memcache->set('publish-status-' . $chart->getID(), round($status*100)); } else { - file_put_contents(ROOT_PATH . 'charts/tmp/publish-status-' . $chart->getID(), round($status*100)); + file_put_contents(chart_publish_directory() . 'tmp/publish-status-' . $chart->getID(), round($status*100)); } } @@ -62,7 +62,7 @@ function _getPublishStatus($chart) { if (isset($_GLOBALS['dw-config']['memcache'])) { return $memcache->get('publish-status-' . $chart->getID()); } else { - $fn = ROOT_PATH . 'charts/tmp/publish-status-' . $chart->getID(); + $fn = chart_publish_directory() . 'tmp/publish-status-' . $chart->getID(); if (!file_exists($fn)) return false; return file_get_contents($fn); } @@ -73,12 +73,12 @@ function _clearPublishStatus($chart) { global $memcache; $memcache->delete('publish-status-' . $chart->getID()); } else { - unlink(ROOT_PATH . 'charts/tmp/publish-status-' . $chart->getID()); + unlink(chart_publish_directory() . 'tmp/publish-status-' . $chart->getID()); } } function get_static_path($chart) { - $static_path = ROOT_PATH . "charts/static/" . $chart->getID(); + $static_path = $chart->getStaticPath(); if (!is_dir($static_path)) { mkdir($static_path); } @@ -113,7 +113,7 @@ function publish_html($user, $chart) { function publish_js($user, $chart) { $cdn_files = array(); - $static_path = ROOT_PATH . 'charts/static/lib/'; + $static_path = chart_publish_directory() . 'static/lib/'; $data = get_chart_content($chart, $user, false, '../'); // generate visualization script @@ -271,3 +271,19 @@ function download($url, $outf) { file_put_contents($outf, $html); } } + +function chart_publish_directory() { + $dir = ROOT_PATH.'charts'; + + if (isset($_GLOBALS['dw-config']['publish_directory'])) { + $dir = $_GLOBALS['dw-config']['publish_directory']; + } + + if (!is_dir($dir)) { + if (!@mkdir($dir, 0755, true)) { + throw new RuntimeException('Could not create chart publish directory "'.$dir.'". Please create it manually and make sure PHP can write to it.'); + } + } + + return rtrim(realpath($dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; +} diff --git a/lib/utils/check_server.php b/lib/utils/check_server.php index c034216c60..ab62628c54 100644 --- a/lib/utils/check_server.php +++ b/lib/utils/check_server.php @@ -3,27 +3,36 @@ /* health check */ function check_path_permissions() { - $paths = array(); - $rel = '..'; - $paths[] = '/charts/static'; - $paths[] = '/charts/data'; - $paths[] = '/charts/images'; - $paths[] = '/charts/data/tmp'; - $paths[] = '/tmp'; + $paths = array(); + $rel = '..'; + $publishRoot = chart_publish_directory(); + + $paths[] = $publishRoot.'static'; + $paths[] = $publishRoot.'data'; + $paths[] = $publishRoot.'images'; + $paths[] = $publishRoot.'data/tmp'; + $paths[] = ROOT_PATH.'tmp'; + $err = array(); foreach ($paths as $path) { - if (!is_writable($rel . $path)) $err[] = $path; + if (!is_writable($path)) { + $err[] = $path; + } } + if (count($err) > 0) { $msg = '

The following folders on your server need to be writable:

'; $msg .= 'Read more about how to change file permissions'; + return $msg; } + return ''; } @@ -71,7 +80,7 @@ function check_database() { return '

Database is not initialized or corrupt

' . '

The database could be accessed but seems not be initialized correctly. ' . 'The following tables are missing:

' - . '' + . '' . '

Have you run the DB initialization in lib/core/build/sql/schema.sql?

'; } return '';