You are here

ueditor.module in UEditor - 百度编辑器 7.3

Same filename and directory in other branches
  1. 8 ueditor.module
  2. 7 ueditor.module
  3. 7.2 ueditor.module

Integration ueditor for wysiwyg.

File

ueditor.module
View source
<?php

/**
 * @file
 * Integration ueditor for wysiwyg.
 */

/**
 * Inject ueditor highlighting on every page except the listed pages.
 */
define('UEDITOR_INJECT_EXCEPT_LISTED', 0);

/**
 * Inject ueditor highlighting on only the listed pages.
 */
define('UEDITOR_INJECT_IF_LISTED', 1);

/**
 * Implements hook_permission().
 */
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;
}

/**
 * Implementation of hook_init().
 */
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');
    }

    // Load SyntaxHighlighter js and css.
    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,
    ));
  }
}

/**
 * Implements hook_menu().
 */
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;
}

/**
 * Implements hook_wysiwyg_include_directory().
 */
function ueditor_wysiwyg_include_directory($type) {
  switch ($type) {
    case 'editors':
      return $type;
  }
}

/**
 * The main entrance of ueditor command.
 */
function _ueditor_controller() {

  //header('Access-Control-Allow-Origin: http://www.baidu.com'); //Set http://www.baidu.com allow cross-domain access

  //header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With'); //Settings allow cross-domain header
  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;

    /* upload image */
    case 'uploadimage':

    /* upload scrawl */
    case 'uploadscrawl':

    /* upload video */
    case 'uploadvideo':

    /* upload file */
    case 'uploadfile':
      $result = (include $file_path . '/includes/action_upload.inc');
      break;

    /* list image */
    case 'listimage':
      $result = (include $file_path . '/includes/action_list.inc');
      break;

    /* list file */
    case 'listfile':
      $result = (include $file_path . '/includes/action_list.inc');
      break;

    /* catch image */
    case 'catchimage':
      $result = (include $file_path . '/includes/action_crawler.inc');
      break;
    default:
      $result = json_encode(array(
        'state' => t('Request address error'),
      ));
      break;
  }

  /* output */
  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;
  }
}

/**
 * Default ueditor config array.
 */
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;
}

/**
 * Change %path to realpath.
 */
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;
}

/**
 * Config options for ueditor global settings.
 */
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"),
    )),
  );

  //ueditor colorbox support
  $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),
  );

  //ueditor watermark support
  $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;
}

/**
 * Validator for the ueditor_global_settings() form.
 */
function ueditor_global_settings_validate($form, &$form_state) {

  // Handle file uploads.
  $validators = array(
    'file_validate_is_image' => array(),
  );

  // Check for a new uploaded watermark.
  $file = file_save_upload('watermark_upload', $validators);
  if (isset($file)) {

    // File upload was attempted.
    if ($file) {

      // Put the temporary file in form_values so we can save it on submit.
      $form_state['values']['watermark_upload'] = $file;
    }
    else {

      // File upload failed.
      form_set_error('watermark_upload', t('The watermark could not be uploaded.'));
    }
  }

  // If the user provided a path for a watermark or favicon file, make sure a file
  // exists at that path.
  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.'));
    }
  }
}

/**
 * Helper function for the system_theme_settings form.
 *
 * Attempts to validate normal system paths, paths relative to the public files
 * directory, or stream wrapper URIs. If the given path is any of the above,
 * returns a valid path or URI that the theme system can display.
 *
 * @param $path
 *   A path relative to the Drupal root or to the public files directory, or
 *   a stream wrapper URI.
 * @return mixed
 *   A valid path that can be displayed through the theme system, or FALSE if
 *   the path could not be validated.
 */
function _ueditor_global_settings_validate_path($path) {

  // Absolute local file paths are invalid.
  if (drupal_realpath($path) == $path) {
    return FALSE;
  }

  // A path relative to the Drupal root or a fully qualified URI is valid.
  if (is_file($path)) {
    return $path;
  }

  // Prepend 'public://' for relative file paths within public filesystem.
  if (file_uri_scheme($path) === FALSE) {
    $path = 'public://' . $path;
  }
  if (is_file($path)) {
    return $path;
  }
  return FALSE;
}

/**
 * Submit callback for ueditor global settings form.
 */
function ueditor_global_settings_submit($form, &$form_state) {

  // Exclude unnecessary elements before saving.
  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 the user entered a path relative to the system files directory for
  // a watermark image, store a public:// URI so the theme system can handle it.
  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.'));
}

/**
 * Implementation of hook_form_alter().
 */
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);
        }
      }
    }
  }
}

/**
 * Replace page break tag.
 */
function ueditor_replace_pagebeaketag($body, $words) {
  $body = str_replace(htmlentities(variable_get('paging_separator', '<!--pagebreak-->')), $words, $body);
  return $body;
}

/**
 * Unreplace page break tag.
 */
function ueditor_unreplace_pagebeaketag($body, $words) {
  $body = str_replace($words, htmlentities(variable_get('paging_separator', '<!--pagebreak-->')), $body);
  return $body;
}

/**
 * Add colorbox tag for ueditor image.
 */
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;
}

/**
 * Implements hook_entity_presave().
 */
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);
            }
          }
        }
      }
    }
  }
}

/**
 * Implements hook_entity_view().
 */
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);

    //add support taxonomy term description field.
    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');
      }
    }
  }
}

/**  
 * ueditor get the save path.
 */
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;
}

/**  
 * ueditor match pages with current path.
 */
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']);

  // Compare with the internal and path alias (if any).
  $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);
}

/**  
 * ueditor match fields with use ueditor.
 */
function _ueditor_get_editor_fields($entity, $type, $editor_profiles) {
  if (!empty($editor_profiles)) {
    $fields = array();

    // Loop through the fields looking for any fields of use ueditor.
    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;
}

/**  
 * ueditor get profiles with use ueditor.
 */
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;
}

Functions

Namesort descending Description
ueditor_add_colorbox Add colorbox tag for ueditor image.
ueditor_config_default Default ueditor config array.
ueditor_entity_presave Implements hook_entity_presave().
ueditor_entity_view Implements hook_entity_view().
ueditor_form_alter Implementation of hook_form_alter().
ueditor_get_savepath ueditor get the save path.
ueditor_global_settings Config options for ueditor global settings.
ueditor_global_settings_submit Submit callback for ueditor global settings form.
ueditor_global_settings_validate Validator for the ueditor_global_settings() form.
ueditor_init Implementation of hook_init().
ueditor_menu Implements hook_menu().
ueditor_permission Implements hook_permission().
ueditor_replace_pagebeaketag Replace page break tag.
ueditor_unreplace_pagebeaketag Unreplace page break tag.
ueditor_wysiwyg_include_directory Implements hook_wysiwyg_include_directory().
_ueditor_controller The main entrance of ueditor command.
_ueditor_get_editor_fields ueditor match fields with use ueditor.
_ueditor_get_wysiwyg ueditor get profiles with use ueditor.
_ueditor_global_settings_validate_path Helper function for the system_theme_settings form.
_ueditor_page_match ueditor match pages with current path.
_ueditor_realpath Change %path to realpath.

Constants

Namesort descending Description
UEDITOR_INJECT_EXCEPT_LISTED Inject ueditor highlighting on every page except the listed pages.
UEDITOR_INJECT_IF_LISTED Inject ueditor highlighting on only the listed pages.