forked from watchdogpolska/ambasadorzy.siecobywatelska.pl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.inc.php
executable file
·73 lines (59 loc) · 2.28 KB
/
functions.inc.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
<?php
include_once('constants.inc.php');
if(!file_exists("db.inc.php")) header('Location: config.php'); // redirect to config when no base settings
error_reporting(-1); // Comments these two lines to disable error reporting - NEED TO BE DONE IN PRODUCTION PHRASE!
ini_set('display_errors', 'On');
session_start();
session_regenerate_id();
if(!isset($_SESSION['csrf'])) csrf_gen();
if(file_exists("db.inc.php") && file_exists("config.php")) unlink("config.php");
// Display basic website head section + body beginning
function showHead($subname, $header = "Ambasadorzy Jawności") {
include ('parts/header.php');
}
// Function to show ending of each site
function showFooter() {
include ('parts/footer.php');
}
// Global URL origin function from http://stackoverflow.com/questions/6768793/get-the-full-url-in-php (CC license)
function url_origin( $s, $use_forwarded_host = false )
{
$ssl = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
$sp = strtolower( $s['SERVER_PROTOCOL'] );
$protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
$port = $s['SERVER_PORT'];
$port = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
$host = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
$host = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
return $protocol . '://' . $host;
}
// CSRF Token Generation & Validation
function csrf_gen() {
$_SESSION['csrf'] = generateRandomString(12);
return $_SESSION['csrf'];
}
function csrf_validate($given = "") {
return $_SESSION['csrf'] == $given;
}
// Random string generation - code from
// http://stackoverflow.com/questions/4356289/php-random-string-generator (CC license)
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
//getting user's IP
function get_client_ip_env() {
$ipaddress = '';
if(getenv('REMOTE_ADDR')) {
$ipaddress = getenv('REMOTE_ADDR');
}else{
$ipaddress = 'UNKNOWN';
}
return $ipaddress;
}
?>