1 | #!/usr/local/bin/php -q |
---|
2 | <?php |
---|
3 | /** |
---|
4 | * The Strangecode Codebase - a general application development framework for PHP |
---|
5 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
6 | * Copyright 2001-2009 Strangecode Internet Consultancy |
---|
7 | * |
---|
8 | * This file is part of The Strangecode Codebase. |
---|
9 | * |
---|
10 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
11 | * modify it under the terms of the GNU General Public License as published by the |
---|
12 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
13 | * any later version. |
---|
14 | * |
---|
15 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
18 | * details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License along with |
---|
21 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | */ |
---|
23 | |
---|
24 | /** |
---|
25 | * module.cli.php |
---|
26 | */ |
---|
27 | |
---|
28 | include_once dirname(__FILE__) . '/_config.inc.php'; |
---|
29 | |
---|
30 | $op = null; |
---|
31 | $valid_ops = array('clean', 'var', 'test'); |
---|
32 | |
---|
33 | // Test for arguments. |
---|
34 | if (isset($_SERVER['argv'][2]) && isset($_SERVER['argv'][3])) { |
---|
35 | $module_name_singular = $_SERVER['argv'][2]; |
---|
36 | $module_name_plural = $_SERVER['argv'][3]; |
---|
37 | } else { |
---|
38 | die(basename($_SERVER['argv'][0]) . " Error: invalid arguments. Try like this: |
---|
39 | |
---|
40 | " . basename($_SERVER['argv'][0]) . " site_directory name_singular name_plural [operation] |
---|
41 | |
---|
42 | Valid operations include: " . join(', ', $valid_ops) . " |
---|
43 | |
---|
44 | The following files will be generated by this script: |
---|
45 | <site_directory>/admin/nameplural.php |
---|
46 | <site_directory>/admin/_templates/namesingular_list.ihtml |
---|
47 | <site_directory>/admin/_templates/namesingular_form.ihtml |
---|
48 | <site_directory>/html/nameplural.php |
---|
49 | <site_directory>/html/_templates/namesingular_list.ihtml |
---|
50 | <site_directory>/html/_templates/namesingular.ihtml |
---|
51 | |
---|
52 | "); |
---|
53 | } |
---|
54 | |
---|
55 | // Test for operation. |
---|
56 | if (isset($_SERVER['argv'][4])) { |
---|
57 | // Optional fourth arg is op. |
---|
58 | $op = $_SERVER['argv'][4]; |
---|
59 | // Make sure op is valid. |
---|
60 | if (!in_array($op, $valid_ops)) { |
---|
61 | die(basename($_SERVER['argv'][0]) . " Warning: Operation '$op' is not something I know how to do Please select one of: " . join(", ", $valid_ops) . "\n"); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | switch ($op) { |
---|
66 | case 'test' : |
---|
67 | echo "RUNNING MODULE MAKER IN TEST MODE.\n\n"; |
---|
68 | break; |
---|
69 | default : |
---|
70 | |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | /******************************************************************** |
---|
75 | * CONFIG |
---|
76 | ********************************************************************/ |
---|
77 | |
---|
78 | // Where deleted files go: |
---|
79 | $user_trash_folder = $_SERVER['HOME'] . '/.Trash'; |
---|
80 | |
---|
81 | // Directories |
---|
82 | $skel_dir = realpath(dirname(__FILE__) . '/skel'); |
---|
83 | $admin_dir = realpath(COMMON_BASE . '/admin'); |
---|
84 | $admin_tpl_dir = realpath(COMMON_BASE . '/admin/_templates'); |
---|
85 | $public_dir = realpath(COMMON_BASE . '/html'); |
---|
86 | $public_tpl_dir = realpath(COMMON_BASE . '/html/_templates'); |
---|
87 | |
---|
88 | // Names |
---|
89 | $module_title = ucwords(str_replace('_', ' ', $module_name_plural)); |
---|
90 | $item_title = ucwords(str_replace('_', ' ', $module_name_singular)); |
---|
91 | $module_name_upper = mb_strtoupper(str_replace('_', ' ', $module_name_plural)); |
---|
92 | |
---|
93 | // Admin files |
---|
94 | $admin_script = $module_name_plural . '.php'; |
---|
95 | $admin_list_template = $module_name_singular . '_list.ihtml'; |
---|
96 | $admin_form_template = $module_name_singular . '_form.ihtml'; |
---|
97 | |
---|
98 | // Public files |
---|
99 | $public_script = $module_name_plural . '.php'; |
---|
100 | $public_list_template = $module_name_singular . '_list.ihtml'; |
---|
101 | $public_detail_template = $module_name_singular . '_view.ihtml'; |
---|
102 | |
---|
103 | // Database names |
---|
104 | $db_tbl = $module_name_singular . '_tbl'; |
---|
105 | $primary_key = $module_name_singular . '_id'; |
---|
106 | |
---|
107 | // Only after we've defined essential files can we clean. |
---|
108 | if ('clean' == $op) { |
---|
109 | echo "Beginning file cleanup\n"; |
---|
110 | trashFile("$admin_dir/$admin_script"); |
---|
111 | trashFile("$admin_tpl_dir/$admin_list_template"); |
---|
112 | trashFile("$admin_tpl_dir/$admin_form_template"); |
---|
113 | trashFile("$public_dir/$public_script"); |
---|
114 | trashFile("$public_tpl_dir/$public_list_template"); |
---|
115 | trashFile("$public_tpl_dir/$public_detail_template"); |
---|
116 | echo "End file cleanup\n"; |
---|
117 | die; |
---|
118 | } |
---|
119 | |
---|
120 | // Go! |
---|
121 | echo 'Running Module Maker. Using database: ' . $app->getParam('db_name') . "\n"; |
---|
122 | |
---|
123 | |
---|
124 | /******************************************************************** |
---|
125 | * PREPROCESSING |
---|
126 | ********************************************************************/ |
---|
127 | |
---|
128 | // Ensure skel files exist. |
---|
129 | if ( |
---|
130 | !file_exists("$skel_dir/admin.php") || |
---|
131 | !file_exists("$skel_dir/public.php") || |
---|
132 | !file_exists("$skel_dir/public.ihtml") || |
---|
133 | !file_exists("$skel_dir/public_list.ihtml") |
---|
134 | ) { |
---|
135 | die(basename($_SERVER['argv'][0]) . " Warning: one or more skeleton source files missing. Please check directory: $skel_dir.\n"); |
---|
136 | } |
---|
137 | |
---|
138 | // Ensure essential directories exist. |
---|
139 | if (!is_dir("$admin_dir")) { |
---|
140 | die(basename($_SERVER['argv'][0]) . " Error: admin_dir '$admin_dir' directory not found.\n"); |
---|
141 | } |
---|
142 | if (!is_dir("$admin_tpl_dir")) { |
---|
143 | die(basename($_SERVER['argv'][0]) . " Error: admin_tpl_dir '$admin_tpl_dir' directory not found.\n"); |
---|
144 | } |
---|
145 | if (!is_dir("$public_dir")) { |
---|
146 | die(basename($_SERVER['argv'][0]) . " Error: public_dir '$public_dir' directory not found.\n"); |
---|
147 | } |
---|
148 | if (!is_dir("$public_tpl_dir")) { |
---|
149 | die(basename($_SERVER['argv'][0]) . " Error: public_tpl_dir '$public_tpl_dir' directory not found.\n"); |
---|
150 | } |
---|
151 | |
---|
152 | // Get DB tables. |
---|
153 | $qid = $db->query("SHOW TABLES"); |
---|
154 | while (list($row) = mysql_fetch_row($qid)) { |
---|
155 | $tables[] = $row; |
---|
156 | } |
---|
157 | |
---|
158 | // Make sure requested table is in database. |
---|
159 | if (!isset($tables)) { |
---|
160 | die(sprintf("%s Warning: %s does not exist in database %s. No DB tables found!", basename($_SERVER['argv'][0]), $db_tbl, $app->getParam('db_name'))); |
---|
161 | } |
---|
162 | if (!in_array($db_tbl, $tables)) { |
---|
163 | die(sprintf("%s Warning: %s does not exist in database %s. Please select one of: \n\n%s\n\n", basename($_SERVER['argv'][0]), $db_tbl, $app->getParam('db_name'), join("\n", $tables))); |
---|
164 | } |
---|
165 | |
---|
166 | // Ensure requested table contains columns. |
---|
167 | // Get DB table column info. |
---|
168 | $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); |
---|
169 | while ($row = mysql_fetch_row($qid)) { |
---|
170 | $cols[] = $row; |
---|
171 | } |
---|
172 | if (!is_array($cols) || empty($cols)) { |
---|
173 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | /****************************************************************************** |
---|
178 | * SETUP VARIABLES |
---|
179 | *****************************************************************************/ |
---|
180 | |
---|
181 | |
---|
182 | // Loop through columns |
---|
183 | $upload_file_capability = false; |
---|
184 | $headers = array(); |
---|
185 | $public_list_page_vars = array(); |
---|
186 | $public_detail_page_vars = array(); |
---|
187 | $set_values_default = array(); |
---|
188 | if (is_array($cols) && !empty($cols)) { |
---|
189 | foreach ($cols as $col) { |
---|
190 | |
---|
191 | // Human readable. |
---|
192 | $field = $col[0]; |
---|
193 | $field_title = ucfirst(str_replace('_', ' ', $field)); |
---|
194 | $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); |
---|
195 | $default = $col[4]; |
---|
196 | |
---|
197 | if (mb_strpos($default, '0000') !== false || '0' == $default) { |
---|
198 | $default = ''; |
---|
199 | } |
---|
200 | |
---|
201 | // Get primary key. |
---|
202 | // if ('PRI' == $col[3]) { |
---|
203 | // $primary_key = $field; |
---|
204 | // } |
---|
205 | |
---|
206 | // Our form will require type="multipart/form-data". |
---|
207 | if (preg_match('/file|image/i', $field)) { |
---|
208 | $upload_file_capability = true; |
---|
209 | } |
---|
210 | |
---|
211 | // Column headers. |
---|
212 | $headers[$field] = $field_title; |
---|
213 | |
---|
214 | // Get php code for printing variables. |
---|
215 | $public_list_page_vars[] = "<\x3fphp echo oTxt(\$" . $module_name_singular . "_list[\$i]['$field']); \x3f>"; |
---|
216 | $public_detail_page_vars[] = "<\x3fphp echo oTxt(\$item['$field']); \x3f>"; |
---|
217 | $set_values_default[] = "'$field' => '$default'"; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | // -------------FILES------------- |
---|
222 | |
---|
223 | $skel_files['skel_admin_script'] = file_get_contents($skel_dir . '/admin.php'); |
---|
224 | $skel_files['skel_admin_list_template'] = file_get_contents($skel_dir . '/adm_list.ihtml'); |
---|
225 | $skel_files['skel_admin_form_template'] = file_get_contents($skel_dir . '/adm_form.ihtml'); |
---|
226 | $skel_files['skel_public_script'] = file_get_contents($skel_dir . '/public.php'); |
---|
227 | $skel_files['skel_public_list_template'] = file_get_contents($skel_dir . '/public_list.ihtml'); |
---|
228 | $skel_files['skel_public_detail_template'] = file_get_contents($skel_dir . '/public.ihtml'); |
---|
229 | |
---|
230 | |
---|
231 | // Search-replace variables ---------------------------------- |
---|
232 | |
---|
233 | // Admin script file upload replacement routines... |
---|
234 | |
---|
235 | $search['admin_form_tag_init'] = '/%ADMIN_FORM_TAG_INIT%/'; |
---|
236 | $replace['admin_form_tag_init'] = "<form method=\"post\" action=\"<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>\" class=\"sc-form\">"; |
---|
237 | $search['admin_upload_include'] = '/%ADMIN_UPLOAD_INCLUDE%/'; |
---|
238 | $replace['admin_upload_include'] = ''; |
---|
239 | $search['admin_upload_config'] = '/%ADMIN_UPLOAD_CONFIG%/'; |
---|
240 | $replace['admin_upload_config'] = ''; |
---|
241 | $search['admin_upload_init'] = '/%ADMIN_UPLOAD_INIT%/'; |
---|
242 | $replace['admin_upload_init'] = ''; |
---|
243 | $search['admin_upload_del'] = '/%ADMIN_UPLOAD_DEL%/'; |
---|
244 | $replace['admin_upload_del'] = ''; |
---|
245 | $search['admin_upload_insert'] = '/%ADMIN_UPLOAD_INSERT%/'; |
---|
246 | $replace['admin_upload_insert'] = ''; |
---|
247 | $search['admin_upload_update'] = '/%ADMIN_UPLOAD_UPDATE%/'; |
---|
248 | $replace['admin_upload_update'] = ''; |
---|
249 | |
---|
250 | if ($upload_file_capability) { |
---|
251 | // Form arguments |
---|
252 | $replace['admin_form_tag_init'] = "<form enctype=\"multipart/form-data\" method=\"post\" action=\"<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>\" class=\"sc-form\">\n<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"<\x3fphp echo intval(ini_get('upload_max_filesize')) * 1024 * 1024; \x3f>\" />"; |
---|
253 | |
---|
254 | // Include statement. |
---|
255 | $replace['admin_upload_include'] = "require_once 'codebase/lib/Upload.inc.php';\n"; |
---|
256 | |
---|
257 | // Config |
---|
258 | $replace['admin_upload_config'] = <<<E_O_F |
---|
259 | |
---|
260 | // This module has file upload capability. |
---|
261 | \$upload = new Upload(); |
---|
262 | \$upload->setParam(array( |
---|
263 | 'upload_path' => COMMON_BASE . '/html/_db_files/__///__', |
---|
264 | 'dest_file_perms' => 0666, |
---|
265 | 'allow_overwriting' => false, |
---|
266 | 'valid_file_extensions' => array('jpg', 'gif', 'png', 'jpeg'), |
---|
267 | )); |
---|
268 | |
---|
269 | E_O_F; |
---|
270 | |
---|
271 | // Main init. |
---|
272 | $replace['admin_upload_init'] = <<<E_O_F |
---|
273 | |
---|
274 | // Copy uploaded image name into form data. |
---|
275 | \$_POST['__///__'] = isset(\$_FILES['__///__']) ? \$_FILES['__///__']['name'] : ''; |
---|
276 | |
---|
277 | |
---|
278 | E_O_F; |
---|
279 | |
---|
280 | // Delete. |
---|
281 | $replace['admin_upload_del'] = <<<E_O_F |
---|
282 | |
---|
283 | // Delete file. |
---|
284 | if ('' != \$upload->getFilenameGlob(getFormData('%PRIMARY_KEY%') . '_*')) { |
---|
285 | \$upload->deleteFile(\$upload->getFilenameGlob(getFormData('%PRIMARY_KEY%') . '_*')); |
---|
286 | } |
---|
287 | E_O_F; |
---|
288 | |
---|
289 | // Insert 1. |
---|
290 | $replace['admin_upload_insert'] = <<<E_O_F |
---|
291 | |
---|
292 | // Upload files with prepended primary key. |
---|
293 | \$new_file = \$upload->process('__///__', \$%PRIMARY_KEY% . '_' . getFormData('__///__')); |
---|
294 | |
---|
295 | // If file upload errors, redirect to edit operation for the inserted record. |
---|
296 | if (\$upload->anyErrors() || false === \$new_file) { |
---|
297 | \$app->dieURL(\$_SERVER['PHP_SELF'] . '?op=edit&%PRIMARY_KEY%=' . \$%PRIMARY_KEY%); |
---|
298 | } |
---|
299 | E_O_F; |
---|
300 | |
---|
301 | // Update. |
---|
302 | $replace['admin_upload_update'] = <<<E_O_F |
---|
303 | |
---|
304 | // Upload new files. |
---|
305 | if (getFormData('__///__')) { |
---|
306 | // Get old file names for deletion. |
---|
307 | \$old_file = \$upload->getFilenameGlob(getFormData('%PRIMARY_KEY%') . '_*'); |
---|
308 | // Process new file upload with prepended primary key. |
---|
309 | \$new_file = \$upload->process('__///__', getFormData('%PRIMARY_KEY%') . '_' . getFormData('__///__')); |
---|
310 | if (false === \$new_file || \$upload->anyErrors()) { |
---|
311 | // Upload failed. Reload form. Display errors. |
---|
312 | \$frm =& editRecordForm(getFormData('%PRIMARY_KEY%')); |
---|
313 | \$frm = array_merge(\$frm, getFormData()); |
---|
314 | \$nav->add(_("Edit %ITEM_TITLE%")); |
---|
315 | \$main_template = '%ADMIN_FORM_TEMPLATE%'; |
---|
316 | break; |
---|
317 | } else { |
---|
318 | // Upload succeeded. Delete old files. |
---|
319 | if ('' != \$old_file && \$old_file != \$new_file[0]['name']) { |
---|
320 | \$upload->deleteFile(\$old_file); |
---|
321 | } |
---|
322 | } |
---|
323 | } |
---|
324 | E_O_F; |
---|
325 | } // End upload_file_capability. |
---|
326 | |
---|
327 | |
---|
328 | // Simple... |
---|
329 | |
---|
330 | $search['date'] = '/%DATE%/'; |
---|
331 | $replace['date'] = date($app->getParam('date_format')); |
---|
332 | |
---|
333 | $search['name_plural'] = '/%NAME_PLURAL%/'; |
---|
334 | $replace['name_plural'] = $module_name_plural; |
---|
335 | |
---|
336 | $search['name_singular'] = '/%NAME_SINGULAR%/'; |
---|
337 | $replace['name_singular'] = $module_name_singular; |
---|
338 | |
---|
339 | $search['title'] = '/%TITLE%/'; |
---|
340 | $replace['title'] = $module_title; |
---|
341 | |
---|
342 | $search['item_title'] = '/%ITEM_TITLE%/'; |
---|
343 | $replace['item_title'] = $item_title; |
---|
344 | |
---|
345 | $search['primary_key'] = '/%PRIMARY_KEY%/'; |
---|
346 | $replace['primary_key'] = $primary_key; |
---|
347 | |
---|
348 | $search['db_tbl'] = '/%DB_TBL%/'; |
---|
349 | $replace['db_tbl'] = $db_tbl; |
---|
350 | |
---|
351 | $search['name_upper'] = '/%NAME_UPPER%/'; |
---|
352 | $replace['name_upper'] = $module_name_upper; |
---|
353 | |
---|
354 | $search['admin_script'] = '/%ADMIN_SCRIPT%/'; |
---|
355 | $replace['admin_script'] = $admin_script; |
---|
356 | |
---|
357 | $search['admin_list_template'] = '/%ADMIN_LIST_TEMPLATE%/'; |
---|
358 | $replace['admin_list_template'] = $admin_list_template; |
---|
359 | |
---|
360 | $search['admin_form_template'] = '/%ADMIN_FORM_TEMPLATE%/'; |
---|
361 | $replace['admin_form_template'] = $admin_form_template; |
---|
362 | |
---|
363 | $search['public_script'] = '/%PUBLIC_SCRIPT%/'; |
---|
364 | $replace['public_script'] = $public_script; |
---|
365 | |
---|
366 | $search['public_list_template'] = '/%PUBLIC_LIST_TEMPLATE%/'; |
---|
367 | $replace['public_list_template'] = $public_list_template; |
---|
368 | |
---|
369 | $search['public_detail_template'] = '/%PUBLIC_DETAIL_TEMPLATE%/'; |
---|
370 | $replace['public_detail_template'] = $public_detail_template; |
---|
371 | |
---|
372 | $search['public_detail_page_vars'] = '/%PUBLIC_DETAIL_PAGE_VARS%/'; |
---|
373 | $replace['public_detail_page_vars'] = join("\n", $public_detail_page_vars); |
---|
374 | |
---|
375 | $search['public_list_page_vars'] = '/%PUBLIC_LIST_PAGE_VARS%/'; |
---|
376 | $replace['public_list_page_vars'] = join("\n", $public_list_page_vars); |
---|
377 | |
---|
378 | $search['set_values_default'] = '/%SET_VALUES_DEFAULT%/'; |
---|
379 | $replace['set_values_default'] = join(",\n ", $set_values_default); |
---|
380 | |
---|
381 | $search['search_fields'] = '/%SEARCH_FIELDS%/'; |
---|
382 | $replace['search_fields'] = join(", ", $headers); |
---|
383 | |
---|
384 | |
---|
385 | |
---|
386 | // Complex.... |
---|
387 | |
---|
388 | echo "Generating admin form table rows...\n"; |
---|
389 | $output = array(); |
---|
390 | exec(dirname($_SERVER['argv'][0]) . "/form_template.cli.php " . COMMON_BASE . " $db_tbl", $output, $return_val); |
---|
391 | if ($return_val == 0) { |
---|
392 | $search['adm_form_table_rows'] = '/%ADM_FORM_TABLE_ROWS%/'; |
---|
393 | $replace['adm_form_table_rows'] = join("\n", $output); |
---|
394 | } else { |
---|
395 | die(basename($_SERVER['argv'][0]) . " Error: could not execute form_template.cli.php.\n"); |
---|
396 | } |
---|
397 | |
---|
398 | echo "Generating admin list table header rows...\n"; |
---|
399 | $output = array(); |
---|
400 | exec(dirname($_SERVER['argv'][0]) . "/list_template.cli.php " . COMMON_BASE . " $db_tbl headerrows", $output, $return_val); |
---|
401 | if ($return_val == 0) { |
---|
402 | $search['adm_list_header_rows'] = '/%ADM_LIST_HEADER_ROWS%/'; |
---|
403 | $replace['adm_list_header_rows'] = join("\n", $output); |
---|
404 | } else { |
---|
405 | die(basename($_SERVER['argv'][0]) . " Error: could not execute list_template.cli.php.\n"); |
---|
406 | } |
---|
407 | |
---|
408 | echo "Generating admin list table rows...\n"; |
---|
409 | $output = array(); |
---|
410 | exec(dirname($_SERVER['argv'][0]) . "/list_template.cli.php " . COMMON_BASE . " $db_tbl listrows", $output, $return_val); |
---|
411 | if ($return_val == 0) { |
---|
412 | $search['adm_list_rows'] = '/%ADM_LIST_ROWS%/'; |
---|
413 | $replace['adm_list_rows'] = join("\n", $output); |
---|
414 | } else { |
---|
415 | die(basename($_SERVER['argv'][0]) . " Error: could not execute list_template.cli.php.\n"); |
---|
416 | } |
---|
417 | |
---|
418 | echo "Generating sortorder...\n"; |
---|
419 | $output = array(); |
---|
420 | exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl sortorder", $output, $return_val); |
---|
421 | if ($return_val == 0) { |
---|
422 | $search['sort_order'] = '/%SORT_ORDER%/'; |
---|
423 | $replace['sort_order'] = join("\n", $output); |
---|
424 | } else { |
---|
425 | die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); |
---|
426 | } |
---|
427 | |
---|
428 | echo "Generating insert data...\n"; |
---|
429 | $output = array(); |
---|
430 | exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl insert", $output, $return_val); |
---|
431 | if ($return_val == 0) { |
---|
432 | $search['insert'] = '/%INSERT%/'; |
---|
433 | $replace['insert'] = join("\n", $output); |
---|
434 | } else { |
---|
435 | die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); |
---|
436 | } |
---|
437 | |
---|
438 | echo "Generating update data...\n"; |
---|
439 | $output = array(); |
---|
440 | exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl update", $output, $return_val); |
---|
441 | if ($return_val == 0) { |
---|
442 | $search['update'] = '/%UPDATE%/'; |
---|
443 | $replace['update'] = join("\n", $output); |
---|
444 | } else { |
---|
445 | die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); |
---|
446 | } |
---|
447 | |
---|
448 | echo "Generating search data...\n"; |
---|
449 | $output = array(); |
---|
450 | exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl search", $output, $return_val); |
---|
451 | if ($return_val == 0) { |
---|
452 | $search['search'] = '/%SEARCH%/'; |
---|
453 | $replace['search'] = join("\n", $output); |
---|
454 | } else { |
---|
455 | die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); |
---|
456 | } |
---|
457 | |
---|
458 | echo "Generating form validation data...\n"; |
---|
459 | $output = array(); |
---|
460 | exec(dirname($_SERVER['argv'][0]) . "/validation.cli.php " . COMMON_BASE . " $db_tbl", $output, $return_val); |
---|
461 | if ($return_val == 0) { |
---|
462 | $search['form_validation'] = '/%FORM_VALIDATION%/'; |
---|
463 | $replace['form_validation'] = join("\n", $output); |
---|
464 | } else { |
---|
465 | die(basename($_SERVER['argv'][0]) . " Error: could not execute validation.cli.php.\n"); |
---|
466 | } |
---|
467 | |
---|
468 | /****************************************************************************** |
---|
469 | * PRINT VAR INSTEAD. |
---|
470 | *****************************************************************************/ |
---|
471 | |
---|
472 | if ('var' == $op) { |
---|
473 | if (isset($_SERVER['argv'][5]) && isset($replace[$_SERVER['argv'][5]])) { |
---|
474 | echo "\n\n" . $replace[$_SERVER['argv'][5]] . "\n\n"; |
---|
475 | } else if (isset($_SERVER['argv'][5]) && isset($skel_files[$_SERVER['argv'][5]])) { |
---|
476 | echo "\n\n" . preg_replace($search, $replace, $skel_files[$_SERVER['argv'][5]]) . "\n\n"; |
---|
477 | } else { |
---|
478 | die(basename($_SERVER['argv'][0]) . " Error: variable " . (isset($_SERVER['argv'][5]) ? $_SERVER['argv'][5] : '') . " not defined. Please choose one of:\n" . join(', ', array_keys(array_merge($replace, $skel_files))) . "\n"); |
---|
479 | } |
---|
480 | die; |
---|
481 | } |
---|
482 | |
---|
483 | |
---|
484 | /****************************************************************************** |
---|
485 | * WRITE FILES |
---|
486 | *****************************************************************************/ |
---|
487 | |
---|
488 | // (1) Admin script. |
---|
489 | if (file_exists("$admin_dir/$admin_script")) { |
---|
490 | echo "Admin script already exists: $admin_dir/$admin_script\n"; |
---|
491 | } else { |
---|
492 | echo "Writing admin script: $admin_dir/$admin_script\n"; |
---|
493 | if ('test' != $op) { |
---|
494 | $fp = fopen("$admin_dir/$admin_script", "w"); |
---|
495 | fwrite($fp, preg_replace($search, $replace, $skel_files['skel_admin_script'])); |
---|
496 | fclose($fp); |
---|
497 | } |
---|
498 | } |
---|
499 | |
---|
500 | // (2) Admin list template. |
---|
501 | if (file_exists("$admin_tpl_dir/$admin_list_template")) { |
---|
502 | echo "Admin list template already exists: $admin_tpl_dir/$admin_list_template\n"; |
---|
503 | } else { |
---|
504 | echo "Writing admin list template: $admin_tpl_dir/$admin_list_template\n"; |
---|
505 | if ('test' != $op) { |
---|
506 | $fp = fopen("$admin_tpl_dir/$admin_list_template", "w"); |
---|
507 | fwrite($fp, preg_replace($search, $replace, $skel_files['skel_admin_list_template'])); |
---|
508 | fclose($fp); |
---|
509 | } |
---|
510 | } |
---|
511 | |
---|
512 | // (3) Admin form template. |
---|
513 | if (file_exists("$admin_tpl_dir/$admin_form_template")) { |
---|
514 | echo "Admin form template already exists: $admin_tpl_dir/$admin_form_template\n"; |
---|
515 | } else { |
---|
516 | echo "Writing admin form template: $admin_tpl_dir/$admin_form_template\n"; |
---|
517 | if ('test' != $op) { |
---|
518 | $fp = fopen("$admin_tpl_dir/$admin_form_template", "w"); |
---|
519 | fwrite($fp, preg_replace($search, $replace, $skel_files['skel_admin_form_template'])); |
---|
520 | fclose($fp); |
---|
521 | } |
---|
522 | } |
---|
523 | |
---|
524 | // (4) Public script. |
---|
525 | if (file_exists("$public_dir/$public_script")) { |
---|
526 | echo "Public script already exists: $public_dir/$public_script\n"; |
---|
527 | } else { |
---|
528 | echo "Writing public script: $public_dir/$public_script\n"; |
---|
529 | if ('test' != $op) { |
---|
530 | $fp = fopen("$public_dir/$public_script", "w"); |
---|
531 | fwrite($fp, preg_replace($search, $replace, $skel_files['skel_public_script'])); |
---|
532 | fclose($fp); |
---|
533 | } |
---|
534 | } |
---|
535 | |
---|
536 | // (5) Public list template. |
---|
537 | if (file_exists("$public_tpl_dir/$public_list_template")) { |
---|
538 | echo "Public list template already exists: $public_tpl_dir/$public_list_template\n"; |
---|
539 | } else { |
---|
540 | echo "Writing public list template: $public_tpl_dir/$public_list_template\n"; |
---|
541 | if ('test' != $op) { |
---|
542 | $fp = fopen("$public_tpl_dir/$public_list_template", "w"); |
---|
543 | fwrite($fp, preg_replace($search, $replace, $skel_files['skel_public_list_template'])); |
---|
544 | fclose($fp); |
---|
545 | } |
---|
546 | } |
---|
547 | |
---|
548 | // (6) Public detail template. |
---|
549 | if (file_exists("$public_tpl_dir/$public_detail_template")) { |
---|
550 | echo "Public detail template already exists: $public_tpl_dir/$public_detail_template\n"; |
---|
551 | } else { |
---|
552 | echo "Writing public detail template: $public_tpl_dir/$public_detail_template\n"; |
---|
553 | if ('test' != $op) { |
---|
554 | $fp = fopen("$public_tpl_dir/$public_detail_template", "w"); |
---|
555 | fwrite($fp, preg_replace($search, $replace, $skel_files['skel_public_detail_template'])); |
---|
556 | fclose($fp); |
---|
557 | } |
---|
558 | } |
---|
559 | |
---|
560 | echo "Done!\n"; |
---|
561 | |
---|
562 | |
---|
563 | /******************************************************************** |
---|
564 | * FUNCTIONS |
---|
565 | ********************************************************************/ |
---|
566 | |
---|
567 | function trashFile($file_path_name) |
---|
568 | { |
---|
569 | global $user_trash_folder; |
---|
570 | static $file_prefix; |
---|
571 | |
---|
572 | if (!isset($file_prefix)) { |
---|
573 | $file_prefix = time(); |
---|
574 | } else { |
---|
575 | $file_prefix++; |
---|
576 | } |
---|
577 | |
---|
578 | // Make user trash folder. |
---|
579 | if (!is_dir($user_trash_folder)) { |
---|
580 | echo "Attempting to create user trash folder: $user_trash_folder/\n"; |
---|
581 | mkdir($user_trash_folder); |
---|
582 | chmod($user_trash_folder, 0700); |
---|
583 | } |
---|
584 | if (!is_dir("$user_trash_folder/") || !is_writable("$user_trash_folder/")) { |
---|
585 | die("User trash directory not available: $user_trash_folder/\n"); |
---|
586 | } |
---|
587 | |
---|
588 | // Move file to trash. |
---|
589 | if (file_exists($file_path_name)) { |
---|
590 | rename($file_path_name, sprintf('%s/%s_%s', $user_trash_folder, $file_prefix, basename($file_path_name))); |
---|
591 | printf("Moved to trash: %s -> %s\n", $file_path_name, sprintf('%s/%s_%s', $user_trash_folder, $file_prefix, basename($file_path_name))); |
---|
592 | } else { |
---|
593 | printf("File not found: %s\n", $file_path_name); |
---|
594 | } |
---|
595 | } |
---|
596 | |
---|
597 | ?> |
---|