access_url_edit.php 4.9 KB

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