Jump to content

Modifying Html purifier allowed scheme to allow image base64 encoded in ckeditor


robotpapier
 Share

Recommended Posts

Hi,

I need to save base64 encoded images in the ckeditor but it keeps purifying it.

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJ...

I don't want to disable the purifier so I edited 

wire\modules\Markup\MarkupHTMLPurifier\htmlpurifier\standalone\HTMLPurifier\ConfigSchema\schema\URI.AllowedSchemes.txt

and added 

'data' => true

to

URI.AllowedSchemes
TYPE: lookup
--DEFAULT--
array (
  'http' => true,
  'https' => true,
  'mailto' => true,
  'ftp' => true,
  'nntp' => true,
  'news' => true
)
--DESCRIP

but it still keep purifying it.

(I even installed the ckeditor plugin pastebase64)

Can someone help me with this?

thanks

Link to comment
Share on other sites

Re,

ok I ended up modifying the content of a CKEditor field before il gets saved.

I made a module with autoload=true and hooked it after the input field is processed.

the module will detect images that are from the web or encoded in base64 (begining with http, ftp and data) and fetch it , copy it to images field and replace the link with the new uploaded image link.

 public function init() {
		// add before-hook to the inputfield render method
		$this->addHookAfter("InputfieldCKEditor::processInput", $this, "downloadPastedImages");
	}

    public function downloadPastedImages(HookEvent $event) {
	
        // // get the current field
        $field = $event->object;
		
        if($field->name === 'projet_body' && $field->value !== '' ) {
			$page = $this->modules->ProcessPageEdit->getPage();
			
			
			// ===== custom class
            $manipulator = new textManipulator;	
			
			 // custom function of the class to get images src into a array
            $images = $manipulator->getImagesArray($field->value);
			
			// vérifie si c'est un lien image qui vient du web avec http en début
			$images_fromweb = array();
			foreach($images as $key=>$image){
				if(substr( $image, 0, 4 ) === "http" || substr( $image, 0, 4 ) === "data" || substr( $image, 0, 3 ) === "ftp") $images_fromweb[] = $image;
			}
			
			$filename = $page->name."_image_";
		
			// si on a des images du web non copié encore => on efface tout pour copier
			if(count($images_fromweb) > 0){
				// efface toute les images
				// autre technique : 
				$page->images->removeAll();
				$page->save();
			}
			
			//wire('pages')->uncache($page); // just in case we need the memory
			
            foreach($images_fromweb as $key=>$image){            
				// save images if not in local server
                $page->setOutputFormatting(false);
				
				
				
				// copie de l'image vers le dossier de la page (sans extension encore)
				$file = $page->images->path . $filename.$key;
				$file_url = $page->images->url . $filename.$key;
				$temp_image = file_get_contents($image);
				file_put_contents($file, $temp_image);
				
				// detecte l'extension				
				$ext = ".png";
				if(mime_content_type($file) === "image/gif") {
					$ext = ".gif";
				} else if(mime_content_type($file) === "image/jpeg") {
					$ext = ".jpg";
				} else if(mime_content_type($file) === "image/bmp") {
					$ext = ".bmp";
				}else if(mime_content_type($file) === "image/svg+xml") {
					$ext = ".svg";
				}								
				// ajoute l'extension
				rename($file,$file.$ext);
				
				// remplace le chemin de l'image à présent copiée dans le texte source
				$field->value = str_replace($image, $file_url.$ext, $field->value);
				
				// add to page 
				$page->images->add($file.$ext);
				
				// save the page
				$page->save();
			}
			
			
        }
    }
  • Like 1
Link to comment
Share on other sites

Re again,

apparently I have a problem with simplehtmldom that I use to get the src of images

and it won't get the src of base64 encoded images anymore...weirdly..

but when it worked when posting lots of base64 encoded images, I would have a mysql error

I guess the size of the field is too big.

Does anyone know how I can process the input before the page is saved to the database?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...