1: <?php
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: class SystemMaintenance
27: {
28: public $db;
29: public $prefix;
30:
31: 32: 33:
34: public function __construct()
35: {
36:
37: $db = XoopsDatabaseFactory::getDatabaseConnection();
38: $this->db = $db;
39: $this->prefix = $this->db->prefix . '_';
40: }
41:
42: 43: 44: 45: 46: 47: 48: 49:
50: public function displayTables($array = true)
51: {
52: $tables = array();
53: $result = $this->db->queryF('SHOW TABLES');
54: while ($myrow = $this->db->fetchArray($result)) {
55: $value = array_values($myrow);
56: $value = substr($value[0], strlen(XOOPS_DB_PREFIX) + 1);
57: $tables[$value] = $value;
58: }
59: if ($array = true) {
60: return $tables;
61: } else {
62: return implode(',', $tables);
63: }
64: }
65:
66: 67: 68: 69: 70:
71: public function CleanSession()
72: {
73: $result = $this->db->queryF('TRUNCATE TABLE ' . $this->db->prefix('session'));
74:
75: return true;
76: }
77:
78: 79: 80: 81: 82: 83: 84: 85: 86:
87: public function CleanAvatar()
88: {
89: $result = $this->db->queryF('SELECT avatar_id, avatar_file FROM ' . $this->db->prefix('avatar') . " WHERE avatar_type='C' AND avatar_id IN (" . 'SELECT t1.avatar_id FROM ' . $this->db->prefix('avatar_user_link') . ' AS t1 ' . 'LEFT JOIN ' . $this->db->prefix('users') . ' AS t2 ON t2.uid=t1.user_id ' . 'WHERE t2.uid IS NULL)');
90:
91: while ($myrow = $this->db->fetchArray($result)) {
92:
93: @unlink(XOOPS_UPLOAD_PATH . '/' . $myrow['avatar_file']);
94:
95: $result1 = $this->db->queryF('DELETE FROM ' . $this->db->prefix('avatar') . ' WHERE avatar_id=' . $myrow['avatar_id']);
96: }
97:
98: $result2 = $this->db->queryF('DELETE FROM ' . $this->db->prefix('avatar_user_link') . ' WHERE user_id NOT IN (SELECT uid FROM ' . $this->db->prefix('users') . ')');
99:
100: return true;
101: }
102:
103: 104: 105: 106: 107: 108: 109:
110: protected function clearDirectory($dir)
111: {
112: if (is_dir($dir)) {
113: if ($dirHandle = opendir($dir)) {
114: while (($file = readdir($dirHandle)) !== false) {
115: if (filetype($dir . $file) === 'file' && $file !== 'index.html') {
116: unlink($dir . $file);
117: }
118: }
119: closedir($dirHandle);
120: }
121: }
122: }
123:
124: 125: 126: 127: 128: 129: 130: 131: 132:
133: public function CleanCache($cacheList)
134: {
135: foreach ($cacheList as $cache) {
136: switch ($cache) {
137: case 1:
138: $this->clearDirectory(XOOPS_VAR_PATH . '/caches/smarty_cache/');
139: break;
140: case 2:
141: $this->clearDirectory(XOOPS_VAR_PATH . '/caches/smarty_compile/');
142: break;
143: case 3:
144: $this->clearDirectory(XOOPS_VAR_PATH . '/caches/xoops_cache/');
145: break;
146: default:
147: return false;
148: }
149: }
150:
151: return true;
152: }
153:
154: 155: 156: 157: 158: 159: 160:
161: public function CheckRepairAnalyzeOptimizeQueries($tables, $maintenance)
162: {
163: $ret = '<table class="outer"><th>' . _AM_SYSTEM_MAINTENANCE_TABLES1 . '</th><th>' . _AM_SYSTEM_MAINTENANCE_TABLES_OPTIMIZE . '</th><th>' . _AM_SYSTEM_MAINTENANCE_TABLES_CHECK . '</th><th>' . _AM_SYSTEM_MAINTENANCE_TABLES_REPAIR . '</th><th>' . _AM_SYSTEM_MAINTENANCE_TABLES_ANALYZE . '</th>';
164: $tab = array();
165: for ($i = 0; $i < 4; ++$i) {
166: $tab[$i] = $i + 1;
167: }
168: $tab1 = array();
169: for ($i = 0; $i < 4; ++$i) {
170: if (in_array($tab[$i], $maintenance)) {
171: $tab1[$i] = $tab[$i];
172: } else {
173: $tab1[$i] = '0';
174: }
175: }
176: unset($tab);
177: $class = 'odd';
178: $tablesCount = count($tables);
179: for ($i = 0; $i < $tablesCount; ++$i) {
180: $ret .= '<tr class="' . $class . '"><td align="center">' . $this->prefix . $tables[$i] . '</td>';
181: for ($j = 0; $j < 4; ++$j) {
182: if ($tab1[$j] == 1) {
183:
184: $result = $this->db->queryF('OPTIMIZE TABLE ' . $this->prefix . $tables[$i]);
185: if ($result) {
186: $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('success.png') . '" /></td>';
187: } else {
188: $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('cancel.png') . '" /></td>';
189: }
190: } elseif ($tab1[$j] == 2) {
191:
192: $result = $this->db->queryF('CHECK TABLE ' . $this->prefix . $tables[$i]);
193: if ($result) {
194: $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('success.png') . '" /></td>';
195: } else {
196: $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('cancel.png') . '" /></td>';
197: }
198: } elseif ($tab1[$j] == 3) {
199:
200: $result = $this->db->queryF('REPAIR TABLE ' . $this->prefix . $tables[$i]);
201: if ($result) {
202: $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('success.png') . '" /></td>';
203: } else {
204: $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('cancel.png') . '" /></td>';
205: }
206: } elseif ($tab1[$j] == 4) {
207:
208: $result = $this->db->queryF('ANALYZE TABLE ' . $this->prefix . $tables[$i]);
209: if ($result) {
210: $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('success.png') . '" /></td>';
211: } else {
212: $ret .= '<td class="xo-actions txtcenter"><img src="' . system_AdminIcons('cancel.png') . '" /></td>';
213: }
214: } else {
215: $ret .= '<td> </td>';
216: }
217: }
218: $ret .= '</tr>';
219: $class = ($class === 'even') ? 'odd' : 'even';
220: }
221: $ret .= '</table>';
222:
223: return $ret;
224: }
225:
226: 227: 228: 229: 230: 231: 232:
233: public function dump_tables($tables, $drop)
234: {
235: $ret = array();
236: $ret[0] = "# \n";
237: $ret[0] .= "# Dump SQL, Generate by Xoops \n";
238: $ret[0] .= '# Date : ' . date('d-m-Y - H:i') . " \n";
239: $ret[1] = '<table class="outer"><tr><th width="30%">' . _AM_SYSTEM_MAINTENANCE_DUMP_TABLES . '</th><th width="35%">' . _AM_SYSTEM_MAINTENANCE_DUMP_STRUCTURES . '</th><th width="35%">' . _AM_SYSTEM_MAINTENANCE_DUMP_NB_RECORDS . '</th></tr>';
240: $class = 'odd';
241: $tablesCount = count($tables);
242: for ($i = 0; $i < $tablesCount; ++$i) {
243:
244: $ret = $this->dump_table_structure($ret, $this->prefix . $tables[$i], $drop, $class);
245:
246: $ret = $this->dump_table_datas($ret, $this->prefix . $tables[$i]);
247: $class = ($class === 'even') ? 'odd' : 'even';
248: }
249: $ret = $this->dump_write($ret);
250: $ret[1] .= '</table>';
251:
252: return $ret;
253: }
254:
255: 256: 257: 258: 259: 260: 261:
262: public function dump_modules($modules, $drop)
263: {
264: $ret = array();
265: $ret[0] = "# \n";
266: $ret[0] .= "# Dump SQL, Generate by Xoops \n";
267: $ret[0] .= '# Date : ' . date('d-m-Y - H:i') . " \n";
268: $ret[0] .= "# \n\n";
269: $ret[1] = '<table class="outer"><tr><th width="30%">' . _AM_SYSTEM_MAINTENANCE_DUMP_TABLES . '</th><th width="35%">' . _AM_SYSTEM_MAINTENANCE_DUMP_STRUCTURES . '</th><th width="35%">' . _AM_SYSTEM_MAINTENANCE_DUMP_NB_RECORDS . '</th></tr>';
270: $class = 'odd';
271: $modulesCount = count($modules);
272: for ($i = 0; $i < $modulesCount; ++$i) {
273:
274: $module_handler = xoops_getHandler('module');
275: $module = $module_handler->getByDirname($modules[$i]);
276: $ret[1] .= '<tr><th colspan="3" align="left">' . ucfirst($modules[$i]) . '</th></tr>';
277: $modtables = $module->getInfo('tables');
278: if ($modtables !== false && is_array($modtables)) {
279: foreach ($modtables as $table) {
280:
281: $ret = $this->dump_table_structure($ret, $this->prefix . $table, $drop, $class);
282:
283: $ret = $this->dump_table_datas($ret, $this->prefix . $table);
284: $class = ($class === 'even') ? 'odd' : 'even';
285: }
286: } else {
287: $ret[1] .= '<tr><td colspan="3" align="center">' . _AM_SYSTEM_MAINTENANCE_DUMP_NO_TABLES . '</td></tr>';
288: }
289: }
290: $ret[1] .= '</table>';
291: $ret = $this->dump_write($ret);
292:
293: return $ret;
294: }
295:
296: 297: 298: 299: 300: 301: 302: 303: 304:
305: public function dump_table_structure($ret, $table, $drop, $class)
306: {
307: $verif = false;
308: $result = $this->db->queryF('SHOW create table `' . $table . '`;');
309: if ($result) {
310: if ($row = $this->db->fetchArray($result)) {
311: $ret[0] .= '# Table structure for table `' . $table . "` \n\n";
312: if ($drop == 1) {
313: $ret[0] .= 'DROP TABLE IF EXISTS `' . $table . "`;\n\n";
314: }
315: $verif = true;
316: $ret[0] .= $row['Create Table'] . ";\n\n";
317: }
318: }
319: $ret[1] .= '<tr class="' . $class . '"><td align="center">' . $table . '</td><td class="xo-actions txtcenter">';
320: $ret[1] .= ($verif === true) ? '<img src="' . system_AdminIcons('success.png') . '" />' : '<img src="' . system_AdminIcons('cancel.png') . '" />';
321: $ret[1] .= '</td>';
322: $this->db->freeRecordSet($result);
323:
324: return $ret;
325: }
326:
327: 328: 329: 330: 331: 332: 333:
334: public function dump_table_datas($ret, $table)
335: {
336: $count = 0;
337: $result = $this->db->queryF('SELECT * FROM ' . $table . ';');
338: if ($result) {
339: $num_rows = $this->db->getRowsNum($result);
340: $num_fields = $this->db->getFieldsNum($result);
341:
342: if ($num_rows > 0) {
343: $field_type = array();
344: $i = 0;
345: while ($i < $num_fields) {
346: $meta = mysqli_fetch_field($result);
347: $field_type[] = $meta->type;
348: ++$i;
349: }
350:
351: $ret[0] .= 'INSERT INTO `' . $table . "` values\n";
352: $index = 0;
353: while ($row = $this->db->fetchRow($result)) {
354: ++$count;
355: $ret[0] .= '(';
356: for ($i = 0; $i < $num_fields; ++$i) {
357: if (null === $row[$i]) {
358: $ret[0] .= 'null';
359: } else {
360: switch ($field_type[$i]) {
361: case 'int':
362: $ret[0] .= $row[$i];
363: break;
364: default:
365: $ret[0] .= "'" . $this->db->escape($row[$i]) . "'";
366: }
367: }
368: if ($i < $num_fields - 1) {
369: $ret[0] .= ',';
370: }
371: }
372: $ret[0] .= ')';
373:
374: if ($index < $num_rows - 1) {
375: $ret[0] .= ',';
376: } else {
377: $ret[0] .= ';';
378: }
379: $ret[0] .= "\n";
380: ++$index;
381: }
382: }
383: }
384: $ret[1] .= '<td align="center">';
385: $ret[1] .= $count . ' ' . _AM_SYSTEM_MAINTENANCE_DUMP_RECORDS . '</td></tr>';
386: $ret[0] .= "\n";
387: $this->db->freeRecordSet($result);
388: $ret[0] .= "\n";
389:
390: return $ret;
391: }
392:
393: 394: 395: 396: 397: 398:
399: public function dump_write($ret)
400: {
401: $file_name = 'dump_' . date('Y.m.d') . '_' . date('H.i.s') . '.sql';
402: $path_file = './admin/maintenance/dump/' . $file_name;
403: if (file_put_contents($path_file, $ret[0])) {
404: $ret[1] .= '<table class="outer"><tr><th colspan="2" align="center">' . _AM_SYSTEM_MAINTENANCE_DUMP_FILE_CREATED . '</th><th>' . _AM_SYSTEM_MAINTENANCE_DUMP_RESULT . '</th></tr><tr><td colspan="2" align="center"><a href="' . XOOPS_URL . '/modules/system/admin/maintenance/dump/' . $file_name . '">' . $file_name . '</a></td><td class="xo-actions txtcenter"><img src="' . system_AdminIcons('success.png') . '" /></td><tr></table>';
405: } else {
406: $ret[1] .= '<table class="outer"><tr><th colspan="2" align="center">' . _AM_SYSTEM_MAINTENANCE_DUMP_FILE_CREATED . '</th><th>' . _AM_SYSTEM_MAINTENANCE_DUMP_RESULT . '</th></tr><tr><td colspan="2" class="xo-actions txtcenter">' . $file_name . '</td><td class="xo-actions txtcenter"><img src="' . system_AdminIcons('cancel.png') . '" /></td><tr></table>';
407: }
408:
409: return $ret;
410: }
411: }
412: