PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/public/phpmyadmin/libraries/Theme.class.php

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