Edit via SFTP
  1. <?php namespace ferret\users;
  2.  
  3. class ctClients extends cLogicTable {
  4.  
  5. // ++ SETUP ++ //
  6.  
  7. // CEMENT
  8. protected function GetTableName() : string { return 'user_client'; }
  9. // CEMENT
  10. protected function SingleRowClass() : string { return crcClient::class; }
  11.  
  12. // -- SETUP -- //
  13. // ++ ENVIRONMENT ++ //
  14.  
  15. static protected function InputCRC() : string {
  16. $sAddr = self::InputAddress();
  17. $sAgent = self::InputBrowser();
  18. $nCRC = crc32($sAddr.' '.$sAgent);
  19. $sCRC = sprintf('%u',$nCRC); // make sure is unsigned
  20. return $sCRC;
  21. }
  22. static protected function InputAddress() { return \fcHTTP::ClientAddress_string(); }
  23. static protected function InputBrowser() { return \fcHTTP::ClientBrowser_string(); }
  24. static protected function InputDomain() { return gethostbyaddr(self::InputAddress()); }
  25.  
  26. // -- ENVIRONMENT -- //
  27. // ++ RECORDS ++ //
  28.  
  29. public function MakeRecord_forCRC() : \ferret\data\cRecordResult {
  30. $sCRC = self::InputCRC();
  31. $osrs = $this->SelectRecords("CRC='$sCRC'");
  32. $rs = $osrs->Rowset()->GetIt();
  33. $rs->RequireSpace();
  34. if ($rs->HasRows()) {
  35. #$rs = $osrs->GetRows();
  36. #$os = $rs->Space();// DEBUG TEST
  37. // the current browser already has a session record
  38. $osrc = $rs->NextRow(); // get first row (should be the only one)
  39. } else {
  40. // need to create a new session record
  41. $sAddr = self::InputAddress();
  42. $sDom = self::InputDomain();
  43.  
  44. $db = $this->Space()->Database()->GetIt();
  45. $sqlAddress = $db->SanitizeValue($sAddr);
  46. if ($sAddr == $sDom) {
  47. $sqlDomain = 'NULL';
  48. } else {
  49. $sqlDomain = $db->SanitizeValue($sDom);
  50. }
  51. $sqlBrowser = $db->SanitizeValue(self::InputBrowser());
  52. $ar = array(
  53. 'CRC' => $db->SanitizeValue($sCRC),
  54. 'Address' => $sqlAddress,
  55. 'Domain' => $sqlDomain,
  56. 'Browser' => $sqlBrowser,
  57. 'WhenFirst' => 'NOW()'
  58. );
  59. $osNew = $this->DoInsert($ar);
  60. if (!$osNew->Action()->GetOkay()) {
  61. $e = new \ferret\except\cInternal('Could not insert new client record.');
  62. $e->AddDiagnostic('CLIENT RECORD TO ADD:'.\fcArray::Render($ar));
  63. $e->AddDiagnostic('SQL: '.$osNew->GetSQL());
  64. throw $e;
  65. }
  66. $idNew = $osNew->GetID();
  67. $osrc = $this->GetRow_fromKey($idNew);
  68. }
  69. return $osrc;
  70. }
  71.  
  72. // -- RECORDS -- //
  73.  
  74. }
  75. class crcClient extends cLogicRecord {
  76.  
  77. // ++ SETUP ++ //
  78.  
  79. public function InitNew() {
  80. $sAddr = \fcHTTP::ClientAddress_string();
  81. $sAgent = \fcHTTP::ClientBrowser_string();
  82. $nCRC = crc32($sAddr.' '.$sAgent);
  83. $sCRC = sprintf('%u',$nCRC); // make sure is unsigned
  84. $arInit = array(
  85. 'ID' => NULL,
  86. 'Address' => $sAddr,
  87. 'Browser' => $sAgent,
  88. 'Domain' => gethostbyaddr($sAddr),
  89. 'CRC' => $sCRC
  90. );
  91. $this->UnloadCurrentRow();
  92. $this->SetFieldValues($arInit);
  93. }
  94.  
  95. // -- SETUP -- //
  96. // ++ FIELD VALUES ++ //
  97.  
  98. protected function AddressString() : string { return $this->CellStatus('Address')->GetIt(); }
  99. protected function BrowserString() : string { return $this->CellStatus('Browser')->GetIt(); }
  100. protected function DomainString() : string { return $this->CellStatus('Domain')->GetIt(); }
  101. protected function CRC() : string { return $this->CellStatus('CRC'); }
  102.  
  103. // ++ FIELD VALUES ++ //
  104. // ++ FIELD CALCULATIONS ++ //
  105.  
  106. public function IsValidNow() : bool {
  107. return (
  108. ($this->AddressString() == $_SERVER["REMOTE_ADDR"]) &&
  109. ($this->BrowserString() == $_SERVER["HTTP_USER_AGENT"])
  110. );
  111. }
  112.  
  113. // -- FIELD CALCULATIONS -- //
  114. // ++ ACTIONS ++ //
  115.  
  116. public function Stamp() { return $this->DoUpdate(array('WhenFinal'=>'NOW()')); }
  117.  
  118. // -- ACTIONS -- //
  119.  
  120. }
  121.