access_url_edit.php 5.9 KB

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