PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/Theme.php

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