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

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

Add configuration option to define template variable or not

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