2

I am trying to integrate HTMLpurifier into codeigniter, for text editor.

I created a helper in App/Helpers named it to htmlpurifier_helper.php and added Purifier function in it like so :

defined('BASEPATH') or exit('No direct script access allowed');

if (!function_exists('html_purify')) {
  public function html_purify($dirty_html, $config = false)
  {
      if (is_array($dirty_html)) {
          foreach ($dirty_html as $key => $val) {
              $clean_html[$key] = html_purify($val, $config);
          }
      } else {
          $ci = &get_instance();

          switch ($config) {
              case 'comment':
                  $config = \HTMLPurifier_Config::createDefault();
                  $config->set('Core.Encoding', $ci->config->item('charset'));
                  $config->set('HTML.Allowed', 'p,a[href|title],abbr[title],acronym[title],b,strong,blockquote[cite],code,em,i,strike');
                  $config->set('AutoFormat.AutoParagraph', true);
                  $config->set('AutoFormat.Linkify', true);
                  $config->set('AutoFormat.RemoveEmpty', true);
                  break;

              case false:
                  $config = HTMLPurifier_Config::createDefault();
                  $config->set('Core.Encoding', $ci->config->item('charset'));
                  $config->set('Core.Encoding', 'utf-8');
                  $config->set("AutoFormat.AutoParagraph", false);
                  $config->set("Core.NormalizeNewlines", true);
                  $config->set('HTML.Allowed', 'iframe[src|title|frameborder|allowfullscreen|class|width|height|loading],p,b,strong,a[href|title],abbr[title],blockquote[cite],code,pre[class],em,i,strike,u,s,sub,sup,ol,ul,li,hr,img[title|alt|src|class|style],h1,h2,h3,h4,h5,h6,object[width|height|data],param[name|value],embed[src|type|allowscriptaccess|width|height],br,*[style]');
                  $config->set('CSS.AllowedProperties', 'font,font-size,font-weight,font-style,font-family,text-decoration,margin-left,margin-right,float,color,background-color,text-align,width,max-width');
                  $config->set('HTML.MaxImgLength', NULL);
                  $config->set('CSS.MaxImgLength', NULL);
                  $config->set('HTML.SafeObject', true);
                  $config->set('HTML.SafeEmbed', true);
                  $config->set('Output.FlashCompat', true);
                  $config->set('AutoFormat.RemoveEmpty', true);
                  $config->set('AutoFormat.RemoveEmpty.RemoveNbsp', true);
                  $config->set('HTML.SafeIframe', true);
                  $config->set('URI.SafeIframeRegexp', '%^//(www.youtube(?:-nocookie)?.com/embed/|player.vimeo.com/video/)%');
                  $def = $config->getHTMLDefinition(true);
                  $def->addAttribute('iframe','allowfullscreen', 'loading', 'Bool');
      
                  break;

              default:
                  show_error('The HTMLPurifier configuration labeled "'.htmlspecialchars($config, ENT_QUOTES, $ci->config->item('charset')).'" could not be found.');
          }
          
          require_once(APPPATH."app/ThirdParty/htmlpurifier/HTMLPurifier.auto.php"); 
          require_once(APPPATH."app/ThirdParty/htmlpurifier/HTMLPurifier.func.php");

          $purifier = new \HTMLPurifier($config);
          $clean_html = $purifier->purify($dirty_html);
      }

      return $clean_html;
  }
}

/* End of htmlpurifier_helper.php */
/* Location: ./app/helpers/htmlpurifier_helper.php */

And I call it in controller to use it like :

$val->setRule('content', translation("content"), 'required');
$val = \App\Helpers\htmlpurifier;
// Or
$val = \App\Helpers\htmlpurifier();
$val = html_purify($val, 'comment');

I get undefined error, I am not familier to CodeIgniter. All I want is purify the content of the text editor.

Thanks for any help.

DLK
  • 161
  • 8
  • Are you really using 'CodeIgniter-4' as specified in your question tags? The source code looks to be that of 'CodeIgniter-3' based on the use of the `get_instance();` method. – steven7mwesigwa Jun 26 '22 at 11:59
  • @steven7mwesigwa yes I am using 4, `CodeIgniter 4 framework.` as I said in question I am not familier to codeigniter, learning from tutorals :) – DLK Jun 26 '22 at 12:11

1 Answers1

1

Instead of:

// ...
$val = \App\Helpers\htmlpurifier; ❌
// Or
$val = \App\Helpers\htmlpurifier(); ❌
$val = html_purify($val, 'comment');

Use this:✅

// ...

helper('htmlpurifier');
$val = html_purify($val, 'comment');

Resource: Loading a Helper


Addendum:

In addition, remove $ci = &get_instance(); since that only applies to CodeIgniter-3.

Lastly:-

Instead of:❌

// ...
$ci->config->item('charset')

Use this:✅

config(\Config\App::class)->charset

Resource: Working With Configuration Files

steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34