-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebtmessage.php
172 lines (154 loc) · 3.36 KB
/
ebtmessage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* EBTMessage
*
* Message Class for EBText system.
*
* @author Erick Birbe <[email protected]>
* @date 2015-09-04
*/
require_once('ebtglobal.php');
require_once('ebtreader.php');
require_once('ebtwriter.php');
define('EBTMSG_FILE', 0);
define('EBTMSG_CONTENT', 1);
define('EBTMSG_NONE', 2);
class EBTMessage extends EBTGlobal
{
private $_raw;
private $_headers_raw;
private $_headers;
private $_message;
private $_filename;
function __construct($message = NULL, $type = EBTMSG_NONE)
{
parent::__construct();
switch ($type)
{
case EBTMSG_FILE:
$this->read($message);
break;
case EBTMSG_CONTENT:
$this->_raw = $message;
break;
default:
break;
}
return $this;
}
protected function _get_headers_raw()
{
if (!$this->_headers_raw)
{
$this->_headers_raw = split("\n\n", $this->_raw)[0];
}
return $this->_headers_raw;
}
protected function _get_headers()
{
if (!$this->_headers)
{
$vHeadersRaw = split("\n", $this->_get_headers_raw());
$vHeaders = array();
foreach ($vHeadersRaw as $sHeader)
{
$vData = split(": ", $sHeader);
$vNew = array($vData[0] => $vData[1]);
$vHeaders = array_merge($vHeaders, $vNew);
}
$this->_headers = $vHeaders;
}
return $this->_headers;
}
function get_header($name)
{
if (array_key_exists($name, $this->_get_headers()))
{
return $this->_headers[$name];
}
}
function set_header($name, $value = NULL)
{
if ($value)
{
$this->_headers[$name] = $value;
}
return $this;
}
function get_message()
{
if (!$this->_message)
{
$vContent = split("\n\n", $this->_raw);
array_shift($vContent);
$sMsg = '';
foreach($vContent as $sLine)
{
$sMsg .= $sLine . "\n\n";
}
// Cleans the last unnecessary '\n\n' characters.
$this->_message = substr_replace($sMsg, "", -2);
}
return $this->_message;
}
function set_message($msg)
{
$this->_message = $msg;
return $this;
}
function set_receiver($number)
{
$this->header('To', $number);
return $this;
}
function read($filename)
{
$this->_filename = $filename;
$oReader = new EBTReader($filename);
$this->_raw = $oReader->read();
//TODO: Rebuild de message object with the new data loaded.
return $this;
}
function write($filename = NULL)
{
$oWriter = new EBTWriter($this->_headers, $this->_message);
$oWriter->write();
return $this;
}
}
// ---------------------------------------------------------------------
// MAIN
// ---------------------------------------------------------------------
if (!debug_backtrace())
{
/*
// Creating SMS from a content string
$oReader = new EBTReader();
$sContent = $oReader->read();
$oMessage = new EBTMessage($sContent, EBTMSG_CONTENT);
echo $oMessage->get_message();
echo "----";
echo $oMessage->get_header('From');
echo $oMessage->get_header('Alphabet');
echo $oMessage->get_header('Help');
echo "----";
$oMessage->write();
*/
/*
// Creating SMS from a File
$oMessage = new EBTMessage('examples/sms_incoming_file.txt', EBTMSG_FILE);
echo $oMessage->get_message();
echo "----";
echo $oMessage->get_header('From');
echo $oMessage->get_header('Alphabet');
echo $oMessage->get_header('Help');
echo "----";
$oMessage->write();
*/
/*
$oMessage = new EBTMessage();
$oMessage->set_header('To', '04128663381');
$oMessage->set_message("Conchale vale que está pasando Dios mío.");
$oMessage->write();
**/
}