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