'bodyLoadedInt()'); $docArr['initInlineScripts'] = array(); } function Doc($docArr, $options = array()) { $this->_template_dir = Constant::get('BASE_PATH') . '/tpl'; $this->_preDocInit($docArr); $this->assign('DOC', $docArr); $this->setMainTpl($docArr['tpl']); $this->setOptions($options); if (is_array($this->tpl_defines) && !empty($this->tpl_defines)) { foreach ($this->tpl_defines as $k=>$v) { $this->assign($v, Constant::get($v)); } } $this->assign('BASE_PATH', dirname(__FILE__).'/../..' ); } public function setOptions($options) { if (isset($options['options'])) { unset($options['options']); } foreach ($options as $key => $value) { $this->setOption($key, $value); } return $this; } public function getOptions() { return $this->_options; } public function setOption($name, $value) { $method = 'set' . ucfirst($name); if (method_exists($this, $method)) { $this->$method($value); } else { $this->_options[$name] = $value; } return $this; } function setTemplateDir($dir) { $dir = strval($dir); $this->_template_dir = $dir; if (is_object($this->Smarty)) { $this->Smarty->template_dir = $this->_template_dir; } return true; } /** * Function set main template * * @param string $tpl path to template file * @access public */ function setMainTpl($tpl) { $this->main_tpl = $tpl; } /** * Function set template's title * * @param string $title title of document * @access private */ function setTitle($title) { $this->content['DOC']['title'] = $title; } function getTitle() { return $this->content['DOC']['title']; } function setDescription($description) { $this->content['DOC']['description'] = $description; } /** * Function assign values to template * * @param string $var key of page parameter * @param bool | array $value value of page parameter * @access public */ function assign($var, $value = false) { $this->content[$var] = $value; } /** * Function * * @param callback $filter address of the filter function/method to apply to the output * @access public */ function registerOutputFilter($filter) { $this->unregisterOutputFilter($filter); $this->_out_filters[] = $filter; } /** * Function * * @param callback $filter address of the filter function/method to look and remove from the list of filters * @access public */ function unregisterOutputFilter($filter) { $key = array_search($filter, $this->_out_filters); if($key !== FALSE) unset($this->_out_filters[$key]); return $key; } /** * Function display a template * * @access public */ function fetch($template = null) { global $_DEBUG; $this->smartyInit(); $revFileName = WWW_PATH.'/rev'; if (file_exists($revFileName) && is_file($revFileName) && is_readable($revFileName)) { $rev = (int)file_get_contents($revFileName); $this->Smarty->assign('CURRENT_REVISION', $rev); } $this->Smarty->assign($this->content); $this->Smarty->assign('initFunctionsHtml', $this->renderInitFunctions()); if (null === $template) { $template = $this->main_tpl; } $html = $this->Smarty->fetch($template); require_once('class/Const.php'); if (Constant::get('DEBUG')) { $this->Smarty->assign('debugInfo', $_DEBUG['msgs']); $debugHtml = $this->Smarty->fetch('debug.tpl'); $html = str_replace('', $debugHtml, $html); } foreach($this->_out_filters as $filter){ $html = call_user_func_array($filter, array($html, &$this)); } return $html; } /** * Function display a template * * @access public */ function display() { header('Content-Type: '.constant('CONTENT_TYPE')); echo $this->fetch(); return true; } /** * Function add content to template * * @param array $item array of parameters to add to template * @access public */ function addContent($item, $el_name = '') { if (!$item['tpl']) foreach ($item as $one_item){ $this->addItem($one_item); } else { $this->addItem($item, $el_name); } } /** * Function adding item to ITEM array * * @access public */ function addItem($item, $el_name = '', $item_list_name = 'ITEMS') { $names = array('JSs','VBs', 'CSSs', 'initFuncs'); foreach ($names as $name) { if (isset($item[$name]) && is_array($item[$name])){ foreach ($item[$name] as $file ) { $this->addItemProp($name, $file); } } unset($item[$name]); } if ($el_name != '') { $this->assign($el_name, $item); } else { $this->content[$item_list_name][] = $item; } } function addItemProp($name, $val) { if (!in_array($val, (array)$this->content['DOC'][$name] )){ $this->content['DOC'][$name][] = $val; } return true; } /** * Function return a Smarty instance * * @access public */ function & getSmarty() { if (!class_exists('Smarty') ){ require_once('lib/Smarty/Smarty/Smarty.class.php'); } $Smarty = new Smarty(); $Smarty->compile_check = true; $Smarty->debugging = SMARTY_DEBUG; //$Smarty->template_dir = Constant::get('BASE_PATH') .'/tpl'; $Smarty->template_dir = $this->_template_dir; $Smarty->cache_lifetime = 40; $Smarty->force_compile = false; $Smarty->php_handling = SMARTY_PHP_PASSTHRU; $Smarty->compile_dir = Constant::get('BASE_PATH').'/tmp/tpl_c'; $Smarty->left_delimiter = '{'; $Smarty->right_delimiter = '}'; $Smarty->file_perms = '0777'; $Smarty->dir_perms = '0777'; foreach ($this->tplPlugins as $dir){ $Smarty->plugins_dir[] = Constant::get('BASE_PATH') .'/'.$dir; } return $Smarty; } /** * Function initialize a Smarty * * @access private */ function smartyInit() { if (!is_object($this->Smarty)) { $this->Smarty = Doc::getSmarty(); } return true; } function _getClassName($type) { $className = str_replace('/', '_', $type).'View'; return $className; } function getClassPath($type) { $classFileName = Doc::_getClassFileName($type); $path = $type.'/'.$classFileName.'.php'; return $path; } function _getClassFileName($type) { preg_match('/([^\/]*)$/', $type, $match); $classFileName = $match[0].'View'; return $classFileName; } function showDocItemList($docItemList) { $this->checkAccess($docItemList); foreach ($docItemList as $docItemArr) { $docItem = SiteMap::getObj(Doc:: getClassPath($docItemArr['type']), $docItemArr); if (!is_null($docItem)){ $docItem->exec(&$this); } } return true; } /* function getDocItemByType($type, $opt = array() ) { $className = Doc::_getClassName($type); $classPath = Doc:: getClassPath($type); if (file_exists(BASE_PATH.'/'.$classPath)){ require_once($classPath); $docItem = new $className($opt); return $docItem; }else { return null; } } */ function enableHTTPS($convert_href = true) { require_once('class/Filter/LinksFilter.php'); $filter = new LinksFilter(array( 'FILTER_HTTPS_FROM' => BASE_URL, 'FILTER_HTTPS_TO' => str_replace('http://', 'https://', BASE_URL), 'FILTER_HTTPS_FORM' => true, 'FILTER_HTTPS_LINK' => $convert_href, )); $this->registerOutputFilter(array(&$filter, 'filter')); } function getACL() { if (null === $this->_acl && $this->aclFileName !== null && $this->aclObjName !== null){ $aclFileName = WWW_PATH.'/_sys/'.$this->aclFileName; if (file_exists($aclFileName) && is_file($aclFileName)) { $this->_acl = include($aclFileName); } } return $this->_acl; } function getDeniedPages($_items) { if (null === ($acl = $this->getACL())) { return false; } $userData = $this->{$this->aclObjName}->getData(); if (!is_array($userData) || empty($userData)) { return false; } $deniedPages = array(); foreach ($this->_acl['items'] as $aclItem) { if (false === ($success = $this->checkAcces2Item($aclItem, $userData))) { unset($aclItem['rules']); while (false !== ($id_doc = $this->getPageIdByItem($aclItem, $_items))) { $deniedPages[] = $id_doc; } } } return $deniedPages; } function getPageIdByItem($aclItem, &$_items) { foreach ($_items as $k => $item) { if ($item['type'] == $aclItem['type'] && (isset($aclItem['name']) && $item['name'] == $aclItem['name'] || !isset($aclItem['name'])) ) { $id_doc = $item['id_doc']; unset($_items[$k]); return $id_doc; } } return false; } function checkAccess(&$docItems) { if (null === ($acl = $this->getACL())) { return false; } if (false === ($aclItem = $this->getACLItem($docItems, $acl['items']))) { return false; } if (!is_array($aclItem['rules']) || empty($aclItem['rules'])) { return false; } $userData = $this->{$this->aclObjName}->getData(); if (!is_array($userData) || empty($userData)) { $docItems = array(); $this->showMessageAccessDeny($aclItem); return false; } $success = $this->checkAcces2Item($aclItem, $userData); if (!$success) { $docItems = array(); $this->showMessageAccessDeny($aclItem); } return $success; } function checkAcces2DocItem($type, $name = null) { $docItem = array('type'=>$type); if (null !== $name) { $docItem['name'] = $name; } if (null === ($acl = $this->getACL())) { return true;; } if (false === ($aclItem = $this->getACLItem(array($docItem), $acl['items']))) { return true; } $userData = $this->{$this->aclObjName}->getData(); if (!is_array($userData) || empty($userData)) { return false; } return $this->checkAcces2Item($aclItem, $userData); } function checkAcces2Item($aclItem, $userData) { $success = false; foreach ($aclItem['rules'] as $rule) { if (true === ($success = $this->checkRule($rule, $userData))) { break; } } return $success; } function checkRule($rule, $userData) { foreach ($rule as $part) { switch($part['comparation']) { case 'in_array': if (!in_array($userData[$part['field']], $part['value'])) { return false; } break; case '=': default: if ($part['value'] != $userData[$part['field']]) { return false; } break; } } return true; } function showMessageAccessDeny($aclItem = array()) { $item = array( 'tpl' => 'acl_error_message.tpl', 'text' => (isset($aclItem['errorMessage']))?$aclItem['errorMessage']:$this->_acl['defaultErrorMessage'], ); $this->addContent($item); } function getACLItem($docItems, $aclItems) { if (!is_array($docItems) || empty($docItems) || !is_array($aclItems) || empty($aclItems)) { return false; } foreach ($docItems as $docItem) { foreach ($aclItems as $aclItem) { if ( $docItem['type'] == $aclItem['type'] && (isset($aclItem['name']) && $docItem['name'] == $aclItem['name'] || !isset($aclItem['name'])) ) { return $aclItem; } } } return false; } function displayError($text) { $this->addContent(array('tpl' => 'msg_error.tpl', 'text' => $text)); } function displayMessage($text) { $this->addContent(array('tpl' => 'msg.tpl', 'text' => $text)); } public function setBodyAttribs($attribs) { $this->content['DOC']['bodyAttribs'] = $attribs; } public function addInitInlineScript($name, $body) { $this->content['DOC']['initInlineScripts'][$name] = $body; } public function setBodyAttrib($name, $value) { $this->content['DOC']['bodyAttribs'][$name] = $value; } public function getBodyAttribs() { return $this->content['DOC']['bodyAttribs']; } public function getMenu() { return $this->content['menu']; } public function getBodyAttrib($name) { if (array_key_exists($name, $this->_content['bodyAttribs'])) { return $this->_content['bodyAttribs'][$name]; } return null; } function addInitObject($name, $params = array(), $assign = null) { $this->content['DOC']['initFunctions'][] = array( 'name' => $name, 'params' => $params, 'assign' => $assign, 'new' => true ); } function addInitFunction($name, $params = array()) { $this->content['DOC']['initFunctions'][] = array('name' => $name, 'params' => $params); } function renderInitFunctions() { if (!is_array($this->content['DOC']['initFunctions']) || empty($this->content['DOC']['initFunctions'])) { return ''; } $html = ''; foreach ($this->content['DOC']['initFunctions'] as $options) { $row = $options['name']; if (!empty($options['params'])) { $params = array(); foreach ($options['params'] as $param) { $params[]= Zend_Json::encode($param, false, array('enableJsonExprFinder' => true)); } $row .= '(' . implode(', ', $params) . ');'; } else { $row .= '();'; } if (isset($options['new']) && $options['new']) { $row = 'new ' . $row; } if (!empty($options['assign'])) { $row = $options['assign'] . ' = ' . $row; } $html .= $row . "\n"; } return $html; } function display404($options = array()) { $view = SiteMap::getObj('NotFound/NotFoundView.php', $options); $view->exec($this); } } function getInitFuncExParams($params = array()) { if (!empty($params)) { $ret = ''; foreach ($params as $param) { if ($param{0} == '{') { $ret .= $param; } else { $ret .= (is_numeric($param))?$param:"'".addslashes($param)."'"; } $ret .= ', '; } $ret = rtrim($ret, ', '); return $ret; } return NULL; }