PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/phpBB/develop/check_flash_bbcodes.php

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