Documentation is available at phone2dev.php
- <?php
- /**
- * Phone2Dev Class
- *
- * Implements Phone2Dev class which abstracts database access
- * to web and voice interfaces
- * @package Phone2Dev
- * @author Stanislav Miroshnikov
- */
- /**
- * Requires PEAR DB package
- */
- require_once 'DB.php';
- /**
- * Phone2Dev Class
- * @package Phone2Dev
- * @author Stanislav Miroshnikov
- */
- class Phone2Dev {
- /**
- * DB handle
- */
- var $objDB;
- /**
- * Connects the class instance to the database
- */
- function dbConnect(){
- // this have to be changed by hand
- $strDsn = 'mysql://stan@tcp(disco.cs.columbia.edu:3316)/devices';
- $arrOptions = array('debug' => 2);
- $this->objDB =& DB::connect($strDsn, $arrOptions);
- // check for connection error
- if (DB::isError($this->objDB)) {
- die(__CLASS__."::".__FUNCTION__."():".$this->objDB->getMessage());
- }
- }
- /**
- * Diconnects the class instance from the database
- */
- function dbDisconnect(){
- $this->objDB->disconnect();
- }
- /**
- * Authenticates using a phone number and pin
- * @param strPhone user's phone number
- * @param strPin user's pin number
- * @returns integer|-1 containing the userId if user was authenticated
- * correctly, else returns -1
- */
- function authPhonePin($strPhone, $strPin) {
- // make sure that all the required parameters are supplied
- if(!isset($strPhone)) {
- die(__CLASS__."::".__FUNCTION__."():"."strPhone is not set.");
- }
- if(!isset($strPin)) {
- die(__CLASS__."::".__FUNCTION__."():"."strPin is not set.");
- }
- if(!isset($this->objDB)) {
- die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
- }
- // get userId query
- $strSql = 'SELECT UserId FROM Users WHERE Phone = ? AND Pin = ?';
- $arr_data = array($strPhone, md5($strPin));
- $obj_res =& $this->objDB->query($strSql, $arr_data);
- // check that result has no errors
- if(DB::isError($obj_res)) {
- die(__CLASS__."::".__FUNCTION__."():".$obj_res->getMessage());
- }
- $intUserId = -1;
- // if the phone number and pin combination exists, get the username
- if($arr_row =& $obj_res->fetchRow()) {
- // user was authenticated
- $intUserId = $arr_row[0];
- }
- return $intUserId;
- }
- /**
- * Authenticates using a username and password
- * @param strUsername user's username
- * @param strPassword user's password
- * @returns an integer containing the user's id if user was authenticated
- * correctly, else returns -1
- */
- function authUserPass($strUsername, $strPassword) {
- // make sure that all the required parameters are supplied
- if(!isset($strUsername)) {
- die(__CLASS__."::".__FUNCTION__."():"."strUsername is not set.");
- }
- if(!isset($strPassword)) {
- die(__CLASS__."::".__FUNCTION__."():"."strPassword is not set.");
- }
- if(!isset($this->objDB)) {
- die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
- }
- // get userId query
- $strSql = 'SELECT UserId FROM Users WHERE Username = ? AND Password = ?';
- $arrData = array($strUsername, md5($strPassword));
- $objResult =& $this->objDB->query($strSql, $arrData);
- // check that result has no errors
- if(DB::isError($objResult)) {
- die($objResult->getMessage());
- }
- $intUserId = -1;
- // if the phone number and pin combination exists, get the username
- if($arrRow =& $objResult->fetchRow()) {
- // user was authenticated
- $intUserId = $arrRow[0];
- }
- return $intUserId;
- }
- /**
- * Checks where the username exists in the database
- * @param strUsername user's username
- * @return true|falseif the username exists
- */
- function userExists($strUsername) {
- // make sure that all the required parameters are supplied
- if(!isset($strUsername)) {
- die(__CLASS__."::".__FUNCTION__."():"."strUsername is not set.");
- }
- if(!isset($this->objDB)) {
- die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
- }
- // get userId query
- $strSql = 'SELECT UserId FROM Users WHERE Username = ?';
- $objResult =& $this->objDB->query($strSql, $strUsername);
- // check that result has no errors
- if(DB::isError($objResult)) {
- die($objResult->getMessage());
- }
- if($objResult->numRows() != 0) return true;
- return false;
- }
- /**
- * Checks where the telephone exists
- * @param strPhone user's phone
- * @returns true if username exists, else false
- */
- function phoneExists($strPhone) {
- // make sure that all the required parameters are supplied
- if(!isset($strPhone)) {
- die(__CLASS__."::".__FUNCTION__."():"."strPhone is not set.");
- }
- if(!isset($this->objDB)) {
- die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
- }
- // get userId query
- $strSql = 'SELECT UserId FROM Users WHERE Phone = ?';
- $objResult =& $this->objDB->query($strSql, $strPhone);
- // check that result has no errors
- if(DB::isError($objResult)) {
- die($objResult->getMessage());
- }
- if($objResult->numRows() != 0) return true;
- return false;
- }
- /**
- * Inserts the registration information into the database
- * @param strLastName validated last name
- * @param strFirstName validated first name
- * @param strUsername validated username
- * @param strPassword validated password
- * @param strPhone validated phone
- * @param strPin validated pin
- * @param strServer validated sip server
- * @param strAddress validated sip address
- */
- function registerUser($strLastName,
- $strFirstName,
- $strUsername,
- $strPassword,
- $strPhone,
- $strPin,
- $strServer,
- $strAddress) {
- // make sure that all the required parameters are supplied
- if(!isset($strLastName) ||
- !isset($strFirstName) ||
- !isset($strUsername) ||
- !isset($strPassword) ||
- !isset($strPhone) ||
- !isset($strPin) ||
- !isset($strServer) ||
- !isset($strAddress)) {
- die(__CLASS__."::".__FUNCTION__."():"
- ."one (or more) of the user parameters is not set.");
- }
- if(!isset($this->objDB)) {
- die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
- }
- // insert user information
- $strSql = 'INSERT INTO Users VALUES(null, ?, ?, ?, ?, ?, ?, ?, ?, null)';
- $arrData = array($strLastName,
- $strFirstName,
- $strUsername,
- md5($strPassword),
- $strPhone,
- md5($strPin),
- $strServer,
- $strAddress);
- $objResult =& $this->objDB->query($strSql, $arrData);
- // check that result has no errors
- if(DB::isError($objResult)) {
- die(__CLASS__."::".__FUNCTION__."():".$objResult->getMessage());
- }
- // get the user id
- $intUserId = $this->getUserId($strUsername);
- // insert main menu for the user
- $strSql = 'INSERT INTO Menus VALUES(null, ?, ?, 0, 0)';
- $arrData = array("Main Menu", $intUserId);
- $objResult =& $this->objDB->query($strSql, $arrData);
- // check that result has no errors
- if(DB::isError($objResult)) {
- die(__CLASS__."::".__FUNCTION__."():".$objResult->getMessage());
- }
- //set the menu location to itself, since its the main menu
- // insert main menu for the user
- $strSql = 'UPDATE Menus SET MenuLocationId = MenuId WHERE OwnerId = ?';
- $objResult =& $this->objDB->query($strSql, $intUserId);
- // check that result has no errors
- if(DB::isError($objResult)) {
- die(__CLASS__."::".__FUNCTION__."():".$objResult->getMessage());
- }
- }
- /**
- * Retrieves the userId for the user
- * @param strUsername a valid username contained in the database
- * @returns integer containing the userId
- */
- function getUserId($strUsername) {
- // make sure that all the required parameters are supplied
- if(!isset($strUsername)) {
- die(__CLASS__."::".__FUNCTION__."():"."strUsername is not set.");
- }
- if(!isset($this->objDB)) {
- die("Phone2Dev::getUserId(): objDB is not set.");
- }
- // get userId query
- $strSql = 'SELECT UserId FROM Users WHERE Username = ?';
- $objResult =& $this->objDB->query($strSql, $strUsername);
- // check that result has no errors
- if(DB::isError($objResult)) {
- die($objResult->getMessage());
- }
- $intUserId = -1;
- if($arrRow =& $objResult->fetchRow()) {
- $intUserId = $arrRow[0];
- } else {
- die(__CLASS__."::".__FUNCTION__."():"."Username does"
- ." not exist in the database");
- }
- return $intUserId;
- }
- /**
- * Retrieves the userId for the user
- * @param strPhone a valid phone number in the database
- * @returns an integer containing the userId
- */
- function getUserIdByPhone($strPhone) {
- // make sure that all the required parameters are supplied
- if(!isset($strPhone)) {
- die(__CLASS__."::".__FUNCTION__."():"."strPhone is not set.");
- }
- if(!isset($this->objDB)) {
- die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
- }
- // get userId query
- $strSql = 'SELECT UserId FROM Users WHERE Phone = ?';
- $objResult =& $this->objDB->query($strSql, $strPhone);
- // check that result has no errors
- if(DB::isError($objResult)) {
- die($__CLASS__."::".__FUNCTION__."():".objResult->getMessage());
- }
- $intUserId = -1;
- if($arrRow =& $objResult->fetchRow()) {
- $intUserId = $arrRow[0];
- } else {
- die("Phone2Dev::getUserIdByPhone(): This phone number does"
- ." not exist in the database.");
- }
- return $intUserId;
- }
- function getMainMenuId($intUserId) {
- // make sure that all the required parameters are supplied
- if(!isset($intUserId)) {
- die(__CLASS__."::".__FUNCTION__."():"."intUserId is not set");
- }
- if(!isset($this->objDB)) {
- die(__CLASS__."::".__FUNCTION__."():"."objDB is not set");
- }
- // get commands sql query
- $strSql = 'SELECT MenuId, MenuLocationId FROM Menus WHERE OwnerId = ?';
- $intOwnerId = $intUserId;
- $objResult =& $this->objDB->query($strSql, $intOwnerId);
- // check that result is not an error
- if (DB::isError($objResult)) {
- die(__CLASS__."::".__FUNCTION__."():".$objResult->getMessage());
- }
- // find the user's main menu
- $intMainMenuId = -1;
- while ($arrRow =& $objResult->fetchRow()) {
- if($arrRow[0] == $arrRow[1]) {
- $intMainMenuId = $arrRow[0];
- break;
- }
- }
- return $intMainMenuId;
- }
- /**
- * Returns sub menus of the given menu
- * @param intMenuId valid id of a menu
- * @param intUserId valid if of a user
- * @returns array sub menu id's as keys and menu names as values
- * @todo return sub menu array with menu order as a key so it
- * can be combined and sorted with the command array
- */
- function getSubMenus($intMenuId, $intUserId) {
- // make sure that all the required parameters are supplied
- if(!isset($this->objDB)){
- die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
- }
- if(!isset($intMenuId)){
- die(__CLASS__."::".__FUNCTION__."():"."intMenuId is not set.");
- }
- if(!isset($intUserId)){
- die(__CLASS__."::".__FUNCTION__."():"."intUserId is not set.");
- }
- // display all menus located in that menu
- $strSql = "SELECT MenuId AS intSubMenuId, Name AS strMenuName FROM Menus "
- ."WHERE MenuLocationId = ? AND OwnerId = ?";
- $arrData = array($intMenuId, $intUserId);
- $arrResult =& $this->objDB->getAssoc($strSql, false, $arrData,
- DB_FETCHMODE_ASSOC);
- if (DB::isError($arrResult)) {
- die(__CLASS__."::".__FUNCTION__."():".$arrResult->getMessage());
- }
- return $arrResult;
- }
- /**
- * Returns commands of the given menu
- * @param intMenuId valid id of a menu
- * @param intUserId valid if of a user
- * @returns array commandId as keys and
- * an associate array of command information as value
- * @todo return commands array with menu order as a key so it
- * can be combined and sorted with the sub menu array
- */
- function getCommands($intMenuId, $intUserId) {
- // make sure that all the required parameters are supplied
- if(!isset($this->objDB)){
- die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
- }
- if(!isset($intMenuId)){
- die(__CLASS__."::".__FUNCTION__."():"."intMenuId is not set.");
- }
- if(!isset($intUserId)){
- die(__CLASS__."::".__FUNCTION__."():"."intUserId is not set.");
- }
- // get commands for the menu
- $strSql = "SELECT CommandId, Commands.Name AS strComName, "
- ."Devices.Name AS strDeviceName, "
- ."Devices.DeviceId AS intDeviceId "
- ."FROM Commands, Devices "
- ."WHERE Commands.DeviceId = Devices.DeviceId "
- ."AND Commands.MenuLocationId = ? AND Commands.OwnerId = ?";
- $arrData = array($intMenuId, $intUserId);
- // try to retrieve the associative array of objects
- $arrResult =& $this->objDB->getAssoc($strSql, true, $arrData,
- DB_FETCHMODE_ASSOC);
- if (DB::isError($arrResult)) {
- die(__CLASS__."::".__FUNCTION__."():".$arrResult->getMessage());
- }
- return $arrResult;
- }
- /**
- * Executes the user command
- * @param int_commandId
- * @param int_deviceId
- * @param intUserId valid if of a user
- */
- function executeCommand($intCommandId, $intDeviceId, $intUserId) {
- // make sure that all the required parameters are supplied
- if(!isset($this->objDB)){
- die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
- }
- if(!isset($intCommandId)){
- die(__CLASS__."::".__FUNCTION__."():"."intCommandId is not set.");
- }
- if(!isset($intDeviceId)){
- die(__CLASS__."::".__FUNCTION__."():"."intDeviceId is not set.");
- }
- if(!isset($intUserId)){
- die(__CLASS__."::".__FUNCTION__."():"."intUserId is not set.");
- }
- // query to retrieve the command information
- $strSql = "SELECT Commands.Name AS CommandName, "
- ."Commands.Type, Commands.Action, "
- ."Devices.Name AS DeviceName, "
- ."Devices.Address AS DeviceAddress, Devices.Count, "
- ."Users.LastName, Users.FirstName, Users.Server, "
- ."Users.Address AS SourceAddress "
- ."FROM Commands, Devices, Users "
- ."WHERE Commands.DeviceId = Devices.DeviceId "
- ."AND Commands.OwnerId = Users.UserId "
- ."AND Commands.OwnerId = Devices.OwnerId "
- ."AND Commands.CommandId = ? AND Commands.OwnerId = ?;";
- // prepare the queries together
- $arrData = array($intCommandId, $intUserId);
- $resResult =& $this->objDB->query($strSql, $arrData);
- // check that result is not an error
- if (DB::isError($resResult)) {
- die(__CLASS__."::".__FUNCTION__."():".$resResult->getMessage());
- }
- $objRow =& $resResult->fetchRow(DB_FETCHMODE_OBJECT);
- // exit if the command type is not supported
- if($objRow->Type != 0) {
- die(__CLASS__."::".__FUNCTION__."():"
- ."Command type ".$objRow->Type." is not supported");
- }
- // query to update the message count for the device
- $strSql = "UPDATE Devices SET Count = Count + 1 Where DeviceId = ?;";
- $resResult =& $this->objDB->query($strSql, $intDeviceId);
- // check that result is not an error
- if (DB::isError($resResult)) {
- die(__CLASS__."::".__FUNCTION__."():".$resResult->getMessage());
- }
- //create the sip message
- $strSipMessage = "MESSAGE sip:".$objRow->DeviceAddress." SIP/2.0\n"
- ."Via: SIP/2.0/\$T \$H:\$P\n"
- ."From: ".$objRow->FirstName." ".$objRow->LastName
- ." <sip:".$objRow->SourceAddress.">\n"
- ."To: ".$objRow->DeviceName
- ." <sip:".$objRow->DeviceAddress.">\n"
- ."CSeq: ".$objRow->Count." MESSAGE\n"
- ."Call-ID: \$C@\$H\n"
- ."Content-Type: text/plain\n"
- ."Content-Length: ".strlen($objRow->Action)."\n\n"
- .$objRow->Action."\n\n";
- // create the message file
- $strMessageFile = "sip_message.txt";
- $resFp = fopen($strMessageFile, "w+");
- // do an exclusive lock
- if (flock($resFp, LOCK_EX)) {
- fwrite($resFp, $strSipMessage);
- // release the lock
- flock($resFp, LOCK_UN);
- } else {
- die("Phone2Dev::executeCommand(): couldn't lock the messsage file.");
- }
- fclose($resFp);
- // send the sip message
- $strOutput = shell_exec("env -i siptc -s ".$objRow->Server
- ." -n ".$strMessageFile." 2>&1");
- /*
- * attempt to send the sip message through STDIN
- *
- $str_sipMessage = "MESSAGE sip:".$obj_row->DeviceAddress." SIP/2.0\\n"
- ."Via: SIP/2.0/\$T \$H:\$P\\n"
- ."From: ".$obj_row->FirstName." ".$obj_row->LastName
- ." <sip:".$obj_row->SourceAddress.">\\n"
- ."To: ".$obj_row->DeviceName
- ." <sip:".$obj_row->DeviceAddress.">\\n"
- ."CSeq: ".$obj_row->Count." MESSAGE\\n"
- ."Call-ID: \$C@\$H\\n"
- ."Content-Type: text/plain\\n"
- ."Content-Length: ".strlen($obj_row->Action)."\\n\\n"
- .$obj_row->Action."\\n\\n";
- print "<p>". $str_sipMessage."</p>";
- print("<p>env -i ksh print \"$str_sipMessage\">output.txt </p>");
- // send the sip message
- $str_output = shell_exec("env -i ksh print \"$str_sipMessage\">output.txt ");
- print $str_output."\n";
- */
- return $strOutput;
- }
- }
- ?>
Documentation generated on Tue, 4 Jan 2005 13:48:04 -0500 by phpDocumentor 1.3.0RC3