From ea57b3fdd1fa474093bff65b706fc783acc9fc2e Mon Sep 17 00:00:00 2001 From: Fabio Chelly Date: Thu, 3 Oct 2013 15:32:10 +0200 Subject: [PATCH] [-] MO watermark: original pictures are now protected through htaccess #PNM-109 --- modules/watermark/watermark.php | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/modules/watermark/watermark.php b/modules/watermark/watermark.php index 55901e21c..9e396006a 100644 --- a/modules/watermark/watermark.php +++ b/modules/watermark/watermark.php @@ -69,6 +69,7 @@ class Watermark extends Module public function install() { + $this->writeHtaccessSection(); if (!parent::install() || !$this->registerHook('watermark')) return false; Configuration::updateValue('WATERMARK_TRANSPARENCY', 60); @@ -79,6 +80,7 @@ class Watermark extends Module public function uninstall() { + $this->removeHtaccessSection(); return (parent::uninstall() && Configuration::deleteByName('WATERMARK_TYPES') && Configuration::deleteByName('WATERMARK_TRANSPARENCY') @@ -207,9 +209,51 @@ class Watermark extends Module '; } + + public function getAdminDir() + { + $admin_dir = str_replace('\\', '/', _PS_ADMIN_DIR_); + $admin_dir = explode('/', $admin_dir); + $len = count($admin_dir); + return $len > 1 ? $admin_dir[$len - 1] : _PS_ADMIN_DIR_; + } + + public function removeHtaccessSection() + { + $key1 = "\n# start ~ module watermark section"; + $key2 = "# end ~ module watermark section\n"; + $path = _PS_ROOT_DIR_ . '/.htaccess'; + if (file_exists($path) && is_writable($path)) { + $s = file_get_contents($path); + $p1 = strpos($s, $key1); + $p2 = strpos($s, $key2, $p1); + if ($p1 === false || $p2 === false) return false; + $s = substr($s, 0, $p1) . substr($s, $p2 + strlen($key2)); + file_put_contents($path, $s); + } + return true; + } + + public function writeHtaccessSection() + { + $admin_dir = $this->getAdminDir(); + $source = "\n# start ~ module watermark section +Options +FollowSymLinks +RewriteEngine On +RewriteCond expr \"! %{HTTP_REFERER} -strmatch '*://%{HTTP_HOST}*/$admin_dir/*'\" +RewriteRule [0-9/]+/[0-9]+\\.jpg$ - [F] +# end ~ module watermark section\n"; + + $path = _PS_ROOT_DIR_ . '/.htaccess'; + file_put_contents($path, $source, FILE_APPEND); + } public function getContent() { + //Modify htaccess to prevent downlaod of original pictures + $this->removeHtaccessSection(); + $this->writeHtaccessSection(); + $this->_html = '

'.$this->displayName.'

'; if (Tools::isSubmit('btnSubmit'))