access_url_edit.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 __DIR__.'/../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 = isset($url_array['active']) ? intval($url_array['active']) : 0;
  24. $url_id = isset($url_array['id']) ? intval($url_array['id']) : 0;
  25. $url_to_go = 'access_urls.php';
  26. if (!empty($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. $fileFields = explode('.', $_FILES[$image_field]['name']);
  44. if (end($fileFields) == 'png') {
  45. if (file_exists($url_images_dir.$url_id.'_'.$image_field.'.png')) {
  46. // if the file exists, we have to remove it before move_uploaded_file
  47. unlink($url_images_dir.$url_id.'_'.$image_field.'.png');
  48. }
  49. move_uploaded_file(
  50. $_FILES[$image_field]['tmp_name'],
  51. $url_images_dir.$url_id.'_'.$image_field.'.png'
  52. );
  53. }
  54. // else fail silently
  55. }
  56. // else fail silently
  57. }
  58. $url_to_go = 'access_urls.php';
  59. $message = get_lang('URLEdited');
  60. } else {
  61. $num = UrlManager::url_exist($url);
  62. if ($num == 0) {
  63. // checking url
  64. if (substr($url, strlen($url) - 1, strlen($url)) == '/') {
  65. UrlManager::add($url, $description, $active);
  66. } else {
  67. //create
  68. UrlManager::add($url.'/', $description, $active);
  69. }
  70. $message = get_lang('URLAdded');
  71. $url_to_go = 'access_urls.php';
  72. } else {
  73. $url_to_go = 'access_url_edit.php';
  74. $message = get_lang('URLAlreadyAdded');
  75. }
  76. // URL Images
  77. $url .= (substr($url, strlen($url) - 1, strlen($url)) == '/') ? '' : '/';
  78. $url_id = UrlManager::get_url_id($url);
  79. $url_images_dir = api_get_path(SYS_PATH).'custompages/url-images/';
  80. $image_fields = array("url_image_1", "url_image_2", "url_image_3");
  81. foreach ($image_fields as $image_field) {
  82. if ($_FILES[$image_field]['error'] == 0) {
  83. // Hardcoded: only PNG files allowed
  84. $fileFields = explode('.', $_FILES[$image_field]['name']);
  85. if (end($fileFields) == 'png') {
  86. move_uploaded_file($_FILES[$image_field]['tmp_name'], $url_images_dir.$url_id.'_'.$image_field.'.png');
  87. }
  88. // else fail silently
  89. }
  90. // else fail silently
  91. }
  92. }
  93. Security::clear_token();
  94. $tok = Security::get_token();
  95. Display::addFlash(Display::return_message($message));
  96. header('Location: '.$url_to_go.'?sec_token='.$tok);
  97. exit();
  98. }
  99. } else {
  100. if (isset($_POST['submit'])) {
  101. Security::clear_token();
  102. }
  103. $token = Security::get_token();
  104. $form->addElement('hidden', 'sec_token');
  105. $form->setConstants(array('sec_token' => $token));
  106. }
  107. $form->addElement('text', 'url', 'URL');
  108. $form->addRule('url', get_lang('ThisFieldIsRequired'), 'required');
  109. $form->addRule('url', '', 'maxlength', 254);
  110. $form->addElement('textarea', 'description', get_lang('Description'));
  111. //the first url with id = 1 will be always active
  112. if (isset($_GET['url_id']) && $_GET['url_id'] != 1) {
  113. $form->addElement('checkbox', 'active', null, get_lang('Active'));
  114. }
  115. $defaults['url'] = 'http://';
  116. $form->setDefaults($defaults);
  117. $submit_name = get_lang('AddUrl');
  118. if (isset($_GET['url_id'])) {
  119. $url_id = intval($_GET['url_id']);
  120. $num_url_id = UrlManager::url_id_exist($url_id);
  121. if ($num_url_id != 1) {
  122. header('Location: access_urls.php');
  123. exit();
  124. }
  125. $url_data = UrlManager::get_url_data_from_id($url_id);
  126. $form->addElement('hidden', 'id', $url_data['id']);
  127. $form->setDefaults($url_data);
  128. $submit_name = get_lang('AddUrl');
  129. }
  130. if (!api_is_multiple_url_enabled()) {
  131. header('Location: index.php');
  132. exit;
  133. }
  134. $tool_name = get_lang('AddUrl');
  135. $interbreadcrumb[] = array("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  136. $interbreadcrumb[] = array("url" => 'access_urls.php', "name" => get_lang('MultipleAccessURLs'));
  137. Display :: display_header($tool_name);
  138. // URL Images
  139. $form->addElement('file', 'url_image_1', 'URL Image 1 (PNG)');
  140. $form->addElement('file', 'url_image_2', 'URL Image 2 (PNG)');
  141. $form->addElement('file', 'url_image_3', 'URL Image 3 (PNG)');
  142. // Submit button
  143. $form->addButtonCreate($submit_name);
  144. $form->display();
  145. Display::display_footer();