/
functions.php
328 lines (296 loc) · 7.8 KB
/
functions.php
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
<?php if (!defined('VB_ENTRY')) die('Access denied.');
/*========================================================================*\
|| ###################################################################### ||
|| # vBulletin 5.1.9 - Licence Number LD18132D6F
|| # ------------------------------------------------------------------ # ||
|| # Copyright 2000-2015 vBulletin Solutions Inc. All Rights Reserved. # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ----------------- VBULLETIN IS NOT FREE SOFTWARE ----------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| ###################################################################### ||
\*========================================================================*/
/**
* vB_Library_Functions
*
* @package vBApi
* @access public
*/
class vB_Library_Functions extends vB_Library
{
/**
* Converts a version number string into an array that can be parsed
* to determine if which of several version strings is the newest.
*
* @param string Version string to parse
*
* @return array Array of 6 bits, in decreasing order of influence; a higher bit value is newer
*/
public static function fetchVersionArray($version)
{
// parse for a main and subversion
if (preg_match('#^([a-z]+ )?([0-9\.]+)[\s-]*([a-z].*)$#i', trim($version), $match))
{
$main_version = $match[2];
$sub_version = $match[3];
}
else
{
$main_version = $version;
$sub_version = '';
}
$version_bits = explode('.', $main_version);
// Pad the main version to 4 parts ( 1.1.1 XXX )
if (sizeof($version_bits) < 4)
{
for ($i = sizeof($version_bits); $i < 4; $i++)
{
$version_bits[$i] = 0;
}
}
// default sub-versions
$version_bits[4] = 0; // for alpha, beta, rc, pl, etc
$version_bits[5] = 0; // alpha, beta, etc number
if (!empty($sub_version))
{
// match the sub-version
if (preg_match('#^(A|ALPHA|B|BETA|G|GAMMA|RC|RELEASE CANDIDATE|GOLD|STABLE|FINAL|PL|PATCH LEVEL)\s*(\d*)\D*$#i', $sub_version, $match))
{
switch (strtoupper($match[1]))
{
case 'A':
case 'ALPHA';
$version_bits[4] = -4;
break;
case 'B':
case 'BETA':
$version_bits[4] = -3;
break;
case 'G':
case 'GAMMA':
$version_bits[4] = -2;
break;
case 'RC':
case 'RELEASE CANDIDATE':
$version_bits[4] = -1;
break;
case 'PL':
case 'PATCH LEVEL';
$version_bits[4] = 1;
break;
case 'GOLD':
case 'STABLE':
case 'FINAL':
default:
$version_bits[4] = 0;
break;
}
$version_bits[5] = $match[2];
}
}
// sanity check -- make sure each bit is an int
for ($i = 0; $i <= 5; $i++)
{
$version_bits[$i] = intval($version_bits[$i]);
}
return $version_bits;
}
/**
* Compares two version strings.
* Returns true if the first is newer than the second.
* Returns true if 'check_same' is set and the versions are equal.
*
* @param string Version string; usually the latest version
* @param string Version string; usually the current version
* @param bool Flag to allow check if the versions are the same
*
* @return bool True or False
*/
public static function isNewerVersion($new_version_str, $cur_version_str, $check_same = false)
{
// if they're the same, don't even bother
if ($cur_version_str != $new_version_str)
{
$cur_version = self::fetchVersionArray($cur_version_str);
$new_version = self::fetchVersionArray($new_version_str);
// iterate parts
for ($i = 0; $i <= 5; $i++)
{
if ($new_version[$i] != $cur_version[$i])
{
// true if newer is greater
return ($new_version[$i] > $cur_version[$i]);
}
}
}
else if ($check_same)
{
return true;
}
return false;
}
/**
* Writes data to a file
*
* @param string Path to file (including file name)
* @param string Data to be saved into the file
* @param boolean If true, will create a backup of the file called {filename}.old
*/
public static function fileWrite($path, $data, $backup = false)
{
if (file_exists($path) != false)
{
if ($backup)
{
$filenamenew = $path . '.old';
rename($path, $filenamenew);
}
else
{
unlink($path);
}
}
if ($data != '')
{
$filenum = fopen($path, 'w');
fwrite($filenum, $data);
fclose($filenum);
}
}
/**
* Returns the contents of a file
*
* @param string Path to file (including file name)
*
* @return string If file does not exist, returns an empty string
*/
public static function fileRead($path)
{
if(!file_exists($path) AND !is_uploaded_file($path))
{
return '';
}
else
{
$filestuff = @file_get_contents($path);
return $filestuff;
}
}
/**
* Installs a product from the xml text, currently calls the legacy function
*
* @return bool True if the product requires a template merge, false otherwise
*/
public static function installProduct($product, $path = '', $filename = '', $overwrite = false, $printinfo = false)
{
if(!$path)
{
$path = DIR . DIRECTORY_SEPARATOR . 'includes'. DIRECTORY_SEPARATOR . 'xml';
}
if(!$filename)
{
// Default filenames
$filename1 = 'product_' . $product . '.xml'; // vB5 format
$filename2 = 'product-' . $product . '.xml'; // vB3/4 format
}
else
{
$filename1 = $filename;
$filename2 = '';
}
$file1 = $path . DIRECTORY_SEPARATOR . $filename1;
$file2 = $path . DIRECTORY_SEPARATOR . $filename2;
if ($xml = self::fileRead($file1))
{
return self::installProductXML($xml, $overwrite, $printinfo);
}
else if ($xml = self::fileRead($file2))
{
return self::installProductXML($xml, $overwrite, $printinfo);
}
else
{
return false;
}
}
/**
* Installs a product from the xml text, currently calls the legacy function
*
* @return bool True if the product requires a template merge, false otherwise
*/
public static function installProductXML($xml, $overwrite = false, $printinfo = false)
{
require_once(DIR . '/includes/adminfunctions.php');
require_once(DIR . '/includes/adminfunctions_product.php');
try
{
$install = install_product($xml, $overwrite, $printinfo);
}
catch (exception $e)
{
return false;
}
return $install;
}
/*
* Recursion creation of directory
*
* @param string Directory to create
* @param octal Mode
*
* @return bool success
*/
public static function vbMkdir($path, $mode = 0777)
{
if (is_dir($path))
{
if (!(is_writable($path)))
{
@chmod($path, $mode);
}
return true;
}
else
{
$oldmask = @umask(0);
$partialpath = dirname($path);
if (!self::vbMkdir($partialpath, $mode))
{
return false;
}
else
{
return @mkdir($path, $mode);
}
}
}
/**
* Escapes quotes in strings destined for Javascript (Taken from includes/functions.php)
*
* @param string String to be prepared for Javascript
* @param string Type of quote (single or double quote)
*
* @return string
*/
public static function addSlashesJs($text, $quotetype = "'")
{
if ($quotetype == "'")
{
// single quotes
$replaced = str_replace(array('\\', '\'', "\n", "\r"), array('\\\\', "\\'","\\n", "\\r"), $text);
}
else
{
// double quotes
$replaced = str_replace(array('\\', '"', "\n", "\r"), array('\\\\', "\\\"","\\n", "\\r"), $text);
}
$replaced = preg_replace('#(-(?=-))#', "-$quotetype + $quotetype", $replaced);
$replaced = preg_replace('#</script#i', "<\\/scr$quotetype + {$quotetype}ipt", $replaced);
return $replaced;
}
}
/*=========================================================================*\
|| #######################################################################
|| # Downloaded: 15:45, Tue Sep 8th 2015
|| # CVS: $RCSfile$ - $Revision: 83435 $
|| #######################################################################
\*=========================================================================*/