access_url_edit.php 5.2 KB

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