· 7 years ago · Jul 15, 2018, 04:46 PM
1<?php
2// vim: foldmethod=marker
3
4ini_set('display_errors', 'off'); // エラー表示ã•ã›ãªã„よã†ã«ã—ã¨ã
5
6/* Generic exception class
7 */
8class OAuthException extends Exception {/*{{{*/
9 // pass
10}/*}}}*/
11
12class OAuthConsumer {/*{{{*/
13 public $key;
14 public $secret;
15
16 function __construct($key, $secret, $callback_url=NULL) {/*{{{*/
17 $this->key = $key;
18 $this->secret = $secret;
19 $this->callback_url = $callback_url;
20 }/*}}}*/
21
22 function __toString() {/*{{{*/
23 return "OAuthConsumer[key=$this->key,secret=$this->secret]";
24 }/*}}}*/
25}/*}}}*/
26
27class OAuthToken {/*{{{*/
28 // access tokens and request tokens
29 public $key;
30 public $secret;
31
32 /**
33 * key = the token
34 * secret = the token secret
35 */
36 function __construct($key, $secret) {/*{{{*/
37 $this->key = $key;
38 $this->secret = $secret;
39 }/*}}}*/
40
41 /**
42 * generates the basic string serialization of a token that a server
43 * would respond to request_token and access_token calls with
44 */
45 function to_string() {/*{{{*/
46 return "oauth_token=" . OAuthUtil::urlencode_rfc3986($this->key) .
47 "&oauth_token_secret=" . OAuthUtil::urlencode_rfc3986($this->secret);
48 }/*}}}*/
49
50 function __toString() {/*{{{*/
51 return $this->to_string();
52 }/*}}}*/
53}/*}}}*/
54
55class OAuthSignatureMethod {/*{{{*/
56 public function check_signature(&$request, $consumer, $token, $signature) {
57 $built = $this->build_signature($request, $consumer, $token);
58 return $built == $signature;
59 }
60}/*}}}*/
61
62class OAuthSignatureMethod_HMAC_SHA1 extends OAuthSignatureMethod {/*{{{*/
63 function get_name() {/*{{{*/
64 return "HMAC-SHA1";
65 }/*}}}*/
66
67 public function build_signature($request, $consumer, $token) {/*{{{*/
68 $base_string = $request->get_signature_base_string();
69 $request->base_string = $base_string;
70
71 $key_parts = array(
72 $consumer->secret,
73 ($token) ? $token->secret : ""
74 );
75
76 $key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
77 $key = implode('&', $key_parts);
78
79 return base64_encode( hash_hmac('sha1', $base_string, $key, true));
80 }/*}}}*/
81}/*}}}*/
82
83class OAuthSignatureMethod_PLAINTEXT extends OAuthSignatureMethod {/*{{{*/
84 public function get_name() {/*{{{*/
85 return "PLAINTEXT";
86 }/*}}}*/
87
88 public function build_signature($request, $consumer, $token) {/*{{{*/
89 $sig = array(
90 OAuthUtil::urlencode_rfc3986($consumer->secret)
91 );
92
93 if ($token) {
94 array_push($sig, OAuthUtil::urlencode_rfc3986($token->secret));
95 } else {
96 array_push($sig, '');
97 }
98
99 $raw = implode("&", $sig);
100 // for debug purposes
101 $request->base_string = $raw;
102
103 return OAuthUtil::urlencode_rfc3986($raw);
104 }/*}}}*/
105}/*}}}*/
106
107class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod {/*{{{*/
108 public function get_name() {/*{{{*/
109 return "RSA-SHA1";
110 }/*}}}*/
111
112 protected function fetch_public_cert(&$request) {/*{{{*/
113 // not implemented yet, ideas are:
114 // (1) do a lookup in a table of trusted certs keyed off of consumer
115 // (2) fetch via http using a url provided by the requester
116 // (3) some sort of specific discovery code based on request
117 //
118 // either way should return a string representation of the certificate
119 throw Exception("fetch_public_cert not implemented");
120 }/*}}}*/
121
122 protected function fetch_private_cert(&$request) {/*{{{*/
123 // not implemented yet, ideas are:
124 // (1) do a lookup in a table of trusted certs keyed off of consumer
125 //
126 // either way should return a string representation of the certificate
127 throw Exception("fetch_private_cert not implemented");
128 }/*}}}*/
129
130 public function build_signature(&$request, $consumer, $token) {/*{{{*/
131 $base_string = $request->get_signature_base_string();
132 $request->base_string = $base_string;
133
134 // Fetch the private key cert based on the request
135 $cert = $this->fetch_private_cert($request);
136
137 // Pull the private key ID from the certificate
138 $privatekeyid = openssl_get_privatekey($cert);
139
140 // Sign using the key
141 $ok = openssl_sign($base_string, $signature, $privatekeyid);
142
143 // Release the key resource
144 openssl_free_key($privatekeyid);
145
146 return base64_encode($signature);
147 } /*}}}*/
148
149 public function check_signature(&$request, $consumer, $token, $signature) {/*{{{*/
150 $decoded_sig = base64_decode($signature);
151
152 $base_string = $request->get_signature_base_string();
153
154 // Fetch the public key cert based on the request
155 $cert = $this->fetch_public_cert($request);
156
157 // Pull the public key ID from the certificate
158 $publickeyid = openssl_get_publickey($cert);
159
160 // Check the computed signature against the one passed in the query
161 $ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
162
163 // Release the key resource
164 openssl_free_key($publickeyid);
165
166 return $ok == 1;
167 } /*}}}*/
168}/*}}}*/
169
170class OAuthRequest {/*{{{*/
171 private $parameters;
172 private $http_method;
173 private $http_url;
174 // for debug purposes
175 public $base_string;
176 public static $version = '1.0';
177
178 function __construct($http_method, $http_url, $parameters=NULL) {/*{{{*/
179 @$parameters or $parameters = array();
180 $this->parameters = $parameters;
181 $this->http_method = $http_method;
182 $this->http_url = $http_url;
183 }/*}}}*/
184
185
186 /**
187 * attempt to build up a request from what was passed to the server
188 */
189 public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) {/*{{{*/
190 $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") ? 'http' : 'https';
191 @$http_url or $http_url = $scheme . '://' . $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
192 @$http_method or $http_method = $_SERVER['REQUEST_METHOD'];
193
194 $request_headers = OAuthRequest::get_headers();
195
196 // let the library user override things however they'd like, if they know
197 // which parameters to use then go for it, for example XMLRPC might want to
198 // do this
199 if ($parameters) {
200 $req = new OAuthRequest($http_method, $http_url, $parameters);
201 } else {
202 // collect request parameters from query string (GET) and post-data (POST) if appropriate (note: POST vars have priority)
203 $req_parameters = $_GET;
204 if ($http_method == "POST" && @strstr($request_headers["Content-Type"], "application/x-www-form-urlencoded") ) {
205 $req_parameters = array_merge($req_parameters, $_POST);
206 }
207
208 // next check for the auth header, we need to do some extra stuff
209 // if that is the case, namely suck in the parameters from GET or POST
210 // so that we can include them in the signature
211 if (@substr($request_headers['Authorization'], 0, 6) == "OAuth ") {
212 $header_parameters = OAuthRequest::split_header($request_headers['Authorization']);
213 $parameters = array_merge($req_parameters, $header_parameters);
214 $req = new OAuthRequest($http_method, $http_url, $parameters);
215 } else $req = new OAuthRequest($http_method, $http_url, $req_parameters);
216 }
217
218 return $req;
219 }/*}}}*/
220
221 /**
222 * pretty much a helper function to set up the request
223 */
224 public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) {/*{{{*/
225 @$parameters or $parameters = array();
226 $defaults = array("oauth_version" => OAuthRequest::$version,
227 "oauth_nonce" => OAuthRequest::generate_nonce(),
228 "oauth_timestamp" => OAuthRequest::generate_timestamp(),
229 "oauth_consumer_key" => $consumer->key);
230 $parameters = array_merge($defaults, $parameters);
231
232 if ($token) {
233 $parameters['oauth_token'] = $token->key;
234 }
235 return new OAuthRequest($http_method, $http_url, $parameters);
236 }/*}}}*/
237
238 public function set_parameter($name, $value) {/*{{{*/
239 $this->parameters[$name] = $value;
240 }/*}}}*/
241
242 public function get_parameter($name) {/*{{{*/
243 return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
244 }/*}}}*/
245
246 public function get_parameters() {/*{{{*/
247 return $this->parameters;
248 }/*}}}*/
249
250 /**
251 * Returns the normalized parameters of the request
252 *
253 * This will be all (except oauth_signature) parameters,
254 * sorted first by key, and if duplicate keys, then by
255 * value.
256 *
257 * The returned string will be all the key=value pairs
258 * concated by &.
259 *
260 * @return string
261 */
262 public function get_signable_parameters() {/*{{{*/
263 // Grab all parameters
264 $params = $this->parameters;
265
266 // Remove oauth_signature if present
267 if (isset($params['oauth_signature'])) {
268 unset($params['oauth_signature']);
269 }
270
271 // Urlencode both keys and values
272 $keys = OAuthUtil::urlencode_rfc3986(array_keys($params));
273 $values = OAuthUtil::urlencode_rfc3986(array_values($params));
274 $params = array_combine($keys, $values);
275
276 // Sort by keys (natsort)
277 uksort($params, 'strcmp');
278
279 // Generate key=value pairs
280 $pairs = array();
281 foreach ($params as $key=>$value ) {
282 if (is_array($value)) {
283 // If the value is an array, it's because there are multiple
284 // with the same key, sort them, then add all the pairs
285 natsort($value);
286 foreach ($value as $v2) {
287 $pairs[] = $key . '=' . $v2;
288 }
289 } else {
290 $pairs[] = $key . '=' . $value;
291 }
292 }
293
294 // Return the pairs, concated with &
295 return implode('&', $pairs);
296 }/*}}}*/
297
298 /**
299 * Returns the base string of this request
300 *
301 * The base string defined as the method, the url
302 * and the parameters (normalized), each urlencoded
303 * and the concated with &.
304 */
305 public function get_signature_base_string() {/*{{{*/
306 $parts = array(
307 $this->get_normalized_http_method(),
308 $this->get_normalized_http_url(),
309 $this->get_signable_parameters()
310 );
311
312 $parts = OAuthUtil::urlencode_rfc3986($parts);
313
314 return implode('&', $parts);
315 }/*}}}*/
316
317 /**
318 * just uppercases the http method
319 */
320 public function get_normalized_http_method() {/*{{{*/
321 return strtoupper($this->http_method);
322 }/*}}}*/
323
324 /**
325 * parses the url and rebuilds it to be
326 * scheme://host/path
327 */
328 public function get_normalized_http_url() {/*{{{*/
329 $parts = parse_url($this->http_url);
330
331 $port = @$parts['port'];
332 $scheme = $parts['scheme'];
333 $host = $parts['host'];
334 $path = @$parts['path'];
335
336 $port or $port = ($scheme == 'https') ? '443' : '80';
337
338 if (($scheme == 'https' && $port != '443')
339 || ($scheme == 'http' && $port != '80')) {
340 $host = "$host:$port";
341 }
342 return "$scheme://$host$path";
343 }/*}}}*/
344
345 /**
346 * builds a url usable for a GET request
347 */
348 public function to_url() {/*{{{*/
349 $out = $this->get_normalized_http_url() . "?";
350 $out .= $this->to_postdata();
351 return $out;
352 }/*}}}*/
353
354 /**
355 * builds the data one would send in a POST request
356 *
357 * TODO(morten.fangel):
358 * this function might be easily replaced with http_build_query()
359 * and corrections for rfc3986 compatibility.. but not sure
360 */
361 public function to_postdata() {/*{{{*/
362 $total = array();
363 foreach ($this->parameters as $k => $v) {
364 if (is_array($v)) {
365 foreach ($v as $va) {
366 $total[] = OAuthUtil::urlencode_rfc3986($k) . "[]=" . OAuthUtil::urlencode_rfc3986($va);
367 }
368 } else {
369 $total[] = OAuthUtil::urlencode_rfc3986($k) . "=" . OAuthUtil::urlencode_rfc3986($v);
370 }
371 }
372 $out = implode("&", $total);
373 return $out;
374 }/*}}}*/
375
376 /**
377 * builds the Authorization: header
378 */
379 public function to_header() {/*{{{*/
380 $out ='Authorization: OAuth realm=""';
381 $total = array();
382 foreach ($this->parameters as $k => $v) {
383 if (substr($k, 0, 5) != "oauth") continue;
384 if (is_array($v)) throw new OAuthException('Arrays not supported in headers');
385 $out .= ',' . OAuthUtil::urlencode_rfc3986($k) . '="' . OAuthUtil::urlencode_rfc3986($v) . '"';
386 }
387 return $out;
388 }/*}}}*/
389
390 public function __toString() {/*{{{*/
391 return $this->to_url();
392 }/*}}}*/
393
394
395 public function sign_request($signature_method, $consumer, $token) {/*{{{*/
396 $this->set_parameter("oauth_signature_method", $signature_method->get_name());
397 $signature = $this->build_signature($signature_method, $consumer, $token);
398 $this->set_parameter("oauth_signature", $signature);
399 }/*}}}*/
400
401 public function build_signature($signature_method, $consumer, $token) {/*{{{*/
402 $signature = $signature_method->build_signature($this, $consumer, $token);
403 return $signature;
404 }/*}}}*/
405
406 /**
407 * util function: current timestamp
408 */
409 private static function generate_timestamp() {/*{{{*/
410 return time();
411 }/*}}}*/
412
413 /**
414 * util function: current nonce
415 */
416 private static function generate_nonce() {/*{{{*/
417 $mt = microtime();
418 $rand = mt_rand();
419
420 return md5($mt . $rand); // md5s look nicer than numbers
421 }/*}}}*/
422
423 /**
424 * util function for turning the Authorization: header into
425 * parameters, has to do some unescaping
426 */
427 private static function split_header($header) {/*{{{*/
428 $pattern = '/(([-_a-z]*)=("([^"]*)"|([^,]*)),?)/';
429 $offset = 0;
430 $params = array();
431 while (preg_match($pattern, $header, $matches, PREG_OFFSET_CAPTURE, $offset) > 0) {
432 $match = $matches[0];
433 $header_name = $matches[2][0];
434 $header_content = (isset($matches[5])) ? $matches[5][0] : $matches[4][0];
435 $params[$header_name] = OAuthUtil::urldecode_rfc3986( $header_content );
436 $offset = $match[1] + strlen($match[0]);
437 }
438
439 if (isset($params['realm'])) {
440 unset($params['realm']);
441 }
442
443 return $params;
444 }/*}}}*/
445
446 /**
447 * helper to try to sort out headers for people who aren't running apache
448 */
449 private static function get_headers() {/*{{{*/
450 if (function_exists('apache_request_headers')) {
451 // we need this to get the actual Authorization: header
452 // because apache tends to tell us it doesn't exist
453 return apache_request_headers();
454 }
455 // otherwise we don't have apache and are just going to have to hope
456 // that $_SERVER actually contains what we need
457 $out = array();
458 foreach ($_SERVER as $key => $value) {
459 if (substr($key, 0, 5) == "HTTP_") {
460 // this is chaos, basically it is just there to capitalize the first
461 // letter of every word that is not an initial HTTP and strip HTTP
462 // code from przemek
463 $key = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key, 5)))));
464 $out[$key] = $value;
465 }
466 }
467 return $out;
468 }/*}}}*/
469}/*}}}*/
470
471class OAuthServer {/*{{{*/
472 protected $timestamp_threshold = 300; // in seconds, five minutes
473 protected $version = 1.0; // hi blaine
474 protected $signature_methods = array();
475
476 protected $data_store;
477
478 function __construct($data_store) {/*{{{*/
479 $this->data_store = $data_store;
480 }/*}}}*/
481
482 public function add_signature_method($signature_method) {/*{{{*/
483 $this->signature_methods[$signature_method->get_name()] =
484 $signature_method;
485 }/*}}}*/
486
487 // high level functions
488
489 /**
490 * process a request_token request
491 * returns the request token on success
492 */
493 public function fetch_request_token(&$request) {/*{{{*/
494 $this->get_version($request);
495
496 $consumer = $this->get_consumer($request);
497
498 // no token required for the initial token request
499 $token = NULL;
500
501 $this->check_signature($request, $consumer, $token);
502
503 $new_token = $this->data_store->new_request_token($consumer);
504
505 return $new_token;
506 }/*}}}*/
507
508 /**
509 * process an access_token request
510 * returns the access token on success
511 */
512 public function fetch_access_token(&$request) {/*{{{*/
513 $this->get_version($request);
514
515 $consumer = $this->get_consumer($request);
516
517 // requires authorized request token
518 $token = $this->get_token($request, $consumer, "request");
519
520
521 $this->check_signature($request, $consumer, $token);
522
523 $new_token = $this->data_store->new_access_token($token, $consumer);
524
525 return $new_token;
526 }/*}}}*/
527
528 /**
529 * verify an api call, checks all the parameters
530 */
531 public function verify_request(&$request) {/*{{{*/
532 $this->get_version($request);
533 $consumer = $this->get_consumer($request);
534 $token = $this->get_token($request, $consumer, "access");
535 $this->check_signature($request, $consumer, $token);
536 return array($consumer, $token);
537 }/*}}}*/
538
539 // Internals from here
540 /**
541 * version 1
542 */
543 private function get_version(&$request) {/*{{{*/
544 $version = $request->get_parameter("oauth_version");
545 if (!$version) {
546 $version = 1.0;
547 }
548 if ($version && $version != $this->version) {
549 throw new OAuthException("OAuth version '$version' not supported");
550 }
551 return $version;
552 }/*}}}*/
553
554 /**
555 * figure out the signature with some defaults
556 */
557 private function get_signature_method(&$request) {/*{{{*/
558 $signature_method =
559 @$request->get_parameter("oauth_signature_method");
560 if (!$signature_method) {
561 $signature_method = "PLAINTEXT";
562 }
563 if (!in_array($signature_method,
564 array_keys($this->signature_methods))) {
565 throw new OAuthException(
566 "Signature method '$signature_method' not supported try one of the following: " . implode(", ", array_keys($this->signature_methods))
567 );
568 }
569 return $this->signature_methods[$signature_method];
570 }/*}}}*/
571
572 /**
573 * try to find the consumer for the provided request's consumer key
574 */
575 private function get_consumer(&$request) {/*{{{*/
576 $consumer_key = @$request->get_parameter("oauth_consumer_key");
577 if (!$consumer_key) {
578 throw new OAuthException("Invalid consumer key");
579 }
580
581 $consumer = $this->data_store->lookup_consumer($consumer_key);
582 if (!$consumer) {
583 throw new OAuthException("Invalid consumer");
584 }
585
586 return $consumer;
587 }/*}}}*/
588
589 /**
590 * try to find the token for the provided request's token key
591 */
592 private function get_token(&$request, $consumer, $token_type="access") {/*{{{*/
593 $token_field = @$request->get_parameter('oauth_token');
594 $token = $this->data_store->lookup_token(
595 $consumer, $token_type, $token_field
596 );
597 if (!$token) {
598 throw new OAuthException("Invalid $token_type token: $token_field");
599 }
600 return $token;
601 }/*}}}*/
602
603 /**
604 * all-in-one function to check the signature on a request
605 * should guess the signature method appropriately
606 */
607 private function check_signature(&$request, $consumer, $token) {/*{{{*/
608 // this should probably be in a different method
609 $timestamp = @$request->get_parameter('oauth_timestamp');
610 $nonce = @$request->get_parameter('oauth_nonce');
611
612 $this->check_timestamp($timestamp);
613 $this->check_nonce($consumer, $token, $nonce, $timestamp);
614
615 $signature_method = $this->get_signature_method($request);
616
617 $signature = $request->get_parameter('oauth_signature');
618 $valid_sig = $signature_method->check_signature(
619 $request,
620 $consumer,
621 $token,
622 $signature
623 );
624
625 if (!$valid_sig) {
626 throw new OAuthException("Invalid signature");
627 }
628 }/*}}}*/
629
630 /**
631 * check that the timestamp is new enough
632 */
633 private function check_timestamp($timestamp) {/*{{{*/
634 // verify that timestamp is recentish
635 $now = time();
636 if ($now - $timestamp > $this->timestamp_threshold) {
637 throw new OAuthException("Expired timestamp, yours $timestamp, ours $now");
638 }
639 }/*}}}*/
640
641 /**
642 * check that the nonce is not repeated
643 */
644 private function check_nonce($consumer, $token, $nonce, $timestamp) {/*{{{*/
645 // verify that the nonce is uniqueish
646 $found = $this->data_store->lookup_nonce($consumer, $token, $nonce, $timestamp);
647 if ($found) {
648 throw new OAuthException("Nonce already used: $nonce");
649 }
650 }/*}}}*/
651
652
653
654}/*}}}*/
655
656class OAuthDataStore {/*{{{*/
657 function lookup_consumer($consumer_key) {/*{{{*/
658 // implement me
659 }/*}}}*/
660
661 function lookup_token($consumer, $token_type, $token) {/*{{{*/
662 // implement me
663 }/*}}}*/
664
665 function lookup_nonce($consumer, $token, $nonce, $timestamp) {/*{{{*/
666 // implement me
667 }/*}}}*/
668
669 function new_request_token($consumer) {/*{{{*/
670 // return a new token attached to this consumer
671 }/*}}}*/
672
673 function new_access_token($token, $consumer) {/*{{{*/
674 // return a new access token attached to this consumer
675 // for the user associated with this token if the request token
676 // is authorized
677 // should also invalidate the request token
678 }/*}}}*/
679
680}/*}}}*/
681
682
683/* A very naive dbm-based oauth storage
684 */
685class SimpleOAuthDataStore extends OAuthDataStore {/*{{{*/
686 private $dbh;
687
688 function __construct($path = "oauth.gdbm") {/*{{{*/
689 $this->dbh = dba_popen($path, 'c', 'gdbm');
690 }/*}}}*/
691
692 function __destruct() {/*{{{*/
693 dba_close($this->dbh);
694 }/*}}}*/
695
696 function lookup_consumer($consumer_key) {/*{{{*/
697 $rv = dba_fetch("consumer_$consumer_key", $this->dbh);
698 if ($rv === FALSE) {
699 return NULL;
700 }
701 $obj = unserialize($rv);
702 if (!($obj instanceof OAuthConsumer)) {
703 return NULL;
704 }
705 return $obj;
706 }/*}}}*/
707
708 function lookup_token($consumer, $token_type, $token) {/*{{{*/
709 $rv = dba_fetch("${token_type}_${token}", $this->dbh);
710 if ($rv === FALSE) {
711 return NULL;
712 }
713 $obj = unserialize($rv);
714 if (!($obj instanceof OAuthToken)) {
715 return NULL;
716 }
717 return $obj;
718 }/*}}}*/
719
720 function lookup_nonce($consumer, $token, $nonce, $timestamp) {/*{{{*/
721 if (dba_exists("nonce_$nonce", $this->dbh)) {
722 return TRUE;
723 } else {
724 dba_insert("nonce_$nonce", "1", $this->dbh);
725 return FALSE;
726 }
727 }/*}}}*/
728
729 function new_token($consumer, $type="request") {/*{{{*/
730 $key = md5(time());
731 $secret = time() + time();
732 $token = new OAuthToken($key, md5(md5($secret)));
733 if (!dba_insert("${type}_$key", serialize($token), $this->dbh)) {
734 throw new OAuthException("doooom!");
735 }
736 return $token;
737 }/*}}}*/
738
739 function new_request_token($consumer) {/*{{{*/
740 return $this->new_token($consumer, "request");
741 }/*}}}*/
742
743 function new_access_token($token, $consumer) {/*{{{*/
744
745 $token = $this->new_token($consumer, 'access');
746 dba_delete("request_" . $token->key, $this->dbh);
747 return $token;
748 }/*}}}*/
749}/*}}}*/
750
751class OAuthUtil {/*{{{*/
752 public static function urlencode_rfc3986($input) {/*{{{*/
753 if (is_array($input)) {
754 return array_map(array('OAuthUtil','urlencode_rfc3986'), $input);
755 } else if (is_scalar($input)) {
756 return str_replace('+', ' ',
757 str_replace('%7E', '~', rawurlencode($input)));
758 } else {
759 return '';
760 }
761 }/*}}}*/
762
763
764 // This decode function isn't taking into consideration the above
765 // modifications to the encoding process. However, this method doesn't
766 // seem to be used anywhere so leaving it as is.
767 public static function urldecode_rfc3986($string) {/*{{{*/
768 return rawurldecode($string);
769 }/*}}}*/
770}/*}}}*/