summaryrefslogtreecommitdiff
path: root/BitSmarty.php
blob: b85bef30f2a433a4f8632dd644efcc763bde0de5 (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
<?php
/**
 * Smarty Library Inteface Class
 *
 * @package Smarty
 * @version $Header$
 *
 * Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
 * All Rights Reserved. See below for details and a complete list of authors.
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details.
 */

/**
 * required setup
 */

require_once( dirname( __FILE__ ).'/smarty/libs/SmartyBC.class.php' ); $smartyClass = 'SmartyBC';
/*
if( file_exists( EXTERNAL_LIBS_PATH.'smarty/libs/Smarty.class.php' )) {
	   // set SMARTY_DIR that we have the absolute path
	   define( 'SMARTY_DIR', EXTERNAL_LIBS_PATH.'smarty/libs/' );
	   // If we have smarty in our kernel, use that.
	   $smartyIncFile = SMARTY_DIR . 'Smarty.class.php';
} elseif( file_exists( dirname( dirname( __FILE__ ) ).'/util/smarty/libs/Smarty.class.php' )) {
	   // set SMARTY_DIR that we have the absolute path
	   define( 'SMARTY_DIR',  dirname( dirname( __FILE__ ) ).'/util/smarty/libs/' );
	   // If we have smarty in our kernel, use that.
	   $smartyIncFile = SMARTY_DIR . 'Smarty.class.php';
} else {
	   // assume it is in php's global include_path
	   // don't set SMARTY_DIR if we are not using the bw copy
	   $smartyIncFile = 'Smarty.class.php';
}
require_once( $smartyIncFile );
*/


/**
 * PermissionCheck
 *
 * @package kernel
 */
class PermissionCheck {
	function check( $perm ) {
		global $gBitUser;
		return $gBitUser->hasPermission( $perm );
	}
}

/**
 * BitSmarty
 *
 * @package kernel
 */
class BitSmarty extends SmartyBC {

	protected $mCompileRsrc;

	/**
	 * BitSmarty initiation
	 *
	 * @access public
	 * @return void
	 */
	function __construct() {
		global $smarty_force_compile, $smarty_debugging;
		parent::__construct();
		$this->mCompileRsrc = NULL;
		$this->config_dir = "configs/";
		// $this->caching = TRUE;
		$this->force_compile = //$smarty_force_compile;
		$this->debugging = isset($smarty_debugging) && $smarty_debugging;
		$this->assign( 'app_name', 'bitweaver' );
		$this->addPluginsDir( THEMES_PKG_PATH . "smartyplugins" );
		$this->register_prefilter( "add_link_ticket" );
		$this->error_reporting = E_ALL & ~E_NOTICE;

		global $permCheck;
		$permCheck = new PermissionCheck();
// SMARTY3	$this->register_object( 'perm', $permCheck, array(), TRUE, array( 'autoComplete' ));
		$this->assign_by_ref( 'perm', $permCheck );
	}

	function scanPackagePluginDirs() {
		global $gBitSystem;
		foreach( $gBitSystem->mPackages as &$packageHash ) {
			if( $packageHash['dir'] != THEMES_PKG_DIR && file_exists( $packageHash['path'].'smartyplugins' ) ) {
				$this->addPluginsDir( $packageHash['path'].'smartyplugins' );
			}
		}
	}

	function addPluginsDir( $dir ) {
		$this->plugins_dir = array_merge( array( $dir ), $this->plugins_dir );
	}

    public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null, $display = false, $merge_tpl_vars = true, $no_output_filter = false) {
		global $gBitSystem;

		if( strpos( $template, ':' )) {
			list( $resource, $location ) = explode( ':', $template);
			if( $resource == 'bitpackage' ) {
				list( $package, $tpl ) = explode( '/', $location );
				// exclude temp, as it contains nexus menus
				if( !$gBitSystem->isPackageActive( $package ) && $package != 'temp' ) {
					return '';
				}
			}
		}
		return parent::fetch($template, $cache_id, $compile_id, $parent, $display, $merge_tpl_vars, $no_output_filter);
	}

	/**
	 * getModuleConfig
	 *
	 * @access public
	 * @return hash of config values set in sibling .cfg file
	 */
	function getModuleConfig( $pModuleRsrc ) {
		global $moduleConfig;
		$moduleConfig = array();
		$moduleConfigFile = str_replace( '.tpl', '.cfg', $pModuleRsrc );
		$this->includeSiblingFile( $moduleConfigFile );
		return $moduleConfig;
	}

	/**
	 * verifyCompileDir
	 *
	 * @access public
	 * @return void
	 */
	function verifyCompileDir() {
		global $gBitThemes, $gBitLanguage, $bitdomain;
		if( !defined( "TEMP_PKG_PATH" )) {
			$temp = BIT_ROOT_PATH . "temp/";
		} else {
			$temp = TEMP_PKG_PATH;
		}

		$endPath = $bitdomain.'/'.$gBitThemes->getStyle().'/'.$gBitLanguage->mLanguage;

		// Compile directory
		$compDir = $temp . "templates_c/$endPath";
		$compDir = str_replace( '//', '/', $compDir );
		$compDir = clean_file_path( $compDir );
		mkdir_p( $compDir );
		$this->setCompileDir( $compDir );

		// Cache directory
		$cacheDir = $temp . "cache/$endPath";
		$cacheDir = str_replace( '//', '/', $cacheDir );
		$cacheDir = clean_file_path( $cacheDir );
		mkdir_p( $cacheDir );
		$this->setCacheDir( $cacheDir );
	}
}

/**
 * add_link_ticket This will insert a ticket on all template URL's that have GET parameters.
 *
 * @param array $pTplSource source of template
 * @access public
 * @return ammended template source
 */
function add_link_ticket( $pTplSource ) {
	global $gBitUser;

	if( is_object( $gBitUser ) && $gBitUser->isRegistered() ) {
//		$from = '#href="(.*PKG_URL.*php)\?(.*)&(.*)"#i';
//		$to = 'href="\\1?\\2&amp;tk={$gBitUser->mTicket}&\\3"';
//		$pTplSource = preg_replace( $from, $to, $pTplSource );
		$from = '#<form([^>]*)>#i';
		// div tag is for stupid XHTML compliance.
		$to = '<form\\1><div style="display:inline"><input type="hidden" name="tk" value="{$gBitUser->mTicket}" /></div>';
		$pTplSource = preg_replace( $from, $to, $pTplSource );
		if( strpos( $pTplSource, '{form}' )) {
			$pTplSource = str_replace( '{form}', '{form}<div style="display:inline"><input type="hidden" name="tk" value="{$gBitUser->mTicket}" /></div>', $pTplSource );
		} elseif( strpos( $pTplSource, '{form ' ) ) {
			$from = '#\{form(\}| [^\}]*)\}#i';
			$to = '{form\\1}<div style="display:inline"><input type="hidden" name="tk" value="{$gBitUser->mTicket}" /></div>';
			$pTplSource = preg_replace( $from, $to, $pTplSource );
		}
	}

	return $pTplSource;
}
?>