<?php if (!defined('VB_ENTRY')) die('Access denied.'); class vB_Api_External extends vB_Api { const TYPE_JS = 'js'; const TYPE_XML = 'xml'; const TYPE_RSS = 'rss'; const TYPE_RSS1 = 'rss1'; const TYPE_RSS2 = 'rss2'; private $instances = []; protected $cleaner; protected $library; private $instancemap = [ 'js' => 'vB_External_Export_Js', 'xml' => 'vB_External_Export_Xml', 'rss' => 'vB_External_Export_Rss_91', 'rss1' => 'vB_External_Export_Rss_1', 'rss2' => 'vB_External_Export_Rss_2' ]; protected function __construct() { parent::__construct(); $this->cleaner = vB::getCleaner(); $this->library = vB_Library::instance('external'); } public function createExternalOutput($type, $options = array()) { $result = $this->library->validateExternalType($type); if ($result['valid'] === false) { throw new vB_Exception_Api($result['phraseid']); } $external = $this->loadInstance($type); if (isset($options['nodeid']) AND !empty($options['nodeid'])) { $options['nodeid'] = explode(',', $options['nodeid']); } $options = $this->cleaner->cleanArray($options, $external->getOptions()); $options = $this->validateOptions($options); return $external->output($options); } public function buildExternalRoute($type, $data = array()) { $type = trim(strtolower($type)); $result = $this->library->validateExternalType($type); if ($result['valid'] === false) { return array('active' => false, 'route' => ''); } if (isset($data['nodeid']) AND !is_array($data['nodeid'])) { $data['nodeid'] = array($data['nodeid']); } $external = $this->loadInstance($type); $cleaned = $this->cleaner->cleanArray($data, $external->getOptions()); $cleaned['type'] = $type; $route = $this->library->getExternalRoute($cleaned, $data); return array('active' => true, 'route' => $route); } public function getHeadersFromLastOutput($type, $data) { $result = $this->library->validateExternalType($type); if ($result['valid'] === false) { throw new vB_Exception_Api($result['phraseid']); } $external = $this->loadInstance($type); $data = $this->cleaner->cleanArray($data, $external->getHeaders()); return $external->getHeadersFromLastOutput($data); } public function getCacheData($type, $options = []) { if (!empty($options['nodeid'])) { $options['nodeid'] = explode(',', $options['nodeid']); } return [ 'cachehash' => $this->getCacheHash($type, $options), 'cachetime' => ($this->getCacheTime() * 60) ]; } protected function validateOptions($options) { $result = []; foreach ($options AS $name => $val) { switch ($name) { case 'nodeid': if (!empty($val)) { $nodes = vB_Api::instanceInternal('node')->getNodes($val); foreach ($nodes AS $node) { if ($node['contenttypeid'] == vB_Types::instance()->getContentTypeID('vBForum_Channel')) { $result['nodeid'][$node['nodeid']] = $node['nodeid']; } } if (empty($result['nodeid'])) { throw new vB_Exception_Api('invalid_data_requested'); } } break; case 'count': $result['count'] = $this->getCount($options['count']); break; default: $result[$name] = (bool)$val; break; } } $result['ttl'] = $this->getCacheTime(); $result['externalcutoff'] = $this->getCutoff(); return $result; } private function loadInstance($type) { if (!isset($this->instances[$type])) { $this->instances[$type] = new $this->instancemap[$type](); } return $this->instances[$type]; } private function getCacheHash($type, $options) { $result = $this->library->validateExternalType($type); if ($result['valid'] === false) { throw new vB_Exception_Api($result['phraseid']); } if (trim(strtolower($type)) == 'js') { return ''; } $external = $this->loadInstance($type); $data = $external->getOptions() + $external->getInternalOptions(); $data = $this->cleaner->cleanArray($options, $data); $count = (!empty($options['count']) ? $options['count'] : 0); $data['count'] = $this->getCount($count); if (empty($options['externalcutoff'])) { $data['externalcutoff'] = $this->getCutoff(); } if (empty($options['ttl'])) { $data['ttl'] = $this->getCacheTime(); } return $external->getCacheHash($data); } private function getCacheTime() { $ttl = vB::getDatastore()->getOption('externalcache'); return ((($ttl > 0) AND ($ttl <= 1440)) ? $ttl : 60); } private function getCutoff() { $cutoff = intval(vB::getDatastore()->getOption('externalcutoff')); return (!$cutoff ? 0 : (vB::getRequest()->getTimeNow() - ($cutoff * 86400))); } private function getCount($count) { $count = intval($count); $externalcount = (vB::getDatastore()->getOption('externalcount') ? vB::getDatastore()->getOption('externalcount') : 15); if (!$count OR ($count > $externalcount)) { $result = $externalcount; } else { $result = $count; } return $result; } } 