kernel2/QF2_vis.php
author foxel
Wed, 30 Dec 2009 16:25:48 +0600
changeset 34 0e05a7ed0206
parent 19 7b6c1826b462
child 37 c971c735661c
permissions -rw-r--r--
модификация процесса обработки redefined.vis
для корректного переопределения блоков из частей стиля, отличных от common
Для указания переопределяемой части нужно вставить строку "<<part '{имя части}'>>' перед блоками, фактически переопределяющими эту часть стиля
foxel@0
     1
<?php
foxel@0
     2
foxel@0
     3
// -------------------------------------------------------------------------- \\
foxel@0
     4
// VIS Class that provides template working and HTML visualization            \\
foxel@0
     5
// -------------------------------------------------------------------------- \\
foxel@0
     6
foxel@0
     7
// Simple InUSE Check
foxel@0
     8
if ( !defined('QF_STARTED') )
foxel@0
     9
        die('Hacking attempt');
foxel@0
    10
foxel@0
    11
foxel@0
    12
if ( defined('QF_KERNEL_VIS_LOADED') )
foxel@0
    13
        die('Scripting error');
foxel@0
    14
foxel@0
    15
define('QF_KERNEL_VIS_LOADED', True);
foxel@0
    16
foxel@0
    17
// Cache prefixes for module data
foxel@0
    18
define('QF_KERNEL_VIS_VPREFIX', 'VIS.');
foxel@0
    19
define('QF_KERNEL_VIS_CPREFIX', 'VIS_CSS.');
foxel@0
    20
define('QF_KERNEL_VIS_JPREFIX', 'VIS_JS.');
foxel@0
    21
define('QF_KERNEL_VIS_COMMON', 'common');
foxel@0
    22
define('QF_KERNEL_VIS_DEFSTYLE', 'qf_def');
foxel@0
    23
foxel@0
    24
// data directories
foxel@0
    25
define('QF_STYLES_DIR', QF_DATA_ROOT.'styles/');
foxel@0
    26
//define('QF_STCNTS_DIR', QF_DATA_ROOT.'vis_consts/'); // not used for now - not needed
foxel@0
    27
define('QF_JSCRIPTS_DIR', QF_DATA_ROOT.'jscripts/');
foxel@0
    28
define('QF_STATICS_DIR', 'static');
foxel@0
    29
define('QF_IMAGES_DIR', QF_STATICS_DIR.'/images');
foxel@0
    30
define('QF_ST_IMGS_DIR', QF_IMAGES_DIR.'/styles/');
foxel@0
    31
foxel@0
    32
// defining some usefull constants
foxel@0
    33
// VIS resource types
foxel@0
    34
define('QF_VIS_NORMAL',  0);
foxel@0
    35
define('QF_VIS_STATIC',  1);
foxel@0
    36
define('QF_VIS_DINAMIC', 2);
foxel@0
    37
// Breakline
foxel@0
    38
define('VIS_BR', "\r\n");
foxel@0
    39
foxel@0
    40
// node flags
foxel@0
    41
define('QF_VISNODE_ARRAY', 1); // node is an array of sametype nodes
foxel@0
    42
foxel@0
    43
foxel@0
    44
class QF_Visual
foxel@0
    45
{
foxel@0
    46
    var $templates  = Array();
foxel@0
    47
foxel@0
    48
    var $VCSS_data  = ''; // CSS loaded from visuals
foxel@0
    49
    var $VJS_data   = ''; // JS loaded from visuals
foxel@0
    50
    var $CSS_data   = '';
foxel@0
    51
    var $JS_data    = '';
foxel@0
    52
    var $Consts     = Array();
foxel@0
    53
foxel@0
    54
    var $node_type  = Array();
foxel@0
    55
    var $node_vars  = Array();
foxel@0
    56
    var $node_subs  = Array();
foxel@0
    57
    var $node_flags = Array();
foxel@0
    58
    var $named_node = Array();
foxel@0
    59
    var $parsed     = Array();
foxel@0
    60
foxel@0
    61
    var $style_name = QF_KERNEL_VIS_DEFSTYLE;
foxel@0
    62
    var $style_vari = QF_KERNEL_VIS_COMMON;
foxel@0
    63
foxel@0
    64
    var $VIS_loaded = Array();
foxel@0
    65
    var $CSS_loaded = false;
foxel@0
    66
    var $JS_loaded  = Array();
foxel@0
    67
foxel@0
    68
    var $lang_name  = null;
foxel@0
    69
    var $lang       = null;     // linking lang vars
foxel@0
    70
    var $LNG_loaded = null;     // linking lang vars
foxel@0
    71
foxel@0
    72
    var $force_append  = true;  // forces to append CSS
foxel@0
    73
    var $force_compact = true;  // forces to compact CSS/JS data
foxel@0
    74
    var $vis_consts = Array();
foxel@0
    75
    var $func_parsers = Array();
foxel@0
    76
foxel@0
    77
    function QF_Visual()
foxel@0
    78
    {
foxel@0
    79
        global $QF;
foxel@0
    80
foxel@0
    81
        if (defined('QF_VASUAL_CREATED'))
foxel@0
    82
            trigger_error('Duplicate visual manager creation!', E_USER_ERROR);
foxel@0
    83
foxel@0
    84
        define('QF_VASUAL_CREATED', true);
foxel@0
    85
foxel@0
    86
        $this->node_type  = Array(0 => 'MAIN_PAGE');
foxel@0
    87
        $this->style_name = QF_KERNEL_VIS_DEFSTYLE;
foxel@0
    88
        $this->node_vars  = Array(0 => Array());
foxel@0
    89
        $this->node_subs  = Array(0 => Array());
foxel@0
    90
        $this->node_flags = Array(0 => 0);
foxel@0
    91
        $this->named_node = Array('PAGE' => 0, 'MAIN' => 0);
foxel@0
    92
        $this->func_parsers = Array(
foxel@6
    93
            'FULLURL' => 'qf_full_url',
foxel@0
    94
            'HTMLQUOTE' => 'htmlspecialchars',
foxel@0
    95
            'URLEN' => 'qf_url_encode_part',
foxel@0
    96
            'JS_DEF' => 'qf_value_JS_definition',
foxel@0
    97
            'FTIME' => Array(&$QF->LNG, 'Time_Format'),
foxel@0
    98
            'FBYTES' => Array(&$QF->LNG, 'Size_Format'),
foxel@0
    99
            );
foxel@9
   100
        $this->vis_consts = Array(
foxel@9
   101
            'TIME' => $QF->Timer->time,
foxel@9
   102
            );
foxel@0
   103
    }
foxel@0
   104
foxel@0
   105
    function _Start()
foxel@0
   106
    {
foxel@0
   107
        global $QF;
foxel@0
   108
foxel@0
   109
        $data = $QF->LNG->Get_Data_Links();
foxel@0
   110
        $this->lang       =& $data['lang'];
foxel@0
   111
        $this->lang_name  =& $data['lang_name'];
foxel@0
   112
        $this->LNG_loaded =& $data['LNG_loaded'];
foxel@19
   113
        $this->Configure(Array(), true);
foxel@0
   114
    }
foxel@0
   115
foxel@0
   116
    // configuring and loading functions
foxel@0
   117
    function Configure($params, $force_clear = false)
foxel@0
   118
    {
foxel@0
   119
        global $QF;
foxel@0
   120
foxel@0
   121
        if (is_array($params))
foxel@0
   122
        {
foxel@0
   123
            extract($params);
foxel@0
   124
foxel@0
   125
            if ($force_clear) // clear all settings
foxel@0
   126
            {
foxel@0
   127
                $this->node_type  = Array(0 => 'MAIN_PAGE');
foxel@0
   128
                $this->style_name = QF_KERNEL_VIS_DEFSTYLE;
foxel@0
   129
                $this->style_vari = QF_KERNEL_VIS_COMMON;
foxel@0
   130
                $this->node_vars  = Array(0 => Array());
foxel@0
   131
                $this->node_subs  = Array(0 => Array());
foxel@0
   132
                $this->named_node = Array('PAGE' => 0);
foxel@0
   133
                $this->named_node = Array('MAIN' => 0);
foxel@0
   134
                $this->templates  = Array();
foxel@0
   135
                $this->VCSS_data  = '';
foxel@0
   136
                $this->VJS_data   = '';
foxel@0
   137
                $this->VIS_loaded = Array();
foxel@0
   138
                $this->CSS_data   = '';
foxel@0
   139
                $this->JS_data    = '';
foxel@0
   140
                $this->CSS_loaded = false;
foxel@0
   141
                $this->JS_loaded  = Array();
foxel@0
   142
                $this->force_append  = true;
foxel@0
   143
                $this->force_compact = true;
foxel@0
   144
            }
foxel@0
   145
foxel@0
   146
            if (isset($lang))
foxel@0
   147
                $QF->LNG->Select($lang);
foxel@0
   148
foxel@0
   149
            if (isset($style))
foxel@0
   150
            {
foxel@0
   151
                $this->style_name = preg_replace('#\W#', '_', $style);
foxel@0
   152
                if (count($this->VIS_loaded))
foxel@0
   153
                {
foxel@0
   154
                    $parts = $this->VIS_loaded;
foxel@0
   155
                    $this->templates = Array();
foxel@0
   156
                    $this->VCSS_data = '';
foxel@0
   157
                    $this->VJS_data  = '';
foxel@0
   158
                    $this->VIS_loaded = Array();
foxel@0
   159
                    if (!$force_clear)
foxel@0
   160
                        foreach ($parts as $part)
foxel@0
   161
                            $this->Load_Templates($part);
foxel@0
   162
                }
foxel@0
   163
foxel@0
   164
                if (isset($CSS))
foxel@0
   165
                    $this->style_vari = $CSS;
foxel@0
   166
                elseif($CSS = $this->CSS_loaded)
foxel@0
   167
                    if (!$force_clear)
foxel@0
   168
                        $this->Load_ECSS($CSS);
foxel@0
   169
foxel@0
   170
            }
foxel@0
   171
            elseif (isset($CSS))
foxel@0
   172
                $this->style_vari = $CSS;
foxel@0
   173
foxel@0
   174
            if (isset($root_node))
foxel@0
   175
                $this->node_type[0] = $root_node;
foxel@0
   176
foxel@0
   177
            if (isset($force_append))
foxel@0
   178
                $this->force_append = (bool) $force_append;
foxel@0
   179
            if (isset($force_compact))
foxel@0
   180
                $this->force_compact = (bool) $force_compact;
foxel@0
   181
        }
foxel@0
   182
    }
foxel@0
   183
foxel@9
   184
    function Set_VConsts($consts, $no_replace = false)
foxel@0
   185
    {
foxel@0
   186
        if (!is_array($consts))
foxel@0
   187
            return false;
foxel@9
   188
foxel@9
   189
        if ($no_replace)
foxel@9
   190
            $this->vis_consts = $this->vis_consts + $consts;
foxel@0
   191
        else
foxel@0
   192
            $this->vis_consts = $consts + $this->vis_consts;
foxel@0
   193
foxel@0
   194
        return true;
foxel@0
   195
    }
foxel@0
   196
foxel@0
   197
    function Load_ECSS($variant = '')
foxel@0
   198
    {
foxel@0
   199
        Global $QF;
foxel@0
   200
foxel@0
   201
        if (!$variant)
foxel@0
   202
            $variant = $this->style_vari;
foxel@0
   203
        else
foxel@0
   204
            $variant = strtolower(preg_replace('#\W#', '_', $variant));
foxel@0
   205
foxel@0
   206
        $cachename = QF_KERNEL_VIS_CPREFIX.$this->style_name.'.'.$this->lang_name.'.'.$variant;
foxel@0
   207
foxel@0
   208
        if ($Cdata = $QF->Cache->Get($cachename))
foxel@0
   209
        {
foxel@0
   210
            $this->CSS_data = $Cdata;
foxel@0
   211
            $QF->Timer->Time_Log($this->style_name.'.'.$variant.' CSS file loaded (from global cache)');
foxel@0
   212
        }
foxel@0
   213
        else
foxel@0
   214
        {
foxel@0
   215
            $file = $variant.'.ecss';
foxel@0
   216
            $style = $this->style_name;
foxel@0
   217
            $cfile = QF_STYLES_DIR.$style.'/'.$file;
foxel@0
   218
            $commons = QF_STYLES_DIR.$style.'/'.QF_KERNEL_VIS_COMMON.'.ecss';
foxel@0
   219
foxel@0
   220
            if (!file_exists($cfile))
foxel@0
   221
            {
foxel@0
   222
                trigger_error('VIS: there is no '.$this->style_name.'.'.$variant.' ECSS file', E_USER_WARNING );
foxel@0
   223
                $cfile = $commons;
foxel@0
   224
            }
foxel@0
   225
foxel@0
   226
            if ($indata = qf_file_get_contents($cfile))
foxel@0
   227
            {
foxel@0
   228
                $consts = Array(
foxel@0
   229
                    'IMGS' => qf_full_url(QF_IMAGES_DIR),
foxel@0
   230
                    'ST_IMGS' => qf_full_url(QF_ST_IMGS_DIR.$style),
foxel@0
   231
                    'STATICS' => qf_full_url(QF_STATICS_DIR),
foxel@0
   232
                    );
foxel@0
   233
foxel@0
   234
                if (($cfile != $commons) && strstr($indata, '{COMMON_ECSS}') && ($commons = qf_file_get_contents($commons)))
foxel@0
   235
                    $indata = str_replace('{COMMON_ECSS}', $commons, $indata);
foxel@0
   236
foxel@0
   237
                $Cdata = $this->Prepare_ECSS($indata, $consts);
foxel@0
   238
foxel@0
   239
                $QF->Cache->Set($cachename, $Cdata);
foxel@0
   240
                $this->CSS_data = $Cdata;
foxel@0
   241
                $QF->Timer->Time_Log($this->style_name.'.'.$variant.' CSS file loaded (from ECSS file)');
foxel@0
   242
foxel@0
   243
            }
foxel@0
   244
            else
foxel@0
   245
                trigger_error('VIS: error loading '.$this->style_name.'.'.$variant.' ECSS file', E_USER_WARNING );
foxel@0
   246
        }
foxel@0
   247
foxel@0
   248
        $this->style_vari = $this->CSS_loaded = $variant;
foxel@0
   249
        return true;
foxel@0
   250
foxel@0
   251
    }
foxel@0
   252
foxel@0
   253
    function Load_EJS($name = '')
foxel@0
   254
    {
foxel@0
   255
        Global $QF;
foxel@0
   256
foxel@0
   257
        if (!$name)
foxel@0
   258
            $name = QF_KERNEL_VIS_COMMON;
foxel@0
   259
        else
foxel@0
   260
            $name = strtolower(preg_replace('#\W#', '_', $name));
foxel@0
   261
foxel@0
   262
        if (!in_array($name, $this->JS_loaded))
foxel@0
   263
        {
foxel@0
   264
            $cachename = QF_KERNEL_VIS_JPREFIX.$this->lang_name.'.'.$name;
foxel@0
   265
foxel@0
   266
            if ($JSData = $QF->Cache->Get($cachename))
foxel@0
   267
            {
foxel@0
   268
                $this->JS_data.= VIS_BR.$JSData;
foxel@0
   269
foxel@0
   270
                $QF->Timer->Time_Log('"'.$name.'" JScript loaded (from global cache)');
foxel@0
   271
            }
foxel@0
   272
            else
foxel@0
   273
            {
foxel@0
   274
                if (!in_array($name, $this->LNG_loaded))
foxel@0
   275
                    $QF->LNG->Load_Language($name);
foxel@0
   276
foxel@0
   277
                $file = $name.'.ejs';
foxel@0
   278
                $cfile = QF_JSCRIPTS_DIR.$file;
foxel@0
   279
foxel@0
   280
                if (!file_exists($cfile))
foxel@0
   281
                {
foxel@0
   282
                    trigger_error('VIS: there is no '.$name.' EJS file', E_USER_WARNING );
foxel@0
   283
                }
foxel@0
   284
                elseif ($indata = qf_file_get_contents($cfile))
foxel@0
   285
                {
foxel@0
   286
                    $style = $this->style_name;
foxel@0
   287
                    $indata = str_replace('{ST_IMGS}', qf_full_url(QF_ST_IMGS_DIR.$style), $indata);
foxel@0
   288
                    $indata = str_replace('{IMGS}', qf_full_url(QF_IMAGES_DIR), $indata);
foxel@0
   289
                    $indata = str_replace('{STATICS}', qf_full_url(QF_STATICS_DIR), $indata);
foxel@0
   290
foxel@0
   291
                    $indata = preg_replace_callback('#\{(?>L_((?:\w+|\"[^\"]+\"|\|)+))\}#',Array(&$this, '_Templ_Lang_CB'),$indata);
foxel@0
   292
                    $indata = preg_replace('#\{(?>CONST:([\w\|]+))\}#e','(isset(\$this->Templ_Consts("$1"))) ? \$this->Templ_Consts("$1") : ""',$indata);
foxel@0
   293
foxel@0
   294
                    $QF->Events->Call_Event_Ref('EJS_PreParse', $indata);
foxel@0
   295
foxel@0
   296
                    $JSData = $this->Prepare_EJS($indata);
foxel@0
   297
                    $this->JS_data.= VIS_BR.$JSData;
foxel@0
   298
foxel@0
   299
                    $QF->Cache->Set($cachename, $JSData );
foxel@0
   300
                    $QF->Timer->Time_Log('"'.$name.'" JScript loaded (from EJS file)');
foxel@0
   301
                }
foxel@0
   302
                else
foxel@0
   303
                    trigger_error('VIS: error loading "'.$name.'" EJS file', E_USER_WARNING );
foxel@0
   304
            }
foxel@0
   305
foxel@0
   306
            $this->JS_loaded[] = $name;
foxel@0
   307
        }
foxel@0
   308
        return true;
foxel@0
   309
    }
foxel@0
   310
foxel@0
   311
    function Load_Templates($part = '', $force_style = false)
foxel@0
   312
    {
foxel@0
   313
        Global $QF;
foxel@0
   314
foxel@0
   315
        if (!$part)
foxel@0
   316
            $part = QF_KERNEL_VIS_COMMON;
foxel@0
   317
        else
foxel@0
   318
            $part = strtolower(preg_replace('#\W#', '_', $part));
foxel@0
   319
foxel@0
   320
        if (!in_array($part, $this->VIS_loaded))
foxel@0
   321
        {
foxel@0
   322
            if (!in_array($part, $this->LNG_loaded))
foxel@0
   323
                $QF->LNG->Load_Language($part);
foxel@0
   324
foxel@0
   325
            $cachename = QF_KERNEL_VIS_VPREFIX.$this->style_name.'.'.$this->lang_name.'.'.$part;
foxel@0
   326
foxel@0
   327
            if (list($Tdata, $VCSS, $VJS) = $QF->Cache->Get($cachename))
foxel@0
   328
            {
foxel@0
   329
                $this->templates += $Tdata;
foxel@0
   330
                $this->VCSS_data .= VIS_BR.$VCSS;
foxel@0
   331
                $this->VJS_data  .= VIS_BR.$VJS;
foxel@0
   332
foxel@0
   333
                // not used for now - not needed
foxel@0
   334
                //if ($part == QF_KERNEL_VIS_COMMON && $add_Consts = qf_file_load_datafile(QF_STCNTS_DIR.QF_KERNEL_VIS_COMMON.'.cnt', true))
foxel@0
   335
                //    $this->Consts += $add_Consts;
foxel@0
   336
foxel@0
   337
                $QF->Timer->Time_Log('"'.$part.'" visuals loaded (from global cache)');
foxel@0
   338
            }
foxel@0
   339
            else
foxel@0
   340
            {
foxel@0
   341
                $file = $part.'.vis';
foxel@0
   342
                $style = ($force_style)
foxel@0
   343
                    ? $force_style
foxel@0
   344
                    : $this->style_name;
foxel@0
   345
                $cfile = QF_STYLES_DIR.$style.'/'.$file;
foxel@0
   346
                if (!file_exists($cfile))
foxel@0
   347
                {
foxel@0
   348
                    $style = QF_KERNEL_VIS_DEFSTYLE;
foxel@0
   349
                    $cfile = QF_STYLES_DIR.$style.'/'.$file;
foxel@0
   350
                }
foxel@0
   351
foxel@0
   352
                if (!file_exists($cfile))
foxel@0
   353
                {
foxel@0
   354
                    trigger_error('VIS: there is no '.$this->style_name.'.'.$part.' VIS file', E_USER_WARNING );
foxel@0
   355
                }
foxel@0
   356
                elseif ($indata = qf_file_get_contents($cfile))
foxel@0
   357
                {
foxel@0
   358
                    // not used for now - not needed
foxel@34
   359
                    //if ($add_Consts = qf_file_load_datafile(QF_STCNTS_DIR.$part.'.cnt', true))
foxel@0
   360
                    //    $this->Consts += $add_Consts;
foxel@0
   361
                    $QF->Events->Call_Event_Ref('VIS_RawParse', $indata, $style, $part);
foxel@0
   362
foxel@0
   363
                    $indata = str_replace('{ST_IMGS}', QF_ST_IMGS_DIR.$style, $indata);
foxel@0
   364
                    $indata = str_replace('{IMGS}', QF_IMAGES_DIR, $indata);
foxel@0
   365
                    $indata = str_replace('{STATICS}', qf_full_url(QF_STATICS_DIR), $indata);
foxel@0
   366
foxel@0
   367
                    $indata = preg_replace_callback('#\{(?>L_((?:\w+|\"[^\"]+\"|\|)+))\}#',Array(&$this, '_Templ_Lang_CB'),$indata);
foxel@34
   368
                    $indata = preg_replace('#\{(?>CONST:([\w\|]+))\}#e','(isset(\$this->Templ_Consts("$1"))) ? \$this->Templ_Consts("$1") : ""',$indata);
foxel@0
   369
foxel@0
   370
                    $QF->Events->Call_Event_Ref('VIS_PreParse', $indata, $style, $part);
foxel@0
   371
foxel@0
   372
                    preg_match_all("#<<\+ '(?>(\w+))'>>(.*?)<<- '\\1'>>#s", $indata, $blocks);
foxel@0
   373
foxel@0
   374
                    if (is_array($blocks[1]))
foxel@0
   375
                    {
foxel@0
   376
                        $Tdata  = Array();
foxel@0
   377
                        $VCSS   = '';
foxel@0
   378
                        $VJS    = '';
foxel@0
   379
                        foreach ($blocks[1] as $num => $name)
foxel@0
   380
                        {
foxel@0
   381
                            $templ = $blocks[2][$num];
foxel@0
   382
foxel@0
   383
                            if ($name == 'CSS')
foxel@0
   384
                                $VCSS.= $this->Prepare_ECSS($templ);
foxel@0
   385
                            elseif ($name == 'JS')
foxel@0
   386
                                $VJS.= $templ; // EJS can contain {V_ links
foxel@0
   387
                                               // so we need to store it first and parse after VIS loading
foxel@0
   388
                            else // normal VIS
foxel@0
   389
                                $Tdata[$name] = $this->Prepare_VIS($templ);
foxel@0
   390
                        }
foxel@0
   391
foxel@0
   392
                        $this->templates += $Tdata;
foxel@0
   393
                        $this->VCSS_data .= VIS_BR.$VCSS;
foxel@0
   394
                        $VJS = $this->Prepare_EJS($VJS);
foxel@0
   395
                        $this->VJS_data  .= VIS_BR.$VJS;
foxel@0
   396
foxel@0
   397
                        $QF->Cache->Set($cachename, Array($Tdata, $VCSS, $VJS) );
foxel@0
   398
                        $QF->Timer->Time_Log('"'.$part.'" visuals loaded (from VIS file)');
foxel@0
   399
                    }
foxel@0
   400
                    else
foxel@0
   401
                        trigger_error('VIS: error parsing "'.$part.'" VIS file for style "'.$this->style_name.'"', E_USER_WARNING );
foxel@0
   402
foxel@0
   403
                }
foxel@0
   404
                else
foxel@0
   405
                    trigger_error('VIS: error loading "'.$part.'" VIS file for style "'.$this->style_name.'"', E_USER_WARNING );
foxel@0
   406
            }
foxel@0
   407
foxel@0
   408
            $this->VIS_loaded[] = $part;
foxel@0
   409
foxel@0
   410
        }
foxel@0
   411
        return true;
foxel@0
   412
    }
foxel@0
   413
foxel@0
   414
    // parsing functions
foxel@0
   415
    function Parse($node = 0)
foxel@0
   416
    {
foxel@0
   417
        if (!is_int($node))
foxel@0
   418
            $node = $this->Find_Node($node);
foxel@0
   419
foxel@0
   420
        if (is_null($node))
foxel@0
   421
            return false;
foxel@0
   422
foxel@0
   423
        if (isset($this->parsed[$node]))
foxel@0
   424
            return $this->parsed[$node];
foxel@0
   425
foxel@0
   426
        if (!isset($this->node_type[$node]))
foxel@0
   427
        {
foxel@0
   428
            trigger_error('VIS: trying to parse a fake node', E_USER_WARNING);
foxel@0
   429
            return false;
foxel@0
   430
        }
foxel@0
   431
foxel@0
   432
        $type =& $this->node_type[$node];
foxel@0
   433
        $vars =& $this->node_vars[$node];
foxel@0
   434
        $subs =& $this->node_subs[$node];
foxel@0
   435
        $flags =& $this->node_flags[$node];
foxel@0
   436
        $data = Array();
foxel@0
   437
        $text = '';
foxel@0
   438
foxel@0
   439
        if ($flags && QF_VISNODE_ARRAY)
foxel@0
   440
        {
foxel@0
   441
            $parts = Array();
foxel@0
   442
            $delimiter = (isset($vars['_DELIM'])) ? $vars['_DELIM'] : '';
foxel@0
   443
            foreach ($vars as $data)
foxel@0
   444
                if (is_array($data))
foxel@0
   445
                    $parts[] = $this->Parse_VIS($type, $data);
foxel@0
   446
            $text = implode($delimiter, $parts);
foxel@0
   447
        }
foxel@0
   448
        else
foxel@0
   449
        {
foxel@0
   450
            foreach ($subs as $var => $subnodes)
foxel@0
   451
                foreach ($subnodes as $subnode)
foxel@0
   452
                    $vars[$var][] = $this->Parse($subnode);
foxel@0
   453
foxel@0
   454
            foreach ($vars as $var => $vals)
foxel@0
   455
                $data[$var] = implode(VIS_BR, $vals);
foxel@0
   456
foxel@0
   457
            $text = $this->Parse_VIS($type, $data);
foxel@0
   458
        }
foxel@0
   459
foxel@0
   460
        if ($node>0)
foxel@0
   461
        {
foxel@0
   462
            unset($this->node_type[$node],
foxel@0
   463
                $this->node_vars[$node],
foxel@0
   464
                $this->node_subs[$node],
foxel@0
   465
                $this->node_flags[$node]);
foxel@0
   466
foxel@0
   467
            $this->parsed[$node] =& $text;
foxel@0
   468
        }
foxel@0
   469
foxel@0
   470
        return $text;
foxel@0
   471
    }
foxel@0
   472
foxel@0
   473
    function Make_HTML()
foxel@0
   474
    {
foxel@0
   475
        if ($this->force_append)
foxel@0
   476
        {
foxel@0
   477
            if (!$this->CSS_loaded)
foxel@0
   478
                $this->load_ECSS();
foxel@0
   479
            if (!in_array(QF_KERNEL_VIS_COMMON, $this->JS_loaded))
foxel@0
   480
                $this->load_EJS();
foxel@0
   481
        }
foxel@0
   482
foxel@0
   483
        if (!count($this->VIS_loaded))
foxel@0
   484
            $this->Load_Templates();
foxel@0
   485
foxel@0
   486
        $type =& $this->node_type[0];
foxel@0
   487
        $vars =& $this->node_vars[0];
foxel@0
   488
        $subs =& $this->node_subs[0];
foxel@0
   489
        $data = Array();
foxel@0
   490
        $text = '';
foxel@0
   491
        $vars['CSS'][] = trim($this->CSS_data.VIS_BR.$this->VCSS_data);
foxel@0
   492
        $vars['JS'][]  = trim($this->JS_data.VIS_BR.$this->VJS_data);
foxel@0
   493
foxel@0
   494
        foreach ($subs as $var => $subnodes)
foxel@0
   495
            foreach ($subnodes as $subnode)
foxel@0
   496
                $vars[$var][] = $this->Parse($subnode);
foxel@0
   497
foxel@0
   498
        foreach ($vars as $var => $vals)
foxel@0
   499
            $data[$var] = implode(VIS_BR, $vals);
foxel@0
   500
foxel@0
   501
foxel@0
   502
        $text = $this->Parse_VIS($type, $data);
foxel@0
   503
foxel@0
   504
        return $text;
foxel@0
   505
    }
foxel@0
   506
foxel@0
   507
    function Make_CSS()
foxel@0
   508
    {
foxel@0
   509
        if (!$this->CSS_loaded)
foxel@0
   510
            $this->load_ECSS();
foxel@0
   511
        return trim($this->CSS_data);
foxel@0
   512
    }
foxel@0
   513
foxel@0
   514
    function Make_JS()
foxel@0
   515
    {
foxel@0
   516
        if (!$this->JS_loaded)
foxel@0
   517
            $this->load_EJS();
foxel@0
   518
        return trim($this->JS_data);
foxel@0
   519
    }
foxel@0
   520
foxel@0
   521
    // tree construction functions
foxel@0
   522
    function Create_Node($template, $data_arr = false, $globname = null)
foxel@0
   523
    {
foxel@0
   524
        $template = (string) $template;
foxel@0
   525
        if (!$template)
foxel@0
   526
            return false;
foxel@0
   527
foxel@0
   528
        end($this->node_type);
foxel@0
   529
        $id = key($this->node_type) + 1;
foxel@0
   530
        $this->node_type[$id] = $template;
foxel@0
   531
        $this->node_vars[$id] = Array();
foxel@0
   532
        $this->node_subs[$id] = Array();
foxel@0
   533
        $this->node_flags[$id] = 0;
foxel@0
   534
foxel@0
   535
        if (is_array($data_arr))
foxel@0
   536
            foreach ($data_arr as $key => $var)
foxel@0
   537
            {
foxel@0
   538
                $key = strtoupper($key);
foxel@0
   539
                if (is_array($var))
foxel@0
   540
                    $var = implode(' ', $var);
foxel@0
   541
                $this->node_vars[$id][$key][] = $var;
foxel@0
   542
            }
foxel@0
   543
foxel@0
   544
        if ($globname && !is_numeric($globname))
foxel@0
   545
        {
foxel@0
   546
            $globname = strtoupper($globname);
foxel@0
   547
            $this->named_node[$globname] = $id;
foxel@0
   548
        }
foxel@0
   549
foxel@0
   550
        return $id;
foxel@0
   551
    }
foxel@0
   552
foxel@0
   553
    function Append_Node($node_id, $varname, $parent = 0)
foxel@0
   554
    {
foxel@0
   555
        if (!is_int($parent))
foxel@0
   556
            $parent = $this->Find_Node($parent);
foxel@0
   557
        if (!is_int($node_id))
foxel@0
   558
            $node_id = $this->Find_Node($node_id);
foxel@0
   559
foxel@0
   560
        $varname  = strtoupper($varname);
foxel@0
   561
foxel@0
   562
        if (!$varname)
foxel@0
   563
            return false;
foxel@0
   564
foxel@0
   565
        if (!isset($this->node_type[$parent]))
foxel@0
   566
        {
foxel@0
   567
            trigger_error('VIS: trying to append data to fake node', E_USER_WARNING);
foxel@0
   568
            return false;
foxel@0
   569
        }
foxel@0
   570
        if (!isset($this->node_type[$node_id]))
foxel@0
   571
        {
foxel@0
   572
            trigger_error('VIS: trying to append a fake node', E_USER_WARNING);
foxel@0
   573
            return false;
foxel@0
   574
        }
foxel@0
   575
foxel@0
   576
        $this->node_subs[$parent][$varname][] = $node_id;
foxel@0
   577
        return true;
foxel@0
   578
    }
foxel@0
   579
foxel@0
   580
    function Add_Node($template, $varname, $parent = 0, $data_arr = false, $globname = null)
foxel@0
   581
    {
foxel@0
   582
        if (!is_int($parent))
foxel@0
   583
            $parent = $this->Find_Node($parent);
foxel@0
   584
foxel@0
   585
        $varname  = strtoupper($varname);
foxel@0
   586
foxel@0
   587
        if (!$varname)
foxel@0
   588
            return false;
foxel@0
   589
foxel@0
   590
        if (!isset($this->node_type[$parent]))
foxel@0
   591
        {
foxel@0
   592
            trigger_error('VIS: trying to append data to fake node', E_USER_WARNING);
foxel@0
   593
            return false;
foxel@0
   594
        }
foxel@0
   595
foxel@0
   596
        if ($id = $this->Create_Node($template, $data_arr, $globname))
foxel@0
   597
        {
foxel@0
   598
            $this->node_subs[$parent][$varname][] = $id;
foxel@0
   599
            return $id;
foxel@0
   600
        }
foxel@0
   601
foxel@0
   602
        return false;
foxel@0
   603
    }
foxel@0
   604
foxel@0
   605
    // Adds arrayed node
foxel@0
   606
    function Add_Node_Array($template, $varname, $parent = 0, $data_arr = false, $delimiter = false)
foxel@0
   607
    {
foxel@0
   608
        if (!is_int($parent))
foxel@0
   609
            $parent = $this->Find_Node($parent);
foxel@0
   610
foxel@0
   611
        $varname  = strtoupper($varname);
foxel@0
   612
foxel@0
   613
        if (!$varname)
foxel@0
   614
            return false;
foxel@0
   615
foxel@0
   616
        if (!isset($this->node_type[$parent]))
foxel@0
   617
        {
foxel@0
   618
            trigger_error('VIS: trying to append data to fake node', E_USER_WARNING);
foxel@0
   619
            return false;
foxel@0
   620
        }
foxel@0
   621
foxel@0
   622
        if ($id = $this->Create_Node($template))
foxel@0
   623
        {
foxel@0
   624
            $this->node_subs[$parent][$varname][] = $id;
foxel@0
   625
            $this->node_flags[$id] = QF_VISNODE_ARRAY;
foxel@0
   626
foxel@0
   627
            if (is_array($data_arr) && count($data_arr))
foxel@0
   628
            {
foxel@0
   629
              $in = 0;
foxel@0
   630
              foreach ($data_arr as $arr)
foxel@0
   631
                if (is_array($arr))
foxel@0
   632
                {
foxel@0
   633
                  foreach ($arr as $key => $var)
foxel@0
   634
                  {
foxel@0
   635
                      $key = strtoupper($key);
foxel@0
   636
                      if (is_array($var))
foxel@0
   637
                          $var = implode(' ', $var);
foxel@0
   638
                      $this->node_vars[$id][$in][$key] = $var;
foxel@0
   639
                  }
foxel@0
   640
foxel@0
   641
                  $this->node_vars[$id][$in]['_POS'] = $in;
foxel@0
   642
foxel@0
   643
                  $in++;
foxel@0
   644
                }
foxel@0
   645
foxel@0
   646
                $this->node_vars[$id][0]['_IS_FIRST'] = '1';
foxel@0
   647
                $this->node_vars[$id][$in-1]['_IS_LAST'] = '1';
foxel@0
   648
            }
foxel@0
   649
foxel@0
   650
            if (strlen($delimiter))
foxel@0
   651
                $this->node_vars[$id]['_DELIM'] = (string) $delimiter;
foxel@0
   652
            return $id;
foxel@0
   653
        }
foxel@0
   654
foxel@0
   655
        return false;
foxel@0
   656
    }
foxel@0
   657
foxel@0
   658
    function Add_Data($node, $varname, $data)
foxel@0
   659
    {
foxel@0
   660
        if (is_null($node))
foxel@0
   661
            return false;
foxel@0
   662
foxel@0
   663
        $varname = strtoupper($varname);
foxel@0
   664
foxel@0
   665
        if (!$varname)
foxel@0
   666
            return false;
foxel@0
   667
foxel@0
   668
        if (!isset($this->node_type[$node]) || ($this->node_flags[$node] && QF_VISNODE_ARRAY))
foxel@0
   669
        {
foxel@0
   670
            trigger_error('VIS: trying to append data to fake node', E_USER_WARNING);
foxel@0
   671
            return false;
foxel@0
   672
        }
foxel@0
   673
foxel@0
   674
        if (is_array($data))
foxel@0
   675
            $data = implode(' ', $data);
foxel@0
   676
        $this->node_vars[$node][$varname][] = $data;
foxel@0
   677
foxel@0
   678
        return true;
foxel@0
   679
    }
foxel@0
   680
foxel@0
   681
    function Add_Data_Array($node, $arr)
foxel@0
   682
    {
foxel@0
   683
        if (is_null($node))
foxel@0
   684
            return false;
foxel@0
   685
foxel@0
   686
        if (!isset($this->node_type[$node]) || ($this->node_flags[$node] && QF_VISNODE_ARRAY))
foxel@0
   687
        {
foxel@0
   688
            trigger_error('VIS: trying to append data to fake node', E_USER_WARNING);
foxel@0
   689
            return false;
foxel@0
   690
        }
foxel@0
   691
foxel@0
   692
        if (!is_array($arr))
foxel@0
   693
            return false;
foxel@0
   694
foxel@0
   695
        foreach ($arr as $key => $var)
foxel@0
   696
        {
foxel@0
   697
            $key = strtoupper($key);
foxel@0
   698
            if (is_array($var))
foxel@0
   699
                $var = implode(' ', $var);
foxel@0
   700
            $this->node_vars[$node][$key][] = $var;
foxel@0
   701
        }
foxel@0
   702
foxel@0
   703
        return true;
foxel@0
   704
    }
foxel@0
   705
foxel@0
   706
    function Find_Node($to_find)
foxel@0
   707
    {
foxel@0
   708
        if (!$to_find)
foxel@0
   709
            return 0;
foxel@0
   710
foxel@0
   711
        if (!is_numeric($to_find))
foxel@0
   712
        {
foxel@0
   713
            $to_find = strtoupper($to_find);
foxel@0
   714
            if (isset($this->named_node[$to_find]))
foxel@0
   715
                $to_find = $this->named_node[$to_find];
foxel@0
   716
            else
foxel@0
   717
                return null;
foxel@0
   718
        }
foxel@0
   719
foxel@0
   720
        $to_find = (int) $to_find;
foxel@0
   721
        return $to_find;
foxel@0
   722
    }
foxel@0
   723
foxel@0
   724
foxel@0
   725
    // Inner parsing functions
foxel@0
   726
foxel@0
   727
    // reparses {L_...} blocks in raw templates
foxel@0
   728
    function _Templ_Lang_CB($matches)
foxel@0
   729
    {
foxel@0
   730
        $code = $matches[1];
foxel@0
   731
        $code = explode('|', $code);
foxel@0
   732
foxel@0
   733
        if (!($lng = strtoupper($code[0])))
foxel@0
   734
            return '';
foxel@0
   735
        if (!isset($this->lang[$lng]))
foxel@0
   736
            return '';
foxel@0
   737
foxel@0
   738
        $data = $this->lang[$lng];
foxel@0
   739
        if (count($code)>1)
foxel@0
   740
        {
foxel@0
   741
            $params = array_slice($code, 1);
foxel@0
   742
            foreach ($params as $id => $val)
foxel@0
   743
                $params[$id] = ($val{0} == '"') ? substr($val, 1, -1) : '{'.$val.'}';
foxel@0
   744
foxel@0
   745
            $data = qf_sprintf_arr($data, $params);
foxel@0
   746
        }
foxel@0
   747
foxel@0
   748
        return $data;
foxel@0
   749
    }
foxel@0
   750
foxel@0
   751
    function Templ_Consts($code)
foxel@0
   752
    {
foxel@0
   753
        $code = explode('|', $code);
foxel@0
   754
foxel@0
   755
        if (!($id = strtoupper($code[0])))
foxel@0
   756
            return '#';
foxel@0
   757
        if (!isset($this->Consts[$id]))
foxel@0
   758
            return '#';
foxel@0
   759
foxel@0
   760
        $data = $this->Consts[$id];
foxel@0
   761
        if (count($code)>1)
foxel@0
   762
        {
foxel@0
   763
            $params = array_slice($code, 1);
foxel@0
   764
            foreach ($params as $id => $val)
foxel@0
   765
                $params[$id] = (is_numeric($val{0})) ? (int) $val : '{'.$val.'}';
foxel@0
   766
            $data = qf_sprintf_arr($data, $params);
foxel@0
   767
        }
foxel@0
   768
foxel@0
   769
        return $data;
foxel@0
   770
    }
foxel@0
   771
foxel@0
   772
    function Prepare_VIS($text)
foxel@0
   773
    {
foxel@0
   774
        global $QF;
foxel@0
   775
foxel@0
   776
        static $consts = Array(
foxel@0
   777
            'QF_MARK'  => 'Powered by<br />QuickFox 2<br />&copy; Foxel aka LION<br /> 2006 - 2009',
foxel@0
   778
            'QF_INDEX' => QF_INDEX,
foxel@6
   779
            );
foxel@0
   780
foxel@0
   781
        $consts['QF_ROOT'] = $QF->HTTP->RootUrl;
foxel@0
   782
        $text = trim($text);
foxel@0
   783
foxel@0
   784
        if ($this->force_compact)
foxel@0
   785
            $text = $this->VIS_compact($text);
foxel@0
   786
foxel@0
   787
        $text = preg_replace('#(?<=\})\n\s*?(?=\{\w)#', '', $text);
foxel@0
   788
        preg_match_all('#\{(\!?)((?>\w+))(?:\:((?:(?>\w+)(?:[\!=\>\<]{1,2}(?:\w+|\"[^\"]*\"))?|\||)*))?\}|[^\{]+|\{#', $text, $struct, PREG_SET_ORDER);
foxel@0
   789
foxel@0
   790
        $writes_to = '$OUT';
foxel@0
   791
        $text = $writes_to.' = <<<QFT'.VIS_BR;
foxel@0
   792
        $vars = Array();
foxel@0
   793
foxel@0
   794
        $iflevel = 0;
foxel@0
   795
        $outiflevel = 0;
foxel@0
   796
        $in_for = false;
foxel@0
   797
foxel@0
   798
        $keys = array_keys($struct);
foxel@0
   799
foxel@0
   800
        foreach ($keys as $key)
foxel@0
   801
        {
foxel@0
   802
            $part =& $struct[$key];
foxel@0
   803
foxel@0
   804
            if (isset($part[2]) && ($tag = strtoupper($part[2])))
foxel@0
   805
            {
foxel@0
   806
                $got_a = ($part[1]) ? true : false;
foxel@0
   807
foxel@0
   808
                $params = Array();
foxel@0
   809
                if (isset($part[3]) && ($got = preg_match_all('#((?>\w+))(?:([\!=\>\<]{1,2})(\w+|\"[^\"]*\"))?#', $part[3], $params, PREG_PATTERN_ORDER)))
foxel@0
   810
                    for ($i = 0; $i < $got; $i++)
foxel@0
   811
                        $params[1][$i] = strtoupper($params[1][$i]);
foxel@0
   812
foxel@0
   813
                if ($tag == 'WRITE')
foxel@0
   814
                {
foxel@0
   815
                    if (isset($params[1]) && count($params[1]) && ($var = $params[1][0]) && !is_numeric($var{0}))
foxel@0
   816
                        $var = '$'.$var;
foxel@0
   817
                    else
foxel@0
   818
                        $var = '$OUT';
foxel@0
   819
                    if ($var != $writes_to)
foxel@0
   820
                    {
foxel@0
   821
                        $writes_to = $var;
foxel@0
   822
                        $text.= VIS_BR.'QFT;'.VIS_BR.$writes_to.(($got_a) ? '' : '.').'= <<<QFT'.VIS_BR;
foxel@0
   823
                    }
foxel@0
   824
                }
foxel@0
   825
                elseif (isset($this->func_parsers[$tag])) //parsing the variable with func
foxel@0
   826
                {
foxel@0
   827
                    $func_parser = $this->func_parsers[$tag];
foxel@0
   828
foxel@0
   829
                    if (!isset($params[1]) || !count($params[1]))
foxel@0
   830
                        continue;
foxel@0
   831
                    $pars = count($params[1]);
foxel@0
   832
                    $needFunc = false;
foxel@0
   833
                    $realPars = Array();
foxel@0
   834
                    $varPars = Array();
foxel@0
   835
                    for ($i = 0; $i < $pars; $i++)
foxel@0
   836
                    {
foxel@0
   837
                        $val = $params[1][$i];
foxel@0
   838
foxel@0
   839
                        if (is_numeric($val{0}))
foxel@0
   840
                            $text.= qf_heredoc_addslashes(qf_func_call($func_parser, intval($val)), 'QFT');
foxel@0
   841
                        elseif ($val{0} == '"')
foxel@0
   842
                            $text.= qf_heredoc_addslashes(qf_func_call($func_parser, substr($val, 1, -1)), 'QFT');
foxel@0
   843
                        else
foxel@0
   844
                        {
foxel@0
   845
                            $val = strtoupper($val);
foxel@0
   846
                            if (substr($val, 0, 2) == 'L_')
foxel@0
   847
                                $text.= qf_heredoc_addslashes(qf_func_call($func_parser, $this->_Templ_Lang_CB(Array(1 => substr($val, 2)))), 'QFT');
foxel@0
   848
                            elseif (isset($consts[$val]))
foxel@0
   849
                                $text.= qf_heredoc_addslashes(qf_func_call($func_parser, $consts[$val]), 'QFT');
foxel@0
   850
                            else
foxel@0
   851
                            {
foxel@0
   852
                                $vars[$val] = '';
foxel@0
   853
                                $text.= VIS_BR.'QFT'.VIS_BR.'.$this->Do_Func(\''.$tag.'\', $'.$val.').<<<QFT'.VIS_BR;
foxel@0
   854
                            }
foxel@0
   855
                        }
foxel@0
   856
                    }
foxel@0
   857
                }
foxel@0
   858
                elseif ($tag == 'SET')
foxel@0
   859
                {
foxel@0
   860
                    if ($pars = count($params[1]))
foxel@0
   861
                    {
foxel@0
   862
                        $sets = '';
foxel@0
   863
                        for($i = 0; $i < $pars; $i++)
foxel@0
   864
                        {
foxel@0
   865
                            $var = $params[1][$i];
foxel@0
   866
                            if (is_numeric($var{0}) || !isset($params[3][$i]) && !strlen($params[3][$i]))
foxel@0
   867
                                continue;
foxel@0
   868
                            $val = $params[3][$i];
foxel@0
   869
                            $sets.= '$'.$var.' = ';
foxel@0
   870
foxel@0
   871
                            if (is_numeric($val{0}))
foxel@0
   872
                                $sets.= intval($val).';';
foxel@0
   873
                            elseif ($val{0} == '"')
foxel@0
   874
                                $sets.= '<<<QFT'.VIS_BR.qf_heredoc_addslashes(substr($val, 1, -1), 'QFT').VIS_BR.'QFT'.VIS_BR.';';
foxel@0
   875
                            else
foxel@0
   876
                            {
foxel@0
   877
                                $val = strtoupper($val);
foxel@0
   878
                                if (substr($val, 0, 2) == 'L_')
foxel@0
   879
                                    $sets.= '<<<QFT'.VIS_BR.qf_heredoc_addslashes($this->_Templ_Lang_CB(Array(1 => substr($val, 2))), 'QFT').VIS_BR.'QFT'.VIS_BR.';';
foxel@0
   880
                                elseif (isset($consts[$val]))
foxel@0
   881
                                    $sets.= '<<<QFT'.VIS_BR.qf_heredoc_addslashes($consts[$val], 'QFT').VIS_BR.'QFT'.VIS_BR.';';
foxel@0
   882
                                else
foxel@0
   883
                                {
foxel@0
   884
                                    $vars[$val] = '';
foxel@0
   885
                                    $sets.= '$'.$val.';';
foxel@0
   886
                                }
foxel@0
   887
                            }
foxel@0
   888
                        }
foxel@0
   889
foxel@0
   890
                        if ($sets)
foxel@0
   891
                            $text.= VIS_BR.'QFT;'.VIS_BR.$sets.VIS_BR.$writes_to.'.= <<<QFT'.VIS_BR;
foxel@0
   892
                    }
foxel@0
   893
                }
foxel@0
   894
                elseif ($tag == 'FOR')
foxel@0
   895
                {
foxel@0
   896
                    if (!isset($params[1]) || !count($params[1]))
foxel@0
   897
                        continue;
foxel@0
   898
                    $params = $params[1];
foxel@0
   899
                    $p1 = array_shift($params);
foxel@0
   900
                    $p2 = array_shift($params);
foxel@0
   901
                    $p3 = array_shift($params);
foxel@0
   902
foxel@0
   903
                    $p1 = (is_numeric($p1{0})) ? intval($p1) : '(int) $'.$p1.($vars[$p1] = '');
foxel@0
   904
                    if ($p2)
foxel@0
   905
                        $p2 = (is_numeric($p2{0})) ? intval($p2) : '(int) $'.$p2.($vars[$p2] = '');
foxel@0
   906
                    else
foxel@0
   907
                    {
foxel@0
   908
                        $p2 = $p1;
foxel@0
   909
                        $p1 = '0';
foxel@0
   910
                    }
foxel@0
   911
                    if ($p3)
foxel@0
   912
                        $p3 = (is_numeric($p3{0})) ? intval($p3) : '(int) $'.$p3.($vars[$p3] = '');
foxel@0
   913
                    else
foxel@0
   914
                        $p3 = '1';
foxel@0
   915
foxel@0
   916
                    $in_for = true;
foxel@0
   917
                    $outiflevel = $iflevel;
foxel@0
   918
                    $iflevel = 0;
foxel@0
   919
                    $text.= VIS_BR.'QFT;'.VIS_BR.'for ($I = '.$p1.'; $I <= '.$p2.'; $I+= '.$p3.') {'.VIS_BR.$writes_to.'.= <<<QFT'.VIS_BR;
foxel@0
   920
                }
foxel@0
   921
                elseif ($tag == 'ENDFOR')
foxel@0
   922
                {
foxel@0
   923
                    if ($in_for)
foxel@0
   924
                    {
foxel@0
   925
                        $text.= VIS_BR.'QFT;'.VIS_BR;
foxel@0
   926
                        $in_for = false;
foxel@0
   927
                        $text.= str_repeat('} ', $iflevel);
foxel@0
   928
                        $text.= VIS_BR.'}'.VIS_BR.$writes_to.'.= <<<QFT'.VIS_BR;
foxel@0
   929
                        $iflevel = $outiflevel;
foxel@0
   930
                    }
foxel@0
   931
                }
foxel@0
   932
                elseif ($tag == 'VIS')
foxel@0
   933
                {
foxel@0
   934
                    if (!isset($params[1]) || !count($params[1]))
foxel@0
   935
                        continue;
foxel@0
   936
                    $visname = $params[1][0];
foxel@0
   937
                    $text.= VIS_BR.'QFT;'.VIS_BR.$writes_to.(($got_a) ? '' : '.').'= $this->Parse_VIS(\''.$visname.'\'';
foxel@0
   938
                    if (count($params[1]) > 1)
foxel@0
   939
                    {
foxel@0
   940
                        $text.= ', Array(';
foxel@0
   941
                        $pars = count($params[1]);
foxel@0
   942
                        for($i = 1; $i < $pars; $i++)
foxel@0
   943
                        {
foxel@0
   944
                            $var = $params[1][$i];
foxel@0
   945
                            $val = (isset($params[3][$i]) && strlen($params[3][$i])) ? $params[3][$i] : '1';
foxel@0
   946
                            $text.= '\''.$var.'\' => ';
foxel@0
   947
foxel@0
   948
                            if (is_numeric($val{0}))
foxel@0
   949
                                $text.= intval($val).',';
foxel@0
   950
                            elseif ($val{0} == '"')
foxel@0
   951
                                $text.= '<<<QFT'.VIS_BR.qf_heredoc_addslashes(substr($val, 1, -1), 'QFT').VIS_BR.'QFT'.VIS_BR.',';
foxel@0
   952
                            else
foxel@0
   953
                            {
foxel@0
   954
                                $val = strtoupper($val);
foxel@0
   955
                                if (substr($val, 0, 2) == 'L_')
foxel@0
   956
                                    $text.= '<<<QFT'.VIS_BR.qf_heredoc_addslashes($this->_Templ_Lang_CB(Array(1 => substr($val, 2))), 'QFT').VIS_BR.'QFT'.VIS_BR.',';
foxel@0
   957
                                elseif (isset($consts[$val]))
foxel@0
   958
                                    $text.= '<<<QFT'.VIS_BR.qf_heredoc_addslashes($consts[$val], 'QFT').VIS_BR.'QFT'.VIS_BR.',';
foxel@0
   959
                                else
foxel@0
   960
                                {
foxel@0
   961
                                    $vars[$val] = '';
foxel@0
   962
                                    $text.= '$'.$val.',';
foxel@0
   963
                                }
foxel@0
   964
                            }
foxel@0
   965
                        }
foxel@0
   966
                        $text.= ') ';
foxel@0
   967
                    }
foxel@0
   968
                    $text.= ');'.VIS_BR.$writes_to.'.= <<<QFT'.VIS_BR;
foxel@0
   969
                }
foxel@0
   970
                elseif ($tag == 'IF' || $tag == 'ELSEIF')
foxel@0
   971
                {
foxel@0
   972
                    if (!isset($params[1]) || !count($params[1]))
foxel@0
   973
                        continue;
foxel@0
   974
                    $varname = $params[1][0];
foxel@0
   975
                    $vars[$varname] = '';
foxel@0
   976
                    if (isset($params[3][0]) && strlen($params[3][0]))
foxel@0
   977
                    {
foxel@0
   978
                        $condvar = $params[3][0];
foxel@0
   979
                        $condition = $params[2][0];
foxel@0
   980
                        switch ($condition)
foxel@0
   981
                        {
foxel@0
   982
                            case '>':
foxel@0
   983
                            case '<':
foxel@0
   984
                            case '>=':
foxel@0
   985
                            case '<=':
foxel@0
   986
                            case '!=':
foxel@0
   987
                                $condition = ' '.$condition.' ';
foxel@0
   988
                                break;
foxel@0
   989
                            default:
foxel@0
   990
                                $condition = ' == ';
foxel@0
   991
foxel@0
   992
                        }
foxel@0
   993
foxel@0
   994
                        if (is_numeric($condvar{0}))
foxel@0
   995
                            $condition = '((int) $'.$varname.$condition.intval($condvar).')';
foxel@0
   996
                        elseif ($condvar{0} == '"')
foxel@0
   997
                        {
foxel@0
   998
                            $condvar = substr($condvar, 1, -1);
foxel@0
   999
                            $condition = '($'.$varname.$condition.'\''.strtr($condvar, Array('\'' => '\\\'', '\\' => '\\\\')).'\')';
foxel@0
  1000
                        }
foxel@0
  1001
                        else
foxel@0
  1002
                            $condition = '($'.$varname.$condition.'(string) $'.$condvar.')';
foxel@0
  1003
                    }
foxel@0
  1004
                    else
foxel@0
  1005
                        $condition = 'strlen($'.$varname.')';
foxel@0
  1006
foxel@0
  1007
                    if ($got_a)
foxel@0
  1008
                        $condition = '!'.$condition;
foxel@0
  1009
foxel@0
  1010
                    if ($tag == 'IF')
foxel@0
  1011
                    {
foxel@0
  1012
                        $text.= VIS_BR.'QFT;'.VIS_BR.'if ('.$condition.') {'.VIS_BR.$writes_to.'.= <<<QFT'.VIS_BR;
foxel@0
  1013
                        $iflevel++;
foxel@0
  1014
                    }
foxel@0
  1015
                    elseif ($iflevel)
foxel@0
  1016
                        $text.= VIS_BR.'QFT;'.VIS_BR.'} elseif('.$condition.') {'.VIS_BR.$writes_to.'.= <<<QFT'.VIS_BR;
foxel@0
  1017
                }
foxel@0
  1018
                elseif ($tag == 'ELSE')
foxel@0
  1019
                {
foxel@0
  1020
                    if ($iflevel)
foxel@0
  1021
                        $text.= VIS_BR.'QFT;'.VIS_BR.'} elseif(true) {'.VIS_BR.$writes_to.'.= <<<QFT'.VIS_BR;
foxel@0
  1022
                }
foxel@0
  1023
                elseif ($tag == 'ENDIF')
foxel@0
  1024
                {
foxel@0
  1025
                    if ($iflevel)
foxel@0
  1026
                    {
foxel@0
  1027
                        $text.= VIS_BR.'QFT;'.VIS_BR.'}'.VIS_BR.$writes_to.'.= <<<QFT'.VIS_BR;
foxel@0
  1028
                        $iflevel--;
foxel@0
  1029
                    }
foxel@0
  1030
                }
foxel@0
  1031
                else
foxel@0
  1032
                {
foxel@0
  1033
                    $varname = $tag;
foxel@0
  1034
                    if (isset($consts[$varname]))
foxel@0
  1035
                        $text.= qf_heredoc_addslashes($consts[$varname], 'QFT');
foxel@0
  1036
                    elseif (!is_numeric($varname{0}))
foxel@0
  1037
                    {
foxel@0
  1038
                        $vars[$varname] = '';
foxel@0
  1039
                        if ($got_a)
foxel@0
  1040
                            $text.= VIS_BR.'QFT'.VIS_BR.'.qf_smart_htmlschars($'.$varname.').<<<QFT'.VIS_BR;
foxel@0
  1041
                        else
foxel@0
  1042
                            $text.= '{$'.$varname.'}';
foxel@0
  1043
                    }
foxel@0
  1044
                }
foxel@0
  1045
            }
foxel@0
  1046
            else
foxel@0
  1047
            {
foxel@0
  1048
                $text.= qf_heredoc_addslashes($part[0], 'QFT');
foxel@0
  1049
            }
foxel@0
  1050
foxel@0
  1051
            unset($struct[$key]);
foxel@0
  1052
        }
foxel@0
  1053
foxel@0
  1054
        $text.= VIS_BR.'QFT;'.VIS_BR;
foxel@0
  1055
foxel@0
  1056
        $text.= str_repeat('} ', $iflevel);
foxel@0
  1057
        if ($in_for)
foxel@0
  1058
            $text.= str_repeat('} ', $outiflevel+1);
foxel@0
  1059
foxel@0
  1060
        $text = preg_replace('#\$\w+\.\=\s*\<\<\<QFT\s+QFT;#', '', $text);
foxel@0
  1061
        return Array('T' => $text, 'V' => $vars);
foxel@0
  1062
    }
foxel@0
  1063
foxel@0
  1064
    function ____PreReplace_VISes($VISes) //RAW VISes including... TODO
foxel@0
  1065
    {
foxel@0
  1066
        foreach ($VISes as $id => $text)
foxel@0
  1067
        {
foxel@0
  1068
            preg_match_all('#\{\!?VIS(?:\:((?:(?>\w+)(?:[\!=\>\<]{1,2}(?:\w+|\"[^\"]*\"))?|\||)*))?\}|[^\{]+|\{#', $text, $struct, PREG_SET_ORDER);
foxel@0
  1069
foxel@0
  1070
            $text = '';
foxel@0
  1071
            $keys = array_keys($struct);
foxel@0
  1072
foxel@0
  1073
            foreach ($keys as $key)
foxel@0
  1074
            {
foxel@0
  1075
                $part =& $struct[$key];
foxel@0
  1076
foxel@0
  1077
                if (isset($part[2]) && ($tag = strtoupper($part[2])))
foxel@0
  1078
                {
foxel@0
  1079
                    $got_a = ($part[1]) ? true : false;
foxel@0
  1080
                    $params = Array();
foxel@0
  1081
                    if (isset($part[3]) && ($got = preg_match_all('#((?>\w+))(?:([\!=\>\<]{1,2})(\w+|\"[^\"]*\"))?#', $part[3], $params, PREG_PATTERN_ORDER)))
foxel@0
  1082
                        for ($i = 0; $i < $got; $i++)
foxel@0
  1083
                            $params[1][$i] = strtoupper($params[1][$i]);
foxel@0
  1084
                }
foxel@0
  1085
            }
foxel@0
  1086
        }
foxel@0
  1087
    }
foxel@0
  1088
foxel@0
  1089
    function Do_Func($func_name, $data) //parsing data with funcParser
foxel@0
  1090
    {
foxel@0
  1091
        if (!isset($this->func_parsers[$func_name]))
foxel@0
  1092
            return $data;
foxel@0
  1093
foxel@0
  1094
        $func_parser = $this->func_parsers[$func_name];
foxel@0
  1095
        return qf_func_call($func_parser, $data);
foxel@0
  1096
    }
foxel@0
  1097
foxel@0
  1098
    function Parse_VIS($vis, $data = Array())
foxel@0
  1099
    {
foxel@0
  1100
        Static $__COUNTER=1;
foxel@0
  1101
        Static $QF_SID;
foxel@0
  1102
        if (!$QF_SID)
foxel@0
  1103
        {
foxel@0
  1104
            global $QF;
foxel@0
  1105
            $QF_SID = $QF->Session->SID;
foxel@0
  1106
            unset($QF);
foxel@0
  1107
        }
foxel@0
  1108
foxel@0
  1109
        $COUNTER = $__COUNTER++;
foxel@0
  1110
        $RANDOM = dechex(rand(0x1FFF, getrandmax()));
foxel@0
  1111
foxel@0
  1112
        if (!isset($this->templates[$vis]))
foxel@0
  1113
            return implode(VIS_BR, $data);
foxel@0
  1114
foxel@0
  1115
        if (extract($this->templates[$vis]['V'], EXTR_SKIP))
foxel@0
  1116
        {
foxel@0
  1117
            extract($data, EXTR_OVERWRITE | EXTR_PREFIX_ALL, 'IN');
foxel@0
  1118
            extract($this->vis_consts, EXTR_OVERWRITE | EXTR_PREFIX_ALL, 'C');
foxel@0
  1119
        }
foxel@0
  1120
foxel@0
  1121
        $OUT = '';
foxel@0
  1122
foxel@0
  1123
        if (eval($this->templates[$vis]['T']) === false)
foxel@0
  1124
            trigger_error('VIS: error running "'.$vis.'" template. CODE ['.$this->templates[$vis]['T'].']', E_USER_ERROR);
foxel@0
  1125
foxel@0
  1126
        return $OUT;
foxel@0
  1127
    }
foxel@0
  1128
foxel@0
  1129
    function Prepare_ECSS($indata, $constants = null)
foxel@0
  1130
    {
foxel@0
  1131
        if (is_array($constants))
foxel@0
  1132
            foreach ($constants as $name=>$val)
foxel@0
  1133
                $indata = str_replace('{'.$name.'}', $val, $indata);
foxel@0
  1134
foxel@0
  1135
        $vars_mask='#\{((?>[\w\-]+))\}\s*=(.*)#';
foxel@0
  1136
        $vars_block='#\{VARS\}(.*?)\{/VARS\}#si';
foxel@0
  1137
foxel@0
  1138
        preg_match_all($vars_block, $indata, $blocks);
foxel@0
  1139
        $blocks = implode(' ', $blocks[0]);
foxel@0
  1140
foxel@0
  1141
        preg_match_all($vars_mask, $blocks, $sets);
foxel@0
  1142
        if (is_array($sets[1]))
foxel@0
  1143
            foreach ($sets[1] as $num => $name)
foxel@0
  1144
                $CSSVars[strtoupper($name)] = trim($sets[2][$num]);
foxel@0
  1145
foxel@0
  1146
        $Cdata = preg_replace($vars_block, '', $indata);
foxel@0
  1147
foxel@0
  1148
        if ($this->force_compact)
foxel@0
  1149
            $Cdata = $this->CSS_compact($Cdata);
foxel@0
  1150
foxel@0
  1151
        $Cdata = preg_replace('#\{(?>(\w+))\}#e', '(isset(\$CSSVars[strtoupper("\1")])) ? \$CSSVars[strtoupper("\1")] : ""', $Cdata);
foxel@0
  1152
foxel@0
  1153
        return $Cdata;
foxel@0
  1154
    }
foxel@0
  1155
foxel@0
  1156
    function Prepare_EJS($Jdata, $constants = null)
foxel@0
  1157
    {
foxel@0
  1158
        if (is_array($constants))
foxel@0
  1159
            foreach ($constants as $name=>$val)
foxel@0
  1160
                $Jdata = str_replace('{'.$name.'}', $val, $Jdata);
foxel@0
  1161
        if ($this->force_compact)
foxel@0
  1162
            $Jdata = $this->EJS_compact($Jdata);
foxel@0
  1163
        return $Jdata;
foxel@0
  1164
    }
foxel@0
  1165
foxel@0
  1166
    function EJS_compact($indata)
foxel@0
  1167
    {
foxel@0
  1168
        $indata = str_replace("\r", '', $indata);
foxel@0
  1169
        $indata = preg_replace('#^//.*?$#m', '', $indata);
foxel@0
  1170
        $indata = preg_replace('#(?<=\s)//.*?$#m', '', $indata);
foxel@0
  1171
        $indata = preg_replace('#(\n\s*)+#', "\n", $indata);
foxel@0
  1172
        $indata = preg_replace('#\n(.{1,5})$#m', ' \\1', $indata);
foxel@0
  1173
        $indata = str_replace("\n", VIS_BR, $indata);
foxel@0
  1174
        $indata = trim($indata);
foxel@0
  1175
        return $indata;
foxel@0
  1176
    }
foxel@0
  1177
foxel@0
  1178
    function CSS_compact($indata)
foxel@0
  1179
    {
foxel@0
  1180
        $indata = str_replace("\r", '', $indata);
foxel@0
  1181
        $indata = preg_replace('#/\*.+\*/#sU', '', $indata);
foxel@0
  1182
        $indata = preg_replace('#(\n\s*)+#', "\n", $indata);
foxel@0
  1183
        $indata = preg_replace('#\n(.{1,5})$#m', ' \\1', $indata);
foxel@0
  1184
        $indata = preg_replace('#\s*(,|:|;|\{)\s+#', '\\1 ', $indata);
foxel@0
  1185
        $indata = preg_replace('#\s+\}\s+#', " }\n", $indata);
foxel@0
  1186
        $indata = str_replace("\n", VIS_BR, $indata);
foxel@0
  1187
        $indata = trim($indata);
foxel@0
  1188
        return $indata;
foxel@0
  1189
    }
foxel@0
  1190
foxel@0
  1191
    function VIS_compact($indata)
foxel@0
  1192
    {
foxel@0
  1193
        $indata = str_replace("\r", '', $indata);
foxel@34
  1194
        $indata = preg_replace('#(\n\s*)+#', "\n", $indata);
foxel@34
  1195
        $indata = preg_replace('#\n(.{1,5})$#m', ' \\1', $indata);
foxel@34
  1196
        $indata = preg_replace('#\x20+#', ' ', $indata);
foxel@34
  1197
        $indata = str_replace("\n", VIS_BR, $indata);
foxel@34
  1198
        $indata = trim($indata);
foxel@34
  1199
        return $indata;
foxel@34
  1200
    }
foxel@34
  1201
}
foxel@34
  1202
foxel@34
  1203
foxel@34
  1204
?>