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

Last change on this file since 5137 was 3810, checked in by Criss, 15 years ago

bug 0001150

Add e-mail address management

File size: 6.4 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[$prefix.'theme'].'/'.
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  elseif (file_exists($template_file))
138  {
139    return $template_file;
140  }
141  else
142  {
143    return $dir.$file;
144  }
145}
146 
147function cf_clean_obsolete_files($obsolete_file_list) {
148  if (!file_exists(CF_PATH.$obsolete_file_list)) {
149    return TRUE;
150  }
151  $obsolete = file(CF_PATH.$obsolete_file_list);
152  array_push($obsolete, $obsolete_file_list);
153  return cf_clean_obsolete_list($obsolete);
154}
155
156function cf_clean_obsolete_list($file_list = array(), &$errors = array()) {
157  // Include language advices
158  load_language('plugin.lang', CF_PATH);
159 
160  if (!function_exists('unlink')) {
161      // No unlink available...
162      array_push($errors, l10n('cf_no_unlink'));
163      return FALSE;
164  }
165  $success = TRUE;
166  foreach ($file_list as $file) {
167    $file = CF_PATH . $file;
168    if (file_exists($file)) {
169      // Remove obsolete file
170      $success &= unlink($file);
171    }
172  }
173  if (!$success) {
174      array_push($errors, l10n('cf_unlink_errors'));
175  }
176  return $success;
177}
178
179function cf_format_date($year, $month, $day, $format='%M %D, %Y') {
180  $format = str_ireplace('%Y', $year, $format);
181  $format = str_ireplace('%M', $month, $format);
182  $format = str_ireplace('%D', $day, $format);
183  return $format;
184}
185
186function cf_get_history_list($file_name, &$errors = array()) {
187  // Include language advices
188  load_language('plugin.lang', CF_PATH);
189  global $lang;
190  $month_list = $lang['month'];
191  $history = array();
192  if (!file_exists($file_name)) {
193    array_push($errors, sprintf(l10n('cf_file_not_found'), $file_name));
194    return $history;
195  }
196  $raw_history = file($file_name,
197                      FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
198
199  $index = -1;
200  array_push($errors, sprintf(l10n('cf_file_empty'), $file_name));
201  foreach($raw_history as $new_line) {
202    $pos = strpos($new_line, ' ');
203    switch ($pos) {
204      case 0:
205        // History item
206        if (isset($history[$index]) and is_array($history[$index])) {
207          array_push($history[$index]['CHANGES'], trim($new_line));
208        }
209        break;
210      default:
211        // New history date
212        $index++;
213        list($date, $version) = explode(' ', $new_line);
214        list($year, $month, $day) = explode('-', $date);
215        $date_array = array('RAW' => $date);
216        if (isset($month)) {
217          $month = $month_list[intval($month)];
218        }
219        if (isset($year) and isset($month) and isset($day)) {
220          $date_array['FORMATTED'] = cf_format_date($year,
221                                                    $month,
222                                                    $day,
223                                                    l10n('cf_format_date'));
224        }
225        $history[$index] = array(
226            'DATE'     => $date_array,
227            'VERSION'  => $version,
228            'CHANGES'  => array(),
229          );
230    }
231  }
232  return $history;
233}
234
235?>
Note: See TracBrowser for help on using the repository browser.