includes/http.php
author Dan
Sun, 28 Mar 2010 21:39:37 -0400
changeset 1224 a54e149f4a78
parent 1135 56c0abbdad3e
child 1227 bdac73ed481e
permissions -rw-r--r--
Added https support in Request_HTTP
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
     1
<?php
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
     2
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
     3
/*
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
     4
 * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
1081
745200a9cc2a Fixed some upgrade bugs; added support for choosing one's own date/time formats; rebrand as 1.1.7
Dan
parents: 1034
diff changeset
     5
 * Copyright (C) 2006-2009 Dan Fuhry
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
     6
 * class_http.php - Pure PHP HTTP client library
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
     7
 *
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
     8
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
     9
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    10
 *
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    12
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    13
 */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    14
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    15
//
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    16
// HTTP status codes
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    17
//
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    18
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    19
// Informational
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    20
define('HTTP_CONTINUE', 100);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    21
define('HTTP_SWITCHING_PROTOCOLS', 101);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    22
define('HTTP_PROCESSING', 102);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    23
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    24
// Success
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    25
define('HTTP_OK', 200);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    26
define('HTTP_CREATED', 201);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    27
define('HTTP_ACCEPTED', 202);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    28
define('HTTP_NON_AUTHORITATIVE', 203);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    29
define('HTTP_NO_CONTENT', 204);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    30
define('HTTP_RESET_CONTENT', 205);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    31
define('HTTP_PARTIAL_CONTENT', 206);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    32
define('HTTP_MULTI_STATUS', 207);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    33
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    34
// Redirection
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    35
define('HTTP_MULTIPLE_CHOICES', 300);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    36
define('HTTP_MOVED_PERMANENTLY', 301);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    37
define('HTTP_FOUND', 302);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    38
define('HTTP_SEE_OTHER', 303);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    39
define('HTTP_NOT_MODIFIED', 304);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    40
define('HTTP_USE_PROXY', 305);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    41
define('HTTP_SWITCH_PROXY', 306);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    42
define('HTTP_TEMPORARY_REDIRECT', 307);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    43
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    44
// Client Error
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    45
define('HTTP_BAD_REQUEST', 400);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    46
define('HTTP_UNAUTHORIZED', 401);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    47
define('HTTP_PAYMENT_REQUIRED', 402);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    48
define('HTTP_FORBIDDEN', 403);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    49
define('HTTP_NOT_FOUND', 404);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    50
define('HTTP_METHOD_NOT_ALLOWED', 405);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    51
define('HTTP_NOT_ACCEPTABLE', 406);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    52
define('HTTP_PROXY_AUTHENTICATION_REQUIRED', 407);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    53
define('HTTP_REQUEST_TIMEOUT', 408);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    54
define('HTTP_CONFLICT', 409);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    55
define('HTTP_GONE', 410);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    56
define('HTTP_LENGTH_REQUIRED', 411);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    57
define('HTTP_PRECONDITION_FAILED', 412);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    58
define('HTTP_REQUEST_ENTITY_TOO_LARGE', 413);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    59
define('HTTP_REQUEST_URI_TOO_LONG', 414);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    60
define('HTTP_UNSUPPORTED_MEDIA_TYPE', 415);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    61
define('HTTP_REQUESTED_RANGE_NOT_SATISFIABLE', 416);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    62
define('HTTP_EXPECTATION_FAILED', 417);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    63
define('HTTP_UNPROCESSABLE_ENTITY', 422);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    64
define('HTTP_LOCKED', 423);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    65
define('HTTP_FAILED_DEPENDENCY', 424);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    66
define('HTTP_UNORDERED_COLLECTION', 425);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    67
define('HTTP_UPGRADE_REQUIRED', 426);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    68
define('HTTP_RETRY_WITH', 449);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    69
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    70
// Server error
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    71
define('HTTP_INTERNAL_SERVER_ERROR', 500);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    72
define('HTTP_NOT_IMPLEMENTED', 501);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    73
define('HTTP_BAD_GATEWAY', 502);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    74
define('HTTP_SERVICE_TEMPORARILY_UNAVAILABLE', 503);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    75
define('HTTP_GATEWAY_TIMEOUT', 504);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    76
define('HTTP_HTTP_VERSION_NOT_SUPPORTED', 505);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    77
define('HTTP_VARIANT_ALSO_NEGOTIATES', 506);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    78
define('HTTP_INSUFFICIENT_STORAGE', 507);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    79
define('HTTP_BANDWIDTH_LIMIT_EXCEEDED', 509);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    80
define('HTTP_NOT_EXTENDED', 510);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    81
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    82
/**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    83
 * Class for making HTTP requests. This can do GET and POST, and when used properly it consumes under a meg of memory, even with huge files.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    84
 * @package Enano
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    85
 * @subpackage Backend functions
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    86
 * @copyright 2007 Dan Fuhry
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    87
 */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    88
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    89
class Request_HTTP
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    90
{
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    91
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    92
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    93
   * Switch to enable or disable debugging. You want this off on production sites.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    94
   * @var bool
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    95
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    96
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    97
  var $debug = false;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    98
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
    99
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   100
   * The host the request will be sent to.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   101
   * @var string
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   102
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   103
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   104
  var $host = '';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   105
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   106
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   107
   * The TCP port our connection is (will be) on.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   108
   * @var int
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   109
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   110
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   111
  var $port = 80;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   112
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   113
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   114
   * The request method. Can be GET or POST, defaults to GET.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   115
   * @var string
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   116
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   117
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   118
  var $method = 'GET';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   119
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   120
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   121
   * The URI to the remote script.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   122
   * @var string
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   123
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   124
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   125
  var $uri = '';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   126
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   127
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   128
   * The parameters to be sent on GET.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   129
   * @var array (associative)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   130
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   131
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   132
  var $parms_get = array();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   133
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   134
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   135
   * The parameters to be sent on POST. Ignored if $this->method == GET.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   136
   * @var array (associative)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   137
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   138
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   139
  var $parms_post = array();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   140
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   141
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   142
   * The list of cookies that will be sent.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   143
   * @var array (associative)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   144
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   145
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   146
  var $cookies_out = array();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   147
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   148
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   149
   * Additional request headers.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   150
   * @var array (associative)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   151
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   152
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   153
  var $headers = array();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   154
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   155
  /**
1135
56c0abbdad3e Added a switch to disable following redirects in Request_HTTP
Dan
parents: 1113
diff changeset
   156
   * Follow server-side redirects; defaults to true.
56c0abbdad3e Added a switch to disable following redirects in Request_HTTP
Dan
parents: 1113
diff changeset
   157
   * @var bool
56c0abbdad3e Added a switch to disable following redirects in Request_HTTP
Dan
parents: 1113
diff changeset
   158
   */
56c0abbdad3e Added a switch to disable following redirects in Request_HTTP
Dan
parents: 1113
diff changeset
   159
  
56c0abbdad3e Added a switch to disable following redirects in Request_HTTP
Dan
parents: 1113
diff changeset
   160
  var $follow_redirects = true;
56c0abbdad3e Added a switch to disable following redirects in Request_HTTP
Dan
parents: 1113
diff changeset
   161
  
56c0abbdad3e Added a switch to disable following redirects in Request_HTTP
Dan
parents: 1113
diff changeset
   162
  /**
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   163
   * Cached response.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   164
   * @var string, or bool:false if the request hasn't been sent yet
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   165
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   166
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   167
  var $response = false;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   168
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   169
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   170
   * Cached response code
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   171
   * @var int set to -1 if request hasn't been sent yet
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   172
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   173
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   174
  var $response_code = -1;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   175
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   176
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   177
   * Cached response code string
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   178
   * @var string or bool:false if the request hasn't been sent yet
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   179
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   180
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   181
  var $response_string = false;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   182
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   183
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   184
   * Resource for the socket. False if a connection currently isn't going.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   185
   * @var resource
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   186
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   187
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   188
  var $socket = false;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   189
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   190
  /**
1224
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   191
   * True if SSL is on, defaults to false
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   192
   * @var bool
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   193
   */
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   194
  
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   195
  var $ssl = false;
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   196
  
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   197
  /**
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   198
   * The state of our request. 0 means it hasn't been made yet. 1 means the socket is open, 2 means the socket is open and the request has been written, 3 means the headers have been fetched, and 4 means the request is completed.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   199
   * @var int
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   200
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   201
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   202
  var $state = 0;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   203
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   204
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   205
   * Constructor.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   206
   * @param string Hostname to send to
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   207
   * @param string URI (/index.php)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   208
   * @param string Request method - GET or POST.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   209
   * @param int Optional. The port to open the request on. Defaults to 80.
1224
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   210
   * @param bool If true, uses SSL (and defaults the port to 443)
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   211
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   212
  
1224
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   213
  function Request_HTTP($host, $uri, $method = 'GET', $port = 'default', $ssl = false)
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   214
  {
1086
6a59951b70e4 Avatar UCP and ACP: Ported Dynano -> jQuery; merged POST processing code. Added support for IPv6 URLs (avatar UCP and RequestHTTP).
Dan
parents: 1081
diff changeset
   215
    if ( !preg_match('/^(?:(([a-z0-9-]+\.)*?)([a-z0-9-]+)|\[[a-f0-9:]+\])$/', $host) )
894
83d180d87f3c HTTP: now throws exceptions instead of flat-out dying
Dan
parents: 847
diff changeset
   216
      throw new Exception(__CLASS__ . ': Invalid hostname');
1224
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   217
    if ( $ssl )
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   218
    {
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   219
      $this->ssl = true;
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   220
      $port = ( $port === 'default' ) ? 443 : $port;
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   221
    }
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   222
    else
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   223
    {
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   224
      $this->ssl = false;
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   225
      $port = ( $port === 'default' ) ? 80 : $port;
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   226
    }
1086
6a59951b70e4 Avatar UCP and ACP: Ported Dynano -> jQuery; merged POST processing code. Added support for IPv6 URLs (avatar UCP and RequestHTTP).
Dan
parents: 1081
diff changeset
   227
    // Yes - this really does support IPv6 URLs!
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   228
    $this->host = $host;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   229
    $this->uri = $uri;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   230
    if ( is_int($port) && $port >= 1 && $port <= 65535 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   231
      $this->port = $port;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   232
    else
894
83d180d87f3c HTTP: now throws exceptions instead of flat-out dying
Dan
parents: 847
diff changeset
   233
      throw new Exception(__CLASS__ . ': Invalid port');
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   234
    $method = strtoupper($method);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   235
    if ( $method == 'GET' || $method == 'POST' )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   236
      $this->method = $method;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   237
    else
894
83d180d87f3c HTTP: now throws exceptions instead of flat-out dying
Dan
parents: 847
diff changeset
   238
      throw new Exception(__CLASS__ . ': Invalid request method');
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   239
      
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   240
    $newline = "\r\n";
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   241
    $php_ver = PHP_VERSION;
847
28d5049803f1 http: minor fix to tolerate missing SERVER_SOFTWARE
Dan
parents: 801
diff changeset
   242
    $server = ( isset($_SERVER['SERVER_SOFTWARE']) ) ? "Server: {$_SERVER['SERVER_SOFTWARE']}" : "CLI";
28d5049803f1 http: minor fix to tolerate missing SERVER_SOFTWARE
Dan
parents: 801
diff changeset
   243
    $this->add_header('User-Agent', "PHP/$php_ver ({$server}; automated bot request)");
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   244
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   245
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   246
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   247
   * Sets one or more cookies to be sent to the server.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   248
   * @param string or array If a string, the cookie name. If an array, associative array in the form of cookiename => cookievalue
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   249
   * @param string or bool If a string, the cookie value. If boolean, defaults to false, param 1 should be an array, and this should not be passed.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   250
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   251
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   252
  function add_cookie($cookiename, $cookievalue = false)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   253
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   254
    if ( is_array($cookiename) && !$cookievalue )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   255
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   256
      foreach ( $cookiename as $name => $value )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   257
      {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   258
        $this->cookies_out[$name] = $value;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   259
      }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   260
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   261
    else if ( is_string($cookiename) && is_string($cookievalue) )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   262
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   263
      $this->cookies_out[$cookiename] = $cookievalue;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   264
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   265
    else
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   266
    {
952
d52dfa1f08da Fixed some error display cosmetics in Request_HTTP
Dan
parents: 894
diff changeset
   267
      throw new Exception(__METHOD__ . ': Invalid argument(s)');
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   268
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   269
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   270
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   271
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   272
   * Sets one or more request header values.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   273
   * @param string or array If a string, the header name. If an array, associative array in the form of headername => headervalue
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   274
   * @param string or bool If a string, the header value. If boolean, defaults to false, param 1 should be an array, and this should not be passed.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   275
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   276
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   277
  function add_header($headername, $headervalue = false)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   278
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   279
    if ( is_array($headername) && !$headervalue )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   280
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   281
      foreach ( $headername as $name => $value )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   282
      {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   283
        $this->headers[$name] = $value;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   284
      }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   285
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   286
    else if ( is_string($headername) && is_string($headervalue) )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   287
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   288
      $this->headers[$headername] = $headervalue;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   289
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   290
    else
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   291
    {
952
d52dfa1f08da Fixed some error display cosmetics in Request_HTTP
Dan
parents: 894
diff changeset
   292
      throw new Exception(__METHOD__ . ': Invalid argument(s)');
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   293
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   294
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   295
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   296
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   297
   * Adds one or more values to be passed on GET.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   298
   * @param string or array If a string, the parameter name. If an array, associative array in the form of parametername => parametervalue
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   299
   * @param string or bool If a string, the parameter value. If boolean, defaults to false, param 1 should be an array, and this should not be passed.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   300
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   301
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   302
  function add_get($getname, $getvalue = false)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   303
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   304
    if ( is_array($getname) && !$getvalue )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   305
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   306
      foreach ( $getname as $name => $value )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   307
      {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   308
        $this->parms_get[$name] = $value;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   309
      }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   310
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   311
    else if ( is_string($getname) && is_string($getvalue) )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   312
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   313
      $this->parms_get[$getname] = $getvalue;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   314
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   315
    else
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   316
    {
952
d52dfa1f08da Fixed some error display cosmetics in Request_HTTP
Dan
parents: 894
diff changeset
   317
      throw new Exception(__METHOD__ . ': Invalid argument(s)');
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   318
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   319
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   320
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   321
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   322
   * Adds one or more values to be passed on POST.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   323
   * @param string or array If a string, the header name. If an array, associative array in the form of headername => headervalue
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   324
   * @param string or bool If a string, the header value. If boolean, defaults to false, param 1 should be an array, and this should not be passed.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   325
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   326
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   327
  function add_post($postname, $postvalue = false)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   328
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   329
    if ( is_array($postname) && !$postvalue )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   330
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   331
      foreach ( $postname as $name => $value )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   332
      {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   333
        $this->parms_post[$name] = $value;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   334
      }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   335
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   336
    else if ( is_string($postname) && is_string($postvalue) )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   337
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   338
      $this->parms_post[$postname] = $postvalue;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   339
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   340
    else
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   341
    {
952
d52dfa1f08da Fixed some error display cosmetics in Request_HTTP
Dan
parents: 894
diff changeset
   342
      throw new Exception(__METHOD__ . ': Invalid argument(s)');
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   343
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   344
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   345
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   346
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   347
   * Internal function to open up the socket.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   348
   * @access private
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   349
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   350
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   351
  function _sock_open(&$connection)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   352
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   353
    // Open connection
1224
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   354
    $ssl_prepend = ( $this->ssl ) ? 'ssl://' : '';
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   355
    $connection = fsockopen($ssl_prepend . $this->host, $this->port, $errno, $errstr);
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   356
    if ( !$connection )
1034
15fa1b4e730a Fixed onload_hooks[] being initted to null after runOnloadHooks() called (should now be initted to an array)
Dan
parents: 952
diff changeset
   357
      throw new Exception(__METHOD__ . ": Could not make connection"); // to {$this->host}:{$this->port}: error $errno: $errstr");
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   358
    
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   359
    // 1 = socket open
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   360
    $this->state = 1;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   361
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   362
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   363
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   364
   * Internal function to actually write the request into the socket.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   365
   * @access private
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   366
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   367
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   368
  function _write_request(&$connection, &$headers, &$cookies, &$get, &$post)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   369
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   370
    $newline = "\r\n";
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   371
    
349
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   372
    if ( $this->debug )
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   373
      echo '<p>Connection opened. Writing main request to socket. Raw socket data follows.</p><pre>';
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   374
    
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   375
    if ( $this->debug )
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   376
    {
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   377
      echo '<hr /><div style="white-space: nowrap;">';
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   378
      echo '<p><b>' . __CLASS__ . ': Sending request</b></p><p>Request parameters:</p>';
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   379
      echo "<p><b>Headers:</b></p><pre>$headers</pre>";
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   380
      echo "<p><b>Cookies:</b> $cookies</p>";
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   381
      echo "<p><b>GET URI:</b> " . htmlspecialchars($this->uri . $get) . "</p>";
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   382
      echo "<p><b>POST DATA:</b> " . htmlspecialchars($post) . "</p>";
354
979d99a0b00e A few more installer tweaks / fixes. Initted upgrade script. Added migration schema from 1.0 to 1.1.
Dan
parents: 349
diff changeset
   383
      echo "<pre>";
349
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   384
    }
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   385
    
462
42a49ceb2373 Minor standards-compliance fix in Request_HTTP
Dan
parents: 411
diff changeset
   386
    $portline = ( $this->port == 80 ) ? '' : ":$this->port";
42a49ceb2373 Minor standards-compliance fix in Request_HTTP
Dan
parents: 411
diff changeset
   387
    
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   388
    $this->_fputs($connection, "{$this->method} {$this->uri}{$get} HTTP/1.1{$newline}");
462
42a49ceb2373 Minor standards-compliance fix in Request_HTTP
Dan
parents: 411
diff changeset
   389
    $this->_fputs($connection, "Host: {$this->host}$portline{$newline}");
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   390
    $this->_fputs($connection, $headers);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   391
    $this->_fputs($connection, $cookies);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   392
    
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   393
    if ( $this->method == 'POST' )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   394
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   395
      // POST-specific parameters
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   396
      $post_length = strlen($post);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   397
      $this->_fputs($connection, "Content-type: application/x-www-form-urlencoded{$newline}");
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   398
      $this->_fputs($connection, "Content-length: {$post_length}{$newline}");
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   399
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   400
    
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   401
    $this->_fputs($connection, "Connection: close{$newline}");
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   402
    $this->_fputs($connection, "{$newline}");
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   403
    
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   404
    if ( $this->method == 'POST' )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   405
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   406
      $this->_fputs($connection, $post);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   407
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   408
    
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   409
    if ( $this->debug )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   410
      echo '</pre><p>Request written. Fetching response.</p>';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   411
    
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   412
    // 2 = request written
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   413
    $this->state = 2;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   414
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   415
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   416
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   417
   * Wrap up and close the socket. Nothing more than a call to fsockclose() except in debug mode.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   418
   * @access private
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   419
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   420
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   421
  function sock_close(&$connection)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   422
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   423
    if ( $this->debug )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   424
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   425
      echo '<p>Response fetched. Closing connection. Response text follows.</p><pre>';
349
fdaf9070566c More progress on the installer. At this point it can install and import the language, but does not rename config files. Still much work to be done, most notably localization and creation of MySQL users and databases.
Dan
parents: 328
diff changeset
   426
      echo htmlspecialchars($this->response);
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   427
      echo '</pre></div><hr />';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   428
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   429
    
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   430
    fclose($connection);
1224
a54e149f4a78 Added https support in Request_HTTP
Dan
parents: 1135
diff changeset
   431
    $this->state = 0;
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   432
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   433
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   434
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   435
   * Internal function to grab the response code and status string
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   436
   * @access string
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   437
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   438
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   439
  function _parse_response_code($buffer)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   440
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   441
    // Retrieve response code and status
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   442
    $pos_newline = strpos($buffer, "\n");
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   443
    $pos_carriage_return = strpos($buffer, "\r");
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   444
    $pos_end_first_line = ( $pos_carriage_return < $pos_newline && $pos_carriage_return > 0 ) ? $pos_carriage_return : $pos_newline;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   445
    
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   446
    // First line is in format of:
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   447
    // HTTP/1.1 ### Blah blah blah(\r?)\n
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   448
    $response_code = substr($buffer, 9, 3);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   449
    $response_string = substr($buffer, 13, ( $pos_end_first_line - 13 ) );
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   450
    $this->response_code = intval($response_code);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   451
    $this->response_string = $response_string;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   452
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   453
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   454
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   455
   * Internal function to send the request.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   456
   * @access private
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   457
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   458
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   459
  function _send_request()
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   460
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   461
    $this->concat_headers($headers, $cookies, $get, $post);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   462
    
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   463
    if ( $this->state < 1 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   464
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   465
      $this->_sock_open($this->socket);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   466
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   467
    if ( $this->state < 2 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   468
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   469
      $this->_write_request($this->socket, $headers, $cookies, $get, $post);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   470
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   471
    if ( $this->state == 2 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   472
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   473
      $buffer = $this->_read_until_newlines($this->socket);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   474
      $this->state = 3;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   475
      $this->_parse_response_code($buffer);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   476
      $this->response = $buffer;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   477
    }
1113
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   478
    // obey redirects
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   479
    $i = 0;
1135
56c0abbdad3e Added a switch to disable following redirects in Request_HTTP
Dan
parents: 1113
diff changeset
   480
    while ( $i < 20 && $this->follow_redirects )
1113
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   481
    {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   482
      $incoming_headers = $this->get_response_headers_array();
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   483
      if ( !$incoming_headers )
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   484
        break;
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   485
      if ( isset($incoming_headers['Location']) )
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   486
      {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   487
        // we've been redirected...
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   488
        $new_uri = $this->_resolve_uri($incoming_headers['Location']);
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   489
        if ( !$new_uri )
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   490
        {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   491
          // ... bad URI, ignore Location header.
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   492
          break;
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   493
        }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   494
        // change location
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   495
        $this->host = $new_uri['host'];
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   496
        $this->port = $new_uri['port'];
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   497
        $this->uri  = $new_uri['uri'];
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   498
        $get = '';
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   499
        
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   500
        // reset
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   501
        $this->sock_close($this->socket);
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   502
        $this->_sock_open($this->socket);
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   503
        $this->_write_request($this->socket, $headers, $cookies, $get, $post);
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   504
        $buffer = $this->_read_until_newlines($this->socket);
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   505
        $this->state = 3;
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   506
        $this->_parse_response_code($buffer);
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   507
        $this->response = $buffer;
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   508
        $i++;
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   509
      }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   510
      else
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   511
      {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   512
        break;
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   513
      }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   514
    }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   515
    if ( $i == 20 )
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   516
    {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   517
      throw new Exception(__METHOD__ . ": Redirect trap. Request_HTTP doesn't do cookies, btw.");
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   518
    }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   519
    
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   520
    if ( $this->state == 3 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   521
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   522
      // Determine transfer encoding
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   523
      $is_chunked = preg_match("/Transfer-Encoding: (chunked)\r?\n/", $this->response);
1113
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   524
      if ( preg_match("/^Content-Length: ([0-9]+)[\s]*$/mi", $this->response, $match) && !$is_chunked )
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   525
      {
1113
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   526
        $size = intval($match[1]);
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   527
        if ( $this->debug )
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   528
        {
1113
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   529
          echo "Pulling response using fread(), size $size\n";
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   530
        }
1113
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   531
        $this->response .= fread($this->socket, $size);
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   532
      }
1113
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   533
      else
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   534
      {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   535
        if ( $this->debug )
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   536
          echo "Pulling response using chunked handler\n";
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   537
          
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   538
        $buffer = '';
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   539
        while ( !feof($this->socket) )
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   540
        {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   541
          $part = fgets($this->socket, 1024);
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   542
          if ( $is_chunked && preg_match("/^([a-f0-9]+)\x0D\x0A$/", $part, $match) )
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   543
          {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   544
            $chunklen = hexdec($match[1]);
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   545
            $part = ( $chunklen > 0 ) ? fread($this->socket, $chunklen) : '';
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   546
            // remove the last newline from $part
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   547
            $part = preg_replace("/\r?\n\$/", "", $part);
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   548
          }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   549
          $buffer .= $part;
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   550
        }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   551
        $this->response .= $buffer;
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   552
      }
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   553
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   554
    $this->state = 4;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   555
    
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   556
    $this->sock_close($this->socket);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   557
    $this->socket = false;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   558
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   559
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   560
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   561
   * Internal function to send the request but only fetch the headers. Leaves a connection open for a finish-up function.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   562
   * @access private
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   563
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   564
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   565
  function _send_request_headers_only()
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   566
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   567
    $this->concat_headers($headers, $cookies, $get, $post);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   568
    
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   569
    if ( $this->state < 1 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   570
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   571
      $this->_sock_open($this->socket);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   572
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   573
    if ( $this->state < 2 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   574
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   575
      $this->_write_request($this->socket, $headers, $cookies, $get, $post);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   576
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   577
    if ( $this->state == 2 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   578
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   579
      $buffer = $this->_read_until_newlines($this->socket);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   580
      $this->state = 3;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   581
      $this->_parse_response_code($buffer);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   582
      $this->response = $buffer;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   583
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   584
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   585
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   586
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   587
   * Internal function to read from a socket until two consecutive newlines are hit.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   588
   * @access private
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   589
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   590
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   591
  function _read_until_newlines($sock)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   592
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   593
    $prev_char = '';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   594
    $prev1_char = '';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   595
    $prev2_char = '';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   596
    $buf = '';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   597
    while ( !feof($sock) )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   598
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   599
      $chr = fread($sock, 1);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   600
      $buf .= $chr;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   601
      if ( ( $chr == "\n" && $prev_char == "\n" ) ||
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   602
           ( $chr == "\n" && $prev_char == "\r" && $prev1_char == "\n" && $prev2_char == "\r" ) )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   603
      {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   604
        return $buf;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   605
      }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   606
      $prev2_char = $prev1_char;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   607
      $prev1_char = $prev_char;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   608
      $prev_char = $chr;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   609
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   610
    return $buf;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   611
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   612
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   613
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   614
   * Returns the response text. If the request hasn't been sent, it will be sent here.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   615
   * @return string
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   616
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   617
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   618
  function get_response()
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   619
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   620
    if ( $this->state == 4 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   621
      return $this->response;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   622
    $this->_send_request();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   623
    return $this->response;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   624
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   625
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   626
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   627
   * Writes the response body to a file. This is good for conserving memory when downloading large files. If the file already exists it will be overwritten.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   628
   * @param string File to write to
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   629
   * @param int Chunk size in KB to read from the socket. Optional and should only be needed in circumstances when extreme memory conservation is needed. Defaults to 768.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   630
   * @param int Maximum file size. Defaults to 0, which means no limit.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   631
   * @return bool True on success, false on failure
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   632
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   633
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   634
  function write_response_to_file($file, $chunklen = 768, $max_file_size = 0)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   635
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   636
    if ( !is_writeable( dirname($file) ) || !file_exists( dirname($file) ) )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   637
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   638
      return false;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   639
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   640
    $handle = @fopen($file, 'w');
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   641
    if ( !$handle )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   642
      return false;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   643
    $chunklen = intval($chunklen);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   644
    if ( $chunklen < 1 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   645
      return false;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   646
    if ( $this->state == 4 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   647
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   648
      // we already have the response, so cheat
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   649
      $response = $this->get_response_body();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   650
      fwrite($handle, $response);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   651
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   652
    else
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   653
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   654
      // read data from the socket, write it immediately, and unset to free memory
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   655
      $headers = $this->get_response_headers();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   656
      $transferred_bytes = 0;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   657
      $bandwidth_exceeded = false;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   658
      // if transfer-encoding is chunked, read using chunk sizes the server specifies
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   659
      $is_chunked = preg_match("/Transfer-Encoding: (chunked)\r?\n/", $this->response);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   660
      if ( $is_chunked )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   661
      {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   662
        $buffer = '';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   663
        while ( !feof($this->socket) )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   664
        {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   665
          $part = fgets($this->socket, ( 1024 * $chunklen ));
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   666
          // Theoretically if the encoding is really chunked then this should always match.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   667
          if ( $is_chunked && preg_match("/^([a-f0-9]+)\x0D\x0A$/", $part, $match) )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   668
          {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   669
            $chunk_length = hexdec($match[1]);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   670
            $part = ( $chunk_length > 0 ) ? fread($this->socket, $chunk_length) : '';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   671
            // remove the last newline from $part
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   672
            $part = preg_replace("/\r?\n\$/m", "", $part);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   673
          }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   674
          
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   675
          $transferred_bytes += strlen($part);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   676
          if ( $max_file_size && $transferred_bytes > $max_file_size )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   677
          {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   678
            // truncate output to $max_file_size bytes
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   679
            $partlen = $max_file_size - ( $transferred_bytes - strlen($part) );
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   680
            $part = substr($part, 0, $partlen);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   681
            $bandwidth_exceeded = true;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   682
          }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   683
          fwrite($handle, $part);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   684
          if ( $bandwidth_exceeded )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   685
          {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   686
            break;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   687
          }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   688
        }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   689
      }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   690
      else
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   691
      {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   692
        $first_chunk = fread($this->socket, ( 1024 * $chunklen ));
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   693
        fwrite($handle, $first_chunk);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   694
        while ( !feof($this->socket) )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   695
        {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   696
          $chunk = fread($this->socket, ( 1024 * $chunklen ));
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   697
          
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   698
          $transferred_bytes += strlen($chunk);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   699
          if ( $max_file_size && $transferred_bytes > $max_file_size )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   700
          {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   701
            // truncate output to $max_file_size bytes
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   702
            $partlen = $max_file_size - ( $transferred_bytes - strlen($chunk) );
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   703
            $chunk = substr($chunk, 0, $partlen);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   704
            $bandwidth_exceeded = true;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   705
          }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   706
          
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   707
          fwrite($handle, $chunk);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   708
          unset($chunk);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   709
          
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   710
          if ( $bandwidth_exceeded )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   711
          {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   712
            break;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   713
          }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   714
        }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   715
      }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   716
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   717
    fclose($handle);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   718
    // close socket and reset state, since we haven't cached the response
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   719
    $this->sock_close($this->socket);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   720
    $this->state = 0;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   721
    return ($bandwidth_exceeded) ? false : true;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   722
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   723
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   724
  /**
1113
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   725
   * Resolves, based on current settings and URI, a URI string to an array consisting of a host, port, and new URI. Returns false on error.
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   726
   * @param string
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   727
   * @return array
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   728
   */
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   729
  
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   730
  function _resolve_uri($uri)
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   731
  {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   732
    // long ass regexp w00t
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   733
    if ( !preg_match('#^(?:https?://((?:(?:[a-z0-9-]+\.)*)(?:[a-z0-9-]+)|\[[a-f0-9:]+\])(?::([0-9]+))?)?(/)(.*)$#i', $uri, $match) )
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   734
    {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   735
      // bad target URI
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   736
      return false;
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   737
    }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   738
    $hostpart = $match[1];
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   739
    if ( empty($hostpart) )
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   740
    {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   741
      // use existing host
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   742
      $host = $this->host;
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   743
      $port = $this->port;
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   744
    }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   745
    else
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   746
    {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   747
      $host = $match[1];
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   748
      $port = empty($match[2]) ? 80 : intval($match[2]);
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   749
    }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   750
    // is this an absolute URI, or relative?
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   751
    if ( empty($match[3]) )
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   752
    {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   753
      // relative
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   754
      $uri = dirname($this->uri) . $match[4];
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   755
    }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   756
    else
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   757
    {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   758
      // absolute
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   759
      $uri = '/' . $match[4];
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   760
    }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   761
    return array(
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   762
        'host' => $host,
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   763
        'port' => $port,
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   764
        'uri'  => $uri
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   765
      );
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   766
  }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   767
  
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   768
  /**
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   769
   * Returns only the response headers.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   770
   * @return string
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   771
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   772
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   773
  function get_response_headers()
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   774
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   775
    if ( $this->state == 3 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   776
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   777
      return $this->response;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   778
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   779
    else if ( $this->state == 4 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   780
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   781
      $pos_end = strpos($this->response, "\r\n\r\n");
1113
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   782
      if ( empty($pos_end) )
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   783
      {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   784
        $pos_end = strpos($this->response, "\n\n");
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   785
      }
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   786
      $data = substr($this->response, 0, $pos_end);
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   787
      return $data;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   788
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   789
    else
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   790
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   791
      $this->_send_request_headers_only();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   792
      return $this->response;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   793
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   794
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   795
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   796
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   797
   * Returns only the response headers, as an associative array.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   798
   * @return array
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   799
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   800
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   801
  function get_response_headers_array()
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   802
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   803
    $data = $this->get_response_headers();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   804
    preg_match_all("/(^|\n)([A-z0-9_-]+?): (.+?)(\r|\n|\$)/", $data, $matches);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   805
    $headers = array();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   806
    for ( $i = 0; $i < count($matches[0]); $i++ )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   807
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   808
      $headers[ $matches[2][$i] ] = $matches[3][$i];
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   809
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   810
    return $headers;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   811
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   812
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   813
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   814
   * Returns only the body (not the headers) of the response. If the request hasn't been sent, it will be sent here.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   815
   * @return string
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   816
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   817
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   818
  function get_response_body()
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   819
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   820
    $data = $this->get_response();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   821
    $pos_start = strpos($data, "\r\n\r\n") + 4;
1113
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   822
    if ( $pos_start == 4 )
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   823
    {
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   824
      $pos_start = strpos($data, "\n\n") + 4;
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   825
    }
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   826
    $data = substr($data, $pos_start);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   827
    return $data;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   828
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   829
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   830
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   831
   * Returns all cookies requested to be set by the server as an associative array. If the request hasn't been sent, it will be sent here.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   832
   * @return array
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   833
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   834
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   835
  function get_cookies()
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   836
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   837
    $data = $this->get_response();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   838
    $data = str_replace("\r\n", "\n", $data);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   839
    $pos = strpos($data, "\n\n");
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   840
    $headers = substr($data, 0, $pos);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   841
    preg_match_all("/Set-Cookie: ([a-z0-9_]+)=([^;]+);( expires=([^;]+);)?( path=(.*?))?\n/", $headers, $cookiematch);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   842
    if ( count($cookiematch[0]) < 1 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   843
      return array();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   844
    $cookies = array();
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   845
    foreach ( $cookiematch[0] as $i => $cookie )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   846
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   847
      $cookies[$cookiematch[1][$i]] = $cookiematch[2][$i];
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   848
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   849
    return $cookies;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   850
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   851
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   852
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   853
   * Internal method to write data to a socket with debugging possibility.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   854
   * @access private
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   855
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   856
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   857
  function _fputs($socket, $data)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   858
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   859
    if ( $this->debug )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   860
      echo htmlspecialchars($data);
1113
000791abdc7e Request_HTTP: Fixed get_response_body(), added HTTP redirect support, and added support for Content-Length.
Dan
parents: 1086
diff changeset
   861
    
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   862
    return fputs($socket, $data);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   863
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   864
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   865
  /**
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   866
   * Internal function to stringify cookies, headers, get, and post.
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   867
   * @access private
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   868
   */
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   869
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   870
  function concat_headers(&$headers, &$cookies, &$get, &$post)
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   871
  {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   872
    $headers = '';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   873
    $cookies = '';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   874
    foreach ( $this->headers as $name => $value )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   875
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   876
      $value = str_replace('\\n', '\\\\n', $value);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   877
      $value = str_replace("\n", '\\n', $value);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   878
      $headers .= "$name: $value\r\n";
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   879
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   880
    unset($value);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   881
    if ( count($this->cookies_out) > 0 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   882
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   883
      $i = 0;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   884
      $cookie_header = 'Cookie: ';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   885
      foreach ( $this->cookies_out as $name => $value )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   886
      {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   887
        $i++;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   888
        if ( $i > 1 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   889
          $cookie_header .= '; ';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   890
        $value = str_replace(';', rawurlencode(';'), $value);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   891
        $value = str_replace('\\n', '\\\\n', $value);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   892
        $value = str_replace("\n", '\\n', $value);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   893
        $cookie_header .= "$name=$value";
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   894
      }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   895
      $cookie_header .= "\r\n";
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   896
      $cookies = $cookie_header;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   897
      unset($value, $cookie_header);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   898
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   899
    if ( count($this->parms_get) > 0 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   900
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   901
      $get = '?';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   902
      $i = 0;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   903
      foreach ( $this->parms_get as $name => $value )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   904
      {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   905
        $i++;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   906
        if ( $i > 1 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   907
          $get .= '&';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   908
        $value = urlencode($value);
462
42a49ceb2373 Minor standards-compliance fix in Request_HTTP
Dan
parents: 411
diff changeset
   909
        if ( !empty($value) || is_string($value) )
328
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   910
          $get .= "$name=$value";
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   911
        else
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   912
          $get .= "$name";
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   913
      }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   914
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   915
    if ( count($this->parms_post) > 0 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   916
    {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   917
      $post = '';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   918
      $i = 0;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   919
      foreach ( $this->parms_post as $name => $value )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   920
      {
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   921
        $i++;
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   922
        if ( $i > 1 )
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   923
          $post .= '&';
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   924
        $value = urlencode($value);
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   925
        $post .= "$name=$value";
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   926
      }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   927
    }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   928
  }
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   929
  
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   930
}
dc838fd61a06 Added initial avatar support. Currently rather feature complete except for admin controls for avatar.
Dan
parents:
diff changeset
   931