Dotclear

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

Revision 1980, 8.3 KB checked in by JcDenis, 14 years ago (diff)

dcAdvancedCleaner 0.3:

  • Fixed localized strings
  • Prepared DC 2.2 break
  • Added versioning
  • Added support for plugin activityReport
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of dcAdvancedCleaner, a plugin for Dotclear 2.
4#
5# Copyright (c) 2009 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     public static $dotclear = array(
18          'settings' => array(
19               'antispam','noviny','system','themes','widgets'
20          ),
21          'tables' => array(
22               'blog','category','comment','link','log','media',
23               'meta','permissions','ping','post','post_media','session',
24               'setting','spamrule','user','version'
25          ),
26          'plugins' => array(
27               'aboutConfig','akismet','antispam','blogroll','blowupConfig',
28               'externalMedia','fairTrackbacks','importExport','maintenance',
29               'metadata','pings','themeEditor','widgets'
30          ),
31          'themes' => array(
32               'default','customCSS','blueSilence','noviny'
33          ),
34          'caches' => array(
35               'cbfeed','cbtpl','versions'
36          ),
37          'versions' => array(
38               'antispam','blogroll','core','metadata'
39          )
40     );
41
42     public static $exclude = array(
43          '.','..','__MACOSX','.svn','CVS','.DS_Store','Thumbs.db'
44     );
45
46     public static function getSettings($core)
47     {
48          $res = $core->con->select(
49               'SELECT setting_ns '.
50               'FROM '.$core->prefix.'setting '.
51               'WHERE blog_id IS NULL '.
52               "OR blog_id IS NOT NULL ".
53               'GROUP BY setting_ns');
54
55          $rs = array();
56          $i = 0;
57          while($res->fetch()) {
58
59               $rs[$i]['key'] = $res->setting_ns;
60               $rs[$i]['value'] = $core->con->select(
61                    'SELECT count(*) FROM '.$core->prefix.'setting '.
62                    "WHERE setting_ns = '".$res->setting_ns."' ".
63                    "AND (blog_id IS NULL OR blog_id IS NOT NULL) ".
64                    "GROUP BY setting_ns ")->f(0);
65               $i++;
66          }
67          return $rs;
68     }
69
70     protected static function deleteGlobalSettings($core,$entry)
71     {
72          return $core->con->execute(
73               'DELETE FROM '.$core->prefix.'setting '.
74               'WHERE blog_id IS NULL '.
75               "AND setting_ns = '".$core->con->escape($entry)."' ");
76     }
77
78     protected static function deleteLocalSettings($core,$entry)
79     {
80          return $core->con->execute(
81               'DELETE FROM '.$core->prefix.'setting '.
82               "WHERE blog_id = '".$core->con->escape($core->blog->id)."' ".
83               "AND setting_ns = '".$core->con->escape($entry)."' ");
84     }
85
86     protected static function deleteAllSettings($core,$entry)
87     {
88          return $core->con->execute(
89               'DELETE FROM '.$core->prefix.'setting '.
90               "WHERE setting_ns = '".$core->con->escape($entry)."' ".
91               "AND (blog_id IS NULL OR blog_id != '') ");
92     }
93
94     public static function getTables($core)
95     {
96          $object = dbSchema::init($core->con);
97          $res = $object->getTables();
98
99          $rs = array();
100          $i = 0;
101          foreach($res as $v) {
102
103               $rs[$i]['key'] = substr($v,strlen($core->prefix));
104               $rs[$i]['value'] = $core->con->select('SELECT count(*) FROM '.$v)->f(0);
105               $i++;
106          }
107          return $rs;
108     }
109
110     protected static function emptyTable($core,$entry)
111     {
112          return (boolean) $core->con->execute(
113               'DELETE FROM '.$core->con->escapeSystem($core->prefix.$entry));
114     }
115
116     protected static function deleteTable($core,$entry)
117     {
118          if (self::emptyTable($core,$entry)) {
119               return (boolean) $core->con->execute(
120                    'DROP TABLE '.$core->con->escapeSystem($core->prefix.$entry));
121          }
122          return false;
123     }
124
125     public static function getVersions($core)
126     {
127          $res = $core->con->select('SELECT * FROM '.$core->prefix.'version');
128
129          $rs = array();
130          $i = 0;
131          while ($res->fetch()) {
132
133               $rs[$i]['key'] = $res->module;
134               $rs[$i]['value'] = $res->version;
135               $i++;
136          }
137          return $rs;
138     }
139
140     protected static function deleteVersion($core,$entry)
141     {
142          return (boolean) $core->con->execute(
143               'DELETE FROM '.$core->prefix.'version '.
144               "WHERE module = '".$core->con->escape($entry)."' ");
145     }
146
147     public static function getPlugins($core)
148     {
149          $res = explode(PATH_SEPARATOR,DC_PLUGINS_ROOT);
150          return self::getDirs($res);
151     }
152
153     protected static function emptyPlugin($core,$entry)
154     {
155          $res = explode(PATH_SEPARATOR,DC_PLUGINS_ROOT);
156          return self::delDir($res,$entry,false);
157     }
158
159     protected static function deletePlugin($core,$entry)
160     {
161          $res = explode(PATH_SEPARATOR,DC_PLUGINS_ROOT);
162          return self::delDir($res,$entry,true);
163     }
164
165     public static function getThemes($core)
166     {
167          return self::getDirs($core->blog->themes_path);
168     }
169
170     protected static function emptyTheme($core,$entry)
171     {
172          return self::delDir($core->blog->themes_path,$entry,false);
173     }
174
175     protected static function deleteTheme($core,$entry)
176     {
177          return self::delDir($core->blog->themes_path,$entry,true);
178     }
179
180     public static function getCaches($core)
181     {
182          return self::getDirs(DC_TPL_CACHE);
183     }
184
185     protected static function emptyCache($core,$entry)
186     {
187          return self::delDir(DC_TPL_CACHE,$entry,false);
188     }
189
190     protected static function deleteCache($core,$entry)
191     {
192          return self::delDir(DC_TPL_CACHE,$entry,true);
193     }
194
195     public static function execute($core,$type,$action,$ns)
196     {
197          if (strtolower($ns) == 'dcadvancedcleaner')
198               throw new exception("dcAdvancedCleaner can't remove itself");
199
200          # BEHAVIOR dcAdvancedCleanerBeforeAction
201          $core->callBehavior('dcAdvancedCleanerBeforeAction',$type,$action,$ns);
202
203          # Delete global settings
204          if ($type == 'settings' && $action == 'delete_global')
205               self::deleteGlobalSettings($core,$ns);
206
207          # Delete local settings
208          if ($type == 'settings' && $action == 'delete_local')
209               self::deleteLocalSettings($core,$ns);
210
211          # Delete all settings
212          if ($type == 'settings' && $action == 'delete_all')
213               self::deleteAllSettings($core,$ns);
214
215          # Empty tables
216          if ($type == 'tables' && $action == 'empty')
217               self::emptyTable($core,$ns);
218
219          # Delete tables
220          if ($type == 'tables' && $action == 'delete')
221               self::deleteTable($core,$ns);
222
223          # Delete versions
224          if ($type == 'versions' && $action == 'delete')
225               self::deleteVersion($core,$ns);
226
227          # Empty plugins
228          if ($type == 'plugins' && $action == 'empty')
229               self::emptyPlugin($core,$ns);
230
231          # Delete plugins
232          if ($type == 'plugins' && $action == 'delete')
233               self::deletePlugin($core,$ns);
234
235          # Empty themes
236          if ($type == 'themes' && $action == 'empty')
237               self::emptyTheme($core,$ns);
238
239          # Delete themes
240          if ($type == 'themes' && $action == 'delete')
241               self::deleteTheme($core,$ns);
242
243          # Empty caches
244          if ($type == 'caches' && $action == 'empty')
245               self::emptyCache($core,$ns);
246
247          # Delete caches
248          if ($type == 'caches' && $action == 'delete')
249               self::deleteCache($core,$ns);
250     }
251
252     protected static function getDirs($roots)
253     {
254          if (!is_array($roots))
255               $roots = array($roots);
256
257          $rs = array();
258          $i = 0;
259          foreach ($roots as $root) {
260
261               $dirs = files::scanDir($root);
262               foreach($dirs as $k) {
263
264                    if ('.' == $k || '..' == $k || !is_dir($root.'/'.$k)) continue;
265
266                    $rs[$i]['key'] = $k;
267                    $rs[$i]['value'] = count(self::scanDir($root.'/'.$k));
268                    $i++;
269               }
270          }
271          return $rs;
272     }
273
274     protected static function delDir($roots,$folder,$delfolder=true)
275     {
276          if (strpos($folder,'/'))
277               return false;
278
279          if (!is_array($roots))
280               $roots = array($roots);
281
282          foreach ($roots as $root)
283          {
284               if (file_exists($root.'/'.$folder))
285                    return self::delTree($root.'/'.$folder,$delfolder);
286          }
287          return false;
288     }
289
290     protected static function scanDir($path,$dir='',$res=array())
291     {
292          $exclude = self::$exclude;
293
294          $path = path::real($path);
295          if (!is_dir($path) || !is_readable($path)) return array();
296
297          $files = files::scandir($path);
298
299          foreach($files AS $file) {
300               if (in_array($file,$exclude)) continue;
301
302               if (is_dir($path.'/'.$file)) {
303
304                    $res[] = $file;
305                    $res = self::scanDir($path.'/'.$file,$dir.'/'.$file,$res);
306               } else {
307
308                    $res[] = empty($dir) ? $file : $dir.'/'.$file;
309               }
310          }
311          return $res;
312     }
313
314     protected static function delTree($dir,$delroot=true)
315     {
316          $current_dir = opendir($dir);
317          while($entryname = readdir($current_dir)) {
318
319               if (is_dir($dir.'/'.$entryname) && ($entryname != '.' && $entryname!='..')) {
320
321                    if (!self::delTree($dir.'/'.$entryname)) return false;
322               }
323               elseif ($entryname != '.' && $entryname!='..') {
324
325                    if (!@unlink($dir.'/'.$entryname)) return false;
326               }
327          }
328          closedir($current_dir);
329
330          if ($delroot)
331               return @rmdir($dir);
332          else
333               return true;
334     }
335}
336?>
Note: See TracBrowser for help on using the repository browser.

Sites map