*/ class ParTCP_LC_Service { public $fileSystem; public $dataDir; public function __construct( $fileSystem, $dataDir ){ $this->fileSystem = $fileSystem; $this->dataDir = $dataDir; } public function get_dir( $id ){ return "provisioning/{$id}"; } public function get_list( $groupId = FALSE ){ $dir = 'events'; if ( is_object( $this->groupsClass ) && $groupId ){ $dir = $this->groupsClass->get_dir( $groupId ) . "/{$dir}"; } if ( ! $this->fileSystem->exists( $dir ) ){ return array (); } $eventList = $this->fileSystem->get_listing( $dir ); $events = array (); foreach ( $eventList as $eventId ){ $eventId = ( $groupId ? "{$groupId}/" : '' ) . $eventId; if ( $data = $this->get_data( $eventId ) ){ $events[] = $data; } } return $events; } public function get_data( $id ){ $dir = $this->get_dir( $id ); $msg = $this->fileSystem->get_recent_contents( $dir, '[0-9]*-{update,definition}*' ); if ( empty( $msg ) ){ return FALSE; } $receipt = yaml_parse( $msg ); if ( empty( $receipt['Event-Data'] ) ){ return FALSE; } $data = $receipt['Event-Data']; $data['lot_codes'] = $this->fileSystem->is_not_empty( "{$dir}/lots" ); return $data; } public function purge_data( $data ){ $validKeys = [ 'id' => 0, 'name' => 0, 'voting_server' => 0, 'event_id' => 0, ]; return array_intersect_key( $data, $validKeys ); } public function generate_code( $args = [] ){ $charList = (string) ( $args['char_list'] ?? 'ABCDEFGHJKLMNPQRSTUVWXYZ123456789' ); $finalLength = (int) ( $args['final_length'] ?? 16 ); $groupLength = $args['group_length'] ?? 4; $groupSeparator = $args['group_separator'] ?? '-'; $crcLength = $args['crc_length'] ?? 0; $netLength = $finalLength - $crcLength; $charListLength = strlen( $charList ) - 1; $code = ''; for ( $x = 1; $x <= $netLength; $x++ ){ $code .= $charList[ rand( 0, $charListLength ) ]; } if ( $groupLength ){ $code .= str_repeat( ' ', $crcLength ); $code = substr( chunk_split( $code, $groupLength, $groupSeparator ), 0, -1 ); $code = rtrim( $code, ' ' ); } if ( $crcLength ){ $code .= substr( crc32( $code ), - $crcLength ); } elseif ( $groupLength ){ $code = rtrim( $code, $groupSeparator ); } return $code; } public function string_to_name( $str ){ $str = strtolower( $str ); $str = preg_replace( '/\s+/', '-', $str ); $str = str_replace( ['ä','ö','ü','ß'], ['ae','oe','ue','ss'], $str ); $str = html_entity_decode( $str, ENT_QUOTES, 'utf-8' ); $str = htmlentities( $str, ENT_QUOTES, 'utf-8' ); $str = preg_replace( '/(&)([a-z])([a-z]+;)/i', '$2', $str ); $str = preg_replace( '/[^a-zA-Z0-9\-\._]/', '', $str ); return $str; } public function generate_secret( $eventId ){ $eventDir = $this->get_dir( $eventId ); $path = "{$this->dataDir}/{$eventDir}/secret"; if ( ! file_exists( dirname( $path ) ) ){ mkdir( dirname( $path ), 0700, TRUE ); } file_put_contents( $path, random_bytes( 64 ) ); chmod( $path, 0600 ); return TRUE; } public function get_secret( $eventId ){ $eventDir = $this->get_dir( $eventId ); $path = "{$this->dataDir}/{$eventDir}/secret"; if ( ! file_exists( $path ) ){ return FALSE; } return file_get_contents( $path ); } } // end of file models/lc_service.class.php