-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwritexml.php
More file actions
130 lines (90 loc) · 3.74 KB
/
writexml.php
File metadata and controls
130 lines (90 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Copyright 2011 Jon Gosier & Ahmed Maawy
* Coded by Ahmed Maawy
* Date: 10/23/11
* Time: 10:50 AM
*/
include_once("movablecite.php");
// Get first X number of strings
function get_first_strings($string, $num_strings) {
$strings = explode(" ", $string);
$result_string = $strings[0];
for($string_loop = 1; $string_loop < $num_strings; $string_loop ++) {
$result_string.=" ".$strings[$string_loop];
}
return $result_string;
}
// Get last X number of strings
function get_last_strings($string, $num_strings) {
$strings = explode(" ", $string);
$string_count = count($strings);
$result_string = $strings[$string_count - 1];
for($string_loop = ($string_count - 2); $string_loop > ($string_count - 1) - $num_strings; $string_loop --) {
$result_string = $strings[$string_loop]." ".$result_string;
}
return $result_string;
}
$xml = simplexml_load_file(CITATAIONS_FILE);
// Posted elements via HTTP
$post_array = array();
$num_objects = $_POST['num_objects'];
// An array list of elements to add
$addition_array = array();
// An array list of elements to update
$update_array = array();
for($my_array_loop = 0; $my_array_loop < $num_objects; $my_array_loop ++) {
$citation_element = new Citation_Element($_POST["cite_$my_array_loop"],
$_POST["url_$my_array_loop"],
$_POST["current_cite_$my_array_loop"]);
array_push($post_array, $citation_element);
}
// URL verification phase
$current_post_array_index = 0;
foreach($post_array as $citation_element) {
$website_content = file_get_contents($citation_element->url);
// Remove HTML tags from the content
$website_content = strip_tags($website_content);
// Find the first x and last x words from citation
$first_words = get_first_strings($citation_element->cite, DEFAULT_CITATAION_SEARCH_WORDS);
$last_words = get_last_strings($citation_element->cite, DEFAULT_CITATAION_SEARCH_WORDS);
$post_array[$current_post_array_index]->first_words = $first_words;
$post_array[$current_post_array_index]->last_words = $last_words;
$first_words_pos = 0;
$last_words_pos = 0;
do {
$first_words_pos = strrpos($website_content, $first_words, $first_words_pos);
$last_words_pos = strrpos($website_content, $last_words, $first_words_pos);
if($first_words_pos === false || $last_words_pos === false) {
break;
}
}
while($first_words_pos >= $last_words_pos);
$citation = substr($website_content, $first_words_pos, ($last_words_pos - $first_words_pos) + strlen($last_words));
if($first_words_pos === false || $last_words_pos === false) {
$post_array[$current_post_array_index]->current_cite = "";
$post_array[$current_post_array_index]->citation_found = false;
}
else if($citation_element->current_cite != $citation) {
$post_array[$current_post_array_index]->current_cite = $citation;
$post_array[$current_post_array_index]->modified = true;
$post_array[$current_post_array_index]->citation_found = true;
}
else {
$post_array[$current_post_array_index]->citation_found = true;
}
$current_post_array_index ++;
}
$xml_string = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><citations>";
foreach($post_array as $citation_element) {
$cite = $citation_element->cite;
$url = $citation_element->url;
$current_cite = $citation_element->current_cite;
$xml_string.="<citation><cite>$cite</cite><url>$url</url><currentcite>$current_cite</currentcite></citation>";
}
$xml_string.="</citations>";
file_put_contents(CITATAIONS_FILE, $xml_string);
$num_elements = count($post_array);
$json_output->num_elements = $num_elements;
$json_output->post_array = $post_array;
echo(json_encode($json_output));