]>
git.defcon.no Git - hermes/blob - api/lib/alias_functions.php
3 # Copyright (c) 2012, Gjøvik University College
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the Gjøvik University College nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY Gjøvik University College ''AS IS'' AND ANY
18 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL Gjøvik University College BE LIABLE FOR ANY
21 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 require_once('config.php');
29 require_once('lib/common_functions.php');
30 require_once('lib/db_functions.php');
33 function get_destination ( $alias_username, $alias_domain )
39 // Terminate early on missing param.
40 if (!( $alias_username && $alias_domain)) return $aliases;
42 $query = sprintf("SELECT CONCAT(username, '@', domain ) AS destination, CONCAT( alias_username, '@', alias_domain) AS alias FROM %s"
43 . " WHERE alias_username = '%s' AND alias_domain = '%s'",
44 $config['kamailio_alias_table'],
45 sql_clean($alias_username),
46 sql_clean($alias_domain)
49 $result = sql_dbquery( $config['kamailio_db'], $query);
50 // If result is empty, there was either an SQL error, or simply no results.
51 // At this point, no error checking is performed, instead the empty array is returned.
52 if ( ! $result ) return $aliases;
54 while ( $row = @mysql_fetch_assoc
( $result ) )
56 array_push( $aliases, array( 'destination' => $row['destination'], 'alias' => $row['alias'] ) );
61 function get_aliases ( $dest_username, $dest_domain )
67 if (( $dest_username == null ) && ( $dest_domain == null) )
69 $query = sprintf("SELECT CONCAT(username, '@', domain ) AS destination, CONCAT( alias_username, '@', alias_domain) AS alias FROM %s" ,
70 $config['kamailio_alias_table']);
74 $query = sprintf("SELECT '%s' AS destination, CONCAT( alias_username, '@', alias_domain) AS alias FROM %s"
75 . " WHERE username = '%s' AND domain = '%s'",
76 sql_clean($dest_username. '@' . $dest_domain),
77 $config['kamailio_alias_table'],
78 sql_clean($dest_username),
79 sql_clean($dest_domain));
81 $result = sql_dbquery( $config['kamailio_db'], $query);
82 // If result is empty, there was either an SQL error, or simply no results.
83 // At this point, no error checking is performed, instead the empty array is returned.
84 if ( ! $result ) return $aliases;
86 while ( $row = @mysql_fetch_assoc
( $result ) )
88 array_push( $aliases, array( 'destination' => $row['destination'], 'alias' => $row['alias'] ) );
93 function get_e164_alias( $dest_username, $dest_domain )
96 $cur_aliases = get_aliases( $dest_username, $dest_domain );
97 foreach ( $cur_aliases as $testalias )
99 list( $tau, $foo ) = split_sipaddress( $testalias['alias'] );
100 if ( verify_e164 ( $tau ) ) return $testalias;
105 function alias_exists( $alias_username, $alias_domain )
108 $query = sprintf("SELECT COUNT(*) AS num FROM %s WHERE alias_username = '%s' AND alias_domain = '%s'",
109 $config['kamailio_alias_table'],
110 sql_clean($alias_username), sql_clean($alias_domain));
111 $result = sql_dbquery($config['kamailio_db'], $query);
113 if ( !$result ) return true; // This is an error. Better to fail claiming alias exists...
114 $row = @mysql_fetch_row
($result);
115 if ( !$row ) return true; // This is an error. Better to fail claiming alias exists...
119 $query = sprintf("SELECT CONCAT(username, '@', domain ) AS destination FROM %s WHERE alias_username = '%s' AND alias_domain = '%s'",
120 $config['kamailio_alias_table'],
121 sql_clean($alias_username), sql_clean($alias_domain));
122 $result = sql_dbquery($config['kamailio_db'], $query);
123 $row = @mysql_fetch_row
($result);
124 if ( is_string( $row[0] ) ) return $row[0];
125 return true; // Failure mode..
131 function add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain )
134 // The following will, in normal cases, lead to a double-test, and double the number of queries...
135 // Defensive comment: better safe than sorry :P
136 if ( alias_exists( $alias_username, $alias_domain ) )
139 $query = sprintf("INSERT INTO %s ( alias_username, alias_domain, username, domain) VALUES ('%s','%s','%s','%s')",
140 $config['kamailio_alias_table'],
141 sql_clean($alias_username), sql_clean($alias_domain),
142 sql_clean($dest_username), sql_clean($dest_domain));
144 return sql_dbexec( $config['kamailio_db'], $query);
148 function remove_alias ( $alias_username, $alias_domain )
151 if (! alias_exists ( $alias_username, $alias_domain ) ) return false;
152 $query = sprintf ("DELETE FROM %s WHERE alias_username = '%s' AND alias_domain = '%s'",
153 $config['kamailio_alias_table'],
154 sql_clean( $alias_username ),
155 sql_clean( $alias_domain ));
156 return sql_dbexec( $config['kamailio_db'], $query);