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

Last change on this file since 533 was 533, checked in by anonymous, 9 years ago

Adapted module maker scripts to use the new cli config file. Updated config to load codebase classes from its own codebase dir.

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