PageRenderTime 120ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/pma/libraries/Theme.class.php

https://bitbucket.org/pavolve/masterskayaludmila
PHP | 445 lines | 220 code | 47 blank | 178 comment | 29 complexity | f47c1bc34a19c2cffaece425a4e2682c 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. /**
  9. * handles theme
  10. *
  11. * @todo add the possibility to make a theme depend on another theme and by default on original
  12. * @todo make all components optional - get missing components from 'parent' theme
  13. * @todo make css optionally replacing 'parent' css or extending it (by appending at the end)
  14. * @todo add an optional global css file - which will be used for both frames
  15. *
  16. * @package PhpMyAdmin
  17. */
  18. class PMA_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 array valid css types
  47. * @access protected
  48. */
  49. var $types = array('left', 'right', 'print');
  50. /**
  51. * @var integer last modification time for info file
  52. * @access protected
  53. */
  54. var $mtime_info = 0;
  55. /**
  56. * needed because sometimes, the mtime for different themes
  57. * is identical
  58. * @var integer filesize for info file
  59. * @access protected
  60. */
  61. var $filesize_info = 0;
  62. /**
  63. * @access public
  64. * @return boolean whether loading them info was successful or not
  65. */
  66. function loadInfo()
  67. {
  68. if (! file_exists($this->getPath() . '/info.inc.php')) {
  69. return false;
  70. }
  71. if ($this->mtime_info === filemtime($this->getPath() . '/info.inc.php')) {
  72. return true;
  73. }
  74. @include $this->getPath() . '/info.inc.php';
  75. // was it set correctly?
  76. if (! isset($theme_name)) {
  77. return false;
  78. }
  79. $this->mtime_info = filemtime($this->getPath() . '/info.inc.php');
  80. $this->filesize_info = filesize($this->getPath() . '/info.inc.php');
  81. if (isset($theme_full_version)) {
  82. $this->setVersion($theme_full_version);
  83. } elseif (isset($theme_generation, $theme_version)) {
  84. $this->setVersion($theme_generation . '.' . $theme_version);
  85. }
  86. $this->setName($theme_name);
  87. return true;
  88. }
  89. /**
  90. * returns theme object loaded from given folder
  91. * or false if theme is invalid
  92. *
  93. * @static
  94. * @access public
  95. * @param string $folder path to theme
  96. * @return object PMA_Theme
  97. */
  98. static public function load($folder)
  99. {
  100. $theme = new PMA_Theme();
  101. $theme->setPath($folder);
  102. if (! $theme->loadInfo()) {
  103. return false;
  104. }
  105. $theme->checkImgPath();
  106. return $theme;
  107. }
  108. /**
  109. * checks image path for existance - if not found use img from original theme
  110. *
  111. * @access public
  112. * @return bool
  113. */
  114. function checkImgPath()
  115. {
  116. if (is_dir($this->getPath() . '/img/')) {
  117. $this->setImgPath($this->getPath() . '/img/');
  118. return true;
  119. } elseif (is_dir($GLOBALS['cfg']['ThemePath'] . '/original/img/')) {
  120. $this->setImgPath($GLOBALS['cfg']['ThemePath'] . '/original/img/');
  121. return true;
  122. } else {
  123. trigger_error(
  124. sprintf(__('No valid image path for theme %s found!'), $this->getName()),
  125. E_USER_ERROR);
  126. return false;
  127. }
  128. }
  129. /**
  130. * returns path to theme
  131. *
  132. * @access public
  133. * @return string $path path to theme
  134. */
  135. function getPath()
  136. {
  137. return $this->path;
  138. }
  139. /**
  140. * returns layout file
  141. *
  142. * @access public
  143. * @return string layout file
  144. */
  145. function getLayoutFile()
  146. {
  147. return $this->getPath() . '/layout.inc.php';
  148. }
  149. /**
  150. * set path to theme
  151. *
  152. * @access public
  153. * @param string $path path to theme
  154. */
  155. function setPath($path)
  156. {
  157. $this->path = trim($path);
  158. }
  159. /**
  160. * sets version
  161. *
  162. * @access public
  163. * @param string new version
  164. */
  165. function setVersion($version)
  166. {
  167. $this->version = trim($version);
  168. }
  169. /**
  170. * returns version
  171. *
  172. * @access public
  173. * @return string version
  174. */
  175. function getVersion()
  176. {
  177. return $this->version;
  178. }
  179. /**
  180. * checks theme version agaisnt $version
  181. * returns true if theme version is equal or higher to $version
  182. *
  183. * @access public
  184. * @param string $version version to compare to
  185. * @return boolean
  186. */
  187. function checkVersion($version)
  188. {
  189. return version_compare($this->getVersion(), $version, 'lt');
  190. }
  191. /**
  192. * sets name
  193. *
  194. * @access public
  195. * @param string $name new name
  196. */
  197. function setName($name)
  198. {
  199. $this->name = trim($name);
  200. }
  201. /**
  202. * returns name
  203. *
  204. * @access public
  205. * @return string name
  206. */
  207. function getName()
  208. {
  209. return $this->name;
  210. }
  211. /**
  212. * sets id
  213. *
  214. * @access public
  215. * @param string $id new id
  216. */
  217. function setId($id)
  218. {
  219. $this->id = trim($id);
  220. }
  221. /**
  222. * returns id
  223. *
  224. * @access public
  225. * @return string id
  226. */
  227. function getId()
  228. {
  229. return $this->id;
  230. }
  231. /**
  232. * @access public
  233. * @param string path to images for this theme
  234. */
  235. function setImgPath($path)
  236. {
  237. $this->img_path = $path;
  238. }
  239. /**
  240. * @access public
  241. * @return string image path for this theme
  242. */
  243. function getImgPath()
  244. {
  245. return $this->img_path;
  246. }
  247. /**
  248. * load css (send to stdout, normally the browser)
  249. *
  250. * @access public
  251. * @param string $type left, right or print
  252. * @return bool
  253. */
  254. function loadCss(&$type)
  255. {
  256. if (empty($type) || ! in_array($type, $this->types)) {
  257. $type = 'left';
  258. }
  259. if ($type == 'right') {
  260. echo PMA_SQP_buildCssData();
  261. }
  262. $_css_file = $this->getPath()
  263. . '/css/theme_' . $type . '.css.php';
  264. if (! file_exists($_css_file)) {
  265. return false;
  266. }
  267. if ($GLOBALS['text_dir'] === 'ltr') {
  268. $right = 'right';
  269. $left = 'left';
  270. } else {
  271. $right = 'left';
  272. $left = 'right';
  273. }
  274. include $_css_file;
  275. if ($type != 'print') {
  276. $_sprites_data_file = $this->getPath() . '/sprites.lib.php';
  277. $_sprites_css_file = './themes/sprites.css.php';
  278. if ( (file_exists($_sprites_data_file) && is_readable($_sprites_data_file))
  279. && (file_exists($_sprites_css_file) && is_readable($_sprites_css_file))
  280. ) {
  281. include $_sprites_data_file;
  282. include $_sprites_css_file;
  283. }
  284. }
  285. return true;
  286. }
  287. /**
  288. * prints out the preview for this theme
  289. *
  290. * @access public
  291. */
  292. function printPreview()
  293. {
  294. echo '<div class="theme_preview">';
  295. echo '<h2>' . htmlspecialchars($this->getName())
  296. .' (' . htmlspecialchars($this->getVersion()) . ')</h2>';
  297. echo '<p>';
  298. echo '<a target="_top" class="take_theme" '
  299. .'name="' . htmlspecialchars($this->getId()) . '" '
  300. . 'href="index.php'.PMA_generate_common_url(array(
  301. 'set_theme' => $this->getId()
  302. )) . '">';
  303. if (@file_exists($this->getPath() . '/screen.png')) {
  304. // if screen exists then output
  305. echo '<img src="' . $this->getPath() . '/screen.png" border="1"'
  306. .' alt="' . htmlspecialchars($this->getName()) . '"'
  307. .' title="' . htmlspecialchars($this->getName()) . '" /><br />';
  308. } else {
  309. echo __('No preview available.');
  310. }
  311. echo '[ <strong>' . __('take it') . '</strong> ]</a>'
  312. .'</p>'
  313. .'</div>';
  314. }
  315. /**
  316. * Remove filter for IE.
  317. *
  318. * @return string CSS code.
  319. */
  320. function getCssIEClearFilter() {
  321. return PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER >= 6 && PMA_USR_BROWSER_VER <= 8
  322. ? 'filter: none'
  323. : '';
  324. }
  325. /**
  326. * Generates code for CSS gradient using various browser extensions.
  327. *
  328. * @param string $start_color Color of gradient start, hex value without #
  329. * @param string $end_color Color of gradient end, hex value without #
  330. *
  331. * @return string CSS code.
  332. */
  333. function getCssGradient($start_color, $end_color)
  334. {
  335. $result = array();
  336. // Opera 9.5+, IE 9
  337. $result[] = 'background-image: url(./themes/svg_gradient.php?from=' . $start_color . '&to=' . $end_color . ');';
  338. $result[] = 'background-size: 100% 100%;';
  339. // Safari 4-5, Chrome 1-9
  340. $result[] = 'background: -webkit-gradient(linear, left top, left bottom, from(#' . $start_color . '), to(#' . $end_color . '));';
  341. // Safari 5.1, Chrome 10+
  342. $result[] = 'background: -webkit-linear-gradient(top, #' . $start_color . ', #' . $end_color . ');';
  343. // Firefox 3.6+
  344. $result[] = 'background: -moz-linear-gradient(top, #' . $start_color . ', #' . $end_color . ');';
  345. // IE 10
  346. $result[] = 'background: -ms-linear-gradient(top, #' . $start_color . ', #' . $end_color . ');';
  347. // Opera 11.10
  348. $result[] = 'background: -o-linear-gradient(top, #' . $start_color . ', #' . $end_color . ');';
  349. // IE 6-8
  350. if (PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER >= 6 && PMA_USR_BROWSER_VER <= 8) {
  351. $result[] = 'filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#' . $start_color . '", endColorstr="#' . $end_color . '");';
  352. }
  353. return implode("\n", $result);
  354. }
  355. /**
  356. * Returns CSS styles for CodeMirror editor based on query formatter colors.
  357. *
  358. * @return string CSS code.
  359. */
  360. function getCssCodeMirror()
  361. {
  362. $result[] = 'span.cm-keyword, span.cm-statement-verb {';
  363. $result[] = ' color: ' . $GLOBALS['cfg']['SQP']['fmtColor']['alpha_reservedWord'] . ';';
  364. $result[] = '}';
  365. $result[] = 'span.cm-variable {';
  366. $result[] = ' color: ' . $GLOBALS['cfg']['SQP']['fmtColor']['alpha_identifier'] . ';';
  367. $result[] = '}';
  368. $result[] = 'span.cm-comment {';
  369. $result[] = ' color: ' . $GLOBALS['cfg']['SQP']['fmtColor']['comment'] . ';';
  370. $result[] = '}';
  371. $result[] = 'span.cm-mysql-string {';
  372. $result[] = ' color: ' . $GLOBALS['cfg']['SQP']['fmtColor']['quote'] . ';';
  373. $result[] = '}';
  374. $result[] = 'span.cm-operator {';
  375. $result[] = ' color: ' . $GLOBALS['cfg']['SQP']['fmtColor']['punct'] . ';';
  376. $result[] = '}';
  377. $result[] = 'span.cm-mysql-word {';
  378. $result[] = ' color: ' . $GLOBALS['cfg']['SQP']['fmtColor']['alpha_identifier'] . ';';
  379. $result[] = '}';
  380. $result[] = 'span.cm-builtin {';
  381. $result[] = ' color: ' . $GLOBALS['cfg']['SQP']['fmtColor']['alpha_functionName'] . ';';
  382. $result[] = '}';
  383. $result[] = 'span.cm-variable-2 {';
  384. $result[] = ' color: ' . $GLOBALS['cfg']['SQP']['fmtColor']['alpha_columnType'] . ';';
  385. $result[] = '}';
  386. $result[] = 'span.cm-variable-3 {';
  387. $result[] = ' color: ' . $GLOBALS['cfg']['SQP']['fmtColor']['alpha_columnAttrib'] . ';';
  388. $result[] = '}';
  389. $result[] = 'span.cm-separator {';
  390. $result[] = ' color: ' . $GLOBALS['cfg']['SQP']['fmtColor']['punct'] . ';';
  391. $result[] = '}';
  392. $result[] = 'span.cm-number {';
  393. $result[] = ' color: ' . $GLOBALS['cfg']['SQP']['fmtColor']['digit_integer'] . ';';
  394. $result[] = '}';
  395. return implode("\n", $result);
  396. }
  397. }
  398. ?>