Esempio n. 1
0
 /**
  * Test the escaping of replacement strings for use with
  * preg_replace.
  */
 public function testPregReplacement()
 {
     $example = 'try \\1 and $0, also backslash \\ and dollar $ alone';
     $this->assertEquals('try \\\\1 and \\$0, also backslash \\ and dollar $ alone', preg_replacement_quote($example));
 }
Esempio n. 2
0
function send_template_email($data, $merge, $bulk = false, $want_bounces = false)
{
    // We should have some email templates in INCLUDESPATH/easyparliament/templates/emails/.
    // $data is like:
    // array (
    //  'template'  => 'send_confirmation',
    //  'to'        => '*****@*****.**',
    //  'subject'   => 'Your confirmation email'
    // );
    // $merge is like:
    // array (
    //  'FIRSTNAME' => 'Phil',
    //  'LATNAME'   => 'Gyford'
    //  etc...
    // );
    // In $data, 'template' and 'to' are mandatory. 'template' is the
    // name of the file (when it has '.txt' added to it).
    // We'll get the text of the template and replace all the $merge
    // keys with their tokens. eg, if '{FIRSTNAME}' in the template will
    // be replaced with 'Phil'.
    // Additionally, the first line of a template may start with
    // 'Subject:'. Any text immediately following that, on the same line
    // will be the subject of the email (it will also have its tokens merged).
    // But this subject can be overridden by sending including a 'subject'
    // pair in $data.
    global $PAGE;
    if (!isset($data['to']) || $data['to'] == '') {
        $PAGE->error_message("We need an email address to send to.");
        return false;
    }
    $filename = INCLUDESPATH . "easyparliament/templates/emails/" . $data['template'] . ".txt";
    if (!file_exists($filename)) {
        $PAGE->error_message("Sorry, we could not find the email template '" . _htmlentities($data['template']) . "'.");
        return false;
    }
    // Get the text from the template.
    $handle = fopen($filename, "r");
    $emailtext = fread($handle, filesize($filename));
    fclose($handle);
    // See if there's a default subject in the template.
    $firstline = substr($emailtext, 0, strpos($emailtext, "\n"));
    // Work out what the subject line is.
    if (preg_match("/Subject:/", $firstline)) {
        if (isset($data['subject'])) {
            $subject = trim($data['subject']);
        } else {
            $subject = trim(substr($firstline, 8));
        }
        // Either way, remove this subject line from the template.
        $emailtext = substr($emailtext, strpos($emailtext, "\n"));
    } elseif (isset($data['subject'])) {
        $subject = $data['subject'];
    } else {
        $PAGE->error_message("We don't have a subject line for the email, so it wasn't sent.");
        return false;
    }
    // Now merge all the tokens from $merge into $emailtext...
    $search = array();
    $replace = array();
    foreach ($merge as $key => $val) {
        $search[] = '/{' . $key . '}/';
        $replace[] = preg_replacement_quote($val);
    }
    $emailtext = preg_replace($search, $replace, $emailtext);
    // Send it!
    $success = send_email($data['to'], $subject, $emailtext, $bulk, 'twfy-DO-NOT-REPLY@' . EMAILDOMAIN, $want_bounces);
    return $success;
}