Project

General

Profile

1 801 doc
<?php
2
3
// $Id$
4
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8 915 Ruebenwurz
 Copyright (C) 2004-2009, Ryan Djurovich
9 801 doc
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24
*/
25
26
/**
27 834 doc
	This file contains routines to edit the optional module files: frontend.css and backend.css
28 801 doc
	Mechanism was introduced with WB 2.7 to provide a global solution for all modules
29
	To use this function, include this file from your module (e.g. from modify.php)
30
	Then simply call the function edit_css('your_module_directory') - that?s it
31 834 doc
	NOTE: Some functions were added for module developers to make the creation of own module easier
32 801 doc
*/
33
34
// prevent this file from being accessed directly
35
if(!defined('WB_PATH')) die(header('Location: index.php'));
36
37 834 doc
/*
38
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
39
 FUNCTIONS REQUIRED TO EDIT THE OPTIONAL MODULE CSS FILES
40
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
41
*/
42
43 801 doc
// this function checks the validity of the specified module directory
44
if(!function_exists('check_module_dir')) {
45
	function check_module_dir($mod_dir) {
46
		// check if module directory is formal correct (only characters: "a-z,0-9,_,-")
47
		if(!preg_match('/^[a-z0-9_-]+$/iD', $mod_dir)) return '';
48
		// check if the module folder contains the required info.php file
49
		return (file_exists(WB_PATH .'/modules/' .$mod_dir .'/info.php')) ? $mod_dir : '';
50
	}
51
}
52
53
// this function checks if the specified optional module file exists
54
if (!function_exists('mod_file_exists')) {
55
	function mod_file_exists($mod_dir, $mod_file='frontend.css') {
56
  	// check if the module file exists
57
		return file_exists(WB_PATH .'/modules/' .$mod_dir .'/' .$mod_file);
58
	}
59
}
60
61
// this function displays the "Edit CSS" button in modify.php
62
if (!function_exists('edit_module_css')) {
63
	function edit_module_css($mod_dir) {
64 810 doc
		global $page_id, $section_id;
65
66 801 doc
		// check if the required edit_module_css.php file exists
67
		if(!file_exists(WB_PATH .'/modules/edit_module_files.php')) return;
68
69
		// check if specified module directory is valid
70
		if(check_module_dir($mod_dir) == '') return;
71
72
		// check if frontend.css or backend.css exist
73
		$frontend_css = mod_file_exists($mod_dir, 'frontend.css');
74
		$backend_css = mod_file_exists($mod_dir, 'backend.css');
75
76
		// output the edit CSS submtin button if required
77
		if($frontend_css || $backend_css) {
78 810 doc
			// default text used for the edit CSS routines if not defined in the WB core language files
79
			$edit_css_caption = (isset($GLOBALS['TEXT']['CAP_EDIT_CSS'])) ?$GLOBALS['TEXT']['CAP_EDIT_CSS'] :'Edit CSS';
80 801 doc
			?>
81
			<form name="edit_module_file" action="<?php echo WB_URL .'/modules/edit_module_files.php?page_id='.$page_id;?>"
82
				method="post" style="margin: 0; align:right;">
83 1069 Ruebenwurz
				<input type="hidden" name="page_id" value="<?php echo $page_id; ?>" />
84
				<input type="hidden" name="section_id" value="<?php echo $section_id; ?>" />
85
				<input type="hidden" name="mod_dir" value="<?php echo $mod_dir; ?>" />
86
				<input type="hidden" name="edit_file" value="<?php echo ($frontend_css) ?'frontend.css' : 'backend.css';?>" />
87
				<input type="hidden" name="action" value="edit" />
88
				<input type="submit" value="<?php echo $edit_css_caption;?>" class="mod_<?php echo $mod_dir;?>_edit_css" />
89 801 doc
			</form>
90
			<?php
91
    }
92
  }
93
}
94
95
// this function displays a button to toggle between CSS files (invoked from edit_css.php)
96
if (!function_exists('toggle_css_file')) {
97
	function toggle_css_file($mod_dir, $base_css_file = 'frontend.css') {
98
		global $page_id, $section_id;
99
		// check if the required edit_module_css.php file exists
100
		if(!file_exists(WB_PATH .'/modules/edit_module_files.php')) return;
101
102
		// check if specified module directory is valid
103
		if(check_module_dir($mod_dir) == '') return;
104
105
		// do sanity check of specified css file
106
		if(!in_array($base_css_file, array('frontend.css', 'backend.css'))) return;
107
108
		// display button to toggle between the two CSS files: frontend.css, backend.css
109
		$toggle_file = ($base_css_file == 'frontend.css') ? 'backend.css' : 'frontend.css';
110
		if(mod_file_exists($mod_dir, $toggle_file)) {
111
			?>
112 806 Ruebenwurz
			<form name="toggle_module_file" action="<?php echo WB_URL .'/modules/edit_module_files.php?page_id='.$page_id;?>" method="post" style="margin: 0; align:right;">
113 1069 Ruebenwurz
				<input type="hidden" name="page_id" value="<?php echo $page_id; ?>" />
114
				<input type="hidden" name="section_id" value="<?php echo $section_id; ?>" />
115
				<input type="hidden" name="mod_dir" value="<?php echo $mod_dir; ?>" />
116
				<input type="hidden" name="edit_file" value="<?php echo $toggle_file; ?>" />
117
				<input type="hidden" name="action" value="edit" />
118
				<input type="submit" value="<?php echo ucwords($toggle_file);?>" class="mod_<?php echo $mod_dir;?>_edit_css" />
119 801 doc
			</form>
120
			<?php
121
		}
122
  }
123
}
124
125 834 doc
/*
126
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
127
 FUNCTIONS WHICH CAN BE USED BY MODULE DEVELOPERS FOR OWN MODULES (E.G. VIEW.PHP, MODIFY.PHP)
128
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
129
*/
130
131
// function to obtain the module language file depending on the backend language of the current user
132
if (!function_exists('get_module_language_file')) {
133
	function get_module_language_file($mymod_dir) {
134
		$mymod_dir = strip_tags($mymod_dir);
135
		if(file_exists(WB_PATH .'/modules/' .$mymod_dir .'/languages/' .LANGUAGE .'.php')) {
136
			// a module language file exists for the users backend language
137
			return (WB_PATH .'/modules/' .$mymod_dir .'/languages/' .LANGUAGE .'.php');
138
		} else {
139
			// an English module language file must exist in all multi-lingual modules
140
			if(file_exists(WB_PATH .'/modules/' .$mymod_dir .'/languages/EN.php')) {
141
				return (WB_PATH .'/modules/' .$mymod_dir .'/languages/EN.php');
142
			} else {
143
				echo '<p><strong>Error: </strong>';
144
				echo 'Default language file (EN.php) of module "' .htmlentities($mymod_dir) .'" does not exist.</p><br />';
145
				return false;
146
			}
147
		}
148
	}
149
}
150
151
// function to include module CSS files in <body> (only if WB < 2.6.7 or register_frontend_modfiles('css') not invoked in template)
152
if (!function_exists('include_module_css')) {
153
	function include_module_css($mymod_dir, $css_file) {
154
		if(!in_array(strtolower($css_file), array('frontend.css', 'backend.css'))) return;
155
156
		if($css_file == 'frontend.css') {
157
			// check if frontend.css needs to be included into the <body> section
158
			if(!((!function_exists('register_frontend_modfiles') || !defined('MOD_FRONTEND_CSS_REGISTERED')) &&
159
					file_exists(WB_PATH .'/modules/' .$mymod_dir .'/frontend.css'))) {
160
				return false;
161
			}
162
		} else {
163
			// check if backend.css needs to be included into the <body> section
164
			global $admin;
165
			if(!(!method_exists($admin, 'register_backend_modfiles') && file_exists(WB_PATH .'/modules/' .$mymod_dir .'/backend.css'))) {
166
				return false;
167
			}
168
		}
169
		// include frontend.css or backend.css into the <body> section
170
		echo "\n".'<style type="text/css">'."\n";
171
  	include(WB_PATH .'/modules/' .$mymod_dir .'/' .$css_file);
172
  	echo "\n</style>\n";
173
		return true;
174
	}
175
}
176
177
// function to check if the optional module Javascript files are loaded into the <head> section
178
if (!function_exists('requires_module_js')) {
179
	function requires_module_js($mymod_dir, $js_file) {
180
		if(!in_array(strtolower($js_file), array('frontend.js', 'backend.js'))) {
181
			echo '<strong>Note: </strong>Javascript file "' .htmlentities($js_file) .'"
182
			specified in module "' .htmlentities($mymod_dir) .'" not valid.';
183
			return false;
184
		}
185
186
		if($js_file == 'frontend.js') {
187
			// check if frontend.js is included to the <head> section
188
			if(!defined('MOD_FRONTEND_JAVASCRIPT_REGISTERED')) {
189
				echo '<p><strong>Note:</strong> The module: "' .htmlentities($mymod_dir) .'" requires WB 2.6.7 or higher</p>
190
				<p>This module uses Javascript functions contained in frontend.js of the module.<br />
191
				Add the code below to the &lt;head&gt; section in the index.php of your template
192
				to ensure that module frontend.js files are automatically loaded if required.</p>
193
				<code style="color: #800000;">&lt;?php<br />if(function_exists(\'register_frontend_modfiles\')) { <br />
194
				&nbsp;&nbsp;register_frontend_modfiles(\'js\');<br />?&gt;</code><br />
195
				<p><strong>Tip:</strong> For WB 2.6.7 copy the code above to the index.php of your template.
196
				Then open the view.php of the "' .htmlentities($mymod_dir) .'" module and set the variable
197
				<code>$requires_frontend_js</code> to false. This may do the trick.</p><p>All WB versions below 2.6.7 needs
198
				to be upgraded to work with this module.</p>
199
				';
200
				return false;
201
			}
202
		} else {
203
			// check if backend.js is included to the <head> section
204
			global $admin;
205
				if(!method_exists($admin, 'register_backend_modfiles') && file_exists(WB_PATH .'/modules/' .$mymod_dir .'/backend.js')) {
206
				echo '<p><strong>Note:</strong> The module: "' .htmlentities($mymod_dir) .'" requires WB 2.6.7 or higher</p>
207
				<p>This module uses Javascript functions contained in backend.js of the module.<br />
208
				You need WB 2.6.7 or higher to ensure that module backend.js files are automatically loaded if required.</p>
209
				<p>Sorry, you can not use this tool with your WB installation, please upgrade to the latest WB version available.</p><br />
210
				';
211
				return false;
212
			}
213
		}
214
		return true;
215
	}
216
}
217
218 801 doc
?>