From 73c362fa5200dc0e3d92c17a867f1db6a282925f Mon Sep 17 00:00:00 2001 From: Michael Crawford Date: Tue, 22 Dec 2015 17:20:32 -0500 Subject: [PATCH] Add avatar url customization --- config.ini-example | 5 +++++ src/Application.php | 15 +++++++++++++++ src/Controller/NetworkController.php | 5 +---- themes/bootstrap3/twig/commit.twig | 2 +- themes/bootstrap3/twig/commits_list.twig | 2 +- themes/default/twig/commit.twig | 2 +- themes/default/twig/commits_list.twig | 2 +- 7 files changed, 25 insertions(+), 8 deletions(-) diff --git a/config.ini-example b/config.ini-example index 5c2aa51..30bd699 100644 --- a/config.ini-example +++ b/config.ini-example @@ -32,3 +32,8 @@ title = "" [date] ; timezone = UTC ; format = 'd/m/Y H:i:s' + +; custom avatar service +[avatar] +; url = '//gravatar.com/avatar/' +; query[] = 'd=identicon' \ No newline at end of file diff --git a/src/Application.php b/src/Application.php index f538a77..4217d31 100644 --- a/src/Application.php +++ b/src/Application.php @@ -37,6 +37,8 @@ class Application extends SilexApplication $this['filetypes'] = $config->getSection('filetypes'); $this['binary_filetypes'] = $config->getSection('binary_filetypes'); $this['cache.archives'] = $this->getCachePath() . 'archives'; + $this['avatar.url'] = $config->get('avatar', 'url'); + $this['avatar.query'] = $config->get('avatar', 'query'); // Register services $this->register(new TwigServiceProvider(), array( @@ -67,6 +69,7 @@ class Application extends SilexApplication $twig->addFilter(new \Twig_SimpleFilter('md5', 'md5')); $twig->addFilter(new \Twig_SimpleFilter('format_date', array($app, 'formatDate'))); $twig->addFilter(new \Twig_SimpleFilter('format_size', array($app, 'formatSize'))); + $twig->addFunction(new \Twig_SimpleFunction('avatar', array($app, 'getAvatar'))); return $twig; })); @@ -107,6 +110,18 @@ class Application extends SilexApplication return round($size, 2) . $units[$i]; } + public function getAvatar($email, $size) + { + $url = $this['avatar.url'] ? $this['avatar.url'] : "//gravatar.com/avatar/"; + $query = array("s=$size"); + if (is_string($this['avatar.query'])) + $query[] = $this['avatar.query']; + else if (is_array($this['avatar.query'])) + $query = array_merge($query, $this['avatar.query']); + $id = md5(strtolower($email)); + return $url . $id . "?" . implode('&', $query); + } + public function getPath() { return $this->path . DIRECTORY_SEPARATOR; diff --git a/src/Controller/NetworkController.php b/src/Controller/NetworkController.php index 70f339f..27f8a19 100644 --- a/src/Controller/NetworkController.php +++ b/src/Controller/NetworkController.php @@ -46,10 +46,7 @@ class NetworkController implements ControllerProviderInterface 'author' => array( 'name' => $commit->getAuthor()->getName(), 'email' => $commit->getAuthor()->getEmail(), - // due to the lack of a inbuilt javascript md5 mechanism, build the full avatar url on the php side - 'image' => '//gravatar.com/avatar/' . md5( - strtolower($commit->getAuthor()->getEmail()) - ) . '?s=40' + 'image' => $app->getAvatar($commit->getAuthor()->getEmail(), 40) ) ); } diff --git a/themes/bootstrap3/twig/commit.twig b/themes/bootstrap3/twig/commit.twig index 7293935..7a9863e 100644 --- a/themes/bootstrap3/twig/commit.twig +++ b/themes/bootstrap3/twig/commit.twig @@ -17,7 +17,7 @@ {% if commit.body is not empty %}

{{ commit.body | nl2br }}

{% endif %} - + {{ commit.author.name }} authored on {{ commit.date | format_date }} {% if commit.author.email != commit.commiter.email %} diff --git a/themes/bootstrap3/twig/commits_list.twig b/themes/bootstrap3/twig/commits_list.twig index ba9057b..dd09c2e 100644 --- a/themes/bootstrap3/twig/commits_list.twig +++ b/themes/bootstrap3/twig/commits_list.twig @@ -9,7 +9,7 @@ {% for item in commit %} - + View {{ item.shortHash }}

{{ item.message }}

diff --git a/themes/default/twig/commit.twig b/themes/default/twig/commit.twig index 495a1ce..c80a05f 100644 --- a/themes/default/twig/commit.twig +++ b/themes/default/twig/commit.twig @@ -16,7 +16,7 @@ {% if commit.body is not empty %}

{{ commit.body | nl2br }}

{% endif %} - + {{ commit.author.name }} authored on {{ commit.date | format_date }} {% if commit.author.email != commit.commiter.email %} diff --git a/themes/default/twig/commits_list.twig b/themes/default/twig/commits_list.twig index 619bc97..6521276 100644 --- a/themes/default/twig/commits_list.twig +++ b/themes/default/twig/commits_list.twig @@ -9,7 +9,7 @@ {% for item in commit %} - + View {{ item.shortHash }}

{{ item.message }}