2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 12/15/2009 *
4 * =================== Last change: 12/15/2009 *
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 *
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. *
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. *
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, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
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... :-)
50 // By default nothing is new... ;-)
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');
59 $ret = $GLOBALS['cache_array']['revision'][$type][0];
60 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . ']=' . $ret);
62 // FQFN of revision file
63 $FQFN = sprintf("%s/.revision", getConfig('CACHE_PATH'));
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
70 // Check for revision file
71 if (!isFileReadable($FQFN)) {
72 // Not found, so we need to create it
75 // Revision file found
76 $ins_vers = explode("\n", readFromFile($FQFN));
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>');
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') {
87 // Generate fake cache entry
88 foreach ($mapper as $map => $idx) {
89 $GLOBALS['cache_array']['revision'][$map][0] = $ins_vers[$idx];
93 $ret = getActualVersion($type);
94 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . ']=' . $ret);
99 // Has it been updated?
102 writeToFile($FQFN, implode("\n", getArrayFromActualVersion()));
104 // ... and call recursive
105 $ret = getActualVersion($type);
106 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . ']=' . $ret);
111 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret[' . $type . ']=' . $ret);
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');
121 // Return the created array
125 // Extracts requested revision info from given file data
126 function extractRevisionInfoFromData ($fileData, $search) {
127 // Default is to return empty string
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);
133 // Make sure only valid and trimmed entries are used
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) {
146 $fileData = readFromFile($FQFN);
148 // Call the extract function and return the result
149 return extractRevisionInfoFromData($fileData, $search);
152 // @TODO Please describe this function
153 function getArrayFromActualVersion () {
155 $GLOBALS['cache_array']['revision'] = array();
160 // Directory to start with search
161 $last_changed = array(
169 // Init value for counting the founded keywords
170 $GLOBALS['revision_res'] = '0';
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');
176 $last_file = readFromFile($last_changed['path_name']);
178 // Save the last-changed filename for debugging
179 $GLOBALS['cache_array']['revision']['File'][0] = $last_changed['path_name'];
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]);
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
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]);
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]);
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');
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));
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';
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];
235 // Return prepared array