PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/wire/core/admin.php

http://github.com/ryancramerdesign/ProcessWire
PHP | 152 lines | 99 code | 27 blank | 26 comment | 40 complexity | 6d7259408bc87a2e993bff23caa5de07 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Controller for ProcessWire Admin
  4. *
  5. * This file is designed for inclusion by /site/templates/admin.php template and all the variables
  6. * it references are from your template namespace.
  7. *
  8. * Copyright 2015 by Ryan Cramer
  9. * This file licensed under Mozilla Public License v2.0 (http://mozilla.org/MPL/2.0/)
  10. *
  11. */
  12. if(!defined("PROCESSWIRE")) die("This file may not be accessed directly.");
  13. header("X-Frame-Options: SAMEORIGIN");
  14. /**
  15. * Ensures a modal GET variable is retained through redirects, when appropriate
  16. *
  17. */
  18. function _hookSessionRedirectModal(HookEvent $event) {
  19. $url = $event->arguments(0);
  20. if(strpos($url, 'modal=1') === false && strpos($url, '://') === false) {
  21. $url .= (strpos($url, '?') === false ? '?' : '&') . 'modal=1';
  22. $event->arguments(0, $url);
  23. }
  24. }
  25. /**
  26. * Check if the current HTTP host is recognized and generate error if not
  27. *
  28. * @param $config
  29. *
  30. */
  31. function _checkForHttpHostError($config) {
  32. $valid = false;
  33. $httpHost = strtolower($config->httpHost);
  34. if(isset($_SERVER['HTTP_HOST']) && $httpHost === strtolower($_SERVER['HTTP_HOST'])) {
  35. $valid = true;
  36. } else if(isset($_SERVER['SERVER_NAME']) && $httpHost === strtolower($_SERVER['SERVER_NAME'])) {
  37. $valid = true;
  38. }
  39. if(!$valid) $config->error(
  40. __('Unrecognized HTTP host:') . "'" .
  41. htmlentities($_SERVER['HTTP_HOST'], ENT_QUOTES, 'UTF-8') . "' - " .
  42. __('Please update your $config->httpHosts setting in /site/config.php') . " - " .
  43. "<a target='_blank' href='https://processwire.com/api/variables/config/#httphosts'>" . __('read more') . "</a>",
  44. Notice::allowMarkup
  45. );
  46. }
  47. // notify superuser if there is an http host error
  48. if($user->isSuperuser()) _checkForHttpHostError($config);
  49. // ensure core jQuery modules are loaded before others
  50. $modules->get("JqueryCore");
  51. $modules->get("JqueryUI");
  52. // tell ProcessWire that any pages loaded from this point forward should have their outputFormatting turned off
  53. $pages->setOutputFormatting(false);
  54. // setup breadcrumbs to current page, and the Process may modify, add to or replace them as needed
  55. $breadcrumbs = new Breadcrumbs();
  56. foreach($page->parents() as $p) {
  57. if($p->id > 1) $breadcrumbs->add(new Breadcrumb($p->url, $p->get("title|name")));
  58. }
  59. Wire::setFuel('breadcrumbs', $breadcrumbs);
  60. $controller = null;
  61. $content = '';
  62. // enable modules to output their own ajax responses if they choose to
  63. if($config->ajax) ob_start();
  64. if($page->process && $page->process != 'ProcessPageView') {
  65. try {
  66. if($config->demo && !in_array($page->process, array('ProcessLogin'))) {
  67. if(count($_POST)) $wire->error("Features that use POST variables are disabled in this demo");
  68. foreach($_POST as $k => $v) unset($_POST[$k]);
  69. foreach($_FILES as $k => $v) unset($_FILES[$k]);
  70. $input->post->removeAll();
  71. }
  72. $controller = new ProcessController();
  73. $controller->setProcessName($page->process);
  74. $initFile = $config->paths->adminTemplates . 'init.php';
  75. if(is_file($initFile)) include($initFile);
  76. if($input->get->modal) $session->addHookBefore('redirect', null, '_hookSessionRedirectModal');
  77. $content = $controller->execute();
  78. } catch(Wire404Exception $e) {
  79. $wire->error($e->getMessage());
  80. } catch(WirePermissionException $e) {
  81. if($controller && $controller->isAjax()) {
  82. $content = $controller->jsonMessage($e->getMessage(), true);
  83. } else if($user->isGuest()) {
  84. $process = $modules->get("ProcessLogin");
  85. $content = $process->execute();
  86. } else {
  87. $wire->error($e->getMessage());
  88. }
  89. } catch(Exception $e) {
  90. $msg = $e->getMessage();
  91. if($config->debug) {
  92. $msg = $sanitizer->entities($msg);
  93. $msg .= "<pre>\n" .
  94. __('DEBUG MODE BACKTRACE') . " " .
  95. "(\$config->debug == true):\n" .
  96. $sanitizer->entities($e->getTraceAsString()) .
  97. "</pre>";
  98. $wire->error("$page->process: $msg", Notice::allowMarkup);
  99. } else {
  100. $wire->error($msg);
  101. }
  102. if($controller && $controller->isAjax()) {
  103. $content = $controller->jsonMessage($e->getMessage(), true);
  104. $wire->trackException($e, false);
  105. } else {
  106. $wire->trackException($e, true);
  107. }
  108. }
  109. } else {
  110. $content = '<p>' . __('This page has no process assigned.') . '</p>';
  111. }
  112. if($config->ajax) {
  113. // enable modules to output their own ajax responses if they choose to
  114. if(!$content) $content = ob_get_contents();
  115. ob_end_clean();
  116. }
  117. $config->js(array('httpHost', 'httpHosts'), true);
  118. if($controller && $controller->isAjax()) {
  119. if(empty($content) && count($notices)) $content = $controller->jsonMessage($notices->last()->text);
  120. echo $content;
  121. } else {
  122. if(!strlen($content)) $content = '<p>' . __('The process returned no content.') . '</p>';
  123. require($config->paths->adminTemplates . 'default.php');
  124. $session->removeNotices();
  125. }