PageRenderTime 21ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/setup/index.php

https://github.com/Excito/horde3
PHP | 284 lines | 233 code | 18 blank | 33 comment | 37 complexity | 81c4ef5c4d10888db894cc5c564397ef MD5 | raw file
  1. <?php
  2. /**
  3. * $Horde: horde/admin/setup/index.php,v 1.28.4.21 2011/04/07 10:27:28 jan Exp $
  4. *
  5. * Copyright 1999-2009 The Horde Project (http://www.horde.org/)
  6. *
  7. * See the enclosed file COPYING for license information (LGPL). If you
  8. * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  9. *
  10. * @author Chuck Hagenbuch <chuck@horde.org>
  11. */
  12. @define('HORDE_BASE', dirname(__FILE__) . '/../..');
  13. require_once HORDE_BASE . '/lib/base.php';
  14. require_once 'Horde/Template.php';
  15. require_once 'Horde/Form/Renderer.php';
  16. if (!Auth::isAdmin()) {
  17. Horde::fatal('Forbidden.', __FILE__, __LINE__);
  18. }
  19. /**
  20. * Returns the CVS version for a given file.
  21. */
  22. function _getVersion($file)
  23. {
  24. $data = @file_get_contents($file);
  25. if (preg_match('/\$.*?conf\.xml,v (.*?) .*\$/', $data, $match)) {
  26. return $match[1];
  27. } else {
  28. return false;
  29. }
  30. }
  31. /**
  32. * Does an FTP upload to save the configuration.
  33. */
  34. function _uploadFTP($params)
  35. {
  36. global $registry, $notification;
  37. require_once 'VFS.php';
  38. $params['hostspec'] = 'localhost';
  39. $vfs = &VFS::singleton('ftp', $params);
  40. if (is_a($vfs, 'PEAR_Error')) {
  41. $notification->push(sprintf(_("Could not connect to server \"%s\" using FTP: %s"), $params['hostspec'], $vfs->getMessage()), 'horde.error');
  42. return false;
  43. }
  44. /* Loop through the config and write to FTP. */
  45. $no_errors = true;
  46. foreach ($_SESSION['_config'] as $app => $config) {
  47. $path = $registry->get('fileroot', $app) . '/config';
  48. /* Try to back up the current conf.php. */
  49. if ($vfs->exists($path, 'conf.php')) {
  50. if (($result = $vfs->rename($path, 'conf.php', $path, '/conf.bak.php')) === true) {
  51. $notification->push(_("Successfully saved backup configuration."), 'horde.success');
  52. } elseif (is_a($result, 'PEAR_Error')) {
  53. $notification->push(sprintf(_("Could not save a backup configuation: %s"), $result->getMessage()), 'horde.error');
  54. } else {
  55. $notification->push(_("Could not save a backup configuation."), 'horde.error');
  56. }
  57. }
  58. $write = $vfs->writeData($path, 'conf.php', $config);
  59. if (is_a($write, 'PEAR_Error')) {
  60. $no_errors = false;
  61. $notification->push(sprintf(_("Could not write configuration for \"%s\": %s"), $app, $write->getMessage()), 'horde.error');
  62. } else {
  63. $notification->push(sprintf(_("Successfully wrote %s"), Util::realPath($path . '/conf.php')), 'horde.success');
  64. unset($_SESSION['_config'][$app]);
  65. }
  66. }
  67. $registry->clearCache();
  68. return $no_errors;
  69. }
  70. /* Check for versions if requested. */
  71. $versions = array();
  72. if (Util::getFormData('check_versions')) {
  73. require_once 'HTTP/Request.php';
  74. require_once 'Horde/DOM.php';
  75. $http = new HTTP_Request('http://www.horde.org/versions.php');
  76. $result = $http->sendRequest();
  77. if (is_a($result, 'PEAR_Error')) {
  78. $notification->push($result, 'horde.error');
  79. } elseif ($http->getResponseCode() != 200) {
  80. $notification->push(_("Unexpected response from server, try again later."), 'horde.error');
  81. } else {
  82. $dom = Horde_DOM_Document::factory(array('xml' => $http->getResponseBody()));
  83. $stable = $dom->get_elements_by_tagname('stable');
  84. if (!count($stable) || !$stable[0]->has_child_nodes()) {
  85. $notification->push(_("Invalid response from server."), 'horde.error');
  86. } else {
  87. for ($app = $stable[0]->first_child();
  88. !empty($app);
  89. $app = $app->next_sibling()) {
  90. if (!is_a($app, 'domelement') &&
  91. !is_a($app, 'Horde_DOM_Element')) {
  92. continue;
  93. }
  94. $version = $app->get_elements_by_tagname('version');
  95. $url = $app->get_elements_by_tagname('url');
  96. $versions[$app->get_attribute('name')] = array(
  97. 'version' => $version[0]->get_content(),
  98. 'url' => $url[0]->get_content());
  99. }
  100. }
  101. }
  102. }
  103. /* Set up some icons. */
  104. $success = Horde::img('alerts/success.png', '', '', $registry->getImageDir('horde'));
  105. $warning = Horde::img('alerts/warning.png', '', '', $registry->getImageDir('horde'));
  106. $error = Horde::img('alerts/error.png', '', '', $registry->getImageDir('horde'));
  107. $conf_url = Horde::applicationUrl('admin/setup/config.php');
  108. $a = $registry->listApps(array('inactive', 'hidden', 'notoolbar', 'active', 'admin'));
  109. $apps = array();
  110. $i = -1;
  111. if (file_exists(HORDE_BASE . '/lib/bundle.php')) {
  112. include HORDE_BASE . '/lib/bundle.php';
  113. $apps[0] = array('sort' => '00',
  114. 'name' => '<strong>' . BUNDLE_FULLNAME . '</strong>',
  115. 'icon' => Horde::img($registry->get('icon', 'horde'),
  116. BUNDLE_FULLNAME, '', ''),
  117. 'version' => '<strong>' . BUNDLE_VERSION . '</strong>');
  118. if (!empty($versions)) {
  119. if (!isset($versions[BUNDLE_NAME])) {
  120. $apps[0]['load'] = $warning;
  121. $apps[0]['vstatus'] = _("No stable version exists yet.");
  122. } elseif (version_compare($versions[BUNDLE_NAME]['version'], BUNDLE_VERSION, '>')) {
  123. $apps[0]['load'] = $error;
  124. $apps[0]['vstatus'] = Horde::link($versions[BUNDLE_NAME]['url'], sprintf(_("Download %s"), BUNDLE_FULLNAME), '', '_blank') . sprintf(_("A newer version (%s) exists."), $versions[BUNDLE_NAME]['version']) . '</a> ';
  125. } else {
  126. $apps[0]['load'] = $success;
  127. $apps[0]['vstatus'] = _("Application is up-to-date.");
  128. }
  129. }
  130. $i++;
  131. }
  132. foreach ($a as $app) {
  133. /* Skip app if no conf.xml file. */
  134. $path = $registry->get('fileroot', $app) . '/config';
  135. if (!file_exists($path . '/conf.xml')) {
  136. continue;
  137. }
  138. $i++;
  139. $path = $registry->get('fileroot', $app) . '/config';
  140. $conf_link = Util::addParameter($conf_url, 'app', $app);
  141. $conf_link = Horde::link($conf_link, sprintf(_("Configure %s"), $app));
  142. $apps[$i]['sort'] = $registry->get('name', $app) . ' (' . $app . ')';
  143. $apps[$i]['name'] = $conf_link . $apps[$i]['sort'] . '</a>';
  144. $apps[$i]['icon'] = Horde::img($registry->get('icon', $app), $registry->get('name', $app), '', '');
  145. $apps[$i]['version'] = '';
  146. if (is_readable($registry->get('fileroot', $app) . '/lib/version.php')) {
  147. require_once $registry->get('fileroot', $app) . '/lib/version.php';
  148. $version_constant = String::upper($app) . '_VERSION';
  149. if (defined($version_constant)) {
  150. $apps[$i]['version'] = constant($version_constant);
  151. if (!empty($versions)) {
  152. if (!isset($versions[$app])) {
  153. $apps[$i]['load'] = $warning;
  154. $apps[$i]['vstatus'] = _("No stable version exists yet.");
  155. } elseif (version_compare(preg_replace('/H\d+ \((.*)\)/', '$1', $versions[$app]['version']), preg_replace('/H\d+ \((.*)\)/', '$1', $apps[$i]['version']), '>')) {
  156. $apps[$i]['load'] = $error;
  157. $apps[$i]['vstatus'] = Horde::link($versions[$app]['url'], sprintf(_("Download %s"), $app), '', '_blank') . sprintf(_("A newer version (%s) exists."), $versions[$app]['version']) . '</a> ';
  158. } else {
  159. $apps[$i]['load'] = $success;
  160. $apps[$i]['vstatus'] = _("Application is up-to-date.");
  161. }
  162. }
  163. }
  164. }
  165. if (!file_exists($path . '/conf.php')) {
  166. /* No conf.php exists. */
  167. $apps[$i]['conf'] = $conf_link . $error . '</a>';
  168. $apps[$i]['status'] = _("Missing configuration. You must generate it before using this application.");
  169. } else {
  170. /* A conf.php exists, get the xml version. */
  171. if (($xml_ver = _getVersion($path . '/conf.xml')) === false) {
  172. $apps[$i]['conf'] = $conf_link . $warning . '</a>';
  173. $apps[$i]['status'] = _("No version found in original configuration. Regenerate configuration.");
  174. continue;
  175. }
  176. /* Get the generated php version. */
  177. if (($php_ver = _getVersion($path . '/conf.php')) === false) {
  178. /* No version found in generated php, suggest regenarating
  179. * just in case. */
  180. $apps[$i]['conf'] = $conf_link . $warning . '</a>';
  181. $apps[$i]['status'] = _("No version found in your configuration. Regenerate configuration.");
  182. continue;
  183. }
  184. if ($xml_ver != $php_ver) {
  185. /* Versions are not the same, configuration is out of date. */
  186. $apps[$i]['conf'] = $conf_link . $error . '</a>';
  187. $apps[$i]['status'] = _("Configuration is out of date.");
  188. continue;
  189. } else {
  190. /* Configuration is ok. */
  191. $apps[$i]['conf'] = $conf_link . $success . '</a>';
  192. $apps[$i]['status'] = _("Application is ready.");
  193. }
  194. }
  195. }
  196. /* Sort the apps by name. */
  197. require_once 'Horde/Array.php';
  198. Horde_Array::arraySort($apps, 'sort');
  199. /* Set up any actions that may be offered. */
  200. $actions = array();
  201. $ftpform = '';
  202. if (!empty($_SESSION['_config'])) {
  203. Horde::addScriptFile('popup.js', 'horde', true);
  204. $url = Horde::applicationUrl('admin/setup/diff.php');
  205. $action = _("Show differences between currently saved and the newly generated configuration.");
  206. $actions[] = array('icon' => Horde::img('search.png', '', 'align="middle"', $registry->getImageDir('horde')),
  207. 'link' => Horde::link('#', '', '', '', 'popup(\'' . $url . '\',640, 480); return false;') . $action . '</a>');
  208. $url = Horde::applicationUrl('admin/setup/scripts.php');
  209. /* Action to download the configuration upgrade PHP script. */
  210. $url = Util::addParameter($url, array('setup' => 'conf', 'type' => 'php'));
  211. $action = _("Download generated configuration as PHP script.");
  212. $actions[] = array('icon' => Horde::img('download.png', '', 'align="middle"', $registry->getImageDir('horde')),
  213. 'link' => Horde::link($url) . $action . '</a>');
  214. /* Action to save the configuration upgrade PHP script. */
  215. $url = Util::addParameter($url, 'save', 'tmp');
  216. $action = _("Save generated configuration as a PHP script to your server's temporary directory.");
  217. $actions[] = array('icon' => Horde::img('save.png', '', 'align="middle"', $registry->getImageDir('horde')),
  218. 'link' => Horde::link($url) . $action . '</a>');
  219. /* Set up the form for FTP upload of scripts. */
  220. require_once 'Horde/Form.php';
  221. require_once 'Horde/Variables.php';
  222. $vars = Variables::getDefaultVariables();
  223. $ftpform = new Horde_Form($vars);
  224. $ftpform->setButtons(_("Upload"), true);
  225. $ftpform->addVariable(_("Username"), 'username', 'text', true, false, null, array('', 20));
  226. $ftpform->addVariable(_("Password"), 'password', 'password', false);
  227. if ($ftpform->validate($vars)) {
  228. $ftpform->getInfo($vars, $info);
  229. $upload = _uploadFTP($info);
  230. if ($upload) {
  231. $notification->push(_("Uploaded all application setup files to the server."), 'horde.success');
  232. $url = Horde::applicationUrl('admin/setup/index.php', true);
  233. header('Location: ' . $url);
  234. exit;
  235. }
  236. }
  237. /* Render the form. */
  238. $ftpform = Util::bufferOutput(array($ftpform, 'renderActive'), new Horde_Form_Renderer(), $vars, 'index.php', 'post');
  239. }
  240. if (file_exists(Horde::getTempDir() . '/horde_setup_upgrade.php')) {
  241. /* Action to remove the configuration upgrade PHP script. */
  242. $url = Horde::applicationUrl('admin/setup/scripts.php');
  243. $url = Util::addParameter($url, 'clean', 'tmp');
  244. $action = _("Remove saved script from server's temporary directory.");
  245. $actions[] = array('icon' => Horde::img('delete.png', '', 'align="middle"', $registry->getImageDir('horde')),
  246. 'link' => Horde::link($url) . $action . '</a>');
  247. }
  248. /* Set up the template. */
  249. $template = new Horde_Template();
  250. $template->setOption('gettext', true);
  251. $template->set('versions', !empty($versions), true);
  252. $template->set('version_action', Horde::applicationUrl('admin/setup/index.php'));
  253. $template->set('version_input', Util::formInput());
  254. $template->set('apps', $apps);
  255. $template->set('actions', $actions, true);
  256. $template->set('ftpform', $ftpform, true);
  257. $title = sprintf(_("%s Setup"), $registry->get('name', 'horde'));
  258. Horde::addScriptFile('stripe.js', 'horde', true);
  259. require HORDE_TEMPLATES . '/common-header.inc';
  260. require HORDE_TEMPLATES . '/admin/menu.inc';
  261. echo $template->fetch(HORDE_TEMPLATES . '/admin/setup/index.html');
  262. require HORDE_TEMPLATES . '/common-footer.inc';