IMAP続き
Bodyの構造解析やらファイルへの保存やら。
構造解析
spam at emielmols dot info
01-Apr-2005 08:47
I've created a function which simply extracts basic message content as a string ($content) and attachments as an array ($attachments). I've tested it with messages from Outlook, Outlook Express and Hotmail. Perhaps it's useful to anyone.
imap_parse_structure()に載ってたサンプルをparse($mbox,$mid)としてでっちあげ。
function parse($mbox, $mid) { (略) return array("content"=>$content,"attachments"=>$attachment); }
手抜き。
サブジェクトの取り出しとかファイルへ保存とか
前述のparse()あたりを使ってコンテンツを取り出してみる。
//subject 取得 $subject = "" ; $name = "" ; $headerInfo = imap_headerinfo($uid,$mail->msgno); $subjects = imap_mime_header_decode ($headerInfo->subject); foreach ($subjects as $chip){ $subject .= $chip->text ; } //コンテンツ解析/処理 $parsed = parse($uid, $mail->msgno); if (isset($parsed["attachments"])){ foreach($parsed["attachments"] as $attachment){ list( $micro, $time ) = explode(' ',microtime()); $time += $micro ; $name = ereg_replace("(.*)(\.[jpg|gif|png|bmp])",$time."\\2",$attachment["filename"]); $fp = fopen(IMAGE_SAVE_DIR."/".$name,"w"); if ($fp){ fwrite($fp,imap_base64($attachment["filedata"])); fclose($fp); } } }
サブジェクト取り出し
基本的に$headerInfo->subjectだけなんだけど、MIMEエンコードされてるからimap_mime_decode()を使ってデコード。
デコード結果は
array( object(encode , text) * )
の形で戻って来るので、配列をくりくり回してtextを結合。
$subjects = imap_mime_header_decode ($headerInfo->subject); foreach ($subjects as $chip){ $subject .= $chip->text ; }
ファイル名の作成
ファイル名を添付されてきたファイル名そのままにすると、重複が起きたりして面倒なので、鯖の処理時間をキーに変名しちゃう。
変名ルールは[処理時間(ミリ秒)].[添付ファイルの拡張子]で。
拡張子はとりあえず[jpg|gif|png|bmp]ぐらいなもんで。
多分これ以外は無いだろう。
list( $micro, $time ) = explode(' ',microtime()); $time += $micro ; $name = ereg_replace("(.*)(\.[jpg|gif|png|bmp])",$time."\\2",$attachment["filename"]);
正規表現で、"."の前後を取り出して「$time+拡張子」の形に変更。
手抜きだけどこんなもんでええやろと。
保存した画像からexif情報取り出し
このへんでやったのを持って来る。
function getExisParams($filename){ if (!ereg(".*\.[jpg|jpeg|tiff]",$filename)){ return array(); } $info = exif_read_data($filename); $result = null; $result->mimetype = $info["MimeType"] ; $result->DateTime = $info["DateTime"] ; if (isset($info["GPSVersion"])){ eval(sprintf("\$result->GPSLatitude = %s + (%s) / 60 + (%s) / 3600;", $info["GPSLatitude"][0], $info["GPSLatitude"][1], $info["GPSLatitude"][2])); eval(sprintf("\$result->GPSLongitude = %s + (%s) / 60 + (%s) / 3600;", $info["GPSLongitude"][0], $info["GPSLongitude"][1], $info["GPSLongitude"][2])); if ($info["GPSMapDatum"] == "WGS-84"){ $result->GPSLatitude -=0.003236111111111111 ; $result->GPSLongitude += .0032027777777777775 ; } } return $result ; }
といっても邪魔臭いデータをフィルタしただけ:-P
WGS-84の時の補正もここでやってデータの中身をGoogle流のTOKYODatumにあわせておく。
おっし
とりあえず必要なデータは集まるようになったぞ。
あとは
- データベースへの格納
- Viewの作成
- エラー処理
あたりだな。