View source
<?php
define('UEDITOR_INJECT_EXCEPT_LISTED', 0);
define('UEDITOR_INJECT_IF_LISTED', 1);
function ueditor_permission() {
$perms = array(
'upload file with ueditor' => array(
'title' => t('Upload file with ueditor'),
),
'administer ueditor' => array(
'title' => t('Administer ueditor'),
),
);
return $perms;
}
function ueditor_init() {
if (variable_get('ueditor_highlighting_format', 1) == 1) {
if (!_ueditor_page_match()) {
return;
}
if (module_exists('libraries')) {
$lib_path = libraries_get_path('ueditor');
}
drupal_add_css($lib_path . '/third-party/SyntaxHighlighter/shCoreDefault.css');
drupal_add_js($lib_path . '/third-party/SyntaxHighlighter/shCore.js', array(
'weight' => -1,
));
drupal_add_js('SyntaxHighlighter.all();', array(
'type' => 'inline',
'scope' => 'footer',
'weight' => 5,
));
}
}
function ueditor_menu() {
$items['admin/config/content/ueditor'] = array(
'title' => 'UEditor Global Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ueditor_global_settings',
),
'description' => 'Configure ueditor global options.',
'access arguments' => array(
'administer ueditor',
),
'restrict access' => TRUE,
);
$items['ueditor/controller'] = array(
'title' => 'Ueditor Control',
'page callback' => '_ueditor_controller',
'access arguments' => array(
'upload file with ueditor',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function ueditor_wysiwyg_include_directory($type) {
switch ($type) {
case 'editors':
return $type;
}
}
function _ueditor_controller() {
date_default_timezone_set("Asia/Chongqing");
error_reporting(E_ERROR);
header("Content-Type: text/html; charset=utf-8");
$file_path = strtr(DRUPAL_ROOT, '\\', '/') . '/' . drupal_get_path('module', 'ueditor');
$CONFIG = drupal_json_decode(preg_replace("/\\/\\*[\\s\\S]+?\\*\\//", "", file_get_contents($file_path . '/includes/config.json')), true);
$ueditor_uploadpath_config = variable_get('ueditor_uploadpath_config', ueditor_config_default());
foreach ($ueditor_uploadpath_config as $key => $pathitem) {
if (isset($CONFIG[$key])) {
$CONFIG[$key] = _ueditor_realpath($pathitem);
}
}
$action = $_GET['action'];
switch ($action) {
case 'config':
$result = json_encode($CONFIG);
break;
case 'uploadimage':
case 'uploadscrawl':
case 'uploadvideo':
case 'uploadfile':
$result = (include $file_path . '/includes/action_upload.inc');
break;
case 'listimage':
$result = (include $file_path . '/includes/action_list.inc');
break;
case 'listfile':
$result = (include $file_path . '/includes/action_list.inc');
break;
case 'catchimage':
$result = (include $file_path . '/includes/action_crawler.inc');
break;
default:
$result = json_encode(array(
'state' => t('Request address error'),
));
break;
}
if (isset($_GET["callback"])) {
if (preg_match("/^[\\w_]+\$/", $_GET["callback"])) {
echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
}
else {
echo json_encode(array(
'state' => t('callback parameters are not legitimate'),
));
}
}
else {
echo $result;
}
}
function ueditor_config_default($settings = NULL) {
$settings['language'] = 'en';
$settings['initial_content'] = '';
$settings['zindex'] = 500;
$settings['initialFrameHeight'] = 320;
$settings['auto_height'] = TRUE;
$settings['auto_float'] = TRUE;
$settings['allowdivtop'] = FALSE;
$settings['show_elementpath'] = TRUE;
$settings['show_wordcount'] = TRUE;
$settings['imagePathFormat'] = '/%b%f/ueditor/%u/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}';
$settings['scrawlPathFormat'] = '/%b%f/ueditor/%u/upload/scrawl/{yyyy}{mm}{dd}/{time}{rand:6}';
$settings['filePathFormat'] = '/%b%f/ueditor/%u/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}';
$settings['fileManagerListPath'] = '/%b%f/ueditor/%u/upload/file/';
$settings['catcherPathFormat'] = '/%b%f/ueditor/%u/upload/catcher/{yyyy}{mm}{dd}/{time}{rand:6}';
$settings['imageManagerListPath'] = '/%b%f/ueditor/%u/upload/image/';
$settings['snapscreenPathFormat'] = '/%b%f/ueditor/%u/upload/snapscreen/{yyyy}{mm}{dd}/{time}{rand:6}';
$settings['videoPathFormat'] = '/%b%f/ueditor/upload/%u/video/{yyyy}{mm}{dd}/{time}{rand:6}';
$settings['toolbars'] = array(
array(
'fullscreen',
'source',
'|',
'undo',
'redo',
'|',
'bold',
'italic',
'underline',
'fontborder',
'strikethrough',
'superscript',
'subscript',
'removeformat',
'formatmatch',
'autotypeset',
'blockquote',
'pasteplain',
'|',
'forecolor',
'backcolor',
'insertorderedlist',
'insertunorderedlist',
'selectall',
'cleardoc',
'|',
'rowspacingtop',
'rowspacingbottom',
'lineheight',
'|',
'customstyle',
'paragraph',
'fontfamily',
'fontsize',
'|',
'directionalityltr',
'directionalityrtl',
'indent',
'|',
'justifyleft',
'justifycenter',
'justifyright',
'justifyjustify',
'|',
'touppercase',
'tolowercase',
'|',
'link',
'unlink',
'anchor',
'|',
'imagenone',
'imageleft',
'imageright',
'imagecenter',
'|',
'simpleupload',
'insertimage',
'emotion',
'scrawl',
'insertvideo',
'music',
'attachment',
'map',
'gmap',
'insertframe',
'insertcode',
'webapp',
'pagebreak',
'template',
'background',
'|',
'horizontal',
'date',
'time',
'spechars',
'snapscreen',
'wordimage',
'|',
'inserttable',
'deletetable',
'insertparagraphbeforetable',
'insertrow',
'deleterow',
'insertcol',
'deletecol',
'mergecells',
'mergeright',
'mergedown',
'splittocells',
'splittorows',
'splittocols',
'charts',
'|',
'print',
'preview',
'searchreplace',
'help',
'drafts',
),
);
if (variable_get('ueditor_enable_formula_editor', 0)) {
$settings['toolbars'][0][] = 'kityformula';
}
return $settings;
}
function _ueditor_realpath($path, $auto = FALSE) {
global $user;
$lib_path = 'sites/all/libraries';
$mod_path = drupal_get_path('module', 'ueditor');
$path = strtr($path, array(
'%b' => base_path(),
'%m' => $mod_path,
'%l' => $lib_path,
'%f' => variable_get('file_public_path', conf_path() . '/files'),
'%d' => strtr(DRUPAL_ROOT, '\\', '/'),
'%u' => $user->uid,
));
$path = strtr($path, '\\', '/');
if ($auto) {
$path .= '/';
}
$path = str_replace('//', '/', $path);
return $path;
}
function ueditor_global_settings($form, &$form_state) {
global $base_url;
$enable_paging = variable_get('ueditor_enable_paging', 0);
if ($enable_paging == 1) {
if (!module_exists('paging')) {
drupal_set_message(t('Please install the <a href="@url">Paging</a> module at first.', array(
'@url' => url("https://www.drupal.org/project/paging"),
)), 'error');
}
}
if (file_exists('sites/all/libraries/ueditor/kityformula-plugin/kityformula/js/kityformula-editor.all.min.js') || file_exists('profiles/' . drupal_get_profile() . '/libraries/ueditor/kityformula-plugin/kityformula/js/kityformula-editor.all.min.js')) {
$kityformula_installed = TRUE;
}
else {
$kityformula_installed = FALSE;
}
$enable_formula_editor = variable_get('ueditor_enable_formula_editor', 0);
if ($enable_formula_editor == 1) {
if (!$kityformula_installed) {
drupal_set_message(t('Please install the <a href="@url">kityformula</a> library at first, and the place should be like this: <i>sites/all/libraries/ueditor/kityformula-plugin/kityformula/js/kityformula-editor.all.min.js</i>', array(
'@url' => url("http://ueditor.baidu.com/download/kityformula-plugin.zip"),
)), 'error');
variable_set('ueditor_enable_formula_editor', 0);
}
}
$enable_colorbox = variable_get('ueditor_enable_colorbox', 0);
if ($enable_colorbox == 1) {
if (!module_exists('colorbox')) {
drupal_set_message(t('Please install the <a href="@url">Colorbox</a> module at first.', array(
'@url' => url("https://www.drupal.org/project/colorbox"),
)), 'error');
}
}
$form['global']['highlighting_format'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Highlighting Code Format'),
'#default_value' => variable_get('ueditor_highlighting_format', 1),
'#description' => t('If enabled, when you insert code, the code will highlighting.'),
);
$form['global']['inject_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Ueditor highlighting js/css code inject settings'),
'#states' => array(
'invisible' => array(
'input[name="highlighting_format"]' => array(
'checked' => FALSE,
),
),
),
);
$options = array(
UEDITOR_INJECT_EXCEPT_LISTED => t('Inject on all pages except those listed'),
UEDITOR_INJECT_IF_LISTED => t('Inject on only the listed pages'),
);
$description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
'%blog' => 'blog',
'%blog-wildcard' => 'blog/*',
'%front' => '<front>',
));
$form['global']['inject_settings']['inject'] = array(
'#type' => 'radios',
'#title' => t('Inject js/css code on specific pages'),
'#options' => $options,
'#default_value' => variable_get('ueditor_inject', UEDITOR_INJECT_EXCEPT_LISTED),
);
$form['global']['inject_settings']['pages'] = array(
'#type' => 'textarea',
'#title' => '<span class="element-invisible">Pages</span>',
'#default_value' => variable_get('ueditor_pages', "admin\nadmin/*\nuser\nuser/*\nimce\nimce/*\nueditor/controller\nueditor/controller/*\n"),
'#description' => $description,
);
$form['global']['enable_paging'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Conent paging in ueditor'),
'#default_value' => variable_get('ueditor_enable_paging', 0),
'#description' => t('If you want to use content paging in the ueditor, you must enable the <a href="@url">Paging</a> module at first.', array(
'@url' => url("https://www.drupal.org/project/paging"),
)),
);
$form['global']['enable_formula_editor'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Formula in ueditor'),
'#default_value' => variable_get('ueditor_enable_formula_editor', 0),
'#description' => t('If you want to use KityFormula in the ueditor, you must install the <a href="@url">KityFormula</a> library at first. and the place should be like this: <i>sites/all/libraries/ueditor/kityformula-plugin/kityformula/js/kityformula-editor.all.min.js</i>', array(
'@url' => url("http://ueditor.baidu.com/download/kityformula-plugin.zip"),
)),
);
$form['global']['enable_colorbox'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Colorbox for ueditor image'),
'#default_value' => variable_get('ueditor_enable_colorbox', 0),
'#description' => t('If you want to use colorbox for ueditor image, you must enable the <a href="@url">Colorbox</a> module at first.', array(
'@url' => url("https://www.drupal.org/project/colorbox"),
)),
);
$form['global']['colorbox_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Colorbox Settings'),
'#collapsible' => TRUE,
'#states' => array(
'invisible' => array(
'input[name="enable_colorbox"]' => array(
'checked' => FALSE,
),
),
),
);
$form['global']['colorbox_settings']['wrapper'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'inner',
),
),
);
$form['global']['colorbox_settings']['wrapper']['colorbox_width'] = array(
'#type' => 'textfield',
'#title' => t('Colorbox Width'),
'#description' => t('The width of colorbox.'),
'#default_value' => variable_get('ueditor_colorbox_width', 500),
);
$form['global']['colorbox_settings']['wrapper']['colorbox_height'] = array(
'#type' => 'textfield',
'#title' => t('Colorbox Height'),
'#description' => t('The height of colorbox.'),
'#default_value' => variable_get('ueditor_colorbox_height', 500),
);
$form['global']['watermark'] = array(
'#type' => 'checkbox',
'#title' => t('Use watermark when upload'),
'#default_value' => variable_get('ueditor_watermark', 0),
'#tree' => FALSE,
'#description' => t('Check here if you want use watermark when upload.'),
);
$form['global']['watermark_settings'] = array(
'#type' => 'container',
'#states' => array(
'invisible' => array(
'input[name="watermark"]' => array(
'checked' => FALSE,
),
),
),
);
$form['global']['watermark_settings']['watermark_type'] = array(
'#type' => 'select',
'#title' => 'The type of watermark',
'#options' => array(
'image' => 'Image',
'text' => 'Text',
),
'#default_value' => variable_get('ueditor_watermark_type', 'image'),
);
$form['global']['watermark_settings']['watermark_image'] = array(
'#type' => 'container',
'#states' => array(
'invisible' => array(
'#edit-watermark-type' => array(
'value' => 'text',
),
),
),
);
$form['global']['watermark_settings']['watermark_image']['watermark_path'] = array(
'#type' => 'textfield',
'#title' => t('Path to custom watermark'),
'#description' => t('The path to the file you would like to use as watermark image.'),
'#default_value' => variable_get('ueditor_watermark_path', ''),
);
$form['global']['watermark_settings']['watermark_image']['watermark_upload'] = array(
'#type' => 'file',
'#title' => t('Upload watermark image'),
'#maxlength' => 40,
'#description' => t("If you don't have direct file access to the server, use this field to upload watermark image."),
);
$form['global']['watermark_settings']['watermark_image']['watermark_alpha'] = array(
'#type' => 'select',
'#title' => t('Watermark Alpha'),
'#options' => drupal_map_assoc(array(
30,
50,
80,
100,
)),
'#default_value' => variable_get('ueditor_watermark_alpha', 50),
);
$form['global']['watermark_settings']['watermark_text'] = array(
'#type' => 'container',
'#states' => array(
'invisible' => array(
'#edit-watermark-type' => array(
'value' => 'image',
),
),
),
);
$form['global']['watermark_settings']['watermark_text']['textcontent'] = array(
'#type' => 'textfield',
'#title' => t('Watermark Text'),
'#description' => t('The path to the file you would like to use as watermark image.'),
'#default_value' => variable_get('ueditor_textcontent', $base_url),
);
$form['global']['watermark_settings']['watermark_text']['textfontsize'] = array(
'#type' => 'select',
'#title' => t('Watermark Text Font Size'),
'#options' => drupal_map_assoc(array(
12,
16,
18,
24,
36,
48,
60,
)),
'#default_value' => variable_get('ueditor_textfontsize', 16),
);
$form['global']['watermark_settings']['watermark_text']['textcolor'] = array(
'#type' => 'textfield',
'#title' => t('Watermark Text Color'),
'#description' => t('The path to the file you would like to use as watermark image.'),
'#default_value' => variable_get('ueditor_textcolor', '#FF0000'),
);
$form['global']['watermark_settings']['watermark_place'] = array(
'#type' => 'select',
'#title' => t('Watermark Place'),
'#options' => array(
'0' => 'Random',
'1' => 'Top Left',
'2' => 'Top Center',
'3' => 'Top Right',
'4' => 'Middle Left',
'5' => 'Middle Center',
'6' => 'Middle Right',
'7' => 'Bottom Left',
'8' => 'Bottom Center',
'9' => 'Bottom Right',
),
'#default_value' => variable_get('ueditor_watermark_place', 9),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function ueditor_global_settings_validate($form, &$form_state) {
$validators = array(
'file_validate_is_image' => array(),
);
$file = file_save_upload('watermark_upload', $validators);
if (isset($file)) {
if ($file) {
$form_state['values']['watermark_upload'] = $file;
}
else {
form_set_error('watermark_upload', t('The watermark could not be uploaded.'));
}
}
if (!empty($form_state['values']['watermark_path'])) {
$path = _ueditor_global_settings_validate_path($form_state['values']['watermark_path']);
if (!$path) {
form_set_error('watermark_path', t('The custom watermark path is invalid.'));
}
}
}
function _ueditor_global_settings_validate_path($path) {
if (drupal_realpath($path) == $path) {
return FALSE;
}
if (is_file($path)) {
return $path;
}
if (file_uri_scheme($path) === FALSE) {
$path = 'public://' . $path;
}
if (is_file($path)) {
return $path;
}
return FALSE;
}
function ueditor_global_settings_submit($form, &$form_state) {
form_state_values_clean($form_state);
$values = $form_state['values'];
if (!empty($values['ueditor_enable_paging'])) {
if ($values['ueditor_enable_paging'] == 1) {
if (!module_exists('paging')) {
drupal_set_message(t('Please install the <a href="@url">Paging</a> module at first.', array(
'@url' => url("https://www.drupal.org/project/paging"),
)), 'error');
}
}
}
if (file_exists('sites/all/libraries/ueditor/kityformula-plugin/kityformula/js/kityformula-editor.all.min.js') || file_exists('profiles/' . drupal_get_profile() . '/libraries/ueditor/kityformula-plugin/kityformula/js/kityformula-editor.all.min.js')) {
$kityformula_installed = TRUE;
}
else {
$kityformula_installed = FALSE;
}
if (!empty($values['ueditor_enable_formula_editor'])) {
if ($values['ueditor_enable_formula_editor'] == 1) {
if (!$kityformula_installed) {
drupal_set_message(t('Please install the <a href="@url">kityformula</a> library at first, and the place should be like this: <i>sites/all/libraries/ueditor/kityformula-plugin/kityformula/js/kityformula-editor.all.min.js</i>', array(
'@url' => url("http://ueditor.baidu.com/download/kityformula-plugin.zip"),
)), 'error');
}
}
}
if (!empty($values['ueditor_enable_colorbox'])) {
if ($values['ueditor_enable_colorbox'] == 1) {
if (!module_exists('colorbox')) {
drupal_set_message(t('Please install the <a href="@url">Colorbox</a> module at first.', array(
'@url' => url("https://www.drupal.org/project/colorbox"),
)), 'error');
}
}
}
if (!empty($values['watermark_upload'])) {
$file = $values['watermark_upload'];
unset($values['watermark_upload']);
$filename = file_unmanaged_copy($file->uri);
$values['watermark_path'] = $filename;
}
if (!empty($values['watermark_path'])) {
$values['watermark_path'] = _ueditor_global_settings_validate_path($values['watermark_path']);
}
foreach ($values as $key => $value) {
variable_set('ueditor_' . $key, $value);
}
drupal_set_message(t('The configuration options have been saved.'));
}
function ueditor_form_alter(&$form, &$form_state, $form_id) {
$words = variable_get('paging_separator', '<!--pagebreak-->');
$editor_profiles = _ueditor_get_wysiwyg();
if (preg_match('/node_form$/', $form_id)) {
$node = $form['#node'];
$fields = _ueditor_get_editor_fields($node, 'node', $editor_profiles);
if (!empty($words) && $fields) {
foreach ($fields as $key => $field) {
if (!empty($field[0]['value'])) {
$language = field_language('node', $node, $key);
$form[$key][$language][0]['#default_value'] = ueditor_unreplace_pagebeaketag($field[0]['value'], $words);
}
}
}
}
}
function ueditor_replace_pagebeaketag($body, $words) {
$body = str_replace(htmlentities(variable_get('paging_separator', '<!--pagebreak-->')), $words, $body);
return $body;
}
function ueditor_unreplace_pagebeaketag($body, $words) {
$body = str_replace($words, htmlentities(variable_get('paging_separator', '<!--pagebreak-->')), $body);
return $body;
}
function ueditor_add_colorbox($body, $settings) {
if (!empty($settings['width'])) {
$colorbox_width = $settings['width'];
}
if (!empty($settings['height'])) {
$colorbox_height = $settings['height'];
}
$reg = '/<img\\s+src="([^"]+)"[^>]+>/';
$replace = "<a href=\"\$1?width={$colorbox_width}&height={$colorbox_height}\" class=\"colorbox-load\" rel=\"ueditor-gallery\"><image src=\"\$1\" /></a>";
$new_body = preg_replace($reg, $replace, $body);
return $new_body;
}
function ueditor_entity_presave($entity, $type) {
if ($type != 'file') {
$info = entity_get_info($type);
list($id) = entity_extract_ids($type, $entity);
$editor_profiles = _ueditor_get_wysiwyg();
$fields = _ueditor_get_editor_fields($entity, $type, $editor_profiles);
if (variable_get('ueditor_enable_paging') == 1 && module_exists('paging')) {
$words = variable_get('paging_separator', '<!--pagebreak-->');
if ($words) {
if (!empty($words) && $fields) {
foreach ($fields as $key => $field) {
if (!empty($field[0]['value'])) {
$language = field_language($type, $entity, $key);
$entity->{$key}[$language][0]['value'] = ueditor_replace_pagebeaketag($entity->{$key}[$language][0]['value'], $words);
}
}
}
}
}
}
}
function ueditor_entity_view($entity, $type, $view_mode, $langcode) {
if ($view_mode == 'full') {
$editor_profiles = _ueditor_get_wysiwyg();
$fields = _ueditor_get_editor_fields($entity, $type, $editor_profiles);
if ($type == 'taxonomy_term' && isset($entity->description)) {
$fields['description'][]['value'] = $entity->description;
}
if (variable_get('ueditor_enable_colorbox') == 1 && module_exists('colorbox')) {
$colorbox_type = variable_get('colorbox_load', 0);
if ($colorbox_type) {
$settings = array(
'width' => variable_get('ueditor_colorbox_width', 500),
'height' => variable_get('ueditor_colorbox_height', 500),
);
if ($fields) {
foreach ($fields as $key => $field) {
if (!empty($field[0]['value'])) {
$entity->content[$key][0]['#markup'] = ueditor_add_colorbox($entity->content[$key][0]['#markup'], $settings);
}
}
}
}
else {
drupal_set_message(t('Plese ensure you enable the !link settings.', array(
'!link' => l(t('Colorbox Load'), 'admin/config/media/colorbox'),
)), 'error');
}
}
}
}
function ueditor_get_savepath($path) {
$basePath = base_path();
$filedir = variable_get('file_public_path', conf_path() . '/files');
if ($path) {
$savePath = str_replace($basePath . $filedir . '/', "", $path);
$savePath = str_replace($GLOBALS['base_root'], "", $savePath);
}
return $savePath;
}
function _ueditor_page_match() {
$inject = variable_get('ueditor_inject', UEDITOR_INJECT_EXCEPT_LISTED);
$pages = variable_get('ueditor_pages', "admin\nadmin/*\nuser\nuser/*\nimce\nimce/*\nueditor/controller\nueditor/controller/*\n");
$path = drupal_get_path_alias($_GET['q']);
$page_match = drupal_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
}
return !($inject xor $page_match);
}
function _ueditor_get_editor_fields($entity, $type, $editor_profiles) {
if (!empty($editor_profiles)) {
$fields = array();
foreach (field_info_field_map() as $field_name => $field_stub) {
if ($field_stub['type'] == 'text_long' || $field_stub['type'] == 'text_with_summary') {
if (isset($entity->{$field_name})) {
$fields[$field_name] = $field_name;
}
}
}
foreach ($fields as $index => $field) {
$language = field_language($type, $entity, $field);
$field_items = field_get_items($type, $entity, $field, $language);
if (!empty($field_items[0]['format']) && in_array($field_items[0]['format'], $editor_profiles)) {
$fields[$field] = $field_items;
}
}
return $fields;
}
return FALSE;
}
function _ueditor_get_wysiwyg() {
$editor_profiles = array();
$profiles = wysiwyg_profile_load_all();
foreach ($profiles as $key => $profile) {
if (isset($profile->editor) && $profile->editor == 'ueditor') {
$editor_profiles[] = $key;
}
}
return $editor_profiles;
}