PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/develop/check_flash_bbcodes.php

https://github.com/naderman/phpbb-orchestra
PHP | 163 lines | 104 code | 29 blank | 30 comment | 14 complexity | b661340b355e56e30331bd05d53d1635 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package phpBB3
  5. * @version $Id$
  6. * @copyright (c) 2009, 2010 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * This script will check your database for potentially dangerous flash BBCode tags
  12. */
  13. //
  14. // Security message:
  15. //
  16. // This script is potentially dangerous.
  17. // Remove or comment the next line (die(".... ) to enable this script.
  18. // Do NOT FORGET to either remove this script or disable it after you have used it.
  19. //
  20. die("Please read the first lines of this script for instructions on how to enable it\n");
  21. /**
  22. */
  23. define('IN_PHPBB', true);
  24. $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
  25. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  26. include($phpbb_root_path . 'common.' . $phpEx);
  27. if (php_sapi_name() != 'cli')
  28. {
  29. header('Content-Type: text/plain');
  30. }
  31. check_table_flash_bbcodes(POSTS_TABLE, 'post_id', 'post_text', 'bbcode_uid', 'bbcode_bitfield');
  32. check_table_flash_bbcodes(PRIVMSGS_TABLE, 'msg_id', 'message_text', 'bbcode_uid', 'bbcode_bitfield');
  33. check_table_flash_bbcodes(USERS_TABLE, 'user_id', 'user_sig', 'user_sig_bbcode_uid', 'user_sig_bbcode_bitfield');
  34. check_table_flash_bbcodes(FORUMS_TABLE, 'forum_id', 'forum_desc', 'forum_desc_uid', 'forum_desc_bitfield');
  35. check_table_flash_bbcodes(FORUMS_TABLE, 'forum_id', 'forum_rules', 'forum_rules_uid', 'forum_rules_bitfield');
  36. check_table_flash_bbcodes(GROUPS_TABLE, 'group_id', 'group_desc', 'group_desc_uid', 'group_desc_bitfield');
  37. echo "If potentially dangerous flash bbcodes were found, please reparse the posts using the Support Toolkit (http://www.phpbb.com/support/stk/) and/or file a ticket in the Incident Tracker (http://www.phpbb.com/incidents/).\n";
  38. function check_table_flash_bbcodes($table_name, $id_field, $content_field, $uid_field, $bitfield_field)
  39. {
  40. echo "Checking $content_field on $table_name\n";
  41. $ids = get_table_flash_bbcode_pkids($table_name, $id_field, $content_field, $uid_field, $bitfield_field);
  42. $size = sizeof($ids);
  43. if ($size)
  44. {
  45. echo "Found $size potentially dangerous flash bbcodes.\n";
  46. echo "$id_field: " . implode(', ', $ids) . "\n";
  47. }
  48. else
  49. {
  50. echo "No potentially dangerous flash bbcodes found.\n";
  51. }
  52. echo "\n";
  53. }
  54. function get_table_flash_bbcode_pkids($table_name, $id_field, $content_field, $uid_field, $bitfield_field)
  55. {
  56. global $db;
  57. $ids = array();
  58. $sql = "SELECT $id_field, $content_field, $uid_field, $bitfield_field
  59. FROM $table_name
  60. WHERE $content_field LIKE '%[/flash:%'
  61. AND $bitfield_field <> ''";
  62. $result = $db->sql_query($sql);
  63. while ($row = $db->sql_fetchrow($result))
  64. {
  65. $uid = $row[$uid_field];
  66. // thanks support toolkit
  67. $content = html_entity_decode_utf8($row[$content_field]);
  68. set_var($content, $content, 'string', true);
  69. $content = utf8_normalize_nfc($content);
  70. $bitfield_data = $row[$bitfield_field];
  71. if (!is_valid_flash_bbcode($content, $uid) && has_flash_enabled($bitfield_data))
  72. {
  73. $ids[] = (int) $row[$id_field];
  74. }
  75. }
  76. $db->sql_freeresult($result);
  77. return $ids;
  78. }
  79. function get_flash_regex($uid)
  80. {
  81. return "#\[flash=([0-9]+),([0-9]+):$uid\](.*?)\[/flash:$uid\]#";
  82. }
  83. // extract all valid flash bbcodes
  84. // check if the bbcode content is a valid URL for each match
  85. function is_valid_flash_bbcode($cleaned_content, $uid)
  86. {
  87. $regex = get_flash_regex($uid);
  88. $url_regex = get_preg_expression('url');
  89. $www_url_regex = get_preg_expression('www_url');
  90. if (preg_match_all($regex, $cleaned_content, $matches))
  91. {
  92. foreach ($matches[3] as $flash_url)
  93. {
  94. if (!preg_match("#^($url_regex|$www_url_regex)$#i", $flash_url))
  95. {
  96. return false;
  97. }
  98. }
  99. }
  100. return true;
  101. }
  102. // check if a bitfield includes flash
  103. // 11 = flash bit
  104. function has_flash_enabled($bitfield_data)
  105. {
  106. $bitfield = new bitfield($bitfield_data);
  107. return $bitfield->get(11);
  108. }
  109. // taken from support toolkit
  110. function html_entity_decode_utf8($string)
  111. {
  112. static $trans_tbl;
  113. // replace numeric entities
  114. $string = preg_replace('~&#x([0-9a-f]+);~ei', 'code2utf8(hexdec("\\1"))', $string);
  115. $string = preg_replace('~&#([0-9]+);~e', 'code2utf8(\\1)', $string);
  116. // replace literal entities
  117. if (!isset($trans_tbl))
  118. {
  119. $trans_tbl = array();
  120. foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key)
  121. $trans_tbl[$key] = utf8_encode($val);
  122. }
  123. return strtr($string, $trans_tbl);
  124. }
  125. // taken from support toolkit
  126. // Returns the utf string corresponding to the unicode value (from php.net, courtesy - romans@void.lv)
  127. function code2utf8($num)
  128. {
  129. if ($num < 128) return chr($num);
  130. if ($num < 2048) return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
  131. if ($num < 65536) return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
  132. if ($num < 2097152) return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
  133. return '';
  134. }