/[drupal]/contributions/modules/active_template/template_file.inc
ViewVC logotype

Contents of /contributions/modules/active_template/template_file.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Fri Aug 15 06:12:26 2008 UTC (15 months, 1 week ago) by collectivecolors
Branch: MAIN
CVS Tags: DRUPAL-5--1-1, HEAD
Changes since 1.1: +23 -38 lines
File MIME type: text/x-php
This patch should fix problems raised in issues 283347 and 292774.

Thanks to math_1048, OnkelTem, and Steve Dondley for finding these issues and suggesting fixes.
1 <?php
2 // $Id$
3 // by Collective Colors - http://collectivecolors.com
4
5 /*******************************************************************************
6 * Template File
7 *
8 * The template file include provides functionality that allows the template api
9 * module to load and save files to the filesystem. This is needed for the css
10 * and javascript page injection mechanisms to function correctly.
11 */
12
13 //------------------------------------------------------------------------------
14 // Template File constants (just in case we change the names later)
15
16 /**
17 * Once again, prefix these constants with ([T]emplate [A]pplication
18 * [P]rograming [I]nterface) so that constants have a much smaller chance of
19 * conflicting with constants defined in the Drupal core and other Drupal
20 * modules.
21 */
22 define('TAPI_ROOT_DIR', file_directory_path() . '/template');
23 define('TAPI_CSS_DIR', TAPI_ROOT_DIR . '/css');
24 define('TAPI_JS_DIR', TAPI_ROOT_DIR . '/javascript');
25
26 /**
27 * This module optionally uses a Javascript packer obtained from:
28 *
29 * http://joliclic.free.fr/php/javascript-packer/en
30 *
31 * Thanks to Nicolas Martin for this great library.
32 */
33 define('TAPI_PACKER_CLASS_FILE', 'class.JavaScriptPacker.php');
34
35 /*******************************************************************************
36 * Creates all required directories for the template api module if they do not
37 * already exist
38 *
39 * @return boolean
40 */
41 function template_api_create_template_directories() {
42
43 // Only variables can be passed by reference to Drupals file_check_directory() and we need these
44 // constants elsewhere in this file.
45 $root_dir = TAPI_ROOT_DIR;
46 $css_dir = TAPI_CSS_DIR;
47 $js_dir = TAPI_JS_DIR;
48
49 if (!file_check_directory($root_dir, FILE_CREATE_DIRECTORY)) {
50 drupal_set_message('Unable to create or write to a template directory in '
51 . file_directory_path());
52 $fail = TRUE;
53 }
54 if (!file_check_directory($css_dir, FILE_CREATE_DIRECTORY)) {
55 drupal_set_message('Unable to create or write a css directory in ' . TAPI_ROOT_DIR);
56 $fail = TRUE;
57 }
58 if (!file_check_directory($js_dir, FILE_CREATE_DIRECTORY)) {
59 drupal_set_message('Unable to create or write a javascript directory in ' . TAPI_ROOT_DIR);
60 $fail = TRUE;
61 }
62 return $fail ? FALSE : TRUE;
63 }
64
65
66 /*******************************************************************************
67 * Save all template files to the file system
68 *
69 * This function is meant for internal use only! The css and javascript
70 * parameters are passed by reference to avoid the overhead of having to copy
71 * potentially long strings during the normal pass by value php invocation.
72 * The css and javascript data is not modified within this function.
73 *
74 * @param int $tpid - template id number
75 * @param string $css_data - css file contents
76 * @param string $js_data - javascript file contents
77 */
78 function template_api_save_template_files($tpid, &$css_data, &$js_data) {
79
80 // Make sure all the proper directories exist. This should not have too much
81 // of a performance penalty because it is only called when we are saving a
82 // template.
83 if (!template_api_create_template_directories()) {
84 drupal_set_message('Unable to create or write to the needed template directories. '
85 . 'Contact your server administrator if you need assistance.');
86 return FALSE;
87 }
88
89 // Save css template file.
90 if (strlen($css_data)) {
91 // Compress the CSS file.
92 $css_data = preg_replace('/\/\*.*?\*\//s', '', $css_data); // trim comments
93 $css_data = preg_replace('/\s+/', ' ', $css_data); // condense space
94 $css_data = preg_replace('/\s?(:|;|{|})\s?/', '$1', $css_data);
95 }
96 $css_path = TAPI_CSS_DIR . '/id' . $tpid . '.css';
97 $css_status = template_api_save_file($css_path, $css_data);
98
99 // Save javascript template file.
100 if (strlen($js_data)) {
101 $packer_lib = drupal_get_path('module', 'template_api')
102 . '/' . TAPI_PACKER_CLASS_FILE;
103
104 if (file_exists(realpath($packer_lib))) {
105 // Pack the Javascript file.
106 require_once($packer_lib);
107
108 $packer = new JavaScriptPacker($js_data, 'Normal', true, false);
109 $js_data = $packer->pack();
110 }
111 }
112 $js_path = TAPI_JS_DIR . '/id' . $tpid . '.js';
113 $js_status = template_api_save_file($js_path, $js_data);
114
115 return ($css_status && $js_status);
116 }
117
118 /*******************************************************************************
119 * Save a single template file to the file system
120 *
121 * This function is meant for internal use only! The file data parameter is
122 * passed by reference to avoid the overhead of having to copy a potentially
123 * long string during the normal pass by value php invocation.
124 *
125 * @param string $path - file path
126 * @param string $data - file contents
127 * @return boolean
128 */
129 function template_api_save_file($path, &$data) {
130
131 // Save template file. If we pass an empty string for the data parameter and
132 // this file exists already then we assume that the existing file is to be
133 // deleted.
134 if (strlen($data)) {
135 // Got data. Save template file.
136 if (!file_save_data($data, $path, FILE_EXISTS_REPLACE)) {
137 return template_api_log_status('File "' . $path . '" save failed',
138 WATCHDOG_ERROR);
139 }
140 else {
141 return template_api_log_status('File "' . $path . '" saved successfully');
142 }
143 }
144 else {
145 // No data. Delete existing template file.
146 return template_api_delete_file($path);
147 }
148 }
149
150 /*******************************************************************************
151 * Delete all template files from the file system if they exist
152 *
153 * This function is meant for internal use only!
154 *
155 * @param int $tpid - template id number
156 * @return boolean
157 */
158 function template_api_delete_template_files($tpid) {
159
160 // Delete css file if it exists.
161 $css_path = TAPI_CSS_DIR . '/id' . $tpid . '.css';
162 $css_status = template_api_delete_file($css_path);
163
164 // Delete javascript file if it exists.
165 $js_path = TAPI_JS_DIR . '/id' . $tpid . '.js';
166 $js_status = template_api_delete_file($js_path);
167
168 return ($css_status && $js_status);
169 }
170
171 /*******************************************************************************
172 * Delete a single template file from the file system
173 *
174 * This function is meant for internal use only!
175 *
176 * @param string $path - file path
177 * @return boolean
178 */
179 function template_api_delete_file($path) {
180
181 if (file_exists($path)) {
182 // File exists. Delete me!!!
183 if (!file_delete($path)) {
184 return template_api_log_status('File "' . $path . '" delete failed',
185 WATCHDOG_ERROR);
186 }
187 else {
188 return template_api_log_status('File "' . $path
189 . '" deleted successfully');
190 }
191 }
192 // File does not exist.
193 return TRUE;
194 }
195
196 /*******************************************************************************
197 * Load a template files contents into memory
198 *
199 * This function is meant for internal use only! Currently this function is
200 * not used. It is defined only for completeness. In fact, I don't know why I
201 * would need this file eventually because the files are meant to be compressed
202 * so the readable file contents get loaded from the database. But I've already
203 * defined it and we never know when we may need it, so here it is.
204 *
205 * @param string $path - file path
206 * @return string - file data
207 */
208 function template_api_load_file($path) {
209 $real_path = realpath($path);
210
211 // If there is no file then just return an empty string. We do not want
212 // warnings if the file does not exist.
213 if (!file_exists($real_path)) {
214 return '';
215 }
216 // File exists. Load em up.
217 return file_get_contents($real_path);
218 }
219
220 /*******************************************************************************
221 * Inject template files into the html page header
222 *
223 * @param int $tpid - template id number
224 */
225 function template_api_inject_files($tpid) {
226 // Inject template css file into the html page header.
227 $css_path = TAPI_CSS_DIR . '/id' . $tpid . '.css';
228 if (file_exists($css_path) && filesize($css_path) > 0) {
229 drupal_add_css($css_path, 'theme');
230 }
231
232 // Inject template javascript file into the html page header.
233 $js_path = TAPI_JS_DIR . '/id' . $tpid . '.js';
234 if (file_exists($js_path) && filesize($js_path) > 0) {
235 drupal_add_js($js_path, 'theme');
236 }
237 }

  ViewVC Help
Powered by ViewVC 1.1.2