PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/concrete/controllers/single_page/dashboard/system/seo/urls.php

http://github.com/concrete5/concrete5
PHP | 189 lines | 171 code | 7 blank | 11 comment | 34 complexity | 2df64c0b6b3d2e198fca45ab57dc50bf MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. namespace Concrete\Controller\SinglePage\Dashboard\System\Seo;
  3. use Concrete\Core\Page\Controller\DashboardSitePageController;
  4. class Urls extends DashboardSitePageController
  5. {
  6. /**
  7. * Dashboard page view.
  8. *
  9. * @param string|bool $strStatus - Result of attempting to update rewrite rules
  10. * @param bool $prettyUrlState - Flag denoting if the rewrite rule has been saved
  11. */
  12. public function view($strStatus = false, $prettyUrlState = '')
  13. {
  14. $globalConfig = $this->app->make('config');
  15. $siteConfig = $this->getSite()->getConfigRepository();
  16. $urlRewriting = (bool) $globalConfig->get('concrete.seo.url_rewriting');
  17. $this->set('fh', $this->app->make('helper/form'));
  18. $this->set('canonical_url', $siteConfig->get('seo.canonical_url'));
  19. $this->set('canonical_url_alternative', $siteConfig->get('seo.canonical_url_alternative'));
  20. $this->set('redirect_to_canonical_url', $globalConfig->get('concrete.seo.redirect_to_canonical_url'));
  21. $this->set('urlRewriting', $urlRewriting);
  22. $this->set('canonical_tag', $siteConfig->get('seo.canonical_tag.enabled'));
  23. $strStatus = (string) $strStatus;
  24. if ($strStatus === 'saved') {
  25. $message = t('Settings Saved.');
  26. $prettyUrlState = (string) $prettyUrlState;
  27. switch ($prettyUrlState) {
  28. case 'saved':
  29. case 'not-needed':
  30. $manager = $this->app->make('Concrete\Core\Service\Manager\ServiceManager');
  31. $services = $manager->getActiveServices();
  32. $service = $services[0];
  33. $rule = $service->getGenerator()->getRule('pretty_urls');
  34. switch ($prettyUrlState) {
  35. case 'saved':
  36. if ($urlRewriting) {
  37. $this->set('configuration_action', t('The following rule has been added to the server configuration'));
  38. } else {
  39. $this->set('configuration_action', t('The following rule has been removed from the server configuration'));
  40. }
  41. break;
  42. case 'not-needed':
  43. if ($urlRewriting) {
  44. $this->set('configuration_action', t('The following rule was already in your server configuration'));
  45. } else {
  46. $this->set('configuration_action', t('The following rule was already missing in your server configuration'));
  47. }
  48. break;
  49. }
  50. $this->set('configuration_code', $rule->getCode());
  51. break;
  52. case 'unrecognized':
  53. $codes = array();
  54. $manager = $this->app->make('Concrete\Core\Service\Manager\ServiceManager');
  55. /* @var \Concrete\Core\Service\Manager\ServiceManager $manager */
  56. foreach ($manager->getAllServices() as $service) {
  57. $rule = $service->getGenerator()->getRule('pretty_urls');
  58. if ($rule !== null) {
  59. if (isset($codes[$rule->getCode()])) {
  60. $codes[$rule->getCode()][] = $service->getName();
  61. } else {
  62. $codes[$rule->getCode()] = array($service->getName());
  63. }
  64. }
  65. }
  66. $actionMessage = t("It was not possible to detect your server kind.");
  67. if ($urlRewriting) {
  68. $actionMessage .= ' '.t("Here's the configuration sections for every supported server: please manually add the one relevant for you to your server configuration.");
  69. } else {
  70. $actionMessage .= ' '.t("Here's the configuration sections for every supported server: please manually remove the one relevant for you from your server configuration.");
  71. }
  72. $this->set('configuration_action', $actionMessage);
  73. $joined = '';
  74. foreach ($codes as $code => $serviceNames) {
  75. if ($joined !== '') {
  76. $joined .= "\n\n";
  77. }
  78. $joined .= '>>> ' . tc(/*i18n %s is one or more server names */'For server', 'For %s', implode(', ', $serviceNames)) . " <<<\n";
  79. $joined .= $code;
  80. }
  81. $this->set('configuration_code', $joined);
  82. break;
  83. case 'not-readable':
  84. case 'not-writable':
  85. $manager = $this->app->make('Concrete\Core\Service\Manager\ServiceManager');
  86. $services = $manager->getActiveServices();
  87. $service = $services[0];
  88. $rule = $service->getGenerator()->getRule('pretty_urls');
  89. $actionMessage = '';
  90. switch ($prettyUrlState) {
  91. case 'not-readable':
  92. $actionMessage .= t('It was not possible read your server configuration.');
  93. break;
  94. case 'not-writable':
  95. $actionMessage .= t('It was not possible write your server configuration.');
  96. break;
  97. }
  98. if ($urlRewriting) {
  99. $actionMessage .= ' '.t('Please add this configuration section to your server configuration:');
  100. } else {
  101. $actionMessage .= ' '.t('Please remove this configuration section from your server configuration');
  102. }
  103. $this->set('configuration_action', $actionMessage);
  104. $this->set('configuration_code', $rule->getCode());
  105. break;
  106. }
  107. $this->set('message', $message);
  108. }
  109. }
  110. /**
  111. * Updates the .htaccess file (if writable).
  112. */
  113. public function save_urls()
  114. {
  115. if ($this->isPost()) {
  116. if (!$this->token->validate('save_urls')) {
  117. $this->error->add($this->token->getErrorMessage());
  118. }
  119. if ($this->post('canonical_url') &&
  120. !(
  121. strpos(strtolower($this->post('canonical_url')), 'http://') === 0 ||
  122. strpos(strtolower($this->post('canonical_url')), 'https://') === 0
  123. )) {
  124. $this->error->add(t('The canonical URL provided must start with "http://" or "https://".'));
  125. }
  126. if ($this->post('canonical_url_alternative') &&
  127. !(
  128. strpos(strtolower($this->post('canonical_url_alternative')), 'http://') === 0 ||
  129. strpos(strtolower($this->post('canonical_url_alternative')), 'https://') === 0
  130. )) {
  131. $this->error->add(t('The alternative canonical URL provided must start with "http://" or "https://".'));
  132. }
  133. if (!$this->error->has()) {
  134. $globalConfig = $this->app->make('config');
  135. $siteConfig = $this->getSite()->getConfigRepository();
  136. $siteConfig->save('seo.canonical_url', $this->post('canonical_url'));
  137. $siteConfig->save('seo.canonical_url_alternative', $this->post('canonical_url_alternative'));
  138. $globalConfig->save('concrete.seo.redirect_to_canonical_url', $this->post('redirect_to_canonical_url') ? 1 : 0);
  139. $siteConfig->save('seo.canonical_tag.enabled', (bool) $this->post('canonical_tag'));
  140. $urlRewriting = (bool) $this->post('URL_REWRITING');
  141. $globalConfig->save('concrete.seo.url_rewriting', $urlRewriting);
  142. $globalConfig->set('concrete.seo.url_rewriting', $urlRewriting);
  143. $manager = $this->app->make('Concrete\Core\Service\Manager\ServiceManager');
  144. /* @var \Concrete\Core\Service\Manager\ServiceManager $manager */
  145. $prettyUrlState = '';
  146. $services = $manager->getActiveServices();
  147. if (empty($services)) {
  148. $prettyUrlState = 'unrecognized';
  149. } else {
  150. $service = $services[0];
  151. if (!$service->getStorage()->canRead()) {
  152. $prettyUrlState = 'not-readable';
  153. } else {
  154. $rule = $service->getGenerator()->getRule('pretty_urls');
  155. if ($rule === null) {
  156. $prettyUrlState = 'not-needed';
  157. } else {
  158. $configuration = $service->getStorage()->read();
  159. if ($service->getConfigurator()->hasRule($configuration, $rule) === $urlRewriting) {
  160. $prettyUrlState = 'not-needed';
  161. } else {
  162. if ($service->getStorage()->canWrite() === false) {
  163. $prettyUrlState = 'not-writable';
  164. } else {
  165. if ($urlRewriting) {
  166. $configuration = $service->getConfigurator()->addRule($configuration, $rule);
  167. } else {
  168. $configuration = $service->getConfigurator()->removeRule($configuration, $rule);
  169. }
  170. $service->getStorage()->write($configuration);
  171. $prettyUrlState = 'saved';
  172. }
  173. }
  174. }
  175. }
  176. }
  177. $this->redirect('/dashboard/system/seo/urls', 'saved', $prettyUrlState);
  178. }
  179. }
  180. $this->view();
  181. }
  182. }