<?php /* $Id$ */ /* freeswitch_fax_edit.php Copyright (C) 2008 Mark J Crane All rights reserved. FreeSWITCH (TM) http://www.freeswitch.org/ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ require("guiconfig.inc"); require("/usr/local/pkg/freeswitch.inc"); $a_fax = &$config['installedpackages']['freeswitchfax']['config']; $id = $_GET['id']; if (strlen($id) == 0) { $id = $_POST['id']; } $parentid = $id; if (isset($id) && $a_fax[$id]) { $pconfig['faxid'] = $a_fax[$id]['faxid']; $faxid = $a_fax[$id]['faxid']; $pconfig['faxextension'] = $a_fax[$id]['faxextension']; $pconfig['faxname'] = $a_fax[$id]['faxname']; $pconfig['faxemail'] = $a_fax[$id]['faxemail']; $pconfig['faxdomain'] = $a_fax[$id]['faxdomain']; $pconfig['faxdescription'] = $a_fax[$id]['faxdescription']; } $dir_fax_inbox = '/usr/local/freeswitch/storage/fax/'.$pconfig['faxextension'].'/inbox/'; $dir_fax_sent = '/usr/local/freeswitch/storage/fax/'.$pconfig['faxextension'].'/sent/'; $dir_fax_temp = '/usr/local/freeswitch/storage/fax/'.$pconfig['faxextension'].'/temp/'; if ($_GET['act'] == "del") { if ($_GET['type'] == 'fax') { if ($a_fax[$_GET['id']]) { $faxid = $a_fax[$_GET['id']][faxid]; $faxname = $a_fax[$_GET['id']][faxname]; $a_dialplan_includes = &$config['installedpackages']['freeswitchdialplanincludes']['config']; $a_dialplan_includes_details = &$config['installedpackages']['freeswitchdialplanincludedetails']['config']; //delete the dialplan include if (count($a_dialplan_includes) > 0) { $i=0; foreach($a_dialplan_includes as $row) { if ($row["dialplanincludeid"] == $faxid) { $order = $row['order']; unset($a_dialplan_includes[$i]); } $i++; } } //delete the dialplan include details. aka. child data if (count($a_dialplan_includes_details) > 0) { $i=0; foreach($a_dialplan_includes_details as $row) { if ($row["dialplanincludeid"] == $faxid) { unset($a_dialplan_includes_details[$i]); } $i++; } } if (file_exists("/usr/local/freeswitch/conf/dialplan/default/".$order."_".$faxname.".xml")){ unlink("/usr/local/freeswitch/conf/dialplan/default/".$order."_".$faxname.".xml"); } //remove fax entries unset($a_fax[$_GET['id']]); write_config(); header("Location: freeswitch_fax.php"); exit; } } } if (($_POST['type'] == "fax_send") && is_uploaded_file($_FILES['fax_file']['tmp_name'])) { $fax_number = $_POST['fax_number']; $fax_name = $_FILES['fax_file']['name']; $fax_name = str_replace(".tif", "", $fax_name); $fax_name = str_replace(".tiff", "", $fax_name); $fax_name = str_replace(".pdf", "", $fax_name); $fax_gateway = $_POST['fax_gateway']; $password = $config['installedpackages']['freeswitchsettings']['config'][0]['event_socket_password']; $port = $config['installedpackages']['freeswitchsettings']['config'][0]['event_socket_port']; $host = $config['interfaces']['lan']['ipaddr']; //upload the file move_uploaded_file($_FILES['fax_file']['tmp_name'], $dir_fax_temp.$_FILES['fax_file']['name']); $fax_file_extension = substr($dir_fax_temp.$_FILES['fax_file']['name'], -4); if ($fax_file_extension == ".pdf") { exec("cd ".$dir_fax_temp.";gs -q -sDEVICE=tiffg3 -r204x98 -dNOPAUSE -sOutputFile=".$fax_name.".tif -- ".$fax_name.".pdf -c quit"); //exec("rm ".$dir_fax_temp.$fax_name.".pdf"); } if ($fax_file_extension == ".tiff") { exec("cp ".$dir_fax_temp.$fax_name.".tiff ".$dir_fax_temp.$fax_name.".tif"); exec("rm ".$dir_fax_temp.$fax_name.".tiff"); } //send the fax $fp = event_socket_create($host, $port, $password); $cmd = "api originate [absolute_codec_string=PCMU]sofia/gateway/".$fax_gateway."/".$fax_number." &txfax(".$dir_fax_temp.$fax_name.".tif)"; $response = event_socket_request($fp, $cmd); $response = str_replace("\n", "", $response); $uuid = str_replace("+OK ", "", $response); fclose($fp); //if ($response >= 1) { // $fp = event_socket_create($host, $port, $password); // $cmd = "api uuid_getvar ".$uuid." fax_result_text"; // echo $cmd."\n"; // $response = event_socket_request($fp, $cmd); // $response = trim($response); // fclose($fp); //} sleep(5); //copy the .tif to the sent directory exec("cp ".$dir_fax_temp.$fax_name.".tif ".$dir_fax_sent.$fax_name.".tif"); //delete the .tif from the temp directory //exec("rm ".$dir_fax_temp.$fax_name.".tif"); //convert the tif to pdf and png exec("cd $dir_fax_sent; /usr/local/bin/tiff2png ".$dir_fax_sent.$fax_name.".tif"); exec("cd $dir_fax_sent; /usr/local/bin/tiff2pdf -f -o ".$fax_name.".pdf ".$dir_fax_sent.$fax_name.".tif"); header("Location: freeswitch_fax_edit.php?id=".$id."&msg=".$response); exit; } if ($_GET['a'] == "download") { session_cache_limiter('public'); if ($_GET['type'] == "fax_inbox") { if (file_exists($dir_fax_inbox.$_GET['filename'])) { $fd = fopen($dir_fax_inbox.$_GET['filename'], "rb"); if ($_GET['t'] == "bin") { header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Description: File Transfer"); header('Content-Disposition: attachment; filename="'.$_GET['filename'].'"'); } else { $file_ext = substr($_GET['filename'], -3); if ($file_ext == "png") { header("Content-Type: image/png"); } } header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past header("Content-Length: " . filesize($dir_fax_inbox.$_GET['filename'])); fpassthru($fd); } else { echo "not found"; } exit; } } else { //echo $dir_fax_inbox.$_GET['filename']; } if ($_GET['a'] == "download") { session_cache_limiter('public'); if ($_GET['type'] == "fax_sent") { if (file_exists($dir_fax_sent.$_GET['filename'])) { $fd = fopen($dir_fax_sent.$_GET['filename'], "rb"); if ($_GET['t'] == "bin") { header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Description: File Transfer"); header('Content-Disposition: attachment; filename="'.$_GET['filename'].'"'); } else { $file_ext = substr($_GET['filename'], -3); if ($file_ext == "png") { header("Content-Type: image/png"); } } header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past header("Content-Length: " . filesize($dir_fax_sent.$_GET['filename'])); fpassthru($fd); } } exit; } else { //echo $dir_fax_inbox.$_GET['filename']; } if ($_GET['act'] == "del") { if ($_GET['type'] == 'fax_inbox') { //if ($a_fax[$_GET['id']]) { $tmp_file_array = split("\.",$_GET['filename']); $file_name = $tmp_file_array[0]; $file_ext = $tmp_file_array[1]; unlink_if_exists($dir_fax_inbox.$file_name.".pdf"); unlink_if_exists($dir_fax_inbox.$file_name.".png"); unlink_if_exists($dir_fax_inbox.$file_name.".tif"); //unset($a_fax[$_GET['id']]); write_config(); header("Location: freeswitch_fax.php"); exit; //} } } if ($_GET['act'] == "del") { if ($_GET['type'] == 'fax_sent') { //if ($a_fax[$_GET['id']]) { $tmp_file_array = split("\.",$_GET['filename']); $file_name = $tmp_file_array[0]; $file_ext = $tmp_file_array[1]; unlink_if_exists($dir_fax_sent.$file_name.".pdf"); unlink_if_exists($dir_fax_sent.$file_name.".png"); unlink_if_exists($dir_fax_sent.$file_name.".tif"); //unset($a_fax[$_GET['id']]); write_config(); header("Location: freeswitch_fax.php"); exit; //} } } if ($_POST) { unset($input_errors); $pconfig = $_POST; if (!$input_errors) { $ent = array(); if (strlen($_POST['faxid']) > 0) { $ent['faxid'] = $_POST['faxid']; } else { $ent['faxid'] = guid(); } $ent['faxextension'] = $_POST['faxextension']; $ent['faxname'] = $_POST['faxname']; $ent['faxemail'] = $_POST['faxemail']; $ent['faxdomain'] = $_POST['faxdomain']; $ent['faxdescription'] = $_POST['faxdescription']; if (isset($id) && $a_fax[$id]) { //update $a_fax[$id] = $ent; } else { //add $a_fax[] = $ent; } if (!is_dir('/usr/local/freeswitch/storage/fax/')) { exec("mkdir /usr/local/freeswitch/storage/fax/"); } $faxfolder = '/usr/local/freeswitch/storage/fax/'.$_POST['faxextension']; if (!is_dir($faxfolder)) { exec('mkdir '.$faxfolder); } if (!is_dir($faxfolder.'/inbox/')) { exec('mkdir '.$faxfolder.'/inbox/'); } if (!is_dir($faxfolder.'/sent/')) { exec('mkdir '.$faxfolder.'/sent/'); } if (!is_dir($faxfolder.'/temp/')) { exec('mkdir '.$faxfolder.'/temp/'); } write_config(); sync_package_freeswitch_fax(); header("Location: freeswitch_fax.php"); exit; } } include("head.inc"); ?> <script type="text/javascript" language="JavaScript"> function show_advanced_config() { document.getElementById("showadvancedbox").innerHTML=''; aodiv = document.getElementById('showadvanced'); aodiv.style.display = "block"; } </script> <body link="#0000CC" vlink="#0000CC" alink="#0000CC"> <?php include("fbegin.inc"); ?> <p class="pgtitle">FreeSWITCH: FAX: Edit</p> <?php if ($input_errors) print_input_errors($input_errors); ?> <div id="mainlevel"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr><td class="tabnavtbl"> <?php display_top_tabs(build_menu()); ?> </td></tr> </table> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td class="tabcont" > <table width="100%" border="0" cellpadding="6" cellspacing="0"> <tr> <td><p><span class="vexpl"><span class="red"><strong>FAX Setup<br> </strong></span> </p></td> </tr> </table> <br /> <form action="freeswitch_fax_edit.php" method="post" name="iform" id="iform"> <table width="100%" border="0" cellpadding="6" cellspacing="0"> <tr> <td width="25%" valign="top" class="vncellreq">Extension</td> <td width="75%" class="vtable"> <input name="faxextension" type="text" class="formfld unknown" id="faxextension" size="40" value="<?=htmlspecialchars($pconfig['faxextension']);?>"> <br><span class="vexpl">Enter the fax extension here.<br></span> </td> </tr> <tr> <td width="25%" valign="top" class="vncellreq">Name</td> <td width="75%" class="vtable"> <input name="faxname" type="text" class="formfld unknown" id="faxname" size="40" value="<?=htmlspecialchars($pconfig['faxname']);?>"> <br><span class="vexpl">Enter the name here.<br></span> </td> </tr> <tr> <td width="25%" valign="top" class="vncellreq">Email</td> <td width="75%" class="vtable"> <input name="faxemail" type="text" class="formfld unknown" id="faxemail" size="40" value="<?=htmlspecialchars($pconfig['faxemail']);?>"> <br><span class="vexpl">Optional: Enter the email address to send the FAX to.<br></span> </td> </tr> <tr> <td width="25%" valign="top" class="vncellreq">Domain</td> <td width="75%" class="vtable"> <input name="faxdomain" type="text" class="formfld unknown" id="faxdomain" size="40" value="<?=htmlspecialchars($pconfig['faxdomain']);?>"> <br><span class="vexpl">Enter the domain here.<br></span> </td> </tr> <!-- <tr> <td width="25%" valign="top" class="vncellreq" nowrap>Attach File</td> <td width="75%" class="vtable"> <?php /* echo " <select name='vm-attach-file' class='formfld unknown'>\n"; echo " <option></option>\n"; switch (htmlspecialchars($pconfig['vm-attach-file'])) { case "true": echo " <option value='true' selected='yes'>true</option>\n"; echo " <option value='false'>false</option>\n"; break; case "false": echo " <option value='true'>true</option>\n"; echo " <option value='false' selected='yes'>false</option>\n"; break; default: echo " <option value='true' selected='yes'>true</option>\n"; echo " <option value='false'>false</option>\n"; } echo " </select>\n"; */ ?> Choose whether to attach the file to the email. </td> </tr> --> <table width="100%" border="0" cellpadding="6" cellspacing="0"> <tr> <td width="25%" valign="top" class="vncellreq">Description</td> <td width="75%" class="vtable"> <input name="faxdescription" type="text" class="formfld unknown" id="faxdescription" size="40" value="<?=htmlspecialchars($pconfig['faxdescription']);?>"> <br><span class="vexpl">Enter the description here.<br></span> </td> </tr> <tr> <td valign="top"> </td> <td> <input name="faxid" type="hidden" value="<?=htmlspecialchars($pconfig['faxid']);?>"> <?php if (isset($id) && $a_fax[$id]): ?> <input name="id" type="hidden" value="<?=$id;?>"> <?php endif; ?> <input name="Submit" type="submit" class="formbtn" value="Save"> <input class="formbtn" type="button" value="Cancel" onclick="history.back()"> </td> </tr> </table> </form> <br /> <br /> <br /> <br /> <table width="100%" border="0" cellpadding="3" cellspacing="0"> <tr> <td width='30%'> <span class="vexpl"><span class="red"><strong>Send</strong></span> </td> </tr> <tr> <td> To send a fax you can upload a .tif file or if ghost script has been installed then you can also send a fax by uploading a PDF. (pkg_add -r ghostscript8-nox11; rehash) When sending a fax you can view status of the transmission by viewing the logs from the Status tab or by watching the response from the FreeSWITCH console. </td> </tr> <tr> <td align='right' nowrap> <form action="" method="POST" enctype="multipart/form-data" name="frmUpload" onSubmit=""> <table border='0' cellpadding='3' cellspacing='0' width='100%'> <tr> <td valign="middle" class="label"> Fax Number </td> <td valign="top" class="label"> <input type="text" name="fax_number" value=""> </td> <td align="left">Upload:</td> <td valign="top" class="label"> <input name="id" type="hidden" value="$id"> <input name="type" type="hidden" value="fax_send"> <input name="fax_file" type="file" class="button" id="fax_file"> </td> <td valign="middle" class="label"> Gateway </td> <td valign="top" class="label"> <?php //create a temporary id for the array $a_gateways = &$config['installedpackages']['freeswitchgateways']['config']; $i = 0; if (count($a_gateways) > 0) { foreach ($a_gateways as $ent) { $a_gateways[$i]['id'] = $i; $i++; } } //order the array function cmp_string($a, $b) { return strcmp($a["gateway"], $b["gateway"]); } if (count($a_gateways) > 0) { usort($a_gateways, "cmp_string"); } echo "<select name='fax_gateway' class='formfld'>"; $i = 0; if (count($a_gateways) > 0) { foreach ($a_gateways as $ent) { echo "<option>".$ent['gateway']."</option>\n"; } } echo "</select>\n"; ?> </td> <td> <input name="submit" type="submit" class="button" id="upload" value="Send FAX"> </td> </tr> </table> </div> </form> </td> </tr> </table> <br /> <br /> <br /> <br /> <table width="100%" border="0" cellpadding="5" cellspacing="0"> <tr> <td> <span class="vexpl"><span class="red"><strong>Inbox</strong></span> </td> <td align='right'> <b>location:</b> <?php echo $dir_fax_inbox; ?> </td> </tr> </table> <div id="niftyOutter"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="50%" class="listhdrr">File Name (download)</td> <td width="10%" class="listhdrr">Download</td> <td width="10%" class="listhdrr">View</td> <td width="20%" class="listhdr">Last Modified</td> <td width="10%" class="listhdr" nowrap>Size</td> </tr> <?php if ($handle = opendir($dir_fax_inbox)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && is_file($dir_fax_inbox.$file)) { $tmp_filesize = filesize($dir_fax_inbox.$file); $tmp_filesize = byte_convert($tmp_filesize); $tmp_file_array = split("\.",$file); $file_name = $tmp_file_array[0]; $file_ext = $tmp_file_array[1]; if ($file_ext == "tif") { echo "<tr>\n"; echo " <td class=\"listlr\" ondblclick=\"\">\n"; echo " <a href=\"freeswitch_fax_edit.php?id=".$id."&a=download&type=fax_inbox&t=bin&filename=".$file."\">\n"; echo " $file"; echo " </a>"; echo " </td>\n"; echo " <td class=\"listlr\" ondblclick=\"\">\n"; echo " <a href=\"freeswitch_fax_edit.php?id=".$id."&a=download&type=fax_inbox&t=bin&filename=".$file_name.".pdf\">\n"; echo " pdf"; echo " </a>"; echo " </td>\n"; echo " <td class=\"listlr\" ondblclick=\"\">\n"; echo " <a href=\"freeswitch_fax_edit.php?id=".$id."&a=download&type=fax_inbox&t=png&filename=".$file_name.".png\" target=\"_blank\">\n"; echo " png"; echo " </a>"; echo " </td>\n"; echo " <td class=\"listlr\" ondblclick=\"\">\n"; echo date ("F d Y H:i:s", filemtime($dir_fax_inbox.$file)); echo " </td>\n"; echo " <td class=\"listlr\" ondblclick=\"\">\n"; echo " ".$tmp_filesize; echo " </td>\n"; echo " <td valign=\"middle\" nowrap class=\"list\">\n"; echo " <table border=\"0\" cellspacing=\"0\" cellpadding=\"1\">\n"; echo " <tr>\n"; //echo " <td valign=\"middle\"><a href=\"freeswitch_fax_edit.php?id=$i\"><img src=\"/themes/".$g['theme']."/images/icons/icon_e.gif\" width=\"17\" height=\"17\" border=\"0\"></a></td>\n"; echo " <td><a href=\"freeswitch_fax_edit.php?id=".$id."&type=fax_inbox&act=del&filename=".$file."\" onclick=\"return confirm('Do you really want to delete this file?')\"><img src=\"/themes/". $g['theme']."/images/icons/icon_x.gif\" width=\"17\" height=\"17\" border=\"0\"></a></td>\n"; echo " </tr>\n"; echo " </table>\n"; echo " </td>\n"; echo "</tr>\n"; } } } closedir($handle); } ?> <tr> <td class="list" colspan="3"></td> <td class="list"></td> </tr> </table> <br /> <br /> <br /> <br /> <table width="100%" border="0" cellpadding="5" cellspacing="0"> <tr> <td> <span class="vexpl"><span class="red"><strong>Sent</strong></span> </td> <td align='right'> <b>location:</b> <?php echo $dir_fax_sent; ?> </td> </tr> </table> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="50%" class="listhdrr">File Name (download)</td> <td width="10%" class="listhdrr">Download</td> <td width="10%" class="listhdrr">View</td> <td width="20%" class="listhdr">Last Modified</td> <td width="10%" class="listhdr" nowrap>Size</td> </tr> <?php if ($handle = opendir($dir_fax_sent)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && is_file($dir_fax_sent.$file)) { $tmp_filesize = filesize($dir_fax_sent.$file); $tmp_filesize = byte_convert($tmp_filesize); $tmp_file_array = split("\.",$file); $file_name = $tmp_file_array[0]; $file_ext = $tmp_file_array[1]; if ($file_ext == "tif") { echo "<tr>\n"; echo " <td class=\"listlr\" ondblclick=\"\">\n"; echo " <a href=\"freeswitch_fax_edit.php?id=".$id."&a=download&type=fax_sent&t=bin&filename=".$file."\">\n"; echo " $file"; echo " </a>"; echo " </td>\n"; echo " <td class=\"listlr\" ondblclick=\"\">\n"; echo " <a href=\"freeswitch_fax_edit.php?id=".$id."&a=download&type=fax_sent&t=bin&filename=".$file_name.".pdf\">\n"; echo " pdf"; echo " </a>"; echo " </td>\n"; echo " <td class=\"listlr\" ondblclick=\"\">\n"; echo " <a href=\"freeswitch_fax_edit.php?id=".$id."&a=download&type=fax_sent&t=png&filename=".$file_name.".png\" target=\"_blank\">\n"; echo " png"; echo " </a>"; echo " </td>\n"; echo " <td class=\"listlr\" ondblclick=\"\">\n"; echo date ("F d Y H:i:s", filemtime($dir_fax_sent.$file)); echo " </td>\n"; echo " <td class=\"listlr\" ondblclick=\"\">\n"; echo " ".$tmp_filesize; echo " </td>\n"; echo " <td valign=\"middle\" nowrap class=\"list\">\n"; echo " <table border=\"0\" cellspacing=\"0\" cellpadding=\"1\">\n"; echo " <tr>\n"; //echo " <td valign=\"middle\"><a href=\"freeswitch_fax_edit.php?id=$i\"><img src=\"/themes/".$g['theme']."/images/icons/icon_e.gif\" width=\"17\" height=\"17\" border=\"0\"></a></td>\n"; echo " <td><a href=\"freeswitch_fax_edit.php?id=".$id."&type=fax_sent&act=del&filename=".$file."\" onclick=\"return confirm('Do you really want to delete this file?')\"><img src=\"/themes/". $g['theme']."/images/icons/icon_x.gif\" width=\"17\" height=\"17\" border=\"0\"></a></td>\n"; echo " </tr>\n"; echo " </table>\n"; echo " </td>\n"; echo "</tr>\n"; } } } closedir($handle); } ?> <tr> <td class="list" colspan="3"></td> <td class="list"></td> </tr> </table> <br /> <br /> <br /> <br /> </td> </tr> </table> </div> <?php include("fend.inc"); ?> </body> </html>