]> git.defcon.no Git - hermes/blob - api/lib/alias_functions.php
First stab at a naive permissions-tool
[hermes] / api / lib / alias_functions.php
1 <?php
2 /*
3 # Copyright (c) 2012, Gjøvik University College
4 # All rights reserved.
5
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.
16 #
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.
27 */
28 require_once('config.php');
29 require_once('lib/common_functions.php');
30 require_once('lib/db_functions.php');
31
32
33 function get_destination ( $alias_username, $alias_domain )
34 {
35 global $config;
36
37 $aliases = array();
38
39 // Terminate early on missing param.
40 if (!( $alias_username && $alias_domain)) return $aliases;
41
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)
47 );
48
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;
53
54 while ( $row = @mysql_fetch_assoc( $result ) )
55 {
56 array_push( $aliases, array( 'destination' => $row['destination'], 'alias' => $row['alias'] ) );
57 }
58 return $aliases;
59 }
60
61 function get_aliases ( $dest_username, $dest_domain )
62 {
63 global $config;
64
65 $aliases = array();
66 $query = "";
67 if (( $dest_username == null ) && ( $dest_domain == null) )
68 {
69 $query = sprintf("SELECT CONCAT(username, '@', domain ) AS destination, CONCAT( alias_username, '@', alias_domain) AS alias FROM %s" ,
70 $config['kamailio_alias_table']);
71 }
72 else
73 {
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));
80 }
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;
85
86 while ( $row = @mysql_fetch_assoc( $result ) )
87 {
88 array_push( $aliases, array( 'destination' => $row['destination'], 'alias' => $row['alias'] ) );
89 }
90 return $aliases;
91 }
92
93 function get_e164_alias( $dest_username, $dest_domain )
94 {
95 global $config;
96 $cur_aliases = get_aliases( $dest_username, $dest_domain );
97 foreach ( $cur_aliases as $testalias )
98 {
99 list( $tau, $foo ) = split_sipaddress( $testalias['alias'] );
100 if ( verify_e164 ( $tau ) ) return $testalias;
101 }
102 return false;
103
104 }
105 function alias_exists( $alias_username, $alias_domain )
106 {
107 global $config;
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);
112
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...
116 $num_r = $row[0];
117 if ( $num_r == 1 )
118 {
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..
126 }
127
128 return false;
129 }
130
131 function add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain )
132 {
133 global $config;
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 ) )
137 return false;
138
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));
143
144 return sql_dbexec( $config['kamailio_db'], $query);
145
146 }
147
148 function remove_alias ( $alias_username, $alias_domain )
149 {
150 global $config;
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);
157
158 }
159
160 ?>