From d95db6493f5bd74089776868c1bd1715f5eccacc Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Wed, 1 Sep 2010 11:34:33 -0600 Subject: [PATCH] Improve Horde#url() API. Add support for options array to make things clearer. Also, give ability to specifically define which app's webroot is desired for URL creation. --- framework/Core/lib/Horde.php | 76 ++++++++++++++++++++++--------------- horde/admin/groups.php | 7 ++-- horde/config/hooks.php.dist | 2 +- horde/services/facebook.php | 4 +- horde/services/obrowser/index.php | 5 +-- horde/services/portal/mobile.php | 2 +- horde/services/problem.php | 2 +- horde/services/twitter.php | 2 +- horde/templates/admin/user/list.inc | 9 +++-- horde/templates/login/login.inc | 4 +- imp/config/hooks.php.dist | 16 ++++---- imp/lib/Mime/Viewer/Html.php | 2 +- imp/rss.php | 6 +-- kronolith/config/prefs.php.dist | 2 +- 14 files changed, 78 insertions(+), 61 deletions(-) diff --git a/framework/Core/lib/Horde.php b/framework/Core/lib/Horde.php index 62ab2e0ab..d2a750d8a 100644 --- a/framework/Core/lib/Horde.php +++ b/framework/Core/lib/Horde.php @@ -461,40 +461,41 @@ HTML; */ static public function getServiceLink($type, $app = null) { - $webroot = $GLOBALS['registry']->get('webroot', 'horde'); + $opts = array('app' => 'horde'); switch ($type) { case 'ajax': - return self::url($webroot . '/services/ajax.php/' . $app . '/') + return self::url('services/ajax.php/' . $app . '/', false, $opts) ->remove('ajaxui'); case 'cache': - return self::url($webroot . '/services/cache.php', false, -1); + $opts['append_session'] = -1; + return self::url('services/cache.php', false, $opts); case 'download': - return self::url($webroot . '/services/download/') + return self::url('services/download/', false, $opts) ->add('module', $app); case 'emailconfirm': - return self::url($webroot . '/services/confirm.php'); + return self::url('services/confirm.php', false, $opts); case 'go': - return self::url($webroot . '/services/go.php') + return self::url('services/go.php', false, $opts) ->remove('ajaxui'); case 'help': - return self::url($webroot . '/services/help/') + return self::url('services/help/', false, $opts) ->add('module', $app); case 'imple': - return self::url($webroot . '/services/imple.php')-> - remove('ajaxui'); + return self::url('services/imple.php', false, $opts) + ->remove('ajaxui'); case 'login': - return self::url($webroot . '/login.php'); + return self::url('login.php', false, $opts); case 'logintasks': - return self::url($webroot . '/services/logintasks.php') + return self::url('services/logintasks.php', false, $opts) ->add('app', $app); case 'logout': @@ -503,7 +504,7 @@ HTML; case 'options': case 'prefsapi': if (!in_array($GLOBALS['conf']['prefs']['driver'], array('', 'none'))) { - $url = self::url($webroot . ($type == 'options' ? '/services/prefs.php' : '/services/prefs/')); + $url = self::url(($type == 'options') ? 'services/prefs.php' : 'services/prefs/', false, $opts); if (!is_null($app)) { $url->add('app', $app); } @@ -512,11 +513,11 @@ HTML; break; case 'problem': - return self::url($webroot . '/services/problem.php') + return self::url('services/problem.php', false, $opts) ->add('return_url', urlencode(self::selfUrl(true, true, true))); case 'sidebar': - return self::url($webroot . '/services/sidebar.php'); + return self::url('services/sidebar.php', false, $opts); } return false; @@ -883,27 +884,40 @@ HTML; * If a full URL is requested, all parameter separators get converted to * "&", otherwise to "&". * - * @param mixed $uri The URI to be modified (either a string - * or any object with a __toString() - * function). - * @param boolean $full Generate a full (http://server/path/) - * URL. - * @param integer $append_session 0 = only if needed, 1 = always, -1 = - * never. - * @param boolean $force_ssl Ignore $conf['use_ssl'] and force - * creation of a SSL URL? + * @param mixed $uri The URI to be modified (either a string or any + * object with a __toString() function). + * @param boolean $full Generate a full (http://server/path/) URL. + * @param mixed $opts Additional options. If a string/integer, it is + * taken to be the 'append_session' option. If an + * array, one of the following: + *
+     * 'app' - (string) Use this app for the webroot.
+     *         DEFAULT: current application
+     * 'append_session' - (integer) 0 = only if needed [DEFAULT], 1 = always,
+     *                    -1 = never.
+     * 'force_ssl' - (boolean) Ignore $conf['use_ssl'] and force creation of a
+     *               SSL URL?
+     *               DEFAULT: false
+     * 
* * @return Horde_Url The URL with the session id appended (if needed). */ - static public function url($uri, $full = false, $append_session = 0, - $force_ssl = false) + static public function url($uri, $full = false, $opts = array()) { - if ($force_ssl) { - $full = true; + if (is_array($opts)) { + $append_session = isset($opts['append_session']) + ? $opts['append_session'] + : 0; + if (!empty($opts['force_ssl'])) { + $full = true; + } + } else { + $append_session = $opts; + $opts = array(); } $url = ''; - $webroot = $GLOBALS['registry']->get('webroot'); + $webroot = $GLOBALS['registry']->get('webroot', empty($opts['app']) ? null : $opts['app']); /* Skip if we already have a full URL. */ if ($full && !preg_match('|^([\w+-]{1,20})://|', $webroot)) { @@ -925,7 +939,7 @@ HTML; case 3: $server_port = ''; - if ($force_ssl) { + if (!empty($opts['force_ssl'])) { $protocol = 'https'; } break; @@ -1237,7 +1251,7 @@ HTML; } } - $url = self::url($url, $full, 0, $force_ssl); + $url = self::url($url, $full, array('force_ssl' => $force_ssl)); return ($nocache && $GLOBALS['browser']->hasQuirk('cache_same_url')) ? $url->unique() @@ -1316,7 +1330,7 @@ HTML; /* If we can send as data, no need to get the full path */ $src = self::base64ImgData($src); if (substr($src, 0, 10) != 'data:image') { - $src = self::url($src, true, -1); + $src = self::url($src, true, array('append_session' => -1)); } $img = ' strval(Horde_Themes::img('group.png'))); -$add = Horde::url('admin/groups.php?actionID=addchild'); +$group_url = Horde::url('admin/groups.php'); +$add = $group_url->copy()->add('actionID', 'addchild'); $add_img = Horde::img('add_group.png'); -$edit = Horde::url('admin/groups.php?actionID=edit'); -$delete = Horde::url('admin/groups.php?actionID=delete'); +$edit = $group_url->copy()->add('actionID', 'edit'); +$delete = $group_url->copy()->add('actionID', 'delete'); $edit_img = Horde::img('edit.png', _("Edit Group")); $delete_img = Horde::img('delete.png', _("Delete Group")); diff --git a/horde/config/hooks.php.dist b/horde/config/hooks.php.dist index bd354acf7..4f1c09e37 100644 --- a/horde/config/hooks.php.dist +++ b/horde/config/hooks.php.dist @@ -506,7 +506,7 @@ class Horde_Hooks // "\n\n" . // $this->_signup_queued_walkdata($extraFields, $data) . // "\n" . -// sprintf(_("You can approve this signup at %s"), Horde::url('admin/user.php', true, -1)); +// sprintf(_("You can approve this signup at %s"), Horde::url('admin/user.php', true, array('append_session' => -1))); // // $GLOBALS['injector']->getInstance('Horde_Mail')->send($_SERVER['SERVER_ADMIN'], $headers, $msg); // } diff --git a/horde/services/facebook.php b/horde/services/facebook.php index dd97f7a67..ff60cb9ef 100644 --- a/horde/services/facebook.php +++ b/horde/services/facebook.php @@ -16,7 +16,7 @@ Horde_Registry::appInit('horde'); try { $facebook = $GLOBALS['injector']->getInstance('Horde_Service_Facebook'); } catch (Horde_Exception $e) { - Horde::url($registry->get('webroot', 'horde') . '/index.php')->redirect(); + Horde::url('index.php', false, array('app' => 'horde'))->redirect(); } /* See why we are here. */ @@ -36,7 +36,7 @@ if ($token = Horde_Util::getFormData('auth_token')) { $uid = $facebook->auth->getUser(); $prefs->setValue('facebook', serialize(array('uid' => $uid, 'sid' => $sid))); $notification->push(_("Succesfully connected your Facebook account."), 'horde.success'); - Horde::url('services/prefs.php', true)->add(array('group' => 'facebook', 'app' => 'horde'))->redirect(); + Horde::url('services/prefs.php', true)->add(array('group' => 'facebook', 'app' => 'horde'))->redirect(); } } else { diff --git a/horde/services/obrowser/index.php b/horde/services/obrowser/index.php index d1325bc36..145cab616 100644 --- a/horde/services/obrowser/index.php +++ b/horde/services/obrowser/index.php @@ -84,9 +84,8 @@ foreach ($list as $path => $values) { // Set the name/link. if (!empty($values['browseable'])) { - $url = Horde::url($registry->get('webroot', 'horde') . '/services/obrowser/'); - $url = Horde_Util::addParameter($url, 'path', $path); - $row['name'] = Horde::link($url) . htmlspecialchars($values['name']) . ''; + $url = Horde::url('services/obrowser', false, array('app' => 'horde'))->add('path', $path); + $row['name'] = $url->link() . htmlspecialchars($values['name']) . ''; } else { $js = "return chooseObject('" . addslashes($path) . "');"; $row['name'] = Horde::link('#', sprintf(_("Choose %s"), $values['name']), '', '', $js) . htmlspecialchars($values['name']) . ''; diff --git a/horde/services/portal/mobile.php b/horde/services/portal/mobile.php index 681142f33..ea1cf310f 100644 --- a/horde/services/portal/mobile.php +++ b/horde/services/portal/mobile.php @@ -22,7 +22,7 @@ if (empty($fullname)) { $links = array(); foreach ($registry->listApps() as $app) { if ($registry->hasMobileView($app)) { - $links[htmlspecialchars($registry->get('name', $app))] = Horde::url($registry->get('webroot', $app) . '/'); + $links[htmlspecialchars($registry->get('name', $app))] = Horde::url('/', false, array('app' => $app)); } } diff --git a/horde/services/problem.php b/horde/services/problem.php index de5fb96a2..c2b09787f 100644 --- a/horde/services/problem.php +++ b/horde/services/problem.php @@ -11,7 +11,7 @@ /* Send the browser back to the correct page. */ function _returnToPage() { - $url = new Horde_Url(Horde_Util::getFormData('return_url', Horde::url($GLOBALS['registry']->get('webroot', 'horde') . '/login.php', true))); + $url = new Horde_Url(Horde_Util::getFormData('return_url', Horde::url('login.php', true, array('app' => 'horde')))); $url->redirect(); } diff --git a/horde/services/twitter.php b/horde/services/twitter.php index 703e8ef9f..acd61b956 100644 --- a/horde/services/twitter.php +++ b/horde/services/twitter.php @@ -15,7 +15,7 @@ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('horde'); if (empty($conf['twitter']['enabled'])) { - Horde::url($registry->get('webroot', 'horde') . '/index.php')->redirect(); + Horde::url('index.php', false, array('app' => 'horde'))->redirect(); } $twitter = $GLOBALS['injector']->getInstance('Horde_Service_Twitter'); diff --git a/horde/templates/admin/user/list.inc b/horde/templates/admin/user/list.inc index e00c1b2fc..5bc296b3b 100644 --- a/horde/templates/admin/user/list.inc +++ b/horde/templates/admin/user/list.inc @@ -21,17 +21,20 @@ hasCapability('remove'); $total = 0; +$admin_url = Horde::url('user.php'); + foreach ($users as $user): if ($total++ < $min || $total > $max) { continue; } + $user_url = $admin_url->copy()->add('user_name', $user); ?> - + add('form', 'remove_f'), _("Delete")) . Horde::img('delete.png', _("Delete")) ?> - - + add('form', 'update_f'), _("Update")) . Horde::img('edit.png', _("Update")) ?> + add('form', 'clear_f'), _("Clear user data")) . Horde::img('reload.png', _("Clear user data")) ?> diff --git a/horde/templates/login/login.inc b/horde/templates/login/login.inc index 1c3b42780..d4f55d9bb 100644 --- a/horde/templates/login/login.inc +++ b/horde/templates/login/login.inc @@ -69,7 +69,7 @@   - get('webroot', 'horde') . '/signup.php')->add('url', $vars->url), _("Don't have an account? Sign up."), 'light') . _("Don't have an account? Sign up.") ?> + 'horde'))->add('url', $vars->url), _("Don't have an account? Sign up."), 'light') . _("Don't have an account? Sign up.") ?> @@ -78,7 +78,7 @@   - get('webroot', 'horde') . '/services/resetpassword.php')->add('url', $vars->url), _("Forgot your password?"), 'light') . _("Forgot your password?") ?> + 'horde'))->add('url', $vars->url), _("Forgot your password?"), 'light') . _("Forgot your password?") ?> diff --git a/imp/config/hooks.php.dist b/imp/config/hooks.php.dist index 90e5ae17a..6092f3019 100644 --- a/imp/config/hooks.php.dist +++ b/imp/config/hooks.php.dist @@ -302,16 +302,16 @@ class IMP_Hooks // // name. // if ((stripos($mailbox, "INBOX/Calendar") !== false) || // preg_match("!^user/[^/]+/Calendar!", $mailbox)) { -// return $GLOBALS['registry']->get('webroot', 'kronolith'); +// return Horde::url('', false, array('app' => 'kronolith')); // } elseif ((stripos($mailbox, "INBOX/Tasks") !== false) || // preg_match("!^user/[^/]+/Tasks!", $mailbox)) { -// return $GLOBALS['registry']->get('webroot', 'nag'); +// return Horde::url('', false, array('app' => 'nag')); // } elseif ((strpos($mailbox, "INBOX/Notes") !== false) || // preg_match("!^user/[^/]+/Notes!", $mailbox)) { -// return $GLOBALS['registry']->get('webroot', 'mnemo'); +// return Horde::url('', false, array('app' => 'mnemo')); // } elseif ((strpos($mailbox, "INBOX/Contacts") !== false) || // preg_match("!^user/[^/]+/Contacts!", $mailbox)) { -// return $GLOBALS['registry']->get('webroot', 'turba'); +// return Horde::url('', false, array('app' => 'turba')); // } // // return ''; @@ -321,16 +321,16 @@ class IMP_Hooks // require_once 'Horde/Kolab.php'; // switch (Kolab::getMailboxType($mailbox)) { // case 'event': -// return $GLOBALS['registry']->get('webroot', 'kronolith'); +// return Horde::url('', false, array('app' => 'kronolith')); // // case 'task': -// return $GLOBALS['registry']->get('webroot', 'nag'); +// return Horde::url('', false, array('app' => 'nag')); // // case 'note': -// return $GLOBALS['registry']->get('webroot', 'mnemo'); +// return Horde::url('', false, array('app' => 'mnemo')); // // case 'contact': -// return $GLOBALS['registry']->get('webroot', 'turba'); +// return Horde::url('', false, array('app' => 'turba')); // // case 'prefs': // return Horde::getServiceLink('options', 'horde'); diff --git a/imp/lib/Mime/Viewer/Html.php b/imp/lib/Mime/Viewer/Html.php index 8ecb058db..9c6ccb150 100644 --- a/imp/lib/Mime/Viewer/Html.php +++ b/imp/lib/Mime/Viewer/Html.php @@ -151,7 +151,7 @@ class IMP_Mime_Viewer_Html extends Horde_Mime_Viewer_Html /* Image filtering. */ if ($this->_imptmp['img']) { - $this->_imptmp['blockimg'] = Horde::url(Horde_Themes::img('spacer_red.png'), true, -1); + $this->_imptmp['blockimg'] = Horde::url(Horde_Themes::img('spacer_red.png'), true, array('append_session' => -1)); } /* Search for inlined images that we can display diff --git a/imp/rss.php b/imp/rss.php index c4de0ac33..57bfd809d 100644 --- a/imp/rss.php +++ b/imp/rss.php @@ -72,7 +72,7 @@ if (count($ids)) { 'title' => $imp_ui->getSubject($ob['envelope']['subject']), 'pubDate' => date('r', strtotime($ob['envelope']['date'])), 'description' => isset($ob['preview']) ? $ob['preview'] : '', - 'url' => Horde::url(IMP::generateIMPUrl('message.php', $mailbox, $ob['uid'], $mailbox), true, -1), + 'url' => Horde::url(IMP::generateIMPUrl('message.php', $mailbox, $ob['uid'], $mailbox), true, array('append_session' => -1)), 'fromAddr' => $from_addr['fullfrom'], 'toAddr' => Horde_Mime_Address::addrArray2String(isset($ob['envelope']['to']) ? $ob['envelope']['to'] : array(), array('charset' => $registry->getCharset())) )); @@ -90,7 +90,7 @@ $t->set('pubDate', htmlspecialchars(date('r'))); $t->set('desc', htmlspecialchars($description)); $t->set('title', htmlspecialchars($registry->get('name') . ' - ' . IMP::getLabel($mailbox))); $t->set('items', $items, true); -$t->set('url', htmlspecialchars(Horde::url(IMP::generateIMPUrl('message.php', $mailbox), true, -1))); -$t->set('rss_url', htmlspecialchars(Horde::url('rss.php', true, -1))); +$t->set('url', htmlspecialchars(Horde::url(IMP::generateIMPUrl('message.php', $mailbox), true, array('append_session' => -1)))); +$t->set('rss_url', htmlspecialchars(Horde::url('rss.php', true, array('append_session' => -1)))); $browser->downloadHeaders('mailbox.rss', 'text/xml', true); echo $t->fetch(IMP_TEMPLATES . '/rss/mailbox.rss'); diff --git a/kronolith/config/prefs.php.dist b/kronolith/config/prefs.php.dist index f7633d9f6..e7797a070 100644 --- a/kronolith/config/prefs.php.dist +++ b/kronolith/config/prefs.php.dist @@ -341,7 +341,7 @@ $_prefs['search_fields'] = array( ); $_prefs['fb_url'] = array( - 'value' => '' . _("My Free/Busy URL") . '
' . _("Copy this URL for use wherever you need your Free/Busy URL:") . '
' . Horde::url('fb.php', true, -1)->add('u', $GLOBALS['registry']->getAuth()) . '
', + 'value' => '' . _("My Free/Busy URL") . '
' . _("Copy this URL for use wherever you need your Free/Busy URL:") . '
' . Horde::url('fb.php', true, array('append_session' => -1))->add('u', $GLOBALS['registry']->getAuth()) . '
', 'type' => 'rawhtml' ); -- 2.11.0