idx
int64
0
60.3k
question
stringlengths
64
4.24k
target
stringlengths
5
618
48,600
private function isLessThan1Year ( $ timeDifference ) { return $ timeDifference >= ( ( $ this -> secondsPerDay * 59 ) + ( $ this -> secondsPerHour * 23 ) + ( $ this -> secondsPerMinute * 59 ) + 30 ) && $ timeDifference < $ this -> secondsPerYear ; }
Checks if the time difference is less than 1 year
48,601
public function setDigestAuthentication ( $ username , $ password = '' ) { $ this -> setOpt ( CURLOPT_HTTPAUTH , CURLAUTH_DIGEST ) ; $ this -> setOpt ( CURLOPT_USERPWD , $ username . ':' . $ password ) ; }
Set Digest Authentication
48,602
public function getResponseCookie ( $ key ) { return isset ( $ this -> responseCookies [ $ key ] ) ? $ this -> responseCookies [ $ key ] : null ; }
Get Response Cookie
48,603
public function setMaxFilesize ( $ bytes ) { $ gte_v550 = version_compare ( PHP_VERSION , '5.5.0' ) >= 0 ; if ( $ gte_v550 ) { $ callback = function ( $ resource , $ download_size , $ downloaded , $ upload_size , $ uploaded ) use ( $ bytes ) { return $ downloaded > $ bytes ? 1 : 0 ; } ; } else { $ callback = function ( $ download_size , $ downloaded , $ upload_size , $ uploaded ) use ( $ bytes ) { return $ downloaded > $ bytes ? 1 : 0 ; } ; } $ this -> progress ( $ callback ) ; }
Set Max Filesize
48,604
public function setDefaultJsonDecoder ( ) { $ args = func_get_args ( ) ; $ this -> jsonDecoder = function ( $ response ) use ( $ args ) { array_unshift ( $ args , $ response ) ; if ( version_compare ( PHP_VERSION , '5.4.0' , '<' ) ) { $ args = array_slice ( $ args , 0 , 3 ) ; } $ json_obj = call_user_func_array ( 'json_decode' , $ args ) ; if ( ! ( $ json_obj === null ) ) { $ response = $ json_obj ; } return $ response ; } ; }
Set Default JSON Decoder
48,605
public function setDefaultXmlDecoder ( ) { $ this -> xmlDecoder = function ( $ response ) { $ xml_obj = @ simplexml_load_string ( $ response ) ; if ( ! ( $ xml_obj === false ) ) { $ response = $ xml_obj ; } return $ response ; } ; }
Set Default XML Decoder
48,606
public function setDefaultDecoder ( $ decoder = 'json' ) { if ( is_callable ( $ decoder ) ) { $ this -> defaultDecoder = $ decoder ; } else { if ( $ decoder === 'json' ) { $ this -> defaultDecoder = $ this -> jsonDecoder ; } elseif ( $ decoder === 'xml' ) { $ this -> defaultDecoder = $ this -> xmlDecoder ; } } }
Set Default Decoder
48,607
public function evaluate ( array $ values ) { foreach ( $ values as $ char ) { if ( ord ( $ char ) < 32 ) { return false ; } if ( ord ( $ char ) === 127 ) { return false ; } } return true ; }
Assumes utf - 8 .
48,608
protected function showBindings ( Collection $ bindings ) { $ table = [ ] ; $ bindings -> map ( function ( $ item , $ key ) { return [ 'shortName' => shorten ( $ key ) , 'abstract' => $ key , 'concrete' => $ item ] ; } ) -> sort ( function ( $ a , $ b ) { return $ a [ 'shortName' ] > $ b [ 'shortName' ] ; } ) -> each ( function ( $ binding ) use ( & $ table ) { $ table [ ] = [ $ binding [ 'shortName' ] , $ binding [ 'abstract' ] , $ binding [ 'concrete' ] , ] ; } ) ; $ this -> table ( $ this -> headers , $ table ) ; }
Displays the list of enum bindings on the output
48,609
protected function registerMigrations ( ) { $ path = $ this -> getBasePath ( ) . '/' . $ this -> convention -> migrationsFolder ( ) ; if ( $ this -> app -> runningInConsole ( ) && is_dir ( $ path ) ) { $ this -> loadMigrationsFrom ( $ path ) ; } }
Register the module s migrations
48,610
protected function registerViews ( ) { $ path = $ this -> getBasePath ( ) . '/' . $ this -> convention -> viewsFolder ( ) ; $ namespace = $ this -> config ( 'views.namespace' , $ this -> shortName ( ) ) ; if ( is_dir ( $ path ) ) { $ this -> loadViewsFrom ( $ path , $ namespace ) ; } }
Register the views folder in a separate namespace
48,611
protected function registerCommands ( ) { if ( $ this -> app -> runningInConsole ( ) ) { $ this -> commands ( [ ModulesCommand :: class , ModelsCommand :: class , EnumsCommand :: class , RequestsCommand :: class , MakeModuleCommand :: class , VersionCommand :: class ] ) ; } }
Register the console commands of this package
48,612
private function resolveConventionClass ( string $ name ) { if ( 'default' == $ name ) { return ConcordDefault :: class ; } elseif ( class_exists ( $ name ) ) { return $ name ; } throw new InvalidArgumentException ( sprintf ( '%s is not a valid convention class' , $ name ) ) ; }
Resolve the convention class
48,613
protected function showModules ( Collection $ modules ) { $ table = [ ] ; $ i = 0 ; foreach ( $ modules as $ module ) { $ i ++ ; $ table [ ] = [ 'no' => sprintf ( '%d.' , $ i ) , 'name' => $ module -> getManifest ( ) -> getName ( ) , 'kind' => $ module -> getKind ( ) -> label ( ) , 'version' => $ module -> getManifest ( ) -> getVersion ( ) , 'id' => $ module -> getId ( ) , 'namespace' => $ module -> getNamespaceRoot ( ) ] ; } $ this -> table ( $ this -> headers , $ table ) ; }
Displays the list of modules on the output
48,614
protected function defaultEnumClassForContract ( $ enumContract ) { return $ this -> oneLevelUp ( $ this -> getNamespace ( $ enumContract ) ) . '\\' . $ this -> enumsFolder ( ) . '\\' . class_basename ( $ enumContract ) ; }
Returns the convention s default enum class for an enum contract
48,615
protected function defaultModelClassForContract ( $ modelContract ) { return $ this -> oneLevelUp ( $ this -> getNamespace ( $ modelContract ) ) . '\\' . $ this -> modelsFolder ( ) . '\\' . class_basename ( $ modelContract ) ; }
Returns the convention s default model class for a model contract
48,616
protected function mergeModuleConfig ( string $ key , array $ config ) { if ( ! empty ( $ config ) ) { $ current = $ this -> app [ 'config' ] -> get ( $ key ) ; if ( is_array ( $ current ) ) { $ this -> app [ 'config' ] -> set ( $ key , array_merge ( $ current , $ config ) ) ; } else { $ this -> app [ 'config' ] -> set ( $ key , $ config ) ; } } }
Merge the module s config with the existing config
48,617
public function push ( $ queueName , Message $ message ) { $ cache = $ this -> get ( $ queueName ) ; $ cache -> enqueue ( $ message ) ; }
Pushes a message to the end of the cache .
48,618
public function export ( RouteCollection $ routes ) { if ( ! is_dir ( $ this -> outputDir ) ) { mkdir ( $ this -> outputDir , 0777 , true ) ; } if ( ! is_writable ( $ this -> outputDir ) ) { throw new \ InvalidArgumentException ( sprintf ( 'The output directory "%s" is not writable.' , $ this -> outputDir ) ) ; } $ this -> exportRoutes ( $ routes ) ; }
Saves the given routes in a routing file . Creates the output directory if it is not exists .
48,619
public function sanitizeForSerialization ( $ data ) { if ( is_scalar ( $ data ) || null === $ data ) { $ sanitized = $ data ; } else if ( $ data instanceof \ DateTime ) { $ sanitized = $ data -> format ( \ DateTime :: ISO8601 ) ; } else if ( is_array ( $ data ) ) { foreach ( $ data as $ property => $ value ) { $ data [ $ property ] = $ this -> sanitizeForSerialization ( $ value ) ; } $ sanitized = $ data ; } else if ( is_object ( $ data ) ) { $ values = array ( ) ; foreach ( array_keys ( $ data :: $ swaggerTypes ) as $ property ) { $ getter = $ data :: $ getters [ $ property ] ; if ( $ data -> $ getter ( ) !== null ) { $ values [ $ data :: $ attributeMap [ $ property ] ] = $ this -> sanitizeForSerialization ( $ data -> $ getter ( ) ) ; } } ksort ( $ values ) ; $ sanitized = $ values ; } else { $ sanitized = ( string ) $ data ; } return $ sanitized ; }
Build a JSON POST object
48,620
public function toString ( $ value ) { if ( $ value instanceof \ DateTime ) { return $ value -> format ( \ DateTime :: ISO8601 ) ; } else if ( is_bool ( $ value ) ) { return $ value ? 1 : 0 ; } else { return $ value ; } }
Take value and turn it into a string suitable for inclusion in the parameter . If it s a string pass through unchanged If it s a datetime object format it in ISO8601
48,621
public function getKey ( $ field , $ assoc = false ) { return $ assoc ? $ this -> get ( $ assoc ) [ $ field ] : $ this -> get ( $ assoc ) -> { $ field } ; }
Get key from returned config
48,622
public function relations ( $ tableName = null , $ assoc = false ) { if ( is_array ( $ tableName ) ) { $ relations = $ this -> getKey ( 'relations' , $ assoc ) ; if ( $ assoc ) { return array_intersect_key ( $ relations , array_flip ( $ tableName ) ) ; } else { return ( object ) array_intersect_key ( ( array ) $ relations , array_flip ( $ tableName ) ) ; } } elseif ( $ tableName !== null ) { return $ assoc ? $ this -> getKey ( 'relations' , $ assoc ) [ $ tableName ] : $ this -> getKey ( 'relations' , $ assoc ) -> { $ tableName } ; } return $ this -> getKey ( 'relations' , $ assoc ) ; }
Return relations configure
48,623
public function getJunction ( ) { if ( $ this -> relationType == 'hasAndBelongsToMany' ) { $ tables = $ this -> tables ; sort ( $ tables ) ; return implode ( '_' , $ tables ) ; } return false ; }
Getter of junction table name in many2many relation
48,624
protected function setKey ( $ type , $ key ) { if ( ! in_array ( null , $ this -> tables ) ) { Validate :: table ( $ this -> tables [ $ type ] ) -> field ( $ key ) ; $ this -> keys [ $ type ] = $ key ; return $ this ; } throw new LazerException ( 'First you must define tables name' ) ; }
Set key name
48,625
public function with ( $ table ) { Validate :: relation ( $ this -> tables [ 'local' ] , $ table ) ; $ this -> setTable ( 'foreign' , $ table ) ; $ this -> setRelationType ( Config :: table ( $ this -> tables [ 'local' ] ) -> relations ( $ this -> tables [ 'foreign' ] ) -> type ) ; $ this -> setKey ( 'local' , Config :: table ( $ this -> tables [ 'local' ] ) -> relations ( $ this -> tables [ 'foreign' ] ) -> keys -> local ) ; $ this -> setKey ( 'foreign' , Config :: table ( $ this -> tables [ 'local' ] ) -> relations ( $ this -> tables [ 'foreign' ] ) -> keys -> foreign ) ; return $ this ; }
Use relation to table
48,626
public function setRelation ( ) { if ( ! in_array ( null , $ this -> tables ) && ! in_array ( null , $ this -> keys ) ) { $ this -> addRelation ( ) ; return true ; } else { throw new LazerException ( 'Tables names or keys missing' ) ; } }
Set specified relation
48,627
public static function filter ( array $ fields ) { if ( array_values ( $ fields ) === $ fields ) { if ( ( $ key = array_search ( 'id' , $ fields ) ) !== false ) { unset ( $ fields [ $ key ] ) ; } } else { unset ( $ fields [ 'id' ] ) ; } return $ fields ; }
Delete ID field from arrays
48,628
protected function getRowKey ( $ id ) { foreach ( $ this -> getData ( ) as $ key => $ data ) { if ( $ data -> id == $ id ) { return $ key ; break ; } } throw new LazerException ( 'No data found with ID: ' . $ id ) ; }
Returns array key of row with specified ID
48,629
protected function setFields ( ) { $ this -> set = new \ stdClass ( ) ; $ schema = $ this -> schema ( ) ; foreach ( $ schema as $ field => $ type ) { if ( Helpers \ Validate :: isNumeric ( $ type ) && $ field != 'id' ) { $ this -> setField ( $ field , 0 ) ; } else { $ this -> setField ( $ field , null ) ; } } }
Setting fields with default values
48,630
public function set ( array $ data ) { foreach ( $ data as $ name => $ value ) { $ this -> setField ( $ name , $ value ) ; } }
Validating array and setting variables to current operations
48,631
public function setField ( $ name , $ value ) { if ( Helpers \ Validate :: table ( $ this -> name ) -> field ( $ name ) && Helpers \ Validate :: table ( $ this -> name ) -> type ( $ name , $ value ) ) { $ this -> set -> { $ name } = is_string ( $ value ) && false === mb_check_encoding ( $ value , 'UTF-8' ) ? utf8_encode ( $ value ) : $ value ; } return $ this ; }
Validating fields and setting variables to current operations
48,632
protected function pending ( ) { $ this -> setData ( ) ; foreach ( $ this -> pending as $ func => $ args ) { if ( ! empty ( $ args ) ) { call_user_func ( array ( $ this , $ func . 'Pending' ) ) ; } } $ this -> clearQuery ( ) ; }
Execute pending functions
48,633
public static function create ( $ name , array $ fields ) { $ fields = Helpers \ Validate :: arrToLower ( $ fields ) ; if ( Helpers \ Data :: table ( $ name ) -> exists ( ) && Helpers \ Config :: table ( $ name ) -> exists ( ) ) { throw new LazerException ( 'helper\Table "' . $ name . '" already exists' ) ; } $ types = array_values ( $ fields ) ; Helpers \ Validate :: types ( $ types ) ; if ( ! array_key_exists ( 'id' , $ fields ) ) { $ fields = array ( 'id' => 'integer' ) + $ fields ; } $ data = new \ stdClass ( ) ; $ data -> last_id = 0 ; $ data -> schema = $ fields ; $ data -> relations = new \ stdClass ( ) ; Helpers \ Data :: table ( $ name ) -> put ( array ( ) ) ; Helpers \ Config :: table ( $ name ) -> put ( $ data ) ; }
Creating new table
48,634
public static function remove ( $ name ) { if ( Helpers \ Data :: table ( $ name ) -> remove ( ) && Helpers \ Config :: table ( $ name ) -> remove ( ) ) { return TRUE ; } return FALSE ; }
Removing table with config
48,635
public function groupBy ( $ column ) { if ( Helpers \ Validate :: table ( $ this -> name ) -> field ( $ column ) ) { $ this -> resetKeys = 0 ; $ this -> pending [ __FUNCTION__ ] = $ column ; } return $ this ; }
Grouping results by one field
48,636
protected function groupByPending ( ) { $ column = $ this -> pending [ 'groupBy' ] ; $ grouped = array ( ) ; foreach ( $ this -> data as $ object ) { $ grouped [ $ object -> { $ column } ] [ ] = $ object ; } $ this -> data = $ grouped ; }
Grouping array pending method
48,637
public function orderBy ( $ key , $ direction = 'ASC' ) { if ( Helpers \ Validate :: table ( $ this -> name ) -> field ( $ key ) ) { $ directions = array ( 'ASC' => SORT_ASC , 'DESC' => SORT_DESC ) ; $ this -> pending [ __FUNCTION__ ] [ $ key ] = isset ( $ directions [ $ direction ] ) ? $ directions [ $ direction ] : 'ASC' ; } return $ this ; }
Sorting data by field
48,638
protected function orderByPending ( ) { $ properties = $ this -> pending [ 'orderBy' ] ; uasort ( $ this -> data , function ( $ a , $ b ) use ( $ properties ) { foreach ( $ properties as $ column => $ direction ) { if ( is_int ( $ column ) ) { $ column = $ direction ; $ direction = SORT_ASC ; } $ collapse = function ( $ node , $ props ) { if ( is_array ( $ props ) ) { foreach ( $ props as $ prop ) { $ node = ( ! isset ( $ node -> $ prop ) ) ? null : $ node -> $ prop ; } return $ node ; } else { return ( ! isset ( $ node -> $ props ) ) ? null : $ node -> $ props ; } } ; $ aProp = $ collapse ( $ a , $ column ) ; $ bProp = $ collapse ( $ b , $ column ) ; if ( $ aProp != $ bProp ) { return ( $ direction == SORT_ASC ) ? strnatcasecmp ( $ aProp , $ bProp ) : strnatcasecmp ( $ bProp , $ aProp ) ; } } return FALSE ; } ) ; }
Sort an array of objects by more than one field .
48,639
public function where ( $ field , $ op , $ value ) { $ this -> pending [ 'where' ] [ ] = array ( 'type' => 'and' , 'field' => $ field , 'op' => $ op , 'value' => $ value , ) ; return $ this ; }
Where function like SQL
48,640
public function asArray ( $ key = null , $ value = null ) { if ( ! is_null ( $ key ) ) { Helpers \ Validate :: table ( $ this -> name ) -> field ( $ key ) ; } if ( ! is_null ( $ value ) ) { Helpers \ Validate :: table ( $ this -> name ) -> field ( $ value ) ; } $ datas = array ( ) ; if ( ! $ this -> resetKeys ) { if ( is_null ( $ key ) && is_null ( $ value ) ) { return $ this -> data ; } else { foreach ( $ this -> data as $ rowKey => $ data ) { $ datas [ $ rowKey ] = array ( ) ; foreach ( $ data as $ row ) { if ( is_null ( $ key ) ) { $ datas [ $ rowKey ] [ ] = $ row -> { $ value } ; } elseif ( is_null ( $ value ) ) { $ datas [ $ rowKey ] [ $ row -> { $ key } ] = $ row ; } else { $ datas [ $ rowKey ] [ $ row -> { $ key } ] = $ row -> { $ value } ; } } } } } else { if ( is_null ( $ key ) && is_null ( $ value ) ) { foreach ( $ this -> data as $ data ) { $ datas [ ] = get_object_vars ( $ data ) ; } } else { foreach ( $ this -> data as $ data ) { if ( is_null ( $ key ) ) { $ datas [ ] = $ data -> { $ value } ; } elseif ( is_null ( $ value ) ) { $ datas [ $ data -> { $ key } ] = $ data ; } else { $ datas [ $ data -> { $ key } ] = $ data -> { $ value } ; } } } } return $ datas ; }
Returning data as indexed or assoc array .
48,641
public function deleteFields ( array $ fields ) { $ fields = Helpers \ Validate :: arrToLower ( $ fields ) ; Helpers \ Validate :: table ( $ this -> name ) -> fields ( $ fields ) ; $ config = $ this -> config ( ) ; $ config -> schema = array_diff_key ( $ this -> schema ( ) , array_flip ( $ fields ) ) ; $ data = $ this -> getData ( ) ; foreach ( $ data as $ key => $ object ) { foreach ( $ fields as $ name ) { unset ( $ data [ $ key ] -> { $ name } ) ; } } Helpers \ Data :: table ( $ this -> name ) -> put ( $ data ) ; Helpers \ Config :: table ( $ this -> name ) -> put ( $ config ) ; }
Delete fields from array
48,642
public function relations ( $ tableName = null ) { return Helpers \ Config :: table ( $ this -> name ) -> relations ( $ tableName , true ) ; }
Returning assoc array with relationed tables
48,643
public function save ( ) { $ data = $ this -> getData ( ) ; if ( ! $ this -> currentId ) { $ config = $ this -> config ( ) ; $ config -> last_id ++ ; $ this -> setField ( 'id' , $ config -> last_id ) ; array_push ( $ data , $ this -> set ) ; Helpers \ Config :: table ( $ this -> name ) -> put ( $ config ) ; } else { $ this -> setField ( 'id' , $ this -> currentId ) ; $ data [ $ this -> currentKey ] = $ this -> set ; } Helpers \ Data :: table ( $ this -> name ) -> put ( $ data ) ; }
Saving inserted or updated data
48,644
public function delete ( ) { $ data = $ this -> getData ( ) ; if ( isset ( $ this -> currentId ) ) { unset ( $ data [ $ this -> currentKey ] ) ; } else { $ this -> pending ( ) ; $ old = $ data ; $ data = array_diff_key ( $ old , $ this -> data ) ; } $ this -> data = array_values ( $ data ) ; return Helpers \ Data :: table ( $ this -> name ) -> put ( $ this -> data ) ? true : false ; }
Deleting loaded data
48,645
public function find ( $ id = NULL ) { if ( $ id !== NULL ) { $ data = $ this -> getData ( ) ; $ this -> currentId = $ id ; $ this -> currentKey = $ this -> getRowKey ( $ id ) ; foreach ( $ data [ $ this -> currentKey ] as $ field => $ value ) { $ this -> setField ( $ field , $ value ) ; } } else { $ this -> limit ( 1 ) -> findAll ( ) ; $ data = $ this -> data ; if ( count ( $ data ) ) { foreach ( $ data [ 0 ] as $ field => $ value ) { $ this -> setField ( $ field , $ value ) ; } $ this -> currentId = $ this -> getField ( 'id' ) ; $ this -> currentKey = $ this -> getRowKey ( $ this -> currentId ) ; } } return clone $ this ; }
Returns one row with specified ID
48,646
public function findAll ( ) { $ this -> pending ( ) ; $ this -> data = $ this -> resetKeys ? array_values ( $ this -> data ) : $ this -> data ; return clone $ this ; }
Make data ready to read
48,647
public function debug ( ) { $ print = "Lazer::table(" . $ this -> name . ")\n" ; foreach ( $ this -> pending as $ function => $ values ) { if ( ! empty ( $ values ) ) { if ( is_array ( $ values ) ) { if ( is_array ( reset ( $ values ) ) ) { foreach ( $ values as $ value ) { if ( $ function == 'where' ) { array_shift ( $ value ) ; } if ( $ function == 'with' ) { $ params = implode ( ':' , $ value ) ; } else { $ params = implode ( ', ' , $ value ) ; } $ print .= "\t" . '->' . $ function . '(' . $ params . ')' . "\n" ; } } else { $ params = implode ( ', ' , $ values ) ; $ print .= "\t" . '->' . $ function . '(' . $ params . ')' . "\n" ; } } else { $ print .= "\t" . '->' . $ function . '(' . $ values . ')' . "\n" ; } } } echo '<pre>' . print_r ( $ print , true ) . '</pre>' ; $ this -> clearQuery ( ) ; }
Debug functions prints whole query with values
48,648
public static function createFromFormat ( $ format , $ time , $ timezone = null ) { $ created = empty ( $ timezone ) ? parent :: createFromFormat ( $ format , $ time ) : parent :: createFromFormat ( $ format , $ time , $ timezone ) ; if ( false === $ created ) { return false ; } $ wp_date_time = new static ( '@' . $ created -> getTimestamp ( ) ) ; return $ wp_date_time -> setTimezone ( $ created -> getTimezone ( ) ) ; }
Overrides upstream method to correct returned instance type to the inheriting one .
48,649
public static function getWpTimezone ( ) { $ timezone_string = get_option ( 'timezone_string' ) ; if ( ! empty ( $ timezone_string ) ) { return new static ( $ timezone_string ) ; } $ offset = get_option ( 'gmt_offset' ) ; $ hours = ( int ) $ offset ; $ minutes = abs ( ( $ offset - ( int ) $ offset ) * 60 ) ; $ offset = sprintf ( '%+03d:%02d' , $ hours , $ minutes ) ; return new static ( $ offset ) ; }
Determine time zone from WordPress options and return as object .
48,650
public function force ( PHPUnit_Framework_TestCase $ test ) { if ( ! $ test -> hasFailed ( ) ) { return ; } $ testReflection = new ReflectionClass ( 'PHPUnit_Framework_TestCase' ) ; $ resultReflection = new ReflectionClass ( 'PHPUnit_Framework_TestResult' ) ; $ result = $ this -> getPropertyValue ( $ testReflection , 'result' , $ test ) ; $ this -> forcePropertyValue ( $ resultReflection , 'errors' , array ( ) , $ result ) -> forcePropertyValue ( $ resultReflection , 'failures' , array ( ) , $ result ) -> forcePropertyValue ( $ resultReflection , 'risky' , array ( ) , $ result ) -> forcePropertyValue ( $ testReflection , 'status' , PHPUnit_Runner_BaseTestRunner :: STATUS_PASSED , $ test ) -> forcePropertyValue ( $ testReflection , 'statusMessage' , '' , $ test ) ; }
Failing test cases are not a problem anymore .
48,651
public function getRam ( ) { $ return = [ ] ; $ return [ 'swapTotal' ] = 0 ; $ return [ 'swapFree' ] = 0 ; $ return [ 'swapInfo' ] = [ ] ; $ return [ 'total' ] = $ this -> sysctl [ 'hw.physmem' ] ; try { $ vmstat = $ this -> exec -> exec ( 'vmstat' ) ; if ( preg_match ( '/\s\d\s\d\s\d\s+\d+\s+(\d+)/' , $ vmstat , $ vmstat_match ) ) { $ hard_ram_free = $ vmstat_match [ 1 ] ; $ return [ 'type' ] = 'Physical' ; $ return [ 'free' ] = $ return [ 'total' ] - ( $ hard_ram_free * 1024 ) ; } } catch ( Exception $ e ) { Errors :: add ( 'Linfo Core' , 'Error using `vmstat` to get memory usage usage' ) ; } try { $ swapinfo = $ this -> exec -> exec ( 'swapctl' , '-k' ) ; @ preg_match_all ( '/^(\S+)\s+(\d+)\s+(\d+)\s+(\d+)/m' , $ swapinfo , $ sm , PREG_SET_ORDER ) ; foreach ( $ sm as $ swap ) { $ return [ 'swapTotal' ] += $ swap [ 2 ] * 1024 ; $ return [ 'swapFree' ] += ( ( $ swap [ 2 ] - $ swap [ 3 ] ) * 1024 ) ; $ ft = is_file ( $ swap [ 1 ] ) ? @ filetype ( $ swap [ 1 ] ) : 'Unknown' ; $ return [ 'swapInfo' ] [ ] = array ( 'device' => $ swap [ 1 ] , 'size' => $ swap [ 2 ] * 1024 , 'used' => $ swap [ 3 ] * 1024 , 'type' => ucfirst ( $ ft ) , ) ; } } catch ( Exception $ e ) { Errors :: add ( 'Linfo Core' , 'Error using `swapctl` to get swap usage' ) ; } return $ return ; }
Get memory usage statistics
48,652
public function getDevs ( ) { if ( ! empty ( $ this -> settings [ 'timer' ] ) ) { $ t = new Timer ( 'Hardware Devices' ) ; } if ( preg_match_all ( '/([a-z]+\d+) at ([a-z]+)\d*.+ "(.+)"/m' , $ this -> dmesg , $ devices_match , PREG_SET_ORDER ) == 0 ) { return [ ] ; } $ devices = [ ] ; foreach ( $ devices_match as $ match ) { $ type = strtoupper ( $ match [ 2 ] ) ; $ devices [ ] = array ( 'vendor' => false , 'device' => $ match [ 3 ] , 'type' => $ type , ) ; } return $ devices ; }
Get hardware devices
48,653
public function getHD ( ) { if ( ! empty ( $ this -> settings [ 'timer' ] ) ) { $ t = new Timer ( 'CPU' ) ; } $ drives = [ ] ; $ curr_hd = false ; foreach ( explode ( "\n" , $ this -> dmesg ) as $ dmesg_line ) { if ( preg_match ( '/^(\w+) at .+<([^>]+)>/' , $ dmesg_line , $ hd_start_match ) ) { $ curr_hd = $ hd_start_match ; } elseif ( $ curr_hd !== false && preg_match ( '/^' . preg_quote ( $ curr_hd [ 1 ] ) . ':.*\b(\d+)MB/' , $ dmesg_line , $ hd_spec_match ) ) { $ drives [ ] = array ( 'name' => trim ( $ curr_hd [ 2 ] , ', ' ) , 'vendor' => false , 'device' => '/dev/' . $ curr_hd [ 1 ] , 'size' => $ hd_spec_match [ 1 ] * 1048576 , 'reads' => false , 'writes' => false , ) ; $ curr_hd = false ; } else { $ curr_hd = false ; } } return $ drives ; }
Get hard disk drives and the like
48,654
private function _window_with_lines ( $ name , $ lines , $ x = 5 , $ y = 5 , $ set_width = false ) { $ lines = ( array ) $ lines ; $ lines = array_filter ( $ lines ) ; if ( ! is_numeric ( $ set_width ) ) { $ longest_line = strlen ( $ name ) + 10 ; foreach ( $ lines as $ line ) { $ length = strlen ( implode ( '' , $ line ) ) ; $ longest_line = $ length > $ longest_line ? $ length : $ longest_line ; } $ width = $ longest_line + 4 ; } else { $ width = $ set_width ; } $ height = 3 + count ( $ lines ) ; $ win = ncurses_newwin ( $ height , $ width , $ x , $ y ) ; $ side = ord ( '|' ) ; ncurses_wborder ( $ win , $ side , $ side , ord ( '-' ) , ord ( '-' ) , ord ( '/' ) , ord ( '\\' ) , ord ( '\\' ) , ord ( '/' ) ) ; ncurses_mvwaddstr ( $ win , 1 , 1 , $ this -> _charpad ( $ name , $ width , 'c' , '=' ) ) ; $ v = 1 ; foreach ( $ lines as $ line ) { ncurses_mvwaddstr ( $ win , $ v + 1 , 1 , $ this -> _charpad ( $ line [ 0 ] . $ this -> _charpad ( $ line [ 1 ] , $ width - strlen ( $ line [ 0 ] ) , 'r' , '.' ) , $ width , 'n' ) ) ; ++ $ v ; } ncurses_wrefresh ( $ win ) ; $ this -> _windows [ ] = & $ win ; return array ( $ width , $ height ) ; }
Create a window with various lines as content
48,655
private function _charpad ( $ string , $ length , $ direction , $ filler = ' ' ) { $ strlen = strlen ( $ string ) ; $ difference = $ length - $ strlen ; if ( $ difference < 0 ) { return substr ( $ string , 0 , $ length ) ; } switch ( $ direction ) { case 'r' : return str_repeat ( $ filler , $ difference - 2 ) . $ string ; break ; case 'l' : return $ string . str_repeat ( $ filler , $ difference - 2 ) ; break ; case 'c' : $ cdiff = floor ( $ difference / 2 ) - ( $ difference % 2 == 0 ? 1 : 0 ) ; return str_repeat ( $ filler , $ cdiff - 1 ) . $ string . str_repeat ( $ filler , $ cdiff ) ; break ; case 'n' : return $ string ; break ; default : return '' ; break ; } }
Because I got tired of sprintf
48,656
private function hit_api ( ) { if ( ! $ this -> _url ) { Errors :: add ( 'transmission extension' , 'Config option transmission_api_url not specified' ) ; return ; } if ( ! extension_loaded ( 'curl' ) ) { Errors :: add ( 'transmission extension' , 'Curl PHP extension not installed' ) ; return ; } $ curl = curl_init ( ) ; if ( ! $ curl ) { Errors :: add ( 'transmission extension' , 'failed initializing curl' ) ; return ; } $ url = $ this -> _url . '/transmission/rpc' ; curl_setopt_array ( $ curl , [ CURLOPT_URL => $ url , CURLOPT_HEADER => true , CURLOPT_RETURNTRANSFER => true , ] ) ; $ result = curl_exec ( $ curl ) ; if ( ! $ result ) { Errors :: add ( 'transmission extension' , 'failed running curl to get token' ) ; return ; } if ( ! preg_match ( '/X-Transmission-Session-Id: (\S+)/' , $ result , $ m ) ) { Errors :: add ( 'transmission extension' , 'Failed parsing token' ) ; return ; } $ token = $ m [ 1 ] ; $ postdata = json_encode ( [ 'method' => 'torrent-get' , 'arguments' => [ 'fields' => [ 'name' , 'percentDone' , 'rateDownload' , 'rateUpload' , 'leftUntilDone' , 'eta' , 'status' , 'uploadRatio' , 'downloadedEver' , 'uploadedEver' , ] ] ] ) ; curl_setopt_array ( $ curl , [ CURLOPT_HTTPHEADER => [ 'X-Transmission-Session-Id: ' . $ token ] , CURLOPT_URL => $ url , CURLOPT_POSTFIELDS => $ postdata , CURLOPT_HEADER => false , ] ) ; $ result = curl_exec ( $ curl ) ; curl_close ( $ curl ) ; if ( ! $ result ) { Errors :: add ( 'transmission extension' , 'failed running curl to get data' ) ; return ; } $ response = json_decode ( $ result , true ) ; if ( ! $ response ) { Errors :: add ( 'transmission extension' , 'failed decoding transmission response as json' ) ; return ; } if ( $ response [ 'result' ] != 'success' ) { Errors :: add ( 'transmission extension' , 'transmission API call was not successful: ' . $ response [ 'result' ] ) ; return ; } return $ response [ 'arguments' ] [ 'torrents' ] ; }
Use libcurl to interact with the transmission server API
48,657
public function getMounts ( ) { if ( ! empty ( $ this -> settings [ 'timer' ] ) ) { $ t = new Timer ( 'Mounted file systems' ) ; } try { $ res = $ this -> exec -> exec ( 'mount' ) ; } catch ( Exception $ e ) { Errors :: add ( 'Linfo Core' , 'Error running `mount` command' ) ; return [ ] ; } if ( preg_match_all ( '/^(\S+) on (\S+) \((\w+)(?:, (.+))?\)/m' , $ res , $ m , PREG_SET_ORDER ) == 0 ) { return [ ] ; } $ mounts = [ ] ; foreach ( $ m as $ mount ) { if ( in_array ( $ mount [ 1 ] , $ this -> settings [ 'hide' ] [ 'storage_devices' ] ) || in_array ( $ mount [ 3 ] , $ this -> settings [ 'hide' ] [ 'filesystems' ] ) ) { continue ; } $ size = @ disk_total_space ( $ mount [ 2 ] ) ; $ free = @ disk_free_space ( $ mount [ 2 ] ) ; $ used = $ size - $ free ; if ( $ this -> settings [ 'show' ] [ 'mounts_options' ] && ! in_array ( $ mount [ 3 ] , ( array ) $ this -> settings [ 'hide' ] [ 'fs_mount_options' ] ) && isset ( $ mount [ 4 ] ) ) { $ mount_options = explode ( ', ' , $ mount [ 4 ] ) ; } else { $ mount_options = [ ] ; } $ mounts [ ] = array ( 'device' => $ mount [ 1 ] , 'mount' => $ mount [ 2 ] , 'type' => $ mount [ 3 ] , 'size' => $ size , 'used' => $ used , 'free' => $ free , 'free_percent' => ( ( bool ) $ free != false && ( bool ) $ size != false ? round ( $ free / $ size , 2 ) * 100 : false ) , 'used_percent' => ( ( bool ) $ used != false && ( bool ) $ size != false ? round ( $ used / $ size , 2 ) * 100 : false ) , 'options' => $ mount_options , ) ; } return $ mounts ; }
Get mounted file systems
48,658
public function getCPU ( ) { if ( ! empty ( $ this -> settings [ 'timer' ] ) ) { $ t = new Timer ( 'CPUs' ) ; } $ cpus = [ ] ; for ( $ i = 0 ; $ i < $ this -> sysctl [ 'hw.ncpu' ] ; ++ $ i ) { $ cpus [ ] = array ( 'Model' => $ this -> sysctl [ 'hw.model' ] , 'MHz' => $ this -> sysctl [ 'hw.clockrate' ] , ) ; } return $ cpus ; }
I still don t really like how this is done
48,659
public function getDevs ( ) { if ( ! empty ( $ this -> settings [ 'timer' ] ) ) { $ t = new Timer ( 'Hardware Devices' ) ; } $ hw = new Hwpci ( null , '/usr/share/misc/pci_vendors' ) ; $ hw -> work ( 'dragonfly' ) ; return $ hw -> result ( ) ; }
Parse dmesg boot log
48,660
public function getProcessStats ( ) { if ( ! empty ( $ this -> settings [ 'timer' ] ) ) { $ t = new Timer ( 'Process Stats' ) ; } $ result = array ( 'exists' => true , 'totals' => array ( 'running' => 0 , 'zombie' => 0 , 'sleeping' => 0 , 'stopped' => 0 , 'idle' => 0 , ) , 'proc_total' => 0 , 'threads' => false , ) ; try { $ ps = $ this -> exec -> exec ( 'ps' , 'ax' ) ; preg_match_all ( '/^\s*\d+\s+[\w?]+\s+([A-Z])\S*\s+.+$/m' , $ ps , $ processes , PREG_SET_ORDER ) ; $ result [ 'proc_total' ] = count ( $ processes ) ; foreach ( $ processes as $ process ) { switch ( $ process [ 1 ] ) { case 'S' : case 'I' : $ result [ 'totals' ] [ 'sleeping' ] ++ ; break ; case 'Z' : $ result [ 'totals' ] [ 'zombie' ] ++ ; break ; case 'R' : case 'D' : $ result [ 'totals' ] [ 'running' ] ++ ; break ; case 'T' : $ result [ 'totals' ] [ 'stopped' ] ++ ; break ; case 'W' : $ result [ 'totals' ] [ 'idle' ] ++ ; break ; } } } catch ( Exception $ e ) { Errors :: add ( 'Linfo Core' , 'Error using `ps` to get process info' ) ; } return $ result ; }
Get stats on processes
48,661
private function _call ( ) { $ t = new Timer ( 'CUPS extension' ) ; try { $ result = $ this -> _CallExt -> exec ( 'lpstat' , '-p -o -l' ) ; } catch ( Exception $ e ) { Errors :: add ( 'CUPS Extension' , $ e -> getMessage ( ) ) ; $ this -> _res = false ; return false ; } $ lines = explode ( "\n" , $ result ) ; $ printers = [ ] ; $ queue = [ ] ; $ begin_queue_list = false ; for ( $ i = 0 , $ num = count ( $ lines ) ; $ i < $ num ; ++ $ i ) { $ lines [ $ i ] = trim ( $ lines [ $ i ] ) ; if ( $ lines [ $ i ] == 'no entries' ) { break ; } elseif ( preg_match ( '/^printer (.+) is idle\. (.+)$/' , $ lines [ $ i ] , $ printers_match ) == 1 ) { $ printers [ ] = array ( 'name' => str_replace ( '_' , ' ' , $ printers_match [ 1 ] ) , 'status' => $ printers_match [ 2 ] , ) ; } elseif ( preg_match ( '/^(.+)+ is (ready|ready and printing|not ready)$/' , $ lines [ $ i ] , $ printers_match ) == 1 ) { $ printers [ ] = array ( 'name' => str_replace ( '_' , ' ' , $ printers_match [ 1 ] ) , 'status' => $ printers_match [ 2 ] , ) ; } elseif ( preg_match ( '/^Rank\s+Owner\s+Job\s+File\(s\)\s+Total Size$/' , $ lines [ $ i ] ) ) { $ begin_queue_list = true ; } elseif ( $ begin_queue_list && preg_match ( '/^([a-z0-9]+)\s+(\S+)\s+(\d+)\s+(.+)\s+(\d+) bytes$/' , $ lines [ $ i ] , $ queue_match ) ) { $ queue [ ] = array ( 'rank' => $ queue_match [ 1 ] , 'owner' => $ queue_match [ 2 ] , 'job' => $ queue_match [ 3 ] , 'files' => $ queue_match [ 4 ] , 'size' => Common :: byteConvert ( $ queue_match [ 5 ] ) , ) ; } } $ this -> _res = array ( 'printers' => $ printers , 'queue' => $ queue , ) ; return true ; }
call lpq and parse it
48,662
public function getBattery ( ) { if ( ! empty ( $ this -> settings [ 'timer' ] ) ) { $ t = new Timer ( 'Batteries' ) ; } $ batts = [ ] ; try { $ res = $ this -> exec -> exec ( 'apm' , '-abl' ) ; } catch ( Exception $ e ) { Errors :: add ( 'Linfo Core' , 'Error using `apm` battery info' ) ; return $ batts ; } list ( , $ bat_status , $ percentage ) = explode ( "\n" , $ res ) ; switch ( $ bat_status ) { case 0 : $ status = 'High' ; break ; case 1 : $ status = 'Low' ; break ; case 2 : $ status = 'Critical' ; break ; case 3 : $ status = 'Charging' ; break ; default : $ status = 'Unknown' ; break ; } $ batts [ ] = array ( 'percentage' => $ percentage . '%' , 'state' => $ status , 'device' => 'battery' , ) ; return $ batts ; }
APM? Seems to only support either one battery of them all collectively
48,663
public function setSearchPaths ( $ paths ) { if ( array_key_exists ( 'additional_paths' , self :: $ settings ) && is_array ( self :: $ settings [ 'additional_paths' ] ) && count ( self :: $ settings [ 'additional_paths' ] ) > 0 ) { $ paths = array_merge ( self :: $ settings [ 'additional_paths' ] , $ paths ) ; } foreach ( $ paths as $ k => $ v ) { $ paths [ $ k ] .= substr ( $ v , - 1 ) == '/' ? '' : '/' ; } $ this -> searchPaths = $ paths ; }
Say where we ll search for execs .
48,664
public function getLoad ( ) { if ( ! empty ( $ this -> settings [ 'timer' ] ) ) { $ t = new Timer ( 'Load Averages' ) ; } $ parts = explode ( ' ' , trim ( $ this -> sysctl [ 'vm.loadavg' ] ) ) ; if ( ! $ parts ) { return [ ] ; } return array_combine ( array ( 'now' , '5min' , '15min' ) , $ parts ) ; }
System load averages
48,665
private function _call ( ) { $ t = new Timer ( 'Truecrypt Extension' ) ; try { $ result = $ this -> _CallExt -> exec ( 'truecrypt' , '-l -v' ) ; } catch ( Exception $ e ) { Errors :: add ( 'Truecrypt Extension' , $ e -> getMessage ( ) ) ; $ this -> _res = false ; return false ; } $ this -> _res = [ ] ; $ curr = false ; $ lines = explode ( "\n" , $ result ) ; for ( $ i = 0 , $ num = count ( $ lines ) ; $ i < $ num ; ++ $ i ) { if ( ! preg_match ( '/^([^:]+): ([^$]+)$/' , $ lines [ $ i ] , $ line_match ) ) { continue ; } switch ( $ line_match [ 1 ] ) { case 'Slot' : if ( $ curr === false ) { $ curr = array ( 'slot' => $ line_match [ 2 ] ) ; } elseif ( is_array ( $ curr ) ) { $ this -> _res [ ] = $ curr ; $ curr = false ; } break ; case 'Volume' : if ( is_array ( $ curr ) ) { $ curr [ 'volume' ] = $ line_match [ 2 ] ; } break ; case 'Virtual Device' : if ( is_array ( $ curr ) ) { $ curr [ 'virtual_device' ] = $ line_match [ 2 ] ; } break ; case 'Mount Directory' : if ( is_array ( $ curr ) ) { $ curr [ 'mount_directory' ] = $ line_match [ 2 ] ; } break ; case 'Size' : if ( is_array ( $ curr ) ) { $ curr [ 'size' ] = $ line_match [ 2 ] ; } break ; case 'Read-Only' : if ( is_array ( $ curr ) ) { $ curr [ 'read_only' ] = $ line_match [ 2 ] ; } break ; default : continue ; break ; } } if ( is_array ( $ curr ) && count ( $ curr ) > 0 ) { $ this -> _res [ ] = $ curr ; } return true ; }
call truecrypt and parse it
48,666
public static function arrayAppendString ( $ array , $ string = '' , $ format = '%1s%2s' ) { foreach ( $ array as $ k => $ v ) { $ array [ $ k ] = is_string ( $ v ) ? sprintf ( $ format , $ v , $ string ) : $ v ; } return $ array ; }
Append a string to the end of each element in a 2d array
48,667
public static function byteConvert ( $ size , $ precision = 2 ) { if ( ! is_numeric ( $ size ) ) { return '?' ; } $ notation = self :: $ settings [ 'byte_notation' ] == 1000 ? 1000 : 1024 ; $ types = array ( 'B' , 'KB' , 'MB' , 'GB' , 'TB' ) ; $ types_i = array ( 'B' , 'KiB' , 'MiB' , 'GiB' , 'TiB' ) ; for ( $ i = 0 ; $ size >= $ notation && $ i < ( count ( $ types ) - 1 ) ; $ size /= $ notation , $ i ++ ) ; return ( round ( $ size , $ precision ) . ' ' . ( $ notation == 1000 ? $ types [ $ i ] : $ types_i [ $ i ] ) ) ; }
Convert bytes to stuff like KB MB GB TB etc
48,668
public static function secondsConvert ( $ uptime ) { $ uptime += $ uptime > 60 ? 30 : 0 ; $ years = floor ( $ uptime / 31556926 ) ; $ uptime %= 31556926 ; $ days = floor ( $ uptime / 86400 ) ; $ uptime %= 86400 ; $ hours = floor ( $ uptime / 3600 ) ; $ uptime %= 3600 ; $ minutes = floor ( $ uptime / 60 ) ; $ seconds = floor ( $ uptime % 60 ) ; $ return = [ ] ; if ( $ years > 0 ) { $ return [ ] = $ years . ' ' . ( $ years > 1 ? self :: $ lang [ 'years' ] : substr ( self :: $ lang [ 'years' ] , 0 , strlen ( self :: $ lang [ 'years' ] ) - 1 ) ) ; } if ( $ days > 0 ) { $ return [ ] = $ days . ' ' . self :: $ lang [ 'days' ] ; } if ( $ hours > 0 ) { $ return [ ] = $ hours . ' ' . self :: $ lang [ 'hours' ] ; } if ( $ minutes > 0 ) { $ return [ ] = $ minutes . ' ' . self :: $ lang [ 'minutes' ] ; } if ( $ seconds > 0 ) { $ return [ ] = $ seconds . ( date ( 'm/d' ) == '06/03' ? ' sex' : ' ' . self :: $ lang [ 'seconds' ] ) ; } return implode ( ', ' , $ return ) ; }
Like above but for seconds
48,669
public static function getContents ( $ file , $ default = '' ) { if ( is_string ( self :: $ path_prefix ) ) { $ file = self :: $ path_prefix . $ file ; } return ! is_file ( $ file ) || ! is_readable ( $ file ) || ! ( $ contents = @ file_get_contents ( $ file ) ) ? $ default : trim ( $ contents ) ; }
Get a file s contents or default to second param
48,670
public static function getLines ( $ file ) { return ! is_file ( $ file ) || ! is_readable ( $ file ) || ! ( $ lines = @ file ( $ file , FILE_SKIP_EMPTY_LINES ) ) ? [ ] : $ lines ; }
Like above but in lines instead of a big string
48,671
public static function getVarFromFile ( $ file , $ variable ) { if ( ! is_file ( $ file ) ) { return false ; } require $ file ; if ( isset ( $ $ variable ) ) { return $ $ variable ; } return false ; }
clobbering the main namespace
48,672
public function getRAM ( ) { if ( ! empty ( $ this -> settings [ 'timer' ] ) ) { $ t = new Timer ( 'Memory' ) ; } return array ( 'type' => 'Physical' , 'total' => $ this -> kstat [ 'unix:0:system_pages:pagestotal' ] * $ this -> kstat [ 'unix:0:seg_cache:slab_size' ] , 'free' => $ this -> kstat [ 'unix:0:system_pages:pagesfree' ] * $ this -> kstat [ 'unix:0:seg_cache:slab_size' ] , 'swapInfo' => [ ] , ) ; }
Get ram stats
48,673
public function getModel ( ) { if ( preg_match ( '/^\s+Model Name:\s+(.+)/m' , $ this -> systemProfiler , $ m ) ) { return $ m [ 1 ] ; } if ( preg_match ( '/^([a-zA-Z]+)/' , $ this -> sysctl [ 'hw.model' ] , $ m ) ) { return $ m [ 1 ] ; } else { return $ this -> sysctl [ 'hw.model' ] ; } }
Model of mac
48,674
private function _call ( ) { $ t = new Timer ( 'apcaccess Extension' ) ; try { $ result = $ this -> _CallExt -> exec ( 'apcaccess' ) ; } catch ( Exception $ e ) { Errors :: add ( 'apcaccess Extension' , $ e -> getMessage ( ) ) ; $ this -> _res = false ; return false ; } $ this -> _res = [ ] ; if ( preg_match ( '/^UPSNAME\s+:\s+(.+)$/m' , $ result , $ m ) ) { $ this -> _res [ 'name' ] = $ m [ 1 ] ; } if ( preg_match ( '/^MODEL\s+:\s+(.+)$/m' , $ result , $ m ) ) { $ this -> _res [ 'model' ] = $ m [ 1 ] ; } if ( preg_match ( '/^BATTV\s+:\s+(\d+\.\d+)/m' , $ result , $ m ) ) { $ this -> _res [ 'volts' ] = $ m [ 1 ] ; } if ( preg_match ( '/^BCHARGE\s+:\s+(\d+(?:\.\d+)?)/m' , $ result , $ m ) ) { $ charge = ( int ) $ m [ 1 ] ; $ this -> _res [ 'charge' ] = ' <div class="bar_chart"> <div class="bar_inner" style="width: ' . ( int ) $ charge . '%;"> <div class="bar_text"> ' . ( $ charge ? $ charge . '%' : '?' ) . ' </div> </div> </div> ' ; } if ( preg_match ( '/^TIMELEFT\s+:\s+([\d\.]+)/m' , $ result , $ m ) ) { $ this -> _res [ 'time_left' ] = Common :: secondsConvert ( $ m [ 1 ] * 60 ) ; } if ( preg_match ( '/^STATUS\s+:\s+([A-Z]+)/m' , $ result , $ m ) ) { $ this -> _res [ 'status' ] = $ m [ 1 ] == 'ONBATT' ? 'On Battery' : ucfirst ( strtolower ( $ m [ 1 ] ) ) ; } if ( preg_match ( '/^LOADPCT\s+:\s+(\d+\.\d+)/m' , $ result , $ m ) ) { $ load = ( int ) $ m [ 1 ] ; $ this -> _res [ 'load' ] = ' <div class="bar_chart"> <div class="bar_inner" style="width: ' . ( int ) $ load . '%;"> <div class="bar_text"> ' . ( $ load ? $ load . '%' : '?' ) . ' </div> </div> </div> ' ; } if ( isset ( $ load ) && preg_match ( '/^NOMPOWER\s+:\s+(\d+)/m' , $ result , $ m ) ) { $ watts = ( int ) $ m [ '1' ] ; $ this -> _res [ 'watts_used' ] = $ load * round ( $ watts / 100 ) ; } else { $ this -> _res [ 'watts_used' ] = false ; } return true ; }
call apcaccess and parse it
48,675
public function getCPU ( ) { if ( ! empty ( $ this -> settings [ 'timer' ] ) ) { $ t = new Timer ( 'CPU' ) ; } if ( preg_match_all ( '/^cpu\d+ at [^:]+: (\S+) ([^,]+), (\d+)MHz/m' , $ this -> dmesg , $ cpu_matches , PREG_SET_ORDER ) == 0 ) { return [ ] ; } $ cpus = [ ] ; foreach ( $ cpu_matches as $ cpu_m ) { $ cpus [ ] = array ( 'Model' => $ cpu_m [ 2 ] , 'MHz' => $ cpu_m [ 3 ] , 'Vendor' => $ cpu_m [ 1 ] , ) ; } return $ cpus ; }
Get cpu s
48,676
public function getUpTime ( ) { if ( ! empty ( $ this -> settings [ 'timer' ] ) ) { $ t = new Timer ( 'Uptime' ) ; } $ contents = Common :: getContents ( '/proc/uptime' , false ) ; if ( $ contents === false ) { Errors :: add ( 'Linfo Core' , '/proc/uptime does not exist.' ) ; return 'Unknown' ; } list ( $ seconds ) = explode ( ' ' , $ contents , 1 ) ; $ uptime = Common :: secondsConvert ( ceil ( $ seconds ) ) ; $ contents = Common :: getContents ( '/proc/stat' , false ) ; if ( $ contents === false ) { return $ uptime ; } if ( preg_match ( '/^btime (\d+)$/m' , $ contents , $ boot ) != 1 ) { return $ uptime ; } list ( , $ boot ) = $ boot ; return array ( 'text' => $ uptime , 'bootedTimestamp' => $ boot , ) ; }
Famously interesting uptime
48,677
private function _populate_cache ( ) { if ( $ this -> _use_json ) { if ( is_readable ( $ this -> _cache_file ) && ( $ loaded = @ json_decode ( Common :: getContents ( $ this -> _cache_file , '' ) , true ) ) && is_array ( $ loaded ) ) { $ this -> _existing_cache_vals = $ loaded ; } } else { if ( is_readable ( $ this -> _cache_file ) && ( $ loaded = @ unserialize ( Common :: getContents ( $ this -> _cache_file , false ) ) ) && is_array ( $ loaded ) ) { $ this -> _existing_cache_vals = $ loaded ; } } }
Run the cache file .
48,678
private function _fetchPciNames ( ) { for ( $ v = false , $ file = @ fopen ( $ this -> _pci_file , 'r' ) ; $ file != false && $ contents = fgets ( $ file ) ; ) { if ( preg_match ( '/^(\S{4})\s+([^$]+)$/' , $ contents , $ vend_match ) == 1 ) { $ v = $ vend_match ; } elseif ( preg_match ( '/^\s+(\S{4})\s+([^$]+)$/' , $ contents , $ dev_match ) == 1 ) { if ( $ v && isset ( $ this -> _pci_entries [ strtolower ( $ v [ 1 ] ) ] [ strtolower ( $ dev_match [ 1 ] ) ] ) ) { $ this -> _pci_devices [ $ v [ 1 ] ] [ $ dev_match [ 1 ] ] = array ( 'vendor' => rtrim ( $ v [ 2 ] ) , 'device' => rtrim ( $ dev_match [ 2 ] ) ) ; } } } $ file && @ fclose ( $ file ) ; }
Use the pci . ids file to translate the ids to names .
48,679
private function _fetchUsbNames ( ) { for ( $ v = false , $ file = @ fopen ( $ this -> _usb_file , 'r' ) ; $ file != false && $ contents = fgets ( $ file ) ; ) { if ( preg_match ( '/^(\S{4})\s+([^$]+)$/' , $ contents , $ vend_match ) == 1 ) { $ v = $ vend_match ; } elseif ( preg_match ( '/^\s+(\S{4})\s+([^$]+)$/' , $ contents , $ dev_match ) == 1 ) { if ( $ v && isset ( $ this -> _usb_entries [ strtolower ( $ v [ 1 ] ) ] [ strtolower ( $ dev_match [ 1 ] ) ] ) ) { $ this -> _usb_devices [ strtolower ( $ v [ 1 ] ) ] [ $ dev_match [ 1 ] ] = array ( 'vendor' => rtrim ( $ v [ 2 ] ) , 'device' => rtrim ( $ dev_match [ 2 ] ) ) ; } } } $ file && @ fclose ( $ file ) ; }
Use the usb . ids file to translate the ids to names .
48,680
private function _is_cache_worthy ( ) { $ pci_good = true ; foreach ( array_keys ( $ this -> _pci_entries ) as $ vendor ) { foreach ( array_keys ( $ this -> _pci_entries [ $ vendor ] ) as $ dever ) { if ( ! isset ( $ this -> _existing_cache_vals [ 'hw' ] [ 'pci' ] [ $ vendor ] [ $ dever ] ) ) { $ pci_good = false ; break 2 ; } } } $ usb_good = true ; foreach ( array_keys ( $ this -> _usb_entries ) as $ vendor ) { foreach ( array_keys ( $ this -> _usb_entries [ $ vendor ] ) as $ dever ) { if ( ! isset ( $ this -> _existing_cache_vals [ 'hw' ] [ 'usb' ] [ $ vendor ] [ $ dever ] ) ) { $ usb_good = false ; break 2 ; } } } return array ( 'pci' => $ pci_good , 'usb' => $ usb_good ) ; }
Decide if the cache file is sufficient enough to not parse the ids files .
48,681
public function work ( $ os ) { switch ( $ os ) { case 'linux' : $ this -> _fetchPciIdsLinux ( ) ; $ this -> _fetchUsbIdsLinux ( ) ; break ; case 'freebsd' : case 'dragonfly' : $ this -> _fetchPciIdsPciConf ( ) ; break ; default : return ; break ; } $ worthiness = $ this -> _is_cache_worthy ( ) ; $ save_cache = false ; if ( ! $ worthiness [ 'pci' ] ) { $ save_cache = true ; $ this -> _fetchPciNames ( ) ; } else { $ this -> _pci_devices = isset ( $ this -> _existing_cache_vals [ 'hw' ] [ 'pci' ] ) ? $ this -> _existing_cache_vals [ 'hw' ] [ 'pci' ] : [ ] ; } if ( ! $ worthiness [ 'usb' ] ) { $ save_cache = true ; $ this -> _fetchUsbNames ( ) ; } else { $ this -> _usb_devices = isset ( $ this -> _existing_cache_vals [ 'hw' ] [ 'usb' ] ) ? $ this -> _existing_cache_vals [ 'hw' ] [ 'usb' ] : [ ] ; } if ( $ save_cache ) { $ this -> _write_cache ( ) ; } }
Do its goddam job .
48,682
public function result ( ) { foreach ( array_keys ( ( array ) $ this -> _pci_devices ) as $ v ) { foreach ( $ this -> _pci_devices [ $ v ] as $ d ) { $ this -> _result [ ] = array ( 'vendor' => $ d [ 'vendor' ] , 'device' => $ d [ 'device' ] , 'type' => 'PCI' ) ; } } foreach ( array_keys ( ( array ) $ this -> _usb_devices ) as $ v ) { foreach ( $ this -> _usb_devices [ $ v ] as $ d ) { $ this -> _result [ ] = array ( 'vendor' => $ d [ 'vendor' ] , 'device' => $ d [ 'device' ] , 'type' => 'USB' ) ; } } return $ this -> _result ; }
Compile and return results .
48,683
public function getMounts ( ) { try { $ res = $ this -> exec -> exec ( 'mount' ) ; } catch ( Exception $ e ) { return [ ] ; } if ( preg_match_all ( '/^(\S+) is .+ mounted on (\S+) \(.+\)$/m' , $ res , $ mount_matches , PREG_SET_ORDER ) == 0 ) { return [ ] ; } $ mounts = [ ] ; foreach ( $ mount_matches as $ mount ) { $ size = @ disk_total_space ( $ mount [ 2 ] ) ; $ free = @ disk_free_space ( $ mount [ 2 ] ) ; $ used = $ size - $ free ; $ mounts [ ] = array ( 'device' => $ mount [ 1 ] , 'mount' => $ mount [ 2 ] , 'type' => '?' , 'size' => $ size , 'used' => $ used , 'free' => $ free , 'free_percent' => ( ( bool ) $ free != false && ( bool ) $ size != false ? round ( $ free / $ size , 2 ) * 100 : false ) , 'used_percent' => ( ( bool ) $ used != false && ( bool ) $ size != false ? round ( $ used / $ size , 2 ) * 100 : false ) , ) ; } return $ mounts ; }
to work here
48,684
public function getNet ( ) { try { $ res = $ this -> exec -> exec ( 'ifconfig' , '-a' ) ; } catch ( Exception $ e ) { return [ ] ; } if ( preg_match_all ( '/^([^:]+)/m' , $ res , $ net_matches , PREG_SET_ORDER ) == 0 ) { return [ ] ; } $ nets = [ ] ; foreach ( $ net_matches as $ net ) { $ nets [ $ net [ 1 ] ] = array ( 'state' => '?' , 'type' => '?' , ) ; } return $ nets ; }
more than just name of interface
48,685
public function read ( $ key , $ next ) { $ session_key = $ this -> sanitize ( $ key ) ; $ data = wp_cache_get ( "session_${session_key}" , 'sessions' ) ; if ( false === $ data ) { $ data = $ next ( $ key ) ; if ( false !== $ data ) { wp_cache_set ( "session_${session_key}" , $ data , 'sessions' , $ this -> getExpiration ( ) ) ; } } return $ data ; }
Grab the item from the cache if it exists otherwise delve deeper into the stack and retrieve from another underlying middleware .
48,686
public function delete ( $ key , $ next ) { $ session_key = $ this -> sanitize ( $ key ) ; wp_cache_delete ( "session_${session_key}" , 'sessions' ) ; return $ next ( $ key ) ; }
Purge an item from the cache immediately .
48,687
protected function generateTimeOccurrences ( $ dateLooper ) { $ occurrences = [ ] ; foreach ( $ this -> byhours as $ hour ) { foreach ( $ this -> byminutes as $ minute ) { foreach ( $ this -> byseconds as $ second ) { $ occurrence = clone $ dateLooper ; $ occurrence -> setTime ( $ hour , $ minute , $ second ) ; $ occurrences [ ] = $ occurrence ; } } } return $ occurrences ; }
not happy with this .
48,688
public static function daysList ( $ days ) { foreach ( $ days as $ day ) { $ day = ltrim ( $ day , "+" ) ; $ day = trim ( $ day ) ; $ ordwk = 1 ; $ weekday = false ; if ( strlen ( $ day ) === 2 ) { $ weekday = $ day ; } else { list ( $ ordwk , $ weekday ) = sscanf ( $ day , "%d%s" ) ; } if ( ! self :: weekDay ( $ weekday ) || ! self :: ordWk ( abs ( $ ordwk ) ) ) { return false ; } } return true ; }
Test if array of days is valid
48,689
public static function itemsList ( $ items , $ validator ) { foreach ( $ items as $ item ) { if ( ! self :: $ validator ( $ item ) ) { return false ; } } return true ; }
Test for valid itemsList
48,690
public function dtatransFilter ( $ id ) { $ translator = $ this -> _container -> get ( 'translator' ) ; $ callback = function ( $ id ) { $ path = $ this -> _container -> get ( 'kernel' ) -> locateResource ( '@AliDatatableBundle/Resources/translations/messages.en.yml' ) ; return \ Symfony \ Component \ Yaml \ Yaml :: parse ( file_get_contents ( $ path ) ) [ 'ali' ] [ 'common' ] [ explode ( '.' , $ id ) [ 2 ] ] ; } ; return $ translator -> trans ( $ id ) === $ id ? $ callback ( $ id ) : $ translator -> trans ( $ id ) ; }
Datatable translate filter
48,691
private function createDeleteForm ( $ id ) { if ( version_compare ( phpversion ( ) , '5.5' , '<' ) ) { return $ this -> createFormBuilder ( array ( 'id' => $ id ) ) -> add ( 'id' , 'hidden' ) -> getForm ( ) ; } else { return $ this -> createFormBuilder ( array ( 'id' => $ id ) ) -> add ( 'id' , 'Symfony\Component\Form\Extension\Core\Type\HiddenType' ) -> getForm ( ) ; } }
create delete form
48,692
public function createFormBuilder ( $ data = null , array $ options = array ( ) ) { if ( version_compare ( phpversion ( ) , '5.5' , '<' ) ) { return $ this -> _container -> get ( 'form.factory' ) -> createBuilder ( 'form' , $ data , $ options ) ; } else { return $ this -> _container -> get ( 'form.factory' ) -> createBuilder ( 'Symfony\Component\Form\Extension\Core\Type\FormType' , $ data , $ options ) ; } }
create form builder
48,693
protected function _addSearch ( \ Doctrine \ ORM \ QueryBuilder $ queryBuilder ) { if ( $ this -> search == TRUE ) { $ request = $ this -> request ; $ search_fields = array_values ( $ this -> fields ) ; foreach ( $ search_fields as $ i => $ search_field ) { $ search_param = $ request -> get ( "sSearch_{$i}" ) ; if ( $ search_param !== false && $ search_param != '' ) { $ field = explode ( ' ' , trim ( $ search_field ) ) ; $ search_field = $ field [ 0 ] ; $ queryBuilder -> andWhere ( " $search_field like :ssearch{$i} " ) ; $ queryBuilder -> setParameter ( "ssearch{$i}" , '%' . $ request -> get ( "sSearch_{$i}" ) . '%' ) ; } } } }
get the search dql
48,694
protected function _toArray ( $ object ) { $ reflectionClass = new \ ReflectionClass ( get_class ( $ object ) ) ; $ array = array ( ) ; foreach ( $ reflectionClass -> getProperties ( ) as $ property ) { $ property -> setAccessible ( true ) ; $ array [ $ property -> getName ( ) ] = $ property -> getValue ( $ object ) ; $ property -> setAccessible ( false ) ; } return $ array ; }
convert object to array
48,695
public function setWhere ( $ where , array $ params = array ( ) ) { $ this -> queryBuilder -> where ( $ where ) ; $ this -> queryBuilder -> setParameters ( $ params ) ; return $ this ; }
set query where
48,696
protected function _delete_form ( ) { if ( version_compare ( phpversion ( ) , '5.5' , '<' ) ) { return $ this -> container -> get ( 'templating.helper.form' ) -> widget ( $ this -> container -> get ( 'form.factory' ) -> createBuilder ( 'form' , array ( 'id' => '@id' ) , array ( ) ) -> add ( 'id' , 'hidden' ) -> getForm ( ) -> createView ( ) ) ; } else { return $ this -> container -> get ( 'templating.helper.form' ) -> widget ( $ this -> container -> get ( 'form.factory' ) -> createBuilder ( 'Symfony\Component\Form\Extension\Core\Type\FormType' , array ( 'id' => '@id' ) , array ( ) ) -> add ( 'id' , 'Symfony\Component\Form\Extension\Core\Type\HiddenType' ) -> getForm ( ) -> createView ( ) ) ; } }
simple form delete prototype
48,697
protected function _applyDefaults ( ) { if ( isset ( $ this -> _config [ 'all' ] ) ) { $ this -> _has_action = $ this -> _config [ 'all' ] [ 'action' ] ; $ this -> _search = $ this -> _config [ 'all' ] [ 'search' ] ; } }
apply default value from datatable config
48,698
public function setEntityManager ( EntityManager $ em ) { $ this -> _queryBuilder = new DoctrineBuilder ( $ this -> _container , $ em ) ; return $ this ; }
set entity manager
48,699
public function addFields ( array $ fields ) { $ oldFields = $ this -> _queryBuilder -> getFields ( ) ? $ this -> _queryBuilder -> getFields ( ) : array ( ) ; $ newFields = array_merge ( $ oldFields , $ fields ) ; $ this -> setFields ( $ newFields ) ; return $ this ; }
Add an array of fields