Theme.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * hold Theme class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin;
  10. use PhpMyAdmin\Template;
  11. use PhpMyAdmin\ThemeManager;
  12. use PhpMyAdmin\Url;
  13. /**
  14. * handles theme
  15. *
  16. * @todo add the possibility to make a theme depend on another theme
  17. * and by default on original
  18. * @todo make all components optional - get missing components from 'parent' theme
  19. *
  20. * @package PhpMyAdmin
  21. */
  22. class Theme
  23. {
  24. /**
  25. * @var string theme version
  26. * @access protected
  27. */
  28. public $version = '0.0.0.0';
  29. /**
  30. * @var string theme name
  31. * @access protected
  32. */
  33. public $name = '';
  34. /**
  35. * @var string theme id
  36. * @access protected
  37. */
  38. public $id = '';
  39. /**
  40. * @var string theme path
  41. * @access protected
  42. */
  43. public $path = '';
  44. /**
  45. * @var string image path
  46. * @access protected
  47. */
  48. public $img_path = '';
  49. /**
  50. * @var integer last modification time for info file
  51. * @access protected
  52. */
  53. public $mtime_info = 0;
  54. /**
  55. * needed because sometimes, the mtime for different themes
  56. * is identical
  57. * @var integer filesize for info file
  58. * @access protected
  59. */
  60. public $filesize_info = 0;
  61. /**
  62. * @var array List of css files to load
  63. * @access private
  64. */
  65. public $_cssFiles = [
  66. 'common',
  67. 'enum_editor',
  68. 'gis',
  69. 'navigation',
  70. 'designer',
  71. 'rte',
  72. 'codemirror',
  73. 'jqplot',
  74. 'resizable-menu',
  75. 'icons',
  76. ];
  77. /**
  78. * @var Template
  79. */
  80. public $template;
  81. /**
  82. * Theme constructor.
  83. */
  84. public function __construct()
  85. {
  86. $this->template = new Template();
  87. }
  88. /**
  89. * Loads theme information
  90. *
  91. * @return boolean whether loading them info was successful or not
  92. * @access public
  93. */
  94. public function loadInfo()
  95. {
  96. $infofile = $this->getPath() . '/theme.json';
  97. if (! @file_exists($infofile)) {
  98. return false;
  99. }
  100. if ($this->mtime_info === filemtime($infofile)) {
  101. return true;
  102. }
  103. $content = @file_get_contents($infofile);
  104. if ($content === false) {
  105. return false;
  106. }
  107. $data = json_decode($content, true);
  108. // Did we get expected data?
  109. if (! is_array($data)) {
  110. return false;
  111. }
  112. // Check that all required data are there
  113. $members = [
  114. 'name',
  115. 'version',
  116. 'supports',
  117. ];
  118. foreach ($members as $member) {
  119. if (! isset($data[$member])) {
  120. return false;
  121. }
  122. }
  123. // Version check
  124. if (! is_array($data['supports'])) {
  125. return false;
  126. }
  127. if (! in_array(PMA_MAJOR_VERSION, $data['supports'])) {
  128. return false;
  129. }
  130. $this->mtime_info = filemtime($infofile);
  131. $this->filesize_info = filesize($infofile);
  132. $this->setVersion($data['version']);
  133. $this->setName($data['name']);
  134. return true;
  135. }
  136. /**
  137. * returns theme object loaded from given folder
  138. * or false if theme is invalid
  139. *
  140. * @param string $folder path to theme
  141. *
  142. * @return Theme|false
  143. * @static
  144. * @access public
  145. */
  146. public static function load($folder)
  147. {
  148. $theme = new Theme();
  149. $theme->setPath($folder);
  150. if (! $theme->loadInfo()) {
  151. return false;
  152. }
  153. $theme->checkImgPath();
  154. return $theme;
  155. }
  156. /**
  157. * checks image path for existence - if not found use img from fallback theme
  158. *
  159. * @access public
  160. * @return bool
  161. */
  162. public function checkImgPath()
  163. {
  164. // try current theme first
  165. if (is_dir($this->getPath() . '/img/')) {
  166. $this->setImgPath($this->getPath() . '/img/');
  167. return true;
  168. }
  169. // try fallback theme
  170. $fallback = './themes/' . ThemeManager::FALLBACK_THEME . '/img/';
  171. if (is_dir($fallback)) {
  172. $this->setImgPath($fallback);
  173. return true;
  174. }
  175. // we failed
  176. trigger_error(
  177. sprintf(
  178. __('No valid image path for theme %s found!'),
  179. $this->getName()
  180. ),
  181. E_USER_ERROR
  182. );
  183. return false;
  184. }
  185. /**
  186. * returns path to theme
  187. *
  188. * @access public
  189. * @return string path to theme
  190. */
  191. public function getPath()
  192. {
  193. return $this->path;
  194. }
  195. /**
  196. * set path to theme
  197. *
  198. * @param string $path path to theme
  199. *
  200. * @return void
  201. * @access public
  202. */
  203. public function setPath($path)
  204. {
  205. $this->path = trim($path);
  206. }
  207. /**
  208. * sets version
  209. *
  210. * @param string $version version to set
  211. *
  212. * @return void
  213. * @access public
  214. */
  215. public function setVersion($version)
  216. {
  217. $this->version = trim($version);
  218. }
  219. /**
  220. * returns version
  221. *
  222. * @return string version
  223. * @access public
  224. */
  225. public function getVersion()
  226. {
  227. return $this->version;
  228. }
  229. /**
  230. * checks theme version against $version
  231. * returns true if theme version is equal or higher to $version
  232. *
  233. * @param string $version version to compare to
  234. *
  235. * @return boolean true if theme version is equal or higher to $version
  236. * @access public
  237. */
  238. public function checkVersion($version)
  239. {
  240. return version_compare($this->getVersion(), $version, 'lt');
  241. }
  242. /**
  243. * sets name
  244. *
  245. * @param string $name name to set
  246. *
  247. * @return void
  248. * @access public
  249. */
  250. public function setName($name)
  251. {
  252. $this->name = trim($name);
  253. }
  254. /**
  255. * returns name
  256. *
  257. * @access public
  258. * @return string name
  259. */
  260. public function getName()
  261. {
  262. return $this->name;
  263. }
  264. /**
  265. * sets id
  266. *
  267. * @param string $id new id
  268. *
  269. * @return void
  270. * @access public
  271. */
  272. public function setId($id)
  273. {
  274. $this->id = trim($id);
  275. }
  276. /**
  277. * returns id
  278. *
  279. * @return string id
  280. * @access public
  281. */
  282. public function getId()
  283. {
  284. return $this->id;
  285. }
  286. /**
  287. * Sets path to images for the theme
  288. *
  289. * @param string $path path to images for this theme
  290. *
  291. * @return void
  292. * @access public
  293. */
  294. public function setImgPath($path)
  295. {
  296. $this->img_path = $path;
  297. }
  298. /**
  299. * Returns the path to image for the theme.
  300. * If filename is given, it possibly fallbacks to fallback
  301. * theme for it if image does not exist.
  302. *
  303. * @param string $file file name for image
  304. * @param string $fallback fallback image
  305. *
  306. * @access public
  307. * @return string image path for this theme
  308. */
  309. public function getImgPath($file = null, $fallback = null)
  310. {
  311. if ($file === null) {
  312. return $this->img_path;
  313. }
  314. if (is_readable($this->img_path . $file)) {
  315. return $this->img_path . $file;
  316. }
  317. if ($fallback !== null) {
  318. return $this->getImgPath($fallback);
  319. }
  320. return './themes/' . ThemeManager::FALLBACK_THEME . '/img/' . $file;
  321. }
  322. /**
  323. * Renders the preview for this theme
  324. *
  325. * @return string
  326. * @access public
  327. */
  328. public function getPrintPreview()
  329. {
  330. $url_params = ['set_theme' => $this->getId()];
  331. $screen = null;
  332. $path = $this->getPath() . '/screen.png';
  333. if (@file_exists($path)) {
  334. $screen = $path;
  335. }
  336. return $this->template->render('theme_preview', [
  337. 'url_params' => $url_params,
  338. 'name' => $this->getName(),
  339. 'version' => $this->getVersion(),
  340. 'id' => $this->getId(),
  341. 'screen' => $screen,
  342. ]);
  343. }
  344. }