Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?PHP
  2. session_start();
  3. $allowedIDs = array(
  4. 'https://www.google.com/accounts/o8/id?id=AItOawk9b32oBoVvjzwEhvC2GOhsxj0MN2mWoc8', // Me
  5. 'https://www.google.com/accounts/o8/id?id=AItOawk8LzSeazeZxqMCTVKm-OkUu0mLDLOqBBs', // Simon
  6. 'https://www.google.com/accounts/o8/id?id=AItOawkM5hzpAq2WOz0DDy3N9PnEtqinHbgNrNI', // Shane
  7. );
  8. if (isset($_SESSION['openid']['error'])) {
  9. // Failed OpenID login attempt
  10. echo 'ERROR: ', htmlentities($_SESSION['openid']['error']);
  11. unset($_SESSION['openid']['error']);
  12. exit;
  13. } else if (isset($_SESSION['openid']['validated']) && $_SESSION['openid']['validated']) {
  14. if (!in_array($_SESSION['openid']['identity'], $allowedIDs)) {
  15. echo 'ERROR: ', htmlentities($_SESSION['openid']['identity']), ' not permitted';
  16. exit;
  17. }
  18. } else {
  19. if (!isset($_REQUEST['openid_mode'])) {
  20. $_POST['openid_url'] = 'https://www.google.com/accounts/o8/id';
  21. }
  22. require('openid/processor.php');
  23. exit;
  24. }
  25. # -------------- End of authentication code --------------------------
  26. require('common.php');
  27. # -------------- Form handling -----------------
  28. function process_activity_add($args) {
  29. $sql = 'INSERT INTO activities (activity_name, activity_parent) VALUES (\'';
  30. $sql .= m($args['name']) . '\', ' . ((int) $args['parent']) . ')';
  31. mysql_query($sql);
  32. }
  33. function process_activity_delete($args) {
  34. $sql = 'DELETE FROM windowclassifications WHERE activity_id = ' . ((int) $args['id']);
  35. mysql_query($sql);
  36. $sql = 'SELECT activity_id FROM activities WHERE activity_parent = ' . ((int) $args['id']);
  37. $res = mysql_query($sql);
  38. while ($row = mysql_fetch_assoc($res)) {
  39. process_activity_delete(array('id' => $row['activity_id']));
  40. }
  41. $sql = 'DELETE FROM activities WHERE activity_id = ' . ((int) $args['id']);
  42. mysql_query($sql);
  43. }
  44. function process_sample_edit($args) {
  45. $sql = 'SELECT wc_id, activity_id, log_id, wc_offset FROM windowclassifications';
  46. $res = mysql_query($sql);
  47. $acs = getActivityArray();
  48. while ($row = mysql_fetch_assoc($res)) {
  49. $name = 'value_' . $row['log_id'] . '_' . $row['wc_offset'];
  50. if (isset($args[$name])) {
  51. if ((int) $args[$name] == (int) $row['activity_id']) {
  52. unset($args[$name]);
  53. } else {
  54. mysql_query('UPDATE windowclassifications SET activity_id = ' . ((int) $args[$name]) . ' WHERE wc_id = ' . $row['wc_id']);
  55. }
  56. }
  57. }
  58. foreach ($args as $name => $val) {
  59. if ($acs[$val] == 'UNCLASSIFIED/PENDING') { continue; }
  60. list($value, $log, $offset) = explode('_', $name);
  61. mysql_query('INSERT INTO windowclassifications (activity_id, log_id, wc_offset) VALUES ('. ((int) $val) . ', ' . ((int) $log) . ', ' . ((int) $offset) . ')');
  62. }
  63. }
  64. function process_export($args) {
  65. $acs = getActivityArray();
  66. $sql = 'SELECT wc_offset, activity_id, log_data FROM activities NATURAL JOIN windowclassifications NATURAL JOIN sensorlogger WHERE log_statuscode = 1 AND (0';
  67. foreach ($acs as $id => $name) {
  68. if (substr($name, 0, 10) == 'CLASSIFIED') {
  69. $sql .= ' OR activity_id = ' . $id;
  70. }
  71. }
  72. $sql .= ')';
  73. $res = mysql_query($sql);
  74. header('Content-type: text/plain');
  75. header('Content-disposition: attachment; filename=data.txt');
  76. while ($row = mysql_fetch_assoc($res)) {
  77. echo 'Activity: ', $acs[$row['activity_id']], "\n";
  78. $data = array_slice(explode("\n", $row['log_data']), $row['wc_offset'], 128);
  79. echo implode("\n", $data);
  80. echo "\n";
  81. }
  82. exit;
  83. }
  84. if (isset($_POST['action'])) {
  85. $args = array();
  86. $action = str_replace('.', '_', $_POST['action']) . '_';
  87. foreach ($_POST as $k => $v) {
  88. if (substr($k, 0, strlen($action)) == $action) {
  89. $args[substr($k, strlen($action))] = $v;
  90. }
  91. }
  92. call_user_func('process_' . str_replace('.', '_', $_POST['action']), $args);
  93. header('Location: /android/admin.php');
  94. exit;
  95. }
  96. # ------------------- End of form handling ----------------------
  97. $acs = getActivityArray();
  98. ?>
  99. <h1>Activity management</h1>
  100. <h2>Add an activity</h2>
  101. <form action="admin.php" method="post">
  102. <input type="hidden" name="action" value="activity.add">
  103. <select name="activity.add.parent">
  104. <?PHP
  105. asort($acs);
  106. foreach ($acs as $id => $name) {
  107. echo ' <option value="', $id, '">', htmlentities($name), '</option>';
  108. }
  109. ?>
  110. </select> /
  111. <input type="text" name="activity.add.name">
  112. <input type="submit" value="Add">
  113. </form>
  114. <h2>Delete an activity</h2>
  115. <form action="admin.php" method="post">
  116. <input type="hidden" name="action" value="activity.delete">
  117. <select name="activity.delete.id">
  118. <?PHP
  119. asort($acs);
  120. foreach ($acs as $id => $name) {
  121. echo ' <option value="', $id, '">', htmlentities($name), '</option>';
  122. }
  123. ?>
  124. <input type="submit" value="Delete">
  125. </form>
  126. <h1>Export</h1>
  127. <form action="admin.php" method="post">
  128. <input type="hidden" name="action" value="export">
  129. <input type="submit" value="Export classified windows">
  130. </form>
  131. <h1>Sample management</h1>
  132. <?PHP
  133. $sql = 'SELECT log_id, log_imei, log_version, log_time, log_activity, log_headers, log_data FROM sensorlogger WHERE log_statuscode = 1';
  134. $res = mysql_query($sql);
  135. ?>
  136. <style type="text/css">
  137. .windowed { background: url('windowbg.png') repeat-y -256px 0px; }
  138. .windowboxes { margin: 0px; padding: 0px; border-right: 1px solid black; display: inline-block; }
  139. .windowboxes li { display: inline-block; width: 255px; text-align: center; border: 1px solid black; margin: 0px; padding: 0px; border-right: 0; }
  140. .windowboxes.odd { padding-left: 128px; }
  141. .windowboxes li select { width: 253px; }
  142. </style>
  143. <script type="text/javascript">
  144. function showWindow(id, offset) {
  145. document.getElementById('window_' + id).style.backgroundPosition = (offset * 2) + "px 0px";
  146. }
  147. function hideWindow(id) {
  148. showWindow(id, -128);
  149. }
  150. function classifyAll(select) {
  151. var tr = select.parentNode.parentNode;
  152. var selects = tr.getElementsByTagName('select');
  153. for (var i = 0; i < selects.length; i++) {
  154. selects[i].value = select.value;
  155. }
  156. }
  157. </script>
  158. <form action="admin.php" method="post">
  159. <input type="hidden" name="action" value="sample.edit">
  160. <?PHP
  161. echo '<table border="1">';
  162. while ($row = mysql_fetch_assoc($res)) {
  163. $sql2 = 'SELECT wc_offset, activity_id FROM windowclassifications WHERE log_id = ' . $row['log_id'];
  164. $res2 = mysql_query($sql2);
  165. $wcs = array();
  166. while ($row2 = mysql_fetch_assoc($res2)) {
  167. $wcs[(int) $row2['wc_offset']] = (int) $row2['activity_id'];
  168. }
  169. $points = 0;
  170. echo '<tr><td><table>';
  171. foreach ($row as $k => $v) { echo '<tr><th>', $k, '</th><td>', $k == 'log_data' ? ($points = count(explode("\n", $v))) . ' line(s)' : nl2br(htmlentities($v)), '</td></tr>'; }
  172. echo '</table>';
  173. echo '<select onChange="classifyAll(this)">';
  174. echo ' <option value="">Classify all as...</option>';
  175. foreach ($acs as $id => $name) {
  176. echo '<option value="', $id, '">', htmlentities($name), '</option>';
  177. }
  178. echo '</select>';
  179. echo '</td><td>';
  180. echo '<ol class="windowboxes even">';
  181. for ($i = 0; $i + 128 < $points; $i += 128) {
  182. echo '<li onMouseOver="showWindow(', $row['log_id'], ', ', $i, ')" onMouseOut="hideWindow(', $row['log_id'], ')">';
  183. echo '<select name="sample.edit.value_', $row['log_id'], '_', $i, '">';
  184. foreach ($acs as $id => $name) {
  185. echo '<option value="', $id, '"', (isset($wcs[$i]) && $wcs[$i] == $id) || (!isset($wcs[$i]) && $name == 'UNCLASSIFIED/PENDING') ? ' selected="selected"' : '','>', htmlentities($name), '</option>';
  186. }
  187. echo '</select>';
  188. echo '</li>';
  189. }
  190. echo '</ol>';
  191. echo '<div class="windowed" id="window_', $row['log_id'], '" styleb"background-color: orange;">';
  192. echo '<img src="data.php?graph=', $row['log_id'], '&amp;ds=1&amp;imei=', $row['log_imei'], '" height="330">';
  193. echo '<br><img src="data.php?graph=', $row['log_id'], '&amp;ds=2&amp;imei=', $row['log_imei'], '" height="330">';
  194. echo '</div>';
  195. echo '<ol class="windowboxes odd">';
  196. for ($i = 64; $i + 128 < $points; $i += 128) {
  197. echo '<li onMouseOver="showWindow(', $row['log_id'], ', ', $i, ')" onMouseOut="hideWindow(', $row['log_id'], ')">';
  198. echo '<select name="sample.edit.value_', $row['log_id'], '_', $i, '">';
  199. foreach ($acs as $id => $name) {
  200. echo '<option value="', $id, '"', (isset($wcs[$i]) && $wcs[$i] == $id) || (!isset($wcs[$i]) && $name == 'UNCLASSIFIED/PENDING') ? ' selected="selected"' : '','>', htmlentities($name), '</option>';
  201. }
  202. echo '</select>';
  203. echo '</li>';
  204. }
  205. echo '</ol>';
  206. echo '</td>';
  207. echo '</tr>';
  208. }
  209. echo '</table>';
  210. ?>
  211. <input type="submit" value="SUBMIT ALL MODIFICATIONS">
  212. </form>