Source for file phone2dev.php

Documentation is available at phone2dev.php

  1. <?php
  2. /**
  3. * Phone2Dev Class
  4. *
  5. * Implements Phone2Dev class which abstracts database access
  6. * to web and voice interfaces
  7. * @package Phone2Dev
  8. * @author Stanislav Miroshnikov
  9. */
  10. /**
  11. * Requires PEAR DB package
  12. */
  13. require_once 'DB.php';
  14. /**
  15. * Phone2Dev Class
  16. * @package Phone2Dev
  17. * @author Stanislav Miroshnikov
  18. */
  19. class Phone2Dev {
  20. /**
  21. * DB handle
  22. */
  23. var $objDB;
  24.  
  25. /**
  26. * Connects the class instance to the database
  27. */
  28. function dbConnect(){
  29. // this have to be changed by hand
  30. $strDsn = 'mysql://stan@tcp(disco.cs.columbia.edu:3316)/devices';
  31. $arrOptions = array('debug' => 2);
  32. $this->objDB =& DB::connect($strDsn, $arrOptions);
  33.  
  34. // check for connection error
  35. if (DB::isError($this->objDB)) {
  36. die(__CLASS__."::".__FUNCTION__."():".$this->objDB->getMessage());
  37. }
  38. }
  39.  
  40. /**
  41. * Diconnects the class instance from the database
  42. */
  43. function dbDisconnect(){
  44. $this->objDB->disconnect();
  45. }
  46.  
  47. /**
  48. * Authenticates using a phone number and pin
  49. * @param strPhone user's phone number
  50. * @param strPin user's pin number
  51. * @returns integer|-1 containing the userId if user was authenticated
  52. * correctly, else returns -1
  53. */
  54. function authPhonePin($strPhone, $strPin) {
  55. // make sure that all the required parameters are supplied
  56. if(!isset($strPhone)) {
  57. die(__CLASS__."::".__FUNCTION__."():"."strPhone is not set.");
  58. }
  59. if(!isset($strPin)) {
  60. die(__CLASS__."::".__FUNCTION__."():"."strPin is not set.");
  61. }
  62. if(!isset($this->objDB)) {
  63. die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
  64. }
  65.  
  66. // get userId query
  67. $strSql = 'SELECT UserId FROM Users WHERE Phone = ? AND Pin = ?';
  68. $arr_data = array($strPhone, md5($strPin));
  69. $obj_res =& $this->objDB->query($strSql, $arr_data);
  70.  
  71. // check that result has no errors
  72. if(DB::isError($obj_res)) {
  73. die(__CLASS__."::".__FUNCTION__."():".$obj_res->getMessage());
  74. }
  75.  
  76. $intUserId = -1;
  77. // if the phone number and pin combination exists, get the username
  78. if($arr_row =& $obj_res->fetchRow()) {
  79. // user was authenticated
  80. $intUserId = $arr_row[0];
  81. }
  82. return $intUserId;
  83. }
  84.  
  85. /**
  86. * Authenticates using a username and password
  87. * @param strUsername user's username
  88. * @param strPassword user's password
  89. * @returns an integer containing the user's id if user was authenticated
  90. * correctly, else returns -1
  91. */
  92. function authUserPass($strUsername, $strPassword) {
  93. // make sure that all the required parameters are supplied
  94. if(!isset($strUsername)) {
  95. die(__CLASS__."::".__FUNCTION__."():"."strUsername is not set.");
  96. }
  97. if(!isset($strPassword)) {
  98. die(__CLASS__."::".__FUNCTION__."():"."strPassword is not set.");
  99. }
  100. if(!isset($this->objDB)) {
  101. die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
  102. }
  103.  
  104. // get userId query
  105. $strSql = 'SELECT UserId FROM Users WHERE Username = ? AND Password = ?';
  106. $arrData = array($strUsername, md5($strPassword));
  107. $objResult =& $this->objDB->query($strSql, $arrData);
  108.  
  109. // check that result has no errors
  110. if(DB::isError($objResult)) {
  111. die($objResult->getMessage());
  112. }
  113.  
  114. $intUserId = -1;
  115. // if the phone number and pin combination exists, get the username
  116. if($arrRow =& $objResult->fetchRow()) {
  117. // user was authenticated
  118. $intUserId = $arrRow[0];
  119. }
  120. return $intUserId;
  121. }
  122.  
  123. /**
  124. * Checks where the username exists in the database
  125. * @param strUsername user's username
  126. * @return true|falseif the username exists
  127. */
  128. function userExists($strUsername) {
  129. // make sure that all the required parameters are supplied
  130. if(!isset($strUsername)) {
  131. die(__CLASS__."::".__FUNCTION__."():"."strUsername is not set.");
  132. }
  133. if(!isset($this->objDB)) {
  134. die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
  135. }
  136.  
  137. // get userId query
  138. $strSql = 'SELECT UserId FROM Users WHERE Username = ?';
  139. $objResult =& $this->objDB->query($strSql, $strUsername);
  140.  
  141. // check that result has no errors
  142. if(DB::isError($objResult)) {
  143. die($objResult->getMessage());
  144. }
  145. if($objResult->numRows() != 0) return true;
  146. return false;
  147. }
  148.  
  149. /**
  150. * Checks where the telephone exists
  151. * @param strPhone user's phone
  152. * @returns true if username exists, else false
  153. */
  154. function phoneExists($strPhone) {
  155. // make sure that all the required parameters are supplied
  156. if(!isset($strPhone)) {
  157. die(__CLASS__."::".__FUNCTION__."():"."strPhone is not set.");
  158. }
  159. if(!isset($this->objDB)) {
  160. die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
  161. }
  162.  
  163. // get userId query
  164. $strSql = 'SELECT UserId FROM Users WHERE Phone = ?';
  165. $objResult =& $this->objDB->query($strSql, $strPhone);
  166.  
  167. // check that result has no errors
  168. if(DB::isError($objResult)) {
  169. die($objResult->getMessage());
  170. }
  171. if($objResult->numRows() != 0) return true;
  172. return false;
  173. }
  174.  
  175. /**
  176. * Inserts the registration information into the database
  177. * @param strLastName validated last name
  178. * @param strFirstName validated first name
  179. * @param strUsername validated username
  180. * @param strPassword validated password
  181. * @param strPhone validated phone
  182. * @param strPin validated pin
  183. * @param strServer validated sip server
  184. * @param strAddress validated sip address
  185. */
  186. function registerUser($strLastName,
  187. $strFirstName,
  188. $strUsername,
  189. $strPassword,
  190. $strPhone,
  191. $strPin,
  192. $strServer,
  193. $strAddress) {
  194. // make sure that all the required parameters are supplied
  195. if(!isset($strLastName) ||
  196. !isset($strFirstName) ||
  197. !isset($strUsername) ||
  198. !isset($strPassword) ||
  199. !isset($strPhone) ||
  200. !isset($strPin) ||
  201. !isset($strServer) ||
  202. !isset($strAddress)) {
  203. die(__CLASS__."::".__FUNCTION__."():"
  204. ."one (or more) of the user parameters is not set.");
  205. }
  206. if(!isset($this->objDB)) {
  207. die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
  208. }
  209.  
  210. // insert user information
  211. $strSql = 'INSERT INTO Users VALUES(null, ?, ?, ?, ?, ?, ?, ?, ?, null)';
  212. $arrData = array($strLastName,
  213. $strFirstName,
  214. $strUsername,
  215. md5($strPassword),
  216. $strPhone,
  217. md5($strPin),
  218. $strServer,
  219. $strAddress);
  220. $objResult =& $this->objDB->query($strSql, $arrData);
  221.  
  222. // check that result has no errors
  223. if(DB::isError($objResult)) {
  224. die(__CLASS__."::".__FUNCTION__."():".$objResult->getMessage());
  225. }
  226.  
  227. // get the user id
  228. $intUserId = $this->getUserId($strUsername);
  229.  
  230. // insert main menu for the user
  231. $strSql = 'INSERT INTO Menus VALUES(null, ?, ?, 0, 0)';
  232. $arrData = array("Main Menu", $intUserId);
  233. $objResult =& $this->objDB->query($strSql, $arrData);
  234.  
  235. // check that result has no errors
  236. if(DB::isError($objResult)) {
  237. die(__CLASS__."::".__FUNCTION__."():".$objResult->getMessage());
  238. }
  239.  
  240. //set the menu location to itself, since its the main menu
  241. // insert main menu for the user
  242. $strSql = 'UPDATE Menus SET MenuLocationId = MenuId WHERE OwnerId = ?';
  243. $objResult =& $this->objDB->query($strSql, $intUserId);
  244.  
  245. // check that result has no errors
  246. if(DB::isError($objResult)) {
  247. die(__CLASS__."::".__FUNCTION__."():".$objResult->getMessage());
  248. }
  249. }
  250.  
  251. /**
  252. * Retrieves the userId for the user
  253. * @param strUsername a valid username contained in the database
  254. * @returns integer containing the userId
  255. */
  256. function getUserId($strUsername) {
  257. // make sure that all the required parameters are supplied
  258. if(!isset($strUsername)) {
  259. die(__CLASS__."::".__FUNCTION__."():"."strUsername is not set.");
  260. }
  261. if(!isset($this->objDB)) {
  262. die("Phone2Dev::getUserId(): objDB is not set.");
  263. }
  264.  
  265. // get userId query
  266. $strSql = 'SELECT UserId FROM Users WHERE Username = ?';
  267. $objResult =& $this->objDB->query($strSql, $strUsername);
  268.  
  269. // check that result has no errors
  270. if(DB::isError($objResult)) {
  271. die($objResult->getMessage());
  272. }
  273.  
  274. $intUserId = -1;
  275. if($arrRow =& $objResult->fetchRow()) {
  276. $intUserId = $arrRow[0];
  277. } else {
  278. die(__CLASS__."::".__FUNCTION__."():"."Username does"
  279. ." not exist in the database");
  280. }
  281.  
  282. return $intUserId;
  283. }
  284.  
  285. /**
  286. * Retrieves the userId for the user
  287. * @param strPhone a valid phone number in the database
  288. * @returns an integer containing the userId
  289. */
  290. function getUserIdByPhone($strPhone) {
  291. // make sure that all the required parameters are supplied
  292. if(!isset($strPhone)) {
  293. die(__CLASS__."::".__FUNCTION__."():"."strPhone is not set.");
  294. }
  295. if(!isset($this->objDB)) {
  296. die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
  297. }
  298.  
  299. // get userId query
  300. $strSql = 'SELECT UserId FROM Users WHERE Phone = ?';
  301. $objResult =& $this->objDB->query($strSql, $strPhone);
  302.  
  303. // check that result has no errors
  304. if(DB::isError($objResult)) {
  305. die($__CLASS__."::".__FUNCTION__."():".objResult->getMessage());
  306. }
  307.  
  308. $intUserId = -1;
  309. if($arrRow =& $objResult->fetchRow()) {
  310. $intUserId = $arrRow[0];
  311. } else {
  312. die("Phone2Dev::getUserIdByPhone(): This phone number does"
  313. ." not exist in the database.");
  314. }
  315.  
  316. return $intUserId;
  317. }
  318.  
  319.  
  320. function getMainMenuId($intUserId) {
  321. // make sure that all the required parameters are supplied
  322. if(!isset($intUserId)) {
  323. die(__CLASS__."::".__FUNCTION__."():"."intUserId is not set");
  324. }
  325. if(!isset($this->objDB)) {
  326. die(__CLASS__."::".__FUNCTION__."():"."objDB is not set");
  327. }
  328. // get commands sql query
  329. $strSql = 'SELECT MenuId, MenuLocationId FROM Menus WHERE OwnerId = ?';
  330. $intOwnerId = $intUserId;
  331. $objResult =& $this->objDB->query($strSql, $intOwnerId);
  332. // check that result is not an error
  333. if (DB::isError($objResult)) {
  334. die(__CLASS__."::".__FUNCTION__."():".$objResult->getMessage());
  335. }
  336. // find the user's main menu
  337. $intMainMenuId = -1;
  338. while ($arrRow =& $objResult->fetchRow()) {
  339. if($arrRow[0] == $arrRow[1]) {
  340. $intMainMenuId = $arrRow[0];
  341. break;
  342. }
  343. }
  344.  
  345. return $intMainMenuId;
  346. }
  347.  
  348. /**
  349. * Returns sub menus of the given menu
  350. * @param intMenuId valid id of a menu
  351. * @param intUserId valid if of a user
  352. * @returns array sub menu id's as keys and menu names as values
  353. * @todo return sub menu array with menu order as a key so it
  354. * can be combined and sorted with the command array
  355. */
  356. function getSubMenus($intMenuId, $intUserId) {
  357. // make sure that all the required parameters are supplied
  358. if(!isset($this->objDB)){
  359. die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
  360. }
  361. if(!isset($intMenuId)){
  362. die(__CLASS__."::".__FUNCTION__."():"."intMenuId is not set.");
  363. }
  364. if(!isset($intUserId)){
  365. die(__CLASS__."::".__FUNCTION__."():"."intUserId is not set.");
  366. }
  367. // display all menus located in that menu
  368. $strSql = "SELECT MenuId AS intSubMenuId, Name AS strMenuName FROM Menus "
  369. ."WHERE MenuLocationId = ? AND OwnerId = ?";
  370. $arrData = array($intMenuId, $intUserId);
  371. $arrResult =& $this->objDB->getAssoc($strSql, false, $arrData,
  372. DB_FETCHMODE_ASSOC);
  373.  
  374. if (DB::isError($arrResult)) {
  375. die(__CLASS__."::".__FUNCTION__."():".$arrResult->getMessage());
  376. }
  377.  
  378. return $arrResult;
  379. }
  380.  
  381. /**
  382. * Returns commands of the given menu
  383. * @param intMenuId valid id of a menu
  384. * @param intUserId valid if of a user
  385. * @returns array commandId as keys and
  386. * an associate array of command information as value
  387. * @todo return commands array with menu order as a key so it
  388. * can be combined and sorted with the sub menu array
  389. */
  390. function getCommands($intMenuId, $intUserId) {
  391. // make sure that all the required parameters are supplied
  392. if(!isset($this->objDB)){
  393. die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
  394. }
  395. if(!isset($intMenuId)){
  396. die(__CLASS__."::".__FUNCTION__."():"."intMenuId is not set.");
  397. }
  398. if(!isset($intUserId)){
  399. die(__CLASS__."::".__FUNCTION__."():"."intUserId is not set.");
  400. }
  401.  
  402. // get commands for the menu
  403. $strSql = "SELECT CommandId, Commands.Name AS strComName, "
  404. ."Devices.Name AS strDeviceName, "
  405. ."Devices.DeviceId AS intDeviceId "
  406. ."FROM Commands, Devices "
  407. ."WHERE Commands.DeviceId = Devices.DeviceId "
  408. ."AND Commands.MenuLocationId = ? AND Commands.OwnerId = ?";
  409. $arrData = array($intMenuId, $intUserId);
  410. // try to retrieve the associative array of objects
  411. $arrResult =& $this->objDB->getAssoc($strSql, true, $arrData,
  412. DB_FETCHMODE_ASSOC);
  413.  
  414. if (DB::isError($arrResult)) {
  415. die(__CLASS__."::".__FUNCTION__."():".$arrResult->getMessage());
  416. }
  417.  
  418. return $arrResult;
  419. }
  420.  
  421. /**
  422. * Executes the user command
  423. * @param int_commandId
  424. * @param int_deviceId
  425. * @param intUserId valid if of a user
  426. */
  427. function executeCommand($intCommandId, $intDeviceId, $intUserId) {
  428. // make sure that all the required parameters are supplied
  429. if(!isset($this->objDB)){
  430. die(__CLASS__."::".__FUNCTION__."():"."objDB is not set.");
  431. }
  432. if(!isset($intCommandId)){
  433. die(__CLASS__."::".__FUNCTION__."():"."intCommandId is not set.");
  434. }
  435. if(!isset($intDeviceId)){
  436. die(__CLASS__."::".__FUNCTION__."():"."intDeviceId is not set.");
  437. }
  438. if(!isset($intUserId)){
  439. die(__CLASS__."::".__FUNCTION__."():"."intUserId is not set.");
  440. }
  441. // query to retrieve the command information
  442. $strSql = "SELECT Commands.Name AS CommandName, "
  443. ."Commands.Type, Commands.Action, "
  444. ."Devices.Name AS DeviceName, "
  445. ."Devices.Address AS DeviceAddress, Devices.Count, "
  446. ."Users.LastName, Users.FirstName, Users.Server, "
  447. ."Users.Address AS SourceAddress "
  448. ."FROM Commands, Devices, Users "
  449. ."WHERE Commands.DeviceId = Devices.DeviceId "
  450. ."AND Commands.OwnerId = Users.UserId "
  451. ."AND Commands.OwnerId = Devices.OwnerId "
  452. ."AND Commands.CommandId = ? AND Commands.OwnerId = ?;";
  453. // prepare the queries together
  454. $arrData = array($intCommandId, $intUserId);
  455. $resResult =& $this->objDB->query($strSql, $arrData);
  456. // check that result is not an error
  457. if (DB::isError($resResult)) {
  458. die(__CLASS__."::".__FUNCTION__."():".$resResult->getMessage());
  459. }
  460. $objRow =& $resResult->fetchRow(DB_FETCHMODE_OBJECT);
  461. // exit if the command type is not supported
  462. if($objRow->Type != 0) {
  463. die(__CLASS__."::".__FUNCTION__."():"
  464. ."Command type ".$objRow->Type." is not supported");
  465. }
  466. // query to update the message count for the device
  467. $strSql = "UPDATE Devices SET Count = Count + 1 Where DeviceId = ?;";
  468. $resResult =& $this->objDB->query($strSql, $intDeviceId);
  469.  
  470. // check that result is not an error
  471. if (DB::isError($resResult)) {
  472. die(__CLASS__."::".__FUNCTION__."():".$resResult->getMessage());
  473. }
  474.  
  475. //create the sip message
  476. $strSipMessage = "MESSAGE sip:".$objRow->DeviceAddress." SIP/2.0\n"
  477. ."Via: SIP/2.0/\$T \$H:\$P\n"
  478. ."From: ".$objRow->FirstName." ".$objRow->LastName
  479. ." <sip:".$objRow->SourceAddress.">\n"
  480. ."To: ".$objRow->DeviceName
  481. ." <sip:".$objRow->DeviceAddress.">\n"
  482. ."CSeq: ".$objRow->Count." MESSAGE\n"
  483. ."Call-ID: \$C@\$H\n"
  484. ."Content-Type: text/plain\n"
  485. ."Content-Length: ".strlen($objRow->Action)."\n\n"
  486. .$objRow->Action."\n\n";
  487.  
  488. // create the message file
  489. $strMessageFile = "sip_message.txt";
  490. $resFp = fopen($strMessageFile, "w+");
  491. // do an exclusive lock
  492. if (flock($resFp, LOCK_EX)) {
  493. fwrite($resFp, $strSipMessage);
  494. // release the lock
  495. flock($resFp, LOCK_UN);
  496. } else {
  497. die("Phone2Dev::executeCommand(): couldn't lock the messsage file.");
  498. }
  499. fclose($resFp);
  500.  
  501. // send the sip message
  502. $strOutput = shell_exec("env -i siptc -s ".$objRow->Server
  503. ." -n ".$strMessageFile." 2>&1");
  504. /*
  505. * attempt to send the sip message through STDIN
  506. *
  507. $str_sipMessage = "MESSAGE sip:".$obj_row->DeviceAddress." SIP/2.0\\n"
  508. ."Via: SIP/2.0/\$T \$H:\$P\\n"
  509. ."From: ".$obj_row->FirstName." ".$obj_row->LastName
  510. ." <sip:".$obj_row->SourceAddress.">\\n"
  511. ."To: ".$obj_row->DeviceName
  512. ." <sip:".$obj_row->DeviceAddress.">\\n"
  513. ."CSeq: ".$obj_row->Count." MESSAGE\\n"
  514. ."Call-ID: \$C@\$H\\n"
  515. ."Content-Type: text/plain\\n"
  516. ."Content-Length: ".strlen($obj_row->Action)."\\n\\n"
  517. .$obj_row->Action."\\n\\n";
  518.  
  519. print "<p>". $str_sipMessage."</p>";
  520.  
  521. print("<p>env -i ksh print \"$str_sipMessage\">output.txt </p>");
  522. // send the sip message
  523. $str_output = shell_exec("env -i ksh print \"$str_sipMessage\">output.txt ");
  524. print $str_output."\n";
  525. */
  526. return $strOutput;
  527. }
  528. }
  529. ?>

Documentation generated on Tue, 4 Jan 2005 13:48:04 -0500 by phpDocumentor 1.3.0RC3