forked from f446843/banshee
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_module
More file actions
executable file
·184 lines (153 loc) · 4.23 KB
/
new_module
File metadata and controls
executable file
·184 lines (153 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/php
<?php
/* Copyright (C) by Hugo Leisink <hugo@leisink.net>
* This file is part of the Banshee PHP framework
* https://www.banshee-php.org/
*
* Licensed under The MIT License
*/
chdir(__DIR__);
require("libraries/core/banshee.php");
require("libraries/core/security.php");
require("libraries/helpers/console.php");
/* Show help and exit
*/
function help_exit() {
printf("Usage: %s public|private <module name> [tm|sf|db|api]\n", $GLOBALS["argv"][0]);
printf(" - tm: create a TableManager module.\n");
printf(" - sf: create a SplitForm module.\n");
printf(" - db: create a CRUD module.\n");
printf(" - api: create an API module.\n");
printf(" Look inside the templates directory for the module templates.\n");
exit;
}
/* Set class name inside file
*/
function set_class_name($directory, $module) {
$filename = $directory."/".$module.".php";
if (($file = file($filename)) === false) {
return false;
}
$module = str_replace("/", "_", $module);
$file[1] = str_replace("XXX", $module, $file[1]);
if (($fp = fopen($filename, "w")) == false) {
return false;
}
fputs($fp, implode("", $file));
fclose($fp);
return true;
}
/* Fix import path in XSLT file
*/
function fix_view_import_path($module) {
$filename = "views/".$module.".xslt";
if (($count = substr_count($module, "/")) == 0) {
return true;
} else if (($file = file($filename)) === false) {
return false;
}
foreach ($file as $i => $line) {
if (substr($line, 0, 11) != "<xsl:import") {
continue;
}
$file[$i] = substr($line, 0, 18).str_repeat("../", $count).substr($line, 18);
}
if (($fp = fopen($filename, "w")) == false) {
return false;
}
fputs($fp, implode("", $file));
fclose($fp);
return true;
}
/* Start
*/
error_reporting(E_ALL & ~E_NOTICE);
if (count($argv) < 3) {
help_exit();
} else if (in_array($argv[1], array("public", "private")) == false) {
help_exit();
}
if (is_dir("settings") == false) {
exit("Not inside a Banshee website directory.\n");
}
$access = $argv[1];
$view = trim($argv[2], "/");
list($module) = explode(".", $view);
$type = $argv[3];
/* Validate module name
*/
$module_characters = VALIDATE_NONCAPITALS.VALIDATE_NUMBERS."/_";
if (valid_input($module, $module_characters) == false) {
exit("Invalid module name.\n");
}
/* Validate type
*/
if ($type === null) {
$type = "page";
} else if ($type == "tm") {
$type = "tablemanager";
} else if ($type == "sf") {
$type = "splitform";
} else if ($type == "db") {
$type = "crud";
} else if ($type == "api") {
$type = "api";
} else {
exit("Invalid module type.\n");
}
/* Check for module existence
*/
chdir("settings");
if (module_exists($module, true)) {
exit;
}
chdir("..");
/* Make directories
*/
$locations = array("controllers", "models", "views", "public/css");
$ofs = 0;
while (($pos = strpos($module, "/", $ofs)) !== false) {
$ofs = $pos + 1;
$subdir = "/".substr($module, 0, $pos);
foreach ($locations as $location) {
if (file_exists($location.$subdir) == false) {
printf("Creating directory %s%s.\n", $location, $subdir);
mkdir($location.$subdir, 0755, true);
}
}
}
/* Copy templates
*/
print "Creating controller, model, view and stylesheet.\n";
safe_copy("templates/".$type."_controller.php", "controllers/".$module.".php");
safe_copy("templates/".$type."_model.php", "models/".$module.".php");
if ($type != "api") {
safe_copy("templates/".$type."_view.xslt", "views/".$view.".xslt");
safe_copy("templates/".$type."_style.css", "public/css/".$module.".css");
touch("public/css/".$module.".css");
}
/* Set class names
*/
print "Setting controller and model class name.\n";
set_class_name("controllers", $module);
set_class_name("models", $module);
/* Fix include path in XSLT
*/
if ($type != "api") {
print "Fixing include paths in view.\n";
fix_view_import_path($module);
}
/* Add to configuration file
*/
printf("Adding module to %s pages configuration.\n", $access);
if (($fp = fopen("settings/".$access."_modules.conf", "a")) !== false) {
fputs($fp, $view."\n");
fclose($fp);
}
/* Register private page in database
*/
if ($access == "private") {
system("database/private_modules");
}
print "Done.\n";
?>