| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Plugins\CoreAdminHome; |
|
|
| use Piwik\Exception\DI\NotFoundException; |
| use Piwik\Config; |
| use Piwik\Container\StaticContainer; |
| use Piwik\Exception\Exception; |
| use Piwik\Filesystem; |
| use Piwik\Option; |
| use Piwik\Piwik; |
| use Piwik\Plugin\Manager; |
| use Piwik\SettingsPiwik; |
|
|
| class CustomLogo |
| { |
| public const LOGO_HEIGHT = 300; |
| public const LOGO_SMALL_HEIGHT = 100; |
| public const FAVICON_HEIGHT = 32; |
|
|
| public const FILENAME_LOGO = 'logo.png'; |
| public const FILENAME_LOGO_HEADER = 'logo-header.png'; |
| public const FILENAME_LOGO_SVG = 'logo.svg'; |
| public const FILENAME_FAVICON = 'favicon.png'; |
|
|
| public function getLogoUrl($pathOnly = false) |
| { |
| $defaultLogo = 'plugins/Morpheus/images/logo.png'; |
| $themeLogo = 'plugins/%s/images/logo.png'; |
| $userLogo = static::getPathUserLogo(); |
| return $this->getPathToLogo($pathOnly, $defaultLogo, $themeLogo, $userLogo); |
| } |
|
|
| public function getHeaderLogoUrl($pathOnly = false) |
| { |
| $defaultLogo = 'plugins/Morpheus/images/logo.svg'; |
| $themeLogo = 'plugins/%s/images/logo-header.png'; |
| $customLogo = static::getPathUserLogoSmall(); |
| return $this->getPathToLogo($pathOnly, $defaultLogo, $themeLogo, $customLogo); |
| } |
|
|
| public function getSVGLogoUrl($pathOnly = false) |
| { |
| $defaultLogo = 'plugins/Morpheus/images/logo.svg'; |
| $themeLogo = 'plugins/%s/images/logo.svg'; |
| $customLogo = static::getPathUserSvgLogo(); |
| $svg = $this->getPathToLogo($pathOnly, $defaultLogo, $themeLogo, $customLogo); |
| return $svg; |
| } |
|
|
| public function isEnabled() |
| { |
| return $this->isCustomLogoFeatureEnabled() && Option::get('branding_use_custom_logo'); |
| } |
|
|
| public function enable() |
| { |
| Option::set('branding_use_custom_logo', '1', true); |
| } |
|
|
| public function disable() |
| { |
| Option::set('branding_use_custom_logo', '0', true); |
| } |
|
|
| public function hasSVGLogo() |
| { |
| if (!$this->isEnabled()) { |
| |
| return true; |
| } |
|
|
| if ($this->isEnabled() && static::logoExists(static::getPathUserSvgLogo())) { |
| return true; |
| } |
|
|
| return false; |
| } |
|
|
| |
| |
| |
| public function isFileUploadEnabled() |
| { |
| return ini_get('file_uploads') == 1; |
| } |
|
|
| public function isCustomLogoFeatureEnabled() |
| { |
| return Config::getInstance()->General['enable_custom_logo'] != 0; |
| } |
|
|
| |
| |
| |
| public function isCustomLogoWritable() |
| { |
| if (Config::getInstance()->General['enable_custom_logo_check'] == 0) { |
| return true; |
| } |
| $pathUserLogo = $this->getPathUserLogo(); |
|
|
| $directoryWritingTo = PIWIK_DOCUMENT_ROOT . '/' . dirname($pathUserLogo); |
|
|
| |
| Filesystem::mkdir($directoryWritingTo); |
|
|
| $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()); |
|
|
| $isCustomLogoWritable = ($logoFilesWriteable || $directoryWritable) && $this->isFileUploadEnabled(); |
|
|
| return $isCustomLogoWritable; |
| } |
|
|
| protected function getPathToLogo($pathOnly, $defaultLogo, $themeLogo, $customLogo) |
| { |
| $logo = $defaultLogo; |
|
|
| $theme = \Piwik\Plugin\Manager::getInstance()->getThemeEnabled(); |
| if (!$theme) { |
| $themeName = Manager::DEFAULT_THEME; |
| } else { |
| $themeName = $theme->getPluginName(); |
| } |
| $themeLogo = sprintf($themeLogo, $themeName); |
|
|
| if (static::logoExists($themeLogo)) { |
| $logo = $themeLogo; |
| } |
| if ($this->isEnabled() && static::logoExists($customLogo)) { |
| $logo = $customLogo; |
| } |
|
|
| if (!$pathOnly) { |
| return SettingsPiwik::getPiwikUrl() . $logo; |
| } |
|
|
| return Filesystem::getPathToPiwikRoot() . '/' . $logo; |
| } |
|
|
| private static function getBasePath() |
| { |
| try { |
| $basePath = StaticContainer::get('path.misc.user'); |
| return $basePath; |
| } catch (NotFoundException $e) { |
| |
| |
| |
| |
| |
| return 'misc/user/'; |
| } |
| } |
|
|
| public static function getTempPathUserLogoUploads(): string |
| { |
| |
| $path = StaticContainer::get('path.tmp') . '/logos/' . sha1(Piwik::getCurrentUserLogin()) . '/'; |
|
|
| if (!is_dir($path)) { |
| Filesystem::mkdir($path); |
| } |
|
|
| return $path; |
| } |
|
|
| public static function getPathUserLogo(): string |
| { |
| return static::rewritePath(self::getBasePath() . self::FILENAME_LOGO); |
| } |
|
|
| public static function getTempPathUserLogo(): string |
| { |
| return static::getTempPathUserLogoUploads() . self::FILENAME_LOGO; |
| } |
|
|
| public static function getPathUserFavicon(): string |
| { |
| return static::rewritePath(self::getBasePath() . self::FILENAME_FAVICON); |
| } |
|
|
| public static function getTempPathUserFavicon(): string |
| { |
| return static::getTempPathUserLogoUploads() . self::FILENAME_FAVICON; |
| } |
|
|
| public static function getPathUserSvgLogo(): string |
| { |
| return static::rewritePath(self::getBasePath() . self::FILENAME_LOGO_SVG); |
| } |
|
|
| public static function getPathUserLogoSmall(): string |
| { |
| return static::rewritePath(self::getBasePath() . self::FILENAME_LOGO_HEADER); |
| } |
|
|
| public static function getTempPathUserLogoSmall(): string |
| { |
| return static::getTempPathUserLogoUploads() . self::FILENAME_LOGO_HEADER; |
| } |
|
|
| protected static function rewritePath(string $path): string |
| { |
| return SettingsPiwik::rewriteMiscUserPathWithInstanceId($path); |
| } |
|
|
| public static function hasTempLogo(): bool |
| { |
| $logoTempPath = static::getTempPathUserLogo(); |
| $smallLogoTempPath = static::getTempPathUserLogoSmall(); |
|
|
| return (file_exists($logoTempPath) && file_exists($smallLogoTempPath)); |
| } |
|
|
| public static function hasTempFavicon(): bool |
| { |
| $faviconTempPath = static::getTempPathUserFavicon(); |
|
|
| return file_exists($faviconTempPath); |
| } |
|
|
| |
| |
| |
| public static function hasUserLogo() |
| { |
| return static::logoExists(static::getPathUserLogo()); |
| } |
|
|
| |
| |
| |
| public static function hasUserFavicon() |
| { |
| return static::logoExists(static::getPathUserFavicon()); |
| } |
|
|
| private function postLogoChangeEvent($imagePath): void |
| { |
| $rootPath = Filesystem::getPathToPiwikRoot(); |
| $absolutePath = $rootPath . '/' . $imagePath; |
|
|
| |
| |
| |
| |
| |
| |
| Piwik::postEvent('CoreAdminHome.customLogoChanged', [$absolutePath]); |
| } |
|
|
| public function uploadFaviconToTempFolder(): bool |
| { |
| $uploadFieldName = 'customFavicon'; |
|
|
| $faviconTempPath = static::getTempPathUserFavicon(); |
|
|
| return $this->uploadImage($uploadFieldName, self::FAVICON_HEIGHT, $faviconTempPath); |
| } |
|
|
| public function uploadLogoToTempFolder(): bool |
| { |
| $uploadFieldName = 'customLogo'; |
|
|
| $logoTempPath = static::getTempPathUserLogo(); |
| $smallLogoTempPath = static::getTempPathUserLogoSmall(); |
|
|
| $success = $this->uploadImage($uploadFieldName, self::LOGO_SMALL_HEIGHT, $smallLogoTempPath); |
| if (!$success) { |
| return false; |
| } |
|
|
| $success = $this->uploadImage($uploadFieldName, self::LOGO_HEIGHT, $logoTempPath); |
| if (!$success) { |
| return false; |
| } |
|
|
| return true; |
| } |
|
|
| |
| |
| |
| public function publishUserLogo(): bool |
| { |
| $logoTempPath = static::getTempPathUserLogo(); |
| $logoUserPath = static::getPathUserLogo(); |
|
|
| $smallLogoTempPath = static::getTempPathUserLogoSmall(); |
| $smallLogoUserPath = static::getPathUserLogoSmall(); |
|
|
| try { |
| if (file_exists($logoTempPath) && file_exists($smallLogoTempPath)) { |
| Filesystem::copy($logoTempPath, $logoUserPath); |
| Filesystem::copy($smallLogoTempPath, $smallLogoUserPath); |
|
|
| $this->postLogoChangeEvent($logoUserPath); |
| $this->postLogoChangeEvent($smallLogoUserPath); |
|
|
| |
| Filesystem::remove($logoTempPath); |
| Filesystem::remove($smallLogoTempPath); |
|
|
| return true; |
| } |
| } catch (Exception $e) { |
| |
| } |
|
|
| return false; |
| } |
|
|
| |
| |
| |
| public function publishUserFavicon(): bool |
| { |
| $faviconTempPath = static::getTempPathUserFavicon(); |
| $faviconUserPath = static::getPathUserFavicon(); |
|
|
| try { |
| if (file_exists($faviconTempPath)) { |
| Filesystem::copy($faviconTempPath, $faviconUserPath); |
|
|
| $this->postLogoChangeEvent($faviconUserPath); |
|
|
| |
| Filesystem::remove($faviconTempPath); |
|
|
| return true; |
| } |
| } catch (Exception $e) { |
| |
| } |
|
|
| return false; |
| } |
|
|
| |
| |
| |
| public function removeLogos(): void |
| { |
| static::removePublishedLogos(); |
| static::removeLogosFromTempFolder(); |
| } |
|
|
| |
| |
| |
| public function removePublishedLogos(): void |
| { |
| $logoUserPath = static::getPathUserLogo(); |
| $smallLogoUserPath = static::getPathUserLogoSmall(); |
| $faviconUserPath = static::getPathUserFavicon(); |
|
|
| Filesystem::deleteFileIfExists($logoUserPath); |
| Filesystem::deleteFileIfExists($smallLogoUserPath); |
| Filesystem::deleteFileIfExists($faviconUserPath); |
| } |
|
|
| |
| |
| |
| public function removeLogosFromTempFolder(): void |
| { |
| $logosUploadTempFolder = static::getTempPathUserLogoUploads(); |
| Filesystem::unlinkRecursive($logosUploadTempFolder, true); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| private function uploadImage($uploadFieldName, $targetHeight, $path): bool |
| { |
| if ( |
| empty($_FILES[$uploadFieldName]) |
| || !empty($_FILES[$uploadFieldName]['error']) |
| ) { |
| return false; |
| } |
|
|
| $file = $_FILES[$uploadFieldName]['tmp_name']; |
| if (!file_exists($file)) { |
| return false; |
| } |
|
|
| list($width, $height) = getimagesize($file); |
| switch ($_FILES[$uploadFieldName]['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; |
| } |
|
|
| |
| if (!is_resource($image) && !($image instanceof \GdImage)) { |
| return false; |
| } |
|
|
| $targetWidth = round($width * $targetHeight / $height); |
|
|
| $newImage = imagecreatetruecolor($targetWidth, $targetHeight); |
|
|
| if ($_FILES[$uploadFieldName]['type'] == 'image/png') { |
| imagealphablending($newImage, false); |
| imagesavealpha($newImage, true); |
| } |
|
|
| $backgroundColor = imagecolorallocate($newImage, 0, 0, 0); |
| imagecolortransparent($newImage, $backgroundColor); |
|
|
| imagecopyresampled($newImage, $image, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height); |
| return imagepng($newImage, $path, 3); |
| } |
|
|
| |
| |
| |
| private static function logoExists($relativePath) |
| { |
| return file_exists(Filesystem::getPathToPiwikRoot() . '/' . $relativePath); |
| } |
|
|
| |
| |
| |
| public function getTempUserLogoBase64(): ?string |
| { |
| $img = static::getTempPathUserLogo(); |
|
|
| if (file_exists($img)) { |
| return base64_encode(file_get_contents($img)); |
| } |
|
|
| return null; |
| } |
|
|
| |
| |
| |
| public function getTempUserFaviconBase64(): ?string |
| { |
| $img = static::getTempPathUserFavicon(); |
|
|
| if (file_exists($img)) { |
| return base64_encode(file_get_contents($img)); |
| } |
|
|
| return null; |
| } |
| } |
|
|