comparison php/get_filtered_count.php @ 2224:760719986df3

Tidy up file locations.
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Thu, 14 Apr 2016 13:54:24 +0100
parents
children 169f08dc9634
comparison
equal deleted inserted replaced
2222:4d1aa94202e3 2224:760719986df3
1 <?php
2 //http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using-php
3 function rel2abs($rel, $base)
4 {
5 /* return if already absolute URL */
6 if (parse_url($rel, PHP_URL_SCHEME) != '' || substr($rel, 0, 2) == '//') return $rel;
7
8 /* queries and anchors */
9 if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
10
11 /* parse base URL and convert to local variables:
12 $scheme, $host, $path */
13 extract(parse_url($base));
14
15 /* remove non-directory element from path */
16 $path = preg_replace('#/[^/]*$#', '', $path);
17
18 /* destroy path if relative url points to root */
19 if ($rel[0] == '/') $path = '';
20
21 /* dirty absolute URL */
22 $abs = "$host$path/$rel";
23
24 /* replace '//' or '/./' or '/foo/../' with '/' */
25 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
26 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
27
28 /* absolute URL is ready! */
29 return $scheme.'://'.$abs;
30 }
31
32 /*
33 This looks for files that pass the filtering response
34 The filtering system uses key-value pairs
35 The key is double encoded using a '-'. The first part is the ID of the item to filter,
36 the second is the method:
37 min - Minimum Inclusive
38 max - Maximum Inclusive
39 exclude-# - exclude, followed by a number to uniquely add, (will create a triple [], ignore the third as random)
40 */
41 $keys = [];
42 $waet_url = null;
43 foreach ($_GET as $key => $value) {
44 $key = explode("-",$key);
45 if ($key[0] == "url") {
46 $waet_url = $value;
47 } else {
48 $v_pair = [$key[1],$value];
49 if(array_key_exists($key[0],$keys)) {
50 // We have some data
51 array_push($keys[$key[0]],$v_pair);
52 } else {
53 // Create new key data
54 $keys[$key[0]] = [$v_pair];
55 }
56 }
57 }
58
59 $files = [];
60 $saves = glob("../saves/*.xml");
61 if (is_array($saves))
62 {
63 foreach($saves as $filename) {
64 $xml_string = file_get_contents($filename, FILE_TEXT);
65 $xml_object = simplexml_load_string($xml_string);
66 if ($xml_object) {
67 // First we must check the URLs match
68 $waet = $xml_object->waet[0];
69 if (urldecode($waet["url"])==$waet_url) {
70 // It is part of the dataset, so now perform checks
71 $continue = true;
72 foreach($keys as $keyId => $keyArr) {
73 $elem = $xml_object->xpath("//*[@ref='".$keyId."']");
74 $elem = $elem[0]; // Can only be one.
75 switch ($elem["type"]) {
76 case "number":
77 // Number, we must check for min/max
78 $value = (real)$elem->response;
79 foreach ($keyArr as $keyCheck) {
80 if ($keyCheck[0] == 'min' && $value < $keyCheck[1]) {
81 $continue = false;
82 break;
83 } else if ($keyCheck[0] == 'max' && $value > $keyCheck[1]) {
84 $continue = false;
85 break;
86 }
87 }
88 break;
89 case "checkbox":
90 // Will have an array of <response>
91 foreach ($elem->response as $response) {
92 foreach ($keyArr as $keyCheck) {
93 if ($response["name"] == $keyCheck[1]) {
94 if($response["checked"] == "true" && $keyCheck[0] == "exclude") {
95 $continue = false;
96 break;
97 }
98 }
99 }
100 if($continue == false) {
101 break;
102 }
103 }
104 break;
105 case "radio":
106 foreach ($keyArr as $keyCheck) {
107 if ($keyCheck[0] == "exclude" && $elem->response["name"] == $keyCheck[1]) {
108
109 $continue = false;
110 break;
111 }
112 }
113 break;
114 default:
115 break;
116 }
117 if ($continue == false) {
118 break;
119 }
120 }
121 if ($continue) {
122 array_push($files,rel2abs($filename,"http://".$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']));
123 }
124 }
125 }
126 }
127 }
128 if (count($files) == 0) {
129 echo '{"urls": []}';
130 } else {
131 echo '{"urls": ["'.implode('","',$files).'"]}';
132 }
133
134 ?>