]> git.defcon.no Git - hermes/blob - api/lib/phone_functions.php
Moved API-files to a separate API directory.
[hermes] / api / lib / phone_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 function get_user_phones ( $username, $domain )
7 {
8 global $config;
9 $query = sprintf("SELECT id FROM %s WHERE username = '%s' AND domain = '%s'",
10 $config['provision_users_table'],
11 sql_clean( $username ),
12 sql_clean( $domain )
13 );
14
15 $user = sql_dbquery_single( $config['provision_db'], $query );
16 if ( ! $user ) return null;
17 $user_id = $user['id'];
18
19 $query = sprintf("SELECT mac FROM %s WHERE user_rel = %d",
20 $config['provision_phones_table'],
21 $user_id
22 );
23
24 $result = sql_dbquery( $config['provision_db'], $query );
25 if ( !$result ) return null;
26 if (mysql_num_rows($result) < 1 ) return null;
27 $rows = array();
28 while ( $row = mysql_fetch_assoc( $result ) )
29 array_push( $rows, $row['mac'] );
30 return $rows;
31 }
32
33 function get_phone_users ( $macaddress )
34 {
35 global $config;
36 $ptbl = $config['provision_phones_table'];
37 $utbl = $config['provision_users_table'];
38 $query = "SELECT ".$ptbl.".mac as mac, CONCAT( ".$utbl.".username, '@', ".$utbl.".domain ) as user
39 FROM ".$ptbl."
40 INNER JOIN ".$utbl." ON ".$ptbl.".user_rel = ".$utbl.".id
41 WHERE ".$ptbl.".mac = '".sql_clean($macaddress ). "'";
42
43 $result = sql_dbquery( $config['provision_db'], $query );
44 if ( !$result ) return null;
45 if (mysql_num_rows($result) < 1 ) return null;
46 $rows = array();
47 while ( $row = mysql_fetch_assoc( $result ) )
48 {
49 array_push( $rows, $row['user'] );
50 }
51 return $rows;
52 }
53
54 function add_phone_user( $mac, $username, $domain )
55 {
56 global $config;
57 // Get ID of user, for use with user_rel field..
58 $user_id = get_provision_userid( $username, $domain );
59 if ( !$user_id ) return false;
60
61 // Doublecheck :)
62 $mac = clean_mac($mac);
63 if (!$mac) return false;
64
65 // Triplecheck :)
66 $phones = get_user_phones ( $username, $domain);
67 if ( $phones && in_array( $mac, $phones ) )
68 return false;
69
70 // OK, so we have the User ID, a valid MAC, and no previous registration
71 // of that combination. Going to add.
72 $query = sprintf("INSERT INTO %s ( mac, user_rel ) VALUES ( '%s', %d )",
73 $config['provision_phones_table'], $mac, $user_id);
74 return sql_dbexec( $config['provision_db'], $query );
75 }
76
77 function delete_phone_user( $mac, $username, $domain )
78 {
79 global $config;
80 // Get ID of user, for use with user_rel field..
81 $user_id = get_provision_userid( $username, $domain );
82 if ( !$user_id ) return false;
83
84 // Doublecheck :)
85 $mac = clean_mac($mac);
86 if (!$mac) return false;
87
88 // Triplecheck :)
89 $phones = get_user_phones ( $username, $domain);
90 if ( !$phones ) return false;
91 if ( ! in_array( $mac, $phones ) ) return false;
92
93 // OK, so we have the User ID, a valid MAC, and no previous registration
94 // of that combination. Going to remove.
95 $query = sprintf("DELETE FROM %s WHERE mac = '%s' AND user_rel = %d",
96 $config['provision_phones_table'], $mac, $user_id);
97 return sql_dbexec( $config['provision_db'], $query );
98 }
99
100
101
102 function list_phones ( $search = null )
103 {
104 global $config;
105 $query = sprintf("SELECT mac FROM %s", $config['provision_phones_table']);
106 if ( $search )
107 $query .= sprintf(" WHERE mac LIKE '%s%%'", sql_clean($search));
108
109 $result = sql_dbquery( $config['provision_db'], $query );
110 if ( !$result ) return null;
111 if (mysql_num_rows($result) < 1 ) return null;
112 $rows = array();
113 while ( $row = mysql_fetch_assoc( $result ) )
114 {
115 array_push( $rows, $row['mac'] );
116 }
117 return $rows;
118 }
119
120 ?>