forked from fasterit/phpicalendar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.php
359 lines (314 loc) · 10.8 KB
/
search.php
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
define('BASE','./');
$current_view = 'search';
require_once(BASE.'functions/ical_parser.php');
require_once(BASE.'functions/list_functions.php');
require_once(BASE.'functions/template.php');
header("Content-Type: text/html; charset=$phpiCal_config->charset");
$display_date = $lang['l_results'];
if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != '') {
$back_page = $_SERVER['HTTP_REFERER'];
} else {
$back_page = BASE.$phpiCal_config->default_view.'.php?cal='.$cal.'&getdate='.$getdate;
}
$query = '';
$search_valid = false;
if (isset($_GET['query']) && $_GET['query'] != '') {
$query = $_GET['query'];
$search_valid = true;
}
$search_box = '';
$search_box .=
'<div style="width: 160px;">'."\n".
'<form action="search.php" method="get">'."\n".
'<input type="hidden" name="cpath" value="'.$cpath.'">'."\n".
'<input type="hidden" name="cal" value="'.$cal.'">'."\n".
'<input type="hidden" name="getdate" value="'.$getdate.'">'."\n".
'<input type="text" style="width: 130px; float: left;" name="query" value="'.$query.'">'."\n".
'<input type="image" src="templates/'.$phpiCal_config->template.'/images/search.gif" border="0" style="border-bottom: 3px;" name="submit" value="Search">'."\n".
'</form>'."\n".
'</div>';
$search_started = getmicrotime();
if ($search_valid) {
$format_search_arr = format_search($query);
if (!$format_search_arr[0]) {
$formatted_search = '<b>'.$no_query_lang.'</b>';
} else {
$formatted_search = $format_search_arr[0];
}
if (isset($master_array) && is_array($master_array)) {
foreach($master_array as $date_key_tmp => $date_tmp) {
if (is_array($date_tmp)) {
foreach($date_tmp as $time_tmp) {
if (is_array($time_tmp)) {
foreach ($time_tmp as $uid_tmp => $event_tmp) {
if (is_array($event_tmp)) {
if (!isset($the_arr[$uid_tmp]) || isset($event_tmp['exception'])) {
#print_r($format_search_arr);
#print_r($event_tmp);
#echo "<br>this event:".$event_tmp['event_text']."<br>";
$results1 = false;
$results2 = false;
if (isset($event_tmp['event_text'])) {
$results1 = search_boolean($format_search_arr,$event_tmp['event_text']);
}
if (!$results1 && isset($event_tmp['description'])) {
$results2 = search_boolean($format_search_arr,$event_tmp['description']);
}
if ($results1 || $results2) {
$event_tmp['date'] = $date_key_tmp;
if (isset($event_tmp['recur'])) {
$event_tmp['recur'] = format_recur($event_tmp['recur']);
}
if (isset($the_arr[$uid_tmp])) {
$the_arr[$uid_tmp]['exceptions'][] = $event_tmp;
} else {
$the_arr[$uid_tmp] = $event_tmp;
}
}
}
}
}
}
}
}
}
}
} else {
$formatted_search = '<b>'.$lang['l_no_query'].'</b>';
}
$search_ended = getmicrotime();
$search_took = number_format(($search_ended-$search_started),3);
// takes a boolean search and formats it into an array
// use with sister function search_boolean()
function format_search($search_str) {
// init arrays
$and_arr = array();
$or_arr = array();
$not_arr = array();
$or_str_arr = array();
$search_str = strtolower($search_str);
if ($search_str == ' ') return array(false,$and_arr,$or_arr,$not_arr);
// clean up search string
$search_str = trim($search_str);
$search_str = str_replace(' and ', ' ', $search_str);
$search_str = str_replace(' - ', ' ', $search_str);
$search_str = preg_replace('[[:space:]]+',' ', $search_str);
$search_str = str_replace(' not ', ' -', $search_str);
// start out with an AND array of all the items
$and_arr = explode(' ', $search_str);
$count = count($and_arr);
$j = 0;
// build an OR array from the items in AND
for($i=0;$i<$count;$i++) {
if ($i != 0 && $and_arr[$i] == 'or') {
while ($and_arr[$i] == 'or') {
$or_arr[$j][] = $and_arr[$i-1];
array_splice($and_arr,$i-1,2);
$i--;
}
if (isset($and_arr[$i])) {
$or_arr[$j][] = $and_arr[$i];
array_splice($and_arr,$i,1);
}
$or_str_arr[$j] = implode('</b> OR <b>', $or_arr[$j]);
$j++;
}
}
// build a NOT array from the items in AND
foreach($and_arr as $key => $val) {
if (substr($val,0,1) == '-') {
$not_arr[] = substr($val,1);
array_splice($and_arr,$key,1);
} elseif(substr($val,0,1) == '+') {
$and_arr[] = substr($val,1);
array_splice($and_arr,$key,1);
}
}
// prepare our formatted search string
if (count($and_arr) > 1) {
$final_str_arr[] = implode('</b> AND <b>', $and_arr);
} elseif (isset($and_arr[0]) && $and_arr[0] != '') {
$final_str_arr[] = $and_arr[0];
}
if (count($or_str_arr) > 1) {
$final_str_arr[] = implode('</b> AND <b>', $or_str_arr);
} elseif (isset($or_str_arr[0]) && $or_str_arr[0] != '') {
$final_str_arr[] = $or_str_arr[0];
}
if (count($not_arr) > 1) {
$final_str_arr[] = '-'.implode('</b> AND <b>-', $not_arr);
} elseif (isset($not_arr[0]) && $not_arr[0] != '') {
$final_str_arr[] = '-'.$not_arr[0];
}
if (count($final_str_arr) > 1) {
$formatted_search = '<b>'.implode('</b> AND <b>', $final_str_arr).'</b>';
} else {
$formatted_search = '<b>'.$final_str_arr[0].'</b>';
}
return array($formatted_search, $and_arr, $or_arr, $not_arr);
}
// takes an array made by format_search() and checks to see if it
// it matches against a string
function search_boolean($needle_arr, $haystack) {
// init arrays
$and_arr = $needle_arr[1];
$or_arr = $needle_arr[2];
$not_arr = $needle_arr[3];
if (!$needle_arr[0]) return false;
if ((sizeof($and_arr) == 0) &&
(sizeof($or_arr) == 0) &&
(sizeof($not_arr) == 0)) return false;
// compare lowercase versions of the strings
$haystack = strtolower($haystack);
// check against the NOT
foreach($not_arr as $s) {
if (is_string(strstr($haystack,$s)) == true) {
return false;
}
}
// check against the AND
foreach($and_arr as $s) {
#echo "haystack: $haystack<br>needle: $s<br>";
if (is_string(strstr($haystack,$s)) == false) {
return false;
}
}
// check against the OR
foreach($or_arr as $or) {
$is_false = true;
foreach($or as $s) {
if (substr($s,0,1) == '-') {
if (is_string(strstr($haystack,substr($s,1))) == false) {
$is_false = false;
break;
}
} elseif (is_string(strstr($haystack,$s)) == true) {
$is_false = false;
break;
}
}
if ($is_false) return false;
}
// if we haven't returned false, then we return true
# echo "return true<br>";
return true;
}
function format_recur($arr) {
global $format_recur_lang, $monthsofyear_lang, $daysofweek_lang;
$d = $format_recur_lang['delimiter'];
$int = '';
$freq = '';
if (isset($arr['INTERVAL']) && isset($arr['FREQ'])) {
$int = $arr['INTERVAL'];
$tmp = (($int == '1') ? 0 : 1);
$freq = $arr['FREQ'];
$freq = $format_recur_lang[$freq][$tmp];
}
if (isset($arr['COUNT'])) $for = str_replace('%int%',$arr['COUNT'],$format_recur_lang['count']);
elseif (isset($arr['UNTIL'])) $for = str_replace('%date%',$arr['UNTIL'], $format_recur_lang['until']);
else $for = '';
$print = $format_recur_lang['start'];
$print = str_replace('%int%', $int, $print);
$print = str_replace('%freq%', $freq, $print);
$print = str_replace('%for%', $for, $print);
if (isset($arr['BYMONTH'])) {
$list = '';
$last = count($arr['BYMONTH']) - 1;
foreach ($arr['BYMONTH'] as $key => $month) {
if ($key == $last) $list .= $monthsofyear_lang[($month-1)];
else $list .= $monthsofyear_lang[($month-1)].$d;
}
$print .= '<br />'."\n";
$print .= str_replace('%list%', $list, $format_recur_lang['bymonth']);
}
if (isset($arr['BYMONTHDAY'])) {
$list = '';
if ($arr['BYMONTHDAY'][(count($arr['BYMONTHDAY']) - 1)] == '0') unset($arr['BYMONTHDAY'][$last]);
$last = count($arr['BYMONTHDAY']) - 1;
foreach ($arr['BYMONTHDAY'] as $key => $day) {
preg_match('/(-{0,1})([0-9]{1,2})/',$day,$regs);
list($junk,$sign,$day) = $regs;
if ($sign != '') $list .= $sign;
if ($key == $last) $list .= $day;
else $list .= $day.$d;
}
$print .= '<br />'."\n";
$print .= str_replace('%list%', $list, $format_recur_lang['bymonthday']);
}
if (isset($arr['BYDAY'])) {
$list = '';
$last = count($arr['BYDAY']) - 1;
foreach ($arr['BYDAY'] as $key => $day) {
preg_match('/([-\+]{0,1})([0-9]{0,1})([A-Z]{2})/',$day,$regs);
list($junk,$sign,$day_num,$day_txt) = $regs;
$num = two2threeCharDays($day_txt,false);
if ($sign != '') $list .= $sign;
if ($day_num != '') $list .= $day_num.' ';
if ($key == $last) $list .= $daysofweek_lang[$num];
else $list .= $daysofweek_lang[$num].$d;
}
$print .= '<br />'."\n";
$print .= str_replace('%list%', $list, $format_recur_lang['byday']);
}
return $print;
}
$page = new Page(BASE.'templates/'.$phpiCal_config->template.'/search.tpl');
$page->draw_search($page);
$page->replace_files(array(
'header' => BASE.'templates/'.$phpiCal_config->template.'/header.tpl',
'footer' => BASE.'templates/'.$phpiCal_config->template.'/footer.tpl',
'sidebar' => BASE.'templates/'.$phpiCal_config->template.'/sidebar.tpl',
'event_js' => BASE.'functions/event.js',
));
$page->replace_tags(array(
'version' => $phpiCal_config->phpicalendar_version,
'default_path' => $phpiCal_config->default_path,
'formatted_search' => $formatted_search,
'l_results' => $lang['l_results'],
'l_query' => $lang['l_query'],
'l_starting_on' => $lang['l_starting_on'],
'l_time' => $lang['l_time'],
'l_summary' => $lang['l_summary'],
'l_location' => $lang['l_location'],
'l_description' => $lang['l_description'],
'l_recurring_event' => $lang['l_recurring_event'],
'l_exception' => $lang['l_exception'],
'l_no_results' => $lang['l_no_results'],
'search_box' => $search_box,
'charset' => $phpiCal_config->charset,
'template' => $phpiCal_config->template,
'cal' => $cal,
'getdate' => $getdate,
'cpath' => $cpath,
'calendar_name' => $cal_displayname,
'display_date' => $display_date,
'rss_powered' => $rss_powered,
'rss_available' => '',
'rss_valid' => '',
'show_goto' => '',
'is_logged_in' => '',
'current_view' => $current_view,
'style_select' => '',
'l_goprint' => $lang['l_goprint'],
'l_preferences' => $lang['l_preferences'],
'l_calendar' => $lang['l_calendar'],
'l_legend' => $lang['l_legend'],
'l_tomorrows' => $lang['l_tomorrows'],
'l_jump' => $lang['l_jump'],
'l_todo' => $lang['l_todo'],
'l_day' => $lang['l_day'],
'l_week' => $lang['l_week'],
'l_month' => $lang['l_month'],
'l_year' => $lang['l_year'],
'l_subscribe' => $lang['l_subscribe'],
'l_download' => $lang['l_download'],
'l_this_months' => $lang['l_this_months'],
'l_powered_by' => $lang['l_powered_by'],
'l_this_site_is' => $lang['l_this_site_is']
));
$page->output();
#echo "<pre>";
#print_r($the_arr);
#echo "</pre>";
?>