Horde version check improvements.
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 14 Jul 2010 20:38:23 +0000 (14:38 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 14 Jul 2010 20:38:23 +0000 (14:38 -0600)
Move code into Horde_Config::.
Use SimpleXML and Horde_Http_Client.

framework/Core/lib/Horde/Config.php
horde/admin/setup/index.php

index fec37cc..172e14c 100644 (file)
@@ -88,16 +88,56 @@ class Horde_Config
     protected $_configEnd = "/* CONFIG END. DO NOT CHANGE ANYTHING IN OR BEFORE THIS LINE. */\n";
 
     /**
+     * Horde URL to check version information.
+     *
+     * @var string
+     */
+    protected $_versionUrl = 'http://www.horde.org/versions.php';
+
+    /**
      * Constructor.
      *
      * @param string $app  The name of the application to be configured.
      */
-    public function __construct($app)
+    public function __construct($app = 'horde')
     {
         $this->_app = $app;
     }
 
     /**
+     * Contact Horde servers and get version information.
+     *
+     * @return array  Keys are app names, values are arrays with two keys:
+     *                'version' and 'url'.
+     * @throws Horde_Exception
+     * @throws Horde_Http_Client_Exception
+     */
+    public function checkVersions()
+    {
+        if (!Horde_Util::extensionExists('SimpleXML')) {
+            throw new Horde_Exception('SimpleXML not available.');
+        }
+
+        $http = $GLOBALS['injector']->getInstance('Horde_Http_Client')->getClient();
+        $response = $http->get($this->_versionUrl);
+        if ($response->code != 200) {
+            throw new Horde_Exception('Unexpected response from server.');
+        }
+
+        $xml = new SimpleXMLElement($response->getBody());
+        $versions = array();
+
+        foreach ($xml->stable->application as $app) {
+            $versions[strval($app['name'])] = array(
+                'version' => $app->version,
+                'url' => $app->url
+            );
+        }
+
+        return $versions;
+    }
+
+    /**
      * Reads the application's conf.xml file and builds an associative array
      * from its XML tree.
      *
@@ -133,7 +173,7 @@ class Horde_Config
         $node = $dom->firstChild;
         while (!empty($node)) {
             if (($node->nodeType == XML_COMMENT_NODE) &&
-                ($vers_tag = self::getVersion($node->nodeValue))) {
+                ($vers_tag = $this->getVersion($node->nodeValue))) {
                 $this->_versionTag = $vers_tag . "\n";
                 break;
             }
@@ -157,7 +197,7 @@ class Horde_Config
      *
      * @return string  The version string or false if not found.
      */
-    static public function getVersion($text)
+    public function getVersion($text)
     {
         // Old CVS tag
         if (preg_match('/\$.*?conf\.xml,v .*? .*\$/', $text, $match) ||
index 627f6ac..2cacc59 100644 (file)
@@ -55,34 +55,15 @@ function _uploadFTP($params)
     return $no_errors;
 }
 
+$hconfig = new Horde_Config();
+
 /* Check for versions if requested. */
 $versions = array();
 if (Horde_Util::getFormData('check_versions')) {
-    $http = new HTTP_Request('http://www.horde.org/versions.php');
-    $result = $http->sendRequest();
-    if (is_a($result, 'PEAR_Error')) {
-        $notification->push($result, 'horde.error');
-    } elseif ($http->getResponseCode() != 200) {
-        $notification->push(_("Unexpected response from server, try again later."), 'horde.error');
-    } else {
-        $dom = DOMDocument::loadXML($http->getResponseBody());
-        $stable = $dom->getElementsByTagName('stable');
-        if (!$stable->length || !$stable->item(0)->hasChildNodes()) {
-            $notification->push(_("Invalid response from server."), 'horde.error');
-        } else {
-            for ($app = $stable->item(0)->firstChild;
-                 !empty($app);
-                 $app = $app->nextSibling) {
-                if (!($app instanceof DOMElement)) {
-                    continue;
-                }
-                $version = $app->getElementsByTagName('version');
-                $url = $app->getElementsByTagName('url');
-                $versions[$app->getAttribute('name')] = array(
-                    'version' => $version->item(0)->textContent,
-                    'url' => $url->item(0)->textContent);
-            }
-        }
+    try {
+        $versions = $hconfig->checkVersions();
+    } catch (Horde_Exception $e) {
+        $notification->push(_("Could not contact server. Try again later."), 'horde.error');
     }
 }
 
@@ -154,13 +135,13 @@ foreach ($a as $app) {
         $apps[$i]['status'] = _("Missing configuration. You must generate it before using this application.");
     } else {
         /* A conf.php exists, get the xml version. */
-        if (($xml_ver = Horde_Config::getVersion(@file_get_contents($path . '/conf.xml'))) === false) {
+        if (($xml_ver = $hconfig->getVersion(@file_get_contents($path . '/conf.xml'))) === false) {
             $apps[$i]['conf'] = $conf_link . $warning . '</a>';
             $apps[$i]['status'] = _("No version found in original configuration. Regenerate configuration.");
             continue;
         }
         /* Get the generated php version. */
-        if (($php_ver = Horde_Config::getVersion(@file_get_contents($path . '/conf.php'))) === false) {
+        if (($php_ver = $hconfig->getVersion(@file_get_contents($path . '/conf.php'))) === false) {
             /* No version found in generated php, suggest regenarating
              * just in case. */
             $apps[$i]['conf'] = $conf_link . $warning . '</a>';