Fix create new access url

pull/3890/head
Julio Montoya 5 years ago
parent fa1d604e48
commit a0a4816858
  1. 133
      public/main/admin/access_url_edit.php
  2. 43
      public/main/inc/lib/urlmanager.lib.php
  3. 25
      public/main/inc/lib/webservices/Rest.php

@ -16,9 +16,42 @@ if (!api_get_multiple_access_url()) {
exit;
}
// Create the form
$form = new FormValidator('add_url');
$form->addUrl('url', 'URL');
$form->addRule('url', get_lang('Required field'), 'required');
$form->addRule('url', '', 'maxlength', 254);
$form->addTextarea( 'description', get_lang('Description'));
// URL Images
$form->addFile( 'url_image_1', get_lang('Image'));
//$form->addElement('file', 'url_image_2', 'URL Image 2 (PNG)');
//$form->addElement('file', 'url_image_3', 'URL Image 3 (PNG)');
$defaults['url'] = 'http://';
$form->setDefaults($defaults);
$submit_name = get_lang('Add URL');
if (isset($_GET['url_id'])) {
$url_id = (int) $_GET['url_id'];
$num_url_id = UrlManager::url_id_exist($url_id);
if (1 != $num_url_id) {
header('Location: access_urls.php');
exit();
}
$url_data = UrlManager::get_url_data_from_id($url_id);
$form->addElement('hidden', 'id', $url_data['id']);
$form->setDefaults($url_data);
$submit_name = get_lang('Add URL');
}
$form->addButtonCreate($submit_name);
//the first url with id = 1 will be always active
if (isset($_GET['url_id']) && 1 != $_GET['url_id']) {
$form->addElement('checkbox', 'active', null, get_lang('active'));
}
if ($form->validate()) {
$check = Security::check_token('post');
if ($check) {
@ -33,73 +66,38 @@ if ($form->validate()) {
if (1 == $url_id) {
$active = 1;
}
// Checking url
if ('/' == substr($url, strlen($url) - 1, strlen($url))) {
UrlManager::update($url_id, $url, $description, $active);
} else {
UrlManager::update($url_id, $url.'/', $description, $active);
}
// URL Images
$url_images_dir = api_get_path(SYS_PATH).'custompages/url-images/';
$image_fields = ['url_image_1', 'url_image_2', 'url_image_3'];
foreach ($image_fields as $image_field) {
if (0 == $_FILES[$image_field]['error']) {
// Hardcoded: only PNG files allowed
$fileFields = explode('.', $_FILES[$image_field]['name']);
if ('png' === end($fileFields)) {
if (file_exists($url_images_dir.$url_id.'_'.$image_field.'.png')) {
// if the file exists, we have to remove it before move_uploaded_file
unlink($url_images_dir.$url_id.'_'.$image_field.'.png');
}
move_uploaded_file(
$_FILES[$image_field]['tmp_name'],
$url_images_dir.$url_id.'_'.$image_field.'.png'
);
}
}
}
$url_to_go = 'access_urls.php';
$message = get_lang('The URL has been edited');
} else {
$num = UrlManager::url_exist($url);
if (0 == $num) {
$url_to_go = 'access_url_edit.php';
$message = get_lang('This URL already exists, please select another URL');
if (0 === $num) {
// checking url
if ('/' == substr($url, strlen($url) - 1, strlen($url))) {
UrlManager::add($url, $description, $active);
$accessUrl = UrlManager::add($url, $description, $active);
} else {
//create
UrlManager::add($url.'/', $description, $active);
$accessUrl = UrlManager::add($url.'/', $description, $active);
}
$message = get_lang('The URL has been added');
$url_to_go = 'access_urls.php';
} else {
$url_to_go = 'access_url_edit.php';
$message = get_lang('This URL already exists, please select another URL');
}
// URL Images
$url .= '/' == substr($url, strlen($url) - 1, strlen($url)) ? '' : '/';
$url_id = UrlManager::get_url_id($url);
$url_images_dir = api_get_path(SYS_PATH).'custompages/url-images/';
$image_fields = ['url_image_1', 'url_image_2', 'url_image_3'];
foreach ($image_fields as $image_field) {
if (0 == $_FILES[$image_field]['error']) {
// Hardcoded: only PNG files allowed
$fileFields = explode('.', $_FILES[$image_field]['name']);
if ('png' == end($fileFields)) {
move_uploaded_file(
$_FILES[$image_field]['tmp_name'],
$url_images_dir.$url_id.'_'.$image_field.'.png'
);
}
if (null !== $accessUrl) {
$message = get_lang('The URL has been added');
$url_to_go = 'access_urls.php';
}
}
}
Security::clear_token();
$tok = Security::get_token();
Display::addFlash(Display::return_message($message));
header('Location: '.$url_to_go.'?sec_token='.$tok);
exit();
exit;
}
} else {
if (isset($_POST['submit'])) {
@ -110,51 +108,10 @@ if ($form->validate()) {
$form->setConstants(['sec_token' => $token]);
}
$form->addElement('text', 'url', 'URL');
$form->addRule('url', get_lang('Required field'), 'required');
$form->addRule('url', '', 'maxlength', 254);
$form->addElement('textarea', 'description', get_lang('Description'));
//the first url with id = 1 will be always active
if (isset($_GET['url_id']) && 1 != $_GET['url_id']) {
$form->addElement('checkbox', 'active', null, get_lang('active'));
}
$defaults['url'] = 'http://';
$form->setDefaults($defaults);
$submit_name = get_lang('Add URL');
if (isset($_GET['url_id'])) {
$url_id = (int) $_GET['url_id'];
$num_url_id = UrlManager::url_id_exist($url_id);
if (1 != $num_url_id) {
header('Location: access_urls.php');
exit();
}
$url_data = UrlManager::get_url_data_from_id($url_id);
$form->addElement('hidden', 'id', $url_data['id']);
$form->setDefaults($url_data);
$submit_name = get_lang('Add URL');
}
if (!api_is_multiple_url_enabled()) {
header('Location: index.php');
exit;
}
$tool_name = get_lang('Add URL');
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
$interbreadcrumb[] = ['url' => 'access_urls.php', 'name' => get_lang('Multiple access URL / Branding')];
Display :: display_header($tool_name);
// URL Images
$form->addElement('file', 'url_image_1', 'URL Image 1 (PNG)');
$form->addElement('file', 'url_image_2', 'URL Image 2 (PNG)');
$form->addElement('file', 'url_image_3', 'URL Image 3 (PNG)');
// Submit button
$form->addButtonCreate($submit_name);
Display::display_header($tool_name);
$form->display();
Display::display_footer();

@ -1,6 +1,10 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Framework\Container;
/**
* Class UrlManager
* This library provides functions for the access_url management.
@ -16,24 +20,28 @@ class UrlManager
* @param string $url The URL of the site
* @param string $description The description of the site
* @param int $active is active or not
*
* @return int
*/
public static function add($url, $description, $active)
public static function add($url, $description, $active): ?AccessUrl
{
$tms = time();
$table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
$sql = "INSERT INTO $table
SET url = '".Database::escape_string($url)."',
description = '".Database::escape_string($description)."',
active = '".intval($active)."',
created_by = '".api_get_user_id()."',
tms = FROM_UNIXTIME(".$tms.")";
Database::query($sql);
$repo = Container::getAccessUrlRepository();
$id = Database::insert_id();
$num = self::url_exist($url);
return $id;
if (0 !== $num) {
return null;
}
$accessUrl = new AccessUrl();
$accessUrl
->setDescription($description)
->setActive($active)
->setUrl($url)
->setCreatedBy(api_get_user_id())
;
$repo->create($accessUrl);
return $accessUrl;
}
/**
@ -115,9 +123,7 @@ class UrlManager
$sql = "SELECT id FROM $table
WHERE url = '".Database::escape_string($url)."' ";
$res = Database::query($sql);
$num = Database::num_rows($res);
return $num;
return (int) Database::num_rows($res);
}
/**
@ -201,9 +207,8 @@ class UrlManager
FROM $table
WHERE id = ".$urlId;
$res = Database::query($sql);
$row = Database::fetch_array($res);
return $row;
return Database::fetch_array($res);
}
/**

@ -1445,27 +1445,24 @@ class Rest extends WebService
{
$urlCampus = Security::remove_XSS($params['url']);
$description = Security::remove_XSS($params['description']);
$active = isset($params['active']) ? (int) $params['active'] : 0;
$active = isset($params['active']) ? intval($params['active']) : 0;
$num = UrlManager::url_exist($urlCampus);
if (0 == $num) {
// checking url
if ('/' == substr($urlCampus, strlen($urlCampus) - 1, strlen($urlCampus))) {
$idCampus = UrlManager::add($urlCampus, $description, $active, true);
} else {
//create
$idCampus = UrlManager::add($urlCampus.'/', $description, $active, true);
}
if ('/' == substr($urlCampus, strlen($urlCampus) - 1, strlen($urlCampus))) {
$url = UrlManager::add($urlCampus, $description, $active, true);
} else {
$url = UrlManager::add($urlCampus.'/', $description, $active, true);
}
if (null === $url) {
return [
'status' => true,
'id_campus' => $idCampus,
'status' => false,
'id_campus' => 0,
];
}
return [
'status' => false,
'id_campus' => 0,
'status' => true,
'id_campus' => $url->getId(),
];
}

Loading…
Cancel
Save