-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
330 lines (302 loc) · 14.7 KB
/
index.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* @license
* Copyright(c) 2002-2019 Jose Ricardo Mantilla Mantilla. All Rights Reserved.
* Author: Jose Ricardo Mantilla Mantilla <[email protected]> / <[email protected]>
* Website Author: http://pepiuox.net/ / http://labemotion.net/
* Author's licenses: http://pepiuox.net/license / http://labemotion.net/license
* Project Name: EasyCRUD
* Contributors:
* Website Contributors:
* Library list.
* Bootstrap http://www.getbootstrap.com
* Jquery http://www.jquery.com ,
* Poppers https://popper.js.org/
* This file is a module or application for make simple CRUD based in PHP.
* Use of this source code is governed by an PEPIUOX / LAB EMOTION License included
* MIT License that can be found in the LICENSE file in the root directory of
* this source tree.
* Distributed under the MIT License
* (license terms are at http://opensource.org/licenses/MIT).
* */
ob_start();
$file = 'db.php';
if (file_exists($file)) {
require_once("db.php");
$path = basename($_SERVER['REQUEST_URI']);
$file = basename($path);
$fileName = basename($_SERVER['PHP_SELF']);
if ($file == $fileName) {
header("Location: index.php?view=select");
}
function protect($string) {
$protection = htmlspecialchars(trim($string), ENT_QUOTES);
return $protection;
}
/* Get id table as first column */
function getID($tble) {
global $link;
if (!empty(protect($_GET['tbl']))) {
$tble = protect($_GET['tbl']);
$query = "SELECT * from " . $tble;
if ($result = $link->query($query)) {
/* Get field information for first column */
$result->field_seek(0);
$deletemeta = $result->fetch_field();
return $deletemeta->name;
}
}
}
$view = protect($_GET['view']);
include 'EasyCRUD.php';
$c = new EasyCRUD();
?>
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8" name = "viewport" content = "width-device=width, initial-scale=1" />
<title><?php
if (isset($_GET['tbl'])) {
$titleTbl = str_replace("_", " ", protect($_GET['tbl']));
}
if ($view === "select") {
echo 'Select your table - PHP CRUD';
} elseif ($view === "list") {
echo 'List ' . $titleTbl . ' - PHP CRUD';
} elseif ($view === "add") {
echo 'Add ' . $titleTbl . ' - PHP CRUD';
} elseif ($view === "edit") {
echo 'Edit ' . $titleTbl . ' - PHP CRUD';
} elseif ($view === "delete") {
echo 'Delete ' . $titleTbl . ' - PHP CRUD';
}
?></title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
<script src="https://unpkg.com/@popperjs/core@2"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<style>
body{
font-size: 14px;
}
</style>
</head>
<body>
<?php
/* Select table */
if ($view === "select") {
?>
<div class="container">
<div class = "row">
<div class="col-md-6">
<h3>Form </h3>
</div>
<div class="col-md-6">
<div class="form-group">
<script>
$(function () {
$("#selecttb").change(function (e) {
e.preventDefault();
var selecttb = $(this).val();
//var path = $(location).attr('href');
var url = 'index.php?view=list&tbl=' + selecttb;
window.location.replace(url);
});
});
</script>
<label class="control-label" for="selecttb">Select Table</label>
<select id="selecttb" name="selecttb" class="form-control">
<option value="">Select Table</option>
<?php
/* Get table names */
$tableList = array();
$res = $link->query("SHOW TABLES");
while ($row = $res->fetch_array()) {
$tableList[] = $row[0];
}
foreach ($tableList as $tname) {
$remp = str_replace("_", " ", $tname);
echo '<option value="' . $tname . '">' . ucfirst($remp) . '</option>' . "\n";
}
?>
</select>
</div>
</div>
</div>
</div>
<?php
/* View data in the selected table */
} elseif ($view == "list") {
if (!empty(protect($_GET['tbl']))) {
$tble = protect($_GET['tbl']);
?>
<div class="container-fluid">
<div class="row">
<div class="w-100 py-2">
<?php
echo '<h2><a href="index.php?view=select" class="btn btn-primary">Back to select</a> List ' . $titleTbl . '</h2>';
?>
</div>
<div class="w-100">
<?php
$tmpfile = 'tmp/' . $tble . '.php';
if (file_exists($tmpfile)) {
unlink($tmpfile);
}
$c->viewList($tble);
?>
</div>
</div>
</div>
<?php
} else {
header("Location: index.php?view=select");
}
/* Add data in the selected table */
} elseif ($view == "add") {
if (!empty(protect($_GET['tbl']))) {
$tble = protect($_GET['tbl']);
$idCol = getID($tble);
?>
<div class="container">
<div class="row">
<div class="w-100 py-2">
<?php
echo '<h2><a href="index.php?view=list" class="btn btn-primary">Back to list</a> Add ' . $titleTbl . '</h2>';
?>
</div>
<div class="w-100">
<?php
$scpt = $c->addPost($tble, $idCol);
$idCols = $c->addTtl($tble, $idCol);
$nvals = $c->addTPost($tble, $idCol);
$mpty = $c->ifMpty($tble, $idCol);
$tmpfile = 'tmp' . $tble . '.php';
$myfile = fopen("$tmpfile", "w") or die("Unable to open file!");
$start = '<?php' . "\n";
fwrite($myfile, $start);
fclose($myfile);
$actual = file_get_contents($tmpfile);
$actual .= '//This is temporal file only for add new row in ' . $tble . "\n";
$actual .= "if (isset(\$_POST['addrow'])) { \r\n";
$actual .= $scpt . "\r\n";
$actual .= " if (" . $mpty . ") { \r\n";
$actual .= ' $query = "INSERT INTO `$tble`(' . $idCols . ') VALUES (' . $nvals . ')";' . "\r\n";
$actual .= 'if ($link->query($query) == TRUE) {
echo "Record added successfully";
} else {
echo "Error added record: " . $link->error;
}
unlink("tmp/' . $tble . '.php");
' . "\r\n";
$actual .= " } \r\n";
$actual .= "} \r\n";
$actual .= "?> \n";
file_put_contents($tmpfile, $actual);
include 'tmp/' . $tble . '.php';
//get add form
$c->addItem($tble, $idCol);
?>
</div>
</div>
</div>
<?php
} else {
header("Location: index.php?view=select");
}
/* Edid data in the selected table */
} elseif ($view == "edit") {
if (!empty(protect($_GET['tbl'])) || !empty(protect($_GET['id']))) {
$tble = protect($_GET['tbl']);
$id = protect($_GET['id']);
$idCol = getID($tble);
?>
<div class="container">
<div class="row">
<div class="w-100 py-2">
<?php
echo '<h2><a href="index.php?view=list" class="btn btn-primary">Back to list</a> Edit ' . $tble . '</h2>';
?>
</div>
<div class="w-100">
<?php
$scpt = $c->addPost($tble, $idCol);
$ecols = $c->updateData($tble, $idCol);
$mpty = $c->ifMpty($tble, $idCol);
$tmpfile = 'tmp' . $tble . '.php';
$myfile = fopen("$tmpfile", "w") or die("Unable to open file!");
$content = '<?php' . "\n";
$content .= '//This is temporal file only for add new row' . "\n";
fwrite($myfile, $content);
fclose($myfile);
$actual = file_get_contents($tmpfile);
$actual .= "if (isset(\$_POST['editrow'])) { \r\n";
$actual .= $scpt . "\r\n";
$actual .= " if (" . $mpty . ") { \r\n";
$actual .= ' $query = "UPDATE `$tble` SET ' . $ecols . ' WHERE ' . $idCol . '=`$id` ";' . "\r\n";
$actual .= 'if ($link->query($query) == TRUE) {
echo "Record added successfully";
} else {
echo "Error added record: " . $link->error;
}
unlink("tmp/' . $tble . '.php");
' . "\r\n";
$actual .= " } \r\n";
$actual .= "} \r\n";
$actual .= "?> \n";
file_put_contents($tmpfile, $actual);
include 'tmp/' . $tble . '.php';
$c->editItem($tble, $id, $idCol);
?>
</div>
</div>
</div>
<?php
} else {
header("Location: index.php?view=select");
}
/* detele data in the selected table */
} elseif ($view == "delete") {
if (!empty(protect($_GET['tbl'])) || !empty(protect($_GET['id']))) {
$tble = protect($_GET['tbl']);
$id = protect($_GET['id']);
$idCol = getID($tble);
?>
<div class="container">
<div class="row">
<div class="w-100 py-2">
<?php
echo '<h2><a href="index.php?view=list" class="btn btn-primary">Back to list</a> Delete ' . $tble . '</h2>';
?>
</div>
<div class="w-100">
<?php
if (isset($_POST["deleterow"])) {
if ($c->ifEmpty($tble, $idCol)) {
$query = "DELETE FROM $tble WHERE $idCol = '$id' ";
if ($link->query($query) === TRUE) {
echo "Record deleted successfully";
header("Location: index.php?w=list");
} else {
echo "Error deleting record: " . $link->error;
}
}
}
$c->deleteItem($tble, $id, $idCol);
?>
</div>
</div>
</div>
<?php
} else {
header("Location: index.php?view=select");
}
}
?>
</body>
</html>
<?php
} else {
header('Location: config.php');
}
?>