/web/classes/IAHx.php

https://github.com/robertatakenaka/Biodiversidade · PHP · 127 lines · 97 code · 25 blank · 5 comment · 15 complexity · dba1e0eed37318d2c465e3a4c468b824 MD5 · raw file

  1. <?php
  2. class IAHx
  3. {
  4. //TODO
  5. var $IAHXSERVER = "";
  6. var $param = array();
  7. function IAHx($site, $collection, $count, $output, $lang ){
  8. global $config;
  9. $this->param["site"] = $site;
  10. $this->param["col"] = $collection;
  11. $this->param["count"] = $count;
  12. $this->param["output"]= $output;
  13. $this->param["lang"] = $lang;
  14. $this->setIAHxServer( $config->search_server );
  15. return;
  16. }
  17. function setIAHxServer($server){
  18. $this->IAHXSERVER = $server;
  19. return;
  20. }
  21. function setParam($param, $value){
  22. if ($value != null && $value != ""){
  23. $this->param[$param] = $value;
  24. }
  25. return;
  26. }
  27. function search($query, $index, $filter, $from){
  28. $this->param["op"] = "search";
  29. $this->param["q"] = $query;
  30. $this->param["index"] = $index;
  31. if ($from != "" && $from > 0){
  32. $this->param["start"] = ($from - 1);
  33. }
  34. if ( isset($filter) ){
  35. $this->mountFilterParam($filter);
  36. }
  37. $searchUrl = $this->requestUrl();
  38. $result = $this->documentPost( $searchUrl );
  39. return trim($result);
  40. }
  41. function mountFilterParam($filter){
  42. $filter = $this->cleanArray($filter); //remove valores vazios do array
  43. $fq = join(" AND ",$filter);
  44. $this->param["fq"] = stripslashes($fq);
  45. return;
  46. }
  47. function requestUrl() {
  48. $urlParam = "";
  49. reset($this->param);
  50. while (list($key, $value) = each($this->param)) {
  51. if ($value != ""){
  52. $urlParam .= "&" . $key . "=" . urlencode($value);
  53. }
  54. }
  55. $requestUrl = "http://" . $this->IAHXSERVER . "/iahx-controller/?" . substr($urlParam,1);
  56. //print $requestUrl;
  57. return $requestUrl;
  58. }
  59. function documentPost( $url )
  60. {
  61. global $debug;
  62. // Strip URL
  63. $url_parts = parse_url($url);
  64. $host = $url_parts["host"];
  65. $port = ($url_parts["port"] ? $url_parts["port"] : "80");
  66. $path = $url_parts["path"];
  67. $query = $url_parts["query"];
  68. $result = "";
  69. $timeout = 10;
  70. $contentLength = strlen($query);
  71. if (isset($debug)){
  72. print "<b>iahx request:</b> " . $url . "<br/>";
  73. }
  74. // Generate the request header
  75. $ReqHeader =
  76. "POST $path HTTP/1.0\r\n".
  77. "Host: $host\r\n".
  78. "User-Agent: PostIt\r\n".
  79. "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n".
  80. "Content-Length: $contentLength\r\n\r\n".
  81. "$query\n";
  82. // Open the connection to the host
  83. $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
  84. fputs( $fp, $ReqHeader );
  85. if ($fp) {
  86. while (!feof($fp)){
  87. $result .= fgets($fp, 4096);
  88. }
  89. }
  90. return substr($result,strpos($result,"\r\n\r\n"));
  91. }
  92. function cleanArray($array) {
  93. foreach ($array as $index => $value) {
  94. if (empty($value)) unset($array[$index]);
  95. }
  96. return $array;
  97. }
  98. function getFilterParam(){
  99. return $this->param["fq"];
  100. }
  101. }
  102. ?>