|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00012 require_once(dirname(__FILE__) . '/socket.php'); 00020 class SimpleEncodedPair { 00021 var $_key; 00022 var $_value; 00023 00029 function SimpleEncodedPair($key, $value) { 00030 $this->_key = $key; 00031 $this->_value = $value; 00032 } 00033 00039 function asRequest() { 00040 return urlencode($this->_key) . '=' . urlencode($this->_value); 00041 } 00042 00048 function asMime() { 00049 $part = 'Content-Disposition: form-data; '; 00050 $part .= "name=\"" . $this->_key . "\"\r\n"; 00051 $part .= "\r\n" . $this->_value; 00052 return $part; 00053 } 00054 00061 function isKey($key) { 00062 return $key == $this->_key; 00063 } 00064 00070 function getKey() { 00071 return $this->_key; 00072 } 00073 00079 function getValue() { 00080 return $this->_value; 00081 } 00082 } 00083 00089 class SimpleAttachment { 00090 var $_key; 00091 var $_content; 00092 var $_filename; 00093 00100 function SimpleAttachment($key, $content, $filename) { 00101 $this->_key = $key; 00102 $this->_content = $content; 00103 $this->_filename = $filename; 00104 } 00105 00111 function asRequest() { 00112 return ''; 00113 } 00114 00120 function asMime() { 00121 $part = 'Content-Disposition: form-data; '; 00122 $part .= 'name="' . $this->_key . '"; '; 00123 $part .= 'filename="' . $this->_filename . '"'; 00124 $part .= "\r\nContent-Type: " . $this->_deduceMimeType(); 00125 $part .= "\r\n\r\n" . $this->_content; 00126 return $part; 00127 } 00128 00135 function _deduceMimeType() { 00136 if ($this->_isOnlyAscii($this->_content)) { 00137 return 'text/plain'; 00138 } 00139 return 'application/octet-stream'; 00140 } 00141 00147 function _isOnlyAscii($ascii) { 00148 for ($i = 0, $length = strlen($ascii); $i < $length; $i++) { 00149 if (ord($ascii[$i]) > 127) { 00150 return false; 00151 } 00152 } 00153 return true; 00154 } 00155 00162 function isKey($key) { 00163 return $key == $this->_key; 00164 } 00165 00171 function getKey() { 00172 return $this->_key; 00173 } 00174 00180 function getValue() { 00181 return $this->_filename; 00182 } 00183 } 00184 00191 class SimpleEncoding { 00192 var $_request; 00193 00201 function SimpleEncoding($query = false) { 00202 if (! $query) { 00203 $query = array(); 00204 } 00205 $this->clear(); 00206 $this->merge($query); 00207 } 00208 00213 function clear() { 00214 $this->_request = array(); 00215 } 00216 00223 function add($key, $value) { 00224 if ($value === false) { 00225 return; 00226 } 00227 if (is_array($value)) { 00228 foreach ($value as $item) { 00229 $this->_addPair($key, $item); 00230 } 00231 } else { 00232 $this->_addPair($key, $value); 00233 } 00234 } 00235 00242 function _addPair($key, $value) { 00243 $this->_request[] = new SimpleEncodedPair($key, $value); 00244 } 00245 00254 function attach($key, $content, $filename) { 00255 $this->_request[] = new SimpleAttachment($key, $content, $filename); 00256 } 00257 00264 function merge($query) { 00265 if (is_object($query)) { 00266 $this->_request = array_merge($this->_request, $query->getAll()); 00267 } elseif (is_array($query)) { 00268 foreach ($query as $key => $value) { 00269 $this->add($key, $value); 00270 } 00271 } 00272 } 00273 00281 function getValue($key) { 00282 $values = array(); 00283 foreach ($this->_request as $pair) { 00284 if ($pair->isKey($key)) { 00285 $values[] = $pair->getValue(); 00286 } 00287 } 00288 if (count($values) == 0) { 00289 return false; 00290 } elseif (count($values) == 1) { 00291 return $values[0]; 00292 } else { 00293 return $values; 00294 } 00295 } 00296 00302 function getAll() { 00303 return $this->_request; 00304 } 00305 00312 function _encode() { 00313 $statements = array(); 00314 foreach ($this->_request as $pair) { 00315 if ($statement = $pair->asRequest()) { 00316 $statements[] = $statement; 00317 } 00318 } 00319 return implode('&', $statements); 00320 } 00321 } 00322 00329 class SimpleGetEncoding extends SimpleEncoding { 00330 00338 function SimpleGetEncoding($query = false) { 00339 $this->SimpleEncoding($query); 00340 } 00341 00347 function getMethod() { 00348 return 'GET'; 00349 } 00350 00356 function writeHeadersTo(&$socket) { 00357 } 00358 00365 function writeTo(&$socket) { 00366 } 00367 00374 function asUrlRequest() { 00375 return $this->_encode(); 00376 } 00377 } 00378 00384 class SimpleHeadEncoding extends SimpleGetEncoding { 00385 00393 function SimpleHeadEncoding($query = false) { 00394 $this->SimpleGetEncoding($query); 00395 } 00396 00402 function getMethod() { 00403 return 'HEAD'; 00404 } 00405 } 00406 00413 class SimplePostEncoding extends SimpleEncoding { 00414 00422 function SimplePostEncoding($query = false) { 00423 if (is_array($query) and $this->hasMoreThanOneLevel($query)) { 00424 $query = $this->rewriteArrayWithMultipleLevels($query); 00425 } 00426 $this->SimpleEncoding($query); 00427 } 00428 00429 function hasMoreThanOneLevel($query) { 00430 foreach ($query as $key => $value) { 00431 if (is_array($value)) { 00432 return true; 00433 } 00434 } 00435 return false; 00436 } 00437 00438 function rewriteArrayWithMultipleLevels($query) { 00439 $query_ = array(); 00440 foreach ($query as $key => $value) { 00441 if (is_array($value)) { 00442 foreach ($value as $sub_key => $sub_value) { 00443 $query_[$key."[".$sub_key."]"] = $sub_value; 00444 } 00445 } else { 00446 $query_[$key] = $value; 00447 } 00448 } 00449 if ($this->hasMoreThanOneLevel($query_)) { 00450 $query_ = $this->rewriteArrayWithMultipleLevels($query_); 00451 } 00452 00453 return $query_; 00454 } 00455 00456 00462 function getMethod() { 00463 return 'POST'; 00464 } 00465 00471 function writeHeadersTo(&$socket) { 00472 $socket->write("Content-Length: " . (integer)strlen($this->_encode()) . "\r\n"); 00473 $socket->write("Content-Type: application/x-www-form-urlencoded\r\n"); 00474 } 00475 00481 function writeTo(&$socket) { 00482 $socket->write($this->_encode()); 00483 } 00484 00491 function asUrlRequest() { 00492 return ''; 00493 } 00494 } 00495 00502 class SimpleMultipartEncoding extends SimplePostEncoding { 00503 var $_boundary; 00504 00512 function SimpleMultipartEncoding($query = false, $boundary = false) { 00513 $this->SimplePostEncoding($query); 00514 $this->_boundary = ($boundary === false ? uniqid('st') : $boundary); 00515 } 00516 00522 function writeHeadersTo(&$socket) { 00523 $socket->write("Content-Length: " . (integer)strlen($this->_encode()) . "\r\n"); 00524 $socket->write("Content-Type: multipart/form-data, boundary=" . $this->_boundary . "\r\n"); 00525 } 00526 00532 function writeTo(&$socket) { 00533 $socket->write($this->_encode()); 00534 } 00535 00542 function _encode() { 00543 $stream = ''; 00544 foreach ($this->_request as $pair) { 00545 $stream .= "--" . $this->_boundary . "\r\n"; 00546 $stream .= $pair->asMime() . "\r\n"; 00547 } 00548 $stream .= "--" . $this->_boundary . "--\r\n"; 00549 return $stream; 00550 } 00551 } 00552 ?>