<?php
 abstract class vB_HumanVerify { use vB_Trait_NoSerialize; private static $instance; protected bool $usestoken = false; protected string $error = ''; protected string $hash = ''; public static function fetch_library() { if (!isset(self::$instance)) { self::setLibrary(vB::getDatastore()->getOption('hv_type') ?: 'Disabled'); } return self::$instance; } public static function disableHV() { self::setLibrary('Disabled'); } private static function setLibrary($library) { self::$instance = self::createHvClass($library); } private static function createHvClass(string $type) : self { $selectclass = 'vB_HumanVerify_' . $type; return new $selectclass(); } protected function delete_token($hash, $answer = null, $viewed = null) { if (!$this->usestoken) { return true; } $data = [ 'hash' => $hash, ]; if ($answer !== null) { $data['answer'] = $answer; } if ($viewed !== null) { $data['viewed'] = intval($viewed); } if ($this->hash == $hash) { $this->hash = ''; } vB::getDbAssertor()->assertQuery('humanverify', [ vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_DELETE, vB_dB_Query::CONDITIONS_KEY => $data, ]); return vB::getDbAssertor()->affected_rows() ? true : false; } public function generate_token($deletehash = true) { if (!$this->usestoken) { return [ 'hash' => '', 'answer' => '', ]; } $verify = [ 'hash' => md5(uniqid(vbrand(), true)), 'answer' => $this->fetch_answer(), ]; if ($deletehash AND $this->hash) { $this->delete_token($this->hash); } $this->hash = $verify['hash']; vB::getDbAssertor()->assertQuery('humanverify', [ vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_INSERT, 'hash' => $verify['hash'], 'answer' => $verify['answer'], 'dateline' => vB::getRequest()->getTimeNow() ]); return $verify; } abstract public function verify_token($input, $action); public function fetch_error() { return $this->error; } protected function fetch_answer() { return ''; } } 