gallery_filter.inc 5.91 KB
Newer Older
1 2 3 4 5
<?php
// $Id$

/**
 * gallery.module : gallery_filter.inc
6
 * Gallery Filter functions
7
 */
8

9 10 11 12
/**
 * Function gallery_filter_process().
 * (implementation of the main filter routine)
 */
13
function gallery_filter_process($text) {
14 15 16 17 18 19 20 21 22
  $prefix = trim(variable_get('gallery_filter_prefix', 'G2'));
  preg_match_all("/\[$prefix:(\d+)\s*(.*?)\]/i", $text, $matches, PREG_SET_ORDER);
  
  // Initialize G2 and set default arguments
  if (count($matches) > 0) {
    if (!_gallery_init(TRUE)) {
      return $text;
    }
    $default['n'] = variable_get('gallery_filter_n_images', 1);
23
    $default['type'] = implode('|', array_fill(0, $default['n'], variable_get('gallery_filter_default_block_type', 'recentImage')));
24 25 26 27 28
    $default['maxsize'] = variable_get('gallery_filter_default_maxsize', GALLERY_FILTER_MAXSIZE_DEFAULT); 
    $default['exactsize'] = variable_get('gallery_filter_default_exactsize', GALLERY_FILTER_EXACTSIZE_DEFAULT);
    $default['class'] = variable_get('gallery_filter_default_div_class', 'nowrap');
    $default['album_frame'] = variable_get('gallery_filter_default_album_frame', 'none');
    $default['item_frame'] = variable_get('gallery_filter_default_item_frame', 'none');
29
    $default['show'] = variable_get('gallery_filter_default_show', array('none'));
30 31 32 33 34 35
    $default['target'] = variable_get('gallery_filter_default_link_target', '');
    $default['link'] = variable_get('gallery_filter_default_link', '');
  }
  else {
    return $text;
  }
36

37 38 39 40 41
  $head_array = array();
  // Loop over all matches
  foreach ($matches as $match) {
    // First argument is numeric => valid G2 filter tag
    if (is_numeric($match[1])) {
profix898's avatar
profix898 committed
42
      $params = array('type' => 'specificItem');
43
      $params['itemId'] = intval($match[1]);
profix898's avatar
profix898 committed
44 45 46 47
      if (preg_match('/user(:([\d]+))?/i', $params['itemId'], $param_uid)) {
        require_once(drupal_get_path('module', 'gallery') .'/gallery_user.inc');
        $params['itemId'] = gallery_user_useralbum(isset($param_uid[2]) ? $param_uid[2] : NULL, FALSE);
      }
48 49 50 51
      $args = array_filter(preg_split('/[\s,]+/', $match[2]));
      // Loop over all (optional) arguments
      foreach ($args as $arg) {
        list($key, $value) = array_filter(explode('=', $arg));
52 53
        $key = preg_replace('/\W/', '', $key);
        $params[$key] = _gallery_filter_sanitize($key, $value);
54 55 56 57 58 59 60 61 62 63
      }
      // Treat 'maxsize' and 'size' as the same
      if (isset($params['size'])) {
        $params['maxsize'] = $params['size'];
        unset($params['size']);
      }
      // Carefully treat the default size method (cannot just merge them as the
      // entered value must take precedence over the default)
      if (isset($params['maxsize'])) {
        unset($default['exactsize']);
profix898's avatar
profix898 committed
64 65
      }
      else if (isset($params['exactsize'])) {
66 67 68 69 70
        unset($default['maxsize']);
      }
      // Merge params with default values
      $params = array_merge($default, $params);
      // Transform 'type' into a valid parameter
profix898's avatar
profix898 committed
71 72 73
      if ($params['n'] > 1 && $params['type'] == 'specificItem') {
        $params['type'] = $default['type'];
      }
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
      if (is_array($params['type'])) {
        // Ensure 'type' contains 'n' elements (auto-append if necessary)
        $count = count($params['type']);
        if (($num = $params['n'] - $count) > 0) {
          $params['type'] += array_fill($count, $num, end($params['type']));
        }
      }
      else {
        $params['type'] = array_fill(0, $params['n'], $params['type']);
      }
      // 'frame' overrides 'album_frame' and 'item_frame'
      if ($params['frame']) {
        $params['album_frame'] = $params['item_frame'] = $params['frame'];
      }
      // Convert into G2-compatible arguments
      $params['blocks'] = implode('|', $params['type']);
profix898's avatar
profix898 committed
90
      if (isset($params['maxsize']) && !empty($params['maxsize'])) {
91 92
        $params['maxSize'] = $params['maxsize'];
      }
profix898's avatar
profix898 committed
93
      else if (isset($params['exactsize']) && !empty($params['exactsize'])) {
94 95 96 97
        $params['exactSize'] = $params['exactsize'];
      }
      $params['albumFrame'] = $params['album_frame'];
      $params['itemFrame'] = $params['item_frame'];
profix898's avatar
profix898 committed
98
      $params['show'] = implode('|', $params['show']);
99
      $params['linkTarget'] = $params['target'];
profix898's avatar
profix898 committed
100 101 102 103 104 105 106 107 108 109 110
      // Unset redundant parameters
      unset(
        $params['n'],
        $params['type'],
        $params['exactsize'],
        $params['maxsize'],
        $params['frame'],
        $params['album_frame'],
        $params['item_frame'],
        $params['target']
      );
111
  		// Fetch the images and format output
112 113
      list($ret, $content, $head) = GalleryEmbed::getImageBlock($params);
      if ($ret) {
114
        gallery_error(t('Unable to get Gallery image block'), $ret);
115 116 117
        continue;
      }
      if ($content) {
profix898's avatar
profix898 committed
118 119
        // Allow modules to alter the filter output
        drupal_alter('gallery_filter', $content);
120 121 122
        // Replace G2 filter tag with image block html
        $params['class'] = 'giImageBlock'. ($params['class'] ? ' '. $params['class'] : '');
        $content = '<div class ="'. $params['class'] .'">'. $content .'</div>';
123
        $text = str_replace($match[0], $content, $text);
124
      }
125
      if ($head) {
126 127
        $head_array[] = trim($head);
      }
128
    }
129 130 131 132 133 134 135
  }
  
  // Add html head items and css
  if (count($head_array)) {
    gallery_set_head(implode("\n", array_unique($head_array)));
  }
  GalleryEmbed::done();
136
  
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  return $text .'<br class="giImageBlock-clear-both" />';
}

/**
 * Function _gallery_filter_sanitize().
 * (sanitize filter parameters)
 */
function _gallery_filter_sanitize($key, $value) {
  switch (strtolower($key)) {
    case 'n':
    case 'size':
    case 'maxsize':
    case 'exactsize':
      return intval(preg_replace('/\D/', '', $value));
    case 'class':
    case 'frame':
    case 'album_frame':
    case 'item_frame':
    case 'target':
    case 'link':
      return preg_replace('/\W/', '', $value);
    case 'type':
    case 'show':
      return explode('|', preg_replace('/[^\w\x7c]/', '', $value));
    default :
      return check_plain($value);
  }
  
  return $value;
166
}