]>
git.defcon.no Git - hermes/blob - api/lib/alias_functions.php
51c3b58379bc9b6accb0cd44c369f77bd53a2869
2 require_once('config.php');
3 require_once('lib/common_functions.php');
4 require_once('lib/db_functions.php');
7 function get_destination ( $alias_username, $alias_domain )
13 // Terminate early on missing param.
14 if (!( $alias_username && $alias_domain)) return $aliases;
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)
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;
28 while ( $row = mysql_fetch_assoc( $result ) )
30 array_push( $aliases, array( 'destination' => $row['destination'], 'alias' => $row['alias'] ) );
35 function get_aliases ( $dest_username, $dest_domain )
41 if (( $dest_username == null ) && ( $dest_domain == null) )
43 $query = sprintf("SELECT CONCAT(username, '@', domain ) AS destination, CONCAT( alias_username, '@', alias_domain) AS alias FROM %s" ,
44 $config['kamailio_alias_table']);
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));
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;
60 while ( $row = mysql_fetch_assoc( $result ) )
62 array_push( $aliases, array( 'destination' => $row['destination'], 'alias' => $row['alias'] ) );
67 function get_e164_alias( $dest_username, $dest_domain )
70 $cur_aliases = get_aliases( $dest_username, $dest_domain );
71 foreach ( $cur_aliases as $testalias )
73 list( $tau, $foo ) = split_sipaddress( $testalias['alias'] );
74 if ( verify_e164 ( $tau ) ) return $testalias;
79 function alias_exists( $alias_username, $alias_domain )
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);
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...
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..
105 function add_alias( $alias_username, $alias_domain, $dest_username, $dest_domain )
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 ) )
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));
118 return sql_dbexec( $config['kamailio_db'], $query);
122 function remove_alias ( $alias_username, $alias_domain )
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);