2013-01-02 00:22:07 +01: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-03-14 20:59:01 +01:00
Portions created by the Initial Developer are Copyright ( C ) 2008 - 2024
2013-01-02 00:22:07 +01:00
the Initial Developer . All Rights Reserved .
Contributor ( s ) :
Mark J Crane < markjcrane @ fusionpbx . com >
*/
//define the voicemail class
class voicemail {
2019-12-10 01:05:31 +01:00
/**
* declare public variables
*/
2013-01-02 00:22:07 +01:00
public $domain_uuid ;
public $domain_name ;
public $voicemail_uuid ;
public $voicemail_id ;
public $voicemail_message_uuid ;
2024-07-16 18:05:06 +02:00
public $user_uuid ;
2013-01-02 00:22:07 +01:00
public $order_by ;
public $order ;
2024-06-07 23:18:14 +02:00
public $offset ;
2020-02-13 15:15:59 +01:00
public $type ;
2019-12-10 01:05:31 +01:00
/**
* declare private variables
*/
private $app_name ;
private $app_uuid ;
private $permission_prefix ;
private $list_page ;
private $table ;
private $uuid_prefix ;
private $toggle_field ;
private $toggle_values ;
2016-07-22 18:31:08 +02:00
2024-03-16 20:46:38 +01:00
/**
2024-07-16 18:05:06 +02:00
* Internal array structure that is populated from the database
* @ var array Array of settings loaded from Default Settings
2024-03-16 20:46:38 +01:00
*/
private $settings ;
2024-07-31 04:32:47 +02:00
2024-07-16 18:05:06 +02:00
/**
* Set in the constructor . Must be a database object and cannot be null .
* @ var database Database Object
*/
private $database ;
2024-03-16 20:46:38 +01:00
public function __construct ( array $params = []) {
2019-12-10 01:05:31 +01:00
2024-08-02 17:46:54 +02:00
//set the domain_uuid if not provided
if ( ! empty ( $params [ 'domain_uuid' ]) && is_uuid ( $params [ 'domain_uuid' ])) {
$this -> domain_uuid = $params [ 'domain_uuid' ];
} else {
$this -> domain_uuid = $_SESSION [ 'domain_uuid' ] ? ? '' ;
}
//set the user_uuid if not provided
if ( ! empty ( $params [ 'user_uuid' ]) && is_uuid ( $params [ 'user_uuid' ])) {
$this -> user_uuid = $params [ 'user_uuid' ];
} else {
$this -> user_uuid = $_SESSION [ 'user_uuid' ] ? ? '' ;
}
2024-07-16 18:05:06 +02:00
//database connection
if ( empty ( $params [ 'database' ])) {
$this -> database = database :: new ();
} else {
$this -> database = $params [ 'database' ];
}
2024-07-31 21:41:08 +02:00
//assign the settings object
2024-08-02 17:46:54 +02:00
if ( empty ( $params [ 'settings' ])) {
$this -> settings = new settings ([ 'database' => $this -> database , 'domain_uuid' => $this -> domain_uuid , 'user_uuid' => $this -> user_uuid ]);
2024-07-31 21:41:08 +02:00
}
else {
2024-08-02 17:46:54 +02:00
$this -> settings = $params [ 'settings' ];
2024-07-31 21:41:08 +02:00
}
2019-12-10 01:05:31 +01:00
//assign private variables
$this -> app_name = 'voicemail' ;
2016-07-22 18:31:08 +02:00
$this -> app_uuid = 'b523c2d2-64cd-46f1-9520-ca4b4098e044' ;
2019-12-10 01:05:31 +01:00
$this -> permission_prefix = 'voicemail_' ;
$this -> list_page = 'voicemails.php' ;
$this -> table = 'voicemails' ;
$this -> uuid_prefix = 'voicemail_' ;
$this -> toggle_field = 'voicemail_enabled' ;
$this -> toggle_values = [ 'true' , 'false' ];
2013-01-02 00:22:07 +01:00
2016-07-22 18:31:08 +02:00
}
2016-12-01 19:53:12 +01:00
public function get_voicemail_id () {
2016-12-03 20:02:14 +01:00
//check if for valid input
2019-09-02 23:57:18 +02:00
if ( ! is_uuid ( $this -> voicemail_uuid ) || ! is_uuid ( $this -> domain_uuid )) {
2016-12-03 20:02:14 +01:00
return false ;
}
//get the voicemail id if it isn't set already
if ( ! isset ( $this -> voicemail_id )) {
$sql = " select voicemail_id from v_voicemails " ;
2019-09-02 23:57:18 +02:00
$sql .= " where domain_uuid = :domain_uuid " ;
$sql .= " and voicemail_uuid = :voicemail_uuid " ;
$parameters [ 'domain_uuid' ] = $this -> domain_uuid ;
$parameters [ 'voicemail_uuid' ] = $this -> voicemail_uuid ;
2024-07-16 18:05:06 +02:00
$voicemail_id = $this -> database -> select ( $sql , $parameters , 'column' );
2019-09-02 23:57:18 +02:00
if ( is_numeric ( $voicemail_id )) {
$this -> voicemail_id = $voicemail_id ;
2016-12-03 20:02:14 +01:00
}
2019-09-02 23:57:18 +02:00
unset ( $sql , $parameters , $voicemail_id );
2016-12-01 19:53:12 +01:00
}
}
2016-07-22 18:31:08 +02:00
public function voicemails () {
2013-11-23 05:19:37 +01:00
2016-12-03 20:02:14 +01:00
//check if for valid input
2019-09-02 23:57:18 +02:00
if ( ! is_uuid ( $this -> domain_uuid )) {
2016-12-03 20:02:14 +01:00
return false ;
}
2024-07-31 04:32:47 +02:00
//get the assigned extensions
$sql = " select e.extension_uuid, e.extension, e.number_alias, e.enabled, e.description " ;
$sql .= " from v_extensions e, v_extension_users eu " ;
$sql .= " where e.extension_uuid = eu.extension_uuid " ;
$sql .= " and eu.user_uuid = :user_uuid " ;
$sql .= " and e.domain_uuid = :domain_uuid " ;
$sql .= " order by e.extension asc " ;
$parameters [ 'domain_uuid' ] = $this -> domain_uuid ;
$parameters [ 'user_uuid' ] = $this -> user_uuid ;
$assigned_extensions = $this -> database -> select ( $sql , $parameters , 'all' );
unset ( $sql , $parameters );
//set the voicemail id arrays
2024-07-31 21:19:58 +02:00
$voicemail_ids = [];
2024-07-31 04:32:47 +02:00
if ( isset ( $assigned_extensions )) {
foreach ( $assigned_extensions as $index => $row ) {
$voicemail_ids [] = ( is_numeric ( $row [ 'number_alias' ])) ? $row [ 'number_alias' ] : $row [ 'extension' ];
2015-04-06 23:57:06 +02:00
}
}
2024-07-31 04:32:47 +02:00
//get the assigned voicemails
2024-08-16 23:10:05 +02:00
$assigned_voicemails = [];
if ( ! empty ( $voicemail_ids ) && @ sizeof ( $voicemail_ids ) != 0 ) {
$sql = " select * from v_voicemails " ;
$sql .= " where voicemail_id in ( " ;
foreach ( $voicemail_ids as $i => $voicemail_id ) {
if ( $i > 0 ) { $sql .= " , " ; }
$sql .= " :voicemail_id_ " . $i ;
$parameters [ 'voicemail_id_' . $i ] = $voicemail_id ;
}
$sql .= " ) " ;
$sql .= " and domain_uuid = :domain_uuid " ;
$parameters [ 'domain_uuid' ] = $this -> domain_uuid ;
$assigned_voicemails = $this -> database -> select ( $sql , $parameters , 'all' );
unset ( $sql , $parameters );
2024-07-31 04:32:47 +02:00
}
//set the voicemail uuid arrays
2024-07-31 21:19:58 +02:00
$voicemail_uuids = [];
2024-07-31 04:32:47 +02:00
if ( isset ( $assigned_voicemails )) {
foreach ( $assigned_voicemails as $row ) {
2023-05-05 18:46:37 +02:00
if ( ! empty ( $row [ 'voicemail_uuid' ])) {
2019-09-02 23:57:18 +02:00
$voicemail_uuids [][ 'voicemail_uuid' ] = $row [ 'voicemail_uuid' ];
}
2014-11-15 01:16:29 +01:00
}
2013-11-23 05:19:37 +01:00
}
//get the uuid and voicemail_id
$sql = " select * from v_voicemails " ;
2019-09-02 23:57:18 +02:00
$sql .= " where domain_uuid = :domain_uuid " ;
if ( is_uuid ( $this -> voicemail_uuid )) {
2013-11-23 05:19:37 +01:00
if ( permission_exists ( 'voicemail_delete' )) {
//view specific voicemail box usually reserved for an admin or superadmin
2019-09-02 23:57:18 +02:00
$sql .= " and voicemail_uuid = :voicemail_uuid " ;
$parameters [ 'voicemail_uuid' ] = $this -> voicemail_uuid ;
2013-11-23 05:19:37 +01:00
}
else {
2015-04-06 23:57:06 +02:00
//ensure that the requested voicemail box is assigned to this user
2013-11-23 05:19:37 +01:00
$found = false ;
2024-07-31 21:19:58 +02:00
foreach ( $voicemail_uuids as $row ) {
if ( $this -> voicemail_uuid == $row [ 'voicemail_uuid' ]) {
$sql .= " and voicemail_uuid = :voicemail_uuid " ;
$parameters [ 'voicemail_uuid' ] = $row [ 'voicemail_uuid' ];
$found = true ;
2013-11-23 05:19:37 +01:00
}
}
//id requested is not owned by the user return no results
if ( ! $found ) {
2015-04-06 23:57:06 +02:00
$sql .= " and voicemail_uuid is null " ;
2013-11-23 05:19:37 +01:00
}
}
}
else {
2023-05-11 06:18:42 +02:00
if ( ! empty ( $voicemail_ids ) && @ sizeof ( $voicemail_ids ) != 0 ) {
2013-11-23 05:19:37 +01:00
//show only the assigned voicemail ids
2019-12-24 08:13:15 +01:00
$sql .= " and " ;
if ( is_numeric ( $this -> voicemail_id ) && in_array ( $this -> voicemail_id , $voicemail_ids )) {
$sql_where = 'voicemail_id = :voicemail_id ' ;
$parameters [ 'voicemail_id' ] = $this -> voicemail_id ;
2013-11-23 05:19:37 +01:00
}
2019-12-24 08:13:15 +01:00
else {
$x = 0 ;
2023-05-11 00:27:19 +02:00
$sql_where = '' ;
2019-12-24 08:13:15 +01:00
foreach ( $voicemail_ids as $voicemail_id ) {
$sql_where_or [] = " voicemail_id = :voicemail_id_ " . $x ;
$parameters [ 'voicemail_id_' . $x ] = $voicemail_id ;
$x ++ ;
}
$sql_where .= '(' . implode ( ' or ' , $sql_where_or ) . ') ' ;
}
$sql .= $sql_where ;
2019-09-02 23:57:18 +02:00
unset ( $sql_where_or );
2013-11-23 05:19:37 +01:00
}
else {
//no assigned voicemail ids so return no results
2015-04-06 23:57:06 +02:00
$sql .= " and voicemail_uuid is null " ;
2013-11-23 05:19:37 +01:00
}
}
2015-01-29 08:18:29 +01:00
$sql .= " order by voicemail_id asc " ;
2019-09-02 23:57:18 +02:00
$parameters [ 'domain_uuid' ] = $this -> domain_uuid ;
2024-07-16 18:05:06 +02:00
$result = $this -> database -> select ( $sql , $parameters , 'all' );
2019-09-02 23:57:18 +02:00
unset ( $sql , $parameters );
2013-11-23 05:19:37 +01:00
return $result ;
}
2013-01-02 00:22:07 +01:00
public function messages () {
2024-08-29 19:17:18 +02:00
2013-11-23 05:19:37 +01:00
//get the voicemails
$voicemails = $this -> voicemails ();
//add the voicemail messages to the array
2019-09-02 23:57:18 +02:00
if ( is_array ( $voicemails )) {
2024-08-29 19:17:18 +02:00
$i = 0 ;
2024-08-22 20:41:10 +02:00
foreach ( $voicemails as $row ) {
2019-09-02 23:57:18 +02:00
//get the voicemail messages
2024-08-29 19:17:18 +02:00
$voicemails [ $i ][ 'messages' ] = $this -> voicemail_messages ( $row [ 'voicemail_id' ]);
$i ++ ;
2019-09-02 23:57:18 +02:00
}
2013-11-23 05:19:37 +01:00
}
2013-01-02 00:22:07 +01:00
2013-11-23 05:19:37 +01:00
//return the array
return $voicemails ;
}
2024-07-31 21:19:58 +02:00
private function voicemail_messages ( $voicemail_id ) : array {
2016-12-03 20:02:14 +01:00
//check if for valid input
2024-07-31 21:19:58 +02:00
if ( ! is_numeric ( $voicemail_id ) || ! is_uuid ( $this -> domain_uuid )) {
return [];
2013-01-02 00:22:07 +01:00
}
2023-05-18 04:41:52 +02:00
//set the time zone
2024-07-31 21:41:08 +02:00
$time_zone = $this -> settings -> get ( 'domain' , 'time_zone' , date_default_timezone_get ());
2023-05-18 04:41:52 +02:00
2016-12-03 20:02:14 +01:00
//get the message from the database
2023-05-18 04:41:52 +02:00
$sql = " select *, " ;
$sql .= " to_char(timezone(:time_zone, to_timestamp(m.created_epoch)), 'DD Mon YYYY') as created_date_formatted, \n " ;
$sql .= " to_char(timezone(:time_zone, to_timestamp(m.created_epoch)), 'HH12:MI:SS am') as created_time_formatted \n " ;
$sql .= " from v_voicemail_messages as m, v_voicemails as v " ;
2019-09-02 23:57:18 +02:00
$sql .= " where m.domain_uuid = :domain_uuid " ;
2016-12-03 20:02:14 +01:00
$sql .= " and m.voicemail_uuid = v.voicemail_uuid " ;
2024-07-31 21:19:58 +02:00
if ( is_array ( $voicemail_id ) && @ sizeof ( $voicemail_id ) != 0 ) {
2016-12-03 20:02:14 +01:00
$x = 0 ;
2019-09-02 23:57:18 +02:00
$sql .= " and ( " ;
2024-07-31 21:19:58 +02:00
foreach ( $voicemail_id as $row ) {
2019-09-02 23:57:18 +02:00
$sql_where_or [] = " v.voicemail_id = :voicemail_id_ " . $x ;
$parameters [ 'voicemail_id_' . $x ] = $row [ 'voicemail_id' ];
2016-12-03 20:02:14 +01:00
$x ++ ;
2013-01-02 00:22:07 +01:00
}
2019-09-02 23:57:18 +02:00
$sql .= implode ( ' or ' , $sql_where_or );
2016-12-03 20:02:14 +01:00
$sql .= " ) " ;
2019-09-02 23:57:18 +02:00
unset ( $sql_where_or );
2016-12-03 20:02:14 +01:00
}
else {
2019-09-02 23:57:18 +02:00
$sql .= " and v.voicemail_id = :voicemail_id " ;
2024-07-31 21:19:58 +02:00
$parameters [ 'voicemail_id' ] = $voicemail_id ;
2016-12-03 20:02:14 +01:00
}
2023-05-05 18:46:37 +02:00
if ( empty ( $this -> order_by )) {
2016-12-03 20:02:14 +01:00
$sql .= " order by v.voicemail_id, m.created_epoch desc " ;
}
else {
2019-09-02 23:57:18 +02:00
$sql .= " order by v.voicemail_id, m. " . $this -> order_by . " " . $this -> order . " " ;
2016-12-03 20:02:14 +01:00
}
2024-06-07 23:18:14 +02:00
//if paging offset defined, apply it along with rows per page
if ( isset ( $this -> offset )) {
2024-07-31 21:41:08 +02:00
$rows_per_page = $this -> settings -> get ( 'domain' , 'paging' , 50 );
2024-06-07 23:18:14 +02:00
$offset = isset ( $this -> offset ) && is_numeric ( $this -> offset ) ? $this -> offset : 0 ;
$sql .= limit_offset ( $rows_per_page , $offset );
}
2019-09-02 23:57:18 +02:00
$parameters [ 'domain_uuid' ] = $this -> domain_uuid ;
2023-05-18 04:41:52 +02:00
$parameters [ 'time_zone' ] = $time_zone ;
2024-07-16 18:05:06 +02:00
$result = $this -> database -> select ( $sql , $parameters , 'all' );
2019-09-02 23:57:18 +02:00
unset ( $sql , $parameters );
2023-05-18 04:41:52 +02:00
2016-12-03 20:02:14 +01:00
//update the array with additional information
if ( is_array ( $result )) {
2024-08-29 19:17:18 +02:00
$i = 0 ;
2024-08-22 20:41:10 +02:00
foreach ( $result as $row ) {
2016-12-03 20:02:14 +01:00
//set the greeting directory
2024-07-31 21:41:08 +02:00
$path = $this -> settings -> get ( 'switch' , 'voicemail' , '/var/lib/freeswitch/storage' ) . '/default/' . $_SESSION [ 'domain_name' ] . '/' . $row [ 'voicemail_id' ];
2016-12-03 20:02:14 +01:00
if ( file_exists ( $path . '/msg_' . $row [ 'voicemail_message_uuid' ] . '.wav' )) {
2024-08-29 19:17:18 +02:00
$result [ $i ][ 'file_path' ] = $path . '/msg_' . $row [ 'voicemail_message_uuid' ] . '.wav' ;
2016-12-03 20:02:14 +01:00
}
if ( file_exists ( $path . '/msg_' . $row [ 'voicemail_message_uuid' ] . '.mp3' )) {
2024-08-29 19:17:18 +02:00
$result [ $i ][ 'file_path' ] = $path . '/msg_' . $row [ 'voicemail_message_uuid' ] . '.mp3' ;
2016-12-03 20:02:14 +01:00
}
2024-08-29 19:17:18 +02:00
$result [ $i ][ 'file_size' ] = filesize ( $row [ 'file_path' ] ? ? '' );
$result [ $i ][ 'file_size_label' ] = byte_convert ( $row [ 'file_size' ] ? ? 0 );
$result [ $i ][ 'file_ext' ] = substr ( $row [ 'file_path' ] ? ? '' , - 3 );
2019-12-13 20:56:48 +01:00
$message_minutes = floor ( $row [ 'message_length' ] / 60 );
$message_seconds = $row [ 'message_length' ] % 60 ;
2024-08-29 19:17:18 +02:00
2019-12-13 20:56:48 +01:00
//use International System of Units (SI) - Source: https://en.wikipedia.org/wiki/International_System_of_Units
2024-08-29 19:17:18 +02:00
$result [ $i ][ 'message_length_label' ] = ( $message_minutes > 0 ? $message_minutes . ' min' : null ) . ( $message_seconds > 0 ? ' ' . $message_seconds . ' s' : null );
$result [ $i ][ 'created_date' ] = date ( " j M Y g:i a " , $row [ 'created_epoch' ]);
$i ;
2013-01-02 00:22:07 +01:00
}
2024-08-01 18:27:15 +02:00
}
else {
2024-07-31 21:19:58 +02:00
$result = [];
2013-01-02 00:22:07 +01:00
}
2016-12-03 20:02:14 +01:00
return $result ;
2013-01-02 00:22:07 +01:00
}
2019-12-10 01:05:31 +01:00
public function voicemail_delete ( $records ) {
if ( permission_exists ( $this -> permission_prefix . 'delete' )) {
2016-12-03 20:02:14 +01:00
2019-12-10 01:05:31 +01:00
//add multi-lingual support
$language = new text ;
$text = $language -> get ();
2016-12-03 21:32:55 +01:00
2019-12-10 01:05:31 +01:00
//validate the token
$token = new token ;
if ( ! $token -> validate ( $_SERVER [ 'PHP_SELF' ])) {
message :: add ( $text [ 'message-invalid_token' ], 'negative' );
header ( 'Location: ' . $this -> list_page );
exit ;
}
2016-12-03 20:02:14 +01:00
2019-12-10 01:05:31 +01:00
//delete multiple records
if ( is_array ( $records ) && @ sizeof ( $records ) != 0 ) {
2016-03-17 01:48:42 +01:00
2019-12-10 01:05:31 +01:00
//filter out unchecked sip profiles
foreach ( $records as $x => $record ) {
2023-06-09 18:51:36 +02:00
if ( ! empty ( $record [ 'checked' ]) && $record [ 'checked' ] == 'true' && is_uuid ( $record [ 'uuid' ])) {
2019-12-10 01:05:31 +01:00
$uuids [] = " ' " . $record [ 'uuid' ] . " ' " ;
}
}
//get necessary voicemail details
if ( is_array ( $uuids ) && @ sizeof ( $uuids ) != 0 ) {
$sql = " select " . $this -> uuid_prefix . " uuid as uuid, voicemail_id from v_ " . $this -> table . " " ;
$sql .= " where " . $this -> uuid_prefix . " uuid in ( " . implode ( ', ' , $uuids ) . " ) " ;
2024-07-16 18:05:06 +02:00
$rows = $this -> database -> select ( $sql , $parameters ? ? null , 'all' );
2019-12-10 01:05:31 +01:00
if ( is_array ( $rows ) && @ sizeof ( $rows ) != 0 ) {
foreach ( $rows as $row ) {
$voicemail_ids [ $row [ 'uuid' ]] = $row [ 'voicemail_id' ];
}
}
unset ( $sql , $parameters , $rows , $row );
}
//loop through voicemail ids
if ( is_array ( $voicemail_ids ) && @ sizeof ( $voicemail_ids ) != 0 ) {
$x = 0 ;
foreach ( $voicemail_ids as $voicemail_uuid => $voicemail_id ) {
//delete voicemail message recording and greeting files
if ( is_numeric ( $voicemail_id )) {
$file_path = $_SESSION [ 'switch' ][ 'voicemail' ][ 'dir' ] . " /default/ " . $_SESSION [ 'domain_name' ] . " / " . $voicemail_id ;
foreach ( glob ( $file_path . " /*.* " ) as $file_name ) {
@ unlink ( $file_name );
}
@ rmdir ( $file_path );
}
//reset message waiting indicator status
$this -> voicemail_id = $voicemail_id ;
$this -> voicemail_uuid = $voicemail_uuid ;
2024-07-16 18:05:06 +02:00
$this -> domain_uuid = $this -> domain_uuid ;
2019-12-10 01:05:31 +01:00
$this -> message_waiting ();
//build the delete array
$array [ $this -> table ][ $x ][ 'voicemail_uuid' ] = $voicemail_uuid ;
2024-07-16 18:05:06 +02:00
$array [ $this -> table ][ $x ][ 'domain_uuid' ] = $this -> domain_uuid ;
2019-12-10 01:05:31 +01:00
$array [ 'voicemail_options' ][ $x ][ 'voicemail_uuid' ] = $voicemail_uuid ;
2024-07-16 18:05:06 +02:00
$array [ 'voicemail_options' ][ $x ][ 'domain_uuid' ] = $this -> domain_uuid ;
2019-12-10 01:05:31 +01:00
$array [ 'voicemail_messages' ][ $x ][ 'voicemail_uuid' ] = $voicemail_uuid ;
2024-07-16 18:05:06 +02:00
$array [ 'voicemail_messages' ][ $x ][ 'domain_uuid' ] = $this -> domain_uuid ;
2019-12-10 01:05:31 +01:00
$array [ 'voicemail_destinations' ][ $x ][ 'voicemail_uuid' ] = $voicemail_uuid ;
2024-07-16 18:05:06 +02:00
$array [ 'voicemail_destinations' ][ $x ][ 'domain_uuid' ] = $this -> domain_uuid ;
2019-12-10 01:05:31 +01:00
if ( is_numeric ( $voicemail_id )) {
$array [ 'voicemail_greetings' ][ $x ][ 'voicemail_id' ] = $voicemail_id ;
2024-07-16 18:05:06 +02:00
$array [ 'voicemail_greetings' ][ $x ][ 'domain_uuid' ] = $this -> domain_uuid ;
2019-12-10 01:05:31 +01:00
}
2019-12-11 22:52:50 +01:00
$x ++ ;
2019-12-10 01:05:31 +01:00
}
}
//delete the checked rows
if ( is_array ( $array ) && @ sizeof ( $array ) != 0 ) {
//grant temporary permissions
2024-11-29 21:57:01 +01:00
$p = permissions :: new ();
2019-12-10 01:05:31 +01:00
$p -> add ( 'voicemail_delete' , 'temp' );
$p -> add ( 'voicemail_option_delete' , 'temp' );
$p -> add ( 'voicemail_message_delete' , 'temp' );
$p -> add ( 'voicemail_destination_delete' , 'temp' );
$p -> add ( 'voicemail_greeting_delete' , 'temp' );
//execute delete
2024-07-16 18:05:06 +02:00
$this -> database -> app_name = $this -> app_name ;
$this -> database -> app_uuid = $this -> app_uuid ;
$this -> database -> delete ( $array );
2019-12-10 01:05:31 +01:00
unset ( $array );
//revoke temporary permissions
$p -> delete ( 'voicemail_delete' , 'temp' );
$p -> delete ( 'voicemail_option_delete' , 'temp' );
$p -> delete ( 'voicemail_message_delete' , 'temp' );
$p -> delete ( 'voicemail_destination_delete' , 'temp' );
$p -> delete ( 'voicemail_greeting_delete' , 'temp' );
2020-11-30 22:15:57 +01:00
//clear the destinations session array
if ( isset ( $_SESSION [ 'destinations' ][ 'array' ])) {
unset ( $_SESSION [ 'destinations' ][ 'array' ]);
}
2019-12-10 01:05:31 +01:00
//set message
message :: add ( $text [ 'message-delete' ]);
}
unset ( $records , $voicemail_ids );
2016-12-03 21:32:55 +01:00
}
2019-12-10 01:05:31 +01:00
}
}
2016-03-17 01:48:42 +01:00
2020-03-04 03:13:16 +01:00
public function voicemail_options_delete ( $records ) {
//assign private variables
$this -> permission_prefix = 'voicemail_option_' ;
$this -> list_page = 'voicemail_edit.php?id=' . $this -> voicemail_uuid ;
$this -> table = 'voicemail_options' ;
$this -> uuid_prefix = 'voicemail_option_' ;
if ( permission_exists ( $this -> permission_prefix . 'delete' )) {
//add multi-lingual support
$language = new text ;
$text = $language -> get ();
//validate the token
$token = new token ;
if ( ! $token -> validate ( $_SERVER [ 'PHP_SELF' ])) {
message :: add ( $text [ 'message-invalid_token' ], 'negative' );
header ( 'Location: ' . $this -> list_page );
exit ;
}
//delete multiple records
if ( is_array ( $records ) && @ sizeof ( $records ) != 0 ) {
//filter out unchecked sip profiles
foreach ( $records as $x => $record ) {
2023-06-09 18:51:36 +02:00
if ( ! empty ( $record [ 'checked' ]) && $record [ 'checked' ] == 'true' && is_uuid ( $record [ 'uuid' ])) {
2020-03-04 03:13:16 +01:00
//build the delete array
$array [ $this -> table ][ $x ][ $this -> uuid_prefix . 'uuid' ] = $record [ 'uuid' ];
$array [ $this -> table ][ $x ][ 'voicemail_uuid' ] = $this -> voicemail_uuid ;
2024-07-16 18:05:06 +02:00
$array [ $this -> table ][ $x ][ 'domain_uuid' ] = $this -> domain_uuid ;
2020-03-04 03:13:16 +01:00
}
}
//delete the checked rows
if ( is_array ( $array ) && @ sizeof ( $array ) != 0 ) {
//execute delete
2024-07-16 18:05:06 +02:00
$this -> database -> app_name = $this -> app_name ;
$this -> database -> app_uuid = $this -> app_uuid ;
$this -> database -> delete ( $array );
2020-03-04 03:13:16 +01:00
unset ( $array );
}
unset ( $records );
}
}
}
public function voicemail_destinations_delete ( $records ) {
//assign private variables
$this -> list_page = 'voicemail_edit.php?id=' . $this -> voicemail_uuid ;
$this -> table = 'voicemail_destinations' ;
$this -> uuid_prefix = 'voicemail_destination_' ;
if ( permission_exists ( 'voicemail_forward' )) {
//add multi-lingual support
$language = new text ;
$text = $language -> get ();
//validate the token
$token = new token ;
if ( ! $token -> validate ( $_SERVER [ 'PHP_SELF' ])) {
message :: add ( $text [ 'message-invalid_token' ], 'negative' );
header ( 'Location: ' . $this -> list_page );
exit ;
}
//delete multiple records
if ( is_array ( $records ) && @ sizeof ( $records ) != 0 ) {
//filter out unchecked sip profiles
foreach ( $records as $x => $record ) {
2023-06-09 18:51:36 +02:00
if ( ! empty ( $record [ 'checked' ]) && $record [ 'checked' ] == 'true' && is_uuid ( $record [ 'uuid' ])) {
2020-03-04 03:13:16 +01:00
//build the delete array
$array [ $this -> table ][ $x ][ $this -> uuid_prefix . 'uuid' ] = $record [ 'uuid' ];
$array [ $this -> table ][ $x ][ 'voicemail_uuid' ] = $this -> voicemail_uuid ;
2024-07-16 18:05:06 +02:00
$array [ $this -> table ][ $x ][ 'domain_uuid' ] = $this -> domain_uuid ;
2020-03-04 03:13:16 +01:00
}
}
//delete the checked rows
2023-06-28 00:22:30 +02:00
if ( ! empty ( $array ) && is_array ( $array ) && @ sizeof ( $array ) != 0 ) {
2020-03-04 03:13:16 +01:00
//grant temporary permissions
2024-11-29 21:57:01 +01:00
$p = permissions :: new ();
2020-03-04 03:13:16 +01:00
$p -> add ( 'voicemail_destination_delete' , 'temp' );
//execute delete
2024-07-16 18:05:06 +02:00
$this -> database -> app_name = $this -> app_name ;
$this -> database -> app_uuid = $this -> app_uuid ;
$this -> database -> delete ( $array );
2020-03-04 03:13:16 +01:00
unset ( $array );
//revoke temporary permissions
$p -> delete ( 'voicemail_destination_delete' , 'temp' );
}
unset ( $records );
}
}
}
2019-12-10 01:05:31 +01:00
public function voicemail_toggle ( $records ) {
if ( permission_exists ( $this -> permission_prefix . 'edit' )) {
2019-09-02 23:57:18 +02:00
2019-12-10 01:05:31 +01:00
//add multi-lingual support
$language = new text ;
$text = $language -> get ();
2019-09-02 23:57:18 +02:00
2019-12-10 01:05:31 +01:00
//validate the token
$token = new token ;
if ( ! $token -> validate ( $_SERVER [ 'PHP_SELF' ])) {
message :: add ( $text [ 'message-invalid_token' ], 'negative' );
header ( 'Location: ' . $this -> list_page );
exit ;
}
2019-09-02 23:57:18 +02:00
2019-12-10 01:05:31 +01:00
//toggle the checked records
if ( is_array ( $records ) && @ sizeof ( $records ) != 0 ) {
2019-09-02 23:57:18 +02:00
2019-12-10 01:05:31 +01:00
//filter out unchecked sip profiles
foreach ( $records as $x => $record ) {
2023-06-09 18:51:36 +02:00
if ( ! empty ( $record [ 'checked' ]) && $record [ 'checked' ] == 'true' && is_uuid ( $record [ 'uuid' ])) {
2019-12-10 01:05:31 +01:00
$uuids [] = " ' " . $record [ 'uuid' ] . " ' " ;
}
}
2019-09-02 23:57:18 +02:00
2019-12-10 01:05:31 +01:00
//get necessary voicemail details
if ( is_array ( $uuids ) && @ sizeof ( $uuids ) != 0 ) {
$sql = " select " . $this -> uuid_prefix . " uuid as uuid, voicemail_id, " . $this -> toggle_field . " as toggle from v_ " . $this -> table . " " ;
$sql .= " where (domain_uuid = :domain_uuid or domain_uuid is null) " ;
$sql .= " and " . $this -> uuid_prefix . " uuid in ( " . implode ( ', ' , $uuids ) . " ) " ;
2024-07-16 18:05:06 +02:00
$parameters [ 'domain_uuid' ] = $this -> domain_uuid ;
$rows = $this -> database -> select ( $sql , $parameters , 'all' );
2019-12-10 01:05:31 +01:00
if ( is_array ( $rows ) && @ sizeof ( $rows ) != 0 ) {
foreach ( $rows as $row ) {
$voicemails [ $row [ 'uuid' ]][ 'state' ] = $row [ 'toggle' ];
$voicemails [ $row [ 'uuid' ]][ 'id' ] = $row [ 'voicemail_id' ];
}
}
unset ( $sql , $parameters , $rows , $row );
}
2016-03-17 01:48:42 +01:00
2019-12-10 01:05:31 +01:00
//loop through voicemails
if ( is_array ( $voicemails ) && @ sizeof ( $voicemails ) != 0 ) {
$x = 0 ;
foreach ( $voicemails as $voicemail_uuid => $voicemail ) {
//reset message waiting indicator status
$this -> voicemail_id = $voicemail [ 'id' ];
$this -> voicemail_uuid = $voicemail_uuid ;
2024-07-16 18:05:06 +02:00
$this -> domain_uuid = $this -> domain_uuid ;
2019-12-10 01:05:31 +01:00
$this -> message_waiting ();
//build update array
$array [ $this -> table ][ $x ][ $this -> uuid_prefix . 'uuid' ] = $voicemail_uuid ;
$array [ $this -> table ][ $x ][ $this -> toggle_field ] = $voicemail [ 'state' ] == $this -> toggle_values [ 0 ] ? $this -> toggle_values [ 1 ] : $this -> toggle_values [ 0 ];
$x ++ ;
}
}
2019-09-02 23:57:18 +02:00
2019-12-10 01:05:31 +01:00
//save the changes
if ( is_array ( $array ) && @ sizeof ( $array ) != 0 ) {
//save the array
2024-07-16 18:05:06 +02:00
$this -> database -> app_name = $this -> app_name ;
$this -> database -> app_uuid = $this -> app_uuid ;
$this -> database -> save ( $array );
2019-12-10 01:05:31 +01:00
unset ( $array );
2020-11-30 22:15:57 +01:00
//clear the destinations session array
if ( isset ( $_SESSION [ 'destinations' ][ 'array' ])) {
unset ( $_SESSION [ 'destinations' ][ 'array' ]);
}
2019-12-10 01:05:31 +01:00
//set message
message :: add ( $text [ 'message-toggle' ]);
}
unset ( $records , $voicemails );
}
}
2016-03-17 01:48:42 +01:00
}
2013-01-02 00:22:07 +01:00
public function message_count () {
2016-07-22 18:31:08 +02:00
2016-12-03 20:02:14 +01:00
//check if for valid input
2019-09-02 23:57:18 +02:00
if ( ! is_uuid ( $this -> voicemail_uuid ) || ! is_uuid ( $this -> domain_uuid )) {
2016-12-03 20:02:14 +01:00
return false ;
2013-01-02 00:22:07 +01:00
}
2024-07-16 18:05:06 +02:00
2016-12-03 20:02:14 +01:00
//return the message count
2019-09-02 23:57:18 +02:00
$sql = " select count(*) from v_voicemail_messages " ;
$sql .= " where domain_uuid = :domain_uuid " ;
$sql .= " and voicemail_uuid = :voicemail_uuid " ;
$parameters [ 'domain_uuid' ] = $this -> domain_uuid ;
$parameters [ 'voicemail_uuid' ] = $this -> voicemail_uuid ;
2024-07-16 18:05:06 +02:00
return $this -> database -> select ( $sql , $parameters , 'column' );
2019-09-02 23:57:18 +02:00
unset ( $sql , $parameters );
2013-01-02 00:22:07 +01:00
}
2013-01-13 07:47:28 +01:00
public function message_waiting () {
2017-08-02 00:02:08 +02:00
//get the voicemail id
$this -> get_voicemail_id ();
2013-01-13 07:47:28 +01:00
//send the message waiting status
2023-05-17 08:59:40 +02:00
2023-12-03 01:16:18 +01:00
$esl = event_socket :: create ();
if ( $esl -> is_connected ()) {
2023-05-17 08:59:40 +02:00
$switch_cmd = " luarun app.lua voicemail mwi " . $this -> voicemail_id . " @ " . $_SESSION [ 'domain_name' ];
2023-12-03 01:16:18 +01:00
$switch_result = event_socket :: api ( $switch_cmd );
2016-03-29 01:50:07 +02:00
}
2013-01-13 07:47:28 +01:00
}
public function message_delete () {
2017-08-02 00:02:08 +02:00
2016-12-03 20:02:14 +01:00
//get the voicemail id
$this -> get_voicemail_id ();
//check if for valid input
2019-09-02 23:57:18 +02:00
if ( ! is_numeric ( $this -> voicemail_id )
|| ! is_uuid ( $this -> voicemail_uuid )
|| ! is_uuid ( $this -> domain_uuid )
|| ! is_uuid ( $this -> voicemail_message_uuid )
) {
2016-12-03 20:02:14 +01:00
return false ;
}
2013-01-02 00:22:07 +01:00
2013-01-13 07:47:28 +01:00
//delete the recording
2015-11-02 18:22:13 +01:00
$file_path = $_SESSION [ 'switch' ][ 'voicemail' ][ 'dir' ] . " /default/ " . $_SESSION [ 'domain_name' ] . " / " . $this -> voicemail_id ;
2019-09-02 23:57:18 +02:00
if ( is_uuid ( $this -> voicemail_message_uuid )) {
2024-08-01 18:27:15 +02:00
foreach ( glob ( $file_path . " /intro_msg_ " . $this -> voicemail_message_uuid . " .* " ) as $file_name ) {
unlink ( $file_name );
}
2016-09-03 23:43:30 +02:00
foreach ( glob ( $file_path . " /intro_ " . $this -> voicemail_message_uuid . " .* " ) as $file_name ) {
unlink ( $file_name );
}
2016-05-11 06:53:42 +02:00
foreach ( glob ( $file_path . " /msg_ " . $this -> voicemail_message_uuid . " .* " ) as $file_name ) {
unlink ( $file_name );
2016-03-17 01:48:42 +01:00
}
2013-11-23 05:19:37 +01:00
}
2016-03-17 01:48:42 +01:00
else {
2019-12-10 01:05:31 +01:00
foreach ( glob ( $file_path . " /msg_*.* " ) as $file_name ) {
2016-05-11 06:53:42 +02:00
unlink ( $file_name ); //remove all recordings
2016-03-17 01:48:42 +01:00
}
}
2019-09-02 23:57:18 +02:00
//build delete array
$array [ 'voicemail_messages' ][ 0 ][ 'domain_uuid' ] = $this -> domain_uuid ;
$array [ 'voicemail_messages' ][ 0 ][ 'voicemail_uuid' ] = $this -> voicemail_uuid ;
if ( is_uuid ( $this -> voicemail_message_uuid )) {
$array [ 'voicemail_messages' ][ 0 ][ 'voicemail_message_uuid' ] = $this -> voicemail_message_uuid ;
2016-03-17 01:48:42 +01:00
}
2019-09-02 23:57:18 +02:00
//grant temporary permissions
2024-11-29 21:57:01 +01:00
$p = permissions :: new ();
2019-09-02 23:57:18 +02:00
$p -> add ( 'voicemail_message_delete' , 'temp' );
//execute delete
2024-07-16 18:05:06 +02:00
$this -> database -> app_name = $this -> app_name ;
$this -> database -> app_name = $this -> app_uuid ;
$this -> database -> delete ( $array );
2019-09-02 23:57:18 +02:00
unset ( $array );
//revoke temporary permissions
$p -> delete ( 'voicemail_message_delete' , 'temp' );
2013-01-02 00:22:07 +01:00
2013-01-13 07:47:28 +01:00
//check the message waiting status
$this -> message_waiting ();
}
2016-03-29 01:50:07 +02:00
public function message_toggle () {
2016-12-03 20:02:14 +01:00
//check if for valid input
2019-09-02 23:57:18 +02:00
if ( ! is_uuid ( $this -> voicemail_uuid )
|| ! is_uuid ( $this -> domain_uuid )
|| ! is_uuid ( $this -> voicemail_message_uuid )
) {
2016-12-03 20:02:14 +01:00
return false ;
}
2016-03-29 01:50:07 +02:00
//get message status
$sql = " select message_status from v_voicemail_messages " ;
2019-09-02 23:57:18 +02:00
$sql .= " where voicemail_message_uuid = :voicemail_message_uuid " ;
$parameters [ 'voicemail_message_uuid' ] = $this -> voicemail_message_uuid ;
2024-07-16 18:05:06 +02:00
$new_status = $this -> database -> select ( $sql , $parameters , 'column' ) != 'saved' ? 'saved' : null ;
2019-09-02 23:57:18 +02:00
unset ( $sql , $parameters );
//build message status update array
$array [ 'voicemail_messages' ][ 0 ][ 'voicemail_message_uuid' ] = $this -> voicemail_message_uuid ;
$array [ 'voicemail_messages' ][ 0 ][ 'message_status' ] = $new_status ;
//grant temporary permissions
2024-11-29 21:57:01 +01:00
$p = permissions :: new ();
2019-09-02 23:57:18 +02:00
$p -> add ( 'voicemail_message_edit' , 'temp' );
//execute update
2024-07-16 18:05:06 +02:00
$this -> database -> app_name = $this -> app_name ;
$this -> database -> app_name = $this -> app_uuid ;
$this -> database -> save ( $array );
2019-09-02 23:57:18 +02:00
unset ( $array );
//revoke temporary permissions
$p -> delete ( 'voicemail_message_edit' , 'temp' );
2016-03-29 01:50:07 +02:00
//check the message waiting status
$this -> message_waiting ();
}
2024-07-11 00:28:41 +02:00
public function message_resend () {
//check if for valid input
if ( ! is_uuid ( $this -> voicemail_uuid )
|| ! is_uuid ( $this -> domain_uuid )
|| ! is_uuid ( $this -> voicemail_message_uuid )
) {
return false ;
}
//add multi-lingual support
$language = new text ;
$text = $language -> get ();
//add the settings object
2024-07-16 18:05:06 +02:00
$settings = new settings ([ " domain_uuid " => $this -> domain_uuid , " user_uuid " => $this -> user_uuid ]);
2024-07-11 00:28:41 +02:00
$email_from = $settings -> get ( 'email' , 'smtp_from' , '' );
$email_from_name = $settings -> get ( 'email' , 'smtp_from_name' , 'PBX' );
$switch_scripts = $settings -> get ( 'switch' , 'scripts' , '/usr/share/freeswitch/scripts' );
$switch_voicemail = $settings -> get ( 'switch' , 'voicemail' , '/var/lib/freeswitch/storage/voicemail' );
$language_dialect = $settings -> get ( 'domain' , 'language' , 'en-us' );
$time_zone = $settings -> get ( 'domain' , 'time_zone' , 'UTC' );
$display_domain_name = $settings -> get ( 'voicemail' , 'display_domain_name' , 'false' );
//get voicemail message details
$sql = " select " ;
$sql .= " vm.*, " ;
$sql .= " to_char(timezone(:time_zone, to_timestamp(vm.created_epoch)), 'Day DD Mon YYYY HH:MI:SS PM') as message_date, " ;
$sql .= " v.voicemail_id, " ;
$sql .= " v.voicemail_mail_to, " ;
$sql .= " v.voicemail_description, " ;
$sql .= " v.voicemail_file, " ;
$sql .= " d.domain_name " ;
$sql .= " from " ;
$sql .= " v_voicemail_messages as vm " ;
$sql .= " left join v_voicemails as v on vm.voicemail_uuid = v.voicemail_uuid " ;
$sql .= " left join v_domains as d on vm.domain_uuid = d.domain_uuid " ;
$sql .= " where " ;
$sql .= " vm.voicemail_message_uuid = :voicemail_message_uuid " ;
$sql .= " limit 1 " ;
$parameters [ 'time_zone' ] = $time_zone ;
$parameters [ 'voicemail_message_uuid' ] = $this -> voicemail_message_uuid ;
2024-07-16 18:05:06 +02:00
$message = $this -> database -> select ( $sql , $parameters , 'row' );
2024-07-11 00:28:41 +02:00
unset ( $sql , $parameters );
//retrieve appropriate email template
$sql = " select " ;
$sql .= " template_subject, " ;
$sql .= " template_body " ;
$sql .= " from " ;
$sql .= " v_email_templates " ;
$sql .= " where " ;
$sql .= " template_language = :template_language " ;
$sql .= " and template_category = 'voicemail' " ;
$sql .= " and template_subcategory = ' " . ( ! empty ( $message [ 'message_transcription' ]) ? 'transcription' : 'default' ) . " ' " ;
$sql .= " and template_type = 'html' " ;
$sql .= " and template_enabled = 'true' " ;
$sql .= " and (domain_uuid = :domain_uuid or domain_uuid is null) " ;
$sql .= " limit 1 " ;
$parameters [ 'template_language' ] = $language_dialect ;
$parameters [ 'domain_uuid' ] = $this -> domain_uuid ;
2024-07-16 18:05:06 +02:00
$template = $this -> database -> select ( $sql , $parameters , 'row' );
2024-07-11 00:28:41 +02:00
unset ( $sql , $parameters );
//determine formatted voicemail name
$voicemail_name_formatted = $message [ 'voicemail_id' ];
if ( $display_domain_name == 'true' ) {
$voicemail_name_formatted = $message [ 'voicemail_id' ] . '@' . $message [ 'domain_name' ];
}
if ( ! empty ( $message [ 'voicemail_description' ])) {
$voicemail_name_formatted .= ' (' . $message [ 'voicemail_description' ] . ')' ;
}
//replace subject variables
if ( ! empty ( $template [ 'template_subject' ])) {
$template [ 'template_subject' ] = str_replace ( '${caller_id_name}' , $message [ 'caller_id_name' ], $template [ 'template_subject' ]);
$template [ 'template_subject' ] = str_replace ( '${caller_id_number}' , $message [ 'caller_id_number' ], $template [ 'template_subject' ]);
$template [ 'template_subject' ] = str_replace ( '${message_date}' , $message [ 'message_date' ], $template [ 'template_subject' ]);
$template [ 'template_subject' ] = str_replace ( '${message_duration}' , '0' . gmdate ( " G:i:s " , ( $message [ 'message_length' ] ? ? 0 )), $template [ 'template_subject' ]);
$template [ 'template_subject' ] = str_replace ( '${account}' , $voicemail_name_formatted , $template [ 'template_subject' ]);
$template [ 'template_subject' ] = str_replace ( '${voicemail_id}' , $message [ 'voicemail_id' ], $template [ 'template_subject' ]);
$template [ 'template_subject' ] = str_replace ( '${voicemail_description}' , $message [ 'voicemail_description' ], $template [ 'template_subject' ]);
$template [ 'template_subject' ] = str_replace ( '${voicemail_name_formatted}' , $voicemail_name_formatted , $template [ 'template_subject' ]);
$template [ 'template_subject' ] = str_replace ( '${domain_name}' , $message [ 'domain_name' ], $template [ 'template_subject' ]);
}
else {
$template [ 'template_subject' ] = $text [ 'label-voicemail_from' ] . ' ' . $message [ 'caller_id_name' ] . ' <' . $message [ 'caller_id_number' ] . '> 0' . gmdate ( " G:i:s " , ( $message [ 'message_length' ] ? ? 0 ));
}
//encode subject
$template [ 'template_subject' ] = trim ( iconv_mime_encode ( null , $template [ 'template_subject' ], [ 'scheme' => 'B' , 'output-charset' => 'utf-8' , 'line-break-chars' => " \n " ]), ': ' );
//determine voicemail message file path and type
$voicemail_message_path = $switch_voicemail . '/default/' . $message [ 'domain_name' ] . '/' . $message [ 'voicemail_id' ];
if (
! empty ( $message [ 'message_base64' ]) &&
! file_exists ( $voicemail_message_path . '/msg_' . $message [ 'voicemail_message_uuid' ] . '.wav' ) &&
! file_exists ( $voicemail_message_path . '/msg_' . $message [ 'voicemail_message_uuid' ] . '.mp3' )
) {
$voicemail_message_decoded = base64_decode ( $message [ 'message_base64' ]);
file_put_contents ( $voicemail_message_path . '/msg_' . $message [ 'voicemail_message_uuid' ] . '.ext' , $voicemail_message_decoded );
$finfo = finfo_open ( FILEINFO_MIME_TYPE );
$voicemail_message_file_mime = finfo_file ( $finfo , $voicemail_message_path . '/msg_' . $message [ 'voicemail_message_uuid' ] . '.ext' );
finfo_close ( $finfo );
unset ( $voicemail_message_decoded );
switch ( $voicemail_message_file_mime ) {
case 'audio/x-wav' :
case 'audio/wav' :
$voicemail_message_file_ext = 'wav' ;
break ;
case 'audio/mpeg' :
case 'audio/mp3' :
$voicemail_message_file_ext = 'mp3' ;
break ;
}
rename ( $voicemail_message_path . '/msg_' . $message [ 'voicemail_message_uuid' ] . '.ext' , $voicemail_message_path . '/msg_' . $message [ 'voicemail_message_uuid' ] . '.' . $voicemail_message_file_ext );
$voicemail_message_file = 'msg_' . $message [ 'voicemail_message_uuid' ] . '.' . $voicemail_message_file_ext ;
}
else {
if ( file_exists ( $voicemail_message_path . '/msg_' . $message [ 'voicemail_message_uuid' ] . '.wav' )) { $voicemail_message_file_ext = 'wav' ; }
if ( file_exists ( $voicemail_message_path . '/msg_' . $message [ 'voicemail_message_uuid' ] . '.mp3' )) { $voicemail_message_file_ext = 'mp3' ; }
$voicemail_message_file = 'msg_' . $message [ 'voicemail_message_uuid' ] . '.' . $voicemail_message_file_ext ;
$voicemail_message_file_mime = mime_content_type ( $voicemail_message_path . '/msg_' . $message [ 'voicemail_message_uuid' ] . '.' . $voicemail_message_file_ext );
}
2024-08-01 18:27:15 +02:00
//determine voicemail intro file path
if (
! empty ( $message [ 'message_intro_base64' ]) &&
! file_exists ( $voicemail_message_path . '/intro_' . $message [ 'voicemail_message_uuid' ] . '.wav' ) &&
! file_exists ( $voicemail_message_path . '/intro_' . $message [ 'voicemail_message_uuid' ] . '.mp3' )
) {
$voicemail_intro_decoded = base64_decode ( $message [ 'message_intro_base64' ]);
file_put_contents ( $voicemail_message_path . '/intro_' . $message [ 'voicemail_message_uuid' ] . '.' . $voicemail_message_file_ext , $voicemail_intro_decoded );
$voicemail_intro_file = 'intro_' . $message [ 'voicemail_message_uuid' ] . '.' . $voicemail_message_file_ext ;
}
else {
$voicemail_intro_file = 'intro_' . $message [ 'voicemail_message_uuid' ] . '.' . $voicemail_message_file_ext ;
}
//combine voicemail intro and message files
$sox = system ( 'which sox' );
if ( file_exists ( $voicemail_message_path . '/' . $voicemail_intro_file ) && ! empty ( $sox )) {
$voicemail_combined_file = 'intro_msg_' . $message [ 'voicemail_message_uuid' ] . '.' . $voicemail_message_file_ext ;
exec ( $sox . ' ' . $voicemail_message_path . '/' . $voicemail_intro_file . ' ' . $voicemail_message_path . '/' . $voicemail_message_file . ' ' . $voicemail_message_path . '/' . $voicemail_combined_file );
if ( file_exists ( $voicemail_message_path . '/' . $voicemail_combined_file )) {
$message [ 'message_combined_base64' ] = base64_encode ( file_get_contents ( $voicemail_message_path . '/' . $voicemail_combined_file ));
}
}
2024-07-11 00:28:41 +02:00
//replace body variables
if ( ! empty ( $template [ 'template_body' ])) {
$template [ 'template_body' ] = str_replace ( '${caller_id_name}' , $message [ 'caller_id_name' ], $template [ 'template_body' ]);
$template [ 'template_body' ] = str_replace ( '${caller_id_number}' , $message [ 'caller_id_number' ], $template [ 'template_body' ]);
$template [ 'template_body' ] = str_replace ( '${message_date}' , $message [ 'message_date' ], $template [ 'template_body' ]);
$template [ 'template_body' ] = str_replace ( '${message_text}' , $message [ 'message_transcription' ], $template [ 'template_body' ]);
$template [ 'template_body' ] = str_replace ( '${message_duration}' , '0' . gmdate ( " G:i:s " , ( $message [ 'message_length' ] ? ? 0 )), $template [ 'template_body' ]);
$template [ 'template_body' ] = str_replace ( '${account}' , $voicemail_name_formatted , $template [ 'template_body' ]);
$template [ 'template_body' ] = str_replace ( '${voicemail_id}' , $message [ 'voicemail_id' ], $template [ 'template_body' ]);
$template [ 'template_body' ] = str_replace ( '${voicemail_description}' , $message [ 'voicemail_description' ], $template [ 'template_body' ]);
$template [ 'template_body' ] = str_replace ( '${voicemail_name_formatted}' , $voicemail_name_formatted , $template [ 'template_body' ]);
$template [ 'template_body' ] = str_replace ( '${domain_name}' , $message [ 'domain_name' ], $template [ 'template_body' ]);
$template [ 'template_body' ] = str_replace ( '${sip_to_user}' , $message [ 'voicemail_id' ], $template [ 'template_body' ]);
$template [ 'template_body' ] = str_replace ( '${dialed_user}' , $message [ 'voicemail_id' ], $template [ 'template_body' ]);
if ( ! empty ( $message [ 'voicemail_file' ])) {
2024-08-01 18:27:15 +02:00
if ( $message [ 'voicemail_file' ] == 'attach' && ( file_exists ( $voicemail_message_path . '/' . $voicemail_combined_file ) || file_exists ( $voicemail_message_path . '/' . $voicemail_message_file ))) {
2024-07-11 00:28:41 +02:00
$template [ 'template_body' ] = str_replace ( '${message}' , $text [ 'label-attached' ], $template [ 'template_body' ]);
}
else if ( $message [ 'voicemail_file' ] == 'link' ) {
$template [ 'template_body' ] = str_replace ( '${message}' , " <a href='https:// " . $message [ 'domain_name' ] . PROJECT_PATH . '/app/voicemails/voicemail_messages.php?action=download&id=' . $message [ 'voicemail_id' ] . '&voicemail_uuid=' . $message [ 'voicemail_uuid' ] . '&uuid=' . $message [ 'voicemail_message_uuid' ] . " &t=bin'> " . $text [ 'label-download' ] . " </a> " , $template [ 'template_body' ]);
}
else { // listen
$template [ 'template_body' ] = str_replace ( '${message}' , " <a href='https:// " . $message [ 'domain_name' ] . PROJECT_PATH . '/app/voicemails/voicemail_messages.php?action=autoplay&id=' . $message [ 'voicemail_uuid' ] . '&uuid=' . $message [ 'voicemail_message_uuid' ] . '&vm=' . $message [ 'voicemail_id' ] . " '> " . $text [ 'label-listen' ] . " </a> " , $template [ 'template_body' ]);
}
}
}
else {
$template [ 'template_body' ] = " <html> \n <body> \n " ;
if ( ! empty ( $message [ 'caller_id_name' ]) && $message [ 'caller_id_name' ] != $message [ 'caller_id_number' ]) {
$template [ 'template_body' ] .= $message [ 'caller_id_name' ] . " <br> \n " ;
}
$template [ 'template_body' ] .= $message [ 'caller_id_number' ] . " <br> \n " ;
$template [ 'template_body' ] .= $message [ 'message_date' ] . " <br> \n " ;
if ( ! empty ( $message [ 'voicemail_file' ])) {
2024-08-01 18:27:15 +02:00
if ( $message [ 'voicemail_file' ] == 'attach' && ( file_exists ( $voicemail_message_path . '/' . $voicemail_combined_file ) || file_exists ( $voicemail_message_path . '/' . $voicemail_message_file ))) {
2024-07-11 00:28:41 +02:00
$template [ 'template_body' ] .= " <br> \n " . $text [ 'label-attached' ];
}
else if ( $message [ 'voicemail_file' ] == 'link' ) {
$template [ 'template_body' ] .= " <br> \n <a href='https:// " . $message [ 'domain_name' ] . PROJECT_PATH . '/app/voicemails/voicemail_messages.php?action=download&id=' . $message [ 'voicemail_id' ] . '&voicemail_uuid=' . $message [ 'voicemail_uuid' ] . '&uuid=' . $message [ 'voicemail_message_uuid' ] . " &t=bin'> " . $text [ 'label-download' ] . '</a>' ;
}
else { // listen
$template [ 'template_body' ] .= " <br> \n <a href='https:// " . $message [ 'domain_name' ] . PROJECT_PATH . '/app/voicemails/voicemail_messages.php?action=autoplay&id=' . $message [ 'voicemail_uuid' ] . '&uuid=' . $message [ 'voicemail_message_uuid' ] . '&vm=' . $message [ 'voicemail_id' ] . " '> " . $text [ 'label-listen' ] . '</a>' ;
}
}
$template [ 'template_body' ] .= " \n </body> \n </html> " ;
}
//build message status update array
$array [ 'email_queue' ][ 0 ][ 'email_queue_uuid' ] = $email_queue_uuid = uuid ();
$array [ 'email_queue' ][ 0 ][ 'domain_uuid' ] = $this -> domain_uuid ;
$array [ 'email_queue' ][ 0 ][ 'hostname' ] = gethostname ();
$array [ 'email_queue' ][ 0 ][ 'email_date' ] = 'now()' ;
$array [ 'email_queue' ][ 0 ][ 'email_from' ] = $email_from_name . '<' . $email_from . '>' ;
$array [ 'email_queue' ][ 0 ][ 'email_to' ] = $message [ 'voicemail_mail_to' ];
$array [ 'email_queue' ][ 0 ][ 'email_subject' ] = $template [ 'template_subject' ];
$array [ 'email_queue' ][ 0 ][ 'email_body' ] = $template [ 'template_body' ];
$array [ 'email_queue' ][ 0 ][ 'email_status' ] = 'waiting' ;
$array [ 'email_queue' ][ 0 ][ 'email_uuid' ] = $this -> voicemail_message_uuid ;
$array [ 'email_queue' ][ 0 ][ 'email_transcription' ] = $message [ 'message_transcription' ];
$array [ 'email_queue' ][ 0 ][ 'insert_date' ] = 'now()' ;
2024-07-16 18:05:06 +02:00
$array [ 'email_queue' ][ 0 ][ 'insert_user' ] = $this -> user_uuid ;
2024-07-11 00:28:41 +02:00
//add voicemail file details (and/or base64) to queue attachments
2024-08-01 18:27:15 +02:00
if ( ! empty ( $message [ 'voicemail_file' ]) && $message [ 'voicemail_file' ] == 'attach' && ( file_exists ( $voicemail_message_path . '/' . $voicemail_combined_file ) || file_exists ( $voicemail_message_path . '/' . $voicemail_message_file ))) {
2024-07-11 00:28:41 +02:00
$array [ 'email_queue_attachments' ][ 0 ][ 'email_queue_attachment_uuid' ] = uuid ();
$array [ 'email_queue_attachments' ][ 0 ][ 'domain_uuid' ] = $this -> domain_uuid ;
$array [ 'email_queue_attachments' ][ 0 ][ 'email_queue_uuid' ] = $email_queue_uuid ;
$array [ 'email_queue_attachments' ][ 0 ][ 'email_attachment_type' ] = $voicemail_message_file_ext ;
$array [ 'email_queue_attachments' ][ 0 ][ 'email_attachment_path' ] = $voicemail_message_path ;
2024-08-01 18:27:15 +02:00
$array [ 'email_queue_attachments' ][ 0 ][ 'email_attachment_name' ] = $voicemail_combined_file ? ? $voicemail_message_file ;
$array [ 'email_queue_attachments' ][ 0 ][ 'email_attachment_base64' ] = $message [ 'message_combined_base64' ] ? ? $message [ 'message_base64' ];
$array [ 'email_queue_attachments' ][ 0 ][ 'email_attachment_cid' ] = ! empty ( $message [ 'message_combined_base64' ]) || ! empty ( $message [ 'message_base64' ]) ? uuid () : null ;
2024-07-11 00:28:41 +02:00
$array [ 'email_queue_attachments' ][ 0 ][ 'email_attachment_mime_type' ] = $voicemail_message_file_mime ;
$array [ 'email_queue_attachments' ][ 0 ][ 'insert_date' ] = 'now()' ;
2024-07-16 18:05:06 +02:00
$array [ 'email_queue_attachments' ][ 0 ][ 'insert_user' ] = $this -> user_uuid ;
2024-07-11 00:28:41 +02:00
}
//grant temporary permissions
2024-11-29 21:57:01 +01:00
$p = permissions :: new ();
2024-07-11 00:28:41 +02:00
$p -> add ( 'email_queue_add' , 'temp' );
$p -> add ( 'email_queue_attachment_add' , 'temp' );
//execute update
2024-07-16 18:05:06 +02:00
$this -> database -> app_name = $this -> app_name ;
$this -> database -> app_name = $this -> app_uuid ;
$this -> database -> save ( $array );
2024-07-11 00:28:41 +02:00
unset ( $array );
//revoke temporary permissions
$p -> delete ( 'email_queue_add' , 'temp' );
$p -> delete ( 'email_queue_attachment_add' , 'temp' );
//remove temp file from base64 output
if ( ! empty ( $message [ 'message_base64' ]) && file_exists ( $voicemail_message_path . '/' . $voicemail_message_file )) {
@ unlink ( $voicemail_message_path . '/' . $voicemail_message_file );
2024-08-01 18:27:15 +02:00
@ unlink ( $voicemail_message_path . '/' . $voicemail_intro_file );
@ unlink ( $voicemail_message_path . '/' . $voicemail_combined_file );
2024-07-11 00:28:41 +02:00
}
}
2024-05-28 21:08:41 +02:00
public function message_transcribe () {
//get the voicemail id
2024-07-11 00:28:41 +02:00
$this -> get_voicemail_id ();
2024-05-28 21:08:41 +02:00
//check if for valid input
2024-07-11 00:28:41 +02:00
if ( ! is_numeric ( $this -> voicemail_id )
|| ! is_uuid ( $this -> voicemail_uuid )
|| ! is_uuid ( $this -> domain_uuid )
|| ! is_uuid ( $this -> voicemail_message_uuid )
) {
return false ;
}
2024-05-28 21:08:41 +02:00
//add the settings object
2024-07-16 18:05:06 +02:00
$settings = new settings ([ " domain_uuid " => $this -> domain_uuid , " user_uuid " => $this -> user_uuid ]);
2024-10-24 22:37:26 +02:00
$transcribe_enabled = $settings -> get ( 'transcribe' , 'enabled' , false );
2024-05-28 21:08:41 +02:00
$transcribe_engine = $settings -> get ( 'transcribe' , 'engine' , '' );
2024-07-11 00:28:41 +02:00
$switch_voicemail = $settings -> get ( 'switch' , 'voicemail' , '/var/lib/freeswitch/storage/voicemail' );
2024-05-28 21:08:41 +02:00
//transcribe multiple recordings
2024-10-24 22:37:26 +02:00
if ( $transcribe_enabled && ! empty ( $transcribe_engine )) {
2024-05-28 21:08:41 +02:00
2024-07-11 00:28:41 +02:00
//get voicemail message base64
$sql = " select message_base64 from v_voicemail_messages where voicemail_message_uuid = :voicemail_message_uuid " ;
$parameters [ 'voicemail_message_uuid' ] = $this -> voicemail_message_uuid ;
2024-07-16 18:05:06 +02:00
$voicemail_message_base64 = $this -> database -> select ( $sql , $parameters , 'column' );
2024-07-11 00:28:41 +02:00
unset ( $sql , $parameters );
//define voicemail message file path
$voicemail_message_path = $switch_voicemail . '/default/' . $_SESSION [ 'domain_name' ] . '/' . $this -> voicemail_id ;
//determine voicemail message file properties (decode if base64)
if (
! empty ( $voicemail_message_base64 ) &&
! file_exists ( $voicemail_message_path . '/msg_' . $this -> voicemail_message_uuid . '.wav' ) &&
! file_exists ( $voicemail_message_path . '/msg_' . $this -> voicemail_message_uuid . '.mp3' )
) {
$voicemail_message_decoded = base64_decode ( $voicemail_message_base64 );
file_put_contents ( $voicemail_message_path . '/msg_' . $this -> voicemail_message_uuid . '.ext' , $voicemail_message_decoded );
$finfo = finfo_open ( FILEINFO_MIME_TYPE );
$voicemail_message_file_mime = finfo_file ( $finfo , $voicemail_message_path . '/msg_' . $this -> voicemail_message_uuid . '.ext' );
finfo_close ( $finfo );
switch ( $voicemail_message_file_mime ) {
case 'audio/x-wav' :
case 'audio/wav' :
$voicemail_message_file_ext = 'wav' ;
break ;
case 'audio/mpeg' :
case 'audio/mp3' :
$voicemail_message_file_ext = 'mp3' ;
break ;
}
unset ( $voicemail_message_decoded , $voicemail_message_file_mime );
rename ( $voicemail_message_path . '/msg_' . $this -> voicemail_message_uuid . '.ext' , $voicemail_message_path . '/msg_' . $this -> voicemail_message_uuid . '.' . $voicemail_message_file_ext );
$voicemail_message_file = 'msg_' . $this -> voicemail_message_uuid . '.' . $voicemail_message_file_ext ;
}
else {
if ( file_exists ( $voicemail_message_path . '/msg_' . $this -> voicemail_message_uuid . '.wav' )) { $voicemail_message_file_ext = 'wav' ; }
if ( file_exists ( $voicemail_message_path . '/msg_' . $this -> voicemail_message_uuid . '.mp3' )) { $voicemail_message_file_ext = 'mp3' ; }
$voicemail_message_file = 'msg_' . $this -> voicemail_message_uuid . '.' . $voicemail_message_file_ext ;
}
unset ( $voicemail_message_file_ext );
2024-05-28 21:08:41 +02:00
//add the transcribe object
$transcribe = new transcribe ( $settings );
2024-07-11 00:28:41 +02:00
//transcribe the voicemail message file
$transcribe -> audio_path = $voicemail_message_path ;
$transcribe -> audio_filename = basename ( $voicemail_message_file );
$message_transcription = $transcribe -> transcribe ();
//build voicemail message data array
if ( ! empty ( $message_transcription )) {
$array [ 'voicemail_messages' ][ 0 ][ 'voicemail_message_uuid' ] = $this -> voicemail_message_uuid ;
$array [ 'voicemail_messages' ][ 0 ][ 'message_transcription' ] = $message_transcription ;
2024-05-28 21:08:41 +02:00
}
//update the checked rows
if ( is_array ( $array ) && @ sizeof ( $array ) != 0 ) {
//grant temporary permissions
2024-11-29 21:57:01 +01:00
$p = permissions :: new ();
2024-05-28 21:08:41 +02:00
$p -> add ( 'voicemail_message_edit' , 'temp' );
//execute update
2024-07-16 18:05:06 +02:00
$this -> database -> app_name = $this -> app_name ;
$this -> database -> app_name = $this -> app_uuid ;
$this -> database -> save ( $array );
2024-05-28 21:08:41 +02:00
unset ( $array );
//revoke temporary permissions
$p -> delete ( 'voicemail_message_edit' , 'temp' );
}
2024-07-11 00:28:41 +02:00
//remove temp file from base64 output
if ( ! empty ( $voicemail_message_base64 ) && file_exists ( $voicemail_message_path . '/' . $voicemail_message_file )) {
@ unlink ( $voicemail_message_path . '/' . $voicemail_message_file );
}
2024-05-28 21:08:41 +02:00
return ! empty ( $message_transcription ) ? true : false ;
}
}
2013-01-13 07:47:28 +01:00
public function message_saved () {
2016-07-22 18:31:08 +02:00
2016-12-03 20:02:14 +01:00
//check if for valid input
2019-09-02 23:57:18 +02:00
if ( ! is_uuid ( $this -> voicemail_uuid )
|| ! is_uuid ( $this -> domain_uuid )
|| ! is_uuid ( $this -> voicemail_message_uuid )
) {
2016-12-03 20:02:14 +01:00
return false ;
}
2019-09-02 23:57:18 +02:00
//build message status update array
$array [ 'voicemail_messages' ][ 0 ][ 'voicemail_message_uuid' ] = $this -> voicemail_message_uuid ;
$array [ 'voicemail_messages' ][ 0 ][ 'message_status' ] = 'saved' ;
//grant temporary permissions
2024-11-29 21:57:01 +01:00
$p = permissions :: new ();
2019-09-02 23:57:18 +02:00
$p -> add ( 'voicemail_message_edit' , 'temp' );
//execute update
2024-07-16 18:05:06 +02:00
$this -> database -> app_name = $this -> app_name ;
$this -> database -> app_name = $this -> app_uuid ;
$this -> database -> save ( $array );
2019-09-02 23:57:18 +02:00
unset ( $array );
//revoke temporary permissions
$p -> delete ( 'voicemail_message_edit' , 'temp' );
2016-03-19 20:54:24 +01:00
2013-01-13 07:47:28 +01:00
//check the message waiting status
$this -> message_waiting ();
}
2024-08-01 18:27:15 +02:00
/**
* download the voicemail message intro
* @ param string domain_name if domain name is not passed , then will be used from the session variable ( if available ) to generate the voicemail file path
*/
public function message_intro_download ( string $domain_name = '' ) {
//check domain name
if ( empty ( $domain_name )) {
$domain_name = $_SESSION [ 'domain_name' ] ? ? '' ;
}
//check if for valid input
if ( ! is_numeric ( $this -> voicemail_id )
|| ! is_uuid ( $this -> voicemail_uuid )
|| ! is_uuid ( $this -> domain_uuid )
|| ! is_uuid ( $this -> voicemail_message_uuid )
) {
return false ;
}
//change the message status
$this -> message_saved ();
//set source folder path
$path = realpath ( $this -> settings -> get ( 'switch' , 'voicemail' , '/var/lib/freeswitch/storage/voicemail' ) . '/default/' . $domain_name ) . '/' . $this -> voicemail_id ;
//prepare base64 content from db, if enabled
if ( $this -> settings -> get ( 'voicemail' , 'storage_type' , '' ) == 'base64' ) {
$sql = " select message_intro_base64 " ;
$sql .= " from " ;
$sql .= " v_voicemail_messages as m, " ;
$sql .= " v_voicemails as v " ;
$sql .= " where " ;
$sql .= " m.voicemail_uuid = v.voicemail_uuid " ;
$sql .= " and v.voicemail_id = :voicemail_id " ;
$sql .= " and m.voicemail_uuid = :voicemail_uuid " ;
$sql .= " and m.domain_uuid = :domain_uuid " ;
$sql .= " and m.voicemail_message_uuid = :voicemail_message_uuid " ;
$parameters [ 'voicemail_id' ] = $this -> voicemail_id ;
$parameters [ 'voicemail_uuid' ] = $this -> voicemail_uuid ;
$parameters [ 'domain_uuid' ] = $this -> domain_uuid ;
$parameters [ 'voicemail_message_uuid' ] = $this -> voicemail_message_uuid ;
$message_intro_base64 = $this -> database -> select ( $sql , $parameters , 'column' );
if ( $message_intro_base64 != '' ) {
$message_intro_decoded = base64_decode ( $message_intro_base64 );
file_put_contents ( $path . '/intro_' . $this -> voicemail_message_uuid . '.ext' , $message_intro_decoded );
$finfo = finfo_open ( FILEINFO_MIME_TYPE ); //determine mime type (requires PHP >= 5.3.0, must be manually enabled on Windows)
$file_mime = finfo_file ( $finfo , $path . '/intro_' . $this -> voicemail_message_uuid . '.ext' );
finfo_close ( $finfo );
switch ( $file_mime ) {
case 'audio/x-wav' :
case 'audio/wav' :
$file_ext = 'wav' ;
break ;
case 'audio/mpeg' :
case 'audio/mp3' :
$file_ext = 'mp3' ;
break ;
}
rename ( $path . '/intro_' . $this -> voicemail_message_uuid . '.ext' , $path . '/intro_' . $this -> voicemail_message_uuid . '.' . $file_ext );
}
unset ( $sql , $parameters , $message_intro_base64 , $message_intro_decoded );
}
//prepare and stream the file
if ( file_exists ( $path . '/intro_' . $this -> voicemail_message_uuid . '.wav' )) {
$file_path = $path . '/intro_' . $this -> voicemail_message_uuid . '.wav' ;
}
else if ( file_exists ( $path . '/intro_' . $this -> voicemail_message_uuid . '.mp3' )) {
$file_path = $path . '/intro_' . $this -> voicemail_message_uuid . '.mp3' ;
}
else {
return false ;
}
if ( empty ( $file_path )) {
return false ;
}
$fd = fopen ( $file_path , " rb " );
if ( $this -> type == 'bin' ) {
header ( " Content-Type: application/force-download " );
header ( " Content-Type: application/octet-stream " );
header ( " Content-Type: application/download " );
header ( " Content-Description: File Transfer " );
$file_ext = pathinfo ( $file_path , PATHINFO_EXTENSION );
switch ( $file_ext ) {
case " wav " : header ( 'Content-Disposition: attachment; filename="intro_' . $this -> voicemail_message_uuid . '.wav"' ); break ;
case " mp3 " : header ( 'Content-Disposition: attachment; filename="intro_' . $this -> voicemail_message_uuid . '.mp3"' ); break ;
case " ogg " : header ( 'Content-Disposition: attachment; filename="intro_' . $this -> voicemail_message_uuid . '.ogg"' ); break ;
}
}
else {
$file_ext = pathinfo ( $file_path , PATHINFO_EXTENSION );
switch ( $file_ext ) {
case " wav " : header ( " Content-Type: audio/x-wav " ); break ;
case " mp3 " : header ( " Content-Type: audio/mpeg " ); break ;
case " ogg " : header ( " Content-Type: audio/ogg " ); break ;
}
}
header ( " Cache-Control: no-cache, must-revalidate " ); // HTTP/1.1
header ( " Expires: Sat, 26 Jul 1997 05:00:00 GMT " ); // date in the past
if ( $this -> type == 'bin' ) {
header ( " Content-Length: " . filesize ( $file_path ));
}
ob_end_clean ();
//content-range
if ( isset ( $_SERVER [ 'HTTP_RANGE' ]) && $this -> type != 'bin' ) {
$this -> range_download ( $file_path );
}
fpassthru ( $fd );
//if base64, remove temp file
if ( $this -> settings -> get ( 'voicemail' , 'storage_type' , '' ) == 'base64' ) {
@ unlink ( $path . '/intro_' . $this -> voicemail_message_uuid . '.' . $file_ext );
}
}
2024-03-16 20:46:38 +01:00
/**
* download the voicemail message
* @ param string domain_name if domain name is not passed , then will be used from the session variable ( if available ) to generate the voicemail file path
*/
public function message_download ( string $domain_name = '' ) {
//check domain name
if ( empty ( $domain_name )) {
$domain_name = $_SESSION [ 'domain_name' ] ? ? '' ;
}
2013-01-13 07:47:28 +01:00
2016-12-03 20:02:14 +01:00
//check if for valid input
2023-03-30 01:46:51 +02:00
if ( ! is_numeric ( $this -> voicemail_id )
|| ! is_uuid ( $this -> voicemail_uuid )
|| ! is_uuid ( $this -> domain_uuid )
|| ! is_uuid ( $this -> voicemail_message_uuid )
) {
return false ;
}
2016-12-03 20:02:14 +01:00
2013-01-13 07:47:28 +01:00
//change the message status
2023-03-30 01:46:51 +02:00
$this -> message_saved ();
2013-01-13 07:47:28 +01:00
2015-04-23 02:10:31 +02:00
//set source folder path
2024-03-16 20:46:38 +01:00
$path = realpath ( $this -> settings -> get ( 'switch' , 'voicemail' , '/var/lib/freeswitch/storage/voicemail' ) . '/default/' . $domain_name ) . '/' . $this -> voicemail_id ;
2015-04-23 02:10:31 +02:00
//prepare base64 content from db, if enabled
2024-03-16 20:46:38 +01:00
if ( $this -> settings -> get ( 'voicemail' , 'storage_type' , '' ) == 'base64' ) {
2023-03-30 01:46:51 +02:00
$sql = " select message_base64 " ;
$sql .= " from " ;
$sql .= " v_voicemail_messages as m, " ;
$sql .= " v_voicemails as v " ;
$sql .= " where " ;
$sql .= " m.voicemail_uuid = v.voicemail_uuid " ;
$sql .= " and v.voicemail_id = :voicemail_id " ;
$sql .= " and m.voicemail_uuid = :voicemail_uuid " ;
$sql .= " and m.domain_uuid = :domain_uuid " ;
$sql .= " and m.voicemail_message_uuid = :voicemail_message_uuid " ;
$parameters [ 'voicemail_id' ] = $this -> voicemail_id ;
$parameters [ 'voicemail_uuid' ] = $this -> voicemail_uuid ;
$parameters [ 'domain_uuid' ] = $this -> domain_uuid ;
$parameters [ 'voicemail_message_uuid' ] = $this -> voicemail_message_uuid ;
2024-07-16 18:05:06 +02:00
$message_base64 = $this -> database -> select ( $sql , $parameters , 'column' );
2023-03-30 01:46:51 +02:00
if ( $message_base64 != '' ) {
$message_decoded = base64_decode ( $message_base64 );
file_put_contents ( $path . '/msg_' . $this -> voicemail_message_uuid . '.ext' , $message_decoded );
$finfo = finfo_open ( FILEINFO_MIME_TYPE ); //determine mime type (requires PHP >= 5.3.0, must be manually enabled on Windows)
$file_mime = finfo_file ( $finfo , $path . '/msg_' . $this -> voicemail_message_uuid . '.ext' );
finfo_close ( $finfo );
switch ( $file_mime ) {
case 'audio/x-wav' :
case 'audio/wav' :
$file_ext = 'wav' ;
break ;
case 'audio/mpeg' :
case 'audio/mp3' :
$file_ext = 'mp3' ;
break ;
2015-04-23 02:10:31 +02:00
}
2023-03-30 01:46:51 +02:00
rename ( $path . '/msg_' . $this -> voicemail_message_uuid . '.ext' , $path . '/msg_' . $this -> voicemail_message_uuid . '.' . $file_ext );
2015-04-23 02:10:31 +02:00
}
2023-03-30 01:46:51 +02:00
unset ( $sql , $parameters , $message_base64 , $message_decoded );
}
2015-04-23 02:10:31 +02:00
//prepare and stream the file
2023-03-30 01:46:51 +02:00
if ( file_exists ( $path . '/msg_' . $this -> voicemail_message_uuid . '.wav' )) {
$file_path = $path . '/msg_' . $this -> voicemail_message_uuid . '.wav' ;
2024-03-16 20:46:38 +01:00
}
else if ( file_exists ( $path . '/msg_' . $this -> voicemail_message_uuid . '.mp3' )) {
2023-03-30 01:46:51 +02:00
$file_path = $path . '/msg_' . $this -> voicemail_message_uuid . '.mp3' ;
2024-03-16 20:46:38 +01:00
}
else {
2023-03-30 01:46:51 +02:00
return false ;
}
2020-03-31 04:08:45 +02:00
2024-03-16 20:46:38 +01:00
if ( empty ( $file_path )) {
2023-03-30 01:46:51 +02:00
return false ;
}
$fd = fopen ( $file_path , " rb " );
if ( $this -> type == 'bin' ) {
header ( " Content-Type: application/force-download " );
header ( " Content-Type: application/octet-stream " );
header ( " Content-Type: application/download " );
header ( " Content-Description: File Transfer " );
$file_ext = pathinfo ( $file_path , PATHINFO_EXTENSION );
switch ( $file_ext ) {
2024-03-16 20:46:38 +01:00
case " wav " : header ( 'Content-Disposition: attachment; filename="msg_' . $this -> voicemail_message_uuid . '.wav"' ); break ;
case " mp3 " : header ( 'Content-Disposition: attachment; filename="msg_' . $this -> voicemail_message_uuid . '.mp3"' ); break ;
case " ogg " : header ( 'Content-Disposition: attachment; filename="msg_' . $this -> voicemail_message_uuid . '.ogg"' ); break ;
2023-03-30 01:46:51 +02:00
}
2024-03-16 20:46:38 +01:00
}
else {
2023-03-30 01:46:51 +02:00
$file_ext = pathinfo ( $file_path , PATHINFO_EXTENSION );
switch ( $file_ext ) {
2024-03-16 20:46:38 +01:00
case " wav " : header ( " Content-Type: audio/x-wav " ); break ;
case " mp3 " : header ( " Content-Type: audio/mpeg " ); break ;
case " ogg " : header ( " Content-Type: audio/ogg " ); break ;
2013-01-02 00:22:07 +01:00
}
2023-03-30 01:46:51 +02:00
}
header ( " Cache-Control: no-cache, must-revalidate " ); // HTTP/1.1
header ( " Expires: Sat, 26 Jul 1997 05:00:00 GMT " ); // date in the past
if ( $this -> type == 'bin' ) {
header ( " Content-Length: " . filesize ( $file_path ));
}
ob_end_clean ();
2023-06-09 18:51:36 +02:00
//content-range
if ( isset ( $_SERVER [ 'HTTP_RANGE' ]) && $this -> type != 'bin' ) {
$this -> range_download ( $file_path );
}
2023-03-30 01:46:51 +02:00
fpassthru ( $fd );
2015-04-23 02:10:31 +02:00
//if base64, remove temp file
2024-03-16 20:46:38 +01:00
if ( $this -> settings -> get ( 'voicemail' , 'storage_type' , '' ) == 'base64' ) {
2023-03-30 01:46:51 +02:00
@ unlink ( $path . '/msg_' . $this -> voicemail_message_uuid . '.' . $file_ext );
}
2015-04-23 02:10:31 +02:00
2019-09-02 23:57:18 +02:00
}
2020-03-31 04:08:45 +02:00
/*
* range download method ( helps safari play audio sources )
*/
private function range_download ( $file ) {
2023-12-03 01:16:18 +01:00
$esl = @ fopen ( $file , 'rb' );
2020-03-31 04:08:45 +02:00
$size = filesize ( $file ); // File size
$length = $size ; // Content length
$start = 0 ; // Start byte
$end = $size - 1 ; // End byte
// Now that we've gotten so far without errors we send the accept range header
/* At the moment we only support single ranges .
* Multiple ranges requires some more work to ensure it works correctly
* and comply with the spesifications : http :// www . w3 . org / Protocols / rfc2616 / rfc2616 - sec19 . html #sec19.2
*
* Multirange support annouces itself with :
* header ( 'Accept-Ranges: bytes' );
*
* Multirange content must be sent with multipart / byteranges mediatype ,
* ( mediatype = mimetype )
* as well as a boundry header to indicate the various chunks of data .
*/
header ( " Accept-Ranges: 0- $length " );
// header('Accept-Ranges: bytes');
// multipart/byteranges
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2
if ( isset ( $_SERVER [ 'HTTP_RANGE' ])) {
$c_start = $start ;
$c_end = $end ;
// Extract the range string
list (, $range ) = explode ( '=' , $_SERVER [ 'HTTP_RANGE' ], 2 );
// Make sure the client hasn't sent us a multibyte range
if ( strpos ( $range , ',' ) !== false ) {
// (?) Shoud this be issued here, or should the first
// range be used? Or should the header be ignored and
// we output the whole content?
header ( 'HTTP/1.1 416 Requested Range Not Satisfiable' );
header ( " Content-Range: bytes $start - $end / $size " );
// (?) Echo some info to the client?
exit ;
}
// If the range starts with an '-' we start from the beginning
// If not, we forward the file pointer
// And make sure to get the end byte if spesified
2023-06-09 18:51:36 +02:00
if ( ! empty ( $range0 ) && $range0 == '-' ) {
2020-03-31 04:08:45 +02:00
// The n-number of the last bytes is requested
$c_start = $size - substr ( $range , 1 );
}
else {
$range = explode ( '-' , $range );
$c_start = $range [ 0 ];
$c_end = ( isset ( $range [ 1 ]) && is_numeric ( $range [ 1 ])) ? $range [ 1 ] : $size ;
}
/* Check the range and make sure it ' s treated according to the specs .
* http :// www . w3 . org / Protocols / rfc2616 / rfc2616 - sec14 . html
*/
// End bytes can not be larger than $end.
$c_end = ( $c_end > $end ) ? $end : $c_end ;
// Validate the requested range and return an error if it's not correct.
if ( $c_start > $c_end || $c_start > $size - 1 || $c_end >= $size ) {
header ( 'HTTP/1.1 416 Requested Range Not Satisfiable' );
header ( " Content-Range: bytes $start - $end / $size " );
// (?) Echo some info to the client?
exit ;
}
$start = $c_start ;
$end = $c_end ;
$length = $end - $start + 1 ; // Calculate new content length
2023-12-03 01:16:18 +01:00
fseek ( $esl , $start );
2020-03-31 04:08:45 +02:00
header ( 'HTTP/1.1 206 Partial Content' );
}
// Notify the client the byte range we'll be outputting
header ( " Content-Range: bytes $start - $end / $size " );
header ( " Content-Length: $length " );
// Start buffered download
$buffer = 1024 * 8 ;
2023-12-03 01:16:18 +01:00
while ( ! feof ( $esl ) && ( $p = ftell ( $esl )) <= $end ) {
2020-03-31 04:08:45 +02:00
if ( $p + $buffer > $end ) {
// In case we're only outputtin a chunk, make sure we don't
// read past the length
$buffer = $end - $p + 1 ;
}
set_time_limit ( 0 ); // Reset time limit for big files
2023-12-03 01:16:18 +01:00
echo fread ( $esl , $buffer );
2020-03-31 04:08:45 +02:00
flush (); // Free up memory. Otherwise large files will trigger PHP's memory limit.
}
}
2024-06-25 21:07:42 +02:00
/**
* Removes old entries for in the database voicemails table
* see { @ link https :// github . com / fusionpbx / fusionpbx - app - maintenance / } FusionPBX Maintenance App
* @ param settings $settings Settings object
* @ return void
*/
public static function database_maintenance ( settings $settings ) : void {
//set table name for query
//$table = self::TABLE;
$table = 'voicemail_messages' ;
//get a database connection
$database = $settings -> database ();
//get a list of domains
$domains = maintenance :: get_domains ( $database );
foreach ( $domains as $domain_uuid => $domain_name ) {
//get domain settings
$domain_settings = new settings ([ 'database' => $database , 'domain_uuid' => $domain_uuid ]);
//ensure we have a retention day
2024-07-07 04:45:25 +02:00
$retention_days = $domain_settings -> get ( 'voicemail' , maintenance :: DATABASE_SUBCATEGORY , '' );
2024-06-25 21:07:42 +02:00
if ( ! empty ( $retention_days ) && is_numeric ( $retention_days )) {
//clear out old records
$sql = " delete from v_ { $table } WHERE to_timestamp(created_epoch) < NOW() - INTERVAL ' { $retention_days } days' "
. " and domain_uuid = ' { $domain_uuid } ' " ;
$database -> execute ( $sql );
2024-07-24 03:58:13 +02:00
$code = $database -> message [ 'code' ] ? ? 0 ;
if ( $database -> message [ 'code' ] == 200 ) {
maintenance_service :: log_write ( self :: class , " Successfully removed entries older than $retention_days " , $domain_uuid );
2024-06-25 21:07:42 +02:00
} else {
2024-07-24 03:58:13 +02:00
$message = $database -> message [ 'message' ] ? ? " An unknown error has occurred " ;
maintenance_service :: log_write ( self :: class , " Unable to remove old database records. Error message: $message ( $code ) " , $domain_uuid , maintenance_service :: LOG_ERROR );
2024-06-25 21:07:42 +02:00
}
}
}
//ensure logs are saved
maintenance_service :: log_flush ();
}
2024-07-07 04:45:25 +02:00
/**
* Called by the maintenance system to remove old files
* @ param settings $settings Settings object
*/
2024-06-25 21:07:42 +02:00
public static function filesystem_maintenance ( settings $settings ) : void {
//get a list of domains
$domains = maintenance :: get_domains ( $settings -> database ());
2024-07-07 04:45:25 +02:00
//loop through domains to handle domains with different defaults
2024-06-25 21:07:42 +02:00
foreach ( $domains as $domain_uuid => $domain_name ) {
2024-07-07 04:45:25 +02:00
//get settings for this domain
2024-06-25 21:07:42 +02:00
$domain_settings = new settings ([ 'database' => $settings -> database (), 'domain_uuid' => $domain_uuid ]);
2024-07-07 04:45:25 +02:00
//get the switch voicemail location
2024-06-25 21:07:42 +02:00
$voicemail_location = $domain_settings -> get ( 'switch' , 'voicemail' , '/var/lib/freeswitch/storage/voicemail' ) . '/default' ;
2024-07-07 04:45:25 +02:00
//get the filesystem retention days
$retention_days = $domain_settings -> get ( 'voicemail' , maintenance :: FILESYSTEM_SUBCATEGORY , '' );
2024-06-25 21:07:42 +02:00
if ( ! empty ( $retention_days )) {
2024-07-07 04:45:25 +02:00
//get all wav and mp3 voicemail files
2024-06-25 21:07:42 +02:00
$mp3_files = glob ( " $voicemail_location / $domain_name /*/msg_*.mp3 " );
$wav_files = glob ( " $voicemail_location / $domain_name /*/msg_*.wav " );
2024-08-01 18:27:15 +02:00
$mp3_intro_files = glob ( " $voicemail_location / $domain_name /*/intro_*.mp3 " );
$wav_intro_files = glob ( " $voicemail_location / $domain_name /*/intro_*.wav " );
$domain_voicemail_files = array_merge ( $mp3_files , $wav_files , $mp3_intro_files , $wav_intro_files );
2024-07-07 04:45:25 +02:00
//delete individually
2024-06-25 21:07:42 +02:00
foreach ( $domain_voicemail_files as $file ) {
2024-07-07 04:45:25 +02:00
//check modified date on file
2024-06-25 21:07:42 +02:00
if ( maintenance_service :: days_since_modified ( $file ) > $retention_days ) {
2024-07-07 04:45:25 +02:00
//date is older so remove
2024-06-25 21:07:42 +02:00
if ( unlink ( $file )) {
2024-07-07 04:45:25 +02:00
//successfully deleted
2024-06-25 21:07:42 +02:00
maintenance_service :: log_write ( self :: class , " Removed $file from voicemails " , $domain_uuid );
} else {
2024-07-07 04:45:25 +02:00
//failed to delete file
2024-06-25 21:07:42 +02:00
maintenance_service :: log_write ( self :: class , " Unable to remove $file " , $domain_uuid , maintenance_service :: LOG_ERROR );
}
}
}
}
else {
2024-07-07 04:45:25 +02:00
//log retention days not valid
2024-06-25 21:07:42 +02:00
maintenance_service :: log_write ( self :: class , " Retention days not set or not a valid number " , $domain_uuid , maintenance_service :: LOG_ERROR );
}
}
2024-07-07 04:45:25 +02:00
//ensure logs are saved
maintenance_service :: log_flush ();
2024-06-25 21:07:42 +02:00
}
2020-03-31 04:08:45 +02:00
2013-01-02 00:22:07 +01:00
}
//example voicemail messages
//require_once "app/voicemails/resources/classes/voicemail.php";
//$voicemail = new voicemail;
//$voicemail->voicemail_uuid = $voicemail_uuid;
//$voicemail->order_by = $order_by;
//$voicemail->order = $order;
//$result = $voicemail->messages();
//$result_count = count($result);
/*
Array
(
[ user ] => 1002
[ extension_uuid ] => e163fc03 - f180 - 459 b - aa12 - 7 ed87fcb6e2c
[ outbound_caller_id_name ] => FusionPBX
2016-07-22 18:31:08 +02:00
[ outbound_caller_id_number ] => 12089068227
2013-01-02 00:22:07 +01:00
)
Array
(
[ user ] => 1020
[ extension_uuid ] => ecfb23df - 7 c59 - 4286 - 891e-2 abdc48856ac
[ outbound_caller_id_name ] => Mark J Crane
2016-07-22 18:31:08 +02:00
[ outbound_caller_id_number ] => 12089068227
2013-01-02 00:22:07 +01:00
)
foreach ( $_SESSION [ 'user' ][ 'extension' ] as $value ) {
2023-05-05 18:46:37 +02:00
if ( ! empty ( $value [ 'user' ])) {
2013-01-02 00:22:07 +01:00
}
}
*/
2024-06-25 21:07:42 +02:00
?>