Files

296 lines
11 KiB
PHP
Raw Permalink Normal View History

2019-03-28 12:18:24 -06: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>
2024-08-22 15:12:03 -06:00
Portions created by the Initial Developer are Copyright (C) 2008-2024
2019-03-28 12:18:24 -06:00
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com>
Luis Daniel Lucio Quiroz <dlucio@okay.com.mx>
*/
2022-10-10 16:35:14 -06:00
//includes files
2023-06-15 14:28:23 -03:00
require_once dirname(__DIR__, 2) . "/resources/require.php";
2019-03-28 12:18:24 -06:00
require_once "resources/check_auth.php";
//check permissions
if (!permission_exists('contact_attachment_edit') && !permission_exists('contact_attachment_add')) {
echo "access denied"; exit;
}
//add multi-lingual support
$language = new text;
$text = $language->get();
//action add or update
2023-06-01 11:50:12 -06:00
$contact_attachment_uuid = $_REQUEST['id'] ?? '';
$contact_uuid = $_REQUEST['contact_uuid'] ?? '';
2019-03-28 12:18:24 -06:00
2023-06-01 11:50:12 -06:00
if (!empty($contact_attachment_uuid) && !empty($contact_uuid) && is_uuid($contact_attachment_uuid) && is_uuid($contact_uuid)) {
2019-03-28 12:18:24 -06:00
$action = 'update';
}
2023-06-01 11:50:12 -06:00
else if (!empty($contact_uuid) && is_uuid($contact_uuid)) {
2019-03-28 12:18:24 -06:00
$action = 'add';
}
else {
exit;
}
//get http post variables and set them to php variables
2023-06-01 11:50:12 -06:00
if (!empty($_POST)) {
2019-03-28 12:18:24 -06:00
2024-08-22 15:12:03 -06:00
//set the variables
2019-03-28 12:18:24 -06:00
$attachment = $_FILES['attachment'];
$attachment_primary = $_POST['attachment_primary'];
$attachment_description = $_POST['attachment_description'];
2019-03-28 12:18:24 -06:00
2019-09-17 22:28:22 -06:00
//validate the token
2024-08-22 15:12:03 -06:00
$token = new token;
if (!$token->validate($_SERVER['PHP_SELF'])) {
message::add($text['message-invalid_token'],'negative');
header('Location: contacts.php');
exit;
}
2019-09-17 22:28:22 -06:00
2024-08-22 15:12:03 -06:00
//get the attachment type
2023-06-01 11:50:12 -06:00
if (empty($attachment) || sizeof($attachment) == 0) {
$attachment_extension = strtolower(pathinfo($_POST['attachment_filename'], PATHINFO_EXTENSION));
2019-03-28 12:18:24 -06:00
}
else {
$attachment_extension = strtolower(pathinfo($attachment['name'], PATHINFO_EXTENSION));
2019-03-28 12:18:24 -06:00
}
//unflag others as primary
2024-08-22 15:12:03 -06:00
$allowed_primary_attachment = false;
if ($attachment_primary && ($attachment_extension == 'jpg' || $attachment_extension == 'jpeg' || $attachment_extension == 'gif' || $attachment_extension == 'png')) {
2024-08-22 15:12:03 -06:00
$sql = "update v_contact_attachments set attachment_primary = 0 ";
$sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and contact_uuid = :contact_uuid ";
$parameters['domain_uuid'] = $domain_uuid;
$parameters['contact_uuid'] = $contact_uuid;
$database = new database;
$database->execute($sql, $parameters ?? null);
unset($sql, $parameters);
2019-03-28 12:18:24 -06:00
2024-08-22 15:12:03 -06:00
$allowed_primary_attachment = true;
}
//get the allowed extensions
$allowed_extensions = array_keys(json_decode($_SESSION['contact']['allowed_attachment_types']['text'], true));
//check the allowed extensions
if ($attachment['error'] == '0' && in_array($attachment_extension, $allowed_extensions)) {
//get the attachment content
$attachment_content = file_get_contents($attachment['tmp_name']);
//read the image from the string then output the image without meta data
if (in_array($attachment_extension, ['png','jpg','jpeg','gif','bmp', 'webp'])) {
2024-08-22 15:12:03 -06:00
//create the image object from the content string
$image = imagecreatefromstring($attachment_content);
//start output buffering to capture the image data
ob_start();
//output the image without the EXIF data
switch ($attachment_extension) {
case 'png':
imagealphablending($image, FALSE);
imagesavealpha($image, TRUE);
imagepng($image);
break;
case 'jpg':
case 'jpeg':
imagejpeg($image);
break;
case 'gif':
imagesavealpha($image, TRUE);
imagegif($image);
break;
case 'bmp':
imagebmp($image);
break;
case 'webp':
imagewebp($image);
break;
}
2024-08-22 15:12:03 -06:00
//get the image from the buffer
$attachment_content = ob_get_contents();
//end the buffering
ob_end_clean();
//free up the memory
imagedestroy($image);
2019-03-28 12:18:24 -06:00
}
2024-08-22 15:12:03 -06:00
}
//prepare the array
$array['contact_attachments'][0]['contact_attachment_uuid'] = $action == 'update' ? $contact_attachment_uuid : uuid();
$array['contact_attachments'][0]['domain_uuid'] = $_SESSION['domain_uuid'] ?? '';
$array['contact_attachments'][0]['contact_uuid'] = $contact_uuid;
$array['contact_attachments'][0]['attachment_primary'] = $allowed_primary_attachment ? 1 : 0;
if ($attachment['error'] == '0' && in_array(strtolower(pathinfo($attachment['name'], PATHINFO_EXTENSION)), $allowed_extensions)) {
$array['contact_attachments'][0]['attachment_filename'] = $attachment['name'];
$array['contact_attachments'][0]['attachment_content'] = base64_encode($attachment_content);
}
$array['contact_attachments'][0]['attachment_description'] = $attachment_description;
if ($action == 'add') {
$array['contact_attachments'][0]['attachment_uploaded_date'] = 'now()';
$array['contact_attachments'][0]['attachment_uploaded_user_uuid'] = $_SESSION['user_uuid'];
}
2019-03-28 12:18:24 -06:00
//save data
2024-08-22 15:12:03 -06:00
$database = new database;
$database->app_name = 'contacts';
$database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
$database->save($array);
unset($array);
2019-03-28 12:18:24 -06:00
//redirect
2024-08-22 15:12:03 -06:00
message::add($text['message-'.($action == 'update' ? 'update' : 'uploaded')]);
header('Location: contact_edit.php?id='.$contact_uuid);
exit;
2019-03-28 12:18:24 -06:00
}
//get form data
2023-06-01 11:50:12 -06:00
if (!empty($_GET)) {
2019-03-28 12:18:24 -06:00
$sql = "select * from v_contact_attachments ";
$sql .= "where domain_uuid = :domain_uuid ";
2019-03-28 12:18:24 -06:00
$sql .= "and contact_attachment_uuid = :contact_attachment_uuid ";
$parameters['domain_uuid'] = $domain_uuid;
$parameters['contact_attachment_uuid'] = $contact_attachment_uuid;
$database = new database;
$row = $database->select($sql, $parameters, 'row');
2023-06-01 11:50:12 -06:00
if (!empty($row)) {
$attachment_primary = $row["attachment_primary"];
$attachment_filename = $row["attachment_filename"];
$attachment_content = $row["attachment_content"];
$attachment_description = $row["attachment_description"];
}
unset($sql, $parameters, $row);
2019-03-28 12:18:24 -06:00
}
2019-09-17 22:28:22 -06:00
//create token
$object = new token;
$token = $object->create($_SERVER['PHP_SELF']);
2019-03-28 12:18:24 -06:00
//show the header
if ($action == "update") {
$document['title'] = $text['title-contact_attachment-edit'];
}
else if ($action == "add") {
$document['title'] = $text['title-contact_attachment-add'];
}
require_once "resources/header.php";
2019-03-28 12:18:24 -06:00
//show the content
echo "<form method='post' name='frm' id='frm' enctype='multipart/form-data'>\n";
echo "<div class='action_bar' id='action_bar'>\n";
echo " <div class='heading'>";
2019-03-28 12:18:24 -06:00
if ($action == "update") {
echo "<b>".$text['header-contact_attachment-edit']."</b>";
2019-03-28 12:18:24 -06:00
}
else if ($action == "add") {
echo "<b>".$text['header-contact_attachment-add']."</b>";
2019-03-28 12:18:24 -06:00
}
echo " </div>\n";
echo " <div class='actions'>\n";
echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'contact_edit.php?id='.urlencode($contact_uuid)]);
echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save']);
echo " </div>\n";
echo " <div style='clear: both;'></div>\n";
echo "</div>\n";
2019-03-28 12:18:24 -06:00
2024-09-05 16:10:04 -07:00
echo "<div class='card'>\n";
2019-03-28 12:18:24 -06:00
echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
echo "<tr>\n";
echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
2019-03-28 12:18:24 -06:00
echo " ".$text['label-attachment']."\n";
echo "</td>\n";
echo "<td width='70%' class='vtable' align='left'>\n";
$attachment_extension = strtolower(pathinfo($attachment_filename ?? '', PATHINFO_EXTENSION));
2019-03-28 12:18:24 -06:00
if ($action == 'update') {
echo "<input type='hidden' name='attachment_filename' value=\"".escape($attachment_filename)."\">\n";
if ($attachment_extension == 'jpg' || $attachment_extension == 'jpeg' || $attachment_extension == 'gif' || $attachment_extension == 'png') {
echo "<img src='data:image/".$attachment_extension.";base64,".$attachment_content."' style='border: none; width: auto; max-height: 400px;' oncontextmenu=\"window.open('contact_attachment.php?id=".$contact_attachment_uuid."&action=download'); return false;\">";
2019-03-28 12:18:24 -06:00
}
else {
echo "<a href='contact_attachment.php?id=".$contact_attachment_uuid."&action=download' style='font-size: 120%;'>".$attachment_filename."</a>";
}
}
else {
$allowed_attachment_types = json_decode($_SESSION['contact']['allowed_attachment_types']['text'], true);
echo " <input type='file' class='formfld' name='attachment' id='attachment' accept='.".implode(',.',array_keys($allowed_attachment_types))."'>\n";
echo " <span style='display: inline-block; margin-top: 5px; font-size: 80%;'>".strtoupper(implode(', ', array_keys($allowed_attachment_types)))."</span>";
}
echo "</td>\n";
echo "</tr>\n";
if ($action == 'update' && ($attachment_extension == 'jpg' || $attachment_extension == 'jpeg' || $attachment_extension == 'gif' || $attachment_extension == 'png')) {
2019-03-28 12:18:24 -06:00
echo "<tr>\n";
echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
echo " ".$text['label-attachment_filename']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
echo " <a href='contact_attachment.php?id=".$contact_attachment_uuid."&action=download' style='font-size: 120%;'>".$attachment_filename."</a>";
echo "</td>\n";
echo "</tr>\n";
}
echo "<tr>\n";
echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
echo " ".$text['label-primary']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
echo " <select class='formfld' name='attachment_primary' id='attachment_primary'>\n";
echo " <option value='0'>".$text['option-false']."</option>\n";
2023-06-02 23:54:00 +00:00
echo " <option value='1' ".(!empty($attachment_primary) && $attachment_primary ? "selected" : null).">".$text['option-true']."</option>\n";
2019-03-28 12:18:24 -06:00
echo " </select>\n";
echo "</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
echo " ".$text['label-attachment_description']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
2023-06-01 11:50:12 -06:00
echo " <input class='formfld' type='text' name='attachment_description' maxlength='255' value=\"".escape($attachment_description ?? '')."\">\n";
2019-03-28 12:18:24 -06:00
echo "</td>\n";
echo "</tr>\n";
echo "</table>";
2024-09-05 16:10:04 -07:00
echo "</div>\n";
2019-03-28 12:18:24 -06:00
echo "<br><br>";
echo "<input type='hidden' name='contact_uuid' value='".escape($contact_uuid)."'>\n";
if ($action == "update") {
echo "<input type='hidden' name='contact_attachment_uuid' value='".escape($contact_attachment_uuid)."'>\n";
}
echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
2019-03-28 12:18:24 -06:00
echo "</form>";
//include the footer
require_once "resources/footer.php";
2024-08-22 15:12:03 -06:00
?>