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

Revision 5896, 4.6 kB (checked in by mbrevda, 5 years ago)

realy, realy fix #1821

  • 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, $gensd){
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 ? definatly only when gensd is checked
82     // If yes I think we should start from 99 going down and leave easier speeddials to users
83
84     if ($gensd == "yes"){
85       if (empty($speeddial)) {
86         for ($nbr = 99; $nbr > 0; $nbr--) {
87           if ($astman->database_get("sysspeeddials",sprintf("%02d",$nbr))===false) {
88             $speeddial = sprintf("%02d", $nbr);
89             break;
90           }
91         }
92       }
93     }
94     $astman->database_put("cidname",$number, '"'.$name.'"');
95     if ($speeddial != '')
96       $astman->database_put("sysspeeddials",$speeddial, '"'.$number.'"');
97   } else {
98     fatal("Cannot connect to Asterisk Manager with ".$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]);
99
100   }
101 }
102
103
104 // TODO: ensures post vars is valid
105 function phonebook_chk($post){
106   return true;
107 }
108
109 /*
110 * @version V1.01 16 June 2004 (c) Petar Nedyalkov (bu@orbitel.bg). All rights reserved.
111 * Released under the GPL license.
112 * http://bu.orbitel.bg/fgetcsvfromline.php
113 */
114
115 function phonebook_fgetcsvfromline ($line, $columnCount, $delimiterChar = ';', $enclosureChar = '"') {
116     $regExpSpecialChars = array (
117         "|" => "\\|",
118         "&" => "\\&",
119         "$" => "\\$",
120         "(" => "\\(",
121         ")" => "\\)",
122         "^" => "\\^",
123         "[" => "\\[",
124         "]" => "\\]",
125         "{" => "\\{",
126         "}" => "\\}",
127         "." => "\\.",
128         "*" => "\\*",
129         "\\" => "\\\\",
130         "/" => "\\/"
131     );
132
133     $matches = array();
134     $delimiterChar = strtr($delimiterChar, $regExpSpecialChars);
135     $enclosureChar = strtr($enclosureChar, $regExpSpecialChars);
136
137     $regExp = "/^";
138     for ($i = 0; $i < $columnCount; $i++) {
139         $regExp .= '('.$enclosureChar.'?)(.*)\\'.(2*$i + 1).$delimiterChar; // construct the regular expression
140     }
141     $regExp = substr($regExp, 0, (strlen($regExp) - strlen($delimiterChar)))."/"; // format the regular expression
142
143     if (preg_match($regExp, $line, $matches)) {
144         $result = array();
145         for ($i = 1; $i < count($matches)/2; $i++) {
146             if (strlen($matches[2*$i]) < 1)
147               $matches[2*$i] = "";
148             $result[$i] = $matches[2*$i]; // get only the fields but not the delimiters
149         }
150         return $result;
151     }
152     return FALSE;
153 }
154
155 ?>
Note: See TracBrowser for help on using the browser.