PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyadmin/libraries/Theme.class.php

https://github.com/drbowen/openemr
PHP | 601 lines | 313 code | 57 blank | 231 comment | 33 complexity | 728a878dafd68eb1747fc97a4c0ed112 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 object PMA_Theme
  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 existance - 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 agaisnt $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. } else {
  292. if (is_readable($this->img_path . $file)) {
  293. return $this->img_path . $file;
  294. } else {
  295. return $GLOBALS['cfg']['ThemePath'] . '/'
  296. . PMA_Theme_Manager::FALLBACK_THEME . '/img/' . $file;
  297. }
  298. }
  299. }
  300. /**
  301. * Builds a CSS rule used for html formatted SQL queries
  302. *
  303. * @param string $classname The class name
  304. * @param string $property The property name
  305. * @param string $value The property value
  306. *
  307. * @return string The CSS rule
  308. *
  309. * @access public
  310. *
  311. * @see PMA_SQP_buildCssData()
  312. */
  313. public function buildSQPCssRule($classname, $property, $value)
  314. {
  315. $str = '.' . $classname . ' {';
  316. if ($value != '') {
  317. $str .= $property . ': ' . $value . ';';
  318. }
  319. $str .= '}' . "\n";
  320. return $str;
  321. } // end of the "PMA_SQP_buildCssRule()" function
  322. /**
  323. * Builds CSS rules used for html formatted SQL queries
  324. *
  325. * @return string The CSS rules set
  326. *
  327. * @access public
  328. *
  329. * @global array The current PMA configuration
  330. *
  331. * @see PMA_SQP_buildCssRule()
  332. */
  333. public function buildSQPCssData()
  334. {
  335. global $cfg;
  336. $css_string = '';
  337. foreach ($cfg['SQP']['fmtColor'] AS $key => $col) {
  338. $css_string .= $this->buildSQPCssRule('syntax_' . $key, 'color', $col);
  339. }
  340. for ($i = 0; $i < 8; $i++) {
  341. $css_string .= $this->buildSQPCssRule(
  342. 'syntax_indent' . $i, 'margin-left',
  343. ($i * $cfg['SQP']['fmtInd']) . $cfg['SQP']['fmtIndUnit']
  344. );
  345. }
  346. return $css_string;
  347. } // end of the "PMA_SQP_buildCssData()" function
  348. /**
  349. * load css (send to stdout, normally the browser)
  350. *
  351. * @return bool
  352. * @access public
  353. */
  354. public function loadCss()
  355. {
  356. $success = true;
  357. echo $this->buildSQPCssData();
  358. if ($GLOBALS['text_dir'] === 'ltr') {
  359. $right = 'right';
  360. $left = 'left';
  361. } else {
  362. $right = 'left';
  363. $left = 'right';
  364. }
  365. foreach ($this->_cssFiles as $file) {
  366. $path = $this->getPath() . "/css/$file.css.php";
  367. $fallback = "./themes/"
  368. . PMA_Theme_Manager::FALLBACK_THEME . "/css/$file.css.php";
  369. if (is_readable($path)) {
  370. echo "\n/* FILE: $file.css.php */\n";
  371. include $path;
  372. } else if (is_readable($fallback)) {
  373. echo "\n/* FILE: $file.css.php */\n";
  374. include $fallback;
  375. } else {
  376. $success = false;
  377. }
  378. }
  379. include './themes/sprites.css.php';
  380. return $success;
  381. }
  382. /**
  383. * Renders the preview for this theme
  384. *
  385. * @return string
  386. * @access public
  387. */
  388. public function getPrintPreview()
  389. {
  390. $url_params = array('set_theme' => $this->getId());
  391. $url = 'index.php'. PMA_generate_common_url($url_params);
  392. $retval = '<div class="theme_preview">';
  393. $retval .= '<h2>';
  394. $retval .= htmlspecialchars($this->getName());
  395. $retval .= ' (' . htmlspecialchars($this->getVersion()) . ') ';
  396. $retval .= '</h2>';
  397. $retval .= '<p>';
  398. $retval .= '<a class="take_theme" ';
  399. $retval .= 'name="' . htmlspecialchars($this->getId()) . '" ';
  400. $retval .= 'href="' . $url . '">';
  401. if (@file_exists($this->getPath() . '/screen.png')) {
  402. // if screen exists then output
  403. $retval .= '<img src="' . $this->getPath() . '/screen.png" border="1"';
  404. $retval .= ' alt="' . htmlspecialchars($this->getName()) . '"';
  405. $retval .= ' title="' . htmlspecialchars($this->getName()) . '" />';
  406. $retval .= '<br />';
  407. } else {
  408. $retval .= __('No preview available.');
  409. }
  410. $retval .= '[ <strong>' . __('take it') . '</strong> ]';
  411. $retval .= '</a>';
  412. $retval .= '</p>';
  413. $retval .= '</div>';
  414. return $retval;
  415. }
  416. /**
  417. * Remove filter for IE.
  418. *
  419. * @return string CSS code.
  420. */
  421. function getCssIEClearFilter()
  422. {
  423. return PMA_USR_BROWSER_AGENT == 'IE'
  424. && PMA_USR_BROWSER_VER >= 6
  425. && PMA_USR_BROWSER_VER <= 8
  426. ? 'filter: none'
  427. : '';
  428. }
  429. /**
  430. * Gets currently configured font size.
  431. *
  432. * @return String with font size.
  433. */
  434. function getFontSize()
  435. {
  436. $fs = $GLOBALS['PMA_Config']->get('fontsize');
  437. if (!is_null($fs)) {
  438. return $fs;
  439. }
  440. if (isset($_COOKIE['pma_fontsize'])) {
  441. return $_COOKIE['pma_fontsize'];
  442. }
  443. return '82%';
  444. }
  445. /**
  446. * Generates code for CSS gradient using various browser extensions.
  447. *
  448. * @param string $start_color Color of gradient start, hex value without #
  449. * @param string $end_color Color of gradient end, hex value without #
  450. *
  451. * @return string CSS code.
  452. */
  453. function getCssGradient($start_color, $end_color)
  454. {
  455. $result = array();
  456. // Opera 9.5+, IE 9
  457. $result[] = 'background-image: url(./themes/svg_gradient.php?from='
  458. . $start_color . '&to=' . $end_color . ');';
  459. $result[] = 'background-size: 100% 100%;';
  460. // Safari 4-5, Chrome 1-9
  461. $result[] = 'background: '
  462. . '-webkit-gradient(linear, left top, left bottom, from(#'
  463. . $start_color . '), to(#' . $end_color . '));';
  464. // Safari 5.1, Chrome 10+
  465. $result[] = 'background: -webkit-linear-gradient(top, #'
  466. . $start_color . ', #' . $end_color . ');';
  467. // Firefox 3.6+
  468. $result[] = 'background: -moz-linear-gradient(top, #'
  469. . $start_color . ', #' . $end_color . ');';
  470. // IE 10
  471. $result[] = 'background: -ms-linear-gradient(top, #'
  472. . $start_color . ', #' . $end_color . ');';
  473. // Opera 11.10
  474. $result[] = 'background: -o-linear-gradient(top, #'
  475. . $start_color . ', #' . $end_color . ');';
  476. // IE 6-8
  477. if (PMA_USR_BROWSER_AGENT == 'IE'
  478. && PMA_USR_BROWSER_VER >= 6
  479. && PMA_USR_BROWSER_VER <= 8
  480. ) {
  481. $result[] = 'filter: '
  482. . 'progid:DXImageTransform.Microsoft.gradient(startColorstr="#'
  483. . $start_color . '", endColorstr="#' . $end_color . '");';
  484. }
  485. return implode("\n", $result);
  486. }
  487. /**
  488. * Returns CSS styles for CodeMirror editor based on query formatter colors.
  489. *
  490. * @return string CSS code.
  491. */
  492. function getCssCodeMirror()
  493. {
  494. if (! $GLOBALS['cfg']['CodemirrorEnable']) {
  495. return '';
  496. }
  497. $result[] = 'span.cm-keyword, span.cm-statement-verb {';
  498. $result[] = ' color: '
  499. . $GLOBALS['cfg']['SQP']['fmtColor']['alpha_reservedWord'] . ';';
  500. $result[] = '}';
  501. $result[] = 'span.cm-variable {';
  502. $result[] = ' color: '
  503. . $GLOBALS['cfg']['SQP']['fmtColor']['alpha_identifier'] . ';';
  504. $result[] = '}';
  505. $result[] = 'span.cm-comment {';
  506. $result[] = ' color: '
  507. . $GLOBALS['cfg']['SQP']['fmtColor']['comment'] . ';';
  508. $result[] = '}';
  509. $result[] = 'span.cm-mysql-string {';
  510. $result[] = ' color: '
  511. . $GLOBALS['cfg']['SQP']['fmtColor']['quote'] . ';';
  512. $result[] = '}';
  513. $result[] = 'span.cm-operator {';
  514. $result[] = ' color: '
  515. . $GLOBALS['cfg']['SQP']['fmtColor']['punct'] . ';';
  516. $result[] = '}';
  517. $result[] = 'span.cm-mysql-word {';
  518. $result[] = ' color: '
  519. . $GLOBALS['cfg']['SQP']['fmtColor']['alpha_identifier'] . ';';
  520. $result[] = '}';
  521. $result[] = 'span.cm-builtin {';
  522. $result[] = ' color: '
  523. . $GLOBALS['cfg']['SQP']['fmtColor']['alpha_functionName'] . ';';
  524. $result[] = '}';
  525. $result[] = 'span.cm-variable-2 {';
  526. $result[] = ' color: '
  527. . $GLOBALS['cfg']['SQP']['fmtColor']['alpha_columnType'] . ';';
  528. $result[] = '}';
  529. $result[] = 'span.cm-variable-3 {';
  530. $result[] = ' color: '
  531. . $GLOBALS['cfg']['SQP']['fmtColor']['alpha_columnAttrib'] . ';';
  532. $result[] = '}';
  533. $result[] = 'span.cm-separator {';
  534. $result[] = ' color: '
  535. . $GLOBALS['cfg']['SQP']['fmtColor']['punct'] . ';';
  536. $result[] = '}';
  537. $result[] = 'span.cm-number {';
  538. $result[] = ' color: '
  539. . $GLOBALS['cfg']['SQP']['fmtColor']['digit_integer'] . ';';
  540. $result[] = '}';
  541. return implode("\n", $result);
  542. }
  543. }
  544. ?>