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