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

Last change on this file since 6547 was 6547, checked in by Gotcha, 14 years ago

v1.0.9

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