From a44d6f060c06f5cadbf86cb88c28eb64f59163f2 Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+github@gmail.com>
Date: Thu, 19 Mar 2015 15:26:58 +0100
Subject: [PATCH] implement absolute_url_for() on MarketController for generic
 use, re #9

---
 classes/MarketImage.class.php       | 19 +++++++++++++++----
 classes/MarketPlugin.class.php      |  5 +++--
 controllers/approving.php           |  5 +++--
 controllers/extern.php              | 25 ++++++++++++++++---------
 controllers/market_controller.php   | 17 +++++++++++++++++
 controllers/myplugins.php           |  6 +++---
 controllers/presenting.php          |  6 +++---
 controllers/update.php              | 10 +++++-----
 views/extern/xml.php                |  4 ++--
 views/myplugins/_edit_release.php   |  2 +-
 views/presenting/follow_release.php |  2 +-
 11 files changed, 69 insertions(+), 32 deletions(-)
 create mode 100644 controllers/market_controller.php

diff --git a/classes/MarketImage.class.php b/classes/MarketImage.class.php
index 7fb8eae..b97d32b 100644
--- a/classes/MarketImage.class.php
+++ b/classes/MarketImage.class.php
@@ -1,7 +1,7 @@
 <?php
 
-class MarketImage extends SimpleORMap {
-
+class MarketImage extends SimpleORMap
+{
     static public function findByPlugin_id($plugin_id) {
         return self::findBySQL("plugin_id = ? ORDER BY position ASC, mkdate ASC", array($plugin_id));
     }
@@ -20,8 +20,19 @@ class MarketImage extends SimpleORMap {
         parent::configure($config);
     }
 
-    public function getURL() {
-        return URLHelper::getURL("plugins.php/pluginmarket/presenting/image/".$this->getId(), array(), true);
+    public function getURL($absolute_url = false)
+    {
+        if ($absolute_url) {
+            $old_base = URLHelper::setBaseURL($GLOBALS['ABSOLUTE_URI_STUDIP']);
+        }
+
+        $url = URLHelper::getURL("plugins.php/pluginmarket/presenting/image/".$this->getId(), array(), true);
+
+        if ($absolute_url) {
+            URLHelper::setBaseURL($old_base);
+        }
+
+        return $url;
     }
 
     public function delete() {
diff --git a/classes/MarketPlugin.class.php b/classes/MarketPlugin.class.php
index c54945a..e5399a8 100644
--- a/classes/MarketPlugin.class.php
+++ b/classes/MarketPlugin.class.php
@@ -66,9 +66,10 @@ class MarketPlugin extends SimpleORMap {
         return $GLOBALS['perm']->have_perm("root", $user_id);
     }
 
-    public function getLogoURL() {
+    public function getLogoURL($absolute_url = false)
+    {
         $firstimage = $this->images->first();
-        return $firstimage ? $firstimage->getURL() : Assets::image_path("icons/blue/plugin.svg");
+        return $firstimage ? $firstimage->getURL($absolute_url) : Assets::image_path("icons/blue/plugin.svg");
     }
 
     public function setTags($tags) {
diff --git a/controllers/approving.php b/controllers/approving.php
index 3732a70..d0cfa36 100644
--- a/controllers/approving.php
+++ b/controllers/approving.php
@@ -1,7 +1,8 @@
 <?php
-require_once 'app/controllers/plugin_controller.php';
+require_once 'market_controller.php';
 
-class ApprovingController extends PluginController {
+class ApprovingController extends MarketController
+{
 
     function before_filter(&$action, &$args)
     {
diff --git a/controllers/extern.php b/controllers/extern.php
index dc03f3b..1c8dcb4 100644
--- a/controllers/extern.php
+++ b/controllers/extern.php
@@ -1,16 +1,23 @@
 <?php
-require_once 'app/controllers/plugin_controller.php';
+require_once 'market_controller.php';
 
-class ExternController extends PluginController
+class ExternController extends MarketController
 {
-    public function xml_action() {
-        $this->plugins = MarketPlugin::findBySQL("publiclyvisible = 1 AND approved = 1 ORDER BY name ASC");
-        URLHelper::setBaseUrl($GLOBALS['ABSOLUTE_URI_STUDIP']);
+    public function before_filter(&$action, &$args)
+    {
+        parent::before_filter($action, $args);
+
         $this->set_layout(null);
+    }
+
+    public function xml_action()
+    {
+        $this->plugins = MarketPlugin::findBySQL("publiclyvisible = 1 AND approved = 1 ORDER BY name ASC");
         $this->response->add_header('Content-Type', "text/xml");
     }
 
-    public function find_releases_action() {
+    public function find_releases_action()
+    {
         $output = array();
         $studipversion = Request::get("studipversion");
         $plugins = MarketPlugin::findByPluginclassname(Request::get("classname"));
@@ -22,9 +29,9 @@ class ExternController extends PluginController
                     if ((!$release['studip_min_version'] || version_compare($studipversion, $release['studip_min_version'], ">="))
                             && (!$release['studip_max_version'] || version_compare($studipversion, $release['studip_max_version'], "<="))) {
                         $output['releases'][] = array(
-                            'version' => $release['version'],
-                            'html_url' => PluginEngine::getURL($this->plugin, array(), "presenting/details/".$plugin->getId()),
-                            'download_url' => PluginEngine::getURL($this->plugin, array(), "presenting/download/".$release->getId())
+                            'version'      => $release['version'],
+                            'html_url'     => $this->url_for('presenting/details/' . $plugin->getId()),
+                            'download_url' => $this->url_for('presenting/download/' . $release->getId()),
                         );
                     }
                 }
diff --git a/controllers/market_controller.php b/controllers/market_controller.php
new file mode 100644
index 0000000..96e2484
--- /dev/null
+++ b/controllers/market_controller.php
@@ -0,0 +1,17 @@
+<?php
+require_once 'app/controllers/plugin_controller.php';
+
+class MarketController extends PluginController
+{
+    public function absolute_url_for($to)
+    {
+        $old_base = URLHelper::setBaseURL($GLOBALS['ABSOLUTE_URI_STUDIP']);
+
+        $args = func_get_args();
+        $url  = call_user_func_array(array($this, 'url_for'), $args);
+
+        URLHelper::setBaseURL($old_base);
+
+        return $url;
+    }
+}
diff --git a/controllers/myplugins.php b/controllers/myplugins.php
index 728f8f7..611f55a 100644
--- a/controllers/myplugins.php
+++ b/controllers/myplugins.php
@@ -1,8 +1,8 @@
 <?php
-require_once 'app/controllers/plugin_controller.php';
-
-class MypluginsController extends PluginController {
+require_once 'market_controller.php';
 
+class MypluginsController extends MarketController
+{
     function before_filter(&$action, &$args)
     {
         parent::before_filter($action, $args);
diff --git a/controllers/presenting.php b/controllers/presenting.php
index 9b56c8a..b6e3aec 100644
--- a/controllers/presenting.php
+++ b/controllers/presenting.php
@@ -1,8 +1,8 @@
 <?php
-require_once 'app/controllers/plugin_controller.php';
-
-class PresentingController extends PluginController {
+require_once 'market_controller.php';
 
+class PresentingController extends MarketController
+{
     protected $last_pluginmarket_visit = null;
 
     function before_filter(&$action, &$args)
diff --git a/controllers/update.php b/controllers/update.php
index 4fea6fa..3810a7b 100644
--- a/controllers/update.php
+++ b/controllers/update.php
@@ -1,9 +1,10 @@
 <?php
-require_once 'app/controllers/plugin_controller.php';
+require_once 'market_controller.php';
 
-class UpdateController extends PluginController {
-
-    public function release_action($release_id) {
+class UpdateController extends MarketController
+{
+    public function release_action($release_id)
+    {
         $release = new MarketRelease($release_id);
         if ($release->isNew()) {
             throw new Exception("Unknown release.");
@@ -20,5 +21,4 @@ class UpdateController extends PluginController {
             $this->render_text("Insecure request.");
         }
     }
-
 }
\ No newline at end of file
diff --git a/views/extern/xml.php b/views/extern/xml.php
index 298077a..929045b 100644
--- a/views/extern/xml.php
+++ b/views/extern/xml.php
@@ -6,13 +6,13 @@
         homepage="<?= htmlReady(studip_utf8encode($marketplugin['url'])) ?>"
         short_description="<?= htmlReady(studip_utf8encode($marketplugin['short_description'])) ?>"
         description="<?= htmlReady(studip_utf8encode($marketplugin['description'])) ?>"
-        image="<?= htmlReady(studip_utf8encode($marketplugin->getLogoURL())) ?>">
+        image="<?= htmlReady(studip_utf8encode($marketplugin->getLogoURL(true))) ?>">
         <? foreach ($marketplugin->releases as $release) : ?>
         <release
             version="<?= htmlReady(studip_utf8encode($release['version'])) ?>"
             studipMinVersion="<?= htmlReady(studip_utf8encode($release['studip_min_version'])) ?>"
             studipMaxVersion="<?= htmlReady(studip_utf8encode($release['studip_min_version'])) ?>"
-            url="<?= htmlReady(studip_utf8encode($controller->url_for('presenting/download/' . $release->getId()))) ?>"
+            url="<?= htmlReady(studip_utf8encode($controller->absolute_url_for('presenting/download/' . $release->getId()))) ?>"
             />
         <? endforeach ?>
     </plugin>
diff --git a/views/myplugins/_edit_release.php b/views/myplugins/_edit_release.php
index e12b257..adc967c 100644
--- a/views/myplugins/_edit_release.php
+++ b/views/myplugins/_edit_release.php
@@ -51,7 +51,7 @@
         <? if (!$release->isNew()) : ?>
         <p class="info">
             <?= _("Webhook-URL zum Einf�gen in github oder gitlab:") ?>
-            <input type="text" readonly style="border: thin solid #cccccc; background-color: #eeeeee; width:100%;" value="<?= $GLOBALS['ABSOLUTE_URI_STUDIP']."plugins.php/pluginmarket/update/release/".$release->getId().'?s='.$release->getSecurityHash() ?>">
+            <input type="text" readonly style="border: thin solid #cccccc; background-color: #eeeeee; width:100%;" value="<?= $controller->absolute_url_for('update/release/' . $release->getId(), array('s' => $release->getSecurityHash())) ?>">
         </p>
             <? if ($domain_warning) : ?>
             <p class="info"><?= htmlReady($domain_warning)  ?></p>
diff --git a/views/presenting/follow_release.php b/views/presenting/follow_release.php
index 76add3f..565ed67 100644
--- a/views/presenting/follow_release.php
+++ b/views/presenting/follow_release.php
@@ -9,7 +9,7 @@
         <legend><?= _("Konfigurieren Sie Ihr Stud.IP") ?></legend>
         <label>
             <?= _("Download-URL - geben Sie diese URL in Ihrem Stud.IP in der Pluginverwaltung ein") ?>
-            <input type="text" readonly value="<?= $GLOBALS['ABSOLUTE_URI_STUDIP'] . $controller->url_for('presenting/download/' . $release->getId()) ?>">
+            <input type="text" readonly value="<?= $controller->absolute_url_for('presenting/download/' . $release->getId()) ?>">
         </label>
     </fieldset>
     <fieldset>
-- 
GitLab