~azzar1/unity/add-show-desktop-key

443 by dcoles
Added Forum application along with unmodifed version of phpBB3 "Olympus" 3.0.0
1
<?php
2
/**
3
*
4
* @package acm
5
* @version $Id: acm_file.php,v 1.55 2007/11/17 12:14:27 acydburn Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
*
9
*/
10
11
/**
12
* @ignore
13
*/
14
if (!defined('IN_PHPBB'))
15
{
16
	exit;
17
}
18
19
/**
20
* ACM File Based Caching
21
* @package acm
22
*/
23
class acm
24
{
25
	var $vars = array();
26
	var $var_expires = array();
27
	var $is_modified = false;
28
29
	var $sql_rowset = array();
30
	var $sql_row_pointer = array();
31
	var $cache_dir = '';
32
33
	/**
34
	* Set cache path
35
	*/
36
	function acm()
37
	{
38
		global $phpbb_root_path;
39
		$this->cache_dir = $phpbb_root_path . 'cache/';
40
	}
41
42
	/**
43
	* Load global cache
44
	*/
45
	function load()
46
	{
47
		global $phpEx;
48
		if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
49
		{
50
			@include($this->cache_dir . 'data_global.' . $phpEx);
51
		}
52
		else
53
		{
54
			return false;
55
		}
56
57
		return true;
58
	}
59
60
	/**
61
	* Unload cache object
62
	*/
63
	function unload()
64
	{
65
		$this->save();
66
		unset($this->vars);
67
		unset($this->var_expires);
68
		unset($this->sql_rowset);
69
		unset($this->sql_row_pointer);
70
71
		$this->vars = array();
72
		$this->var_expires = array();
73
		$this->sql_rowset = array();
74
		$this->sql_row_pointer = array();
75
	}
76
77
	/**
78
	* Save modified objects
79
	*/
80
	function save()
81
	{
82
		if (!$this->is_modified)
83
		{
84
			return;
85
		}
86
87
		global $phpEx;
88
89
		if ($fp = @fopen($this->cache_dir . 'data_global.' . $phpEx, 'wb'))
90
		{
91
			@flock($fp, LOCK_EX);
92
			fwrite($fp, "<?php\n\$this->vars = " . var_export($this->vars, true) . ";\n\n\$this->var_expires = " . var_export($this->var_expires, true) . "\n?>");
93
			@flock($fp, LOCK_UN);
94
			fclose($fp);
95
96
			@chmod($this->cache_dir . 'data_global.' . $phpEx, 0666);
97
		}
98
		else
99
		{
100
			// Now, this occurred how often? ... phew, just tell the user then...
101
			if (!@is_writable($this->cache_dir))
102
			{
103
				trigger_error($this->cache_dir . ' is NOT writable.', E_USER_ERROR);
104
			}
105
106
			trigger_error('Not able to open ' . $this->cache_dir . 'data_global.' . $phpEx, E_USER_ERROR);
107
		}
108
109
		$this->is_modified = false;
110
	}
111
112
	/**
113
	* Tidy cache
114
	*/
115
	function tidy()
116
	{
117
		global $phpEx;
118
119
		$dir = @opendir($this->cache_dir);
120
121
		if (!$dir)
122
		{
123
			return;
124
		}
125
126
		while (($entry = readdir($dir)) !== false)
127
		{
128
			if (!preg_match('/^(sql_|data_(?!global))/', $entry))
129
			{
130
				continue;
131
			}
132
133
			$expired = true;
134
			@include($this->cache_dir . $entry);
135
			if ($expired)
136
			{
137
				$this->remove_file($this->cache_dir . $entry);
138
			}
139
		}
140
		closedir($dir);
141
142
		if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
143
		{
144
			if (!sizeof($this->vars))
145
			{
146
				$this->load();
147
			}
148
149
			foreach ($this->var_expires as $var_name => $expires)
150
			{
151
				if (time() > $expires)
152
				{
153
					$this->destroy($var_name);
154
				}
155
			}
156
		}
157
		
158
		set_config('cache_last_gc', time(), true);
159
	}
160
161
	/**
162
	* Get saved cache object
163
	*/
164
	function get($var_name)
165
	{
166
		if ($var_name[0] == '_')
167
		{
168
			global $phpEx;
169
170
			if (!$this->_exists($var_name))
171
			{
172
				return false;
173
			}
174
175
			@include($this->cache_dir . "data{$var_name}.$phpEx");
176
			return (isset($data)) ? $data : false;
177
		}
178
		else
179
		{
180
			return ($this->_exists($var_name)) ? $this->vars[$var_name] : false;
181
		}
182
	}
183
184
	/**
185
	* Put data into cache
186
	*/
187
	function put($var_name, $var, $ttl = 31536000)
188
	{
189
		if ($var_name[0] == '_')
190
		{
191
			global $phpEx;
192
193
			if ($fp = @fopen($this->cache_dir . "data{$var_name}.$phpEx", 'wb'))
194
			{
195
				@flock($fp, LOCK_EX);
196
				fwrite($fp, "<?php\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\n\$data = " . var_export($var, true) . ";\n?>");
197
				@flock($fp, LOCK_UN);
198
				fclose($fp);
199
200
				@chmod($this->cache_dir . "data{$var_name}.$phpEx", 0666);
201
			}
202
		}
203
		else
204
		{
205
			$this->vars[$var_name] = $var;
206
			$this->var_expires[$var_name] = time() + $ttl;
207
			$this->is_modified = true;
208
		}
209
	}
210
211
	/**
212
	* Purge cache data
213
	*/
214
	function purge()
215
	{
216
		// Purge all phpbb cache files
217
		$dir = @opendir($this->cache_dir);
218
219
		if (!$dir)
220
		{
221
			return;
222
		}
223
224
		while (($entry = readdir($dir)) !== false)
225
		{
226
			if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
227
			{
228
				continue;
229
			}
230
231
			$this->remove_file($this->cache_dir . $entry);
232
		}
233
		closedir($dir);
234
235
		unset($this->vars);
236
		unset($this->var_expires);
237
		unset($this->sql_rowset);
238
		unset($this->sql_row_pointer);
239
240
		$this->vars = array();
241
		$this->var_expires = array();
242
		$this->sql_rowset = array();
243
		$this->sql_row_pointer = array();
244
245
		$this->is_modified = false;
246
	}
247
248
	/**
249
	* Destroy cache data
250
	*/
251
	function destroy($var_name, $table = '')
252
	{
253
		global $phpEx;
254
255
		if ($var_name == 'sql' && !empty($table))
256
		{
257
			if (!is_array($table))
258
			{
259
				$table = array($table);
260
			}
261
262
			$dir = @opendir($this->cache_dir);
263
264
			if (!$dir)
265
			{
266
				return;
267
			}
268
269
			while (($entry = readdir($dir)) !== false)
270
			{
271
				if (strpos($entry, 'sql_') !== 0)
272
				{
273
					continue;
274
				}
275
276
				// The following method is more failproof than simply assuming the query is on line 3 (which it should be)
277
				$check_line = @file_get_contents($this->cache_dir . $entry);
278
279
				if (empty($check_line))
280
				{
281
					continue;
282
				}
283
284
				// Now get the contents between /* and */
285
				$check_line = substr($check_line, strpos($check_line, '/* ') + 3, strpos($check_line, ' */') - strpos($check_line, '/* ') - 3);
286
287
				$found = false;
288
				foreach ($table as $check_table)
289
				{
290
					// Better catch partial table names than no table names. ;)
291
					if (strpos($check_line, $check_table) !== false)
292
					{
293
						$found = true;
294
						break;
295
					}
296
				}
297
298
				if ($found)
299
				{
300
					$this->remove_file($this->cache_dir . $entry);
301
				}
302
			}
303
			closedir($dir);
304
305
			return;
306
		}
307
308
		if (!$this->_exists($var_name))
309
		{
310
			return;
311
		}
312
313
		if ($var_name[0] == '_')
314
		{
315
			$this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx");
316
		}
317
		else if (isset($this->vars[$var_name]))
318
		{
319
			$this->is_modified = true;
320
			unset($this->vars[$var_name]);
321
			unset($this->var_expires[$var_name]);
322
323
			// We save here to let the following cache hits succeed
324
			$this->save();
325
		}
326
	}
327
328
	/**
329
	* Check if a given cache entry exist
330
	*/
331
	function _exists($var_name)
332
	{
333
		if ($var_name[0] == '_')
334
		{
335
			global $phpEx;
336
			return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx");
337
		}
338
		else
339
		{
340
			if (!sizeof($this->vars))
341
			{
342
				$this->load();
343
			}
344
345
			if (!isset($this->var_expires[$var_name]))
346
			{
347
				return false;
348
			}
349
350
			return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]);
351
		}
352
	}
353
354
	/**
355
	* Load cached sql query
356
	*/
357
	function sql_load($query)
358
	{
359
		global $phpEx;
360
361
		// Remove extra spaces and tabs
362
		$query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
363
		$query_id = sizeof($this->sql_rowset);
364
365
		if (!file_exists($this->cache_dir . 'sql_' . md5($query) . ".$phpEx"))
366
		{
367
			return false;
368
		}
369
370
		@include($this->cache_dir . 'sql_' . md5($query) . ".$phpEx");
371
372
		if (!isset($expired))
373
		{
374
			return false;
375
		}
376
		else if ($expired)
377
		{
378
			$this->remove_file($this->cache_dir . 'sql_' . md5($query) . ".$phpEx");
379
			return false;
380
		}
381
382
		$this->sql_row_pointer[$query_id] = 0;
383
384
		return $query_id;
385
	}
386
387
	/**
388
	* Save sql query
389
	*/
390
	function sql_save($query, &$query_result, $ttl)
391
	{
392
		global $db, $phpEx;
393
394
		// Remove extra spaces and tabs
395
		$query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
396
		$filename = $this->cache_dir . 'sql_' . md5($query) . '.' . $phpEx;
397
398
		if ($fp = @fopen($filename, 'wb'))
399
		{
400
			@flock($fp, LOCK_EX);
401
402
			$query_id = sizeof($this->sql_rowset);
403
			$this->sql_rowset[$query_id] = array();
404
			$this->sql_row_pointer[$query_id] = 0;
405
406
			while ($row = $db->sql_fetchrow($query_result))
407
			{
408
				$this->sql_rowset[$query_id][] = $row;
409
			}
410
			$db->sql_freeresult($query_result);
411
412
			$file = "<?php\n\n/* " . str_replace('*/', '*\/', $query) . " */\n";
413
			$file .= "\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n";
414
415
			fwrite($fp, $file . "\n\$this->sql_rowset[\$query_id] = " . var_export($this->sql_rowset[$query_id], true) . ";\n?>");
416
			@flock($fp, LOCK_UN);
417
			fclose($fp);
418
419
			@chmod($filename, 0666);
420
421
			$query_result = $query_id;
422
		}
423
	}
424
425
	/**
426
	* Ceck if a given sql query exist in cache
427
	*/
428
	function sql_exists($query_id)
429
	{
430
		return isset($this->sql_rowset[$query_id]);
431
	}
432
433
	/**
434
	* Fetch row from cache (database)
435
	*/
436
	function sql_fetchrow($query_id)
437
	{
438
		if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
439
		{
440
			return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
441
		}
442
443
		return false;
444
	}
445
446
	/**
447
	* Fetch a field from the current row of a cached database result (database)
448
	*/
449
	function sql_fetchfield($query_id, $field)
450
	{
451
		if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
452
		{
453
			return (isset($this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field])) ? $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field] : false;
454
		}
455
456
		return false;
457
	}
458
459
	/**
460
	* Seek a specific row in an a cached database result (database)
461
	*/
462
	function sql_rowseek($rownum, $query_id)
463
	{
464
		if ($rownum >= sizeof($this->sql_rowset[$query_id]))
465
		{
466
			return false;
467
		}
468
469
		$this->sql_row_pointer[$query_id] = $rownum;
470
		return true;
471
	}
472
473
	/**
474
	* Free memory used for a cached database result (database)
475
	*/
476
	function sql_freeresult($query_id)
477
	{
478
		if (!isset($this->sql_rowset[$query_id]))
479
		{
480
			return false;
481
		}
482
483
		unset($this->sql_rowset[$query_id]);
484
		unset($this->sql_row_pointer[$query_id]);
485
486
		return true;
487
	}
488
489
	/**
490
	* Removes/unlinks file
491
	*/
492
	function remove_file($filename)
493
	{
494
		if (!@unlink($filename))
495
		{
496
			// E_USER_ERROR - not using language entry - intended.
497
			trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR);
498
		}
499
	}
500
}
501
502
?>