access_url_edit.php 6.0 KB

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