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