Skip to content

Commit 5f6949c

Browse files
author
Marius Burkard
committed
Merge branch '6312-fix-compatibility-issues-on-systems-with-php-5-centos-7-systems-2' into 'develop'
Resolve "Fix compatibility issues on systems running php5" Closes #6312 and #6310 See merge request ispconfig/ispconfig3!1588
2 parents 225e158 + 5cf9e79 commit 5f6949c

File tree

5 files changed

+170
-1
lines changed

5 files changed

+170
-1
lines changed

install/lib/compatibility.inc.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2021, Jesse Norell <jesse@kci.net>
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
/* random_bytes can be dropped when php 5.6 support is dropped */
32+
if (! function_exists('random_bytes')) {
33+
function random_bytes($length) {
34+
return openssl_random_pseudo_bytes($length);
35+
}
36+
}
37+
38+
/* random_int can be dropped when php 5.6 support is dropped */
39+
if (! function_exists('random_int')) {
40+
function random_int($min=null, $max=null) {
41+
if (null === $min) {
42+
$min = PHP_INT_MIN;
43+
}
44+
45+
if (null === $max) {
46+
$min = PHP_INT_MAX;
47+
}
48+
49+
if (!is_int($min) || !is_int($max)) {
50+
trigger_error('random_int: $min and $max must be integer values', E_USER_NOTICE);
51+
$min = (int)$min;
52+
$max = (int)$max;
53+
}
54+
55+
if ($min > $max) {
56+
trigger_error('random_int: $max can\'t be lesser than $min', E_USER_WARNING);
57+
return null;
58+
}
59+
60+
$range = $counter = $max - $min;
61+
$bits = 1;
62+
63+
while ($counter >>= 1) {
64+
++$bits;
65+
}
66+
67+
$bytes = (int)max(ceil($bits/8), 1);
68+
$bitmask = pow(2, $bits) - 1;
69+
70+
if ($bitmask >= PHP_INT_MAX) {
71+
$bitmask = PHP_INT_MAX;
72+
}
73+
74+
do {
75+
$result = hexdec(bin2hex(random_bytes($bytes))) & $bitmask;
76+
} while ($result > $range);
77+
78+
return $result + $min;
79+
}
80+
}

install/lib/install.lib.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
*/
3030
error_reporting(E_ALL|E_STRICT);
3131

32+
if(version_compare(phpversion(), '7.0', '<')) {
33+
require_once 'compatibility.inc.php';
34+
}
3235

3336
$FILE = realpath('../install.php');
3437

interface/lib/app.inc.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

31-
require_once 'compatibility.inc.php';
31+
if(version_compare(phpversion(), '7.0', '<')) {
32+
require_once 'compatibility.inc.php';
33+
}
3234

3335
//* Enable gzip compression for the interface
3436
ob_start('ob_gzhandler');

server/lib/app.inc.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
*/
2929

30+
if(version_compare(phpversion(), '7.0', '<')) {
31+
require_once 'compatibility.inc.php';
32+
}
33+
3034
// Set timezone
3135
if(isset($conf['timezone']) && $conf['timezone'] != '') { // note: !empty($conf['timezone']) should give the same result and is more idiomatic for current versions of PHP (gwyneth 20220315)
3236
date_default_timezone_set($conf['timezone']);

server/lib/compatibility.inc.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2021, Jesse Norell <jesse@kci.net>
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
/* random_bytes can be dropped when php 5.6 support is dropped */
32+
if (! function_exists('random_bytes')) {
33+
function random_bytes($length) {
34+
return openssl_random_pseudo_bytes($length);
35+
}
36+
}
37+
38+
/* random_int can be dropped when php 5.6 support is dropped */
39+
if (! function_exists('random_int')) {
40+
function random_int($min=null, $max=null) {
41+
if (null === $min) {
42+
$min = PHP_INT_MIN;
43+
}
44+
45+
if (null === $max) {
46+
$min = PHP_INT_MAX;
47+
}
48+
49+
if (!is_int($min) || !is_int($max)) {
50+
trigger_error('random_int: $min and $max must be integer values', E_USER_NOTICE);
51+
$min = (int)$min;
52+
$max = (int)$max;
53+
}
54+
55+
if ($min > $max) {
56+
trigger_error('random_int: $max can\'t be lesser than $min', E_USER_WARNING);
57+
return null;
58+
}
59+
60+
$range = $counter = $max - $min;
61+
$bits = 1;
62+
63+
while ($counter >>= 1) {
64+
++$bits;
65+
}
66+
67+
$bytes = (int)max(ceil($bits/8), 1);
68+
$bitmask = pow(2, $bits) - 1;
69+
70+
if ($bitmask >= PHP_INT_MAX) {
71+
$bitmask = PHP_INT_MAX;
72+
}
73+
74+
do {
75+
$result = hexdec(bin2hex(random_bytes($bytes))) & $bitmask;
76+
} while ($result > $range);
77+
78+
return $result + $min;
79+
}
80+
}

0 commit comments

Comments
 (0)