-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpipe.php
213 lines (171 loc) · 6.19 KB
/
pipe.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/local/bin/php
<?php
define('TICKETS_PIPE', true);
error_reporting(1);
$environment = 'development';
$system_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'system';
$application_folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'application';
if (realpath($system_path) !== false) {
$system_path = realpath($system_path) . '/';
}
$system_path = rtrim($system_path, '/') . '/';
define('BASEPATH', str_replace('\\', '/', $system_path));
define('APPPATH', $application_folder . '/');
define('EXT', '.php');
define('ENVIRONMENT', $environment ? $environment : 'development');
define('FCPATH', dirname(__FILE__) . '/');
if (file_exists(APPPATH . 'config/' . ENVIRONMENT . '/constants.php')) {
require(APPPATH . 'config/' . ENVIRONMENT . '/constants.php');
} else {
require(APPPATH . 'config/constants.php');
}
require(BASEPATH . 'core/Common.php');
if ($composer_autoload = config_item('composer_autoload')) {
if ($composer_autoload === true) {
file_exists(APPPATH . 'vendor/autoload.php')
? require_once(APPPATH . 'vendor/autoload.php')
: log_message('error', '$config[\'composer_autoload\'] is set to TRUE but ' . APPPATH . 'vendor/autoload.php was not found.');
} elseif (file_exists($composer_autoload)) {
require_once($composer_autoload);
} else {
log_message('error', 'Could not find the specified $config[\'composer_autoload\'] path: ' . $composer_autoload);
}
} else {
// Fix for user who don't replace all the files during update
if (file_exists(APPPATH . 'vendor/autoload.php')) {
require_once(APPPATH . 'vendor/autoload.php');
}
}
// Only uses the hooks() function.
require_once(APPPATH . 'config/hooks.php');
// Load the classes autoloader
require_once(APPPATH . 'hooks/App_Autoloader.php');
(new App_Autoloader)->register();
if (extension_loaded('mbstring')) {
define('MB_ENABLED', true);
// mbstring.internal_encoding is deprecated starting with PHP 5.6
// and it's usage triggers E_DEPRECATED messages.
@ini_set('mbstring.internal_encoding', $charset);
// This is required for mb_convert_encoding() to strip invalid characters.
// That's utilized by CI_Utf8, but it's also done for consistency with iconv.
mb_substitute_character('none');
} else {
define('MB_ENABLED', false);
}
// There's an ICONV_IMPL constant, but the PHP manual says that using
// iconv's predefined constants is "strongly discouraged".
if (extension_loaded('iconv')) {
define('ICONV_ENABLED', true);
// iconv.internal_encoding is deprecated starting with PHP 5.6
// and it's usage triggers E_DEPRECATED messages.
@ini_set('iconv.internal_encoding', $charset);
} else {
define('ICONV_ENABLED', false);
}
$GLOBALS['CFG'] = & load_class('Config', 'core');
$GLOBALS['UNI'] = & load_class('Utf8', 'core');
if (file_exists(BASEPATH . 'core/Security.php')) {
$GLOBALS['SEC'] = & load_class('Security', 'core');
}
load_class('Router', 'core');
load_class('Input', 'core');
load_class('Lang', 'core');
require(BASEPATH . 'core/Controller.php');
function &get_instance()
{
return CI_Controller::get_instance();
}
$class = 'CI_Controller';
$instance = new $class();
$fd = fopen('php://stdin', 'r');
$input = '';
while (!feof($fd)) {
$input .= fread($fd, 1024);
}
fclose($fd);
require_once(APPPATH . 'hooks/InitHook.php');
_app_init_load();
$instance->load->model('tickets_model');
$instance->load->helper('files');
$instance->load->helper('func');
$instance->load->helper('misc');
$instance->load->helper('database');
$mailParser = new \ZBateson\MailMimeParser\MailMimeParser();
$message = $mailParser->parse($input);
$body = $message->getTextContent();
if (!$body) {
$body = $message->getHtmlContent();
}
$body = trim($body);
if (!$body) {
$body = 'No message found.';
}
$attachments = [];
$mailAttachments = $message->getAllAttachmentParts();
foreach ($mailAttachments as $attachment) {
$filename = $attachment->getHeaderParameter('Content-Disposition', 'filename');
if (empty($filename)) {
$filename = $attachment->getHeaderParameter('Content-Disposition', 'name');
}
if (!$filename) {
continue;
}
$attachments[] = [
'data' => $attachment->getContent(),
'filename' => sanitize_file_name($filename),
];
}
$subject = $message->getHeaderValue('subject');
$fromemail = $message->getHeaderValue('from');
$fromname = '';
if ($fromHeader = $message->getHeader('from')) {
$fromname = $fromHeader->getPersonName();
}
if (empty($fromname)) {
$fromname = $fromemail;
}
if ($reply_to = $message->getHeaderValue('reply-to')) {
$fromemail = $reply_to;
}
$toemails = [];
foreach (['to', 'cc', 'bcc'] as $checkHeader) {
$addreses = $message->getHeader($checkHeader);
if ($addreses) {
foreach ($addreses->getAddresses() as $addr) {
$toemails[] = $addr->getEmail();
}
}
}
$to = implode(',', $toemails);
$body = handle_google_drive_links_in_text($body);
if (class_exists('EmailReplyParser\EmailReplyParser')
&& get_option('ticket_import_reply_only') === '1'
&& (mb_substr_count($subject, 'FWD:') == 0 && mb_substr_count($subject, 'FW:') == 0)) {
$parsedBody = \EmailReplyParser\EmailReplyParser::parseReply($body);
$parsedBody = trim($parsedBody);
// For some emails this is causing an issue and not returning the email, instead is returning empty string
// In this case, only use parsed email reply if not empty
if (!empty($parsedBody)) {
$body = $parsedBody;
}
}
// Trim message
$body = trim($body);
$body = str_replace(' ', ' ', $body);
// Remove html tags - strips inline styles also
$body = trim(strip_html_tags($body, '<br/>, <br>, <a>'));
// Once again do security
$body = $instance->security->xss_clean($body);
// Remove duplicate new lines
$body = preg_replace("/[\r\n]+/", "\n", $body);
// new lines with <br />
$body = preg_replace('/\n(\s*\n)+/', '<br />', $body);
$body = preg_replace('/\n/', '<br />', $body);
$instance->tickets_model->insert_piped_ticket([
'to' => $to,
'fromname' => $fromname,
'email' => $fromemail,
'subject' => $subject,
'body' => $body,
'attachments' => $attachments,
]);