source: extensions/ExtendedDescription/include/events.inc.php @ 26424

Last change on this file since 26424 was 26424, checked in by mistic100, 10 years ago

reorganize code, clean EOL, remove [img] (deprecated since 08/2012)

File size: 7.2 KB
Line 
1<?php
2defined('EXTENDED_DESC_PATH') or die('Hacking attempt!');
3
4/**
5 * Parse [lang] tags
6 */
7function get_user_language_desc($desc, $user_lang=null)
8{
9  global $user;
10
11  if (is_null($user_lang))
12  {
13    $user_lang = $user['language'];
14  }
15
16  $small_user_lang = substr($user_lang, 0, 2);
17
18  if (!preg_match('#\[lang=('.$user_lang.'|'.$small_user_lang.')\]#i', $desc))
19  {
20    $user_lang = 'default';
21    $small_user_lang = 'default';
22  }
23
24  if (preg_match('#\[lang=('.$user_lang.'|'.$small_user_lang.')\]#i', $desc))
25  {
26    // la balise avec la langue de l'utilisateur a été trouvée
27    $patterns[] = '#(^|\[/lang\])(.*?)(\[lang=(' . $user_lang . '|' . $small_user_lang . '|all)\]|$)#is';
28    $replacements[] = '';
29    $patterns[] = '#\[lang=(' . $user_lang . '|' . $small_user_lang . '|all)\](.*?)\[/lang\]#is';
30    $replacements[] = '\\1';
31  }
32  else
33  {
34    // la balise avec la langue de l'utilisateur n'a pas été trouvée
35    // On prend tout ce qui est hors balise
36    $patterns[] = '#\[lang=all\](.*?)\[/lang\]#is';
37    $replacements[] = '\\1';
38    $patterns[] = '#\[lang=.*\].*\[/lang\]#is';
39    $replacements[] = '';
40  }
41
42  return preg_replace($patterns, $replacements, $desc);
43}
44
45/**
46 * Parse [lang] tags in keywords url
47 */
48function get_user_language_tag_url($tag)
49{
50  return get_user_language_desc($tag, get_default_language());
51}
52
53/**
54 * Get all variations of [lang] tags
55 */
56function ed_get_all_alt_names($arr, $name)
57{
58  if (preg_match_all('#\[lang=(.*?)\](.*?)\[/lang\]#is', $name, $matches, PREG_SET_ORDER))
59  {
60    foreach ($matches as $match)
61    {
62      @$arr[$match[1]] .= $match[2];
63    }
64  }
65  return $arr;
66}
67
68/**
69 * Parse all ED tags
70 */
71function get_extended_desc($desc, $param='')
72{
73  global $conf, $page;
74
75  // [redirect]
76  // Only parse if category description on category page
77  // or element description on element page
78  // to avoid undesired redirect when parsing other where
79  if (
80    $param != 'subcatify_category_description'
81    and $param != 'main_page_element_description'
82    and ( script_basename() != 'picture' or $param != 'main_page_category_description' )
83    and preg_match('#\[redirect (.*?)\]#i', $desc, $m1)
84  )
85  {
86    if (preg_match('#^(img|cat|search)=(\d*)\.?(\d*|)$#i', $m1[1], $m2))
87    {
88      switch ($m2[1])
89      {
90        case 'img':
91        $url_params = array('image_id' => $m2[2]);
92        if (!empty($m2[3]))
93        {
94          $url_params['category'] = array('id' => $m2[3], 'name' => '', 'permalink' => '');
95        }
96        $url = rtrim(make_picture_url($url_params), '-');
97        break;
98
99        case 'cat':
100        $url_params = array('category' => array('id' => $m2[2], 'name' => '', 'permalink' => ''));
101        $url = rtrim(make_index_url($url_params), '-');
102        break;
103
104        case 'search':
105        $url = make_index_url(array('section' => 'search', 'search' => $m2[2]));
106      }
107    }
108    else
109    {
110      $url = $m1[1];
111    }
112    if (is_admin())
113    {
114      global $header_notes;
115      load_language('plugin.lang', EXTENDED_DESC_PATH);
116      $header_notes[] = sprintf(l10n('This category is redirected to %s'), '<a href="'.$url.'">'.$url.'</a>');
117    }
118    else
119    {
120      redirect($url);
121    }
122  }
123
124  $desc = get_user_language_desc($desc);
125
126  // Remove unparsed redirect tag
127  $patterns[] = '#\[redirect .*?\]#i';
128  $replacements[] = '';
129
130  // [cat=xx]
131  $patterns[] = '#\[cat=(\d*)\]#ie';
132  $replacements[] = ($param == 'subcatify_category_description') ? '' : 'get_cat_thumb("$1")';
133
134  // [img=xx.yy,xx.yy,xx.yy;left|right|;name|titleName|]
135  $patterns[] = '#\[img([^\]]*)\]#ie';
136  $replacements[] = ($param == 'subcatify_category_description') ? '' : '[img] must be replaced by [photo]';
137
138  // [photo id=xx album=yy size=SQ|TH|XXS|XS|S|M|L|XL|XXL html=yes|no link=yes|no]
139  $patterns[] = '#\[photo ([^\]]+)\]#ie';
140  $replacements[] = ($param == 'subcatify_category_description') ? '' : 'get_photo_sized("$1")';
141
142  // [random album=xx size=SQ|TH|XXS|XS|S|M|L|XL|XXL html=yes|no link=yes|no]
143  $patterns[] = '#\[random([^\]]*)\]#ie';
144  $replacements[] = ($param == 'subcatify_category_description') ? '' : 'extdesc_get_random_photo("$1")';
145
146  // [slider album=xx nb_images=yy random=yes|no list=aa,bb,cc size=SQ|TH|XXS|XS|S|M|L|XL|XXL speed=z title=yes|no effect=... arrows=yes|no control=yes|no|thumb elastic=yes|no thumbs_size=dd]
147  $patterns[] = '#\[slider ([^\]]+)\]#ie';
148  $replacements[] = ($param == 'subcatify_category_description') ? '' : 'get_slider("$1")';
149
150  // <!--complete-->, <!--more--> et <!--up-down-->
151  switch ($param)
152  {
153    case 'subcatify_category_description' :
154      $patterns[] = '#^(.*?)(' . preg_quote($conf['ExtendedDescription']['complete']) . '|' . preg_quote($conf['ExtendedDescription']['more']) . '|' . preg_quote($conf['ExtendedDescription']['up-down']) . ').*$#is';
155      $replacements[] = '$1';
156      $desc = preg_replace($patterns, $replacements, $desc);
157      break;
158
159    case 'main_page_category_description' :
160      $patterns[] = '#^.*' . preg_quote($conf['ExtendedDescription']['complete']) . '|' . preg_quote($conf['ExtendedDescription']['more']) . '#is';
161      $replacements[] = '';
162      $desc = preg_replace($patterns, $replacements, $desc);
163
164      if (substr_count($desc, $conf['ExtendedDescription']['up-down']))
165      {
166        list($desc, $conf['ExtendedDescription']['bottom_comment']) = explode($conf['ExtendedDescription']['up-down'], $desc);
167        add_event_handler('loc_end_index', 'add_bottom_description');
168      }
169      break;
170
171    default:
172      $desc = preg_replace($patterns, $replacements, $desc);
173  }
174
175  return $desc;
176}
177
178/**
179 * Parse ED tags on NBM mail
180 */
181function extended_desc_mail_group_assign_vars($assign_vars)
182{
183  if (isset($assign_vars['CPL_CONTENT']))
184  {
185    $assign_vars['CPL_CONTENT'] = get_extended_desc($assign_vars['CPL_CONTENT']);
186  }
187  return $assign_vars;
188}
189
190/**
191 * Add bottom description when <!--up-down--> found
192 */
193function add_bottom_description()
194{
195  global $template, $conf;
196
197  $template->concat('PLUGIN_INDEX_CONTENT_END', '
198    <div class="additional_info">
199    ' . $conf['ExtendedDescription']['bottom_comment'] . '
200    </div>');
201}
202
203/**
204 * Remove categories with <!--hidden-->
205 */
206function ext_remove_cat($tpl_var)
207{
208  global $conf;
209
210  $i=0;
211  while ($i<count($tpl_var))
212  {
213    if (substr_count($tpl_var[$i]['NAME'], $conf['ExtendedDescription']['not_visible']))
214    {
215      array_splice($tpl_var, $i, 1);
216    }
217    else
218    {
219      $i++;
220    }
221  }
222
223  return $tpl_var;
224}
225
226/**
227 * Remove categories with <!--mb-hidden-->
228 */
229function ext_remove_menubar_cats($where)
230{
231  global $conf;
232
233  $query = 'SELECT id, uppercats
234    FROM '.CATEGORIES_TABLE.'
235    WHERE name LIKE "%'.$conf['ExtendedDescription']['mb_not_visible'].'%"';
236
237  $result = pwg_query($query);
238  while ($row = pwg_db_fetch_assoc($result))
239  {
240    $ids[] = $row['id'];
241    $where .= '
242      AND uppercats NOT LIKE "'.$row['uppercats'].',%"';
243  }
244  if (!empty($ids))
245  {
246    $where .= '
247      AND id NOT IN ('.implode(',', $ids).')';
248  }
249  return $where;
250}
251
252/**
253 * Remove elements with <!--hidden-->
254 */
255function ext_remove_image($tpl_var, $pictures)
256{
257  global $conf;
258
259  $i=0;
260  while ($i<count($tpl_var))
261  {
262    if (substr_count($pictures[$i]['name'], $conf['ExtendedDescription']['not_visible']))
263    {
264      array_splice($tpl_var, $i, 1);
265      array_splice($pictures, $i, 1);
266    }
267    else
268    {
269      $i++;
270    }
271  }
272
273  return $tpl_var;
274}
Note: See TracBrowser for help on using the repository browser.