functions.inc.php
11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/*
This is phpSitemapNG, a php script that creates your personal google sitemap
It can be downloaded from http://enarion.net/google/
License: GPL
Tobias Kluge, enarion.net
*/
/**
* misc functions
*/
function info($param, $msg = '') {
global $SETTINGS, $LAYOUT;
if ($param == "" && $msg == "")
return;
if (is_array($param)) {
$LAYOUT->addInfo(arrToStringReadable($param, "<br>\n"),$msg);
} else {
$LAYOUT->addInfo($param, $msg);
}
}
function debug($param, $msg = '') {
global $SETTINGS, $LAYOUT;
if (isset($_SESSION[PSNG_DEBUG]) && isset($SETTINGS[PSNG_DEBUG]))
{
if ($SETTINGS[PSNG_DEBUG] === TRUE && $_SESSION[PSNG_DEBUG] === TRUE) {
if ($param == "" && $msg == "")
return;
if (is_array($param)) {
$LAYOUT->addDebug(arrToStringReadable($param, "<br>\n"),$msg);
} else {
$LAYOUT->addDebug($param, $msg);
}
}
}
}
// source: http://de2.php.net/microtime
function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
/**
* returns a filehandle if file is accessable
*/
function openFile($filename, $writable = FALSE) {
global $openFile_error;
$openFile_error = "";
// check if file exists - if yes, perform tests:
if (file_exists($filename)) {
// check if file is accessable
if (!is_readable($filename)) {
$openFile_error = "File $filename is not readable";
return FALSE;
}
if ($writable && !is_writable($filename)) {
$openFile_error = "File $filename is not writable";
return FALSE;
}
} else {
// file does not exist, try to create file
}
$accessLevel = 'r+';
if ($writable === TRUE) {
$accessLevel = 'w+';
}
$filehandle = @fopen($filename, $accessLevel);
if ($filehandle === FALSE) {
$openFile_error = "File $filename could not be opened, don't know why";
@fclose($filehandle);
if (!file_exists($filename)) {
$openFile_error = "File $filename does not exist and I do not have the rights to create it!";
}
return FALSE;
}
return $filehandle;
}
/**
* start session
* read settings from session
* set and return action
*/
function init() {
global $SETTINGS, $_REQUEST, $LAYOUT;
session_start();
// set layout engine
$LAYOUT = new LayoutEngine("phpSitemapNG");
$LAYOUT->addContentFooter('<div align="center"><p>Copyright by enarion.net. This script is licensed under GPL and can be downloaded from
<a target="_blank" href="http://enarion.net/google/">enarion.net/google/</a></p></div>');
$LAYOUT->setTitle("create your personal google sitemap file");
$LAYOUT->setCharSet("iso-8859-1");
$LAYOUT->addCss('.history, .required { background-color:#E0E0E0; }');
$LAYOUT->addCss('.source_fs { background-color:#FF70CC; }');
$LAYOUT->addCss('.source_website { background-color:#CCFF70; }');
$LAYOUT->addCss('.source_fs_website { background-color:#70CCFF; }');
$LAYOUT->addCss('.notfound { background-color:#FF3030; }');
$LAYOUT->addCss('Label {color:#000099; font-weight: bold; }');
$LAYOUT->addCss('h1,h2,h3 {color:#000099; }');
$LAYOUT->addCss('.error {color:#cc0000; font-weight: bold; }');
$LAYOUT->addCss('.warning {color:#000000; font-weight: italic; }');
$LAYOUT->addCss('.info {color:#000000; font-weight: normal; }');
$LAYOUT->addCss('.success {color:#009900; font-weight: bold; }');
$LAYOUT->addCss('body {color:#000000; font-family:helvetica; background-color:#ebb150; }');
$LAYOUT->switchOffBuffer();
print $LAYOUT->getHeaderLayout();
/* repair NOTICES mk/2005-11-08 */
if (isset($_REQUEST[PSNG_DEBUG]))
{
/**/
if ($_REQUEST[PSNG_DEBUG] == 'on') {
$SETTINGS[PSNG_DEBUG] = TRUE;
$_SESSION[PSNG_DEBUG] = TRUE;
debug('', 'Debug on');
} elseif ($_REQUEST[PSNG_DEBUG] == 'off') {
debug('', 'Debug off');
$SETTINGS[PSNG_DEBUG] = FALSE;
$_SESSION[PSNG_DEBUG] = FALSE;
}
/* repair NOTICES mk/2005-11-08 */
}
else # mk assume off
{
debug('', 'Debug off');
$SETTINGS[PSNG_DEBUG] = FALSE;
$_SESSION[PSNG_DEBUG] = FALSE;
}
/**/
if (isset($_SESSION[PSNG_SETTINGS])) $SETTINGS = array_merge($_SESSION[PSNG_SETTINGS],$SETTINGS);
$LAYOUT->addContentHeader('<a href="'.$SETTINGS[PSNG_SCRIPT].'?action=setup" title="Edit settings">Setup</a>');
$LAYOUT->addContentHeader('<a href="'.$SETTINGS[PSNG_SCRIPT].'?action='.PSNG_ACTION_CHECK_UPDATESTATUS.'" title="Invoke an update check at enarion.net to get information of recent versions">Check for updates</a>');
if (isset($SETTINGS[PSNG_SETTINGS_EXECUTED][PSNG_ACTION_SETTINGS_GET]))
{
if ($SETTINGS[PSNG_SETTINGS_EXECUTED][PSNG_ACTION_SETTINGS_GET] == TRUE)
$LAYOUT->addContentHeader('<a href="'.$SETTINGS[PSNG_SCRIPT].'?action=parse" title="Start the scan for files">Start scan</a>');
}
if (@file_exists($SETTINGS[PSNG_SITEMAP_FILE]) && ( @filesize($SETTINGS[PSNG_SITEMAP_FILE]) > 0))
$LAYOUT->addContentHeader('<a href="'.$SETTINGS[PSNG_SITEMAP_URL].'" target="_blank" title="View the created sitemap in a new browser window">View sitemap </a>');
$LAYOUT->addContentHeader('<div align="left"><a href="http://enarion.net/google/phpsitemapng/feedback/" target="_blank" title="Create a feedback request at enarion.net in a new browser window">Give feedback</a>');
$LAYOUT->addContentHeader('<div align="left"><p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="enarion@enarion.net">
<input type="hidden" name="item_name" value="Development of phpSitemapNG">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="return" value="http://enarion.net/google/phpsitemapng/donated.php">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="tax" value="0">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but04.gif" border="0" name="submit" alt="Donate the development of phpSitemapNG" title="Donate the development of phpSitemapNG" target="_blank">
</form></p></div>');
debug('version: '.PSNG_VERSION, 'This is phpSitemapNG');
debug($SETTINGS, 'Merged settings');
debug($SETTINGS[PSNG_SETTINGS_STATE], 'last state');
$action = '';
if (isset($_REQUEST[PSNG_ACTION])) $action = $_REQUEST[PSNG_ACTION];
// handle some special actions, eg submit buttons without approriate hidden action tag
if (isset($_REQUEST[PSNG_ACTION_SETTINGS_RESET])) {
if ($_REQUEST[PSNG_ACTION_SETTINGS_RESET] != '') $action = PSNG_ACTION_SETTINGS_RESET;
}
$SETTINGS[PSNG_SETTINGS_STATE] = $action;
debug($SETTINGS[PSNG_SETTINGS_STATE], "current state");
$SETTINGS[PSNG_SETTINGS_EXECUTED][$action] = TRUE;
$SETTINGS[PSNG_TIMEOUT_IS] = FALSE;
$SETTINGS[PSNG_TIMEOUT_IS] = '';
if ($SETTINGS[PSNG_TIMEOUT] == PSNG_TIMEOUT_NONE) $SETTINGS[PSNG_TIMEOUT_TIME_DEADLINE] = time()+60*60*24; // timeout is now in 24 h
return $action;
}
function resetRunon() {
global $SETTINGS;
unset($SETTINGS[PSNG_TIMEOUT_DONE]);
unset($SETTINGS[PSNG_TIMEOUT_TODO]);
unset($SETTINGS[PSNG_TIMEOUT_FILE]);
unset($SETTINGS[PSNG_TIMEOUT_FILE_LASTRUN]);
unset($SETTINGS[PSNG_TIMEOUT_ACTION]);
unset($SETTINGS[PSNG_TIMEOUT_IS]);
unset($SETTINGS[PSNG_TIMEOUT_TIME]);
unset($SETTINGS[PSNG_TIMEOUT_TIME_START]);
// unset($SETTINGS[PSNG_TIMEOUT_TIME_DURATION]);
unset($SETTINGS[PSNG_TIMEOUT_TIME_SHUTDOWN]);
unset($SETTINGS[PSNG_TIMEOUT_TIME_DEADLINE]);
}
/**
* overwrite existing filelist-file
*/
function resetFiles() {
global $SETTINGS;
// write empty settings to file
storeSettings(array (), $SETTINGS['files_file'], "FILES");
// all settings are okay for now, so go on
return '<p>Your filelist '.$SETTINGS['files_file'].' is reseted now!<br/>'.'<a href="'.$SETTINGS[PSNG_SCRIPT].'">You can now create your personal google sitemap</a></p>';
}
/**
* checks a given filename if it exists and is writable
* returns empty string, if okay; otherwise the error message
*/
function checkFile($filename) {
$file = @ fopen($filename, "r");
// check if writable
$msg = "";
if (!file_exists($filename)) {
$msg = "File ".$filename." does not exist; create file and set permission with chmod to 0666";
}
elseif (!is_writable($filename)) {
$msg = "File ".$filename." does exist but cannot be written; change permission with chmod to 0666";
}
elseif ($file === FALSE) {
$msg = "Error while opening ".$filename." for write access. Check existence and permission of file!";
}
@ fclose($file);
return $msg;
}
function getDateTimeISO($timestamp) {
return date("Y-m-d\TH:i:s", $timestamp) . substr(date("O"),0,3) . ":" . substr(date("O"),3);
}
function getDateTimeISO_short($timestamp) {
return date("Y-m-d", $timestamp);
}
function getFrequency($lastmod) {
// set changefreq
$age = time() - $lastmod;
$change_freq = "monthly"; // default value
if ($age < 10) {
$change_freq = "always";
} elseif ($age < 60*60) {
$change_freq = "hourly";
} elseif ($age < 60*60*24) {
$change_freq = "daily";
} elseif ($age < 60*60*24*7) {
$change_freq = "weekly";
} elseif ($age < 60*60*24*31) { // longest month has 31 days
$change_freq = "monthly";
} elseif ($age < 60*60*24*365) {
$change_freq = "yearly";
} else {
$change_freq = "never";
}
return $change_freq;
}
/* write settings to file */
function storeSettings($SETTINGS, $filename, $keyname) {
global $openFile_error;
$file = openFile($filename, TRUE);
if ($file === FALSE)
return $openFile_error;
fputs($file, "<?php\n");
foreach ($SETTINGS as $key => $val) {
if (is_array($val)) {
foreach ($val as $key2 => $val2) {
fputs($file, '$'.$keyname."['".stringToVariableName($key)."']['".stringToVariableName($key2)."'] = '".stringToVariableName($val2)."';\n");
}
continue;
}
fputs($file, '$'.$keyname."['".stringToVariableName($key)."'] = '".stringToVariableName($val)."';\n");
}
fputs($file, "?>\n");
fclose($file);
return NULL;
}
/* remove maybe existing ' in string */
function stringToVariableName($str) {
return ((!get_magic_quotes_gpc()) ? addslashes($str) : $str);
}
function variableNameToString($var_name) {
if(get_magic_quotes_gpc()) {
return stripslashes($var_name);
} else {
$res = $var_name;
if (substr($res, 0, 1) == "'")
$res = substr($res, 1);
if (substr($res, strlen($res) - 1) == "'")
$res = substr($res, 0, strlen($res) - 1);
return $res;
}
}
/* explodes the array for given deliminator and returns a correct array */
function toArray($str, $delim = "\n") {
$res = array ();
$res = explode($delim, $str);
for ($i = 0; $i < count($res); $i ++) {
$res[$i] = trim($res[$i]);
}
return $res;
}
/* returns a string of all entries of array with delim */
function arrToStringReadable($array, $delim) {
$res = "";
if (is_array($array)) {
$i = 0;
foreach ($array as $key => $val) {
if (is_array($val)) {
$res .= "$key: ".arrToStringReadable($val, $delim);
} else {
$res .= "$key: $val";
}
if ($i < (count($array) - 1))
$res .= $delim;
$i ++;
}
}
return $res;
}
/* returns a string of all entries of array with delim */
function arrToString($array, $delim = "\n") {
$res = "";
if (is_array($array)) {
for ($i = 0; $i < count($array); $i ++) {
$res .= $array[$i];
if ($i < (count($array) - 1))
$res .= $delim;
}
}
return $res;
}
?>