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

Last change on this file since 413 was 413, checked in by anonymous, 12 years ago

Added header and footer to docs/examples. Updated message printing methods to include before, after, and gotohash options. Minor other fixes.

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