/horde/admin/config/diff.php

https://github.com/ewandor/horde · PHP · 88 lines · 58 code · 10 blank · 20 comment · 12 complexity · c83d40b39d065d1ded54bd88b708391a MD5 · raw file

  1. <?php
  2. /**
  3. * Script to show the differences between the currently saved and the newly
  4. * generated configuration.
  5. *
  6. * Copyright 2004-2012 Horde LLC (http://www.horde.org/)
  7. *
  8. * See the enclosed file COPYING for license information (LGPL). If you
  9. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  10. */
  11. require_once dirname(__FILE__) . '/../../lib/Application.php';
  12. $permission = 'configuration';
  13. Horde_Registry::appInit('horde');
  14. if (!$registry->isAdmin() &&
  15. !$injector->getInstance('Horde_Perms')->hasPermission('horde:administration:'.$permission, $registry->getAuth(), Horde_Perms::SHOW)) {
  16. $registry->authenticateFailure('horde', new Horde_Exception(sprintf("Not an admin and no %s permission", $permission)));
  17. }
  18. /* Set up the diff renderer. */
  19. $render_type = Horde_Util::getFormData('render', 'inline');
  20. $class = 'Horde_Text_Diff_Renderer_' . Horde_String::ucfirst($render_type);
  21. $renderer = new $class();
  22. /**
  23. * Private function to render the differences for a specific app.
  24. */
  25. function _getDiff($app)
  26. {
  27. global $registry, $renderer, $session;
  28. /* Read the existing configuration. */
  29. $current_config = '';
  30. $path = $registry->get('fileroot', $app) . '/config';
  31. $current_config = @file_get_contents($path . '/conf.php');
  32. /* Calculate the differences. */
  33. $diff = new Horde_Text_Diff('auto',
  34. array(explode("\n", $current_config),
  35. explode("\n", $session->get('horde', 'config/' . $app))));
  36. $diff = $renderer->render($diff);
  37. return empty($diff)
  38. ? _("No change.")
  39. : $diff;
  40. }
  41. $diffs = array();
  42. /* Only bother to do anything if there is any config. */
  43. if ($config = $session->get('horde', 'config/')) {
  44. /* Set up the toggle button for inline/unified. */
  45. $url = Horde::url('admin/config/diff.php')->add('render', ($render_type == 'inline') ? 'unified' : 'inline');
  46. if ($app = Horde_Util::getFormData('app')) {
  47. /* Handle a single app request. */
  48. $toggle_renderer = Horde::link($url . '#' . $app) . (($render_type == 'inline') ? _("unified") : _("inline")) . '</a>';
  49. $diff = _getDiff($app);
  50. if ($render_type != 'inline') {
  51. $diff = htmlspecialchars($diff);
  52. }
  53. $diffs[] = array('app' => $app,
  54. 'diff' => $diff,
  55. 'toggle_renderer' => $toggle_renderer);
  56. } else {
  57. /* List all the apps with generated configuration. */
  58. ksort($config);
  59. foreach ($config as $app => $config) {
  60. $toggle_renderer = Horde::link($url . '#' . $app) . (($render_type == 'inline') ? _("unified") : _("inline")) . '</a>';
  61. $diff = _getDiff($app);
  62. if ($render_type != 'inline') {
  63. $diff = htmlspecialchars($diff);
  64. }
  65. $diffs[] = array('app' => $app,
  66. 'diff' => $diff,
  67. 'toggle_renderer' => $toggle_renderer);
  68. }
  69. }
  70. }
  71. /* Set up the template. */
  72. $template = $injector->createInstance('Horde_Template');
  73. $template->setOption('gettext', true);
  74. $template->set('diffs', $diffs, true);
  75. $title = _("Configuration Differences");
  76. require HORDE_TEMPLATES . '/common-header.inc';
  77. echo $template->fetch(HORDE_TEMPLATES . '/admin/config/diff.html');
  78. require HORDE_TEMPLATES . '/common-footer.inc';