From 61a9098b7d88656d0297a18c1b7685c04d1c64dc Mon Sep 17 00:00:00 2001
From: Bart Visscher <bartv@thisnet.nl>
Date: Thu, 26 Sep 2013 18:41:19 +0200
Subject: [PATCH] Add Helper and URLGenerator interfaces to server container

---
 lib/apphelper.php               |  25 +++++++
 lib/private/helper.php          |  52 ++-------------
 lib/private/server.php          |  20 ++++++
 lib/private/util.php            |   4 +-
 lib/public/ihelper.php          |  23 +++++++
 lib/public/iservercontainer.php |  10 +++
 lib/public/iurlgenerator.php    |  47 ++++++++++++++
 lib/urlgenerator.php            | 111 ++++++++++++++++++++++++++++++++
 8 files changed, 242 insertions(+), 50 deletions(-)
 create mode 100644 lib/apphelper.php
 create mode 100644 lib/public/ihelper.php
 create mode 100644 lib/public/iurlgenerator.php
 create mode 100644 lib/urlgenerator.php

diff --git a/lib/apphelper.php b/lib/apphelper.php
new file mode 100644
index 0000000000..bd02f3aabf
--- /dev/null
+++ b/lib/apphelper.php
@@ -0,0 +1,25 @@
+<?php
+/**
+ * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ *
+ */
+
+namespace OC;
+
+/**
+ * TODO: Description
+ */
+class AppHelper implements \OCP\IHelper {
+	/**
+	 * Gets the content of an URL by using CURL or a fallback if it is not
+	 * installed
+	 * @param string $url the url that should be fetched
+	 * @return string the content of the webpage
+	 */
+	public function getUrlContent($url) {
+		return \OC_Util::getUrlContent($url);
+	}
+}
diff --git a/lib/private/helper.php b/lib/private/helper.php
index 66e7acb407..a34640d8e3 100644
--- a/lib/private/helper.php
+++ b/lib/private/helper.php
@@ -41,8 +41,7 @@ class OC_Helper {
 	 * Returns a url to the given app and file.
 	 */
 	public static function linkToRoute($route, $parameters = array()) {
-		$urlLinkTo = OC::getRouter()->generate($route, $parameters);
-		return $urlLinkTo;
+		return OC::$server->getURLGenerator()->linkToRoute($route, $parameters);
 	}
 
 	/**
@@ -56,32 +55,7 @@ class OC_Helper {
 	 * Returns a url to the given app and file.
 	 */
 	public static function linkTo( $app, $file, $args = array() ) {
-		if( $app != '' ) {
-			$app_path = OC_App::getAppPath($app);
-			// Check if the app is in the app folder
-			if ($app_path && file_exists($app_path . '/' . $file)) {
-				if (substr($file, -3) == 'php' || substr($file, -3) == 'css') {
-					$urlLinkTo = OC::$WEBROOT . '/index.php/apps/' . $app;
-					$urlLinkTo .= ($file != 'index.php') ? '/' . $file : '';
-				} else {
-					$urlLinkTo = OC_App::getAppWebPath($app) . '/' . $file;
-				}
-			} else {
-				$urlLinkTo = OC::$WEBROOT . '/' . $app . '/' . $file;
-			}
-		} else {
-			if (file_exists(OC::$SERVERROOT . '/core/' . $file)) {
-				$urlLinkTo = OC::$WEBROOT . '/core/' . $file;
-			} else {
-				$urlLinkTo = OC::$WEBROOT . '/' . $file;
-			}
-		}
-
-		if ($args && $query = http_build_query($args, '', '&')) {
-			$urlLinkTo .= '?' . $query;
-		}
-
-		return $urlLinkTo;
+		return OC::$server->getURLGenerator()->linkTo($app, $file, $args);
 	}
 
 	/**
@@ -107,7 +81,7 @@ class OC_Helper {
 	 * Returns a absolute url to the given app and file.
 	 */
 	public static function makeURLAbsolute($url) {
-		return OC_Request::serverProtocol() . '://' . OC_Request::serverHost() . $url;
+		return OC::$server->getURLGenerator()->makeURLAbsolute($url);
 	}
 
 	/**
@@ -156,25 +130,7 @@ class OC_Helper {
 	 * Returns the path to the image.
 	 */
 	public static function imagePath($app, $image) {
-		// Read the selected theme from the config file
-		$theme = OC_Util::getTheme();
-
-		// Check if the app is in the app folder
-		if (file_exists(OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) {
-			return OC::$WEBROOT . "/themes/$theme/apps/$app/img/$image";
-		} elseif (file_exists(OC_App::getAppPath($app) . "/img/$image")) {
-			return OC_App::getAppWebPath($app) . "/img/$image";
-		} elseif (!empty($app) and file_exists(OC::$SERVERROOT . "/themes/$theme/$app/img/$image")) {
-			return OC::$WEBROOT . "/themes/$theme/$app/img/$image";
-		} elseif (!empty($app) and file_exists(OC::$SERVERROOT . "/$app/img/$image")) {
-			return OC::$WEBROOT . "/$app/img/$image";
-		} elseif (file_exists(OC::$SERVERROOT . "/themes/$theme/core/img/$image")) {
-			return OC::$WEBROOT . "/themes/$theme/core/img/$image";
-		} elseif (file_exists(OC::$SERVERROOT . "/core/img/$image")) {
-			return OC::$WEBROOT . "/core/img/$image";
-		} else {
-			throw new RuntimeException('image not found: image:' . $image . ' webroot:' . OC::$WEBROOT . ' serverroot:' . OC::$SERVERROOT);
-		}
+		return OC::$server->getURLGenerator()->imagePath($app, $image);
 	}
 
 	/**
diff --git a/lib/private/server.php b/lib/private/server.php
index e4cc0c6da8..4000f546a3 100644
--- a/lib/private/server.php
+++ b/lib/private/server.php
@@ -105,6 +105,12 @@ class Server extends SimpleContainer implements IServerContainer {
 		$this->registerService('L10NFactory', function($c) {
 			return new \OC\L10N\Factory();
 		});
+		$this->registerService('URLGenerator', function($c) {
+			return new \OC\URLGenerator();
+		});
+		$this->registerService('AppHelper', function($c) {
+			return new \OC\AppHelper();
+		});
 		$this->registerService('UserCache', function($c) {
 			return new UserCache();
 		});
@@ -229,6 +235,20 @@ class Server extends SimpleContainer implements IServerContainer {
 		return $this->query('L10NFactory')->get($app);
 	}
 
+	/**
+	 * @return \OC\URLGenerator
+	 */
+	function getURLGenerator() {
+		return $this->query('URLGenerator');
+	}
+
+	/**
+	 * @return \OC\Helper
+	 */
+	function getHelper() {
+		return $this->query('AppHelper');
+	}
+
 	/**
 	 * Returns an ICache instance
 	 *
diff --git a/lib/private/util.php b/lib/private/util.php
index ae9aef69b4..04a020ff00 100755
--- a/lib/private/util.php
+++ b/lib/private/util.php
@@ -982,9 +982,9 @@ class OC_Util {
 	 * @param string $url Url to get content
 	 * @return string of the response or false on error
 	 * This function get the content of a page via curl, if curl is enabled.
-	 * If not, file_get_element is used.
+	 * If not, file_get_contents is used.
 	 */
-	public static function getUrlContent($url){
+	public static function getUrlContent($url) {
 		if (function_exists('curl_init')) {
 			$curl = curl_init();
 
diff --git a/lib/public/ihelper.php b/lib/public/ihelper.php
new file mode 100644
index 0000000000..fad02f7556
--- /dev/null
+++ b/lib/public/ihelper.php
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ *
+ */
+
+namespace OCP;
+
+/**
+ * Functions that don't have any specific interface to place
+ */
+interface IHelper {
+	/**
+	 * Gets the content of an URL by using CURL or a fallback if it is not
+	 * installed
+	 * @param string $url the url that should be fetched
+	 * @return string the content of the webpage
+	 */
+	public function getUrlContent($url);
+}
diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php
index e8bc1bedf9..3afb2b6599 100644
--- a/lib/public/iservercontainer.php
+++ b/lib/public/iservercontainer.php
@@ -108,6 +108,16 @@ interface IServerContainer {
 	 */
 	function getL10N($app);
 
+	/**
+	 * @return \OCP\IURLGenerator
+	 */
+	function getURLGenerator();
+
+	/**
+	 * @return \OCP\IHelper
+	 */
+	function getHelper();
+
 	/**
 	 * Returns an ICache instance
 	 *
diff --git a/lib/public/iurlgenerator.php b/lib/public/iurlgenerator.php
new file mode 100644
index 0000000000..4eb4c0f831
--- /dev/null
+++ b/lib/public/iurlgenerator.php
@@ -0,0 +1,47 @@
+<?php
+/**
+ * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ *
+ */
+
+namespace OCP;
+
+/**
+ * Class to generate URLs
+ */
+interface IURLGenerator {
+	/**
+	 * Returns the URL for a route
+	 * @param string $routeName the name of the route
+	 * @param array $arguments an array with arguments which will be filled into the url
+	 * @return string the url
+	 */
+	public function linkToRoute($routeName, $arguments = array());
+
+	/**
+	 * Returns an URL for an image or file
+	 * @param string $appName the name of the app
+	 * @param string $file the name of the file
+	 * @return string the url
+	 */
+	public function linkTo($appName, $file);
+
+	/**
+	 * Returns the link to an image, like linkTo but only with prepending img/
+	 * @param string $appName the name of the app
+	 * @param string $file the name of the file
+	 * @return string the url
+	 */
+	public function imagePath($appName, $file);
+
+
+	/**
+	 * Makes an URL absolute
+	 * @param string $url the url in the owncloud host
+	 * @return string the absolute version of the url
+	 */
+	public function getAbsoluteURL($url);
+}
diff --git a/lib/urlgenerator.php b/lib/urlgenerator.php
new file mode 100644
index 0000000000..1db4c36cc5
--- /dev/null
+++ b/lib/urlgenerator.php
@@ -0,0 +1,111 @@
+<?php
+/**
+ * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ *
+ */
+
+namespace OC;
+
+/**
+ * Class to generate URLs
+ */
+class URLGenerator {
+	/**
+	 * @brief Creates an url using a defined route
+	 * @param $route
+	 * @param array $parameters
+	 * @return
+	 * @internal param array $args with param=>value, will be appended to the returned url
+	 * @returns the url
+	 *
+	 * Returns a url to the given app and file.
+	 */
+	public function linkToRoute($route, $parameters = array()) {
+		$urlLinkTo = \OC::getRouter()->generate($route, $parameters);
+		return $urlLinkTo;
+	}
+
+	/**
+	 * @brief Creates an url
+	 * @param string $app app
+	 * @param string $file file
+	 * @param array $args array with param=>value, will be appended to the returned url
+	 *    The value of $args will be urlencoded
+	 * @return string the url
+	 *
+	 * Returns a url to the given app and file.
+	 */
+	public function linkTo( $app, $file, $args = array() ) {
+		if( $app != '' ) {
+			$app_path = \OC_App::getAppPath($app);
+			// Check if the app is in the app folder
+			if ($app_path && file_exists($app_path . '/' . $file)) {
+				if (substr($file, -3) == 'php' || substr($file, -3) == 'css') {
+					$urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $app;
+					$urlLinkTo .= ($file != 'index.php') ? '/' . $file : '';
+				} else {
+					$urlLinkTo = \OC_App::getAppWebPath($app) . '/' . $file;
+				}
+			} else {
+				$urlLinkTo = \OC::$WEBROOT . '/' . $app . '/' . $file;
+			}
+		} else {
+			if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) {
+				$urlLinkTo = \OC::$WEBROOT . '/core/' . $file;
+			} else {
+				$urlLinkTo = \OC::$WEBROOT . '/' . $file;
+			}
+		}
+
+		if ($args && $query = http_build_query($args, '', '&')) {
+			$urlLinkTo .= '?' . $query;
+		}
+
+		return $urlLinkTo;
+	}
+
+	/**
+	 * @brief Creates path to an image
+	 * @param string $app app
+	 * @param string $image image name
+	 * @return string the url
+	 *
+	 * Returns the path to the image.
+	 */
+	public function imagePath($app, $image) {
+		// Read the selected theme from the config file
+		$theme = \OC_Util::getTheme();
+
+		// Check if the app is in the app folder
+		if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) {
+			return \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$image";
+		} elseif (file_exists(\OC_App::getAppPath($app) . "/img/$image")) {
+			return \OC_App::getAppWebPath($app) . "/img/$image";
+		} elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$image")) {
+			return \OC::$WEBROOT . "/themes/$theme/$app/img/$image";
+		} elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/$app/img/$image")) {
+			return \OC::$WEBROOT . "/$app/img/$image";
+		} elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$image")) {
+			return \OC::$WEBROOT . "/themes/$theme/core/img/$image";
+		} elseif (file_exists(\OC::$SERVERROOT . "/core/img/$image")) {
+			return \OC::$WEBROOT . "/core/img/$image";
+		} else {
+			throw new RuntimeException('image not found: image:' . $image . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
+		}
+	}
+
+	/**
+	 * @brief Makes an $url absolute
+	 * @param string $url the url
+	 * @return string the absolute url
+	 *
+	 * Returns a absolute url to the given app and file.
+	 */
+	public function makeURLAbsolute($url) {
+		return \OC_Request::serverProtocol() . '://' . \OC_Request::serverHost() . $url;
+	}
+
+}
-- 
GitLab