Subversion Repositories Sigmater

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 Andrea 1
<?php
2
define('CRLF', "\r\n");
3
define('LF', "\n");
4
define('RLEN', 8192);
5
 
6
/*******************************************************************/
7
/* class: HTTP_Header                                              */
8
/* purpose: represents an http header string ie. "<name>: <value>" */
9
/*******************************************************************/
10
 
11
class HTTP_Header
12
  {
13
  var $m_name;
14
  var $m_value;
15
  var $m_header;
16
 
17
  /***********************************************************/
18
  /* Constructor                                             */
19
  /* input: fieldName - name of the header eg. 'Referer'     */
20
  /* input: filedValue - value of the header eg. 'localhost' */
21
  /***********************************************************/
22
 
23
  function HTTP_Header($fieldName, $fieldValue)
24
    {
25
    $this->m_name = $fieldName;
26
    $this->m_value = $fieldValue;
27
    $this->m_header = $fieldName.": ".$fieldValue;
28
    }	
29
 
30
  /***************************************/
31
  /* purpose: get the entire header line */
32
  /* return: header string without crlf  */
33
  /***************************************/
34
 
35
  function Get()
36
    {
37
    return($this->m_header);
38
    }
39
 
40
  /*********************************************/
41
  /* purpose: get just the value of the header */
42
  /* return: string value of header            */
43
  /*********************************************/
44
 
45
  function GetValue()
46
    {	
47
    return($this->m_value);
48
    }
49
 
50
  /********************************************/
51
  /* purpose: get just the name of the header */
52
  /* return: string name of header            */
53
  /********************************************/
54
 
55
  function GetName()
56
    {
57
    return($this->m_name);
58
    }
59
  };
60
 
61
/**************************************/
62
/* class: HTTP_Message_hr                */
63
/* purpose: represents a http message */
64
/**************************************/
65
 
66
class HTTP_Message_hr
67
  {
68
  var $m_headers;
69
  var $m_entityBody;
70
 
71
  /***********************************************************************/
72
  /* Constructor                                                         */
73
  /* input: HTTP_Headers - array of header objs to use with this message */
74
  /* input: entityBody - an entity body                                  */
75
  /***********************************************************************/
76
 
77
  function HTTP_Message_hr($HTTP_Headers, $entityBody = "")
78
    {		
79
    $this->m_headers = array();
80
    $this->SetHeaders($HTTP_Headers);
81
    $this->m_entityBody = $entityBody;
82
    }
83
 
84
  /***********************************************/
85
  /* purpose: append header objs to this message */
86
  /* input: array of header objs                 */
87
  /***********************************************/
88
 
89
  function SetHeaders($HTTP_Headers)
90
    {
91
    if(is_array($HTTP_Headers))
92
      {
93
      reset($HTTP_Headers);
94
      while(list(, $HTTP_Header) = each($HTTP_Headers))
95
        {
96
        $this->m_headers[] = $HTTP_Header;
97
        }	
98
      }		
99
    }
100
 
101
  /***************************************/
102
  /* purpose: append a single header obj */
103
  /* input: a header obj                 */
104
  /***************************************/
105
 
106
  function SetHeader($HTTP_Header)
107
    {
108
    $this->m_headers[] = $HTTP_Header;
109
    }
110
 
111
  /****************************************************************/
112
  /* purpose: return the first header obj found based on its name */
113
  /* return: header obj                                           */
114
  /****************************************************************/
115
 
116
  function GetHeader($fieldName)
117
    {
118
    if(is_array($this->m_headers))
119
      {
120
      reset($this->m_headers);
121
      while(list(, $HTTP_Header) = each($this->m_headers))			
122
        {
123
        if($HTTP_Header->GetName() == $fieldName)
124
          return($HTTP_Header);
125
        }
126
      }
127
    return(0);
128
    }
129
 
130
  /*********************************************************/
131
  /* purpose: return array of header objs for this message */
132
  /* return: array of header objs                          */
133
  /*********************************************************/
134
 
135
  function GetHeaders()
136
    {
137
    return($this->m_headers);
138
    }
139
 
140
  /***********************************/
141
  /* purpose: return the entity body */
142
  /* return: entity body string      */
143
  /***********************************/
144
 
145
  function GetEntityBody()
146
    {
147
    return($this->m_entityBody);
148
    }
149
  };
150
 
151
/*******************************************************************/
152
/* class: HTTP_Req extends HTTP_Message_hr                            */
153
/* purpose: represents a http request to send to the server.       */
154
/*          Care must be taken with uri for use with proxy server. */
155
/*******************************************************************/
156
 
157
class HTTP_Req extends HTTP_Message_hr
158
  {	
159
  var $m_method;
160
  var $m_uri;
161
  var $m_version;
162
 
163
  /******************************************************/
164
  /* Constructor:                                       */
165
  /* input: method - the method for this request        */
166
  /* input: uri - the URI for this request              */
167
  /* input: version - the HTTP verison for this request */
168
  /* input: HTTP_Headers - array of header objs         */
169
  /* input: entityBody - entity body this request       */
170
  /******************************************************/
171
 
172
  function HTTP_Req($method, $uri, $version, $HTTP_Headers, $entityBody = "")
173
    {
174
    $this->HTTP_Message_hr($HTTP_Headers, $entityBody);
175
    $this->m_version = $version;
176
    $this->m_method = $method;
177
    $this->m_uri = $uri;
178
    }
179
 
180
  /*******************************/
181
  /* purpose: returns the method */
182
  /* return: method string       */
183
  /*******************************/
184
 
185
  function GetMethod()
186
    {
187
    return($this->m_method);
188
    }
189
 
190
  /********************************/
191
  /* purpose: returns request uri */
192
  /* return: uri string           */
193
  /********************************/
194
 
195
  function GetURI()
196
    {
197
    return($this->m_uri);
198
    }
199
 
200
  /************************************/
201
  /* purpose: return the http version */
202
  /* return: http version string      */
203
  /************************************/
204
 
205
  function GetVersion()
206
    {
207
    return($this->m_version);
208
    }
209
  };
210
 
211
/********************************************************************/
212
/* class: HTTP_Response_hr extends HTTP_Message_hr                        */
213
/* purpose: represents a http response message read from the server */
214
/********************************************************************/
215
 
216
class HTTP_Response_hr extends HTTP_Message_hr
217
  {
218
  var $m_version;
219
  var $m_statusCode;
220
  var $m_phrase;
221
 
222
  /******************************************/
223
  /* Constructor                            */
224
  /* input: version - http version          */
225
  /* input: statusCode - statusCode         */
226
  /* input: phrase - phrase                 */
227
  /* input: headers - array of header objs  */
228
  /* input: entityBody - entity body        */
229
  /******************************************/
230
 
231
  function HTTP_Response_hr($version , $statusCode, $phrase, $HTTP_Headers = "",
232
                         $entityBody = "")
233
    {
234
    $this->HTTP_Message_hr($HTTP_Headers, $entityBody);
235
    $this->m_version = $version;
236
    $this->m_statusCode = $statusCode;
237
    $this->m_phrase = $phrase;
238
    }
239
 
240
  /************************************/
241
  /* purpose: return the http version */
242
  /* return: http version string      */
243
  /************************************/
244
 
245
  function GetVersion()
246
    {
247
    return($this->m_version);
248
    }
249
 
250
  /***********************************/
251
  /* purpose: return the status code */
252
  /* return: status code string      */
253
  /***********************************/
254
 
255
  function GetStatusCode()
256
    {
257
    return($this->m_statusCode);
258
    }
259
 
260
  /******************************/
261
  /* purpose: return the phrase */
262
  /* return: phrase string      */
263
  /******************************/
264
 
265
  function GetPhrase()
266
    {
267
    return($this->m_phrase);
268
    }
269
  };
270
 
271
/************************************************************/
272
/* class: HTTP_Connection                                   */
273
/* purpose: represents a http connection to the http server */
274
/*          use to send and read http messages from server  */
275
/************************************************************/
276
 
277
class HTTP_Connection
278
  {
279
  var $m_host;
280
  var $m_port;
281
  var $m_timeout;
282
  var $m_fp;
283
 
284
  /**********************************************************/
285
  /* Constructor                                            */
286
  /* input: host - host name to connect (ip or domain name) */
287
  /* input: port - port to use on host for http             */
288
  /* input: timeout - timeout for connection                */
289
  /**********************************************************/
290
 
291
  function HTTP_Connection($host, $port=80, $timeOut=30)
292
    {
293
    $this->m_host = $host;
294
    $this->m_port = $port;
295
    $this->m_timeOut = $timeOut;
296
    }
297
 
298
  /***********************************************/
299
  /* purpose: connect to the host in constructor */
300
  /***********************************************/
301
 
302
  function Connect()
303
    {
304
    $this->m_fp = fsockopen($this->m_host, $this->m_port, $errno, $errstr,
305
                            $this->m_timeOut);	
306
    if(!$this->m_fp)
307
      return(1);
308
 
309
    return(0);
310
    }
311
 
312
  /*********************************************/
313
  /* purpose: close the connection to the host */
314
  /*********************************************/
315
 
316
  function Close()
317
    {
318
    fclose($this->m_fp);
319
    }
320
 
321
  /****************************************************************/
322
  /* purpose: send a http request to the host/port in constructor */
323
  /* input: HTTP_Req - request obj                                */
324
  /****************************************************************/
325
 
326
  function Request($HTTP_Req)
327
    {
328
    // send method + URI + HTTP_version
329
    $buffer = $HTTP_Req->GetMethod()." ".$HTTP_Req->GetURI().
330
              " ".$HTTP_Req->GetVersion().CRLF;
331
 
332
    // send headers
333
    $HTTP_Headers = $HTTP_Req->GetHeaders();
334
    reset($HTTP_Headers);
335
    while(list(, $HTTP_Header) = each($HTTP_Headers))
336
      $buffer .= $HTTP_Header->Get().CRLF;
337
    $buffer .= CRLF;
338
 
339
    // send entity body
340
    $body = $HTTP_Req->GetEntityBody();
341
    if(!empty($body))
342
      {
343
      $HTTP_Header = $HTTP_Req->GetHeader("Content-Length");
344
      $buffer .= substr($body, 0, $HTTP_Header->GetValue());
345
      }
346
    fwrite($this->m_fp, $buffer);
347
    }
348
 
349
  /**************************************************/
350
  /* purpose: passthru response to calling browser. */
351
  /* NB: auto-closes the connection to the host     */
352
  /**************************************************/
353
 
354
  function Passthru()
355
    {
356
    // read status line
357
    $statusLine = fgets($this->m_fp, RLEN);
358
    Header(trim($statusLine));
359
 
360
    // read in headers
361
    $row = '';
362
    while(!feof($this->m_fp))
363
      {
364
      $line = fgets($this->m_fp, RLEN);
365
      if($line == CRLF)
366
        {
367
        if(!empty($row))
368
          Header($row);
369
        break;
370
        }
371
      $header = trim($line);
372
      $pos = strpos($header, ':');
373
      if($pos === false)
374
        {
375
        if(!empty($row))
376
          $row .= " $header";
377
        }
378
      else
379
        {
380
        if(!empty($row))
381
          Header($row);
382
        $row = $header;
383
        $fieldName = substr($header, 0, $pos);
384
        $fieldValue = substr($header, $pos+2);
385
        if(!strcasecmp($fieldName, 'Content-Length'))
386
          $len = $fieldValue;
387
        }
388
      }
389
 
390
    // read in entity body (if there is one)
391
    if(!$len)
392
      $len = RLEN;
393
    while(!feof($this->m_fp))
394
      $body .= fread($this->m_fp, $len);
395
    if(0 < strlen($body))
396
      {
397
      echo $body;
398
      unset($body);
399
      }
400
 
401
    // close connection
402
    $this->Close();
403
    }
404
 
405
  /**************************************************/
406
  /* purpose: proxy req/resp for calling browser.   */
407
  /* input: HTTP_Req - request obj                  */
408
  /**************************************************/
409
 
410
  function Proxy($HTTP_Req)
411
    {
412
    $rc = $this->Connect();
413
    if(!$rc)
414
      {
415
      $this->Request($HTTP_Req);
416
//      fpassthru($this->m_fp);
417
      $this->Passthru();
418
      }
419
    return($rc);
420
    }
421
 
422
  /******************************************************/
423
  /* purpose: SSL proxy req/resp for calling browser.   */
424
  /* input: HTTP_Req - request obj                      */
425
  /******************************************************/
426
 
427
  function Proxy_SSL($HTTP_Req)
428
    {
429
    $ch = curl_init();
430
    curl_setopt($ch, CURLOPT_URL, 'https://'.$this->m_host.
431
                $HTTP_Req->GetURI());
432
    curl_setopt($ch, CURLOPT_TIMEOUT, $this->m_timeout);
433
 
434
    $HTTP_Headers = $HTTP_Req->GetHeaders();
435
    while(list(, $HTTP_Header) = each($HTTP_Headers))
436
      {
437
      if(!strcasecmp('User-Agent', $HTTP_Header->GetName()))
438
        $rc = curl_setopt($ch, CURLOPT_USERAGENT, $HTTP_Header->GetValue());
439
      elseif(!strcasecmp('Authorization', $HTTP_Header->GetName()))
440
        $rc = curl_setopt($ch, CURLOPT_USERPWD, $HTTP_Header->GetValue());
441
      elseif(!strcasecmp('Cookie', $HTTP_Header->GetName()))
442
        $rc = curl_setopt($ch, CURLOPT_COOKIE, $HTTP_Header->GetValue());
443
      elseif(stristr($HTTP_Header->GetName(), 'Accept'))
444
        $rc = curl_setopt($ch, CURLOPT_HTTPHEADER, $HTTP_Header->Get());
445
      }
446
 
447
    if('POST' == $HTTP_Req->GetMethod())
448
      {
449
      curl_setopt($ch, CURLOPT_POST, 1);
450
      curl_setopt($ch, CURLOPT_POSTFIELDS, $HTTP_Req->GetEntityBody());
451
      }
452
 
453
    curl_exec($ch);
454
    curl_close($ch);
455
    }
456
 
457
  /**************************************************/
458
  /* purpose: read a http response from the host    */
459
  /* output: HTTP_Response_hr - creates a response obj */
460
  /*         20020225: Expand gzipped response      */
461
  /**************************************************/
462
 
463
  function Response(&$HTTP_Response_hr)
464
    {
465
    // read status line
466
    $statusLine = fgets($this->m_fp, RLEN);
467
 
468
    $this->_SplitResponseLine($statusLine, $version, $statusCode, $phrase);
469
 
470
    // read in headers
471
    $tmp_array = array();
472
    while(!feof($this->m_fp))
473
      {
474
      $line = fgets($this->m_fp, RLEN);
475
      if($line == CRLF)
476
        break;
477
      $header = trim($line);
478
      $pos = strpos($header, ':');
479
      $fieldName = substr($header, 0, $pos);
480
      $fieldValue = substr($header, $pos+2);
481
      if(!strcasecmp($fieldName, 'Content-Length'))
482
        $len = $fieldValue;
483
      if(!strcasecmp($fieldName, 'Content-Encoding') &&
484
         stristr($fieldValue, 'gzip'))
485
        $type = 'gzip';
486
      $tmp_array["$fieldName"] = $fieldValue;
487
      }
488
 
489
    // read in entity body (if there is one)
490
    if(!$len)
491
      $len = RLEN;
492
    while(!feof($this->m_fp))
493
      $body .= fread($this->m_fp, $len);
494
 
495
    // Expand gzipped response
496
    if('gzip' == $type)
497
      $body = gzinflate(substr($body, 10));
498
 
499
    // create the response object
500
    $HTTP_Headers = array();
501
    while(list($k, $v) = each($tmp_array))
502
      {
503
      if('gzip' == $type && !strcasecmp($k, 'Content-Length'))
504
        $v = strlen($body);
505
      if('gzip' == $type && !strcasecmp($k, 'Content-Encoding'))
506
        continue;
507
      $HTTP_Headers[] = new HTTP_Header($k, $v);
508
      }
509
    $HTTP_Response_hr = new HTTP_Response_hr($version, $statusCode, $phrase,
510
                                       $HTTP_Headers, $body);
511
    }
512
 
513
  /********************************************************/
514
  /* purpose: splits up a header into its different parts */
515
  /********************************************************/
516
 
517
  function _SplitHeader($header, &$fieldName, &$fieldValue)
518
    {
519
    $header = str_replace(CRLF, "", $header);
520
    $pos = strpos($header, ':');
521
 
522
    $fieldName = substr($header, 0, $pos);
523
    $fieldValue = substr($header, $pos+2);
524
    }
525
 
526
  /***************************************************************/
527
  /* purpose: splits up a response line into its different parts */
528
  /***************************************************************/
529
 
530
  function _SplitResponseLine($line, &$version, &$statusCode, &$phrase)
531
    {
532
    $line = str_replace(CRLF, "", $line);
533
    $regs = split("[[:space:]]+", $line, 3);
534
 
535
    $version = $regs[0];
536
    $statusCode = $regs[1];
537
    $phrase = $regs[2];
538
    }
539
  };
540
 
541
/*************************************************/
542
/* class: HTTP_Cookie_Header extends HTTP_Header */
543
/* purpose: represents a http cookie header      */
544
/*************************************************/
545
 
546
class HTTP_Cookie_Header extends HTTP_Header
547
  {
548
  /*******************************************************************/
549
  /* constructor:                                                    */
550
  /* input: hash containing keys=>values to be stored in this cookie */
551
  /*******************************************************************/
552
 
553
  function HTTP_Cookie_Header($values)
554
    {
555
    while(list($k, $v) = each($values))
556
      $encValues .= $k."=".urlencode($v).";";
557
    $this->HTTP_Header("Cookie", $encValues);
558
    }
559
  };
560
 
561
/****************************************************/
562
/* class: HTTP_BasicAuth_Header extends HTTP_Header */
563
/* purpose: represents a http basic auth header     */
564
/****************************************************/
565
 
566
class HTTP_BasicAuth_Header extends HTTP_Header
567
  {
568
  /***************************************/
569
  /* constructor:                        */
570
  /* input: username - username for auth */
571
  /* input: password - password for auth */
572
  /***************************************/
573
 
574
  function HTTP_BasicAuth_Header($username, $password)
575
    {
576
    $this->HTTP_Header("Authorization",
577
                       "Basic ".base64_encode($username.":".$password));
578
    }
579
  };
580
 
581
/************************************************/
582
/* class: HTTP_GET_Request extends HTTP_Req     */
583
/* purpose: represents a http GET request       */
584
/************************************************/
585
 
586
class HTTP_GET_Request extends HTTP_Req
587
  {
588
  /***************************************************************/
589
  /* constructor:                                                */
590
  /* input: uri - uri to get                                     */
591
  /* input: HTTP_Headers - array of header objs for this request */
592
  /***************************************************************/
593
 
594
  function HTTP_GET_Request($uri, $HTTP_Headers = 0)
595
    {
596
    $this->HTTP_Req("GET", $uri, "HTTP/1.0", $HTTP_Headers);
597
    }
598
  };
599
 
600
/*************************************************/
601
/* class: HTTP_HEAD_Request extends HTTP_Req     */
602
/* purpose: represents a http HEAD request       */
603
/*************************************************/
604
 
605
class HTTP_HEAD_Request extends HTTP_Req
606
  {
607
  /***************************************************************/
608
  /* constructor:                                                */
609
  /* input: uri - uri to get headers for                         */
610
  /* input: HTTP_Headers - array of header objs for this request */
611
  /***************************************************************/
612
 
613
  function HTTP_HEAD_Request($uri, $HTTP_Headers = 0)
614
    {
615
    $this->HTTP_Req("HEAD", $uri, "HTTP/1.0", $HTTP_Headers);
616
    }
617
  };
618
 
619
/***********************************************************************/
620
/* class: HTTP_POST_Request extends HTTP_Req                           */
621
/* purpose: represent a POST http request.                             */
622
/* Headers (Content-Type, Content-Length) and entityBody sent for you. */
623
/***********************************************************************/
624
 
625
class HTTP_POST_Request extends HTTP_Req
626
  {
627
  /***************************************************************/
628
  /* constructor:                                                */
629
  /* input: uri - uri to post to                                 */
630
  /* input: postValues - associative array of unencoded post     */
631
  /*                     values to use                           */
632
  /* input: HTTP_Headers - array of header objs for this request */
633
  /***************************************************************/
634
 
635
  function HTTP_POST_Request($uri, $postValues, $HTTP_Headers)
636
    {
637
    while(list($key, $val) = each($postValues))
638
      $queryString .= urlencode($key)."=".urlencode($val)."&";
639
 
640
    $HTTP_Headers[] = new HTTP_Header("Content-Type",
641
                                      "application/x-www-form-urlencoded");
642
    $HTTP_Headers[] = new HTTP_Header("Content-Length", strlen($queryString));
643
 
644
    $this->HTTP_Req("POST", $uri, "HTTP/1.0", $HTTP_Headers, $queryString);
645
    }
646
  };
647
?>