/** * retrieves information by regex from the messages (headers/body) * * if regex matches it will return what is in the first () * * @param string $regex * @param clsMailMsg $oMSG * @param bool $header should be scanned from specified header rather * @return string */ function getfrommmsg($regex, $oMSG, $header = false) { /* header match */ if ($header !== false) { if (isset($oMSG->headers[$header])) { if (preg_match($regex, $oMSG->headers[$header], $m)) { return $m[1]; } } return false; } /* body match */ if (!is_array($oMSG->parts)) { $oMSG->parts = array($oMSG->body); } $oBP = new clsMailMsg(); $oBP->processMessage(implode("\n", $oMSG->parts[0])); if (isset($oBP->headers["Content-Transfer-Encoding"]) && $oBP->headers["Content-Transfer-Encoding"] == "base64") { $bp = explode("\n", base64_decode(implode("\n", $oBP->body))); } else { $bp = $oBP->body; } foreach ($bp as $l) { $l = trim($l); if (preg_match($regex, $l, $m)) { return $m[1]; } } return false; }
function errSend() { /* check for valid email settings */ $settings = new dbSelect("esettings", "cubit"); $settings->run(); if ($settings->num_rows() <= 0) { r2sListSet("emailsettings"); header("Location: email-settings.php"); exit; } $settings->fetch_array(); $server = $settings->d["smtp_host"]; $from = $settings->d["fromname"]; $reply = $settings->d["reply"]; /* build the email */ $data = errData($_GET["id"]); $msg = new clsMailMsg(); $msg->newMessage($from, $reply, "Error Report: {$data['errtime']}", "Error report file attached."); $msg->addAttachment("application/octet-stream", "error{$_GET['id']}-{$data['errtime']}.cer", $data["errdata"]); $md = $msg->getNewMessage(); /* send the email */ /** * ok, so lets stop catching errors because if the email sending fails * we are just going to go back to "an error has occured" */ disableErrorNet(); $smtp = new clsSMTPMail(); $smtp->sendMessages($server, 25, false, false, false, ERRORNET_EMAIL, $md["from"], $md["subject"], $md["body"], $md["headers"]); $OUTPUT = "<h3>Error Report</h3>"; if ($smtp->bool_success !== true) { $OUTPUT .= "Error sending report. Please save report and email it\n\t\t\tto <a class='nav' href='mailto: " . ERRORNET_EMAIL . "'>" . ERRORNET_EMAIL . "</a><br />\n\t\t\t<br />\n\t\t\t<input type='button' value='Save Error Report'\n\t\t\t\tonClick='document.location.href=\"" . relpath("geterror.php") . "?id={$_GET['id']}\";' />"; } else { $OUTPUT .= "Successfully sent report. Thank You."; } return $OUTPUT; }
/** * sends a trh message. * * if you dont pass the trh config variable, it will be fetched automatically. * * @param string $who must be "supp" or "cust", what other side is too me * @param int $id customer/supplier id * @param string $msgtype 6 character message type * @param string $data what to send * @param array $config optionally the companies trh configuration * @return bool */ function send_trhmsg($who, $id, $email, $msgtype, $data, $config = false) { if (defined("CONSOLE")) { print "sending response:\n"; print "\twho: {$who}\n"; print "\tid: {$id}\n"; print "\tmsgtype: {$msgtype}\n"; print "\tdata: {$data}\n"; } require_lib("mail.smtp"); require_lib("mail.msg"); /* configuration */ if ($config === false) { $config = getTrhConfig(); } extract($config); /* determines new message parameters */ $subject = "trh_{$msgtype}"; $body = array(); $attachments = array(); $headers = array(); $compinfo = qryCompInfo(); /* determine my ip address */ if (empty($_SERVER["SERVER_NAME"])) { $myipaddr = "0.0.0.0"; } else { $myipaddr = gethostbyname($_SERVER['SERVER_NAME']); } /* determine key request function */ if ($who == "supp") { $keyFunc = "trhKeySupp"; } else { $keyFunc = "trhKeyCust"; } /* standard message body details */ $body[] = "compname: {$compinfo['compname']}"; $body[] = "ipaddr: {$myipaddr}"; $body[] = "bustel: {$compinfo['tel']}"; $body[] = "fromwho: " . ($who == "supp" ? "cust" : "supp"); $body[] = "email: {$SMTP_FROM}"; switch ($msgtype) { /* request a new key */ case "reqkey": $subject .= " {$data}"; break; /* respond to a newly requested key */ /* respond to a newly requested key */ case "rspkey": /* get my key from this guy */ $qry = new dbSelect("keys", "trh", grp(m("cols", "(key).send_key AS mykey"), m("where", "{$who}id='{$id}'"))); $qry->run(); if ($qry->num_rows() <= 0) { return false; } $d = $qry->fetch_array(); $mykey = $d["mykey"]; $subject .= " {$data}"; $body[] = "mykey: {$mykey}"; break; /* requests a new purchase */ /* requests a new purchase */ case "reqpur": $keys = $keyFunc($id, "recv_key, send_key"); $subject .= " {$keys['send_key']}"; $body[] = "yourkey: {$keys['recv_key']}"; $attachments["data.xml"] = $data; break; /* responds to a new purchase request */ /* responds to a new purchase request */ case "rsppur": $keys = $keyFunc($id, "recv_key, send_key"); $subject .= " {$keys['send_key']}"; $body[] = "yourkey: {$keys['recv_key']}"; $body[] = "purid: {$data['purid']}"; $body[] = "status: {$data['status']}"; break; } /* create new mail message */ $msg = new clsMailMsg(); $msg->newMessage($SMTP_FROM, $SMTP_FROM, $subject, implode("\n", $body), $headers, true); if (count($attachments)) { foreach ($attachments as $fname => $contents) { $msg->addAttachment("text/plain", $fname, $data); } } $smtp = new clsSMTPMail(); $ret = $smtp->sendMessages($SMTP_SERVER, 25, !empty($SMTP_USER), $SMTP_USER, $SMTP_PASS, $email, $msg->getNewMessage()); if ($smtp->bool_success !== true) { return $ret; } return true; }
/** * handles a order request * * @param string $key * @param clsMailMsg $oMSG * @param array $config * @return bool */ function request_order($key, $oMSG, $config) { if (($stds = msg_std($oMSG)) === false) { return false; } list($compname, $ipaddr, $bustel, $fromwho, $email) = $stds; /* other side key */ if (($yourkey = getfrommmsg(REGEX_YOURKEY, $oMSG)) === false) { return false; } /* validate keys */ if (($keyinfo = trhKeyPair($key, $yourkey)) === false) { return false; } $custid = $keyinfo["custid"]; if (count($oMSG->parts) < 2) { print "Invalid message: count(parts) < 2\n"; return false; } $attach = new clsMailMsg(); $attach->processMessage(implode("\r\n", $oMSG->parts[1])); if ($attach->getAttachmentFilename() != "data.xml") { print "Invalid message part. Disposition name != data.xml\n"; return false; } $XML = base64_decode(preg_replace("/[ \r\n\t]/", "", implode("", $attach->body))); global $reqpur_activetag, $purch_info, $purch_items; $reqpur_activetag = $purch_info = $purch_items = array(); $parser = xml_parser_create(); xml_set_element_handler($parser, "stElement", "endElement"); xml_parse($parser, $XML, true); xml_parser_free($parser); $i = grp(m("approved", "n"), m("custid", $custid), m(raw("trhkey"), dbrow("0.0.0.0/0", "{$keyinfo['send_key']}", "{$keyinfo['recv_key']}"))); $purch_info = array_merge($purch_info, $i); foreach ($purch_info as $k => $v) { if (empty($v)) { $purch_info[$k] = raw("NULL"); } } $upd = new dbUpdate("recvpurch", "trh", $purch_info); $upd->run(DB_INSERT); $recvpurch_id = pglib_lastid("trh.recvpurch", "id"); $upd->setTable("recvpurch_items", "trh"); foreach ($purch_items as $pi_det) { unset($pi_det["id"]); $pi_det["recvpurch_id"] = $recvpurch_id; $upd->setOpt($pi_det); $upd->run(DB_INSERT); } print "Purchase inserted.\n"; $userinfo = qryUsers($config["MANAGEUSER"]); msgSend($userinfo["username"], "Purchase received via Transheks. Click <a target='mainframe' href='../transheks/order_approve.php'>here</a> to view."); }