]> git.defcon.no Git - hermes/blob - api/lib/alias_functions.php
Added alias/list functionality! Also, moved some data around in lib/alias_functions...
[hermes] / api / 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 $aliases = array();
12
13 // Terminate early on missing param.
14 if (!( $alias_username && $alias_domain)) return $aliases;
15
16 $query = sprintf("SELECT CONCAT(username, '@', domain ) AS destination, CONCAT( alias_username, '@', alias_domain) AS alias FROM %s"
17 . " WHERE alias_username = '%s' AND alias_domain = '%s'",
18 $config['kamailio_alias_table'],
19 sql_clean($alias_username),
20 sql_clean($alias_domain)
21 );
22
23 $result = sql_dbquery( $config['kamailio_db'], $query);
24 // If result is empty, there was either an SQL error, or simply no results.
25 // At this point, no error checking is performed, instead the empty array is returned.
26 if ( ! $result ) return $aliases;
27
28 while ( $row = mysql_fetch_assoc( $result ) )
29 {
30 array_push( $aliases, array( 'destination' => $row['destination'], 'alias' => $row['alias'] ) );
31 }
32 return $aliases;
33 }
34
35 function get_aliases ( $dest_username, $dest_domain )
36 {
37 global $config;
38
39 $aliases = array();
40 $query = "";
41 if (( $dest_username == null ) && ( $dest_domain == null) )
42 {
43 $query = sprintf("SELECT CONCAT(username, '@', domain ) AS destination, CONCAT( alias_username, '@', alias_domain) AS alias FROM %s" ,
44 $config['kamailio_alias_table']);
45 }
46 else
47 {
48 $query = sprintf("SELECT '%s' AS destination, CONCAT( alias_username, '@', alias_domain) AS alias FROM %s"
49 . " WHERE username = '%s' AND domain = '%s'",
50 sql_clean($dest_username. '@' . $dest_domain),
51 $config['kamailio_alias_table'],
52 sql_clean($dest_username),
53 sql_clean($dest_domain));
54 }
55 $result = sql_dbquery( $config['kamailio_db'], $query);
56 // If result is empty, there was either an SQL error, or simply no results.
57 // At this point, no error checking is performed, instead the empty array is returned.
58 if ( ! $result ) return $aliases;
59
60 while ( $row = mysql_fetch_assoc( $result ) )
61 {
62 array_push( $aliases, array( 'destination' => $row['destination'], 'alias' => $row['alias'] ) );
63 }
64 return $aliases;
65 }
66
67 function get_e164_alias( $dest_username, $dest_domain )
68 {
69 global $config;
70 $cur_aliases = get_aliases( $dest_username, $dest_domain );
71 foreach ( $cur_aliases as $testalias )
72 {
73 list( $tau, $foo ) = split_sipaddress( $testalias['alias'] );
74 if ( verify_e164 ( $tau ) ) return $testalias;
75 }
76 return false;
77
78 }
79 function alias_exists( $alias_username, $alias_domain )
80 {
81 global $config;
82 $query = sprintf("SELECT COUNT(*) AS num FROM %s WHERE alias_username = '%s' AND alias_domain = '%s'",
83 $config['kamailio_alias_table'],
84 sql_clean($alias_username), sql_clean($alias_domain));
85 $result = sql_dbquery($config['kamailio_db'], $query);
86
87 if ( !$result ) return true; // This is an error. Better to fail claiming alias exists...
88 $row = mysql_fetch_row($result);
89 if ( !$row ) return true; // This is an error. Better to fail claiming alias exists...
90 $num_r = $row[0];
91 if ( $num_r == 1 )
92 {
93 $query = sprintf("SELECT CONCAT(username, '@', domain ) AS destination FROM %s WHERE alias_username = '%s' AND alias_domain = '%s'",
94 $config['kamailio_alias_table'],
95 sql_clean($alias_username), sql_clean($alias_domain));
96 $result = sql_dbquery($config['kamailio_db'], $query);
97 $row = mysql_fetch_row($result);
98 if ( is_string( $row[0] ) ) return $row[0];
99 return true; // Failure mode..
100 }
101
102 return false;
103 }
104
105 function add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain )
106 {
107 global $config;
108 // The following will, in normal cases, lead to a double-test, and double the number of queries...
109 // Defensive comment: better safe than sorry :P
110 if ( alias_exists( $alias_username, $alias_domain ) )
111 return false;
112
113 $query = sprintf("INSERT INTO %s ( alias_username, alias_domain, username, domain) VALUES ('%s','%s','%s','%s')",
114 $config['kamailio_alias_table'],
115 sql_clean($alias_username), sql_clean($alias_domain),
116 sql_clean($dest_username), sql_clean($dest_domain));
117
118 return sql_dbexec( $config['kamailio_db'], $query);
119
120 }
121
122 function remove_alias ( $alias_username, $alias_domain )
123 {
124 global $config;
125 if (! alias_exists ( $alias_username, $alias_domain ) ) return false;
126 $query = sprintf ("DELETE FROM %s WHERE alias_username = '%s' AND alias_domain = '%s'",
127 $config['kamailio_alias_table'],
128 sql_clean( $alias_username ),
129 sql_clean( $alias_domain ));
130 return sql_dbexec( $config['kamailio_db'], $query);
131
132 }
133
134 ?>