/
S3QLgoodies.php
305 lines (232 loc) · 7.85 KB
/
S3QLgoodies.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
<?php
#Use this script to make a call on S3DB with a simple structured array:
#Helena F Deus (helenadeus@gmail.com), Aug 13 2007
#This script is distributed under the GNU general public license (http://www.gnu.org/copyleft/gpl.html)
//$s3ql = array('url'=>'http://ibl.mdanderson.org/s3dbdemo3','key'=>'6OhmdK6M5KNVScq','select'=>'*','from'=>'projects','format'=>'php');
//$data = S3DBcall($s3ql);
//var_dump($data);
ini_set('memory_limit','300M');
set_time_limit(30000);
function S3DBcall($s3ql)
{#S3DBcall takes in an array formatted in S3QL terms (for exmaple, $s3ql = array('url','http://ibl.mdanderson.org/s3db','key','xxxxxx','select','*','from','projects');)
#and return the output from s3db parsed in an array
if($s3ql['format']!='php') $s3ql['format']='php';
$url = S3QLquery($s3ql);
#echo $url;
$h = fopen($url,'r');
while(!$h){
$h = fopen($url,'r');
}
$data = stream_get_contents($h);
return (array($url,unserialize($data)));
}
function S3QLquery($s3ql)
{
#Function S3QLquery builds the S3QL query for any remote uri
#INPUT: $s3ql is an array with at least key
#OUTPUT: a string, containing the URI with the information on the input element UID
#Helena F Deus
if($s3ql['url']=='')
if ($_SERVER['HTTP_X_FORWARDED_HOST'] != '')
$s3ql['url'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
else $s3ql['url'] = $_SERVER['HTTP_HOST'];
$wrap .= $s3ql['url'].'/S3QL.php';
if ($s3ql['user_id']!='') {
$wrap .= '?user_id='.$s3ql['user_id'];
}
if ($s3ql['format']!='') {
$wrap .= (($s3ql['user_id']!='')?'&':'?').'format='.$s3ql['format'];
}
$wrap .= (($s3ql['user_id']=='' && $s3ql['format']=='')?'?':'&').'query=<S3QL>';
$wrap .= '<key>'.$s3ql['key'].'</key>';
#remove the elements already used to build the query, keep the rest
$s3ql = array_diff_key($s3ql, array('url'=>'', 'key'=>'', 'db'=>'','user_id'=>'','format'=>''));
foreach($s3ql as $field=>$value)
{
if(!is_array($s3ql[$field])) #if is not an array, just build the simple xml
$wrap .= '<'.$field.'>'.$s3ql[$field].'</'.$field.'>';
else #for arrays, build the nested xml
{$wrap .= '<'.$field.'>';
foreach($value as $subfield=>$subvalue)
{
$wrap .= '<'.$subfield.'>'.$subvalue.'</'.$subfield.'>';
}
$wrap .= '</'.$field.'>';
}
}
$wrap .= '</S3QL>';
return $wrap;
}
function html2cell($html)
{
#Parse what's inside the table in $html into a nice PHP array
#Helena F Deus
if (eregi('<TABLE>(.*)</TABLE>', $html, $table_contents)) {
#parse teh lines
if (eregi('<TR>(.*)</TR>',$table_contents[1], $row_contents)) {
#$row = explode('<TR>', $row_contents[0]);
$row = spliti('<TR>', $row_contents[0]);
$row = array_filter($row);#explode has this annoying habit of adding empty values
foreach ($row as $rowi=>$a_row) {
#remove </tr>
$a_row = str_ireplace('</TR>', '', $a_row);
#parse teh cells
if (eregi('<TD>(.*)</TD>', $a_row, $cell_contents)) {
$cells[$rowi] = spliti('<TD>', $cell_contents[1]);
#remove emptyes
$cells[$rowi] = array_filter($cells[$rowi]);
#remove the /td
$cells[$rowi] = array_filter($cells[$rowi]);
foreach ($cells[$rowi] as $col=>$a_cell) {
$a_cell = str_ireplace('</TD>', '', $a_cell);
$cells[$rowi][$col] = $a_cell;
if ($rowi>1) {
$cells[$rowi][trim($cells[1][$col])] = $a_cell;
}
}
}
else {
return ('No cells');
}
}
}
else {
return ('No rows');
}
}
else {
'No html tables found';
}
return ($cells);
}
function move2s3db($file, $url,$key,$rule_id,$item_id)
{
#move2s3db reads a file, breaks it in pieces, sends teh individual pieces to S3DB and insert the file on S3DB through a statement.
$s3ql=compact('url','key');
$time=time();
$size = filesize($file);
$name= basename($file);
#now let s3db now that a file is coming
$url = $s3ql['url'].'/uploads.php?key='.$s3ql['key'].'&filename='.$name.'&filesize='.$size.'&format=php';
$now=date('His');
#Try open for 3 sec
while (!$c && date('His')-$now<3) {
$c = @stream_get_contents(@fopen($url,'r'));
}
$filekey=unserialize($c);
$filekey = $filekey[0]['filekey'];
$a = @fopen($file,'r');
$Mb = 5000;
#$Mb=200;
$f=0;
$fraNr=1;
$fragTotal = ceil($size/$Mb);
while ($f<$size) {
#open and read the file up to 1Mb
$frag = urlencode(base64_encode(fread($a, $Mb)));
$url = $s3ql['url'].'/uploads.php?filekey='.$filekey.'&fragNr='.$fraNr.'/'.$fragTotal.'&encode=2&fileStr='.$frag.'&format=php';
$now=date('His');
unset($d);
while (!$d && date('His')-$now<3) {
$d = @stream_get_contents(@fopen($url,'r'));
}
$success= unserialize($d);
#echo $url;
#echo '<pre>';print_r($success);
if($success[0]['error_code']=='0'){
$f = $f+$Mb;
$fraNr++;
}
else {
echo "Could not upload file ".$filename.". It may be corrupted or non-accessible";
exit;
}
}
#Now insert a statement
$s3ql['insert']='file';
$s3ql['where']['rule_id']=$rule_id;
$s3ql['where']['item_id']=$item_id;
$s3ql['where']['filekey']=$filekey;
list($url, $data)=S3DBcall($s3ql);
#echo '<pre>';print_r($data);
return ($data);
}
function findCollections($url,$key, $project_id)
{
$s3ql=compact('url','key');
$s3ql['select']='*';
$s3ql['from']='collections';
$s3ql['where']['project_id']=$project_id;
list($url, $collections)=S3DBCall($s3ql);
foreach ($collections as $collections_info) {
$collection_names[] = strtolower($collections_info['name']);
$collection_ids[] = $collections_info['collection_id'];
}
return (array($collections, $collection_names, $collection_ids));
}
function findRules($url,$key, $project_id)
{
$s3ql=compact('url','key');
$s3ql['select']='*';
$s3ql['from']='rules';
$s3ql['where']['project_id']=$project_id;
list($q, $rules) = S3DBCall($s3ql);
#Now make a list of rule subject, verbs, object and rule_ids
foreach ($rules as $rule_info) {
$ruleSubs[] = strtolower($rule_info['subject']);
$ruleVerbs[] = strtolower($rule_info['verb']);
$ruleObjs[] = strtolower($rule_info['object']);
$ruleIds[] = $rule_info['rule_id'];
}
return(array($rules, $ruleSubs, $ruleVerbs, $ruleObjs, $ruleIds));
}
function findRulesBySubject($subject, $rules)
{
foreach ($rules as $rule_info) {
if($rule_info['subject_id']==$subject){
$atbRule[] =$rule_info['rule_id'];
$atbVerb[] =strtolower($rule_info['verb']);
$atbObj[] =strtolower($rule_info['object']);
}
}
return (array($atbRule, $atbVerb, $atbObj));
}
function insertItem($url, $key, $collection_id, $notes, $repeat=False)
{
##First let's check if item with these notes already exists
if(!$repeat){
$s3ql=compact('url','key');
$s3ql['format']='php';
$s3ql['from']='items';
$s3ql['where']['notes']=$notes;
$s3ql['where']['collection_id']=$collection_id;
list($u, $item_info) = S3DBcall($s3ql);
if($item_info[0]['item_id']!='')
$Item_id = $item_info[0]['item_id'];
}
if($Item_id ==''){
$s3ql=compact('url','key');
$s3ql['format']='php';
$s3ql['insert']='item';
$s3ql['where']['notes']=$notes;
$s3ql['where']['collection_id']=$collection_id;
list($u, $Inserted) = S3DBcall($s3ql);
$Item_id = $Inserted[0]['item_id'];
}
return ($Item_id);
}
function insertStatement($url, $key, $item_id, $rule_id, $value)
{
##First let's check if item with these notes already exists
$s3ql=compact('url','key');
$s3ql['format']='php';
$s3ql['insert']='statement';
$s3ql['where']['item_id']=$item_id;
$s3ql['where']['rule_id']=$rule_id;
$s3ql['where']['value']=$value;
list($u, $inserted) = S3DBcall($s3ql);
if( $inserted[0]['statement_id']!='')
$statement_id= $inserted[0]['statement_id'];
return (true);
}
?>