Skip to content

Commit

Permalink
added possibility to upload a favicon
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed May 14, 2014
1 parent 6e2a002 commit e8b4a64
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -12,6 +12,7 @@ misc/*.dat
misc/user/logo-header.png
misc/user/logo.png
misc/user/logo.svg
misc/user/favicon.png
php_errors.log
piwik-min.js
plugins/*.zip
Expand Down Expand Up @@ -53,4 +54,4 @@ tests/PHPUnit/proxy/libs
tests/PHPUnit/proxy/piwik.js
tests/PHPUnit/proxy/plugins
tests/PHPUnit/proxy/tests
config/*.config.ini.php
config/*.config.ini.php
1 change: 1 addition & 0 deletions core/Plugin/Controller.php
Expand Up @@ -606,6 +606,7 @@ protected function setBasicVariablesView($view)

$customLogo = new CustomLogo();
$view->isCustomLogo = $customLogo->isEnabled();
$view->customFavicon = $customLogo->getPathUserFavicon();

$view->logoHeader = \Piwik\Plugins\API\API::getInstance()->getHeaderLogoUrl();
$view->logoLarge = \Piwik\Plugins\API\API::getInstance()->getLogoUrl();
Expand Down
1 change: 1 addition & 0 deletions lang/en.json
Expand Up @@ -142,6 +142,7 @@
"LatestStableRelease": "The latest stable release",
"LogoNotWriteableInstruction": "To use your custom logo instead of the default Piwik logo, give write permission to this directory: %1$s Piwik needs write access for your logos stored in the files %2$s.",
"LogoUpload": "Select a Logo to upload",
"FaviconUpload": "Select a Favicon to upload",
"LogoUploadHelp": "Please upload a file in %s formats with a minimum height of %s pixels.",
"MenuDiagnostic": "Diagnostic",
"MenuGeneralSettings": "General settings",
Expand Down
6 changes: 4 additions & 2 deletions plugins/CoreAdminHome/Controller.php
Expand Up @@ -55,6 +55,7 @@ public function generalSettings()
$view->branding = array('use_custom_logo' => $logo->isEnabled());
$view->logosWriteable = $logo->isCustomLogoWritable();
$view->pathUserLogo = CustomLogo::getPathUserLogo();
$view->pathUserFavicon = CustomLogo::getPathUserFavicon();
$view->pathUserLogoSmall = CustomLogo::getPathUserLogoSmall();
$view->pathUserLogoSVG = CustomLogo::getPathUserSvgLogo();
$view->pathUserLogoDirectory = realpath(dirname($view->pathUserLogo) . '/');
Expand Down Expand Up @@ -260,9 +261,10 @@ public function uploadCustomLogo()
Piwik::checkUserHasSuperUserAccess();

$logo = new CustomLogo();
$success = $logo->copyUploadedLogoToFilesystem();
$successLogo = $logo->copyUploadedLogoToFilesystem();
$successFavicon = $logo->copyUploadedFaviconToFilesystem();

if($success) {
if($successLogo || $successFavicon) {
return '1';
}
return '0';
Expand Down
49 changes: 31 additions & 18 deletions plugins/CoreAdminHome/CustomLogo.php
Expand Up @@ -17,6 +17,7 @@ class CustomLogo
{
const LOGO_HEIGHT = 300;
const LOGO_SMALL_HEIGHT = 100;
const FAVICON_HEIGHT = 32;

public function getLogoUrl($pathOnly = false)
{
Expand Down Expand Up @@ -129,6 +130,11 @@ public static function getPathUserLogo()
return self::rewritePath('misc/user/logo.png');
}

public static function getPathUserFavicon()
{
return self::rewritePath('misc/user/favicon.png');
}

public static function getPathUserSvgLogo()
{
return self::rewritePath('misc/user/logo.svg');
Expand All @@ -146,20 +152,36 @@ protected static function rewritePath($path)

public function copyUploadedLogoToFilesystem()
{
$uploadFieldName = 'customLogo';

$success = $this->uploadImage($uploadFieldName, self::LOGO_SMALL_HEIGHT, $this->getPathUserLogoSmall());
$success = $success && $this->uploadImage($uploadFieldName, self::LOGO_HEIGHT, $this->getPathUserLogo());

return $success;
}

public function copyUploadedFaviconToFilesystem()
{
$uploadFieldName = 'customFavicon';

return $this->uploadImage($uploadFieldName, self::FAVICON_HEIGHT, $this->getPathUserFavicon());
}

if (empty($_FILES['customLogo'])
|| !empty($_FILES['customLogo']['error'])
private function uploadImage($uploadFieldName, $targetHeight, $userPath)
{
if (empty($_FILES[$uploadFieldName])
|| !empty($_FILES[$uploadFieldName]['error'])
) {
return false;
}

$file = $_FILES['customLogo']['tmp_name'];
$file = $_FILES[$uploadFieldName]['tmp_name'];
if (!file_exists($file)) {
return false;
}

list($width, $height) = getimagesize($file);
switch ($_FILES['customLogo']['type']) {
switch ($_FILES[$uploadFieldName]['type']) {
case 'image/jpeg':
$image = imagecreatefromjpeg($file);
break;
Expand All @@ -173,30 +195,21 @@ public function copyUploadedLogoToFilesystem()
return false;
}

$widthExpected = round($width * self::LOGO_HEIGHT / $height);
$smallWidthExpected = round($width * self::LOGO_SMALL_HEIGHT / $height);
$smallWidthExpected = round($width * $targetHeight / $height);

$logo = imagecreatetruecolor($widthExpected, self::LOGO_HEIGHT);
$logoSmall = imagecreatetruecolor($smallWidthExpected, self::LOGO_SMALL_HEIGHT);
$logoSmall = imagecreatetruecolor($smallWidthExpected, $targetHeight);

// 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);
if ($_FILES[$uploadFieldName]['type'] == 'image/png') {
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);
imagecopyresized($logoSmall, $image, 0, 0, 0, 0, $smallWidthExpected, $targetHeight, $width, $height);
imagepng($logoSmall, PIWIK_DOCUMENT_ROOT . '/' . $userPath, 3);
return true;
}

Expand Down
17 changes: 12 additions & 5 deletions plugins/CoreAdminHome/javascripts/generalSettings.js
Expand Up @@ -55,10 +55,14 @@ function isCustomLogoEnabled() {
}

function refreshCustomLogo() {
var imageDiv = $("#currentLogo");
if (imageDiv && imageDiv.attr("src")) {
var logoUrl = imageDiv.attr("src").split("?")[0];
imageDiv.attr("src", logoUrl + "?" + (new Date()).getTime());
var selectors = ['#currentLogo', '#currentFavicon'];
var index;
for (index = 0; index < selectors.length; index++) {
var imageDiv = $(selectors[index]);
if (imageDiv && imageDiv.attr("src")) {
var logoUrl = imageDiv.attr("src").split("?")[0];
imageDiv.attr("src", logoUrl + "?" + (new Date()).getTime());
}
}
}

Expand Down Expand Up @@ -122,7 +126,10 @@ $(document).ready(function () {
submittingForm.attr("target", frameName);
});

$('#customLogo').change(function () {$("#logoUploadForm").submit()});
$('#customLogo,#customFavicon').change(function () {
$("#logoUploadForm").submit();
$(this).val('');
});

// trusted hosts event handling
var trustedHostSettings = $('#trustedHostSettings');
Expand Down
25 changes: 19 additions & 6 deletions plugins/CoreAdminHome/templates/generalSettings.twig
Expand Up @@ -235,18 +235,31 @@
<div id='logoSettings'>
<form id="logoUploadForm" method="post" enctype="multipart/form-data" action="index.php?module=CoreAdminHome&format=json&action=uploadCustomLogo">
<table class="adminTable" style='width:550px;'>
<tr>
{% if logosWriteable %}
{% if logosWriteable %}
<tr>
<td>
<label for="customLogo">{{ 'CoreAdminHome_LogoUpload'|translate }}:<br/>
<span class="form-description">{{ 'CoreAdminHome_LogoUploadHelp'|translate("JPG / PNG / GIF",110) }}</span>
<span class="form-description">{{ 'CoreAdminHome_LogoUploadHelp'|translate("JPG / PNG / GIF", 110) }}</span>
</label>
</td>
<td style="width:200px;">
<input name="customLogo" type="file" id="customLogo"/>
<img src="{{ pathUserLogo }}?r={{ random() }}" id="currentLogo" height="150"/>
</td>
{% else %}
</tr>
<tr>
<td>
<label for="customLogo">{{ 'CoreAdminHome_FaviconUpload'|translate }}:<br/>
<span class="form-description">{{ 'CoreAdminHome_LogoUploadHelp'|translate("JPG / PNG / GIF", 16) }}</span>
</label>
</td>
<td style="width:200px;">
<input name="customFavicon" type="file" id="customFavicon"/>
<img src="{{ pathUserFavicon }}?r={{ random() }}" id="currentFavicon" width="16" height="16"/>
</td>
</tr>
{% else %}
<tr>
<td>
<div style="display:inline-block;margin-top:10px;" id="CoreAdminHome_LogoNotWriteable">
{{ 'CoreAdminHome_LogoNotWriteableInstruction'
Expand All @@ -256,8 +269,8 @@

</div>
</td>
{% endif %}
</tr>
</tr>
{% endif %}
</table>
</form>
</div>
Expand Down
2 changes: 1 addition & 1 deletion plugins/Zeitgeist/templates/admin.twig
Expand Up @@ -8,7 +8,7 @@
<meta charset="utf-8">
<title>{% if not isCustomLogo %}Piwik &rsaquo; {% endif %}{{ 'CoreAdminHome_Administration'|translate }}</title>
<meta name="generator" content="Piwik - Open Source Web Analytics"/>
<link rel="shortcut icon" href="plugins/CoreHome/images/favicon.ico"/>
<link rel="shortcut icon" href="{{ customFavicon|default('plugins/CoreHome/images/favicon.ico') }}"/>

{% include "_jsGlobalVariables.twig" %}
{% include "_piwikTag.twig" %}
Expand Down
2 changes: 1 addition & 1 deletion plugins/Zeitgeist/templates/dashboard.twig
Expand Up @@ -12,7 +12,7 @@
<meta name="generator" content="Piwik - Open Source Web Analytics"/>
<meta name="description" content="Web Analytics report for '{{ siteName|escape("html_attr") }}' - Piwik"/>
<meta name="apple-itunes-app" content="app-id=737216887" />
<link rel="shortcut icon" href="plugins/CoreHome/images/favicon.ico"/>
<link rel="shortcut icon" href="{{ customFavicon|default('plugins/CoreHome/images/favicon.ico') }}"/>
{% include "_jsGlobalVariables.twig" %}
{% include "_piwikTag.twig" %}
<!--[if lt IE 9]>
Expand Down

0 comments on commit e8b4a64

Please sign in to comment.