(bug 13690) Fix PHP notice on accessing some URLs. parse_url() in some versions...
[mediawiki.git] / includes / memcached-client.php
blob8adb5233a0708c243c89ca5b3a8d52a2b1d24b56
1 <?php
2 //
3 // +---------------------------------------------------------------------------+
4 // | memcached client, PHP |
5 // +---------------------------------------------------------------------------+
6 // | Copyright (c) 2003 Ryan T. Dean <rtdean@cytherianage.net> |
7 // | All rights reserved. |
8 // | |
9 // | Redistribution and use in source and binary forms, with or without |
10 // | modification, are permitted provided that the following conditions |
11 // | are met: |
12 // | |
13 // | 1. Redistributions of source code must retain the above copyright |
14 // | notice, this list of conditions and the following disclaimer. |
15 // | 2. Redistributions in binary form must reproduce the above copyright |
16 // | notice, this list of conditions and the following disclaimer in the |
17 // | documentation and/or other materials provided with the distribution. |
18 // | |
19 // | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
20 // | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
21 // | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
22 // | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
23 // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
24 // | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 // | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 // +---------------------------------------------------------------------------+
30 // | Author: Ryan T. Dean <rtdean@cytherianage.net> |
31 // | Heavily influenced by the Perl memcached client by Brad Fitzpatrick. |
32 // | Permission granted by Brad Fitzpatrick for relicense of ported Perl |
33 // | client logic under 2-clause BSD license. |
34 // +---------------------------------------------------------------------------+
36 // $TCAnet$
39 /**
40 * This is the PHP client for memcached - a distributed memory cache daemon.
41 * More information is available at http://www.danga.com/memcached/
43 * Usage example:
45 * require_once 'memcached.php';
47 * $mc = new memcached(array(
48 * 'servers' => array('127.0.0.1:10000',
49 * array('192.0.0.1:10010', 2),
50 * '127.0.0.1:10020'),
51 * 'debug' => false,
52 * 'compress_threshold' => 10240,
53 * 'persistant' => true));
55 * $mc->add('key', array('some', 'array'));
56 * $mc->replace('key', 'some random string');
57 * $val = $mc->get('key');
59 * @author Ryan T. Dean <rtdean@cytherianage.net>
60 * @package memcached-client
61 * @version 0.1.2
64 // {{{ requirements
65 // }}}
67 // {{{ class memcached
68 /**
69 * memcached client class implemented using (p)fsockopen()
71 * @author Ryan T. Dean <rtdean@cytherianage.net>
72 * @addtogroup Cache
74 class memcached
76 // {{{ properties
77 // {{{ public
79 // {{{ constants
80 // {{{ flags
82 /**
83 * Flag: indicates data is serialized
85 const SERIALIZED = 1;
87 /**
88 * Flag: indicates data is compressed
90 const COMPRESSED = 2;
92 // }}}
94 /**
95 * Minimum savings to store data compressed
97 const COMPRESSION_SAVINGS = 0.20;
99 // }}}
103 * Command statistics
105 * @var array
106 * @access public
108 var $stats;
110 // }}}
111 // {{{ private
114 * Cached Sockets that are connected
116 * @var array
117 * @access private
119 var $_cache_sock;
122 * Current debug status; 0 - none to 9 - profiling
124 * @var boolean
125 * @access private
127 var $_debug;
130 * Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'
132 * @var array
133 * @access private
135 var $_host_dead;
138 * Is compression available?
140 * @var boolean
141 * @access private
143 var $_have_zlib;
146 * Do we want to use compression?
148 * @var boolean
149 * @access private
151 var $_compress_enable;
154 * At how many bytes should we compress?
156 * @var integer
157 * @access private
159 var $_compress_threshold;
162 * Are we using persistant links?
164 * @var boolean
165 * @access private
167 var $_persistant;
170 * If only using one server; contains ip:port to connect to
172 * @var string
173 * @access private
175 var $_single_sock;
178 * Array containing ip:port or array(ip:port, weight)
180 * @var array
181 * @access private
183 var $_servers;
186 * Our bit buckets
188 * @var array
189 * @access private
191 var $_buckets;
194 * Total # of bit buckets we have
196 * @var integer
197 * @access private
199 var $_bucketcount;
202 * # of total servers we have
204 * @var integer
205 * @access private
207 var $_active;
210 * Stream timeout in seconds. Applies for example to fread()
212 * @var integer
213 * @access private
215 var $_timeout_seconds;
218 * Stream timeout in microseconds
220 * @var integer
221 * @access private
223 var $_timeout_microseconds;
226 * Connect timeout in seconds
228 var $_connect_timeout;
231 * Number of connection attempts for each server
233 var $_connect_attempts;
235 // }}}
236 // }}}
237 // {{{ methods
238 // {{{ public functions
239 // {{{ memcached()
242 * Memcache initializer
244 * @param array $args Associative array of settings
246 * @return mixed
247 * @access public
249 function memcached ($args)
251 $this->set_servers(@$args['servers']);
252 $this->_debug = @$args['debug'];
253 $this->stats = array();
254 $this->_compress_threshold = @$args['compress_threshold'];
255 $this->_persistant = array_key_exists('persistant', $args) ? (@$args['persistant']) : false;
256 $this->_compress_enable = true;
257 $this->_have_zlib = function_exists("gzcompress");
259 $this->_cache_sock = array();
260 $this->_host_dead = array();
262 $this->_timeout_seconds = 1;
263 $this->_timeout_microseconds = 0;
265 $this->_connect_timeout = 0.01;
266 $this->_connect_attempts = 3;
269 // }}}
270 // {{{ add()
273 * Adds a key/value to the memcache server if one isn't already set with
274 * that key
276 * @param string $key Key to set with data
277 * @param mixed $val Value to store
278 * @param integer $exp (optional) Time to expire data at
280 * @return boolean
281 * @access public
283 function add ($key, $val, $exp = 0)
285 return $this->_set('add', $key, $val, $exp);
288 // }}}
289 // {{{ decr()
292 * Decriment a value stored on the memcache server
294 * @param string $key Key to decriment
295 * @param integer $amt (optional) Amount to decriment
297 * @return mixed FALSE on failure, value on success
298 * @access public
300 function decr ($key, $amt=1)
302 return $this->_incrdecr('decr', $key, $amt);
305 // }}}
306 // {{{ delete()
309 * Deletes a key from the server, optionally after $time
311 * @param string $key Key to delete
312 * @param integer $time (optional) How long to wait before deleting
314 * @return boolean TRUE on success, FALSE on failure
315 * @access public
317 function delete ($key, $time = 0)
319 if (!$this->_active)
320 return false;
322 $sock = $this->get_sock($key);
323 if (!is_resource($sock))
324 return false;
326 $key = is_array($key) ? $key[1] : $key;
328 @$this->stats['delete']++;
329 $cmd = "delete $key $time\r\n";
330 if(!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
332 $this->_dead_sock($sock);
333 return false;
335 $res = trim(fgets($sock));
337 if ($this->_debug)
338 $this->_debugprint(sprintf("MemCache: delete %s (%s)\n", $key, $res));
340 if ($res == "DELETED")
341 return true;
342 return false;
345 // }}}
346 // {{{ disconnect_all()
349 * Disconnects all connected sockets
351 * @access public
353 function disconnect_all ()
355 foreach ($this->_cache_sock as $sock)
356 fclose($sock);
358 $this->_cache_sock = array();
361 // }}}
362 // {{{ enable_compress()
365 * Enable / Disable compression
367 * @param boolean $enable TRUE to enable, FALSE to disable
369 * @access public
371 function enable_compress ($enable)
373 $this->_compress_enable = $enable;
376 // }}}
377 // {{{ forget_dead_hosts()
380 * Forget about all of the dead hosts
382 * @access public
384 function forget_dead_hosts ()
386 $this->_host_dead = array();
389 // }}}
390 // {{{ get()
393 * Retrieves the value associated with the key from the memcache server
395 * @param string $key Key to retrieve
397 * @return mixed
398 * @access public
400 function get ($key)
402 $fname = 'memcached::get';
403 wfProfileIn( $fname );
405 if ( $this->_debug ) {
406 $this->_debugprint( "get($key)\n" );
409 if (!$this->_active) {
410 wfProfileOut( $fname );
411 return false;
414 $sock = $this->get_sock($key);
416 if (!is_resource($sock)) {
417 wfProfileOut( $fname );
418 return false;
421 @$this->stats['get']++;
423 $cmd = "get $key\r\n";
424 if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
426 $this->_dead_sock($sock);
427 wfProfileOut( $fname );
428 return false;
431 $val = array();
432 $this->_load_items($sock, $val);
434 if ($this->_debug)
435 foreach ($val as $k => $v)
436 $this->_debugprint(sprintf("MemCache: sock %s got %s\n", serialize($sock), $k));
438 wfProfileOut( $fname );
439 return @$val[$key];
442 // }}}
443 // {{{ get_multi()
446 * Get multiple keys from the server(s)
448 * @param array $keys Keys to retrieve
450 * @return array
451 * @access public
453 function get_multi ($keys)
455 if (!$this->_active)
456 return false;
458 $this->stats['get_multi']++;
459 $sock_keys = array();
461 foreach ($keys as $key)
463 $sock = $this->get_sock($key);
464 if (!is_resource($sock)) continue;
465 $key = is_array($key) ? $key[1] : $key;
466 if (!isset($sock_keys[$sock]))
468 $sock_keys[$sock] = array();
469 $socks[] = $sock;
471 $sock_keys[$sock][] = $key;
474 // Send out the requests
475 foreach ($socks as $sock)
477 $cmd = "get";
478 foreach ($sock_keys[$sock] as $key)
480 $cmd .= " ". $key;
482 $cmd .= "\r\n";
484 if ($this->_safe_fwrite($sock, $cmd, strlen($cmd)))
486 $gather[] = $sock;
487 } else
489 $this->_dead_sock($sock);
493 // Parse responses
494 $val = array();
495 foreach ($gather as $sock)
497 $this->_load_items($sock, $val);
500 if ($this->_debug)
501 foreach ($val as $k => $v)
502 $this->_debugprint(sprintf("MemCache: got %s\n", $k));
504 return $val;
507 // }}}
508 // {{{ incr()
511 * Increments $key (optionally) by $amt
513 * @param string $key Key to increment
514 * @param integer $amt (optional) amount to increment
516 * @return integer New key value?
517 * @access public
519 function incr ($key, $amt=1)
521 return $this->_incrdecr('incr', $key, $amt);
524 // }}}
525 // {{{ replace()
528 * Overwrites an existing value for key; only works if key is already set
530 * @param string $key Key to set value as
531 * @param mixed $value Value to store
532 * @param integer $exp (optional) Experiation time
534 * @return boolean
535 * @access public
537 function replace ($key, $value, $exp=0)
539 return $this->_set('replace', $key, $value, $exp);
542 // }}}
543 // {{{ run_command()
546 * Passes through $cmd to the memcache server connected by $sock; returns
547 * output as an array (null array if no output)
549 * NOTE: due to a possible bug in how PHP reads while using fgets(), each
550 * line may not be terminated by a \r\n. More specifically, my testing
551 * has shown that, on FreeBSD at least, each line is terminated only
552 * with a \n. This is with the PHP flag auto_detect_line_endings set
553 * to falase (the default).
555 * @param resource $sock Socket to send command on
556 * @param string $cmd Command to run
558 * @return array Output array
559 * @access public
561 function run_command ($sock, $cmd)
563 if (!is_resource($sock))
564 return array();
566 if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
567 return array();
569 while (true)
571 $res = fgets($sock);
572 $ret[] = $res;
573 if (preg_match('/^END/', $res))
574 break;
575 if (strlen($res) == 0)
576 break;
578 return $ret;
581 // }}}
582 // {{{ set()
585 * Unconditionally sets a key to a given value in the memcache. Returns true
586 * if set successfully.
588 * @param string $key Key to set value as
589 * @param mixed $value Value to set
590 * @param integer $exp (optional) Experiation time
592 * @return boolean TRUE on success
593 * @access public
595 function set ($key, $value, $exp=0)
597 return $this->_set('set', $key, $value, $exp);
600 // }}}
601 // {{{ set_compress_threshold()
604 * Sets the compression threshold
606 * @param integer $thresh Threshold to compress if larger than
608 * @access public
610 function set_compress_threshold ($thresh)
612 $this->_compress_threshold = $thresh;
615 // }}}
616 // {{{ set_debug()
619 * Sets the debug flag
621 * @param boolean $dbg TRUE for debugging, FALSE otherwise
623 * @access public
625 * @see memcahced::memcached
627 function set_debug ($dbg)
629 $this->_debug = $dbg;
632 // }}}
633 // {{{ set_servers()
636 * Sets the server list to distribute key gets and puts between
638 * @param array $list Array of servers to connect to
640 * @access public
642 * @see memcached::memcached()
644 function set_servers ($list)
646 $this->_servers = $list;
647 $this->_active = count($list);
648 $this->_buckets = null;
649 $this->_bucketcount = 0;
651 $this->_single_sock = null;
652 if ($this->_active == 1)
653 $this->_single_sock = $this->_servers[0];
657 * Sets the timeout for new connections
659 * @param integer $seconds Number of seconds
660 * @param integer $microseconds Number of microseconds
662 * @access public
664 function set_timeout ($seconds, $microseconds)
666 $this->_timeout_seconds = $seconds;
667 $this->_timeout_microseconds = $microseconds;
670 // }}}
671 // }}}
672 // {{{ private methods
673 // {{{ _close_sock()
676 * Close the specified socket
678 * @param string $sock Socket to close
680 * @access private
682 function _close_sock ($sock)
684 $host = array_search($sock, $this->_cache_sock);
685 fclose($this->_cache_sock[$host]);
686 unset($this->_cache_sock[$host]);
689 // }}}
690 // {{{ _connect_sock()
693 * Connects $sock to $host, timing out after $timeout
695 * @param integer $sock Socket to connect
696 * @param string $host Host:IP to connect to
698 * @return boolean
699 * @access private
701 function _connect_sock (&$sock, $host)
703 list ($ip, $port) = explode(":", $host);
704 $sock = false;
705 $timeout = $this->_connect_timeout;
706 $errno = $errstr = null;
707 for ($i = 0; !$sock && $i < $this->_connect_attempts; $i++) {
708 if ($i > 0) {
709 # Sleep until the timeout, in case it failed fast
710 $elapsed = microtime(true) - $t;
711 if ( $elapsed < $timeout ) {
712 usleep(($timeout - $elapsed) * 1e6);
714 $timeout *= 2;
716 $t = microtime(true);
717 if ($this->_persistant == 1)
719 $sock = @pfsockopen($ip, $port, $errno, $errstr, $timeout);
720 } else
722 $sock = @fsockopen($ip, $port, $errno, $errstr, $timeout);
725 if (!$sock) {
726 if ($this->_debug)
727 $this->_debugprint( "Error connecting to $host: $errstr\n" );
728 return false;
731 // Initialise timeout
732 stream_set_timeout($sock, $this->_timeout_seconds, $this->_timeout_microseconds);
734 return true;
737 // }}}
738 // {{{ _dead_sock()
741 * Marks a host as dead until 30-40 seconds in the future
743 * @param string $sock Socket to mark as dead
745 * @access private
747 function _dead_sock ($sock)
749 $host = array_search($sock, $this->_cache_sock);
750 @list ($ip, /* $port */) = explode(":", $host);
751 $this->_host_dead[$ip] = time() + 30 + intval(rand(0, 10));
752 $this->_host_dead[$host] = $this->_host_dead[$ip];
753 unset($this->_cache_sock[$host]);
756 // }}}
757 // {{{ get_sock()
760 * get_sock
762 * @param string $key Key to retrieve value for;
764 * @return mixed resource on success, false on failure
765 * @access private
767 function get_sock ($key)
769 if (!$this->_active)
770 return false;
772 if ($this->_single_sock !== null) {
773 $this->_flush_read_buffer($this->_single_sock);
774 return $this->sock_to_host($this->_single_sock);
777 $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key);
779 if ($this->_buckets === null)
781 foreach ($this->_servers as $v)
783 if (is_array($v))
785 for ($i=0; $i<$v[1]; $i++)
786 $bu[] = $v[0];
787 } else
789 $bu[] = $v;
792 $this->_buckets = $bu;
793 $this->_bucketcount = count($bu);
796 $realkey = is_array($key) ? $key[1] : $key;
797 for ($tries = 0; $tries<20; $tries++)
799 $host = $this->_buckets[$hv % $this->_bucketcount];
800 $sock = $this->sock_to_host($host);
801 if (is_resource($sock)) {
802 $this->_flush_read_buffer($sock);
803 return $sock;
805 $hv += $this->_hashfunc($tries . $realkey);
808 return false;
811 // }}}
812 // {{{ _hashfunc()
815 * Creates a hash integer based on the $key
817 * @param string $key Key to hash
819 * @return integer Hash value
820 * @access private
822 function _hashfunc ($key)
824 # Hash function must on [0,0x7ffffff]
825 # We take the first 31 bits of the MD5 hash, which unlike the hash
826 # function used in a previous version of this client, works
827 return hexdec(substr(md5($key),0,8)) & 0x7fffffff;
830 // }}}
831 // {{{ _incrdecr()
834 * Perform increment/decriment on $key
836 * @param string $cmd Command to perform
837 * @param string $key Key to perform it on
838 * @param integer $amt Amount to adjust
840 * @return integer New value of $key
841 * @access private
843 function _incrdecr ($cmd, $key, $amt=1)
845 if (!$this->_active)
846 return null;
848 $sock = $this->get_sock($key);
849 if (!is_resource($sock))
850 return null;
852 $key = is_array($key) ? $key[1] : $key;
853 @$this->stats[$cmd]++;
854 if (!$this->_safe_fwrite($sock, "$cmd $key $amt\r\n"))
855 return $this->_dead_sock($sock);
857 stream_set_timeout($sock, 1, 0);
858 $line = fgets($sock);
859 $match = array();
860 if (!preg_match('/^(\d+)/', $line, $match))
861 return null;
862 return $match[1];
865 // }}}
866 // {{{ _load_items()
869 * Load items into $ret from $sock
871 * @param resource $sock Socket to read from
872 * @param array $ret Returned values
874 * @access private
876 function _load_items ($sock, &$ret)
878 while (1)
880 $decl = fgets($sock);
881 if ($decl == "END\r\n")
883 return true;
884 } elseif (preg_match('/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match))
886 list($rkey, $flags, $len) = array($match[1], $match[2], $match[3]);
887 $bneed = $len+2;
888 $offset = 0;
890 while ($bneed > 0)
892 $data = fread($sock, $bneed);
893 $n = strlen($data);
894 if ($n == 0)
895 break;
896 $offset += $n;
897 $bneed -= $n;
898 @$ret[$rkey] .= $data;
901 if ($offset != $len+2)
903 // Something is borked!
904 if ($this->_debug)
905 $this->_debugprint(sprintf("Something is borked! key %s expecting %d got %d length\n", $rkey, $len+2, $offset));
907 unset($ret[$rkey]);
908 $this->_close_sock($sock);
909 return false;
912 if ($this->_have_zlib && $flags & memcached::COMPRESSED)
913 $ret[$rkey] = gzuncompress($ret[$rkey]);
915 $ret[$rkey] = rtrim($ret[$rkey]);
917 if ($flags & memcached::SERIALIZED)
918 $ret[$rkey] = unserialize($ret[$rkey]);
920 } else
922 $this->_debugprint("Error parsing memcached response\n");
923 return 0;
928 // }}}
929 // {{{ _set()
932 * Performs the requested storage operation to the memcache server
934 * @param string $cmd Command to perform
935 * @param string $key Key to act on
936 * @param mixed $val What we need to store
937 * @param integer $exp When it should expire
939 * @return boolean
940 * @access private
942 function _set ($cmd, $key, $val, $exp)
944 if (!$this->_active)
945 return false;
947 $sock = $this->get_sock($key);
948 if (!is_resource($sock))
949 return false;
951 @$this->stats[$cmd]++;
953 $flags = 0;
955 if (!is_scalar($val))
957 $val = serialize($val);
958 $flags |= memcached::SERIALIZED;
959 if ($this->_debug)
960 $this->_debugprint(sprintf("client: serializing data as it is not scalar\n"));
963 $len = strlen($val);
965 if ($this->_have_zlib && $this->_compress_enable &&
966 $this->_compress_threshold && $len >= $this->_compress_threshold)
968 $c_val = gzcompress($val, 9);
969 $c_len = strlen($c_val);
971 if ($c_len < $len*(1 - memcached::COMPRESSION_SAVINGS))
973 if ($this->_debug)
974 $this->_debugprint(sprintf("client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len));
975 $val = $c_val;
976 $len = $c_len;
977 $flags |= memcached::COMPRESSED;
980 if (!$this->_safe_fwrite($sock, "$cmd $key $flags $exp $len\r\n$val\r\n"))
981 return $this->_dead_sock($sock);
983 $line = trim(fgets($sock));
985 if ($this->_debug)
987 if ($flags & memcached::COMPRESSED)
988 $val = 'compressed data';
989 $this->_debugprint(sprintf("MemCache: %s %s => %s (%s)\n", $cmd, $key, $val, $line));
991 if ($line == "STORED")
992 return true;
993 return false;
996 // }}}
997 // {{{ sock_to_host()
1000 * Returns the socket for the host
1002 * @param string $host Host:IP to get socket for
1004 * @return mixed IO Stream or false
1005 * @access private
1007 function sock_to_host ($host)
1009 if (isset($this->_cache_sock[$host]))
1010 return $this->_cache_sock[$host];
1012 $sock = null;
1013 $now = time();
1014 list ($ip, /* $port */) = explode (":", $host);
1015 if (isset($this->_host_dead[$host]) && $this->_host_dead[$host] > $now ||
1016 isset($this->_host_dead[$ip]) && $this->_host_dead[$ip] > $now)
1017 return null;
1019 if (!$this->_connect_sock($sock, $host))
1020 return $this->_dead_sock($host);
1022 // Do not buffer writes
1023 stream_set_write_buffer($sock, 0);
1025 $this->_cache_sock[$host] = $sock;
1027 return $this->_cache_sock[$host];
1030 function _debugprint($str){
1031 print($str);
1035 * Write to a stream, timing out after the correct amount of time
1037 * @return bool false on failure, true on success
1040 function _safe_fwrite($f, $buf, $len = false) {
1041 stream_set_blocking($f, 0);
1043 if ($len === false) {
1044 wfDebug("Writing " . strlen( $buf ) . " bytes\n");
1045 $bytesWritten = fwrite($f, $buf);
1046 } else {
1047 wfDebug("Writing $len bytes\n");
1048 $bytesWritten = fwrite($f, $buf, $len);
1050 $n = stream_select($r=NULL, $w = array($f), $e = NULL, 10, 0);
1051 # $this->_timeout_seconds, $this->_timeout_microseconds);
1053 wfDebug("stream_select returned $n\n");
1054 stream_set_blocking($f, 1);
1055 return $n == 1;
1056 return $bytesWritten;
1060 * Original behaviour
1062 function _safe_fwrite($f, $buf, $len = false) {
1063 if ($len === false) {
1064 $bytesWritten = fwrite($f, $buf);
1065 } else {
1066 $bytesWritten = fwrite($f, $buf, $len);
1068 return $bytesWritten;
1072 * Flush the read buffer of a stream
1074 function _flush_read_buffer($f) {
1075 if (!is_resource($f)) {
1076 return;
1078 $n = stream_select($r=array($f), $w = NULL, $e = NULL, 0, 0);
1079 while ($n == 1 && !feof($f)) {
1080 fread($f, 1024);
1081 $n = stream_select($r=array($f), $w = NULL, $e = NULL, 0, 0);
1085 // }}}
1086 // }}}
1087 // }}}
1090 // vim: sts=3 sw=3 et
1092 // }}}