[795] | 1 | <?php |
---|
| 2 | /**************************************************************************\ |
---|
| 3 | * eGroupWare * |
---|
| 4 | * http://www.egroupware.org * |
---|
| 5 | * -------------------------------------------- * |
---|
| 6 | * This program is free software; you can redistribute it and/or modify it * |
---|
| 7 | * under the terms of the GNU General Public License as published by the * |
---|
| 8 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
| 9 | * option) any later version. * |
---|
| 10 | \**************************************************************************/ |
---|
| 11 | |
---|
| 12 | require_once 'common.inc.php'; |
---|
| 13 | |
---|
| 14 | /** |
---|
| 15 | * Classe que redireciona requisições de arquivos de acordo com o template utilizado |
---|
| 16 | * @author Sidnei Augusto Drovetto Jr. - drovetto@gmail.com |
---|
| 17 | * @version 1.0 |
---|
| 18 | * @package Workflow |
---|
| 19 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 20 | */ |
---|
| 21 | class TemplateServer |
---|
| 22 | { |
---|
| 23 | /** |
---|
| 24 | * @var string $file O nome do arquivo requisitado |
---|
| 25 | * @access private |
---|
| 26 | */ |
---|
| 27 | private $file; |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * @var string $currentTemplate O nome do template em uso |
---|
| 31 | * @access private |
---|
| 32 | */ |
---|
| 33 | private $currentTemplate; |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * @var string $DEFAULT_TEMPLATE O nome do template padrão |
---|
| 37 | * @access private |
---|
| 38 | */ |
---|
| 39 | private $DEFAULT_TEMPLATE = 'default'; |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * @var string $FILE_SYSTEM_DIR O caminho (no sistema de arquivos) para o diretório de templates |
---|
| 43 | * @access private |
---|
| 44 | */ |
---|
| 45 | private $FILE_SYSTEM_DIR; |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * @var string $WEB_WORKFLOW_BASE O caminho (na Web) para o diretório do Workflow |
---|
| 49 | * @access private |
---|
| 50 | */ |
---|
| 51 | private $WEB_WORKFLOW_BASE; |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * @var string $WEB_PATH O caminho (na Web) para o diretório de templates |
---|
| 55 | * @access private |
---|
| 56 | */ |
---|
| 57 | private $WEB_PATH; |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * @var array $cache O cache de templates (arquivo => template) |
---|
| 61 | * @access private |
---|
| 62 | */ |
---|
| 63 | private $cache; |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * @var int $CACHE_SIZE O tamanho do cache |
---|
| 67 | * @access private |
---|
| 68 | */ |
---|
| 69 | private $CACHE_SIZE = 100; |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * Construtor da classe |
---|
| 73 | * @return object |
---|
| 74 | * @access public |
---|
| 75 | */ |
---|
| 76 | public function TemplateServer() |
---|
| 77 | { |
---|
| 78 | if (!isset($_SESSION['workflow']['TemplateServer']['cache'])) |
---|
| 79 | $_SESSION['workflow']['TemplateServer']['cache'] = array(); |
---|
| 80 | $this->cache = &$_SESSION['workflow']['TemplateServer']['cache']; |
---|
| 81 | |
---|
| 82 | /* encontra o template atualmente em uso */ |
---|
| 83 | if (isset($_SESSION['workflow']['TemplateServer']['templateSet'])) |
---|
| 84 | { |
---|
| 85 | $this->currentTemplate = $_SESSION['workflow']['TemplateServer']['templateSet']; |
---|
| 86 | } |
---|
| 87 | else |
---|
| 88 | { |
---|
| 89 | if (isset($_SESSION['phpgw_info']['expresso']['server']['template_set'])) |
---|
| 90 | { |
---|
| 91 | $this->currentTemplate = $_SESSION['phpgw_info']['expresso']['server']['template_set']; |
---|
| 92 | } |
---|
| 93 | else |
---|
| 94 | { |
---|
| 95 | if (isset($GLOBALS['phpgw_info']['login_template_set'])) |
---|
| 96 | { |
---|
| 97 | $this->currentTemplate = $GLOBALS['phpgw_info']['login_template_set']; |
---|
| 98 | } |
---|
| 99 | else |
---|
| 100 | { |
---|
[2591] | 101 | Factory::getInstance('WorkflowMacro')->prepareEnvironment(); |
---|
[795] | 102 | if (!isset($GLOBALS['phpgw_info']['login_template_set'])) |
---|
| 103 | return false; |
---|
| 104 | $this->currentTemplate = $GLOBALS['phpgw_info']['login_template_set']; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | $_SESSION['workflow']['TemplateServer']['templateSet'] = $this->currentTemplate; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | $this->file = $_GET['file']; |
---|
| 111 | $this->FILE_SYSTEM_DIR = dirname(__FILE__) . '/../templates'; |
---|
| 112 | |
---|
| 113 | /* tenta carregar o endereço Web do Workflow */ |
---|
| 114 | if (isset($_SESSION['workflow']['TemplateServer']['workflowBase'])) |
---|
| 115 | { |
---|
| 116 | $this->WEB_WORKFLOW_BASE = $_SESSION['workflow']['TemplateServer']['workflowBase']; |
---|
| 117 | } |
---|
| 118 | else |
---|
| 119 | { |
---|
| 120 | if (isset($GLOBALS['phpgw_info']['server']) && is_array($GLOBALS['phpgw_info']['server']) && array_key_exists('webserver_url', $GLOBALS['phpgw_info']['server'])) |
---|
| 121 | { |
---|
| 122 | $this->WEB_WORKFLOW_BASE = ((string) $GLOBALS['phpgw_info']['server']['webserver_url']) . '/workflow'; |
---|
| 123 | } |
---|
| 124 | else |
---|
| 125 | { |
---|
| 126 | if (isset($_SESSION['phpgw_info']['workflow']['server']) && is_array($_SESSION['phpgw_info']['workflow']['server']) && array_key_exists('webserver_url', $_SESSION['phpgw_info']['workflow']['server'])) |
---|
| 127 | { |
---|
| 128 | $this->WEB_WORKFLOW_BASE = ((string) $_SESSION['phpgw_info']['workflow']['server']['webserver_url']) . '/workflow'; |
---|
| 129 | } |
---|
| 130 | else |
---|
| 131 | { |
---|
| 132 | /* se não for encontrado em nenhuma variável de ambiente, tenta carregar do banco de dados */ |
---|
[2591] | 133 | $webServerURL = (string) Factory::getInstance('WorkflowObjects')->getDBExpresso()->Link_ID->GetOne('SELECT config_value FROM phpgw_config WHERE config_app = ? AND config_name = ?', array('phpgwapi', 'webserver_url')); |
---|
[795] | 134 | $this->WEB_WORKFLOW_BASE = str_replace('//', '/', "{$webServerURL}/workflow"); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | $_SESSION['workflow']['TemplateServer']['workflowBase'] = $this->WEB_WORKFLOW_BASE; |
---|
| 138 | } |
---|
| 139 | $this->WEB_PATH = $this->WEB_WORKFLOW_BASE . '/templates'; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | * Redireciona a requisição para o arquivo do template adequado |
---|
| 144 | * @return void |
---|
| 145 | * @access public |
---|
| 146 | */ |
---|
| 147 | public function redirect() |
---|
| 148 | { |
---|
| 149 | if (strpos($this->file, '..') !== false) |
---|
| 150 | return false; |
---|
| 151 | |
---|
| 152 | if (($selectedTemplate = $this->getTemplateForFile($this->file)) === false) |
---|
| 153 | return false; |
---|
| 154 | |
---|
| 155 | $filename = $this->getSystemFile($this->file, $selectedTemplate); |
---|
| 156 | $webFile = $this->getWebFile($this->file, $selectedTemplate); |
---|
| 157 | |
---|
| 158 | if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == filemtime($filename))) |
---|
| 159 | { |
---|
| 160 | /* o cache do cliente está atualizado. Apenas envia um 304 (Não modificado) */ |
---|
| 161 | header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($filename)).' GMT', true, 304); |
---|
| 162 | } |
---|
| 163 | else |
---|
| 164 | { |
---|
| 165 | header("Location: {$webFile}"); |
---|
| 166 | exit; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
| 172 | * Encontra o template para o arquivo solicitado |
---|
| 173 | * @param string $file O nome do arquivo |
---|
| 174 | * @return string O nome do template que contém o arquivo (será 'default' ou o nome do template em uso) |
---|
| 175 | * @access public |
---|
| 176 | */ |
---|
| 177 | public function getTemplateForFile($file) |
---|
| 178 | { |
---|
| 179 | if (!is_null(($output = $this->getCache($file)))) |
---|
| 180 | return $output; |
---|
| 181 | |
---|
| 182 | $output = false; |
---|
| 183 | if (file_exists($filename = "{$this->FILE_SYSTEM_DIR}/{$this->currentTemplate}/{$file}")) |
---|
| 184 | $output = $this->currentTemplate; |
---|
| 185 | else |
---|
| 186 | if (file_exists($filename = "{$this->FILE_SYSTEM_DIR}/{$this->DEFAULT_TEMPLATE}/{$file}")) |
---|
| 187 | $output = $this->DEFAULT_TEMPLATE; |
---|
| 188 | |
---|
| 189 | $this->setCache($file, $output); |
---|
| 190 | return $output; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | /** |
---|
| 194 | * Encontra o endereço (Web) do arquivo |
---|
| 195 | * @param string $file O nome do arquivo |
---|
| 196 | * @param string $template O template do arquivo (se não for passado, a classe tentará encontrar o template adeqüado) |
---|
| 197 | * @return string O endereço (Web) do arquivo |
---|
| 198 | * @access public |
---|
| 199 | */ |
---|
| 200 | public function getWebFile($file, $template = null) |
---|
| 201 | { |
---|
| 202 | if (is_null($template)) |
---|
| 203 | if (($template = $this->getTemplateForFile($file)) === false) |
---|
| 204 | return false; |
---|
| 205 | |
---|
| 206 | return str_replace('//', '/', "{$this->WEB_PATH}/{$template}/{$file}"); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | /** |
---|
| 210 | * Encontra o endereço (no sistema de arquivos) do arquivo |
---|
| 211 | * @param string $file O nome do arquivo |
---|
| 212 | * @param string $template O template do arquivo (se não for passado, a classe tentará encontrar o template adeqüado) |
---|
| 213 | * @return string O endereço (no sistema de arquivos) do arquivo |
---|
| 214 | * @access public |
---|
| 215 | */ |
---|
| 216 | public function getSystemFile($file, $template = null) |
---|
| 217 | { |
---|
| 218 | if (is_null($template)) |
---|
| 219 | if (($template = $this->getTemplateForFile($file)) === false) |
---|
| 220 | return false; |
---|
| 221 | |
---|
| 222 | return "{$this->FILE_SYSTEM_DIR}/{$template}/{$file}"; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
| 226 | * Gera um link Web, através do servidor de templates (esta classe), para o arquivo informado |
---|
| 227 | * @param string $file O nome do arquivo |
---|
| 228 | * @return string O endereço do arquivo |
---|
| 229 | * @access public |
---|
| 230 | */ |
---|
| 231 | public function generateLink($file) |
---|
| 232 | { |
---|
| 233 | return "{$this->WEB_WORKFLOW_BASE}/templateFile.php?file={$file}"; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | /** |
---|
| 237 | * Gera um link Web, através do servidor de templates (esta classe), para a imagem informada |
---|
| 238 | * @param string $file O nome da imagem |
---|
| 239 | * @return string O endereço do arquivo |
---|
| 240 | * @access public |
---|
| 241 | */ |
---|
| 242 | public function generateImageLink($file) |
---|
| 243 | { |
---|
| 244 | return $this->generateLink("images/{$file}"); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
| 248 | * Define um elemento no cache |
---|
| 249 | * @param string $key O nome da chave do cache |
---|
| 250 | * @param mixed $value O valor que será armazenado no cache |
---|
| 251 | * @return void |
---|
| 252 | * @access public |
---|
| 253 | */ |
---|
| 254 | private function setCache($key, $value) |
---|
| 255 | { |
---|
| 256 | if (isset($this->cache[$key])) |
---|
| 257 | unset($this->cache[$key]); |
---|
| 258 | |
---|
| 259 | $this->cache[$key] = $value; |
---|
| 260 | |
---|
| 261 | if (count($this->cache) > $this->CACHE_SIZE) |
---|
| 262 | array_shift($this->cache); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | /** |
---|
| 266 | * Busca um elemento do cache |
---|
| 267 | * @param string $key O nome da chave do cache |
---|
| 268 | * @return mixed O valor que está armazenado no cache. Caso não seja encontrado, será retornado null |
---|
| 269 | * @return void |
---|
| 270 | * @access public |
---|
| 271 | */ |
---|
| 272 | private function getCache($key) |
---|
| 273 | { |
---|
| 274 | if (!isset($this->cache[$key])) |
---|
| 275 | return null; |
---|
| 276 | |
---|
| 277 | /* assegura que o elemento buscado fique em último lugar da array e, assim, garantindo que ficará mais tempo em cache (princípio da temporalidade) */ |
---|
| 278 | $output = $this->cache[$key]; |
---|
| 279 | unset($this->cache[$key]); |
---|
| 280 | $this->cache[$key] = $output; |
---|
| 281 | |
---|
| 282 | return $output; |
---|
| 283 | } |
---|
| 284 | } |
---|
| 285 | ?> |
---|