source: Dev/branches/jQueryUI/server/api.php @ 316

Last change on this file since 316 was 249, checked in by hendrikvanantwerpen, 13 years ago

This one's for Subversion, because it's so close...

First widget (stripped down sequencer).
Seperated client and server code in two direcotry trees.

File size: 4.7 KB
Line 
1<?php
2
3require_once 'classes/master.php';
4
5$rawInput = $HTTP_RAW_POST_DATA;
6$output = array();
7$returnValue = 418;
8
9if (!empty($rawInput)) {
10
11    $input = json_decode($rawInput);
12
13    switch ($input->action) {
14        case "login":
15            $username = $input->args->username;
16            $password = $input->args->password;
17            $returnValue = 401;
18
19            $user_results = User::get(array("name" => $username));
20            if (!empty($user_results)) {
21                $user = $user_results[0];
22                if ($password == $user->password) {
23                    $output = $user;
24                    $returnValue = 200;
25                }
26            }
27            break;
28        case "register":
29            $username = $input->args->username;
30            $password = $input->args->password;
31            $user_results = User::get(array("name" => $username));
32            if (!empty($user_results)) {
33                $returnValue = 409;
34            } else {
35                $user = new User();
36                $user->name = $username;
37                $user->password = $password;
38                $user->save();
39                $output = $user;
40                $returnValue = 200;
41            }
42            break;
43    }
44
45    if (isset($input->uid)) {
46        $user_results = User::get(array("uid" => $input->uid));
47        if (!empty($user_results)) {
48            $user = $user_results[0];
49            switch ($input->action) {
50                case "createObject":
51                    $uid = null;
52                    switch (strtolower($input->args->type)) {
53                        case "application":
54                            $newApp = new Application(null,
55                                            $input->args->values->title,
56                                            $input->args->values->description,
57                                            $input->args->values->style);
58                            $newApp->save();
59                            $uid = $newApp->uid;
60                            $returnValue = 200;
61                            break;
62                        case "survey":
63                            $newSurvey = new Survey(null,
64                                            $input->args->values->title,
65                                            $input->args->values->description,
66                                            $user, null);
67                            $newSurvey->save();
68                            $uid = $newSurvey->uid;
69                            $returnValue = 200;
70                            break;
71                        case "dashboard":
72                            //TODO
73                            break;
74                        case "question":
75                            $newQuestion = new Question(null,
76                                            $input->args->values->qCode,
77                                            $input->args->values->title,
78                                            $input->args->values->dataType,
79                                            $input->args->values->description,
80                                            $input->args->values->category,
81                                            array());
82                            $newQuestion->save();
83                            $uid = $newQuestion->uid;
84                            $returnValue = 200;
85                            break;
86                        case "session":
87                            $newSession = new Session(null,
88                                            $input->args->values->title,
89                                            $user, new DateTime(), null, null);
90                            $newSession->save();
91                            $uid = $newSession->uid;
92                            $returnValue = 200;
93                            break;
94                    }
95                    $output['uid'] = $uid;
96                    break;
97                case "getObject":
98                    $type = $input->args->type;
99                    $objects = $type::get(array("uid" => $input->args->uid));
100                    if (!empty($objects)) {
101                        $output->object = $objects[0];
102                        $returnValue = 200;
103                    }
104                    break;
105                case "getObjects":
106                    $type = $input->args->type;
107                    $output['objects'] = $type::get($input->args->predicates);
108                    $returnValue = 200;
109                    break;
110            }
111        } else {
112            $returnValue = 403;
113        }
114    }
115}
116
117header("HTTP/1.1 " . $returnValue);
118header("Content-Type: application/json");
119header("Cache-Control: no-cache, must-revalidate");
120header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
121echo(json_encode($output));
122?>
Note: See TracBrowser for help on using the repository browser.