source: sandbox/workflow/branches/993/inc/class.TemplateServer.inc.php @ 2492

Revision 2492, 7.4 KB checked in by pedroerp, 14 years ago (diff)

Ticket #993 - Trocando acessos à GLOBALS por acessos à Settings.

  • Property svn:executable set to *
Line 
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
12require_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 */
21class 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                                {
101                                        Factory::getInstance('WorkflowMacro')->prepareEnvironment();
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                $this->WEB_WORKFLOW_BASE = WF_BASE_URL;
115
116                $this->WEB_PATH = $this->WEB_WORKFLOW_BASE . '/templates';
117        }
118
119        /**
120         * Redireciona a requisição para o arquivo do template adequado
121         * @return void
122         * @access public
123         */
124        public function redirect()
125        {
126                if (strpos($this->file, '..') !== false)
127                        return false;
128
129                if (($selectedTemplate = $this->getTemplateForFile($this->file)) === false)
130                        return false;
131
132                $filename = $this->getSystemFile($this->file, $selectedTemplate);
133                $webFile = $this->getWebFile($this->file, $selectedTemplate);
134
135                if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == filemtime($filename)))
136                {
137                        /* o cache do cliente está atualizado. Apenas envia um 304 (Não modificado) */
138                        header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($filename)).' GMT', true, 304);
139                }
140                else
141                {
142                        header("Location: {$webFile}");
143                        exit;
144                }
145
146        }
147
148        /**
149         * Encontra o template para o arquivo solicitado
150         * @param string $file O nome do arquivo
151         * @return string O nome do template que contém o arquivo (será 'default' ou o nome do template em uso)
152         * @access public
153         */
154        public function getTemplateForFile($file)
155        {
156                if (!is_null(($output = $this->getCache($file))))
157                        return $output;
158
159                $output = false;
160                if (file_exists($filename = "{$this->FILE_SYSTEM_DIR}/{$this->currentTemplate}/{$file}"))
161                        $output = $this->currentTemplate;
162                else
163                        if (file_exists($filename = "{$this->FILE_SYSTEM_DIR}/{$this->DEFAULT_TEMPLATE}/{$file}"))
164                                $output = $this->DEFAULT_TEMPLATE;
165
166                $this->setCache($file, $output);
167                return $output;
168        }
169
170        /**
171         * Encontra o endereço (Web) do arquivo
172         * @param string $file O nome do arquivo
173         * @param string $template O template do arquivo (se não for passado, a classe tentará encontrar o template adeqüado)
174         * @return string O endereço (Web) do arquivo
175         * @access public
176         */
177        public function getWebFile($file, $template = null)
178        {
179                if (is_null($template))
180                        if (($template = $this->getTemplateForFile($file)) === false)
181                                return false;
182
183                return str_replace('//', '/', "{$this->WEB_PATH}/{$template}/{$file}");
184        }
185
186        /**
187         * Encontra o endereço (no sistema de arquivos) do arquivo
188         * @param string $file O nome do arquivo
189         * @param string $template O template do arquivo (se não for passado, a classe tentará encontrar o template adeqüado)
190         * @return string O endereço (no sistema de arquivos) do arquivo
191         * @access public
192         */
193        public function getSystemFile($file, $template = null)
194        {
195                if (is_null($template))
196                        if (($template = $this->getTemplateForFile($file)) === false)
197                                return false;
198
199                return "{$this->FILE_SYSTEM_DIR}/{$template}/{$file}";
200        }
201
202        /**
203         * Gera um link Web, através do servidor de templates (esta classe), para o arquivo informado
204         * @param string $file O nome do arquivo
205         * @return string O endereço do arquivo
206         * @access public
207         */
208        public function generateLink($file)
209        {
210                return "{$this->WEB_WORKFLOW_BASE}/templateFile.php?file={$file}";
211        }
212
213        /**
214         * Gera um link Web, através do servidor de templates (esta classe), para a imagem informada
215         * @param string $file O nome da imagem
216         * @return string O endereço do arquivo
217         * @access public
218         */
219        public function generateImageLink($file)
220        {
221                return $this->generateLink("images/{$file}");
222        }
223
224        /**
225         * Define um elemento no cache
226         * @param string $key O nome da chave do cache
227         * @param mixed $value O valor que será armazenado no cache
228         * @return void
229         * @access public
230         */
231        private function setCache($key, $value)
232        {
233                if (isset($this->cache[$key]))
234                        unset($this->cache[$key]);
235
236                $this->cache[$key] = $value;
237
238                if (count($this->cache) > $this->CACHE_SIZE)
239                        array_shift($this->cache);
240        }
241
242        /**
243         * Busca um elemento do cache
244         * @param string $key O nome da chave do cache
245         * @return mixed O valor que está armazenado no cache. Caso não seja encontrado, será retornado null
246         * @return void
247         * @access public
248         */
249        private function getCache($key)
250        {
251                if (!isset($this->cache[$key]))
252                        return null;
253
254                /* assegura que o elemento buscado fique em último lugar da array e, assim, garantindo que ficará mais tempo em cache (princípio da temporalidade) */
255                $output = $this->cache[$key];
256                unset($this->cache[$key]);
257                $this->cache[$key] = $output;
258
259                return $output;
260        }
261}
262?>
Note: See TracBrowser for help on using the repository browser.