]> git.defcon.no Git - hermes/blob - lib/alias_functions.php
Hermes is a framework for managing SIP-accounts, aliases and automatic phone-provisio...
[hermes] / lib / alias_functions.php
1 <?php
2 require_once('config.php');
3 require_once('lib/common_functions.php');
4 require_once('lib/db_functions.php');
5
6
7 function get_destination ( $alias_username, $alias_domain )
8 {
9 global $config;
10 }
11 function get_aliases ( $dest_username, $dest_domain )
12 {
13 global $config;
14
15 $aliases = array();
16 $query = sprintf("SELECT CONCAT( alias_username, '@', alias_domain) AS alias FROM %s"
17 . " WHERE username = '%s' AND domain = '%s'",
18 $config['kamailio_alias_table'], $dest_username, $dest_domain);
19 $result = sql_dbquery( $config['kamailio_db'], $query);
20 // If result is empty, there was either an SQL error, or simply no results.
21 // At this point, no error checking is performed, instead the empty array is returned.
22 if ( ! $result ) return $aliases;
23
24 while ( $row = mysql_fetch_assoc( $result ) )
25 {
26 array_push( $aliases, $row['alias'] );
27 }
28 return $aliases;
29 }
30
31 function get_e164_alias( $dest_username, $dest_domain )
32 {
33 global $config;
34 $cur_aliases = get_aliases( $dest_username, $dest_domain );
35 foreach ( $cur_aliases as $testalias )
36 {
37 list( $tau, $foo ) = split_sipaddress( $testalias );
38 if ( verify_e164 ( $tau ) ) return $testalias;
39 }
40 return false;
41
42 }
43 function alias_exists( $alias_username, $alias_domain )
44 {
45 global $config;
46 $query = sprintf("SELECT COUNT(*) AS num FROM %s WHERE alias_username = '%s' AND alias_domain = '%s'",
47 $config['kamailio_alias_table'],
48 sql_clean($alias_username), sql_clean($alias_domain));
49 $result = sql_dbquery($config['kamailio_db'], $query);
50
51 if ( !$result ) return true; // This is an error. Better to fail claiming alias exists...
52 $row = mysql_fetch_row($result);
53 if ( !$row ) return true; // This is an error. Better to fail claiming alias exists...
54 $num_r = $row[0];
55 if ( $num_r == 1 )
56 {
57 $query = sprintf("SELECT CONCAT(username, '@', domain ) AS destination FROM %s WHERE alias_username = '%s' AND alias_domain = '%s'",
58 $config['kamailio_alias_table'],
59 sql_clean($alias_username), sql_clean($alias_domain));
60 $result = sql_dbquery($config['kamailio_db'], $query);
61 $row = mysql_fetch_row($result);
62 if ( is_string( $row[0] ) ) return $row[0];
63 return true; // Failure mode..
64 }
65
66 return false;
67 }
68
69 function add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain )
70 {
71 global $config;
72 // The following will, in normal cases, lead to a double-test, and double the number of queries...
73 // Defensive comment: better safe than sorry :P
74 if ( alias_exists( $alias_username, $alias_domain ) )
75 return false;
76
77 $query = sprintf("INSERT INTO %s ( alias_username, alias_domain, username, domain) VALUES ('%s','%s','%s','%s')",
78 $config['kamailio_alias_table'],
79 sql_clean($alias_username), sql_clean($alias_domain),
80 sql_clean($dest_username), sql_clean($dest_domain));
81
82 return sql_dbexec( $config['kamailio_db'], $query);
83
84 }
85
86 function remove_alias ( $alias_username, $alias_domain )
87 {
88 global $config;
89 if (! alias_exists ( $alias_username, $alias_domain ) ) return false;
90 $query = sprintf ("DELETE FROM %s WHERE alias_username = '%s' AND alias_domain = '%s'",
91 $config['kamailio_alias_table'],
92 sql_clean( $alias_username ),
93 sql_clean( $alias_domain ));
94 return sql_dbexec( $config['kamailio_db'], $query);
95
96 }
97
98 ?>