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