Skip to content
Permalink
Browse files Browse the repository at this point in the history
Fix XML import user_import.php results page
Add new function Import:xml and use libxml_disable_entity_loader
Move code into a function
  • Loading branch information
jmontoyaa committed May 12, 2021
1 parent a203caa commit e71437c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
40 changes: 28 additions & 12 deletions main/admin/user_import.php
@@ -1,4 +1,5 @@
<?php

/* For licensing terms, see /license.txt */

use Chamilo\CoreBundle\Entity\ExtraFieldOptions;
Expand All @@ -13,7 +14,6 @@
// Set this option to true to enforce strict purification for usenames.
$purification_option_for_usernames = false;
$userId = api_get_user_id();

api_protect_admin_script(true, null);
api_protect_limit_for_session_admin();
set_time_limit(0);
Expand Down Expand Up @@ -220,7 +220,6 @@ function save_data($users, $sendMail = false)
if (!isset($inserted_in_course)) {
$inserted_in_course = [];
}

$usergroup = new UserGroup();
if (is_array($users)) {
$efo = new ExtraFieldOption('user');
Expand Down Expand Up @@ -433,10 +432,9 @@ function parse_csv_data($users, $fileName, $sendEmail = 0, $checkUniqueEmail = t
*
* @return array All user information read from the file
*/
function parse_xml_data($file)
function parse_xml_data($file, $sendEmail = 0, $checkUniqueEmail = true)
{
$crawler = new \Symfony\Component\DomCrawler\Crawler();
$crawler->addXmlContent(file_get_contents($file));
$crawler = Import::xml($file);
$crawler = $crawler->filter('Contacts > Contact ');
$array = [];
foreach ($crawler as $domElement) {
Expand All @@ -451,6 +449,16 @@ function parse_xml_data($file)
}
}

Session::write(
'user_import_data_'.api_get_user_id(),
[
'check_unique_email' => $checkUniqueEmail,
'send_email' => $sendEmail,
'date' => api_get_utc_datetime(),
'log_messages' => '',
]
);

return $array;
}

Expand Down Expand Up @@ -548,7 +556,11 @@ function processUsers(&$users, $sendMail)
$users = validate_data($users, $checkUniqueEmail);
$error_kind_file = false;
} elseif (strcmp($file_type, 'xml') === 0 && $ext_import_file == $allowed_file_mimetype[1]) {
$users = parse_xml_data($_FILES['import_file']['tmp_name']);
$users = parse_xml_data(
$_FILES['import_file']['tmp_name'],
$sendMail,
$checkUniqueEmail
);
$users = validate_data($users, $checkUniqueEmail);
$error_kind_file = false;
}
Expand Down Expand Up @@ -590,15 +602,17 @@ function processUsers(&$users, $sendMail)
$formContinue = false;
$resumeStop = true;
if (!empty($importData)) {
$isResume = $importData['resume'];
$isResume = $importData['resume'] ?? false;

$formContinue = new FormValidator('user_import_continue', 'post', api_get_self());
$label = get_lang('Results');
if ($isResume) {
$label = get_lang('ContinueLastImport');
}
$formContinue->addHeader($label);
$formContinue->addLabel(get_lang('File'), $importData['filename']);
if (isset($importData['filename'])) {
$formContinue->addLabel(get_lang('File'), $importData['filename'] ?? '');
}

$resumeStop = true;
if ($isResume) {
Expand All @@ -614,10 +628,12 @@ function processUsers(&$users, $sendMail)
$importData['counter'].' / '.count($importData['complete_list'])
);
} else {
$formContinue->addLabel(
get_lang('Users'),
count($importData['complete_list'])
);
if (!empty($importData['complete_list'])) {
$formContinue->addLabel(
get_lang('Users'),
count($importData['complete_list'])
);
}
}

$formContinue->addLabel(
Expand Down
3 changes: 1 addition & 2 deletions main/admin/user_update_import.php
Expand Up @@ -256,8 +256,7 @@ function parse_csv_data($file)

function parse_xml_data($file)
{
$crawler = new Crawler();
$crawler->addXmlContent(file_get_contents($file));
$crawler = Import::xml($file);
$crawler = $crawler->filter('Contacts > Contact ');
$array = [];
foreach ($crawler as $domElement) {
Expand Down
20 changes: 18 additions & 2 deletions main/inc/lib/import.lib.php
@@ -1,8 +1,10 @@
<?php

/* For licensing terms, see /license.txt */

use Ddeboer\DataImport\Reader\ExcelReader;
use League\Csv\Reader;
use Symfony\Component\DomCrawler\Crawler;

/**
* Class Import
Expand Down Expand Up @@ -79,8 +81,22 @@ public static function xlsToArray($filename)
}

$file = new \SplFileObject($filename);
$reader = new ExcelReader($file, 0);

return $reader;
return new ExcelReader($file, 0);
}

/**
* @param string $file
*
* @return Crawler
*/
public static function xml($file)
{
@libxml_disable_entity_loader(true);

$crawler = new Crawler();
$crawler->addXmlContent(file_get_contents($file));

return $crawler;
}
}
3 changes: 1 addition & 2 deletions main/inc/lib/myspace.lib.php
Expand Up @@ -3727,8 +3727,7 @@ public function parse_csv_data($file)
*/
public static function parse_xml_data($file)
{
$crawler = new \Symfony\Component\DomCrawler\Crawler();
$crawler->addXmlContent(file_get_contents($file));
$crawler = Import::xml($file);
$crawler = $crawler->filter('Contacts > Contact ');
$array = [];
foreach ($crawler as $domElement) {
Expand Down
5 changes: 3 additions & 2 deletions main/lp/scorm.class.php
Expand Up @@ -95,8 +95,9 @@ public function parse_manifest($file = '')
// UTF-8 is supported by DOMDocument class, this is for sure.
$xml = api_utf8_encode_xml($xml, $this->manifest_encoding);

$crawler = new Crawler();
$crawler->addXmlContent($xml);
/*$crawler = new Crawler();
$crawler->addXmlContent($xml);*/
$crawler = Import::xml($xml);

$xmlErrors = libxml_get_errors();

Expand Down

0 comments on commit e71437c

Please sign in to comment.