Espo
Espo

Reputation: 41939

How do I change image upload path *dynamically* in FCKeditor

I'm using the ASP.NET binary for my FCKeditor, and needs to insert two editors on the same page. The uploaded images/browsing needs to go to two different directories, how do I do this from code-behind?

I know the path for uploaded files are set in the config.ascx-file with the UserFilesPath setting, but I can not find a way to override this value from my aspx.cs file.

Also, I found (conflicting) documentation stating that Session["FCKeditor:UserFilesPath"] could be set, but I dont like putting usercontrol-specific information in a global session variable.

Upvotes: 6

Views: 6579

Answers (5)

user2827246
user2827246

Reputation: 11

Full Subject: FCK editor 2.x: File/image/video upload in different folders for different applications using a single FCKeditor, by making $Config['UserFilesPath'] fully dynamic in a secure way

It can be done in many ways. I am explaining a process, which I applied as per my php applications' code structure. The same code structure/framework I followed for different applications, with each application as a sub-folder in my server. So, there is a logical need to use one single FCKeditor and configure it in some way, so that it work properly for all the applications. The content part of FCKeditor is ok. It can easily be reused by different applications or projects from a single FCKeditor component. But the problem arises with file upload, like image, video or any other document. To make it applicable for different project, the files must be uploaded in separe folders for different projects. And for that $Config['UserFilesPath'] must by configured with dynamic folder path, means different folder path for each project, but calling the the same FCKeditor component in the same location. I am explaning some differnt process together in a step-by-step way. Those worked for me fine with FCKeditor version 2.5.1 and VersionBuild 17566 and I hope they will work for others as well. If it does not work for other developrs, then may be they need to make some tweaks in those process as per their project code structure and folder write permission as well as per the FCKeditor version.

1) In fckeditor\editor\filemanager\connectors\phpconfig.php file

a) Go after global $Config ; and $Config['Enabled'] = false ; i) There, if want a session dependent secure method: only for single site setting: i.e. one FCKeditor for each one project domain or subdomain, not one FCKeditor for multiple project then place this code:

if(!isset($_SESSION)){
session_start(); 
}

if(isset($_SESSION['SESSION_SERVER_RELATIVEPATH']) && $_SESSION['SESSION_SERVER_RELATIVEPATH']!="") { 
$relative_path=$_SESSION['SESSION_SERVER_RELATIVEPATH']; 
include_once($_SERVER['DOCUMENT_ROOT'].$relative_path."configurations/configuration.php");
}

N.B.: Here, $_SESSION['SESSION_SERVER_RELATIVEPATH']: relative folder path of the project corresponding to the webroot; should be like "/project/folder/path/" and set this session variable in a common file in your project where the session started. And there should be a configurations/configuration.php as the configuration file in your project. If it's name or path is different you have to place the corresponding path here instead of configurations/configuration.php

ii) If want to use a single FCKeditor component for different projects represented as different sub-folders and with a session dependent secure way (Assuming different session_name for different projects, to differentiate their sessions in a single server). But it will not work if projects represented as sub-domains or different domains, then have to use the session independent way (iii) provided bellow (though it is insecure). Place this code:

if(!isset($_SESSION)){
session_name($_REQUEST['param_project_to_fck']); 
session_start(); 
}

if(isset($_SESSION['SESSION_SERVER_RELATIVEPATH']) && $_SESSION['SESSION_SERVER_RELATIVEPATH']!="") { 
$relative_path=$_SESSION['SESSION_SERVER_RELATIVEPATH']; 
include_once($_SERVER['DOCUMENT_ROOT'].$relative_path."configurations/configuration.php");
}

Please read N.B. at the end of previous point, i.e. point (i)

iii) If want to use a single FCKeditor component for different projects represented either different sub-folders as well as sub-domains or domains (though it is not fully secure). Place this code:

if(isset($_REQUEST['param_project_to_fck']) && $_REQUEST['param_project_to_fck']!=""){ //base64 encoded relative folder path of the project corresponding to the webroot; should be like "/project/folder/path/" before encoding 
$relative_path=base64_decode($_REQUEST['param_project_to_fck']);
include_once($_SERVER['DOCUMENT_ROOT'].$relative_path."configurations/configuration.php");
}

Please read N.B. at the end of point (i)

b)Now after that for any case you selected, please find this code:

// Path to user files relative to the document root.
$Config['UserFilesPath'] = '/userfiles/' ;

and replace the following code:

if(isset($SERVER_RELATIVEPATH) &&  $SERVER_RELATIVEPATH==$relative_path) { //to make it relatively secure so that hackers can not create any upload folder automatcally in the server, using a direct link and can not upload files there 
$Config['Enabled'] = true ;
$file_upload_relative_path=$SERVER_RELATIVEPATH;
}else{
$Config['Enabled'] = false ;
exit();
}
// Path to user files relative to the document root.
//$Config['UserFilesPath'] = '/userfiles/' ;
//$Config['UserFilesPath'] = $file_upload_relative_path.'userfiles/' ;
$Config['UserFilesPath'] = '/userfiles'.$file_upload_relative_path;

Here $SERVER_RELATIVEPATH is the relative path and it must be set in your project's configuration file included previously.

Here you can set the $Config['UserFilesPath'] with any other dynamic folder path using $file_upload_relative_path variable.In my bluehost linux server, as their was a folder user permission conflict between the project root folder (0755 permission) and the userfiles folder under it and subfolders under userfiles (should be 0777 as per FCKeditor coding), so it does not allow uploading files in those folders. So, I created a folder userfiles at the server webroot (beyond the project root folder), and set the permission to 0777 to it, use the code for the $config setting as :

$Config['UserFilesPath'] = '/userfiles'.$file_upload_relative_path; 

But, if you have no problem with write permission in the project's subfolders in your case, then you can use the previous line (commented out in the previous code segment):

$Config['UserFilesPath'] = $file_upload_relative_path.'userfiles/' ;

Mind it, you mast comment out the existing $Config['UserFilesPath'] = '/userfiles/' ; in this file by either replacing or simply commenting out if it exist in other place of the file.

2) If you choose 1) (a) (ii) or (iii) method then open
(a) fckeditor\editor\filemanager\browser\default\browser.html file.

Search for this line: var sConnUrl = GetUrlParam( 'Connector' ) ;

Put these commands after that line:

var param_project_to_fck = GetUrlParam( 'param_project_to_fck' ) ; 

Now, Search for this line: sUrl += '&CurrentFolder=' + encodeURIComponent( this.CurrentFolder ) ;

Put this command after that line:

sUrl += '&param_project_to_fck=' + param_project_to_fck ; 

(b) Now, open ckeditor\editor\filemanager\browser\default\frmupload.html file.

Search for this line (it should be in the SetCurrentFolder() function):

sUrl += '&CurrentFolder=' + encodeURIComponent( folderPath ) ;

Put this command after that line:

sUrl += '&param_project_to_fck='+window.parent.param_project_to_fck;

3) Now where you want to show the FCKeditor in your project, you have to put those lines first in the corresponding php file/page:

include_once(Absolute/Folder/path/for/FCKeditor/."fckeditor/fckeditor.php") ; 
$oFCKeditor = new FCKeditor(Field_name_for_editor_content_area) ;
$oFCKeditor->BasePath = http_full_path_for_FCKeditor_location.'fckeditor/' ;
$oFCKeditor->Height = 400;
$oFCKeditor->Width = 600;
$oFCKeditor->Value =Your_desired_content_to_show_in_editor;
$oFCKeditor->Create() ; 

a) Now, if you choose 1) (a) (ii) or (iii) method then place the following code segment before that line: $oFCKeditor->Create() ;

$oFCKeditor->Config["LinkBrowserURL"] = ($oFCKeditor->BasePath)."editor/filemanager/browser/default/browser.html?Connector=../../connectors/php/connector.php&param_project_to_fck=".base64_encode($SERVER_RELATIVEPATH);
$oFCKeditor->Config["ImageBrowserURL"] = ($oFCKeditor->BasePath)."editor/filemanager/browser/default/browser.html?Type=Image&Connector=../../connectors/php/connector.php&param_project_to_fck=".base64_encode($SERVER_RELATIVEPATH);
$oFCKeditor->Config["FlashBrowserURL"] = ($oFCKeditor->BasePath)."editor/filemanager/browser/default/browser.html?Type=Flash&Connector=../../connectors/php/connector.php&param_project_to_fck=".base64_encode($SERVER_RELATIVEPATH);

b) if you chose 1) (a) (ii) method, then in the above code code segment, just replace all the texts: base64_encode($SERVER_RELATIVEPATH) with this one: base64_encode(session_name())

And you are done.

Upvotes: 0

D.J
D.J

Reputation: 2534

first you need assign User Identity information into Session["UserInfo"]

then go to [fckeditor root folder]/filemanager/connector/aspx/config.ascx

string Userfolder = Session["UserInfo"].ToString(); // URL path to user files. UserFilesPath = "~/Upload/" + Userfolder;

Upvotes: 1

Bill Bingham
Bill Bingham

Reputation: 244

Sorry, I think your best bet is to stop using the control, and use the javascript api instead.

Upvotes: 0

Sebastien Lachance
Sebastien Lachance

Reputation: 2007

This will probably works. At least it worked for me.

Session["FCKeditor:UserFilesPath"] = "~/images/";

Upvotes: 0

TheVillageIdiot
TheVillageIdiot

Reputation: 40527

Oh dear, after much struggle only thing I'm able to get is:

fckEditor1.Config

property. Try setting it for the editor you want to configure:

fckEditor1.Config["UserFilesPath"]="your path"

Upvotes: 0

Related Questions