source: extensions/ContactForm/classes/cf_plugin.class.php @ 3746

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

Add default value to language translation

File size: 13.3 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4/**
5 * CF_Plugin class
6 */
7class CF_Plugin {
8  protected $plugin_id;
9  protected $plugin_title;
10  protected $config;
11  protected $debug=array();
12 
13  /* ************************ */
14  /* ** Constructor        ** */
15  /* ************************ */
16
17  function CF_Plugin($plugin_id, $title=CF_TITLE) {
18    $this->plugin_id = $plugin_id;
19    $this->plugin_title = $title;
20    $this->config = new CF_Config();
21    $this->config->set_db_key(CF_CFG_DB_KEY);
22    $this->config->load_config();
23    $this->config->set_value(CF_CFG_COMMENT, CF_CFG_DB_COMMENT);
24  }
25 
26  /* ************************ */
27  /* ** Trigger management ** */
28  /* ************************ */
29 
30  function get_admin_plugin_menu_links($menu) {
31    array_push(
32        $menu,
33        array(
34            'NAME' => $this->get_title(),
35            'URL'  => $this->get_plugin_admin_url()
36        )
37    );
38    return $menu;
39  }
40 
41  function loc_begin_page_header() {
42    global $template;
43    $cf_values = array(
44        'TEXT'  => $this->config->get_lang_value('contact_form_link'),
45        'URL'   => make_index_url(array('section' => CF_URL_PARAMETER)),
46      );
47    $template->assign('CF_FOOTER_VALUES', $cf_values);
48    $template->assign($this->config->get_value(CF_CFG_CONTACT_LINK),
49                      $this->get_html_contact_form_link());
50  }
51 
52  function blockmanager_apply($aMenuRefArray) {
53    if (!$this->check_allowed()) {
54      return;
55    }
56    $menu = &$aMenuRefArray[0];
57    $block_mbMenu = $menu->get_block('mbMenu');
58    if (null == $block_mbMenu) {
59      return;
60    }
61    // Include language advices
62    load_language('plugin.lang', CF_PATH);
63   
64    if (!isset($block_mbMenu->data[CF_MENUBAR_KEY])) {
65      $contact_form_menu = array(
66          'TITLE' => $this->config->get_lang_value('contact_form_title'),
67          'NAME'  => $this->config->get_lang_value('contact_form'),
68          'URL'   => make_index_url(array('section' => CF_URL_PARAMETER)),
69        );
70      $block_mbMenu->data[CF_MENUBAR_KEY] = $contact_form_menu;
71    }
72   
73  }
74 
75  function loc_end_index() {
76    if ('index' != script_basename()) {
77      return;
78    }
79    if (!$this->check_allowed()) {
80      return;
81    }
82    global $tokens;
83    $form_requested = false;
84    foreach($tokens as $token) {
85      if ($token == CF_URL_PARAMETER) {
86        $form_requested = true;
87      }
88    }
89    if ($form_requested) {
90      if (isset($_POST['cf_key'])) {
91        $this->valid_form();
92      } else {
93        $this->display_form($this->create_infos_array());
94      }
95    }
96  }
97
98  function send_mail_content($mail_content) {
99    remove_event_handler('send_mail_content',             
100                          array(&$this, 'send_mail_content'));
101    global $user,$conf_mail,$conf;
102    if (!isset($conf_mail)) {
103      $conf_mail = get_mail_configuration();
104    }
105    $keyargs_content_admin_info = array(
106      get_l10n_args('Connected user: %s', $user['username']),
107      get_l10n_args('IP: %s', $_SERVER['REMOTE_ADDR']),
108      get_l10n_args('Browser: %s', $_SERVER['HTTP_USER_AGENT'])
109    );
110    $newline = "\n";
111    $separator  = $newline;
112    $separator .= str_repeat($this->config->get_value(CF_CFG_SEPARATOR),
113                             $this->config->get_value(CF_CFG_SEPARATOR_LEN));
114    $separator .= $newline;
115    if ('text/html' == $conf_mail['default_email_format']) {
116      $newline = "<br/>";
117      $separator .= '<hr style="width: ';
118      $separator .= $this->config->get_value(CF_CFG_SEPARATOR_LEN);
119      $separator .= 'em; text-align: left;" />';
120    }
121    $footer  = $newline;
122    $footer .= $separator;
123    $footer .= l10n_args($keyargs_content_admin_info, $newline);
124    $footer .= $separator;
125   
126    $mail_content = str_replace(CF_SEPARATOR_PATTERN, $footer, $mail_content);
127    return $mail_content;
128  }
129 
130  function loc_end_page_tail() {
131    CF_Debug::show_debug();
132  }
133 
134  /* ************************ */
135  /* ** Accessors          ** */
136  /* ************************ */
137
138  function get_config() {
139    return $this->config;
140  }
141
142  function get_version() {
143    return $this->config->get_version();
144  }
145 
146  function get_title() {
147    return $this->plugin_title;
148  }
149 
150  /* ************************ */
151  /* ** Private functions  ** */
152  /* ************************ */
153 
154  protected function get_html_contact_form_link() {
155    global $template;
156    $cf_link = array(
157        'TEXT'  => $this->config->get_lang_value('contact_form_link'),
158        'URL'   => make_index_url(array('section' => CF_URL_PARAMETER)),
159      );
160    $template->set_filenames(array(
161        'contact_form_link' => realpath(cf_get_template('cf_link.tpl')),
162      ));
163    $template->assign('CF_LINK', $cf_link);
164   
165    $link = $template->parse('contact_form_link', true);
166    return $link;
167  }
168 
169  protected function display_form($infos) {
170    global $template,$user;
171
172    $template->set_filenames(array(
173        'index'       => realpath(cf_get_template('cf_index.tpl')),
174        'cf_title'    => realpath(cf_get_template('cf_title.tpl')),
175        'cf_form'     => realpath(cf_get_template('cf_form.tpl')),
176        'cf_messages' => realpath(cf_get_template('cf_messages.tpl')),
177      ));
178    $template->block_html_head( '',
179              '<link rel="stylesheet" type="text/css" '.
180              'href="' . CF_INCLUDE . 'contactform.css' . '">',
181              $smarty, $repeat);
182    $template->block_html_head( '',
183              '<script type="text/javascript" '.
184              'src="' . CF_INCLUDE . 'contactform.js' . '">'.
185              '</script>',
186              $smarty, $repeat);
187    $cf = array(
188        'TITLE'     => 'contact_form_title',
189        'NEED_NAME' => $this->config->get_value(CF_CFG_NAME_MANDATORY),
190        'NEED_MAIL' => $this->config->get_value(CF_CFG_MAIL_MANDATORY),
191        'F_ACTION'  => make_index_url(array('section' => CF_URL_PARAMETER)),
192        'LOGGED'    => !is_a_guest(),
193        'ID'        => $infos['cf_id'],
194        'EMAIL'     => $infos['cf_from_mail'],
195        'NAME'      => $infos['cf_from_name'],
196        'SUBJECT'   => $infos['cf_subject'],
197        'MESSAGE'   => $infos['cf_message'],
198        'KEY'       => get_comment_post_key($infos['cf_id']),
199      );
200    if (!empty($infos['errors'])) {
201      $template->assign('errors', $infos['errors']);
202    }
203    if (!empty($infos['infos'])) {
204      $template->assign('infos', $infos['infos']);
205    }
206    $template->assign('CF', $cf);
207    $template->assign_var_from_handle('CF_TITLE', 'cf_title');
208    $template->assign_var_from_handle('CF_MESSAGES', 'cf_messages');
209    $template->assign_var_from_handle('CF_FORM', 'cf_form');
210  }
211
212  protected function redirect($redirect_url, $infos) {
213    global $template;
214    $template->set_filenames(array(
215        'cf_redirect' => realpath(cf_get_template('cf_redirect.tpl')),
216        'cf_title'    => realpath(cf_get_template('cf_title.tpl')),
217        'cf_messages' => realpath(cf_get_template('cf_messages.tpl')),
218      ));
219    $template->block_html_head( '',
220              '<link rel="stylesheet" type="text/css" '.
221              'href="' . CF_INCLUDE . 'contactform.css' . '">',
222              $smarty, $repeat);
223    $cf = array(
224        'TITLE'     => 'contact_redirect_title',
225        'CSS'       => '<link rel="stylesheet" type="text/css" '.
226                       'href="' . CF_INCLUDE . 'contactform.css' . '">'
227      );
228             
229    if (!empty($infos['infos'])) {
230      $template->assign('infos', $infos['infos']);
231    }
232    $template->assign('CF', $cf);
233    $template->assign_var_from_handle('CF_TITLE', 'cf_title');
234    $template->assign_var_from_handle('CF_MESSAGES', 'cf_messages');
235    $redirect_msg = $template->parse('cf_redirect', true);
236    $redirect_delay = $this->config->get_value(CF_CFG_REDIRECT_DELAY);
237    redirect_html($redirect_url, $redirect_msg, $redirect_delay);
238  }
239 
240  protected function valid_form() {
241    if ($this->check_form_params($infos)) {
242      global $template;
243      if (!$this->send_message($infos)) {
244        // Include language advices
245        load_language('plugin.lang', CF_PATH);
246        $this->display_form($infos);
247      } else {
248        $this->redirect(make_index_url(),$infos);
249      }
250    } else {
251      $this->display_form($infos);
252    }
253  }
254 
255  protected function send_message(&$infos) {
256    //redirect(make_index_url());
257//    include(PHPWG_ROOT_PATH . 'include/functions_mail.inc.php');
258    $webmaster_mail = get_webmaster_mail_address();
259    if (null == $webmaster_mail) {
260      $webmaster_mail = '';
261    }
262    $admin_mails = cf_get_admins_emails($webmaster_mail);
263    if (('' == $webmaster_mail) and (0 == count($admin_mails))) {
264      // No admin mail...
265      return true;
266    }
267   
268    global $conf,$user;
269    cf_switch_to_default_lang();
270   
271    $from = format_email($infos['cf_from_name'], $infos['cf_from_mail']);
272    $subject_prefix = $this->config->get_value(CF_CFG_SUBJECT_PREFIX);
273    if (empty($subject_prefix)) {
274      $subject_prefix  = '['.CF_DEFAULT_PREFIX.'] ';
275    }
276    $subject  = '['.$subject_prefix.'] ';
277    $subject .= $infos['cf_subject'];
278    $content  = "\n\n".$infos['cf_message']."\n";
279    $content .= CF_SEPARATOR_PATTERN;
280    $mail_args = array(
281        'from' => $from,
282        'Bcc' => $admin_mails,
283        'subject' => $subject,
284        'content' => $content,
285        'content_format' => 'text/plain',
286      );
287    add_event_handler('send_mail_content',             
288                      array(&$this, 'send_mail_content'));
289    $return = @pwg_mail(
290      $webmaster_mail,
291      $mail_args
292    );
293    cf_switch_back_to_user_lang();
294    if (!$return) {
295      array_push( $infos['errors'], l10n('cf_error_sending_mail'));
296    } else {
297      array_push( $infos['infos'], l10n('cf_sending_mail_successful'));
298    }
299    return $return;
300  }
301 
302  protected function check_form_params(&$infos) {
303    // Include language advices
304    load_language('plugin.lang', CF_PATH);
305   
306    $infos = $this->create_infos_array(false);
307    $params_ok = true;
308    $return = '';
309    $value = '';
310    // Key
311    if ($this->check_key()) {
312      $infos['cf_id'] = trim( stripslashes($_POST['cf_id']));
313    } else {
314      $infos['cf_id'] = rand();
315      $params_ok = false;
316      array_push( $infos['errors'], l10n('cf_form_error'));
317    }
318    // From name
319    if (isset($_POST['cf_from_name'])) {
320      $value = trim( stripslashes($_POST['cf_from_name']) );
321      if (strlen($value) > 0) {
322        $infos['cf_from_name'] = $value;
323      } else {
324        array_push( $infos['errors'], l10n('cf_from_name_error'));
325        $params_ok = false;
326      }
327    } else {
328      array_push( $infos['errors'], l10n('cf_from_name_error'));
329      $params_ok = false;
330    }
331    // From mail
332    if (isset($_POST['cf_from_mail'])) {
333      $value = trim( stripslashes($_POST['cf_from_mail']) );
334      $return = cf_validate_mail_format($value);
335      if (null == $return) {
336        $infos['cf_from_mail'] = $value;
337      } else {
338        $params_ok = false;
339        array_push( $infos['errors'], $return);
340      }
341    } else {
342      array_push( $infos['errors'], l10n('cf_mail_format_error'));
343      $params_ok = false;
344    }
345    // Subject
346    if (isset($_POST['cf_subject'])) {
347      $value = trim( stripslashes($_POST['cf_subject']) );
348      if (strlen($value) > 0) {
349        $infos['cf_subject'] = $value;
350      } else {
351        array_push( $infos['errors'], l10n('cf_subject_error'));
352        $params_ok = false;
353      }
354    } else {
355      array_push( $infos['errors'], l10n('cf_subject_error'));
356      $params_ok = false;
357    }
358    // Message
359    if (isset($_POST['cf_message'])) {
360      $value = trim( stripslashes($_POST['cf_message']) );
361      if (strlen($value) > 0) {
362        $infos['cf_message'] = $value;
363      } else {
364        array_push( $infos['errors'], l10n('cf_message_error'));
365        $params_ok = false;
366      }
367    } else {
368      array_push( $infos['errors'], l10n('cf_message_error'));
369      $params_ok = false;
370    }
371    return $params_ok;
372  }
373 
374  protected function check_key() {
375    global $conf;
376    $key='';
377    $id=0;
378    if (isset($_POST['cf_key'])) {
379      $key = trim( stripslashes($_POST['cf_key']));
380    }
381    if (isset($_POST['cf_id'])) {
382      $id = trim( stripslashes($_POST['cf_id']));
383    }
384   
385    $key = explode( ':', $key );
386    if ( count($key)!=2
387          or $key[0]>time()-2 // page must have been retrieved more than 2 sec ago
388          or $key[0]<time()-3600 // 60 minutes expiration
389          or hash_hmac(
390                'md5', $key[0].':'.$id, $conf['secret_key']
391              ) != $key[1]
392        )
393    {
394      return false;
395    }
396    return true;
397  }
398 
399  protected function create_infos_array($fill=true) {
400    $infos = array(
401        'cf_id'         => '',
402        'cf_from_name'  => '',
403        'cf_from_mail'  => '',
404        'cf_subject'    => '',
405        'cf_message'    => '',
406        'errors'        => array(),
407        'infos'         => array(),
408      );
409    if ($fill) {
410      global $user;
411      // Include language advices
412      load_language('plugin.lang', CF_PATH);
413     
414      $infos['cf_id'] = rand();
415      $infos['cf_from_name'] = is_a_guest()?'':$user['username'];
416      $infos['cf_from_mail'] = $user['email'];
417      $infos['cf_subject'] = l10n('title_send_mail');
418    }
419    return $infos;
420  }
421
422  protected function check_allowed() {
423    if (is_a_guest() and !$this->config->get_value(CF_CFG_ALLOW_GUEST)) {
424      // Not allowed
425      return false;
426    }
427    return true;
428  }
429  protected function get_plugin_admin_url() {
430    return get_admin_plugin_menu_link(CF_PATH . 'config.php');
431  }
432}
433?>
Note: See TracBrowser for help on using the repository browser.