diff options
Diffstat (limited to 'web/query.php')
-rw-r--r-- | web/query.php | 593 |
1 files changed, 593 insertions, 0 deletions
diff --git a/web/query.php b/web/query.php new file mode 100644 index 0000000..13b701b --- /dev/null +++ b/web/query.php | |||
@@ -0,0 +1,593 @@ | |||
1 | <?php | ||
2 | //The description of the flags used in this file are being based on the | ||
3 | //DirFindFlags enum which is defined in OpenMetaverse/DirectoryManager.cs | ||
4 | //of the libopenmetaverse library. | ||
5 | |||
6 | include("../config/os_modules_mysql.php"); | ||
7 | |||
8 | $now = time(); | ||
9 | |||
10 | // | ||
11 | // Search DB | ||
12 | // | ||
13 | mysql_connect ($DB_HOST, $DB_USER, $DB_PASSWORD); | ||
14 | mysql_select_db ($DB_NAME); | ||
15 | |||
16 | # | ||
17 | # Copyright (c)Melanie Thielker (http://opensimulator.org/) | ||
18 | # | ||
19 | |||
20 | ###################### No user serviceable parts below ##################### | ||
21 | |||
22 | //Join a series of terms together with optional parentheses around the result. | ||
23 | //This function is used in place of the simpler join to handle the cases where | ||
24 | //one or more of the supplied terms are an empty string. The parentheses can | ||
25 | //be added when mixing AND and OR clauses in a SQL query. | ||
26 | function join_terms($glue, $terms, $add_paren) | ||
27 | { | ||
28 | if (count($terms) > 1) | ||
29 | { | ||
30 | $type = join($glue, $terms); | ||
31 | if ($add_paren == True) | ||
32 | $type = "(" . $type . ")"; | ||
33 | } | ||
34 | else | ||
35 | { | ||
36 | if (count($terms) == 1) | ||
37 | $type = $terms[0]; | ||
38 | else | ||
39 | $type = ""; | ||
40 | } | ||
41 | |||
42 | return $type; | ||
43 | } | ||
44 | |||
45 | |||
46 | function process_region_type_flags($flags) | ||
47 | { | ||
48 | $terms = array(); | ||
49 | |||
50 | if ($flags & 16777216) //IncludePG (1 << 24) | ||
51 | $terms[] = "mature = 'PG'"; | ||
52 | if ($flags & 33554432) //IncludeMature (1 << 25) | ||
53 | $terms[] = "mature = 'Mature'"; | ||
54 | if ($flags & 67108864) //IncludeAdult (1 << 26) | ||
55 | $terms[] = "mature = 'Adult'"; | ||
56 | |||
57 | return join_terms(" OR ", $terms, True); | ||
58 | } | ||
59 | |||
60 | |||
61 | # | ||
62 | # The XMLRPC server object | ||
63 | # | ||
64 | |||
65 | $xmlrpc_server = xmlrpc_server_create(); | ||
66 | |||
67 | # | ||
68 | # Places Query | ||
69 | # | ||
70 | |||
71 | xmlrpc_server_register_method($xmlrpc_server, "dir_places_query", | ||
72 | "dir_places_query"); | ||
73 | |||
74 | function dir_places_query($method_name, $params, $app_data) | ||
75 | { | ||
76 | $req = $params[0]; | ||
77 | |||
78 | $flags = $req['flags']; | ||
79 | $text = $req['text']; | ||
80 | $category = $req['category']; | ||
81 | $query_start = $req['query_start']; | ||
82 | |||
83 | $pieces = split(" ", $text); | ||
84 | $text = join("%", $pieces); | ||
85 | |||
86 | if ($text == "%%%") | ||
87 | { | ||
88 | $response_xml = xmlrpc_encode(array( | ||
89 | 'success' => False, | ||
90 | 'errorMessage' => "Invalid search terms" | ||
91 | )); | ||
92 | |||
93 | print $response_xml; | ||
94 | |||
95 | return; | ||
96 | } | ||
97 | |||
98 | $terms = array(); | ||
99 | |||
100 | $type = process_region_type_flags($flags); | ||
101 | if ($type != "") | ||
102 | $type = " AND " . $type; | ||
103 | |||
104 | if ($flags & 1024) | ||
105 | $order = "dwell DESC,"; | ||
106 | |||
107 | if ($category > 0) | ||
108 | $category = "searchcategory = '".mysql_real_escape_string($category)."' AND "; | ||
109 | else | ||
110 | $category = ""; | ||
111 | |||
112 | $text = mysql_real_escape_string($text); | ||
113 | $result = mysql_query("SELECT * FROM parcels WHERE $category " . | ||
114 | "(parcelname LIKE '%$text%'" . | ||
115 | " OR description LIKE '%$text%')" . | ||
116 | $type . " ORDER BY $order parcelname" . | ||
117 | " LIMIT ".(0+$query_start).",101"); | ||
118 | |||
119 | $data = array(); | ||
120 | while (($row = mysql_fetch_assoc($result))) | ||
121 | { | ||
122 | $data[] = array( | ||
123 | "parcel_id" => $row["infouuid"], | ||
124 | "name" => $row["parcelname"], | ||
125 | "for_sale" => "False", | ||
126 | "auction" => "False", | ||
127 | "dwell" => $row["dwell"]); | ||
128 | } | ||
129 | $response_xml = xmlrpc_encode(array( | ||
130 | 'success' => True, | ||
131 | 'errorMessage' => "", | ||
132 | 'data' => $data | ||
133 | )); | ||
134 | |||
135 | print $response_xml; | ||
136 | } | ||
137 | |||
138 | # | ||
139 | # Popular Places Query | ||
140 | # | ||
141 | |||
142 | xmlrpc_server_register_method($xmlrpc_server, "dir_popular_query", | ||
143 | "dir_popular_query"); | ||
144 | |||
145 | function dir_popular_query($method_name, $params, $app_data) | ||
146 | { | ||
147 | $req = $params[0]; | ||
148 | |||
149 | $text = $req['text']; | ||
150 | $flags = $req['flags']; | ||
151 | $query_start = $req['query_start']; | ||
152 | |||
153 | $terms = array(); | ||
154 | |||
155 | if ($flags & 0x1000) //PicturesOnly (1 << 12) | ||
156 | $terms[] = "has_picture = 1"; | ||
157 | |||
158 | if ($flags & 0x0800) //PgSimsOnly (1 << 11) | ||
159 | $terms[] = "mature = 0"; | ||
160 | |||
161 | if ($text != "") | ||
162 | { | ||
163 | $text = mysql_real_escape_string($text); | ||
164 | $terms[] = "(name LIKE '%$text%')"; | ||
165 | } | ||
166 | |||
167 | if (count($terms) > 0) | ||
168 | $where = " WHERE " . join_terms(" AND ", $terms, False); | ||
169 | else | ||
170 | $where = ""; | ||
171 | |||
172 | $result = mysql_query("SELECT * FROM popularplaces" . $where . | ||
173 | " LIMIT " . mysql_real_escape_string($query_start) . ",101"); | ||
174 | |||
175 | $data = array(); | ||
176 | while (($row = mysql_fetch_assoc($result))) | ||
177 | { | ||
178 | $data[] = array( | ||
179 | "parcel_id" => $row["infoUUID"], | ||
180 | "name" => $row["name"], | ||
181 | "dwell" => $row["dwell"]); | ||
182 | } | ||
183 | |||
184 | $response_xml = xmlrpc_encode(array( | ||
185 | 'success' => True, | ||
186 | 'errorMessage' => "", | ||
187 | 'data' => $data)); | ||
188 | |||
189 | print $response_xml; | ||
190 | } | ||
191 | |||
192 | # | ||
193 | # Land Query | ||
194 | # | ||
195 | |||
196 | xmlrpc_server_register_method($xmlrpc_server, "dir_land_query", | ||
197 | "dir_land_query"); | ||
198 | |||
199 | function dir_land_query($method_name, $params, $app_data) | ||
200 | { | ||
201 | $req = $params[0]; | ||
202 | |||
203 | $flags = $req['flags']; | ||
204 | $type = $req['type']; | ||
205 | $price = $req['price']; | ||
206 | $area = $req['area']; | ||
207 | $query_start = $req['query_start']; | ||
208 | |||
209 | $terms = array(); | ||
210 | |||
211 | if ($type != 4294967295) //Include all types of land? | ||
212 | { | ||
213 | //Do this check first so we can bail out quickly on Auction search | ||
214 | if (($type & 26) == 2) // Auction (from SearchTypeFlags enum) | ||
215 | { | ||
216 | $response_xml = xmlrpc_encode(array( | ||
217 | 'success' => False, | ||
218 | 'errorMessage' => "No auctions listed")); | ||
219 | |||
220 | print $response_xml; | ||
221 | |||
222 | return; | ||
223 | } | ||
224 | |||
225 | if (($type & 24) == 8) //Mainland (24=0x18 [bits 3 & 4]) | ||
226 | $terms[] = "parentestate = 1"; | ||
227 | if (($type & 24) == 16) //Estate (24=0x18 [bits 3 & 4]) | ||
228 | $terms[] = "parentestate <> 1"; | ||
229 | } | ||
230 | |||
231 | $s = process_region_type_flags($flags); | ||
232 | if ($s != "") | ||
233 | $terms[] = $s; | ||
234 | |||
235 | if ($flags & 0x100000) //LimitByPrice (1 << 20) | ||
236 | $terms[] = "saleprice <= '" . mysql_real_escape_string($price) . "'"; | ||
237 | if ($flags & 0x200000) //LimitByArea (1 << 21) | ||
238 | $terms[] = "area >= '" . mysql_real_escape_string($area) . "'"; | ||
239 | |||
240 | //The PerMeterSort flag is always passed from a map item query. | ||
241 | //It doesn't hurt to have this as the default search order. | ||
242 | $order = "lsq"; //PerMeterSort (1 << 17) | ||
243 | |||
244 | if ($flags & 0x80000) //NameSort (1 << 19) | ||
245 | $order = "parcelname"; | ||
246 | if ($flags & 0x10000) //PriceSort (1 << 16) | ||
247 | $order = "saleprice"; | ||
248 | if ($flags & 0x40000) //AreaSort (1 << 18) | ||
249 | $order = "area"; | ||
250 | if (!($flags & 0x8000)) //SortAsc (1 << 15) | ||
251 | $order .= " DESC"; | ||
252 | |||
253 | if (count($terms) > 0) | ||
254 | $where = " WHERE " . join_terms(" AND ", $terms, False); | ||
255 | else | ||
256 | $where = ""; | ||
257 | |||
258 | $sql = "SELECT *, saleprice/area AS lsq FROM parcelsales" . $where . | ||
259 | " ORDER BY " . $order . " LIMIT " . | ||
260 | mysql_real_escape_string($query_start) . ",101"; | ||
261 | |||
262 | $result = mysql_query($sql); | ||
263 | |||
264 | $data = array(); | ||
265 | while (($row = mysql_fetch_assoc($result))) | ||
266 | { | ||
267 | $data[] = array( | ||
268 | "parcel_id" => $row["infoUUID"], | ||
269 | "name" => $row["parcelname"], | ||
270 | "auction" => "false", | ||
271 | "for_sale" => "true", | ||
272 | "sale_price" => $row["saleprice"], | ||
273 | "landing_point" => $row["landingpoint"], | ||
274 | "region_UUID" => $row["regionUUID"], | ||
275 | "area" => $row["area"]); | ||
276 | } | ||
277 | |||
278 | $response_xml = xmlrpc_encode(array( | ||
279 | 'success' => True, | ||
280 | 'errorMessage' => "", | ||
281 | 'data' => $data)); | ||
282 | |||
283 | print $response_xml; | ||
284 | } | ||
285 | |||
286 | # | ||
287 | # Events Query | ||
288 | # | ||
289 | |||
290 | xmlrpc_server_register_method($xmlrpc_server, "dir_events_query", | ||
291 | "dir_events_query"); | ||
292 | |||
293 | function dir_events_query($method_name, $params, $app_data) | ||
294 | { | ||
295 | $req = $params[0]; | ||
296 | |||
297 | $text = $req['text']; | ||
298 | $flags = $req['flags']; | ||
299 | $query_start = $req['query_start']; | ||
300 | |||
301 | if ($text == "%%%") | ||
302 | { | ||
303 | $response_xml = xmlrpc_encode(array( | ||
304 | 'success' => False, | ||
305 | 'errorMessage' => "Invalid search terms" | ||
306 | )); | ||
307 | |||
308 | print $response_xml; | ||
309 | |||
310 | return; | ||
311 | } | ||
312 | |||
313 | $pieces = explode("|", $text); | ||
314 | |||
315 | $day = $pieces[0]; | ||
316 | $category = $pieces[1]; | ||
317 | if (count($pieces) < 3) | ||
318 | $search_text = ""; | ||
319 | else | ||
320 | $search_text = $pieces[2]; | ||
321 | |||
322 | //Get todays date/time and adjust it to UTC | ||
323 | $now = time() - date_offset_get(new DateTime); | ||
324 | |||
325 | $terms = array(); | ||
326 | |||
327 | if ($day == "u") | ||
328 | $terms[] = "dateUTC > ".$now; | ||
329 | else | ||
330 | { | ||
331 | //Is $day a number of days before or after current date? | ||
332 | if ($day != 0) | ||
333 | $now += $day * 86400; | ||
334 | $now -= ($now % 86400); | ||
335 | $then = $now + 86400; | ||
336 | $terms[] = "(dateUTC > ".$now." AND dateUTC <= ".$then.")"; | ||
337 | } | ||
338 | |||
339 | if ($category != 0) | ||
340 | $terms[] = "category = ".$category.""; | ||
341 | |||
342 | $type = array(); | ||
343 | if ($flags & 16777216) //IncludePG (1 << 24) | ||
344 | $type[] = "eventflags = 0"; | ||
345 | if ($flags & 33554432) //IncludeMature (1 << 25) | ||
346 | $type[] = "eventflags = 1"; | ||
347 | if ($flags & 67108864) //IncludeAdult (1 << 26) | ||
348 | $type[] = "eventflags = 2"; | ||
349 | |||
350 | //Was there at least one PG, Mature, or Adult flag? | ||
351 | if (count($type) > 0) | ||
352 | $terms[] = join_terms(" OR ", $type, True); | ||
353 | |||
354 | if ($search_text != "") | ||
355 | { | ||
356 | $search_text = mysql_real_escape_string($search_text); | ||
357 | $terms[] = "(name LIKE '%$search_text%' OR " . | ||
358 | "description LIKE '%$search_text%')"; | ||
359 | } | ||
360 | |||
361 | if (count($terms) > 0) | ||
362 | $where = " WHERE " . join_terms(" AND ", $terms, False); | ||
363 | else | ||
364 | $where = ""; | ||
365 | |||
366 | $sql = "SELECT * FROM events". $where. | ||
367 | " LIMIT " . mysql_real_escape_string($query_start) . ",101"; | ||
368 | |||
369 | $result = mysql_query($sql); | ||
370 | |||
371 | $data = array(); | ||
372 | |||
373 | while (($row = mysql_fetch_assoc($result))) | ||
374 | { | ||
375 | $date = strftime("%m/%d %I:%M %p",$row["dateUTC"]); | ||
376 | |||
377 | $data[] = array( | ||
378 | "owner_id" => $row["owneruuid"], | ||
379 | "name" => $row["name"], | ||
380 | "event_id" => $row["eventid"], | ||
381 | "date" => $date, | ||
382 | "unix_time" => $row["dateUTC"], | ||
383 | "event_flags" => $row["eventflags"], | ||
384 | "landing_point" => $row["globalPos"], | ||
385 | "region_UUID" => $row["simname"]); | ||
386 | } | ||
387 | |||
388 | $response_xml = xmlrpc_encode(array( | ||
389 | 'success' => True, | ||
390 | 'errorMessage' => "", | ||
391 | 'data' => $data)); | ||
392 | |||
393 | print $response_xml; | ||
394 | } | ||
395 | |||
396 | # | ||
397 | # Classifieds Query | ||
398 | # | ||
399 | |||
400 | xmlrpc_server_register_method($xmlrpc_server, "dir_classified_query", | ||
401 | "dir_classified_query"); | ||
402 | |||
403 | function dir_classified_query ($method_name, $params, $app_data) | ||
404 | { | ||
405 | $req = $params[0]; | ||
406 | |||
407 | $text = $req['text']; | ||
408 | $flags = $req['flags']; | ||
409 | $category = $req['category']; | ||
410 | $query_start = $req['query_start']; | ||
411 | |||
412 | if ($text == "%%%") | ||
413 | { | ||
414 | $response_xml = xmlrpc_encode(array( | ||
415 | 'success' => False, | ||
416 | 'errorMessage' => "Invalid search terms" | ||
417 | )); | ||
418 | |||
419 | print $response_xml; | ||
420 | |||
421 | return; | ||
422 | } | ||
423 | |||
424 | $terms = array(); | ||
425 | |||
426 | //Renew Weekly flag is bit 5 (32) in $flags. | ||
427 | $f = array(); | ||
428 | if ($flags & 4) //PG (1 << 2) | ||
429 | $f[] = "classifiedflags & 4 = 4"; | ||
430 | if ($flags & 8) //Mature (1 << 3) | ||
431 | $f[] = "classifiedflags & 8 = 8"; | ||
432 | if ($flags & 64) //Adult (1 << 6) | ||
433 | $f[] = "classifiedflags & 64 = 64"; | ||
434 | |||
435 | //Was there at least one PG, Mature, or Adult flag? | ||
436 | if (count($f) > 0) | ||
437 | $terms[] = join_terms(" OR ", $f, True); | ||
438 | |||
439 | //Only restrict results based on category if it is not 0 (Any Category) | ||
440 | if ($category != 0) | ||
441 | $terms[] = "category = " . $category; | ||
442 | |||
443 | if ($text != "") | ||
444 | $terms[] = "(name LIKE '%$text%'" . | ||
445 | " OR description LIKE '%$text%')"; | ||
446 | |||
447 | //Was there at least condition for the search? | ||
448 | if (count($terms) > 0) | ||
449 | $where = " WHERE " . join_terms(" AND ", $terms, False); | ||
450 | else | ||
451 | $where = ""; | ||
452 | |||
453 | $sql = "SELECT * FROM classifieds" . $where . | ||
454 | " ORDER BY priceforlisting DESC" . | ||
455 | " LIMIT " . mysql_real_escape_string($query_start) . ",101"; | ||
456 | |||
457 | $result = mysql_query($sql); | ||
458 | |||
459 | $data = array(); | ||
460 | while (($row = mysql_fetch_assoc($result))) | ||
461 | { | ||
462 | $data[] = array( | ||
463 | "classifiedid" => $row["classifieduuid"], | ||
464 | "name" => $row["name"], | ||
465 | "classifiedflags" => $row["classifiedflags"], | ||
466 | "creation_date" => $row["creationdate"], | ||
467 | "expiration_date" => $row["expirationdate"], | ||
468 | "priceforlisting" => $row["priceforlisting"]); | ||
469 | } | ||
470 | |||
471 | $response_xml = xmlrpc_encode(array( | ||
472 | 'success' => True, | ||
473 | 'errorMessage' => "", | ||
474 | 'data' => $data)); | ||
475 | |||
476 | print $response_xml; | ||
477 | } | ||
478 | |||
479 | # | ||
480 | # Events Info Query | ||
481 | # | ||
482 | |||
483 | xmlrpc_server_register_method($xmlrpc_server, "event_info_query", | ||
484 | "event_info_query"); | ||
485 | |||
486 | function event_info_query($method_name, $params, $app_data) | ||
487 | { | ||
488 | $req = $params[0]; | ||
489 | |||
490 | $eventID = $req['eventID']; | ||
491 | |||
492 | $sql = "SELECT * FROM events WHERE eventID = " . | ||
493 | mysql_real_escape_string($eventID); | ||
494 | |||
495 | $result = mysql_query($sql); | ||
496 | |||
497 | $data = array(); | ||
498 | while (($row = mysql_fetch_assoc($result))) | ||
499 | { | ||
500 | $date = strftime("%G-%m-%d %H:%M:%S",$row["dateUTC"]); | ||
501 | |||
502 | $category = "*Unspecified*"; | ||
503 | if ($row['category'] == 18) $category = "Discussion"; | ||
504 | if ($row['category'] == 19) $category = "Sports"; | ||
505 | if ($row['category'] == 20) $category = "Live Music"; | ||
506 | if ($row['category'] == 22) $category = "Commercial"; | ||
507 | if ($row['category'] == 23) $category = "Nightlife/Entertainment"; | ||
508 | if ($row['category'] == 24) $category = "Games/Contests"; | ||
509 | if ($row['category'] == 25) $category = "Pageants"; | ||
510 | if ($row['category'] == 26) $category = "Education"; | ||
511 | if ($row['category'] == 27) $category = "Arts and Culture"; | ||
512 | if ($row['category'] == 28) $category = "Charity/Support Groups"; | ||
513 | if ($row['category'] == 29) $category = "Miscellaneous"; | ||
514 | |||
515 | $data[] = array( | ||
516 | "event_id" => $row["eventid"], | ||
517 | "creator" => $row["creatoruuid"], | ||
518 | "name" => $row["name"], | ||
519 | "category" => $category, | ||
520 | "description" => $row["description"], | ||
521 | "date" => $date, | ||
522 | "dateUTC" => $row["dateUTC"], | ||
523 | "duration" => $row["duration"], | ||
524 | "covercharge" => $row["covercharge"], | ||
525 | "coveramount" => $row["coveramount"], | ||
526 | "simname" => $row["simname"], | ||
527 | "globalposition" => $row["globalPos"], | ||
528 | "eventflags" => $row["eventflags"]); | ||
529 | } | ||
530 | |||
531 | $response_xml = xmlrpc_encode(array( | ||
532 | 'success' => True, | ||
533 | 'errorMessage' => "", | ||
534 | 'data' => $data)); | ||
535 | |||
536 | print $response_xml; | ||
537 | } | ||
538 | |||
539 | # | ||
540 | # Classifieds Info Query | ||
541 | # | ||
542 | |||
543 | xmlrpc_server_register_method($xmlrpc_server, "classifieds_info_query", | ||
544 | "classifieds_info_query"); | ||
545 | |||
546 | function classifieds_info_query($method_name, $params, $app_data) | ||
547 | { | ||
548 | $req = $params[0]; | ||
549 | |||
550 | $classifiedID = $req['classifiedID']; | ||
551 | |||
552 | $sql = "SELECT * FROM classifieds WHERE classifieduuid = '" . | ||
553 | mysql_real_escape_string($classifiedID). "'"; | ||
554 | |||
555 | $result = mysql_query($sql); | ||
556 | |||
557 | $data = array(); | ||
558 | while (($row = mysql_fetch_assoc($result))) | ||
559 | { | ||
560 | $data[] = array( | ||
561 | "classifieduuid" => $row["classifieduuid"], | ||
562 | "creatoruuid" => $row["creatoruuid"], | ||
563 | "creationdate" => $row["creationdate"], | ||
564 | "expirationdate" => $row["expirationdate"], | ||
565 | "category" => $row["category"], | ||
566 | "name" => $row["name"], | ||
567 | "description" => $row["description"], | ||
568 | "parceluuid" => $row["parceluuid"], | ||
569 | "parentestate" => $row["parentestate"], | ||
570 | "snapshotuuid" => $row["snapshotuuid"], | ||
571 | "simname" => $row["simname"], | ||
572 | "posglobal" => $row["posglobal"], | ||
573 | "parcelname" => $row["parcelname"], | ||
574 | "classifiedflags" => $row["classifiedflags"], | ||
575 | "priceforlisting" => $row["priceforlisting"]); | ||
576 | } | ||
577 | |||
578 | $response_xml = xmlrpc_encode(array( | ||
579 | 'success' => True, | ||
580 | 'errorMessage' => "", | ||
581 | 'data' => $data)); | ||
582 | |||
583 | print $response_xml; | ||
584 | } | ||
585 | |||
586 | # | ||
587 | # Process the request | ||
588 | # | ||
589 | |||
590 | $request_xml = file_get_contents("php://input"); | ||
591 | xmlrpc_server_call_method($xmlrpc_server, $request_xml, ''); | ||
592 | xmlrpc_server_destroy($xmlrpc_server); | ||
593 | ?> | ||