summaryrefslogtreecommitdiffstats
path: root/lib/security.lib.php
blob: b49ab7525b971c8a4f0e9558fc1a785311193f48 (plain)
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
<?php
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
/**
 * Debug Library
 *
 * This library is loaded when somehow our mojo has
 * been lost, it contains functions for checking sql
 * connections, web paths etc..
 *
 *
 * LICENSE: GNU General Public License, version 2 (GPLv2)
 * Copyright (c) 2001 - 2011 Ampache.org All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License v2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * @package	Ampache
 * @copyright	2001 - 2011 Ampache.org
 * @license	http://opensource.org/licenses/gpl-2.0 GPLv2
 * @link	http://www.ampache.org/
 */

/**
 * check_ampache_version
 *
 * This function checks latest ampache stable from Ampache web site.
 * If new version found, return error message.
 *
 * @return	string
 */
function check_ampache_version() {

	$my_ampache = Config::get('version');
	if(preg_match('#-#', $my_ampache)) {
		$my_ampache = explode('-', $my_ampache);
		$my_ampache = $my_ampache[0];
	}

	$latest_ampache = get_latest('ampache');
	$latest_ampache = $latest_ampache['ampache'];

	if(version_compare($my_ampache, $latest_ampache, '>=')) {
		$results = debug_result(_('No problem found.'),1);
	}
	else {
		$results = debug_result(sprintf(_('You are running old ampache: %s'), $my_ampache),0);
	}

	return $results;

} // check_ampache_version

/**
 * check_php_version
 *
 * This function checks latest PHP stable from php web site.
 * If new version found, return error message.
 * Also, if version is older than 5.2.x, return error message.
 *
 * @return	string
 */
function check_php_version() {

	$my_php = PHP_VERSION;

	$latest_php = get_latest('php');
	if (preg_match('#^5\.3#', $my_php)) {
		$latest_php = $latest_php['php5.3'];
	}
	elseif (preg_match('#^5\.2#', $my_php)) {
		$latest_php = $latest_php['php5.2'];
	}
	else {
		$results = debug_result(sprintf(_('Your PHP version may be too old: %s'), $my_php),0);
		return $results;
	}
	if(version_compare($my_php, $latest_php, '>=')) {
		$results = debug_result(_('No probrem found.'),1);
	}
	else {
		$results = debug_result(sprintf(_('You are running old php: %s'), $my_php),0);
	}

	return $results;

} // check_php_version

/**
 * get_latest
 *
 * This function gets from each sites.
 * Pattern may change in a future...
 *
 * @param	string	$type	Type you want to get.
 * @return	array	return version number.
 */
function get_latest($type = null) {

	if (!$type) { return false; }
	$version = array();

	switch ($type) {
		case 'php':
			$url = "http://www.php.net/downloads.php";
			$pattern = '#<h1 id="v(.*)">PHP (.*)</h1>#';
		break;
		case 'ampache':
			$url = "http://ampache.org/download/";
			$pattern = '#<a onclick=.*>(.*) Stable</a>#';
		break;
		default:
			$url = "";
		break;
	}
	if (!$url) { return false; }

	if (!extension_loaded('curl')) {
		return false;
	}
	$ch = curl_init($url);
	$phost = Config::get('proxy_host');
	$pport = Config::get('proxy_port');
	$header = array(
		"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 1.1.4322; OfficeLiveConnector.1.3; OfficeLivePatch.0.0)",
		"Accept: */*",
		"Accept-Encoding: none",
		"Cache-Control: no-cache",
		"Pragma: no-cache",
		"Connection: keep-alive");
	if (isset($phost) && isset($pport)) {
		curl_setopt($ch, CURLOPT_PROXY, $phost);
		curl_setopt($ch, CURLOPT_PROXYPORT, $pport);
	}
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

	ob_start();

	curl_exec($ch);
	curl_close($ch);

	$body = ob_get_contents();
	ob_end_clean();

	preg_match_all($pattern, $body, $versions);
	if (strcmp($type, "ampache") == 0) {
		$version['ampache'] = $versions[1][0];
	}
	elseif (strcmp($type, "php") == 0) {
		$version['php5.3'] = $versions[1][0];
		$version['php5.2'] = $versions[1][1];
	}

	return $version;

} // get_latest

/**
 * check_security
 *
 * This function tests wheter vulnerable settings on your php.ini
 * 
 * @return	array	Show security messages, if found.
 */
function check_security() {

	$warnings = array();

	if(ini_get('display_errors') == '1') {
		$warnings['display_errors'] = _('Provide useful information to attack the error information.');
	}
	if(ini_get('expose_php') == '1') {
		$warnings['expose_php'] = _('Including the PHP version that is described in the HTTP header. It is "INI_SYSTEM" because it is set can be changed only in the server configuration.');
	}
	if(ini_get('session.use_only_cookies') == '0') {
		$warnings['session.use_only_cookies'] = _('URL specified in session and the session ID is initialized using. (permissive session management. if you set 1 URL, POST in the session ID is ignored)');
	}

	if(count($warnings) == 0) {
		$warnings['no_probrem'] = _('There is no probrem.');
	}

	return $warnings;

} // check_security
?>