root/modules/branches/2.2/phonebook/functions.inc.php

Revision 2881, 4.5 kB (checked in by webrainstorm, 2 years ago)

Fix for #1234 - Warning with empty phonebook and #999 related fixes

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
Line 
1 <?php /* $Id */
2 //Copyright (C) 2006 WeBRainstorm S.r.l. (ask@webrainstorm.it)
3 //
4 //This program is free software; you can redistribute it and/or
5 //modify it under the terms of the GNU General Public License
6 //as published by the Free Software Foundation; either version 2
7 //of the License, or (at your option) any later version.
8 //
9 //This program is distributed in the hope that it will be useful,
10 //but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //GNU General Public License for more details.
13
14 function phonebook_list() {
15         global $amp_conf;
16         global $astman;
17
18         if ($astman) {
19                 $list = $astman->database_show();
20                 foreach ($list as $k => $v) {
21                         if (isset($v)) { // Somehow, a 'null' value is leaking into astdb.
22                                 if (substr($k, 1, 7) == 'cidname')
23                                         $numbers['foo'.substr($k, 9)]['name'] = $v ;
24                                 if (substr($k, 1, 13) == 'sysspeeddials')
25                                         $numbers['foo'.$v]['speeddial'] = substr($k, 15) ;
26                         }
27                 }
28
29                 if (isset($numbers) && is_array($numbers)) {
30                         foreach ($numbers as $key => $row) {
31                                 $names[$key]  = strtolower($row['name']);
32                         }
33                         // Array multisort renumber keys if they are numeric, (casting doesn't work), that's why I added 'foo' in front of the key
34                         // Quite ugly, I know... should recode it
35                         array_multisort($names, SORT_ASC, SORT_STRING, $numbers);
36                         foreach ($numbers as $key => $value) {
37                                 $retnumbers[substr($key, 3)] = $value;
38                         }
39                 }
40
41                 return isset($retnumbers)?$retnumbers:null;
42         } else {
43                 fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
44         }
45 }
46
47 function phonebook_del($number, $speeddial){
48         global $amp_conf;
49         global $astman;
50
51         if ($astman) {
52                 $astman->database_del("cidname",$number);
53                 if ($speeddial != '')
54                         $astman->database_del("sysspeeddials",$speeddial);
55         } else {
56                 fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
57         }
58 }
59
60 function phonebook_empty(){
61         global $amp_conf;
62         global $astman;
63
64         if ($astman) {
65                 $astman->database_deltree("cidname");
66                 $astman->database_deltree("sysspeeddials");
67         } else {
68                 fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
69         }
70 }
71
72 function phonebook_add($number, $name, $speeddial){
73         global $amp_conf;
74         global $astman;
75
76         if(!phonebook_chk($number))
77                 return false;
78
79         if ($astman) {
80                 // Was the user a twonk and didn't specify a speeddial?
81                 // Should we really automatically generate a speeddial ?
82                 // If yes I think we should start from 99 going down and leave easier speeddials to users
83                 if (empty($speeddial)) {
84                         for ($nbr = 99; $nbr > 0; $nbr--) {
85                                 if ($astman->database_get("sysspeeddials",sprintf("%02d",$nbr))===false) {
86                                         $speeddial = sprintf("%02d", $nbr);
87                                         break;
88                                 }
89                         }
90                 }
91                 $astman->database_put("cidname",$number, '"'.$name.'"');
92                 if ($speeddial != '')
93                         $astman->database_put("sysspeeddials",$speeddial, '"'.$number.'"');
94         } else {
95                 fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
96         }
97 }
98
99
100 // TODO: ensures post vars is valid
101 function phonebook_chk($post){
102         return true;
103 }
104
105 /*
106 * @version V1.01 16 June 2004 (c) Petar Nedyalkov (bu@orbitel.bg). All rights reserved.
107 * Released under the GPL license.
108 * http://bu.orbitel.bg/fgetcsvfromline.php
109 */
110
111 function phonebook_fgetcsvfromline ($line, $columnCount, $delimiterChar = ';', $enclosureChar = '"') {
112     $regExpSpecialChars = array (
113         "|" => "\\|",
114         "&" => "\\&",
115         "$" => "\\$",
116         "(" => "\\(",
117         ")" => "\\)",
118         "^" => "\\^",
119         "[" => "\\[",
120         "]" => "\\]",
121         "{" => "\\{",
122         "}" => "\\}",
123         "." => "\\.",
124         "*" => "\\*",
125         "\\" => "\\\\",
126         "/" => "\\/"
127     );
128
129     $matches = array();
130     $delimiterChar = strtr($delimiterChar, $regExpSpecialChars);
131     $enclosureChar = strtr($enclosureChar, $regExpSpecialChars);
132
133     $regExp = "/^";
134     for ($i = 0; $i < $columnCount; $i++) {
135         $regExp .= '('.$enclosureChar.'?)(.*)\\'.(2*$i + 1).$delimiterChar; // construct the regular expression
136     }
137     $regExp = substr($regExp, 0, (strlen($regExp) - strlen($delimiterChar)))."/"; // format the regular expression
138
139     if (preg_match($regExp, $line, $matches)) {
140         $result = array();
141         for ($i = 1; $i < count($matches)/2; $i++) {
142             if (strlen($matches[2*$i]) < 1)
143               $matches[2*$i] = "";
144             $result[$i] = $matches[2*$i]; // get only the fields but not the delimiters
145         }
146         return $result;
147     }
148     return FALSE;
149 }
150
151 ?>
Note: See TracBrowser for help on using the browser.
Donate



Support
Download
Develop
Forums
News
Documentation
Paid Support
About

Paid Ads