PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyadmin/libraries/Theme.php

https://gitlab.com/luyxtran264/myproject
PHP | 482 lines | 232 code | 48 blank | 202 comment | 26 complexity | 1200dcbe12c6fba913b0037f47a0b992 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * hold Theme class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PMA\libraries;
  9. /**
  10. * handles theme
  11. *
  12. * @todo add the possibility to make a theme depend on another theme
  13. * and by default on original
  14. * @todo make all components optional - get missing components from 'parent' theme
  15. *
  16. * @package PhpMyAdmin
  17. */
  18. class Theme
  19. {
  20. /**
  21. * @var string theme version
  22. * @access protected
  23. */
  24. var $version = '0.0.0.0';
  25. /**
  26. * @var string theme name
  27. * @access protected
  28. */
  29. var $name = '';
  30. /**
  31. * @var string theme id
  32. * @access protected
  33. */
  34. var $id = '';
  35. /**
  36. * @var string theme path
  37. * @access protected
  38. */
  39. var $path = '';
  40. /**
  41. * @var string image path
  42. * @access protected
  43. */
  44. var $img_path = '';
  45. /**
  46. * @var integer last modification time for info file
  47. * @access protected
  48. */
  49. var $mtime_info = 0;
  50. /**
  51. * needed because sometimes, the mtime for different themes
  52. * is identical
  53. * @var integer filesize for info file
  54. * @access protected
  55. */
  56. var $filesize_info = 0;
  57. /**
  58. * @var array List of css files to load
  59. * @access private
  60. */
  61. private $_cssFiles = array(
  62. 'common',
  63. 'enum_editor',
  64. 'gis',
  65. 'navigation',
  66. 'pmd',
  67. 'rte',
  68. 'codemirror',
  69. 'jqplot',
  70. 'resizable-menu'
  71. );
  72. /**
  73. * Loads theme information
  74. *
  75. * @return boolean whether loading them info was successful or not
  76. * @access public
  77. */
  78. function loadInfo()
  79. {
  80. if (! file_exists($this->getPath() . '/info.inc.php')) {
  81. return false;
  82. }
  83. if ($this->mtime_info === filemtime($this->getPath() . '/info.inc.php')) {
  84. return true;
  85. }
  86. @include $this->getPath() . '/info.inc.php';
  87. // was it set correctly?
  88. if (! isset($theme_name)) {
  89. return false;
  90. }
  91. $this->mtime_info = filemtime($this->getPath() . '/info.inc.php');
  92. $this->filesize_info = filesize($this->getPath() . '/info.inc.php');
  93. if (isset($theme_full_version)) {
  94. $this->setVersion($theme_full_version);
  95. } elseif (isset($theme_generation, $theme_version)) {
  96. $this->setVersion($theme_generation . '.' . $theme_version);
  97. }
  98. $this->setName($theme_name);
  99. return true;
  100. }
  101. /**
  102. * returns theme object loaded from given folder
  103. * or false if theme is invalid
  104. *
  105. * @param string $folder path to theme
  106. *
  107. * @return Theme|false
  108. * @static
  109. * @access public
  110. */
  111. static public function load($folder)
  112. {
  113. $theme = new Theme();
  114. $theme->setPath($folder);
  115. if (! $theme->loadInfo()) {
  116. return false;
  117. }
  118. $theme->checkImgPath();
  119. return $theme;
  120. }
  121. /**
  122. * checks image path for existence - if not found use img from fallback theme
  123. *
  124. * @access public
  125. * @return bool
  126. */
  127. public function checkImgPath()
  128. {
  129. // try current theme first
  130. if (is_dir($this->getPath() . '/img/')) {
  131. $this->setImgPath($this->getPath() . '/img/');
  132. return true;
  133. }
  134. // try fallback theme
  135. $fallback = $GLOBALS['cfg']['ThemePath'] . '/'
  136. . ThemeManager::FALLBACK_THEME
  137. . '/img/';
  138. if (is_dir($fallback)) {
  139. $this->setImgPath($fallback);
  140. return true;
  141. }
  142. // we failed
  143. trigger_error(
  144. sprintf(
  145. __('No valid image path for theme %s found!'),
  146. $this->getName()
  147. ),
  148. E_USER_ERROR
  149. );
  150. return false;
  151. }
  152. /**
  153. * returns path to theme
  154. *
  155. * @access public
  156. * @return string path to theme
  157. */
  158. public function getPath()
  159. {
  160. return $this->path;
  161. }
  162. /**
  163. * returns layout file
  164. *
  165. * @access public
  166. * @return string layout file
  167. */
  168. public function getLayoutFile()
  169. {
  170. return $this->getPath() . '/layout.inc.php';
  171. }
  172. /**
  173. * set path to theme
  174. *
  175. * @param string $path path to theme
  176. *
  177. * @return void
  178. * @access public
  179. */
  180. public function setPath($path)
  181. {
  182. $this->path = trim($path);
  183. }
  184. /**
  185. * sets version
  186. *
  187. * @param string $version version to set
  188. *
  189. * @return void
  190. * @access public
  191. */
  192. public function setVersion($version)
  193. {
  194. $this->version = trim($version);
  195. }
  196. /**
  197. * returns version
  198. *
  199. * @return string version
  200. * @access public
  201. */
  202. public function getVersion()
  203. {
  204. return $this->version;
  205. }
  206. /**
  207. * checks theme version against $version
  208. * returns true if theme version is equal or higher to $version
  209. *
  210. * @param string $version version to compare to
  211. *
  212. * @return boolean true if theme version is equal or higher to $version
  213. * @access public
  214. */
  215. public function checkVersion($version)
  216. {
  217. return version_compare($this->getVersion(), $version, 'lt');
  218. }
  219. /**
  220. * sets name
  221. *
  222. * @param string $name name to set
  223. *
  224. * @return void
  225. * @access public
  226. */
  227. public function setName($name)
  228. {
  229. $this->name = trim($name);
  230. }
  231. /**
  232. * returns name
  233. *
  234. * @access public
  235. * @return string name
  236. */
  237. public function getName()
  238. {
  239. return $this->name;
  240. }
  241. /**
  242. * sets id
  243. *
  244. * @param string $id new id
  245. *
  246. * @return void
  247. * @access public
  248. */
  249. public function setId($id)
  250. {
  251. $this->id = trim($id);
  252. }
  253. /**
  254. * returns id
  255. *
  256. * @return string id
  257. * @access public
  258. */
  259. public function getId()
  260. {
  261. return $this->id;
  262. }
  263. /**
  264. * Sets path to images for the theme
  265. *
  266. * @param string $path path to images for this theme
  267. *
  268. * @return void
  269. * @access public
  270. */
  271. public function setImgPath($path)
  272. {
  273. $this->img_path = $path;
  274. }
  275. /**
  276. * Returns the path to image for the theme.
  277. * If filename is given, it possibly fallbacks to fallback
  278. * theme for it if image does not exist.
  279. *
  280. * @param string $file file name for image
  281. *
  282. * @access public
  283. * @return string image path for this theme
  284. */
  285. public function getImgPath($file = null)
  286. {
  287. if (is_null($file)) {
  288. return $this->img_path;
  289. }
  290. if (is_readable($this->img_path . $file)) {
  291. return $this->img_path . $file;
  292. }
  293. return $GLOBALS['cfg']['ThemePath'] . '/'
  294. . ThemeManager::FALLBACK_THEME . '/img/' . $file;
  295. }
  296. /**
  297. * load css (send to stdout, normally the browser)
  298. *
  299. * @return bool
  300. * @access public
  301. */
  302. public function loadCss()
  303. {
  304. $success = true;
  305. if ($GLOBALS['text_dir'] === 'ltr') {
  306. $right = 'right';
  307. $left = 'left';
  308. } else {
  309. $right = 'left';
  310. $left = 'right';
  311. }
  312. foreach ($this->_cssFiles as $file) {
  313. $path = $this->getPath() . "/css/$file.css.php";
  314. $fallback = "./themes/"
  315. . ThemeManager::FALLBACK_THEME . "/css/$file.css.php";
  316. if (is_readable($path)) {
  317. echo "\n/* FILE: " , $file , ".css.php */\n";
  318. include $path;
  319. } else if (is_readable($fallback)) {
  320. echo "\n/* FILE: " , $file , ".css.php */\n";
  321. include $fallback;
  322. } else {
  323. $success = false;
  324. }
  325. }
  326. include './themes/sprites.css.php';
  327. return $success;
  328. }
  329. /**
  330. * Renders the preview for this theme
  331. *
  332. * @return string
  333. * @access public
  334. */
  335. public function getPrintPreview()
  336. {
  337. $url_params = array('set_theme' => $this->getId());
  338. $url = 'index.php' . PMA_URL_getCommon($url_params);
  339. $retval = '<div class="theme_preview">';
  340. $retval .= '<h2>';
  341. $retval .= htmlspecialchars($this->getName());
  342. $retval .= ' (' . htmlspecialchars($this->getVersion()) . ') ';
  343. $retval .= '</h2>';
  344. $retval .= '<p>';
  345. $retval .= '<a class="take_theme" ';
  346. $retval .= 'name="' . htmlspecialchars($this->getId()) . '" ';
  347. $retval .= 'href="' . $url . '">';
  348. if (@file_exists($this->getPath() . '/screen.png')) {
  349. // if screen exists then output
  350. $retval .= '<img src="' . $this->getPath() . '/screen.png" border="1"';
  351. $retval .= ' alt="' . htmlspecialchars($this->getName()) . '"';
  352. $retval .= ' title="' . htmlspecialchars($this->getName()) . '" />';
  353. $retval .= '<br />';
  354. } else {
  355. $retval .= __('No preview available.');
  356. }
  357. $retval .= '[ <strong>' . __('take it') . '</strong> ]';
  358. $retval .= '</a>';
  359. $retval .= '</p>';
  360. $retval .= '</div>';
  361. return $retval;
  362. }
  363. /**
  364. * Remove filter for IE.
  365. *
  366. * @return string CSS code.
  367. */
  368. function getCssIEClearFilter()
  369. {
  370. return PMA_USR_BROWSER_AGENT == 'IE'
  371. && PMA_USR_BROWSER_VER >= 6
  372. && PMA_USR_BROWSER_VER <= 8
  373. ? 'filter: none'
  374. : '';
  375. }
  376. /**
  377. * Gets currently configured font size.
  378. *
  379. * @return String with font size.
  380. */
  381. function getFontSize()
  382. {
  383. $fs = $GLOBALS['PMA_Config']->get('fontsize');
  384. if (!is_null($fs)) {
  385. return $fs;
  386. }
  387. if (isset($_COOKIE['pma_fontsize'])) {
  388. return htmlspecialchars($_COOKIE['pma_fontsize']);
  389. }
  390. return '82%';
  391. }
  392. /**
  393. * Generates code for CSS gradient using various browser extensions.
  394. *
  395. * @param string $start_color Color of gradient start, hex value without #
  396. * @param string $end_color Color of gradient end, hex value without #
  397. *
  398. * @return string CSS code.
  399. */
  400. function getCssGradient($start_color, $end_color)
  401. {
  402. $result = array();
  403. // Opera 9.5+, IE 9
  404. $result[] = 'background-image: url(./themes/svg_gradient.php?from='
  405. . $start_color . '&to=' . $end_color . ');';
  406. $result[] = 'background-size: 100% 100%;';
  407. // Safari 4-5, Chrome 1-9
  408. $result[] = 'background: '
  409. . '-webkit-gradient(linear, left top, left bottom, from(#'
  410. . $start_color . '), to(#' . $end_color . '));';
  411. // Safari 5.1, Chrome 10+
  412. $result[] = 'background: -webkit-linear-gradient(top, #'
  413. . $start_color . ', #' . $end_color . ');';
  414. // Firefox 3.6+
  415. $result[] = 'background: -moz-linear-gradient(top, #'
  416. . $start_color . ', #' . $end_color . ');';
  417. // IE 10
  418. $result[] = 'background: -ms-linear-gradient(top, #'
  419. . $start_color . ', #' . $end_color . ');';
  420. // Opera 11.10
  421. $result[] = 'background: -o-linear-gradient(top, #'
  422. . $start_color . ', #' . $end_color . ');';
  423. // IE 6-8
  424. if (PMA_USR_BROWSER_AGENT == 'IE'
  425. && PMA_USR_BROWSER_VER >= 6
  426. && PMA_USR_BROWSER_VER <= 8
  427. ) {
  428. $result[] = 'filter: '
  429. . 'progid:DXImageTransform.Microsoft.gradient(startColorstr="#'
  430. . $start_color . '", endColorstr="#' . $end_color . '");';
  431. }
  432. return implode("\n", $result);
  433. }
  434. }