PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/webservices/common.php

https://github.com/robertatakenaka/Web
PHP | 255 lines | 213 code | 30 blank | 12 comment | 34 complexity | a781e432897ebc702d97a599d55f2cdb MD5 | raw file
  1. <?PHP
  2. //=============================================================================================
  3. function getElementValue ($xml, $element, $attributes = '')
  4. {
  5. $elementPeace = "";
  6. if ($attributes == '') {
  7. $start_string = "<" . $element . ">";
  8. } else {
  9. $start_string = "<" . $element . " " . $attributes . ">";
  10. }
  11. $end_string = "</" . $element . ">";
  12. if (strpos($xml, $start_string) > 0){
  13. $elementPeace = substr($xml, strpos($xml, $start_string) + strlen($start_string), strpos($xml, $end_string) - strpos($xml, $start_string) - strlen($start_string));
  14. }
  15. return $elementPeace;
  16. }
  17. //=============================================================================================
  18. function getIndicators($indicator,$applServer,$databasePath,$issn)
  19. {
  20. $result="";
  21. switch ($indicator){
  22. case "journalTotal":
  23. $serviceUrl = "http://" . $applServer . "/cgi-bin/wxis.exe/webservices/wxis/?IsisScript=list.xis&database=".$databasePath ."title/title&count=1";
  24. $XML = readData($serviceUrl,true);
  25. $result=getElementValue(getElementValue(str_replace("<hr>","<hr />",$XML) , "Isis_Total"),"occ");
  26. break;
  27. case "articleTotal":
  28. if(isset($issn) && is_array($issn)){ /* selective harvesting */
  29. $issnTmp='';
  30. foreach($issn as $key => $value){
  31. if($key > 0){
  32. $issnTmp .= ' or ';
  33. }
  34. $issnTmp .= 'HR=S'.$value.'$';
  35. }
  36. $issn = $issnTmp;
  37. $serviceUrl = "http://" . $applServer . "/cgi-bin/wxis.exe/webservices/wxis/?IsisScript=search.xis&database=".$databasePath ."artigo/artigo&search=".$issn."&count=1";
  38. }else{ /* full harvesting */
  39. $serviceUrl = "http://" . $applServer . "/cgi-bin/wxis.exe/webservices/wxis/?IsisScript=search.xis&database=".$databasePath ."artigo/artigo&search=tp=o&count=1";
  40. }
  41. $XML = readData($serviceUrl,true);
  42. $result=getElementValue(getElementValue(str_replace("<hr>","<hr />",$XML) , "Isis_Total"),"occ");
  43. break;
  44. case "issueTotal":
  45. if(isset($issn) && is_array($issn)){ /* selective harvesting */
  46. $issnTmp='';
  47. foreach($issn as $key => $value){
  48. if($key > 0){
  49. $issnTmp .= ' or ';
  50. }
  51. $issnTmp .= 'seq='.$value.'$';
  52. }
  53. $issn = $issnTmp;
  54. $serviceUrl = "http://" . $applServer . "/cgi-bin/wxis.exe/webservices/wxis/?IsisScript=search.xis&database=".$databasePath ."issue/issue&search=".$issn."&count=1";
  55. }else{ /* full harvesting */
  56. $serviceUrl = "http://" . $applServer . "/cgi-bin/wxis.exe/webservices/wxis/?IsisScript=search.xis&database=".$databasePath ."artigo/artigo&search=tp=i&count=1";
  57. }
  58. $XML = readData($serviceUrl,true);
  59. $result=getElementValue(getElementValue(str_replace("<hr>","<hr />",$XML) , "Isis_Total"),"occ");
  60. break;
  61. case "citationTotal":
  62. if(isset($issn) && is_array($issn)){ /* selective harvesting */
  63. $issnTmp='';
  64. foreach($issn as $key => $value){
  65. if($key > 0){
  66. $issnTmp .= ' or ';
  67. }
  68. $issnTmp .= 'R=S'.$value.'$';
  69. }
  70. $issn = $issnTmp;
  71. $serviceUrl = "http://" . $applServer . "/cgi-bin/wxis.exe/webservices/wxis/?IsisScript=search.xis&database=".$databasePath ."artigo/artigo&search=".$issn."&count=1";
  72. }else{ /* full harvesting */
  73. $serviceUrl = "http://" . $applServer . "/cgi-bin/wxis.exe/webservices/wxis/?IsisScript=search.xis&database=".$databasePath ."artigo/artigo&search=tp=c&count=1";
  74. }
  75. $XML = readData($serviceUrl,true);
  76. $result=getElementValue(getElementValue(str_replace("<hr>","<hr />",$XML) , "Isis_Total"),"occ");
  77. break;
  78. case "journalArticleTotal":
  79. $serviceUrl = "http://" . $applServer . "/cgi-bin/wxis.exe/webservices/wxis/?IsisScript=search.xis&database=".$databasePath ."artigo/artigo&search=HR=S".$issn."$&count=1";
  80. $XML = readData($serviceUrl,true);
  81. $result=getElementValue(getElementValue(str_replace("<hr>","<hr />",$XML) , "Isis_Total"),"occ");
  82. break;
  83. case "journalIssueTotal":
  84. $serviceUrl = "http://" . $applServer . "/cgi-bin/wxis.exe/webservices/wxis/?IsisScript=search.xis&database=".$databasePath ."issue/issue&search=Y".$issn."$&count=1";
  85. $XML = readData($serviceUrl,true);
  86. $result=getElementValue(getElementValue(str_replace("<hr>","<hr />",$XML) , "Isis_Total"),"occ");
  87. break;
  88. case "journalCitationTotal":
  89. $serviceUrl = "http://" . $applServer . "/cgi-bin/wxis.exe/webservices/wxis/?IsisScript=search.xis&database=".$databasePath ."artigo/artigo&search=R=S".$issn."$&count=1";
  90. $XML = readData($serviceUrl,true);
  91. $result=getElementValue(getElementValue(str_replace("<hr>","<hr />",$XML) , "Isis_Total"),"occ");
  92. break;
  93. }
  94. return $result;
  95. }
  96. //=============================================================================================
  97. function process($serviceUrl, $redirectHtml = "")
  98. {
  99. global $output, $serviceRoot;
  100. if ($output == "html"){
  101. if ($redirectHtml == ""){
  102. print "Service not available";
  103. }else{
  104. header("Location: " . $redirectHtml);
  105. }
  106. }else{
  107. $xml = readData($serviceUrl, true);
  108. if ($output == "xml"){
  109. header("Content-type: text/xml");
  110. return envelopeXml($xml, $serviceRoot);
  111. }else{
  112. return utf8_encode($xml);
  113. }
  114. }
  115. }
  116. //============================================================================================
  117. function envelopeXml($str, $root)
  118. {
  119. global $version;
  120. $envelope = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
  121. $envelope.= "<" . $root . " version=\"". $version . "\">\n";
  122. $envelope.= $str;
  123. $envelope.= "</" . $root . ">\n";
  124. return $envelope;
  125. }
  126. //============================================================================================
  127. function readData($readFrom, $removeHeader)
  128. {
  129. $str = "";
  130. $query = strstr($readFrom, "?");
  131. if ( strlen($query) > 250 ){
  132. $str = PostIt($readFrom);
  133. }else{
  134. $readUrl = encodeValues($readFrom);
  135. $fp = fopen($readUrl,"r");
  136. if ($fp)
  137. {
  138. while (!feof ($fp)) {
  139. $buffer= fgets($fp, 8096);
  140. $str.= $buffer;
  141. }
  142. fclose ($fp);
  143. }
  144. }
  145. $str = trim($str);
  146. if ( $removeHeader )
  147. {
  148. // remove xml processing instruction
  149. if ( strncasecmp($str, "<?xml", 5) == 0 )
  150. {
  151. $pos = strpos($str, "?>");
  152. if ( $pos > 0 )
  153. {
  154. $str = substr_replace($str,"",0,$pos + 2);
  155. }
  156. }
  157. }
  158. return $str;
  159. }
  160. //=============================================================================================
  161. function PostIt($url) {
  162. // Strip URL
  163. $url_parts = parse_url($url);
  164. $host = $url_parts["host"];
  165. $port = ($url_parts["port"]) ? $url_parts["port"] : 80;
  166. $path = $url_parts["path"];
  167. $query = $url_parts["query"];
  168. $timeout = 10;
  169. $contentLength = strlen($query);
  170. // Generate the request header
  171. $ReqHeader =
  172. "POST $path HTTP/1.0\n".
  173. "Host: $host\n".
  174. "User-Agent: PostIt\n".
  175. "Content-Type: application/x-www-form-urlencoded\n".
  176. "Content-Length: $contentLength\n\n".
  177. "$query\n";
  178. // Open the connection to the host
  179. $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
  180. fputs( $fp, $ReqHeader );
  181. if ($fp) {
  182. while (!feof($fp)){
  183. $result .= fgets($fp, 4096);
  184. }
  185. }
  186. //return strstr($result,"<");
  187. return trim( substr( $result,strpos($result,"\n\r")+1 ) );
  188. }
  189. //================================================================================================
  190. function encodeValues( $docURL )
  191. {
  192. if (ereg("\?", $docURL)) {
  193. $splited1[0] = substr($docURL, 0, strpos($docURL,"?"));
  194. $splited1[1] = substr($docURL, strpos($docURL,"?")+1);
  195. }else{
  196. return $docURL;
  197. }
  198. $splited2 = split( "&", $splited1[1] );
  199. if ( count($splited2) < 2 )
  200. {
  201. return $docURL;
  202. }
  203. $docURL = $splited1[0] . "?";
  204. $fisrt = true;
  205. foreach ($splited2 as $value)
  206. {
  207. if ( $first )
  208. {
  209. $first = false;
  210. }
  211. else
  212. {
  213. $docURL .= "&";
  214. }
  215. $splited3 = split("=",$value);
  216. $docURL .= $splited3[0];
  217. if ( count($splited3) > 1 ){
  218. for ($i = 1; $i < count($splited3); $i++){
  219. $docURL .= "=" . urlencode($splited3[$i]);
  220. }
  221. }
  222. }
  223. return $docURL;
  224. }
  225. ?>