source: extensions/ContactForm/include/cf_functions.inc.php @ 7197

Last change on this file since 7197 was 7197, checked in by plg, 14 years ago

feature 1897 added: the core "Contact Webmaster" link in footer is replaced by
a link to the ContactForm page. It works with a smarty prefilter, so it should
work with all themes.

The configurable template variable name was removed. It is hard coded as
$ContactFormLink. I've seen no example using another variable name. It was
hard to understand for users in the administration panel and not very useful.
The corresponding language keys were removed from all languages.

bug 1916 fixed: apply the same rule for link display in footer as in menu. If
the administrator has allowed the ContactForm page for guests, let's display
it in the menu + footer.

File size: 6.6 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4// Include language advices
5load_language('plugin.lang', CF_PATH);
6
7/**
8 * Include class file
9 * @param $aClassName
10 */
11function cf_require_class($aClassName) {
12  require_once CF_CLASSES .strtolower($aClassName) . '.class.php';
13}
14
15include_once(PHPWG_ROOT_PATH . 'include/functions_mail.inc.php');
16
17function cf_switch_to_default_lang() {
18  global $switch_lang,$user;
19  if (!isset($switch_lang['stack']) or
20      !in_array($user['language'], $switch_lang['stack'])) {
21    $switch_lang['stack'][] = $user['language'];
22  }
23  switch_lang_to(get_default_language());
24  // Include language advices
25  load_language('plugin.lang', CF_PATH);
26}
27function cf_switch_back_to_user_lang() {
28  switch_lang_back();
29}
30function cf_validate_mail_format($email) {
31  $atom   = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]';   // before  arobase
32  $domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)'; // domain name
33  $regex  = '/^' . $atom . '+' . '(\.' . $atom . '+)*';
34  $regex .= '@' . '(' . $domain . '{1,63}\.)+' . $domain . '{2,63}$/i';
35  if ( !preg_match( $regex, $email ) ) {
36    return l10n('cf_mail_format_error');
37  }
38  return null;
39}
40
41function cf_get_admins_emails($webmaster_email) {
42  global $conf, $user;
43  $admins = array();
44
45  $query = '
46select
47  U.'.$conf['user_fields']['username'].' as username,
48  U.'.$conf['user_fields']['email'].' as mail_address
49from
50  '.USERS_TABLE.' as U,
51  '.USER_INFOS_TABLE.' as I
52where
53  I.user_id =  U.'.$conf['user_fields']['id'].' and
54  I.status in (\'webmaster\',  \'admin\') and
55  I.adviser = \'false\' and
56  '.$conf['user_fields']['email'].' is not null and
57  I.user_id <> '.$user['id'].'
58order by
59  username
60';
61
62  $datas = pwg_query($query);
63  if (!empty($datas)) {
64    while ($admin = mysql_fetch_array($datas)) {
65      if (!empty($admin['mail_address']) and
66          (0!=strcasecmp($admin['mail_address'], $webmaster_email))) {
67        array_push( $admins,
68                    format_email($admin['username'], $admin['mail_address'])
69                  );
70      }
71    }
72  }
73  return $admins;
74}
75
76function cf_get_admins_contacts() {
77  global $conf, $user;
78  $admins = array();
79
80  $query = '
81select
82  U.'.$conf['user_fields']['username'].' as username,
83  U.'.$conf['user_fields']['email'].' as mail_address
84from
85  '.USERS_TABLE.' as U,
86  '.USER_INFOS_TABLE.' as I
87where
88  I.user_id =  U.'.$conf['user_fields']['id'].' and
89  I.status in (\'webmaster\',  \'admin\') and
90  I.adviser = \'false\' and
91  '.$conf['user_fields']['email'].' is not null
92order by
93  username
94';
95 
96  $webmaster_mail = get_webmaster_mail_address();
97  $datas = pwg_query($query);
98  if (!empty($datas)) {
99    while ($admin = mysql_fetch_array($datas)) {
100      if (!empty($admin['mail_address'])) {
101        $name = $admin['username'];
102        $webmaster = 0;
103        if (0 == strcasecmp($webmaster_mail, $admin['mail_address'])) {
104          $name = l10n('Webmaster');
105          $webmaster = 1;
106        }
107        $admins[$admin['mail_address']] = array(
108            'NAME'     => $name,
109            'EMAILSTR' => format_email($name,
110                                       $admin['mail_address']),
111            'ACTIVE'   => 1,
112            'WEBMASTER'=> $webmaster,
113          );
114      }
115    }
116  }
117  return $admins;
118 
119}
120
121/* Return template for user template/theme*/
122function cf_get_template($file, $dir=CF_TEMPLATE, $prefix='') {
123  global $user, $template;
124
125  $theme_file = $dir.
126                //$user[$prefix.'template'].'/'.
127                //$user['themes'].'/'.
128                $file;
129  $template_file = $dir.
130                   //$user[$prefix.'template'].'/'.
131                   $file;
132
133  if (file_exists($theme_file))
134  {
135    return $theme_file;
136  }
137  else
138  {
139    return $dir.$file;
140  }
141}
142
143function contactForm_prefilter($content, &$smarty) {
144  $search = '#{if\s+isset\s*\(\s*\$CONTACT_MAIL\s*\)\s*}.*?{/if}#s';
145  $replacement = '{if isset($ContactFormLink)}{$ContactFormLink}{/if}';
146
147  return preg_replace($search, $replacement, $content);
148}
149 
150function cf_clean_obsolete_files($obsolete_file_list) {
151  if (!file_exists(CF_PATH.$obsolete_file_list)) {
152    return TRUE;
153  }
154  $obsolete = file(CF_PATH.$obsolete_file_list);
155  array_push($obsolete, $obsolete_file_list);
156  return cf_clean_obsolete_list($obsolete);
157}
158
159function cf_clean_obsolete_list($file_list = array(), &$errors = array()) {
160  // Include language advices
161  load_language('plugin.lang', CF_PATH);
162 
163  if (!function_exists('unlink')) {
164      // No unlink available...
165      array_push($errors, l10n('cf_no_unlink'));
166      return FALSE;
167  }
168  $success = TRUE;
169  foreach ($file_list as $file) {
170    $file = CF_PATH . $file;
171    if (file_exists($file)) {
172      // Remove obsolete file
173      $success &= unlink($file);
174    }
175  }
176  if (!$success) {
177      array_push($errors, l10n('cf_unlink_errors'));
178  }
179  return $success;
180}
181
182function cf_format_date($year, $month, $day, $format='%M %D, %Y') {
183  $format = str_ireplace('%Y', $year, $format);
184  $format = str_ireplace('%M', $month, $format);
185  $format = str_ireplace('%D', $day, $format);
186  return $format;
187}
188
189function cf_get_history_list($file_name, &$errors = array()) {
190  // Include language advices
191  load_language('plugin.lang', CF_PATH);
192  global $lang;
193  $month_list = $lang['month'];
194  $history = array();
195  if (!file_exists($file_name)) {
196    array_push($errors, sprintf(l10n('cf_file_not_found'), $file_name));
197    return $history;
198  }
199  $raw_history = file($file_name,
200                      FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
201
202  $index = -1;
203  array_push($errors, sprintf(l10n('cf_file_empty'), $file_name));
204  foreach($raw_history as $new_line) {
205    $pos = strpos($new_line, ' ');
206    switch ($pos) {
207      case 0:
208        // History item
209        if (isset($history[$index]) and is_array($history[$index])) {
210          array_push($history[$index]['CHANGES'], trim($new_line));
211        }
212        break;
213      default:
214        // New history date
215        $index++;
216        list($date, $version) = explode(' ', $new_line);
217        list($year, $month, $day) = explode('-', $date);
218        $date_array = array('RAW' => $date);
219        if (isset($month)) {
220          $month = $month_list[intval($month)];
221        }
222        if (isset($year) and isset($month) and isset($day)) {
223          $date_array['FORMATTED'] = cf_format_date($year,
224                                                    $month,
225                                                    $day,
226                                                    l10n('cf_format_date'));
227        }
228        $history[$index] = array(
229            'DATE'     => $date_array,
230            'VERSION'  => $version,
231            'CHANGES'  => array(),
232          );
233    }
234  }
235  return $history;
236}
237
238?>
Note: See TracBrowser for help on using the repository browser.