-
Notifications
You must be signed in to change notification settings - Fork 6
/
YaCyAPI4.php
717 lines (583 loc) · 20.3 KB
/
YaCyAPI4.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
<?php
/**
* YAPP - YaCy PHP API class and xml/json Proxy
* Version 4
*
* Copyright (c) 2010 yacy.net / dulcedo
* URL: http://yacymin.walamt.de
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*/
class YaCyAPI {
private $_queryTerms; # The input to search
private $_appID; # Application ID
# Optional queries
public $_version = "0.4a";
public $_market = "en-us";
public $_sources; # The SourceType of the query / yet obsolete
public $_options; # An array containing optional parameters
public $_format; # What document will it be output into? XML, jSON
public $_debugQuery;
private $_holder;
protected $_curlArray;
//############# public API functions ismael ##########################
//
public function addJob() {
}
public function errstr() {
}
public function getProperties() {
$peername="http://".$this->PeerURL.":".$this->PeerPort."/";
$command="Network.xml";
$appid=$this->PeerAppid;
$this->_dur0=microtime(true); #for measuerement
$xml = $this->peerCommandDirect($peername,$command,$appid); #credentials...
$this->_dur2=microtime(true)-$this->_dur0; #time for command
//parse xml...
$resultarray=$this->xml2array2($xml); #, $get_attributes = 1, $priority = 'tag');
$items=$resultarray['peers']['your'];
return $items;
}
public function getPropertie() {
}
# php reserved
#public function new() {
#}
public function setProperties($url,$appid,$name,$timout=2000){
$u=explode(":",$url);
$this->PeerURL=$u[0];
$this->PeerPort=$u[1];
$this->PeerName=$name;
$this->PeerAppid=$appid;
$this->PeerTimeout=$timout;
$this->format= "rss";
$this->startRecord= "0";
$this->maximumRecords= "20";
$this->verify= "true";
$this->contentdom= "text";
$this->prefermaskfilter= "";
$this->urlmaskfilter= ".*";
$this->resource= "global";
$this->Lr= "";
}
public function ping() {
$to=0.5;
ini_set("default_socket_timeout",$to); #for web / local timeout in fsockopen
$peerinfo['host']=$this->PeerURL;
$peerinfo['port']=$this->PeerPort;
$peerinfo['appid']=$this->PeerAppid;
$peerinfo['name']=$this->PeerName;
$this->_dur0=microtime(true); #for measuerement
$fp = @fsockopen($this->PeerURL, $this->PeerPort, $errno, $errstr, $to);
$this->_dur1=microtime(true)-$this->_dur0; #time for select
$peerinfo['ping']=$this->_dur1;
if (!$fp)
{
#echo $YaCyURL." offline! (".$errno."-".$errstr.")";
$peerinfo['status']="0";
}
else
{
$peerinfo['status']="1";
}
return $peerinfo;
}
public function pauseCrawling() {
$peername="http://".$this->PeerURL.":".$this->PeerPort."/";
$command="Status.html?pauseCrawlJob=&jobType=localCrawl";
$appid=$this->PeerAppid;
$this->_dur0=microtime(true); #for measuerement
$xml = $this->peerCommandDirect($peername,$command,$appid);
$this->_dur2=microtime(true)-$this->_dur0; #time for command
}
public function resumeCrawling() {
$peername="http://".$this->PeerURL.":".$this->PeerPort."/";
$command="Status.html?resumeCrawlJob=&jobType=localCrawl";
$appid=$this->PeerAppid;
$this->_dur0=microtime(true); #for measuerement
$xml = $this->peerCommandDirect($peername,$command,$appid);
$this->_dur2=microtime(true)-$this->_dur0; #time for command
}
public function setTransferProperties() {
#indexDistribute=>on/off, indexDistributeWhileCrawling=>on/off, indexReceive=>on/off, indexReceiveBlockBlacklist=>on/off
}
public function stop() {
$peername="http://".$this->PeerURL.":".$this->PeerPort."/";
$command="Steering.html?shutdown=";
$appid=$this->PeerAppid;
$this->_dur0=microtime(true); #for measuerement
$xml = $this->peerCommandDirect($peername,$command,$appid);
$this->_dur2=microtime(true)-$this->_dur0; #time for command
}
### search specific
#########################################
public function setFormat($format) {
$this->format= "rss";
if ($format=="HTML" || $format=="html") {$this->format="html";}
if ($format=="JSON" || $format=="json") {$this->format="json";}
}
public function setStartRecord($n)
{
if ($n<0 || !$n) {$n="0";}
$this->startRecord= $n;
}
public function setMaximumRecords($n)
{
if ($n<0 || !$n) {$n="0";}
$this->maximumRecords= $n;
}
public function setVerify($n)
{
$this->verify= $n;
}
public function setSources($n)
{
$this->contentDom= $n;
}
public function setContentDom($n)
{
$this->contentDom= $n;
}
public function setResource($n)
{
$this->resource= $n;
}
public function setPrefermaskfilter($n)
{
$this->prefermaskfilter= $n;
}
public function setURLmaskfilter($n)
{
$this->urlmaskfilter= $n;
}
public function setLr($n)
{
$this->Lr= $n;
}
#--------------------------------------------------------------------------------
#get results
#search
#see below
public function getMeta(){
}
public function getTitle(){
}
public function getLink(){
}
#########################################
#### ---- extended ---------------------------------------------------------
public function findFirstPeer($frompeer)
{
include 'peerlist_inc.php';
$maxpeers=count($this_YaCyPeer); #local inst
$peerno=0;
while ($peerno<$maxpeers)
{
$peer=$this_YaCyPeer[$peerno][0];
$port=$this_YaCyPeer[$peerno][1];
$appid=$this_YaCyPeer[$peerno][2];
$name=$this_YaCyPeer[$peerno][3];
$peername="http://".$peer.":".$port."/";
$res=$this->setProperties($peer.":".$port,$appid,$name);
$info=$this->ping();
if ($info['status']) #port online?
{
#$res=$this->getStatus();
$command="Network.xml";
$res = $this->peerCommandDirect($peername,$command,$appid);
if ($res)
{
$result['peer']=$peer;
$result['port']=$port;
$result['appid']=$appid;
# echo "<br>found:".$peerno; print_r($info);
return $result;
$peerno=$maxpeers;
}
else
{
# echo "<br>not answering:".$peerno; print_r($info);
}
# exit;
}
else
{
# echo "<br>not found:".$peerno; print_r($info);
# exit;
}
$peerno++;
} #endwhile
}
public function crawlInfo(){
$peername="http://".$this->PeerURL.":".$this->PeerPort."/";
$command="CrawlProfileEditor_p.xml";
$appid=$this->PeerAppid;
$this->_dur0=microtime(true); #for measuerement
$xml = $this->peerCommandDirect($peername,$command,$appid); #credentials...
$this->_dur3=microtime(true)-$this->_dur0; #time for command
//parse xml...
$resultarray=xml2array($xml); #, $get_attributes = 1, $priority = 'tag');
$items=$resultarray['crawlProfiles']['crawlProfile'];
return $items;
}
public function getStats($n) {
$peername="http://".$this->PeerURL.":".$this->PeerPort."/";
$command="AccessTracker_p.xml?page=".$n;
$appid=$this->PeerAppid;
#echo $command;
$this->_dur0=microtime(true); #for measuerement
$xml = $this->peerCommandDirect($peername,$command,$appid); #credentials...
$this->_dur2=microtime(true)-$this->_dur0; #time for command
//parse xml...
$resultarray=$this->xml2array2($xml); #, $get_attributes = 1, $priority = 'tag');
$items=$resultarray['AccessTracker']; #['serverAccessDetails']['entry'];
return $items;
}
public function getStatus() {
$peername="http://".$this->PeerURL.":".$this->PeerPort."/";
$command="api/status_p.xml";
$appid=$this->PeerAppid;
$this->_dur0=microtime(true); #for measuerement
$xml = $this->peerCommandDirect($peername,$command,$appid); #credentials...
$this->_dur3=microtime(true)-$this->_dur0; #time for command
#print_r($xml);
# exit;
if($xml)
{
//parse xml...
$resultarray=$this->xml2array2($xml); #, $get_attributes = 1, $priority = 'tag');
$items=$resultarray['status'];
return $items;
}
else
{
#not impl.
$items="";
return $items;
}
}
public function getYconf($key) {
$peername="http://".$this->PeerURL.":".$this->PeerPort."/";
$command="api/config_p.xml";
$appid=$this->PeerAppid;
$this->_dur0=microtime(true); #for measuerement
$xml = $this->peerCommandDirect($peername,$command,$appid); #credentials...
$this->_dur2=microtime(true)-$this->_dur0; #time for command
//parse xml...
$resultarray=$this->xml2array2($xml); #, $get_attributes = 1, $priority = 'tag');
$itemarray=$resultarray['settings']['option'];
foreach ($itemarray as $item)
{
if ($item['key']==$key)
{
$value=$item['value'];
}
}
return $value;
}
#------------------------------
# returns search in simple array
public function search($s)
{
$xml=$this->getResults($s);
$resultarray=$this->xml2array2($xml);
$items=$resultarray['rss']['channel']['item'];
#print_r($xml);
#exit;
if ($items)
{
#print_r($items);
#echo "###";
# map and store unique result-set
foreach ($items as $item)
{
$l=$item['link'];
#$exists=array_search($l,$links);
#if (!$exists)
{
$lfd++;
#$links[$lfd]=$item['link']; # for dupes
$allres[$lfd]['link']=$item['link'];
$allres[$lfd]['title']=$item['title'];
$allres[$lfd]['description']=$item['description'];
$allres[$lfd]['date']=$item['pubDate'];
$allres[$lfd]['host']=$item['yacy:host'];
$allres[$lfd]['file']=$item['yacy:file'];
$allres[$lfd]['size']=$item['yacy:size'].$item['yacy:sizename'];
$allres[$lfd]['favicon']="http://".$item['yacy:host']."/favicon.ico";
$cp = curl_init( $allres[$lfd]['favicon'] );
curl_setopt($cp,CURLOPT_TIMEOUT,10);
curl_setopt($cp,CURLOPT_FAILONERROR,1);
curl_setopt($cp,CURLOPT_RETURNTRANSFER,1);
curl_exec($cp);
if (curl_errno($cp) != 0)
{
$allres[$lfd]['favicon']="images/doc.png";
}
else
{
$webserver[$key]['status'] = true;
}
curl_close($cp);
}
}
} else
{
#echo "no results";
}
return $allres;
}
#------------------------------
// returns xml or json results from search query
//
public function getResults($this_queryTerms)
{
$peername="http://".$this->PeerURL.":".$this->PeerPort."/";
$command="Network.xml";
$appid=$this->PeerAppid;
try {
if(empty($this->PeerURL)) { throw new Exception('Please select at least one peer'); }#
} catch(Exception $e) {
echo "An error has been detected: ".$e->getMessage();
die(); # Stop the execution
}
$this_queryTerms="?query=".$this_queryTerms;
$this_queryTerms=$this_queryTerms."&verify=".$this->verify; #true
$this_queryTerms=$this_queryTerms."&contentdom=".$this->contentdom; #text
$this_queryTerms=$this_queryTerms."&maximumRecords=".$this->maximumRecords; #25
$this_queryTerms=$this_queryTerms."&startRecord=".$this->startRecord; #0
$this_queryTerms=$this_queryTerms."&resource=".$this->resource; #global";
$this_queryTerms=$this_queryTerms."&nav=all"; # .$this->nav;
$this_queryTerms=$this_queryTerms."&urlmaskfilter=".$this->urlmaskfilter; #.*";
$this_queryTerms=$this_queryTerms."&prefermaskfilter=".$this->prefermaskfilter;
$this_queryTerms=$this_queryTerms."&indexof=off";
$this_queryTerms=$this_queryTerms."&meanCount=5";
#$qs=$_SERVER["QUERY_STRING"];
#$yquery="query=".$_GET['query'];
#i#f ($yquery=="query=") $yquery= "query=".$_GET['s'];
#if ($yquery=="query=") $yquery= "query=".$_GET['search'];
#if ($yquery=="query=" ||$yquery=="") # no ?query=
#{
# $this_queryTerms=$_GET['search']; # use original arg-line for proxy-mode
# $this_queryTerms=$this_queryTerms."&verify=true&contentdom=text&maximumRecords=25&startRecord=0&resource=global&nav=all&urlmaskfilter=.*&prefermaskfilter=&indexof=off&meanCount=5 ";
# } else {
# $this_queryTerms=$yquery."&verify=true&contentdom=text&maximumRecords=25&startRecord=0&resource=global&nav=all&urlmaskfilter=.*&prefermaskfilter=&indexof=off&meanCount=5 ";
# }
$this->_dur0=microtime(true);
# Start curl
$cu=$this->PeerURL.":".$this->PeerPort."/yacysearch.".$this->format.$this_queryTerms;
#
#echo "<br>CU:".$cu;
#exit;
$queryServer = curl_init($cu);
$this->_debugQuery = $cu;
$to=2000;
curl_setopt($queryServer, CURLOPT_HEADER, 0);
curl_setopt($queryServer, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($queryServer, CURLOPT_USERPWD,$tappid);
curl_setopt($queryServer, CURLOPT_CONNECTTIMEOUT_MS, $to);
if(is_array($this->_curlArray)) {
curl_setopt_array($queryServer, $this->_curlArray);
}
# Deliver
$holder = curl_exec($queryServer);
# done
curl_close($queryServer);
$this->_dur3=microtime(true)-$this->_dur0; #time for select
return $holder;
}
// BASIC: ------------------------------------------------
// for execution of server commands or query xml/json
// send html to peer selected by name-------------------
public function peerCommandDirect($peername,$command,$credentials) #="admin:yacy")
{
$YaCyURL=$peername;
$cu=$YaCyURL.$command;
#echo "<br>CU:".$cu;
#$to=2000;
$cntimeout=1;
$ctimeout=2;
$ti=microtime(true);
$queryServer = curl_init($cu);
$this->_debugQuery = $cu;
curl_setopt($queryServer, CURLOPT_HEADER, 0);
curl_setopt($queryServer, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($queryServer, CURLOPT_USERPWD,$credentials);
#MS not functional!
# curl_setopt($queryServer, CURLOPT_CONNECTTIMEOUT_MS, $to);
// connection timeout seconds
curl_setopt($queryServer, CURLOPT_CONNECTTIMEOUT, $cntimeout);
// curl timeout
curl_setopt($queryServer, CURLOPT_TIMEOUT, $ctimeout);
$holder = curl_exec($queryServer);
$this->_status = curl_getinfo($queryServer, CURLINFO_HTTP_CODE);
# done
curl_close($queryServer);
#$this->_dur2=microtime(true)-$ti; #time for select
if($this->_status>=200 && $this->_status<300)
{
return $holder;
}
else
{
#echo "<br>".$this->_status;
return false;
}
}
################ helpers #########################
// <root>
// <child1>
// <child1child1/>
// </child1>
// </root>
//
// funtion creates an array like
// array[root][child1][child1child1]
public function xml2array2($contents, $get_attributes = 1, $priority = 'tag')
{
if (!function_exists('xml_parser_create'))
{
return array ();
}
$parser = xml_parser_create('');
# if (!($fp = @ fopen($url, 'rb')))
# {
# return array ();
# }
# while (!feof($fp))
# {
# $contents .= fread($fp, 8192);
# }
# fclose($fp);
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if (!$xml_values)
return; //Hmm...
$xml_array = array ();
$parents = array ();
$opened_tags = array ();
$arr = array ();
$current = & $xml_array;
$repeated_tag_index = array ();
foreach ($xml_values as $data)
{
unset ($attributes, $value);
extract($data);
$result = array ();
$attributes_data = array ();
if (isset ($value))
{
if ($priority == 'tag')
$result = $value;
else
$result['value'] = $value;
}
if (isset ($attributes) and $get_attributes)
{
foreach ($attributes as $attr => $val)
{
if ($priority == 'tag')
$attributes_data[$attr] = $val;
else
$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
if ($type == "open")
{
$parent[$level -1] = & $current;
if (!is_array($current) or (!in_array($tag, array_keys($current))))
{
$current[$tag] = $result;
if ($attributes_data)
$current[$tag . '_attr'] = $attributes_data;
$repeated_tag_index[$tag . '_' . $level] = 1;
$current = & $current[$tag];
}
else
{
if (isset ($current[$tag][0]))
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
$repeated_tag_index[$tag . '_' . $level]++;
}
else
{
$current[$tag] = array (
$current[$tag],
$result
);
$repeated_tag_index[$tag . '_' . $level] = 2;
if (isset ($current[$tag . '_attr']))
{
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
$current = & $current[$tag][$last_item_index];
}
}
elseif ($type == "complete")
{
if (!isset ($current[$tag]))
{
$current[$tag] = $result;
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $attributes_data)
$current[$tag . '_attr'] = $attributes_data;
}
else
{
if (isset ($current[$tag][0]) and is_array($current[$tag]))
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
if ($priority == 'tag' and $get_attributes and $attributes_data)
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag . '_' . $level]++;
}
else
{
$current[$tag] = array (
$current[$tag],
$result
);
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $get_attributes)
{
if (isset ($current[$tag . '_attr']))
{
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
if ($attributes_data)
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
}
}
}
elseif ($type == 'close')
{
$current = & $parent[$level -1];
}
}
return ($xml_array);
}
}
// END API CLASS
?>