forked from robwent/joomla-json-db-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-db-check.php
230 lines (207 loc) · 6.8 KB
/
json-db-check.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
<?php
/**
* Turn on outputbuffering for servers that have it disabled.
*/
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Joomla json Check</title>
<style>
.btn {
/* Structure */
display: inline-block;
zoom: 1;
line-height: normal;
white-space: nowrap;
vertical-align: middle;
text-align: center;
cursor: pointer;
-webkit-user-drag: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
font-family: inherit;
font-size: 100%;
padding: 0.5em 1em;
border: 1px solid #999; /*IE 6/7/8*/
border: none rgba(0, 0, 0, 0); /*IE9 + everything else*/
background-color: rgb(0, 120, 231);
color: #fff;
text-decoration: none;
border-radius: 2px;
}
</style>
</head>
<body>
<?php
//Initiate Joomla so we can use it's functions
/**
* Constant that is checked in included files to prevent direct access.
* define() is used in the installation folder rather than "const" to not error for PHP 5.2 and lower
*/
define('_JEXEC', 1);
if (file_exists(__DIR__ . '/defines.php'))
{
include_once __DIR__ . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', __DIR__);
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';
// Instantiate the application.
$app = JFactory::getApplication('site');
$db = JFactory::getDbo();
$config = JFactory::getConfig();
$jinput = $app->input;
$fullcheck = $jinput->get('fullcheck', 0, 'INT');
$columns = $jinput->get('columns', array(), 'array');
function is_trying_to_be_json($data)
{
$data = trim($data);
return ((substr($data, 0, 1) === '{') || (substr($data, -1, 1) === '}')) ? true : false;
}
function is_json()
{
call_user_func_array('json_decode', func_get_args());
return (json_last_error() === JSON_ERROR_NONE);
}
//The columns to check
$column_string = 'COLUMN_NAME = \'params\' OR COLUMN_NAME = \'rules\' OR COLUMN_NAME = \'attribs\'';
if(count($columns) > 0)
{
$column_string = '';
$first = 1;
if(in_array('params', $columns))
{
$column_string .= 'COLUMN_NAME = \'params\'';
$first = 0;
}
if(in_array('rules', $columns))
{
$column_string .= $first ? 'COLUMN_NAME = \'rules\'' : ' OR COLUMN_NAME = \'rules\'';
$first = 0;
}
if(in_array('attribs', $columns))
{
$column_string .= $first ? 'COLUMN_NAME = \'attribs\'' : ' OR COLUMN_NAME = \'attribs\'';
}
}
//We use this for both checks
$query = $db->getQuery(true)
->select('TABLE_NAME,COLUMN_NAME')
->from('INFORMATION_SCHEMA.COLUMNS')
->where($column_string)
->andWhere('TABLE_SCHEMA = \'' . $config->get('db') . '\'');
$db->setQuery($query);
$results = $db->loadObjectList();
?>
<?php if ($fullcheck == 0) : ?>
<h4>Checking for Invalid Empty Parameters</h4>
<?php
if ($results)
{
foreach ($results as $result)
{
echo "Checking table: {$result->TABLE_NAME}, column {$result->COLUMN_NAME}<br>";
$query = $db->getQuery(true)
->update($result->TABLE_NAME)
->set($result->COLUMN_NAME . ' = "{}"')
->where($result->COLUMN_NAME . ' = "" OR ' . $result->COLUMN_NAME . ' = \'{\"\"}\' OR ' . $result->COLUMN_NAME . ' = \'{\\\\\"\\\\\"}\' ');
$db->setQuery($query);
$results = $db->execute();
$changes = $db->getAffectedRows();
if ($changes != 0)
{
echo $changes . " rows modified.<br>";
}
}
}
?>
<h4>Finished checking empty parameters</h4>
<form>
<button class="btn" name="fullcheck" value="1">Check For All Invalid Values</button>
Columns to check:
<label for="params" class="pure-checkbox">
<input id="params" type="checkbox" name="columns[]" value="params" checked>
params
</label>
<label for="params" class="pure-checkbox">
<input id="params" type="checkbox" name="columns[]" value="rules" checked>
rules
</label>
<label for="params" class="pure-checkbox">
<input id="params" type="checkbox" name="columns[]" value="attribs" checked>
attribs
</label>
</form>
<p></p>
<p><small>(This will not replace any values, you will need to manaully fix them)</small></p>
<?php else :
if(count($columns) > 0)
{
?>
<h4>Checking '<?php echo implode(', ', $columns) ?>' Entries for Invalid Syntax</h4>
<?php
// Check all params for invalid syntax
if ($results)
{
foreach ($results as $result)
{
echo "<p>Checking table: {$result->TABLE_NAME}, column {$result->COLUMN_NAME}</p>";
$query = $db->getQuery(true)
->select('*')
->from($result->TABLE_NAME)
->where($result->COLUMN_NAME . ' != "{}"');
$db->setQuery($query);
$results = $db->loadAssocList();
if ($results)
{
foreach ($results as $row)
{
if (!is_json($row[$result->COLUMN_NAME]) && is_trying_to_be_json($row[$result->COLUMN_NAME]))
{
$error = json_last_error_msg();
reset($row);
$first_key = key($row);
echo "Row {$row[$first_key]} is not valid JSON. Error: ($error)<br>";
echo "Content: {$row[$result->COLUMN_NAME]}<br><hr>";
}
}
}
}
}?>
<h4>Finished checking invalid parameters</h4>
<p>Check invalid rules at <a target="_blank" href="http://jsonlint.com/">jsonlint.com</a></p>
<?php }
else
{
//No columns selected ?>
<h4>You need to select at least one column to check!</h4>
<?php } ?>
<form>
<button class="btn" name="fullcheck" value="1">Check Again</button>
Columns to check:
<label for="params" class="pure-checkbox">
<input id="params" type="checkbox" name="columns[]" value="params" <?php echo (in_array('params', $columns)) ? 'checked' : ''; ?>>
params
</label>
<label for="params" class="pure-checkbox">
<input id="params" type="checkbox" name="columns[]" value="rules" <?php echo (in_array('rules', $columns)) ? 'checked' : ''; ?>>
rules
</label>
<label for="params" class="pure-checkbox">
<input id="params" type="checkbox" name="columns[]" value="attribs" <?php echo (in_array('attribs', $columns)) ? 'checked' : ''; ?>>
attribs
</label>
</form>
<?php endif; ?>
</body>
</html>