From 2690ec5053c035872d45e213088cddfb072e326d Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Wed, 31 Oct 2018 13:49:51 +0100 Subject: [PATCH] Introduces avatar.php and replaces all addressbook.photo urls accordingly --- addressbook/inc/class.addressbook_ui.inc.php | 60 --------------- api/avatar.php | 30 ++++++++ api/js/etemplate/et2_widget_image.js | 4 +- api/src/Contacts.php | 74 +++++++++++++++++-- api/src/Framework.php | 3 +- ...ss.calendar_owner_etemplate_widget.inc.php | 6 +- collabeditor/src/Bo.php | 2 +- collabeditor/src/So.php | 2 +- mail/inc/class.mail_compose.inc.php | 3 +- pixelegg/js/fw_mobile.js | 2 +- 10 files changed, 108 insertions(+), 78 deletions(-) create mode 100644 api/avatar.php diff --git a/addressbook/inc/class.addressbook_ui.inc.php b/addressbook/inc/class.addressbook_ui.inc.php index e24140103f..f11fd8a88a 100644 --- a/addressbook/inc/class.addressbook_ui.inc.php +++ b/addressbook/inc/class.addressbook_ui.inc.php @@ -3186,66 +3186,6 @@ window.egw_LAB.wait(function() { } } - /** - * download photo of the given ($_GET['contact_id'] or $_GET['account_id']) contact - */ - function photo() - { - ob_start(); - $contact_id = isset($_GET['contact_id']) ? $_GET['contact_id'] : - (isset($_GET['account_id']) ? 'account:'.$_GET['account_id'] : 0); - - if (substr($contact_id,0,8) == 'account:') - { - $contact_id = $GLOBALS['egw']->accounts->id2name(substr($contact_id,8),'person_id'); - } - if (!($contact = $this->read($contact_id)) || - empty($contact['jpegphoto']) && // LDAP/AD (not updated SQL) - !(($contact['files'] & Api\Contacts::FILES_BIT_PHOTO) && // new SQL in VFS - ($size = filesize($url=Api\Link::vfs_path('addressbook', $contact_id, Api\Contacts::FILES_PHOTO))))) - { - if (is_array($contact)) - { - echo Api\avatar::lavatar(array( - 'id' => $contact['id'], - 'firstname' => $contact['n_given'], - 'lastname' => $contact['n_family']) - ); - exit(); - } - Egw::redirect(Api\Image::find('addressbook','photo')); - } - // use an etag over the image mapp - $etag = '"'.$contact_id.':'.$contact['etag'].'"'; - if (!ob_get_contents()) - { - header('Content-type: image/jpeg'); - header('ETag: '.$etag); - // if etag parameter given in url, we can allow browser to cache picture via an Expires header - // different url with different etag parameter will force a reload - if (isset($_GET['etag'])) - { - Api\Session::cache_control(30*86400); // cache for 30 days - } - // if servers send a If-None-Match header, response with 304 Not Modified, if etag matches - if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] === $etag) - { - header("HTTP/1.1 304 Not Modified"); - } - elseif(!empty($contact['jpegphoto'])) - { - header('Content-length: '.bytes($contact['jpegphoto'])); - echo $contact['jpegphoto']; - } - else - { - header('Content-length: '.$size); - readfile($url); - } - exit(); - } - } - /** * Migrate contacts to or from LDAP (called by Admin >> Addressbook >> Site configuration (Admin only) * diff --git a/api/avatar.php b/api/avatar.php new file mode 100644 index 0000000000..dbe473a48b --- /dev/null +++ b/api/avatar.php @@ -0,0 +1,30 @@ + + * @package api + * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License + */ + +use EGroupware\Api; + +// switch evtl. set output-compression off, as we cant calculate a Content-Length header with transparent compression +ini_set('zlib.output_compression', 0); + +$GLOBALS['egw_info'] = array( + 'flags' => array( + 'currentapp' => 'api', + 'noheader' => true, + 'nocachecontrol' => true, + ) +); + +include '../header.inc.php'; + +$contacts = new Api\Contacts(); + +$contacts->photo(); \ No newline at end of file diff --git a/api/js/etemplate/et2_widget_image.js b/api/js/etemplate/et2_widget_image.js index ac4789a4c6..7a413b673b 100644 --- a/api/js/etemplate/et2_widget_image.js +++ b/api/js/etemplate/et2_widget_image.js @@ -361,7 +361,7 @@ var et2_avatar = (function(){ "use strict"; return et2_image.extend( set_contact_id: function(_contact_id) { var params = { - menuaction: 'addressbook.addressbook_ui.photo' + id:'' }; var id = 'contact_id'; @@ -383,7 +383,7 @@ var et2_avatar = (function(){ "use strict"; return et2_image.extend( params[id] = _contact_id; this.image.addClass('et2_avatar'); - var url = egw.link('/index.php',params); + var url = egw.link('/api/avatar.php',params); this.set_src(url); }, diff --git a/api/src/Contacts.php b/api/src/Contacts.php index ad91a0cdf1..471ee976ce 100755 --- a/api/src/Contacts.php +++ b/api/src/Contacts.php @@ -759,17 +759,16 @@ class Contacts extends Contacts\Storage * @param boolean $jpeg =false jpeg exists or not * @param string $default ='' image-name to use if !$jpeg, eg. 'template' * @param string $etag =null etag to set in url to allow caching with Expires header - * @return string/array + * @return string */ function photo_src($id,$jpeg,$default='',$etag=null) { //error_log(__METHOD__."($id, ..., etag=$etag) ". function_backtrace()); - return $jpeg || !$default ? array( - 'menuaction' => 'addressbook.addressbook_ui.photo', - 'contact_id' => $id, + return $jpeg || !$default ? Egw::link('/api/avatar.php', array( + 'contact_id' => $id )+(isset($etag) ? array( 'etag' => $etag, - ) : array()) : $default; + ) : array())) : $default; } /** @@ -2585,4 +2584,69 @@ class Contacts extends Contacts\Storage //error_log(__METHOD__.'('.array2string($owner).') returning '.array2string($ctag)); return $ctag; } + + /** + * download photo of the given ($_GET['contact_id'] or $_GET['account_id']) contact + */ + function photo() + { + ob_start(); + + $contact_id = isset($_GET['contact_id']) ? $_GET['contact_id'] : + (isset($_GET['account_id']) ? 'account:'.$_GET['account_id'] : 0); + + if (substr($contact_id,0,8) == 'account:') + { + $contact_id = $GLOBALS['egw']->accounts->id2name(substr($contact_id,8),'person_id'); + } + + $contact = $this->read($contact_id); + + if (!($contact) || + empty($contact['jpegphoto']) && // LDAP/AD (not updated SQL) + !(($contact['files'] & \EGroupware\Api\Contacts::FILES_BIT_PHOTO) && // new SQL in VFS + ($size = filesize($url= \EGroupware\Api\Link::vfs_path('addressbook', $contact_id, \EGroupware\Api\Contacts::FILES_PHOTO))))) + { + if (is_array($contact)) + { + echo \EGroupware\Api\avatar::lavatar(array( + 'id' => $contact['id'], + 'firstname' => $contact['n_given'], + 'lastname' => $contact['n_family']) + ); + exit(); + } + Egw::redirect(\EGroupware\Api\Image::find('addressbook','photo')); + } + + // use an etag over the image mapp + $etag = '"'.$contact_id.':'.$contact['etag'].'"'; + if (!ob_get_contents()) + { + header('Content-type: image/jpeg'); + header('ETag: '.$etag); + // if etag parameter given in url, we can allow browser to cache picture via an Expires header + // different url with different etag parameter will force a reload + if (isset($_GET['etag'])) + { + \EGroupware\Api\Session::cache_control(30*86400); // cache for 30 days + } + // if servers send a If-None-Match header, response with 304 Not Modified, if etag matches + if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] === $etag) + { + header("HTTP/1.1 304 Not Modified"); + } + elseif(!empty($contact['jpegphoto'])) + { + header('Content-length: '.bytes($contact['jpegphoto'])); + echo $contact['jpegphoto']; + } + else + { + header('Content-length: '.$size); + readfile($url); + } + exit(); + } + } } diff --git a/api/src/Framework.php b/api/src/Framework.php index 5c58e109ae..aa77cc7397 100644 --- a/api/src/Framework.php +++ b/api/src/Framework.php @@ -1496,8 +1496,7 @@ abstract class Framework extends Framework\Extra foreach($key_pair as $account_id => $name) { $contact = $contact_obj->read('account:'.$account_id, true); - $accounts[] = array('value' => $account_id, 'label' => $name, 'icon' => self::link('/index.php', array( - 'menuaction' => 'addressbook.addressbook_ui.photo', + $accounts[] = array('value' => $account_id, 'label' => $name, 'icon' => self::link('/api/avatar.php', array( 'contact_id' => $contact['id'], 'etag' => $contact['etag'] ))); diff --git a/calendar/inc/class.calendar_owner_etemplate_widget.inc.php b/calendar/inc/class.calendar_owner_etemplate_widget.inc.php index 7f2fee7957..71113190a3 100644 --- a/calendar/inc/class.calendar_owner_etemplate_widget.inc.php +++ b/calendar/inc/class.calendar_owner_etemplate_widget.inc.php @@ -70,8 +70,7 @@ class calendar_owner_etemplate_widget extends Etemplate\Widget\Taglist 'value' => ''.$account_id, 'label' => $account_name, 'app' => lang('api-accounts'), - 'icon' => Api\Framework::link('/index.php', array( - 'menuaction' => 'addressbook.addressbook_ui.photo', + 'icon' => Api\Framework::link('/api/avatar.php', array( 'contact_id' => $contact['id'], 'etag' => $contact['etag'] ? $contact['etag'] : 1 )) @@ -244,8 +243,7 @@ class calendar_owner_etemplate_widget extends Etemplate\Widget\Taglist case 'c': case '': $contact = $contacts_obj->read($type === '' ? 'account:'.$id : $id, true); - if (is_array($contact)) $value['icon'] = Api\Framework::link('/index.php', array( - 'menuaction' => 'addressbook.addressbook_ui.photo', + if (is_array($contact)) $value['icon'] = Api\Framework::link('/api/avatar.php', array( 'contact_id' => $contact['id'], 'etag' => $contact['etag'] ? $contact['etag'] : 1 )); diff --git a/collabeditor/src/Bo.php b/collabeditor/src/Bo.php index 3728988b6f..c8f4e6022e 100644 --- a/collabeditor/src/Bo.php +++ b/collabeditor/src/Bo.php @@ -214,7 +214,7 @@ class Bo extends So { 'setProperties' => array( 'fullName' => $GLOBALS['egw_info']['user']['account_fullname'], 'color' => $GLOBALS['egw_info']['user']['preferences']['filemanager']['collab_user_color'], - 'imageUrl' => $GLOBALS['egw_info']['server']['webserver_url'].'/index.php?menuaction=addressbook.addressbook_ui.photo&account_id='.$use_id, + 'imageUrl' => $GLOBALS['egw_info']['server']['webserver_url'].'/api/avatar.php?account_id='.$use_id, 'uid' => $use_id ) ) diff --git a/collabeditor/src/So.php b/collabeditor/src/So.php index 1726b67d31..5b7137fb75 100644 --- a/collabeditor/src/So.php +++ b/collabeditor/src/So.php @@ -79,7 +79,7 @@ class So $color = $GLOBALS['egw_info']['user']['preferences']['filemanager']['collab_user_color']; $user_id = $GLOBALS['egw_info']['user']['account_id']; - $imageUrl = $GLOBALS['egw_info']['server']['webserver_url'].'/index.php?menuaction=addressbook.addressbook_ui.photo&account_id='.$user_id; + $imageUrl = $GLOBALS['egw_info']['server']['webserver_url'].'/api/avatar.php?account_id='.$user_id; if (!($result = self::db2id($query->fetchRow())) && $es_id) { diff --git a/mail/inc/class.mail_compose.inc.php b/mail/inc/class.mail_compose.inc.php index fa87043ea5..37d24b2fca 100644 --- a/mail/inc/class.mail_compose.inc.php +++ b/mail/inc/class.mail_compose.inc.php @@ -3647,8 +3647,7 @@ class mail_compose // Add just name for nice display, with title for hover 'name' => $contact['n_fn'], 'title' => $email, - 'icon' => Egw::link('/index.php', array( - 'menuaction' => 'addressbook.addressbook_ui.photo', + 'icon' => Egw::link('/api/avatar.php', array( 'contact_id' => $contact['id'], 'etag' => $contact['etag'] )) diff --git a/pixelegg/js/fw_mobile.js b/pixelegg/js/fw_mobile.js index b681b11906..cec24a685b 100644 --- a/pixelegg/js/fw_mobile.js +++ b/pixelegg/js/fw_mobile.js @@ -400,7 +400,7 @@ var $user = jQuery('#egw_fw_userinfo .user'); var $avatar = jQuery('#egw_fw_userinfo .avatar img'); - $avatar.attr('src', egw.webserverUrl + '/index.php?menuaction=addressbook.addressbook_ui.photo&account_id=' + egw.user('account_id')); + $avatar.attr('src', egw.webserverUrl + '/api/avatar.php?account_id=' + egw.user('account_id')); var $sidebar = jQuery('#egw_fw_sidebar'); $sidebar.removeClass('avatarSubmenu');