Improve Horde#url() API.
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 1 Sep 2010 17:34:33 +0000 (11:34 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 1 Sep 2010 17:50:53 +0000 (11:50 -0600)
Add support for options array to make things clearer.  Also, give
ability to specifically define which app's webroot is desired for URL
creation.

14 files changed:
framework/Core/lib/Horde.php
horde/admin/groups.php
horde/config/hooks.php.dist
horde/services/facebook.php
horde/services/obrowser/index.php
horde/services/portal/mobile.php
horde/services/problem.php
horde/services/twitter.php
horde/templates/admin/user/list.inc
horde/templates/login/login.inc
imp/config/hooks.php.dist
imp/lib/Mime/Viewer/Html.php
imp/rss.php
kronolith/config/prefs.php.dist

index 62ab2e0..d2a750d 100644 (file)
@@ -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 "&amp;".
      *
-     * @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:
+     * <pre>
+     * '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
+     * </pre>
      *
      * @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 = '<img';
index 2fcd1ca..4000ee7 100644 (file)
@@ -206,10 +206,11 @@ $nodes[Horde_Group::ROOT] = Horde_Group::ROOT;
 /* Set up some node params. */
 $spacer = '&nbsp;&nbsp;&nbsp;&nbsp;';
 $group_node = array('icon' => 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"));
 
index bd354ac..4f1c09e 100644 (file)
@@ -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);
 //    }
index dd97f7a..ff60cb9 100644 (file)
@@ -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 {
 
index d1325bc..145cab6 100644 (file)
@@ -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']) . '</a>';
+        $url = Horde::url('services/obrowser', false, array('app' => 'horde'))->add('path', $path);
+        $row['name'] = $url->link() . htmlspecialchars($values['name']) . '</a>';
     } else {
         $js = "return chooseObject('" . addslashes($path) . "');";
         $row['name'] = Horde::link('#', sprintf(_("Choose %s"), $values['name']), '', '', $js) . htmlspecialchars($values['name']) . '</a>';
index 681142f..ea1cf31 100644 (file)
@@ -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));
     }
 }
 
index de5fb96..c2b0978 100644 (file)
@@ -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();
 }
 
index 703e8ef..acd61b9 100644 (file)
@@ -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');
index e00c1b2..5bc296b 100644 (file)
 <?php
 $remove = $auth->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);
 ?>
  <tr>
 <?php if ($remove): ?>
-  <td width="1%"><?php echo Horde::link(Horde::url('admin/user.php?form=remove_f&user_name=' . $user), _("Delete")) . Horde::img('delete.png', _("Delete")) ?></a></td>
+  <td width="1%"><?php echo Horde::link($user_url->add('form', 'remove_f'), _("Delete")) . Horde::img('delete.png', _("Delete")) ?></a></td>
 <?php endif; ?>
-  <td width="1%"><?php echo Horde::link(Horde::url('admin/user.php?form=update_f&user_name=' . $user), _("Update")) .  Horde::img('edit.png', _("Update")) ?></a></td>
-  <td width="1%"><?php echo Horde::link(Horde::url('admin/user.php?form=clear_f&user_name=' . $user), _("Clear user data")) .  Horde::img('reload.png', _("Clear user data")) ?></a></td>
+  <td width="1%"><?php echo Horde::link($user_url->add('form', 'update_f'), _("Update")) .  Horde::img('edit.png', _("Update")) ?></a></td>
+  <td width="1%"><?php echo Horde::link($user_url->add('form', 'clear_f'), _("Clear user data")) .  Horde::img('reload.png', _("Clear user data")) ?></a></td>
   <td class="leftAlign"><?php echo $user ?></td>
  </tr>
 <?php endforeach; ?>
index 1c3b427..d4f55d9 100644 (file)
@@ -69,7 +69,7 @@
      <tr>
       <td>&nbsp;</td>
       <td class="light leftAlign">
-       <?php echo Horde::link(Horde::url($registry->get('webroot', 'horde') . '/signup.php')->add('url', $vars->url), _("Don't have an account? Sign up."), 'light') . _("Don't have an account? Sign up.") ?></a>
+       <?php echo Horde::link(Horde::url('signup.php', false, array('app' => 'horde'))->add('url', $vars->url), _("Don't have an account? Sign up."), 'light') . _("Don't have an account? Sign up.") ?></a>
       </td>
      </tr>
 <?php endif; ?>
@@ -78,7 +78,7 @@
      <tr>
       <td>&nbsp;</td>
       <td class="light">
-       <?php echo Horde::link(Horde::url($registry->get('webroot', 'horde') . '/services/resetpassword.php')->add('url', $vars->url), _("Forgot your password?"), 'light') . _("Forgot your password?") ?></a>
+       <?php echo Horde::link(Horde::url('services/resetpassword.php', false, array('app' => 'horde'))->add('url', $vars->url), _("Forgot your password?"), 'light') . _("Forgot your password?") ?></a>
       </td>
      </tr>
 <?php endif; ?>
index 90e5ae1..6092f30 100644 (file)
@@ -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');
index 8ecb058..9c6ccb1 100644 (file)
@@ -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
index c4de0ac..57bfd80 100644 (file)
@@ -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');
index f7633d9..e7797a0 100644 (file)
@@ -341,7 +341,7 @@ $_prefs['search_fields'] = array(
 );
 
 $_prefs['fb_url'] = array(
-    'value' => '<strong>' . _("My Free/Busy URL") . '</strong><div class="fburl"><div>' . _("Copy this URL for use wherever you need your Free/Busy URL:") . '</div><div class="fixed">' . Horde::url('fb.php', true, -1)->add('u', $GLOBALS['registry']->getAuth()) . '</div></div>',
+    'value' => '<strong>' . _("My Free/Busy URL") . '</strong><div class="fburl"><div>' . _("Copy this URL for use wherever you need your Free/Busy URL:") . '</div><div class="fixed">' . Horde::url('fb.php', true, array('append_session' => -1))->add('u', $GLOBALS['registry']->getAuth()) . '</div></div>',
     'type' => 'rawhtml'
 );