access_url_edit.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Create or edit access urls and branches
  5. * @package chamilo.admin
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  8. */
  9. /**
  10. * Initialization
  11. */
  12. $language_file = 'admin';
  13. $cidReset = true;
  14. require_once '../inc/global.inc.php';
  15. $this_section = SECTION_PLATFORM_ADMIN;
  16. //api_protect_admin_script();
  17. api_protect_global_admin_script();
  18. if (!api_get_multiple_access_url()) {
  19. header('Location: index.php');
  20. exit;
  21. }
  22. // Create the form
  23. $form = new FormValidator('add_url');
  24. if( $form->validate()) {
  25. $check = Security::check_token('post');
  26. if($check) {
  27. $url_array = $form->getSubmitValues();
  28. $url = Security::remove_XSS($url_array['url']);
  29. $description = Security::remove_XSS($url_array['description']);
  30. $active = intval($url_array['active']);
  31. $url_id = $url_array['id'];
  32. $url_to_go='access_urls.php';
  33. if ($url_id!='') {
  34. //we can't change the status of the url with id=1
  35. if ($url_id==1)
  36. $active=1;
  37. //checking url
  38. if (substr($url,-1)!=='/') {
  39. $url_id .= '/';
  40. }
  41. UrlManager::udpate($url_id, $url, $description, $active, $url_array['url_type'], $url_array);
  42. // URL Images
  43. $url_images_dir = api_get_path(SYS_PATH).'custompages/url-images/';
  44. $image_fields = array("url_image_1", "url_image_2", "url_image_3");
  45. foreach ($image_fields as $image_field) {
  46. if ($_FILES[$image_field]['error'] == 0) {
  47. // Hardcoded: only PNG files allowed
  48. if (end(explode('.', $_FILES[$image_field]['name'])) == 'png') {
  49. move_uploaded_file($_FILES[$image_field]['tmp_name'], $url_images_dir.$url_id.'_'.$image_field.'.png');
  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,-1)!='/' && $url_array['url_type'] == 1) {
  62. $url.='/';
  63. }
  64. $url_array['ip'] = $url_array['url'];
  65. UrlManager::add($url, $description, $active, $url_array['url_type'], $url_array);
  66. $message = get_lang('URLAdded');
  67. $url_to_go='access_urls.php';
  68. } else {
  69. $url_to_go='access_url_edit.php';
  70. $message = get_lang('URLAlreadyAdded');
  71. }
  72. // URL Images
  73. $url .= (substr($url,strlen($url)-1, strlen($url))=='/') ? '' : '/';
  74. $url_id = UrlManager::get_url_id($url);
  75. $url_images_dir = api_get_path(SYS_PATH).'custompages/url-images/';
  76. $image_fields = array("url_image_1", "url_image_2", "url_image_3");
  77. foreach ($image_fields as $image_field) {
  78. if ($_FILES[$image_field]['error'] == 0) {
  79. // Hardcoded: only PNG files allowed
  80. if (end(explode('.', $_FILES[$image_field]['name'])) == 'png') {
  81. move_uploaded_file($_FILES[$image_field]['tmp_name'], $url_images_dir.$url_id.'_'.$image_field.'.png');
  82. }
  83. // else fail silently
  84. }
  85. // else fail silently
  86. }
  87. }
  88. Security::clear_token();
  89. $tok = Security::get_token();
  90. header('Location: '.$url_to_go.'?action=show_message&message='.urlencode($message).'&sec_token='.$tok);
  91. exit();
  92. }
  93. } else {
  94. if(isset($_POST['submit'])) {
  95. Security::clear_token();
  96. }
  97. $token = Security::get_token();
  98. $form->addElement('hidden','sec_token');
  99. $form->setConstants(array('sec_token' => $token));
  100. }
  101. $form->addElement('text','url', get_lang('URLIP'), array('class'=>'span6'));
  102. $form->addRule('url', get_lang('ThisFieldIsRequired'), 'required');
  103. $form->addRule('url', '', 'maxlength',254);
  104. $types = array(
  105. 1=>get_lang('AccessURL'),
  106. 2=>get_lang('SincroServer'),
  107. 3=>get_lang('SincroClient'),
  108. );
  109. $form->addElement('select', 'url_type', get_lang('Type'), $types);
  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. //$form->addRule('checkbox', get_lang('ThisFieldIsRequired'), 'required');
  116. $defaults['url']='http://';
  117. $form->setDefaults($defaults);
  118. $submit_name = get_lang('AddUrl');
  119. if (isset($_GET['url_id'])) {
  120. $url_id = Database::escape_string($_GET['url_id']);
  121. $num_url_id = UrlManager::url_id_exist($url_id);
  122. if($num_url_id != 1) {
  123. header('Location: access_urls.php');
  124. exit();
  125. }
  126. $url_data = UrlManager::get_url_data_from_id($url_id);
  127. $form->addElement('hidden','id',$url_data['id']);
  128. $form->setDefaults($url_data);
  129. $submit_name = get_lang('AddUrl');
  130. }
  131. if (!api_is_multiple_url_enabled()) {
  132. header('Location: index.php');
  133. exit;
  134. }
  135. $tool_name = get_lang('AddUrl');
  136. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  137. $interbreadcrumb[] = array ("url" => 'access_urls.php', "name" => get_lang('MultipleAccessURLs'));
  138. /**
  139. * View
  140. */
  141. Display :: display_header($tool_name);
  142. if (isset ($_GET['action'])) {
  143. switch ($_GET['action']) {
  144. case 'show_message' :
  145. Display :: display_normal_message(stripslashes($_GET['message']));
  146. break;
  147. }
  148. }
  149. // URL Images
  150. $form->addElement('file','url_image_1','URL Image 1 (PNG)');
  151. $form->addElement('file','url_image_2','URL Image 2 (PNG)');
  152. $form->addElement('file','url_image_3','URL Image 3 (PNG)');
  153. // Submit button
  154. $form->addElement('style_submit_button', 'submit', $submit_name, 'class="add"');
  155. $form->display();
  156. Display::display_footer();