forked from FreePBX/superfecta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.inc.php
218 lines (207 loc) · 7.6 KB
/
functions.inc.php
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
function superfecta_hook_core($viewing_itemid, $target_menuid) {
global $db;
$sql = "SELECT * FROM superfectaconfig WHERE field ='order' ORDER BY value ASC";
$schemes = $db->getAll($sql, array(), DB_FETCHMODE_ASSOC);
$html = '';
if ($target_menuid == 'did') {
if (superfecta_did_get($viewing_itemid)) {
$enabled = true;
} else {
$enabled = false;
}
$html.= '
<!--Enable CID Lookup-->
<div class="element-container">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="form-group">
<div class="col-md-3">
<label class="control-label" for="enable_superfecta">'. _("Enable Superfecta Lookup") .'</label>
<i class="fa fa-question-circle fpbx-help-icon" data-for="enable_superfecta"></i>
</div>
<div class="col-md-9 radioset">
<input type="radio" name="enable_superfecta" id="enable_superfecta_yes" value="yes" '. ($enabled?"CHECKED":"").'>
<label for="enable_superfecta_yes">'. _("Yes").'</label>
<input type="radio" name="enable_superfecta" id="enable_superfecta_no" value="no" '.($enabled?"":"CHECKED").'>
<label for="enable_superfecta_no">'. _("No").'</label>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<span id="enable_superfecta-help" class="help-block fpbx-help-block">'. _("Sources can be added/removed in CID Superfecta section").'</span>
</div>
</div>
</div>
<!--END Enable CID Lookup-->
';
//$html.='<tr><td colspan="2"><h5>' . _("Superfecta CID Lookup") . '<hr></h5></td></tr>';
$info = explode("/", $viewing_itemid);
$sql = "SELECT scheme FROM superfecta_to_incoming WHERE extension = ?";
$q = $db->prepare($sql);
$q->execute(array($info[0]));
$scheme = $q->fetchColumn();
$first = '<option value="ALL|ALL" {$selected}>'._('ALL').'</option>';
$has_selected = FALSE;
$last = '';
foreach ($schemes as $data) {
if ($scheme == $data['source']) {
$selected = 'selected';
$has_selected = TRUE;
} else {
$selected = '';
}
$name = explode("_", $data['source']);
$last .= '<option value="' . $data['source'] . '" ' . $selected . '>' . $name[1] . '</option>';
}
$selected = ($has_selected) ? 'selected' : '';
$first = str_replace('{$selected}', $selected, $first);
$opts = $first . $last;
$html .= '
<!--Scheme-->
<div class="element-container">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="form-group">
<div class="col-md-3">
<label class="control-label" for="superfecta_scheme">'. _("Superfecta Scheme") .'</label>
<i class="fa fa-question-circle fpbx-help-icon" data-for="superfecta_scheme"></i>
</div>
<div class="col-md-9">
<select class="form-control" id="superfecta_scheme" name="superfecta_scheme">
'.$opts.'
</select>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<span id="superfecta_scheme-help" class="help-block fpbx-help-block">'. _("Setup Schemes in CID Superfecta section").'</span>
</div>
</div>
</div>
<!--END Scheme-->
';
}
return $html;
}
function superfecta_hookProcess_core($viewing_itemid, $request) {
$db = \FreePBX::Database();
// TODO: move sql to functions superfecta_did_(add, del, edit)
if (!isset($request['action'])) {
return;
}
$result = '';
switch ($request['action']) {
case 'addIncoming':
if ($request['enable_superfecta'] == 'yes') {
$sql = "REPLACE INTO superfecta_to_incoming (extension, cidnum, scheme) values (:extension, :cidnum,:superfecta_scheme)";
$q = $db->prepare($sql);
$q->bindParam(':extension', $request['extension'], \PDO::PARAM_STR);
$q->bindParam(':cidnum', $request['cidnum'], \PDO::PARAM_STR);
$q->bindParam(':superfecta_scheme', $request['superfecta_scheme'], \PDO::PARAM_STR);
$q->execute();
$result = $db->lastInsertId();
}
break;
case 'delIncoming':
$extarray = explode('/', $request['extdisplay'], 2);
if (count($extarray) == 2) {
$sql = "DELETE FROM superfecta_to_incoming WHERE extension = :extension AND cidnum = :cidnum";
$q = $db->prepare($sql);
$q->bindParam(':extension', $extarray[0], \PDO::PARAM_STR);
$q->bindParam(':cidnum', $extarray[1], \PDO::PARAM_STR);
$q->execute();
$result = $db->lastInsertId();
}
break;
case 'edtIncoming': // deleting and adding as in core module
$extarray = explode('/', $request['extdisplay'], 2);
if (count($extarray) == 2) {
$sql = "DELETE FROM superfecta_to_incoming WHERE extension = :extension AND cidnum = :cidnum";
$q = $db->prepare($sql);
$q->bindParam(':extension', $extarray[0], \PDO::PARAM_STR);
$q->bindParam(':cidnum', $extarray[1], \PDO::PARAM_STR);
$q->execute();
}
if ($request['enable_superfecta'] == 'yes') {
$sql = "REPLACE INTO superfecta_to_incoming (extension, cidnum, scheme) values (:extension, :cidnum,:superfecta_scheme)";
$q = $db->prepare($sql);
$q->bindParam(':extension', $request['extension'], \PDO::PARAM_STR);
$q->bindParam(':cidnum', $request['cidnum'], \PDO::PARAM_STR);
$q->bindParam(':superfecta_scheme', $request['superfecta_scheme'], \PDO::PARAM_STR);
$q->execute();
$result = $db->lastInsertId();
}
break;
}
return $result;
}
function superfecta_hookGet_config($engine) {
// TODO: integrating with direct extension <-> DID association
// TODO: add option to avoid callerid lookup if the telco already supply a callerid name (GosubIf)
global $ext; // is this the best way to pass this?
switch ($engine) {
case "asterisk":
$pairing = superfecta_did_list();
if (is_array($pairing)) {
foreach ($pairing as $item) {
if ($item['superfecta_to_incoming_id'] != 0) {
// Code from modules/core/functions.inc.php core_get_config inbound routes
$exten = trim($item['extension']);
$cidnum = trim($item['cidnum']);
$scheme = trim($item['scheme']);
if ($scheme == '') {
$scheme = 'base_Default';
}
if ($cidnum != '' && $exten == '') {
$exten = 's';
$pricid = ($item['pricid']) ? true : false;
} else if (($cidnum != '' && $exten != '') || ($cidnum == '' && $exten == '')) {
$pricid = true;
} else {
$pricid = false;
}
$context = ($pricid) ? "ext-did-0001" : "ext-did-0002";
$exten = (empty($exten) ? "s" : $exten);
$exten = $exten . (empty($cidnum) ? "" : "/" . $cidnum); //if a CID num is defined, add it
//https://github.com/POSSA/Caller-ID-Superfecta/issues/144
$ext->splice($context, $exten, 'did-cid-hook', new ext_setvar('CIDSFSCHEME', base64_encode($scheme)));
$ext->splice($context, $exten, 'did-cid-hook', new ext_agi(dirname(__FILE__) . '/agi/superfecta.agi'));
$ext->splice($context, $exten, 'did-cid-hook', new ext_setvar('CALLERID(name)', '${lookupcid}'));
}
}
}
break;
}
}
function superfecta_did_get($did) {
$extarray = explode('/', $did, 2);
if (count($extarray) == 2) {
$sql = "SELECT * FROM superfecta_to_incoming WHERE extension = " . q($extarray[0]) . " AND cidnum = " . q($extarray[1]);
$result = sql($sql, "getAll", DB_FETCHMODE_ASSOC);
if (is_array($result) && count($result)) {
return true;
}
}
return false;
}
function superfecta_did_list($id=false) {
$sql = "
SELECT superfecta_to_incoming_id, a.extension extension, a.cidnum cidnum, pricid, scheme FROM superfecta_to_incoming a
INNER JOIN incoming b
ON a.extension = b.extension AND a.cidnum = b.cidnum
";
if ($id !== false && ctype_digit($id)) {
$sql .= " WHERE superfecta_to_incoming_id = '" . q($id) . "'";
}
$results = sql($sql, "getAll", DB_FETCHMODE_ASSOC);
return is_array($results) ? $results : array();
}