myIpDir = $myIp->getDir(); $this->myipId = $myIp->ipDir; $this->myIpFile = $myIp->getFile(); parent::__construct( $myData ); if ( is_file( $this->getFile() ) ) { $myBrowser = unserialize( file_get_contents( $this->getFile() ) ); $this->dirId=$myBrowser->dirId; $this->userAgentData=$myBrowser->userAgentData; $this->pageReaded=$myBrowser->pageReaded; $this->tentative=$myBrowser->tentative; $this->sessionsToken=$myBrowser->sessionsToken; } else { while ( is_dir( $this->getDir() ) ) $this->dirId++; mkdir ( $this->getDir() ); require_once "{$darkauthDir}/ext/phpUserAgent.php"; $parser = new \donatj\UserAgent\UserAgentParser(); // object-oriented call $ua = $parser->parse( $this->data[ "user-agent" ] ); // or command style invocation //$ua = $parser(); $this->userAgentData = array( 'platform' => $ua->platform() ,'browser' => $ua->browser() ,'browserVersion' => $ua->browserVersion() ); $this->tentative=0; $this->sessionsToken=array(); file_put_contents( $this->getFile() ,serialize( $this ) ); } } public function getIp() : Ip { return unserialize( file_get_contents( $this->myIpFile ) ); } public function writeXml( $xml ,$child/*, $myipDir = null */) { $xmlMyBrowser = $xml->createElement( "browser" ); $xmlMyBrowser->setAttribute("id", $this->id ); $xmlMyBrowser->appendChild( $xml->createElement( "id", $this->id ) ); $xmlMyBrowser->appendChild( $xml->createElement( "myipId", substr( $this->myIpDir ,strpos( $this->myIpDir ,"-" )+1 ) ) ); $xmlMyBrowser->appendChild( $xml->createElement( "myDirId", $this->dirId ) ); foreach( $this->data as $key => $data ) { $xmlMyBrowser->appendChild( $xml->createElement( $key, $data ) ); } $child->appendChild( $xmlMyBrowser ); $xmlUserAgent=$xml->createElement( "user-agent-data" ); $xmlMyBrowser->appendChild( $xmlUserAgent ); foreach( $this->userAgentData as $key => $value ) $xmlUserAgent->appendChild( $xml->createElement( $key, $value ) ); return $xmlMyBrowser; } // /darkauth\db\auth\Browser::listToXml( $xml ,$child ) public static function listToXml( $xml ,$child ) { $xmlBrowsers = $xml->createElement( "browsers" ); $child->appendChild( $xmlBrowsers ); foreach( scandir( \darkauth\db\auth\Ip::$ipsDir ) as $anValue ) if ( $anValue=="." || $anValue==".." ) continue; elseif ( is_dir( ( $anIpDir = \darkauth\db\auth\Ip::$ipsDir."/$anValue" ) ) ) foreach( scandir( $anIpDir ) as $anOtherValue ) if ( $anOtherValue=="." || $anOtherValue==".." ) continue; elseif ( is_file( ( $serializedFile = "{$anIpDir}/{$anOtherValue}" ) ) ) ( unserialize( file_get_contents( $serializedFile ) ) )->writeXml( $xml, $xmlBrowsers /*,$anIpDir */ ); return $xmlBrowsers; } public function getDir() { return $this->myIpDir."/browser-".$this->dirId; } public function getFile() { return $this->myIpDir."/".$this->id.".dbphp"; } private const MAX_KEYS_BY_POST = 10; // private function getSessionFile( array $post ) : ?string { $found = null; if ( self::MAX_KEYS_BY_POST > \count( $post ) ) { foreach( $post as $key => $value ) { if ( isset( $this->sessionsToken[ $key ] ) ) { $found = $this->sessionsToken[ $key ]; unset( $this->sessionsToken[ $key ] ); break; } } } return $found; } public function setSession( \darkauth\db\session\DwBrowserSession $dwBrowserSession ) { $this->sessionsToken[ $dwBrowserSession->id ] = $dwBrowserSession->getFile(); file_put_contents( $this->getFile() ,serialize( $this ) ); } public function getSession( array $post ) : ?\darkauth\db\session\DwBrowserSession { $found = null; $filePathSession = $this->getSessionFile( $post ); if ( is_file( $filePathSession ) ) { file_put_contents( $this->getFile() ,serialize( $this ) ); $found = unserialize( file_get_contents( $filePathSession ) ); $found->regnerateId(); } return $found; } // \darkauth\db\auth\Browser::fromApache2( $_SERVER ); public static function fromApache2( Ip $myIp ,$myServer ) : ?self { return new Browser( $myIp , array ( "user-agent" => $myServer['HTTP_USER_AGENT'] ,"accept-language" => $myServer['HTTP_ACCEPT_LANGUAGE'] ,"accept-encoding" => $myServer['HTTP_ACCEPT_ENCODING'] ) ); } /* // \darkauth\db\auth\Browser::fromApache2getSession( $_SERVER ,$referenceReaded ); public static function fromApache2getSession( $myServer ,array $post ) : ?string { $found=null; $myIpFile=\darkauth\db\auth\Ip::getIpFilePath( \darkauth\db\auth\Ip::getIpFile( $myServer['REMOTE_ADDR'] ) ); if ( is_file( $myIpFile ) ) $myIp = unserialize( file_get_contents( $myIpFile ) ); //else // $myIp =\darkauth\db\auth\Ip::fromApache2( $myServer ); $browser = \darkauth\db\auth\Browser::fromApache2( $myIp ,$myServer ); $found = $browser->getSessionFile( $post ); file_put_contents( $browser->getFile() ,serialize( $browser ) ); return $found; } //*/ // \darkauth\db\auth\Browser::fromApache2readed( $_SERVER ,$referenceReaded ); public static function fromApache2readed( $myServer ,string $referenceReaded ) : void { $myIpFile=\darkauth\db\auth\Ip::getIpFilePath( \darkauth\db\auth\Ip::getIpFile( $myServer['REMOTE_ADDR'] ) ); if ( is_file( $myIpFile ) ) $myIp = unserialize( file_get_contents( $myIpFile ) ); else $myIp =\darkauth\db\auth\Ip::fromApache2( $myServer ); $browser = \darkauth\db\auth\Browser::fromApache2( $myIp ,$myServer ); if ( ! isset( $browser->pageReaded[ $referenceReaded ] ) ) $browser->pageReaded[ $referenceReaded ] = array(); array_push( $browser->pageReaded[ $referenceReaded ] ,date("Y-m-d H:i:s") ); file_put_contents( $browser->getFile() ,serialize( $browser ) ); } // \darkauth\db\auth\Browser::fromApache2readedCpt( $_SERVER ,$referenceReaded ); public static function fromApache2readedCpt( $myServer ,string $referenceReaded ) : int { $myIpFile=\darkauth\db\auth\Ip::getIpFilePath( \darkauth\db\auth\Ip::getIpFile( $myServer['REMOTE_ADDR'] ) ); if ( is_file( $myIpFile ) ) $myIp = unserialize( file_get_contents( $myIpFile ) ); else $myIp =\darkauth\db\auth\Ip::fromApache2( $myServer ); $browser = \darkauth\db\auth\Browser::fromApache2( $myIp ,$myServer ); if ( ! isset( $browser->pageReaded[ $referenceReaded ] ) ) $browser->pageReaded[ $referenceReaded ] = array(); return count( $browser->pageReaded[ $referenceReaded ] ); } } ?>