PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/phpBB/develop/imageset_to_css.php

http://github.com/phpbb/phpbb3
PHP | 374 lines | 330 code | 21 blank | 23 comment | 51 complexity | 632df64c268d7e1572a51f13ce3175a8 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /*
  3. Converts imageset to CSS code
  4. Change style name and path below, open in browser.
  5. */
  6. $phpbb_root_path = '../';
  7. $style = 'prosilver';
  8. $imageset_path = $phpbb_root_path . 'styles/' . $style . '/imageset';
  9. $theme_path = $phpbb_root_path . 'styles/' . $style . '/theme';
  10. // Start output buffering
  11. ob_start();
  12. // Get global and English images
  13. $images_global = get_imageset($imageset_path);
  14. if ($images_global === false)
  15. {
  16. echo 'imageset.cfg was not found.';
  17. echo ob_get_clean();
  18. return;
  19. }
  20. $images_en = get_imageset($imageset_path, 'en');
  21. if ($images_en === false)
  22. {
  23. echo 'English imageset.cfg was not found.';
  24. echo ob_get_clean();
  25. return;
  26. }
  27. // Remove duplicate images
  28. foreach ($images_en as $key => $row)
  29. {
  30. unset($images_global[$key]);
  31. }
  32. // CSS replacements
  33. $not_compatible = array(
  34. '{T_TEMPLATE_PATH}',
  35. '{T_IMAGESET_PATH}',
  36. '{T_IMAGESET_LANG_PATH}',
  37. '{T_STYLESHEET_NAME}',
  38. '{S_USER_LANG}'
  39. );
  40. $replace = array(
  41. '{T_THEME_PATH}' => '.',
  42. );
  43. // Enable/disable one of lines below to enable/disable replacement of English buttons
  44. // $replace = array_merge($replace, get_replacements($images_global));
  45. $replace = array_merge($replace, get_replacements($images_global), get_replacements($images_en));
  46. // BIDI code
  47. $bidi_code = css($images_global, './images/', true);
  48. // Get all CSS files, parse them
  49. $files = list_files($theme_path, 'css');
  50. if ($files === false || !count($files))
  51. {
  52. echo 'No CSS files found in theme directory.<br />';
  53. }
  54. else for ($i=0; $i<count($files); $i++)
  55. {
  56. $file = $theme_path . '/' . $files[$i];
  57. $data = file_get_contents($file);
  58. $hash = md5($data);
  59. $data = strtr($data, $replace);
  60. $errors = false;
  61. for($j=0; $j<count($not_compatible); $j++)
  62. {
  63. if (strpos($data, $not_compatible[$j]) !== false)
  64. {
  65. echo 'Error: ', $file, ' contains ', $not_compatible[$j], '. That variable cannot be converted.<br />';
  66. continue;
  67. }
  68. }
  69. if (basename($file) == 'bidi.css' && strpos($data, '/* Former imageset */') === false && strlen($bidi_code))
  70. {
  71. // Add bidi data
  72. $data .= "\n/* Former imageset */\n" . $bidi_code;
  73. $bidi_code = '';
  74. echo 'Note: RTL imageset entries were added at the end of file below:<br />';
  75. }
  76. if (md5($data) == $hash)
  77. {
  78. echo 'Nothing to replace in ', $file, '<br />';
  79. }
  80. else
  81. {
  82. echo 'Updated ', $file, ':', dump_code($data, $files[$i]);
  83. }
  84. }
  85. // Check if there are invalid images in imageset
  86. $list = array_merge($images_global, $images_en);
  87. foreach ($list as $key => $row)
  88. {
  89. if ($row['skip'])
  90. {
  91. echo 'Unable to generate code to add to CSS files because some images are missing or invalid. See errors above.';
  92. echo ob_get_clean();
  93. return;
  94. }
  95. }
  96. // Code to add to CSS files
  97. $code = '
  98. /* Former imageset */
  99. span.imageset {
  100. display: inline-block;
  101. background: transparent none 0 0 no-repeat;
  102. margin: 0;
  103. padding: 0;
  104. width: 0;
  105. height: 0;
  106. overflow: hidden;
  107. }
  108. /* Global imageset items */
  109. ' . css($images_global, './images/') . '
  110. /* English images for fallback */
  111. ' . css($images_en, './en/');
  112. if (strlen($bidi_code))
  113. {
  114. $code .= "\n/* RTL imageset entries */\n" . $bidi_code;
  115. }
  116. echo 'Code to add to CSS file:', dump_code($code, 'imageset.css');
  117. $list = list_languages($imageset_path);
  118. for ($i=0; $i<count($list); $i++)
  119. {
  120. $lang = $list[$i];
  121. $images = get_imageset($imageset_path . '/' . $lang);
  122. if (!count($images))
  123. {
  124. continue;
  125. }
  126. $code = '/* ' . strtoupper($lang) . ' Language Pack */
  127. ' . css($images, './');
  128. echo 'New CSS file: ', $theme_path, '/', $lang, '/stylesheet.css', dump_code($code, 'stylesheet_' . $lang . '.css');
  129. }
  130. echo ob_get_clean();
  131. return;
  132. /*
  133. Functions
  134. */
  135. function get_imageset($path, $lang = '')
  136. {
  137. $cfg = $path . ($lang ? '/' . $lang : '') . '/imageset.cfg';
  138. if (!@file_exists($cfg))
  139. {
  140. return false;
  141. }
  142. $data = file($cfg);
  143. $result = array();
  144. for ($i=0; $i<count($data); $i++)
  145. {
  146. $str = trim($data[$i]);
  147. if (substr($str, 0, 4) != 'img_')
  148. {
  149. continue;
  150. }
  151. $list = explode('=', $data[$i]);
  152. if (count($list) != 2)
  153. {
  154. continue;
  155. }
  156. $key = trim($list[0]);
  157. $row = explode('*', trim($list[1]));
  158. $file = trim($row[0]);
  159. $height = isset($row[1]) && intval($row[1]) ? intval($row[1]) : false;
  160. $width = isset($row[2]) && intval($row[2]) ? intval($row[2]) : false;
  161. $skip = false;
  162. if (strlen($file) && (!$width || !$height))
  163. {
  164. // Try to detect width/height
  165. $filename = $path . ($lang ? '/' . $lang : '') . '/' . $file;
  166. if (!@file_exists($filename))
  167. {
  168. echo 'Error: file ', $filename, ' does not exist and its dimensions are not available in imageset.cfg<br />';
  169. $skip = true;
  170. }
  171. else
  172. {
  173. $size = @getimagesize($filename);
  174. if ($size === false)
  175. {
  176. echo 'Error: file ', $filename, ' is not a valid image<br />';
  177. $skip = true;
  178. }
  179. else
  180. {
  181. if(!$width) $width = intval($size[0]);
  182. if(!$height) $height = intval($size[1]);
  183. }
  184. }
  185. }
  186. $result[$key] = array(
  187. 'lang' => $lang,
  188. 'file' => $file,
  189. 'height' => $height,
  190. 'width' => $width,
  191. 'skip' => $skip
  192. );
  193. }
  194. return $result;
  195. }
  196. function get_replacements($list)
  197. {
  198. $result = array();
  199. foreach ($list as $key => $row)
  200. {
  201. $key = '{' . strtoupper($key);
  202. $result[$key . '_SRC}'] = strlen($row['file']) ? ($row['lang'] ? './' . $row['lang'] : './images') . '/' . $row['file'] : '';
  203. $result[$key . '_WIDTH}'] = intval($row['width']);
  204. $result[$key . '_HEIGHT}'] = intval($row['height']);
  205. }
  206. return $result;
  207. }
  208. function list_files($dir, $ext)
  209. {
  210. $res = @opendir($dir);
  211. if ($res === false)
  212. {
  213. return false;
  214. }
  215. $files = array();
  216. while (($file = readdir($res)) !== false)
  217. {
  218. $list = explode('.', $file);
  219. if(count($list) > 1 && strtolower($list[count($list) - 1]) == $ext)
  220. {
  221. $files[] = $file;
  222. }
  223. }
  224. closedir($res);
  225. return $files;
  226. }
  227. function list_languages($dir)
  228. {
  229. $res = @opendir($dir);
  230. if ($res === false)
  231. {
  232. return array();
  233. }
  234. $files = array();
  235. while (($file = readdir($res)) !== false)
  236. {
  237. if (substr($file, 0, 1) == '.')
  238. {
  239. continue;
  240. }
  241. $filename = $dir . '/' . $file;
  242. if (is_dir($filename) && file_exists($filename . '/imageset.cfg'))
  243. {
  244. $files[] = $file;
  245. }
  246. }
  247. closedir($res);
  248. return $files;
  249. }
  250. function dump_code($code, $filename = 'file.txt')
  251. {
  252. $hash = md5($code);
  253. if (isset($_GET['download']) && $_GET['download'] === $hash)
  254. {
  255. // Download file
  256. ob_end_clean();
  257. header('Pragma: public');
  258. header('Expires: 0');
  259. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  260. header('Content-Type: application/force-download');
  261. header('Content-Disposition: attachment; filename="' . $filename . '";');
  262. header('Content-Transfer-Encoding: binary');
  263. header('Content-Length: ' . strlen($code));
  264. echo $code;
  265. exit;
  266. }
  267. $list = explode("\n", $code);
  268. $height = 15 * count($list);
  269. echo ' [ <a href="?download=', $hash, '">download</a> <a href="javascript:void(0);" onclick="document.getElementById(\'code-', $hash, '\').style.height = \'', $height, 'px\'; this.style.display = \'none\'; return false;">expand</a> ]<br />';
  270. echo '<textarea id="code-', $hash, '" onfocus="this.select();" style="width: 98%; height: 200px;">', htmlspecialchars($code), '</textarea><br />';
  271. }
  272. function css($list, $path = './', $bidi = false)
  273. {
  274. $code = '';
  275. // Change value to true if you want images to be grouped up by size
  276. $group = $bidi;
  277. if ($group)
  278. {
  279. // group up images by size
  280. $groups = array();
  281. foreach ($list as $key => $row)
  282. {
  283. if (!strlen($row['file']))
  284. {
  285. continue;
  286. }
  287. $groups[$row['width'] . '*' . $row['height']][] = $key;
  288. }
  289. foreach ($groups as $size => $keys)
  290. {
  291. $extra = '';
  292. for ($i=0; $i<count($keys); $i++)
  293. {
  294. $code .= ($i == 0 ? '' : ', ') . ($bidi ? '.rtl ' : '') . '.imageset.' . substr($keys[$i], 4);
  295. if (!$bidi)
  296. {
  297. $extra .= '.imageset.' . substr($keys[$i], 4) . ' { background-image: url("' . $path . $list[$keys[$i]]['file'] . "\"); }\n";
  298. }
  299. }
  300. $row = $list[$keys[0]];
  301. $code .= ' {';
  302. if ($bidi)
  303. {
  304. $code .= '
  305. padding-right: ' . $row['width'] . 'px;
  306. padding-left: 0;
  307. }
  308. ';
  309. }
  310. else
  311. {
  312. $code .= '
  313. padding-left: ' . $row['width'] . 'px;
  314. padding-top: ' . $row['height'] . 'px;
  315. }
  316. ' . $extra;
  317. }
  318. }
  319. }
  320. else
  321. {
  322. foreach ($list as $key => $row)
  323. {
  324. if (!strlen($row['file']))
  325. {
  326. continue;
  327. }
  328. $code .= ($bidi ? '.rtl ' : '') . '.imageset.' . substr($key, 4) . ' {';
  329. if ($bidi)
  330. {
  331. $code .= '
  332. padding-right: ' . $row['width'] . 'px;
  333. padding-left: 0;
  334. }
  335. ';
  336. }
  337. else
  338. {
  339. $code .= '
  340. background-image: url("' . $path . $row['file'] . '");
  341. padding-left: ' . $row['width'] . 'px;
  342. padding-top: ' . $row['height'] . 'px;
  343. }
  344. ';
  345. }
  346. }
  347. }
  348. return $code;
  349. }