Exemplo n.º 1
0
 function handler_send($page)
 {
     $page->changeTpl('emails/send.tpl');
     $page->setTitle('Envoyer un email');
     // action si on recoit un formulaire
     if (Post::has('save')) {
         if (!S::has_xsrf_token()) {
             return PL_FORBIDDEN;
         }
         unset($_POST['save']);
         if (trim(preg_replace('/-- .*/', '', Post::v('contenu'))) != "") {
             Post::set('to_contacts', explode(';', Post::s('to_contacts')));
             Post::set('cc_contacts', explode(';', Post::s('cc_contacts')));
             $data = serialize($_POST);
             XDB::execute('INSERT INTO  email_send_save (uid, data)
                                VALUES  ({?}, {?})
               ON DUPLICATE KEY UPDATE  data = VALUES(data)', S::user()->id('uid'), $data);
         }
         exit;
     } else {
         if (Env::v('submit') == 'Envoyer') {
             S::assert_xsrf_token();
             function getEmails($aliases)
             {
                 if (!is_array($aliases)) {
                     return null;
                 }
                 $uf = new UserFilter(new UFC_Hrpid($aliases));
                 $users = $uf->iterUsers();
                 $ret = array();
                 while ($user = $users->next()) {
                     $ret[] = $user->forlife;
                 }
                 return join(', ', $ret);
             }
             $error = false;
             foreach ($_FILES as &$file) {
                 if ($file['name'] && !PlUpload::get($file, S::user()->login(), 'emails.send', false)) {
                     $page->trigError(PlUpload::$lastError);
                     $error = true;
                     break;
                 }
             }
             if (!$error) {
                 XDB::execute("DELETE FROM  email_send_save\n                                    WHERE  uid = {?}", S::user()->id());
                 $to2 = getEmails(Env::v('to_contacts'));
                 $cc2 = getEmails(Env::v('cc_contacts'));
                 $txt = str_replace('^M', '', Env::v('contenu'));
                 $to = str_replace(';', ',', Env::t('to'));
                 $subj = Env::t('sujet');
                 $from = Env::t('from');
                 $cc = str_replace(';', ',', Env::t('cc'));
                 $bcc = str_replace(';', ',', Env::t('bcc'));
                 $email_regex = '/^[a-z0-9.\\-+_\\$]+@([\\-.+_]?[a-z0-9])+$/i';
                 foreach (explode(',', $to . ',' . $cc . ',' . $bcc) as $email) {
                     $email = trim($email);
                     if ($email != '' && !preg_match($email_regex, $email)) {
                         $page->trigError("L'adresse email " . $email . ' est erronée.');
                         $error = true;
                     }
                 }
                 if (empty($to) && empty($cc) && empty($to2) && empty($bcc) && empty($cc2)) {
                     $page->trigError("Indique au moins un destinataire.");
                     $error = true;
                 }
                 if ($error) {
                     $page->assign('uploaded_f', PlUpload::listFilenames(S::user()->login(), 'emails.send'));
                 } else {
                     $mymail = new PlMailer();
                     $mymail->setFrom($from);
                     $mymail->setSubject($subj);
                     if (!empty($to)) {
                         $mymail->addTo($to);
                     }
                     if (!empty($cc)) {
                         $mymail->addCc($cc);
                     }
                     if (!empty($bcc)) {
                         $mymail->addBcc($bcc);
                     }
                     if (!empty($to2)) {
                         $mymail->addTo($to2);
                     }
                     if (!empty($cc2)) {
                         $mymail->addCc($cc2);
                     }
                     $files =& PlUpload::listFiles(S::user()->login(), 'emails.send');
                     foreach ($files as $name => &$upload) {
                         $mymail->addUploadAttachment($upload, $name);
                     }
                     if (Env::v('wiki') == 'text') {
                         $mymail->setTxtBody(wordwrap($txt, 78, "\n"));
                     } else {
                         $mymail->setWikiBody($txt);
                     }
                     if ($mymail->send()) {
                         $page->trigSuccess("Ton email a bien été envoyé.");
                         $_REQUEST = array('bcc' => S::user()->bestEmail());
                         PlUpload::clear(S::user()->login(), 'emails.send');
                     } else {
                         $page->trigError("Erreur lors de l'envoi du courriel, réessaye.");
                         $page->assign('uploaded_f', PlUpload::listFilenames(S::user()->login(), 'emails.send'));
                     }
                 }
             }
         } else {
             $res = XDB::query("SELECT  data\n                                 FROM  email_send_save\n                                WHERE  uid = {?}", S::i('uid'));
             if ($res->numRows() == 0) {
                 PlUpload::clear(S::user()->login(), 'emails.send');
                 $_REQUEST['bcc'] = S::user()->bestEmail();
             } else {
                 $data = unserialize($res->fetchOneCell());
                 $_REQUEST = array_merge($_REQUEST, $data);
             }
         }
     }
     $uf = new UserFilter(new PFC_And(new UFC_Contact(S::user()), new UFC_Registered()), UserFilter::sortByName());
     $contacts = $uf->getProfiles();
     $page->assign('contacts', $contacts);
     $page->assign('maxsize', ini_get('upload_max_filesize') . 'o');
     $page->assign('user', S::user());
     $preferences = XDB::fetchOneAssoc('SELECT  from_email, from_format
                                          FROM  accounts
                                         WHERE  uid = {?}', S::user()->id());
     if ($preferences['from_email'] == '') {
         $preferences['from_email'] = '"' . S::user()->fullName() . '" <' . S::user()->bestEmail() . '>';
     }
     $page->assign('preferences', $preferences);
 }