aboutsummaryrefslogtreecommitdiffstats
path: root/include/template.class.php
blob: b35d02c135d943a4802da00efbe57cf7846c54f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
// +-----------------------------------------------------------------------+
// | PhpWebGallery - a PHP based picture gallery                           |
// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
// | Copyright (C) 2003-2008 PhpWebGallery Team - http://phpwebgallery.net |
// +-----------------------------------------------------------------------+
// | file          : $Id$
// | last update   : $Date$
// | last modifier : $Author$
// | revision      : $Revision$
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+


require 'smarty/libs/Smarty.class.php';

// migrate lang:XXX
//    sed "s/{lang:\([^}]\+\)}/{\'\1\'|@translate}/g" my_template.tpl
// migrate change root level vars {XXX}
//    sed "s/{pwg_root}/{ROOT_URL}/g" my_template.tpl
// migrate change root level vars {XXX}
//    sed "s/{\([a-zA-Z_]\+\)}/{$\1}/g" my_template.tpl
// migrate all
//    cat my_template.tpl | sed "s/{lang:\([^}]\+\)}/{\'\1\'|@translate}/g" | sed "s/{pwg_root}/{ROOT_URL}/g" | sed "s/{\([a-zA-Z_]\+\)}/{$\1}/g"


class Template {

  var $smarty;

  var $_old;

  var $output = '';

  // Hash of filenames for each template handle.
  var $files = array();

  function Template($root = ".", $theme= "")
  {
    global $conf;

    $this->smarty = new Smarty;
    $this->smarty->debugging = $conf['debug_template'];
    //$this->smarty->force_compile = true;

    if ( isset($conf['compiled_template_dir'] ) )
    {
      $compile_dir = $conf['compiled_template_dir'];
    }
    else
    {
      $compile_dir = $conf['local_data_dir'];
      if ( !is_dir($compile_dir) )
      {
        mkdir( $compile_dir, 0777);
        file_put_contents($compile_dir.'/index.htm', '');
      }
      $compile_dir .= '/templates_c';
    }
    if ( !is_dir($compile_dir) )
    {
      mkdir( $compile_dir, 0777 );
      file_put_contents($compile_dir.'/index.htm', '');
    }

    $this->smarty->compile_dir = $compile_dir;

    $this->smarty->register_function( 'lang', array('Template', 'fn_l10n') );

    $this->smarty->assign_by_ref( 'pwg', new PwgTemplateAdapter() );
    $this->smarty->register_modifier( 'translate', array('Template', 'mod_translate') );

    if ( !empty($theme) )
    {
      include($root.'/theme/'.$theme.'/themeconf.inc.php');
      $this->smarty->assign('themeconf', $themeconf);
    }

    $this->_old = & new TemplateOld($root, $theme);

    $this->set_template_dir($root);
  }

  /**
   * Sets the template root directory for this Template object.
   */
  function set_template_dir($dir)
  {
    $this->_old->set_rootdir($dir);
    $this->smarty->template_dir = $dir;

    $real_dir = realpath($dir);
    $compile_id = crc32( $real_dir===false ? $dir : $real_dir);
    $this->smarty->compile_id = sprintf('%08X', $compile_id );
  }

  /**
   * Gets the template root directory for this Template object.
   */
  function get_template_dir()
  {
    return $this->smarty->template_dir;
  }

  /**
   * Deletes all compiled templates.
   */
  function delete_compiled_templates()
  {
      $save_compile_id = $this->smarty->compile_id;
      $this->smarty->compile_id = null;
      $this->smarty->clear_compiled_tpl();
      $this->smarty->compile_id = $save_compile_id;
      file_put_contents($this->smarty->compile_dir.'/index.htm', '');
  }

  /** DEPRECATED */
  function get_themeconf($val)
  {
    $tc = $this->smarty->get_template_vars('themeconf');
    return isset($tc[$val]) ? $tc[$val] : '';
  }

  /**
   * Sets the template filename for handle.
   */
  function set_filename($handle, $filename)
  {
    return $this->set_filenames( array($handle=>$filename) );
  }

  /**
   * Sets the template filenames for handles. $filename_array should be a
   * hash of handle => filename pairs.
   */
  function set_filenames($filename_array)
  {
    if (!is_array($filename_array))
    {
      return false;
    }

    reset($filename_array);
    while(list($handle, $filename) = each($filename_array))
    {
      if (is_null($filename))
        unset( $this->files[$handle] );
      else
        $this->files[$handle] = $filename;
    }
    return true;
  }

  /**
   * DEPRECATED - backward compatibility only; use assign
   */
  function assign_vars($vararray)
  {
    is_array( $vararray ) || die('assign_vars parameter not array');
    $this->assign( $vararray );
  }

  /**
   * DEPRECATED - backward compatibility only; use assign
   */
  function assign_var($varname, $varval)
  {
    !is_array( $varname ) || die('assign_var parameter name is array');
    $this->assign( $varname, $varval );
  }

  /**
   * Inserts the uncompiled code for $handle as the value of $varname in the
   * root-level. This can be used to effectively include a template in the
   * middle of another template.
   * This is equivalent to assign($varname, $this->parse($handle, true))
   */
  function assign_var_from_handle($varname, $handle)
    {
      $this->assign($varname, $this->parse($handle, true));
      return true;
    }

  /**
   * DEPRECATED - backward compatibility only
   */
  function assign_block_vars($blockname, $vararray)
  {
    if (strstr($blockname, '.')!==false)
    {
      $blocks = explode('.', $blockname);
      $blockcount = sizeof($blocks) - 1;
      $root_var = & $this->smarty->get_template_vars();

      $str = '$root_var';
      for ($i = 0; $i < $blockcount; $i++)
      {
        $str .= '[\'' . $blocks[$i] . '\']';
        eval('$lastiteration = isset('.$str.') ? sizeof('.$str.')-1:0;');
        $str .= '[' . $lastiteration . ']';
      }
      $str .= '[\'' . $blocks[$blockcount] . '\'][] = $vararray;';
      eval($str);
    }
    else
      $this->smarty->append( $blockname, $vararray );

    $this->_old->assign_block_vars($blockname, $vararray);
  }

  /**
   * DEPRECATED - backward compatibility only
   */
  function merge_block_vars($blockname, $vararray)
  {
    if (strstr($blockname, '.')!==false)
    {
      $blocks = explode('.', $blockname);
      $blockcount = count($blocks);
      $root_var = & $this->smarty->get_template_vars();

      $str = '$root_var';
      for ($i = 0; $i < $blockcount; $i++)
      {
        $str .= '[\'' . $blocks[$i] . '\']';
        eval('$lastiteration = isset('.$str.') ? sizeof('.$str.')-1:-1;');
        if ($lastiteration==-1)
        {
          return false;
        }
        $str .= '[' . $lastiteration . ']';
      }
      $str = $str.'=array_merge('.$str.', $vararray);';
      eval($str);
    }
    else
      $this->smarty->append( $blockname, $vararray, true );

    $this->_old->merge_block_vars($blockname, $vararray);
    return true;
  }

  /**
   * Load the file for the handle, eventually compile the file and run the compiled
   * code. This will add the output to the results or return the result if $return
   * is true.
   */
  function parse($handle, $return=false)
  {
    if ( !isset($this->files[$handle]) )
    {
      die("Template->parse(): Couldn't load template file for handle $handle");
    }

    $is_new = true;
    $params = array('resource_name' => $this->files[$handle], 'quiet'=>true, 'get_source'=>true);
    if ( $this->smarty->_fetch_resource_info($params) )
    {
      if (!preg_match('~{(/(if|section|foreach))|ldelim|\$[a-zA-Z_]+}~', @$params['source_content']) )
        $is_new = false;
    }

    if ($is_new)
    {
      $this->smarty->assign( 'ROOT_URL', get_root_url() );
      $this->smarty->assign( 'TAG_INPUT_ENABLED',
        ((is_adviser()) ? 'disabled="disabled" onclick="return false;"' : ''));
      $v = $this->smarty->fetch($this->files[$handle], null, null, false);
    }
    else
    {
      $this->_old->assign_vars(array('TAG_INPUT_ENABLED' =>
        ((is_adviser()) ? 'disabled onclick="return false;"' : '')));
      $this->_old->set_filename( $handle, $this->files[$handle] );
      $v = $this->_old->parse($handle, true);
    }
    if ($return)
    {
      return $v;
    }
    $this->output .= $v;
  }

  /**
   * Load the file for the handle, eventually compile the file and run the compiled
   * code. This will print out the results of executing the template.
   */
  function pparse($handle)
  {
    $this->parse($handle, false);
    echo $this->output;
    $this->output='';

  }


  /** flushes the output */
  function p()
  {
    $start = get_moment();

    echo $this->output;
    $this->output='';

    if ($this->smarty->debugging)
    {
      global $t2;
      $this->smarty->assign(
        array(
        'AAAA_DEBUG_OUTPUT_TIME__' => get_elapsed_time($start, get_moment()),
        'AAAA_DEBUG_TOTAL_TIME__' => get_elapsed_time($t2, get_moment())
        )
        );
      require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php');
      echo smarty_core_display_debug_console(null, $this->smarty);
    }
  }

  /**
   * Root-level variable concatenation. Appends a  string to an existing
   * variable assignment with the same name.
   */
  function concat_var($tpl_var, $value)
  {
    $old_val = & $this->smarty->get_template_vars($tpl_var);
    if ( isset($old_val) )
    {
      $old_val .= $value;
      $this->_old->concat_var( $tpl_var, $value );
    }
    else
    {
      $this->assign($tpl_var, $value);
    }
  }

  /** see smarty assign http://www.smarty.net/manual/en/api.assign.php */
  function assign($tpl_var, $value = null)
  {
    $this->smarty->assign( $tpl_var, $value );

    if ( is_array($tpl_var) )
      $this->_old->assign_vars( $tpl_var );
    else
      $this->_old->assign_var( $tpl_var, $value );
  }

  /** see smarty append http://www.smarty.net/manual/en/api.append.php */
  function append($tpl_var, $value=null, $merge=false)
  {
    $this->smarty->append( $tpl_var, $value, $merge );
  }

  /** see smarty get_template_vars http://www.smarty.net/manual/en/api.get_template_vars.php */
  function &get_template_vars($name=null)
  {
    return $this->smarty->get_template_vars( $name );
  }

  /** see smarty append http://www.smarty.net/manual/en/api.clear_assign.php */
  function clear_assign($tpl_var)
  {
    $this->smarty->clear_assign( $tpl_var );
  }

  /*static*/ function fn_l10n($params, &$smarty)
  {
    return l10n($params['t']);
  }

  /**
   * translate variable modifiers - translates a text to the currently loaded
   * language
   */
  /*static*/ function mod_translate($text)
  {
    return l10n($text);
  }
}

/**
 * This class contains basic functions that can be called directly from the
 * templates in the form $pwg->l10n('edit')
 */
class PwgTemplateAdapter
{
  function l10n($text)
  {
    return l10n($text);
  }

  function l10n_dec($s, $p, $v)
  {
    return l10n_dec($s, $p, $v);
  }

  function sprintf()
  {
    $args = func_get_args();
    return call_user_func_array('sprintf',  $args );
  }
}

?>