-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.php
150 lines (121 loc) · 3.72 KB
/
index.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
<?php
/**
* Controller.
*
* @package PHPCheatsheets
*/
define( 'APP_DIR', dirname( __FILE__ ) );
/**
* Catch requests for static files (which have not been caught by htaccess).
*/
if ( ( isset( $_GET['page'], $_GET['phpversion'] ) && in_array( $_GET['page'], array( 'arithmetic', 'compare', 'test' ), true ) ) && preg_match( '`^php[457](?:\.[0-9]+){2}(?:-[0-9]|(?:alpha|beta|RC)(?:[0-9])?)?$`', $_GET['phpversion'] ) ) {
$file = APP_DIR . '/static_results/' . $_GET['page'] . '/' . $_GET['phpversion'] . '.html';
if ( is_file( $file ) ) {
$tab = '';
if ( isset( $_GET['tab'] ) && preg_match( '`[a-z0-9_-]+`', $_GET['tab'] ) ) {
$tab = '#' . $_GET['tab'];
}
$host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : 'phpcheatsheets.com';
$url = 'https://' . $host . '/static_results/' . $_GET['page'] . '/' . $_GET['phpversion'] . '.html' . $tab;
header( "Location: $url", true, 301 );
exit;
}
else {
// 404 not found
$_GET['page'] = 'error';
$_GET['e'] = '404';
}
}
require_once APP_DIR . '/include/setup-env.php';
/**
* Determine what has been requested.
*/
$type = null;
$page = null;
$tab = null;
$page_title = 'PHP Cheatsheets';
if ( isset( $_GET['page'] ) ) {
switch ( $_GET['page'] ) {
case 'compare':
$type = 'compare';
$page_title = 'PHP Variable Comparison';
break;
case 'arithmetic':
$type = 'arithmetic';
$page_title = 'PHP Arithmetic Operations';
break;
case 'test':
$type = 'test';
$page_title = 'PHP Variable Testing';
break;
case 'other-cheat-sheets':
$page = 'other-cheat-sheets';
$page_title = 'More PHP Cheatsheets';
break;
case 'about':
$page = 'about';
$page_title = 'About phpcheatsheets.com';
break;
case 'error':
default:
$page = 'error';
$page_title = 'Page not found';
$protocol = ( ( isset( $_SERVER['SERVER_PROTOCOL'] ) && $_SERVER['SERVER_PROTOCOL'] !== '' && preg_match( '`HTTP/1\.[0-9]`', $_SERVER['SERVER_PROTOCOL'] ) ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1' );
header( "$protocol 404 Not Found" );
break;
}
}
// Send headers.
header( 'Content-type: text/html; charset=utf-8' );
$class = 'Vartype' . ucfirst( $type );
$file = 'class.vartype-' . $type . '.php';
/**
* Load a cheatsheet page.
*/
if ( isset( $type ) && file_exists( APP_DIR . '/' . $file ) ) {
include_once APP_DIR . '/' . $file;
$current_tests = new $class();
if ( isset( $_GET['tab'] ) && $_GET['tab'] !== '' ) {
$tab = $_GET['tab']; // WPCS: ok - validation is done before use in VarType::get_test_group() method.
}
/**
* Only return the table if it's an ajax call.
*/
if ( isset( $_GET['do'] ) && $_GET['do'] === 'ajax' ) {
$current_tests->run_test( $tab );
}
/**
* Return a full page if not.
*/
else {
$tab_title = $current_tests->get_tab_title( $tab );
include_once APP_DIR . '/views/header.php';
include_once APP_DIR . '/views/notes-legend.php';
// Hidden feature - pre-load all tabs, slow, but useful for source compare & generating of static files.
$all = false;
if ( isset( $_GET['all'] ) && $_GET['all'] === '1' ) {
$all = true;
ini_set( 'max_execution_time', '180' ); // Lengthen allowed execution time.
}
$current_tests->do_page( $all );
include_once APP_DIR . '/views/footer.php';
}
}
/**
* Load an extraneous page (about, links etc).
*/
else {
include_once APP_DIR . '/views/header.php';
if ( isset( $page ) && $page !== '' ) {
if ( file_exists( APP_DIR . '/views/' . $page . '.php' ) ) {
include_once APP_DIR . '/views/' . $page . '.php';
}
else {
include_once APP_DIR . '/views/cheat-sheet-menu.php';
}
}
else {
include_once APP_DIR . '/views/cheat-sheet-menu.php';
}
include_once APP_DIR . '/views/footer.php';
}