Config.php 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Configuration handling.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use DirectoryIterator;
  10. use PhpMyAdmin\Core;
  11. use PhpMyAdmin\Error;
  12. use PhpMyAdmin\LanguageManager;
  13. use PhpMyAdmin\ThemeManager;
  14. use PhpMyAdmin\Url;
  15. use PhpMyAdmin\UserPreferences;
  16. use PhpMyAdmin\Util;
  17. use PhpMyAdmin\Utils\HttpRequest;
  18. /**
  19. * Indication for error handler (see end of this file).
  20. */
  21. $GLOBALS['pma_config_loading'] = false;
  22. /**
  23. * Configuration class
  24. *
  25. * @package PhpMyAdmin
  26. */
  27. class Config
  28. {
  29. /**
  30. * @var string default config source
  31. */
  32. var $default_source = './libraries/config.default.php';
  33. /**
  34. * @var array default configuration settings
  35. */
  36. var $default = array();
  37. /**
  38. * @var array configuration settings, without user preferences applied
  39. */
  40. var $base_settings = array();
  41. /**
  42. * @var array configuration settings
  43. */
  44. var $settings = array();
  45. /**
  46. * @var string config source
  47. */
  48. var $source = '';
  49. /**
  50. * @var int source modification time
  51. */
  52. var $source_mtime = 0;
  53. var $default_source_mtime = 0;
  54. var $set_mtime = 0;
  55. /**
  56. * @var boolean
  57. */
  58. var $error_config_file = false;
  59. /**
  60. * @var boolean
  61. */
  62. var $error_config_default_file = false;
  63. /**
  64. * @var array
  65. */
  66. var $default_server = array();
  67. /**
  68. * @var boolean whether init is done or not
  69. * set this to false to force some initial checks
  70. * like checking for required functions
  71. */
  72. var $done = false;
  73. /**
  74. * @var UserPreferences
  75. */
  76. private $userPreferences;
  77. /**
  78. * constructor
  79. *
  80. * @param string $source source to read config from
  81. */
  82. public function __construct($source = null)
  83. {
  84. $this->settings = array('is_setup' => false);
  85. // functions need to refresh in case of config file changed goes in
  86. // PhpMyAdmin\Config::load()
  87. $this->load($source);
  88. // other settings, independent from config file, comes in
  89. $this->checkSystem();
  90. $this->base_settings = $this->settings;
  91. $this->userPreferences = new UserPreferences();
  92. }
  93. /**
  94. * sets system and application settings
  95. *
  96. * @return void
  97. */
  98. public function checkSystem()
  99. {
  100. $this->set('PMA_VERSION', '4.8.3');
  101. /* Major version */
  102. $this->set(
  103. 'PMA_MAJOR_VERSION',
  104. implode('.', array_slice(explode('.', $this->get('PMA_VERSION'), 3), 0, 2))
  105. );
  106. $this->checkWebServerOs();
  107. $this->checkWebServer();
  108. $this->checkGd2();
  109. $this->checkClient();
  110. $this->checkUpload();
  111. $this->checkUploadSize();
  112. $this->checkOutputCompression();
  113. }
  114. /**
  115. * whether to use gzip output compression or not
  116. *
  117. * @return void
  118. */
  119. public function checkOutputCompression()
  120. {
  121. // If zlib output compression is set in the php configuration file, no
  122. // output buffering should be run
  123. if (ini_get('zlib.output_compression')) {
  124. $this->set('OBGzip', false);
  125. }
  126. // enable output-buffering (if set to 'auto')
  127. if (strtolower($this->get('OBGzip')) == 'auto') {
  128. $this->set('OBGzip', true);
  129. }
  130. }
  131. /**
  132. * Sets the client platform based on user agent
  133. *
  134. * @param string $user_agent the user agent
  135. *
  136. * @return void
  137. */
  138. private function _setClientPlatform($user_agent)
  139. {
  140. if (mb_strstr($user_agent, 'Win')) {
  141. $this->set('PMA_USR_OS', 'Win');
  142. } elseif (mb_strstr($user_agent, 'Mac')) {
  143. $this->set('PMA_USR_OS', 'Mac');
  144. } elseif (mb_strstr($user_agent, 'Linux')) {
  145. $this->set('PMA_USR_OS', 'Linux');
  146. } elseif (mb_strstr($user_agent, 'Unix')) {
  147. $this->set('PMA_USR_OS', 'Unix');
  148. } elseif (mb_strstr($user_agent, 'OS/2')) {
  149. $this->set('PMA_USR_OS', 'OS/2');
  150. } else {
  151. $this->set('PMA_USR_OS', 'Other');
  152. }
  153. }
  154. /**
  155. * Determines platform (OS), browser and version of the user
  156. * Based on a phpBuilder article:
  157. *
  158. * @see http://www.phpbuilder.net/columns/tim20000821.php
  159. *
  160. * @return void
  161. */
  162. public function checkClient()
  163. {
  164. if (Core::getenv('HTTP_USER_AGENT')) {
  165. $HTTP_USER_AGENT = Core::getenv('HTTP_USER_AGENT');
  166. } else {
  167. $HTTP_USER_AGENT = '';
  168. }
  169. // 1. Platform
  170. $this->_setClientPlatform($HTTP_USER_AGENT);
  171. // 2. browser and version
  172. // (must check everything else before Mozilla)
  173. $is_mozilla = preg_match(
  174. '@Mozilla/([0-9]\.[0-9]{1,2})@',
  175. $HTTP_USER_AGENT,
  176. $mozilla_version
  177. );
  178. if (preg_match(
  179. '@Opera(/| )([0-9]\.[0-9]{1,2})@',
  180. $HTTP_USER_AGENT,
  181. $log_version
  182. )) {
  183. $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
  184. $this->set('PMA_USR_BROWSER_AGENT', 'OPERA');
  185. } elseif (preg_match(
  186. '@(MS)?IE ([0-9]{1,2}\.[0-9]{1,2})@',
  187. $HTTP_USER_AGENT,
  188. $log_version
  189. )) {
  190. $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
  191. $this->set('PMA_USR_BROWSER_AGENT', 'IE');
  192. } elseif (preg_match(
  193. '@Trident/(7)\.0@',
  194. $HTTP_USER_AGENT,
  195. $log_version
  196. )) {
  197. $this->set('PMA_USR_BROWSER_VER', intval($log_version[1]) + 4);
  198. $this->set('PMA_USR_BROWSER_AGENT', 'IE');
  199. } elseif (preg_match(
  200. '@OmniWeb/([0-9]{1,3})@',
  201. $HTTP_USER_AGENT,
  202. $log_version
  203. )) {
  204. $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
  205. $this->set('PMA_USR_BROWSER_AGENT', 'OMNIWEB');
  206. // Konqueror 2.2.2 says Konqueror/2.2.2
  207. // Konqueror 3.0.3 says Konqueror/3
  208. } elseif (preg_match(
  209. '@(Konqueror/)(.*)(;)@',
  210. $HTTP_USER_AGENT,
  211. $log_version
  212. )) {
  213. $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
  214. $this->set('PMA_USR_BROWSER_AGENT', 'KONQUEROR');
  215. // must check Chrome before Safari
  216. } elseif ($is_mozilla
  217. && preg_match('@Chrome/([0-9.]*)@', $HTTP_USER_AGENT, $log_version)
  218. ) {
  219. $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
  220. $this->set('PMA_USR_BROWSER_AGENT', 'CHROME');
  221. // newer Safari
  222. } elseif ($is_mozilla
  223. && preg_match('@Version/(.*) Safari@', $HTTP_USER_AGENT, $log_version)
  224. ) {
  225. $this->set(
  226. 'PMA_USR_BROWSER_VER', $log_version[1]
  227. );
  228. $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
  229. // older Safari
  230. } elseif ($is_mozilla
  231. && preg_match('@Safari/([0-9]*)@', $HTTP_USER_AGENT, $log_version)
  232. ) {
  233. $this->set(
  234. 'PMA_USR_BROWSER_VER', $mozilla_version[1] . '.' . $log_version[1]
  235. );
  236. $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
  237. // Firefox
  238. } elseif (! mb_strstr($HTTP_USER_AGENT, 'compatible')
  239. && preg_match('@Firefox/([\w.]+)@', $HTTP_USER_AGENT, $log_version)
  240. ) {
  241. $this->set(
  242. 'PMA_USR_BROWSER_VER', $log_version[1]
  243. );
  244. $this->set('PMA_USR_BROWSER_AGENT', 'FIREFOX');
  245. } elseif (preg_match('@rv:1\.9(.*)Gecko@', $HTTP_USER_AGENT)) {
  246. $this->set('PMA_USR_BROWSER_VER', '1.9');
  247. $this->set('PMA_USR_BROWSER_AGENT', 'GECKO');
  248. } elseif ($is_mozilla) {
  249. $this->set('PMA_USR_BROWSER_VER', $mozilla_version[1]);
  250. $this->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
  251. } else {
  252. $this->set('PMA_USR_BROWSER_VER', 0);
  253. $this->set('PMA_USR_BROWSER_AGENT', 'OTHER');
  254. }
  255. }
  256. /**
  257. * Whether GD2 is present
  258. *
  259. * @return void
  260. */
  261. public function checkGd2()
  262. {
  263. if ($this->get('GD2Available') == 'yes') {
  264. $this->set('PMA_IS_GD2', 1);
  265. return;
  266. }
  267. if ($this->get('GD2Available') == 'no') {
  268. $this->set('PMA_IS_GD2', 0);
  269. return;
  270. }
  271. if (!function_exists('imagecreatetruecolor')) {
  272. $this->set('PMA_IS_GD2', 0);
  273. return;
  274. }
  275. if (function_exists('gd_info')) {
  276. $gd_nfo = gd_info();
  277. if (mb_strstr($gd_nfo["GD Version"], '2.')) {
  278. $this->set('PMA_IS_GD2', 1);
  279. } else {
  280. $this->set('PMA_IS_GD2', 0);
  281. }
  282. } else {
  283. $this->set('PMA_IS_GD2', 0);
  284. }
  285. }
  286. /**
  287. * Whether the Web server php is running on is IIS
  288. *
  289. * @return void
  290. */
  291. public function checkWebServer()
  292. {
  293. // some versions return Microsoft-IIS, some Microsoft/IIS
  294. // we could use a preg_match() but it's slower
  295. if (Core::getenv('SERVER_SOFTWARE')
  296. && stristr(Core::getenv('SERVER_SOFTWARE'), 'Microsoft')
  297. && stristr(Core::getenv('SERVER_SOFTWARE'), 'IIS')
  298. ) {
  299. $this->set('PMA_IS_IIS', 1);
  300. } else {
  301. $this->set('PMA_IS_IIS', 0);
  302. }
  303. }
  304. /**
  305. * Whether the os php is running on is windows or not
  306. *
  307. * @return void
  308. */
  309. public function checkWebServerOs()
  310. {
  311. // Default to Unix or Equiv
  312. $this->set('PMA_IS_WINDOWS', 0);
  313. // If PHP_OS is defined then continue
  314. if (defined('PHP_OS')) {
  315. if (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) {
  316. // Is it some version of Windows
  317. $this->set('PMA_IS_WINDOWS', 1);
  318. } elseif (stristr(PHP_OS, 'OS/2')) {
  319. // Is it OS/2 (No file permissions like Windows)
  320. $this->set('PMA_IS_WINDOWS', 1);
  321. }
  322. }
  323. }
  324. /**
  325. * detects if Git revision
  326. *
  327. * @return boolean
  328. */
  329. public function isGitRevision()
  330. {
  331. if (!$this->get('ShowGitRevision')) {
  332. return false;
  333. }
  334. // caching
  335. if (isset($_SESSION['is_git_revision'])) {
  336. if ($_SESSION['is_git_revision']) {
  337. $this->set('PMA_VERSION_GIT', 1);
  338. }
  339. return $_SESSION['is_git_revision'];
  340. }
  341. // find out if there is a .git folder
  342. $git_folder = '.git';
  343. if (! @file_exists($git_folder)
  344. || ! @file_exists($git_folder . '/config')
  345. ) {
  346. $_SESSION['is_git_revision'] = false;
  347. return false;
  348. }
  349. $_SESSION['is_git_revision'] = true;
  350. return true;
  351. }
  352. /**
  353. * detects Git revision, if running inside repo
  354. *
  355. * @return void
  356. */
  357. public function checkGitRevision()
  358. {
  359. // find out if there is a .git folder
  360. $git_folder = '.git';
  361. if (! $this->isGitRevision()) {
  362. return;
  363. }
  364. if (! $ref_head = @file_get_contents($git_folder . '/HEAD')) {
  365. return;
  366. }
  367. $branch = false;
  368. // are we on any branch?
  369. if (strstr($ref_head, '/')) {
  370. // remove ref: prefix
  371. $ref_head = substr(trim($ref_head), 5);
  372. if (substr($ref_head, 0, 11) === 'refs/heads/') {
  373. $branch = substr($ref_head, 11);
  374. } else {
  375. $branch = basename($ref_head);
  376. }
  377. $ref_file = $git_folder . '/' . $ref_head;
  378. if (@file_exists($ref_file)) {
  379. $hash = @file_get_contents($ref_file);
  380. if (! $hash) {
  381. return;
  382. }
  383. $hash = trim($hash);
  384. } else {
  385. // deal with packed refs
  386. $packed_refs = @file_get_contents($git_folder . '/packed-refs');
  387. if (! $packed_refs) {
  388. return;
  389. }
  390. // split file to lines
  391. $ref_lines = explode("\n", $packed_refs);
  392. foreach ($ref_lines as $line) {
  393. // skip comments
  394. if ($line[0] == '#') {
  395. continue;
  396. }
  397. // parse line
  398. $parts = explode(' ', $line);
  399. // care only about named refs
  400. if (count($parts) != 2) {
  401. continue;
  402. }
  403. // have found our ref?
  404. if ($parts[1] == $ref_head) {
  405. $hash = $parts[0];
  406. break;
  407. }
  408. }
  409. if (! isset($hash)) {
  410. // Could not find ref
  411. return;
  412. }
  413. }
  414. } else {
  415. $hash = trim($ref_head);
  416. }
  417. $commit = false;
  418. if (! preg_match('/^[0-9a-f]{40}$/i', $hash)) {
  419. $commit = false;
  420. } elseif (isset($_SESSION['PMA_VERSION_COMMITDATA_' . $hash])) {
  421. $commit = $_SESSION['PMA_VERSION_COMMITDATA_' . $hash];
  422. } elseif (function_exists('gzuncompress')) {
  423. $git_file_name = $git_folder . '/objects/'
  424. . substr($hash, 0, 2) . '/' . substr($hash, 2);
  425. if (@file_exists($git_file_name) ) {
  426. if (! $commit = @file_get_contents($git_file_name)) {
  427. return;
  428. }
  429. $commit = explode("\0", gzuncompress($commit), 2);
  430. $commit = explode("\n", $commit[1]);
  431. $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
  432. } else {
  433. $pack_names = array();
  434. // work with packed data
  435. $packs_file = $git_folder . '/objects/info/packs';
  436. if (@file_exists($packs_file)
  437. && $packs = @file_get_contents($packs_file)
  438. ) {
  439. // File exists. Read it, parse the file to get the names of the
  440. // packs. (to look for them in .git/object/pack directory later)
  441. foreach (explode("\n", $packs) as $line) {
  442. // skip blank lines
  443. if (strlen(trim($line)) == 0) {
  444. continue;
  445. }
  446. // skip non pack lines
  447. if ($line[0] != 'P') {
  448. continue;
  449. }
  450. // parse names
  451. $pack_names[] = substr($line, 2);
  452. }
  453. } else {
  454. // '.git/objects/info/packs' file can be missing
  455. // (atlease in mysGit)
  456. // File missing. May be we can look in the .git/object/pack
  457. // directory for all the .pack files and use that list of
  458. // files instead
  459. $dirIterator = new DirectoryIterator(
  460. $git_folder . '/objects/pack'
  461. );
  462. foreach ($dirIterator as $file_info) {
  463. $file_name = $file_info->getFilename();
  464. // if this is a .pack file
  465. if ($file_info->isFile() && substr($file_name, -5) == '.pack'
  466. ) {
  467. $pack_names[] = $file_name;
  468. }
  469. }
  470. }
  471. $hash = strtolower($hash);
  472. foreach ($pack_names as $pack_name) {
  473. $index_name = str_replace('.pack', '.idx', $pack_name);
  474. // load index
  475. $index_data = @file_get_contents(
  476. $git_folder . '/objects/pack/' . $index_name
  477. );
  478. if (! $index_data) {
  479. continue;
  480. }
  481. // check format
  482. if (substr($index_data, 0, 4) != "\377tOc") {
  483. continue;
  484. }
  485. // check version
  486. $version = unpack('N', substr($index_data, 4, 4));
  487. if ($version[1] != 2) {
  488. continue;
  489. }
  490. // parse fanout table
  491. $fanout = unpack(
  492. "N*",
  493. substr($index_data, 8, 256 * 4)
  494. );
  495. // find where we should search
  496. $firstbyte = intval(substr($hash, 0, 2), 16);
  497. // array is indexed from 1 and we need to get
  498. // previous entry for start
  499. if ($firstbyte == 0) {
  500. $start = 0;
  501. } else {
  502. $start = $fanout[$firstbyte];
  503. }
  504. $end = $fanout[$firstbyte + 1];
  505. // stupid linear search for our sha
  506. $found = false;
  507. $offset = 8 + (256 * 4);
  508. for ($position = $start; $position < $end; $position++) {
  509. $sha = strtolower(
  510. bin2hex(
  511. substr($index_data, $offset + ($position * 20), 20)
  512. )
  513. );
  514. if ($sha == $hash) {
  515. $found = true;
  516. break;
  517. }
  518. }
  519. if (! $found) {
  520. continue;
  521. }
  522. // read pack offset
  523. $offset = 8 + (256 * 4) + (24 * $fanout[256]);
  524. $pack_offset = unpack(
  525. 'N',
  526. substr($index_data, $offset + ($position * 4), 4)
  527. );
  528. $pack_offset = $pack_offset[1];
  529. // open pack file
  530. $pack_file = fopen(
  531. $git_folder . '/objects/pack/' . $pack_name, 'rb'
  532. );
  533. if ($pack_file === false) {
  534. continue;
  535. }
  536. // seek to start
  537. fseek($pack_file, $pack_offset);
  538. // parse header
  539. $header = ord(fread($pack_file, 1));
  540. $type = ($header >> 4) & 7;
  541. $hasnext = ($header & 128) >> 7;
  542. $size = $header & 0xf;
  543. $offset = 4;
  544. while ($hasnext) {
  545. $byte = ord(fread($pack_file, 1));
  546. $size |= ($byte & 0x7f) << $offset;
  547. $hasnext = ($byte & 128) >> 7;
  548. $offset += 7;
  549. }
  550. // we care only about commit objects
  551. if ($type != 1) {
  552. continue;
  553. }
  554. // read data
  555. $commit = fread($pack_file, $size);
  556. $commit = gzuncompress($commit);
  557. $commit = explode("\n", $commit);
  558. $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
  559. fclose($pack_file);
  560. }
  561. }
  562. }
  563. $httpRequest = new HttpRequest();
  564. // check if commit exists in Github
  565. if ($commit !== false
  566. && isset($_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash])
  567. ) {
  568. $is_remote_commit = $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash];
  569. } else {
  570. $link = 'https://www.phpmyadmin.net/api/commit/' . $hash . '/';
  571. $is_found = $httpRequest->create($link, 'GET');
  572. switch($is_found) {
  573. case false:
  574. $is_remote_commit = false;
  575. $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;
  576. break;
  577. case null:
  578. // no remote link for now, but don't cache this as Github is down
  579. $is_remote_commit = false;
  580. break;
  581. default:
  582. $is_remote_commit = true;
  583. $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = true;
  584. if ($commit === false) {
  585. // if no local commit data, try loading from Github
  586. $commit_json = json_decode($is_found);
  587. }
  588. break;
  589. }
  590. }
  591. $is_remote_branch = false;
  592. if ($is_remote_commit && $branch !== false) {
  593. // check if branch exists in Github
  594. if (isset($_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash])) {
  595. $is_remote_branch = $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash];
  596. } else {
  597. $link = 'https://www.phpmyadmin.net/api/tree/' . $branch . '/';
  598. $is_found = $httpRequest->create($link, 'GET', true);
  599. switch($is_found) {
  600. case true:
  601. $is_remote_branch = true;
  602. $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = true;
  603. break;
  604. case false:
  605. $is_remote_branch = false;
  606. $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = false;
  607. break;
  608. case null:
  609. // no remote link for now, but don't cache this as Github is down
  610. $is_remote_branch = false;
  611. break;
  612. }
  613. }
  614. }
  615. if ($commit !== false) {
  616. $author = array('name' => '', 'email' => '', 'date' => '');
  617. $committer = array('name' => '', 'email' => '', 'date' => '');
  618. do {
  619. $dataline = array_shift($commit);
  620. $datalinearr = explode(' ', $dataline, 2);
  621. $linetype = $datalinearr[0];
  622. if (in_array($linetype, array('author', 'committer'))) {
  623. $user = $datalinearr[1];
  624. preg_match('/([^<]+)<([^>]+)> ([0-9]+)( [^ ]+)?/', $user, $user);
  625. $user2 = array(
  626. 'name' => trim($user[1]),
  627. 'email' => trim($user[2]),
  628. 'date' => date('Y-m-d H:i:s', $user[3]));
  629. if (isset($user[4])) {
  630. $user2['date'] .= $user[4];
  631. }
  632. $$linetype = $user2;
  633. }
  634. } while ($dataline != '');
  635. $message = trim(implode(' ', $commit));
  636. } elseif (isset($commit_json) && isset($commit_json->author) && isset($commit_json->committer)) {
  637. $author = array(
  638. 'name' => $commit_json->author->name,
  639. 'email' => $commit_json->author->email,
  640. 'date' => $commit_json->author->date);
  641. $committer = array(
  642. 'name' => $commit_json->committer->name,
  643. 'email' => $commit_json->committer->email,
  644. 'date' => $commit_json->committer->date);
  645. $message = trim($commit_json->message);
  646. } else {
  647. return;
  648. }
  649. $this->set('PMA_VERSION_GIT', 1);
  650. $this->set('PMA_VERSION_GIT_COMMITHASH', $hash);
  651. $this->set('PMA_VERSION_GIT_BRANCH', $branch);
  652. $this->set('PMA_VERSION_GIT_MESSAGE', $message);
  653. $this->set('PMA_VERSION_GIT_AUTHOR', $author);
  654. $this->set('PMA_VERSION_GIT_COMMITTER', $committer);
  655. $this->set('PMA_VERSION_GIT_ISREMOTECOMMIT', $is_remote_commit);
  656. $this->set('PMA_VERSION_GIT_ISREMOTEBRANCH', $is_remote_branch);
  657. }
  658. /**
  659. * loads default values from default source
  660. *
  661. * @return boolean success
  662. */
  663. public function loadDefaults()
  664. {
  665. $cfg = array();
  666. if (! @file_exists($this->default_source)) {
  667. $this->error_config_default_file = true;
  668. return false;
  669. }
  670. $old_error_reporting = error_reporting(0);
  671. ob_start();
  672. $GLOBALS['pma_config_loading'] = true;
  673. $eval_result = include $this->default_source;
  674. $GLOBALS['pma_config_loading'] = false;
  675. ob_end_clean();
  676. error_reporting($old_error_reporting);
  677. if ($eval_result === false) {
  678. $this->error_config_default_file = true;
  679. return false;
  680. }
  681. $this->default_source_mtime = filemtime($this->default_source);
  682. $this->default_server = $cfg['Servers'][1];
  683. unset($cfg['Servers']);
  684. $this->default = $cfg;
  685. $this->settings = array_replace_recursive($this->settings, $cfg);
  686. $this->error_config_default_file = false;
  687. return true;
  688. }
  689. /**
  690. * loads configuration from $source, usually the config file
  691. * should be called on object creation
  692. *
  693. * @param string $source config file
  694. *
  695. * @return bool
  696. */
  697. public function load($source = null)
  698. {
  699. $this->loadDefaults();
  700. if (null !== $source) {
  701. $this->setSource($source);
  702. }
  703. if (! $this->checkConfigSource()) {
  704. return false;
  705. }
  706. $cfg = array();
  707. /**
  708. * Parses the configuration file, we throw away any errors or
  709. * output.
  710. */
  711. $old_error_reporting = error_reporting(0);
  712. ob_start();
  713. $GLOBALS['pma_config_loading'] = true;
  714. $eval_result = include $this->getSource();
  715. $GLOBALS['pma_config_loading'] = false;
  716. ob_end_clean();
  717. error_reporting($old_error_reporting);
  718. if ($eval_result === false) {
  719. $this->error_config_file = true;
  720. } else {
  721. $this->error_config_file = false;
  722. $this->source_mtime = filemtime($this->getSource());
  723. }
  724. /**
  725. * Ignore keys with / as we do not use these
  726. *
  727. * These can be confusing for user configuration layer as it
  728. * flatten array using / and thus don't see difference between
  729. * $cfg['Export/method'] and $cfg['Export']['method'], while rest
  730. * of thre code uses the setting only in latter form.
  731. *
  732. * This could be removed once we consistently handle both values
  733. * in the functional code as well.
  734. *
  735. * It could use array_filter(...ARRAY_FILTER_USE_KEY), but it's not
  736. * supported on PHP 5.5 and HHVM.
  737. */
  738. $matched_keys = array_filter(
  739. array_keys($cfg),
  740. function ($key) {return strpos($key, '/') === false;}
  741. );
  742. $cfg = array_intersect_key($cfg, array_flip($matched_keys));
  743. /**
  744. * Backward compatibility code
  745. */
  746. if (!empty($cfg['DefaultTabTable'])) {
  747. $cfg['DefaultTabTable'] = str_replace(
  748. '_properties',
  749. '',
  750. str_replace(
  751. 'tbl_properties.php',
  752. 'tbl_sql.php',
  753. $cfg['DefaultTabTable']
  754. )
  755. );
  756. }
  757. if (!empty($cfg['DefaultTabDatabase'])) {
  758. $cfg['DefaultTabDatabase'] = str_replace(
  759. '_details',
  760. '',
  761. str_replace(
  762. 'db_details.php',
  763. 'db_sql.php',
  764. $cfg['DefaultTabDatabase']
  765. )
  766. );
  767. }
  768. $this->settings = array_replace_recursive($this->settings, $cfg);
  769. return true;
  770. }
  771. /**
  772. * Sets the connection collation
  773. *
  774. * @return void
  775. */
  776. private function _setConnectionCollation()
  777. {
  778. $collation_connection = $this->get('DefaultConnectionCollation');
  779. if (! empty($collation_connection)
  780. && $collation_connection != $GLOBALS['collation_connection']
  781. ) {
  782. $GLOBALS['dbi']->setCollation($collation_connection);
  783. }
  784. }
  785. /**
  786. * Loads user preferences and merges them with current config
  787. * must be called after control connection has been established
  788. *
  789. * @return void
  790. */
  791. public function loadUserPreferences()
  792. {
  793. // index.php should load these settings, so that phpmyadmin.css.php
  794. // will have everything available in session cache
  795. $server = isset($GLOBALS['server'])
  796. ? $GLOBALS['server']
  797. : (!empty($GLOBALS['cfg']['ServerDefault'])
  798. ? $GLOBALS['cfg']['ServerDefault']
  799. : 0);
  800. $cache_key = 'server_' . $server;
  801. if ($server > 0 && !defined('PMA_MINIMUM_COMMON')) {
  802. $config_mtime = max($this->default_source_mtime, $this->source_mtime);
  803. // cache user preferences, use database only when needed
  804. if (! isset($_SESSION['cache'][$cache_key]['userprefs'])
  805. || $_SESSION['cache'][$cache_key]['config_mtime'] < $config_mtime
  806. ) {
  807. $prefs = $this->userPreferences->load();
  808. $_SESSION['cache'][$cache_key]['userprefs']
  809. = $this->userPreferences->apply($prefs['config_data']);
  810. $_SESSION['cache'][$cache_key]['userprefs_mtime'] = $prefs['mtime'];
  811. $_SESSION['cache'][$cache_key]['userprefs_type'] = $prefs['type'];
  812. $_SESSION['cache'][$cache_key]['config_mtime'] = $config_mtime;
  813. }
  814. } elseif ($server == 0
  815. || ! isset($_SESSION['cache'][$cache_key]['userprefs'])
  816. ) {
  817. $this->set('user_preferences', false);
  818. return;
  819. }
  820. $config_data = $_SESSION['cache'][$cache_key]['userprefs'];
  821. // type is 'db' or 'session'
  822. $this->set(
  823. 'user_preferences',
  824. $_SESSION['cache'][$cache_key]['userprefs_type']
  825. );
  826. $this->set(
  827. 'user_preferences_mtime',
  828. $_SESSION['cache'][$cache_key]['userprefs_mtime']
  829. );
  830. // load config array
  831. $this->settings = array_replace_recursive($this->settings, $config_data);
  832. $GLOBALS['cfg'] = array_replace_recursive($GLOBALS['cfg'], $config_data);
  833. if (defined('PMA_MINIMUM_COMMON')) {
  834. return;
  835. }
  836. // settings below start really working on next page load, but
  837. // changes are made only in index.php so everything is set when
  838. // in frames
  839. // save theme
  840. /** @var ThemeManager $tmanager */
  841. $tmanager = ThemeManager::getInstance();
  842. if ($tmanager->getThemeCookie() || isset($_REQUEST['set_theme'])) {
  843. if ((! isset($config_data['ThemeDefault'])
  844. && $tmanager->theme->getId() != 'original')
  845. || isset($config_data['ThemeDefault'])
  846. && $config_data['ThemeDefault'] != $tmanager->theme->getId()
  847. ) {
  848. // new theme was set in common.inc.php
  849. $this->setUserValue(
  850. null,
  851. 'ThemeDefault',
  852. $tmanager->theme->getId(),
  853. 'original'
  854. );
  855. }
  856. } else {
  857. // no cookie - read default from settings
  858. if ($this->settings['ThemeDefault'] != $tmanager->theme->getId()
  859. && $tmanager->checkTheme($this->settings['ThemeDefault'])
  860. ) {
  861. $tmanager->setActiveTheme($this->settings['ThemeDefault']);
  862. $tmanager->setThemeCookie();
  863. }
  864. }
  865. // save language
  866. if (isset($_COOKIE['pma_lang']) || isset($_POST['lang'])) {
  867. if ((! isset($config_data['lang'])
  868. && $GLOBALS['lang'] != 'en')
  869. || isset($config_data['lang'])
  870. && $GLOBALS['lang'] != $config_data['lang']
  871. ) {
  872. $this->setUserValue(null, 'lang', $GLOBALS['lang'], 'en');
  873. }
  874. } else {
  875. // read language from settings
  876. if (isset($config_data['lang'])) {
  877. $language = LanguageManager::getInstance()->getLanguage(
  878. $config_data['lang']
  879. );
  880. if ($language !== false) {
  881. $language->activate();
  882. $this->setCookie('pma_lang', $language->getCode());
  883. }
  884. }
  885. }
  886. // set connection collation
  887. $this->_setConnectionCollation();
  888. }
  889. /**
  890. * Sets config value which is stored in user preferences (if available)
  891. * or in a cookie.
  892. *
  893. * If user preferences are not yet initialized, option is applied to
  894. * global config and added to a update queue, which is processed
  895. * by {@link loadUserPreferences()}
  896. *
  897. * @param string $cookie_name can be null
  898. * @param string $cfg_path configuration path
  899. * @param mixed $new_cfg_value new value
  900. * @param mixed $default_value default value
  901. *
  902. * @return true|PhpMyAdmin\Message
  903. */
  904. public function setUserValue($cookie_name, $cfg_path, $new_cfg_value,
  905. $default_value = null
  906. ) {
  907. $result = true;
  908. // use permanent user preferences if possible
  909. $prefs_type = $this->get('user_preferences');
  910. if ($prefs_type) {
  911. if ($default_value === null) {
  912. $default_value = Core::arrayRead($cfg_path, $this->default);
  913. }
  914. $result = $this->userPreferences->persistOption($cfg_path, $new_cfg_value, $default_value);
  915. }
  916. if ($prefs_type != 'db' && $cookie_name) {
  917. // fall back to cookies
  918. if ($default_value === null) {
  919. $default_value = Core::arrayRead($cfg_path, $this->settings);
  920. }
  921. $this->setCookie($cookie_name, $new_cfg_value, $default_value);
  922. }
  923. Core::arrayWrite($cfg_path, $GLOBALS['cfg'], $new_cfg_value);
  924. Core::arrayWrite($cfg_path, $this->settings, $new_cfg_value);
  925. return $result;
  926. }
  927. /**
  928. * Reads value stored by {@link setUserValue()}
  929. *
  930. * @param string $cookie_name cookie name
  931. * @param mixed $cfg_value config value
  932. *
  933. * @return mixed
  934. */
  935. public function getUserValue($cookie_name, $cfg_value)
  936. {
  937. $cookie_exists = isset($_COOKIE) && !empty($_COOKIE[$cookie_name]);
  938. $prefs_type = $this->get('user_preferences');
  939. if ($prefs_type == 'db') {
  940. // permanent user preferences value exists, remove cookie
  941. if ($cookie_exists) {
  942. $this->removeCookie($cookie_name);
  943. }
  944. } elseif ($cookie_exists) {
  945. return $_COOKIE[$cookie_name];
  946. }
  947. // return value from $cfg array
  948. return $cfg_value;
  949. }
  950. /**
  951. * set source
  952. *
  953. * @param string $source source
  954. *
  955. * @return void
  956. */
  957. public function setSource($source)
  958. {
  959. $this->source = trim($source);
  960. }
  961. /**
  962. * check config source
  963. *
  964. * @return boolean whether source is valid or not
  965. */
  966. public function checkConfigSource()
  967. {
  968. if (! $this->getSource()) {
  969. // no configuration file set at all
  970. return false;
  971. }
  972. if (! @file_exists($this->getSource())) {
  973. $this->source_mtime = 0;
  974. return false;
  975. }
  976. if (! @is_readable($this->getSource())) {
  977. // manually check if file is readable
  978. // might be bug #3059806 Supporting running from CIFS/Samba shares
  979. $contents = false;
  980. $handle = @fopen($this->getSource(), 'r');
  981. if ($handle !== false) {
  982. $contents = @fread($handle, 1); // reading 1 byte is enough to test
  983. fclose($handle);
  984. }
  985. if ($contents === false) {
  986. $this->source_mtime = 0;
  987. Core::fatalError(
  988. sprintf(
  989. function_exists('__')
  990. ? __('Existing configuration file (%s) is not readable.')
  991. : 'Existing configuration file (%s) is not readable.',
  992. $this->getSource()
  993. )
  994. );
  995. return false;
  996. }
  997. }
  998. return true;
  999. }
  1000. /**
  1001. * verifies the permissions on config file (if asked by configuration)
  1002. * (must be called after config.inc.php has been merged)
  1003. *
  1004. * @return void
  1005. */
  1006. public function checkPermissions()
  1007. {
  1008. // Check for permissions (on platforms that support it):
  1009. if ($this->get('CheckConfigurationPermissions') && @file_exists($this->getSource())) {
  1010. $perms = @fileperms($this->getSource());
  1011. if (!($perms === false) && ($perms & 2)) {
  1012. // This check is normally done after loading configuration
  1013. $this->checkWebServerOs();
  1014. if ($this->get('PMA_IS_WINDOWS') == 0) {
  1015. $this->source_mtime = 0;
  1016. Core::fatalError(
  1017. __(
  1018. 'Wrong permissions on configuration file, '
  1019. . 'should not be world writable!'
  1020. )
  1021. );
  1022. }
  1023. }
  1024. }
  1025. }
  1026. /**
  1027. * Checks for errors
  1028. * (must be called after config.inc.php has been merged)
  1029. *
  1030. * @return void
  1031. */
  1032. public function checkErrors()
  1033. {
  1034. if ($this->error_config_default_file) {
  1035. Core::fatalError(
  1036. sprintf(
  1037. __('Could not load default configuration from: %1$s'),
  1038. $this->default_source
  1039. )
  1040. );
  1041. }
  1042. if ($this->error_config_file) {
  1043. $error = '[strong]' . __('Failed to read configuration file!') . '[/strong]'
  1044. . '[br][br]'
  1045. . __(
  1046. 'This usually means there is a syntax error in it, '
  1047. . 'please check any errors shown below.'
  1048. )
  1049. . '[br][br]'
  1050. . '[conferr]';
  1051. trigger_error($error, E_USER_ERROR);
  1052. }
  1053. }
  1054. /**
  1055. * returns specific config setting
  1056. *
  1057. * @param string $setting config setting
  1058. *
  1059. * @return mixed value
  1060. */
  1061. public function get($setting)
  1062. {
  1063. if (isset($this->settings[$setting])) {
  1064. return $this->settings[$setting];
  1065. }
  1066. return null;
  1067. }
  1068. /**
  1069. * sets configuration variable
  1070. *
  1071. * @param string $setting configuration option
  1072. * @param mixed $value new value for configuration option
  1073. *
  1074. * @return void
  1075. */
  1076. public function set($setting, $value)
  1077. {
  1078. if (! isset($this->settings[$setting])
  1079. || $this->settings[$setting] !== $value
  1080. ) {
  1081. $this->settings[$setting] = $value;
  1082. $this->set_mtime = time();
  1083. }
  1084. }
  1085. /**
  1086. * returns source for current config
  1087. *
  1088. * @return string config source
  1089. */
  1090. public function getSource()
  1091. {
  1092. return $this->source;
  1093. }
  1094. /**
  1095. * returns a unique value to force a CSS reload if either the config
  1096. * or the theme changes
  1097. *
  1098. * @return int Summary of unix timestamps and fontsize,
  1099. * to be unique on theme parameters change
  1100. */
  1101. public function getThemeUniqueValue()
  1102. {
  1103. if (null !== $this->get('FontSize')) {
  1104. $fontsize = intval($this->get('FontSize'));
  1105. } else {
  1106. $fontsize = 0;
  1107. }
  1108. return (
  1109. $fontsize +
  1110. $this->source_mtime +
  1111. $this->default_source_mtime +
  1112. $this->get('user_preferences_mtime') +
  1113. $GLOBALS['PMA_Theme']->mtime_info +
  1114. $GLOBALS['PMA_Theme']->filesize_info);
  1115. }
  1116. /**
  1117. * checks if upload is enabled
  1118. *
  1119. * @return void
  1120. */
  1121. public function checkUpload()
  1122. {
  1123. if (!ini_get('file_uploads')) {
  1124. $this->set('enable_upload', false);
  1125. return;
  1126. }
  1127. $this->set('enable_upload', true);
  1128. // if set "php_admin_value file_uploads Off" in httpd.conf
  1129. // ini_get() also returns the string "Off" in this case:
  1130. if ('off' == strtolower(ini_get('file_uploads'))) {
  1131. $this->set('enable_upload', false);
  1132. }
  1133. }
  1134. /**
  1135. * Maximum upload size as limited by PHP
  1136. * Used with permission from Moodle (https://moodle.org/) by Martin Dougiamas
  1137. *
  1138. * this section generates $max_upload_size in bytes
  1139. *
  1140. * @return void
  1141. */
  1142. public function checkUploadSize()
  1143. {
  1144. if (! $filesize = ini_get('upload_max_filesize')) {
  1145. $filesize = "5M";
  1146. }
  1147. if ($postsize = ini_get('post_max_size')) {
  1148. $this->set(
  1149. 'max_upload_size',
  1150. min(Core::getRealSize($filesize), Core::getRealSize($postsize))
  1151. );
  1152. } else {
  1153. $this->set('max_upload_size', Core::getRealSize($filesize));
  1154. }
  1155. }
  1156. /**
  1157. * Checks if protocol is https
  1158. *
  1159. * This function checks if the https protocol on the active connection.
  1160. *
  1161. * @return bool
  1162. */
  1163. public function isHttps()
  1164. {
  1165. if (null !== $this->get('is_https')) {
  1166. return $this->get('is_https');
  1167. }
  1168. $url = $this->get('PmaAbsoluteUri');
  1169. $is_https = false;
  1170. if (! empty($url) && parse_url($url, PHP_URL_SCHEME) === 'https') {
  1171. $is_https = true;
  1172. } elseif (strtolower(Core::getenv('HTTP_SCHEME')) == 'https') {
  1173. $is_https = true;
  1174. } elseif (strtolower(Core::getenv('HTTPS')) == 'on') {
  1175. $is_https = true;
  1176. } elseif (substr(strtolower(Core::getenv('REQUEST_URI')), 0, 6) == 'https:') {
  1177. $is_https = true;
  1178. } elseif (strtolower(Core::getenv('HTTP_HTTPS_FROM_LB')) == 'on') {
  1179. // A10 Networks load balancer
  1180. $is_https = true;
  1181. } elseif (strtolower(Core::getenv('HTTP_FRONT_END_HTTPS')) == 'on') {
  1182. $is_https = true;
  1183. } elseif (strtolower(Core::getenv('HTTP_X_FORWARDED_PROTO')) == 'https') {
  1184. $is_https = true;
  1185. } elseif (Core::getenv('SERVER_PORT') == 443) {
  1186. $is_https = true;
  1187. }
  1188. $this->set('is_https', $is_https);
  1189. return $is_https;
  1190. }
  1191. /**
  1192. * Get phpMyAdmin root path
  1193. *
  1194. * @return string
  1195. */
  1196. public function getRootPath()
  1197. {
  1198. static $cookie_path = null;
  1199. if (null !== $cookie_path && !defined('TESTSUITE')) {
  1200. return $cookie_path;
  1201. }
  1202. $url = $this->get('PmaAbsoluteUri');
  1203. if (! empty($url)) {
  1204. $path = parse_url($url, PHP_URL_PATH);
  1205. if (! empty($path)) {
  1206. if (substr($path, -1) != '/') {
  1207. return $path . '/';
  1208. }
  1209. return $path;
  1210. }
  1211. }
  1212. $parsed_url = parse_url($GLOBALS['PMA_PHP_SELF']);
  1213. $parts = explode(
  1214. '/',
  1215. rtrim(str_replace('\\', '/', $parsed_url['path']), '/')
  1216. );
  1217. /* Remove filename */
  1218. if (substr($parts[count($parts) - 1], -4) == '.php') {
  1219. $parts = array_slice($parts, 0, count($parts) - 1);
  1220. }
  1221. /* Remove extra path from javascript calls */
  1222. if (defined('PMA_PATH_TO_BASEDIR')) {
  1223. $parts = array_slice($parts, 0, count($parts) - 1);
  1224. }
  1225. $parts[] = '';
  1226. return implode('/', $parts);
  1227. }
  1228. /**
  1229. * enables backward compatibility
  1230. *
  1231. * @return void
  1232. */
  1233. public function enableBc()
  1234. {
  1235. $GLOBALS['cfg'] = $this->settings;
  1236. $GLOBALS['default_server'] = $this->default_server;
  1237. unset($this->default_server);
  1238. $GLOBALS['is_upload'] = $this->get('enable_upload');
  1239. $GLOBALS['max_upload_size'] = $this->get('max_upload_size');
  1240. $GLOBALS['is_https'] = $this->get('is_https');
  1241. $defines = array(
  1242. 'PMA_VERSION',
  1243. 'PMA_MAJOR_VERSION',
  1244. 'PMA_THEME_VERSION',
  1245. 'PMA_THEME_GENERATION',
  1246. 'PMA_IS_WINDOWS',
  1247. 'PMA_IS_GD2',
  1248. 'PMA_USR_OS',
  1249. 'PMA_USR_BROWSER_VER',
  1250. 'PMA_USR_BROWSER_AGENT'
  1251. );
  1252. foreach ($defines as $define) {
  1253. if (! defined($define)) {
  1254. define($define, $this->get($define));
  1255. }
  1256. }
  1257. }
  1258. /**
  1259. * returns options for font size selection
  1260. *
  1261. * @param string $current_size current selected font size with unit
  1262. *
  1263. * @return array selectable font sizes
  1264. */
  1265. protected static function getFontsizeOptions($current_size = '82%')
  1266. {
  1267. $unit = preg_replace('/[0-9.]*/', '', $current_size);
  1268. $value = preg_replace('/[^0-9.]*/', '', $current_size);
  1269. $factors = array();
  1270. $options = array();
  1271. $options["$value"] = $value . $unit;
  1272. if ($unit === '%') {
  1273. $factors[] = 1;
  1274. $factors[] = 5;
  1275. $factors[] = 10;
  1276. $options['100'] = '100%';
  1277. } elseif ($unit === 'em') {
  1278. $factors[] = 0.05;
  1279. $factors[] = 0.2;
  1280. $factors[] = 1;
  1281. } elseif ($unit === 'pt') {
  1282. $factors[] = 0.5;
  1283. $factors[] = 2;
  1284. } elseif ($unit === 'px') {
  1285. $factors[] = 1;
  1286. $factors[] = 5;
  1287. $factors[] = 10;
  1288. } else {
  1289. //unknown font size unit
  1290. $factors[] = 0.05;
  1291. $factors[] = 0.2;
  1292. $factors[] = 1;
  1293. $factors[] = 5;
  1294. $factors[] = 10;
  1295. }
  1296. foreach ($factors as $key => $factor) {
  1297. $option_inc = $value + $factor;
  1298. $option_dec = $value - $factor;
  1299. while (count($options) < 21) {
  1300. $options["$option_inc"] = $option_inc . $unit;
  1301. if ($option_dec > $factors[0]) {
  1302. $options["$option_dec"] = $option_dec . $unit;
  1303. }
  1304. $option_inc += $factor;
  1305. $option_dec -= $factor;
  1306. if (isset($factors[$key + 1])
  1307. && $option_inc >= $value + $factors[$key + 1]
  1308. ) {
  1309. break;
  1310. }
  1311. }
  1312. }
  1313. ksort($options);
  1314. return $options;
  1315. }
  1316. /**
  1317. * returns html selectbox for font sizes
  1318. *
  1319. * @return string html selectbox
  1320. */
  1321. protected static function getFontsizeSelection()
  1322. {
  1323. $current_size = $GLOBALS['PMA_Config']->get('FontSize');
  1324. // for the case when there is no config file (this is supported)
  1325. if (empty($current_size)) {
  1326. $current_size = '82%';
  1327. }
  1328. $options = Config::getFontsizeOptions($current_size);
  1329. $return = '<label for="select_fontsize">' . __('Font size')
  1330. . ':</label>' . "\n"
  1331. . '<select name="set_fontsize" id="select_fontsize"'
  1332. . ' class="autosubmit">' . "\n";
  1333. foreach ($options as $option) {
  1334. $return .= '<option value="' . $option . '"';
  1335. if ($option == $current_size) {
  1336. $return .= ' selected="selected"';
  1337. }
  1338. $return .= '>' . $option . '</option>' . "\n";
  1339. }
  1340. $return .= '</select>';
  1341. return $return;
  1342. }
  1343. /**
  1344. * return complete font size selection form
  1345. *
  1346. * @return string html selectbox
  1347. */
  1348. public static function getFontsizeForm()
  1349. {
  1350. return '<form name="form_fontsize_selection" id="form_fontsize_selection"'
  1351. . ' method="post" action="index.php" class="disableAjax">' . "\n"
  1352. . Url::getHiddenInputs() . "\n"
  1353. . Config::getFontsizeSelection() . "\n"
  1354. . '</form>';
  1355. }
  1356. /**
  1357. * removes cookie
  1358. *
  1359. * @param string $cookie name of cookie to remove
  1360. *
  1361. * @return boolean result of setcookie()
  1362. */
  1363. public function removeCookie($cookie)
  1364. {
  1365. if (defined('TESTSUITE')) {
  1366. if (isset($_COOKIE[$cookie])) {
  1367. unset($_COOKIE[$cookie]);
  1368. }
  1369. return true;
  1370. }
  1371. return setcookie(
  1372. $cookie,
  1373. '',
  1374. time() - 3600,
  1375. $this->getRootPath(),
  1376. '',
  1377. $this->isHttps()
  1378. );
  1379. }
  1380. /**
  1381. * sets cookie if value is different from current cookie value,
  1382. * or removes if value is equal to default
  1383. *
  1384. * @param string $cookie name of cookie to remove
  1385. * @param mixed $value new cookie value
  1386. * @param string $default default value
  1387. * @param int $validity validity of cookie in seconds (default is one month)
  1388. * @param bool $httponly whether cookie is only for HTTP (and not for scripts)
  1389. *
  1390. * @return boolean result of setcookie()
  1391. */
  1392. public function setCookie($cookie, $value, $default = null,
  1393. $validity = null, $httponly = true
  1394. ) {
  1395. if (strlen($value) > 0 && null !== $default && $value === $default
  1396. ) {
  1397. // default value is used
  1398. if (isset($_COOKIE[$cookie])) {
  1399. // remove cookie
  1400. return $this->removeCookie($cookie);
  1401. }
  1402. return false;
  1403. }
  1404. if (strlen($value) === 0 && isset($_COOKIE[$cookie])) {
  1405. // remove cookie, value is empty
  1406. return $this->removeCookie($cookie);
  1407. }
  1408. if (! isset($_COOKIE[$cookie]) || $_COOKIE[$cookie] !== $value) {
  1409. // set cookie with new value
  1410. /* Calculate cookie validity */
  1411. if ($validity === null) {
  1412. /* Valid for one month */
  1413. $validity = time() + 2592000;
  1414. } elseif ($validity == 0) {
  1415. /* Valid for session */
  1416. $validity = 0;
  1417. } else {
  1418. $validity = time() + $validity;
  1419. }
  1420. if (defined('TESTSUITE')) {
  1421. $_COOKIE[$cookie] = $value;
  1422. return true;
  1423. }
  1424. return setcookie(
  1425. $cookie,
  1426. $value,
  1427. $validity,
  1428. $this->getRootPath(),
  1429. '',
  1430. $this->isHttps(),
  1431. $httponly
  1432. );
  1433. }
  1434. // cookie has already $value as value
  1435. return true;
  1436. }
  1437. /**
  1438. * Error handler to catch fatal errors when loading configuration
  1439. * file
  1440. *
  1441. *
  1442. * PMA_Config_fatalErrorHandler
  1443. * @return void
  1444. */
  1445. public static function fatalErrorHandler()
  1446. {
  1447. if (!isset($GLOBALS['pma_config_loading'])
  1448. || !$GLOBALS['pma_config_loading']
  1449. ) {
  1450. return;
  1451. }
  1452. $error = error_get_last();
  1453. if ($error === null) {
  1454. return;
  1455. }
  1456. Core::fatalError(
  1457. sprintf(
  1458. 'Failed to load phpMyAdmin configuration (%s:%s): %s',
  1459. Error::relPath($error['file']),
  1460. $error['line'],
  1461. $error['message']
  1462. )
  1463. );
  1464. }
  1465. /**
  1466. * Wrapper for footer/header rendering
  1467. *
  1468. * @param string $filename File to check and render
  1469. * @param string $id Div ID
  1470. *
  1471. * @return string
  1472. */
  1473. private static function _renderCustom($filename, $id)
  1474. {
  1475. $retval = '';
  1476. if (@file_exists($filename)) {
  1477. $retval .= '<div id="' . $id . '">';
  1478. ob_start();
  1479. include $filename;
  1480. $retval .= ob_get_contents();
  1481. ob_end_clean();
  1482. $retval .= '</div>';
  1483. }
  1484. return $retval;
  1485. }
  1486. /**
  1487. * Renders user configured footer
  1488. *
  1489. * @return string
  1490. */
  1491. public static function renderFooter()
  1492. {
  1493. return self::_renderCustom(CUSTOM_FOOTER_FILE, 'pma_footer');
  1494. }
  1495. /**
  1496. * Renders user configured footer
  1497. *
  1498. * @return string
  1499. */
  1500. public static function renderHeader()
  1501. {
  1502. return self::_renderCustom(CUSTOM_HEADER_FILE, 'pma_header');
  1503. }
  1504. /**
  1505. * Returns temporary dir path
  1506. *
  1507. * @param string $name Directory name
  1508. *
  1509. * @return string|null
  1510. */
  1511. public function getTempDir($name)
  1512. {
  1513. static $temp_dir = array();
  1514. if (isset($temp_dir[$name]) && !defined('TESTSUITE')) {
  1515. return $temp_dir[$name];
  1516. }
  1517. $path = $this->get('TempDir');
  1518. if (empty($path)) {
  1519. $path = null;
  1520. } else {
  1521. $path .= '/' . $name;
  1522. if (! @is_dir($path)) {
  1523. @mkdir($path, 0770, true);
  1524. }
  1525. if (! @is_dir($path) || ! @is_writable($path)) {
  1526. $path = null;
  1527. }
  1528. }
  1529. $temp_dir[$name] = $path;
  1530. return $path;
  1531. }
  1532. /**
  1533. * Returns temporary directory
  1534. *
  1535. * @return string
  1536. */
  1537. public function getUploadTempDir()
  1538. {
  1539. // First try configured temp dir
  1540. // Fallback to PHP upload_tmp_dir
  1541. $dirs = array(
  1542. $this->getTempDir('upload'),
  1543. ini_get('upload_tmp_dir'),
  1544. sys_get_temp_dir(),
  1545. );
  1546. foreach ($dirs as $dir) {
  1547. if (! empty($dir) && @is_writable($dir)) {
  1548. return realpath($dir);
  1549. }
  1550. }
  1551. return null;
  1552. }
  1553. /**
  1554. * Selects server based on request parameters.
  1555. *
  1556. * @return integer
  1557. */
  1558. public function selectServer() {
  1559. $server = 0;
  1560. $request = empty($_REQUEST['server']) ? 0 : $_REQUEST['server'];
  1561. /**
  1562. * Lookup server by name
  1563. * (see FAQ 4.8)
  1564. */
  1565. if (! is_numeric($request)) {
  1566. foreach ($this->settings['Servers'] as $i => $server) {
  1567. $verboseToLower = mb_strtolower($server['verbose']);
  1568. $serverToLower = mb_strtolower($request);
  1569. if ($server['host'] == $request
  1570. || $server['verbose'] == $request
  1571. || $verboseToLower == $serverToLower
  1572. || md5($verboseToLower) === $serverToLower
  1573. ) {
  1574. $request = $i;
  1575. break;
  1576. }
  1577. }
  1578. if (is_string($request)) {
  1579. $request = 0;
  1580. }
  1581. }
  1582. /**
  1583. * If no server is selected, make sure that $this->settings['Server'] is empty (so
  1584. * that nothing will work), and skip server authentication.
  1585. * We do NOT exit here, but continue on without logging into any server.
  1586. * This way, the welcome page will still come up (with no server info) and
  1587. * present a choice of servers in the case that there are multiple servers
  1588. * and '$this->settings['ServerDefault'] = 0' is set.
  1589. */
  1590. if (is_numeric($request) && ! empty($request) && ! empty($this->settings['Servers'][$request])) {
  1591. $server = $request;
  1592. $this->settings['Server'] = $this->settings['Servers'][$server];
  1593. } else {
  1594. if (!empty($this->settings['Servers'][$this->settings['ServerDefault']])) {
  1595. $server = $this->settings['ServerDefault'];
  1596. $this->settings['Server'] = $this->settings['Servers'][$server];
  1597. } else {
  1598. $server = 0;
  1599. $this->settings['Server'] = array();
  1600. }
  1601. }
  1602. return $server;
  1603. }
  1604. /**
  1605. * Checks whether Servers configuration is valid and possibly apply fixups.
  1606. *
  1607. * @return void
  1608. */
  1609. public function checkServers() {
  1610. // Do we have some server?
  1611. if (! isset($this->settings['Servers']) || count($this->settings['Servers']) == 0) {
  1612. // No server => create one with defaults
  1613. $this->settings['Servers'] = array(1 => $this->default_server);
  1614. } else {
  1615. // We have server(s) => apply default configuration
  1616. $new_servers = array();
  1617. foreach ($this->settings['Servers'] as $server_index => $each_server) {
  1618. // Detect wrong configuration
  1619. if (!is_int($server_index) || $server_index < 1) {
  1620. trigger_error(
  1621. sprintf(__('Invalid server index: %s'), $server_index),
  1622. E_USER_ERROR
  1623. );
  1624. }
  1625. $each_server = array_merge($this->default_server, $each_server);
  1626. // Final solution to bug #582890
  1627. // If we are using a socket connection
  1628. // and there is nothing in the verbose server name
  1629. // or the host field, then generate a name for the server
  1630. // in the form of "Server 2", localized of course!
  1631. if (empty($each_server['host']) && empty($each_server['verbose'])) {
  1632. $each_server['verbose'] = sprintf(__('Server %d'), $server_index);
  1633. }
  1634. $new_servers[$server_index] = $each_server;
  1635. }
  1636. $this->settings['Servers'] = $new_servers;
  1637. }
  1638. }
  1639. }
  1640. if (!defined('TESTSUITE')) {
  1641. register_shutdown_function(array('PhpMyAdmin\Config', 'fatalErrorHandler'));
  1642. }