[17483] | 1 | <?php |
---|
| 2 | if (!defined('CONTACT_FORM_PATH')) die('Hacking attempt!'); |
---|
| 3 | |
---|
| 4 | /** |
---|
| 5 | * define page section from url |
---|
| 6 | */ |
---|
| 7 | function contact_form_section_init() |
---|
| 8 | { |
---|
| 9 | global $tokens, $page, $conf; |
---|
| 10 | |
---|
| 11 | if ($tokens[0] == 'contact') |
---|
| 12 | { |
---|
| 13 | $page['section'] = 'contact'; |
---|
| 14 | $page['title'] = '<a href="'.get_absolute_root_url().'">'.l10n('Home').'</a>'.$conf['level_separator'].'<a href="'.CONTACT_FORM_PUBLIC.'">'.l10n('Contact').'</a>'; |
---|
| 15 | } |
---|
| 16 | } |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | * contact page |
---|
| 20 | */ |
---|
| 21 | function contact_form_page() |
---|
| 22 | { |
---|
| 23 | global $page; |
---|
| 24 | |
---|
| 25 | if (isset($page['section']) and $page['section'] == 'contact') |
---|
| 26 | { |
---|
| 27 | include(CONTACT_FORM_PATH . '/include/contact_form.inc.php'); |
---|
| 28 | } |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * public menu link |
---|
| 33 | */ |
---|
| 34 | function contact_form_applymenu($menu_ref_arr) |
---|
| 35 | { |
---|
| 36 | global $conf; |
---|
| 37 | |
---|
| 38 | if ( !$conf['ContactForm']['cf_menu_link'] ) return; |
---|
| 39 | if ( !is_classic_user() and !$conf['ContactForm']['cf_allow_guest'] ) return; |
---|
| 40 | if ( !count(get_contact_emails()) ) return; |
---|
| 41 | |
---|
| 42 | $menu = &$menu_ref_arr[0]; |
---|
| 43 | if (($block = $menu->get_block('mbMenu')) != null) |
---|
| 44 | { |
---|
| 45 | array_push($block->data, array( |
---|
| 46 | 'URL' => CONTACT_FORM_PUBLIC, |
---|
| 47 | 'NAME' => l10n('Contact'), |
---|
| 48 | )); |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /** |
---|
[17491] | 53 | * change contact on link on footer |
---|
| 54 | */ |
---|
| 55 | function contact_form_footer_link($content, &$smarty) |
---|
| 56 | { |
---|
| 57 | $search = '<a href="mailto:{$CONTACT_MAIL}?subject={\'A comment on your site\'|@translate|@escape:url}">'; |
---|
| 58 | $replace = '<a href="'.CONTACT_FORM_PUBLIC.'">'; |
---|
| 59 | return str_replace($search, $replace, $content); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
[17483] | 63 | * init emails list |
---|
| 64 | */ |
---|
| 65 | function contact_form_initialize_emails() |
---|
| 66 | { |
---|
| 67 | global $conf; |
---|
| 68 | |
---|
| 69 | $query = ' |
---|
| 70 | SELECT |
---|
| 71 | u.'.$conf['user_fields']['username'].' AS username, |
---|
| 72 | u.'.$conf['user_fields']['email'].' AS email |
---|
| 73 | FROM '.USERS_TABLE.' AS u |
---|
| 74 | JOIN '.USER_INFOS_TABLE.' AS i |
---|
| 75 | ON i.user_id = u.'.$conf['user_fields']['id'].' |
---|
| 76 | WHERE i.status in (\'webmaster\', \'admin\') |
---|
| 77 | AND '.$conf['user_fields']['email'].' IS NOT NULL |
---|
| 78 | ORDER BY username |
---|
| 79 | ;'; |
---|
| 80 | $result = pwg_query($query); |
---|
| 81 | |
---|
| 82 | $emails = array(); |
---|
| 83 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 84 | { |
---|
| 85 | array_push($emails, array( |
---|
| 86 | 'name' => $row['username'], |
---|
| 87 | 'email' => $row['email'], |
---|
[17945] | 88 | 'active' => 'true', |
---|
[17483] | 89 | )); |
---|
| 90 | } |
---|
| 91 | |
---|
[17945] | 92 | mass_inserts( |
---|
| 93 | CONTACT_FORM_TABLE, |
---|
| 94 | array('name','email','active'), |
---|
[18331] | 95 | $emails |
---|
[17945] | 96 | ); |
---|
| 97 | |
---|
[17483] | 98 | $conf['ContactForm']['cf_must_initialize'] = false; |
---|
| 99 | conf_update_param('ContactForm', serialize($conf['ContactForm'])); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Send comment to subscribers |
---|
| 105 | */ |
---|
| 106 | function send_contact_form(&$comm, $key) |
---|
| 107 | { |
---|
| 108 | global $conf, $page, $template, $conf_mail; |
---|
| 109 | |
---|
| 110 | if (!isset($conf_mail)) |
---|
| 111 | { |
---|
| 112 | $conf_mail = get_mail_configuration(); |
---|
| 113 | } |
---|
| 114 | |
---|
[17945] | 115 | $query = ' |
---|
| 116 | SELECT DISTINCT group_name |
---|
| 117 | FROM '. CONTACT_FORM_TABLE .' |
---|
| 118 | ORDER BY group_name |
---|
| 119 | ;'; |
---|
| 120 | $groups = array_from_query($query, 'group_name'); |
---|
| 121 | |
---|
[17483] | 122 | $comm = array_merge($comm, |
---|
| 123 | array( |
---|
| 124 | 'ip' => $_SERVER['REMOTE_ADDR'], |
---|
| 125 | 'agent' => $_SERVER['HTTP_USER_AGENT'] |
---|
| 126 | ) |
---|
| 127 | ); |
---|
| 128 | |
---|
| 129 | $comment_action='validate'; |
---|
| 130 | |
---|
| 131 | // check key |
---|
| 132 | if (!verify_ephemeral_key(@$key)) |
---|
| 133 | { |
---|
| 134 | $comment_action='reject'; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | // check author |
---|
| 138 | if ( $conf['ContactForm']['cf_mandatory_name'] and empty($comm['author']) ) |
---|
| 139 | { |
---|
| 140 | array_push($page['errors'], l10n('Please enter a name')); |
---|
| 141 | $comment_action='reject'; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | // check email |
---|
| 145 | if ( $conf['ContactForm']['cf_mandatory_mail'] and empty($comm['email']) ) |
---|
| 146 | { |
---|
| 147 | array_push($page['errors'], l10n('Please enter an e-mail')); |
---|
| 148 | $comment_action='reject'; |
---|
| 149 | } |
---|
| 150 | else if ( !empty($comm['email']) and !check_email_validity($comm['email']) ) |
---|
| 151 | { |
---|
| 152 | array_push($page['errors'], l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)')); |
---|
| 153 | $comment_action='reject'; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | // check subject |
---|
| 157 | if (empty($comm['subject'])) |
---|
| 158 | { |
---|
| 159 | array_push($page['errors'], l10n('Please enter a subject')); |
---|
| 160 | $comment_action='reject'; |
---|
| 161 | } |
---|
| 162 | else if (strlen($comm['subject']) < 4) |
---|
| 163 | { |
---|
| 164 | array_push($page['errors'], sprintf(l10n('%s must not be less than %d characters long'), l10n('Subject'), 4)); |
---|
| 165 | $comment_action='reject'; |
---|
| 166 | } |
---|
| 167 | else if (strlen($comm['subject']) > 100) |
---|
| 168 | { |
---|
| 169 | array_push($page['errors'], sprintf(l10n('%s must not be more than %d characters long'), l10n('Subject'), 100)); |
---|
| 170 | $comment_action='reject'; |
---|
| 171 | } |
---|
| 172 | |
---|
[17945] | 173 | // check group |
---|
| 174 | if ( count($groups) > 1 and $comm['group'] == -1 ) |
---|
| 175 | { |
---|
| 176 | $comm['group'] = true; |
---|
| 177 | array_push($page['errors'], l10n('Please choose a category')); |
---|
| 178 | $comment_action='reject'; |
---|
| 179 | } |
---|
| 180 | |
---|
[17483] | 181 | // check content |
---|
| 182 | if (empty($comm['content'])) |
---|
| 183 | { |
---|
| 184 | array_push($page['errors'], l10n('Please enter a message')); |
---|
| 185 | $comment_action='reject'; |
---|
| 186 | } |
---|
| 187 | else if (strlen($comm['content']) < 20) |
---|
| 188 | { |
---|
| 189 | array_push($page['errors'], sprintf(l10n('%s must not be less than %d characters long'), l10n('Message'), 20)); |
---|
| 190 | $comment_action='reject'; |
---|
| 191 | } |
---|
| 192 | else if (strlen($comm['subject']) > 2000) |
---|
| 193 | { |
---|
| 194 | array_push($page['errors'], sprintf(l10n('%s must not be more than %d characters long'), l10n('Message'), 2000)); |
---|
| 195 | $comment_action='reject'; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php'); |
---|
| 199 | include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php'); |
---|
| 200 | |
---|
| 201 | add_event_handler('contact_form_check', 'user_comment_check', |
---|
| 202 | EVENT_HANDLER_PRIORITY_NEUTRAL, 2); |
---|
| 203 | |
---|
| 204 | // perform more spam check |
---|
| 205 | $comment_action = trigger_event('contact_form_check', $comment_action, $comm); |
---|
| 206 | |
---|
| 207 | // get admin emails |
---|
[17945] | 208 | $emails = get_contact_emails($comm['group']); |
---|
[17483] | 209 | if (!count($emails)) |
---|
| 210 | { |
---|
| 211 | array_push($page['errors'], l10n('Error while sending e-mail')); |
---|
| 212 | $comment_action='reject'; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | if ($comment_action == 'validate') |
---|
| 216 | { |
---|
| 217 | // format subject |
---|
| 218 | $prefix = str_replace('%gallery_title%', $conf['gallery_title'], $conf['ContactForm']['cf_subject_prefix']); |
---|
| 219 | $subject = '['.$prefix.'] '.$comm['subject']; |
---|
| 220 | $subject = trim(preg_replace('#[\n\r]+#s', '', $subject)); |
---|
| 221 | $subject = encode_mime_header($subject); |
---|
| 222 | |
---|
| 223 | // format content |
---|
| 224 | if ($conf['ContactForm']['cf_mail_type'] == 'text/html') |
---|
| 225 | { |
---|
| 226 | $comm['content'] = nl2br($comm['content']); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | // format expeditor |
---|
| 230 | if (empty($comm['email'])) |
---|
| 231 | { |
---|
| 232 | $args['from'] = $conf_mail['formated_email_webmaster']; |
---|
| 233 | } |
---|
| 234 | else |
---|
| 235 | { |
---|
| 236 | $args['from'] = format_email($comm['author'], $comm['email']); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | // hearders |
---|
| 240 | $headers = 'From: '.$args['from']."\n"; |
---|
| 241 | $headers.= 'MIME-Version: 1.0'."\n"; |
---|
| 242 | $headers.= 'X-Mailer: Piwigo Mailer'."\n"; |
---|
| 243 | $headers.= 'Content-Transfer-Encoding: 8bit'."\n"; |
---|
| 244 | $headers.= 'Content-Type: '.$conf['ContactForm']['cf_mail_type'].'; charset="'.get_pwg_charset().'";'."\n"; |
---|
| 245 | |
---|
| 246 | set_make_full_url(); |
---|
| 247 | switch_lang_to(get_default_language()); |
---|
| 248 | load_language('plugin.lang', CONTACT_FORM_PATH); |
---|
| 249 | |
---|
| 250 | // template |
---|
| 251 | if ($conf['ContactForm']['cf_mail_type'] == 'text/html') |
---|
| 252 | { |
---|
| 253 | $mail_css = file_get_contents(dirname(__FILE__).'/../template/mail/style.css'); |
---|
| 254 | $template->set_filename('contact_mail', dirname(__FILE__).'/../template/mail/content_html.tpl'); |
---|
| 255 | } |
---|
| 256 | else |
---|
| 257 | { |
---|
| 258 | $mail_css = null; |
---|
| 259 | $template->set_filename('contact_mail', dirname(__FILE__).'/../template/mail/content_plain.tpl'); |
---|
| 260 | } |
---|
| 261 | |
---|
[17662] | 262 | $comm['show_ip'] = isset($conf['contact_form_show_ip']) ? $conf['contact_form_show_ip'] : true; |
---|
| 263 | |
---|
[17483] | 264 | $template->assign(array( |
---|
| 265 | 'cf_prefix' => $prefix, |
---|
| 266 | 'contact' => $comm, |
---|
| 267 | 'GALLERY_URL' => get_gallery_home_url(), |
---|
| 268 | 'PHPWG_URL' => PHPWG_URL, |
---|
| 269 | 'CONTACT_MAIL_CSS' => $mail_css, |
---|
| 270 | )); |
---|
| 271 | |
---|
| 272 | // mail content |
---|
| 273 | $content = $template->parse('contact_mail', true); |
---|
| 274 | $content = wordwrap($content, 70, "\n", true); |
---|
| 275 | |
---|
| 276 | // send mail |
---|
| 277 | $result = |
---|
| 278 | trigger_event('send_mail', |
---|
| 279 | false, /* Result */ |
---|
[17662] | 280 | trigger_event('send_mail_to', implode(',', $emails)), |
---|
[17483] | 281 | trigger_event('send_mail_subject', $subject), |
---|
| 282 | trigger_event('send_mail_content', $content), |
---|
| 283 | trigger_event('send_mail_headers', $headers), |
---|
| 284 | $args |
---|
| 285 | ); |
---|
| 286 | |
---|
| 287 | unset_make_full_url(); |
---|
| 288 | switch_lang_back(); |
---|
| 289 | load_language('plugin.lang', CONTACT_FORM_PATH); |
---|
| 290 | |
---|
| 291 | if ($result == false) |
---|
| 292 | { |
---|
| 293 | array_push($page['errors'], l10n('Error while sending e-mail')); |
---|
| 294 | $comment_action='reject'; |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | return $comment_action; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | /** |
---|
| 302 | * get contact emails |
---|
[17945] | 303 | * @param mixed group: |
---|
| 304 | * - bool true: all emails |
---|
| 305 | * - empty string: emails without group |
---|
| 306 | * - string: emails with the specified group |
---|
[17483] | 307 | */ |
---|
[17945] | 308 | function get_contact_emails($group=true) |
---|
[17483] | 309 | { |
---|
| 310 | global $conf; |
---|
| 311 | |
---|
| 312 | include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php'); |
---|
| 313 | |
---|
[17945] | 314 | $where = '1=1'; |
---|
| 315 | if ($group!==true) |
---|
[17483] | 316 | { |
---|
[17945] | 317 | if (empty($group)) |
---|
[17483] | 318 | { |
---|
[17945] | 319 | $where = 'group_name IS NULL'; |
---|
[17483] | 320 | } |
---|
[17945] | 321 | else |
---|
| 322 | { |
---|
| 323 | $where = 'group_name="'.$group.'"'; |
---|
| 324 | } |
---|
[17483] | 325 | } |
---|
| 326 | |
---|
[17945] | 327 | $query = ' |
---|
| 328 | SELECT * |
---|
| 329 | FROM '. CONTACT_FORM_TABLE .' |
---|
| 330 | WHERE |
---|
| 331 | '.$where.' |
---|
| 332 | AND active = "true" |
---|
| 333 | ORDER BY name ASC |
---|
| 334 | '; |
---|
| 335 | $result = pwg_query($query); |
---|
| 336 | |
---|
| 337 | $emails = array(); |
---|
| 338 | while ($data = pwg_db_fetch_assoc($result)) |
---|
| 339 | { |
---|
| 340 | array_push($emails, format_email($data['name'], $data['email'])); |
---|
| 341 | } |
---|
| 342 | |
---|
[17483] | 343 | return $emails; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | |
---|
| 347 | /** |
---|
| 348 | * check if email is valid |
---|
| 349 | */ |
---|
| 350 | function check_email_validity($mail_address) |
---|
| 351 | { |
---|
| 352 | if (version_compare(PHP_VERSION, '5.2.0') >= 0) |
---|
| 353 | { |
---|
| 354 | return filter_var($mail_address, FILTER_VALIDATE_EMAIL)!==false; |
---|
| 355 | } |
---|
| 356 | else |
---|
| 357 | { |
---|
| 358 | $atom = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]'; // before arobase |
---|
| 359 | $domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)'; // domain name |
---|
| 360 | $regex = '/^' . $atom . '+' . '(\.' . $atom . '+)*' . '@' . '(' . $domain . '{1,63}\.)+' . $domain . '{2,63}$/i'; |
---|
| 361 | |
---|
| 362 | return (bool)preg_match($regex, $mail_address); |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | ?> |
---|