diff options
author | mattab <matthieu.aubry@gmail.com> | 2014-01-08 07:24:57 +0400 |
---|---|---|
committer | mattab <matthieu.aubry@gmail.com> | 2014-01-08 07:24:57 +0400 |
commit | 659160cf8ef74cafda848fbd7ff83a0b1fe1d24d (patch) | |
tree | 793beefce3bc7c0f789079c02f6303facf0af571 /plugins/CoreAdminHome | |
parent | 78e66bbae81ca3b53f73fb402c033c99ee837630 (diff) |
Refactoring Custom Logo code into its own class + improvements
Diffstat (limited to 'plugins/CoreAdminHome')
-rw-r--r-- | plugins/CoreAdminHome/Controller.php | 69 | ||||
-rw-r--r-- | plugins/CoreAdminHome/CustomLogo.php | 186 | ||||
-rw-r--r-- | plugins/CoreAdminHome/templates/generalSettings.twig | 4 |
3 files changed, 201 insertions, 58 deletions
diff --git a/plugins/CoreAdminHome/Controller.php b/plugins/CoreAdminHome/Controller.php index c995de2b0b..550c559fa3 100644 --- a/plugins/CoreAdminHome/Controller.php +++ b/plugins/CoreAdminHome/Controller.php @@ -34,9 +34,6 @@ use Piwik\View; */ class Controller extends \Piwik\Plugin\ControllerAdmin { - const LOGO_HEIGHT = 300; - const LOGO_SMALL_HEIGHT = 100; - const SET_PLUGIN_SETTINGS_NONCE = 'CoreAdminHome.setPluginSettings'; public function index() @@ -60,12 +57,13 @@ class Controller extends \Piwik\Plugin\ControllerAdmin $view->trustedHosts = $trustedHosts; $view->branding = Config::getInstance()->branding; - $directoryWritable = is_writable(PIWIK_DOCUMENT_ROOT . '/misc/user/'); - $logoFilesWriteable = is_writeable(PIWIK_DOCUMENT_ROOT . '/misc/user/logo.png') - && is_writeable(PIWIK_DOCUMENT_ROOT . '/misc/user/logo.svg') - && is_writeable(PIWIK_DOCUMENT_ROOT . '/misc/user/logo-header.png');; - $view->logosWriteable = ($logoFilesWriteable || $directoryWritable) && ini_get('file_uploads') == 1; + $logo = new CustomLogo(); + $view->logosWriteable = $logo->isCustomLogoWritable(); + $view->pathUserLogo = CustomLogo::getPathUserLogo(); + $view->pathUserLogoSmall = CustomLogo::getPathUserLogoSmall(); + $view->pathUserLogoSVG = CustomLogo::getPathUserSvgLogo(); + $view->pathUserLogoDirectory = dirname($view->pathUserLogo) . '/'; } $view->language = LanguagesManager::getLanguageCodeForCurrentUser(); @@ -271,57 +269,14 @@ class Controller extends \Piwik\Plugin\ControllerAdmin public function uploadCustomLogo() { Piwik::checkUserIsSuperUser(); - if (empty($_FILES['customLogo']) - || !empty($_FILES['customLogo']['error']) - ) { - return '0'; - } - $file = $_FILES['customLogo']['tmp_name']; - if (!file_exists($file)) { - return '0'; - } + $logo = new CustomLogo(); + $success = $logo->copyUploadedLogoToFilesystem(); - list($width, $height) = getimagesize($file); - switch ($_FILES['customLogo']['type']) { - case 'image/jpeg': - $image = imagecreatefromjpeg($file); - break; - case 'image/png': - $image = imagecreatefrompng($file); - break; - case 'image/gif': - $image = imagecreatefromgif($file); - break; - default: - return '0'; + if($success) { + return '1'; } - - $widthExpected = round($width * self::LOGO_HEIGHT / $height); - $smallWidthExpected = round($width * self::LOGO_SMALL_HEIGHT / $height); - - $logo = imagecreatetruecolor($widthExpected, self::LOGO_HEIGHT); - $logoSmall = imagecreatetruecolor($smallWidthExpected, self::LOGO_SMALL_HEIGHT); - - // Handle transparency - $background = imagecolorallocate($logo, 0, 0, 0); - $backgroundSmall = imagecolorallocate($logoSmall, 0, 0, 0); - imagecolortransparent($logo, $background); - imagecolortransparent($logoSmall, $backgroundSmall); - - if ($_FILES['customLogo']['type'] == 'image/png') { - imagealphablending($logo, false); - imagealphablending($logoSmall, false); - imagesavealpha($logo, true); - imagesavealpha($logoSmall, true); - } - - imagecopyresized($logo, $image, 0, 0, 0, 0, $widthExpected, self::LOGO_HEIGHT, $width, $height); - imagecopyresized($logoSmall, $image, 0, 0, 0, 0, $smallWidthExpected, self::LOGO_SMALL_HEIGHT, $width, $height); - - imagepng($logo, PIWIK_DOCUMENT_ROOT . '/misc/user/logo.png', 3); - imagepng($logoSmall, PIWIK_DOCUMENT_ROOT . '/misc/user/logo-header.png', 3); - return '1'; + return '0'; } private function isGeneralSettingsAdminEnabled() @@ -382,4 +337,6 @@ class Controller extends \Piwik\Plugin\ControllerAdmin $view->mail = Config::getInstance()->mail; $this->displayWarningIfConfigFileNotWritable(); } + + } diff --git a/plugins/CoreAdminHome/CustomLogo.php b/plugins/CoreAdminHome/CustomLogo.php new file mode 100644 index 0000000000..1ab773a3ea --- /dev/null +++ b/plugins/CoreAdminHome/CustomLogo.php @@ -0,0 +1,186 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + * @category Piwik_Plugins + * @package Piwik_API + */ +namespace Piwik\Plugins\CoreAdminHome; + +use Piwik\Config; +use Piwik\Filesystem; +use Piwik\SettingsPiwik; + +class CustomLogo +{ + const LOGO_HEIGHT = 300; + const LOGO_SMALL_HEIGHT = 100; + + public function getLogoUrl($pathOnly = false) + { + $defaultLogo = 'plugins/Zeitgeist/images/logo.png'; + $themeLogo = 'plugins/%s/images/logo.png'; + $userLogo = CustomLogo::getPathUserLogo(); + return $this->getPathToLogo($pathOnly, $defaultLogo, $themeLogo, $userLogo); + } + + public function getHeaderLogoUrl($pathOnly = false) + { + $defaultLogo = 'plugins/Zeitgeist/images/logo-header.png'; + $themeLogo = 'plugins/%s/images/logo-header.png'; + $customLogo = CustomLogo::getPathUserLogoSmall(); + return $this->getPathToLogo($pathOnly, $defaultLogo, $themeLogo, $customLogo); + } + + public function getSVGLogoUrl($pathOnly = false) + { + $defaultLogo = 'plugins/Zeitgeist/images/logo.svg'; + $themeLogo = 'plugins/%s/images/logo.svg'; + $customLogo = CustomLogo::getPathUserSvgLogo(); + $svg = $this->getPathToLogo($pathOnly, $defaultLogo, $themeLogo, $customLogo); + return $svg; + } + + public function hasSVGLogo() + { + if (Config::getInstance()->branding['use_custom_logo'] == 0) { + /* We always have our application logo */ + return true; + } + + if (Config::getInstance()->branding['use_custom_logo'] == 1 + && file_exists(Filesystem::getPathToPiwikRoot() . '/' . CustomLogo::getPathUserSvgLogo()) + ) { + return true; + } + + return false; + } + + /** + * @return bool + */ + public function isCustomLogoWritable() + { + $pathUserLogo = $this->getPathUserLogo(); + + $directoryWritingTo = PIWIK_DOCUMENT_ROOT . '/' . dirname($pathUserLogo); + + // Create directory if not already created + Filesystem::mkdir($directoryWritingTo, $denyAccess = false); + + $directoryWritable = is_writable($directoryWritingTo); + $logoFilesWriteable = is_writeable(PIWIK_DOCUMENT_ROOT . '/' . $pathUserLogo) + && is_writeable(PIWIK_DOCUMENT_ROOT . '/' . $this->getPathUserSvgLogo()) + && is_writeable(PIWIK_DOCUMENT_ROOT . '/' . $this->getPathUserLogoSmall());; + + $serverUploadEnabled = ini_get('file_uploads') == 1; + $isCustomLogoWritable = ($logoFilesWriteable || $directoryWritable) && $serverUploadEnabled; + + return $isCustomLogoWritable; + } + + protected function getPathToLogo($pathOnly, $defaultLogo, $themeLogo, $customLogo) + { + $pathToPiwikRoot = Filesystem::getPathToPiwikRoot(); + + $logo = $defaultLogo; + + $themeName = \Piwik\Plugin\Manager::getInstance()->getThemeEnabled()->getPluginName(); + $themeLogo = sprintf($themeLogo, $themeName); + + if (file_exists($pathToPiwikRoot . '/' . $themeLogo)) { + $logo = $themeLogo; + } + if (Config::getInstance()->branding['use_custom_logo'] == 1 + && file_exists($pathToPiwikRoot . '/' . $customLogo) + ) { + $logo = $customLogo; + } + + if (!$pathOnly) { + return SettingsPiwik::getPiwikUrl() . $logo; + } + return $pathToPiwikRoot . '/' . $logo; + } + + public static function getPathUserLogo() + { + return self::rewritePath('misc/user/logo.png'); + } + + public static function getPathUserSvgLogo() + { + return self::rewritePath('misc/user/logo.svg'); + } + + public static function getPathUserLogoSmall() + { + return self::rewritePath('misc/user/logo-header.png'); + } + + protected static function rewritePath($path) + { + return SettingsPiwik::rewriteMiscUserPathWithHostname($path); + } + + public function copyUploadedLogoToFilesystem() + { + + if (empty($_FILES['customLogo']) + || !empty($_FILES['customLogo']['error']) + ) { + return false; + } + + $file = $_FILES['customLogo']['tmp_name']; + if (!file_exists($file)) { + return false; + } + + list($width, $height) = getimagesize($file); + switch ($_FILES['customLogo']['type']) { + case 'image/jpeg': + $image = imagecreatefromjpeg($file); + break; + case 'image/png': + $image = imagecreatefrompng($file); + break; + case 'image/gif': + $image = imagecreatefromgif($file); + break; + default: + return false; + } + + $widthExpected = round($width * self::LOGO_HEIGHT / $height); + $smallWidthExpected = round($width * self::LOGO_SMALL_HEIGHT / $height); + + $logo = imagecreatetruecolor($widthExpected, self::LOGO_HEIGHT); + $logoSmall = imagecreatetruecolor($smallWidthExpected, self::LOGO_SMALL_HEIGHT); + + // Handle transparency + $background = imagecolorallocate($logo, 0, 0, 0); + $backgroundSmall = imagecolorallocate($logoSmall, 0, 0, 0); + imagecolortransparent($logo, $background); + imagecolortransparent($logoSmall, $backgroundSmall); + + if ($_FILES['customLogo']['type'] == 'image/png') { + imagealphablending($logo, false); + imagealphablending($logoSmall, false); + imagesavealpha($logo, true); + imagesavealpha($logoSmall, true); + } + + imagecopyresized($logo, $image, 0, 0, 0, 0, $widthExpected, self::LOGO_HEIGHT, $width, $height); + imagecopyresized($logoSmall, $image, 0, 0, 0, 0, $smallWidthExpected, self::LOGO_SMALL_HEIGHT, $width, $height); + + imagepng($logo, PIWIK_DOCUMENT_ROOT . '/' . $this->getPathUserLogo(), 3); + imagepng($logoSmall, PIWIK_DOCUMENT_ROOT . '/' . $this->getPathUserLogoSmall(), 3); + return true; + } + +}
\ No newline at end of file diff --git a/plugins/CoreAdminHome/templates/generalSettings.twig b/plugins/CoreAdminHome/templates/generalSettings.twig index 3148b46f51..a845f5e48d 100644 --- a/plugins/CoreAdminHome/templates/generalSettings.twig +++ b/plugins/CoreAdminHome/templates/generalSettings.twig @@ -203,12 +203,12 @@ </td> <td style="width:200px;"> <input name="customLogo" type="file" id="customLogo"/> - <img src="misc/user/logo.png?r={{ random() }}" id="currentLogo" height="150"/> + <img src="{{ pathUserLogo }}?r={{ random() }}" id="currentLogo" height="150"/> </td> {% else %} <td> <div style="display:inline-block;margin-top:10px;" id="CoreAdminHome_LogoNotWriteable"> - {{ 'CoreAdminHome_LogoNotWriteable'|translate("<ul style='list-style: disc inside;'><li>/misc/user/</li><li>/misc/user/logo.png</li><li>/misc/user/logo-header.png</li></ul>")|notification({'placeAt': '#CoreAdminHome_LogoNotWriteable', 'noclear': true, 'context': 'warning', 'raw': true}) }} + {{ 'CoreAdminHome_LogoNotWriteable'|translate("<ul style='list-style: disc inside;'><li>"~ pathUserLogoDirectory ~"</li><li>"~ pathUserLogo ~"</li><li>"~ pathUserLogoSmall ~"</li><li>"~ pathUserLogoSVG ~"</li></ul>")|notification({'placeAt': '#CoreAdminHome_LogoNotWriteable', 'noclear': true, 'context': 'warning', 'raw': true}) }} </div> </td> {% endif %} |