Customize CKFinder paths dynamically with JS, can it be done?

柔情痞子 提交于 2019-12-25 03:24:48

问题


I'm doing an administrative function to work with CKEditor v4.x and need a file/folder view, upload and selection tool. For the time being I'm using CKFinder as I'd like to avoid writing a complete plugin by myself. However for the purpose I need to be able to switch baseDir and baseUrl dynamically.

I tried older code examples like

CKFinder.setupCKEditor( 
          editor, 
          { 
                 basePath: '/ckfinder/', 
                 baseUrl: 'http://www.example.com/mydirectory/', 
                 baseDir: '/mydirectory/' 
           } 
);

But this doesn't work. Apparently you need to set the paths by PHP (server side). As I'm having many CKEditor instances on one page, generated dynamically, and all should use different CKFinder paths it is a great deal of work if I need to change the path asynchronously through AJAX calls... I can of course see the security considerations by letting client side code control baseDir. For the record this application, and CKFinder, is only available after login by administrative people.


回答1:


In our CMS area we use CKFinder.setupCKEditor() and I was unable to pass variables or flags to the config file correctly.

So I simply went to where the 'IsAuthorized' flag is set (that allows access to use CKFinder in their CheckAuthentication() function), and I set two more session variables: 'ckfinder_baseDir' and 'ckfinder_baseUrl'.

*Note that I have a Config class that checks the enviroment, hence Config::isDev(). You can check it any way that makes sense for you.

$_SESSION['IsAuthorized'] = 1;
$_SESSION['ckfinder_baseUrl'] = Config::isDev() ? 'http://devurl.com/' : 'http://produrl.com';
$_SESSION['ckfinder_baseUrl'] = Config::isDev() ? '/path/to/dev/uploads/' : 'path/to/prod/uploads';

Then I simply use these flags when in the CKFinder config.php file.

$baseUrl = $_SESSION['ckfinder_baseUrl'];
$baseDir = $_SESSION['ckfinder_baseDir'];



回答2:


You can specify the parameter on the frontend side in the definition of the filebrowser button as following

{
  type: 'button',
  label:'Button for filebrowser',
  filebrowser: {
      action: 'Browse',
      params: {
      'id': '{someID}'
      }
 },



回答3:


With help from the discussion Customize baseUrl and baseDir in CKFinder I got close to the answer with Travis comment.

There is a way to call different server side settings for each instance of CKFinder by just using a GET parameter in the path to CKFinder. I set the id of the filebrowserpath

filebrowserBrowseUrl: '/ckfinder/ckfinder.html?id=testdir'

And then in the config.php:

if ($_GET['id'] && $_GET['id'] == "testdir") {
   $baseDir = $baseDir . 'testdir/';
   $baseUrl = $baseUrl . 'testdir/';
}

This way each instance of CKeditor can use different basePath and baseUrl settings, and also other specific config.




回答4:


in config.ascx in setConfig() method

    string userName = string.Empty;
    if (HttpContext.Current != null)
        userName = HttpContext.Current.User.Identity.Name;
    else
    throw new Exception("User Name is not provided");
    BaseUrl = "~/Uploads/Users/" + userName + "/";


来源:https://stackoverflow.com/questions/24159701/customize-ckfinder-paths-dynamically-with-js-can-it-be-done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!