Skip to content

Commit

Permalink
Merge pull request #705 from nextcloud/backport-408-psr4-for-theming
Browse files Browse the repository at this point in the history
[stable10] Theming input validation
  • Loading branch information
juliusknorr authored Aug 5, 2016
2 parents f4f43dd + 3d3614d commit efe8f7d
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 75 deletions.
2 changes: 1 addition & 1 deletion apps/theming/appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'v' => \OC::$server->getConfig()->getAppValue('theming', 'cachebuster', '0'),
]
);
\OC_Util::addHeader(
\OCP\Util::addHeader(
'link',
[
'rel' => 'stylesheet',
Expand Down
6 changes: 2 additions & 4 deletions apps/theming/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
*
*/

namespace OCA\Theming\AppInfo;

(new \OCP\AppFramework\App('theming'))->registerRoutes($this, array('routes' => array(
return ['routes' => [
[
'name' => 'Theming#updateStylesheet',
'url' => '/ajax/updateStylesheet',
Expand Down Expand Up @@ -57,5 +55,5 @@
'url' => '/loginbackground',
'verb' => 'GET',
],
)));
]];

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
use OCA\Theming\Template;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataDownloadResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\StreamResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IL10N;
Expand All @@ -47,6 +50,10 @@
class ThemingController extends Controller {
/** @var Template */
private $template;
/** @var Util */
private $util;
/** @var ITimeFactory */
private $timeFactory;
/** @var IL10N */
private $l;
/** @var IConfig */
Expand All @@ -61,6 +68,8 @@ class ThemingController extends Controller {
* @param IRequest $request
* @param IConfig $config
* @param Template $template
* @param Util $util
* @param ITimeFactory $timeFactory
* @param IL10N $l
* @param IRootFolder $rootFolder
*/
Expand All @@ -69,12 +78,16 @@ public function __construct(
IRequest $request,
IConfig $config,
Template $template,
Util $util,
ITimeFactory $timeFactory,
IL10N $l,
IRootFolder $rootFolder
) {
parent::__construct($appName, $request);

$this->template = $template;
$this->util = $util;
$this->timeFactory = $timeFactory;
$this->l = $l;
$this->config = $config;
$this->rootFolder = $rootFolder;
Expand All @@ -87,6 +100,50 @@ public function __construct(
* @internal param string $color
*/
public function updateStylesheet($setting, $value) {
$value = trim($value);
switch ($setting) {
case 'name':
if (strlen($value) > 250) {
return new DataResponse([
'data' => [
'message' => $this->l->t('The given name is too long'),
],
'status' => 'error'
]);
}
break;
case 'url':
if (strlen($value) > 500) {
return new DataResponse([
'data' => [
'message' => $this->l->t('The given web address is too long'),
],
'status' => 'error'
]);
}
break;
case 'slogan':
if (strlen($value) > 500) {
return new DataResponse([
'data' => [
'message' => $this->l->t('The given slogan is too long'),
],
'status' => 'error'
]);
}
break;
case 'color':
if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
return new DataResponse([
'data' => [
'message' => $this->l->t('The given color is invalid'),
],
'status' => 'error'
]);
}
break;
}

$this->template->set($setting, $value);
return new DataResponse(
[
Expand Down Expand Up @@ -166,18 +223,17 @@ public function undo($setting) {
* @PublicPage
* @NoCSRFRequired
*
* @return Http\StreamResponse
* @return StreamResponse|DataResponse
*/
public function getLogo() {
$pathToLogo = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/') . '/themedinstancelogo';
if(!file_exists($pathToLogo)) {
return new DataResponse();
}

\OC_Response::setExpiresHeader(gmdate('D, d M Y H:i:s', time() + (60*60*24*45)) . ' GMT');
\OC_Response::enableCaching();
$response = new Http\StreamResponse($pathToLogo);
$response->cacheFor(3600);
$response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime()));
$response->addHeader('Content-Disposition', 'attachment');
$response->addHeader('Content-Type', $this->config->getAppValue($this->appName, 'logoMime', ''));
return $response;
Expand All @@ -187,18 +243,17 @@ public function getLogo() {
* @PublicPage
* @NoCSRFRequired
*
* @return Http\StreamResponse
* @return StreamResponse|DataResponse
*/
public function getLoginBackground() {
$pathToLogo = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/') . '/themedbackgroundlogo';
if(!file_exists($pathToLogo)) {
return new DataResponse();
}

\OC_Response::setExpiresHeader(gmdate('D, d M Y H:i:s', time() + (60*60*24*45)) . ' GMT');
\OC_Response::enableCaching();
$response = new Http\StreamResponse($pathToLogo);
$response = new StreamResponse($pathToLogo);
$response->cacheFor(3600);
$response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime()));
$response->addHeader('Content-Disposition', 'attachment');
$response->addHeader('Content-Type', $this->config->getAppValue($this->appName, 'backgroundMime', ''));
return $response;
Expand All @@ -208,13 +263,13 @@ public function getLoginBackground() {
* @NoCSRFRequired
* @PublicPage
*
* @return Http\DataDownloadResponse
* @return DataDownloadResponse
*/
public function getStylesheet() {
$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
$responseCss = '';
$color = $this->config->getAppValue($this->appName, 'color');
$elementColor = Util::elementColor($color);
$elementColor = $this->util->elementColor($color);
if($color !== '') {
$responseCss .= sprintf(
'#body-user #header,#body-settings #header,#body-public #header,#body-login,.searchbox input[type="search"]:focus,.searchbox input[type="search"]:active,.searchbox input[type="search"]:valid {background-color: %s}' . "\n",
Expand All @@ -229,7 +284,7 @@ public function getStylesheet() {
$elementColor
);
$responseCss .= 'input[type="radio"].radio:checked:not(.radio--white):not(:disabled) + label:before {' .
'background-image: url(\'data:image/svg+xml;base64,'.Util::generateRadioButton($elementColor).'\');' .
'background-image: url(\'data:image/svg+xml;base64,'.$this->util->generateRadioButton($elementColor).'\');' .
"}\n";
$responseCss .= '
#firstrunwizard .firstrunwizard-header {
Expand Down Expand Up @@ -265,16 +320,15 @@ public function getStylesheet() {
'background-image: url(\'./loginbackground?v='.$cacheBusterValue.'\');' .
'}' . "\n";
}
if(Util::invertTextColor($color)) {
if($this->util->invertTextColor($color)) {
$responseCss .= '#header .header-appname, #expandDisplayName { color: #000000; }' . "\n";
$responseCss .= '#header .icon-caret { background-image: url(\'' . \OC::$WEBROOT . '/core/img/actions/caret-dark.svg\'); }' . "\n";
$responseCss .= '.searchbox input[type="search"] { background: transparent url(\'' . \OC::$WEBROOT . '/core/img/actions/search.svg\') no-repeat 6px center; color: #000; }' . "\n";
$responseCss .= '.searchbox input[type="search"]:focus,.searchbox input[type="search"]:active,.searchbox input[type="search"]:valid { color: #000; border: 1px solid rgba(0, 0, 0, .5); }' . "\n";
}

\OC_Response::setExpiresHeader(gmdate('D, d M Y H:i:s', time() + (60*60*24*45)) . ' GMT');
\OC_Response::enableCaching();
$response = new Http\DataDownloadResponse($responseCss, 'style', 'text/css');
$response = new DataDownloadResponse($responseCss, 'style', 'text/css');
$response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime()));
$response->cacheFor(3600);
return $response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
class Template extends \OC_Defaults {
/** @var IConfig */
private $config;
/** @var IL10N */
/** @var IL10N */
private $l;
/** @var IURLGenerator */
private $urlGenerator;
Expand Down
12 changes: 6 additions & 6 deletions apps/theming/lib/util.php → apps/theming/lib/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class Util {
* @param string $color rgb color value
* @return bool
*/
public static function invertTextColor($color) {
$l = self::calculateLuminance($color);
public function invertTextColor($color) {
$l = $this->calculateLuminance($color);
if($l>0.5) {
return true;
} else {
Expand All @@ -44,8 +44,8 @@ public static function invertTextColor($color) {
* @param $color
* @return string
*/
public static function elementColor($color) {
$l = self::calculateLuminance($color);
public function elementColor($color) {
$l = $this->calculateLuminance($color);
if($l>0.8) {
return '#555555';
} else {
Expand All @@ -57,7 +57,7 @@ public static function elementColor($color) {
* @param string $color rgb color value
* @return float
*/
public static function calculateLuminance($color) {
public function calculateLuminance($color) {
$hex = preg_replace("/[^0-9A-Fa-f]/", '', $color);
if (strlen($hex) === 3) {
$hex = $hex{0} . $hex{0} . $hex{1} . $hex{1} . $hex{2} . $hex{2};
Expand All @@ -75,7 +75,7 @@ public static function calculateLuminance($color) {
* @param $color
* @return string base64 encoded radio button svg
*/
public static function generateRadioButton($color) {
public function generateRadioButton($color) {
$radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' .
'<path d="M8 1a7 7 0 0 0-7 7 7 7 0 0 0 7 7 7 7 0 0 0 7-7 7 7 0 0 0-7-7zm0 1a6 6 0 0 1 6 6 6 6 0 0 1-6 6 6 6 0 0 1-6-6 6 6 0 0 1 6-6zm0 2a4 4 0 1 0 0 8 4 4 0 0 0 0-8z" fill="'.$color.'"/></svg>';
return base64_encode($radioButtonIcon);
Expand Down
4 changes: 1 addition & 3 deletions apps/theming/settings/settings-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*
*/

\OC_Util::checkAdminUser();

$config = \OC::$server->getConfig();
$l = \OC::$server->getL10N('theming');
$urlGenerator = \OC::$server->getURLGenerator();
Expand All @@ -40,7 +38,7 @@
$errorMessage = $l->t('You already use a custom theme');
}

$template = new OCP\Template('theming', 'settings-admin');
$template = new \OCP\Template('theming', 'settings-admin');

$template->assign('themable', $themable);
$template->assign('errorMessage', $errorMessage);
Expand Down
8 changes: 4 additions & 4 deletions apps/theming/templates/settings-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@
<?php } else { ?>
<p>
<label><span><?php p($l->t('Name')) ?></span>
<input id="theming-name" type="text" placeholder="<?php p($l->t('Name')); ?>" value="<?php p($_['name']) ?>" />
<input id="theming-name" type="text" placeholder="<?php p($l->t('Name')); ?>" value="<?php p($_['name']) ?>" maxlength="250" />
</label>
<span data-setting="name" data-toggle="tooltip" data-original-title="<?php p($l->t('reset to default')); ?>" class="theme-undo icon icon-history"></span>
</p>
<p>
<label><span><?php p($l->t('Web address')) ?></span>
<input id="theming-url" type="text" placeholder="<?php p($l->t('Web address https://…')); ?>" value="<?php p($_['url']) ?>" />
<input id="theming-url" type="text" placeholder="<?php p($l->t('Web address https://…')); ?>" value="<?php p($_['url']) ?>" maxlength="500" />
</label>
<span data-setting="url" data-toggle="tooltip" data-original-title="<?php p($l->t('reset to default')); ?>" class="theme-undo icon icon-history"></span>
</p>
<p>
<label><span><?php p($l->t('Slogan')) ?></span>
<input id="theming-slogan" type="text" placeholder="<?php p($l->t('Slogan')); ?>" value="<?php p($_['slogan']) ?>" />
<input id="theming-slogan" type="text" placeholder="<?php p($l->t('Slogan')); ?>" value="<?php p($_['slogan']) ?>" maxlength="500" />
</label>
<span data-setting="slogan" data-toggle="tooltip" data-original-title="<?php p($l->t('reset to default')); ?>" class="theme-undo icon icon-history"></span>
</p>
<p>
<label><span><?php p($l->t('Color')) ?></span>
<input id="theming-color" type="text" class="jscolor" value="<?php p($_['color']) ?>" />
<input id="theming-color" type="text" class="jscolor" maxlength="6" value="<?php p($_['color']) ?>" />
</label>
<span data-setting="color" data-toggle="tooltip" data-original-title="<?php p($l->t('reset to default')); ?>" class="theme-undo icon icon-history"></span>
</p>
Expand Down
Loading

0 comments on commit efe8f7d

Please sign in to comment.