* @license PHP License * @package WB * @subpackage content */ /** * load base class */ WBClass::load('WBFormProcessor'); /** * Content component * * @version 1.0.0 * @package WB * @subpackage content */ class WBContent extends WBFormProcessor { /** * my part * @var string */ protected $part = 'main'; /** * my parameter list * @var array */ protected $config = array('tmplDir' => ''); /** * variables meant to bubble up * @var array */ protected $bubbles = array(); /** * tell whether to use dynanmic bubbles * @var bool */ protected $useBubbles = true; /** * user object * @var WBUser_Auth */ protected $user; /** * whether to use template engine or not * @var bool */ protected $withFormTmpl = true; /** * template engine * @var patTemplate */ protected $tmpl; /** * session object * @var patSession_Storage */ protected $sess; /** * request object * @var WBRequest */ protected $req; /** * This content component is cachable * @var bool */ protected $cachable = true; /** * anonymous user */ const GROUP_ANON = '__anonymous'; /** * constructor * * Configure template folder by class name * * Instantiate a vew standard objects * - req: request object * - tmpl: template engine patTemplate * - sess: session storage patSession_Storage * - user: current user * * Make sure all subclasses execute this constructor */ public function __construct() { $this->config['tmplDir'] = str_replace('_', '/', substr(get_class($this), strlen(__CLASS__) + 1)); $this->req = WBClass::create('WBRequest'); $this->sess = WBClass::create('patSession'); $this->tmpl = WBClass::create('patTemplate'); WBClass::load('WBUser'); $this->user = WBUser::getCurrent(); } /** * location of form config * * Return sub directory where form element definitions are located * * @return string folder */ protected function getFormConfigDir() { return strtolower(str_replace('_', '/', substr(get_class($this), strlen(__CLASS__) + 1))); } /** * Set config values * * Setup and configure this content component. * * @param string $part named part where the content is located * @param array configuration options * @param array bubbles * @return bool true on success */ public function configure($part, $config, $bubbles) { if (!is_array($config)) { $config = array(); } if (!is_array($bubbles)) { if (1 > intval($bubbles)) { $this->useBubbles = false; } $bubbles = array(); } $this->part = $part; $this->config = array_merge($this->config, $config); $this->bubbles = $bubbles; // force leading slash if (strlen($part) && $part[0] != '/') { $part = '/' . $part; } // add user data and group membership if ($this->user && $this->user->isAuthenticated()) { $this->tmpl->addGlobalVars($this->user->getData(), 'user_current_'); $groups = array(); foreach ($this->user->getGroups() as $gid => $gname) { $groups[$gname] = '1'; } $this->tmpl->addGlobalVars($groups, 'user_current_group_'); } // misc global variables $globals = array( 'request_path' => $this->req->path, 'content_path' => $part, 'session_name' => $this->sess->getName(), 'session_id' => $this->sess->getId(), ); $this->tmpl->addGlobalVars($globals); $this->init(); return true; } /** * 2nd constructor * * Called after configuration was done */ protected function init() { } /** * run * * run component * * @return array parameter list */ public function run() { return $this->config; } /** * Receive output * * Fetch output of this content component. Implement standard behaviour: * get parsed template named "snippet". * * @return string */ public function getString() { return $this->tmpl->getParsedTemplate('snippet'); } /** * Download file * * CAUTION: This method usually does not return! * If file exists, it will send file to browser and exit programme! * * @param string $file * @param string $type mime-type * @param string $disposition file name for content disposition */ protected function downloadFile($file, $type = 'text/xml', $disposition = '') { $res = WBClass::create('WBResponse'); /** @var $res WBResponse */ if (!file_exists($file)) { $res->addHeader('Content-Type', 'text/plain'); $res->add('not found'); $res->send($this->req); exit(0); } // switch off compression - otherwise complete download files are loaded to RAM WBParam::set('wb/response/compress', 0); WBParam::set('wb/cache/use', 0); $res->addHeader('Content-Type', $type); $res->addHeader('Cache-Control', 'no-cache, must-revalidate'); $res->addHeader('Expires', gmdate('D, d M Y H:i:s') . ' GMT'); if (!empty($disposition)) { $res->addHeader('Content-Disposition', 'inline; filename="' . $disposition . '"'); } $res->addStream(fopen($file, 'r'), md5_file($file)); $res->send($this->req); exit(0); } /** * Get response status code * * @return int */ public function getStatusCode() { return 200; } /** * fetch variables to bubble up * * @return array parameter list */ public function getBubbles() { if ($this->useBubbles) { return $this->bubbles; } return array(); } /** * tell whether this component is cachable * * @return bool */ public function isCachable() { return $this->cachable; } /** * load templates from file * * @param string $tmpl * @param bool local templates */ protected function loadTemplates($tmpl, $local = true) { if ($local) { $tmpl = $this->config['tmplDir'] . '/' . $tmpl; } return $this->tmpl->readTemplatesFromInput($tmpl . '.tmpl'); } /** * Add Config to Template * * Most content components make the current config available as template variables * * @see addGlobalVars() * @return bool */ protected function addConfigAsGlobalVars() { return $this->addGlobalVars($this->config, 'CONFIG_'); } /** * Add Scala Values as Global Vars * * Auxilliary method to add template variables without notices. * * @param array $vars * @param string prefix - optional * @return bool */ protected function addGlobalVars($vars, $prefix = '') { $tmp = array(); foreach ($vars as $k => $v) { if (is_scalar($v)) { $tmp[$k] = $v; continue; } if (is_array($v)) { $tmp[$k . '_count'] = count($v); } } return $this->tmpl->addGlobalVars($tmp, $prefix); } /** * Verify if user is in group * * Checks if user is in specified group * * @param string $name group name to validate * @param bool $id whether $name is the group's id * @return bool true in case user is member of given group */ protected function isUserInGroup($name, $id = false) { if (self::GROUP_ANON == $name) { return true; } return $this->user->isInGroup($name, $id); } /** * Login user if not already logged in * * Check for authentication and try to login. * CAUTION: this uses form processor, therefore form defintion and templates are required * * @return bool true for authenticated users */ protected function autoAuthenticate() { if ($this->user->isAuthenticated()) { return true; } $this->processForm('login'); return $this->user->isAuthenticated(); } /** * login user * * Use form data to login */ public function onLoginValid($form, $values) { $this->user->login($values); if ($this->user->isAuthenticated()) { WBClass::load('WBService'); WBService::addEvent('login'); return false; } return true; } }