<?php
include "root.php";
require_once "resources/require.php";
require_once "resources/functions/object_to_array.php";
require_once "resources/functions/parse_message.php";
require_once "resources/classes/text.php";
$sql = "select * from v_fax ";
$sql .= "where fax_email_connection_host <> '' ";
$sql .= "and fax_email_connection_host is not null ";
$database = new database;
$result = $database->select($sql, null, 'all');
unset($sql);
function arr_to_map(&$arr){
if(is_array($arr)){
$map = Array();
foreach($arr as &$val){
$map[$val] = true;
}
return $map;
}
return false;
}
if (is_array($result) && @sizeof($result) != 0) {
$default_settings = load_default_settings();
$sql = "select event_socket_ip_address, event_socket_port, event_socket_password from v_settings";
$database = new database;
$row = $database->select($sql, null, 'row');
$event_socket['ip_address'] = $row['event_socket_ip_address'];
$event_socket['port'] = $row['event_socket_port'];
$event_socket['password'] = $row['event_socket_password'];
unset($sql, $row);
$fax_send_mode_default = $_SESSION['fax']['send_mode']['text'];
if(strlen($fax_send_mode_default) == 0){
$fax_send_mode_default = 'direct';
}
$fax_cover_font_default = $_SESSION['fax']['cover_font']['text'];
$fax_allowed_extension_default = arr_to_map($_SESSION['fax']['allowed_extension']);
if($fax_allowed_extension_default == false){
$tmp = array('.pdf', '.tiff', '.tif');
$fax_allowed_extension_default = arr_to_map($tmp);
}
foreach ($result as $row) {
$fax_uuid = $row["fax_uuid"];
$domain_uuid = $row["domain_uuid"];
$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"];
$fax_email_outbound_authorized_senders = strtolower($row["fax_email_outbound_authorized_senders"]);
$fax_send_greeting = $row["fax_send_greeting"];
$fax_accountcode = $row["accountcode"];
unset($_SESSION);
$_SESSION = $default_settings;
load_domain_settings($domain_uuid);
$fax_send_mode = $_SESSION['fax']['send_mode']['text'];
if(strlen($fax_send_mode) == 0){
$fax_send_mode = $fax_send_mode_default;
}
$fax_cover_font = $_SESSION['fax']['cover_font']['text'];
if(strlen($fax_cover_font) == 0){
$fax_cover_font = $fax_cover_font_default;
}
$fax_allowed_extension = arr_to_map($_SESSION['fax']['allowed_extension']);
if($fax_allowed_extension == false){
$fax_allowed_extension = $fax_allowed_extension_default;
}
$_SESSION['event_socket_ip_address'] = $event_socket['ip_address'];
$_SESSION['event_socket_port'] = $event_socket['port'];
$_SESSION['event_socket_password'] = $event_socket['password'];
$sql = "select domain_name from v_domains where domain_uuid = :domain_uuid ";
$parameters['domain_uuid'] = $domain_uuid;
$database = new database;
$row = $database->select($sql, $parameters, 'row');
$domain_name = $row['domain_name'];
$_SESSION['domain_name'] = $row['domain_name'];
$_SESSION['domain_uuid'] = $domain_uuid;
unset($sql, $parameters, $row);
$fax_page_size = $_SESSION['fax']['page_size']['text'];
$fax_resolution = $_SESSION['fax']['resolution']['text'];
$fax_header = $_SESSION['fax']['cover_header']['text'];
$fax_footer = $_SESSION['fax']['cover_footer']['text'];
$fax_sender = $fax_caller_id_name;
$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;
}
if ($emails = imap_search($connection, "ALL", SE_UID)) {
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;
}
sort($emails);
foreach ($emails as $email_id) {
$metadata = object_to_array(imap_fetch_overview($connection, $email_id, FT_UID));
$tmp = object_to_array(imap_rfc822_parse_adrlist($metadata[0]['from'], null));
$metadata[0]['from'] = strtolower($tmp[0]['mailbox']."@".$tmp[0]['host']);
$sender_authorized = false;
if (in_array($metadata[0]['from'],$authorized_senders)) { $sender_authorized = true; }
if ($sender_authorized) {
$language = new text;
$text = $language->get();
$mailto_address_user = $metadata[0]['from'];
$fax_subject = $metadata[0]['to'];
$tmp = strstr($fax_subject, '@', true);
$tmp = str_replace('|', ',', $tmp);
if (substr_count($tmp, ',') > 0) {
$fax_numbers = explode(',', $tmp);
}
else {
$fax_numbers[] = $tmp;
}
unset($fax_subject);
$message = parse_message($connection, $email_id, FT_UID);
$fax_message = '';
print('attachments:' . "\n");
foreach($message['attachments'] as &$attachment){
print(' - ' . $attachment['type'] . ' - ' . $attachment['name'] . ': ' . $attachment['size'] . ' disposition: ' . $attachment['disposition'] . "\n");
}
print('messages:' . "\n");
foreach($message['messages'] as &$msg){
print(' - ' . $msg['type'] . ' - ' . $msg['size'] . "\n");
}
foreach($message['messages'] as &$msg){
if(($msg['size'] > 0)) {
$fax_message = $msg['data'];
break;
}
}
if ($fax_message != '') {
$fax_message = strip_tags($fax_message);
$fax_message = html_entity_decode($fax_message);
$fax_message = str_replace("\r\n\r\n", "\r\n", $fax_message);
}
$fax_dir = $_SESSION['switch']['storage']['dir'].'/fax'.(($domain_name != '') ? '/'.$domain_name : null);
$emailed_files = Array();
$attachments = $message['attachments'];
if (sizeof($attachments) > 0) {
foreach ($attachments as &$attachment) {
$fax_file_extension = pathinfo($attachment['name'], PATHINFO_EXTENSION);
if ($fax_file_extension == '') {continue; }
if (!$fax_allowed_extension['.' . $fax_file_extension]) { continue; }
$uuid_filename = uuid();
$local_filepath = $fax_dir.'/'.$fax_extension.'/temp/'.$uuid_filename."-".$attachment['name'];
file_put_contents($local_filepath, $attachment['data']);
$emailed_files['error'][] = 0;
$emailed_files['size'][] = $attachment['size'];
$emailed_files['tmp_name'][] = $uuid_filename."-".$attachment['name'];
$emailed_files['name'][] = $uuid_filename."-".$attachment['name'];
}
}
print('***********************' . "\n");
print('fax message:' . "\n");
print(' - length: ' . strlen($fax_message) . "\n");
print('fax files [' . sizeof($emailed_files['name']) . ']:' . "\n");
for($i = 0; $i < sizeof($emailed_files['name']);++$i){
print(' - ' . $emailed_files['name'][$i] . ' - ' . $emailed_files['size'][$i] . "\n");
}
print('***********************' . "\n");
$cwd = getcwd();
$included = true;
require("fax_send.php");
if($cwd){
chdir($cwd);
}
unset($fax_numbers);
}
if (imap_delete($connection, $email_id, FT_UID)) {
imap_expunge($connection);
}
}
unset($authorized_senders);
}
imap_close($connection);
}
}
function load_default_settings() {
$sql = "select * from v_default_settings ";
$sql .= "where default_setting_enabled = 'true' ";
$database = new database;
$result = $database->select($sql, null, 'all');
if (is_array($result) && @sizeof($result) != 0) {
foreach ($result as $row) {
$name = $row['default_setting_name'];
$category = $row['default_setting_category'];
$subcategory = $row['default_setting_subcategory'];
if (strlen($subcategory) == 0) {
if ($name == "array") {
$settings[$category][] = $row['default_setting_value'];
}
else {
$settings[$category][$name] = $row['default_setting_value'];
}
}
else {
if ($name == "array") {
$settings[$category][$subcategory][] = $row['default_setting_value'];
}
else {
$settings[$category][$subcategory][$name] = $row['default_setting_value'];
$settings[$category][$subcategory][$name] = $row['default_setting_value'];
}
}
}
}
unset($sql, $parameters, $result, $row);
return $settings;
}
function load_domain_settings($domain_uuid) {
if (is_uuid($domain_uuid)) {
$sql = "select * from v_domain_settings ";
$sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and domain_setting_enabled = 'true' ";
$sql .= "order by domain_setting_order asc ";
$parameters['domain_uuid'] = $domain_uuid;
$database = new database;
$result = $database->select($sql, $parameters, 'all');
if (is_array($result) && @sizeof($result) != 0) {
foreach ($result as $row) {
$name = $row['domain_setting_name'];
$category = $row['domain_setting_category'];
$subcategory = $row['domain_setting_subcategory'];
if ($name == "array") {
unset($_SESSION[$category][$subcategory]);
}
}
foreach ($result as $row) {
$name = $row['domain_setting_name'];
$category = $row['domain_setting_category'];
$subcategory = $row['domain_setting_subcategory'];
if (strlen($subcategory) == 0) {
if ($name == "array") {
$_SESSION[$category][] = $row['domain_setting_value'];
}
else {
$_SESSION[$category][$name] = $row['domain_setting_value'];
}
}
else {
if ($name == "array") {
$_SESSION[$category][$subcategory][] = $row['domain_setting_value'];
}
else {
$_SESSION[$category][$subcategory][$name] = $row['domain_setting_value'];
}
}
}
}
}
}
?>