PageRenderTime 77ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/popoon/classes/externalinput.php

https://github.com/chregu/fluxcms
PHP | 88 lines | 42 code | 15 blank | 31 comment | 7 complexity | cdcdee1205c8bceec05565ae1eb05a02 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | popoon |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 2001-2008 Liip AG |
  6. // +----------------------------------------------------------------------+
  7. // | Licensed under the Apache License, Version 2.0 (the "License"); |
  8. // | you may not use this file except in compliance with the License. |
  9. // | You may obtain a copy of the License at |
  10. // | http://www.apache.org/licenses/LICENSE-2.0 |
  11. // | Unless required by applicable law or agreed to in writing, software |
  12. // | distributed under the License is distributed on an "AS IS" BASIS, |
  13. // | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
  14. // | implied. See the License for the specific language governing |
  15. // | permissions and limitations under the License. |
  16. // +----------------------------------------------------------------------+
  17. // | Author: Christian Stocker <christian.stocker@liip.ch> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id$
  21. class popoon_classes_externalinput {
  22. // this basic clean should clean html code from
  23. // lot of possible malicious code for Cross Site Scripting
  24. // use it whereever you get external input
  25. static function basicClean($string) {
  26. if (get_magic_quotes_gpc()) {
  27. $string = stripslashes($string);
  28. }
  29. //if the newer externalinput class exists, use this
  30. if (method_exists('lx_externalinput_clean','basic')) {
  31. return lx_externalinput_clean::basic($string);
  32. }
  33. $string = str_replace(array("&amp;","&lt;","&gt;"),array("&amp;amp;","&amp;lt;","&amp;gt;"),$string);
  34. // fix &entitiy\n;
  35. $string = preg_replace('#(&\#*\w+)[\x00-\x20]+;#u',"$1;",$string);
  36. $string = preg_replace('#(&\#x*)([0-9A-F]+);*#iu',"$1$2;",$string);
  37. $string = html_entity_decode($string, ENT_COMPAT, "UTF-8");
  38. // remove any attribute starting with "on" or xmlns
  39. $string = preg_replace('#(<[^>]+[\x00-\x20\"\'\/])(on|xmlns)[^>]*>#iUu', "$1>", $string);
  40. // remove javascript: and vbscript: protocol
  41. $string = preg_replace('#([a-z]*)[\x00-\x20\/]*=[\x00-\x20\/]*([\`\'\"]*)[\x00-\x20\/]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu', '$1=$2nojavascript...', $string);
  42. $string = preg_replace('#([a-z]*)[\x00-\x20\/]*=[\x00-\x20\/]*([\`\'\"]*)[\x00-\x20\/]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu', '$1=$2novbscript...', $string);
  43. $string = preg_replace('#([a-z]*)[\x00-\x20\/]*=[\x00-\x20\/]*([\`\'\"]*)[\x00-\x20\/]*-moz-binding[\x00-\x20]*:#Uu', '$1=$2nomozbinding...', $string);
  44. $string = preg_replace('#([a-z]*)[\x00-\x20\/]*=[\x00-\x20\/]*([\`\'\"]*)[\x00-\x20\/]*data[\x00-\x20]*:#Uu', '$1=$2nodata...', $string);
  45. //remove any style attributes, IE allows too much stupid things in them, eg.
  46. //<span style="width: expression(alert('Ping!'));"></span>
  47. // and in general you really don't want style declarations in your UGC
  48. $string = preg_replace('#(<[^>]+[\x00-\x20\"\'\/])style[^>]*>#iUu', "$1>", $string);
  49. //remove namespaced elements (we do not need them...)
  50. $string = preg_replace('#</*\w+:\w[^>]*>#i',"",$string);
  51. //remove really unwanted tags
  52. do {
  53. $oldstring = $string;
  54. $string = preg_replace('#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i',"",$string);
  55. } while ($oldstring != $string);
  56. return $string;
  57. }
  58. static function removeMagicQuotes($data) {
  59. if (get_magic_quotes_gpc()) {
  60. $newdata = array();
  61. foreach ($data as $name => $value) {
  62. $name = stripslashes($name);
  63. if (is_array($value)) {
  64. $newdata[$name] = self::removeMagicQuotes($value);
  65. } else {
  66. $newdata[$name] = stripslashes($value);
  67. }
  68. }
  69. return $newdata;
  70. }
  71. return $data;
  72. }
  73. }