Theme.php 8.8 KB

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