Template improved, old lost code removed, some fixes
[mailer.git] / inc / revision-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 12/15/2009 *
4  * ===================                          Last change: 12/15/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : revision-functions.php                           *
8  * -------------------------------------------------------------------- *
9  * Short description : Revison-info related functions                   *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Revsions-Info relevante Funktionen               *
12  * -------------------------------------------------------------------- *
13  * $Revision:: 1645                                                   $ *
14  * $Date:: 2009-12-06 15:30:15 +0100 (Sun, 06 Dec 2009)               $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author:: quix0r                                                   $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } // END - if
43
44 // "Getter" for revision/version data
45 function getActualVersion ($type = 'Revision') {
46         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . '] - ENTRY!');
47         // Default is an invalid value to find bugs... :-)
48         $ret = 'INVALID';
49
50         // By default nothing is new... ;-)
51         $new = false;
52
53         // Is the cache entry there?
54         if (isset($GLOBALS['cache_array']['revision'][$type][0])) {
55                 // Found so increase cache hit
56                 incrementStatsEntry('cache_hits');
57
58                 // Return it
59                 $ret = $GLOBALS['cache_array']['revision'][$type][0];
60                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . ']=' . $ret);
61         } else {
62                 // FQFN of revision file
63                 $FQFN = sprintf("%s/.revision", getConfig('CACHE_PATH'));
64
65                 // Check if 'check_revision_data' is setted (switch for manually rewrite the .revision-File)
66                 if ((isGetRequestParameterSet('check_revision_data')) && (getRequestParameter('check_revision_data') == 'yes')) {
67                         // Forced rebuild of .revision file
68                         $new = true;
69                 } else {
70                         // Check for revision file
71                         if (!isFileReadable($FQFN)) {
72                                 // Not found, so we need to create it
73                                 $new = true;
74                         } else {
75                                 // Revision file found
76                                 $ins_vers = explode("\n", readFromFile($FQFN));
77
78                                 // Get array for mapping information
79                                 $mapper = array_flip(getSearchFor());
80                                 //* DEBUG: */ print('<pre>mapper='.print_r($mapper, true).'</pre>ins_vers=<pre>'.print_r($ins_vers, true).'</pre>');
81
82                                 // Is the content valid?
83                                 if ((!is_array($ins_vers)) || (count($ins_vers) <= 0) || (!isset($ins_vers[$mapper[$type]])) || (trim($ins_vers[$mapper[$type]]) == '') || ($ins_vers[0]) == 'new') {
84                                         // File needs update!
85                                         $new = true;
86                                 } else {
87                                         // Generate fake cache entry
88                                         foreach ($mapper as $map => $idx) {
89                                                 $GLOBALS['cache_array']['revision'][$map][0] = $ins_vers[$idx];
90                                         } // END - foreach
91
92                                         // Return found value
93                                         $ret = getActualVersion($type);
94                                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . ']=' . $ret);
95                                 }
96                         }
97                 }
98
99                 // Has it been updated?
100                 if ($new === true)  {
101                         // Write it
102                         writeToFile($FQFN, implode("\n", getArrayFromActualVersion()));
103
104                         // ... and call recursive
105                         $ret = getActualVersion($type);
106                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . ']=' . $ret);
107                 } // END - if
108         }
109
110         // Return the value
111         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . ']=' . $ret);
112         return $ret;
113 }
114
115 // Repares an array we are looking for
116 // The returned Array is needed twice (in getArrayFromActualVersion() and in getActualVersion() in the old .revision-fallback) so I puted it in an extra function to not polute the global namespace
117 function getSearchFor () {
118         // Add Revision, Date, Tag and Author
119         $searchFor = array('File', 'Revision', 'Date', 'Tag', 'Author');
120
121         // Return the created array
122         return $searchFor;
123 }
124
125 // Extracts requested revision info from given file data
126 function extractRevisionInfoFromData ($fileData, $search) {
127         // Default is to return empty string
128         $ret = '';
129
130         // Searches for "$search-tag:VALUE$" or "$search-tag::VALUE$"(the stylish keywordversion ;-)) in the lates modified file
131         $GLOBALS['revision_res'] += preg_match('@\$' . $search . '(:|::) (.*) \$@U', $fileData, $t);
132
133         // Make sure only valid and trimmed entries are used
134         if (isset($t[2])) {
135                 $ret = trim($t[2]);
136         } // END - if
137
138         // Return the result
139         return $ret;
140 }
141
142 // Extracts requested revison info for given file name by reading it's content
143 // and parsing it with extractRevisionInfoFromData().
144 function extractRevisionInfoFromFile ($FQFN, $search) {
145         // Read the file
146         $fileData = readFromFile($FQFN);
147
148         // Call the extract function and return the result
149         return extractRevisionInfoFromData($fileData, $search);
150 }
151
152 // @TODO Please describe this function
153 function getArrayFromActualVersion () {
154         // Init array
155         $GLOBALS['cache_array']['revision'] = array();
156
157         // Init variables
158         $next_dir = '';
159
160         // Directory to start with search
161         $last_changed = array(
162                 'path_name' => '',
163                 'time'      => 0
164         );
165
166         // Init return array
167         $akt_vers = array();
168
169         // Init value for counting the founded keywords
170         $GLOBALS['revision_res'] = '0';
171
172         // Searches all Files and there date of the last modifikation and puts the newest File in $last_changed.
173         searchDirsRecursive($next_dir, $last_changed, 'Revision');
174
175         // Get file
176         $last_file = readFromFile($last_changed['path_name']);
177
178         // Save the last-changed filename for debugging
179         $GLOBALS['cache_array']['revision']['File'][0] = $last_changed['path_name'];
180
181         // This foreach loops the $searchFor-Tags (array('Revision', 'Date', 'Tag', 'Author') --> could easaly extended in the future)
182         foreach (getSearchFor() as $search) {
183                 // This extracts the requested data $search from file data $last_file
184                 if ($search != 'File') {
185                         // Skip 'File' because we have set it some lines above
186                         $GLOBALS['cache_array']['revision'][$search][0] = extractRevisionInfoFromData($last_file, $search);
187                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'search=' . $search . ',data=' . $GLOBALS['cache_array']['revision'][$search][0]);
188                 } // END - if
189         } // END - foreach
190
191         // at least 3 keyword-Tags are needed for propper values
192         if ($GLOBALS['revision_res'] && $GLOBALS['revision_res'] >= 3
193         && isset($GLOBALS['cache_array']['revision']['Revision'][0]) && $GLOBALS['cache_array']['revision']['Revision'][0] != ''
194         && isset($GLOBALS['cache_array']['revision']['Date'][0]) && $GLOBALS['cache_array']['revision']['Date'][0] != ''
195         && isset($GLOBALS['cache_array']['revision']['Tag'][0]) && $GLOBALS['cache_array']['revision']['Tag'][0] != '') {
196                 // Prepare content witch need special treadment
197
198                 // Prepare timestamp for date
199                 preg_match('@(....)-(..)-(..) (..):(..):(..)@', $GLOBALS['cache_array']['revision']['Date'][0], $match_d);
200                 $GLOBALS['cache_array']['revision']['Date'][0] = mktime($match_d[4], $match_d[5], $match_d[6], $match_d[2], $match_d[3], $match_d[1]);
201
202                 // Add author to the Tag if the author is set and is not quix0r (lead coder)
203                 if ((isset($GLOBALS['cache_array']['revision']['Author'][0])) && ($GLOBALS['cache_array']['revision']['Author'][0] != 'quix0r')) {
204                         $GLOBALS['cache_array']['revision']['Tag'][0] .= '-'.strtoupper($GLOBALS['cache_array']['revision']['Author'][0]);
205                 } // END - if
206
207         } else {
208                 // No valid Data from the last modificated file so read the Revision from the Server. Fallback-solution!! Should not be removed I think.
209                 $version = sendGetRequest('check-updates3.php');
210
211                 // Invalid request reply?
212                 if (!isset($version[11])) {
213                         // Cannot continue here
214                         debug_report_bug('Invalid response from check-updates3.php, count should be 10+, is ' . count($version));
215                 } // END - if
216
217                 // Prepare content
218                 // Only sets not setted or not proper values to the Online-Server-Fallback-Solution
219                 if (!isset($GLOBALS['cache_array']['revision']['File'][0])     || $GLOBALS['cache_array']['revision']['File'][0]     == '') $GLOBALS['cache_array']['revision']['File'][0]     = trim($version[11]);
220                 if (!isset($GLOBALS['cache_array']['revision']['Revision'][0]) || $GLOBALS['cache_array']['revision']['Revision'][0] == '') $GLOBALS['cache_array']['revision']['Revision'][0] = trim($version[10]);
221                 if (!isset($GLOBALS['cache_array']['revision']['Date'][0])     || $GLOBALS['cache_array']['revision']['Date'][0]     == '') $GLOBALS['cache_array']['revision']['Date'][0]     = trim($version[9]);
222                 if (!isset($GLOBALS['cache_array']['revision']['Tag'][0])      || $GLOBALS['cache_array']['revision']['Tag'][0]      == '') $GLOBALS['cache_array']['revision']['Tag'][0]      = trim($version[8]);
223                 if (!isset($GLOBALS['cache_array']['revision']['Author'][0])   || $GLOBALS['cache_array']['revision']['Author'][0]   == '') $GLOBALS['cache_array']['revision']['Author'][0]   = 'quix0r';
224         }
225
226         // Temporary remove [0] from array
227         $array = $GLOBALS['cache_array']['revision'];
228         foreach ($array as $key => $value) {
229                 if ((is_array($value)) && (isset($value[0]))) {
230                         unset($array[$key][0]);
231                         $array[$key] = $value[0];
232                 } // END - if
233         } // END - if
234
235         // Return prepared array
236         return $array;
237 }
238
239 // [EOF]
240 ?>