Advertisement
Guest User

voicemail.module FreePBX Patch

a guest
Mar 16th, 2012
1,450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 32.76 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Functions for the interface to the voicemail recordings
  6.  */
  7.  
  8. /**
  9.   * Class for voicemail
  10.   */
  11. class Voicemail {
  12.   var $callme_num = "";
  13.  
  14.   /*
  15.    * rank (for prioritizing modules)
  16.    */
  17.   function rank() {
  18.  
  19.     $rank = 2;
  20.     return $rank;
  21.   }
  22.  
  23.   /*
  24.    * init
  25.    */
  26.   function init() {
  27.     $extension = $_SESSION['ari_user']['extension'];
  28.     $this->callme_num = callme_getnum($extension);
  29.     if (empty($this->callme_num)) {
  30.     $this->callme_num = $extension;     // callme_num defaults to user's extension.
  31.     callme_setnum($extension, $extension);
  32.     }
  33.   }
  34.  
  35.   /*
  36.    * Adds menu item to nav menu
  37.    *
  38.    * @param $args
  39.    *   Common arguments
  40.    */
  41.   function navMenu($args) {
  42.  
  43.     global $ARI_NO_LOGIN;
  44.  
  45.     // check logout
  46.     if ($_SESSION['ari_user'] && !$ARI_NO_LOGIN) {
  47.       $logout = 1;
  48.     }
  49.  
  50.     if ($logout!='') {
  51.       $ret .= "<small><small><a href='" . $_SESSION['ARI_ROOT'] . "?m=Voicemail&f=display'>" . _("Voicemail") . "</a></small></small>";
  52.     }
  53.  
  54.     return $ret;
  55.   }
  56.  
  57.   /*
  58.    * Deletes selected voicemails and updates page
  59.    *
  60.    * @param $args
  61.    *   Common arguments
  62.    */
  63.   function navSubMenu($args) {
  64.  
  65.     global $ASTERISK_VOICEMAIL_PATH;
  66.     global $ASTERISK_VOICEMAIL_FOLDERS;
  67.  
  68.     // args
  69.     $m = getArgument($args,'m');
  70.     $q = getArgument($args,'q');
  71.     $current_folder = getArgument($args,'folder');
  72.  
  73.     $context = $_SESSION['ari_user']['context'];
  74.     $extension = $_SESSION['ari_user']['extension'];
  75.  
  76.     // check for voicemail enabled or admin
  77.     if ($_SESSION['ari_user']['voicemail_enabled']!=1 ||
  78.           $extension=='admin') {
  79.       return;
  80.     }
  81.  
  82.     // make folder list
  83.     $paths = preg_split('/;/',$ASTERISK_VOICEMAIL_PATH);
  84.     $i = 0;
  85.     while ($ASTERISK_VOICEMAIL_FOLDERS[$i]) {
  86.  
  87.       $f = $ASTERISK_VOICEMAIL_FOLDERS[$i]['folder'];
  88.       $fn = $ASTERISK_VOICEMAIL_FOLDERS[$i]['name'];
  89.  
  90.       foreach($paths as $key => $path) {
  91.  
  92.         $path = appendPath($path,$context);
  93.         $path = appendPath($path,$extension);
  94.  
  95.         if (is_dir($path) && is_readable($path)) {
  96.           $dh = opendir($path);
  97.           while (false!== ($folder = readdir($dh))) {
  98.  
  99.             $folder_path = AppendPath($path,$folder);
  100.  
  101.             if($folder!="." && $folder!=".." &&
  102.                  filetype($folder_path)=='dir') {
  103.  
  104.               if ($f==$folder) {
  105.  
  106.                 // get message count
  107.                 $indexes = $this->getVoicemailIndex($folder_path,$q,$order,$sort);
  108.                 $record_count = 0;
  109.                 $record_count += $this->getVoicemailCount($indexes);
  110.  
  111.                 // set current folder color
  112.                 $class='';
  113.                 if ($current_folder==$folder ||
  114.                      ($current_folder=='' && $ASTERISK_VOICEMAIL_FOLDERS[0]['folder']==$folder)) {
  115.                   $class = "class='current'";
  116.                 }
  117.  
  118.                 // add folder to list
  119.                 $ret .= "<p><small><small>
  120.                           <a " . $class . " href='" . $_SESSION['ARI_ROOT'] . "?m=Voicemail&q=" . urlencode($q) . "&folder=" . $f. "'>
  121.                           " . $fn . " (" . $record_count . ")" . "
  122.                           </a>
  123.                         </small></small></p>";
  124.               }
  125.             }
  126.           }
  127.         }
  128.       }
  129.       $i++;
  130.     }
  131.  
  132.     return $ret;
  133.   }
  134.  
  135.   /*
  136.    * Acts on the selected voicemails in the method indicated by the action and updates page
  137.    *
  138.    * @param $args
  139.    *   Common arguments
  140.    */
  141.   function msgAction($args) {
  142.  
  143.     global $ASTERISK_VOICEMAIL_FOLDERS;
  144.  
  145.     // args
  146.     $m = getArgument($args,'m');
  147.     $a = getArgument($args,'a');
  148.     $folder = getArgument($args,'folder');
  149.     $q = getArgument($args,'q');
  150.     $start = getArgument($args,'start');
  151.     $span = getArgument($args,'span');
  152.     $order = getArgument($args,'order');
  153.     $sort = getArgument($args,'sort');
  154.  
  155.     // get files
  156.     $files = array();
  157.     foreach($_REQUEST as $key => $value) {
  158.       if (preg_match('/selected/',$key) && isset($_SESSION['ari_user']['recfiles'][$value])) {
  159.         array_push($files, $_SESSION['ari_user']['recfiles'][$value]);
  160.       }
  161.     }
  162.  
  163.     if ($a=='delete') {
  164.       if (count($files) > 0) {
  165.         $this->deleteVoicemailData($files);
  166.       }
  167.       else {
  168.         $_SESSION['ari_error']
  169.           = _("One or more messages must be selected before clicking delete.");
  170.       }
  171.     }
  172.     else if ($a=='move_to') {
  173.       $folder_rx = getArgument($args,'folder_rx');
  174.       if ($folder_rx=='') {
  175.         $_SESSION['ari_error']
  176.           = _("A folder must be selected before the message can be moved.");
  177.       }
  178.       else if (count($files) > 0) {
  179.         $context = $_SESSION['ari_user']['context'];
  180.         $extension = $_SESSION['ari_user']['extension'];
  181.         $this->moveVoicemailData($files, $context, $extension, $folder_rx);
  182.       }
  183.       else {
  184.         $_SESSION['ari_error']
  185.           = _("One or more messages must be selected before clicking move_to.");
  186.       }
  187.     }
  188.     else if ($a=='forward_to') {
  189.  
  190.       $mailbox_rx = getArgument($args,'mailbox_rx');
  191.       list($context_rx,$extension_rx) = preg_split('/\//',$mailbox_rx);
  192.       if ($extension_rx=='') {
  193.         $_SESSION['ari_error']
  194.           = _("An extension must be selected before the message can be forwarded.");
  195.       }
  196.       else if (count($files) > 0) {
  197.         $folder_rx = $ASTERISK_VOICEMAIL_FOLDERS[0]['folder'];
  198.         $this->moveVoicemailData($files, $context_rx, $extension_rx, $folder_rx, false);
  199.       }
  200.       else {
  201.         $_SESSION['ari_error']
  202.           = _("One or more messages must be selected before clicking forward_to.");
  203.       }
  204.     }
  205.     else if ($a=='email_to') {
  206.         $em_to = getArgument($args,'email_to_addr');
  207.         if ($em_to=='') {
  208.       $_SESSION['ari_error']
  209.             = _("You must enter one or more email addresses (comma-separated) before clicking email_to.");
  210.         }
  211.         else if (count($files) > 0) {
  212.         $_SESSION['ari_error'] = "";
  213.         $line_end = "\n";
  214.         // Check email address(es) and construct address list for TO field.
  215.         // TO
  216.         $email_list = preg_split("/,/", $em_to);
  217.         foreach ($email_list as $list_elem) {
  218.             $list_elem = trim($list_elem);
  219.             if (preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i", $list_elem)) {
  220.                 $email_to .= $list_elem . ",";
  221.             } else {
  222.                 $_SESSION['ari_error'] .= _("Warning: ") . $list_elem . _("is not a valid email address");
  223.             }
  224.         }
  225.         $email_to = trim($email_to, ","); // remove trailing comma
  226.         if ($email_to != "") {
  227.             $email_to = "To: " . $email_to;
  228.             // SUBJECT
  229.             $email_subject = "Subject: Voicemail forwarded from mailbox " . $_SESSION['ari_user']['extension'];
  230.             // DATE
  231.             $email_date = "Date: " . date("Y-m-d");
  232.             // MIME VERSION
  233.             $email_mime_ver = "MIME-Version: 1.0";
  234.  
  235.             // HEADERS
  236.             $headers = $email_to . $line_end . $email_subject . $line_end . $email_date . $line_end;
  237.             $headers .= $email_mime_ver . $line_end;
  238.  
  239.             $semi_rand = md5(time());
  240.             $boundary = "==VMAIL_MSG_Multipart_Boundary_x{$semi_rand}x";
  241.  
  242.             $headers .= "Content-Type: multipart/mixed; boundary=\"{$boundary}\"" . $line_end;
  243.  
  244.             $headers .= $line_end;  // end of headers
  245.  
  246.             $body = "This is a MIME message.  Please use a MIME-capable email client." . $line_end;
  247.             $body .= "--{$boundary}" . $line_end;
  248.             $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $line_end;
  249.             $body .= $line_end;
  250.  
  251.             if (count($files) == 1) {
  252.                 $body .= "You have been forwarded a voicemail message from mailbox " . $_SESSION['ari_user']['extension'] . "." . $line_end;
  253.                 $body .= "Please listen to the attached recording.  Thank you." . $line_end;
  254.             } else {
  255.                 $body .= "You have been forwarded " . count($files) . " voicemail messages from mailbox " . $_SESSION['ari_user']['extension'] . "." . $line_end;
  256.                 $body .= "Please listen to the attached recordings.  Thank you." . $line_end;
  257.             }
  258.             $body .= $line_end;
  259.             $wav_pat = '/wav$/i';
  260.             $gsm_pat = '/gsm$/i';
  261.             $att_counter = -1;
  262.             foreach ($files as $f) {
  263.                 if (preg_match($wav_pat, $f)) {
  264.                     $content_type_audio = "Content-Type: audio/x-wav";
  265.                 } else if (preg_match($gsm_pat, $f)) {
  266.                     $content_type_audio = "Content-Type: audio/gsm";
  267.                 } else {
  268.                     $content_type_audio = "Content-Type: application/octet-stream"; // binary file attachment
  269.                     $_SESSION['ari_error'] .= $f . ": " . _("The file type was not recognized.");
  270.                 }
  271.                 // Read in the attachment(s)
  272.                 $file = fopen($f, "rb");
  273.                 $file_data = fread($file, filesize($f));
  274.                 fclose($file);
  275.  
  276.                 $body .= "--" . $boundary . $line_end;
  277.                 $body .= $content_type_audio . $line_end;
  278.  
  279.                 preg_match("/msg[\d]*\.[A-Za-z]*/", $f, $fname_matches);
  280.                 $email_fname = $fname_matches[0];
  281.  
  282.                 $body .= "Content-Disposition: attachment; filename=\"$email_fname\"" . $line_end;
  283.                 $body .= "Content-Transfer-Encoding: base64" . $line_end;
  284.                 $body .= $line_end;
  285.  
  286.                 $body .= chunk_split(base64_encode($file_data));
  287.                 $body .= $line_end;
  288.             }
  289.  
  290.             $body .= $line_end;
  291.             $body .= "--{$boundary}--";
  292.  
  293.             $email_message = $headers . $body;
  294.             // If alternate mailer is defined in voicemail.conf, use it.
  295.             if (function_exists("parse_voicemailconf")) {
  296.                 global $amp_conf;
  297.                 $vmconf = null;
  298.                 $section = null;
  299.                 $vmail_path = rtrim($amp_conf["ASTETCDIR"],"/") . "/voicemail.conf";
  300.                 parse_voicemailconf($vmail_path, $vmconf, $section);
  301.             } else {
  302.                 $_SESSION['ari_error'] .= _("Warning: Could not access voicemail.conf.  Using default mailer.");
  303.             }
  304.             if (isset($vmconf["general"]["mailcmd"]) && $vmconf["general"]["mailcmd"] != "") {
  305.                 $email_mailcmd = $vmconf["general"]["mailcmd"];
  306.             } else {
  307.                 $email_mailcmd = "/usr/sbin/sendmail -t";
  308.             }
  309.             // Mail it!
  310.             if (($sendmail_pipe = popen($email_mailcmd, "w"))) {
  311.                 fputs($sendmail_pipe, $email_message);
  312.                 fclose($sendmail_pipe);
  313.             } else {
  314.                 $_SESSION['ari_error'] .= _("Mail operation failed");
  315.             }
  316.         } else {
  317.             $_SESSION['ari_error'] = _("No valid email address given.  Please try again.");
  318.         }
  319.     }
  320.     else {
  321.         $_SESSION['ari_error'] = _("One or more messages must be selected before clicking email_to.");
  322.     }
  323.     }
  324.  
  325.     // redirect to see updated page
  326.     $ret .= "
  327.      <head>
  328.        <script>
  329.        <!--
  330.          window.location = \"" . $_SESSION['ARI_ROOT'] . "?m=" . $m . "&folder=" . $folder . "&q=" . urlencode($q) . "&start=" . $start . "&span=" . $span . "&order=" . $order . "&sort=" . $sort .
  331. "\"
  332.        // -->
  333.        </script>
  334.      </head>";
  335.  
  336.     return $ret;
  337.   }
  338.  
  339.   /*
  340.    * Displays stats page
  341.    *
  342.    * @param $args
  343.    *   Common arguments
  344.    */
  345.   function display($args) {
  346.  
  347.     global $ASTERISK_VOICEMAIL_CONF;
  348.     global $ASTERISK_VOICEMAIL_PATH;
  349.     global $ASTERISK_VOICEMAIL_FOLDERS;
  350.     global $AJAX_PAGE_REFRESH_ENABLE;
  351.  
  352.     $voicemail_audio_format = $_COOKIE['ari_voicemail_audio_format'];
  353.  
  354.     $display = new DisplaySearch();
  355.  
  356.     // args
  357.     $m = getArgument($args,'m');
  358.     $f = getArgument($args,'f');
  359.     $q = getArgument($args,'q');
  360.     $start = getArgument($args,'start');
  361.     $span = getArgument($args,'span');
  362.     $order = getArgument($args,'order');
  363.     $sort = getArgument($args,'sort');
  364.  
  365.     $start = $start=='' ? 0 : $start;
  366.     $span = $span=='' ? 15 : $span;
  367.     $order = $order=='' ? 'calldate' : $order;
  368.     $sort = ($sort=='' || strtolower($sort) == 'desc') ? 'desc' : 'asc';
  369.  
  370.     $paths = preg_split('/;/',$ASTERISK_VOICEMAIL_PATH);
  371.  
  372.     $displayname = $_SESSION['ari_user']['displayname'];
  373.     $extension = $_SESSION['ari_user']['extension'];
  374.     $context = $_SESSION['ari_user']['context'];
  375.     $folder = getArgument($args,'folder');
  376.     if (!$folder) {
  377.       $folder = $ASTERISK_VOICEMAIL_FOLDERS[0]['folder'];
  378.     }
  379.  
  380.     // get data
  381.     $data = array();
  382.     foreach($paths as $key => $path) {
  383.       $path = fixPathSlash($path);
  384.       $vm_path = $path . "$context/$extension/$folder";
  385.       $indexes = $this->getVoicemailIndex($vm_path,$q,$order,$sort);
  386.       $record_count += $this->getVoicemailCount($indexes);
  387.       $data = array_merge($data,$this->getVoicemailData($indexes,$start,$span));
  388.     }
  389.  
  390.     // build controls
  391.  
  392.     // get the recordings from the asterisk server
  393.     $filter = '';
  394.     $recursiveMax = 1;
  395.     $recursiveCount = 0;
  396.     $files = array();
  397.     foreach($paths as $key => $path) {
  398.       $path_files = GetFiles($path,$filter,$recursiveMax,$recursiveCount);
  399.       $files = array_merge($files,$path_files);
  400.     }
  401.  
  402.     // move options
  403.     $i=0;
  404.     while ($ASTERISK_VOICEMAIL_FOLDERS[$i]) {
  405.       $cf = $ASTERISK_VOICEMAIL_FOLDERS[$i]['folder'];
  406.       $fn = $ASTERISK_VOICEMAIL_FOLDERS[$i]['name'];
  407.       if ($cf!=$folder) {
  408.         $move_options .= "<option VALUE='" . $cf . "'>&nbsp;&nbsp;&nbsp;&nbsp;" .  $fn;
  409.       }
  410.       $i++;
  411.     }
  412.  
  413.     // forward options
  414.     if (is_readable($ASTERISK_VOICEMAIL_CONF)) {
  415.       $lines = file($ASTERISK_VOICEMAIL_CONF);
  416.       $ext_array = array();
  417.       foreach ($lines as $key => $line) {
  418.  
  419.         // get context for forward to mailbox
  420.         if (preg_match("/\[.*\]/i",$line)) {
  421.           $forwardContext = trim(preg_replace('/\[|\]/', '', $line));
  422.         }
  423.                 if ($forwardContext!=$_SESSION['ari_user']['context']) {
  424.                     continue;
  425.                 }
  426.  
  427.         // get username and add to options
  428.         if (preg_match("/\=\>/i",$line)) {
  429.           list($username,$value) = preg_split('/=>/',$line);
  430.           $username = trim($username);
  431.           if ($username!=$_SESSION['ari_user']['extension']) {
  432.             //$ext_array[] = $username . "|" . $forwardContext;
  433.             list(,$real_name,) = preg_split("/,/",$value,3);
  434.             $ext_array[] = $real_name . "|" . $username . "|" . $forwardContext;
  435.           }
  436.         }
  437.       } //foreach
  438.       //sort the array
  439.       sort($ext_array);
  440.  
  441.       //get the size of the array
  442.       $array_size = count($ext_array) - 1;
  443.  
  444.       //loop through the array and build the drop down list
  445.       foreach ($ext_array as $item)
  446.       {
  447.          //split the values apart
  448.          list($real_name,$username,$context) = explode("|",$item);
  449.  
  450.          //add it to the drop down
  451.          $forward_options .= "<option VALUE='" . $context . "/" . $username . "'>" . substr($real_name,0,15) . " <" . $username . ">";
  452.       }
  453.     }
  454.     else {
  455.       $_SESSION['ari_error'] = "File not readable: " . $ASTERISK_VOICEMAIL_CONF;
  456.       return;
  457.     }
  458.  
  459.     // table controls
  460.     $controls = "
  461.          <button class='infobar' type='submit' onclick=\"document.voicemail_form.a.value='delete'\">
  462.          " . _("delete") . "
  463.          </button>
  464.          <button class='infobar' type='submit' onclick=\"document.voicemail_form.a.value='move_to'\">
  465.          " . _("move_to") . "
  466.          </button>
  467.          <select name='folder_rx' style='width:124px;'>
  468.            <option VALUE=''>" . _("Folder") . "
  469.            " . $move_options . "
  470.          </select>
  471.          <button class='infobar' type='submit' onclick=\"document.voicemail_form.a.value='forward_to'\">
  472.          " . _("forward_to") . "
  473.          </button>
  474.          <select name='mailbox_rx'>
  475.            <option VALUE=''>
  476.            " . $forward_options . "
  477.       </select>
  478.          <button class='infobar' type='submit' onclick=\"document.voicemail_form.a.value='email_to'\">
  479.          " . _("email_to") . "
  480.          </button>
  481.          <input type='text' name='email_to_addr'/>";
  482.  
  483.     // table header
  484.     $recording_delete_header = "<th></th>";
  485.  
  486.     $fields[0]['field'] = "calldate";
  487.     $fields[0]['text'] = _("Date");
  488.     $fields[1]['field'] = "calldate";
  489.     $fields[1]['text'] = _("Time");
  490.     $fields[2]['field'] = "clid";
  491.     $fields[2]['text'] = _("Caller ID");
  492.     $fields[3]['field'] = "priority";
  493.     $fields[3]['text'] = _("Priority");
  494.     $fields[4]['field'] = "origmailbox";
  495.     $fields[4]['text'] = _("Orig Mailbox");
  496.     $fields[5]['field'] = "duration";
  497.     $fields[5]['text'] = _("Duration");
  498.     $i = 0;
  499.     while ($fields[$i]) {
  500.  
  501.       $field = $fields[$i]['field'];
  502.       $text = $fields[$i]['text'];
  503.       if ($order==$field) {
  504.         if ($sort=='asc') {
  505.           $currentSort = 'desc';
  506.           $arrowImg = "<img src='theme/images/arrow-asc.gif' alt='sort'>";
  507.         }
  508.         else {
  509.           $currentSort = 'asc';
  510.           $arrowImg = "<img src='theme/images/arrow-desc.gif' alt='sort'>";
  511.         }
  512.  
  513.         if ($i==1) {
  514.           $arrowImg = '';
  515.         }
  516.       }
  517.       else {
  518.         $arrowImg = '';
  519.         $currentSort = 'desc';
  520.       }
  521.  
  522.       $unicode_q = urlencode($q);
  523.       $recording_header .= "<th><a href=" .  $_SESSION['ARI_ROOT'] . "?m=" . $m . "&f=" . $f . "&q=" . $unicode_q . "&folder=" . $folder . "&order=" . $field . "&sort=" . $currentSort . ">" . $text . $arrowImg . "</a></th>";
  524.  
  525.       $i++;
  526.     }
  527.     $recording_header .= "<th>" . _("Playback") . "</th>";
  528.  
  529.     // Column to provide a download link for each message in voicemail.
  530.     $download_header .= "<th>" . _("Download"). "</th>";
  531.     // table body
  532.     unset($_SESSION['ari_user']['recfiles']);
  533.     if (isset($data)) {
  534.       $playbackRow = 2; // Index for where playback control rows used by javascript playback() should appear in the table.
  535.             // First control row would appear below row 1 (hence $playbackRow starts at 2); control rows are inserted/deleted as needed.
  536.       foreach($data as $file=>$value) {
  537.         $i++;
  538.         // Playback links
  539.         $voicemail_audio_format = $voicemail_audio_format=='' ? '.wav' : $voicemail_audio_format;
  540.         $recording = preg_replace('/.txt/', $voicemail_audio_format, $file);
  541.         $date = GetDateFormat($value['origtime']);
  542.         $time = GetTimeFormat($value['origtime']);
  543.         $from = $value[callerid];
  544.         $priority = $value[priority];
  545.         $to = $value[origmailbox];
  546.         $duration = $value[duration];
  547.         if (is_file($recording)) {
  548.           $_SESSION['ari_user']['recfiles'][$i] = $recording;
  549.       $recordingLink = "<a href='#' onClick=\"javascript:playback('play', $playbackRow, 'misc/play_page.php?recindex=$i'); return false;\"><img src='theme/images/sound.png' title=". _("Play") ."></img></a>";
  550.       $callmePage = "'misc/callme_page.php?recindex=$i&action=c'";
  551.       $callme_tooltip = _("Play message at: ") . $this->callme_num;
  552.           $callmeLink = "<a href='#' onClick=\"javascript:playback('callme', $playbackRow, $callmePage); return false;\"><img src='theme/images/telephone.png' title='$callme_tooltip'></img></a>";
  553.       $downloadLink = "<a href=misc/audio.php?recindex=$i><img src='theme/images/drive_go.png' title=" . _("Download") . "></img></a>";
  554.         }
  555.         else {
  556.           $_SESSION['ari_error'] = _("Voicemail recording(s) was not found.") . "<br>" .
  557.           sprintf(_("On settings page, change voicemail audio format. It is currently set to %s"), $voicemail_audio_format);
  558.         }
  559.  
  560.         $tableText .= "
  561.          <tr>
  562.            <td class='checkbox'><input type=checkbox name='selected" . $i . "' value=" . $i . "></td>
  563.            <td width=68>" . $date . "</td>
  564.            <td>" . $time . "</td>
  565.            <td width=100>" . $from . "</td>
  566.            <td>" . $value[priority] . "</td>
  567.            <td width=90>" . $to . "</td>
  568.            <td>" . $duration . " sec</td>
  569.            <td>" . $recordingLink . "&nbsp;&nbsp;" . $callmeLink . "</td>
  570.            <td>" . $downloadLink . "</td>
  571.          </tr>";
  572.  
  573.     $playbackRow++;
  574.       }
  575.     }
  576.  
  577.     // options
  578.     $url_opts = array();
  579.     $url_opts['folder'] = $folder;
  580.     $url_opts['sort'] = $sort;
  581.     $url_opts['order'] = $order;
  582.  
  583.     $error = 0;
  584.  
  585.     // check for voicemail enabled
  586.     if ($_SESSION['ari_user']['voicemail_enabled']!=1) {
  587.       $_SESSION['ari_error'] = _("Voicemail Login not found.") . "<br>" .
  588.                                _("No access to voicemail");
  589.       $error = 1;
  590.     }
  591.  
  592.     // check admin
  593.     if ($extension=='admin') {
  594.       $_SESSION['ari_error'] = _("No Voicemail Recordings for Admin");
  595.       $error = 1;
  596.     }
  597.  
  598.     // build page content
  599.     $ret .= checkErrorMessage();
  600.     if ($error) {
  601.       return $ret;
  602.     }
  603.  
  604.     // ajax page refresh script
  605.     if ($AJAX_PAGE_REFRESH_ENABLE) {
  606. //      $ret .= ajaxRefreshScript($args);
  607.     }
  608.  
  609.     // header
  610.     $ret .= $display->displayHeaderText(sprintf(_("Voicemail for %s (%s)"),$displayname,$extension));
  611.     $ret .= $display->displaySearchBlock('left',$m,$q,$url_opts,true);
  612.  
  613.     // pb_load_inprogress is a hidden element that is used by the javascript playback()
  614.     // as a boolean to keep track of whether or not a Playback (Call Me or Computer Play) from this page is in progress ("loading").
  615.     // start form
  616.     $ret .= "
  617.      <form name='voicemail_form' action='" . $_SESSION['ARI_ROOT'] . "' method='GET'>
  618.        <input type=hidden id='pb_load_inprogress' value='false'>
  619.        <input type=hidden name=m value=" . $m . ">
  620.        <input type=hidden name=f value=msgAction>
  621.        <input type=hidden name=a value=''>
  622.        <input type=hidden name=q value=" . urlencode($q) . ">
  623.        <input type=hidden name=folder value=" . $folder . ">
  624.        <input type=hidden name=start value=" . $start . ">
  625.        <input type=hidden name=span value=" . $span . ">
  626.        <input type=hidden name=order value=" . $order . ">
  627.        <input type=hidden name=sort value=" . $sort . ">";
  628.  
  629.     $ret .= $display->displayInfoBarBlock($controls,$q,$start,$span,$record_count);
  630.  
  631.     // Variables used in generating playback() javascript.
  632.     $callme_status_msg0 = _("Calling: ");
  633.     $callme_status_msg1 = _(". Please wait patiently...");
  634.  
  635.     // add javascript for playback and message actions
  636.     $ret .= "
  637.      <SCRIPT LANGUAGE='JavaScript'>
  638.      <!-- Begin
  639.      function checkAll(form,set) {
  640.        var elem = 0;
  641.        var i = 0;
  642.        while (elem = form.elements[i]) {
  643.          if (set) {
  644.            elem.checked = true;
  645.          } else {
  646.            elem.checked = false;
  647.          }
  648.          i++;
  649.        }
  650.        return true;
  651.      }
  652.  
  653.      // Playback function
  654.      function playback(mode, row_num, link) {
  655.     var playbackId = \"CURRENT__MSG\";
  656.     var i = 0;
  657.     var vmTable = document.getElementById('vmail_table');
  658.     var inprogress = document.getElementById('pb_load_inprogress').value;
  659.     // Only start a Playback control if another one is NOT in progress.
  660.     if (inprogress == \"false\") {
  661.         // Only one Playback control row can be open at a time.
  662.         // If one is already open (e.g. a call that is now over or a message already loaded for playback), close it.
  663.         for (i = 0; i < vmTable.rows.length; i++) {
  664.             if (vmTable.rows[i].id == playbackId) {
  665.                 // Delete the row; it's a Playback control row.
  666.                 vmTable.deleteRow(vmTable.rows[i].rowIndex);
  667.             }
  668.         }
  669.         // Make our Playback row.
  670.         playback_src = \"<iframe width='100%' height='25px' marginheight='0' marginwidth='0' frameborder='0' scrolling='no' src=\" + link + \"></iframe>\";
  671.         document.getElementById('pb_load_inprogress').value = \"true\";
  672.         newRow = vmTable.insertRow(row_num);
  673.         newRow.id = playbackId;
  674.         cell_left = newRow.insertCell(0);
  675.         if (mode == 'callme') {
  676.             cell_left.colSpan = 4;
  677.             cell_left.innerHTML = \"<div id='callme_status'>" . $callme_status_msg0 . $this->callme_num . $callme_status_msg1 . "</div>\";
  678.             cell_right = newRow.insertCell(1);
  679.             cell_right.colSpan = 5;
  680.             cell_right.innerHTML = playback_src;
  681.         } else {
  682.             cell_left.colSpan = 9;
  683.             cell_left.innerHTML = playback_src;
  684.         }
  685.     } else {
  686.         // Change background color of status cell to alert user that the playback is still loading.
  687.         document.getElementById(\"callme_status\").parentNode.style.backgroundColor = 'yellow';
  688.     }
  689.      }
  690.      // End -->
  691.      </script>";
  692.  
  693.     // voicemail delete recording controls
  694.     $ret .= "
  695.      <table>
  696.        <tr>
  697.          <td>
  698.            <small>" . _("select") . ": </small>
  699.            <small><a href='' OnClick=\"checkAll(document.voicemail_form,true); return false;\">" . _("all") . "</a></small>
  700.            <small><a href='' OnClick=\"checkAll(document.voicemail_form,false); return false;\">" . _("none") . "</a></small>
  701.          </td>
  702.        </tr>
  703.      </table>";
  704.  
  705.     // table
  706.     $ret .= "
  707.      <table id='vmail_table' class='voicemail'>
  708.        <tr>
  709.           " . $recording_delete_header . "
  710.           " . $recording_header . "
  711.           " . $download_header . "
  712.        </tr>
  713.        " . $tableText . "
  714.      </table>";
  715.  
  716.     // end form
  717.     $ret .= "</form>";
  718.  
  719.     $ret .= $display->displaySearchBlock('center',$m,$q,$url_opts,false);
  720.     $ret .= $display->displayNavigationBlock($m,$q,$url_opts,$start,$span,$record_count);
  721.  
  722.     return $ret;
  723.   }
  724.  
  725.   /*
  726.    * Gets voicemail data
  727.    *
  728.    * @param $data
  729.    *   Reference to the variable to store the data in
  730.    * @param $q
  731.    *   search string
  732.    */
  733.   function getVoicemailIndex($path,$q,$order,$sort) {
  734.  
  735.     $indexes = array();
  736.  
  737.     $filter = '.txt';
  738.     $recursiveMax = 0;
  739.     $recursiveCount = 0;
  740.     $files = getFiles($path,$filter,$recursiveMax,$recursiveCount);
  741.  
  742.     if (isset($files)) {
  743.  
  744.       // ugly, but sorts array by time stamp
  745.       foreach ($files as $file) {
  746.  
  747.         if (is_file($file)) {
  748.  
  749.           $lines = file($file);
  750.           foreach ($lines as $key => $line) {
  751.             unset($value);
  752.             list($key,$value) = preg_split('/=/',$line);
  753.             if ($value) {
  754.  
  755.               if ($key=="origtime") {
  756.                 $calldate = $value;
  757.                 $date = GetDateFormat($value);
  758.                 $time = GetTimeFormat($value);
  759.               }
  760.               if ($key=="callerid") {
  761.                 $callerid = $value;
  762.               }
  763.               if ($key=="priority") {
  764.                 $priority = $value;
  765.               }
  766.               if ($key=="origmailbox") {
  767.                 $origmailbox = $value;
  768.               }
  769.               if ($key=="duration") {
  770.                 $duration = (int)$value;
  771.               }
  772.             }
  773.           }
  774.  
  775.           // search filter
  776.           $found = 1;
  777.           if ($q) {
  778.  
  779.             $found = 0;
  780.  
  781.             if (preg_match("/" . $q . "/", $origmailbox) ||
  782.                   preg_match("/" . $q . "/", $callerid) ||
  783.                   preg_match("/" . $q . "/", $date) ||
  784.                   preg_match("/" . $q . "/", $time)) {
  785.               $found = 1;
  786.             }
  787.           }
  788.         }
  789.  
  790.         // add to index
  791.         if ($found) {
  792.           $indexes[$file] = $$order;
  793.         }
  794.       }
  795.  
  796.       if (count($indexes)) {
  797.         if ($sort=='desc') {
  798.           arsort($indexes);
  799.         }
  800.         else {
  801.           asort($indexes);
  802.         }
  803.       }
  804.     }
  805.  
  806.     return $indexes;
  807.   }
  808.  
  809.   /*
  810.    * Deletes selected voicemails
  811.    *
  812.    * @param $files
  813.    *   Array of files to delete
  814.    */
  815.   function deleteVoicemailData($files) {
  816.  
  817.     foreach($files as $key => $path) {
  818.  
  819.       // get file parts for search
  820.       $path_parts = pathinfo($path);
  821.       $path = fixPathSlash($path_parts['dirname']);
  822.  
  823.       list($name,$ext) = preg_split("/\./",$path_parts['basename']);
  824.  
  825.       // delete all related files using a wildcard
  826.       if (is_dir($path)) {
  827.         $hdl = opendir($path);
  828.         while ($fn = readdir($hdl)) {
  829.           if (preg_match("/" . $name ."/",$fn)) {
  830.             $file = $path . $fn;
  831.             unlink($file);
  832.           }
  833.         }
  834.         closedir($hdl);
  835.       }
  836.     }
  837.   }
  838.  
  839.   /*
  840.    * Moves selected voicemails to a specified folder
  841.    *
  842.    * @param $files
  843.    *   Array of files to delete
  844.    * @param $extension_rx
  845.    *   Mailbox to move message to
  846.    * @param $folder_rx
  847.    *   Folder to move the messages to
  848.    * @param $delete_moved
  849.    *   If original should be deleted, default true (otherwise it is a copy)
  850.    */
  851.   function moveVoicemailData($files,$context_rx,$extension_rx,$folder_rx,$delete_moved=true) {
  852.  
  853.     global $ASTERISK_VOICEMAIL_PATH;
  854.  
  855.     $perm = fileperms($ASTERISK_VOICEMAIL_PATH);
  856.     $uid = fileowner($ASTERISK_VOICEMAIL_PATH);
  857.     $gid = filegroup($ASTERISK_VOICEMAIL_PATH);
  858.  
  859.     // recieving path
  860.     $paths = preg_split('/;/',$ASTERISK_VOICEMAIL_PATH);
  861.     $path_rx = appendPath($paths[0],$context_rx);
  862.     if (!is_dir($path_rx)) {
  863.       mkdir($path_rx, $perm);
  864.       chown($path_rx,intval($uid));
  865.       chgrp($path_rx,intval($gid));
  866.     }
  867.     $path_rx = appendPath($path_rx,$extension_rx);
  868.     if (!is_dir($path_rx)) {
  869.       mkdir($path_rx, $perm);
  870.       chown($path_rx,intval($uid));
  871.       chgrp($path_rx,intval($gid));
  872.     }
  873.     $path_rx = appendPath($path_rx,$folder_rx);
  874.     if (!is_dir($path_rx)) {
  875.       mkdir($path_rx, $perm);
  876.       chown($path_rx,intval($uid));
  877.       chgrp($path_rx,intval($gid));
  878.     }
  879.  
  880.     // get recieving folder last message number
  881.     if (is_dir($path_rx)) {
  882.  
  883.       $lastNum = -1;
  884.       $lastNumLen = 4;
  885.  
  886.       $dh = opendir($path_rx);
  887.       while (false != ($filename = readdir($dh))) {
  888.         if($filename!="." && $filename!="..") {
  889.  
  890.           $msg_path = $path_rx;
  891.           $msg_path = appendPath($msg_path,$filename);
  892.           if (is_file($msg_path)) {
  893.             $path_parts = pathinfo($msg_path);
  894.             //fix for Serge Mankovski's "Voicemail RSS"
  895.             //split file basename into two pieces at the first '.'
  896.             //so that files like msg0000.7025f35d463ebbafa101db8a88c71b681aa8443d.mp3
  897.             //don't interfere with finding the true last file number
  898.             list($name,$ext) = preg_split("/\./",$path_parts['basename'],2);
  899.             $num = preg_replace("/[a-zA-Z]/",'', $name);
  900.             if ($num > $lastNum) {
  901.               $lastNum = $num;
  902.               $lastNumLen = strlen($lastNum);
  903.             }
  904.           }
  905.         }
  906.       }
  907.     }
  908.     else {
  909.       $_SESSION['ari_error'] = sprintf(_("Could not create mailbox folder %s on the server"),$folder_rx);
  910.       return;
  911.     }
  912.  
  913.     foreach($files as $key => $pathPlain) {
  914.       // add plain path to new array
  915.       $filesPlain[] = $pathPlain;
  916.     }
  917.  
  918.     // copy files to new location, incrementing each message number
  919.     asort($files);
  920.     foreach($files as $key => $path) {
  921.  
  922.       // get file parts for search
  923.       $path_parts = pathinfo($path);
  924.       $path = $path_parts['dirname'];
  925.       $path = fixPathSlash($path);
  926.       list($name,$ext) = preg_split("/\./",$path_parts['basename']);
  927.       if (is_dir($path)) {
  928.  
  929.         $lastNum++;
  930.         $hdl = opendir($path);
  931.         while ($fn = readdir($hdl)) {
  932.           if (preg_match("/" . $name . "/",$fn)) {
  933.             $src = $path . $fn;
  934.             $path_parts = pathinfo($src);
  935.             //fix for Serge Mankovski's "Voicemail RSS"
  936.             //split file basename into two pieces at the first '.'
  937.             //so that files like msg0000.7025f35d463ebbafa101db8a88c71b681aa8443d.mp3
  938.             //don't get clobbered by preg_replace() of digits
  939.             list($name,$ext) = preg_split("/\./",$path_parts['basename'],2);
  940.             $folder_rx = preg_replace("/\d+/",sprintf("%0" . $lastNumLen . "d",$lastNum),$name) . "." . $ext;
  941.             $dst = appendPath($path_rx,$folder_rx);
  942.             if (is_writable($src) && is_writable($path_rx)) {
  943.  
  944.               $perm = fileperms($src);
  945.               $uid = fileowner($src);
  946.               $gid = filegroup($src);
  947.  
  948.               copy($src,$dst);
  949.  
  950.               if (is_writable($dst)) {
  951.                 chmod($dst, $perm);
  952.                 chown($dst,intval($uid));
  953.                 chgrp($dst,intval($gid));
  954.               }
  955.  
  956.               if ($delete_moved) {
  957.                 unlink($src);
  958.               }
  959.             }
  960.             else {
  961.               $_SESSION['ari_error'] = sprintf(_("Permission denied on folder %s or %s"),$src,$path_rx);
  962.               return;
  963.             }
  964.           }
  965.         }
  966.         closedir($hdl);
  967.       }
  968.     }
  969.   }
  970.  
  971.   /*
  972.    * Gets voicemail record count
  973.    *
  974.    * @param $indexes
  975.    *   array of files to be counted
  976.    * @return $count
  977.    *   number of cdr records counted
  978.    */
  979.   function getVoicemailCount($indexes) {
  980.  
  981.     $count = count($indexes);
  982.  
  983.     return $count;
  984.   }
  985.  
  986.   /*
  987.    * Gets voicemail data
  988.    *
  989.    * @param $indexes
  990.    *   array of voicemail files
  991.    * @param $start
  992.    *   message number to start page with
  993.    * @param $span
  994.    *   number of messages to display on page
  995.    * @param $data
  996.    *   Reference to the variable to store the data in
  997.    */
  998.   function getVoicemailData($indexes,$start,$span) {
  999.  
  1000.     $data = array();
  1001.  
  1002.     if (!isset($indexes)) {
  1003.       return;
  1004.     }
  1005.  
  1006.     // populate array
  1007.     $i = 0;
  1008.     foreach ($indexes as $file => $index) {
  1009.       if ($i>$start-1+$span) {
  1010.         return $data;
  1011.       }
  1012.       elseif ($i>$start-1 && $i<$start+$span) {
  1013.         $lines = file($file);
  1014.         foreach ($lines as $key => $line) {
  1015.           unset($value);
  1016.           list($key,$value) = preg_split('/=/',$line);
  1017.           $key = trim($key);
  1018.           $value = trim($value);
  1019.           if ($value) {
  1020.             $data[$file][$key] = $value;
  1021.           }
  1022.         }
  1023.       }
  1024.       $i++;
  1025.     }
  1026.  
  1027.     return $data;
  1028.   }
  1029.  
  1030. }
  1031.  
  1032. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement