<?php

//
// $Id$
//

//
// Copyright (c) 2001-2014, Andrew Aksyonoff
// Copyright (c) 2008-2014, Sphinx Technologies Inc
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org/
//

/////////////////////////////////////////////////////////////////////////////
// PHP version of Sphinx searchd client (PHP API)
/////////////////////////////////////////////////////////////////////////////

/// known searchd commands
define ( "SEARCHD_COMMAND_SEARCH",		0 );
define ( "SEARCHD_COMMAND_EXCERPT",		1 );
define ( "SEARCHD_COMMAND_UPDATE",		2 );
define ( "SEARCHD_COMMAND_KEYWORDS",	3 );
define ( "SEARCHD_COMMAND_PERSIST",		4 );
define ( "SEARCHD_COMMAND_STATUS",		5 );
define ( "SEARCHD_COMMAND_FLUSHATTRS",	7 );

/// current client-side command implementation versions
define ( "VER_COMMAND_SEARCH",		0x11D );
define ( "VER_COMMAND_EXCERPT",		0x104 );
define ( "VER_COMMAND_UPDATE",		0x103 );
define ( "VER_COMMAND_KEYWORDS",	0x100 );
define ( "VER_COMMAND_STATUS",		0x100 );
define ( "VER_COMMAND_QUERY",		0x100 );
define ( "VER_COMMAND_FLUSHATTRS",	0x100 );

/// known searchd status codes
define ( "SEARCHD_OK",				0 );
define ( "SEARCHD_ERROR",			1 );
define ( "SEARCHD_RETRY",			2 );
define ( "SEARCHD_WARNING",			3 );

/// known match modes
define ( "SPH_MATCH_ALL",			0 );
define ( "SPH_MATCH_ANY",			1 );
define ( "SPH_MATCH_PHRASE",		2 );
define ( "SPH_MATCH_BOOLEAN",		3 );
define ( "SPH_MATCH_EXTENDED",		4 );
define ( "SPH_MATCH_FULLSCAN",		5 );
define ( "SPH_MATCH_EXTENDED2",		6 );	// extended engine V2 (TEMPORARY, WILL BE REMOVED)

/// known ranking modes (ext2 only)
define ( "SPH_RANK_PROXIMITY_BM25",	0 );	///< default mode, phrase proximity major factor and BM25 minor one
define ( "SPH_RANK_BM25",			1 );	///< statistical mode, BM25 ranking only (faster but worse quality)
define ( "SPH_RANK_NONE",			2 );	///< no ranking, all matches get a weight of 1
define ( "SPH_RANK_WORDCOUNT",		3 );	///< simple word-count weighting, rank is a weighted sum of per-field keyword occurence counts
define ( "SPH_RANK_PROXIMITY",		4 );
define ( "SPH_RANK_MATCHANY",		5 );
define ( "SPH_RANK_FIELDMASK",		6 );
define ( "SPH_RANK_SPH04",			7 );
define ( "SPH_RANK_EXPR",			8 );
define ( "SPH_RANK_TOTAL",			9 );

/// known sort modes
define ( "SPH_SORT_RELEVANCE",		0 );
define ( "SPH_SORT_ATTR_DESC",		1 );
define ( "SPH_SORT_ATTR_ASC",		2 );
define ( "SPH_SORT_TIME_SEGMENTS", 	3 );
define ( "SPH_SORT_EXTENDED", 		4 );
define ( "SPH_SORT_EXPR", 			5 );

/// known filter types
define ( "SPH_FILTER_VALUES",		0 );
define ( "SPH_FILTER_RANGE",		1 );
define ( "SPH_FILTER_FLOATRANGE",	2 );

/// known attribute types
define ( "SPH_ATTR_INTEGER",		1 );
define ( "SPH_ATTR_TIMESTAMP",		2 );
define ( "SPH_ATTR_ORDINAL",		3 );
define ( "SPH_ATTR_BOOL",			4 );
define ( "SPH_ATTR_FLOAT",			5 );
define ( "SPH_ATTR_BIGINT",			6 );
define ( "SPH_ATTR_STRING",			7 );
define ( "SPH_ATTR_FACTORS",			1001 );
define ( "SPH_ATTR_MULTI",			0x40000001 );
define ( "SPH_ATTR_MULTI64",			0x40000002 );

/// known grouping functions
define ( "SPH_GROUPBY_DAY",			0 );
define ( "SPH_GROUPBY_WEEK",		1 );
define ( "SPH_GROUPBY_MONTH",		2 );
define ( "SPH_GROUPBY_YEAR",		3 );
define ( "SPH_GROUPBY_ATTR",		4 );
define ( "SPH_GROUPBY_ATTRPAIR",	5 );

// important properties of PHP's integers:
//  - always signed (one bit short of PHP_INT_SIZE)
//  - conversion from string to int is saturated
//  - float is double
//  - div converts arguments to floats
//  - mod converts arguments to ints

// the packing code below works as follows:
//  - when we got an int, just pack it
//    if performance is a problem, this is the branch users should aim for
//
//  - otherwise, we got a number in string form
//    this might be due to different reasons, but we assume that this is
//    because it didn't fit into PHP int
//
//  - factor the string into high and low ints for packing
//    - if we have bcmath, then it is used
//    - if we don't, we have to do it manually (this is the fun part)
//
//    - x64 branch does factoring using ints
//    - x32 (ab)uses floats, since we can't fit unsigned 32-bit number into an int
//
// unpacking routines are pretty much the same.
//  - return ints if we can
//  - otherwise format number into a string

/// pack 64-bit signed
function sphPackI64 ( $v )
{
	assert ( is_numeric($v) );

	// x64
	if ( PHP_INT_SIZE>=8 )
	{
		$v = (int)$v;
		return pack ( "NN", $v>>32, $v&0xFFFFFFFF );
	}

	// x32, int
	if ( is_int($v) )
		return pack ( "NN", $v < 0 ? -1 : 0, $v );

	// x32, bcmath
	if ( function_exists("bcmul") )
	{
		if ( bccomp ( $v, 0 ) == -1 )
			$v = bcadd ( "18446744073709551616", $v );
		$h = bcdiv ( $v, "4294967296", 0 );
		$l = bcmod ( $v, "4294967296" );
		return pack ( "NN", (float)$h, (float)$l ); // conversion to float is intentional; int would lose 31st bit
	}

	// x32, no-bcmath
	$p = max(0, strlen($v) - 13);
	$lo = abs((float)substr($v, $p));
	$hi = abs((float)substr($v, 0, $p));

	$m = $lo + $hi*1316134912.0; // (10 ^ 13) % (1 << 32) = 1316134912
	$q = floor($m/4294967296.0);
	$l = $m - ($q*4294967296.0);
	$h = $hi*2328.0 + $q; // (10 ^ 13) / (1 << 32) = 2328

	if ( $v<0 )
	{
		if ( $l==0 )
			$h = 4294967296.0 - $h;
		else
		{
			$h = 4294967295.0 - $h;
			$l = 4294967296.0 - $l;
		}
	}
	return pack ( "NN", $h, $l );
}

/// pack 64-bit unsigned
function sphPackU64 ( $v )
{
	assert ( is_numeric($v) );

	// x64
	if ( PHP_INT_SIZE>=8 )
	{
		assert ( $v>=0 );

		// x64, int
		if ( is_int($v) )
			return pack ( "NN", $v>>32, $v&0xFFFFFFFF );

		// x64, bcmath
		if ( function_exists("bcmul") )
		{
			$h = bcdiv ( $v, 4294967296, 0 );
			$l = bcmod ( $v, 4294967296 );
			return pack ( "NN", $h, $l );
		}

		// x64, no-bcmath
		$p = max ( 0, strlen($v) - 13 );
		$lo = (int)substr ( $v, $p );
		$hi = (int)substr ( $v, 0, $p );

		$m = $lo + $hi*1316134912;
		$l = $m % 4294967296;
		$h = $hi*2328 + (int)($m/4294967296);

		return pack ( "NN", $h, $l );
	}

	// x32, int
	if ( is_int($v) )
		return pack ( "NN", 0, $v );

	// x32, bcmath
	if ( function_exists("bcmul") )
	{
		$h = bcdiv ( $v, "4294967296", 0 );
		$l = bcmod ( $v, "4294967296" );
		return pack ( "NN", (float)$h, (float)$l ); // conversion to float is intentional; int would lose 31st bit
	}

	// x32, no-bcmath
	$p = max(0, strlen($v) - 13);
	$lo = (float)substr($v, $p);
	$hi = (float)substr($v, 0, $p);

	$m = $lo + $hi*1316134912.0;
	$q = floor($m / 4294967296.0);
	$l = $m - ($q * 4294967296.0);
	$h = $hi*2328.0 + $q;

	return pack ( "NN", $h, $l );
}

// unpack 64-bit unsigned
function sphUnpackU64 ( $v )
{
	list ( $hi, $lo ) = array_values ( unpack ( "N*N*", $v ) );

	if ( PHP_INT_SIZE>=8 )
	{
		if ( $hi<0 ) $hi += (1<<32); // because php 5.2.2 to 5.2.5 is totally fucked up again
		if ( $lo<0 ) $lo += (1<<32);

		// x64, int
		if ( $hi<=2147483647 )
			return ($hi<<32) + $lo;

		// x64, bcmath
		if ( function_exists("bcmul") )
			return bcadd ( $lo, bcmul ( $hi, "4294967296" ) );

		// x64, no-bcmath
		$C = 100000;
		$h = ((int)($hi / $C) << 32) + (int)($lo / $C);
		$l = (($hi % $C) << 32) + ($lo % $C);
		if ( $l>$C )
		{
			$h += (int)($l / $C);
			$l  = $l % $C;
		}

		if ( $h==0 )
			return $l;
		return sprintf ( "%d%05d", $h, $l );
	}

	// x32, int
	if ( $hi==0 )
	{
		if ( $lo>0 )
			return $lo;
		return sprintf ( "%u", $lo );
	}

	$hi = sprintf ( "%u", $hi );
	$lo = sprintf ( "%u", $lo );

	// x32, bcmath
	if ( function_exists("bcmul") )
		return bcadd ( $lo, bcmul ( $hi, "4294967296" ) );

	// x32, no-bcmath
	$hi = (float)$hi;
	$lo = (float)$lo;

	$q = floor($hi/10000000.0);
	$r = $hi - $q*10000000.0;
	$m = $lo + $r*4967296.0;
	$mq = floor($m/10000000.0);
	$l = $m - $mq*10000000.0;
	$h = $q*4294967296.0 + $r*429.0 + $mq;

	$h = sprintf ( "%.0f", $h );
	$l = sprintf ( "%07.0f", $l );
	if ( $h=="0" )
		return sprintf( "%.0f", (float)$l );
	return $h . $l;
}

// unpack 64-bit signed
function sphUnpackI64 ( $v )
{
	list ( $hi, $lo ) = array_values ( unpack ( "N*N*", $v ) );

	// x64
	if ( PHP_INT_SIZE>=8 )
	{
		if ( $hi<0 ) $hi += (1<<32); // because php 5.2.2 to 5.2.5 is totally fucked up again
		if ( $lo<0 ) $lo += (1<<32);

		return ($hi<<32) + $lo;
	}

	// x32, int
	if ( $hi==0 )
	{
		if ( $lo>0 )
			return $lo;
		return sprintf ( "%u", $lo );
	}
	// x32, int
	elseif ( $hi==-1 )
	{
		if ( $lo<0 )
			return $lo;
		return sprintf ( "%.0f", $lo - 4294967296.0 );
	}

	$neg = "";
	$c = 0;
	if ( $hi<0 )
	{
		$hi = ~$hi;
		$lo = ~$lo;
		$c = 1;
		$neg = "-";
	}

	$hi = sprintf ( "%u", $hi );
	$lo = sprintf ( "%u", $lo );

	// x32, bcmath
	if ( function_exists("bcmul") )
		return $neg . bcadd ( bcadd ( $lo, bcmul ( $hi, "4294967296" ) ), $c );

	// x32, no-bcmath
	$hi = (float)$hi;
	$lo = (float)$lo;

	$q = floor($hi/10000000.0);
	$r = $hi - $q*10000000.0;
	$m = $lo + $r*4967296.0;
	$mq = floor($m/10000000.0);
	$l = $m - $mq*10000000.0 + $c;
	$h = $q*4294967296.0 + $r*429.0 + $mq;
	if ( $l==10000000 )
	{
		$l = 0;
		$h += 1;
	}

	$h = sprintf ( "%.0f", $h );
	$l = sprintf ( "%07.0f", $l );
	if ( $h=="0" )
		return $neg . sprintf( "%.0f", (float)$l );
	return $neg . $h . $l;
}

function sphFixUint ( $value )
{
	if ( PHP_INT_SIZE>=8 )
	{
		// x64 route, workaround broken unpack() in 5.2.2+
		if ( $value<0 ) $value += (1<<32);
		return $value;
	}
	else
	{
		// x32 route, workaround php signed/unsigned braindamage
		return sprintf ( "%u", $value );
	}
}

function sphSetBit ( $flag, $bit, $on )
{
	if ( $on )
	{
		$flag += ( 1<<$bit );
	} else
	{
		$reset = 255 ^ ( 1<<$bit );
		$flag = $flag & $reset;
	}
	return $flag;
}

/// sphinx searchd client class
class SphinxClient
{
	var $_host;			///< searchd host (default is "localhost")
	var $_port;			///< searchd port (default is 9312)
	var $_offset;		///< how many records to seek from result-set start (default is 0)
	var $_limit;		///< how many records to return from result-set starting at offset (default is 20)
	var $_mode;			///< query matching mode (default is SPH_MATCH_ALL)
	var $_weights;		///< per-field weights (default is 1 for all fields)
	var $_sort;			///< match sorting mode (default is SPH_SORT_RELEVANCE)
	var $_sortby;		///< attribute to sort by (defualt is "")
	var $_min_id;		///< min ID to match (default is 0, which means no limit)
	var $_max_id;		///< max ID to match (default is 0, which means no limit)
	var $_filters;		///< search filters
	var $_groupby;		///< group-by attribute name
	var $_groupfunc;	///< group-by function (to pre-process group-by attribute value with)
	var $_groupsort;	///< group-by sorting clause (to sort groups in result set with)
	var $_groupdistinct;///< group-by count-distinct attribute
	var $_maxmatches;	///< max matches to retrieve
	var $_cutoff;		///< cutoff to stop searching at (default is 0)
	var $_retrycount;	///< distributed retries count
	var $_retrydelay;	///< distributed retries delay
	var $_anchor;		///< geographical anchor point
	var $_indexweights;	///< per-index weights
	var $_ranker;		///< ranking mode (default is SPH_RANK_PROXIMITY_BM25)
	var $_rankexpr;		///< ranking mode expression (for SPH_RANK_EXPR)
	var $_maxquerytime;	///< max query time, milliseconds (default is 0, do not limit)
	var $_fieldweights;	///< per-field-name weights
	var $_overrides;	///< per-query attribute values overrides
	var $_select;		///< select-list (attributes or expressions, with optional aliases)
	var $_query_flags; ///< per-query various flags
	var $_predictedtime; ///< per-query max_predicted_time
	var $_outerorderby; ///< outer match sort by
	var $_outeroffset; ///< outer offset
	var $_outerlimit; ///< outer limit
	var $_hasouter;

	var $_error;		///< last error message
	var $_warning;		///< last warning message
	var $_connerror;		///< connection error vs remote error flag

	var $_reqs;			///< requests array for multi-query
	var $_mbenc;		///< stored mbstring encoding
	var $_arrayresult;	///< whether $result["matches"] should be a hash or an array
	var $_timeout;		///< connect timeout

	/////////////////////////////////////////////////////////////////////////////
	// common stuff
	/////////////////////////////////////////////////////////////////////////////

	/// create a new client object and fill defaults
	function SphinxClient ()
	{
		// per-client-object settings
		$this->_host		= "localhost";
		$this->_port		= 9312;
		$this->_path		= false;
		$this->_socket		= false;

		// per-query settings
		$this->_offset		= 0;
		$this->_limit		= 20;
		$this->_mode		= SPH_MATCH_ALL;
		$this->_weights		= array ();
		$this->_sort		= SPH_SORT_RELEVANCE;
		$this->_sortby		= "";
		$this->_min_id		= 0;
		$this->_max_id		= 0;
		$this->_filters		= array ();
		$this->_groupby		= "";
		$this->_groupfunc	= SPH_GROUPBY_DAY;
		$this->_groupsort	= "@group desc";
		$this->_groupdistinct= "";
		$this->_maxmatches	= 1000;
		$this->_cutoff		= 0;
		$this->_retrycount	= 0;
		$this->_retrydelay	= 0;
		$this->_anchor		= array ();
		$this->_indexweights= array ();
		$this->_ranker		= SPH_RANK_PROXIMITY_BM25;
		$this->_rankexpr	= "";
		$this->_maxquerytime= 0;
		$this->_fieldweights= array();
		$this->_overrides 	= array();
		$this->_select		= "*";
		$this->_query_flags = 0;
		$this->_predictedtime = 0;
		$this->_outerorderby = "";
		$this->_outeroffset = 0;
		$this->_outerlimit = 0;
		$this->_hasouter = false;

		$this->_error		= ""; // per-reply fields (for single-query case)
		$this->_warning		= "";
		$this->_connerror	= false;

		$this->_reqs		= array ();	// requests storage (for multi-query case)
		$this->_mbenc		= "";
		$this->_arrayresult	= false;
		$this->_timeout		= 0;
	}

	function __destruct()
	{
		if ( $this->_socket !== false )
			fclose ( $this->_socket );
	}

	/// get last error message (string)
	function GetLastError ()
	{
		return $this->_error;
	}

	/// get last warning message (string)
	function GetLastWarning ()
	{
		return $this->_warning;
	}

	/// get last error flag (to tell network connection errors from searchd errors or broken responses)
	function IsConnectError()
	{
		return $this->_connerror;
	}

	/// set searchd host name (string) and port (integer)
	function SetServer ( $host, $port = 0 )
	{
		assert ( is_string($host) );
		if ( $host[0] == '/')
		{
			$this->_path = 'unix://' . $host;
			return;
		}
		if ( substr ( $host, 0, 7 )=="unix://" )
		{
			$this->_path = $host;
			return;
		}

		$this->_host = $host;
		$port = intval($port);
		assert ( 0<=$port && $port<65536 );
		$this->_port = ( $port==0 ) ? 9312 : $port;
		$this->_path = '';
	}

	/// set server connection timeout (0 to remove)
	function SetConnectTimeout ( $timeout )
	{
		assert ( is_numeric($timeout) );
		$this->_timeout = $timeout;
	}

	function _Send ( $handle, $data, $length )
	{
		if ( feof($handle) || fwrite ( $handle, $data, $length ) !== $length )
		{
			$this->_error = 'connection unexpectedly closed (timed out?)';
			$this->_connerror = true;
			return false;
		}
		return true;
	}

	/////////////////////////////////////////////////////////////////////////////

	/// enter mbstring workaround mode
	function _MBPush ()
	{
		$this->_mbenc = "";
		if ( ini_get ( "mbstring.func_overload" ) & 2 )
		{
			$this->_mbenc = mb_internal_encoding();
			mb_internal_encoding ( "latin1" );
		}
    }

	/// leave mbstring workaround mode
	function _MBPop ()
	{
		if ( $this->_mbenc )
			mb_internal_encoding ( $this->_mbenc );
	}

	/// connect to searchd server
	function _Connect ()
	{
		if ( $this->_socket!==false )
		{
			// we are in persistent connection mode, so we have a socket
			// however, need to check whether it's still alive
			if ( !@feof ( $this->_socket ) )
				return $this->_socket;

			// force reopen
			$this->_socket = false;
		}

		$errno = 0;
		$errstr = "";
		$this->_connerror = false;

		if ( $this->_path )
		{
			$host = $this->_path;
			$port = 0;
		}
		else
		{
			$host = $this->_host;
			$port = $this->_port;
		}

		if ( $this->_timeout<=0 )
			$fp = @fsockopen ( $host, $port, $errno, $errstr );
		else
			$fp = @fsockopen ( $host, $port, $errno, $errstr, $this->_timeout );

		if ( !$fp )
		{
			if ( $this->_path )
				$location = $this->_path;
			else
				$location = "{$this->_host}:{$this->_port}";

			$errstr = trim ( $errstr );
			$this->_error = "connection to $location failed (errno=$errno, msg=$errstr)";
			$this->_connerror = true;
			return false;
		}

		// send my version
		// this is a subtle part. we must do it before (!) reading back from searchd.
		// because otherwise under some conditions (reported on FreeBSD for instance)
		// TCP stack could throttle write-write-read pattern because of Nagle.
		if ( !$this->_Send ( $fp, pack ( "N", 1 ), 4 ) )
		{
			fclose ( $fp );
			$this->_error = "failed to send client protocol version";
			return false;
		}

		// check version
		list(,$v) = unpack ( "N*", fread ( $fp, 4 ) );
		$v = (int)$v;
		if ( $v<1 )
		{
			fclose ( $fp );
			$this->_error = "expected searchd protocol version 1+, got version '$v'";
			return false;
		}

		return $fp;
	}

	/// get and check response packet from searchd server
	function _GetResponse ( $fp, $client_ver )
	{
		$response = "";
		$len = 0;

		$header = fread ( $fp, 8 );
		if ( strlen($header)==8 )
		{
			list ( $status, $ver, $len ) = array_values ( unpack ( "n2a/Nb", $header ) );
			$left = $len;
			while ( $left>0 && !feof($fp) )
			{
				$chunk = fread ( $fp, min ( 8192, $left ) );
				if ( $chunk )
				{
					$response .= $chunk;
					$left -= strlen($chunk);
				}
			}
		}
		if ( $this->_socket === false )
			fclose ( $fp );

		// check response
		$read = strlen ( $response );
		if ( !$response || $read!=$len )
		{
			$this->_error = $len
				? "failed to read searchd response (status=$status, ver=$ver, len=$len, read=$read)"
				: "received zero-sized searchd response";
			return false;
		}

		// check status
		if ( $status==SEARCHD_WARNING )
		{
			list(,$wlen) = unpack ( "N*", substr ( $response, 0, 4 ) );
			$this->_warning = substr ( $response, 4, $wlen );
			return substr ( $response, 4+$wlen );
		}
		if ( $status==SEARCHD_ERROR )
		{
			$this->_error = "searchd error: " . substr ( $response, 4 );
			return false;
		}
		if ( $status==SEARCHD_RETRY )
		{
			$this->_error = "temporary searchd error: " . substr ( $response, 4 );
			return false;
		}
		if ( $status!=SEARCHD_OK )
		{
			$this->_error = "unknown status code '$status'";
			return false;
		}

		// check version
		if ( $ver<$client_ver )
		{
			$this->_warning = sprintf ( "searchd command v.%d.%d older than client's v.%d.%d, some options might not work",
				$ver>>8, $ver&0xff, $client_ver>>8, $client_ver&0xff );
		}

		return $response;
	}

	/////////////////////////////////////////////////////////////////////////////
	// searching
	/////////////////////////////////////////////////////////////////////////////

	/// set offset and count into result set,
	/// and optionally set max-matches and cutoff limits
	function SetLimits ( $offset, $limit, $max=0, $cutoff=0 )
	{
		assert ( is_int($offset) );
		assert ( is_int($limit) );
		assert ( $offset>=0 );
		assert ( $limit>0 );
		assert ( $max>=0 );
		$this->_offset = $offset;
		$this->_limit = $limit;
		if ( $max>0 )
			$this->_maxmatches = $max;
		if ( $cutoff>0 )
			$this->_cutoff = $cutoff;
	}

	/// set maximum query time, in milliseconds, per-index
	/// integer, 0 means "do not limit"
	function SetMaxQueryTime ( $max )
	{
		assert ( is_int($max) );
		assert ( $max>=0 );
		$this->_maxquerytime = $max;
	}

	/// set matching mode
	function SetMatchMode ( $mode )
	{
		assert ( $mode==SPH_MATCH_ALL
			|| $mode==SPH_MATCH_ANY
			|| $mode==SPH_MATCH_PHRASE
			|| $mode==SPH_MATCH_BOOLEAN
			|| $mode==SPH_MATCH_EXTENDED
			|| $mode==SPH_MATCH_FULLSCAN
			|| $mode==SPH_MATCH_EXTENDED2 );
		$this->_mode = $mode;
	}

	/// set ranking mode
	function SetRankingMode ( $ranker, $rankexpr="" )
	{
		assert ( $ranker===0 || $ranker>=1 && $ranker<SPH_RANK_TOTAL );
		assert ( is_string($rankexpr) );
		$this->_ranker = $ranker;
		$this->_rankexpr = $rankexpr;
	}

	/// set matches sorting mode
	function SetSortMode ( $mode, $sortby="" )
	{
		assert (
			$mode==SPH_SORT_RELEVANCE ||
			$mode==SPH_SORT_ATTR_DESC ||
			$mode==SPH_SORT_ATTR_ASC ||
			$mode==SPH_SORT_TIME_SEGMENTS ||
			$mode==SPH_SORT_EXTENDED ||
			$mode==SPH_SORT_EXPR );
		assert ( is_string($sortby) );
		assert ( $mode==SPH_SORT_RELEVANCE || strlen($sortby)>0 );

		$this->_sort = $mode;
		$this->_sortby = $sortby;
	}

	/// bind per-field weights by order
	/// DEPRECATED; use SetFieldWeights() instead
	function SetWeights ( $weights )
	{
		assert ( is_array($weights) );
		foreach ( $weights as $weight )
			assert ( is_int($weight) );

		$this->_weights = $weights;
	}

	/// bind per-field weights by name
	function SetFieldWeights ( $weights )
	{
		assert ( is_array($weights) );
		foreach ( $weights as $name=>$weight )
		{
			assert ( is_string($name) );
			assert ( is_int($weight) );
		}
		$this->_fieldweights = $weights;
	}

	/// bind per-index weights by name
	function SetIndexWeights ( $weights )
	{
		assert ( is_array($weights) );
		foreach ( $weights as $index=>$weight )
		{
			assert ( is_string($index) );
			assert ( is_int($weight) );
		}
		$this->_indexweights = $weights;
	}

	/// set IDs range to match
	/// only match records if document ID is beetwen $min and $max (inclusive)
	function SetIDRange ( $min, $max )
	{
		assert ( is_numeric($min) );
		assert ( is_numeric($max) );
		assert ( $min<=$max );
		$this->_min_id = $min;
		$this->_max_id = $max;
	}

	/// set values set filter
	/// only match records where $attribute value is in given set
	function SetFilter ( $attribute, $values, $exclude=false )
	{
		assert ( is_string($attribute) );
		assert ( is_array($values) );
		assert ( count($values) );

		if ( is_array($values) && count($values) )
		{
			foreach ( $values as $value )
				assert ( is_numeric($value) );

			$this->_filters[] = array ( "type"=>SPH_FILTER_VALUES, "attr"=>$attribute, "exclude"=>$exclude, "values"=>$values );
		}
	}

	/// set range filter
	/// only match records if $attribute value is beetwen $min and $max (inclusive)
	function SetFilterRange ( $attribute, $min, $max, $exclude=false )
	{
		assert ( is_string($attribute) );
		assert ( is_numeric($min) );
		assert ( is_numeric($max) );
		assert ( $min<=$max );

		$this->_filters[] = array ( "type"=>SPH_FILTER_RANGE, "attr"=>$attribute, "exclude"=>$exclude, "min"=>$min, "max"=>$max );
	}

	/// set float range filter
	/// only match records if $attribute value is beetwen $min and $max (inclusive)
	function SetFilterFloatRange ( $attribute, $min, $max, $exclude=false )
	{
		assert ( is_string($attribute) );
		assert ( is_float($min) );
		assert ( is_float($max) );
		assert ( $min<=$max );

		$this->_filters[] = array ( "type"=>SPH_FILTER_FLOATRANGE, "attr"=>$attribute, "exclude"=>$exclude, "min"=>$min, "max"=>$max );
	}

	/// setup anchor point for geosphere distance calculations
	/// required to use @geodist in filters and sorting
	/// latitude and longitude must be in radians
	function SetGeoAnchor ( $attrlat, $attrlong, $lat, $long )
	{
		assert ( is_string($attrlat) );
		assert ( is_string($attrlong) );
		assert ( is_float($lat) );
		assert ( is_float($long) );

		$this->_anchor = array ( "attrlat"=>$attrlat, "attrlong"=>$attrlong, "lat"=>$lat, "long"=>$long );
	}

	/// set grouping attribute and function
	function SetGroupBy ( $attribute, $func, $groupsort="@group desc" )
	{
		assert ( is_string($attribute) );
		assert ( is_string($groupsort) );
		assert ( $func==SPH_GROUPBY_DAY
			|| $func==SPH_GROUPBY_WEEK
			|| $func==SPH_GROUPBY_MONTH
			|| $func==SPH_GROUPBY_YEAR
			|| $func==SPH_GROUPBY_ATTR
			|| $func==SPH_GROUPBY_ATTRPAIR );

		$this->_groupby = $attribute;
		$this->_groupfunc = $func;
		$this->_groupsort = $groupsort;
	}

	/// set count-distinct attribute for group-by queries
	function SetGroupDistinct ( $attribute )
	{
		assert ( is_string($attribute) );
		$this->_groupdistinct = $attribute;
	}

	/// set distributed retries count and delay
	function SetRetries ( $count, $delay=0 )
	{
		assert ( is_int($count) && $count>=0 );
		assert ( is_int($delay) && $delay>=0 );
		$this->_retrycount = $count;
		$this->_retrydelay = $delay;
	}

	/// set result set format (hash or array; hash by default)
	/// PHP specific; needed for group-by-MVA result sets that may contain duplicate IDs
	function SetArrayResult ( $arrayresult )
	{
		assert ( is_bool($arrayresult) );
		$this->_arrayresult = $arrayresult;
	}

	/// set attribute values override
	/// there can be only one override per attribute
	/// $values must be a hash that maps document IDs to attribute values
	function SetOverride ( $attrname, $attrtype, $values )
	{
		assert ( is_string ( $attrname ) );
		assert ( in_array ( $attrtype, array ( SPH_ATTR_INTEGER, SPH_ATTR_TIMESTAMP, SPH_ATTR_BOOL, SPH_ATTR_FLOAT, SPH_ATTR_BIGINT ) ) );
		assert ( is_array ( $values ) );

		$this->_overrides[$attrname] = array ( "attr"=>$attrname, "type"=>$attrtype, "values"=>$values );
	}

	/// set select-list (attributes or expressions), SQL-like syntax
	function SetSelect ( $select )
	{
		assert ( is_string ( $select ) );
		$this->_select = $select;
	}

	function SetQueryFlag ( $flag_name, $flag_value )
	{
		$known_names = array ( "reverse_scan", "sort_method", "max_predicted_time", "boolean_simplify", "idf" );
		$flags = array (
		"reverse_scan" => array ( 0, 1 ),
		"sort_method" => array ( "pq", "kbuffer" ),
		"max_predicted_time" => array ( 0 ),
		"boolean_simplify" => array ( true, false ),
		"idf" => array ("normalized", "plain" )
		);

		assert ( isset ( $flag_name, $known_names ) );
		assert ( in_array( $flag_value, $flags[$flag_name], true ) || ( $flag_name=="max_predicted_time" && is_int ( $flag_value ) && $flag_value>=0 ) );

		if ( $flag_name=="reverse_scan" )	$this->_query_flags = sphSetBit ( $this->_query_flags, 0, $flag_value==1 );
		if ( $flag_name=="sort_method" )	$this->_query_flags = sphSetBit ( $this->_query_flags, 1, $flag_value=="kbuffer" );
		if ( $flag_name=="max_predicted_time" )
		{
			$this->_query_flags = sphSetBit ( $this->_query_flags, 2, $flag_value>0 );
			$this->_predictedtime = (int)$flag_value;
		}
		if ( $flag_name=="boolean_simplify" )	$this->_query_flags = sphSetBit ( $this->_query_flags, 3, $flag_value );
		if ( $flag_name=="idf" )	$this->_query_flags = sphSetBit ( $this->_query_flags, 4, $flag_value=="plain" );
	}

	/// set outer order by parameters
	function SetOuterSelect ( $orderby, $offset, $limit )
	{
		assert ( is_string($orderby) );
		assert ( is_int($offset) );
		assert ( is_int($limit) );
		assert ( $offset>=0 );
		assert ( $limit>0 );

		$this->_outerorderby = $orderby;
		$this->_outeroffset = $offset;
		$this->_outerlimit = $limit;
		$this->_hasouter = true;
	}

	//////////////////////////////////////////////////////////////////////////////

	/// clear all filters (for multi-queries)
	function ResetFilters ()
	{
		$this->_filters = array();
		$this->_anchor = array();
	}

	/// clear groupby settings (for multi-queries)
	function ResetGroupBy ()
	{
		$this->_groupby		= "";
		$this->_groupfunc	= SPH_GROUPBY_DAY;
		$this->_groupsort	= "@group desc";
		$this->_groupdistinct= "";
	}

	/// clear all attribute value overrides (for multi-queries)
	function ResetOverrides ()
    {
    	$this->_overrides = array ();
    }

	function ResetQueryFlag ()
	{
		$this->_query_flags = 0;
		$this->_predictedtime = 0;
	}

	function ResetOuterSelect ()
	{
		$this->_outerorderby = '';
		$this->_outeroffset = 0;
		$this->_outerlimit = 0;
		$this->_hasouter = false;
	}

	//////////////////////////////////////////////////////////////////////////////

	/// connect to searchd server, run given search query through given indexes,
	/// and return the search results
	function Query ( $query, $index="*", $comment="" )
	{
		assert ( empty($this->_reqs) );

		$this->AddQuery ( $query, $index, $comment );
		$results = $this->RunQueries ();
		$this->_reqs = array (); // just in case it failed too early

		if ( !is_array($results) )
			return false; // probably network error; error message should be already filled

		$this->_error = $results[0]["error"];
		$this->_warning = $results[0]["warning"];
		if ( $results[0]["status"]==SEARCHD_ERROR )
			return false;
		else
			return $results[0];
	}

	/// helper to pack floats in network byte order
	function _PackFloat ( $f )
	{
		$t1 = pack ( "f", $f ); // machine order
		list(,$t2) = unpack ( "L*", $t1 ); // int in machine order
		return pack ( "N", $t2 );
	}

	/// add query to multi-query batch
	/// returns index into results array from RunQueries() call
	function AddQuery ( $query, $index="*", $comment="" )
	{
		// mbstring workaround
		$this->_MBPush ();

		// build request
		$req = pack ( "NNNNN", $this->_query_flags, $this->_offset, $this->_limit, $this->_mode, $this->_ranker );
		if ( $this->_ranker==SPH_RANK_EXPR )
			$req .= pack ( "N", strlen($this->_rankexpr) ) . $this->_rankexpr;
		$req .= pack ( "N", $this->_sort ); // (deprecated) sort mode
		$req .= pack ( "N", strlen($this->_sortby) ) . $this->_sortby;
		$req .= pack ( "N", strlen($query) ) . $query; // query itself
		$req .= pack ( "N", count($this->_weights) ); // weights
		foreach ( $this->_weights as $weight )
			$req .= pack ( "N", (int)$weight );
		$req .= pack ( "N", strlen($index) ) . $index; // indexes
		$req .= pack ( "N", 1 ); // id64 range marker
		$req .= sphPackU64 ( $this->_min_id ) . sphPackU64 ( $this->_max_id ); // id64 range

		// filters
		$req .= pack ( "N", count($this->_filters) );
		foreach ( $this->_filters as $filter )
		{
			$req .= pack ( "N", strlen($filter["attr"]) ) . $filter["attr"];
			$req .= pack ( "N", $filter["type"] );
			switch ( $filter["type"] )
			{
				case SPH_FILTER_VALUES:
					$req .= pack ( "N", count($filter["values"]) );
					foreach ( $filter["values"] as $value )
						$req .= sphPackI64 ( $value );
					break;

				case SPH_FILTER_RANGE:
					$req .= sphPackI64 ( $filter["min"] ) . sphPackI64 ( $filter["max"] );
					break;

				case SPH_FILTER_FLOATRANGE:
					$req .= $this->_PackFloat ( $filter["min"] ) . $this->_PackFloat ( $filter["max"] );
					break;

				default:
					assert ( 0 && "internal error: unhandled filter type" );
			}
			$req .= pack ( "N", $filter["exclude"] );
		}

		// group-by clause, max-matches count, group-sort clause, cutoff count
		$req .= pack ( "NN", $this->_groupfunc, strlen($this->_groupby) ) . $this->_groupby;
		$req .= pack ( "N", $this->_maxmatches );
		$req .= pack ( "N", strlen($this->_groupsort) ) . $this->_groupsort;
		$req .= pack ( "NNN", $this->_cutoff, $this->_retrycount, $this->_retrydelay );
		$req .= pack ( "N", strlen($this->_groupdistinct) ) . $this->_groupdistinct;

		// anchor point
		if ( empty($this->_anchor) )
		{
			$req .= pack ( "N", 0 );
		} else
		{
			$a =& $this->_anchor;
			$req .= pack ( "N", 1 );
			$req .= pack ( "N", strlen($a["attrlat"]) ) . $a["attrlat"];
			$req .= pack ( "N", strlen($a["attrlong"]) ) . $a["attrlong"];
			$req .= $this->_PackFloat ( $a["lat"] ) . $this->_PackFloat ( $a["long"] );
		}

		// per-index weights
		$req .= pack ( "N", count($this->_indexweights) );
		foreach ( $this->_indexweights as $idx=>$weight )
			$req .= pack ( "N", strlen($idx) ) . $idx . pack ( "N", $weight );

		// max query time
		$req .= pack ( "N", $this->_maxquerytime );

		// per-field weights
		$req .= pack ( "N", count($this->_fieldweights) );
		foreach ( $this->_fieldweights as $field=>$weight )
			$req .= pack ( "N", strlen($field) ) . $field . pack ( "N", $weight );

		// comment
		$req .= pack ( "N", strlen($comment) ) . $comment;

		// attribute overrides
		$req .= pack ( "N", count($this->_overrides) );
		foreach ( $this->_overrides as $key => $entry )
		{
			$req .= pack ( "N", strlen($entry["attr"]) ) . $entry["attr"];
			$req .= pack ( "NN", $entry["type"], count($entry["values"]) );
			foreach ( $entry["values"] as $id=>$val )
			{
				assert ( is_numeric($id) );
				assert ( is_numeric($val) );

				$req .= sphPackU64 ( $id );
				switch ( $entry["type"] )
				{
					case SPH_ATTR_FLOAT:	$req .= $this->_PackFloat ( $val ); break;
					case SPH_ATTR_BIGINT:	$req .= sphPackI64 ( $val ); break;
					default:				$req .= pack ( "N", $val ); break;
				}
			}
		}

		// select-list
		$req .= pack ( "N", strlen($this->_select) ) . $this->_select;

		// max_predicted_time
		if ( $this->_predictedtime>0 )
			$req .= pack ( "N", (int)$this->_predictedtime );

		$req .= pack ( "N", strlen($this->_outerorderby) ) . $this->_outerorderby;
		$req .= pack ( "NN", $this->_outeroffset, $this->_outerlimit );
		if ( $this->_hasouter )
			$req .= pack ( "N", 1 );
		else
			$req .= pack ( "N", 0 );

		// mbstring workaround
		$this->_MBPop ();

		// store request to requests array
		$this->_reqs[] = $req;
		return count($this->_reqs)-1;
	}

	/// connect to searchd, run queries batch, and return an array of result sets
	function RunQueries ()
	{
		if ( empty($this->_reqs) )
		{
			$this->_error = "no queries defined, issue AddQuery() first";
			return false;
		}

		// mbstring workaround
		$this->_MBPush ();

		if (!( $fp = $this->_Connect() ))
		{
			$this->_MBPop ();
			return false;
		}

		// send query, get response
		$nreqs = count($this->_reqs);
		$req = join ( "", $this->_reqs );
		$len = 8+strlen($req);
		$req = pack ( "nnNNN", SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len, 0, $nreqs ) . $req; // add header

		if ( !( $this->_Send ( $fp, $req, $len+8 ) ) ||
			 !( $response = $this->_GetResponse ( $fp, VER_COMMAND_SEARCH ) ) )
		{
			$this->_MBPop ();
			return false;
		}

		// query sent ok; we can reset reqs now
		$this->_reqs = array ();

		// parse and return response
		return $this->_ParseSearchResponse ( $response, $nreqs );
	}

	/// parse and return search query (or queries) response
	function _ParseSearchResponse ( $response, $nreqs )
	{
		$p = 0; // current position
		$max = strlen($response); // max position for checks, to protect against broken responses

		$results = array ();
		for ( $ires=0; $ires<$nreqs && $p<$max; $ires++ )
		{
			$results[] = array();
			$result =& $results[$ires];

			$result["error"] = "";
			$result["warning"] = "";

			// extract status
			list(,$status) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
			$result["status"] = $status;
			if ( $status!=SEARCHD_OK )
			{
				list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
				$message = substr ( $response, $p, $len ); $p += $len;

				if ( $status==SEARCHD_WARNING )
				{
					$result["warning"] = $message;
				} else
				{
					$result["error"] = $message;
					continue;
				}
			}

			// read schema
			$fields = array ();
			$attrs = array ();

			list(,$nfields) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
			while ( $nfields-->0 && $p<$max )
			{
				list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
				$fields[] = substr ( $response, $p, $len ); $p += $len;
			}
			$result["fields"] = $fields;

			list(,$nattrs) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
			while ( $nattrs-->0 && $p<$max  )
			{
				list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
				$attr = substr ( $response, $p, $len ); $p += $len;
				list(,$type) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
				$attrs[$attr] = $type;
			}
			$result["attrs"] = $attrs;

			// read match count
			list(,$count) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
			list(,$id64) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;

			// read matches
			$idx = -1;
			while ( $count-->0 && $p<$max )
			{
				// index into result array
				$idx++;

				// parse document id and weight
				if ( $id64 )
				{
					$doc = sphUnpackU64 ( substr ( $response, $p, 8 ) ); $p += 8;
					list(,$weight) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
				}
				else
				{
					list ( $doc, $weight ) = array_values ( unpack ( "N*N*",
						substr ( $response, $p, 8 ) ) );
					$p += 8;
					$doc = sphFixUint($doc);
				}
				$weight = sprintf ( "%u", $weight );

				// create match entry
				if ( $this->_arrayresult )
					$result["matches"][$idx] = array ( "id"=>$doc, "weight"=>$weight );
				else
					$result["matches"][$doc]["weight"] = $weight;

				// parse and create attributes
				$attrvals = array ();
				foreach ( $attrs as $attr=>$type )
				{
					// handle 64bit ints
					if ( $type==SPH_ATTR_BIGINT )
					{
						$attrvals[$attr] = sphUnpackI64 ( substr ( $response, $p, 8 ) ); $p += 8;
						continue;
					}

					// handle floats
					if ( $type==SPH_ATTR_FLOAT )
					{
						list(,$uval) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
						list(,$fval) = unpack ( "f*", pack ( "L", $uval ) );
						$attrvals[$attr] = $fval;
						continue;
					}

					// handle everything else as unsigned ints
					list(,$val) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
					if ( $type==SPH_ATTR_MULTI )
					{
						$attrvals[$attr] = array ();
						$nvalues = $val;
						while ( $nvalues-->0 && $p<$max )
						{
							list(,$val) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
							$attrvals[$attr][] = sphFixUint($val);
						}
					} else if ( $type==SPH_ATTR_MULTI64 )
					{
						$attrvals[$attr] = array ();
						$nvalues = $val;
						while ( $nvalues>0 && $p<$max )
						{
							$attrvals[$attr][] = sphUnpackI64 ( substr ( $response, $p, 8 ) ); $p += 8;
							$nvalues -= 2;
						}
					} else if ( $type==SPH_ATTR_STRING )
					{
						$attrvals[$attr] = substr ( $response, $p, $val );
						$p += $val;
					} else if ( $type==SPH_ATTR_FACTORS )
					{
						$attrvals[$attr] = substr ( $response, $p, $val-4 );
						$p += $val-4;
					} else
					{
						$attrvals[$attr] = sphFixUint($val);
					}
				}

				if ( $this->_arrayresult )
					$result["matches"][$idx]["attrs"] = $attrvals;
				else
					$result["matches"][$doc]["attrs"] = $attrvals;
			}

			list ( $total, $total_found, $msecs, $words ) =
				array_values ( unpack ( "N*N*N*N*", substr ( $response, $p, 16 ) ) );
			$result["total"] = sprintf ( "%u", $total );
			$result["total_found"] = sprintf ( "%u", $total_found );
			$result["time"] = sprintf ( "%.3f", $msecs/1000 );
			$p += 16;

			while ( $words-->0 && $p<$max )
			{
				list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
				$word = substr ( $response, $p, $len ); $p += $len;
				list ( $docs, $hits ) = array_values ( unpack ( "N*N*", substr ( $response, $p, 8 ) ) ); $p += 8;
				$result["words"][$word] = array (
					"docs"=>sprintf ( "%u", $docs ),
					"hits"=>sprintf ( "%u", $hits ) );
			}
		}

		$this->_MBPop ();
		return $results;
	}

	/////////////////////////////////////////////////////////////////////////////
	// excerpts generation
	/////////////////////////////////////////////////////////////////////////////

	/// connect to searchd server, and generate exceprts (snippets)
	/// of given documents for given query. returns false on failure,
	/// an array of snippets on success
	function BuildExcerpts ( $docs, $index, $words, $opts=array() )
	{
		assert ( is_array($docs) );
		assert ( is_string($index) );
		assert ( is_string($words) );
		assert ( is_array($opts) );

		$this->_MBPush ();

		if (!( $fp = $this->_Connect() ))
		{
			$this->_MBPop();
			return false;
		}

		/////////////////
		// fixup options
		/////////////////

		if ( !isset($opts["before_match"]) )		$opts["before_match"] = "<b>";
		if ( !isset($opts["after_match"]) )			$opts["after_match"] = "</b>";
		if ( !isset($opts["chunk_separator"]) )		$opts["chunk_separator"] = " ... ";
		if ( !isset($opts["limit"]) )				$opts["limit"] = 256;
		if ( !isset($opts["limit_passages"]) )		$opts["limit_passages"] = 0;
		if ( !isset($opts["limit_words"]) )			$opts["limit_words"] = 0;
		if ( !isset($opts["around"]) )				$opts["around"] = 5;
		if ( !isset($opts["exact_phrase"]) )		$opts["exact_phrase"] = false;
		if ( !isset($opts["single_passage"]) )		$opts["single_passage"] = false;
		if ( !isset($opts["use_boundaries"]) )		$opts["use_boundaries"] = false;
		if ( !isset($opts["weight_order"]) )		$opts["weight_order"] = false;
		if ( !isset($opts["query_mode"]) )			$opts["query_mode"] = false;
		if ( !isset($opts["force_all_words"]) )		$opts["force_all_words"] = false;
		if ( !isset($opts["start_passage_id"]) )	$opts["start_passage_id"] = 1;
		if ( !isset($opts["load_files"]) )			$opts["load_files"] = false;
		if ( !isset($opts["html_strip_mode"]) )		$opts["html_strip_mode"] = "index";
		if ( !isset($opts["allow_empty"]) )			$opts["allow_empty"] = false;
		if ( !isset($opts["passage_boundary"]) )	$opts["passage_boundary"] = "none";
		if ( !isset($opts["emit_zones"]) )			$opts["emit_zones"] = false;
		if ( !isset($opts["load_files_scattered"]) )		$opts["load_files_scattered"] = false;

		/////////////////
		// build request
		/////////////////

		// v.1.2 req
		$flags = 1; // remove spaces
		if ( $opts["exact_phrase"] )	$flags |= 2;
		if ( $opts["single_passage"] )	$flags |= 4;
		if ( $opts["use_boundaries"] )	$flags |= 8;
		if ( $opts["weight_order"] )	$flags |= 16;
		if ( $opts["query_mode"] )		$flags |= 32;
		if ( $opts["force_all_words"] )	$flags |= 64;
		if ( $opts["load_files"] )		$flags |= 128;
		if ( $opts["allow_empty"] )		$flags |= 256;
		if ( $opts["emit_zones"] )		$flags |= 512;
		if ( $opts["load_files_scattered"] )	$flags |= 1024;
		$req = pack ( "NN", 0, $flags ); // mode=0, flags=$flags
		$req .= pack ( "N", strlen($index) ) . $index; // req index
		$req .= pack ( "N", strlen($words) ) . $words; // req words

		// options
		$req .= pack ( "N", strlen($opts["before_match"]) ) . $opts["before_match"];
		$req .= pack ( "N", strlen($opts["after_match"]) ) . $opts["after_match"];
		$req .= pack ( "N", strlen($opts["chunk_separator"]) ) . $opts["chunk_separator"];
		$req .= pack ( "NN", (int)$opts["limit"], (int)$opts["around"] );
		$req .= pack ( "NNN", (int)$opts["limit_passages"], (int)$opts["limit_words"], (int)$opts["start_passage_id"] ); // v.1.2
		$req .= pack ( "N", strlen($opts["html_strip_mode"]) ) . $opts["html_strip_mode"];
		$req .= pack ( "N", strlen($opts["passage_boundary"]) ) . $opts["passage_boundary"];

		// documents
		$req .= pack ( "N", count($docs) );
		foreach ( $docs as $doc )
		{
			assert ( is_string($doc) );
			$req .= pack ( "N", strlen($doc) ) . $doc;
		}

		////////////////////////////
		// send query, get response
		////////////////////////////

		$len = strlen($req);
		$req = pack ( "nnN", SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, $len ) . $req; // add header
		if ( !( $this->_Send ( $fp, $req, $len+8 ) ) ||
			 !( $response = $this->_GetResponse ( $fp, VER_COMMAND_EXCERPT ) ) )
		{
			$this->_MBPop ();
			return false;
		}

		//////////////////
		// parse response
		//////////////////

		$pos = 0;
		$res = array ();
		$rlen = strlen($response);
		for ( $i=0; $i<count($docs); $i++ )
		{
			list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) );
			$pos += 4;

			if ( $pos+$len > $rlen )
			{
				$this->_error = "incomplete reply";
				$this->_MBPop ();
				return false;
			}
			$res[] = $len ? substr ( $response, $pos, $len ) : "";
			$pos += $len;
		}

		$this->_MBPop ();
		return $res;
	}

	/////////////////////////////////////////////////////////////////////////////
	// keyword generation
	/////////////////////////////////////////////////////////////////////////////

	/// connect to searchd server, and generate keyword list for a given query
	/// returns false on failure,
	/// an array of words on success
	function BuildKeywords ( $query, $index, $hits )
	{
		assert ( is_string($query) );
		assert ( is_string($index) );
		assert ( is_bool($hits) );

		$this->_MBPush ();

		if (!( $fp = $this->_Connect() ))
		{
			$this->_MBPop();
			return false;
		}

		/////////////////
		// build request
		/////////////////

		// v.1.0 req
		$req  = pack ( "N", strlen($query) ) . $query; // req query
		$req .= pack ( "N", strlen($index) ) . $index; // req index
		$req .= pack ( "N", (int)$hits );

		////////////////////////////
		// send query, get response
		////////////////////////////

		$len = strlen($req);
		$req = pack ( "nnN", SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len ) . $req; // add header
		if ( !( $this->_Send ( $fp, $req, $len+8 ) ) ||
			 !( $response = $this->_GetResponse ( $fp, VER_COMMAND_KEYWORDS ) ) )
		{
			$this->_MBPop ();
			return false;
		}

		//////////////////
		// parse response
		//////////////////

		$pos = 0;
		$res = array ();
		$rlen = strlen($response);
		list(,$nwords) = unpack ( "N*", substr ( $response, $pos, 4 ) );
		$pos += 4;
		for ( $i=0; $i<$nwords; $i++ )
		{
			list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) );	$pos += 4;
			$tokenized = $len ? substr ( $response, $pos, $len ) : "";
			$pos += $len;

			list(,$len) = unpack ( "N*", substr ( $response, $pos, 4 ) );	$pos += 4;
			$normalized = $len ? substr ( $response, $pos, $len ) : "";
			$pos += $len;

			$res[] = array ( "tokenized"=>$tokenized, "normalized"=>$normalized );

			if ( $hits )
			{
				list($ndocs,$nhits) = array_values ( unpack ( "N*N*", substr ( $response, $pos, 8 ) ) );
				$pos += 8;
				$res [$i]["docs"] = $ndocs;
				$res [$i]["hits"] = $nhits;
			}

			if ( $pos > $rlen )
			{
				$this->_error = "incomplete reply";
				$this->_MBPop ();
				return false;
			}
		}

		$this->_MBPop ();
		return $res;
	}

	function EscapeString ( $string )
	{
		$from = array ( '\\', '(',')','|','-','!','@','~','"','&', '/', '^', '$', '=', '<' );
		$to   = array ( '\\\\', '\(','\)','\|','\-','\!','\@','\~','\"', '\&', '\/', '\^', '\$', '\=', '\<' );

		return str_replace ( $from, $to, $string );
	}

	/////////////////////////////////////////////////////////////////////////////
	// attribute updates
	/////////////////////////////////////////////////////////////////////////////

	/// batch update given attributes in given rows in given indexes
	/// returns amount of updated documents (0 or more) on success, or -1 on failure
	function UpdateAttributes ( $index, $attrs, $values, $mva=false, $ignorenonexistent=false )
	{
		// verify everything
		assert ( is_string($index) );
		assert ( is_bool($mva) );
		assert ( is_bool($ignorenonexistent) );

		assert ( is_array($attrs) );
		foreach ( $attrs as $attr )
			assert ( is_string($attr) );

		assert ( is_array($values) );
		foreach ( $values as $id=>$entry )
		{
			assert ( is_numeric($id) );
			assert ( is_array($entry) );
			assert ( count($entry)==count($attrs) );
			foreach ( $entry as $v )
			{
				if ( $mva )
				{
					assert ( is_array($v) );
					foreach ( $v as $vv )
						assert ( is_int($vv) );
				} else
					assert ( is_int($v) );
			}
		}

		// build request
		$this->_MBPush ();
		$req = pack ( "N", strlen($index) ) . $index;

		$req .= pack ( "N", count($attrs) );
		$req .= pack ( "N", $ignorenonexistent ? 1 : 0 );
		foreach ( $attrs as $attr )
		{
			$req .= pack ( "N", strlen($attr) ) . $attr;
			$req .= pack ( "N", $mva ? 1 : 0 );
		}

		$req .= pack ( "N", count($values) );
		foreach ( $values as $id=>$entry )
		{
			$req .= sphPackU64 ( $id );
			foreach ( $entry as $v )
			{
				$req .= pack ( "N", $mva ? count($v) : $v );
				if ( $mva )
					foreach ( $v as $vv )
						$req .= pack ( "N", $vv );
			}
		}

		// connect, send query, get response
		if (!( $fp = $this->_Connect() ))
		{
			$this->_MBPop ();
			return -1;
		}

		$len = strlen($req);
		$req = pack ( "nnN", SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len ) . $req; // add header
		if ( !$this->_Send ( $fp, $req, $len+8 ) )
		{
			$this->_MBPop ();
			return -1;
		}

		if (!( $response = $this->_GetResponse ( $fp, VER_COMMAND_UPDATE ) ))
		{
			$this->_MBPop ();
			return -1;
		}

		// parse response
		list(,$updated) = unpack ( "N*", substr ( $response, 0, 4 ) );
		$this->_MBPop ();
		return $updated;
	}

	/////////////////////////////////////////////////////////////////////////////
	// persistent connections
	/////////////////////////////////////////////////////////////////////////////

	function Open()
	{
		if ( $this->_socket !== false )
		{
			$this->_error = 'already connected';
			return false;
		}
		if ( !$fp = $this->_Connect() )
			return false;

		// command, command version = 0, body length = 4, body = 1
		$req = pack ( "nnNN", SEARCHD_COMMAND_PERSIST, 0, 4, 1 );
		if ( !$this->_Send ( $fp, $req, 12 ) )
			return false;

		$this->_socket = $fp;
		return true;
	}

	function Close()
	{
		if ( $this->_socket === false )
		{
			$this->_error = 'not connected';
			return false;
		}

		fclose ( $this->_socket );
		$this->_socket = false;

		return true;
	}

	//////////////////////////////////////////////////////////////////////////
	// status
	//////////////////////////////////////////////////////////////////////////

	function Status ()
	{
		$this->_MBPush ();
		if (!( $fp = $this->_Connect() ))
		{
			$this->_MBPop();
			return false;
		}

		$req = pack ( "nnNN", SEARCHD_COMMAND_STATUS, VER_COMMAND_STATUS, 4, 1 ); // len=4, body=1
		if ( !( $this->_Send ( $fp, $req, 12 ) ) ||
			 !( $response = $this->_GetResponse ( $fp, VER_COMMAND_STATUS ) ) )
		{
			$this->_MBPop ();
			return false;
		}

		$res = substr ( $response, 4 ); // just ignore length, error handling, etc
		$p = 0;
		list ( $rows, $cols ) = array_values ( unpack ( "N*N*", substr ( $response, $p, 8 ) ) ); $p += 8;

		$res = array();
		for ( $i=0; $i<$rows; $i++ )
			for ( $j=0; $j<$cols; $j++ )
		{
			list(,$len) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
			$res[$i][] = substr ( $response, $p, $len ); $p += $len;
		}

		$this->_MBPop ();
		return $res;
	}

	//////////////////////////////////////////////////////////////////////////
	// flush
	//////////////////////////////////////////////////////////////////////////

	function FlushAttributes ()
	{
		$this->_MBPush ();
		if (!( $fp = $this->_Connect() ))
		{
			$this->_MBPop();
			return -1;
		}

		$req = pack ( "nnN", SEARCHD_COMMAND_FLUSHATTRS, VER_COMMAND_FLUSHATTRS, 0 ); // len=0
		if ( !( $this->_Send ( $fp, $req, 8 ) ) ||
			 !( $response = $this->_GetResponse ( $fp, VER_COMMAND_FLUSHATTRS ) ) )
		{
			$this->_MBPop ();
			return -1;
		}

		$tag = -1;
		if ( strlen($response)==4 )
			list(,$tag) = unpack ( "N*", $response );
		else
			$this->_error = "unexpected response length";

		$this->_MBPop ();
		return $tag;
	}
}

//
// $Id$
//


<?php namespace Kharanenka\Helper;

use Config;
use System\Models\File;
use October\Rain\Database\Collection;

/**
 * Class DataFileModel
 * @package Kharanenka\Helper;
 * @author Andrey Kharanenka, kharanenka@gmail.com
 */
trait DataFileModel {

    /**
     * Get file data
     * @param string $sFieldName
     * @return array|null
     */
    public function getFileData($sFieldName) {

        if(empty($sFieldName)) {
            return null;
        }

        /** @var File $obFile */
        $obFile = $this->$sFieldName;
        if(empty($obFile) || !$obFile instanceof File) {
            return null;
        }

        return $this->getFileDataValue($obFile);
    }

    /**
     * Get file data value
     * @param File $obFile
     * @return array|null
     */
    protected function getFileDataValue($obFile) {

        if(empty($obFile) || !$obFile instanceof File) {
            return null;
        }

        $sUploadFolder = Config::get('cms.storage.uploads.path', '/storage/app/uploads');

        return [
            'full_path' => $obFile->getPath(),
            'path'      => $sUploadFolder . str_replace('uploads', '', $obFile->getDiskPath()),
            'title'     => $obFile->getAttribute('title'),
            'alt'       => $obFile->getAttribute('description'),
        ];
    }

    /**
     * Get file list data
     * @param $sFieldName
     * @return array
     */
    public function getFileListData($sFieldName) {

        if(empty($sFieldName)) {
            return [];
        }

        /** @var Collection $obFileList */
        $obFileList = $this->$sFieldName;
        if($obFileList->isEmpty()) {
            return [];
        }

        $arResult = [];
        /** @var File $obFile */
        foreach($obFileList as $obFile) {
            if(empty($obFile) || !$obFile instanceof File) {
                continue;
            }

            $arResult[] = $this->getFileDataValue($obFile);
        }

        return $arResult;
    }
}


<?php
/**
 *
 * User: davidlin
 * Date: 16/11/2013
 * Time: 11:11 PM
 *
 */

namespace Dlin\Zendesk\Search;

abstract class BaseFilter
{

    /**
     * @return string
     */
    abstract public function getType();

    /**
     * property values can either be a scalar value or an array with ':', '<', or '>' as keys
     *
     * if it is a scalar value, it is assumed a ':'
     *
     *
     * @return array
     */
    public function toArray()
    {
        $vars = get_object_vars($this);
        $vars['type'] = $this->getType();

        $queries = array();
        foreach ($vars as $k => $v) {
            if ($v !== null) {
                $values = is_array($v) ? $v : array(':'=>$v);
                foreach ($values as $matcher=>$value) {
                    $queries[] = $k . $matcher .  $value;
                }
            }
        }
        return $queries;
    }
}


<?php namespace Arcanesoft\Foundation\Http\Controllers\Admin;

/**
 * Class     SettingsController
 *
 * @package  Arcanesoft\Foundation\Http\Controllers\Admin
 * @author   ARCANEDEV <arcanedev.maroc@gmail.com>
 */
class SettingsController extends Controller
{
    /* -----------------------------------------------------------------
     |  Constructor
     | -----------------------------------------------------------------
     */

    /**
     * SettingsController constructor.
     */
    public function __construct()
    {
        parent::__construct();

        $this->setCurrentPage('foundation-settings');
        $this->addBreadcrumbRoute('Settings', 'admin::foundation.settings.index');
    }

    /* -----------------------------------------------------------------
     |  Main Methods
     | -----------------------------------------------------------------
     */

    public function index()
    {
        $title = 'Generals';
        $this->setTitle("Settings - {$title}");
        $this->addBreadcrumb($title);
        $this->setCurrentPage('foundation-settings-generals');

        return $this->view('admin.settings.index');
    }
}


<?php namespace Arcanesoft\Foundation\ViewComposers\System;

use Illuminate\View\View;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

/**
 * Class     ApplicationInfoComposer
 *
 * @package  Arcanesoft\Foundation\ViewComposers\System
 * @author   ARCANEDEV <arcanedev.maroc@gmail.com>
 */
class ApplicationInfoComposer
{
    /* -----------------------------------------------------------------
     |  Constants
     | -----------------------------------------------------------------
     */

    const VIEW = 'foundation::admin.system.information._includes.application';

    /* -----------------------------------------------------------------
     |  Main Methods
     | -----------------------------------------------------------------
     */

    /**
     * Compose the view.
     *
     * @param  \Illuminate\View\View  $view
     */
    public function compose(View $view)
    {
        $app = app();

        $view->with('application', [
            'url'                 => config('app.url'),
            'locale'              => strtoupper(config('app.locale')),
            'timezone'            => config('app.timezone'),
            'debug_mode'          => config('app.debug', false),
            'maintenance_mode'    => $app->isDownForMaintenance(),
            'arcanesoft_version'  => foundation()->version(),
            'laravel_version'     => $app->version(),
            'app_size'            => $this->getApplicationSize(),
            'database_connection' => config('database.default'),
            'cache_driver'        => config('cache.default'),
            'session_driver'      => config('session.driver')
        ]);
    }

    /* -----------------------------------------------------------------
     |  Other Methods
     | -----------------------------------------------------------------
     */

    /**
     * Get the application size.
     *
     * @return string
     */
    private function getApplicationSize()
    {
        $size = cache()->remember('foundation.app.size', 5, function () {
            return $this->getFolderSize(base_path());
        });

        return $this->formatSize($size);
    }

    /**
     * Get the folder size.
     *
     * @param  string  $path
     *
     * @return int
     */
    private function getFolderSize($path)
    {
        $size = 0;

        foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $directory) {
            /** @var  \SplFileInfo  $directory */
            $size += $directory->getSize();
        }

        return $size;
    }

    /**
     * Format the size for humans.
     *
     * @param  int  $bytes
     *
     * @return string
     */
    private function formatSize($bytes)
    {
        $kb = 1024;
        $mb = $kb * 1024;
        $gb = $mb * 1024;
        $tb = $gb * 1024;

        if (($bytes >= 0) && ($bytes < $kb)) {
            return $bytes . ' B';
        }
        elseif (($bytes >= $kb) && ($bytes < $mb)) {
            return ceil($bytes / $kb).' KB';
        }
        elseif (($bytes >= $mb) && ($bytes < $gb)) {
            return ceil($bytes / $mb).' MB';
        }
        elseif (($bytes >= $gb) && ($bytes < $tb)) {
            return ceil($bytes / $gb).' GB';
        }
        elseif ($bytes >= $tb) {
            return ceil($bytes / $tb).' TB';
        }

        return $bytes.' B';
    }
}


<?php

/*
 * This file is part of gpupo/search
 *
 * (c) Gilmar Pupo <contact@gpupo.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Gpupo\Search\Sphinx;

use Doctrine\Common\Collections\ArrayCollection;

class SphinxService
{
    protected static $_instance;

    protected $client;

    protected $parameters;

    public function setParameters(Array $customParameters = [])
    {
        $defaultParameters = [
            'host'    => 'localhost',
            'port'    => '9313',
            'timeout' => 5,
        ];

        $array = array_merge($defaultParameters, $customParameters);

        $this->parameters = new ArrayCollection($array);

        return $this;
    }

    public function getParameters()
    {
        if (!$this->parameters) {
            $this->setParameters();
        }

        return $this->parameters;
    }

    /**
     * Factory e Configuracao padrao de um SphinxClient.
     *
     * @return SphinxClient
     */
    public function createService()
    {
        $host = $this->getParameters()->get('host');
        $port = $this->getParameters()->get('port');
        $timeout = $this->getParameters()->get('timeout');

        if (is_null($host) || is_null($port)) {
            throw new \Exception(
                'No sphinx server information found within the configuration!'
            );
        }

        $sphinxClient = new SphinxClient();
        $sphinxClient->SetServer($host, $port);
        $sphinxClient->SetConnectTimeout($timeout);
        $sphinxClient->SetArrayResult(true);
        $sphinxClient->setMatchModeByModeName('any');
        $sphinxClient->SetSortMode(SPH_SORT_RELEVANCE);
        $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY);

        return $sphinxClient;
    }

    /**
     * Acesso ao Cliente Sphinx Server.
     *
     * @return Gpupo\Search\Sphinx\SphinxClient
     */
    public function getClient()
    {
        if (!$this->client) {
            $this->client = $this->createService();
        }

        return $this->client;
    }

    /**
     * Semelhante a *getClient()* mas com reset.
     *
     * @return Gpupo\Search\Sphinx\SphinxClient
     */
    public function getFreshClient()
    {
        return $this->reset()->getClient();
    }

    /**
     * Limpa informações de pesquisas anteriores.
     */
    public function reset()
    {
        $this->getClient()->ResetFilters();
        $this->getClient()->ResetGroupBy();
        $this->getClient()->ResetOverrides();

        return $this;
    }

    public static function getInstance()
    {
        if (!isset(self::$_instance)) {
            $class = get_called_class();
            self::$_instance = new $class();
        }

        return self::$_instance;
    }
}


<?php
/**
 *
 * User: davidlin
 * Date: 5/11/2013
 * Time: 10:03 AM
 *
 */

namespace Dlin\Zendesk\Client;

use Dlin\Zendesk\Entity\BaseEntity;
use Dlin\Zendesk\Entity\TicketAudit;
use Dlin\Zendesk\Exception\ZendeskException;
use Dlin\Zendesk\Result\ChangeResult;
use Dlin\Zendesk\Result\PaginatedResult;
use Dlin\Zendesk\ZendeskApi;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Service\Client;

abstract class BaseClient
{
    /**
     * @var \Dlin\Zendesk\ZendeskApi
     */
    protected $api;

    /**
     *
     * Constructor
     *
     * @param ZendeskApi $api
     */
    public function __construct(ZendeskApi $api)
    {
        $this->api = $api;
    }

    /**
     *
     * This is to make entity able to call update an delete methods
     *
     * @param BaseEntity $entity
     */
    public function manage(BaseEntity $entity){
        $entity->setManagingClient($this);
    }

    /**
     * @return string
     */
    abstract public function getType();

    /**
     * Generic function for getting a collection of entities
     *
     * @param $end_point
     * @param int $page
     * @param int $per_page
     * @param null $sort_by
     * @param string $sort_order
     * @return \Dlin\Zendesk\Result\PaginatedResult
     */
    public function getCollection($end_point, $collectionName,  $page = 1, $per_page = 100, $sort_by = null, $sort_order = 'asc')
    {
        $end_point = strtolower($end_point);
        if (strpos($end_point, 'http') !== 0) {
            $end_point = $this->api->getApiUrl() . $end_point;
        }

        $request = $this->api->get($end_point);
        $query = $request->getQuery()->set('page', $page)->set('per_page', $per_page);
        if ($sort_by) {
            $query->set('sort_by', $sort_by)->set('sort_order', $sort_order == 'asc' ? 'asc' : 'desc');
        }

        $response = $this->processRequest($request);

        $values = $response->json();

        $result = new PaginatedResult();
        $result->setClient($this);

        if (array_key_exists('count', $values)) {
            $result->setCount($values['count']);
        }

        /*
        if (array_key_exists('next_page', $values)) {
            $result->setNextPage($values['next_page']); //Note: the api skips the per_page parameter, making it useless
        }
        if (array_key_exists('previous_page', $values)) {
            $result->setPreviousPage($values['previous_page']); //Note: the api always return null
        }
        */

        $result->setCurrentPage($page);
        $result->setPerPage($per_page);
        $result->setEndPoint($end_point);

        $type = $this->getType();

        if (array_key_exists($collectionName, $values) && is_array($values[$collectionName])) {
            foreach ($values[$collectionName] as $value) {
                $entity = new $type();
                $this->manage($entity);
                $result[] = $entity->fromArray($value);
            }
        }
        return $result;

    }

    /**
     * Generic function for getting one entity
     *
     * @param $end_point
     * @return \Dlin\Zendesk\Entity\BaseEntity
     */
    protected function getOne($end_point)
    {

        $end_point = strtolower($end_point);
        if (strpos($end_point, 'http') !== 0) {
            $end_point = $this->api->getApiUrl() . $end_point;
        }

        $request = $this->api->get($end_point);
        $response = $this->processRequest($request);
        $result = $response->json();

        $type = $this->getType();
        $className = explode('\\', $type);
        $baseName = strtolower(end($className));

        if ($result && isset($result[$baseName])) {
            $t = new $type();
            $t->setManagingClient($this);
            return $t->fromArray($result[$baseName]);
        }
        return null;

    }

    /**
     * Create an entity
     *
     * @param BaseEntity $entity
     * @param $endPoint
     * @return ChangeResult|null
     */
    public function saveEntity(BaseEntity $entity, $endPoint='', $extraData=null)
    {
        $end_point = strtolower($endPoint);

        if (strpos($end_point, 'http') !== 0) {
            $end_point = $this->api->getApiUrl() . $end_point;
        }
        $type = $this->getType();
        $className = explode('\\', $type);
        $baseName = strtolower(end($className));

        $method = $entity->getId() ? 'put':'post';
        if($method == 'post'){
            $entity->checkCreatable();
        }

        $changes = $entity->toArray(true, $extraData);
        if(empty($changes)){
            return null;
        }

        $request = $this->api->$method($end_point, null, json_encode(array($baseName => $changes)));
        $response = $this->processRequest($request);
        $result = $response->json();

        if ($result && isset($result[$baseName])) {
            $changeResult = new ChangeResult();
            $t = new $type();
            $this->manage($t);
            $t->fromArray($result[$baseName]);
            $changeResult->setItem($t);
            if (isset($result['audit'])) {
                $audit = new TicketAudit();
                $audit->fromArray($result['audit']);
                $changeResult->setAudit($audit);
            }
            return $changeResult;
        }
        return null;
    }

    public function deleteById($id, $endPoint=''){

        $end_point = strtolower($endPoint).'/'.$id.'.json';

        if (strpos($end_point, 'http') !== 0) {
            $end_point = $this->api->getApiUrl() . $end_point;
        }
        $response = $this->api->delete($end_point)->send();
        return $response->getStatusCode() == 200;

    }

    public function deleteByIds(array $ids, $end_point=''){

        if (strpos($end_point, 'http') !== 0) {
            $end_point = $this->api->getApiUrl() . $end_point;
        }
        $request = $this->api->delete($end_point);
        $request->getQuery()->set('ids',implode(',',$ids) );
        $response=$request->send();
        return $response->getStatusCode() == 200;

    }

    /**
     * Process request into a response object
     *
     * @param RequestInterface $request
     * @return \Guzzle\Http\Message\Response
     * @throws \Dlin\Zendesk\Exception\ZendeskException
     */
    public function processRequest(RequestInterface $request)
    {

        $response = $request->send();

        $attempt = 0;
        while ($response->getStatusCode() == 429 && $attempt < 5) {
            $wait = $response->getHeader('Retry-After');
            if ($wait > 0) {
                sleep($wait);
            }
            $attempt++;
            $response = $request->send();
        }

        if ($response->getStatusCode() >= 500) {
            throw new ZendeskException('Zendesk Server Error Detected.');
        }

        if ($response->getStatusCode() >= 400) {
            if ($response->getContentType() == 'application/json') {
                $result = $response->json();

                $description = array_key_exists($result, 'description') ? $result['description'] : 'Invalid Request';
                $value = array_key_exists($result, 'value') ? $result['value'] : array();

                $exception = new ZendeskException($description);
                $exception->setError($value);

                throw $exception;

            } else {
                throw new ZendeskException('Invalid API Request');
            }
        }

        return $response;
    }

}


<?php
namespace Peridot\Plugin\Watcher;

use Evenement\EventEmitterInterface;
use Peridot\Configuration;
use Peridot\Console\Environment;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

class WatcherPlugin
{
    /**
     * @var EventEmitterInterface
     */
    protected $emitter;

    /**
     * @var array
     */
    protected $events;

    /**
     * @var WatcherInterface
     */
    protected $watcher;

    /**
     * @var array
     */
    protected $trackedPaths = [];

    /**
     * @var array
     */
    protected $criteria = ['/\.php$/'];

    /**
     * @param EventEmitterInterface $emitter
     */
    public function __construct(EventEmitterInterface $emitter)
    {
        $this->emitter = $emitter;
        $this->events = $this->getDefaultEvents();
        $this->listen();
    }

    /**
     * @param Configuration $config
     */
    public function onPeridotConfigure(Configuration $config)
    {
        $this->track($config->getPath());
    }

    /**
     * @param Environment $environment
     */
    public function onPeridotStart(Environment $environment)
    {
        $definition = $environment->getDefinition();
        $definition->option('watch', null, InputOption::VALUE_NONE, "watch tests for changes and re-run them");
    }

    /**
     * @param $exitCode
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    public function onPeridotEnd($exitCode, InputInterface $input, OutputInterface $output)
    {
        $this->emitter->removeListener('peridot.end', [$this, 'onPeridotEnd']);

        if (!$input->getOption('watch')) {
            return;
        }

        $watcher = $this->getWatcher();
        $watcher->setInput($input);
        $watcher->setOutput($output);
        $watcher->setCriteria($this->getFileCriteria());

        $this->watch($watcher);
    }

    /**
     * Return an array of default events to watch for
     *
     * @return array
     */
    public function getDefaultEvents()
    {
        return [WatcherInterface::MODIFY_EVENT];
    }

    /**
     * Return the array of events to watch for
     *
     * @return array
     */
    public function getEvents()
    {
        return $this->events;
    }

    /**
     * @param array $events
     */
    public function setEvents(array $events)
    {
        $filtered = array_filter($events, [$this, 'supportsEvent']);
        $this->events = array_values($filtered);
        if (empty($this->events)) {
            $this->events = $this->getDefaultEvents();
        }
        return $this;
    }

    /**
     * See if the supplied event identifier is supported by Watcher
     *
     * @param $eventId
     */
    public function supportsEvent($eventId)
    {
        $supportedEvents = [WatcherInterface::CREATE_EVENT, WatcherInterface::MODIFY_EVENT, WatcherInterface::DELETE_EVENT, WatcherInterface::ALL_EVENT];
        return array_search($eventId, $supportedEvents, true) !== false;
    }

    /**
     * @return WatcherInterface
     */
    public function getWatcher()
    {
        if (is_null($this->watcher)) {
            $this->watcher = new LurkerWatcher();
        }
        return $this->watcher;
    }

    /**
     * @param WatcherInterface $watcher
     */
    public function setWatcher(WatcherInterface $watcher)
    {
        $this->watcher = $watcher;
        return $this;
    }

    /**
     * Watch file events and rerun peridot when changes are received
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    public function watch(WatcherInterface $watcher)
    {
        $events = $this->getEvents();
        $watcher->watch($this->getTrackedPaths(), $events, [$this, 'runPeridot']);
    }

    /**
     * Track an additional path
     *
     * @param $path
     * @return $this
     */
    public function track($path)
    {
        $this->trackedPaths[] = $path;
        return $this;
    }

    /**
     * Run an isolated process of Peridot and feed results to the output interface.
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    public function runPeridot(InputInterface $input, OutputInterface $output)
    {
        global $argv;
        $command = $this->joinCommand($argv);
        $process = new Process($command);
        $process->run(function($type, $buffer) use ($output) {
            $buffer = preg_replace('/\[([\d]{1,2})m/', "\033[$1m", $buffer);
            $output->write($buffer);
        });
    }

    /**
     * Join an array of arg parts into a command.
     *
     * @param array $parts
     * @return string
     */
    public function joinCommand(array $parts)
    {
        $command = 'php ' . implode(' ', $parts);
        $stripped = str_replace('--watch', '', $command);
        return trim($stripped);
    }

    /**
     * Return all tracked paths
     *
     * @return array
     */
    public function getTrackedPaths()
    {
        return $this->trackedPaths;
    }

    /**
     * Return an array of regular expressions used for
     * matching a file type
     *
     * @return array
     */
    public function getFileCriteria()
    {
        return $this->criteria;
    }

    /**
     * @param $pattern
     * @return $this
     */
    public function addFileCriteria($pattern)
    {
        $this->criteria[] = $pattern;
        return $this;
    }

    /**
     * Listen for Peridot events
     */
    private function listen()
    {
        $this->emitter->on('peridot.configure', [$this, 'onPeridotConfigure']);
        $this->emitter->on('peridot.start', [$this, 'onPeridotStart']);
        $this->emitter->on('peridot.end', [$this, 'onPeridotEnd']);
    }
}


<?php namespace Arcanesoft\Foundation\Http\Controllers\Admin\System;

use Arcanedev\LaravelApiHelper\Traits\JsonResponses;
use Arcanedev\LogViewer\Contracts\LogViewer as LogViewerContract;
use Arcanedev\LogViewer\Entities\Log;
use Arcanedev\LogViewer\Entities\LogEntry;
use Arcanesoft\Foundation\Policies\LogViewerPolicy;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

/**
 * Class     LogViewerController
 *
 * @package  Arcanesoft\Foundation\Http\Controllers\Admin\System
 * @author   ARCANEDEV <arcanedev.maroc@gmail.com>
 */
class LogViewerController extends Controller
{
    /* -----------------------------------------------------------------
     |  Trait
     | -----------------------------------------------------------------
     */

    use JsonResponses;

    /* -----------------------------------------------------------------
     |  Properties
     | -----------------------------------------------------------------
     */

    /**
     * The LogViewer instance.
     *
     * @var \Arcanedev\LogViewer\Contracts\LogViewer
     */
    protected $logViewer;

    /**
     * Logs per page.
     *
     * @var int
     */
    protected $perPage = 30;

    /* -----------------------------------------------------------------
     |  Constructor
     | -----------------------------------------------------------------
     */

    /**
     * LogViewerController constructor.
     *
     * @param  \Arcanedev\LogViewer\Contracts\LogViewer  $logViewer
     */
    public function __construct(LogViewerContract $logViewer)
    {
        parent::__construct();

        $this->logViewer = $logViewer;
        $this->perPage   = config('arcanesoft.foundation.log-viewer.per-page', $this->perPage);
        $this->setCurrentPage('foundation-system-logviewer');
        $this->addBreadcrumbRoute('LogViewer', 'admin::foundation.system.log-viewer.index');
    }

    /* ------------------------------------------------------------------------------------------------
     |  Main Functions
     | ------------------------------------------------------------------------------------------------
     */

    /**
     * Show the LogViewer Dashboard.
     *
     * @return \Illuminate\View\View
     */
    public function index()
    {
        $this->authorize(LogViewerPolicy::PERMISSION_DASHBOARD);

        $stats    = $this->logViewer->statsTable();
        $percents = $this->calcPercentages($stats->footer(), $stats->header());

        $this->setTitle('LogViewer Dashboard');
        $this->addBreadcrumb('Dashboard');

        return $this->view('admin.system.log-viewer.dashboard', compact('percents'));
    }

    /**
     * List all logs.
     *
     * @param  \Illuminate\Http\Request  $request
     *
     * @return \Illuminate\View\View
     */
    public function listLogs(Request $request)
    {
        $this->authorize(LogViewerPolicy::PERMISSION_LIST);

        $stats   = $this->logViewer->statsTable();
        $headers = $stats->header();
        // $footer   = $stats->footer();

        $page    = $request->get('page', 1);
        $offset  = ($page * $this->perPage) - $this->perPage;
        $rows    = new LengthAwarePaginator(
            array_slice($stats->rows(), $offset, $this->perPage, true),
            count($stats->rows()),
            $this->perPage,
            $page
        );
        $rows->setPath($request->url());

        $this->setTitle($title = trans('foundation::log-viewer.titles.logs-list'));
        $this->addBreadcrumb($title);

        return $this->view('admin.system.log-viewer.list', compact('headers', 'rows', 'footer'));
    }

    /**
     * Show the log entries by date.
     *
     * @param  \Arcanedev\LogViewer\Entities\Log  $log
     *
     * @return \Illuminate\View\View
     */
    public function show(Log $log)
    {
        $this->authorize(LogViewerPolicy::PERMISSION_SHOW);

        $levels  = $this->logViewer->levelsNames();
        $entries = $log->entries($level = 'all')->paginate($this->perPage);

        $this->addBreadcrumbRoute(trans('foundation::log-viewer.titles.logs-list'), 'admin::foundation.system.log-viewer.logs.list');
        $this->setTitle($title = "Log : {$log->date}");
        $this->addBreadcrumb($title);

        return $this->view('admin.system.log-viewer.show', compact('log', 'levels', 'level', 'search', 'entries'));
    }

    /**
     * Filter the log entries by date and level.
     *
     * @param  \Arcanedev\LogViewer\Entities\Log  $log
     * @param  string                             $level
     *
     * @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
     */
    public function showByLevel(Log $log, $level)
    {
        $this->authorize(LogViewerPolicy::PERMISSION_SHOW);

        if ($level == 'all')
            return redirect()->route('admin::foundation.system.log-viewer.logs.show', [$log->date]);

        $levels  = $this->logViewer->levelsNames();
        $entries = $this->logViewer->entries($log->date, $level)->paginate($this->perPage);

        $this->addBreadcrumbRoute(trans('foundation::log-viewer.titles.logs-list'), 'admin::foundation.system.log-viewer.logs.list');

        $this->setTitle($log->date.' | '.ucfirst($level));
        $this->addBreadcrumbRoute($log->date, 'admin::foundation.system.log-viewer.logs.show', [$log->date]);
        $this->addBreadcrumb(ucfirst($level));

        return $this->view('admin.system.log-viewer.show', compact('log', 'levels', 'entries', 'level'));
    }

    /**
     * Show the log with the search query.
     *
     * @param  \Arcanedev\LogViewer\Entities\Log  $log
     * @param  string                             $level
     * @param  \Illuminate\Http\Request           $request
     *
     * @return \Illuminate\View\View
     */
    public function search(Log $log, $level = 'all', Request $request)
    {
        if (is_null($query = $request->get('query')))
            return redirect()->route('admin::foundation.system.log-viewer.logs.show', [$log->date]);

        $levels  = $this->logViewer->levelsNames();
        $entries = $log->entries($level)->filter(function (LogEntry $value) use ($query) {
            return Str::contains($value->header, $query);
        })->paginate($this->perPage);

        return $this->view('admin.system.log-viewer.show', compact('log', 'levels', 'level', 'query', 'entries'));
    }

    /**
     * Download the log.
     *
     * @param  \Arcanedev\LogViewer\Entities\Log  $log
     *
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function download(Log $log)
    {
        $this->authorize(LogViewerPolicy::PERMISSION_DOWNLOAD);

        return $this->logViewer->download($log->date);
    }

    /**
     * Delete a log.
     *
     * @param  \Arcanedev\LogViewer\Entities\Log  $log
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Log $log)
    {
        $this->authorize(LogViewerPolicy::PERMISSION_DELETE);

        $date = $log->date;

        if ($this->logViewer->delete($date)) {
            $this->notifySuccess(
                $message = trans('foundation::log-viewer.messages.deleted.message', compact('date')),
                trans('foundation::log-viewer.messages.deleted.title')
            );

            return $this->jsonResponseSuccess(compact('message'));
        }

        return $this->jsonResponseError([
            'message' => "An error occurred while deleting the log [$date]"
        ]);
    }

    /* -----------------------------------------------------------------
     |  Other Methods
     | -----------------------------------------------------------------
     */

    /**
     * Calculate the percentage.
     *
     * @param  array  $total
     * @param  array  $names
     *
     * @return array
     */
    private function calcPercentages(array $total, array $names)
    {
        $percents = [];
        $all      = Arr::get($total, 'all');

        foreach ($total as $level => $count) {
            $percents[$level] = [
                'name'    => $names[$level],
                'count'   => $count,
                'percent' => round(($count / $all) * 100, 2),
            ];
        }

        return $percents;
    }
}


<?php namespace Arcanesoft\Foundation\Http\Controllers\Admin\System;

use Arcanedev\RouteViewer\Contracts\RouteViewer;
use Arcanesoft\Foundation\Policies\RouteViewerPolicy;

/**
 * Class     RoutesController
 *
 * @package  Arcanesoft\Foundation\Http\Controllers\System
 * @author   ARCANEDEV <arcanedev.maroc@gmail.com>
 */
class RoutesController extends Controller
{
    /* -----------------------------------------------------------------
     |  Properties
     | -----------------------------------------------------------------
     */

    /**
     * The route viewer instance.
     *
     * @var \Arcanedev\RouteViewer\Contracts\RouteViewer
     */
    protected $routeViewer;

    /* -----------------------------------------------------------------
     |  Constructor
     | -----------------------------------------------------------------
     */

    /**
     * RoutesController constructor.
     *
     * @param  \Arcanedev\RouteViewer\Contracts\RouteViewer  $routeViewer
     */
    public function __construct(RouteViewer $routeViewer)
    {
        parent::__construct();

        $this->routeViewer = $routeViewer;

        $this->setCurrentPage('foundation-system-routes');
        $this->addBreadcrumbRoute(trans('foundation::route-viewer.titles.routes'), 'admin::foundation.system.routes.index');
    }

    /* -----------------------------------------------------------------
     |  Main Methods
     | -----------------------------------------------------------------
     */

    public function index()
    {
        $this->authorize(RouteViewerPolicy::PERMISSION_LIST);

        $this->setTitle($title = trans('foundation::route-viewer.titles.routes-list'));
        $this->addBreadcrumb($title);

        return $this->view('admin.system.routes.list', [
            'routes' => $this->routeViewer->all()
        ]);
    }
}


<?php namespace Arcanesoft\Foundation\Seeds;

use Arcanesoft\Auth\Seeds\PermissionsSeeder;
use Arcanesoft\Foundation\Policies\LogViewerPolicy;

/**
 * Class     PermissionTableSeeder
 *
 * @package  Arcanesoft\Foundation\Seeds
 * @author   ARCANEDEV <arcanedev.maroc@gmail.com>
 */
class PermissionsTableSeeder extends PermissionsSeeder
{
    /* -----------------------------------------------------------------
     |  Main Methods
     | -----------------------------------------------------------------
     */

    /**
     * Run the database seeds.
     */
    public function run()
    {
        $this->seed([
            [
                'group'       => [
                    'name'        => 'Foundation',
                    'slug'        => 'foundation',
                    'description' => 'Foundation permissions group',
                ],
                'permissions' => array_merge(
                    $this->getSettingsPermissions(),
                    $this->getLogViewerPermissions()
                ),
            ],
        ]);
    }

    /* -----------------------------------------------------------------
     |  Permissions
     | -----------------------------------------------------------------
     */

    /**
     * Get the Settings permissions.
     *
     * @return array
     */
    private function getSettingsPermissions()
    {
        return [
            [
                'name'        => 'Settings - View the general settings',
                'description' => 'Allow to view the general settings.',
                'slug'        => 'foundation.settings.general', // TODO: Create Settings Policies
            ],
        ];
    }

    /**
     * Get the LogViewer permissions.
     *
     * @return array
     */
    private function getLogViewerPermissions()
    {
        return [
            [
                'name'        => 'LogViewer - View dashboard',
                'description' => 'Allow to view the LogViewer dashboard.',
                'slug'        => LogViewerPolicy::PERMISSION_DASHBOARD,
            ],
            [
                'name'        => 'LogViewer - List all logs',
                'description' => 'Allow to list all the logs.',
                'slug'        => LogViewerPolicy::PERMISSION_LIST,
            ],
            [
                'name'        => 'LogViewer - View a log',
                'description' => 'Allow to display a log.',
                'slug'        => LogViewerPolicy::PERMISSION_SHOW,
            ],
            [
                'name'        => 'LogViewer - Download a log',
                'description' => 'Allow to download a log.',
                'slug'        => LogViewerPolicy::PERMISSION_DOWNLOAD,
            ],
            [
                'name'        => 'LogViewer - Delete a log',
                'description' => 'Allow to delete a log.',
                'slug'        => LogViewerPolicy::PERMISSION_DELETE,
            ],
        ];
    }
}


<?php

/*
 * This file is part of gpupo/search
 *
 * (c) Gilmar Pupo <contact@gpupo.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Gpupo\Search\Sphinx;

assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);

include dirname(__FILE__).'/sphinxapi.php';

/**
 * Acesso a API PHP Oficial do Sphinx.
 *
 * @see https://code.google.com/p/sphinxsearch/
 */
class SphinxClient extends \SphinxClient
{
    /**
     * {@inheritdoc }.
     *
     * SPH_MATCH_ALL, matches all query words (<b>default mode</b>);
     * SPH_MATCH_ANY, matches any of the query words;
     * SPH_MATCH_PHRASE, matches query as a phrase, requiring perfect match;
     * SPH_MATCH_BOOLEAN, matches query as a boolean expression;
     * SPH_MATCH_EXTENDED, matches query as an expression in Sphinx internal query language;
     * SPH_MATCH_FULLSCAN, matches query, forcibly using the "full scan" mode as below.
     *
     * @see http://sphinxsearch.com/docs/current.html#matching-modes Matching modes
     */
    public function SetMatchMode($mode)
    {
        return parent::SetMatchMode($mode);
    }

    /**
     * Uso amigavel da definicao de Match Mode.
     *
     * @param string $modeName all|any|phrase|boolean
     */
    public function setMatchModeByModeName($modeName)
    {
        $modes = [
            'all'       => 0,
            'any'       => 1,
            'phrase'    => 2,
            'boolean'   => 3,
            'extended'  => 4,
            'fullscan'  => 5,
        ];

        if (array_key_exists($modeName, $modes)) {
            $mode = $modes[$modeName];
            $this->SetMatchMode($mode);
        } else {
            throw new \LogicException('Wrong Mode');
        }
    }

    /**
     * Define a ordem de resultados.
     *
     * @param string $string
     */
    public function setSortExtended($string)
    {
        $this->SetSortMode(SPH_SORT_EXTENDED, $string);
    }

    /**
     * Agrupa resultados por atributo.
     *
     * @param string $attr
     */
    public function setGroupByAttr($attr)
    {
        $this->SetGroupBy($attr, SPH_GROUPBY_ATTR);
    }

    /**
     * Adiciona multiplas queries a partir de uma query
     * matriz e chaves usadas na busca facetada.
     *
     * Multi-queries are just a mechanism that lets you send several search
     * queries to searchd in one batch. That, in turn, lets searchd internally
     * optimize common parts between the queries. And that’s where the savings
     * come from.
     *
     * @see http://sphinxsearch.com/blog/2010/04/05/facets-multi-queries-and-searching-3x-faster/
     *
     * @param string $query
     * @param string $index
     * @param array  $keys
     */
    public function addFacetedQuery($query, $index, array $keys)
    {
        $this->AddQuery($query, $index);

        //Clear Offset
        $currentOffset = $this->_offset;
        $mode = $this->_sort;
        $sortby = $this->_sortby;
        $limit = $this->_limit;

        $this->_offset = 0;
        $this->_sort = 0;
        $this->_sortby = '';
        $this->SetLimits(0, 999);

        foreach ($keys as $key) {
            $this->setGroupByAttr($key);
            $this->AddQuery($query, $index);
        }

        //Reset
        $this->_offset = $currentOffset;
        $this->_sort = $mode;
        $this->_sortby = $sortby;
        $this->SetLimits($currentOffset, $limit);
    }
}


<?php
/**
 * Wrapper for default Magento 2 logger or for Cascaded Monolog logger.
 * User: Alex Gusev <alex@flancer64.com>
 */

namespace Praxigento\Logging;

use Cascade\Cascade;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\ObjectManagerInterface;
use Symfony\Component\Filesystem\Filesystem;

class Logger implements \Psr\Log\LoggerInterface
{
    const DEFAULT_LOGGER_NAME = 'main';
    /**
     * Logger (default M2 or Cascaded Monolog).
     *
     * @var \Psr\Log\LoggerInterface
     */
    private $_logger;
    private $_obm;

    /**
     * Logger constructor.
     * @param null $configFile
     * @param string $loggerName
     * @param ObjectManagerInterface $obm
     */
    public function __construct(
        ObjectManagerInterface $obm,
        $configFile = null,
        $loggerName = self::DEFAULT_LOGGER_NAME
    ) {
        $this->_obm = $obm;
        if (is_null($configFile)) {
            /* use default Magento 2 logger */
            $this->_logger = $this->_obm->get(\Magento\Framework\Logger\Monolog::class);
        } else {
            /* use Cascaded Monolog */
            $this->_initLoggerCascade($configFile, $loggerName);
        }
    }

    /**
     * Configure Cascaded Monolog logger and use it.
     *
     * @param        $configFile
     * @param string $loggerName
     */
    private function _initLoggerCascade($configFile, $loggerName)
    {
        $err = '';
        try {
            $fs = $this->_obm->get(Filesystem::class);
            if ($fs->isAbsolutePath($configFile)) {
                $fileName = $configFile;
            } else {
                $fileName = BP . '/' . $configFile;
            }
            $realPath = realpath($fileName);
            if ($realPath) {
                Cascade::fileConfig($realPath);
                $this->_logger = Cascade::getLogger($loggerName);
            } else {
                $err = "Cannot open logging configuration file '$fileName'. Default Magento logger is used.";
            }
        } catch (\Exception $e) {
            $err = $e->getMessage();
        } finally {
            if (is_null($this->_logger)) {
                $this->_logger = $this->_obm->get(\Magento\Framework\Logger\Monolog::class);
                $this->warning($err);
            }
        }
    }

    public function alert($message, array $context = [])
    {
        $this->_logger->alert($message, $context);
    }

    public function critical($message, array $context = [])
    {
        $this->_logger->critical($message, $context);
    }

    public function debug($message, array $context = [])
    {
        $this->_logger->debug($message, $context);
    }

    public function emergency($message, array $context = [])
    {
        $this->_logger->emergency($message, $context);
    }

    public function error($message, array $context = [])
    {
        $this->_logger->error($message, $context);
    }

    public function info($message, array $context = [])
    {
        $this->_logger->info($message, $context);
    }

    public function log($level, $message, array $context = [])
    {
        $this->_logger->log($level, $message, $context);
    }

    public function notice($message, array $context = [])
    {
        $this->_logger->notice($message, $context);
    }

    public function warning($message, array $context = [])
    {
        $this->_logger->warning($message, $context);
    }
}