Source of state-sqlite.php:
  1. <?
  2. // vim: ts=4:sw=4:nu:fdc=4
  3.  
  4. // get posted values
  5. $cmd = isset($_POST["cmd"]) ? $_POST["cmd"] : false;
  6. $clientArgs->id = isset($_POST["id"]) ? $_POST["id"] : 1;
  7. $clientArgs->user = isset($_POST["user"]) ? $_POST["user"] : "jozo";
  8. $clientArgs->session = isset($_POST["session"]) ? $_POST["session"] : "session";
  9. $clientArgs->data = isset($_POST["data"]) ? json_decode($_POST["data"]) : array();
  10.  
  11. // get variables and connection to sqlite
  12. $stateFile = "state.sqlite";
  13. $DSN="sqlite:" . realpath(".") . "/$stateFile";
  14. $odb = new PDO("$DSN");
  15. $odb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  16.  
  17. createTable($odb);
  18.  
  19. if(!$cmd) {
  20. echo '{"success":false,"error":"No command"}';
  21. exit;
  22. }
  23.  
  24. // execute command
  25. $cmd($odb, $clientArgs);
  26. exit;
  27.  
  28. // {{{
  29. /**
  30. * readState: reads state
  31. *
  32. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  33. * @date 24. March 2008
  34. * @return void
  35. * @param PDO $odb
  36. * @param object $clientArgs
  37. */
  38. function readState($odb, $clientArgs) {
  39. $sql =
  40. "select name,value from state where "
  41. ."id={$clientArgs->id} and user='{$clientArgs->user}' and session='{$clientArgs->session}'"
  42. ;
  43. try {
  44. $ostmt = $odb->query($sql);
  45. $data = $ostmt->fetchAll(PDO::FETCH_OBJ);
  46. }
  47. catch(PDOException $e) {
  48. echo "{\"success\":false,\"error\":\"$e\"}";
  49. exit;
  50. }
  51.  
  52. $o = array(
  53. "success"=>true
  54. ,"data"=>json_encode($data)
  55. );
  56. echo json_encode($o);
  57. } // eo function readState
  58. // }}}
  59. // {{{
  60. /**
  61. * saveState: saves state
  62. *
  63. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  64. * @date 24. March 2008
  65. * @return void
  66. * @param PDO $odb
  67. * @param object $clientArgs
  68. */
  69. function saveState($odb, $clientArgs) {
  70. foreach($clientArgs->data as $row) {
  71. $sql =
  72. "replace into state (id,user,session,name,value) values"
  73. ." ({$clientArgs->id},'{$clientArgs->user}','{$clientArgs->session}','{$row->name}','{$row->value}')"
  74. ;
  75. try {
  76. $odb->exec($sql);
  77. }
  78. catch(PDOException $e) {
  79. echo "{\"success\":false,\"error\":\"$e\"}";
  80. exit;
  81. }
  82. }
  83. echo '{"success":true}';
  84. } // eo function saveState
  85. // }}}
  86. // {{{
  87. /**
  88. * createTable: create state table if it doesn't exist
  89. *
  90. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  91. * @date 24. March 2008
  92. * @param PDO $odb
  93. * @return void
  94. */
  95. function createTable($odb) {
  96. // check if table exists
  97. $ostmt = $odb->query("select name from sqlite_master where type='table' and name='state'");
  98. $table = $ostmt->fetchAll(PDO::FETCH_NUM);
  99. if(!sizeof($table)) {
  100. // create table
  101. $sql =
  102. "create table state"
  103. ."(id integer"
  104. .",user varchar(40)"
  105. .",session varchar(80)"
  106. .",name varchar(80)"
  107. .",value text"
  108. .")"
  109. ;
  110. $odb->exec($sql);
  111.  
  112. // create unique index
  113. $sql = "create unique index idx on state(id,user,session,name)";
  114. $odb->exec($sql);
  115. }
  116. } // eo function createTable
  117. // }}}
  118.  
  119. // eof
  120. ?>