<?php
 class vB_Api_Hv extends vB_Api { protected $disableWhiteList = ['fetchRequireHvcheck']; protected $hvRequired = []; protected $assertor; protected function __construct() { parent::__construct(); $this->assertor = vB::getDbAssertor(); } public function fetchHvType() { $vboptions = vB::getDatastore()->getValue('options'); return ($vboptions['hv_type'] ? $vboptions['hv_type'] : 'Disabled'); } public function generateToken() { $verify = vB_HumanVerify::fetch_library(); $token = $verify->generate_token(); if (!defined('VB_UNITTEST')) { unset($token['answer']); } return $token; } public function verifyToken($input, $action, $return = false) { if (!$this->fetchRequireHvcheck($action)) { return true; } $verify = vB_HumanVerify::fetch_library(); $data = $verify->verify_token($input, $action); if ($data) { return true; } else { if ($return) { return false; } else { throw new vB_Exception_Api($verify->fetch_error()); } } } public function fetchHvImage($hash = '') { $vboptions = vB::getDatastore()->getValue('options'); $moveabout = true; if (!$hash OR $hash == 'test' OR $vboptions['hv_type'] != 'Image') { $imageinfo = [ 'answer' => 'vBulletin', ]; $moveabout = $hash == 'test' ? true : false; } else if ( !( $imageinfo = $this->assertor->getRow('humanverify', [ 'hash' => $hash, 'viewed' => 0, ]) ) ) { return [ 'type' => 'gif', 'data' => file_get_contents(DIR . '/clear.gif') ]; } else { $this->assertor->assertQuery('humanverify', [ vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_UPDATE, 'viewed' => 1, vB_dB_Query::CONDITIONS_KEY => [ 'hash' => $hash, 'viewed' => 0, ] ]); if ($this->assertor->affected_rows() == 0) { return [ 'type' => 'gif', 'data' => file_get_contents(DIR . '/clear.gif') ]; } } $image = vB_Image::instance('hv'); $imageInfo = $image->getImageFromString($imageinfo['answer'], $moveabout); return [ 'type' => $imageInfo['filetype'], 'data' => $imageInfo['filedata'] ]; } public function fetchHvQuestion($hash = '') { if (!$hash) { throw new vB_Exception_Api('invalid_hash'); } $hv = $this->assertor->getRow('humanverify', ['hash' => $hash]); $questionid = $hv['answer']; if (!$questionid) { return false; } $phrases = vB_Api::instanceInternal('phrase')->fetch(['question' . $questionid]); return $phrases['question' . $questionid]; } public function getRecaptchaLanguage() { $data = []; $userinfo = vB_Api::instanceInternal('user')->fetchCurrentUserinfo(); if (preg_match('#^([a-z]{2})-?#i', $userinfo['lang_code'], $matches)) { $data['langcode'] = strtolower($matches[1]); } return $data; } public function fetchRequireHvcheck($action) { if (!empty($this->hvRequired[$action])) { return $this->hvRequired[$action]; } $datastore = vB::getDatastore(); $options = $datastore->getValue('options'); $bf_misc_hvcheck = $datastore->getValue('bf_misc_hvcheck'); assert(isset($bf_misc_hvcheck[$action]), "Missing hv check for $action"); if (!$options['hv_type'] OR !($options['hvcheck'] & $bf_misc_hvcheck[$action])) { return false; } $usercontext = vB::getUserContext(); switch ($action) { case 'register': { $guestuser = [ 'userid' => 0, 'usergroupid' => 0, ]; cache_permissions($guestuser); } case 'lostpw': { if ($usercontext->isAdministrator()) { $results[$action] = false; return $results[$action]; } break; } } $this->hvRequired[$action] = $usercontext->hasPermission('genericoptions', 'requirehvcheck'); return $this->hvRequired[$action]; } } 