index.php 9.79 KB
Newer Older
JULIO JARAMILLO's avatar
JULIO JARAMILLO committed
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
<?php

/**
 * Yii Requirement Checker script
 *
 * This script will check if your system meets the requirements for running
 * Yii-powered Web applications.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.yiiframework.com/
 * @copyright 2008-2013 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 * @package system
 * @since 1.0
 */
/**
 * @var array List of requirements (name, required or not, result, used by, memo)
 */
$requirements=array(
	array(
		t('yii','PHP version'),
		true,
		version_compare(PHP_VERSION,"5.1.0",">="),
		'<a href="http://www.yiiframework.com">Yii Framework</a>',
		t('yii','PHP 5.1.0 or higher is required.')),
	array(
		t('yii','$_SERVER variable'),
		true,
		'' === $message=checkServerVar(),
		'<a href="http://www.yiiframework.com">Yii Framework</a>',
		$message),
	array(
		t('yii','Reflection extension'),
		true,
		class_exists('Reflection',false),
		'<a href="http://www.yiiframework.com">Yii Framework</a>',
		''),
	array(
		t('yii','PCRE extension'),
		true,
		extension_loaded("pcre"),
		'<a href="http://www.yiiframework.com">Yii Framework</a>',
		''),
	array(
		t('yii','SPL extension'),
		true,
		extension_loaded("SPL"),
		'<a href="http://www.yiiframework.com">Yii Framework</a>',
		''),
	array(
		t('yii','DOM extension'),
		false,
		class_exists("DOMDocument",false),
		'<a href="http://www.yiiframework.com/doc/api/CHtmlPurifier">CHtmlPurifier</a>, <a href="http://www.yiiframework.com/doc/api/CWsdlGenerator">CWsdlGenerator</a>',
		''),
	array(
		t('yii','PDO extension'),
		false,
		extension_loaded('pdo'),
		t('yii','All <a href="http://www.yiiframework.com/doc/api/#system.db">DB-related classes</a>'),
		''),
	array(
		t('yii','PDO SQLite extension'),
		false,
		extension_loaded('pdo_sqlite'),
		t('yii','All <a href="http://www.yiiframework.com/doc/api/#system.db">DB-related classes</a>'),
		t('yii','Required for SQLite database.')),
	array(
		t('yii','PDO MySQL extension'),
		false,
		extension_loaded('pdo_mysql'),
		t('yii','All <a href="http://www.yiiframework.com/doc/api/#system.db">DB-related classes</a>'),
		t('yii','Required for MySQL database.')),
	array(
		t('yii','PDO PostgreSQL extension'),
		false,
		extension_loaded('pdo_pgsql'),
		t('yii','All <a href="http://www.yiiframework.com/doc/api/#system.db">DB-related classes</a>'),
		t('yii','Required for PostgreSQL database.')),
	array(
		t('yii','PDO Oracle extension'),
		false,
		extension_loaded('pdo_oci'),
		t('yii','All <a href="http://www.yiiframework.com/doc/api/#system.db">DB-related classes</a>'),
		t('yii','Required for Oracle database.')),
	array(
		t('yii','PDO MSSQL extension (pdo_mssql)'),
		false,
		extension_loaded('pdo_mssql'),
		t('yii','All <a href="http://www.yiiframework.com/doc/api/#system.db">DB-related classes</a>'),
		t('yii','Required for MSSQL database from MS Windows')),
	array(
		t('yii','PDO MSSQL extension (pdo_dblib)'),
		false,
		extension_loaded('pdo_dblib'),
		t('yii','All <a href="http://www.yiiframework.com/doc/api/#system.db">DB-related classes</a>'),
		t('yii','Required for MSSQL database from GNU/Linux or other UNIX.')),
	array(
		t('yii','PDO MSSQL extension (<a href="http://sqlsrvphp.codeplex.com/">pdo_sqlsrv</a>)'),
		false,
		extension_loaded('pdo_sqlsrv'),
		t('yii','All <a href="http://www.yiiframework.com/doc/api/#system.db">DB-related classes</a>'),
		t('yii','Required for MSSQL database with the driver provided by Microsoft.')),
	array(
		t('yii','PDO ODBC extension'),
		false,
		extension_loaded('pdo_odbc'),
		t('yii','All <a href="http://www.yiiframework.com/doc/api/#system.db">DB-related classes</a>'),
		t('yii','Required in case database interaction will be through ODBC layer.')),
	array(
		t('yii','Memcache extension'),
		false,
		extension_loaded("memcache") || extension_loaded("memcached"),
		'<a href="http://www.yiiframework.com/doc/api/CMemCache">CMemCache</a>',
		extension_loaded("memcached") ? t('yii', 'To use memcached set <a href="http://www.yiiframework.com/doc/api/CMemCache#useMemcached-detail">CMemCache::useMemcached</a> to <code>true</code>.') : ''),
	array(
		t('yii','APC extension'),
		false,
		extension_loaded("apc"),
		'<a href="http://www.yiiframework.com/doc/api/CApcCache">CApcCache</a>',
		''),
	array(
		t('yii','Mcrypt extension'),
		false,
		extension_loaded("mcrypt"),
		'<a href="http://www.yiiframework.com/doc/api/CSecurityManager">CSecurityManager</a>',
		t('yii','Required by encrypt and decrypt methods.')),
	array(
		t('yii','crypt() CRYPT_BLOWFISH option'),
		false,
		function_exists('crypt') && defined('CRYPT_BLOWFISH') && CRYPT_BLOWFISH,
		'<a href="http://www.yiiframework.com/doc/api/1.1/CPasswordHelper">CPasswordHelper</a>',
		t('yii','Required for secure password storage.')),
	array(
		t('yii','SOAP extension'),
		false,
		extension_loaded("soap"),
		'<a href="http://www.yiiframework.com/doc/api/CWebService">CWebService</a>, <a href="http://www.yiiframework.com/doc/api/CWebServiceAction">CWebServiceAction</a>',
		''),
	array(
		t('yii','GD extension with<br />FreeType support<br />or ImageMagick<br />extension with<br />PNG support'),
		false,
		'' === $message=checkCaptchaSupport(),
		'<a href="http://www.yiiframework.com/doc/api/CCaptchaAction">CCaptchaAction</a>',
		$message),
	array(
		t('yii','Ctype extension'),
		false,
		extension_loaded("ctype"),
		'<a href="http://www.yiiframework.com/doc/api/CDateFormatter">CDateFormatter</a>, <a href="http://www.yiiframework.com/doc/api/CDateFormatter">CDateTimeParser</a>, <a href="http://www.yiiframework.com/doc/api/CTextHighlighter">CTextHighlighter</a>, <a href="http://www.yiiframework.com/doc/api/CHtmlPurifier">CHtmlPurifier</a>',
		''
	),
	array(
		t('yii','Fileinfo extension'),
		false,
		extension_loaded("fileinfo"),
		'<a href="http://www.yiiframework.com/doc/api/CFileValidator">CFileValidator</a>',
		t('yii','Required for MIME-type validation')
	),
);

function checkServerVar()
{
	$vars=array('HTTP_HOST','SERVER_NAME','SERVER_PORT','SCRIPT_NAME','SCRIPT_FILENAME','PHP_SELF','HTTP_ACCEPT','HTTP_USER_AGENT');
	$missing=array();
	foreach($vars as $var)
	{
		if(!isset($_SERVER[$var]))
			$missing[]=$var;
	}
	if(!empty($missing))
		return t('yii','$_SERVER does not have {vars}.',array('{vars}'=>implode(', ',$missing)));

	if(realpath($_SERVER["SCRIPT_FILENAME"]) !== realpath(__FILE__))
		return t('yii','$_SERVER["SCRIPT_FILENAME"] must be the same as the entry script file path.');

	if(!isset($_SERVER["REQUEST_URI"]) && isset($_SERVER["QUERY_STRING"]))
		return t('yii','Either $_SERVER["REQUEST_URI"] or $_SERVER["QUERY_STRING"] must exist.');

	if(!isset($_SERVER["PATH_INFO"]) && strpos($_SERVER["PHP_SELF"],$_SERVER["SCRIPT_NAME"]) !== 0)
		return t('yii','Unable to determine URL path info. Please make sure $_SERVER["PATH_INFO"] (or $_SERVER["PHP_SELF"] and $_SERVER["SCRIPT_NAME"]) contains proper value.');

	return '';
}

function checkCaptchaSupport()
{
	if(extension_loaded('imagick'))
	{
		$imagick=new Imagick();
		$imagickFormats=$imagick->queryFormats('PNG');
	}
	if(extension_loaded('gd'))
		$gdInfo=gd_info();
	if(isset($imagickFormats) && in_array('PNG',$imagickFormats))
		return '';
	elseif(isset($gdInfo))
	{
		if($gdInfo['FreeType Support'])
			return '';
		return t('yii','GD installed,<br />FreeType support not installed');
	}
	return t('yii','GD or ImageMagick not installed');
}

function getYiiVersion()
{
	$coreFile=dirname(__FILE__).'/../framework/YiiBase.php';
	if(is_file($coreFile))
	{
		$contents=file_get_contents($coreFile);
		$matches=array();
		if(preg_match('/public static function getVersion.*?return \'(.*?)\'/ms',$contents,$matches) > 0)
			return $matches[1];
	}
	return '';
}

/**
 * Returns a localized message according to user preferred language.
 * @param string message category
 * @param string message to be translated
 * @param array parameters to be applied to the translated message
 * @return string translated message
 */
function t($category,$message,$params=array())
{
	static $messages;

	if($messages === null)
	{
		$messages=array();
		if(($lang=getPreferredLanguage()) !== false)
		{
			$file=dirname(__FILE__)."/messages/$lang/yii.php";
			if(is_file($file))
				$messages=include($file);
		}
	}

	if(empty($message))
		return $message;

	if(isset($messages[$message]) && $messages[$message] !== '')
		$message=$messages[$message];

	return $params !== array() ? strtr($message,$params) : $message;
}

function getPreferredLanguage()
{
	if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && ($n=preg_match_all('/([\w\-]+)\s*(;\s*q\s*=\s*(\d*\.\d*))?/',$_SERVER['HTTP_ACCEPT_LANGUAGE'],$matches)) > 0)
	{
		$languages=array();
		for($i=0; $i < $n; ++$i)
			$languages[$matches[1][$i]]=empty($matches[3][$i]) ? 1.0 : floatval($matches[3][$i]);
		arsort($languages);
		foreach($languages as $language=>$pref)
		{
			$lang=strtolower(str_replace('-','_',$language));
			if (preg_match("/^en\_?/", $lang))
				return false;
			if (!is_file($viewFile=dirname(__FILE__)."/views/$lang/index.php"))
				$lang=false;
			else
				break;
		}
		return $lang;
	}
	return false;
}

function getServerInfo()
{
	$info[]=isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : '';
	$info[]='<a href="http://www.yiiframework.com/">Yii Framework</a>/'.getYiiVersion();
	$info[]=@strftime('%Y-%m-%d %H:%M',time());

	return implode(' ',$info);
}

function renderFile($_file_,$_params_=array())
{
	extract($_params_);
	require($_file_);
}

$result=1;  // 1: all pass, 0: fail, -1: pass with warnings

foreach($requirements as $i=>$requirement)
{
	if($requirement[1] && !$requirement[2])
		$result=0;
	else if($result > 0 && !$requirement[1] && !$requirement[2])
		$result=-1;
	if($requirement[4] === '')
		$requirements[$i][4]='&nbsp;';
}

$lang=getPreferredLanguage();
$viewFile=dirname(__FILE__)."/views/$lang/index.php";
if(!is_file($viewFile))
	$viewFile=dirname(__FILE__).'/views/index.php';

renderFile($viewFile,array(
	'requirements'=>$requirements,
	'result'=>$result,
	'serverInfo'=>getServerInfo()));