Fork of FusionPBX but with LDAP kinda working
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

424 lines
16 KiB

2 years ago
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <markjcrane@fusionpbx.com>
  16. Portions created by the Initial Developer are Copyright (C) 2018-2020
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <markjcrane@fusionpbx.com>
  20. */
  21. //includes
  22. require_once "root.php";
  23. require_once "resources/require.php";
  24. require_once "resources/check_auth.php";
  25. require_once "resources/paging.php";
  26. //check permissions
  27. if (permission_exists('fax_file_view')) {
  28. //access granted
  29. }
  30. else {
  31. echo "access denied";
  32. exit;
  33. }
  34. //add multi-lingual support
  35. $language = new text;
  36. $text = $language->get();
  37. //get variables used to control the order
  38. $order_by = $_REQUEST["order_by"];
  39. $order = $_REQUEST["order"];
  40. //get the http post data
  41. if (is_array($_POST['fax_files'])) {
  42. $action = $_POST['action'];
  43. $fax_uuid = $_POST['fax_uuid'];
  44. $box = $_POST['box'];
  45. $fax_files = $_POST['fax_files'];
  46. }
  47. //process the http post data by action
  48. if ($action != '' && is_array($fax_files) && @sizeof($fax_files) != 0) {
  49. switch ($action) {
  50. case 'delete':
  51. if (permission_exists('fax_file_delete')) {
  52. $obj = new fax;
  53. $obj->fax_uuid = $fax_uuid;
  54. $obj->box = $box;
  55. $obj->delete_files($fax_files);
  56. }
  57. break;
  58. }
  59. header('Location: fax_files.php?orderby='.$order_by.'&order='.$order.'&id='.$fax_uuid.'&box='.$box);
  60. exit;
  61. }
  62. //get fax extension
  63. if (is_uuid($_GET["id"])) {
  64. $fax_uuid = $_GET["id"];
  65. if (permission_exists('fax_extension_view_domain')) {
  66. //show all fax extensions
  67. $sql = "select fax_name, fax_extension from v_fax ";
  68. $sql .= "where domain_uuid = :domain_uuid ";
  69. $sql .= "and fax_uuid = :fax_uuid ";
  70. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  71. $parameters['fax_uuid'] = $fax_uuid;
  72. }
  73. else {
  74. //show only assigned fax extensions
  75. $sql = "select fax_name, fax_extension from v_fax as f, v_fax_users as u ";
  76. $sql .= "where f.fax_uuid = u.fax_uuid ";
  77. $sql .= "and f.domain_uuid = :domain_uuid ";
  78. $sql .= "and f.fax_uuid = :fax_uuid ";
  79. $sql .= "and u.user_uuid = :user_uuid ";
  80. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  81. $parameters['fax_uuid'] = $fax_uuid;
  82. $parameters['user_uuid'] = $_SESSION['user_uuid'];
  83. }
  84. $database = new database;
  85. $row = $database->select($sql, $parameters, 'row');
  86. if (is_array($row) && @sizeof($row) != 0) {
  87. //set database fields as variables
  88. $fax_name = $row["fax_name"];
  89. $fax_extension = $row["fax_extension"];
  90. }
  91. else {
  92. if (!permission_exists('fax_extension_view_domain')) {
  93. echo "access denied";
  94. exit;
  95. }
  96. }
  97. unset($sql, $parameters, $row);
  98. }
  99. //set the fax directory
  100. $fax_dir = $_SESSION['switch']['storage']['dir'].'/fax/'.$_SESSION['domain_name'];
  101. //download the fax
  102. if ($_GET['a'] == "download") {
  103. //test to see if it is in the inbox or sent directory.
  104. if ($_GET['type'] == "fax_inbox") {
  105. if (file_exists($fax_dir.'/'.$_GET['ext'].'/inbox/'.$_GET['filename'])) {
  106. $tmp_faxdownload_file = $fax_dir.'/'.$_GET['ext'].'/inbox/'.$_GET['filename'];
  107. }
  108. }
  109. else if ($_GET['type'] == "fax_sent") {
  110. if (file_exists($fax_dir.'/'.$_GET['ext'].'/sent/'.$_GET['filename'])) {
  111. $tmp_faxdownload_file = $fax_dir.'/'.$_GET['ext'].'/sent/'.$_GET['filename'];
  112. }
  113. }
  114. //let's see if we found it
  115. if (strlen($tmp_faxdownload_file) > 0) {
  116. $fd = fopen($tmp_faxdownload_file, "rb");
  117. if ($_GET['t'] == "bin") {
  118. header("Content-Type: application/force-download");
  119. header("Content-Type: application/octet-stream");
  120. header("Content-Description: File Transfer");
  121. header('Content-Disposition: attachment; filename="'.$_GET['filename'].'"');
  122. }
  123. else {
  124. $file_ext = substr($_GET['filename'], -3);
  125. if ($file_ext == "tif") {
  126. header("Content-Type: image/tiff");
  127. }
  128. else if ($file_ext == "png") {
  129. header("Content-Type: image/png");
  130. }
  131. else if ($file_ext == "jpg") {
  132. header('Content-Type: image/jpeg');
  133. }
  134. else if ($file_ext == "pdf") {
  135. header("Content-Type: application/pdf");
  136. }
  137. }
  138. header('Accept-Ranges: bytes');
  139. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  140. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // date in the past
  141. header("Content-Length: ".filesize($tmp_faxdownload_file));
  142. fpassthru($fd);
  143. }
  144. else {
  145. echo $text['label-file'];
  146. }
  147. exit;
  148. }
  149. //get the fax extension
  150. if (strlen($fax_extension) > 0) {
  151. //set the fax directories. example /usr/local/freeswitch/storage/fax/329/inbox
  152. $dir_fax_inbox = $fax_dir.'/'.$fax_extension.'/inbox';
  153. $dir_fax_sent = $fax_dir.'/'.$fax_extension.'/sent';
  154. $dir_fax_temp = $fax_dir.'/'.$fax_extension.'/temp';
  155. //make sure the directories exist
  156. if (!is_dir($_SESSION['switch']['storage']['dir'])) {
  157. mkdir($_SESSION['switch']['storage']['dir'], 0770, false);
  158. }
  159. if (!is_dir($fax_dir.'/'.$fax_extension)) {
  160. mkdir($fax_dir.'/'.$fax_extension, 0770, false);
  161. }
  162. if (!is_dir($dir_fax_inbox)) {
  163. mkdir($dir_fax_inbox, 0770, false);
  164. }
  165. if (!is_dir($dir_fax_sent)) {
  166. mkdir($dir_fax_sent, 0770, false);
  167. }
  168. if (!is_dir($dir_fax_temp)) {
  169. mkdir($dir_fax_temp, 0770, false);
  170. }
  171. }
  172. //prepare to page the results
  173. $sql = "select count(fax_file_uuid) from v_fax_files ";
  174. $sql .= "where fax_uuid = :fax_uuid ";
  175. $sql .= "and domain_uuid = :domain_uuid ";
  176. if ($_REQUEST['box'] == 'inbox') {
  177. $sql .= "and fax_mode = 'rx' ";
  178. }
  179. if ($_REQUEST['box'] == 'sent') {
  180. $sql .= "and fax_mode = 'tx' ";
  181. }
  182. $parameters['fax_uuid'] = $fax_uuid;
  183. $parameters['domain_uuid'] = $domain_uuid;
  184. $database = new database;
  185. $num_rows = $database->select($sql, $parameters, 'column');
  186. unset($sql, $parameters);
  187. //prepare to page the results
  188. $rows_per_page = ($_SESSION['domain']['paging']['numeric'] != '') ? $_SESSION['domain']['paging']['numeric'] : 50;
  189. $param = "&id=".$fax_uuid."&box=".$_GET['box']."&order_by=".$_GET['order_by']."&order=".$_GET['order'];
  190. $page = is_numeric($_GET['page']) ? $_GET['page'] : 0;
  191. list($paging_controls, $rows_per_page) = paging($num_rows, $param, $rows_per_page);
  192. list($paging_controls_mini, $rows_per_page) = paging($num_rows, $param, $rows_per_page, true);
  193. $offset = $rows_per_page * $page;
  194. //get the list
  195. $sql = "select * from v_fax_files ";
  196. $sql .= "where fax_uuid = :fax_uuid ";
  197. $sql .= "and domain_uuid = :domain_uuid ";
  198. if ($_REQUEST['box'] == 'inbox') {
  199. $sql .= "and fax_mode = 'rx' ";
  200. }
  201. if ($_REQUEST['box'] == 'sent') {
  202. $sql .= "and fax_mode = 'tx' ";
  203. }
  204. $parameters['fax_uuid'] = $fax_uuid;
  205. $parameters['domain_uuid'] = $domain_uuid;
  206. $sql .= order_by($order_by, $order, 'fax_date', 'desc');
  207. $sql .= limit_offset($rows_per_page, $offset);
  208. $database = new database;
  209. $fax_files = $database->select($sql, $parameters, 'all');
  210. unset($sql, $parameters);
  211. //create token
  212. $object = new token;
  213. $token = $object->create($_SERVER['PHP_SELF']);
  214. //include the header
  215. if ($_REQUEST['box'] == 'inbox' && permission_exists('fax_inbox_view')) {
  216. $document['title'] = escape($fax_name)." [".escape($fax_extension)."]: ".$text['title-inbox'];
  217. }
  218. if ($_REQUEST['box'] == 'sent' && permission_exists('fax_sent_view')) {
  219. $document['title'] = escape($fax_name)." [".escape($fax_extension)."]: ".$text['title-sent_faxes'];
  220. }
  221. require_once "resources/header.php";
  222. //show the content
  223. echo "<div class='action_bar' id='action_bar'>\n";
  224. echo " <div class='heading'>";
  225. if ($_REQUEST['box'] == 'inbox' && permission_exists('fax_inbox_view')) {
  226. echo "<b>".escape($fax_name)." [".escape($fax_extension)."]: ".$text['header-inbox']." (".$num_rows.")</b>";
  227. }
  228. if ($_REQUEST['box'] == 'sent' && permission_exists('fax_sent_view')) {
  229. echo "<b>".escape($fax_name)." [".escape($fax_extension)."]: ".$text['header-sent_faxes']." (".$num_rows.")</b>";
  230. }
  231. echo " </div>\n";
  232. echo " <div class='actions'>\n";
  233. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','link'=>'fax.php']);
  234. if (permission_exists('fax_file_delete') && $fax_files) {
  235. echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'name'=>'btn_delete','style'=>'margin-left: 15px;','onclick'=>"modal_open('modal-delete','btn_delete');"]);
  236. }
  237. if ($paging_controls_mini != '') {
  238. echo "<span style='margin-left: 15px;'>".$paging_controls_mini."</span>\n";
  239. }
  240. echo " </div>\n";
  241. echo " <div style='clear: both;'></div>\n";
  242. echo "</div>\n";
  243. if (permission_exists('fax_file_delete') && $fax_files) {
  244. echo modal::create(['id'=>'modal-delete','type'=>'delete','actions'=>button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_delete','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('delete'); list_form_submit('form_list');"])]);
  245. }
  246. echo "<form id='form_list' method='post'>\n";
  247. echo "<input type='hidden' id='action' name='action' value=''>\n";
  248. echo "<input type='hidden' name='fax_uuid' value='".escape($fax_uuid)."'>\n";
  249. echo "<input type='hidden' name='box' value='".escape($_REQUEST['box'])."'>\n";
  250. echo "<table class='list'>\n";
  251. echo "<tr class='list-header'>\n";
  252. if (permission_exists('fax_file_delete')) {
  253. echo " <th class='checkbox'>\n";
  254. echo " <input type='checkbox' id='checkbox_all' name='checkbox_all' onclick='list_all_toggle();' ".($fax_files ?: "style='visibility: hidden;'").">\n";
  255. echo " </th>\n";
  256. }
  257. echo th_order_by('fax_caller_id_name', $text['label-fax_caller_id_name'], $order_by, $order, "&id=".$fax_uuid."&box=".$_GET['box']."&page=".$_GET['page']);
  258. echo th_order_by('fax_caller_id_number', $text['label-fax_caller_id_number'], $order_by, $order, "&id=".$fax_uuid."&box=".$_GET['box']."&page=".$_GET['page']);
  259. if ($_REQUEST['box'] == 'sent') {
  260. echo th_order_by('fax_destination', $text['label-fax_destination'], $order_by, $order, "&id=".$fax_uuid."&box=".$_GET['box']."&page=".$_GET['page']);
  261. }
  262. echo "<th>".$text['table-file']."</th>\n";
  263. echo "<th width='10%'>".$text['table-view']."</th>\n";
  264. echo th_order_by('fax_date', $text['label-fax_date'], $order_by, $order, "&id=".$fax_uuid."&box=".$_GET['box']."&page=".$_GET['page']);
  265. echo "</tr>\n";
  266. if (is_array($fax_files) && @sizeof($fax_files) != 0) {
  267. $x = 0;
  268. foreach ($fax_files as $row) {
  269. $file = basename($row['fax_file_path']);
  270. if (strtolower(substr($file, -3)) == "tif" || strtolower(substr($file, -3)) == "pdf") {
  271. $file_name = substr($file, 0, (strlen($file) -4));
  272. }
  273. $file_ext = $row['fax_file_type'];
  274. //decode the base64
  275. if (strlen($row['fax_base64']) > 0) {
  276. if ($_REQUEST['box'] == 'inbox' && permission_exists('fax_inbox_view')) {
  277. if (!file_exists($dir_fax_inbox.'/'.$file)) {
  278. file_put_contents($dir_fax_inbox.'/'.$file, base64_decode($row['fax_base64']));
  279. }
  280. }
  281. if ($_REQUEST['box'] == 'sent' && permission_exists('fax_sent_view')) {
  282. if (!file_exists($dir_fax_sent.'/'.$file)) {
  283. //decode the base64
  284. file_put_contents($dir_fax_sent.'/'.$file, base64_decode($row['fax_base64']));
  285. }
  286. }
  287. }
  288. //convert the tif to pdf
  289. unset($dir_fax);
  290. if ($_REQUEST['box'] == 'inbox' && permission_exists('fax_inbox_view')) {
  291. if (!file_exists($dir_fax_inbox.'/'.$file_name.".pdf")) {
  292. $dir_fax = $dir_fax_inbox;
  293. }
  294. }
  295. if ($_REQUEST['box'] == 'sent' && permission_exists('fax_sent_view')) {
  296. if (!file_exists($dir_fax_sent.'/'.$file_name.".pdf")) {
  297. $dir_fax = $dir_fax_sent;
  298. }
  299. }
  300. if ($dir_fax != '') {
  301. chdir($dir_fax);
  302. //get fax resolution (ppi, W & H)
  303. $resp = exec("tiffinfo ".$file_name.".tif | grep 'Resolution:'");
  304. $resp_array = explode(' ', trim($resp));
  305. $ppi_w = (int) $resp_array[1];
  306. $ppi_h = (int) $resp_array[2];
  307. unset($resp_array);
  308. $gs_r = $ppi_w.'x'.$ppi_h; //used by ghostscript
  309. //get page dimensions/size (pixels/inches, W & H)
  310. $resp = exec("tiffinfo ".$file_name.".tif | grep 'Image Width:'");
  311. $resp_array = explode(' ', trim($resp));
  312. $pix_w = $resp_array[2];
  313. $pix_h = $resp_array[5];
  314. unset($resp_array);
  315. $gs_g = $pix_w.'x'.$pix_h; //used by ghostscript
  316. $page_width = $pix_w / $ppi_w;
  317. $page_height = $pix_h / $ppi_h;
  318. if ($page_width > 8.4 && $page_height > 13) {
  319. $page_width = 8.5;
  320. $page_height = 14;
  321. $page_size = 'legal';
  322. }
  323. else if ($page_width > 8.4 && $page_height < 12) {
  324. $page_width = 8.5;
  325. $page_height = 11;
  326. $page_size = 'letter';
  327. }
  328. else if ($page_width < 8.4 && $page_height > 11) {
  329. $page_width = 8.3;
  330. $page_height = 11.7;
  331. $page_size = 'a4';
  332. }
  333. //generate pdf from tif
  334. $cmd_tif2pdf = "tiff2pdf -u i -p ".$page_size." -w ".$page_width." -l ".$page_height." -f -o ".$dir_fax.'/'.$file_name.".pdf ".$dir_fax.'/'.$file_name.".tif";
  335. exec($cmd_tif2pdf);
  336. //echo $cmd_tif2pdf."<br >\n";
  337. //clean up temporary files, if any
  338. if (file_exists($dir_fax_temp.'/'.$file_name.'.pdf')) { @unlink($dir_fax_temp.'/'.$file_name.'.pdf'); }
  339. if (file_exists($dir_fax_temp.'/'.$file_name.'.tif')) { @unlink($dir_fax_temp.'/'.$file_name.'.tif'); }
  340. }
  341. if ($_REQUEST['box'] == 'inbox' && permission_exists('fax_inbox_view')) {
  342. $list_row_url = "fax_files.php?id=".urlencode($fax_uuid)."&a=download&type=fax_inbox&t=bin&ext=".urlencode($fax_extension)."&filename=".urlencode($file);
  343. }
  344. if ($_REQUEST['box'] == 'sent' && permission_exists('fax_sent_view')) {
  345. $list_row_url = "fax_files.php?id=".urlencode($fax_uuid)."&a=download&type=fax_sent&t=bin&ext=".urlencode($fax_extension)."&filename=".urlencode($file);
  346. }
  347. echo "<tr class='list-row' href='".$list_row_url."'>\n";
  348. if (permission_exists('fax_file_delete')) {
  349. echo " <td class='checkbox'>\n";
  350. echo " <input type='checkbox' name='fax_files[$x][checked]' id='checkbox_".$x."' value='true' onclick=\"if (!this.checked) { document.getElementById('checkbox_all').checked = false; }\">\n";
  351. echo " <input type='hidden' name='fax_files[$x][uuid]' value='".escape($row['fax_file_uuid'])."' />\n";
  352. echo " </td>\n";
  353. }
  354. echo " <td>".escape($row['fax_caller_id_name'])."&nbsp;</td>\n";
  355. echo " <td>".escape(format_phone($row['fax_caller_id_number']))."&nbsp;</td>\n";
  356. if ($_REQUEST['box'] == 'sent') {
  357. echo " <td>".escape(format_phone($row['fax_destination']))."&nbsp;</td>\n";
  358. }
  359. echo " <td><a href='".$list_row_url."'>".$file_name."</a></td>\n";
  360. echo " <td class='no-link'>\n";
  361. if ($_REQUEST['box'] == 'inbox') {
  362. $dir_fax = $dir_fax_inbox;
  363. }
  364. if ($_REQUEST['box'] == 'sent') {
  365. $dir_fax = $dir_fax_sent;
  366. }
  367. if (file_exists($dir_fax.'/'.$file_name.".pdf")) {
  368. if ($_REQUEST['box'] == 'inbox' && permission_exists('fax_inbox_view')) {
  369. echo " <a href=\"fax_files.php?id=".urlencode($fax_uuid)."&a=download&type=fax_inbox&t=bin&ext=".urlencode($fax_extension)."&filename=".urlencode($file_name).".pdf\">PDF</a>\n";
  370. }
  371. if ($_REQUEST['box'] == 'sent' && permission_exists('fax_sent_view')) {
  372. echo " <a href=\"fax_files.php?id=".urlencode($fax_uuid)."&a=download&type=fax_sent&t=bin&ext=".urlencode($fax_extension)."&filename=".urlencode($file_name).".pdf\">PDF</a>\n";
  373. }
  374. }
  375. echo " </td>\n";
  376. $fax_date = ($_SESSION['domain']['time_format']['text'] == '12h') ? date("F d Y H:i", $row['fax_epoch']) : date("F d Y H:i", $row['fax_epoch']);
  377. echo " <td>".$fax_date."&nbsp;</td>\n";
  378. echo "</tr>\n";
  379. $x++;
  380. }
  381. }
  382. unset($fax_files);
  383. echo "</table>\n";
  384. echo "<br />\n";
  385. echo "<div align='center'>".$paging_controls."</div>\n";
  386. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  387. echo "</form>\n";
  388. //include the footer
  389. require_once "resources/footer.php";
  390. ?>