source: trunk/bin/module_maker/module.cli.php @ 556

Last change on this file since 556 was 556, checked in by anonymous, 8 years ago

Updated module maker to require operation argument with either 'admin' or 'public' to generate only files for that half.

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