plugin.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if ($isPlatformAdmin) {
  13. $originalFile = api_get_path(SYS_PATH).'robots.dist.txt';
  14. $extraContentFile = api_get_home_path().'header_extra_content.txt';
  15. if (!file_exists($originalFile)) {
  16. copy($file, $originalFile);
  17. }
  18. if (!file_exists($extraContentFile)) {
  19. file_put_contents($extraContentFile, '');
  20. }
  21. $originalContent = file_get_contents($originalFile);
  22. /** @var FormValidator $form */
  23. $form = $plugin_info['settings_form'];
  24. if ($form && $form->validate()) {
  25. $values = $form->getSubmitValues();
  26. $continue = false;
  27. if (is_readable($file) && is_writable($file) &&
  28. file_exists($originalFile) && is_readable($originalFile) && is_writable($originalFile) &&
  29. file_exists($extraContentFile) && is_readable($extraContentFile) && is_writable($extraContentFile)
  30. ) {
  31. $continue = true;
  32. }
  33. $continue = false;
  34. if ($continue) {
  35. $contents = file_get_contents($originalFile);
  36. $noFollow = '<meta name="robots" content="noindex" />';
  37. if (isset($values['tool_enable']) && $values['tool_enable'] == 'true') {
  38. $result = file_put_contents($file, $contents."\nDisallow: /\n");
  39. $value = '';
  40. if (file_exists($extraContentFile)) {
  41. $backup = file_get_contents($extraContentFile);
  42. file_put_contents($extraContentFile, $backup.$noFollow);
  43. } else {
  44. $value = file_put_contents($extraContentFile, $noFollow);
  45. }
  46. } else {
  47. file_put_contents($file, $contents);
  48. if (file_exists($extraContentFile)) {
  49. $backup = file_get_contents($extraContentFile);
  50. $backup = str_replace($noFollow, '', $backup);
  51. file_put_contents($extraContentFile, $backup);
  52. }
  53. }
  54. } else {
  55. /*Display::addFlash(
  56. Display::return_message($plugin->get_lang('CheckTheWritingPermissionsOfRobotsFile'), 'warning')
  57. );*/
  58. api_delete_settings_params(
  59. [
  60. 'category = ? AND access_url = ? AND subkey = ? AND type = ? and variable = ?' => [
  61. 'Plugins',
  62. api_get_current_access_url_id(),
  63. 'nosearchindex',
  64. 'setting',
  65. 'nosearchindex_tool_enable',
  66. ]
  67. ]
  68. );
  69. $form->setElementError('tool_enable', $plugin->get_lang('CheckTheWritingPermissionsOfRobotsFile'));
  70. }
  71. }
  72. $plugin_info['settings_form'] = $form;
  73. }