[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/phpgwapi/inc/ -> class.http.inc.php (source)

   1  <?php
   2    /**************************************************************************\
   3    * eGroupWare API - HTTP protocol class                                     *
   4    * http://www.egroupware.org/api                                            *
   5    * ------------------------------------------------------------------------ *
   6    * This is not part of eGroupWare, but is used by eGroupWare.               * 
   7    * ------------------------------------------------------------------------ *
   8    * This program is free software; you can redistribute it and/or modify it  *
   9    * under the terms of the GNU General Public License as published by the    *
  10    * Free Software Foundation; either version 2 of the License, or (at your   *
  11    * option) any later version.                                               *
  12    \**************************************************************************/
  13  
  14    /* $Id: class.http.inc.php 15134 2004-05-05 12:06:13Z reinerj $ */
  15  
  16      class http
  17      {
  18          var $host_name = '';
  19          var $host_port = 80;
  20          var $proxy_host_name = '';
  21          var $proxy_host_port = 80;
  22  
  23          var $request_method = 'GET';
  24          var $user_agent = 'Manuel Lemos HTTP class test script';
  25          var $request_uri = '';
  26          var $protocol_version = '1.0';
  27          var $debug = 0;
  28          var $support_cookies = 1;
  29          var $cookies = array();
  30  
  31          /* private variables - DO NOT ACCESS */
  32  
  33          var $state = 'Disconnected';
  34          var $connection = 0;
  35          var $content_length = 0;
  36          var $read_length = 0;
  37          var $request_host = '';
  38          var $months = array(
  39              'Jan' => '01',
  40              'Feb' => '02',
  41              'Mar' => '03',
  42              'Apr' => '04',
  43              'May' => '05',
  44              'Jun' => '06',
  45              'Jul' => '07',
  46              'Aug' => '08',
  47              'Sep' => '09',
  48              'Oct' => '10',
  49              'Nov' => '11',
  50              'Dec' => '12'
  51          );
  52  
  53          /* Private methods - DO NOT CALL */
  54  
  55  		function OutputDebug($message)
  56          {
  57              echo $message,"\n";
  58          }
  59  
  60  		function GetLine()
  61          {
  62              for($line='';;)
  63              {
  64                  if(feof($this->connection) || !($part=fgets($this->connection,100)))
  65                  {
  66                      return(0);
  67                  }
  68                  $line.=$part;
  69                  $length=strlen($line);
  70                  if($length>=2 && substr($line,$length-2,2)=="\r\n")
  71                  {
  72                      $line=substr($line,0,$length-2);
  73                      if($this->debug)
  74                      {
  75                          $this->OutputDebug("< $line");
  76                      }
  77                      return($line);
  78                  }
  79              }
  80          }
  81  
  82  		function PutLine($line)
  83          {
  84              if($this->debug)
  85              {
  86                  $this->OutputDebug("> $line");
  87              }
  88              return(fputs($this->connection,"$line\r\n"));
  89          }
  90  
  91  		function PutData($data)
  92          {
  93              if($this->debug)
  94              {
  95                  $this->OutputDebug("> $data");
  96              }
  97              return(fputs($this->connection,$data));
  98          }
  99  
 100  		function Readbytes($length)
 101          {
 102              if($this->debug)
 103              {
 104                  if(($bytes=fread($this->connection,$length))!="")
 105                  {
 106                      $this->OutputDebug("< $bytes");
 107                  }
 108                  return($bytes);
 109              }
 110              else
 111              {
 112                  return(fread($this->connection,$length));
 113              }
 114          }
 115  
 116  		function EndOfInput()
 117          {
 118              return(feof($this->connection));
 119          }
 120  
 121  		function Connect($host_name,$host_port)
 122          {
 123              if($this->debug)
 124              {
 125                  $this->OutputDebug("Connecting to $host_name...");
 126              }
 127              if(($this->connection=fsockopen($host_name,$host_port,&$error))==0)
 128              {
 129                  switch($error)
 130                  {
 131                      case -3:
 132                          return('-3 socket could not be created');
 133                      case -4:
 134                          return('-4 dns lookup on hostname "'.$host_name.'" failed');
 135                      case -5:
 136                          return('-5 connection refused or timed out');
 137                      case -6:
 138                          return('-6 fdopen() call failed');
 139                      case -7:
 140                          return('-7 setvbuf() call failed');
 141                      default:
 142                          return($error.' could not connect to the host "'.$host_name.'"');
 143                  }
 144              }
 145              else
 146              {
 147                  if($this->debug)
 148                  {
 149                      $this->OutputDebug("Connected to $host_name");
 150                  }
 151                  $this->state='Connected';
 152                  return("");
 153              }
 154          }
 155  
 156  		function Disconnect()
 157          {
 158              if($this->debug)
 159              {
 160                  $this->OutputDebug('Disconnected from '.$this->host_name);
 161              }
 162              fclose($this->connection);
 163              return('');
 164          }
 165  
 166          /* Public methods */
 167  
 168  		function Open($arguments)
 169          {
 170              if($this->state!='Disconnected')
 171              {
 172                  return('1 already connected');
 173              }
 174              if(IsSet($arguments['HostName']))
 175              {
 176                  $this->host_name=$arguments['HostName'];
 177              }
 178              if(IsSet($arguments['HostPort']))
 179              {
 180                  $this->host_port=$arguments['HostPort'];
 181              }
 182              if(IsSet($arguments['ProxyHostName']))
 183              {
 184                  $this->proxy_host_name=$arguments['ProxyHostName'];
 185              }
 186              if(IsSet($arguments['ProxyHostPort']))
 187              {
 188                  $this->proxy_host_port=$arguments['ProxyHostPort'];
 189              }
 190              if(strlen($this->proxy_host_name)==0)
 191              {
 192                  if(strlen($this->host_name)==0)
 193                  {
 194                      return('2 it was not specified a valid hostname');
 195                  }
 196                  $host_name = $this->host_name;
 197                  $host_port = $this->host_port;
 198              }
 199              else
 200              {
 201                  $host_name = $this->proxy_host_name;
 202                  $host_port = $this->proxy_host_port;
 203              }
 204              $error = $this->Connect($host_name,$host_port);
 205              if(strlen($error)==0)
 206              {
 207                  $this->state = 'Connected';
 208              }
 209              return($error);
 210          }
 211  
 212  		function Close()
 213          {
 214              if($this->state == 'Disconnected')
 215              {
 216                  return('1 already disconnected');
 217              }
 218              $error = $this->Disconnect();
 219              if(strlen($error) == 0)
 220              {
 221                  $this->state = 'Disconnected';
 222              }
 223              return($error);
 224          }
 225  
 226  		function SendRequest($arguments)
 227          {
 228              switch($this->state)
 229              {
 230                  case 'Disconnected':
 231                      return('1 connection was not yet established');
 232                  case 'Connected':
 233                      break;
 234                  default:
 235                      return('2 can not send request in the current connection state');
 236              }
 237              if(IsSet($arguments['RequestMethod']))
 238              {
 239                  $this->request_method = $arguments['RequestMethod'];
 240              }
 241              if(IsSet($arguments['User-Agent']))
 242              {
 243                  $this->user_agent = $arguments['User-Agent'];
 244              }
 245              if(strlen($this->request_method) == 0)
 246              {
 247                  return('3 it was not specified a valid request method');
 248              }
 249              if(IsSet($arguments['RequestURI']))
 250              {
 251                  $this->request_uri = $arguments['RequestURI'];
 252              }
 253              if(strlen($this->request_uri) == 0 || substr($this->request_uri,0,1) != '/')
 254              {
 255                  return('4 it was not specified a valid request URI');
 256              }
 257              $request_body = '';
 258              $headers=(IsSet($arguments['Headers']) ? $arguments['Headers'] : array());
 259              if($this->request_method == 'POST')
 260              {
 261                  if(IsSet($arguments['PostValues']))
 262                  {
 263                      $values = $arguments['PostValues'];
 264                      if(!@is_array($values))
 265                      {
 266                          return('5 it was not specified a valid POST method values array');
 267                      }
 268                      for($request_body = '',Reset($values),$value=0;$value<count($values);Next($values),$value++)
 269                      {
 270                          if($value>0)
 271                          {
 272                              $request_body .= '&';
 273                          }
 274                          $request_body.=Key($values).'='.UrlEncode($values[Key($values)]);
 275                      }
 276                      $headers['Content-type'] = 'application/x-www-form-urlencoded';
 277                  }
 278              }
 279              if(strlen($this->proxy_host_name) == 0)
 280              {
 281                  $request_uri = $this->request_uri;
 282              }
 283              else
 284              {
 285                  $request_uri = 'http://'.$this->host_name.($this->host_port==80 ? '' : ':'.$this->host_port).$this->request_uri;
 286              }
 287              if(($success = $this->PutLine($this->request_method.' '.$request_uri.' HTTP/'.$this->protocol_version)))
 288              {
 289                  if(($body_length = strlen($request_body)))
 290                  {
 291                      $headers['Content-length'] = $body_length;
 292                  }
 293                  for($host_set=0,Reset($headers),$header=0;$header<count($headers);Next($headers),$header++)
 294                  {
 295                      $header_name  = Key($headers);
 296                      $header_value = $headers[$header_name];
 297                      if(@is_array($header_value))
 298                      {
 299                          for(Reset($header_value),$value=0;$value<count($header_value);Next($header_value),$value++)
 300                          {
 301                              if(!$success = $this->PutLine("$header_name: ".$header_value[Key($header_value)]))
 302                              {
 303                                  break 2;
 304                              }
 305                          }
 306                      }
 307                      else
 308                      {
 309                          if(!$success = $this->PutLine("$header_name: $header_value"))
 310                          {
 311                              break;
 312                          }
 313                      }
 314                      if(strtolower(Key($headers)) == 'host')
 315                      {
 316                          $this->request_host = strtolower($header_value);
 317                          $host_set = 1;
 318                      }
 319                  }
 320                  if($success)
 321                  {
 322                      if(!$host_set)
 323                      {
 324                          $success = $this->PutLine('Host: '.$this->host_name);
 325                          $this->request_host = strtolower($this->host_name);
 326                      }
 327                      if(count($this->cookies) && IsSet($this->cookies[0]))
 328                      {
 329                          $now = gmdate('Y-m-d H-i-s');
 330                          for($cookies = array(),$domain=0,Reset($this->cookies[0]);$domain<count($this->cookies[0]);Next($this->cookies[0]),$domain++)
 331                          {
 332                              $domain_pattern = Key($this->cookies[0]);
 333                              $match = strlen($this->request_host)-strlen($domain_pattern);
 334                              if($match >= 0 &&
 335                                  !strcmp($domain_pattern,substr($this->request_host,$match)) &&
 336                                  ($match == 0 || $domain_pattern[0] == '.' || $this->request_host[$match-1] == '.'))
 337                              {
 338                                  for(Reset($this->cookies[0][$domain_pattern]),$path_part=0;$path_part<count($this->cookies[0][$domain_pattern]);Next($this->cookies[0][$domain_pattern]),$path_part++)
 339                                  {
 340                                      $path = Key($this->cookies[0][$domain_pattern]);
 341                                      if(strlen($this->request_uri) >= strlen($path) && substr($this->request_uri,0,strlen($path)) == $path)
 342                                      {
 343                                          for(Reset($this->cookies[0][$domain_pattern][$path]),$cookie = 0;$cookie<count($this->cookies[0][$domain_pattern][$path]);Next($this->cookies[0][$domain_pattern][$path]),$cookie++)
 344                                          {
 345                                              $cookie_name = Key($this->cookies[0][$domain_pattern][$path]);
 346                                              $expires     = $this->cookies[0][$domain_pattern][$path][$cookie_name]['expires'];
 347                                              if($expires == '' || strcmp($now,$expires)<0)
 348                                              {
 349                                                  $cookies[$cookie_name] = $this->cookies[0][$domain_pattern][$path][$cookie_name];
 350                                              }
 351                                          }
 352                                      }
 353                                  }
 354                              }
 355                          }
 356                          for(Reset($cookies),$cookie=0;$cookie<count($cookies);Next($cookies),$cookie++)
 357                          {
 358                              $cookie_name = Key($cookies);
 359                              if(!($success = $this->PutLine('Cookie: '.UrlEncode($cookie_name).'='.$cookies[$cookie_name]['value'].';')))
 360                              {
 361                                  break;
 362                              }
 363                          }
 364                      }
 365                      if($success)
 366                      {
 367                          if($success)
 368                          {
 369                              $success = $this->PutLine('');
 370                              if($body_length && $success)
 371                              {
 372                                  $success = $this->PutData($request_body);
 373                              }
 374                          }
 375                      }
 376                  }
 377              }
 378              if(!$success)
 379              {
 380                  return('5 could not send the HTTP request');
 381              }
 382              $this->state = 'RequestSent';
 383              return('');
 384          }
 385  
 386  		function ReadReplyHeaders(&$headers)
 387          {
 388              switch($this->state)
 389              {
 390                  case 'Disconnected':
 391                      return('1 connection was not yet established');
 392                  case 'Connected':
 393                      return('2 request was not sent');
 394                  case 'RequestSent':
 395                      break;
 396                  default:
 397                      return('3 can not get request headers in the current connection state');
 398              }
 399              $headers = array();
 400              $this->content_length = $this->read_length = 0;
 401              $this->content_length_set = 0;
 402              for(;;)
 403              {
 404                  $line = $this->GetLine();
 405                  if(!is_string($line))
 406                  {
 407                      return('4 could not read request reply');
 408                  }
 409                  if($line == '')
 410                  {
 411                      $this->state = 'GotReplyHeaders';
 412                      return('');
 413                  }
 414                  $header_name  = strtolower(strtok($line,':'));
 415                  $header_value = Trim(Chop(strtok("\r\n")));
 416                  if(IsSet($headers[$header_name]))
 417                  {
 418                      if(is_string($headers[$header_name]))
 419                      {
 420                          $headers[$header_name] = array($headers[$header_name]);
 421                      }
 422                      $headers[$header_name][] = $header_value;
 423                  }
 424                  else
 425                  {
 426                      $headers[$header_name] = $header_value;
 427                  }
 428                  switch($header_name)
 429                  {
 430                      case 'content-length':
 431                      $this->content_length = (int)$headers[$header_name];
 432                      $this->content_length_set = 1;
 433                      break;
 434                      case 'set-cookie':
 435                      if($this->support_cookies)
 436                      {
 437                          $cookie_name  = trim(strtok($headers[$header_name],'='));
 438                          $cookie_value = strtok(';');
 439                          $domain = $this->request_host;
 440                          $path = '/';
 441                          $expires = '';
 442                          $secure = 0;
 443                          while(($name=strtolower(trim(strtok('=')))) != '')
 444                          {
 445                              $value=UrlDecode(strtok(';'));
 446                              switch($name)
 447                              {
 448                                  case 'domain':
 449                                      if($value == '' || !strpos($value,'.',$value[0] == '.'))
 450                                      {
 451                                          break;
 452                                      }
 453                                      $domain = strtolower($value);
 454                                      break;
 455                                  case 'path':
 456                                      if($value != '' && $value[0] == '/')
 457                                      {
 458                                          $path = $value;
 459                                      }
 460                                      break;
 461                                  case 'expires':
 462                                      if(ereg("^((Mon|Monday|Tue|Tuesday|Wed|Wednesday|Thu|Thursday|Fri|Friday|Sat|Saturday|Sun|Sunday), )?([0-9]{2})\\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\-([0-9]{2,4}) ([0-9]{2})\\:([0-9]{2})\\:([0-9]{2}) GMT$",$value,$matches))
 463                                      {
 464                                          $year = (int)$matches[5];
 465                                          if($year<1900)
 466                                          {
 467                                              $year += ($year<70 ? 2000 : 1900);
 468                                          }
 469                                          $expires = "$year-".$this->months[$matches[4]].'-'.$matches[3].' '.$matches[6].':'.$matches[7].':'.$matches[8];
 470                                      }
 471                                      break;
 472                                  case 'secure':
 473                                      $secure = 1;
 474                                      break;
 475                              }
 476                          }
 477                          $this->cookies[$secure][$domain][$path][$cookie_name] = array(
 478                              'name'    => $cookie_name,
 479                              'value'   => $cookie_value,
 480                              'domain'  => $domain,
 481                              'path'    => $path,
 482                              'expires' => $expires,
 483                              'secure'  => $secure
 484                          );
 485                      }
 486                  }
 487              }
 488          }
 489  
 490  		function ReadReplyBody(&$body,$length)
 491          {
 492              switch($this->state)
 493              {
 494                  case 'Disconnected':
 495                      return('1 connection was not yet established');
 496                  case 'Connected':
 497                      return('2 request was not sent');
 498                  case 'RequestSent':
 499                      if(($error = $this->ReadReplyHeaders(&$headers)) != '')
 500                      {
 501                          return($error);
 502                      }
 503                      break;
 504                  case 'GotReplyHeaders':
 505                      break;
 506                  default:
 507                      return('3 can not get request headers in the current connection state');
 508              }
 509              $body = '';
 510              if($this->content_length_set)
 511              {
 512                  $length = min($this->content_length-$this->read_length,$length);
 513              }
 514              if($length>0 && !$this->EndOfInput() && ($body = $this->ReadBytes($length)) == '')
 515              {
 516                  return('4 could not get the request reply body');
 517              }
 518              return('');
 519          }
 520  
 521  		function GetPersistentCookies(&$cookies)
 522          {
 523              $now = gmdate('Y-m-d H-i-s');
 524              $cookies = array();
 525              for($secure_cookies = 0,Reset($this->cookies);$secure_cookies<count($this->cookies);Next($this->cookies),$secure_cookies++)
 526              {
 527                  $secure = Key($this->cookies);
 528                  for($domain = 0,Reset($this->cookies[$secure]);$domain<count($this->cookies[$secure]);Next($this->cookies[$secure]),$domain++)
 529                  {
 530                      $domain_pattern = Key($this->cookies[$secure]);
 531                      for(Reset($this->cookies[$secure][$domain_pattern]),$path_part=0;$path_part<count($this->cookies[$secure][$domain_pattern]);Next($this->cookies[$secure][$domain_pattern]),$path_part++)
 532                      {
 533                          $path=Key($this->cookies[$secure][$domain_pattern]);
 534                          for(Reset($this->cookies[$secure][$domain_pattern][$path]),$cookie=0;$cookie<count($this->cookies[$secure][$domain_pattern][$path]);Next($this->cookies[$secure][$domain_pattern][$path]),$cookie++)
 535                          {
 536                              $cookie_name = Key($this->cookies[$secure][$domain_pattern][$path]);
 537                              $expires     = $this->cookies[$secure][$domain_pattern][$path][$cookie_name]['expires'];
 538                              if($expires != '' && strcmp($now,$expires)<0)
 539                              {
 540                                  $cookies[$secure][$domain_pattern][$path][$cookie_name] = $this->cookies[$secure][$domain_pattern][$path][$cookie_name];
 541                              }
 542                          }
 543                      }
 544                  }
 545              }
 546          }
 547      }


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7