You are here

textimage.install in Textimage 5.2

File

textimage.install
View source
<?php

/**
 * @file
 */

/**
 * Implementation of hook_requirements().
 */
function textimage_requirements($phase) {

  // Ensure translations don't break at install time
  $t = get_t();
  $requirements['textimage_gd'] = array(
    'title' => $t('GD library'),
  );
  if (function_exists('imagegd2')) {
    $info = gd_info();
    $requirements['textimage_gd']['value'] = $info['GD Version'];

    // Check FreeType support
    if (function_exists('imagettftext') && $info["FreeType Support"]) {
      if (!module_exists('color') && !module_exists('imageapi_gd')) {
        $requirements['textimage_gd']['severity'] = REQUIREMENT_OK;
      }
      else {
        $requirements = array();
      }
    }
    else {
      $requirements['textimage_gd']['severity'] = REQUIREMENT_ERROR;
      $requirements['textimage_gd']['description'] = t('The GD library for PHP is enabled, but was compiled without FreeType support. Please check the <a href="@url">PHP image documentation</a> for information on how to correct this.', array(
        '@url' => 'http://www.php.net/manual/en/ref.image.php',
      ));
    }
  }
  else {
    $requirements['textimage_gd'] = array(
      'value' => $t('Not installed'),
      'severity' => REQUIREMENT_ERROR,
      'description' => $t('The GD library for PHP is missing or outdated. Please check the <a href="@url">PHP image documentation</a> for information on how to correct this.', array(
        '@url' => 'http://www.php.net/manual/en/image.setup.php',
      )),
    );
  }
  return $requirements;
}

/**
 * Implementation of hook_install().
 */
function textimage_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {textimage_preset} (\n        pid INT UNSIGNED NOT NULL PRIMARY KEY,\n        name VARCHAR(255) NOT NULL DEFAULT '',\n        description MEDIUMTEXT NOT NULL,\n        settings TEXT NOT NULL)\n        /*!40100 DEFAULT CHARACTER SET utf8 */");
      db_query("CREATE TABLE {textimage_image} (\n        pid INT UNSIGNED NOT NULL DEFAULT 0,\n        file VARCHAR(255) NOT NULL PRIMARY KEY,\n        data TEXT NOT NULL)\n        /*!40100 DEFAULT CHARACTER SET utf8 */");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {textimage_preset} (\n        pid INTEGER PRIMARY KEY CHECK (pid > 0),\n        name VARCHAR(255) NOT NULL DEFAULT '',\n        description TEXT NOT NULL,\n        settings TEXT NOT NULL DEFAULT '');");
      db_query("CREATE SEQUENCE {textimage_preset_pid_seq} INCREMENT 1 START 1;");
      db_query("CREATE TABLE {textimage_image} (\n        pid INTEGER NOT NULL DEFAULT 0,\n        file VARCHAR(255) PRIMARY KEY,\n        data TEXT NOT NULL DEFAULT '');");
      break;
  }
}

/**
 * Implementation of hook_uninstall().
 */
function textimage_uninstall() {
  include_once drupal_get_path('module', 'textimage') . '/textimage_admin.inc';
  db_query('DROP TABLE {textimage_preset}');
  db_query('DROP TABLE {textimage_image}');
  if ($GLOBALS['db_type'] == 'pgsql') {
    db_query('DROP SEQUENCE {textimage_preset_pid_seq};');
  }
  $path = realpath(file_directory_path() . '/textimage');
  if ($path != FALSE) {
    _textimage_recursive_delete($path);
  }
  db_query("DELETE from {variable} WHERE name LIKE '%%textimage_%%'");
}

/**
 * Implementation of hook_update_N().
 *
 * Install the textimage tables and seperate variable names for captcha specific variables
 */
function textimage_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE {textimage_preset} (\n        pid INT UNSIGNED NOT NULL PRIMARY KEY,\n        name VARCHAR(255) NOT NULL DEFAULT '',\n        settings TEXT NOT NULL DEFAULT '' )\n        /*!40100 DEFAULT CHARACTER SET utf8 */");
      break;
    case 'pgsql':
      $ret[] = update_sql("CREATE TABLE {textimage_preset} (\n        pid INTEGER NOT NULL CHECK (pid > 0),\n        name VARCHAR(255) NOT NULL DEFAULT '',\n        settings TEXT NOT NULL DEFAULT ''\n        PRIMARY KEY (pid));");
      $ret[] = update_sql("CREATE SEQUENCE {textimage_preset_pid_seq} INCREMENT 1 START 1;");
      break;
  }
  return $ret;
}
function textimage_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE {textimage_image} (\n        pid INT UNSIGNED NOT NULL DEFAULT 0,\n        file VARCHAR(255) NOT NULL PRIMARY KEY,\n        data TEXT NOT NULL)\n        /*!40100 DEFAULT CHARACTER SET utf8 */");
      break;
    case 'pgsql':
      $ret[] = update_sql("CREATE TABLE {textimage_image} (\n        pid INTEGER NOT NULL DEFAULT 0,\n        file VARCHAR(255) NOT NULL,\n        data TEXT NOT NULL DEFAULT ''\n        PRIMARY KEY (file));");
      break;
  }
  include_once drupal_get_path('module', 'textimage') . '/textimage_admin.inc';
  foreach (textimage_get_presets() as $preset) {
    $path = realpath(file_directory_path() . '/textimage/' . $preset->name);
    if (is_dir($path)) {
      $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
      foreach ($files as $file => $object) {
        $file = str_replace($path . '/', '', $file);
        $args = explode('/', $file);
        $filename = urldecode(array_pop($args));
        $additional_text = $args;
        preg_match('/\\.([a-z]+)$/i', $filename, $matches);
        $format = $matches[1];
        if ($format == 'jpg') {
          $format = 'jpeg';
        }
        $text = preg_replace('/\\.([a-z]+)$/i', '', $filename);
        db_query("INSERT {textimage_image} (pid, file, data) VALUES (%d, '%s', '%s')", $preset['pid'], file_directory_path() . '/textimage/' . $preset->name . '/' . $file, serialize(array(
          'format' => $format,
          'text' => $text,
          'additional_text' => $additional_text,
        )));
      }
    }
  }
  return $ret;
}
function textimage_update_3() {
  $ret = array();
  include_once drupal_get_path('module', 'textimage') . '/textimage_admin.inc';
  $presets = textimage_get_presets();
  foreach ($presets as $preset) {
    if ($preset->settings['text']['angle'] != 0) {
      $preset->settings['text']['angle'] = -$preset->settings['text']['angle'];
      _textimage_preset_update($preset->pid, $preset->name, $preset->description, $preset->settings);
    }
  }
  return $ret;
}
function textimage_update_4() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("ALTER TABLE {textimage_preset} ADD COLUMN description MEDIUMTEXT NOT NULL");
      break;
    case 'pgsql':
      db_add_column($ret = array(), 'textimage_preset', 'dsecription', 'text', array(
        'not null' => TRUE,
        'default' => '',
      ));
      break;
  }
  include_once drupal_get_path('module', 'textimage') . '/textimage_admin.inc';
  $presets = textimage_get_presets();
  foreach ($presets as $preset) {

    // Update font settings.
    if (!isset($preset->settings['font'])) {
      $preset->settings['font'] = array(
        'file' => $preset->settings['text']['font'],
        'size' => $preset->settings['text']['size'],
        'color' => array(
          'hex' => $preset->settings['text']['color'],
          'opacity' => 100,
        ),
      );
      unset($preset->settings['text']['font']);
      unset($preset->settings['text']['size']);
      unset($preset->settings['text']['color']);
    }

    // Update margin settings
    if (!isset($preset->settings['text']['margin'])) {
      $preset->settings['text']['margin'] = array(
        'top' => $preset->settings['text']['margin_top'],
        'right' => $preset->settings['text']['margin_right'],
        'bottom' => $preset->settings['text']['margin_bottom'],
        'left' => $preset->settings['text']['margin_left'],
      );
      unset($preset->settings['text']['margin_top']);
      unset($preset->settings['text']['margin_right']);
      unset($preset->settings['text']['margin_bottom']);
      unset($preset->settings['text']['margin_left']);
    }

    // Update stroke settings.
    if (!isset($preset->settings['text']['stroke'])) {
      $preset->settings['text']['stroke'] = array(
        'width' => $preset->settings['text']['stroke_width'],
        'color' => $preset->settings['text']['stroke_color'],
      );
      unset($preset->settings['text']['stroke_width']);
      unset($preset->settings['text']['stroke_color']);
    }

    // Update Preview text.
    if (!isset($preset->settings['preview']['text']['default'])) {
      $preset->settings['preview']['text']['default'] = $preset->settings['preview']['text'];
      unset($preset->settings['preview']['text']);
    }

    // Update description.
    if (isset($preset->settings['description'])) {
      $preset->description = $preset->settings['description'];
      unset($preset->settings['description']);
    }
    _textimage_preset_update($preset->pid, $preset->name, $preset->description, $preset->settings);
  }
  return $ret;
}