Completed
Pull Request — master (#58)
by Juliette
03:15
created

index.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 15.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Controller.
4
 *
5
 * @package PHPCheatsheets
6
 */
7
8
define( 'APP_DIR', dirname( __FILE__ ) );
9
10
11
/**
12
 * Catch requests for static files (which have not been caught by htaccess).
13
 */
14
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'] ) ) {
15
	$file = APP_DIR . '/static_results/' . $_GET['page'] . '/' . $_GET['phpversion'] . '.html';
16
	if ( is_file( $file ) ) {
17
18
		$tab = '';
19
		if ( isset( $_GET['tab'] ) && preg_match( '`[a-z0-9_-]+`', $_GET['tab'] ) ) {
20
			$tab = '#' . $_GET['tab'];
21
		}
22
23
		$host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : 'phpcheatsheets.com';
24
		$url  = 'https://' . $host . '/static_results/' . $_GET['page'] . '/' . $_GET['phpversion'] . '.html' . $tab;
25
		header( "Location: $url", true, 301 );
26
		exit;
27
	}
28
	else {
29
		// 404 not found
30
		$_GET['page'] = 'error';
31
		$_GET['e']    = '404';
32
	}
33
}
34
35
36
require_once APP_DIR . '/include/setup-env.php';
37
38
/**
39
 * Determine what has been requested.
40
 */
41
$type       = null;
42
$page       = null;
43
$tab        = null;
44
$page_title = 'PHP Cheatsheets';
45
46
if ( isset( $_GET['page'] ) ) {
47
	switch ( $_GET['page'] ) {
48
		case 'compare':
49
			$type       = 'compare';
50
			$page_title = 'PHP Variable Comparison';
51
			break;
52
53
		case 'arithmetic':
54
			$type       = 'arithmetic';
55
			$page_title = 'PHP Arithmetic Operations';
56
			break;
57
58
		case 'test':
59
			$type       = 'test';
60
			$page_title = 'PHP Variable Testing';
61
			break;
62
63
		case 'other-cheat-sheets':
64
			$page       = 'other-cheat-sheets';
65
			$page_title = 'More PHP Cheatsheets';
66
			break;
67
68
		case 'about':
69
			$page       = 'about';
70
			$page_title = 'About phpcheatsheets.com';
71
			break;
72
73
		case 'error':
74
		default:
75
			$page       = 'error';
76
			$page_title = 'Page not found';
77
			$protocol   = ( ( isset( $_SERVER['SERVER_PROTOCOL'] ) && $_SERVER['SERVER_PROTOCOL'] !== '' && preg_match( '`HTTP/1\.[0-9]`', $_SERVER['SERVER_PROTOCOL'] ) ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1' );
78
			header( "$protocol 404 Not Found" );
79
			break;
80
	}
81
}
82
83
84
// Send headers.
85
header( 'Content-type: text/html; charset=utf-8' );
86
87
88
$class = 'Vartype' . ucfirst( $type );
89
$file  = 'class.vartype-' . $type . '.php';
90
91
/**
92
 * Load a cheatsheet page.
93
 */
94
if ( isset( $type ) && file_exists( APP_DIR . '/' . $file ) ) {
95
	include_once APP_DIR . '/' . $file;
96
	$current_tests = new $class();
97
98 View Code Duplication
	if ( isset( $_GET['tab'] ) && $_GET['tab'] !== '' ) {
99
		$tab = $_GET['tab']; // WPCS: ok - validation is done before use in VarType::get_test_group() method.
100
	}
101
102
	/**
103
	 * Only return the table if it's an ajax call.
104
	 */
105
	if ( isset( $_GET['do'] ) && $_GET['do'] === 'ajax' ) {
106
		$current_tests->run_test( $tab );
107
	}
108
109
	/**
110
	 * Return a full page if not.
111
	 */
112
	else {
113
		$tab_title = $current_tests->get_tab_title( $tab );
114
115
		include_once APP_DIR . '/views/header.php';
116
		include_once APP_DIR . '/views/notes-legend.php';
117
118
		// Hidden feature - pre-load all tabs, slow, but useful for source compare & generating of static files.
119
		$all = false;
120
		if ( isset( $_GET['all'] ) && $_GET['all'] === '1' ) {
121
			$all = true;
122
			ini_set( 'max_execution_time', '180' ); // Lengthen allowed execution time.
123
		}
124
125
		$current_tests->do_page( $all );
126
127
		include_once APP_DIR . '/views/footer.php';
128
	}
129
}
130
131
/**
132
 * Load an extraneous page (about, links etc).
133
 */
134
else {
135
	include_once APP_DIR . '/views/header.php';
136
137
	if ( isset( $page ) && $page !== '' ) {
138
		if ( file_exists( APP_DIR . '/views/' . $page . '.php' ) ) {
139
			include_once APP_DIR . '/views/' . $page . '.php';
140
		}
141
		else {
142
			include_once APP_DIR . '/views/cheat-sheet-menu.php';
143
		}
144
	}
145
	else {
146
		include_once APP_DIR . '/views/cheat-sheet-menu.php';
147
	}
148
149
	include_once APP_DIR . '/views/footer.php';
150
}
151