-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcucmappuser.php
More file actions
139 lines (122 loc) · 5.07 KB
/
cucmappuser.php
File metadata and controls
139 lines (122 loc) · 5.07 KB
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
<?php
ini_set("soap.wsdl_cache_enabled", "0");
$conf = parse_ini_file(dirname(__FILE__) . DIRECTORY_SEPARATOR . "conf" . DIRECTORY_SEPARATOR . "cucm.conf");
$host = $conf['host'];
$username = $conf['username'];
$password = $conf['password'];
if (!isset($argv[1]) || !isset($argv[2]) || !in_array($argv[2], ["add","del"])) {
die("ERROR: Missing parameters, check if you specify appuser and action('add','del')");
}
$appUser = $argv[1];
$action = $argv[2];
$userExists = false;
// Read file and put phones mac's in array - delimiters can be "," , ";" ,"\n"
$myfile = fopen("phones.txt", "r") or die("Unable to open template file!");
$phones = fread($myfile, filesize("phones.txt"));
fclose($myfile);
$phones = multiexplode(array(",",".","|",":","\n"), $phones);
//connect to CUCM via Soap
$context = stream_context_create(array('ssl'=>array('allow_self_signed'=>true)));
$wsdl = dirname(__FILE__) . DIRECTORY_SEPARATOR. $conf['cucm_version'] . DIRECTORY_SEPARATOR . 'AXLAPI.wsdl';
$client = new SoapClient(
$wsdl,
array('trace'=>true,
'exceptions'=>true,
'location'=>"https://".$host.":8443/axl",
'login'=>$username,
'password'=>$password,
'stream_context'=> stream_context_create(
['ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
],
]
),
)
);
// Check if appuser parametar exists
$sql = "select * from applicationuser" ;
$response = $client->executeSQLQuery(array("sql" => $sql));
$appusers = $response->return->row;
foreach ($appusers as $appuser) {
if ($appuser->name == $appUser) {
$userExists = true;
}
}
if (!$userExists) {
die("ERROR: Check if appuser : $appUser exists in CUCM ! \n");
}
// Make folder with appuser name - there will be logs.
if (!file_exists($appUser)) {
mkdir($appUser);
}
writeAppUserDataBeforeUpdate();
$num_of_proccessed_phones = 0;
$success = array();
$failed = array();
foreach ($phones as $phone) {
if ($action == "add") {
$sql = "insert into applicationuserdevicemap (fkapplicationuser, fkdevice, tkuserassociation) select au.pkid, d.pkid, 1 from applicationuser au cross join device d where au.name = '$appUser' and d.name = '$phone' and
d.pkid not in (select fkdevice from applicationuserdevicemap where fkapplicationuser = au.pkid )";
}
if ($action == "del") {
$sql = "delete from applicationuserdevicemap where fkapplicationuser = (select pkid from applicationuser au where au.name = '$appUser') and fkdevice in (select pkid from device d where d.name = '$phone')" ;
}
$response = $client->executeSQLUpdate(array("sql" => $sql));
if ($response->return->rowsUpdated != 1) {
$failed[] = $phone;
} else {
$success[] = $phone;
}
}
writeLog();
if (count($failed) > 0) {
exit("Script finished with ERROR : " .count($failed) . " records failed - see log file \n");
} else {
exit("Script finished SUCCESSFULLY : " .count($success) . " records updated \n");
}
// function to convert various delimiters to common one
function multiexplode($delimiters, $string)
{
$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return $launch;
}
// write assosiated devices before update
function writeAppUserDataBeforeUpdate()
{
global $appUser;
global $client;
$sql = "select d.name from device d
join applicationuserdevicemap am on d.pkid = am.fkdevice
join applicationuser ap on ap.pkid = am.fkapplicationuser
where ap.name = '$appUser'";
$response = $client->executeSQLQuery(array("sql" => $sql));
//check if there was phones associated with this appuse
if (isset($response->return->row)) {
$phones = $response->return->row;
//var_dump($phones);
$myfile = fopen("$appUser".DIRECTORY_SEPARATOR."beforeLastUpdate.txt", "w");
foreach ($phones as $phone) {
fwrite($myfile, $phone->name ."\n");
}
fclose($myfile);
}
}
function writeLog()
{
global $appUser;
global $success;
global $failed;
$myfile = fopen("$appUser".DIRECTORY_SEPARATOR."log.txt", "wa");
fwrite($myfile, date("d-m-Y h:i:s", time())."\n\n");
fwrite($myfile, "Number of succesful records : " . count($success) . "\n\n");
fwrite($myfile, "Number of failed records : " . count($failed) . "\n");
if (count($failed) > 0) {
foreach ($failed as $phone) {
fwrite($myfile, $phone."\n");
}
}
fclose($myfile);
}