source: extensions/Prune_History/include/functions.inc.php @ 14838

Last change on this file since 14838 was 14838, checked in by Eric, 12 years ago

Initial commit

  • Property svn:eol-style set to LF
File size: 5.8 KB
Line 
1<?php
2/* Function called from main.inc.php to get the plugin version and name */
3function PHInfos($dir)
4{
5  $path = $dir;
6
7  $plg_data = implode( '', file($path.'main.inc.php') );
8  if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) )
9  {
10    $plugin['name'] = trim( $val[1] );
11  }
12  if (preg_match("|Version: (.*)|", $plg_data, $val))
13  {
14    $plugin['version'] = trim($val[1]);
15  }
16  if ( preg_match("|Plugin URI: (.*)|", $plg_data, $val) )
17  {
18    $plugin['uri'] = trim($val[1]);
19  }
20  if ($desc = load_language('description.txt', $path.'/', array('return' => true)))
21  {
22    $plugin['description'] = trim($desc);
23  }
24  elseif ( preg_match("|Description: (.*)|", $plg_data, $val) )
25  {
26    $plugin['description'] = trim($val[1]);
27  }
28  if ( preg_match("|Author: (.*)|", $plg_data, $val) )
29  {
30    $plugin['author'] = trim($val[1]);
31  }
32  if ( preg_match("|Author URI: (.*)|", $plg_data, $val) )
33  {
34    $plugin['author uri'] = trim($val[1]);
35  }
36  if (!empty($plugin['uri']) and strpos($plugin['uri'] , 'extension_view.php?eid='))
37  {
38    list( , $extension) = explode('extension_view.php?eid=', $plugin['uri']);
39    if (is_numeric($extension)) $plugin['extension'] = $extension;
40  }
41// IMPORTANT SECURITY !
42  $plugin = array_map('htmlspecialchars', $plugin);
43
44  return $plugin ;
45}
46
47
48/* *************************************** */
49/* Update plugin version in conf table     */
50/* Used everytime a new version is updated */
51/* even if no database upgrade is needed   */
52/* *************************************** */
53function PH_version_update()
54{
55  global $conf;
56 
57  load_language('plugin.lang', PH_PATH);
58 
59  // Get current plugin version
60  // --------------------------
61  $plugin =  PHInfos(PH_PATH);
62  $version = $plugin['version'];
63
64  // Upgrading options
65  // -----------------
66  $query = '
67SELECT value
68FROM '.CONFIG_TABLE.'
69WHERE param = "PruneHistory"
70;';
71
72  $result = pwg_query($query);
73  $conf_PH = pwg_db_fetch_assoc($result);
74   
75  $Newconf_PH = unserialize($conf_PH['value']);
76 
77  $Newconf_PH[0] = $version;
78
79  $update_conf = serialize($Newconf_PH);
80
81  conf_update_param('PruneHistory', pwg_db_real_escape_string($update_conf));
82
83
84// Check #_plugin table consistency
85// Only useful if a previous version upgrade has not worked correctly (rare case)
86// ------------------------------------------------------------------------------
87  $query = '
88SELECT version
89  FROM '.PLUGINS_TABLE.'
90WHERE id = "PruneHistory"
91;';
92 
93  $data = pwg_db_fetch_assoc(pwg_query($query));
94 
95  if (empty($data['version']) or $data['version'] <> $version)
96  {
97    $query = '
98UPDATE '.PLUGINS_TABLE.'
99SET version="'.$version.'"
100WHERE id = "PruneHistory"
101LIMIT 1
102;';
103
104    pwg_query($query);
105  }
106}
107
108
109/**
110 * Delete obsolete files on plugin upgrade
111 * Obsolete files are listed in file obsolete.list
112 *
113 */
114function PH_Obsolete_Files()
115{
116  if (file_exists(PH_PATH.'obsolete.list')
117    and $old_files = file(PH_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES)
118    and !empty($old_files))
119  {
120    array_push($old_files, 'obsolete.list');
121    foreach($old_files as $old_file)
122    {
123      $path = PH_PATH.$old_file;
124      if (is_file($path))
125      {
126        @unlink($path);
127      }
128      elseif (is_dir($path))
129      {
130        @rmdir($path);
131      }
132    }
133  }
134}
135
136
137/**
138 * Manual prune function
139 *
140 * @param : Start and stop range to prune
141 *
142 * @return : boolean
143 */
144function history_prune($startdate,$stopdate)
145{
146  list($year, $month, $day) = explode('-', $startdate); // explode date of $startdate                                           
147        $starttimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp
148
149  list($year, $month, $day) = explode('-', $stopdate); // explode date of $stopdate                                             
150        $stoptimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp
151
152  if ($starttimestamp < $stoptimestamp)
153  {
154    $query = '
155DELETE
156FROM '.HISTORY_TABLE.'
157WHERE date < "'.$stopdate.'"
158AND date > "'.$startdate.'"
159AND summarized = "true";';
160
161    $r = pwg_query($query);
162
163    if ($r)
164    {
165      $query = 'OPTIMIZE TABLE '.HISTORY_TABLE.';';
166      pwg_query($query);
167    }
168    return true;
169  }
170  else if ($starttimestamp == $stoptimestamp)
171  {
172    $query = '
173DELETE FROM '.HISTORY_TABLE.'
174WHERE date = "'.$stopdate.'"
175AND summarized = "true";';
176
177    $r = pwg_query($query);
178
179    if ($r)
180    {
181      $query = 'OPTIMIZE TABLE '.HISTORY_TABLE.';';
182      pwg_query($query);
183    }
184    return true;
185  }
186
187  return false;
188}
189
190
191/**
192 * Automatic prune function
193 *
194 * @param : Start and stop range to prune
195 *
196 * @return : boolean
197 */
198function history_autoprune()
199{
200  global $conf;
201 
202  $conf_PH = unserialize($conf['PruneHistory']);
203
204  // Definition of the range to keep
205  if (isset($conf_PH[1]) and $conf_PH[1] == 'true' and isset($conf_PH[2]) and $conf_PH[2] <> '0' and isset($conf_PH[3]) and $conf_PH[3] <> '0')
206  {
207    if ($conf_PH[3] == '1') // Ranged for days
208    {
209      $limit = mktime(0, 0, 0, date("m") , date("d")-$conf_PH[2], date("Y"));
210      $limit = date('Y-m-d', $limit);
211    }
212    else if ($conf_PH[3] == '2') // Ranged for months
213    {
214      $limit = mktime(0, 0, 0, date("m")-$conf_PH[2] , date("d"), date("Y"));
215      $limit = date('Y-m-d', $limit);
216    }
217    else if  ($conf_PH[3] == '3') // Ranged for years
218    {
219      $limit = mktime(0, 0, 0, date("m") , date("d"), date("Y")-$conf_PH[2]);
220      $limit = date('Y-m-d', $limit);
221    }
222  }
223
224  $query = '
225DELETE FROM '.HISTORY_TABLE.'
226WHERE date < "'.$limit.'"
227AND summarized = "true";';
228
229  $r = pwg_query($query);
230
231  if($r)
232        {
233    $query = 'OPTIMIZE TABLE '.HISTORY_TABLE.';';
234    pwg_query($query);
235  }
236}
237
238
239
240/**
241 * Useful for debugging - 4 vars can be set
242 * Output result to log.txt file
243 *
244 */
245function PHLog($var1, $var2, $var3, $var4)
246{
247   $fo=fopen (PH_PATH.'log.txt','a') ;
248   fwrite($fo,"======================\n") ;
249   fwrite($fo,'le ' . date('D, d M Y H:i:s') . "\r\n");
250   fwrite($fo,$var1 ."\r\n") ;
251   fwrite($fo,$var2 ."\r\n") ;
252   fwrite($fo,$var3 ."\r\n") ;
253   fwrite($fo,$var4 ."\r\n") ;
254   fclose($fo) ;
255}
256?>
Note: See TracBrowser for help on using the repository browser.