Files

294 lines
10 KiB
PHP
Raw Permalink Normal View History

2016-04-25 20:30:23 -05:00
<?php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
2025-03-14 15:19:49 -07:00
Portions created by the Initial Developer are Copyright (C) 2015-2025
2016-04-25 20:30:23 -05:00
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com>
James Rose <james.o.rose@gmail.com>
*/
2022-10-10 16:35:14 -06:00
//includes files
2023-09-16 02:02:38 -06:00
require_once dirname(__DIR__, 2) . "/resources/require.php";
2016-04-25 20:30:23 -05:00
require_once "resources/functions/object_to_array.php";
require_once "resources/functions/parse_message.php";
//get accounts to monitor
2024-09-09 20:38:26 -06:00
$sql = "select d.domain_name, f.* ";
$sql .= "from v_fax as f, v_domains as d ";
2016-04-25 20:30:23 -05:00
$sql .= "where fax_email_connection_host <> '' ";
2024-09-09 20:38:26 -06:00
$sql .= "and f.domain_uuid = d.domain_uuid ";
2016-04-25 20:30:23 -05:00
$sql .= "and fax_email_connection_host is not null ";
$sql .= "and fax_email_outbound_subject_tag is not null ";
$database = database::new();
2019-08-07 18:59:26 -06:00
$result = $database->select($sql, null, 'all');
unset($sql);
2016-04-25 20:30:23 -05:00
function arr_to_map(&$arr){
2023-06-28 10:21:00 -06:00
if (!empty($arr)){
2016-04-25 20:30:23 -05:00
$map = Array();
foreach ($arr as $val){
2016-04-25 20:30:23 -05:00
$map[$val] = true;
}
return $map;
}
return false;
}
2023-06-28 10:21:00 -06:00
if (!empty($result) && @sizeof($result) != 0) {
2016-04-25 20:30:23 -05:00
foreach ($result as $row) {
2024-09-09 20:38:26 -06:00
2016-04-25 20:30:23 -05:00
//get fax server and account connection details
$fax_uuid = $row["fax_uuid"];
$domain_uuid = $row["domain_uuid"];
2024-09-09 20:38:26 -06:00
$domain_name = $row["domain_name"];
2016-04-25 20:30:23 -05:00
$fax_extension = $row["fax_extension"];
$fax_email = $row["fax_email"];
$fax_pin_number = $row["fax_pin_number"];
$fax_caller_id_name = $row["fax_caller_id_name"];
$fax_caller_id_number = $row["fax_caller_id_number"];
$fax_email_connection_type = $row["fax_email_connection_type"];
$fax_email_connection_host = $row["fax_email_connection_host"];
$fax_email_connection_port = $row["fax_email_connection_port"];
$fax_email_connection_security = $row["fax_email_connection_security"];
$fax_email_connection_validate = $row["fax_email_connection_validate"];
$fax_email_connection_username = $row["fax_email_connection_username"];
$fax_email_connection_password = $row["fax_email_connection_password"];
$fax_email_connection_mailbox = $row["fax_email_connection_mailbox"];
$fax_email_outbound_subject_tag = $row["fax_email_outbound_subject_tag"];
2019-09-17 14:39:31 -04:00
$fax_email_outbound_authorized_senders = strtolower($row["fax_email_outbound_authorized_senders"]);
2018-02-25 02:04:28 -07:00
$fax_accountcode = $row["accountcode"];
$fax_toll_allow = $row["fax_toll_allow"];
2016-04-25 20:30:23 -05:00
2024-09-09 20:38:26 -06:00
//send the domain name to the command line output
if (!empty($fax_email_connection_host) && !empty($fax_email_connection_username)) {
echo "Domain Name: ".$domain_name."\n";
}
2023-09-16 02:02:38 -06:00
2024-09-09 20:38:26 -06:00
//get the cover font
2025-03-14 15:19:49 -07:00
$fax_cover_font_default =$settings->get('fax','cover_font');
$fax_cover_font = $settings->get('fax','cover_font');
2024-09-09 20:38:26 -06:00
if(empty($fax_cover_font)) {
$fax_cover_font = $fax_cover_font_default;
}
//get the allowed file extensions
2025-03-14 15:19:49 -07:00
$fax_allowed_extension_default = arr_to_map($settings->get('fax','allowed_extension'));
2023-09-16 02:02:38 -06:00
if($fax_allowed_extension_default == false){
$tmp = array('.pdf', '.tiff', '.tif');
$fax_allowed_extension_default = arr_to_map($tmp);
}
2025-03-14 15:19:49 -07:00
$fax_allowed_extension = arr_to_map($settings->get('fax','allowed_extension'));
if($fax_allowed_extension == false) {
2016-04-25 20:30:23 -05:00
$fax_allowed_extension = $fax_allowed_extension_default;
}
//set needed variables
2025-03-14 15:19:49 -07:00
$fax_page_size = $settings->get('fax','page_size');
$fax_resolution = $settings->get('fax','resolution');
$fax_header = $settings->get('fax','cover_header');
$fax_footer = $settings->get('fax','cover_footer');
2016-04-25 20:30:23 -05:00
$fax_sender = $fax_caller_id_name;
//open account connection
$fax_email_connection = "{".$fax_email_connection_host.":".$fax_email_connection_port."/".$fax_email_connection_type;
$fax_email_connection .= ($fax_email_connection_security != '') ? "/".$fax_email_connection_security : "/notls";
$fax_email_connection .= "/".(($fax_email_connection_validate == 'false') ? "no" : null)."validate-cert";
$fax_email_connection .= "}".$fax_email_connection_mailbox;
if (!$connection = imap_open($fax_email_connection, $fax_email_connection_username, $fax_email_connection_password)) {
print_r(imap_errors());
continue; // try next account
}
2024-09-09 21:06:30 -06:00
//send more log information
echo " Connection: ".$fax_email_connection."\n";
echo " Required Subject: [".$fax_email_outbound_subject_tag."]\n";
//get emails from imap
2016-04-25 20:30:23 -05:00
if ($emails = imap_search($connection, "SUBJECT \"[".$fax_email_outbound_subject_tag."]\"", SE_UID)) {
//get authorized sender(s)
if (substr_count($fax_email_outbound_authorized_senders, ',') > 0) {
$authorized_senders = explode(',', $fax_email_outbound_authorized_senders);
}
else {
$authorized_senders[] = $fax_email_outbound_authorized_senders;
}
2024-09-09 21:06:30 -06:00
//sort the emails as oldest first
sort($emails);
//send information to the console
echo " Number of Emails: ".count($emails)."\n";
//process each email
2016-04-25 20:30:23 -05:00
foreach ($emails as $email_id) {
2022-04-01 17:25:57 -06:00
//get email meta data
2016-04-25 20:30:23 -05:00
$metadata = object_to_array(imap_fetch_overview($connection, $email_id, FT_UID));
2022-04-01 17:25:57 -06:00
//print_r($metadata);
2016-04-25 20:30:23 -05:00
//format from address
//$tmp = object_to_array(imap_rfc822_parse_adrlist($metadata[0]['from'], null));
//$metadata[0]['from'] = strtolower($tmp[0]['mailbox']."@".$tmp[0]['host']);
//$sender_email = $metadata[0]['from'];
//get the sender email address
if (strstr($metadata[0]['from'], '<') && strstr($metadata[0]['from'], '>')) {
2022-04-01 17:25:57 -06:00
$sender_email = preg_match('/^.*<(.+)>/', $metadata[0]['from'], $matches) ? strtolower($matches[1]) : '';
}
else {
2022-04-01 17:25:57 -06:00
$sender_email = strtolower($metadata[0]['from']);
}
2016-04-25 20:30:23 -05:00
//check sender
$sender_domain = explode('@', $sender_email)[1];
$sender_authorized = in_array($sender_email, $authorized_senders) || in_array($sender_domain, $authorized_senders) ? true : false;
2016-04-25 20:30:23 -05:00
if ($sender_authorized) {
2022-04-01 17:25:57 -06:00
//debug info
2024-09-09 21:06:30 -06:00
echo " Sender Authorized: ".$sender_email."\n";
2022-04-01 17:25:57 -06:00
2016-04-25 20:30:23 -05:00
//add multi-lingual support
$language = new text;
$text = $language->get();
//sent sender address (used in api call)
$mailto_address_user = $metadata[0]['from'];
//parse recipient fax number(s)
$fax_subject = $metadata[0]['subject'];
$tmp = explode(']', $fax_subject); //closing bracket of subject tag
$tmp = $tmp[1];
$tmp = str_replace(':', ',', $tmp);
$tmp = str_replace(';', ',', $tmp);
$tmp = str_replace('|', ',', $tmp);
if (substr_count($tmp, ',') > 0) {
$fax_numbers = explode(',', $tmp);
}
else {
$fax_numbers[] = $tmp;
}
unset($fax_subject); //clear so not on cover page
$message = parse_message($connection, $email_id, FT_UID);
//get email body (if any) for cover page
$fax_message = '';
//Debug print
2024-09-09 21:06:30 -06:00
print(' Attachments:' . "\n");
foreach ($message['attachments'] as $attachment){
2024-09-09 21:06:30 -06:00
print(' ' . $attachment['type'] . ' - ' . $attachment['name'] . ': ' . $attachment['size'] . ' disposition: ' . $attachment['disposition'] . "\n");
2016-04-25 20:30:23 -05:00
}
2024-09-09 21:06:30 -06:00
print(' Messages:' . "\n");
foreach ($message['messages'] as $msg){
2024-09-09 21:06:30 -06:00
print(' ' . $msg['type'] . ' - ' . $msg['size'] . "\n");
2016-04-25 20:30:23 -05:00
// print($msg['data']);
// print("\n--------------------------------------------------------\n");
}
foreach ($message['messages'] as $msg){
2016-09-30 14:47:22 -07:00
if(($msg['size'] > 0)) {
2016-04-25 20:30:23 -05:00
$fax_message = $msg['data'];
break;
}
}
if ($fax_message != '') {
$fax_message = strip_tags($fax_message);
$fax_message = html_entity_decode($fax_message);
2016-04-25 20:30:23 -05:00
$fax_message = str_replace("\r\n\r\n", "\r\n", $fax_message);
}
2022-04-17 14:21:54 -06:00
// set fax directory (used for pdf creation - cover and/or attachments)
2025-03-14 15:19:49 -07:00
$fax_dir = $settings->get('switch','storage').'/fax'.(($domain_name != '') ? '/'.$domain_name : null);
2016-04-25 20:30:23 -05:00
//handle attachments (if any)
$emailed_files = Array();
$attachments = $message['attachments'];
if (sizeof($attachments) > 0) {
foreach ($attachments as $attachment) {
2024-09-09 21:06:30 -06:00
//get the fax file extension
2016-04-25 20:30:23 -05:00
$fax_file_extension = pathinfo($attachment['name'], PATHINFO_EXTENSION);
//block unknown files
2024-09-09 21:06:30 -06:00
if ($fax_file_extension == '') {continue; }
2016-04-25 20:30:23 -05:00
//block unauthorized files
2024-09-09 21:06:30 -06:00
if (!$fax_allowed_extension['.' . $fax_file_extension]) { continue; }
2016-04-25 20:30:23 -05:00
//support only attachments
2024-09-09 21:06:30 -06:00
//if($attachment['disposition'] != 'attachment'){ continue; }
2016-04-25 20:30:23 -05:00
//store attachment in local fax temp folder
2024-09-09 21:06:30 -06:00
$uuid_filename = uuid();
$local_filepath = $fax_dir.'/'.$fax_extension.'/temp/'.$uuid_filename."-".$attachment['name'];
file_put_contents($local_filepath, $attachment['data']);
2016-04-25 20:30:23 -05:00
//load files array with attachments
2024-09-09 21:06:30 -06:00
$emailed_files['error'][] = 0;
$emailed_files['size'][] = $attachment['size'];
$emailed_files['tmp_name'][] = $uuid_filename."-".$attachment['name'];
$emailed_files['name'][] = $uuid_filename."-".$attachment['name'];
2016-04-25 20:30:23 -05:00
}
}
//Debug print
2024-09-09 21:06:30 -06:00
print(' FAX Message:' . "\n");
print(' Message Length: ' . strlen($fax_message) . "\n");
2023-06-30 18:51:25 +00:00
if (isset($emailed_files['name'])) {
2024-09-09 21:06:30 -06:00
print(' FAX Files [' . sizeof($emailed_files['name']) . ']:' . "\n");
2023-06-28 10:21:00 -06:00
for($i = 0; $i < sizeof($emailed_files['name']);++$i){
2024-09-09 21:06:30 -06:00
print(' ' . $emailed_files['name'][$i] . ' - ' . $emailed_files['size'][$i] . "\n");
2023-06-28 10:21:00 -06:00
}
2016-04-25 20:30:23 -05:00
}
//send fax
$cwd = getcwd();
require "fax_send.php";
2022-04-17 14:21:54 -06:00
if($cwd){
2016-04-25 20:30:23 -05:00
chdir($cwd);
}
//reset variables
unset($fax_numbers);
//delete email
if (imap_delete($connection, $email_id, FT_UID)) {
imap_expunge($connection);
}
}
else {
//unauthorized sender
2016-04-25 20:30:23 -05:00
}
}
unset($authorized_senders);
}
//close account connection
imap_close($connection);
2024-09-09 21:06:30 -06:00
//add a line feed
echo "\n";
2016-04-25 20:30:23 -05:00
}
}
2018-02-25 02:04:28 -07:00
?>