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

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

bug 0001147

Replace redirect page by a message on the index one

File size: 5.2 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
76/* Return template for user template/theme*/
77function cf_get_template($file, $dir=CF_TEMPLATE, $prefix='') {
78  global $user, $template;
79
80  $theme_file = $dir.
81                $user[$prefix.'template'].'/'.
82                $user[$prefix.'theme'].'/'.
83                $file;
84  $template_file = $dir.
85                   $user[$prefix.'template'].'/'.
86                   $file;
87
88  if (file_exists($theme_file))
89  {
90    return $theme_file;
91  }
92  elseif (file_exists($template_file))
93  {
94    return $template_file;
95  }
96  else
97  {
98    return $dir.$file;
99  }
100}
101 
102function cf_clean_obsolete_files($obsolete_file_list) {
103  if (!file_exists(CF_PATH.$obsolete_file_list)) {
104    return TRUE;
105  }
106  $obsolete = file(CF_PATH.$obsolete_file_list);
107  array_push($obsolete, $obsolete_file_list);
108  return cf_clean_obsolete_list($obsolete);
109}
110
111function cf_clean_obsolete_list($file_list = array(), &$errors = array()) {
112  // Include language advices
113  load_language('plugin.lang', CF_PATH);
114 
115  if (!function_exists('unlink')) {
116      // No unlink available...
117      array_push($errors, l10n('cf_no_unlink'));
118      return FALSE;
119  }
120  $success = TRUE;
121  foreach ($file_list as $file) {
122    $file = CF_PATH . $file;
123    if (file_exists($file)) {
124      // Remove obsolete file
125      $success &= unlink($file);
126    }
127  }
128  if (!$success) {
129      array_push($errors, l10n('cf_unlink_errors'));
130  }
131  return $success;
132}
133
134function cf_format_date($year, $month, $day, $format='%M %D, %Y') {
135  $format = str_ireplace('%Y', $year, $format);
136  $format = str_ireplace('%M', $month, $format);
137  $format = str_ireplace('%D', $day, $format);
138  return $format;
139}
140
141function cf_get_history_list($file_name, &$errors = array()) {
142  // Include language advices
143  load_language('plugin.lang', CF_PATH);
144  global $lang;
145  $month_list = $lang['month'];
146  $history = array();
147  if (!file_exists($file_name)) {
148    array_push($errors, sprintf(l10n('cf_file_not_found'), $file_name));
149    return $history;
150  }
151  $raw_history = file($file_name,
152                      FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
153
154  $index = -1;
155  array_push($errors, sprintf(l10n('cf_file_empty'), $file_name));
156  foreach($raw_history as $new_line) {
157    $pos = strpos($new_line, ' ');
158    switch ($pos) {
159      case 0:
160        // History item
161        if (isset($history[$index]) and is_array($history[$index])) {
162          array_push($history[$index]['CHANGES'], trim($new_line));
163        }
164        break;
165      default:
166        // New history date
167        $index++;
168        list($date, $version) = explode(' ', $new_line);
169        list($year, $month, $day) = explode('-', $date);
170        $date_array = array('RAW' => $date);
171        if (isset($month)) {
172          $month = $month_list[intval($month)];
173        }
174        if (isset($year) and isset($month) and isset($day)) {
175          $date_array['FORMATTED'] = cf_format_date($year,
176                                                    $month,
177                                                    $day,
178                                                    l10n('cf_format_date'));
179        }
180        $history[$index] = array(
181            'DATE'     => $date_array,
182            'VERSION'  => $version,
183            'CHANGES'  => array(),
184          );
185    }
186  }
187  return $history;
188}
189
190?>
Note: See TracBrowser for help on using the repository browser.