Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 4 sessions for external use
#1

(This post was last modified: 10-07-2023, 08:07 AM by xsPurX.)

I am trying to intergrate KCfinder into CKeditor, and my session data is'nt accessable from kcfinders end. This used to work in CI2 and CI3. 
For example I am loading this

PHP Code:
$ckeditor_basepath '/assets/js/ckeditor/';
require_once( 
$_SERVER["DOCUMENT_ROOT"] . $ckeditor_basepath'ckeditor.php' );
//if data is an array then extract name else instanceName is $data
$instanceName = ( is_array($data) && isset($data['name'])  ) ? $data['name'] : $data;
$ckeditor = new CKEditor();
$ckeditor->Value html_entity_decode($value);
$initialValue $value;
$ckeditor->basePath $ckeditor_basepath;
    //session_start();
  // $_SESSION['upload_image_file_manager'] = TRUE;
  //session_start();
    $_SESSION['KCFINDER'] = array(); 
    $_SESSION['KCFINDER']['disabled'] = false;

//todo
if( is_array($data) )
{
}
$config['extraPlugins'] = 'youtube,justify,showblocks,div';
$config['toolbar'] = array(
array(
'Format''Styles''Source''Maximize''ShowBlocks''Preview'),
array(
'Cut''Copy''Paste''PasteText''PasteFromWord''-''Undo''Redo'),
array(
'Find''Replace''SelectAll''SpellChecker''Scayt'),
array(
'Bold''Italic''Underline''Strike''RemoveFormat'),
array(
'NumberedList''BulletedList''Blockquote''CreateDiv''JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'),
array(
'Link''Unlink''Anchor'),
array(
'Image','Flash''Table''HorizontalRule''Smiley''SpecialChar','Iframe','Youtube')
);
    //print_r($_SESSION);
return $ckeditor->editor($instanceName$initialValue$config); 

The value is not being sent over to kcfinder. It seems to create it's own session which doesn't enable kcfinder as I have the config of disabled = true. I am trying to change that to false, but the session information is not passing..
Any ideas?
Thasnks,
\\\\
Reply
#2

I decided I ddin't really now need the browser just the upload functionality. So I wrote an uploader controller to handle the requests, that way I can use ckeditor with upload option.
Below is the uploader controller

PHP Code:
<?php
namespace App\Controllers;
use 
CodeIgniter\Files\File;

class 
Uploader extends BaseController
{

    protected $helpers = ['form'];

    public function __construct()
        {
        }

    function index()
        {
        $validation = \Config\Services::validation();
        $rules = [
                'upload' => [
                    'label' => 'Image File',
                    'rules' => [
                        'is_image[upload]',
                        'mime_in[upload,image/jpg,image/jpeg,image/gif,image/png,image/webp]',
                        'max_size[upload,500000]',
                        'max_dims[upload,500000,500000]',
                    ],
                ],
        ];
        if (! $this->validate($rules))
            {
            $request = \Config\Services::request();
            echo "error";
            }
        else
            {
            $request = \Config\Services::request();
            $img $this->request->getFile('upload');
                if (! $img->hasMoved()) 
                    {
                    $filepath =  $img->store(config('WebsiteConfig')->upload_folder);

                    $userfile = ['uploaded_fileinfo' => new File($filepath)];
                    $file_name explode ('/'$userfile['uploaded_fileinfo']);
                    $image = \Config\Services::image();
                        try {
                            $image->withFile($_SERVER["DOCUMENT_ROOT"] . config('WebsiteConfig')->thumb_path $img->getName())
                                ->resize(551300)
                                ->save($_SERVER["DOCUMENT_ROOT"] . config('WebsiteConfig')->thumb_path'thumbs/' $img->getName());
                        } catch (CodeIgniter\Images\Exceptions\ImageException $e) {
                            echo $e->getMessage();
                        }
                    $function_number $_GET['CKEditorFuncNum'];
                    $message '';
                    $url '/assets/uploads/'$img->getName();
                    echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($function_number, '$url', '$message')</script>";
                    }
                }  
            
}
        

This uploads to the /assets/uploads folder. So change it if you want something else.

Once that is done you just need to change ckeditor config like below.
PHP Code:
$config['filebrowserImageUploadUrl'] = '/uploader'

And lastly but most important if you are using an auth system. You will want to protect the uploader maybe? So you can define filter using Shield.

Hope someone finds this helpful. Smile
Reply
#3

(This post was last modified: 11-21-2023, 12:19 AM by ThanogHamros.)

The issue is that KCfinder uses its own session, and it does not share the session with CodeIgniter. To fix this, you need to pass the CodeIgniter session to KCfinder.

https://codeigniter.com/user_guide/echatrandom/libraries/sessions.html?highlight=s

To do this, you can use the following code:

PHP
// Get the CodeIgniter session
$ciSession = session();

// Pass the CodeIgniter session to KCfinder
$_SESSION['KCFINDER']['session'] = $ciSession;
Use code with caution. Learn more
This will ensure that KCfinder uses the same session as CodeIgniter.

Here is an example of how to use the code:

PHP
$ckeditor_basepath = '/assets/js/ckeditor/';
require_once($_SERVER["DOCUMENT_ROOT"] . $ckeditor_basepath . 'ckeditor.php');

// Get the CodeIgniter session
$ciSession = session();

// Pass the CodeIgniter session to KCfinder
$_SESSION['KCFINDER']['session'] = $ciSession;

// Create the CKEditor instance
$ckeditor = new CKEditor();

// Set the CKEditor value
$ckeditor->Value = html_entity_decode($value);

// Set the CKEditor initial value
$initialValue = $value;

// Set the CKEditor base path
$ckeditor->basePath = $ckeditor_basepath;

// Set the CKEditor configuration
$config['extraPlugins'] = 'youtube,justify,showblocks,div';
$config['toolbar'] = array(
    array('Format', 'Styles', 'Source', 'Maximize', 'ShowBlocks', 'Preview'),
    array('Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'),
    array('Find', 'Replace', 'SelectAll', 'SpellChecker', 'Scayt'),
    array('Bold', 'Italic', 'Underline', 'Strike', 'RemoveFormat'),
    array('NumberedList', 'BulletedList', 'Blockquote', 'CreateDiv', 'JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'),
    array('Link', 'Unlink', 'Anchor'),
    array('Image','Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar','Iframe','Youtube')
);

// Return the CKEditor editor
return $ckeditor->editor($instanceName, $initialValue, $config);

This should fix the issue of the session data not being accessible from KCfinder.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB