plugin.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.plugin
  5. */
  6. /** @var \NoSearchIndex $plugin */
  7. $plugin = NoSearchIndex::create();
  8. $plugin_info = $plugin->get_info();
  9. $isPlatformAdmin = api_is_platform_admin();
  10. $editFile = false;
  11. $file = api_get_path(SYS_PATH).'robots.txt';
  12. $originalFile = api_get_path(SYS_PATH).'robots.dist.txt';
  13. $extraContentFile = api_get_home_path().'header_extra_content.txt';
  14. if ($isPlatformAdmin) {
  15. /** @var FormValidator $form */
  16. $form = $plugin_info['settings_form'];
  17. if ($form && $form->validate()) {
  18. if (is_writable(api_get_path(SYS_PATH))) {
  19. if (!file_exists($originalFile)) {
  20. copy($file, $originalFile);
  21. }
  22. } else {
  23. Display::addFlash(
  24. Display::return_message(
  25. sprintf(
  26. $plugin->get_lang('CheckDirectoryPermissionsInX'),
  27. api_get_path(SYS_PATH)
  28. ),
  29. 'warning'
  30. )
  31. );
  32. }
  33. if (!file_exists($extraContentFile)) {
  34. file_put_contents($extraContentFile, '');
  35. }
  36. $values = $form->getSubmitValues();
  37. $continue = false;
  38. if (file_exists($file) && is_readable($file) && is_writable($file) &&
  39. file_exists($originalFile) && is_readable($originalFile) && is_writable($originalFile) &&
  40. file_exists($extraContentFile) && is_readable($extraContentFile) && is_writable($extraContentFile)
  41. ) {
  42. $continue = true;
  43. }
  44. if ($continue) {
  45. $contents = file_get_contents($originalFile);
  46. $noFollow = '<meta name="robots" content="noindex" />';
  47. if (isset($values['tool_enable']) && $values['tool_enable'] == 'true') {
  48. $result = file_put_contents($file, $contents."\nDisallow: /\n");
  49. $value = '';
  50. if (file_exists($extraContentFile)) {
  51. $backup = file_get_contents($extraContentFile);
  52. file_put_contents($extraContentFile, $backup.$noFollow);
  53. } else {
  54. $value = file_put_contents($extraContentFile, $noFollow);
  55. }
  56. } else {
  57. file_put_contents($file, $contents);
  58. if (file_exists($extraContentFile)) {
  59. $backup = file_get_contents($extraContentFile);
  60. $backup = str_replace($noFollow, '', $backup);
  61. file_put_contents($extraContentFile, $backup);
  62. }
  63. }
  64. } else {
  65. api_delete_settings_params(
  66. [
  67. 'category = ? AND access_url = ? AND subkey = ? AND type = ? and variable = ?' => [
  68. 'Plugins',
  69. api_get_current_access_url_id(),
  70. 'nosearchindex',
  71. 'setting',
  72. 'nosearchindex_tool_enable',
  73. ],
  74. ]
  75. );
  76. $form->setElementError('tool_enable', $plugin->get_lang('CheckTheWritingPermissionsOfRobotsFile'));
  77. }
  78. }
  79. $plugin_info['settings_form'] = $form;
  80. }