api.php 5.52 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php include_once('include/presets.php');?>
<?php
  if (isset($_POST['q'])) {
    $status = array('online');
    $userrole = get_user_role($_POST['username']);
    if ($userrole == 'internal') {
      $status[] = 'internal';
    }
    if ($userrole == 'admin') {
      $status[] = 'develop';
      $status[] = 'internal';
    }
    $status = "('" .join("', '", $status) ."')";

15
    if ($_POST['q'] == 'obs' && isset($_POST['platform'])) {
16
      $db = db_connect();
Reto Da Forno's avatar
Reto Da Forno committed
17
      $platform = strtolower(mysqli_real_escape_string($db, $_POST['platform']));
18
19
20
21
22
23
24
25
26
27
      // return a list of the currently available observers
      $sql = "SELECT obs.observer_id AS obsid FROM flocklab.tbl_serv_observer AS obs
              LEFT JOIN flocklab.tbl_serv_tg_adapt_list AS a ON obs.slot_1_tg_adapt_list_fk = a.serv_tg_adapt_list_key
              LEFT JOIN flocklab.tbl_serv_tg_adapt_types AS slot1 ON a.tg_adapt_types_fk = slot1.serv_tg_adapt_types_key
              LEFT JOIN flocklab.tbl_serv_tg_adapt_list AS b ON obs.slot_2_tg_adapt_list_fk = b.serv_tg_adapt_list_key
              LEFT JOIN flocklab.tbl_serv_tg_adapt_types AS slot2 ON b.tg_adapt_types_fk = slot2.serv_tg_adapt_types_key
              LEFT JOIN flocklab.tbl_serv_tg_adapt_list AS c ON obs.slot_3_tg_adapt_list_fk = c.serv_tg_adapt_list_key
              LEFT JOIN flocklab.tbl_serv_tg_adapt_types AS slot3 ON c.tg_adapt_types_fk = slot3.serv_tg_adapt_types_key
              LEFT JOIN flocklab.tbl_serv_tg_adapt_list AS d ON obs.slot_4_tg_adapt_list_fk = d.serv_tg_adapt_list_key
              LEFT JOIN flocklab.tbl_serv_tg_adapt_types AS slot4 ON d.tg_adapt_types_fk = slot4.serv_tg_adapt_types_key
Reto Da Forno's avatar
Reto Da Forno committed
28
              WHERE obs.status IN $status AND '$platform' IN (LOWER(slot1.name), LOWER(slot2.name), LOWER(slot3.name), LOWER(slot4.name))
29
30
31
32
33
34
              ORDER BY obs.observer_id;";
      $res = mysqli_query($db, $sql);
      if (!$res) {
        echo json_encode(array('status' => 'error', 'output' => mysqli_error($db)));
      }
      else {
35
36
        $output = implode(" ", array_column(mysqli_fetch_all($res, MYSQLI_ASSOC), "obsid"));
        echo json_encode(array('status' => 'ok', 'output' => $output));
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
      }
    }
    else if ($_POST['q'] == 'platform') {
      $db = db_connect();
      $sql = "SELECT obs.observer_id, slot1.name AS name1, slot1.description AS desc1,
              slot2.name AS name2, slot2.description AS desc2,
              slot3.name AS name3, slot3.description AS desc3,
              slot4.name AS name4, slot4.description AS desc4
              FROM `flocklab`.`tbl_serv_observer` AS obs
              LEFT JOIN `flocklab`.`tbl_serv_tg_adapt_list` AS a ON obs.slot_1_tg_adapt_list_fk = a.serv_tg_adapt_list_key
              LEFT JOIN `flocklab`.`tbl_serv_tg_adapt_types` AS slot1 ON a.tg_adapt_types_fk = slot1.serv_tg_adapt_types_key
              LEFT JOIN `flocklab`.`tbl_serv_tg_adapt_list` AS b ON obs.slot_2_tg_adapt_list_fk = b.serv_tg_adapt_list_key
              LEFT JOIN `flocklab`.`tbl_serv_tg_adapt_types` AS slot2 ON b.tg_adapt_types_fk = slot2.serv_tg_adapt_types_key
              LEFT JOIN `flocklab`.`tbl_serv_tg_adapt_list` AS c ON obs.slot_3_tg_adapt_list_fk = c.serv_tg_adapt_list_key
              LEFT JOIN `flocklab`.`tbl_serv_tg_adapt_types` AS slot3 ON c.tg_adapt_types_fk = slot3.serv_tg_adapt_types_key
              LEFT JOIN `flocklab`.`tbl_serv_tg_adapt_list` AS d ON obs.slot_4_tg_adapt_list_fk = d.serv_tg_adapt_list_key
              LEFT JOIN `flocklab`.`tbl_serv_tg_adapt_types` AS slot4 ON d.tg_adapt_types_fk = slot4.serv_tg_adapt_types_key
              WHERE obs.status IN $status
              ORDER BY obs.observer_id;";
      $res = mysqli_query($db, $sql);
      if (!$res) {
        echo json_encode(array('status' => 'error', 'output' => mysqli_error($db)));
      }
      else {
        $output = [];
        while ($row = mysqli_fetch_assoc($res)) {
          $output[] = $row['name1'];
          $output[] = $row['name2'];
          $output[] = $row['name3'];
          $output[] = $row['name4'];
        }
        echo json_encode(array('status' => 'ok', 'output' => join(" ", array_unique(array_filter($output)))));
      }
    }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    else if ($_POST['q'] == 'testinfo') {
      if (!isset($_POST['id'])) {
        echo json_encode(array('status' => 'error', 'output' => 'no test ID specified'));
        exit();
      }
      $db = db_connect();
      $sql = "SELECT title, description, test_status as status, UNIX_TIMESTAMP(time_start_wish) AS start_planned, UNIX_TIMESTAMP(time_start_act) AS start_act, UNIX_TIMESTAMP(time_end_wish) AS end_planned, UNIX_TIMESTAMP(time_end_act) AS end_act
              FROM `flocklab`.`tbl_serv_tests`
              WHERE serv_tests_key=".intval($_POST['id'])." AND owner_fk=$_SESSION[serv_users_key]";
      $res = mysqli_query($db, $sql);
      if (!$res) {
        echo json_encode(array('status' => 'error', 'output' => mysqli_error($db)));
      }
      else {
        $row = mysqli_fetch_assoc($res);
        if ($row) {
          echo json_encode(array('status' => 'ok', 'output' => array('title' => $row['title'], 'description' => $row['description'], 'status' => $row['status'], 'start_planned' => $row['start_planned'], 'start' => $row['start_act'], 'end_planned' => $row['end_planned'], 'end' => $row['end_act'])));
        } else {
          echo json_encode(array('status' => 'error', 'output' => 'test ID '.intval($_POST['id']).' not found'));
        }
      }
    }
93
94
95
96
97
98
99
    else {
      echo json_encode(array('status' => 'error', 'output' => 'unknown query'));
    }
  } else {
    echo json_encode(array('status' => 'error', 'output' => 'invalid API usage'));
  }
?>