Introduced wrapper function addCreateTableSql(), fixed parameter order:
[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 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://www.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
60 // "Getter" for revision/version data
61 function getRepositoryData ($type = 'Revision') {
62         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . '] - ENTRY!');
63         // Default is an invalid value to find bugs... :-)
64         $ret = 'INVALID';
65
66         // By default nothing is new... ;-)
67         $new = false;
68
69         // Is the cache entry there?
70         if (isset($GLOBALS['cache_array']['revision'][$type][0])) {
71                 // Found so increase cache hit
72                 incrementStatsEntry('cache_hits');
73
74                 // Return it
75                 $ret = $GLOBALS['cache_array']['revision'][$type][0];
76                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . ']=' . $ret);
77         } else {
78                 // FQFN of revision file
79                 $FQFN = sprintf("%s/.revision", getCachePath());
80
81                 // Check if 'check_revision_data' is setted (switch for manually rewrite the .revision-File)
82                 if ((isGetRequestParameterSet('check_revision_data')) && (getRequestParameter('check_revision_data') == 'yes')) {
83                         // Forced rebuild of .revision file
84                         $new = true;
85                 } else {
86                         // Check for revision file
87                         if (!isFileReadable($FQFN)) {
88                                 // Not found, so we need to create it
89                                 $new = true;
90                         } else {
91                                 // Revision file found
92                                 $ins_vers = explode("\n", readFromFile($FQFN));
93
94                                 // Get array for mapping information
95                                 $mapper = array_flip(getSearchFor());
96                                 //* DEBUG: */ debugOutput('<pre>mapper='.print_r($mapper, true).'</pre>ins_vers=<pre>'.print_r($ins_vers, true).'</pre>');
97
98                                 // Is the content valid?
99                                 if ((!is_array($ins_vers)) || (count($ins_vers) <= 0) || (!isset($ins_vers[$mapper[$type]])) || (trim($ins_vers[$mapper[$type]]) == '') || ($ins_vers[0]) == 'new') {
100                                         // File needs update!
101                                         $new = true;
102                                 } else {
103                                         // Generate fake cache entry
104                                         foreach ($mapper as $map => $idx) {
105                                                 $GLOBALS['cache_array']['revision'][$map][0] = $ins_vers[$idx];
106                                         } // END - foreach
107
108                                         // Return found value
109                                         $ret = getRepositoryData($type);
110                                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . ']=' . $ret);
111                                 }
112                         }
113                 }
114
115                 // Has it been updated?
116                 if ($new === true)  {
117                         // Write it
118                         writeToFile($FQFN, implode("\n", getArrayFromRepositoryData()));
119
120                         // ... and call recursive
121                         $ret = getRepositoryData($type);
122                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . ']=' . $ret);
123                 } // END - if
124         }
125
126         // Return the value
127         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . ']=' . $ret);
128         return $ret;
129 }
130
131 // Repares an array we are looking for
132 // The returned Array is needed twice (in getArrayFromRepositoryData() and in getRepositoryData() in the old .revision-fallback) so I puted it in an extra function to not polute the global namespace
133 function getSearchFor () {
134         // Add Revision, Date, Tag and Author
135         $searchFor = array('File', 'Revision', 'Date', 'Tag', 'Author');
136
137         // Return the created array
138         return $searchFor;
139 }
140
141 // Extracts requested revision info from given file data
142 function extractRevisionInfoFromData ($fileData, $search) {
143         // Default is to return empty string
144         $ret = '';
145
146         // Searches for "$search-tag:VALUE$" or "$search-tag::VALUE$"(the stylish keywordversion ;-)) in the lates modified file
147         $GLOBALS['revision_res'] += preg_match('@\$' . $search . '(:|::) (.*) \$@U', $fileData, $t);
148
149         // Make sure only valid and trimmed entries are used
150         if (isset($t[2])) {
151                 $ret = trim($t[2]);
152         } // END - if
153
154         // Return the result
155         return $ret;
156 }
157
158 // Extracts requested revison info for given file name by reading it's content
159 // and parsing it with extractRevisionInfoFromData().
160 function extractRevisionInfoFromFile ($FQFN, $search) {
161         // Read the file
162         $fileData = readFromFile($FQFN);
163
164         // Call the extract function and return the result
165         return extractRevisionInfoFromData($fileData, $search);
166 }
167
168 // Gets an array back for current repository data.
169 // @TODO This function does also set and get in 'cache_array'
170 function getArrayFromRepositoryData () {
171         // Init array
172         $GLOBALS['cache_array']['revision'] = array();
173
174         // Init variables
175         $next_dir = '';
176
177         // Directory to start with search
178         $last_changed = array(
179                 'path_name' => '',
180                 'time'      => 0
181         );
182
183         // Init return array
184         $akt_vers = array();
185
186         // Init value for counting the founded keywords
187         $GLOBALS['revision_res'] = '0';
188
189         // Searches all Files and there date of the last modifikation and puts the newest File in $last_changed.
190         searchDirsRecursive($next_dir, $last_changed, 'Revision');
191
192         // Get file
193         $last_file = readFromFile($last_changed['path_name']);
194
195         // Save the last-changed filename for debugging
196         $GLOBALS['cache_array']['revision']['File'][0] = $last_changed['path_name'];
197
198         // This foreach loops the $searchFor-Tags (array('Revision', 'Date', 'Tag', 'Author') --> could easaly extended in the future)
199         foreach (getSearchFor() as $search) {
200                 // This extracts the requested data $search from file data $last_file
201                 if ($search != 'File') {
202                         // Skip 'File' because we have set it some lines above
203                         $GLOBALS['cache_array']['revision'][$search][0] = extractRevisionInfoFromData($last_file, $search);
204                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'search=' . $search . ',data=' . $GLOBALS['cache_array']['revision'][$search][0]);
205                 } // END - if
206         } // END - foreach
207
208         // at least 3 keyword-Tags are needed for propper values
209         if ($GLOBALS['revision_res'] && $GLOBALS['revision_res'] >= 3
210         && isset($GLOBALS['cache_array']['revision']['Revision'][0]) && !empty($GLOBALS['cache_array']['revision']['Revision'][0])
211         && isset($GLOBALS['cache_array']['revision']['Date'][0]) && !empty($GLOBALS['cache_array']['revision']['Date'][0])
212         && isset($GLOBALS['cache_array']['revision']['Tag'][0]) && !empty($GLOBALS['cache_array']['revision']['Tag'][0])) {
213                 // Prepare content witch need special treadment
214
215                 // Prepare timestamp for date
216                 preg_match('@(....)-(..)-(..) (..):(..):(..)@', $GLOBALS['cache_array']['revision']['Date'][0], $match_d);
217                 $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]);
218
219                 // Add author to the Tag if the author is set and is not quix0r (lead coder)
220                 if ((isset($GLOBALS['cache_array']['revision']['Author'][0])) && ($GLOBALS['cache_array']['revision']['Author'][0] != 'quix0r')) {
221                         $GLOBALS['cache_array']['revision']['Tag'][0] .= '-'.strtoupper($GLOBALS['cache_array']['revision']['Author'][0]);
222                 } // END - if
223
224         } else {
225                 // No valid Data from the last modificated file so read the Revision from the Server. Fallback-solution!! Should not be removed I think.
226                 $version = sendGetRequest('check-updates3.php');
227
228                 // Invalid request reply?
229                 if (!isset($version[11])) {
230                         // Cannot continue here
231                         debug_report_bug(__FUNCTION__, __LINE__, 'Invalid response from check-updates3.php, count should be at least 11, is ' . count($version));
232                 } // END - if
233
234                 // Prepare content
235                 // Only sets not setted or not proper values to the Online-Server-Fallback-Solution
236                 foreach (getSearchFor() as $entry) {
237                         // Is it not set or empty?
238                         if ((!isset($GLOBALS['cache_array']['revision'][$entry][0])) || (empty($GLOBALS['cache_array']['revision']['File'][0]))) {
239                                 // Is default data entry nummerical?
240                                 if (is_numeric($GLOBALS['default_repository_data'][$entry])) {
241                                         // Use entry from $version
242                                         $GLOBALS['cache_array']['revision'][$entry][0] = trim($version[$GLOBALS['default_repository_data'][$entry]]);
243                                 } else {
244                                         // Non-numeric -> use it directly
245                                         $GLOBALS['cache_array']['revision'][$entry][0] = $GLOBALS['default_repository_data'][$entry];
246                                 }
247                         } // END - if
248                 } // END - if
249         }
250
251         // Temporary remove [0] from array
252         $array = $GLOBALS['cache_array']['revision'];
253         foreach ($array as $key => $value) {
254                 if ((is_array($value)) && (isset($value[0]))) {
255                         unset($array[$key][0]);
256                         $array[$key] = $value[0];
257                 } // END - if
258         } // END - if
259
260         // Return prepared array
261         return $array;
262 }
263
264 // [EOF]
265 ?>