PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/web/system/SecurityCenterModule/Util.php

https://github.com/antoniom/core
PHP | 145 lines | 57 code | 22 blank | 66 comment | 13 complexity | 4a904f9338fa0783ae9e3684832fcf60 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MIT
  1. <?php
  2. /**
  3. * Copyright Zikula Foundation 2009 - Zikula Application Framework
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Zikula
  10. *
  11. * Please see the NOTICE file distributed with this source code for further
  12. * information regarding copyright and licensing.
  13. */
  14. namespace SecurityCenterModule;
  15. use ZLanguage, ThemeUtil, UserUtil, ModUtil, CacheUtil;
  16. class Util
  17. {
  18. /**
  19. * Retrieves default configuration array for HTML Purifier.
  20. *
  21. * @return array HTML Purifier default configuration settings.
  22. */
  23. private static function _getpurifierdefaultconfig()
  24. {
  25. $purifierDefaultConfig = \HTMLPurifier_Config::createDefault();
  26. $purifierDefaultConfigValues = $purifierDefaultConfig->def->defaults;
  27. $config = array();
  28. foreach ($purifierDefaultConfigValues as $key => $val) {
  29. $keys = explode(".", $key, 2);
  30. $config[$keys[0]][$keys[1]] = $val;
  31. }
  32. $charset = ZLanguage::getEncoding();
  33. if (strtolower($charset) != 'utf-8') {
  34. // set a different character encoding with iconv
  35. $config['Core']['Encoding'] = $charset;
  36. // Note that HTML Purifier's support for non-Unicode encodings is crippled by the
  37. // fact that any character not supported by that encoding will be silently
  38. // dropped, EVEN if it is ampersand escaped. If you want to work around
  39. // this, you are welcome to read docs/enduser-utf8.html in the full package for a fix,
  40. // but please be cognizant of the issues the "solution" creates (for this
  41. // reason, I do not include the solution in this document).
  42. }
  43. // determine doctype of current theme
  44. // supported doctypes include:
  45. //
  46. // HTML 4.01 Strict
  47. // HTML 4.01 Transitional
  48. // XHTML 1.0 Strict
  49. // XHTML 1.0 Transitional (default)
  50. // XHTML 1.1
  51. //
  52. // TODO - we need a new theme field for doctype declaration
  53. // for now we will use non-strict modes
  54. $currentThemeID = ThemeUtil::getIDFromName(UserUtil::getTheme());
  55. $themeInfo = ThemeUtil::getInfo($currentThemeID);
  56. $useXHTML = (isset($themeInfo['xhtml']) && $themeInfo['xhtml']) ? true : false;
  57. // as XHTML 1.0 Transitional is the default, we only set HTML (for now)
  58. if (!$useXHTML) {
  59. $config['HTML']['Doctype'] = 'HTML 4.01 Transitional';
  60. }
  61. // allow nofollow and imageviewer to be used as document relationships in the rel attribute
  62. // see http://htmlpurifier.org/live/configdoc/plain.html#Attr.AllowedRel
  63. $config['Attr']['AllowedRel'] = array('nofollow' => true, 'imageviewer' => true, 'lightbox' => true);
  64. // allow Youtube by default
  65. $config['Filter']['YouTube'] = false; // technically deprecated in favour of HTML.SafeEmbed and HTML.Object
  66. // general enable for embeds and objects
  67. $config['HTML']['SafeObject'] = true;
  68. $config['Output']['FlashCompat'] = true;
  69. $config['HTML']['SafeEmbed'] = true;
  70. return $config;
  71. }
  72. /**
  73. * Retrieves configuration array for HTML Purifier.
  74. *
  75. * @param array $args All parameters for the function.
  76. * boolean $args['forcedefault'] true to force return of default config / false to auto detect
  77. * @param
  78. *
  79. * @return array HTML Purifier configuration settings.
  80. */
  81. public static function getpurifierconfig($args)
  82. {
  83. if (isset($args['forcedefault']) && $args['forcedefault'] == true) {
  84. $config = self::_getpurifierdefaultconfig();
  85. } else {
  86. // don't change the following statement to getVar()
  87. // $this is not allowed in functions declared as static
  88. $currentconfig = ModUtil::getVar('SecurityCenter', 'htmlpurifierConfig');
  89. if (!is_null($currentconfig) && ($currentconfig !== false)) {
  90. $config = unserialize($currentconfig);
  91. } else {
  92. $config = self::_getpurifierdefaultconfig();
  93. }
  94. }
  95. return $config;
  96. }
  97. /**
  98. * Retrieves an instance of HTMLPurifier.
  99. *
  100. * The instance returned is either a newly created instance, or previously created instance
  101. * that has been cached in a static variable.
  102. *
  103. * @param array $args All arguments for the function.
  104. * bool $args['force'] If true, the HTMLPurifier instance will be generated anew, rather than using an
  105. * existing instance from the static variable.
  106. *
  107. * @staticvar array $purifier The HTMLPurifier instance.
  108. *
  109. * @return \HTMLPurifier The HTMLPurifier instance, returned by reference.
  110. */
  111. public static function getpurifier($args = null)
  112. {
  113. $force = (isset($args['force']) ? $args['force'] : false);
  114. // prepare htmlpurifier class
  115. static $purifier;
  116. if (!isset($purifier) || $force) {
  117. $config = self::getpurifierconfig(array('forcedefault' => false));
  118. $config['Cache']['SerializerPath'] = CacheUtil::getLocalDir() . '/purifierCache';
  119. $purifier = new \HTMLPurifier($config);
  120. }
  121. return $purifier;
  122. }
  123. }