forked from m-dark/sip-phone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phonebook_xml.pl
98 lines (94 loc) · 3.82 KB
/
phonebook_xml.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/perl -w
#
# FreePBX Phonebook Creation Script
# (c) Kruk Ivan
# 1. Base FPBX install. Database in use = asterisk
# 2. Localhost requires no authentication
# 3. Standard Yealink XML Phonebook
# 4. Apache webdir = /var/www/html
use 5.010;
use strict;
use warnings;
use POSIX qw(strftime);
use locale;
use Time::Local;
use encoding 'utf-8';
use DBI;
use File::Copy;
my $dir = '/opt/asterisk/script'; #Директория для файла conf_number_line.conf (который содержит информацию о том, за каким номером аккаунта прописан номер телефона).
my $host = ''; #"localhost"; # MySQL-сервер нашего хостинга
my $port = ''; #"3306"; # порт, на который открываем соединение
my $user = ''; #"freepbxuser"; # имя пользователя
my $pass = ''; #пароль /etc/freepbx.conf
my $db = ''; #"asterisk"; # имя базы данных.
#my @invisible = (121,152,521,523,524);
my @invisible = '';
my $invisible_yes = 0;
open (my $freepbx_pass, '<:encoding(UTF-8)', "$dir/freepbx.pass") || die "Error opening file: freepbx.pass $!";
while (defined(my $line_freepbx_pass = <$freepbx_pass>)){
chomp ($line_freepbx_pass);
if ($line_freepbx_pass =~ /^\#/){
next;
}
my @array_freepbx_pass = split (/ = /,$line_freepbx_pass,-1);
given($array_freepbx_pass[0]){
when('host'){
$host = $array_freepbx_pass[1];
}when('port'){
$port = $array_freepbx_pass[1];
}when('user'){
$user = $array_freepbx_pass[1];
}when('pass'){
$pass = $array_freepbx_pass[1];
}when('db'){
$db = $array_freepbx_pass[1];
}when('invisible'){
@invisible = split (/,/,$array_freepbx_pass[1]);
}default{
next;
}
}
}
close($freepbx_pass);
my $dbasterisk = DBI->connect("DBI:mysql:$db:$host:$port",$user,$pass,{ mysql_enable_utf8 => 1 });
my $sth = $dbasterisk->prepare("SELECT name,extension FROM users ORDER BY name;");
$sth->execute; # исполняем запрос
open (my $file, '>:encoding(UTF-8)', "$dir/phonebook.xml") || die "Error opening file: phonebook.xml $!";
print $file "\<\?xml version=\"1.0\"\?\>\n\<YealinkIPPhoneDirectory\>\n";
while (my $ref = $sth->fetchrow_arrayref){
my $name = $$ref[0];
my $extension = $$ref[1];
$invisible_yes = 0;
foreach my $invisible (@invisible){
if($extension == $invisible){
$invisible_yes++;
}
}
if ($invisible_yes == 0){
print $file " \<DirectoryEntry\>
\<Name\>$name\<\/Name\>
\<Telephone\>$extension\<\/Telephone\>
\<\/DirectoryEntry\>\n";
}
}
open (my $file_phonebook_cfg, '<:encoding(UTF-8)', "$dir/phonebook.cfg") || die "Error opening file: phonebook.cfg $!";
while (defined(my $line_phonebook_cfg = <$file_phonebook_cfg>)){
if($line_phonebook_cfg !~ /^#/){
chomp ($line_phonebook_cfg);
my @array_phonebook_cfg = split (/\t/,$line_phonebook_cfg,-1);
print $file " \<DirectoryEntry\>
\<Name\>$array_phonebook_cfg[0]\<\/Name\>
\<Telephone\>$array_phonebook_cfg[1]\<\/Telephone\>
\<\/DirectoryEntry\>\n";
}else{
next;
}
}
close ($file_phonebook_cfg);
print $file "\<\/YealinkIPPhoneDirectory\>\n";
close ($file);
my $rc = $sth->finish;
$rc = $dbasterisk->disconnect; # закрываем соединение
`cp $dir/phonebook.xml /var/www/html/phonebook.xml`
#`chmod 644 /var/www/html/phonebook.xml`