PageRenderTime 34ms CodeModel.GetById 81ms RepoModel.GetById 17ms app.codeStats 1ms

/libs/elavaarkisto.class.php

https://github.com/orktes/xelavaarkisto
PHP | 213 lines | 164 code | 31 blank | 18 comment | 32 complexity | eec95400411f2647cd2fc9eb2dfb8edd MD5 | raw file
  1. <?php
  2. /* Copyright Jaakko Lukkari 2011
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. class ElavaArkisto {
  19. var $storeCookiesToSession=false;
  20. var $cookiesSessionVariable="elavaarkistocookies";
  21. var $cookie_file="cookie.txt";
  22. var $serviceUrl="";
  23. function ElavaArkisto($storeCookiesToSession=false) {
  24. $this->storeCookiesToSession=$storeCookiesToSession;
  25. }
  26. function setCookiesSessionVariable($cookiesSessionVariable) {
  27. $this->cookiesSessionVariable=$cookiesSessionVariable;
  28. }
  29. function getCookiesSessionVariable() {
  30. return $this->cookiesSessionVariable;
  31. }
  32. function setCookieFile($cookieFile) {
  33. $this->cookie_file=$cookieFile;
  34. }
  35. function getCookieFile($cookieFile) {
  36. return $this->cookie_file;
  37. }
  38. private function open_page($url,$f=1,$c=2,$r=0,$a=0,$cf=0,$pd=""){
  39. global $oldheader;
  40. $url = str_replace("http://","",$url);
  41. if (preg_match("#/#","$url")){
  42. $page = $url;
  43. $url = @explode("/",$url);
  44. $url = $url[0];
  45. $page = str_replace($url,"",$page);
  46. if (!$page || $page == ""){
  47. $page = "/";
  48. }
  49. $ip = gethostbyname($url);
  50. }else{
  51. $ip = gethostbyname($url);
  52. $page = "/";
  53. }
  54. $open = fsockopen($ip, 80, $errno, $errstr, 60);
  55. if ($pd){
  56. $send = "POST $page HTTP/1.0\r\n";
  57. }else{
  58. $send = "GET $page HTTP/1.0\r\n";
  59. }
  60. $send .= "Host: $url\r\n";
  61. if ($r){
  62. $send .= "Referer: $r\r\n";
  63. }else{
  64. if (isset($_SERVER['HTTP_REFERER'])){
  65. $send .= "Referer: {$_SERVER['HTTP_REFERER']}\r\n";
  66. }
  67. }
  68. if ($cf){
  69. if (@file_exists($cf)||$this->storeCookiesToSession){
  70. if($this->storeCookiesToSession) {
  71. session_start();
  72. if(isset($_SESSION[$this->cookiesSessionVariable])){
  73. $cookie=urldecode($_SESSION[$this->cookiesSessionVariable]);
  74. }
  75. } else {
  76. $cookie = urldecode(@file_get_contents($cf));
  77. }
  78. if ($cookie){
  79. $send .= "Cookie: $cookie\r\n";
  80. }
  81. }
  82. }
  83. $send .= "Accept-Language: en-us, en;q=0.50\r\n";
  84. if ($a){
  85. $send .= "User-Agent: $a\r\n";
  86. }else if(isset($_SERVER['HTTP_USER_AGENT'])){
  87. $send .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
  88. } else {
  89. $send .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; fi-FI; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.04 (lucid) Firefox/3.6.13\r\n";
  90. }
  91. if ($pd){
  92. $send .= "Content-Type: application/x-www-form-urlencoded\r\n";
  93. $send .= "Content-Length: " .strlen($pd) ."\r\n\r\n";
  94. $send .= $pd;
  95. }else{
  96. $send .= "Connection: Close\r\n\r\n";
  97. }
  98. fputs($open, $send);
  99. $results="";
  100. while (!feof($open)) {
  101. $results .= fgets($open, 4096);
  102. }
  103. fclose($open);
  104. $results = @explode("\r\n\r\n",$results,2);
  105. $header = $results[0];
  106. if ($cf){
  107. if (preg_match("/Set\-Cookie\: /i","$header")){
  108. $cookie = @explode("Set-Cookie: ",$header,2);
  109. $cookie = $cookie[1];
  110. $cookie = explode("\r",$cookie);
  111. $cookie = $cookie[0];
  112. $cookie = str_replace("path=/","",$cookie);
  113. if(!$this->storeCookiesToSession) {
  114. $add = fopen($cf,'w');
  115. fwrite($add,$cookie,strlen($cookie));
  116. fclose($add);
  117. } else {
  118. session_start();
  119. $_SESSION[$this->cookiesSessionVariable]=$cookie;
  120. }
  121. }
  122. }
  123. if ($oldheader){
  124. $header = "$oldheader<br /><br />\n$header";
  125. }
  126. $header = str_replace("\n","<br />",$header);
  127. if ($results[1]){
  128. $body = $results[1];
  129. }else{
  130. $body = "";
  131. }
  132. if ($c === 2){
  133. if ($body){
  134. $results = $body;
  135. }else{
  136. $results = $header;
  137. }
  138. }
  139. if ($c === 1){
  140. $results = $header;
  141. }
  142. if ($c === 3){
  143. $results = "$header$body";
  144. }
  145. if ($f){
  146. if (preg_match("/Location\:/","$header")){
  147. $url = @explode("Location: ",$header);
  148. $url = $url[1];
  149. $url = @explode("\r",$url);
  150. $url = $url[0];
  151. $oldheader = str_replace("\r\n\r\n","",$header);
  152. $l = "&#76&#111&#99&#97&#116&#105&#111&#110&#58";
  153. $oldheader = str_replace("Location:",$l,$oldheader);
  154. return open_page($url,$f,$c,$r,$a,$cf,$pd);
  155. }else{
  156. return $results;
  157. }
  158. }else{
  159. return $results;
  160. }
  161. }
  162. private function get($url) {
  163. $f = 1;
  164. $c = 2;
  165. $r = NULL;
  166. $a = NULL;
  167. $cf = $this->cookie_file;
  168. $pd = NULL;
  169. $data = $this->open_page($url,$f,$c,$r,$a,$cf,$pd);
  170. //echo $url."\n";
  171. // echo $data;
  172. return $data;
  173. }
  174. private function get_string_between($string, $start, $end){
  175. $string = " ". $string;
  176. $ini = strpos($string,$start);
  177. if ($ini == 0) return "";
  178. $ini += strlen($start);
  179. $len = strpos($string, $end, $ini) - $ini;
  180. return substr($string, $ini, $len);
  181. }
  182. }