Skip to content

Commit e55b5f5

Browse files
authored
Merge pull request #2778 from chamilo/xml_entity_fixes
Xml entity fixes
2 parents 8a21d41 + 61f08ac commit e55b5f5

15 files changed

+456
-1174
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
"ext-xml": "*",
4343
"ext-json": "*",
4444
"ext-mbstring": "*",
45+
"ext-libxml": "*",
46+
"ext-dom": "*",
4547
"twig/twig": "1.*",
4648
"twig/extensions": "~1.0",
4749
"doctrine/orm": "~2.4",
@@ -109,7 +111,8 @@
109111
"culqi/culqi-php": "1.3.4",
110112
"knplabs/knp-components": "~1.3",
111113
"guzzlehttp/guzzle": "~6.0",
112-
"onelogin/php-saml": "^3.0"
114+
"onelogin/php-saml": "^3.0",
115+
"symfony/dom-crawler": "~3.4"
113116
},
114117
"require-dev": {
115118
"behat/behat": "@stable",

main/admin/skills_import.php

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -98,76 +98,6 @@ function parse_csv_data($file)
9898
return $skills;
9999
}
100100

101-
/**
102-
* XML-parser: handle start of element.
103-
*/
104-
function element_start($parser, $data)
105-
{
106-
$data = api_utf8_decode($data);
107-
global $skill;
108-
global $current_tag;
109-
switch ($data) {
110-
case 'Skill':
111-
$skill = [];
112-
break;
113-
default:
114-
$current_tag = $data;
115-
}
116-
}
117-
118-
/**
119-
* XML-parser: handle end of element.
120-
*/
121-
function element_end($parser, $data)
122-
{
123-
$data = api_utf8_decode($data);
124-
global $skill;
125-
global $skills;
126-
global $current_value;
127-
switch ($data) {
128-
case 'Skill':
129-
$skills[] = $skill;
130-
break;
131-
default:
132-
$skill[$data] = $current_value;
133-
break;
134-
}
135-
}
136-
137-
/**
138-
* XML-parser: handle character data.
139-
*/
140-
function character_data($parser, $data)
141-
{
142-
$data = trim(api_utf8_decode($data));
143-
global $current_value;
144-
$current_value = $data;
145-
}
146-
147-
/**
148-
* Read the XML-file.
149-
*
150-
* @param string $file Path to the XML-file
151-
*
152-
* @return array All userinformation read from the file
153-
*/
154-
function parse_xml_data($file)
155-
{
156-
global $current_tag;
157-
global $current_value;
158-
global $skill;
159-
global $skills;
160-
$skills = [];
161-
$parser = xml_parser_create('UTF-8');
162-
xml_set_element_handler($parser, 'element_start', 'element_end');
163-
xml_set_character_data_handler($parser, 'character_data');
164-
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
165-
xml_parse($parser, api_utf8_encode_xml(file_get_contents($file)));
166-
xml_parser_free($parser);
167-
168-
return $skills;
169-
}
170-
171101
$this_section = SECTION_PLATFORM_ADMIN;
172102
api_protect_admin_script(true);
173103

@@ -183,7 +113,7 @@ function parse_xml_data($file)
183113
$file_type = $_POST['file_type'];
184114
Security::clear_token();
185115
$tok = Security::get_token();
186-
$allowed_file_mimetype = ['csv', 'xml'];
116+
$allowed_file_mimetype = ['csv'];
187117
$error_kind_file = false;
188118
$error_message = '';
189119

@@ -194,10 +124,6 @@ function parse_xml_data($file)
194124
$skills = parse_csv_data($_FILES['import_file']['tmp_name']);
195125
$errors = validate_data($skills);
196126
$error_kind_file = false;
197-
} elseif (strcmp($file_type, 'xml') === 0 && $ext_import_file == $allowed_file_mimetype[1]) {
198-
$skills = parse_xml_data($_FILES['import_file']['tmp_name']);
199-
$errors = validate_data($skills);
200-
$error_kind_file = false;
201127
} else {
202128
$error_kind_file = true;
203129
}
@@ -222,8 +148,6 @@ function parse_xml_data($file)
222148

223149
if (strcmp($file_type, 'csv') === 0) {
224150
save_data($skills_to_insert);
225-
} elseif (strcmp($file_type, 'xml') === 0) {
226-
save_data($skills_to_insert);
227151
} else {
228152
$error_message = get_lang('YouMustImportAFileAccordingToSelectedOption');
229153
}
@@ -272,7 +196,7 @@ function parse_xml_data($file)
272196
'radio',
273197
'file_type',
274198
'',
275-
'CSV (<a href="skill_example.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)',
199+
'CSV (<a href="skill_example.csv" target="_blank" download>'.get_lang('ExampleCSVFile').'</a>)',
276200
'csv'
277201
);
278202
$form->addGroup($group, '', get_lang('FileType'));
@@ -283,23 +207,6 @@ function parse_xml_data($file)
283207
$form->setDefaults($defaults);
284208
$form->display();
285209

286-
$list = [];
287-
$list_reponse = [];
288-
$result_xml = '';
289-
$i = 0;
290-
$count_fields = count($extra_fields);
291-
if ($count_fields > 0) {
292-
foreach ($extra_fields as $extra) {
293-
$list[] = $extra[1];
294-
$list_reponse[] = 'xxx';
295-
$spaces = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
296-
$result_xml .= $spaces.'&lt;'.$extra[1].'&gt;xxx&lt;/'.$extra[1].'&gt;';
297-
if ($i != $count_fields - 1) {
298-
$result_xml .= '<br/>';
299-
}
300-
$i++;
301-
}
302-
}
303210
?>
304211
<p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
305212

main/admin/user_import.php

Lines changed: 18 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -307,66 +307,6 @@ function parse_csv_data($file)
307307

308308
return $users;
309309
}
310-
/**
311-
* XML-parser: handle start of element.
312-
*
313-
* @param string $parser Deprecated?
314-
* @param string $data The data to be parsed
315-
*/
316-
function element_start($parser, $data)
317-
{
318-
$data = api_utf8_decode($data);
319-
global $user;
320-
global $current_tag;
321-
switch ($data) {
322-
case 'Contact':
323-
$user = [];
324-
break;
325-
default:
326-
$current_tag = $data;
327-
}
328-
}
329-
330-
/**
331-
* XML-parser: handle end of element.
332-
*
333-
* @param string $parser Deprecated?
334-
* @param string $data The data to be parsed
335-
*/
336-
function element_end($parser, $data)
337-
{
338-
$data = api_utf8_decode($data);
339-
global $user;
340-
global $users;
341-
global $current_value;
342-
switch ($data) {
343-
case 'Contact':
344-
if ($user['Status'] == '5') {
345-
$user['Status'] = STUDENT;
346-
}
347-
if ($user['Status'] == '1') {
348-
$user['Status'] = COURSEMANAGER;
349-
}
350-
$users[] = $user;
351-
break;
352-
default:
353-
$user[$data] = $current_value;
354-
break;
355-
}
356-
}
357-
358-
/**
359-
* XML-parser: handle character data.
360-
*
361-
* @param string $parser Parser (deprecated?)
362-
* @param string $data The data to be parsed
363-
*/
364-
function character_data($parser, $data)
365-
{
366-
$data = trim(api_utf8_decode($data));
367-
global $current_value;
368-
$current_value = $data;
369-
}
370310

371311
/**
372312
* Read the XML-file.
@@ -377,16 +317,23 @@ function character_data($parser, $data)
377317
*/
378318
function parse_xml_data($file)
379319
{
380-
global $users;
381-
$users = [];
382-
$parser = xml_parser_create('UTF-8');
383-
xml_set_element_handler($parser, 'element_start', 'element_end');
384-
xml_set_character_data_handler($parser, 'character_data');
385-
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
386-
xml_parse($parser, api_utf8_encode_xml(file_get_contents($file)));
387-
xml_parser_free($parser);
320+
$crawler = new \Symfony\Component\DomCrawler\Crawler();
321+
$crawler->addXmlContent(file_get_contents($file));
322+
$crawler = $crawler->filter('Contacts > Contact ');
323+
$array = [];
324+
foreach ($crawler as $domElement) {
325+
$row = [];
326+
foreach ($domElement->childNodes as $node) {
327+
if ($node->nodeName != '#text') {
328+
$row[$node->nodeName] = $node->nodeValue;
329+
}
330+
}
331+
if (!empty($row)) {
332+
$array[] = $row;
333+
}
334+
}
388335

389-
return $users;
336+
return $array;
390337
}
391338

392339
$this_section = SECTION_PLATFORM_ADMIN;
@@ -520,14 +467,14 @@ function parse_xml_data($file)
520467
'radio',
521468
'file_type',
522469
'',
523-
'CSV (<a href="example.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)',
470+
'CSV (<a href="example.csv" target="_blank" download>'.get_lang('ExampleCSVFile').'</a>)',
524471
'csv'
525472
),
526473
$form->createElement(
527474
'radio',
528475
'file_type',
529476
null,
530-
'XML (<a href="example.xml" target="_blank">'.get_lang('ExampleXMLFile').'</a>)',
477+
'XML (<a href="example.xml" target="_blank" download>'.get_lang('ExampleXMLFile').'</a>)',
531478
'xml'
532479
),
533480
];

main/admin/user_update_import.php

Lines changed: 16 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -254,86 +254,26 @@ function parse_csv_data($file)
254254

255255
return $users;
256256
}
257-
/**
258-
* XML-parser: handle start of element.
259-
*
260-
* @param string $parser Deprecated?
261-
* @param string $data The data to be parsed
262-
*/
263-
function element_start($parser, $data)
264-
{
265-
$data = api_utf8_decode($data);
266-
global $user;
267-
global $current_tag;
268-
switch ($data) {
269-
case 'Contact':
270-
$user = [];
271-
break;
272-
default:
273-
$current_tag = $data;
274-
}
275-
}
276257

277-
/**
278-
* XML-parser: handle end of element.
279-
*
280-
* @param string $parser Deprecated?
281-
* @param string $data The data to be parsed
282-
*/
283-
function element_end($parser, $data)
258+
function parse_xml_data($file)
284259
{
285-
$data = api_utf8_decode($data);
286-
global $user;
287-
global $users;
288-
global $current_value;
289-
switch ($data) {
290-
case 'Contact':
291-
if ($user['Status'] == '5') {
292-
$user['Status'] = STUDENT;
293-
}
294-
if ($user['Status'] == '1') {
295-
$user['Status'] = COURSEMANAGER;
260+
$crawler = new \Symfony\Component\DomCrawler\Crawler();
261+
$crawler->addXmlContent(file_get_contents($file));
262+
$crawler = $crawler->filter('Contacts > Contact ');
263+
$array = [];
264+
foreach ($crawler as $domElement) {
265+
$row = [];
266+
foreach ($domElement->childNodes as $node) {
267+
if ($node->nodeName != '#text') {
268+
$row[$node->nodeName] = $node->nodeValue;
296269
}
297-
$users[] = $user;
298-
break;
299-
default:
300-
$user[$data] = $current_value;
301-
break;
270+
}
271+
if (!empty($row)) {
272+
$array[] = $row;
273+
}
302274
}
303-
}
304275

305-
/**
306-
* XML-parser: handle character data.
307-
*
308-
* @param string $parser Parser (deprecated?)
309-
* @param string $data The data to be parsed
310-
*/
311-
function character_data($parser, $data)
312-
{
313-
$data = trim(api_utf8_decode($data));
314-
global $current_value;
315-
$current_value = $data;
316-
}
317-
318-
/**
319-
* Read the XML-file.
320-
*
321-
* @param string $file Path to the XML-file
322-
*
323-
* @return array All user information read from the file
324-
*/
325-
function parse_xml_data($file)
326-
{
327-
global $users;
328-
$users = [];
329-
$parser = xml_parser_create('UTF-8');
330-
xml_set_element_handler($parser, 'element_start', 'element_end');
331-
xml_set_character_data_handler($parser, 'character_data');
332-
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
333-
xml_parse($parser, api_utf8_encode_xml(file_get_contents($file)));
334-
xml_parser_free($parser);
335-
336-
return $users;
276+
return $array;
337277
}
338278

339279
$this_section = SECTION_PLATFORM_ADMIN;
@@ -429,7 +369,7 @@ function parse_xml_data($file)
429369
exit;
430370
}
431371
}
432-
Display :: display_header($tool_name);
372+
Display::display_header($tool_name);
433373

434374
if (!empty($error_message)) {
435375
echo Display::return_message($error_message, 'error');

main/admin/usergroup_import.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function save_data($classes)
100100
'radio',
101101
'file_type',
102102
'',
103-
'CSV (<a href="example_class.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)',
103+
'CSV (<a href="example_class.csv" target="_blank" download>'.get_lang('ExampleCSVFile').'</a>)',
104104
'csv'
105105
);
106106
$form->addGroup($group, '', get_lang('FileType'), null);

0 commit comments

Comments
 (0)