[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/ckeditor/includes/ -> ckeditor.lib.inc (source)

   1  <?php
   2  // $Id: ckeditor.lib.inc,v 1.1 2009/12/04 20:36:57 wwalc Exp $
   3  /**
   4   * CKEditor - The text editor for Internet - http://ckeditor.com
   5   * Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
   6   *
   7   * == BEGIN LICENSE ==
   8   *
   9   * Licensed under the terms of any of the following licenses at your
  10   * choice:
  11   *
  12   *  - GNU General Public License Version 2 or later (the "GPL")
  13   *    http://www.gnu.org/licenses/gpl.html
  14   *
  15   *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
  16   *    http://www.gnu.org/licenses/lgpl.html
  17   *
  18   *  - Mozilla Public License Version 1.1 or later (the "MPL")
  19   *    http://www.mozilla.org/MPL/MPL-1.1.html
  20   *
  21   * == END LICENSE ==
  22   *
  23   * @file
  24   * CKEditor Module for Drupal 6.x
  25   *
  26   * This module allows Drupal to replace textarea fields with CKEditor.
  27   *
  28   * This HTML text editor brings to the web many of the powerful functionalities
  29   * of known desktop editors like Word. It's really  lightweight and doesn't
  30   * require any kind of installation on the client computer.
  31   */
  32  
  33  /**
  34   * Guess the absolute server path to the document root
  35   * Usually it should return $_SERVER['DOCUMENT_ROOT']
  36   *
  37   * @todo Improve me!!
  38   * Returns absolute path or false on failure
  39   *
  40   * @return string|boolean
  41   */
  42  function ckeditor_get_document_root_full_path() {
  43    $found=0;
  44    $index_dir  = realpath(dirname($_SERVER['SCRIPT_FILENAME'])); // {/dir1/dir2/home/drupal}/index.php
  45    if (getcwd() == $index_dir) {
  46      $found++;
  47    }
  48    $drupal_dir = base_path();
  49    $index_dir  = str_replace('\\', "/", $index_dir);
  50    $drupal_dir = str_replace('\\', "/", $drupal_dir);
  51    $document_root_dir   = $index_dir .'/'. str_repeat('../', substr_count($drupal_dir, '/')-1);
  52    $document_root_dir   = realpath($document_root_dir);
  53    $document_root_dir   = rtrim($document_root_dir, '/\\');
  54    if ($document_root_dir == $_SERVER['DOCUMENT_ROOT']) {
  55      $found++;
  56    }
  57    $document_root_dir   = str_replace('\\', '/', $document_root_dir);
  58  
  59    if ($document_root_dir != '') {
  60      $found++;
  61    }
  62    if (file_exists($document_root_dir)) {
  63      $found++;
  64    }
  65    if (file_exists($document_root_dir . base_path() .'includes/bootstrap.inc')) {
  66      $found++;
  67    }
  68  
  69    if ($found >= 3) {
  70      return $document_root_dir;
  71    }
  72    else{
  73      return FALSE;
  74    }
  75  }
  76  
  77  /**
  78   * Emulates the asp Server.mapPath function.
  79   * Given an url path return the physical directory that it corresponds to.
  80   *
  81   * Returns absolute path or false on failure
  82   *
  83   * @param string $path
  84   * @return @return string|boolean
  85   */
  86  function ckeditor_resolve_url($path) {
  87    if (function_exists('apache_lookup_uri')) {
  88      $info = @apache_lookup_uri($path);
  89      if (!$info) {
  90        return FALSE;
  91      }
  92      return $info->filename . $info->path_info ;
  93    }
  94  
  95    $document_root = ckeditor_get_document_root_full_path();
  96    if ($document_root !== FALSE) {
  97      return $document_root . $path;
  98    }
  99  
 100    return FALSE;
 101  }
 102  
 103  function ckeditor_load_toolbar_options() {
 104    $arr = array('Basic' => 'Basic', 'Full' => 'Full');
 105    $module_drupal_path = drupal_get_path('module', 'ckeditor');
 106    $editor_local_path  = ckeditor_path(TRUE);
 107    $ckconfig_js = $editor_local_path .'/config.js';
 108    $ckeditor_config_js = $module_drupal_path .'/ckeditor.config.js';
 109    if (file_exists($ckconfig_js) && is_readable($ckconfig_js)) {
 110      $fp = @fopen($ckconfig_js, "r");
 111      if ($fp) {
 112        while (!feof($fp)) {
 113          $line = fgets($fp, 1024);
 114          $matches = array();
 115          if (preg_match('/config.toolbar_([a-z0-9_]+)/i', $line, $matches)) {
 116            $arr[$matches[1]] = drupal_ucfirst($matches[1]);
 117          }
 118        }
 119        fclose($fp);
 120      }
 121    }
 122    if (file_exists($ckeditor_config_js) && is_readable($ckeditor_config_js)) {
 123      $fp = @fopen($ckeditor_config_js, "r");
 124      if ($fp) {
 125        while (!feof($fp)) {
 126          $line = fgets($fp, 1024);
 127          $matches = array();
 128          if (preg_match('/config.toolbar_([a-z0-9_]+)/i', $line, $matches)) {
 129            $arr[$matches[1]] = drupal_ucfirst($matches[1]);
 130          }
 131        }
 132        fclose($fp);
 133      }
 134    }
 135  
 136    //oops, we have no information about toolbars, let's use hardcoded array
 137    if (empty($arr)) {
 138      $arr = array(
 139        'Basic' => 'Basic',
 140        'Default' => 'Default',
 141      );
 142    }
 143    asort($arr);
 144  
 145    return $arr;
 146  }
 147  
 148  function ckeditor_load_skin_options() {
 149    $arr = array();
 150    $editor_local_path  = ckeditor_path(TRUE);
 151    $skin_dir     = $editor_local_path .'/skins';
 152    if (is_dir($skin_dir)) {
 153      $dh = @opendir($skin_dir);
 154      if (FALSE !== $dh) {
 155        while (($file = readdir($dh)) !== FALSE ) {
 156          if (in_array($file, array(".", "..", "CVS", ".svn"))) {
 157            continue;
 158          }
 159          if (is_dir($skin_dir . DIRECTORY_SEPARATOR . $file)) {
 160            $arr[$file] = drupal_ucfirst($file);
 161          }
 162        }
 163        closedir( $dh );
 164      }
 165    }
 166  
 167    //oops, we have no information about skins, let's use only default
 168    if (empty($arr)) {
 169      $arr = array(
 170        'kama' => 'Kama',
 171      );
 172    }
 173    asort($arr);
 174  
 175    return $arr;
 176  }
 177  
 178  function ckeditor_load_lang_options() {
 179    $arr = array();
 180    $editor_local_path  = ckeditor_path(TRUE);
 181    $lang_dir     = $editor_local_path .'/editor/lang';
 182    if (is_dir($lang_dir)) {
 183      $dh = @opendir($lang_dir);
 184      if (FALSE !== $dh ) {
 185        while (($file = readdir($dh)) !== FALSE) {
 186          if (in_array($file, array(".", "..", "CVS", ".svn"))) {
 187            continue;
 188          }
 189          $matches = array();
 190          if (is_file($lang_dir . DIRECTORY_SEPARATOR . $file) && preg_match('/^(.*?)\.js$/', $file, $matches)) {
 191            $lang = $matches[1];
 192            $arr[$lang] = drupal_strtoupper($lang);
 193          }
 194        }
 195        closedir( $dh );
 196      }
 197    }
 198  
 199    //oops, we have no information about languages, let's use those available in CKEditor 2.4.3
 200    if (empty($arr)) {
 201      $arr = array(
 202        'af' => 'Afrikaans',
 203        'ar' => 'Arabic',
 204        'bg' => 'Bulgarian',
 205        'bn' => 'Bengali/Bangla',
 206        'bs' => 'Bosnian',
 207        'ca' => 'Catalan',
 208        'cs' => 'Czech',
 209        'da' => 'Danish',
 210        'de' => 'German',
 211        'el' => 'Greek',
 212        'en' => 'English',
 213        'en-au' => 'English (Australia)',
 214        'en-ca' => 'English (Canadian)',
 215        'en-uk' => 'English (United Kingdom)',
 216        'eo' => 'Esperanto',
 217        'es' => 'Spanish',
 218        'et' => 'Estonian',
 219        'eu' => 'Basque',
 220        'fa' => 'Persian',
 221        'fi' => 'Finnish',
 222        'fo' => 'Faroese',
 223        'fr' => 'French',
 224        'gl' => 'Galician',
 225        'he' => 'Hebrew',
 226        'hi' => 'Hindi',
 227        'hr' => 'Croatian',
 228        'hu' => 'Hungarian',
 229        'it' => 'Italian',
 230        'ja' => 'Japanese',
 231        'km' => 'Khmer',
 232        'ko' => 'Korean',
 233        'lt' => 'Lithuanian',
 234        'lv' => 'Latvian',
 235        'mn' => 'Mongolian',
 236        'ms' => 'Malay',
 237        'nb' => 'Norwegian Bokmal',
 238        'nl' => 'Dutch',
 239        'no' => 'Norwegian',
 240        'pl' => 'Polish',
 241        'pt' => 'Portuguese (Portugal)',
 242        'pt-br' => 'Portuguese (Brazil)',
 243        'ro' => 'Romanian',
 244        'ru' => 'Russian',
 245        'sk' => 'Slovak',
 246        'sl' => 'Slovenian',
 247        'sr' => 'Serbian (Cyrillic)',
 248        'sr-latn' => 'Serbian (Latin)',
 249        'sv' => 'Swedish',
 250        'th' => 'Thai',
 251        'tr' => 'Turkish',
 252        'uk' => 'Ukrainian',
 253        'vi' => 'Vietnamese',
 254        'zh' => 'Chinese Traditional',
 255        'zh-cn' => 'Chinese Simplified',
 256      );
 257    }
 258  
 259    asort($arr);
 260  
 261    return $arr;
 262  }
 263  
 264  function ckeditor_user_get_setting($user, $profile, $setting) {
 265    $default = array(
 266      'default' => 't',
 267      'show_toggle' => 't',
 268      'popup' => 'f',
 269      'skin' => 'kama',
 270      'toolbar' => 'default',
 271      'expand' => 't',
 272      'width' => '100%',
 273      'lang' => 'en',
 274      'auto_lang' => 't',
 275    );
 276  
 277    if ($profile->settings['allow_user_conf']) {
 278      $status = isset($user->{'ckeditor_'. $setting}) ? $user->{'ckeditor_'. $setting} : (isset($profile->settings[$setting]) ? $profile->settings[$setting] : $default[$setting]);
 279    }
 280    else {
 281      $status = isset($profile->settings[$setting]) ? $profile->settings[$setting] : $default[$setting];
 282    }
 283  
 284    return $status;
 285  }
 286  
 287  function ckeditor_user_get_profile($user, $element_id = NULL) {
 288    $rids = array();
 289  
 290    // Since ckeditor_profile_load() makes a db hit, only call it when we're pretty sure
 291    // we're gonna render ckeditor.
 292    $sorted_roles = ckeditor_sorted_roles();
 293    foreach (array_keys($sorted_roles) as $rid) {
 294      if (isset($user->roles[$rid])) {
 295        $rids[] = $rid;
 296      }
 297    }
 298  
 299    if ($user->uid == 1 && !sizeof($rids)) {
 300      $r = db_fetch_object(db_query_range("SELECT r.rid FROM {ckeditor_role} r ORDER BY r.rid DESC", 1));
 301      $rids[] = $r->rid;
 302    }
 303  
 304    $profile_names = array();
 305    if (sizeof($rids)) {
 306      $result = db_query("SELECT r.rid, s.name FROM {ckeditor_settings} s INNER JOIN {ckeditor_role} r ON r.name = s.name WHERE r.rid IN (". implode(",", $rids) .")");
 307      while (($row = db_fetch_array($result))) {
 308        if (!isset($profile_names[$row['rid']])) {
 309          $profile_names[$row['rid']] = array();
 310        }
 311        array_push($profile_names[$row['rid']], $row['name']);
 312      }
 313    }
 314  
 315    foreach ($rids as $rid) {
 316      if (!empty($profile_names[$rid])) {
 317        foreach ($profile_names[$rid] as $profile_name) {
 318          $profile = ckeditor_profile_load($profile_name);
 319  
 320          $conf = $profile->settings;
 321          $enabled = ckeditor_is_enabled(empty($conf['excl_mode']) ? '0' : $conf['excl_mode'], empty($conf['excl_regex']) ? '' : $conf['excl_regex'], $element_id, $_GET['q']);
 322  
 323          if ($enabled) {
 324            return $profile;
 325          }
 326        }
 327      }
 328    }
 329  
 330    return FALSE;
 331  }
 332  
 333  /**
 334   * sort roles according to precedence settings. previously sorted roles are followed by latest added roles.
 335   */
 336  function ckeditor_sorted_roles($clear = FALSE) {
 337    static $order;
 338    if (isset($order) && $clear !== TRUE) {
 339      return $order;
 340    }
 341    $order = array();
 342    $roles = user_roles(0, 'access ckeditor');
 343  
 344    $result = db_query("SELECT settings FROM {ckeditor_settings} WHERE name='CKEditor Global Profile'");
 345    $data = db_fetch_object($result);
 346    if (!empty($data->settings)) {
 347      $settings = unserialize($data->settings);
 348      if (isset($settings['rank']) && !empty($settings['rank']))
 349      foreach ($settings['rank'] as $rid) {
 350        if (isset($roles[$rid])) {
 351          $order[$rid] = $roles[$rid];
 352          unset($roles[$rid]);
 353        }
 354      }
 355    }
 356    krsort($roles);//sort the remaining unsorted roles by id, descending.
 357    $order += $roles;
 358    return $order;
 359  }
 360  
 361  /**
 362   * @param int $excl_mode 1/include, exclude otherwise
 363   * @param string $excl_regex paths (drupal paths with ids attached)
 364   * @param string $element_id current ID
 365   * @param string $get_q current path
 366   *
 367   * @return boolean
 368   *    returns true if CKEditor is enabled
 369   */
 370  function ckeditor_is_enabled($excl_mode, $excl_regex, $element_id, $get_q) {
 371    $front = variable_get('site_frontpage', 'node');
 372    $excl_regex = str_replace('<front>', $front, $excl_regex);
 373    $nodetype = ckeditor_get_nodetype($get_q);
 374    $element_id = str_replace('.', '\.', $element_id);
 375  
 376    $match = !empty($excl_regex) && preg_match($excl_regex, $nodetype .'@'. $get_q .'.'. $element_id);
 377  
 378    return ($excl_mode == '0' xor $match);
 379  }
 380  
 381  function _ckeditor_script_path() {
 382    $jspath = FALSE;
 383    $module_path=drupal_get_path('module', 'ckeditor');
 384  
 385    if (file_exists($module_path . '/ckeditor/ckeditor.js')) {
 386      $jspath='%m/ckeditor';
 387    }
 388    elseif (file_exists($module_path . '/ckeditor/ckeditor/ckeditor.js')) {
 389      $jspath='%m/ckeditor/ckeditor';
 390    }
 391    elseif (file_exists('sites/all/libraries/ckeditor/ckeditor.js')) {
 392      $jspath='%b/sites/all/libraries/ckeditor';
 393    }
 394    return $jspath;
 395  }
 396  
 397  /**
 398   * Determines whether the CKEditor sources are present
 399   *
 400   * It checks if ckeditor.js is present.
 401   *
 402   * This function is used by ckeditor_requirements()
 403   *
 404   * @return boolean True if CKEditor is installed
 405   */
 406  function _ckeditor_requirements_isinstalled() {
 407    $editor_path = ckeditor_path(TRUE);
 408    $jspath = $editor_path .'/ckeditor.js';
 409    $jsp=file_exists($jspath);
 410    if (!$jsp && ($editor_path = _ckeditor_script_path())) {
 411      $result = db_query("SELECT name, settings FROM {ckeditor_settings} WHERE name = 'CKEditor Global Profile'");
 412      if ($rs=db_fetch_array($result)) {
 413        $rs['settings']=unserialize($rs['settings']);
 414        $rs['settings']['ckeditor_path']=$editor_path;
 415        $rs['settings']=serialize($rs['settings']);
 416        db_query("UPDATE {ckeditor_settings} SET settings='%s' WHERE name = 'CKEditor Global Profile'", $rs['settings']);
 417        $jsp=TRUE;
 418        ckeditor_path(TRUE, TRUE);
 419      }
 420    }
 421    return $jsp;
 422  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7