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

Last change on this file since 807 was 807, checked in by anonymous, 3 months ago

Minor improvements. Add Validator::IPAddress() method.

File size: 22.7 KB
Line 
1#!/usr/bin/env php
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 (is_null($default) || 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
235$skel_files = array();
236$skel_files['skel_admin_script'] = file_get_contents($skel_dir . '/admin.php');
237$skel_files['skel_admin_list_template'] = file_get_contents($skel_dir . '/adm_list.ihtml');
238$skel_files['skel_admin_form_template'] = file_get_contents($skel_dir . '/adm_form.ihtml');
239$skel_files['skel_public_script'] = file_get_contents($skel_dir . '/public.php');
240$skel_files['skel_public_list_template'] = file_get_contents($skel_dir . '/public_list.ihtml');
241$skel_files['skel_public_detail_template'] = file_get_contents($skel_dir . '/public.ihtml');
242
243
244// Search-replace variables ----------------------------------
245
246// Admin script file upload replacement routines...
247
248$search['admin_form_tag_init'] = '/%ADMIN_FORM_TAG_INIT%/';
249$replace['admin_form_tag_init'] = "<form method=\"post\" action=\"<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>\" class=\"sc-form\">";
250$search['admin_upload_include'] = '/%ADMIN_UPLOAD_INCLUDE%/';
251$replace['admin_upload_include'] = '';
252$search['admin_upload_config'] = '/%ADMIN_UPLOAD_CONFIG%/';
253$replace['admin_upload_config'] = '';
254$search['admin_upload_init'] = '/%ADMIN_UPLOAD_INIT%/';
255$replace['admin_upload_init'] = '';
256$search['admin_upload_del'] = '/%ADMIN_UPLOAD_DEL%/';
257$replace['admin_upload_del'] = '';
258$search['admin_upload_insert'] = '/%ADMIN_UPLOAD_INSERT%/';
259$replace['admin_upload_insert'] = '';
260$search['admin_upload_update'] = '/%ADMIN_UPLOAD_UPDATE%/';
261$replace['admin_upload_update'] = '';
262
263if ($upload_file_capability) {
264    // Form arguments
265    $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>\" />";
266
267    // Include statement.
268    $replace['admin_upload_include'] = "require_once 'codebase/lib/Upload.inc.php';\n";
269
270    // Config
271    $replace['admin_upload_config'] = <<<E_O_F
272
273// This module has file upload capability.
274\$upload = new Upload();
275\$upload->setParam(array(
276    'upload_path' => COMMON_BASE . '/html/_db_files/__///__',
277    'dest_file_perms' => 0666,
278    'allow_overwriting' => false,
279    'valid_file_extensions' => array('jpg', 'gif', 'png', 'jpeg'),
280));
281
282E_O_F;
283
284    // Main init.
285    $replace['admin_upload_init'] = <<<E_O_F
286
287// Copy uploaded image name into form data.
288\$_POST['__///__'] = isset(\$_FILES['__///__']) ? \$_FILES['__///__']['name'] : '';
289
290
291E_O_F;
292
293    // Delete.
294    $replace['admin_upload_del'] = <<<E_O_F
295
296    // Delete file.
297    if ('' != \$upload->getFilenameGlob(getFormData('%PRIMARY_KEY%') . '_*')) {
298        \$upload->deleteFile(\$upload->getFilenameGlob(getFormData('%PRIMARY_KEY%') . '_*'));
299    }
300E_O_F;
301
302    // Insert 1.
303    $replace['admin_upload_insert'] = <<<E_O_F
304
305        // Upload files with prepended primary key.
306        \$new_file = \$upload->process('__///__',  \$%PRIMARY_KEY% . '_' . getFormData('__///__'));
307
308        // If file upload errors, redirect to edit operation for the inserted record.
309        if (\$upload->anyErrors() || false === \$new_file) {
310            \$app->dieURL(\$_SERVER['PHP_SELF'] . '?op=edit&%PRIMARY_KEY%=' . \$%PRIMARY_KEY%);
311        }
312E_O_F;
313
314    // Update.
315    $replace['admin_upload_update'] = <<<E_O_F
316
317        // Upload new files.
318        if (getFormData('__///__')) {
319            // Get old file names for deletion.
320            \$old_file = \$upload->getFilenameGlob(getFormData('%PRIMARY_KEY%') . '_*');
321            // Process new file upload with prepended primary key.
322            \$new_file = \$upload->process('__///__',  getFormData('%PRIMARY_KEY%') . '_' . getFormData('__///__'));
323            if (false === \$new_file || \$upload->anyErrors()) {
324                // Upload failed. Reload form. Display errors.
325                \$frm =& editRecordForm(getFormData('%PRIMARY_KEY%'));
326                \$frm = array_merge(\$frm, getFormData());
327                \$nav->add(_("Edit %ITEM_TITLE%"));
328                \$main_template = '%ADMIN_FORM_TEMPLATE%';
329                break;
330            } else {
331                // Upload succeeded. Delete old files.
332                if ('' != \$old_file && \$old_file != \$new_file[0]['name']) {
333                    \$upload->deleteFile(\$old_file);
334                }
335            }
336        }
337E_O_F;
338} // End upload_file_capability.
339
340
341// Simple...
342
343$search['date'] = '/%DATE%/';
344$replace['date'] = date($app->getParam('date_format'));
345
346$search['name_plural'] = '/%NAME_PLURAL%/';
347$replace['name_plural'] = $module_name_plural;
348
349$search['name_singular'] = '/%NAME_SINGULAR%/';
350$replace['name_singular'] = $module_name_singular;
351
352$search['title'] = '/%TITLE%/';
353$replace['title'] = $module_title;
354
355$search['item_title'] = '/%ITEM_TITLE%/';
356$replace['item_title'] = $item_title;
357
358$search['primary_key'] = '/%PRIMARY_KEY%/';
359$replace['primary_key'] = $primary_key;
360
361$search['db_tbl'] = '/%DB_TBL%/';
362$replace['db_tbl'] = $db_tbl;
363
364$search['name_upper'] = '/%NAME_UPPER%/';
365$replace['name_upper'] = $module_name_upper;
366
367$search['admin_script'] = '/%ADMIN_SCRIPT%/';
368$replace['admin_script'] = $admin_script;
369
370$search['admin_list_template'] = '/%ADMIN_LIST_TEMPLATE%/';
371$replace['admin_list_template'] = $admin_list_template;
372
373$search['admin_form_template'] = '/%ADMIN_FORM_TEMPLATE%/';
374$replace['admin_form_template'] = $admin_form_template;
375
376$search['public_script'] = '/%PUBLIC_SCRIPT%/';
377$replace['public_script'] = $public_script;
378
379$search['public_list_template'] = '/%PUBLIC_LIST_TEMPLATE%/';
380$replace['public_list_template'] = $public_list_template;
381
382$search['public_detail_template'] = '/%PUBLIC_DETAIL_TEMPLATE%/';
383$replace['public_detail_template'] = $public_detail_template;
384
385$search['public_detail_page_vars'] = '/%PUBLIC_DETAIL_PAGE_VARS%/';
386$replace['public_detail_page_vars'] = join("\n", $public_detail_page_vars);
387
388$search['public_list_page_vars'] = '/%PUBLIC_LIST_PAGE_VARS%/';
389$replace['public_list_page_vars'] = join("\n", $public_list_page_vars);
390
391$search['set_values_default'] = '/%SET_VALUES_DEFAULT%/';
392$replace['set_values_default'] = join(",\n        ", $set_values_default);
393
394$search['search_fields'] = '/%SEARCH_FIELDS%/';
395$replace['search_fields'] = join(", ", $headers);
396
397
398
399// Complex....
400
401echo "Generating admin form table rows...\n";
402$output = array();
403exec(dirname($_SERVER['argv'][0]) . "/form_template.cli.php " . COMMON_BASE . " $db_tbl", $output, $return_val);
404if ($return_val == 0) {
405    $search['adm_form_table_rows'] = '/%ADM_FORM_TABLE_ROWS%/';
406    $replace['adm_form_table_rows'] = join("\n", $output);
407} else {
408    die(basename($_SERVER['argv'][0]) . " Error: could not execute form_template.cli.php.\n");
409}
410
411echo "Generating admin list table header rows...\n";
412$output = array();
413exec(dirname($_SERVER['argv'][0]) . "/list_template.cli.php " . COMMON_BASE . " $db_tbl headerrows", $output, $return_val);
414if ($return_val == 0) {
415    $search['adm_list_header_rows'] = '/%ADM_LIST_HEADER_ROWS%/';
416    $replace['adm_list_header_rows'] = join("\n", $output);
417} else {
418    die(basename($_SERVER['argv'][0]) . " Error: could not execute list_template.cli.php.\n");
419}
420
421echo "Generating admin list table rows...\n";
422$output = array();
423exec(dirname($_SERVER['argv'][0]) . "/list_template.cli.php " . COMMON_BASE . " $db_tbl listrows", $output, $return_val);
424if ($return_val == 0) {
425    $search['adm_list_rows'] = '/%ADM_LIST_ROWS%/';
426    $replace['adm_list_rows'] = join("\n", $output);
427} else {
428    die(basename($_SERVER['argv'][0]) . " Error: could not execute list_template.cli.php.\n");
429}
430
431echo "Generating sortorder...\n";
432$output = array();
433exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl sortorder", $output, $return_val);
434if ($return_val == 0) {
435    $search['sort_order'] = '/%SORT_ORDER%/';
436    $replace['sort_order'] = join("\n", $output);
437} else {
438    die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n");
439}
440
441echo "Generating insert data...\n";
442$output = array();
443exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl insert", $output, $return_val);
444if ($return_val == 0) {
445    $search['insert'] = '/%INSERT%/';
446    $replace['insert'] = join("\n", $output);
447} else {
448    die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n");
449}
450
451echo "Generating update data...\n";
452$output = array();
453exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl update", $output, $return_val);
454if ($return_val == 0) {
455    $search['update'] = '/%UPDATE%/';
456    $replace['update'] = join("\n", $output);
457} else {
458    die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n");
459}
460
461echo "Generating search data...\n";
462$output = array();
463exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl search", $output, $return_val);
464if ($return_val == 0) {
465    $search['search'] = '/%SEARCH%/';
466    $replace['search'] = join("\n", $output);
467} else {
468    die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n");
469}
470
471echo "Generating form validation data...\n";
472$output = array();
473exec(dirname($_SERVER['argv'][0]) . "/validation.cli.php " . COMMON_BASE . " $db_tbl", $output, $return_val);
474if ($return_val == 0) {
475    $search['form_validation'] = '/%FORM_VALIDATION%/';
476    $replace['form_validation'] = join("\n", $output);
477} else {
478    die(basename($_SERVER['argv'][0]) . " Error: could not execute validation.cli.php.\n");
479}
480
481/******************************************************************************
482 * PRINT VAR INSTEAD.
483 *****************************************************************************/
484
485if ('var' == $op) {
486    if (isset($_SERVER['argv'][5]) && isset($replace[$_SERVER['argv'][5]])) {
487        echo "\n\n" . $replace[$_SERVER['argv'][5]] . "\n\n";
488    } else if (isset($_SERVER['argv'][5]) && isset($skel_files[$_SERVER['argv'][5]])) {
489        echo "\n\n" . preg_replace($search, $replace, $skel_files[$_SERVER['argv'][5]]) . "\n\n";
490    } else {
491        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");
492    }
493    die;
494}
495
496
497/******************************************************************************
498 * WRITE FILES
499 *****************************************************************************/
500
501if ('admin' == $op) {
502    // (1) Admin script.
503    if (file_exists("$admin_dir/$admin_script")) {
504        echo "Admin script already exists: $admin_dir/$admin_script\n";
505    } else {
506        echo "Writing admin script: $admin_dir/$admin_script\n";
507        if ('test' != $op) {
508            $fp = fopen("$admin_dir/$admin_script", "w");
509            fwrite($fp, preg_replace($search, $replace, $skel_files['skel_admin_script']));
510            fclose($fp);
511        }
512    }
513
514    // (2) Admin list template.
515    if (file_exists("$admin_tpl_dir/$admin_list_template")) {
516        echo "Admin list template already exists: $admin_tpl_dir/$admin_list_template\n";
517    } else {
518        echo "Writing admin list template: $admin_tpl_dir/$admin_list_template\n";
519        if ('test' != $op) {
520            $fp = fopen("$admin_tpl_dir/$admin_list_template", "w");
521            fwrite($fp, preg_replace($search, $replace, $skel_files['skel_admin_list_template']));
522            fclose($fp);
523        }
524    }
525
526    // (3) Admin form template.
527    if (file_exists("$admin_tpl_dir/$admin_form_template")) {
528        echo "Admin form template already exists: $admin_tpl_dir/$admin_form_template\n";
529    } else {
530        echo "Writing admin form template: $admin_tpl_dir/$admin_form_template\n";
531        if ('test' != $op) {
532            $fp = fopen("$admin_tpl_dir/$admin_form_template", "w");
533            fwrite($fp, preg_replace($search, $replace, $skel_files['skel_admin_form_template']));
534            fclose($fp);
535        }
536    }
537}
538
539if ('public' == $op) {
540    // (4) Public script.
541    if (file_exists("$public_dir/$public_script")) {
542        echo "Public script already exists: $public_dir/$public_script\n";
543    } else {
544        echo "Writing public script: $public_dir/$public_script\n";
545        if ('test' != $op) {
546            $fp = fopen("$public_dir/$public_script", "w");
547            fwrite($fp, preg_replace($search, $replace, $skel_files['skel_public_script']));
548            fclose($fp);
549        }
550    }
551
552    // (5) Public list template.
553    if (file_exists("$public_tpl_dir/$public_list_template")) {
554        echo "Public list template already exists: $public_tpl_dir/$public_list_template\n";
555    } else {
556        echo "Writing public list template: $public_tpl_dir/$public_list_template\n";
557        if ('test' != $op) {
558            $fp = fopen("$public_tpl_dir/$public_list_template", "w");
559            fwrite($fp, preg_replace($search, $replace, $skel_files['skel_public_list_template']));
560            fclose($fp);
561        }
562    }
563
564    // (6) Public detail template.
565    if (file_exists("$public_tpl_dir/$public_detail_template")) {
566        echo "Public detail template already exists: $public_tpl_dir/$public_detail_template\n";
567    } else {
568        echo "Writing public detail template: $public_tpl_dir/$public_detail_template\n";
569        if ('test' != $op) {
570            $fp = fopen("$public_tpl_dir/$public_detail_template", "w");
571            fwrite($fp, preg_replace($search, $replace, $skel_files['skel_public_detail_template']));
572            fclose($fp);
573        }
574    }
575
576}
577echo "Done!\n";
578
579
580/********************************************************************
581* FUNCTIONS
582********************************************************************/
583
584function trashFile($file_path_name)
585{
586    global $user_trash_folder;
587    static $file_prefix;
588
589    if (!isset($file_prefix)) {
590        $file_prefix = time();
591    } else {
592        $file_prefix++;
593    }
594
595    // Make user trash folder.
596    if (!is_dir($user_trash_folder)) {
597        echo "Attempting to create user trash folder: $user_trash_folder/\n";
598        mkdir($user_trash_folder);
599        chmod($user_trash_folder, 0700);
600    }
601    if (!is_dir("$user_trash_folder/") || !is_writable("$user_trash_folder/")) {
602        die("User trash directory not available: $user_trash_folder/\n");
603    }
604
605    // Move file to trash.
606    if (file_exists($file_path_name)) {
607        rename($file_path_name, sprintf('%s/%s_%s', $user_trash_folder, $file_prefix, basename($file_path_name)));
608        printf("Moved to trash: %s -> %s\n", $file_path_name, sprintf('%s/%s_%s', $user_trash_folder, $file_prefix, basename($file_path_name)));
609    } else {
610        printf("File not found: %s\n", $file_path_name);
611    }
612}
Note: See TracBrowser for help on using the repository browser.