Ckeditor+Ckfinder+User Based Dynamic Folders Integration With Yii

Hello Yii developpers,

After I spent few days on figuring out how can i integrate ckfinder (user based separate folders) with ckeditor in Yii, i want just to share this with others. I hope this will be helpful for you. I also will be glad if someone propose another solution.

If you do not know where to download ckfinder or ckeditor, just google it. I just want to say that together it is a powerful connection. Also, ckfinder is not free, there are other opportunities (for example kcfinder is free). But i chose it because the free editors are not supported in time.

So,

Starting point: Ckeditor installed already in my webroot folder (which is by the way ‘root/www/’ )

STEP 1.

download CKFinder and install it (you can download it for free and use it in demo version on a development server). I installed it in my webroot folder

STEP 2.

Connect CKEditor with CKFinder.

To do so find config.js file of CKEditor and it looks like this.




CKEDITOR.editorConfig = function( config ) {

// Define changes to default configuration here. For example:

// config.language = 'fr';

// config.uiColor = '#AADC6E';

   config.filebrowserBrowseUrl = '/ckfinder/ckfinder.html';

   config.filebrowserImageBrowseUrl = '/ckfinder/ckfinder.html?type=Images';

   config.filebrowserFlashBrowseUrl = '/ckfinder/ckfinder.html?type=Flash';

   config.filebrowserUploadUrl = '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files';

   config.filebrowserImageUploadUrl = '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images';

   config.filebrowserFlashUploadUrl = '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash';

};



At this point CKEditor works with CKFinder

STEP 3.

Find config.php in CKFinder folder

Here is my config.php where marked what I added so a different folder is created for each different user in the way that it can`t be accessed by chance or by a guess.





////////////////////////////////////////////

/*Added by Oleksiy Roshchyn*/

session_start();

$_uniqueUserAuth=$_SESSION['user_key'];

$_baseDirUser =$_SESSION['user_dir'];

//salt for encoding (Must be the same! in this file)

$_salt = 'ririrituwpwmckeokpkpewfw93478962bk';

/*Added by Oleksiy Roshchyn*/ 

////////////////////////////////////////////




/*

 * ### CKFinder : Configuration File - Basic Instructions

 *

  

 * @return boolean

 */

function CheckAuthentication()

{

       

        ////////////////////////////////////////////

       /*Added by Oleksiy Roshchyn*/

        //we create another Yii instance just for taking the user specific unique key. Here we will take user name

        $yii=dirname(dirname(__FILE__)).'/../yii/framework/yii.php'; //we specify where our yii framework is situated according to this file

$config=dirname(dirname(__FILE__)).'/../../protected/config/main.php'; //we specify where our config file is situated according to this file

require_once($yii);

Yii::createWebApplication($config);

        

        $_uniqueUserAuth=Yii::app()->user->name;

        $_baseDirUser =dirname(dirname(__FILE__)).'/uploads/';

        

        //passing elements to sessions for reuse

        $_SESSION['user_key']= $_uniqueUserAuth;

        $_SESSION['user_dir']= $_baseDirUser;

       

        //salt for encoding (Must be the same! in this file)

        $_salt = 'ririrituwpwmckeokpkpewfw93478962bk';

      

       //User is authenticated and if the folder does not exist we create a new folder with a folder name something like 'ewqweFEWwe4234fwef5435rf54'

      if(!empty($_uniqueUserAuth) &&  !Yii::app()->user->isGuest)

        {

            if(!is_dir( $_baseDirUser.md5($_uniqueUserAuth.$_salt)))

                mkdir( $_baseDirUser.md5($_uniqueUserAuth.$_salt), 0777);

            return true;

        }

        else

        {

            return false;

        }


/*Added by Oleksiy Roshchyn*/ 

        ////////////////////////////////////////////


}


 


 




////////////////////////////////////////

/*Added by Oleksiy Roshchyn*/

$baseUrl = '/uploads/'.md5($_uniqueUserAuth.$_salt).'/';

/*Added by Oleksiy Roshchyn*/

////////////////////////////////////////




 




////////////////////////////////////////

/*Added by Oleksiy Roshchyn*/

$baseDir = $_baseDirUser.md5($_uniqueUserAuth.$_salt).'/';

/*Added by Oleksiy Roshchyn*/

/////////////////////////////////////




/*

 * ### Advanced Settings

 */


.......




After that everything worked fine.

I also understand that there are few repetitions in the code, but somehow global variables did not work. So i fully understand that there is a better way to write this code.

If you have questions, write them in comments, i will answer them.

Just to say that i tried to fully synchronize sessions with Yii sessions, but it only worked for CheckAuthentication().