]>
git.defcon.no Git - hermes/blob - api/lib/phone_functions.php
17fbe10fac8e7ce9e4293eccd3c14df481e03dfa
2 require_once('config.php');
3 require_once('lib/common_functions.php');
4 require_once('lib/db_functions.php');
6 function get_user_phones ( $username, $domain )
9 $query = sprintf("SELECT id FROM %s WHERE username = '%s' AND domain = '%s'",
10 $config['provision_users_table'],
11 sql_clean( $username ),
15 $user = sql_dbquery_single( $config['provision_db'], $query );
16 if ( ! $user ) return null;
17 $user_id = $user['id'];
19 $query = sprintf("SELECT mac FROM %s WHERE user_rel = %d",
20 $config['provision_phones_table'],
24 $result = sql_dbquery( $config['provision_db'], $query );
25 if ( !$result ) return null;
26 if (mysql_num_rows($result) < 1 ) return null;
28 while ( $row = mysql_fetch_assoc( $result ) )
29 array_push( $rows, $row['mac'] );
33 function get_phone_users ( $macaddress )
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
40 INNER JOIN ".$utbl." ON ".$ptbl.".user_rel = ".$utbl.".id
41 WHERE ".$ptbl.".mac = '".sql_clean($macaddress ). "'";
43 $result = sql_dbquery( $config['provision_db'], $query );
44 if ( !$result ) return null;
45 if (mysql_num_rows($result) < 1 ) return null;
47 while ( $row = mysql_fetch_assoc( $result ) )
49 array_push( $rows, $row['user'] );
54 function add_phone_user( $mac, $username, $domain )
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;
62 $mac = clean_mac($mac);
63 if (!$mac) return false;
66 $phones = get_user_phones ( $username, $domain);
67 if ( $phones && in_array( $mac, $phones ) )
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 );
77 function delete_phone_user( $mac, $username, $domain )
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;
85 $mac = clean_mac($mac);
86 if (!$mac) return false;
89 $phones = get_user_phones ( $username, $domain);
90 if ( !$phones ) return false;
91 if ( ! in_array( $mac, $phones ) ) return false;
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 );
102 function list_phones ( $search = null )
105 $query = sprintf("SELECT mac FROM %s", $config['provision_phones_table']);
107 $query .= sprintf(" WHERE mac LIKE '%s%%'", sql_clean($search));
109 $result = sql_dbquery( $config['provision_db'], $query );
110 if ( !$result ) return null;
111 if (mysql_num_rows($result) < 1 ) return null;
113 while ( $row = mysql_fetch_assoc( $result ) )
115 array_push( $rows, $row['mac'] );