File: persist.lib.php

package info (click to toggle)
warzone2100 4.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,348 kB
  • sloc: cpp: 675,711; ansic: 387,204; javascript: 75,107; python: 16,628; php: 4,294; sh: 3,941; makefile: 2,330; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (129 lines) | stat: -rw-r--r-- 4,217 bytes parent folder | download | duplicates (4)
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
<?php

/****************************************************************
 ** Persistence library                                        **
 ** Version 1.0 RC8                                            **
 ** By Guangcong Luo - http://en.wikipedia.org/wiki/User:Zarel **
 ** released under CC0 and public domain                       **
 ****************************************************************
 *
 * Meant for PHP 4.0.4 and up. I've tried to make it work in
 * PHP 3, but I've never tested it, so use it in PHP 3 at
 * your own risk.
 *
 **[ Description ]***********************************************
 *
 * Creates a new global variable called $_PERSIST, stored in
 * persist.inc.php . If you call persist_update(),
 * persist.inc.php will be updated to store the new
 * value of $_PERSIST.
 *
 * Since you'll probably want to store more than one variable, use
 * $_PERSIST as an array.
 *
 * Make sure PHP has permissions to edit persist.inc.php (If all else
 * fails, CHMOD to 777).
 *
 * The location and name of persist.inc.php, as well as the name of
 * $_PERSIST, can be set either before persist.lib.php is called or
 * below.
 *
 * Example code:
 * <?php
 *  $persist_path = 'includes/custom.inc.php';
 *  $persist_name = 'CUSTOM';
 *  include 'persist.lib.php';
 *  echo $CUSTOM['value'];
 *  $CUSTOM['value'] = 'New value';
 *  persist_update();
 *  echo $CUSTOM['value'];
 * ?>
 *
 * If a script does not need to update the value of $_PERSIST, it can
 * simply include persist.inc.php instead of persist.lib.php .
 *
 **[ Function reference ]****************************************
 *
 *  persist_update()
 *   Updates stored $_PERSIST.
 *
 ****************************************************************/

if (!isset($persist_name) || !$persist_name)
// $persist_name is the name of the variable you want to persist.
// Do not add $ before it; it will be added automatically.
// Defaults to _PERSIST (Which would be the variable $_PERSIST)
	$persist_name = '_PERSIST';

if (!isset($persist_path) || !$persist_path)
// $persist_path is URL of whatever file contains the data you want to
// persist.
// Defaults to persist.inc.php in the directory containing
// persist.lib.php (as opposed to the directory the script is running
// in).
	$persist_path = substr(__FILE__,0,strrpos(__FILE__,'/')+1).strtolower(substr($persist_name,0,1)==='_'?substr($persist_name,1):$persist_name).'.inc.php';

@include_once $persist_path;

function persist_update($name='', $path='')
// Updates $_PERSIST
{
	global $persist_name,$persist_path;
	if (!$name) $name = $persist_name;
	if (!$path && $name == $persist_name) $path = $persist_path;
	if (!$path || !file_exists($path)) $path = substr(__FILE__,0,strrpos(__FILE__,'/')+1).strtolower(substr($name,0,1)==='_'?substr($name,1):$name).'.inc.php';
	// Open persist.inc.php and start editing it
	if (!is_writable($path)) return false;
	$res = fopen($path,"w");
	if (!$res) return false;
	fwrite($res,"<?php\n\$".$name." = ".persist_tophp($GLOBALS[$name]).";\n?>\n");
	fclose($res);
	return true;
}

function persist_tophp($var, $pre='')
// Recursive function to create array
// It really needs a better name
{
	if (is_null($var)) // NULL
		return 'NULL';
	if (is_bool($var)) // Boolean
		return ($var?'TRUE':'FALSE');
	if (is_int($var) || is_float($var)) // Number
		return ''.$var;
	if (is_string($var)) // String
		return "'".php_escape($var)."'";
	if (is_array($var)) //Array
	{
		if (empty($var)) return 'array()';
		$buf = "array(\n";
		$nleft = count($var); $i = -1; reset($var);
		// Recurse (Whee!)
		while (($cur = each($var)) !== FALSE)
		{
			$buf .= $pre."\t";
			if (!is_int($cur[0]))
			{	$buf .= "'".php_escape($cur[0])."' => ";
				$i = FALSE;
			}
			else if ($i===FALSE || $cur[0] != ++$i) 
			{	$buf .= $cur[0].' => ';
				$i = ($i!==FALSE&&$cur[0]>$i?$cur[0]:FALSE);
			}
			$buf .= persist_tophp($cur[1], $pre."\t");
			if (--$nleft) $buf .= ',';
			$buf .= "\n";
		}
		return $buf.$pre.')';
	}
	return "unserialize('".php_escape(serialize($var))."')";
}

function php_escape($str)
{
	return strtr($str,array("\\" => "\\\\", "'" => "\\'"));
}

if (isset($_REQUEST['forceupdate'])) if (persist_update()) echo 'SUCCESS'; else echo 'ERROR';

?>