You are here

ueditor.inc in UEditor - 百度编辑器 7

Same filename and directory in other branches
  1. 7.3 editors/ueditor.inc
  2. 7.2 editors/ueditor.inc

Editor integration functions for ueditor.

File

editors/ueditor.inc
View source
<?php

/**
 * @file
 * Editor integration functions for ueditor.
 */

/**
 * Plugin implementation of hook_editor().
 */
function ueditor_ueditor_editor() {
  $editor['ueditor'] = array(
    'title' => 'Ueditor',
    'vendor url' => 'http://ueditor.baidu.com/website/index.html',
    'download url' => 'http://ueditor.baidu.com/website/download.html',
    'libraries' => array(
      '' => array(
        'title' => 'Source',
        'files' => array(
          'ueditor.all.js',
        ),
      ),
    ),
    'version callback' => 'ueditor_version',
    'settings callback' => 'ueditor_settings',
    'settings form callback' => 'ueditor_settings_form',
    'versions' => array(
      '1.4.3' => array(
        'js files' => array(
          'ueditor.js',
        ),
      ),
    ),
  );
  return $editor;
}

/**
 * Detect editor version.
 *
 * @param array $editor
 *   An array containing editor properties as returned from hook_editor().
 *
 * @return int
 *   The installed editor version.
 */
function ueditor_version($editor) {
  $script = $editor['library path'] . '/ueditor.all.js';
  if (!file_exists($script)) {
    return;
  }
  $script = fopen($script, 'r');
  $max_lines = 50;
  while ($max_lines && ($line = fgets($script, 500))) {
    if (preg_match('/^UE.*?"([\\d\\.]+)"/', $line, $version)) {
      fclose($script);
      return $version[1];
    }
    $max_lines--;
  }
  fclose($script);
}

/**
 * Enhances the editor profile settings form for UEditor.
 */
function ueditor_settings_form(&$form, &$form_state) {
  $settings = $form_state['wysiwyg_profile']->settings;
  $suggest = '<code>Suggested path:</code> sites/default/files/ueditor/<br>';

  // Customize config js filename.
  $form['basic']['config'] = array(
    '#type' => 'textfield',
    '#title' => t('Editor config js filename'),
    '#description' => t('The filename of config.'),
    '#default_value' => isset($settings['config']) ? $settings['config'] : 'ueditor.config.js',
    '#maxlength' => 255,
    '#required' => TRUE,
  );
  $form['basic']['zindex'] = array(
    '#type' => 'textfield',
    '#title' => t('Editor zindex'),
    '#description' => t('The official website of the default zindex 900,<br />
      and Drupal overlay module ( #overlay= page ) conflict,
      so default change from 900 to 90 or you can customize.'),
    '#default_value' => isset($settings['zindex']) ? $settings['zindex'] : 99,
    '#size' => 5,
    '#maxlength' => 4,
    '#required' => TRUE,
  );
  $form['basic']['initial_content'] = array(
    '#type' => 'textfield',
    '#title' => t('Editor initial content'),
    '#description' => t('Editor initial content, after editor loading in the textarea.'),
    '#default_value' => isset($settings['initial_content']) ? $settings['initial_content'] : '',
    '#maxlength' => 255,
  );

  // config the upload path.
  $form['basic']['path_help'] = array(
    '#markup' => implode('<br>', array(
      '%b' => '<code>%b</code> - the base URL path of the Drupal installation (<code>' . _ueditor_realpath('%b') . '</code>)',
      '%m' => '<code>%m</code> - path where the UEditor module is stored (<code>' . _ueditor_realpath('%m') . '</code>)',
      '%l' => '<code>%l</code> - path to the libraries directory (<code>' . _ueditor_realpath('%l') . '</code>)',
      '%f' => '<code>%f</code> - the Drupal file system path where the files are stored (<code>' . _ueditor_realpath('%f') . '</code>)',
      '%d' => '<code>%d</code> - the server path to the document root (<code>' . _ueditor_realpath('%d') . '</code>)',
      '%u' => '<code>%u</code> - User ID (<code>' . _ueditor_realpath('%u') . '</code>)',
      '<br>',
    )),
    '#prefix' => '<div class="region region-help"><div class="block block-system"><div class="content">',
    '#suffix' => '</div></div></div>',
  );
  $imagePath = !empty($settings['imagePath']) ? $settings['imagePath'] : '/%b%f/ueditor/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}';
  $form['basic']['uploadpath']['imagePath'] = array(
    '#type' => 'textfield',
    '#title' => t('imagePath'),
    '#default_value' => $imagePath,
    '#description' => $suggest . 'Current path:<code>' . _ueditor_realpath($imagePath) . '</code>',
  );
  $scrawlPath = !empty($settings['scrawlPath']) ? $settings['scrawlPath'] : '/%b%f/ueditor/upload/scrawl/{yyyy}{mm}{dd}/{time}{rand:6}';
  $form['basic']['uploadpath']['scrawlPath'] = array(
    '#type' => 'textfield',
    '#title' => t('scrawlPath'),
    '#default_value' => $scrawlPath,
    '#description' => $suggest . 'Current path:<code>' . _ueditor_realpath($scrawlPath) . '</code>',
  );
  $filePath = !empty($settings['filePath']) ? $settings['filePath'] : '/%b%f/ueditor/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}';
  $form['basic']['uploadpath']['filePath'] = array(
    '#type' => 'textfield',
    '#title' => t('filePath'),
    '#default_value' => $filePath,
    '#description' => $suggest . 'Current path:<code>' . _ueditor_realpath($filePath) . '</code>',
  );
  $catcherPath = !empty($settings['catcherPath']) ? $settings['catcherPath'] : '/%b%f/ueditor/upload/catcher/{yyyy}{mm}{dd}/{time}{rand:6}';
  $form['basic']['uploadpath']['catcherPath'] = array(
    '#type' => 'textfield',
    '#title' => t('catcherPath'),
    '#default_value' => $catcherPath,
    '#description' => $suggest . 'Current path:<code>' . _ueditor_realpath($catcherPath) . '</code>',
  );
  $imageManagerPath = !empty($settings['imageManagerPath']) ? $settings['imageManagerPath'] : '/%b%f/ueditor/upload/image/';
  $form['basic']['uploadpath']['imageManagerPath'] = array(
    '#type' => 'textfield',
    '#title' => t('imageManagerPath'),
    '#default_value' => $imageManagerPath,
    '#description' => $suggest . 'Current path:<code>' . _ueditor_realpath($imageManagerPath) . '</code>',
  );
  $snapscreenPath = !empty($settings['snapscreenPath']) ? $settings['snapscreenPath'] : '/%b%f/ueditor/upload/snapscreen/{yyyy}{mm}{dd}/{time}{rand:6}';
  $form['basic']['uploadpath']['snapscreenPath'] = array(
    '#type' => 'textfield',
    '#title' => t('snapscreenPath'),
    '#default_value' => $snapscreenPath,
    '#description' => $suggest . 'Current path:<code>' . _ueditor_realpath($snapscreenPath) . '</code>',
  );
  $videoPath = !empty($settings['videoPath']) ? $settings['videoPath'] : '/%b%f/ueditor/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}';
  $form['basic']['uploadpath']['videoPath'] = array(
    '#type' => 'textfield',
    '#title' => t('videoPath'),
    '#default_value' => $videoPath,
    '#description' => $suggest . 'Current path:<code>' . _ueditor_realpath($videoPath) . '</code>',
  );
  $form['basic']['uploadpath']['#element_validate'][] = 'ueditor_settings_form_validate_uploadpath';
}

/**
 * Deal with settings form submit.
 */
function ueditor_settings_form_validate_uploadpath($element, &$form_state) {
  $uploadpath = array();
  if (!empty($element['imagePath']['#value'])) {
    $uploadpath['imagePathFormat'] = $element['imagePath']['#value'];
  }
  if (!empty($element['scrawlPath']['#value'])) {
    $uploadpath['scrawlPathFormat'] = $element['scrawlPath']['#value'];
  }
  if (!empty($element['filePath']['#value'])) {
    $uploadpath['filePathFormat'] = $element['filePath']['#value'];
  }
  if (!empty($element['catcherPath']['#value'])) {
    $uploadpath['catcherPathFormat'] = $element['catcherPath']['#value'];
  }
  if (!empty($element['imageManagerPath']['#value'])) {
    $uploadpath['imageManagerListPath'] = $element['imageManagerPath']['#value'];
  }
  if (!empty($element['snapscreenPath']['#value'])) {
    $uploadpath['snapscreenPathFormat'] = $element['snapscreenPath']['#value'];
  }
  if (!empty($element['videoPath']['#value'])) {
    $uploadpath['videoPathFormat'] = $element['videoPath']['#value'];
  }
  if (!empty($uploadpath) && !empty($form_state['wysiwyg']['editor'])) {
    _ueditor_modify_config($form_state['wysiwyg']['editor'], $uploadpath);
  }
}

/**
 * Return runtime editor settings for a given wysiwyg profile.
 *
 * @param array $editor
 *   A processed hook_editor() array of editor properties.
 * @param array $config
 *   An array containing wysiwyg editor profile settings.
 * @param string $theme
 *   The name of a theme/GUI/skin to use.
 *
 * @return array
 *   A settings array to be populated in
 *   Drupal.settings.wysiwyg.configs.{editor}
 */
function ueditor_settings($editor, $config, $theme) {

  // Settings.
  $settings['editorPath'] = base_path() . $editor['library path'] . '/';

  // Change the chinese language code.
  if (isset($config['language']) && $config['language'] == 'zh-hans') {
    $config['language'] = 'zh-cn';
  }
  $settings['language'] = !empty($config['language']) ? $config['language'] : 'en';
  $settings['zindex'] = !empty($config['zindex']) ? $config['zindex'] : 99;
  $settings['initialContent'] = !empty($config['initial_content']) ? $config['initial_content'] : '';

  // Load config js.
  drupal_add_js('window.UEDITOR_HOME_URL = "' . $settings['editorPath'] . '";', array(
    'type' => 'inline',
    'weight' => -2,
  ));
  drupal_add_js($editor['library path'] . '/' . (!empty($config['config']) ? $config['config'] : 'ueditor.config.js'), array(
    'weight' => -1,
  ));

  // Theme.
  // Ueditor temporarily does not support multiple themes,
  // Can customize CSS overwrite.
  drupal_add_css($editor['library path'] . '/themes/default/css/ueditor.css');
  drupal_add_css($editor['library path'] . '/themes/default/iframe.css');
  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;
}

/**
 * Modify the config.json file data.
 */
function _ueditor_modify_config($editor, $uploadpath) {
  $config_json = $editor['library path'] . '/php/config.json';
  $changed = FALSE;
  if (!($changed = FALSE)) {
    $CONFIG = drupal_json_decode(preg_replace("/\\/\\*[\\s\\S]+?\\*\\//", "", file_get_contents($config_json)), true);
    $changed = TRUE;
  }
  else {
    $CONFIG = drupal_json_decode(file_get_contents($config_json));
  }
  foreach ($uploadpath as $key => $pathitem) {
    if (isset($CONFIG[$key])) {
      $CONFIG[$key] = _ueditor_realpath($pathitem);
    }
  }
  $new_val = drupal_json_encode($CONFIG);
  $file = fopen($config_json, "wb");
  fwrite($file, $new_val);
  fclose($file);
}

Functions

Namesort descending Description
ueditor_settings Return runtime editor settings for a given wysiwyg profile.
ueditor_settings_form Enhances the editor profile settings form for UEditor.
ueditor_settings_form_validate_uploadpath Deal with settings form submit.
ueditor_ueditor_editor Plugin implementation of hook_editor().
ueditor_version Detect editor version.
_ueditor_modify_config Modify the config.json file data.
_ueditor_realpath Change %path to realpath.