-<?php
-require_once('config.php');
-require_once('lib/common_functions.php');
-require_once('lib/db_functions.php');
-
-function get_user_phones ( $username, $domain )
-{
- global $config;
- $query = sprintf("SELECT id FROM %s WHERE username = '%s' AND domain = '%s'",
- $config['provision_users_table'],
- sql_clean( $username ),
- sql_clean( $domain )
- );
-
- $user = sql_dbquery_single( $config['provision_db'], $query );
- if ( ! $user ) return null;
- $user_id = $user['id'];
-
- $query = sprintf("SELECT mac FROM %s WHERE user_rel = %d",
- $config['provision_phones_table'],
- $user_id
- );
-
- $result = sql_dbquery( $config['provision_db'], $query );
- if ( !$result ) return null;
- if (mysql_num_rows($result) < 1 ) return null;
- $rows = array();
- while ( $row = mysql_fetch_assoc( $result ) )
- array_push( $rows, $row['mac'] );
- return $rows;
-}
-
-function get_phone_users ( $macaddress )
-{
- global $config;
- $ptbl = $config['provision_phones_table'];
- $utbl = $config['provision_users_table'];
- $query = "SELECT ".$ptbl.".mac as mac, CONCAT( ".$utbl.".username, '@', ".$utbl.".domain ) as user
- FROM ".$ptbl."
- INNER JOIN ".$utbl." ON ".$ptbl.".user_rel = ".$utbl.".id
- WHERE ".$ptbl.".mac = '".sql_clean($macaddress ). "'";
-
- $result = sql_dbquery( $config['provision_db'], $query );
- if ( !$result ) return null;
- if (mysql_num_rows($result) < 1 ) return null;
- $rows = array();
- while ( $row = mysql_fetch_assoc( $result ) )
- {
- array_push( $rows, $row['user'] );
- }
- return $rows;
-}
-
-function add_phone_user( $mac, $username, $domain )
-{
- global $config;
- // Get ID of user, for use with user_rel field..
- $user_id = get_provision_userid( $username, $domain );
- if ( !$user_id ) return false;
-
- // Doublecheck :)
- $mac = clean_mac($mac);
- if (!$mac) return false;
-
- // Triplecheck :)
- $phones = get_user_phones ( $username, $domain);
- if ( $phones && in_array( $mac, $phones ) )
- return false;
-
- // OK, so we have the User ID, a valid MAC, and no previous registration
- // of that combination. Going to add.
- $query = sprintf("INSERT INTO %s ( mac, user_rel ) VALUES ( '%s', %d )",
- $config['provision_phones_table'], $mac, $user_id);
- return sql_dbexec( $config['provision_db'], $query );
-}
-
-function delete_phone_user( $mac, $username, $domain )
-{
- global $config;
- // Get ID of user, for use with user_rel field..
- $user_id = get_provision_userid( $username, $domain );
- if ( !$user_id ) return false;
-
- // Doublecheck :)
- $mac = clean_mac($mac);
- if (!$mac) return false;
-
- // Triplecheck :)
- $phones = get_user_phones ( $username, $domain);
- if ( !$phones ) return false;
- if ( ! in_array( $mac, $phones ) ) return false;
-
- // OK, so we have the User ID, a valid MAC, and no previous registration
- // of that combination. Going to remove.
- $query = sprintf("DELETE FROM %s WHERE mac = '%s' AND user_rel = %d",
- $config['provision_phones_table'], $mac, $user_id);
- return sql_dbexec( $config['provision_db'], $query );
-}
-
-
-
-function list_phones ( $search = null )
-{
- global $config;
- $query = sprintf("SELECT mac FROM %s", $config['provision_phones_table']);
- if ( $search )
- $query .= sprintf(" WHERE mac LIKE '%s%%'", sql_clean($search));
-
- $result = sql_dbquery( $config['provision_db'], $query );
- if ( !$result ) return null;
- if (mysql_num_rows($result) < 1 ) return null;
- $rows = array();
- while ( $row = mysql_fetch_assoc( $result ) )
- {
- array_push( $rows, $row['mac'] );
- }
- return $rows;
-}
-
-?>