Dotclear

source: plugins/dcAdvancedCleaner/inc/class.dc.advanced.cleaner.php @ 2199

Revision 2199, 9.6 KB checked in by JcDenis, 13 years ago (diff)

dcAdvancedCleaner 0.4:

  • Fixed list of tables (closes #440)
  • Fixed direct uninstaller
  • Added readable errors return
  • Added DC 2.2 compatibility (new settings)
  • Changed admin design
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of dcAdvancedCleaner, a plugin for Dotclear 2.
4#
5# Copyright (c) 2009-2010 JC Denis and contributors
6# jcdenis@gdwd.com
7#
8# Licensed under the GPL version 2.0 license.
9# A copy of this license is available in LICENSE file or at
10# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11# -- END LICENSE BLOCK ------------------------------------
12
13if (!defined('DC_ADMIN_CONTEXT')){return;}
14
15class dcAdvancedCleaner
16{
17     protected static $errors = array(
18          'settings' => array(
19               'delete_global' => 'Failed to delete global settings',
20               'delete_local' => 'Failed to delete local settings',
21               'delete_all' => 'Failed to delete all settings'
22          ),
23          'tables' => array(
24               'empty' => 'Failed to empty table',
25               'delete' => 'Failed to delete table'
26          ),
27          'plugins' => array(
28               'empty' => 'Failed to empty plugin folder',
29               'delete' => 'Failed to delete plugin folder'
30          ),
31          'themes' => array(
32               'empty' => 'Failed to empty themes folder',
33               'delete' => 'Failed to delete themes folder'
34          ),
35          'caches' => array(
36               'empty' => 'Failed to empty cache folder',
37               'delete' => 'Failed to delete cache folder'
38          ),
39          'versions' => array(
40               'delete' => 'Failed to delete version'
41          )
42     );
43
44     public static $dotclear = array(
45          'settings' => array(
46               'antispam','noviny','system','themes','widgets'
47          ),
48          'tables' => array(
49               'blog','category','comment','link','log','media',
50               'meta','permissions','ping','post','post_media','session',
51               'setting','spamrule','user','version'
52          ),
53          'plugins' => array(
54               'aboutConfig','akismet','antispam','blogroll','blowupConfig',
55               'externalMedia','fairTrackbacks','importExport','maintenance',
56               'metadata','pings','themeEditor','widgets'
57          ),
58          'themes' => array(
59               'default','customCSS','blueSilence','noviny'
60          ),
61          'caches' => array(
62               'cbfeed','cbtpl','versions'
63          ),
64          'versions' => array(
65               'antispam','blogroll','core','metadata'
66          )
67     );
68
69     public static $exclude = array(
70          '.','..','__MACOSX','.svn','CVS','.DS_Store','Thumbs.db'
71     );
72
73     public static function getSettings($core)
74     {
75          $res = $core->con->select(
76               'SELECT setting_ns '.
77               'FROM '.$core->prefix.'setting '.
78               'WHERE blog_id IS NULL '.
79               "OR blog_id IS NOT NULL ".
80               'GROUP BY setting_ns');
81
82          $rs = array();
83          $i = 0;
84          while($res->fetch()) {
85
86               $rs[$i]['key'] = $res->setting_ns;
87               $rs[$i]['value'] = $core->con->select(
88                    'SELECT count(*) FROM '.$core->prefix.'setting '.
89                    "WHERE setting_ns = '".$res->setting_ns."' ".
90                    "AND (blog_id IS NULL OR blog_id IS NOT NULL) ".
91                    "GROUP BY setting_ns ")->f(0);
92               $i++;
93          }
94          return $rs;
95     }
96
97     protected static function deleteGlobalSettings($core,$entry)
98     {
99          $core->con->execute(
100               'DELETE FROM '.$core->prefix.'setting '.
101               'WHERE blog_id IS NULL '.
102               "AND setting_ns = '".$core->con->escape($entry)."' "
103          );
104     }
105
106     protected static function deleteLocalSettings($core,$entry)
107     {
108          $core->con->execute(
109               'DELETE FROM '.$core->prefix.'setting '.
110               "WHERE blog_id = '".$core->con->escape($core->blog->id)."' ".
111               "AND setting_ns = '".$core->con->escape($entry)."' "
112          );
113     }
114
115     protected static function deleteAllSettings($core,$entry)
116     {
117          $core->con->execute(
118               'DELETE FROM '.$core->prefix.'setting '.
119               "WHERE setting_ns = '".$core->con->escape($entry)."' ".
120               "AND (blog_id IS NULL OR blog_id != '') "
121          );
122     }
123
124     public static function getTables($core)
125     {
126          $object = dbSchema::init($core->con);
127          $res = $object->getTables();
128
129          $rs = array();
130          $i = 0;
131          foreach($res as $k => $v)
132          {
133               if ('' != $core->prefix)
134               {
135                    if (!preg_match('/^'.$core->prefix.'/',$v)) continue;
136                    $v = substr($v,strlen($core->prefix));
137               }
138               $rs[$i]['key'] = $v;
139               $rs[$i]['value'] = $core->con->select('SELECT count(*) FROM '.$res[$k])->f(0);
140               $i++;
141          }
142          return $rs;
143     }
144
145     protected static function emptyTable($core,$entry)
146     {
147          $core->con->execute(
148               'DELETE FROM '.$core->con->escapeSystem($core->prefix.$entry)
149          );
150     }
151
152     protected static function deleteTable($core,$entry)
153     {
154          self::emptyTable($core,$entry);
155
156          $core->con->execute(
157               'DROP TABLE '.$core->con->escapeSystem($core->prefix.$entry)
158          );
159     }
160
161     public static function getVersions($core)
162     {
163          $res = $core->con->select('SELECT * FROM '.$core->prefix.'version');
164
165          $rs = array();
166          $i = 0;
167          while ($res->fetch()) {
168
169               $rs[$i]['key'] = $res->module;
170               $rs[$i]['value'] = $res->version;
171               $i++;
172          }
173          return $rs;
174     }
175
176     protected static function deleteVersion($core,$entry)
177     {
178          $core->con->execute(
179               'DELETE FROM '.$core->prefix.'version '.
180               "WHERE module = '".$core->con->escape($entry)."' "
181          );
182     }
183
184     public static function getPlugins($core)
185     {
186          $res = explode(PATH_SEPARATOR,DC_PLUGINS_ROOT);
187          return self::getDirs($res);
188     }
189
190     protected static function emptyPlugin($core,$entry)
191     {
192          $res = explode(PATH_SEPARATOR,DC_PLUGINS_ROOT);
193          self::delDir($res,$entry,false);
194     }
195
196     protected static function deletePlugin($core,$entry)
197     {
198          $res = explode(PATH_SEPARATOR,DC_PLUGINS_ROOT);
199          self::delDir($res,$entry,true);
200     }
201
202     public static function getThemes($core)
203     {
204          return self::getDirs($core->blog->themes_path);
205     }
206
207     protected static function emptyTheme($core,$entry)
208     {
209          self::delDir($core->blog->themes_path,$entry,false);
210     }
211
212     protected static function deleteTheme($core,$entry)
213     {
214          self::delDir($core->blog->themes_path,$entry,true);
215     }
216
217     public static function getCaches($core)
218     {
219          return self::getDirs(DC_TPL_CACHE);
220     }
221
222     protected static function emptyCache($core,$entry)
223     {
224          self::delDir(DC_TPL_CACHE,$entry,false);
225     }
226
227     protected static function deleteCache($core,$entry)
228     {
229          self::delDir(DC_TPL_CACHE,$entry,true);
230     }
231
232     public static function execute($core,$type,$action,$ns)
233     {
234          if (strtolower($ns) == 'dcadvancedcleaner')
235               throw new exception("dcAdvancedCleaner can't remove itself");
236
237          # BEHAVIOR dcAdvancedCleanerBeforeAction
238          $core->callBehavior('dcAdvancedCleanerBeforeAction',$type,$action,$ns);
239
240          try {
241               # Delete global settings
242               if ($type == 'settings' && $action == 'delete_global')
243                    self::deleteGlobalSettings($core,$ns);
244
245               # Delete local settings
246               if ($type == 'settings' && $action == 'delete_local')
247                    self::deleteLocalSettings($core,$ns);
248
249               # Delete all settings
250               if ($type == 'settings' && $action == 'delete_all')
251                    self::deleteAllSettings($core,$ns);
252
253               # Empty tables
254               if ($type == 'tables' && $action == 'empty')
255                    self::emptyTable($core,$ns);
256
257               # Delete tables
258               if ($type == 'tables' && $action == 'delete')
259                    self::deleteTable($core,$ns);
260
261               # Delete versions
262               if ($type == 'versions' && $action == 'delete')
263                    self::deleteVersion($core,$ns);
264
265               # Empty plugins
266               if ($type == 'plugins' && $action == 'empty')
267                    self::emptyPlugin($core,$ns);
268
269               # Delete plugins
270               if ($type == 'plugins' && $action == 'delete')
271                    self::deletePlugin($core,$ns);
272
273               # Empty themes
274               if ($type == 'themes' && $action == 'empty')
275                    self::emptyTheme($core,$ns);
276
277               # Delete themes
278               if ($type == 'themes' && $action == 'delete')
279                    self::deleteTheme($core,$ns);
280
281               # Empty caches
282               if ($type == 'caches' && $action == 'empty')
283                    self::emptyCache($core,$ns);
284
285               # Delete caches
286               if ($type == 'caches' && $action == 'delete')
287                    self::deleteCache($core,$ns);
288
289               return true;
290          }
291          catch(Exception $e) {
292               $errors = self::$errors;
293               if (isset($errors[$type][$action])) {
294                    throw new Exception(__($errors[$type][$action]));
295               }
296               else {
297                    throw new Exception(sprintf(__('Cannot execute "%s" of type "%s"'),$action,$type));
298               }
299               return false;
300          }
301     }
302
303     protected static function getDirs($roots)
304     {
305          if (!is_array($roots))
306               $roots = array($roots);
307
308          $rs = array();
309          $i = 0;
310          foreach ($roots as $root) {
311
312               $dirs = files::scanDir($root);
313               foreach($dirs as $k) {
314
315                    if ('.' == $k || '..' == $k || !is_dir($root.'/'.$k)) continue;
316
317                    $rs[$i]['key'] = $k;
318                    $rs[$i]['value'] = count(self::scanDir($root.'/'.$k));
319                    $i++;
320               }
321          }
322          return $rs;
323     }
324
325     protected static function delDir($roots,$folder,$delfolder=true)
326     {
327          if (strpos($folder,'/'))
328               return false;
329
330          if (!is_array($roots))
331               $roots = array($roots);
332
333          foreach ($roots as $root)
334          {
335               if (file_exists($root.'/'.$folder))
336                    return self::delTree($root.'/'.$folder,$delfolder);
337          }
338          return false;
339     }
340
341     protected static function scanDir($path,$dir='',$res=array())
342     {
343          $exclude = self::$exclude;
344
345          $path = path::real($path);
346          if (!is_dir($path) || !is_readable($path)) return array();
347
348          $files = files::scandir($path);
349
350          foreach($files AS $file) {
351               if (in_array($file,$exclude)) continue;
352
353               if (is_dir($path.'/'.$file)) {
354
355                    $res[] = $file;
356                    $res = self::scanDir($path.'/'.$file,$dir.'/'.$file,$res);
357               } else {
358
359                    $res[] = empty($dir) ? $file : $dir.'/'.$file;
360               }
361          }
362          return $res;
363     }
364
365     protected static function delTree($dir,$delroot=true)
366     {
367          if (!is_dir($dir) || !is_readable($dir)) return false;
368
369          if (substr($dir,-1) != '/') $dir .= '/';
370
371          if (($d = @dir($dir)) === false) return false;
372
373          while (($entryname = $d->read()) !== false)
374          {
375               if ($entryname != '.' && $entryname != '..')
376               {
377                    if (is_dir($dir.'/'.$entryname))
378                    {
379                         if (!self::delTree($dir.'/'.$entryname)) return false;
380                    }
381                    else
382                    {
383                         if (!@unlink($dir.'/'.$entryname)) return false;
384                    }
385               }
386          }
387          $d->close();
388
389          if ($delroot)
390               return @rmdir($dir);
391          else
392               return true;
393     }
394}
395?>
Note: See TracBrowser for help on using the repository browser.

Sites map