PageRenderTime 74ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/titania/colorizeit.php

http://github.com/phpbb/customisation-db
PHP | 319 lines | 251 code | 27 blank | 41 comment | 76 complexity | 596a4b2c1afd0eea5705e8424f3f6643 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Titania
  5. * @copyright (c) 2008 phpBB Customisation Database Team
  6. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License, version 2
  7. *
  8. */
  9. /**
  10. * @ignore
  11. */
  12. define('IN_TITANIA', true);
  13. if (!defined('TITANIA_ROOT')) define('TITANIA_ROOT', './');
  14. if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
  15. require TITANIA_ROOT . 'common.' . PHP_EXT;
  16. phpbb::$user->add_lang('viewtopic');
  17. // Get ColorizeIt configuration
  18. if(!strlen(titania::$config->colorizeit)) clr_error('ERROR_NO_ATTACHMENT');
  19. switch(titania::$config->colorizeit_auth)
  20. {
  21. case 'HEADER':
  22. $var = strtoupper(str_replace('-', '_', titania::$config->colorizeit_var));
  23. if(!isset($_SERVER['HTTP_' . $var]) || $_SERVER['HTTP_' . $var] != titania::$config->colorizeit_value) clr_error('ERROR_NO_ATTACHMENT');
  24. break;
  25. case 'POST':
  26. $var = titania::$config->colorizeit_var;
  27. if(!isset($_POST[$var]) || $_POST[$var] != titania::$config->colorizeit_value) clr_error('ERROR_NO_ATTACHMENT');
  28. }
  29. /**
  30. * Get attachment
  31. **/
  32. $download_id = request_var('id', 0);
  33. if(!$download_id) clr_error('ERROR_NO_ATTACHMENT');
  34. $sql = 'SELECT *
  35. FROM ' . TITANIA_ATTACHMENTS_TABLE . "
  36. WHERE attachment_id = $download_id";
  37. $result = phpbb::$db->sql_query_limit($sql, 1);
  38. $attachment = phpbb::$db->sql_fetchrow($result);
  39. phpbb::$db->sql_freeresult($result);
  40. if (!$attachment || $attachment['object_type'] != TITANIA_CONTRIB)
  41. {
  42. clr_error('ERROR_NO_ATTACHMENT');
  43. }
  44. if((int) $attachment['object_type'] != TITANIA_CONTRIB || $attachment['is_orphan'] || $attachment['attachment_access'] < titania::$access_level)
  45. {
  46. clr_error('ERROR_NO_ATTACHMENT');
  47. }
  48. /**
  49. * Get revision and contribution
  50. */
  51. $sql = 'SELECT revision_id, contrib_id, revision_status, revision_clr_options FROM ' . TITANIA_REVISIONS_TABLE . '
  52. WHERE attachment_id = ' . $attachment['attachment_id'];
  53. $result = phpbb::$db->sql_query($sql);
  54. $revision = phpbb::$db->sql_fetchrow($result);
  55. phpbb::$db->sql_freeresult($result);
  56. $contrib = new titania_contribution;
  57. if (!$contrib->load((int) $revision['contrib_id']) || $revision['revision_status'] != TITANIA_REVISION_APPROVED)
  58. {
  59. clr_error('SORRY_AUTH_VIEW_ATTACH');
  60. }
  61. if (!$contrib->has_colorizeit())
  62. {
  63. clr_error('CLR_ERROR_NOSAMPLE');
  64. }
  65. /**
  66. * Get file options
  67. */
  68. if (!strlen($revision['revision_clr_options']))
  69. {
  70. $zip_file = titania::$config->upload_path . utf8_basename($attachment['attachment_directory']) . '/' . utf8_basename($attachment['physical_filename']);
  71. if(!@file_exists($zip_file))
  72. {
  73. clr_error('ERROR_NO_ATTACHMENT');
  74. }
  75. $new_dir_name = md5(serialize($_SERVER)) . '_' . microtime();
  76. $contrib_tools = new titania_contrib_tools($zip_file, $new_dir_name);
  77. $phpbb_data = clr_phpbb_data($contrib_tools->unzip_dir);
  78. $contrib_tools->remove_temp_files();
  79. // save data
  80. $revision['revision_clr_options'] = serialize($phpbb_data);
  81. $sql_ary = array(
  82. 'revision_clr_options' => $revision['revision_clr_options']
  83. );
  84. $sql = 'UPDATE ' . TITANIA_REVISIONS_TABLE . '
  85. SET ' . phpbb::$db->sql_build_array('UPDATE', $sql_ary) . '
  86. WHERE revision_id = ' . $revision['revision_id'];
  87. phpbb::$db->sql_query($sql);
  88. }
  89. /**
  90. * Build XML data
  91. */
  92. $data = unserialize($revision['revision_clr_options']);
  93. $base_url = titania::$config->titania_script_path . 'download/id_';
  94. if(substr($base_url, 0, 1) !== '/') $base_url = '/' . $base_url;
  95. $xml = '<' . '?xml version="1.0" encoding="UTF-8"?' . ">\n<colorizeit>\n";
  96. $xml .= "\t<name>" . htmlspecialchars($contrib->contrib_name) . "</name>\n";
  97. $url = titania_url::build_url('download', array('id' => $attachment['attachment_id']));
  98. $xml .= "\t<file src=\"" . htmlspecialchars($base_url) . $attachment['attachment_id'] . '" created="' . $attachment['filetime'] . "\" />\n";
  99. $xml .= "\t<sample src=\"" . htmlspecialchars($base_url) . $contrib->clr_sample['attachment_id'] . '" created="' . $contrib->clr_sample['filetime'] . "\" />\n";
  100. $xml .= "\t<colors>" . htmlspecialchars($contrib->contrib_clr_colors) . "</colors>\n";
  101. $parser = $data['parser'];
  102. $xml .= "\t<parser>" . $parser . "</parser>\n";
  103. if($data['options'])
  104. {
  105. foreach($data as $key => $value)
  106. {
  107. if(substr($key, 0, strlen($parser)) == $parser)
  108. {
  109. $xml .= "\t<option name=\"" . htmlspecialchars($key) . "\">" . htmlspecialchars($value) . "</option>\n";
  110. }
  111. }
  112. if(isset($data['phpbb3_dir']))
  113. {
  114. $xml .= "\t<filename>" . htmlspecialchars($data['phpbb3_dir']) . ".zip</filename>\n";
  115. }
  116. }
  117. $xml .= '</colorizeit>';
  118. echo $xml;
  119. clr_gc();
  120. /**
  121. * Show error message and exit
  122. */
  123. function clr_error($message)
  124. {
  125. if(isset(phpbb::$user->lang[$message])) $message = phpbb::$user->lang[$message];
  126. echo '<?xml version="1.0" encoding="UTF-8"?>', "\n", '<error>', $message, '</error>';
  127. clr_gc();
  128. }
  129. /**
  130. * Exit
  131. */
  132. function clr_gc()
  133. {
  134. if (!empty(phpbb::$cache))
  135. {
  136. phpbb::$cache->unload();
  137. }
  138. phpbb::$db->sql_close();
  139. exit;
  140. }
  141. /**
  142. * Extract parser data from style. Based on colorizeit.com code
  143. **/
  144. function clr_phpbb_data($base_dir)
  145. {
  146. $files = clr_find_cfg($base_dir, '');
  147. if(!count($files))
  148. {
  149. // no cfg files were found. not a phpBB style
  150. return array('parser' => 'default', 'options' => false);
  151. }
  152. // check all files
  153. $items = array();
  154. $languages = array();
  155. for($i=0; $i<count($files); $i++)
  156. {
  157. if($files[$i]['name'] == 'style.cfg')
  158. {
  159. $result = clr_phpbb_check_cfg(file_get_contents($base_dir . $files[$i]['dir'] . '/' . $files[$i]['name']));
  160. if($result === false) continue;
  161. $list = explode('/', $files[$i]['dir']);
  162. $dir = $list[count($list) - 1];
  163. $items['dir'][$dir] = $dir;
  164. $items['style'][$result['name']] = $result['name'];
  165. }
  166. elseif(in_array($files[$i]['name'], array('theme.cfg', 'template.cfg', 'imageset.cfg')))
  167. {
  168. $component = substr($files[$i]['name'], 0, strlen($files[$i]['name']) - 4);
  169. $list = explode('/', $files[$i]['dir']);
  170. if($list[count($list) - 1] != $component) continue;
  171. $result = clr_phpbb_check_cfg(file_get_contents($base_dir . $files[$i]['dir'] . '/' . $files[$i]['name']));
  172. if($result !== false)
  173. {
  174. $dir = $list[count($list) - 2];
  175. $items['dir'][$dir] = $dir;
  176. $items[$component][$result['name']] = $result['name'];
  177. }
  178. if($files[$i]['name'] == 'imageset.cfg')
  179. {
  180. // find all language packs
  181. $search = $files[$i]['dir'] . '/';
  182. for($j=0; $j<count($files); $j++)
  183. if($j != $i && $files[$j]['name'] == 'imageset.cfg' && strpos($files[$j]['dir'], $search) === 0)
  184. {
  185. $img_dir = substr($files[$j]['dir'], strlen($search));
  186. if(strlen($img_dir) && strpos($img_dir, '/') === false && !in_array($img_dir, $languages) && preg_match('/^[a-zA-Z0-9\-_]+$/', $img_dir))
  187. {
  188. $languages[] = $img_dir;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. if(!count($items))
  195. {
  196. // no phpbb style was found
  197. return array('parser' => 'default', 'options' => false);
  198. }
  199. // remove duplicates
  200. $result = array();
  201. if(isset($items['style']))
  202. {
  203. foreach(array('theme', 'template', 'imageset') as $component)
  204. if(isset($items[$component]))
  205. {
  206. foreach($items[$component] as $key)
  207. {
  208. if(isset($items['style'][$key]))
  209. {
  210. unset($items[$component][$key]);
  211. }
  212. }
  213. if(!count($items[$component]))
  214. {
  215. unset($items[$component]);
  216. }
  217. }
  218. }
  219. // generate result
  220. $result = array(
  221. 'parser' => 'phpbb3',
  222. 'options' => true,
  223. );
  224. foreach(array('style', 'theme', 'template', 'imageset', 'dir') as $component)
  225. {
  226. if(isset($items[$component]))
  227. {
  228. $i = 0;
  229. foreach($items[$component] as $key)
  230. {
  231. $result['phpbb3_' . $component . ($i > 0 ? $i : '')] = $key;
  232. $i ++;
  233. }
  234. }
  235. }
  236. if(count($languages))
  237. {
  238. $result['phpbb3_lang'] = implode(',', $languages);
  239. }
  240. if(isset($items['style']) && count($items['style']) == 1)
  241. {
  242. foreach($items['style'] as $key) $result['style_name'] = $key;
  243. }
  244. return $result;
  245. }
  246. function clr_find_cfg($base_dir, $dir)
  247. {
  248. $result = array();
  249. foreach (scandir($base_dir . $dir) as $item)
  250. {
  251. if ($item == '.' || $item == '..')
  252. {
  253. continue;
  254. }
  255. if (is_dir($base_dir . $dir . '/' . $item))
  256. {
  257. $result = array_merge($result, clr_find_cfg($base_dir, $dir . '/' . $item));
  258. }
  259. else if(substr($item, -4) == '.cfg')
  260. {
  261. $result[] = array(
  262. 'dir' => $dir,
  263. 'name' => $item
  264. );
  265. }
  266. }
  267. return $result;
  268. }
  269. function clr_phpbb_check_cfg($data)
  270. {
  271. $list = explode("\n", $data);
  272. $name = false;
  273. // $version = false;
  274. for($i=0; $i<count($list); $i++)
  275. {
  276. $str = trim($list[$i]);
  277. if(strlen($str))
  278. {
  279. $list2 = explode(' =', $str);
  280. if(count($list2) != 2 && substr($str, 0, 1) != '#')
  281. {
  282. return false;
  283. }
  284. else
  285. {
  286. if($list2[0] == 'name') $name = trim($list2[1]);
  287. // if($list2[0] == 'version') $version = trim($list2[1]);
  288. }
  289. }
  290. }
  291. return $name === false ? false : array('name' => $name/*, 'version' => $version*/);
  292. }