idx
int64
0
60.3k
question
stringlengths
64
4.24k
target
stringlengths
5
618
10,600
protected function compileInitialization ( Buffer $ buffer ) { $ configurationLoader = new Configuration \ ConfigurationLoader ( $ this -> staticConfig , new FileLocator ( array ( getcwd ( ) ) ) , new Configuration \ FileLoader ( new Configuration \ PathDefaultFileLocator ( 'ZPLUGINPATH' , $ this -> staticPluginPaths ) ) ) ; $ compiler = new ContainerCompiler ( $ configurationLoader -> processConfiguration ( ) , array ( ) ) ; $ this -> phar [ 'container.php' ] = $ compiler -> compileContainerCode ( ) ; $ buffer -> writeln ( '$container = require_once \'phar://z.phar/container.php\';' ) ; foreach ( $ configurationLoader -> getPlugins ( ) as $ name => $ plugin ) { $ className = get_class ( $ plugin ) ; $ embeddedFilename = 'plugins/' . $ name . '.php' ; $ class = new \ ReflectionClass ( $ className ) ; $ this -> phar [ $ embeddedFilename ] = file_get_contents ( $ class -> getFileName ( ) ) ; $ buffer -> write ( 'require_once ' ) -> asPhp ( 'phar://z.phar/' . $ embeddedFilename ) -> raw ( ';' ) -> eol ( ) ; $ buffer -> write ( '$p = new ' ) -> write ( $ className ) -> raw ( '();' ) -> eol ( ) ; $ buffer -> writeln ( '$p->setContainer($container);' ) ; } $ buffer -> writeln ( 'Zicht\Tool\Application::$HEADER = \'\';' ) -> write ( '$app = new Zicht\Tool\Application(' ) -> asPhp ( $ this -> appName ) -> raw ( ', Zicht\Version\Version::fromString(' ) -> asPhp ( $ this -> appVersion ) -> raw ( ') ?: new Zicht\Version\Version());' ) -> eol ( ) ; $ buffer -> writeln ( '$app->setContainer($container);' ) ; }
Writes the initialization code for a static build of Z .
10,601
private static function getParamsToSign ( array $ params , array $ ignoreParamKeys = [ ] , int $ currentLevel = 1 , string $ prefix = '' ) : array { $ paramsToSign = [ ] ; foreach ( $ params as $ key => $ value ) { if ( ( in_array ( $ key , $ ignoreParamKeys ) && $ currentLevel == 1 ) ) { continue ; } $ paramKey = ( $ prefix ? $ prefix . ':' : '' ) . $ key ; if ( is_array ( $ value ) ) { if ( $ currentLevel >= self :: MAXIMUM_RECURSION_DEPTH ) { $ paramsToSign [ $ paramKey ] = ( string ) $ paramKey . ':' ; } else { $ subArray = self :: getParamsToSign ( $ value , $ ignoreParamKeys , $ currentLevel + 1 , $ paramKey ) ; $ paramsToSign = array_merge ( $ paramsToSign , $ subArray ) ; } } else { if ( is_bool ( $ value ) ) { $ value = $ value ? '1' : '0' ; } else { $ value = ( string ) $ value ; } $ paramsToSign [ $ paramKey ] = ( string ) $ paramKey . ':' . $ value ; } } if ( $ currentLevel == 1 ) { ksort ( $ paramsToSign , SORT_NATURAL ) ; } return $ paramsToSign ; }
Get parameters to sign
10,602
protected function addConstants ( $ constants , $ fileDescriptor ) { foreach ( $ constants as $ constant ) { $ constantDescriptor = $ this -> getBuilder ( ) -> buildDescriptor ( $ constant ) ; if ( $ constantDescriptor ) { $ constantDescriptor -> setLocation ( $ fileDescriptor , $ constant -> getLineNumber ( ) ) ; if ( count ( $ constantDescriptor -> getTags ( ) -> get ( 'package' , new Collection ( ) ) ) == 0 ) { $ constantDescriptor -> getTags ( ) -> set ( 'package' , $ fileDescriptor -> getTags ( ) -> get ( 'package' , new Collection ( ) ) ) ; } $ fileDescriptor -> getConstants ( ) -> set ( $ constantDescriptor -> getFullyQualifiedStructuralElementName ( ) , $ constantDescriptor ) ; } } }
Registers the child constants with the generated File Descriptor .
10,603
protected function addFunctions ( $ functions , $ fileDescriptor ) { foreach ( $ functions as $ function ) { $ functionDescriptor = $ this -> getBuilder ( ) -> buildDescriptor ( $ function ) ; if ( $ functionDescriptor ) { $ functionDescriptor -> setLocation ( $ fileDescriptor , $ function -> getLineNumber ( ) ) ; if ( count ( $ functionDescriptor -> getTags ( ) -> get ( 'package' , new Collection ( ) ) ) == 0 ) { $ functionDescriptor -> getTags ( ) -> set ( 'package' , $ fileDescriptor -> getTags ( ) -> get ( 'package' , new Collection ( ) ) ) ; } $ fileDescriptor -> getFunctions ( ) -> set ( $ functionDescriptor -> getFullyQualifiedStructuralElementName ( ) , $ functionDescriptor ) ; } } }
Registers the child functions with the generated File Descriptor .
10,604
protected function addInterfaces ( $ interfaces , $ fileDescriptor ) { foreach ( $ interfaces as $ interface ) { $ interfaceDescriptor = $ this -> getBuilder ( ) -> buildDescriptor ( $ interface ) ; if ( $ interfaceDescriptor ) { $ interfaceDescriptor -> setLocation ( $ fileDescriptor , $ interface -> getLineNumber ( ) ) ; if ( count ( $ interfaceDescriptor -> getTags ( ) -> get ( 'package' , new Collection ( ) ) ) == 0 ) { $ interfaceDescriptor -> getTags ( ) -> set ( 'package' , $ fileDescriptor -> getTags ( ) -> get ( 'package' , new Collection ( ) ) ) ; } $ fileDescriptor -> getInterfaces ( ) -> set ( $ interfaceDescriptor -> getFullyQualifiedStructuralElementName ( ) , $ interfaceDescriptor ) ; } } }
Registers the child interfaces with the generated File Descriptor .
10,605
protected function addTraits ( $ traits , $ fileDescriptor ) { foreach ( $ traits as $ trait ) { $ traitDescriptor = $ this -> getBuilder ( ) -> buildDescriptor ( $ trait ) ; if ( $ traitDescriptor ) { $ traitDescriptor -> setLocation ( $ fileDescriptor , $ trait -> getLineNumber ( ) ) ; if ( count ( $ traitDescriptor -> getTags ( ) -> get ( 'package' , new Collection ( ) ) ) == 0 ) { $ traitDescriptor -> getTags ( ) -> set ( 'package' , $ fileDescriptor -> getTags ( ) -> get ( 'package' , new Collection ( ) ) ) ; } $ fileDescriptor -> getTraits ( ) -> set ( $ traitDescriptor -> getFullyQualifiedStructuralElementName ( ) , $ traitDescriptor ) ; } } }
Registers the child traits with the generated File Descriptor .
10,606
protected function addMarkers ( $ markers , $ fileDescriptor ) { foreach ( $ markers as $ marker ) { list ( $ type , $ message , $ line ) = $ marker ; $ fileDescriptor -> getMarkers ( ) -> add ( array ( 'type' => $ type , 'message' => $ message , 'line' => $ line , ) ) ; } }
Registers the markers that were found in a File with the File Descriptor .
10,607
protected function bindNumericParameters ( ) : void { $ values = $ this -> getValues ( ) ; $ cnt = count ( $ values ) ; for ( $ index0 = 0 , $ index1 = 1 ; $ index0 < $ cnt ; $ index0 ++ , $ index1 ++ ) { $ value = $ this -> toScalar ( $ values [ $ index0 ] ) ; $ this -> getWrappedStatement ( ) -> bindValue ( $ index1 , $ value , $ this -> getPdoType ( $ value ) ) ; } }
Bind ? parameters
10,608
protected function loop ( ) { while ( $ this -> a !== false && ! is_null ( $ this -> a ) && $ this -> a !== '' ) { switch ( $ this -> a ) { case "\n" : if ( strpos ( '(-+{[@' , $ this -> b ) !== false ) { echo $ this -> a ; $ this -> saveString ( ) ; break ; } if ( $ this -> b === ' ' ) break ; case ' ' : if ( static :: isAlphaNumeric ( $ this -> b ) ) echo $ this -> a ; $ this -> saveString ( ) ; break ; default : switch ( $ this -> b ) { case "\n" : if ( strpos ( '}])+-"\'' , $ this -> a ) !== false ) { echo $ this -> a ; $ this -> saveString ( ) ; break ; } else { if ( static :: isAlphaNumeric ( $ this -> a ) ) { echo $ this -> a ; $ this -> saveString ( ) ; } } break ; case ' ' : if ( ! static :: isAlphaNumeric ( $ this -> a ) ) break ; default : if ( $ this -> a === '/' && ( $ this -> b === '\'' || $ this -> b === '"' ) ) { $ this -> saveRegex ( ) ; continue ; } echo $ this -> a ; $ this -> saveString ( ) ; break ; } } $ this -> b = $ this -> getReal ( ) ; if ( ( $ this -> b == '/' && strpos ( '(,=:[!&|?' , $ this -> a ) !== false ) ) $ this -> saveRegex ( ) ; } }
The primary action occurs here . This function loops through the input string outputting anything that s relevant and discarding anything that is not .
10,609
protected function getChar ( ) { if ( isset ( $ this -> c ) ) { $ char = $ this -> c ; unset ( $ this -> c ) ; } else { $ char = substr ( $ this -> input , $ this -> index , 1 ) ; if ( isset ( $ char ) && $ char === false ) { return false ; } $ this -> index ++ ; } if ( $ char !== "\n" && ord ( $ char ) < 32 ) return ' ' ; return $ char ; }
Returns the next string for processing based off of the current index .
10,610
public function get ( ) { if ( ! isset ( $ this -> data ) && isset ( $ this -> session [ $ this -> key ] ) ) { $ this -> data = $ this -> session [ $ this -> key ] ; unset ( $ this -> session [ $ this -> key ] ) ; } return $ this -> data ? ( object ) $ this -> data : null ; }
Get the flash .
10,611
public function reissue ( ) { if ( ! isset ( $ this -> data ) && isset ( $ this -> session [ $ this -> key ] ) ) { $ this -> data = $ this -> session [ $ this -> key ] ; } else { $ this -> session [ $ this -> key ] = $ this -> data ; } }
Reissue the flash .
10,612
final protected function build_query ( ) { $ select = empty ( $ this -> _select ) ? $ this -> default_select ( ) : implode ( ', ' , $ this -> _select ) ; $ calc = $ this -> _calc_rows ? ' SQL_CALC_FOUND_ROWS' : '' ; $ distinct = $ this -> _distinct ? sprintf ( 'DISTINCT(%s)' , $ this -> escape_column_name ( $ this -> _distinct ) ) : '' ; $ from = empty ( $ this -> _from ) ? $ this -> table : $ this -> _from ; $ sql = <<<SQL SELECT{$calc} {$distinct} {$select} FROM {$from}SQL ; $ join = [ ] ; if ( ! empty ( $ this -> _join ) ) { $ join = $ this -> _join ; } else { $ default_join = $ this -> default_join ( ) ; if ( ! empty ( $ default_join ) ) { $ join = $ default_join ; } } foreach ( $ join as list ( $ table , $ on , $ method ) ) { $ method = 'INNER' === strtoupper ( $ method ) ? 'INNER' : 'LEFT' ; $ sql .= <<<SQL {$method} JOIN {$table} ON {$on}SQL ; } if ( ! empty ( $ this -> _wheres ) ) { $ where_clause = $ this -> build_where ( $ this -> _wheres ) ; $ sql .= <<<SQL WHERE {$where_clause}SQL ; } if ( ! empty ( $ this -> _group_by ) ) { $ group_by = implode ( ', ' , $ this -> _group_by ) ; $ sql .= <<<SQL GROUP BY {$group_by}SQL ; } if ( ! empty ( $ this -> _order_by ) ) { $ order_by = implode ( ', ' , $ this -> _order_by ) ; $ sql .= <<<SQL ORDER BY {$order_by}SQL ; } if ( ! empty ( $ this -> _limit ) ) { list ( $ per_page , $ offset ) = $ this -> _limit ; $ limit = <<<SQL LIMIT %d, %dSQL ; $ sql .= sprintf ( $ limit , $ offset , $ per_page ) ; } return $ sql ; }
Build query with current setting
10,613
private function build_where ( array $ where_array ) { $ where_clause = [ ] ; $ counter = 0 ; foreach ( $ where_array as list ( $ glue , $ where ) ) { if ( $ counter ) { $ where_clause [ ] = $ glue ; } if ( is_array ( $ where ) ) { $ where_clause [ ] = $ this -> build_where ( $ where_array ) ; } else { $ where_clause [ ] = sprintf ( '(%s)' , $ where ) ; } $ counter ++ ; } return sprintf ( '(%s)' , implode ( ' ' , $ where_clause ) ) ; }
Build where clause
10,614
final protected function select ( $ name , $ escape = true ) { $ this -> _select [ ] = $ escape ? $ this -> escape_column_name ( $ name ) : $ name ; return $ this ; }
Set select param
10,615
final protected function where_not_null ( $ column , $ or = false ) { $ this -> _wheres [ ] = [ $ this -> and_or ( $ or ) , sprintf ( "%s IS NOT NULL" , $ this -> escape_column_name ( $ column ) ) ] ; return $ this ; }
Add where not null clause
10,616
final protected function where_like ( $ column , $ replace , $ position = 'both' , $ or = false ) { switch ( strtolower ( $ position ) ) { case 'left' : $ replace = '%' . $ replace ; break ; case 'right' : $ replace = $ replace . '%' ; break ; default : $ replace = '%' . $ replace . '%' ; break ; } $ column = $ this -> escape_column_name ( $ column ) ; $ this -> _wheres [ ] = [ $ this -> and_or ( $ or ) , $ this -> db -> prepare ( "{$column} LIKE %s" , $ replace ) ] ; return $ this ; }
Add Where LIKE
10,617
final protected function wheres ( array $ wheres , $ or = false ) { foreach ( $ wheres as $ where => $ replace ) { $ this -> where ( $ where , $ replace , $ or ) ; } return $ this ; }
Add where in one shot
10,618
final protected function group_by ( $ column , $ order = 'ASC' ) { $ order = 'DESC' === strtoupper ( $ order ) ? 'DESC' : 'ASC' ; $ this -> _group_by [ ] = sprintf ( '%s %s' , $ this -> escape_column_name ( $ column ) , $ order ) ; return $ this ; }
Add group by
10,619
final protected function order_by ( $ column , $ order = 'ASC' ) { $ order = 'DESC' === strtoupper ( $ order ) ? 'DESC' : 'ASC' ; $ this -> _order_by [ ] = sprintf ( '%s %s' , $ this -> escape_column_name ( $ column ) , $ order ) ; return $ this ; }
Add order by
10,620
private function escape_column_name ( $ column_names ) { $ columns = [ ] ; foreach ( explode ( ',' , $ column_names ) as $ column_name ) { $ column_name = trim ( $ column_name ) ; $ columns [ ] = preg_replace ( '/[`\'";\n\r\\\\\\0]/u' , '' , $ column_name ) ; } return implode ( ', ' , $ columns ) ; }
Wrap table name with backtick
10,621
final protected function clear ( ) { $ this -> _distinct = false ; $ this -> _select = [ ] ; $ this -> _from = '' ; $ this -> _join = [ ] ; $ this -> _wheres = [ ] ; $ this -> _group_by = [ ] ; $ this -> _order_by = [ ] ; $ this -> _limit = [ ] ; }
Clear all result
10,622
final protected function cache_query ( $ sql , $ result ) { if ( isset ( $ this -> query_cache [ $ sql ] ) ) { $ this -> query_cache [ $ sql ] = $ result ; } elseif ( count ( $ this -> query_cache ) >= $ this -> cache_limit ) { array_shift ( $ this -> query_cache ) ; $ this -> query_cache [ $ sql ] = $ result ; } }
Save query cache
10,623
public function create ( array $ users , int $ public = 0 ) : ? string { if ( $ this -> check ( ) ) { $ join = '' ; $ where = '' ; $ values = [ $ this -> user -> get_id ( ) , $ public ] ; foreach ( $ users as $ i => $ u ) { $ join .= "JOIN bbn_chats_users AS u$i ON u$i.id_chat = bbn_chats.id" . PHP_EOL ; $ where .= "AND u$i.id_user = ?" . PHP_EOL ; $ values [ ] = $ u ; } $ sql = <<<SQLSELECT idFROM bbn_chats $joinWHERE creator = ?AND public = ? $whereSQL ; if ( ( $ id_chat = $ this -> db -> get_one ( $ sql , $ values ) ) && ( count ( $ users ) === $ this -> db -> count ( 'bbn_chat_users' , [ 'id_chat' => $ id_chat ] ) ) ) { return $ id_chat ; } if ( $ this -> db -> insert ( 'bbn_chats' , [ 'creator' => $ this -> user -> get_id ( ) , 'creation' => date ( 'Y-m-d H:i:s' ) , 'public' => $ public ? 1 : 0 ] ) ) { $ id_chat = $ this -> db -> last_id ( ) ; $ this -> db -> insert ( 'bbn_chats_users' , [ 'id_chat' => $ id_chat , 'id_user' => $ this -> user -> get_id ( ) , 'entrance' => microtime ( true ) , 'admin' => 1 ] ) ; foreach ( $ users as $ user ) { $ this -> db -> insert_ignore ( 'bbn_chats_users' , [ 'id_chat' => $ id_chat , 'id_user' => $ user , 'entrance' => microtime ( true ) , 'admin' => 0 ] ) ; } return $ id_chat ; } } return null ; }
Creates a new chat with the current userr and another participant .
10,624
public function make_admin ( string $ id_chat , string $ id_user ) : bool { if ( $ this -> is_admin ( $ id_chat ) ) { return ( bool ) $ this -> db -> update_ignore ( 'bbn_chats_users' , [ 'admin' => 1 ] , [ 'id_chat' => $ id_chat , 'id_user' => $ id_user , ] ) ; } return false ; }
Makes the given participant an admin of the given chat provided the current user is admin of this chat .
10,625
public function is_participant ( string $ id_chat , string $ id_user = null ) : ? bool { if ( $ this -> check ( ) ) { return ( bool ) $ this -> db -> count ( 'bbn_chats_users' , [ 'id_chat' => $ id_chat , 'id_user' => $ id_user ? : $ this -> user -> get_id ( ) ] ) ; } return null ; }
Checks whether the given user is participant of the given chat .
10,626
public function info ( $ id_chat ) : ? array { if ( $ this -> check ( ) ) { return $ this -> db -> rselect ( 'bbn_chats' , [ ] , [ 'id' => $ id_chat ] ) ? : null ; } return null ; }
Gets information about the given chat .
10,627
public function get_participants ( string $ id_chat ) : ? array { if ( $ this -> check ( ) ) { return $ this -> db -> get_field_values ( 'bbn_chats_users' , 'id_user' , [ 'id_chat' => $ id_chat ] ) ; } return null ; }
Returns the participants of the given chat as an array of id_user .
10,628
public function talk ( string $ id_chat , string $ message ) : ? int { if ( $ this -> check ( ) && ( $ chat = $ this -> info ( $ id_chat ) ) && ! $ chat [ 'blocked' ] ) { $ users = $ this -> get_participants ( $ id_chat ) ; if ( \ in_array ( $ this -> user -> get_id ( ) , $ users , true ) ) { $ time = microtime ( true ) ; $ st = bbn \ util \ enc :: crypt ( json_encode ( [ 'time' => $ time , 'user' => $ this -> user -> get_id ( ) , 'message' => $ message ] ) ) ; $ day = date ( 'Y-m-d' ) ; foreach ( $ users as $ user ) { $ dir = BBN_DATA_PATH . 'users/' . $ user . '/chat/' . $ id_chat . '/' . $ day ; if ( bbn \ file \ dir :: create_path ( $ dir ) ) { file_put_contents ( $ dir . '/' . $ time . '.msg' , $ st ) ; } } return $ this -> db -> update ( 'bbn_chats' , [ 'last_message' => $ time ] , [ 'id' => $ id_chat ] ) ; } } return null ; }
Sends a new message from the current user in the given chat .
10,629
public function is_admin ( $ id_chat ) : ? bool { if ( $ this -> check ( ) && ( $ chat = $ this -> info ( $ id_chat ) ) && ! $ chat [ 'blocked' ] ) { return ( bool ) $ this -> db -> count ( 'bbn_chats_users' , [ 'id_chat' => $ id_chat , 'id_user' => $ this -> user -> get_id ( ) , 'admin' => 1 ] ) ; } return null ; }
Checks whether the current user is an admin of the given chat or not .
10,630
public function block ( $ id_chat ) : bool { if ( $ this -> is_admin ( $ id_chat ) ) { return ( bool ) $ this -> db -> update ( 'bbn_chats' , [ 'blocked' => 1 ] , [ 'id' => $ id_chat ] ) ; } return false ; }
Close a chat by setting blocked to 1 .
10,631
public static function setApiVersion ( $ version ) { if ( ! in_array ( $ version , [ 1 , 2 ] ) ) { throw new \ Exception ( sprintf ( "The %d version is not supported" , $ version ) ) ; } self :: $ apiVersion = $ version ; if ( self :: $ url ) { self :: initServices ( ) ; } }
Define which API version to use
10,632
public static function initServices ( $ refresh = false ) { $ path = sprintf ( '%s%sxac-services-v%d.conf' , ! empty ( self :: $ logPath ) ? self :: $ logPath : sys_get_temp_dir ( ) , DIRECTORY_SEPARATOR , self :: $ apiVersion ) ; if ( $ refresh === true || ! file_exists ( $ path ) ) { self :: log ( sprintf ( "Refresh services definition on-demand for v%d API" , self :: $ apiVersion ) ) ; $ res = self :: getInstance ( ) -> call ( 'endpoints' ) ; $ res [ '_timestamp' ] = time ( ) ; file_put_contents ( $ path , serialize ( $ res ) ) ; chmod ( $ path , 0777 ) ; self :: $ services = $ res ; } else { self :: $ services = unserialize ( file_get_contents ( $ path ) ) ; if ( time ( ) - self :: $ services [ '_timestamp' ] > self :: $ ttl ) { self :: log ( sprintf ( "Refresh outdated services definition for v%d API" , self :: $ apiVersion ) ) ; self :: initServices ( true ) ; } } }
Retrieve definition of available API services
10,633
public function call ( $ endpoint , $ method = 'GET' , array $ data = [ ] ) { if ( substr ( $ endpoint , - 1 ) == '/' ) { $ endpoint = substr ( $ endpoint , 0 , strlen ( $ endpoint ) - 1 ) ; } self :: log ( "Calling $method /api/$endpoint" ) ; $ params = [ 'headers' => [ 'Accept' => sprintf ( 'application/vnd.xac.v%d+json' , self :: $ apiVersion ) , 'AC-API-KEY' => self :: $ apiKey , ] , ] ; if ( count ( $ data ) > 0 ) { $ params [ 'json' ] = $ data ; } try { $ this -> initTransport ( ) ; $ res = $ this -> transport -> request ( $ method , $ endpoint , $ params ) ; } catch ( ConnectException $ e ) { self :: log ( $ e -> getMessage ( ) , Logger :: ERROR ) ; throw new \ Exception ( $ e -> getMessage ( ) ) ; } catch ( ServerException $ e ) { $ response = json_decode ( ( string ) $ e -> getResponse ( ) -> getBody ( ) , true ) ; self :: $ lastResponse = $ response [ 'ACResponse' ] ; self :: $ lastResponse [ 'httpCode' ] = $ e -> getResponse ( ) -> getStatusCode ( ) ; self :: log ( $ e -> getMessage ( ) , Logger :: ERROR ) ; throw new \ Exception ( self :: $ lastResponse [ 'message' ] ) ; } catch ( ClientException $ e ) { $ response = json_decode ( ( string ) $ e -> getResponse ( ) -> getBody ( ) , true ) ; self :: $ lastResponse = $ response [ 'ACResponse' ] ; self :: $ lastResponse [ 'httpCode' ] = $ e -> getResponse ( ) -> getStatusCode ( ) ; self :: log ( $ e -> getMessage ( ) , Logger :: ERROR ) ; throw new \ Exception ( self :: $ lastResponse [ 'message' ] ) ; } finally { if ( isset ( $ res ) ) { $ response = json_decode ( ( string ) $ res -> getBody ( ) , ! self :: $ returnObject ) ; $ response = $ response [ 'ACResponse' ] ; } } self :: $ lastResponse = $ response ; return $ response [ 'body' ] [ 'data' ] ; }
Make a HTTP Request and return response
10,634
public static function log ( $ message , $ level = Logger :: INFO ) { if ( ! self :: $ logger ) { $ logfile = sprintf ( '%s%sarrowsphere-client.log' , sys_get_temp_dir ( ) , DIRECTORY_SEPARATOR ) ; self :: $ logger = new Logger ( 'xAC' ) ; self :: $ logger -> pushHandler ( new StreamHandler ( $ logfile , Logger :: INFO ) ) ; } return self :: $ logger -> addRecord ( $ level , $ message ) ; }
Log messages to client log file
10,635
public static function getInstance ( HttpClient $ transport = null ) { if ( is_null ( self :: $ client ) ) { self :: $ client = new self ( ) ; if ( ! is_null ( $ transport ) ) { self :: $ client -> setTransport ( $ transport ) ; } } return self :: $ client ; }
Return a singleton instance of the class
10,636
public static function getServicesList ( ) { $ map = [ 'GET' => 'get' , 'POST' => 'create' , 'PUT' => 'update' , 'DELETE' => 'delete' , ] ; $ table = new \ Console_Table ( ) ; $ table -> setHeaders ( [ 'Method' , 'Type' , 'Description' , 'Parameters' ] ) ; foreach ( self :: getServices ( ) as $ key => $ service ) { if ( $ service [ 'type' ] == 'entity' ) { foreach ( $ service [ 'actions' ] as $ action ) { $ table -> addRow ( [ sprintf ( 'xAC::%s()->%s()' , $ service [ 'endpoint' ] , $ action [ 'endpoint' ] == 'default' ? $ map [ $ action [ 'method' ] ] : $ action [ 'endpoint' ] ) , 'entity/' . $ action [ 'type' ] , $ action [ 'description' ] ] ) ; } } else { $ table -> addRow ( [ 'xAC::' . $ service [ 'endpoint' ] . '()->get()' , $ service [ 'type' ] , $ service [ 'description' ] ] ) ; } } echo $ table -> getTable ( ) ; }
Display a list of available service on console
10,637
public static function load ( $ file ) { add_filter ( 'calderawp_file_locator_allow_extensions' , function ( $ allowed , $ context ) { if ( self :: $ context === $ context ) { $ allowed = array ( 'html' , 'htm' ) ; } return $ allowed ; } , 10 , 2 ) ; $ file = file_locator :: locate ( $ file , self :: $ context , true ) ; if ( is_string ( $ file ) ) { return array ( 'html' => array ( 'code' => file_get_contents ( $ file ) ) ) ; } }
Attempts to load a file and if so puts its contents in an array as expected by Metaplate
10,638
public function merge ( array $ newOptions ) { foreach ( $ newOptions as $ key => $ value ) { $ this -> __set ( $ key , $ value ) ; } }
Merge an array into the actual options object . This method merges an array of new options into the actual options object .
10,639
public function invoke ( callable $ function , array $ parameters = [ ] ) { $ reflection = $ this -> reflectionTools -> getReflectionFunction ( $ function ) ; $ parameters = $ this -> getFunctionParameters ( $ reflection , $ parameters ) ; return call_user_func_array ( $ function , $ parameters ) ; }
Invokes a function after resolving its parameters .
10,640
public function instantiate ( string $ class , array $ parameters = [ ] ) { $ class = new \ ReflectionClass ( $ class ) ; $ instance = $ class -> newInstanceWithoutConstructor ( ) ; $ this -> inject ( $ instance ) ; $ constructor = $ class -> getConstructor ( ) ; if ( $ constructor ) { $ parameters = $ this -> getFunctionParameters ( $ constructor , $ parameters ) ; $ constructor -> setAccessible ( true ) ; $ constructor -> invokeArgs ( $ instance , $ parameters ) ; } return $ instance ; }
Instantiates a class by resolving its constructor parameters and injects dependencies in the resulting object .
10,641
public function inject ( $ object ) : void { $ reflection = new \ ReflectionObject ( $ object ) ; $ this -> injectProperties ( $ reflection , $ object ) ; $ this -> injectMethods ( $ reflection , $ object ) ; }
Injects dependencies in an object .
10,642
private function getFunctionParameters ( \ ReflectionFunctionAbstract $ function , array $ parameters = [ ] ) : array { $ result = [ ] ; foreach ( $ function -> getParameters ( ) as $ parameter ) { $ name = $ parameter -> getName ( ) ; if ( array_key_exists ( $ name , $ parameters ) ) { $ value = $ parameters [ $ name ] ; } else { $ value = $ this -> resolver -> getParameterValue ( $ parameter ) ; } if ( $ parameter -> isVariadic ( ) ) { $ result = array_merge ( $ result , $ value ) ; } else { $ result [ ] = $ value ; } } return $ result ; }
Returns an associative array of parameters to call a given function .
10,643
public static function allElementsEqual ( array $ haystack , $ needle = null ) { $ return = true ; if ( func_num_args ( ) == 2 ) { $ compareAgainst = $ needle ; } else { $ compareAgainst = reset ( $ haystack ) ; } foreach ( $ haystack as $ element ) { if ( $ compareAgainst != $ element ) { $ return = false ; break ; } } return $ return ; }
Checks if all elements of the array have same value
10,644
public static function isA ( $ name , $ mask ) { if ( ! static :: isElement ( $ name ) ) { return false ; } return ( static :: element ( $ name ) & $ mask ) == $ mask ; }
Check whether the given element meets the given criterion .
10,645
public static function element ( $ name ) { if ( isset ( static :: $ html5 [ $ name ] ) ) { return static :: $ html5 [ $ name ] ; } if ( isset ( static :: $ svg [ $ name ] ) ) { return static :: $ svg [ $ name ] ; } if ( isset ( static :: $ mathml [ $ name ] ) ) { return static :: $ mathml [ $ name ] ; } return false ; }
Get the element mask for the given element name .
10,646
public static function normalizeSvgElement ( $ name ) { $ name = strtolower ( $ name ) ; if ( isset ( static :: $ svgCaseSensitiveElementMap [ $ name ] ) ) { $ name = static :: $ svgCaseSensitiveElementMap [ $ name ] ; } return $ name ; }
Normalize a SVG element name to its proper case and form .
10,647
public static function normalizeSvgAttribute ( $ name ) { $ name = strtolower ( $ name ) ; if ( isset ( static :: $ svgCaseSensitiveAttributeMap [ $ name ] ) ) { $ name = static :: $ svgCaseSensitiveAttributeMap [ $ name ] ; } return $ name ; }
Normalize a SVG attribute name to its proper case and form .
10,648
private function summarize ( array $ whereClause , $ selector ) { switch ( count ( $ whereClause ) ) { case 1 : return $ whereClause [ 0 ] ; default : if ( $ selector instanceof RecordSelector ) { return [ '@OR' => $ whereClause ] ; } if ( $ selector instanceof DocumentSelector ) { return [ '$or' => $ whereClause ] ; } } return [ ] ; }
Summarize expression array .
10,649
public static function log ( $ st , $ file = 'misc' ) { if ( \ defined ( 'BBN_DATA_PATH' ) ) { if ( ! \ is_string ( $ file ) ) { $ file = 'misc' ; } $ log_file = BBN_DATA_PATH . 'logs/' . $ file . '.log' ; $ backtrace = array_filter ( debug_backtrace ( ) , function ( $ a ) { return $ a [ 'function' ] === 'log' ; } ) ; $ i = end ( $ backtrace ) ; $ r = "[" . date ( 'd/m/Y H:i:s' ) . "]\t" . $ i [ 'file' ] . " - line " . $ i [ 'line' ] . self :: get_dump ( $ st ) . PHP_EOL ; if ( php_sapi_name ( ) === 'cli' ) { global $ argv ; if ( isset ( $ argv [ 2 ] ) && ( $ argv [ 2 ] === 'log' ) ) { echo self :: get_dump ( $ st ) . PHP_EOL ; } } $ s = ( file_exists ( $ log_file ) ) ? filesize ( $ log_file ) : 0 ; if ( $ s > 1048576 ) { file_put_contents ( $ log_file . '.old' , file_get_contents ( $ log_file ) , FILE_APPEND ) ; file_put_contents ( $ log_file , $ r ) ; } else { file_put_contents ( $ log_file , $ r , FILE_APPEND ) ; } } }
Saves logs to a file .
10,650
public static function log_error ( $ errno , $ errstr , $ errfile , $ errline ) { if ( \ defined ( 'BBN_DATA_PATH' ) ) { if ( is_dir ( BBN_DATA_PATH . 'logs' ) ) { $ file = BBN_DATA_PATH . 'logs/_php_error.json' ; $ backtrace = debug_backtrace ( DEBUG_BACKTRACE_IGNORE_ARGS , 20 ) ; $ r = false ; if ( is_file ( $ file ) ) { $ r = json_decode ( file_get_contents ( $ file ) , 1 ) ; } if ( ! $ r ) { $ r = [ ] ; } $ t = date ( 'Y-m-d H:i:s' ) ; if ( class_exists ( '\\bbn\\mvc' ) ) { $ mvc = mvc :: get_instance ( ) ; } $ idx = self :: find ( $ r , [ 'type' => $ errno , 'error' => $ errstr , 'file' => $ errfile , 'line' => $ errline , 'request' => '' ] ) ; if ( $ idx !== false ) { $ r [ $ idx ] [ 'count' ] ++ ; $ r [ $ idx ] [ 'last_date' ] = $ t ; $ r [ $ idx ] [ 'backtrace' ] = $ backtrace ; } else { $ r [ ] = [ 'first_date' => $ t , 'last_date' => $ t , 'count' => 1 , 'type' => $ errno , 'error' => $ errstr , 'file' => $ errfile , 'line' => $ errline , 'backtrace' => $ backtrace , 'request' => '' ] ; } self :: sort_by ( $ r , 'last_date' , 'DESC' ) ; file_put_contents ( $ file , json_encode ( $ r , JSON_PRETTY_PRINT ) ) ; } if ( $ errno > 8 ) { die ( $ errstr ) ; } } return false ; }
Puts the PHP errors into a JSON file .
10,651
public static function merge_objects ( $ o1 , $ o2 ) { $ args = \ func_get_args ( ) ; if ( \ count ( $ args ) > 2 ) { for ( $ i = \ count ( $ args ) - 1 ; $ i > 1 ; $ i -- ) { $ args [ $ i - 1 ] = self :: merge_arrays ( $ args [ $ i - 1 ] , $ args [ $ i ] ) ; } $ o2 = $ args [ 1 ] ; } $ a1 = self :: to_array ( $ o1 ) ; $ a2 = self :: to_array ( $ o2 ) ; $ res = self :: merge_arrays ( $ a1 , $ a2 ) ; return self :: to_object ( $ res ) ; }
Returns to a merged object from two objects .
10,652
public static function merge_arrays ( array $ a1 , array $ a2 ) { $ args = \ func_get_args ( ) ; if ( \ count ( $ args ) > 2 ) { for ( $ i = \ count ( $ args ) - 1 ; $ i > 1 ; $ i -- ) { $ args [ $ i - 1 ] = self :: merge_arrays ( $ args [ $ i - 1 ] , $ args [ $ i ] ) ; } $ a2 = $ args [ 1 ] ; } if ( ( self :: is_assoc ( $ a1 ) || empty ( $ a1 ) ) && ( self :: is_assoc ( $ a2 ) || empty ( $ a2 ) ) ) { $ keys = array_unique ( array_merge ( array_keys ( $ a1 ) , array_keys ( $ a2 ) ) ) ; $ r = [ ] ; foreach ( $ keys as $ k ) { if ( ! array_key_exists ( $ k , $ a1 ) && ! array_key_exists ( $ k , $ a2 ) ) { continue ; } else if ( ! array_key_exists ( $ k , $ a2 ) ) { $ r [ $ k ] = $ a1 [ $ k ] ; } else if ( ! array_key_exists ( $ k , $ a1 ) || ! \ is_array ( $ a2 [ $ k ] ) || ! \ is_array ( $ a1 [ $ k ] ) || is_numeric ( key ( $ a2 [ $ k ] ) ) ) { $ r [ $ k ] = $ a2 [ $ k ] ; } else { $ r [ $ k ] = self :: merge_arrays ( $ a1 [ $ k ] , $ a2 [ $ k ] ) ; } } } else { $ r = array_merge ( $ a1 , $ a2 ) ; } return $ r ; }
Returns to a merged array from two or more arrays .
10,653
public static function is_assoc ( array $ r ) { $ keys = array_keys ( $ r ) ; $ c = \ count ( $ keys ) ; for ( $ i = 0 ; $ i < $ c ; $ i ++ ) { if ( $ keys [ $ i ] !== $ i ) { return 1 ; } } return false ; }
Checks if the given array is associative .
10,654
public static function get_dump ( ) { $ args = \ func_get_args ( ) ; $ st = '' ; foreach ( $ args as $ a ) { $ r = $ a ; if ( \ is_null ( $ a ) ) { $ r = 'null' ; } else if ( $ a === false ) { $ r = 'false' ; } else if ( $ a === true ) { $ r = 'true' ; } else if ( $ a === 0 ) { $ r = '0' ; } else if ( $ a === '' ) { $ r = '""' ; } else if ( $ a === [ ] ) { $ r = '[]' ; } else if ( ! $ a ) { $ r = '0' ; } else if ( ! \ is_string ( $ a ) && \ is_callable ( $ a ) ) { $ r = 'Function' ; } else if ( \ is_object ( $ a ) ) { $ n = \ get_class ( $ a ) ; if ( $ n === 'stdClass' ) { $ r = str :: export ( $ a ) ; } else { $ r = $ n . ' Object' ; } } else if ( \ is_array ( $ a ) ) { $ r = str :: export ( $ a ) ; } else if ( \ is_resource ( $ a ) ) { $ r = 'Resource ' . get_resource_type ( $ a ) ; } else if ( str :: is_buid ( $ a ) ) { $ r = '0x' . bin2hex ( $ a ) ; } $ st .= $ r . PHP_EOL ; } return PHP_EOL . $ st . PHP_EOL ; }
Returns a dump of the given variable .
10,655
public static function to_keypair ( $ arr , $ protected = 1 ) { $ num = \ count ( $ arr ) ; $ res = [ ] ; if ( ( $ num % 2 ) === 0 ) { $ i = 0 ; while ( isset ( $ arr [ $ i ] ) ) { if ( ! \ is_string ( $ arr [ $ i ] ) || ( ! $ protected && ! preg_match ( '/[0-9A-z\-_]+/8' , str :: cast ( $ arr [ $ i ] ) ) ) ) { return false ; } $ res [ $ arr [ $ i ] ] = $ arr [ $ i + 1 ] ; $ i += 2 ; } } return $ res ; }
Converts a numeric array into an associative one alternating key and value .
10,656
public static function min_with_key ( $ array , $ key ) { if ( ! \ is_array ( $ array ) || \ count ( $ array ) == 0 ) return false ; $ min = $ array [ 0 ] [ $ key ] ; foreach ( $ array as $ a ) { if ( $ a [ $ key ] < $ min ) { $ min = $ a [ $ key ] ; } } return $ min ; }
Returns the minimum value of an index from a multidimensional array .
10,657
public static function debug ( $ file = '' ) { $ debug = array_map ( function ( $ a ) { if ( isset ( $ a [ 'object' ] ) ) { unset ( $ a [ 'object' ] ) ; } return $ a ; } , debug_backtrace ( ) ) ; if ( empty ( $ file ) ) { self :: hdump ( $ debug ) ; } else { self :: log ( $ debug , $ file ) ; } }
Gets the backtrace and dumps or logs it into a file .
10,658
public static function find ( array $ ar , array $ where ) { if ( ! empty ( $ where ) ) { foreach ( $ ar as $ i => $ v ) { $ ok = 1 ; foreach ( $ where as $ k => $ w ) { if ( ! isset ( $ v [ $ k ] ) || ( $ v [ $ k ] !== $ w ) ) { $ ok = false ; break ; } } if ( $ ok ) { return $ i ; } } } return false ; }
Returns the array s first index which satisfies the where condition .
10,659
public static function get_field ( array $ r , array $ where , string $ field ) { if ( ( $ res = self :: get_row ( $ r , $ where ) ) && isset ( $ res [ $ field ] ) ) { return $ res [ $ field ] ; } return false ; }
Returns the first value of a specific field of an array .
10,660
public static function sort ( & $ ar ) { usort ( $ ar , function ( $ a , $ b ) { if ( ! str :: is_number ( $ a , $ b ) ) { $ a = str_replace ( '.' , '0' , str_replace ( '_' , '1' , str :: change_case ( $ a , 'lower' ) ) ) ; $ b = str_replace ( '.' , '0' , str_replace ( '_' , '1' , str :: change_case ( $ b , 'lower' ) ) ) ; return strcmp ( $ a , $ b ) ; } if ( $ a > $ b ) { return 1 ; } else if ( $ a == $ b ) { return 0 ; } return - 1 ; } ) ; }
Sorts the items of an array .
10,661
public static function curl ( string $ url , array $ param = null , array $ options = [ 'post' => 1 ] ) { $ ch = curl_init ( ) ; self :: $ last_curl = $ ch ; curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , 1 ) ; if ( \ is_object ( $ param ) ) { $ param = self :: to_array ( $ param ) ; } if ( \ defined ( 'BBN_IS_SSL' ) && \ defined ( 'BBN_IS_DEV' ) && BBN_IS_SSL && BBN_IS_DEV ) { curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , false ) ; curl_setopt ( $ ch , CURLOPT_SSL_VERIFYHOST , false ) ; } $ options = array_change_key_case ( $ options , CASE_UPPER ) ; foreach ( $ options as $ opt => $ val ) { if ( \ defined ( 'CURLOPT_' . $ opt ) ) { curl_setopt ( $ ch , constant ( 'CURLOPT_' . $ opt ) , $ val ) ; } } if ( $ param ) { if ( ! empty ( $ options [ 'POST' ] ) ) { curl_setopt ( $ ch , CURLOPT_URL , $ url ) ; curl_setopt ( $ ch , CURLOPT_POSTFIELDS , $ param ) ; } else if ( ! empty ( $ options [ 'DELETE' ] ) ) { curl_setopt ( $ ch , CURLOPT_URL , $ url . '?' . http_build_query ( $ param ) ) ; curl_setopt ( $ ch , CURLOPT_CUSTOMREQUEST , 'DELETE' ) ; curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true ) ; } else { curl_setopt ( $ ch , CURLOPT_URL , $ url . '?' . http_build_query ( $ param ) ) ; } } else { curl_setopt ( $ ch , CURLOPT_URL , $ url ) ; if ( ! empty ( $ options [ 'DELETE' ] ) ) { curl_setopt ( $ ch , CURLOPT_CUSTOMREQUEST , 'DELETE' ) ; curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true ) ; } } $ r = curl_exec ( $ ch ) ; if ( ! $ r ) { self :: log ( [ "PROBLEME AVEC L'URL $url" , curl_error ( $ ch ) , curl_getinfo ( $ ch ) ] , 'curl' ) ; } return $ r ; }
Makes a Curl call towards a URL and returns the result as a string .
10,662
public static function get_tree ( $ ar ) { $ res = [ ] ; foreach ( $ ar as $ k => $ a ) { $ r = [ 'text' => $ k ] ; if ( \ is_object ( $ a ) ) { $ a = self :: to_array ( $ a ) ; } if ( \ is_array ( $ a ) ) { $ r [ 'items' ] = self :: get_tree ( $ a ) ; } else if ( \ is_null ( $ a ) ) { $ r [ 'text' ] .= ': null' ; } else if ( $ a === false ) { $ r [ 'text' ] .= ': false' ; } else if ( $ a === true ) { $ r [ 'text' ] .= ': true' ; } else { $ r [ 'text' ] .= ': ' . ( string ) $ a ; } array_push ( $ res , $ r ) ; } return $ res ; }
Returns the given array or object as a tree structure ready for a JS tree .
10,663
public static function is_same ( string $ file1 , string $ file2 , $ strict = false ) { if ( ! is_file ( $ file1 ) || ! is_file ( $ file2 ) ) { throw Exception ( "Boo! One of the files given to the x::is_same function doesn't exist" ) ; } else { $ same = filesize ( $ file1 ) === filesize ( $ file2 ) ; if ( ! $ strict || ! $ same ) { return $ same ; } return filemtime ( $ file1 ) === filemtime ( $ file2 ) ; } }
Checks if two files are the same .
10,664
public static function to_excel ( array $ data , string $ file , bool $ with_titles = true ) : bool { $ checked = false ; $ todo = [ ] ; foreach ( $ data as $ d ) { if ( ! $ checked && self :: is_assoc ( $ d ) ) { if ( $ with_titles ) { $ line1 = [ ] ; $ line2 = [ ] ; foreach ( $ d as $ k => $ v ) { $ line1 [ ] = $ k ; $ line2 [ ] = '' ; } $ todo [ ] = $ line1 ; $ todo [ ] = $ line2 ; } $ checked = true ; } $ todo [ ] = array_values ( $ d ) ; } if ( count ( $ todo ) ) { $ objPHPExcel = new \ PhpOffice \ PhpSpreadsheet \ Spreadsheet ( ) ; $ objPHPExcel -> getActiveSheet ( ) -> fromArray ( $ todo , NULL , 'A1' ) ; $ objPHPExcel -> getDefaultStyle ( ) -> getNumberFormat ( ) -> setFormatCode ( \ PhpOffice \ PhpSpreadsheet \ Style \ NumberFormat :: FORMAT_TEXT ) ; $ ow = new \ PhpOffice \ PhpSpreadsheet \ Writer \ Xlsx ( $ objPHPExcel ) ; $ ow -> save ( $ file ) ; return \ is_file ( $ file ) ; } return false ; }
Creates an Excel file from a given array .
10,665
public static function make_uid ( $ binary = false , $ hyphens = false ) { $ tmp = sprintf ( $ hyphens ? '%04x%04x-%04x-%04x-%04x-%04x%04x%04x' : '%04x%04x%04x%04x%04x%04x%04x%04x' , mt_rand ( 0 , 0xffff ) , mt_rand ( 0 , 0xffff ) , mt_rand ( 0 , 0xffff ) , mt_rand ( 0 , 0x0fff ) | 0x4000 , mt_rand ( 0 , 0x3fff ) | 0x8000 , mt_rand ( 0 , 0xffff ) , mt_rand ( 0 , 0xffff ) , mt_rand ( 0 , 0xffff ) ) ; return $ binary ? hex2bin ( $ tmp ) : $ tmp ; }
Makes a UID .
10,666
public static function convert_uids ( $ st ) { if ( \ is_array ( $ st ) || \ is_object ( $ st ) ) { foreach ( $ st as & $ s ) { $ s = self :: convert_uids ( $ s ) ; } } else if ( \ bbn \ str :: is_uid ( $ st ) ) { $ st = bin2hex ( $ st ) ; } return $ st ; }
Converts a hex UID to a binary UID . You can also give an array or an object to convert the array s items or the object s properties .
10,667
public static function compare_floats ( $ v1 , $ v2 , string $ operator = '===' , int $ precision = 4 ) : bool { $ v1 = round ( ( float ) $ v1 * pow ( 10 , $ precision ) ) ; $ v2 = round ( ( float ) $ v2 * pow ( 10 , $ precision ) ) ; switch ( $ operator ) { case '===' : return $ v1 === $ v2 ; case '==' : return $ v1 == $ v2 ; case '>=' : return $ v1 >= $ v2 ; case '<=' : return $ v1 <= $ v2 ; case '>' : return $ v1 > $ v2 ; case '<' : return $ v1 < $ v2 ; } return false ; }
Compares two float numbers with the given operator .
10,668
public static function json_base64_decode ( $ st ) : ? array { $ res = \ is_string ( $ st ) ? json_decode ( $ st , true ) : $ st ; if ( \ is_array ( $ res ) ) { foreach ( $ res as $ i => $ a ) { if ( \ is_array ( $ a ) ) { $ res [ $ i ] = self :: json_base64_decode ( $ a ) ; } else if ( \ is_string ( $ a ) ) { $ res [ $ i ] = base64_decode ( $ a ) ; } else { $ res [ $ i ] = $ a ; } } return $ res ; } return null ; }
Decodes the base64 array s values . You can also give a JSON string of an array .
10,669
public static function index_by_first_val ( array $ ar ) : array { if ( empty ( $ ar ) || ! isset ( $ ar [ 0 ] ) || ! \ count ( $ ar [ 0 ] ) ) { return $ ar ; } $ cols = array_keys ( $ ar [ 0 ] ) ; $ idx = array_shift ( $ cols ) ; $ num_cols = \ count ( $ cols ) ; $ res = [ ] ; foreach ( $ ar as $ d ) { $ index = $ d [ $ idx ] ; unset ( $ d [ $ idx ] ) ; $ res [ $ index ] = $ num_cols > 1 ? $ d : $ d [ $ cols [ 0 ] ] ; } return $ res ; }
Creates an associative array based on the first array s value .
10,670
public function getQueryString ( ) { $ requestUri = $ this -> getRequestUri ( ) ; if ( false !== $ qPos = mb_strpos ( $ requestUri , '?' ) ) { return mb_substr ( $ requestUri , $ qPos + 1 ) ; } return '' ; }
The query string .
10,671
public function getQueryStringAsArray ( ) { $ queryString = $ this -> getQueryString ( ) ; if ( '' === $ queryString ) { return [ ] ; } $ queryStringArray = [ ] ; parse_str ( $ queryString , $ queryStringArray ) ; return $ queryStringArray ; }
The query string as array .
10,672
public function getQueryParameter ( $ key ) { $ queryStringArray = $ this -> getQueryStringAsArray ( ) ; if ( array_key_exists ( $ key , $ queryStringArray ) ) { return $ queryStringArray [ $ key ] ; } return null ; }
Return a specific query string parameter value .
10,673
public function getRoot ( ) { $ requestUri = $ this -> getRequestUri ( ) ; $ pathInfo = $ this -> getPathInfo ( ) ; if ( false !== $ qPos = mb_strpos ( $ requestUri , '?' ) ) { $ requestUri = mb_substr ( $ requestUri , 0 , $ qPos + 1 ) ; } if ( false !== $ pPos = mb_strrpos ( $ requestUri , $ pathInfo ) ) { $ requestUri = mb_substr ( $ requestUri , 0 , $ pPos + 1 ) ; } return $ requestUri ; }
Get the REQUEST_URI without PATH_INFO and QUERY_STRING taking server rewriting in consideration .
10,674
public function getAuthority ( ) { $ scheme = $ this -> getScheme ( ) ; $ serverName = $ this -> getHost ( ) ; $ serverPort = $ this -> getPort ( ) ; $ authority = sprintf ( '%s://%s' , $ scheme , $ serverName ) ; if ( ( 'https' === $ scheme && 443 !== $ serverPort ) || ( 'http' === $ scheme && 80 !== $ serverPort ) ) { $ authority .= sprintf ( ':%d' , $ serverPort ) ; } return $ authority ; }
Get the authority part of the URL . That is the scheme host and optional port if it is not a standard port .
10,675
public function isGranted ( string $ action ) : bool { if ( empty ( $ this -> allowed ) ) { return false ; } if ( isset ( $ this -> denied [ $ action ] ) || isset ( $ this -> denied [ self :: WILDCARD ] ) ) { return false ; } $ rules = $ this -> getRules ( $ action ) ; foreach ( $ rules as $ rule ) { if ( isset ( $ this -> denied [ $ rule ] ) ) { return false ; } } foreach ( $ rules as $ rule ) { if ( isset ( $ this -> allowed [ $ rule ] ) ) { return true ; } } return false ; }
Returns true if the policy should allow the action to be carried out .
10,676
private function getRules ( string $ action ) : array { $ rules = [ ] ; $ parts = explode ( self :: DELIMITER , $ action ) ; while ( array_pop ( $ parts ) ) { $ rules [ ] = implode ( self :: DELIMITER , $ parts ) . self :: DELIMITER . self :: WILDCARD ; } $ rules = array_reverse ( $ rules ) ; $ rules [ 0 ] = trim ( $ rules [ 0 ] , self :: DELIMITER ) ; $ rules [ ] = $ action ; return $ rules ; }
Converts an action with potentially colon delimiters into a set of permissions to check for .
10,677
public function getLoginLogs ( ) { $ class = $ this -> loginLogClass ; try { return $ class :: getLatests ( $ this , Login :: GET_ALL_LATESTS ) ; } catch ( \ Exception $ ex ) { Yii :: error ( $ ex -> getMessage ( ) , __METHOD__ ) ; return [ ] ; } }
Get login logs .
10,678
public function recordLogin ( $ config = [ ] ) { if ( empty ( $ this -> loginLogClass ) ) { return false ; } $ log = $ this -> create ( $ this -> loginLogClass , $ config ) ; try { return $ log -> save ( ) ; } catch ( \ Exception $ ex ) { Yii :: error ( $ ex -> getMessage ( ) , __METHOD__ ) ; } }
Record login .
10,679
public function setData ( $ collection , $ resource_name = '' ) { $ this -> collection = collect ( ) ; if ( ! $ this -> resource_name ) { $ this -> resource_name = empty ( $ resource_name ) ? 'undefined' : $ resource_name ; } $ this -> data = [ ] ; foreach ( $ collection as $ item ) { $ this -> collection -> push ( clone $ item ) ; $ i = new Item ( $ item , $ this -> resource_name ) ; array_push ( $ this -> data , $ i -> getData ( ) ) ; } $ this -> response [ 'data' ] = $ this -> data ; }
Set collection data
10,680
public static function configure ( $ config = [ ] ) { $ defaults = include dirname ( __DIR__ ) . '/conf/defaults.php' ; self :: $ config = array_replace_recursive ( $ defaults , $ config ) ; if ( isset ( self :: $ driver ) ) { if ( self :: $ driver -> connected ) { self :: $ driver -> disconnect ( ) ; } self :: $ driver = null ; } $ class = '\\Nymph\\Drivers\\' . self :: $ config [ 'driver' ] . 'Driver' ; $ Nymph = new $ class ( self :: $ config ) ; if ( class_exists ( '\\SciActive\\Hook' ) ) { \ SciActive \ Hook :: hookObject ( $ Nymph , 'Nymph->' ) ; } if ( self :: $ config [ 'pubsub' ] ) { \ Nymph \ PubSub \ HookMethods :: setup ( ) ; } self :: $ driver = $ Nymph ; }
Apply configuration to Nymph .
10,681
public static function checkData ( & $ data , & $ sdata , $ selectors , $ guid = null , $ tags = null , $ typesAlreadyChecked = [ ] , $ dataValsAreadyChecked = [ ] ) { return self :: $ driver -> checkData ( $ data , $ sdata , $ selectors , $ guid , $ tags , $ typesAlreadyChecked , $ dataValsAreadyChecked ) ; }
Check entity data to see if it matches given selectors .
10,682
public static function hsort ( & $ array , $ property = null , $ parentProperty = null , $ caseSensitive = false , $ reverse = false ) { return self :: $ driver -> hsort ( $ array , $ property , $ parentProperty , $ caseSensitive , $ reverse ) ; }
Sort an array of entities hierarchically by a specified property s value .
10,683
public static function sort ( & $ array , $ property = null , $ caseSensitive = false , $ reverse = false ) { return self :: $ driver -> sort ( $ array , $ property , $ caseSensitive , $ reverse ) ; }
Sort an array of entities by a specified property s value .
10,684
public function convertSymfonyRequest ( SymfonyRequest $ request ) { $ session = $ request -> getSession ( ) ; if ( $ session ) { $ session = $ session -> all ( ) ; } else { $ session = [ ] ; } $ parameters = $ request -> request -> all ( ) ; if ( in_array ( $ request -> getMethod ( ) , [ 'POST' , 'PUT' , 'PATCH' ] ) && 'json' == $ request -> getContentType ( ) ) { $ parameters = json_decode ( $ request -> getContent ( ) , true ) ; } $ req = new Request ( $ request -> query -> all ( ) , $ parameters , $ request -> cookies -> all ( ) , $ _FILES , $ request -> server -> all ( ) , $ session ) ; $ req -> setParams ( $ request -> attributes -> all ( ) ) ; return $ req ; }
Converts a Symfony request object into an Infuse request object .
10,685
public function convertInfuseResponse ( Response $ res ) { $ response = new SymfonyResponse ( $ res -> getBody ( ) , $ res -> getCode ( ) , $ res -> headers ( ) ) ; foreach ( $ res -> cookies ( ) as $ name => $ params ) { list ( $ value , $ expire , $ path , $ domain , $ secure , $ httponly ) = $ params ; $ cookie = new Cookie ( $ name , $ value , $ expire , $ path , $ domain , $ secure , $ httponly ) ; $ response -> headers -> setCookie ( $ cookie ) ; } return $ response ; }
Converts an Infuse response object into a Symfony response object .
10,686
private function _init_user ( bbn \ user $ user ) : preferences { $ this -> user = $ user ; $ this -> id_user = $ this -> user -> get_id ( ) ; $ this -> id_group = $ this -> user -> get_group ( ) ; return $ this ; }
Sets the user variables using a user object
10,687
private function _insert ( string $ id_option , array $ cfg ) : int { $ json = ( $ tmp = $ this -> get_cfg ( false , $ cfg ) ) ? json_encode ( $ tmp ) : NULL ; return $ this -> db -> insert ( $ this -> class_cfg [ 'table' ] , [ $ this -> fields [ 'id_option' ] => $ id_option , $ this -> fields [ 'num' ] => $ cfg [ $ this -> fields [ 'num' ] ] ?? NULL , $ this -> fields [ 'text' ] => $ cfg [ $ this -> fields [ 'text' ] ] ?? NULL , $ this -> fields [ 'id_link' ] => $ cfg [ $ this -> fields [ 'id_link' ] ] ?? NULL , $ this -> fields [ 'id_alias' ] => $ cfg [ $ this -> fields [ 'id_alias' ] ] ?? NULL , $ this -> fields [ 'id_user' ] => $ this -> id_user , $ this -> fields [ 'cfg' ] => $ json ] ) ; }
Actually inserts a row into the preferences table
10,688
public function retrieve_user_ids ( string $ id_option = null , string $ id_user ) : ? array { return $ this -> _retrieve_ids ( $ id_option , $ id_user ) ; }
Returns preferences IDs from the option s ID and the given user ID
10,689
public function retrieve_group_ids ( string $ id_option = null , string $ id_group ) : ? array { return $ this -> _retrieve_ids ( $ id_option , null , $ id_group ) ; }
Returns preferences IDs from the option s ID and the given group ID
10,690
public function has ( string $ id_option = null , bool $ force = false ) : bool { if ( ! $ force && $ this -> user -> is_dev ( ) ) { return true ; } return ( bool ) $ this -> retrieve_ids ( $ id_option ) ; }
Returns true if the current user can access a preference false otherwise
10,691
public function user_has ( string $ id_option , string $ id_user ) : bool { return ( bool ) $ this -> _retrieve_ids ( $ id_option , $ id_user ) ; }
Checks if a user has the given preference
10,692
public function group_has ( string $ id_option , string $ id_group ) : bool { return ( bool ) $ this -> _retrieve_ids ( $ id_option , null , $ id_group ) ; }
Checks if a group has the given preference
10,693
public function get ( string $ id , bool $ with_config = true ) : ? array { if ( bbn \ str :: is_uid ( $ id ) ) { $ cols = implode ( ', ' , array_map ( function ( $ a ) { return $ a ; } , $ this -> fields ) ) ; $ table = $ this -> db -> tsn ( $ this -> class_cfg [ 'table' ] , true ) ; $ uid = $ this -> db -> csn ( $ this -> fields [ 'id' ] , true ) ; $ id_user = $ this -> db -> csn ( $ this -> fields [ 'id_user' ] , true ) ; $ id_group = $ this -> db -> csn ( $ this -> fields [ 'id_group' ] , true ) ; $ public = $ this -> db -> csn ( $ this -> fields [ 'public' ] , true ) ; $ sql = <<< MYSQLSELECT $colsFROM $tableWHERE $uid = UNHEX(?)AND ($id_user = UNHEX(?)OR $id_group = UNHEX(?)OR $public = 1)MYSQL ; if ( $ row = $ this -> db -> get_row ( $ sql , $ id , $ this -> id_user , $ this -> id_group ) ) { $ cfg = $ row [ 'cfg' ] ; unset ( $ row [ 'cfg' ] ) ; if ( ( $ cfg = json_decode ( $ cfg , true ) ) && $ with_config ) { $ row = bbn \ x :: merge_arrays ( $ cfg , $ row ) ; } return $ row ; } } return null ; }
Returns the current user s preference based on the given id his own profile and his group s
10,694
public function get_all ( string $ id_option = null , bool $ with_config = true ) : ? array { if ( $ id_option = $ this -> _get_id_option ( $ id_option ) ) { $ fields = $ this -> fields ; if ( ! $ with_config ) { unset ( $ fields [ 'cfg' ] ) ; } $ table = $ this -> db -> tsn ( $ this -> class_table , true ) ; $ cols = implode ( ', ' , array_map ( function ( $ a ) use ( $ table ) { return $ a === $ this -> fields [ 'id' ] ? "$table.$a" : "IFNULL(aliases.$a, $table.$a) AS $a" ; } , $ fields ) ) ; $ id_opt = $ this -> db -> cfn ( $ this -> fields [ 'id_option' ] , $ this -> class_table , true ) ; $ id_user = $ this -> db -> cfn ( $ this -> fields [ 'id_user' ] , $ this -> class_table , true ) ; $ id_group = $ this -> db -> cfn ( $ this -> fields [ 'id_group' ] , $ this -> class_table , true ) ; $ num = $ this -> db -> cfn ( $ this -> fields [ 'num' ] , $ this -> class_table , true ) ; $ text = $ this -> db -> cfn ( $ this -> fields [ 'text' ] , $ this -> class_table , true ) ; $ id_alias = $ this -> db -> cfn ( $ this -> fields [ 'id_alias' ] , $ this -> class_table , true ) ; $ public = $ this -> db -> cfn ( $ this -> fields [ 'public' ] , $ this -> class_table , true ) ; $ sql = <<< MYSQLSELECT $colsFROM $table LEFT JOIN $table as aliases ON aliases.id = $id_aliasWHERE $id_opt = UNHEX(?)AND ($id_user = UNHEX(?)OR $id_group = UNHEX(?)OR $public = 1)ORDER BY IFNULL($num, $text)MYSQL ; if ( $ rows = $ this -> db -> get_rows ( $ sql , $ id_option , $ this -> id_user , $ this -> id_group ) ) { return $ with_config ? array_map ( function ( $ a ) { $ cfg = $ a [ 'cfg' ] ; unset ( $ a [ 'cfg' ] ) ; if ( ( $ cfg = json_decode ( $ cfg , true ) ) ) { $ a = bbn \ x :: merge_arrays ( $ cfg , $ a ) ; } return $ a ; } , $ rows ) : $ rows ; } return [ ] ; } return null ; }
Returns an array of the current user s preferences based on the given id_option his own profile and his group s
10,695
public function set_by_option ( string $ id_option , array $ cfg ) : int { if ( $ id = $ this -> retrieve_user_ids ( $ id_option , $ this -> id_user ) ) { return $ this -> set ( $ id [ 0 ] , $ cfg ) ; } return $ this -> _insert ( $ id_option , $ cfg ) ; }
Sets the permission row for the current user by the option s ID
10,696
public function set ( string $ id , array $ cfg = null ) : int { return $ this -> db -> update ( $ this -> class_cfg [ 'table' ] , [ $ this -> fields [ 'cfg' ] => $ cfg ? json_encode ( $ this -> get_cfg ( false , $ cfg ) ) : null ] , [ $ this -> fields [ 'id' ] => $ id ] ) ; }
Sets the permission config for the current user by the preference s ID
10,697
public function update ( string $ id , array $ cfg ) : int { return $ this -> db -> update ( $ this -> class_cfg [ 'table' ] , [ $ this -> fields [ 'text' ] => $ cfg [ $ this -> fields [ 'text' ] ] ?? NULL , $ this -> fields [ 'num' ] => $ cfg [ $ this -> fields [ 'num' ] ] ?? NULL , $ this -> fields [ 'id_link' ] => $ cfg [ $ this -> fields [ 'id_link' ] ] ?? NULL , $ this -> fields [ 'id_alias' ] => $ cfg [ $ this -> fields [ 'id_alias' ] ] ?? NULL , $ this -> fields [ 'id_user' ] => $ this -> id_user , $ this -> fields [ 'cfg' ] => ( $ mp = $ this -> get_cfg ( false , $ cfg ) ) ? json_encode ( $ tmp ) : NULL ] , [ $ this -> fields [ 'id' ] => $ id ] ) ; }
Sets the permission row for the current user by the preference s ID
10,698
public function add ( string $ id_option = null , array $ cfg ) : ? string { return ( ( $ id_option = $ this -> _get_id_option ( $ id_option ) ) && $ this -> _insert ( $ id_option , $ cfg ) ) ? $ this -> db -> last_id ( ) : null ; }
Adds a new preference for the given option for the current user .
10,699
public function delete ( $ id ) : ? int { return $ this -> db -> delete ( $ this -> class_cfg [ 'table' ] , [ $ this -> fields [ 'id' ] => $ id ] ) ; }
Deletes the given permission