[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/transliteration/ -> transliteration.module (source)

   1  <?php
   2  // $Id: transliteration.module,v 1.5.2.7 2010/03/15 06:42:43 smk Exp $
   3  
   4  /**
   5   * @file
   6   * Converts non-latin text to US-ASCII and sanitizes file names.
   7   *
   8   * @author Stefan M. Kudwien (http://drupal.org/user/48898)
   9   */
  10  
  11  /**
  12   * Implements hook_menu().
  13   */
  14  function transliteration_menu() {
  15    $items['admin/settings/file-system/settings'] = array(
  16      'title' => 'Settings',
  17      'file path' => drupal_get_path('module', 'system'),
  18      'weight' => -10,
  19      'type' => MENU_DEFAULT_LOCAL_TASK,
  20    );
  21    $items['admin/settings/file-system/transliteration'] = array(
  22      'title' => 'Transliteration',
  23      'description' => 'Convert existing file names to US-ASCII.',
  24      'page callback' => 'drupal_get_form',
  25      'page arguments' => array('transliteration_retroactive'),
  26      'access arguments' => array('administer site configuration'),
  27      'file' => 'transliteration.admin.inc',
  28      'weight' => 10,
  29      'type' => MENU_LOCAL_TASK,
  30    );
  31    return $items;
  32  }
  33  
  34  /**
  35   * Implements hook_form_FORM_ID_alter().
  36   *
  37   * Add transliteration settings to the file system configuration form.
  38   */
  39  function transliteration_form_system_file_system_settings_alter(&$form, &$form_state) {
  40    $form['transliteration'] = array(
  41      '#type' => 'item',
  42      '#title' => t('Transliteration'),
  43      '#value' => '',
  44    );
  45    $form['transliteration']['transliteration_file_uploads'] = array(
  46      '#type' => 'checkbox',
  47      '#title' => t('Transliterate file names during upload.'),
  48      '#description' => t('Enable to convert file names to US-ASCII character set for cross-platform compatibility.'),
  49      '#default_value' => variable_get('transliteration_file_uploads', TRUE),
  50    );
  51    $form['transliteration']['transliteration_file_lowercase'] = array(
  52      '#type' => 'checkbox',
  53      '#title' => t('Lowercase transliterated file names.'),
  54      '#default_value' => variable_get('transliteration_file_lowercase', TRUE),
  55      '#description' => t('This is a recommended setting to prevent issues with case-insensitive file systems. It has no effect if transliteration has been disabled.'),
  56    );
  57    $form['buttons']['#weight'] = 1;
  58  }
  59  
  60  /**
  61   * Transliterate and sanitize a file name.
  62   *
  63   * The resulting file name has white space replaced with underscores, consists
  64   * of only US-ASCII characters, and is converted to lowercase (if configured).
  65   *
  66   * @param $filename
  67   *   A file name.
  68   * @param $source_langcode
  69   *   Optional ISO 639 language code that denotes the language of the input and
  70   *   is used to apply language-specific variations. If the source language is
  71   *   not known at the time of transliteration, it is recommended to set this
  72   *   argument to the site default language to produce consistent results.
  73   *   Otherwise the current display language will be used.
  74   * @return
  75   *   Sanitized file name.
  76   *
  77   * @see language_default()
  78   */
  79  function transliteration_clean_filename($filename, $source_langcode = NULL) {
  80    $filename = transliteration_get($filename, '', $source_langcode);
  81    // Replace whitespace.
  82    $filename = str_replace(' ', '_', $filename);
  83    // Remove remaining unsafe characters.
  84    $filename = preg_replace('![^0-9A-Za-z_.-]!', '', $filename);
  85    // Force lowercase to prevent issues on case-insensitive file systems.
  86    if (variable_get('transliteration_file_lowercase', TRUE)) {
  87      $filename = strtolower($filename);
  88    }
  89    return $filename;
  90  }
  91  
  92  /**
  93   * Transliterate text.
  94   *
  95   * Takes an input string in any language and character set, and tries to
  96   * represent it in US-ASCII characters by conveying, in Roman letters, the
  97   * pronunciation expressed by the text in some other writing system.
  98   *
  99   * @param $text
 100   *   UTF-8 encoded text input.
 101   * @param $unknown
 102   *   Replacement string for characters that do not have a suitable ASCII
 103   *   equivalent.
 104   * @param $source_langcode
 105   *   Optional ISO 639 language code that denotes the language of the input and
 106   *   is used to apply language-specific variations. If the source language is
 107   *   not known at the time of transliteration, it is recommended to set this
 108   *   argument to the site default language to produce consistent results.
 109   *   Otherwise the current display language will be used.
 110   * @return
 111   *   Transliterated text.
 112   *
 113   * @see language_default()
 114   */
 115  function transliteration_get($text, $unknown = '?', $source_langcode = NULL) {
 116    static $loaded = FALSE;
 117    if (!$loaded) {
 118      module_load_include('inc', 'transliteration');
 119      $loaded = TRUE;
 120    }
 121    return transliteration_process($text, $unknown, $source_langcode);
 122  }
 123  
 124  /**
 125   * Implementation of hook_init().
 126   *
 127   * Sanitize file names during upload.
 128   */
 129  function transliteration_init() {
 130    if (!empty($_FILES['files']) && variable_get('transliteration_file_uploads', TRUE)) {
 131      // Figure out language, which is available in $_POST['language'] for node
 132      // forms.
 133      $langcode = NULL;
 134      if (!empty($_POST['language'])) {
 135        $languages = language_list();
 136        if (isset($languages[$_POST['language']])) {
 137          $langcode = $_POST['language'];
 138        }
 139      }
 140      foreach ($_FILES['files']['name'] as $field => $filename) {
 141        // Keep a copy of the unaltered file name.
 142        $_FILES['files']['orig_name'][$field] = $filename;
 143        $_FILES['files']['name'][$field] = transliteration_clean_filename($filename, $langcode);
 144      }
 145    }
 146  }
 147  


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