Search code examples
phplaravelmodeltinymcestrip

Laravel Model Removing html attribute


Created a simple miniCMS in a portal for content creation. The issue at first was in TinyMCE stripping of id attribute from html tag I've resolved that using valid_elements now the request is being sent to Model as is with no issues however in the Model level it's stripping the id again

Example

<div id="agreement">text ......... </div>

Being Saved in model as

<div>text ......... </div>

The controller code:

public function frontendContent(Request $request, $key)
{
    $purifier = new \HTMLPurifier();
    $valInputs = $request->except('_token', 'image_input', 'key', 'status', 'type');
    foreach ($valInputs as $keyName => $input) {
        if (gettype($input) == 'array') {
            $inputContentValue[$keyName] = $input;
            continue;
        }
        $inputContentValue[$keyName] = $purifier->purify($input);
    }
    $type = $request->type;
    if (!$type) {
        abort(404);
    }
    $imgJson = @getPageSections()->$key->$type->images;
    $validation_rule = [];
    $validation_message = [];
    foreach ($request->except('_token', 'video') as $input_field => $val) {
        if ($input_field == 'has_image' && $imgJson) {
            foreach ($imgJson as $imgValKey => $imgJsonVal) {
                $validation_rule['image_input.'.$imgValKey] = ['nullable','image','mimes:jpeg,jpg,png,svg'];
                $validation_message['image_input.'.$imgValKey.'.image'] = inputTitle($imgValKey).' must be an image';
                $validation_message['image_input.'.$imgValKey.'.mimes'] = inputTitle($imgValKey).' file type not supported';
            }
            continue;
        }elseif($input_field == 'seo_image'){
            $validation_rule['image_input'] = ['nullable', 'image', new FileTypeValidate(['jpeg', 'jpg', 'png'])];
            continue;
        }
        $validation_rule[$input_field] = 'required';
    }
    $request->validate($validation_rule, $validation_message, ['image_input' => 'image']);
    if ($request->id) {
        $content = Frontend::findOrFail($request->id);
    } else {
        $content = Frontend::where('data_keys', $key . '.' . $request->type)->first();
        if (!$content || $request->type == 'element') {
            $content = Frontend::create(['data_keys' => $key . '.' . $request->type]);
        }
    }
    if ($type == 'data') {
        $inputContentValue['image'] = @$content->data_values->image;
        if ($request->hasFile('image_input')) {
            try {
                $inputContentValue['image'] = uploadImage($request->image_input,imagePath()['seo']['path'], imagePath()['seo']['size'], @$content->data_values->image);
            } catch (\Exception $exp) {
                $notify[] = ['error', 'Could not upload the Image.'];
                return back()->withNotify($notify);
            }
        }
    }else{
        if ($imgJson) {
            foreach ($imgJson as $imgKey => $imgValue) {
                $imgData = @$request->image_input[$imgKey];
                if (is_file($imgData)) {
                    try {
                        $inputContentValue[$imgKey] = $this->storeImage($imgJson,$type,$key,$imgData,$imgKey,@$content->data_values->$imgKey);
                    } catch (\Exception $exp) {
                        $notify[] = ['error', 'Could not upload the Image.'];
                        return back()->withNotify($notify);
                    }
                } else if (isset($content->data_values->$imgKey)) {
                    $inputContentValue[$imgKey] = $content->data_values->$imgKey;
                }
            }
        }
    }
    $content->update(['data_values' => $inputContentValue]);
    $notify[] = ['success', 'Content has been updated.'];
    return back()->withNotify($notify);
}

When I dd the request

as dd($request) I can see the html tag in full

<div id="agreement">text ......... </div>

But when I dd the content

as dd($content) I can see that the id attribute is stripped

<div>text ......... </div>

The model part

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Frontend extends Model
{
    protected $guarded = ['id'];

    protected $table = "frontends";
    protected $casts = [
        'data_values' => 'object'
    ];

    public static function scopeGetContent($data_keys)
    {
        return Frontend::where('data_keys', $data_keys);
    }
}

Kindly asking for help, thank you!


Solution

  • While checking the forum here at SOF I found a solution with a remark from @FarhanIbnWahid thanks to him.

    $config     = HTMLPurifier_Config::createDefault();
    $config->set('Attr.EnableID', true);
    $purifier   = new \HTMLPurifier($config);
    

    This will resolve the issue.