-
Notifications
You must be signed in to change notification settings - Fork 1
/
03_generate.php
executable file
·278 lines (240 loc) · 9.04 KB
/
03_generate.php
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php include dirname(__FILE__) . '/header.php';
try {
if (!isset($_POST['path'])) {
throw new RuntimeException('This page has expired');
}
$path = rtrim(trim($_POST['path']), '\\/');
if (!is_dir($path)) {
throw new RuntimeException('Invalid path');
}
if (
!(
file_exists("$path/lib.php") &&
file_exists("$path/db.php") &&
file_exists("$path/index.php")
)
) {
throw new RuntimeException(
'The given path is not a valid AppGini project path'
);
}
if (!is_writable($path . '/hooks')) {
throw new RuntimeException(
'The hooks folder of the given path is not writable'
);
}
if (!is_writable($path . '/resources')) {
throw new RuntimeException(
'The resources folder of the given path is not writable'
);
}
} catch (RuntimeException $e) {
echo '<br>' . $MyPlugin->error_message($e->getMessage());
exit();
}
//-------------------------------------------------------------------------------------
$write_to_hooks = $_REQUEST['dont_write_to_hooks'] == 1 ? false : true;
?>
<div class="bs-docs-section row">
<h1 class="page-header">
<img src="<?php echo $MyPlugin->logo; ?>" style="height: 1em;">
<?php echo $MyPlugin->title; ?>
</h1>
<p class="lead">
<a href="./index.php">Home</a> >
<a href="./02_output.php"> Select output folder</a> > Coping Files <?php echo $MyPlugin->name; ?>
</p>
</div>
<h4>Progress log</h4>
<?php
$MyPlugin->progress_log->add("Output folder: $path", 'text-info');
//coping resources
$MyPlugin->progress_log->ok();
$MyPlugin->progress_log->line();
//admin dest resources
$source = dirname(__FILE__) . '/app-resources/admin/auditLog.php';
$dest = $path . '/admin/auditLog.php';
$MyPlugin->my_copy_file($source, $dest, true);
$source = dirname(__FILE__) . '/app-resources/admin/auditLogTimeLine.php';
$dest = $path . '/admin/auditLogTimeLine.php';
$MyPlugin->my_copy_file($source, $dest, true);
//hooks dest resources
$source = dirname(__FILE__) . '/app-resources/hooks/auditLog_functions.php';
$dest = $path . '/hooks/audit/auditLog_functions.php';
$MyPlugin->my_copy_file($source, $dest, true);
$source = dirname(__FILE__) . '/app-resources/hooks/auditLog_timeline.php';
$dest = $path . '/hooks/audit/auditLog_timeline.php';
$MyPlugin->my_copy_file($source, $dest, true);
$source = dirname(__FILE__) . '/app-resources/dropdown_menu.html';
$dest = $path . '/hooks/audit/dropdown_menu.html';
$MyPlugin->my_copy_file($source, $dest, true);
$source = dirname(__FILE__) . '/app-resources/Clipboard_AppGini_AuditLog_Table.txt';
$dest = $path . '/hooks/audit/Clipboard_AppGini_AuditLog_Table.txt';
$MyPlugin->my_copy_file($source, $dest, true);
$source = dirname(__FILE__) . '/app-resources/button.js';
$dest = $path . '/hooks/audit/button.js';
$MyPlugin->my_copy_file($source, $dest, true);
$source = dirname(__FILE__) . '/app-resources/scripts.php';
$dest = $path . '/hooks/audit/scripts.php';
$MyPlugin->my_copy_file($source, $dest, true);
$MyPlugin->progress_log->line();
$MyPlugin->progress_log->add(
'Adding New table on current data base',
'text-info'
);
$sql = file_get_contents(
dirname(__FILE__) . '/app-resources/audit_tableSQL.sql'
);
if ($sql) {
$eo = ['silentErrors' => true];
$res = sql($sql, $eo);
if ($eo['error'] != '') {
$MyPlugin->progress_log->add(
'ERROR: Audit table not created',
'text-danger spacer'
);
} else {
$MyPlugin->progress_log->add('Audit table ready');
}
}
$MyPlugin->progress_log->line();
$MyPlugin->progress_log->add('Adding code to hooks', 'text-info');
$MyPlugin->progress_log->add('File: __global.php');
$code = "<?php include('audit/scripts.php');?>";
$file_path = $path . '/hooks/__global.php';
if ($write_to_hooks) {
$check="include('audit/scripts.php')";
$chk_A = $MyPlugin->check_if_exist_code($file_path, false, $code);
$chk_B = $MyPlugin->check_if_exist_code($file_path, false, $check);
if (!($chk_A === $chk_B)) {
$MyPlugin->progress_log->add("Error adding code into file : {$file_path}.<br>The code or part of code ({$check}) exist in this file, please delete before continue", 'text-danger');
} else {
$res = $MyPlugin->add_to_file($file_path, false, $code);
inspect_result($res, $file_path, $MyPlugin);
}
} else {
$code = "include('audit/scripts.php');";
$MyPlugin->progress_log->add("File: {$file_path}");
$MyPlugin->progress_log->add('On top of function');
$MyPlugin->progress_log->add("Install code: {$code}");
$MyPlugin->progress_log->line();
}
// write to hooks files
$tables = getTableList(true);
foreach ($tables as $tn => $table) {
$MyPlugin->progress_log->add('Table: ' . $table[0], 'text-info');
$hook = "{$path}/hooks/{$tn}.php";
//* Step 4.A
$function = "{$tn}_init";
$code = "\$_SESSION ['tablenam'] = \$options->TableName; \$_SESSION ['tableID'] = \$options->PrimaryKey;";
$check=["\$_SESSION ['tablenam'] = \$options->TableName","\$_SESSION ['tableID'] = \$options->PrimaryKey"];
$res = hooks_code($hook, $function, $code, $MyPlugin, $write_to_hooks, $check);
if (!$res) {
break;
}
//* Step 4.B
$function = "{$tn}_after_insert";
$code = "table_after_change(\$_SESSION, \$memberInfo, \$data, 'INSERTION');";
$check ="table_after_change";
$res = hooks_code($hook, $function, $code, $MyPlugin, $write_to_hooks, $check);
if (!$res) {
break;
}
//* Step 4.C
$function = "{$tn}_before_update";
$code = "table_before_change(\$_SESSION, \$data['selectedID']);";
$check ="table_before_change";
$res = hooks_code($hook, $function, $code, $MyPlugin, $write_to_hooks, $check);
if (!$res) {
break;
}
//* Step 4.D
$function = "{$tn}_after_update";
$code = "table_after_change(\$_SESSION, \$memberInfo, \$data, 'UPDATE');";
$check ="table_after_change";
$res = hooks_code($hook, $function, $code, $MyPlugin, $write_to_hooks, $check);
if (!$res) {
break;
}
//* Step 4.E
$function = "{$tn}_before_delete";
$code = "table_before_change(\$_SESSION, \$selectedID);";
$check ="table_before_change";
$res = hooks_code($hook, $function, $code, $MyPlugin, $write_to_hooks, $check);
if (!$res) {
break;
}
//* Step 4.F
$function = "{$tn}_after_delete";
$code = "table_after_change(\$_SESSION, \$memberInfo, \$selectedID, 'DELETION');";
$check ="table_after_change";
$res = hooks_code($hook, $function, $code, $MyPlugin, $write_to_hooks, $check);
if (!$res) {
break;
}
}
echo $MyPlugin->progress_log->show();
?>
<center>
<a style="margin:20px;" href="index.php" class="btn btn-success btn-lg"><span class="glyphicon glyphicon-home" ></span> Start page</a>
</center>
<script>
$j( function(){
$j("#progress").height( $j(window).height() - $j("#progress").offset().top - $j(".btn-success").height() - 100 );
//add resize event
$j( window ).resize(function() {
$j("#progress").height( $j(window).height() - $j("#progress").offset().top - $j(".btn-success").height() - 100 );
});
});
</script>
<?php
include dirname(__FILE__) . '/footer.php';
function inspect_result($res, $file_path, &$MyPlugin)
{
if ($res) {
$MyPlugin->progress_log->add(
"Installed code into '{$file_path}'.",
'text-success spacer'
);
} else {
$error = $MyPlugin->last_error();
if ($error == 'Code already exists') {
$MyPlugin->progress_log->add(
"Skipped installing to '{$file_path}', code is already installed.",
'text-warning spacer'
);
} else {
$MyPlugin->progress_log->add(
"Failed to install code '{$file_path}': {$error}",
'text-danger spacer'
);
}
}
}
function hooks_code($hook, $function, $code, &$MyPlugin, $write_to_hooks=true, $check=false)
{
if ($write_to_hooks) {
if ($check) {
if (!is_array($check)) {
$check=[$check];
}
foreach ($check as $value) {
$chk_A = $MyPlugin->check_if_exist_code($hook, $function, $code);
$chk_B = $MyPlugin->check_if_exist_code($hook, $function, $value);
if (!($chk_A === $chk_B)) {
$MyPlugin->progress_log->add("Error on hook file : {$hook}.<br>The code or part of code exist in function <strong>{$function}</strong>.<br>Please delete the calling <strong>{$value}</strong> before continue", 'text-danger');
return false;
}
}
}
$res = $MyPlugin->add_to_hook($hook, $function, $code);
inspect_result($res, $function, $MyPlugin);
} else {
$MyPlugin->progress_log->add("File: {$hook}", 'text-info');
$MyPlugin->progress_log->add("Hook function: {$function}");
$MyPlugin->progress_log->add("Install code: {$code}");
$MyPlugin->progress_log->line();
}
return true;
}
?>