function substr($s, $a, $b = null) { if (function_exists('mb_orig_substr')) { return $b === null ? mb_orig_substr($s, $a) : mb_orig_substr($s, $a, $b); } return $b === null ? substr($s, $a) : substr($s, $a, $b); }
public static function substr($s, $a, $b = null) { if (function_exists('mb_orig_substr')) return $b === null ? mb_orig_substr($s, $a) : mb_orig_substr($s, $a, $b); return $b === null ? substr($s, $a) : substr($s, $a, $b); }
/** * Write to shared memory * @param string $data Data * @param integer $offset Offset * @return boolean Success */ public function write($data, $offset) { $segno = floor($offset / $this->segsize); if (!isset($this->segments[$segno])) { if (!$this->open($segno, true)) { return false; } } $sOffset = $offset % $this->segsize; $d = $this->segsize - ($sOffset + mb_orig_strlen($data)); if ($d < 0) { $this->write(mb_orig_substr($data, $d), ($segno + 1) * $this->segsize); $data = mb_orig_substr($data, 0, $d); } //Daemon::log('writing to #'.$offset.' (segno '.$segno.')'); shmop_write($this->segments[$segno], $data, $sOffset); return true; }
/** * Called when new data received * @return void */ public function onRead() { Timer::setTimeout($this->keepaliveTimer); while (($line = $this->readline()) !== null) { if ($line === '') { continue; } if (mb_orig_strlen($line) > 512) { Daemon::$process->log('IRCBouncerConnection error: buffer overflow.'); $this->finish(); return; } $line = mb_orig_substr($line, 0, -mb_orig_strlen($this->EOL)); $p = mb_orig_strpos($line, ':', 1); $max = $p ? substr_count($line, " ", 0, $p) + 1 : 18; $e = explode(" ", $line, $max); $i = 0; $cmd = $e[$i++]; $args = []; for ($s = min(sizeof($e), 14); $i < $s; ++$i) { if ($e[$i][0] === ':') { $args[] = mb_orig_substr($e[$i], 1); break; } $args[] = $e[$i]; } if (ctype_digit($cmd)) { $code = (int) $cmd; $cmd = isset(IRC::$codes[$code]) ? IRC::$codes[$code] : 'UNKNOWN-' . $code; } $this->onCommand($cmd, $args); } if (mb_orig_strlen($this->buf) > 512) { Daemon::$process->log('IRCClientConnection error: buffer overflow.'); $this->finish(); } }
/** * Called when new UDP packet received. * @param string $pct * @return void */ public function onUdpPacket($pct) { if (mb_orig_strlen($pct) < 10) { return; } $orig = $pct; $this->response = []; Binary::getWord($pct); // ID $bitmap = Binary::getBitmap(Binary::getByte($pct)) . Binary::getBitmap(Binary::getByte($pct)); //$qr = (int) $bitmap[0]; $opcode = bindec(substr($bitmap, 1, 4)); //$aa = (int) $bitmap[5]; //$tc = (int) $bitmap[6]; //$rd = (int) $bitmap[7]; //$ra = (int) $bitmap[8]; //$z = bindec(substr($bitmap, 9, 3)); $rcode = bindec(mb_orig_substr($bitmap, 12)); $this->response['status'] = ['rcode' => $rcode, 'msg' => $this->getMessageByRcode($rcode)]; $qdcount = Binary::getWord($pct); $ancount = Binary::getWord($pct); $nscount = Binary::getWord($pct); $arcount = Binary::getWord($pct); for ($i = 0; $i < $qdcount; ++$i) { $name = Binary::parseLabels($pct, $orig); $typeInt = Binary::getWord($pct); $type = isset(Pool::$type[$typeInt]) ? Pool::$type[$typeInt] : 'UNK(' . $typeInt . ')'; $classInt = Binary::getWord($pct); $class = isset(Pool::$class[$classInt]) ? Pool::$class[$classInt] : 'UNK(' . $classInt . ')'; if (!isset($this->response[$type])) { $this->response[$type] = []; } $record = ['name' => $name, 'type' => $type, 'class' => $class]; $this->response['query'][] = $record; } $getResRecord = function (&$pct) use($orig) { $name = Binary::parseLabels($pct, $orig); $typeInt = Binary::getWord($pct); $type = isset(Pool::$type[$typeInt]) ? Pool::$type[$typeInt] : 'UNK(' . $typeInt . ')'; $classInt = Binary::getWord($pct); $class = isset(Pool::$class[$classInt]) ? Pool::$class[$classInt] : 'UNK(' . $classInt . ')'; $ttl = Binary::getDWord($pct); $length = Binary::getWord($pct); $data = mb_orig_substr($pct, 0, $length); $pct = mb_orig_substr($pct, $length); $record = ['name' => $name, 'type' => $type, 'class' => $class, 'ttl' => $ttl]; if ($type === 'A' || $type === 'AAAA') { if ($data === "") { $record['ip'] = false; $record['ttl'] = 5; } else { $record['ip'] = inet_ntop($data); } } elseif ($type === 'NS') { $record['ns'] = Binary::parseLabels($data, $orig); } elseif ($type === 'CNAME') { $record['cname'] = Binary::parseLabels($data, $orig); } elseif ($type === 'SOA') { $record['mname'] = Binary::parseLabels($data, $orig); $record['rname'] = Binary::parseLabels($data, $orig); $record['serial'] = Binary::getDWord($data); $record['refresh'] = Binary::getDWord($data); $record['retry'] = Binary::getDWord($data); $record['expire'] = Binary::getDWord($data); $record['nx'] = Binary::getDWord($data); } elseif ($type === 'MX') { $record['preference'] = Binary::getWord($data); $record['exchange'] = Binary::parseLabels($data, $orig); } elseif ($type === 'TXT') { $record['text'] = ''; $lastLength = -1; while (($length = mb_orig_strlen($data)) > 0 && $length !== $lastLength) { $record['text'] .= Binary::parseLabels($data, $orig); $lastLength = $length; } } elseif ($type === 'SRV') { $record['priority'] = Binary::getWord($data); $record['weight'] = Binary::getWord($data); $record['port'] = Binary::getWord($data); $record['target'] = Binary::parseLabels($data, $orig); } return $record; }; for ($i = 0; $i < $ancount; ++$i) { $record = $getResRecord($pct); if (!isset($this->response[$record['type']])) { $this->response[$record['type']] = []; } $this->response[$record['type']][] = $record; } for ($i = 0; $i < $nscount; ++$i) { $record = $getResRecord($pct); if (!isset($this->response[$record['type']])) { $this->response[$record['type']] = []; } $this->response[$record['type']][] = $record; } for ($i = 0; $i < $arcount; ++$i) { $record = $getResRecord($pct); if (!isset($this->response[$record['type']])) { $this->response[$record['type']] = []; } $this->response[$record['type']][] = $record; } $this->onResponse->executeOne($this->response); if (!$this->keepalive) { $this->finish(); return; } else { $this->checkFree(); } }
/** * Output some data * @param string $s String to out * @param boolean $flush ob_flush? * @return boolean Success */ public function out($s, $flush = true) { if ($flush) { if (!Daemon::$obInStack) { // preventing recursion ob_flush(); } } if ($this->aborted) { return false; } if (!isset($this->upstream)) { return false; } $l = mb_orig_strlen($s); $this->responseLength += $l; $this->ensureSentHeaders(); if ($this->attrs->chunked) { for ($o = 0; $o < $l;) { $c = min($this->upstream->pool->config->chunksize->value, $l - $o); $chunk = dechex($c) . "\r\n" . ($c === $l ? $s : mb_orig_substr($s, $o, $c)) . "\r\n"; if ($this->sendfp) { $this->sendfp->write($chunk); } else { $this->upstream->requestOut($this, $chunk); } $o += $c; } return true; } else { if ($this->sendfp) { $this->sendfp->write($s); return true; } if (Daemon::$compatMode) { echo $s; return true; } return $this->upstream->requestOut($this, $s); } }
private function _getFilteredDirs($dirs) { $curdir = getcwd(); foreach ($dirs as $idx => $dir) { if ($dir === $curdir) { $dir = "."; } if (mb_orig_strpos($dir, "{$curdir}/") === 0) { $dir = mb_orig_substr($dir, mb_orig_strlen($curdir) + 1); } $parts = explode("/", $dir); foreach ($parts as $p) { if ($p === ".") { continue; } if (isset($this->exclude[$p])) { unset($dirs[$idx]); continue 2; } } /* check if dir is still present, because event could be delivered when dir does not exist anymore */ $stat = $this->_stat($dir); if ($stat !== 'dir') { unset($dirs[$idx]); continue; } $dirs[$idx] = $dir; } return $dirs; }
/** * Called when new data received * @return void */ protected function onRead() { start: if ($this->state === static::STATE_STANDBY) { // outside of packet while (($l = $this->readline()) !== null) { if ($l === '') { continue; } $char = $l[0]; $val = mb_orig_substr($l, 1); if ($char === ':') { // inline integer $this->pushValue((int) $val); goto start; } elseif ($char === '+' || $char === '-') { // inline string $this->error = $char === '-'; $this->pushValue($val); goto start; } elseif ($char === '*') { // defines number of elements of incoming array $length = (int) $val; if ($length <= 0) { $this->pushValue([]); goto start; } $ptr = []; if (is_array($this->ptr)) { $this->ptr[] =& $ptr; } $this->ptr =& $ptr; $this->stack[] = [&$ptr, $length]; $this->levelLength = $length; unset($ptr); goto start; } elseif ($char === '$') { // defines size of the data block if ($l[1] === '-') { $this->pushValue(null); goto start; } $this->valueLength = (int) $val; if ($this->valueLength + 2 > $this->pool->maxAllowedPacket) { $this->log('max-allowed-packet (' . $this->pool->config->maxallowedpacket->getHumanValue() . ') exceed, aborting connection'); $this->finish(); return; } $this->setWatermark($this->valueLength + 2); $this->state = static::STATE_BINARY; // binary data block break; // stop reading line-by-line } } } if ($this->state === static::STATE_BINARY) { // inside of binary data block if ($this->getInputLength() < $this->valueLength + 2) { return; //we do not have a whole packet } $value = $this->read($this->valueLength); if ($this->read(2) !== $this->EOL) { $this->finish(); return; } $this->state = static::STATE_STANDBY; $this->setWatermark(3); $this->pushValue($value); goto start; } }
/** * Called when new data received * @return void */ public function onRead() { while (($line = $this->readline()) !== null) { if ($line === '') { continue; } if (mb_orig_strlen($line) > 512) { Daemon::$process->log('IRCClientConnection error: buffer overflow.'); $this->finish(); return; } $line = mb_orig_substr($line, 0, -mb_orig_strlen($this->EOL)); $p = mb_orig_strpos($line, ' :', 1); $max = $p !== false ? substr_count($line, " ", 0, $p + 1) + 1 : 18; $e = explode(" ", $line, $max); $i = 0; $from = IRC::parseUsermask($e[$i][0] === ':' ? mb_orig_substr($e[$i++], 1) : null); $cmd = $e[$i++]; $args = []; for ($s = min(sizeof($e), 14); $i < $s; ++$i) { if ($e[$i][0] === ':') { $args[] = mb_orig_substr($e[$i], 1); break; } $args[] = $e[$i]; } if (ctype_digit($cmd)) { $code = (int) $cmd; $cmd = isset(IRC::$codes[$code]) ? IRC::$codes[$code] : $code; } $this->lastLine = $line; $this->onCommand($from, $cmd, $args); } if (mb_orig_strlen($this->buf) > 512) { Daemon::$process->log('IRCClientConnection error: buffer overflow.'); $this->finish(); } }
function substr($a, $b, $c = null) { if ($c === null) { return function_exists('mb_orig_substr') ? mb_orig_substr($a, $b) : substr($a, $b); } else { return function_exists('mb_orig_substr') ? mb_orig_substr($a, $b, $c) : substr($a, $b, $c); } }
protected static function fastSubstr($string, $start, $length = null) { if (function_exists("mb_orig_substr")) { return mb_orig_substr($string, $start, $length); } return substr($string, $start, $length); }
/** * Called when new data received * @return void */ public function onRead() { start: if ($this->freed) { return; } if ($this->state === self::STATE_ROOT) { if (false === ($hdr = $this->readExact(16))) { return; // we do not have a header } $this->hdr = unpack('Vlen/VreqId/VresponseTo/VopCode', $hdr); $this->hdr['plen'] = $this->hdr['len'] - 16; $this->setWatermark($this->hdr['plen'], $this->hdr['plen']); $this->state = self::STATE_PACKET; } if ($this->state === self::STATE_PACKET) { if (false === ($pct = $this->readExact($this->hdr['plen']))) { return; //we do not have a whole packet } $this->state = self::STATE_ROOT; $this->setWatermark(16, 0xffffff); if ($this->hdr['opCode'] === Pool::OP_REPLY) { $r = unpack('Vflag/VcursorID1/VcursorID2/Voffset/Vlength', mb_orig_substr($pct, 0, 20)); $r['cursorId'] = mb_orig_substr($pct, 4, 8); $id = (int) $this->hdr['responseTo']; if (isset($this->requests[$id])) { $req = $this->requests[$id]; if (sizeof($req) === 1) { // get more $r['cursorId'] = $req[0]; } } else { $req = false; } $flagBits = str_pad(strrev(decbin($r['flag'])), 8, '0', STR_PAD_LEFT); $curId = $r['cursorId'] !== "" ? 'c' . $r['cursorId'] : 'r' . $this->hdr['responseTo']; if ($req && isset($req[2]) && $req[2] === false && !isset($this->cursors[$curId])) { $cur = new Cursor($curId, $req[0], $this); $this->cursors[$curId] = $cur; $cur->failure = $flagBits[1] === '1'; $cur->await = $flagBits[3] === '1'; $cur->callback = $req[1]; $cur->parseOplog = isset($req[3]) && $req[3]; $cur->tailable = isset($req[4]) && $req[4]; } else { $cur = isset($this->cursors[$curId]) ? $this->cursors[$curId] : false; } if ($cur && ($r['length'] === 0 || mb_orig_substr($curId, 0, 1) === 'r')) { if ($cur->tailable) { if ($cur->finished = $flagBits[0] === '1') { $cur->destroy(); } } else { $cur->finished = true; } } $p = 20; $items = []; while ($p < $this->hdr['plen']) { $dl = unpack('Vlen', mb_orig_substr($pct, $p, 4)); $doc = bson_decode(mb_orig_substr($pct, $p, $dl['len'])); if ($cur) { if ($cur->parseOplog && isset($doc['ts'])) { $tsdata = unpack('Vsec/Vinc', mb_orig_substr($pct, $p + 8, 8)); $doc['ts'] = $tsdata['sec'] . ' ' . $tsdata['inc']; } $cur->items[] = $doc; ++$cur->counter; } else { $items[] = $doc; } $p += $dl['len']; } $this->setFree(true); if (isset($req[2]) && $req[2] && $req[1]) { $req[1](sizeof($items) ? $items[0] : false); if ($cur) { if ($cur instanceof Cursor) { $cur->destroy(); } else { unset($this->cursors[$curId]); } } } elseif ($cur) { $func = $cur->callback; $func($cur); } unset($this->requests[$id]); $req = null; } } goto start; }
protected function _getChunk_mb_orig() { if ($this->_bufferPosition >= $this->_bufferLength) { if (!feof($this->_file)) { $this->_buffer = fread($this->_file, $this->_chunkReadSize); $this->_bufferPosition = 0; $this->_bufferLength = mb_orig_strlen($this->_buffer); } else { return false; } } //Skip line delimiters (ltrim) $position = mb_orig_strpos($this->_buffer, "<", $this->_bufferPosition); while ($position === $this->_bufferPosition) { $this->_bufferPosition++; $this->_filePosition++; //Buffer ended with white space so we can refill it if ($this->_bufferPosition >= $this->_bufferLength) { if (!feof($this->_file)) { $this->_buffer = fread($this->_file, $this->_chunkReadSize); $this->_bufferPosition = 0; $this->_bufferLength = mb_orig_strlen($this->_buffer); } else { return false; } } $position = mb_orig_strpos($this->_buffer, "<", $this->_bufferPosition); } //Let's find next line delimiter while ($position === false) { $next_search = $this->_bufferLength; //Delimiter not in buffer so try to add more data to it if (!feof($this->_file)) { $this->_buffer .= fread($this->_file, $this->_chunkReadSize); $this->_bufferLength = mb_orig_strlen($this->_buffer); } else { break; } //Let's find xml tag start $position = mb_orig_strpos($this->_buffer, "<", $next_search); } if ($position === false) { $position = $this->_bufferLength + 1; } $len = $position - $this->_bufferPosition; $this->_filePosition += $len; $result = mb_orig_substr($this->_buffer, $this->_bufferPosition, $len); $this->_bufferPosition = $position; return $result; }
/** * @TODO * @param string $mask * @param string $str * @return string */ protected static function mask($mask, $str) { $out = ''; $l = mb_orig_strlen($str); $ml = mb_orig_strlen($mask); while (($o = mb_orig_strlen($out)) < $l) { $out .= mb_orig_substr($str, $o, $ml) ^ $mask; } return $out; }
/** * @return bool */ public function loadFile() { $this->_openConnection(); fwrite($this->_socket, $this->_requestHeader); // stream_set_blocking($this->_socket, 0); :) $bHeaderRead = false; $header = ''; $startTime = getmicrotime(); while (!($this->_bComplete = feof($this->_socket) || $this->_rangeTo > 0 && $this->_fileLoaded >= $this->_rangeTo)) { if ($this->_timeLimit > 0 && getmicrotime() - $startTime > $this->_timeLimit) { break; } $result = fread($this->_socket, 256 * 1024); if (false === $bHeaderRead) { $header .= $result; if (static::$_bMBStringOrig) { $posHSplit = mb_orig_strpos($header, "\r\n\r\n"); } else { $posHSplit = strpos($header, "\r\n\r\n"); } if ($posHSplit !== false) { if (static::$_bMBStringOrig) { $result = mb_orig_substr($header, $posHSplit + 4); $header = mb_orig_substr($header, 0, $posHSplit + 2); $this->_headerSize = mb_orig_strlen($header); } else { $result = substr($header, $posHSplit + 4); $header = substr($header, 0, $posHSplit + 2); $this->_headerSize = strlen($header); } $this->_readHeaderData($header); $bHeaderRead = true; } else { continue; } } $this->_writeResult($result); } if ($this->_bufferLoaded > 0) { $result = null; $this->_writeResult($result, true); } if (true === $this->_bComplete) { @unlink($this->_dwnStateFilePath); } else { $this->_saveStepToFile(); } fclose($this->_socket); $this->_socket = null; fclose($this->_dwnFileHandler); $this->_dwnFileHandler = null; return $this->_bComplete; }
/** * Convert bitmap into bytes * @param string $bitmap Bitmap * @param integer $check_len Check length? * @return string|false */ public static function bitmap2bytes($bitmap, $check_len = 0) { $r = ''; $bitmap = str_pad($bitmap, ceil(mb_orig_strlen($bitmap) / 8) * 8, '0', STR_PAD_LEFT); for ($i = 0, $n = mb_orig_strlen($bitmap) / 8; $i < $n; ++$i) { $r .= chr((int) bindec(mb_orig_substr($bitmap, $i * 8, 8))); } if ($check_len && mb_orig_strlen($r) !== $check_len) { return false; } return $r; }
public function _get_xml_chunk_mb_orig($fp) { if ($this->buf_position >= $this->buf_len) { if (!feof($fp)) { $this->buf = fread($fp, $this->read_size); $this->buf_position = 0; $this->buf_len = mb_orig_strlen($this->buf); } else { return false; } } //Skip line delimiters (ltrim) $xml_position = mb_orig_strpos($this->buf, "<", $this->buf_position); while ($xml_position === $this->buf_position) { $this->buf_position++; $this->file_position++; //Buffer ended with white space so we can refill it if ($this->buf_position >= $this->buf_len) { if (!feof($fp)) { $this->buf = fread($fp, $this->read_size); $this->buf_position = 0; $this->buf_len = mb_orig_strlen($this->buf); } else { return false; } } $xml_position = mb_orig_strpos($this->buf, "<", $this->buf_position); } //Let's find next line delimiter while ($xml_position === false) { $next_search = $this->buf_len; //Delimiter not in buffer so try to add more data to it if (!feof($fp)) { $this->buf .= fread($fp, $this->read_size); $this->buf_len = mb_orig_strlen($this->buf); } else { break; } //Let's find xml tag start $xml_position = mb_orig_strpos($this->buf, "<", $next_search); } if ($xml_position === false) { $xml_position = $this->buf_len + 1; } $len = $xml_position - $this->buf_position; $this->file_position += $len; $result = mb_orig_substr($this->buf, $this->buf_position, $len); $this->buf_position = $xml_position; return $result; }
/** * Cursor's destructor. Sends a signal to the server * @return void */ public function __destruct() { try { if (mb_orig_substr($this->id, 0, 1) === 'c') { $this->conn->pool->killCursors([mb_orig_substr($this->id, 1)], $this->conn); } } catch (ConnectionFinished $e) { } }
/** * Handles the output from downstream requests * @param object $req Request * @param string $out The output * @return boolean Success */ public function requestOut($req, $out) { $cs = $this->pool->config->chunksize->value; if (mb_orig_strlen($out) > $cs) { while (($ol = mb_orig_strlen($out)) > 0) { $l = min($cs, $ol); if ($this->sendChunk($req, mb_orig_substr($out, 0, $l)) === false) { $req->abort(); return false; } $out = mb_orig_substr($out, $l); } } elseif ($this->sendChunk($req, $out) === false) { $req->abort(); return false; } return true; }
/** * Applies a binary delta to a base object * * @param string $delta delta string * @param string $base base object data * @return string patched content */ private static function ApplyDelta($delta, $base) { /* * algorithm from patch-delta.c */ $pos = 0; GitPHP_PackData::ParseVarInt($delta, $pos); // base size GitPHP_PackData::ParseVarInt($delta, $pos); // result size $data = ''; $deltalen = strlen($delta); while ($pos < $deltalen) { $opcode = ord($delta[$pos++]); if ($opcode & 0x80) { $off = 0; if ($opcode & 0x1) { $off = ord($delta[$pos++]); } if ($opcode & 0x2) { $off |= ord($delta[$pos++]) << 8; } if ($opcode & 0x4) { $off |= ord($delta[$pos++]) << 16; } if ($opcode & 0x8) { $off |= ord($delta[$pos++]) << 24; } $len = 0; if ($opcode & 0x10) { $len = ord($delta[$pos++]); } if ($opcode & 0x20) { $len |= ord($delta[$pos++]) << 8; } if ($opcode & 0x40) { $len |= ord($delta[$pos++]) << 16; } if ($len == 0) { $len = 0x10000; } $data .= mb_orig_substr($base, $off, $len); } else { if ($opcode > 0) { $data .= mb_orig_substr($delta, $pos, $opcode); $pos += $opcode; } } } return $data; }