jodit/connector

Jodit 文件浏览器和上传连接器


README

官方 Jodit WYSIWYG PHP 连接器

旧版本(适用于 Jodit 2.x)

安装

composer create-project --no-dev jodit/connector

或下载 ZIP 压缩包

配置

可用选项

  • $config['saveSameFileNameStrategy'] = "addNumber" - 如果上传的文件与服务器上的文件同名时的策略。
    • "addNumber" - 将数字添加到文件名中,例如 "olsen.png" => "olsen(1).png",如果此类文件存在,则变为 "olsen(2).png" 等。
    • "replace" - 直接替换文件
    • "error" - 抛出错误 - "文件已存在"
  • $config['quality'] = 90 - 图像质量
  • $config['datetimeFormat'] = 'd/m/Y' - 日期格式
  • $config['root'] = __DIR__ - 用户文件的基本目录
  • $config['baseurl'] = ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . '/' - 用户文件的基本 URL(例如 http://xdsoft.net
  • $config['createThumb'] = true - 布尔值,true - 创建预览缩略图(true
  • $config['safeThumbsCountInOneTime'] = 20 - 整数,如果启用 createThumb 选项,那么当文件夹中有大量文件时,服务器在生成这么多预览时将明显变慢。因此,一次只处理这么多张图片。
  • $config['thumbFolderName'] = '_thumbs' - 缩略图文件夹
  • $config['excludeDirectoryNames'] = ['.tmb', '.quarantine'], - 排除这些文件夹
  • $config['extensions'] = ['jpg', 'png', 'gif', 'jpeg'] - 允许加载的有效文件扩展名数组(['jpg', 'png', 'gif', 'jpeg']
  • $config['maxFileSize'] = 8mb - 最大文件大小(0 - 无限制)默认 8Mb
  • $config['allowCrossOrigin'] = false - 允许跨源请求
  • $config['allowReplaceSourceFile'] = true - 允许替换缩放或裁剪后的源图像
  • $config['sources'] - 选项数组
  • $config['accessControl'] - 检查允许/拒绝权限的数组 阅读更多
  • $config['defaultRole']="guest" - 访问控制 的默认角色
  • $config['roleSessionVar']="JoditUserRole" - Jodit 连接器将用于检查当前用户角色的会话键名。 阅读更多

可以定义多个源,并覆盖一些选项

$config['sources'] = [
    'images' => [
        'root' => __DIR__ . '/images',
        'baseurl' => 'http://xdsoft.net/images',
        'maxFileSize' => '100kb',
        'createThumb' => false,
        'extensions' => ['jpg'],
    ]
];

如何与 Jodit 一起使用

文件浏览器设置 详细选项

new Jodit('#editor', {
    filebrowser: {
        ajax: {
            url: 'connector/index.php'
        }
    }
});

和上传选项 默认选项

new Jodit('#editor', {
    uploader: {
        url: 'connector/index.php?action=fileUpload',
    }
});

自定义配置

更改 config.php

不要修改默认的 default.config.php 文件,而是覆盖 config.php 文件中的设置

return [
    'sources' => [
        'joomla Images' => [
            'root' => JPATH_BASE.'/images/',
            'baseurl' => '/images/',
            'createThumb' => true,
            'thumbFolderName' => '_thumbs',
            'extensions' => array('jpg', 'png', 'gif', 'jpeg'),
        ],
        'joomla Media' => [
            'root' => JPATH_BASE.'/media/',
            'baseurl' => '/medias/',
            'createThumb' => false,
            'thumbFolderName' => '_thumbs',
            'extensions' => array('jpg', 'png', 'gif', 'jpeg'),
        ],
    ]
];

身份验证

connector/Application.php 中更改 connector/checkAuthentication

例如

function checkAuthentication () {
    /********************************************************************************/
    if (empty($_SESSION['filebrowser'])) {
        throw new \ErrorException('You do not have permission to view this directory', 403);
    }
    /********************************************************************************/
}

示例:集成到 Joomla

修改 Application.php

<?php
define('_JEXEC', 1);
define('JPATH_BASE', realpath(realpath(__DIR__).'/../../../../../')); // replace to valid path

require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

JFactory::getApplication('site');


class JoditRestApplication extends \jodit\JoditApplication {
    function checkAuthentication() {
        $user = JFactory::getUser();
        if (!$user->id) {
            trigger_error('You are not authorized!', E_USER_WARNING);
        }
    }
}

您可以使用 $action 来允许或拒绝访问

function checkPermissions () {
    /********************************************************************************/
    if (!empty($_SESSION['filebrowser'])) {
        switch ($this->action) {
        case "imageResize":
        case "fileMove":
        case "folderCreate":
        case "fileRemove":
        case "fileUploadRemote":
        case "fileUpload":
            throw new \ErrorException('You do not have permission to view this action', 403);
        }
        return true;
    }
    
    throw new \ErrorException('You do not have permission to view this directory', 403);
    /********************************************************************************/
}

但最好使用 AllowControl 选项

访问控制

roleSessionVar - Jodit 连接器将用于检查当前用户角色的会话键名。

$config['roleSessionVar'] = 'JoditUserRole';

之后,您就可以在脚本中使用 $_SESSION['JoditUserRole'] 来设置用户角色,用户认证后即可使用

在您的脚本中的某个地方

session_start();
//...
$_SESSION['JoditUserRole'] = 'administrator';

default.config.php 中,您可以找到默认的 ACL 配置

$config['roleSessionVar'] = 'JoditUserRole';

$config['accessControl'][] = array(
	'role'                => '*',
	'extensions'          => '*',
	'path'                => '/',
	'FILES'               => true,
	'FILE_MOVE'           => true,
	'FILE_UPLOAD'         => true,
	'FILE_UPLOAD_REMOTE'  => true,
	'FILE_REMOVE'         => true,
	'FILE_RENAME'         => true,

	'FOLDERS'             => true,
	'FOLDER_MOVE'         => true,
	'FOLDER_REMOVE'       => true,
	'FOLDER_RENAME'       => true,

	'IMAGE_RESIZE'        => true,
	'IMAGE_CROP'          => true,
);

$config['accessControl'][] = array(
	'role'                => '*',
   
	'extensions'          => 'exe,bat,com,sh,swf',

	'FILE_MOVE'           => false,
	'FILE_UPLOAD'         => false,
	'FILE_UPLOAD_REMOTE'  => false,
	'FILE_RENAME'         => false,
);

这意味着所有已认证的用户都将拥有所有权限,但他们不允许下载可执行文件。

config.php 中,您可以对其进行自定义。例如,为所有用户设置只读权限,但为具有 administrator 角色的用户提供完全访问权限

$config['accessControl'][] = Array(
 'role'                => '*',

 'FILES'               => false,
 'FILE_MOVE'           => false,
 'FILE_UPLOAD'         => false,
 'FILE_UPLOAD_REMOTE'  => false,
 'FILE_REMOVE'         => false,
 'FILE_RENAME'         => false,
 
 'FOLDERS'             => false,
 'FOLDER_MOVE'         => false,
 'FOLDER_REMOVE'       => false,
 'FOLDER_RENAME'       => false,
 
 'IMAGE_RESIZE'        => false,
 'IMAGE_CROP'          => false,
);

$config['accessControl'][] = Array(
 'role' => 'administrator',
 'FILES'               => true,
 'FILE_MOVE'           => true,
 'FILE_UPLOAD'         => true,
 'FILE_UPLOAD_REMOTE'  => true,
 'FILE_REMOVE'         => true,
 'FILE_RENAME'         => true,
 
 'FOLDERS'             => true,
 'FOLDER_MOVE'         => true,
 'FOLDER_REMOVE'       => true,
 'FOLDER_RENAME'       => true,
 
 'IMAGE_RESIZE'        => true,
 'IMAGE_CROP'          => true,
);

API

所有操作均区分大小写

操作

权限 - 获取当前路径的权限。此操作应在每次更改路径后调用。

GET index.php?action=permission&source=:source&path=:path
  • [:source=default] - 配置中的键(例如,来自 Joomla 配置的 `joomla Media`)
  • [:path=source.root] - source.root 的相对路径

tests/api/PermissionsCept.php

JSON 示例回答

{
    "success": true,
    "time": "2018-03-05 10:14:44",
    "data": {
        "permissions": {
            "allowFiles": true,
            "allowFileMove": true,
            "allowFileUpload": true,
            "allowFileUploadRemote": true,
            "allowFileRemove": true,
            "allowFileRename": true,
            "allowFolders": true,
            "allowFolderMove": true,
            "allowFolderCreate": true,
            "allowFolderRemove": true,
            "allowFolderRename": true,
            "allowImageResize": true,
            "allowImageCrop": true
        },
        "code": 220
    }
}

文件 - 获取文件夹中的所有文件

GET index.php?action=files&source=:source&path=:path
  • [:source=default] - 配置中的键(例如,来自 Joomla 配置的 `joomla Media`)
  • [:path=source.root] - source.root 的相对路径

tests/api/getAllFilesByAllSourcesCept.phptests/api/getAllFilesByOneSourceCept.php

JSON 示例回答

{
    "success": true,
    "time": "2017-07-10 17:10:26",
    "data": {
        "sources": {
            "test": {
                "baseurl": "https://:8181/tests/files/",
                "path": "",
                "files": [
                    {
                        "file": "artio.jpg",
                        "thumb": "_thumbs\\artio.jpg",
                        "changed": "07/07/2017 3:06 PM",
                        "size": "53.50kB"
                    }
                ]
            },
            "folder1": {
                "baseurl": "https://:8181/tests/files/folder1/",
                "path": "",
                "files": [
                    {
                        "file": "artio2.jpg",
                        "thumb": "_thumbs\\artio2.jpg",
                        "changed": "07/07/2017 3:06 PM",
                        "size": "53.50kB"
                    }
                ]
            }
        },
        "code": 220
    }
}

文件夹 - 获取路径中的所有文件夹

GET index.php?action=folders&source=:source&path=:path
  • [:source=default] - 配置中的键(例如,来自 Joomla 配置的 `joomla Media`)
  • [:path=source.root] - source.root 的相对路径。

tests/api/getAllFoldersByAllSourcesCept.phptests/api/getAllFoldersByOneSourceCept.php

JSON 示例回答

{
    "success": true,
    "time": "2017-07-10 17:11:10",
    "data": {
        "sources": {
            "test": {
                "baseurl": "https://:8181/tests/files/",
                "path": "",
                "folders": [
                    ".",
                    "folder1"
                ]
            },
            "folder1": {
                "baseurl": "https://:8181/tests/files/folder1/",
                "path": "",
                "folders": [
                    ".",
                    "folder2"
                ]
            }
        },
        "code": 220
    }
}

fileUploadRemote - 从另一个服务器下载图像

GET index.php?action=fileUploadRemote&source=:source&path=:path&url=:url
  • [:source=default] - 配置中的键(例如,来自 Joomla 配置的 `joomla Media`)
  • [:path=source.root] - source.root 的相对路径。
  • :url - 完整的图像 URL

tests/api/uploadImageByUrlToServerCept.php

JSON 示例回答

{
    "success": true,
    "time": "2017-07-10 17:13:49",
    "data": {
        "newfilename": "icon-joomla.png",
        "baseurl": "https://:8181/tests/files/",
        "code": 220
    }
}

fileUpload - 将文件上传到服务器

POST index.php
$_POST = [
    action=fileUpload,
    source=:source,
    path=:path,
]
$_FILES = [
    files=[...]
]
  • [:source=default] - 配置中的键(例如,来自 Joomla 配置的 `joomla Media`)
  • [:path=source.root] - source.root 的相对路径。
  • :files - 文件

tests/api/uploadImageToServerCept.php

fileRemove - 删除文件

GET index.php?action=fileRemove&source=:source&path=:path&name=:name
  • [:source=default] - 配置中的键(例如,来自 Joomla 配置的 `joomla Media`)
  • [:path=source.root] - source.root 的相对路径。
  • :name - 文件名或文件夹名

tests/api/removeImageFromServerCept.php

folderRemove - 从服务器删除文件夹

GET index.php?action=folderRemove&source=:source&path=:path&name=:name
  • [:source=default] - 配置中的键(例如,来自 Joomla 配置的 `joomla Media`)
  • [:path=source.root] - source.root 的相对路径。
  • :name - 文件名或文件夹名

folderCreate - 在服务器上创建文件夹

GET index.php?action=folderCreate&source=:source&path=:path&name=:name
  • [:source=default] - 配置中的键(例如,来自 Joomla 配置的 `joomla Media`)
  • [:path=source.root] - source.root 的相对路径。
  • :name - 新文件夹名称

tests/api/createFolderCept.php

folderMove - 将文件夹移动到另一个位置

GET index.php?action=folderMove&source=:source&path=:path&from=:from
  • [:source=default] - 配置中的键(例如,来自 Joomla 配置的 `joomla Media`)
  • [:path=source.root] - source.root 的相对路径。这是文件将被移动的位置
  • :from - 相对路径(来自 source.root)文件或文件夹

tests/api/moveFileCept.php

fileMove - 将文件移动到另一个位置

GET index.php?action=fileMove&source=:source&path=:path&from=:from
  • [:source=default] - 配置中的键(例如,来自 Joomla 配置的 `joomla Media`)
  • [:path=source.root] - source.root 的相对路径。这是文件将被移动的位置
  • :from - 相对路径(来自 source.root)文件或文件夹

tests/api/moveFileCept.php

imageResize - 调整图像大小

GET index.php?action=imageResize&source=:source&path=:path&name=:name&box[w]=:box_width&box[h]=:box_height
  • [:source=default] - 配置中的键(例如,来自 Joomla 配置的 `joomla Media`)
  • [:path=source.root] - source.root 的相对路径。
  • :name - :path 中的文件源
  • :newname - :path 中的新文件名。可以等于 :name
  • :box - 新的宽度和高度

tests/api/resizeImageCept.php

imageCrop - 剪裁图像

GET index.php?action=crop&source=:source&path=:path&name=:name&box[w]=:box_width&box[h]=:box_height&box[x]=:box_start_x&box[y]=:box_start_y
  • [:source=default] - 配置中的键(例如,来自 Joomla 配置的 `joomla Media`)
  • [:path=source.root] - source.root 的相对路径。
  • :name - :path 中的文件源
  • :newname - :path 中的新文件名。可以等于 :name
  • :box - 约束框

tests/api/cropImageCept.php

getLocalFileByUrl - 通过 URL 获取本地文件

GET index.php?action=getlocalfilebyurl&source=:source&path=:path&url=:url
  • [:source=default] - 配置中的键(例如,来自 Joomla 配置的 `joomla Media`)
  • [:path=source.root] - source.root 的相对路径。
  • :url - 源的完整文件 URL

tests/api/getlocalFileByURLCept.php 示例

index.php?action=getLocalFileByUrl&source=test&url=https://:8181/tests/files/artio.jpg

JSON 示例回答

{
    "success": true,
    "time": "2017-07-10 17:34:29",
    "data": {
        "path": "",
        "name": "artio.jpg",
        "code": 220
    }
}

路线图

  • 跨源请求
  • 添加分页
  • 创建 FTP/SFTP 源
  • 创建图像过滤器(噪声、灰度等)

联系方式