source: branches/feature-module-update/data/class/GC_MobileKaraMail.php @ 15532

Revision 15532, 5.0 KB checked in by nanasess, 17 years ago (diff)

svn:mime-type 修正

  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/**
3 *
4 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
5 *
6 * http://www.lockon.co.jp/
7 *
8 * 空メール受け付けアドレスのコマンド名とトークンの間の区切り文字
9 */
10define('MOBILE_KARA_MAIL_EXTENSION_DELIMITER', '_');
11
12/**
13 * モバイルサイトの空メールを扱うクラス
14 */
15class GC_MobileKaraMail {
16    /**
17     * 環境変数から MTA を判別し、対応する GC_MobileKaraMail またはそのサブクラス
18     * のインスタンスを作成する。
19     *
20     * @return object GC_MobileKaraMail またはそのサブクラスのインスタンスを返す。
21     */
22    function &factory() {
23        if (isset($_ENV['EXTENSION'])) {
24            $objInstance = new GC_MobileKaraMail_Postfix;
25        } elseif (isset($_ENV['DEFAULT'])) {
26            $objInstance = new GC_MobileKaraMail_Qmail;
27        } else {
28            $objInstance = new GC_MobileKaraMail;
29        }
30
31        return $objInstance;
32    }
33
34    /**
35     * 標準入力からメールを読み込み、必要な情報を取得する。
36     *
37     * @return void
38     */
39    function parse() {
40        if (@$this->parsed) {
41            return;
42        }
43
44        require_once DATA_PATH . '/module/Mail/mimeDecode.php';
45
46        $fp = fopen('php://stdin', 'r');
47
48        // From 行を解析する。
49        $from_line = rtrim(fgets($fp));
50        if (preg_match('/^From\\s+"?([^\\s"@]+)"?@([^\\s@]+)/', $from_line, $matches)) {
51            $this->sender = $matches[1] . '@' . $matches[2];
52        } else {
53            trigger_error("Invalid from line: $from_line");
54            $this->sender = null;
55        }
56
57        // 残りのヘッダーを解析する。
58        $data = '';
59        while (!feof($fp)) {
60            $data .= fgets($fp);
61            if (rtrim($data, "\r\n") == '') {
62                break;
63            }
64        }
65        $structure = Mail_mimeDecode::decode(array('input' => $data));
66        $this->recipient = @$structure->headers['to'];
67
68        // 宛先アドレスから拡張部分を取得する。
69        $pos = strpos($this->recipient, MOBILE_KARA_MAIL_ADDRESS_DELIMITER);
70        if ($pos !== false) {
71            $extension_and_domain = substr($this->recipient, $pos + 1);
72            $pos = strpos($extension_and_domain, '@');
73            if ($pos !== false) {
74                $this->extension = substr($extension_and_domain, 0, $pos);
75            } else {
76                $this->extension = $extension_and_domain;
77            }
78        } else {
79            trigger_error("Invalid recipient: {$this->recipient}");
80            $this->extension = null;
81        }
82
83        $this->parsed = true;
84    }
85
86    /**
87     * 配信が完了したことを示す終了ステータスでプログラムを終了する。
88     *
89     * @return void
90     */
91    function success() {
92        exit(0);
93    }
94
95    /**
96     * 一時的なエラーを示す終了ステータスでプログラムを終了する。
97     *
98     * @return void
99     */
100    function temporaryFailure() {
101        exit(75);
102    }
103
104    /**
105     * 送信者のメールアドレスを取得する。
106     *
107     * parse() 実行後に使用すること。
108     *
109     * @return string|false 送信者のメールアドレスを返す。取得できなかった場合は false を返す。
110     */
111    function getSender() {
112        return isset($this->sender) ? $this->sender : false;
113    }
114
115    /**
116     * 宛先のメールアドレスの拡張部分からコマンド名を取得する。
117     *
118     * parse() 実行後に使用すること。
119     *
120     * @return string|false コマンド名を返す。取得できなかった場合は false を返す。
121     */
122    function getCommand() {
123        if (!isset($this->extension)) {
124            return false;
125        }
126
127        $pos = strpos($this->extension, MOBILE_KARA_MAIL_EXTENSION_DELIMITER);
128        if ($pos === false) {
129            return false;
130        }
131
132        return substr($this->extension, 0, $pos);
133    }
134
135    /**
136     * 宛先のメールアドレスの拡張部分からトークンを取得する。
137     *
138     * parse() 実行後に使用すること。
139     *
140     * @return string|false トークンを返す。取得できなかった場合は false を返す。
141     */
142    function getToken() {
143        if (!isset($this->extension)) {
144            return false;
145        }
146
147        $pos = strpos($this->extension, MOBILE_KARA_MAIL_EXTENSION_DELIMITER);
148        if ($pos === false) {
149            return false;
150        }
151
152        return substr($this->extension, $pos + 1);
153    }
154}
155
156/**
157 * モバイルサイトの空メールを扱うクラス (Postfix用)
158 */
159class GC_MobileKaraMail_Postfix extends GC_MobileKaraMail {
160    /**
161     * @see GC_MobileKaraMail::parse()
162     */
163    function parse() {
164        if (@$this->parsed) {
165            return;
166        }
167
168        $this->sender = $_ENV['SENDER'];
169        $this->recipient = $_ENV['RECIPIENT'];
170        $this->extension = $_ENV['EXTENSION'];
171
172        $this->parsed = true;
173    }
174}
175
176/**
177 * モバイルサイトの空メールを扱うクラス (qmail用)
178 */
179class GC_MobileKaraMail_Qmail extends GC_MobileKaraMail {
180    /**
181     * @see GC_MobileKaraMail::parse()
182     */
183    function parse() {
184        if (@$this->parsed) {
185            return;
186        }
187
188        $this->sender = $_ENV['SENDER'];
189        $this->recipient = $_ENV['RECIPIENT'];
190        $this->extension = $_ENV['DEFAULT'];
191
192        $this->parsed = true;
193    }
194
195    /**
196     * 一時的なエラーを示す終了ステータスでプログラムを終了する。
197     *
198     * @return void
199     */
200    function temporaryFailure() {
201        exit(111);
202    }
203}
204?>
Note: See TracBrowser for help on using the repository browser.