access_url_edit.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. *
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. $this_section = SECTION_PLATFORM_ADMIN;
  11. api_protect_global_admin_script();
  12. if (!api_get_multiple_access_url()) {
  13. header('Location: index.php');
  14. exit;
  15. }
  16. // Create the form
  17. $form = new FormValidator('add_url');
  18. if ($form->validate()) {
  19. $check = Security::check_token('post');
  20. if ($check) {
  21. $url_array = $form->getSubmitValues();
  22. $url = Security::remove_XSS($url_array['url']);
  23. $description = Security::remove_XSS($url_array['description']);
  24. $active = isset($url_array['active']) ? intval($url_array['active']) : 0;
  25. $url_id = isset($url_array['id']) ? intval($url_array['id']) : 0;
  26. $url_to_go = 'access_urls.php';
  27. if (!empty($url_id)) {
  28. //we can't change the status of the url with id=1
  29. if ($url_id == 1) {
  30. $active = 1;
  31. }
  32. //checking url
  33. if (substr($url, strlen($url) - 1, strlen($url)) == '/') {
  34. UrlManager::update($url_id, $url, $description, $active);
  35. } else {
  36. UrlManager::update($url_id, $url.'/', $description, $active);
  37. }
  38. // URL Images
  39. $url_images_dir = api_get_path(SYS_PATH).'custompages/url-images/';
  40. $image_fields = ["url_image_1", "url_image_2", "url_image_3"];
  41. foreach ($image_fields as $image_field) {
  42. if ($_FILES[$image_field]['error'] == 0) {
  43. // Hardcoded: only PNG files allowed
  44. $fileFields = explode('.', $_FILES[$image_field]['name']);
  45. if (end($fileFields) == 'png') {
  46. if (file_exists($url_images_dir.$url_id.'_'.$image_field.'.png')) {
  47. // if the file exists, we have to remove it before move_uploaded_file
  48. unlink($url_images_dir.$url_id.'_'.$image_field.'.png');
  49. }
  50. move_uploaded_file(
  51. $_FILES[$image_field]['tmp_name'],
  52. $url_images_dir.$url_id.'_'.$image_field.'.png'
  53. );
  54. }
  55. // else fail silently
  56. }
  57. // else fail silently
  58. }
  59. $url_to_go = 'access_urls.php';
  60. $message = get_lang('URLEdited');
  61. } else {
  62. $num = UrlManager::url_exist($url);
  63. if ($num == 0) {
  64. // checking url
  65. if (substr($url, strlen($url) - 1, strlen($url)) == '/') {
  66. UrlManager::add($url, $description, $active);
  67. } else {
  68. //create
  69. UrlManager::add($url.'/', $description, $active);
  70. }
  71. $message = get_lang('URLAdded');
  72. $url_to_go = 'access_urls.php';
  73. } else {
  74. $url_to_go = 'access_url_edit.php';
  75. $message = get_lang('URLAlreadyAdded');
  76. }
  77. // URL Images
  78. $url .= (substr($url, strlen($url) - 1, strlen($url)) == '/') ? '' : '/';
  79. $url_id = UrlManager::get_url_id($url);
  80. $url_images_dir = api_get_path(SYS_PATH).'custompages/url-images/';
  81. $image_fields = ["url_image_1", "url_image_2", "url_image_3"];
  82. foreach ($image_fields as $image_field) {
  83. if ($_FILES[$image_field]['error'] == 0) {
  84. // Hardcoded: only PNG files allowed
  85. $fileFields = explode('.', $_FILES[$image_field]['name']);
  86. if (end($fileFields) == 'png') {
  87. move_uploaded_file($_FILES[$image_field]['tmp_name'], $url_images_dir.$url_id.'_'.$image_field.'.png');
  88. }
  89. // else fail silently
  90. }
  91. // else fail silently
  92. }
  93. }
  94. Security::clear_token();
  95. $tok = Security::get_token();
  96. Display::addFlash(Display::return_message($message));
  97. header('Location: '.$url_to_go.'?sec_token='.$tok);
  98. exit();
  99. }
  100. } else {
  101. if (isset($_POST['submit'])) {
  102. Security::clear_token();
  103. }
  104. $token = Security::get_token();
  105. $form->addElement('hidden', 'sec_token');
  106. $form->setConstants(['sec_token' => $token]);
  107. }
  108. $form->addElement('text', 'url', 'URL');
  109. $form->addRule('url', get_lang('ThisFieldIsRequired'), 'required');
  110. $form->addRule('url', '', 'maxlength', 254);
  111. $form->addElement('textarea', 'description', get_lang('Description'));
  112. //the first url with id = 1 will be always active
  113. if (isset($_GET['url_id']) && $_GET['url_id'] != 1) {
  114. $form->addElement('checkbox', 'active', null, get_lang('Active'));
  115. }
  116. $defaults['url'] = 'http://';
  117. $form->setDefaults($defaults);
  118. $submit_name = get_lang('AddUrl');
  119. if (isset($_GET['url_id'])) {
  120. $url_id = intval($_GET['url_id']);
  121. $num_url_id = UrlManager::url_id_exist($url_id);
  122. if ($num_url_id != 1) {
  123. header('Location: access_urls.php');
  124. exit();
  125. }
  126. $url_data = UrlManager::get_url_data_from_id($url_id);
  127. $form->addElement('hidden', 'id', $url_data['id']);
  128. $form->setDefaults($url_data);
  129. $submit_name = get_lang('AddUrl');
  130. }
  131. if (!api_is_multiple_url_enabled()) {
  132. header('Location: index.php');
  133. exit;
  134. }
  135. $tool_name = get_lang('AddUrl');
  136. $interbreadcrumb[] = ["url" => 'index.php', "name" => get_lang('PlatformAdmin')];
  137. $interbreadcrumb[] = ["url" => 'access_urls.php', "name" => get_lang('MultipleAccessURLs')];
  138. Display :: display_header($tool_name);
  139. // URL Images
  140. $form->addElement('file', 'url_image_1', 'URL Image 1 (PNG)');
  141. $form->addElement('file', 'url_image_2', 'URL Image 2 (PNG)');
  142. $form->addElement('file', 'url_image_3', 'URL Image 3 (PNG)');
  143. // Submit button
  144. $form->addButtonCreate($submit_name);
  145. $form->display();
  146. Display::display_footer();