-
Notifications
You must be signed in to change notification settings - Fork 1
/
phonebook_xml_yea_and_gran.pl
149 lines (139 loc) · 5.76 KB
/
phonebook_xml_yea_and_gran.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/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'; #"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_conf, '<:encoding(UTF-8)', "/etc/freepbx.conf") || die "Error opening file: freepbx.conf $!";
while (defined(my $line_freepbx_conf = <$freepbx_conf>)){
chomp ($line_freepbx_conf);
if ($line_freepbx_conf =~ /^\#/){
next;
}
$line_freepbx_conf =~ s/\$amp_conf\[\"//;
$line_freepbx_conf =~ s/\"\]//;
$line_freepbx_conf =~ s/\"//;
$line_freepbx_conf =~ s/\"\;//;
my @array_freepbx_conf = split (/ = /,$line_freepbx_conf,-1);
given($array_freepbx_conf[0]){
when('AMPDBHOST'){
$host = $array_freepbx_conf[1];
}when('AMPDBUSER'){
$user = $array_freepbx_conf[1];
}when('AMPDBPASS'){
$pass = $array_freepbx_conf[1];
}when('AMPDBNAME'){
$db = $array_freepbx_conf[1];
}default{
next;
}
}
}
close($freepbx_conf);
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('invisible'){
$array_freepbx_pass[1] =~ s/ //;
@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_yealink.xml") || die "Error opening file: phonebook_yealink.xml $!";
open (my $file_grandstream, '>:encoding(UTF-8)', "$dir/phonebook_grandstream.xml") || die "Error opening file: phonebook_grandstream.xml $!";
print $file "\<\?xml version=\"1.0\"\?\>\n\<YealinkIPPhoneDirectory\>\n";
print $file_grandstream "\<\?xml version=\"1.0\" encoding=\"UTF-8\"\?\>\n\<AddressBook\>\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";
print $file_grandstream " \<Contact\>
\<FirstName\>\<\/FirstName\>
\<LastName\>$name\<\/LastName\>
\<Phone\>
\<phonenumber\>$extension\<\/phonenumber\>
\<accountindex\>1\<\/accountindex\>
\<downloaded\>1\<\/downloaded\>
\</Phone\>
\<Groups\>
\<groupid\>2\<\/groupid\>
\<\/Groups\>
\<\/Contact\>\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 =~ /^#/){
next;
}
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";
print $file_grandstream " \<Contact\>
\<FirstName\>\<\/FirstName\>
\<LastName\>$array_phonebook_cfg[0]\<\/LastName\>
\<Phone\>
\<phonenumber\>$array_phonebook_cfg[1]\<\/phonenumber\>
\<accountindex\>1\<\/accountindex\>
\<downloaded\>1\<\/downloaded\>
\</Phone\>
\<Groups\>
\<groupid\>2\<\/groupid\>
\<\/Groups\>
\<\/Contact\>\n";
}
close ($file_phonebook_cfg);
print $file "\<\/YealinkIPPhoneDirectory\>\n";
print $file_grandstream "\<\/AddressBook\>\n";
close ($file);
close ($file_grandstream);
my $rc = $sth->finish;
$rc = $dbasterisk->disconnect; # закрываем соединение
`cp $dir/phonebook_yealink.xml /var/www/html/phonebook_yealink.xml`;
`cp $dir/phonebook_grandstream.xml /var/www/html/phonebook_grandstream.xml`;
#`chmod 644 /var/www/html/phonebook.xml`;