zdb = new ZuboraDB(); } function decodeMail($mail){ $params = array(); $params['include_bodies'] = true; $params['decode_bodies'] = true; $params['decode_headers'] = true; $decoder = new Mail_mimeDecode($mail); try { $mail = $decoder->decode($params); }catch(Exception $e) { $this->ERROR .= "メール解析時にエラーが発生しました。(".$e->getMessage().")\n"; return false; } if(!isSet($mail->headers['from']) or !isSet($mail->headers['to']) or !isSet($mail->headers["subject"])){ $this->ERROR .= "必要な情報がメールから取得できませんでした。\n"; return false; } $from = Mail_RFC822::parseAddressList($mail->headers['from']); $from = $from[0]->mailbox.'@'.$from[0]->host; $to = Mail_RFC822::parseAddressList($mail->headers['to']); $to = $to[0]->mailbox.'@'.$to[0]->host; list($mbox,$domain) = explode("@",$to); list($prefix,$book,$himoku) = explode(PREFIX_SEP_MAIL,$mbox); $price = $mail->headers["subject"]; $this->checkPrefix($prefix); $this->checkBook($book,$domain); $this->checkHimokuPrice($book,$himoku,$price); $this->checkUser($book,$from); if($this->ERROR != ""){ mb_language("ja"); $headers = 'From: '.$to."\r\n". 'Reply-To: '.$to."\r\n". 'X-Mailer: PHP/'. phpversion(); //常にメインメールアドレスに送信(携帯メールだと文字化けやサーバーにはねられる可能性がある為)$this->zdb->getMainMailAddr($from) mail($from,mb_encode_mimeheader(mb_convert_encoding("ズボラ家計簿エラー!", "ISO-2022-JP","AUTO")),mb_convert_encoding($this->ERROR, "ISO-2022-JP","AUTO"),$headers,"-f$from"); //このエラーをもっと分かりやすくする。 return false; }else{ $this->session["note"] = trim(mb_convert_encoding($mail->body,"UTF-8","ISO-2022-JP")); return true; } } function checkPrefix($prefix){ if(PREFIX != $prefix){ $this->ERROR .= "メールアドレスのプレフィクス指定が間違っています。(".$prefix.")\n"; }else{ $this->session["prefix"] = $prefix; } } function checkBook($book,$domain){ $book = $this->zdb->getBook($book,$domain); if(false === $book){ $this->ERROR .= "投稿アドレスドメインに対応づけられた家計簿が存在しません。\n"; }else{ $this->session["book"] = $book["book"]; } } function checkHimokuPrice($book,$himoku,$price){ if(!is_numeric($price)){ $this->ERROR .= "金額欄が数字ではありません。(".$price.")\n"; $price = 0; }else{ $price = intval($price); } $himoku = $this->zdb->getHimoku($book,$himoku); if(false === $himoku){ $this->ERROR .= "存在しない費目が指定されています。(".$himoku.")\n"; }else{ if(($himoku["plus"] == true and $price < 0)or($himoku["plus"] == false and $price > 0)){ $price = -1 * $price; } $this->session["price"] = $price; $this->session["himoku"] = $himoku["himoku"]; } } function checkUser($book,$from){ $mail = $this->zdb->getMainMailAddr($from); $isSubscribed = $this->zdb->isSubscribed($book,$mail); if(false === $mail){ $this->ERROR .= "この家計簿で有効な送信メールアドレスではありません。(".$from.")\n"; }else{ if($isSubscribed){ $this->session["mail"] = $mail; }else{ $this->ERROR .= "この家計簿に関連付けれていないアドレスから送信されました。(".$mail.")\n"; } } } function doInsert(){ if(!$this->zdb->addBalance($this->session["book"],$this->session["himoku"],$this->session["price"],$this->session["mail"],$this->session["note"])){ $this->ERROR .= $zdb->ERROR; } file_put_contents(dirname(__FILE__)."/log.hide",$this->ERROR."\nsuccess\n\n"."ユーザ: ".$this->session["mail"]." 金額: ".$this->session["price"]." 費目: ".$this->session["himoku"]." ノート: ".$this->session["note"]."\n\n"); } function printError(){ file_put_contents(dirname(__FILE__)."/log.hide","error\n\n".$this->ERROR); } } $dbw = new DBWriter(); if($dbw->decodeMail(file_get_contents('php://stdin'))){ $dbw->doInsert(); }else{ $dbw->printError(); } ?>