16
16
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
17
17
require APPPATH .'/libraries/REST_Controller.php ' ;
18
18
19
- class Timezone extends REST_Controller
20
- {
19
+ class Timezone extends REST_Controller {
21
20
function isAuth (){
22
21
$ id = $ this ->session ->userdata ('id ' );
23
22
return isset ($ id ) && $ id > 0 ;
24
23
}
25
24
26
25
function get_get (){
27
- if (!$ this ->isAuth ()){
28
- $ this ->response (array ('error ' => 'no login ' ), 200 );
29
- }else {
26
+ // if(!$this->isAuth()){
27
+ // $this->response(array('error' => 'no login'), 200);
28
+ // }else{
30
29
$ id = $ this ->session ->userdata ('id ' );
30
+ if (!is_numeric ($ id )) {
31
+ $ this ->response (array ('error ' => 'invalid user id ' ), 403 );
32
+ }
31
33
$ query = $ this ->db ->query ("SELECT * FROM timezone WHERE user_id= {$ id }" );
32
34
$ result = $ query ->result ();
33
35
$ data = array ();
@@ -39,7 +41,7 @@ function get_get(){
39
41
// $item->comments = $this->getComments($item->id);
40
42
}
41
43
$ this ->response (array ('data ' => $ result ), 200 );
42
- }
44
+ // }
43
45
}
44
46
45
47
function getComments ($ id ){
@@ -50,11 +52,31 @@ function getComments($id){
50
52
51
53
// function update_post(){
52
54
function update_put (){
53
- if (!$ this ->isAuth ()) {
54
- $ this ->response (array ('error ' => 'no login ' ), 200 );
55
- } else {
55
+ // if (!$this->isAuth()) {
56
+ // $this->response(array('error' => 'no login'), 200);
57
+ // } else {
56
58
$ id = $ this ->put ('id ' );
57
- $ user_id = $ this ->session ->userdata ('id ' );
59
+ // $user_id = $this->session->userdata('id');
60
+ $ name = $ this ->put ('name ' );
61
+ $ city = $ this ->put ('city ' );
62
+ $ timezone = $ this ->put ('timezone ' );
63
+ $ apikey = $ this ->put ('apikey ' );
64
+
65
+
66
+ if (empty ($ name ) || empty ($ city ) || empty ($ timezone ) || empty ($ id ) || empty ($ apikey ) ||
67
+ strlen ($ name ) > 20 || strlen ($ city ) > 20 || !preg_match ('/^GMT[\+\-]1?\d$/ ' , $ timezone )){
68
+ $ this ->response (array ('status ' => 'false ' , 'error ' => 'invalid input ' ), 403 );
69
+ }
70
+
71
+ $ query = $ this ->db ->query ("SELECT * FROM `keys` WHERE `key`=' {$ apikey }' " );
72
+ if ($ query ->num_rows == 1 ) {
73
+ $ result = $ query ->result ();
74
+ // var_dump($result[0]);
75
+ $ user_id = $ result [0 ];
76
+ $ user_id = $ user_id ->user_id ;
77
+ } else {
78
+ $ this ->response (array ('status ' => 'false ' , 'error ' => 'Wrong API key ' ), 403 );
79
+ }
58
80
$ data = array (
59
81
'name ' => $ this ->put ('name ' ),
60
82
'city ' => $ this ->put ('city ' ),
@@ -76,18 +98,47 @@ function update_put(){
76
98
}else {
77
99
$ this ->response (array ('status ' => 'not exists ' , 'error ' => 'not exists ' ), 200 );
78
100
}
79
- }
101
+ // }
80
102
}
81
103
82
104
function add_post () {
83
- if (!$ this ->isAuth ()) {
84
- $ this ->response (array ('error ' => 'no login ' ), 200 );
85
- } else {
105
+
106
+ // if (!$this->isAuth()) {
107
+ // $this->response(array('error' => 'no login'), 200);
108
+ // } else {
109
+ $ name = $ this ->post ('name ' );
110
+ $ city = $ this ->post ('city ' );
111
+ $ timezone = $ this ->post ('timezone ' );
112
+ $ apikey = $ this ->post ('apikey ' );
113
+
114
+ // echo substr($timezone, 0, 3);
115
+ // if (empty($name) || empty($city) || empty($timezone)
116
+ // || strlen($name) > 20 || strlen($city) > 20 || substr($timezone, 0, 3) != 'GMT') {
117
+ // header("HTTP/1.1 200 OK");
118
+ // echo json_encode(array('status' => 'invalid parameters'));
119
+ // return;
120
+ // }
121
+
122
+ if (empty ($ name ) || empty ($ city ) || empty ($ timezone ) || empty ($ apikey ) ||
123
+ strlen ($ name ) > 20 || strlen ($ city ) > 20 || !preg_match ('/^GMT[\+\-]1?\d$/ ' , $ timezone )){
124
+ $ this ->response (array ('status ' => 'false ' , 'error ' => 'invalid input ' ), 403 );
125
+ }
126
+
127
+ $ query = $ this ->db ->query ("SELECT * FROM `keys` WHERE `key`=' {$ apikey }' " );
128
+ if ($ query ->num_rows == 1 ) {
129
+ $ result = $ query ->result ();
130
+ // var_dump($result[0]);
131
+ $ user_id = $ result [0 ];
132
+ $ user_id = $ user_id ->user_id ;
133
+ } else {
134
+ $ this ->response (array ('status ' => 'false ' , 'error ' => 'Wrong API key ' ), 403 );
135
+ }
86
136
$ data = array (
87
137
'name ' => $ this ->post ('name ' ),
88
138
'city ' => $ this ->post ('city ' ),
89
139
'timezone ' => $ this ->post ('timezone ' ),
90
- 'user_id ' => $ this ->session ->userdata ('id ' )
140
+ // 'user_id' => $this->session->userdata('id')
141
+ 'user_id ' => $ user_id
91
142
);
92
143
93
144
$ this ->db ->insert ('timezone ' , $ data );
@@ -100,7 +151,7 @@ function add_post() {
100
151
}
101
152
102
153
$ this ->response (array ('status ' => 'fail ' ), 200 );
103
- }
154
+ // }
104
155
}
105
156
106
157
function addComment_post (){
@@ -126,12 +177,23 @@ function addComment_post(){
126
177
}
127
178
128
179
// function delete_post(){
129
- function delete_delete (){
130
- if (!$ this ->isAuth ()) {
131
- $ this ->response (array ('error ' => 'no login ' ), 200 );
132
- } else {
180
+ function delete_post (){
181
+ // if (!$this->isAuth()) {
182
+ // $this->response(array('error' => 'no login'), 200);
183
+ // } else {
133
184
$ id = $ this ->get ('id ' );
134
- $ query = $ this ->db ->query ("SELECT * from timezone WHERE id= {$ id } " );
185
+ $ apikey = $ this ->post ('apikey ' );
186
+ // echo 'delete' . $apikey;
187
+ $ query = $ this ->db ->query ("SELECT * FROM `keys` WHERE `key`=' {$ apikey }' " );
188
+ if ($ query ->num_rows == 1 ) {
189
+ $ result = $ query ->result ();
190
+ // var_dump($result[0]);
191
+ $ user_id = $ result [0 ];
192
+ $ user_id = $ user_id ->user_id ;
193
+ } else {
194
+ $ this ->response (array ('status ' => 'false ' , 'error ' => 'Wrong API key ' ), 403 );
195
+ }
196
+ $ query = $ this ->db ->query ("SELECT * from timezone WHERE id=' {$ id }' AND user_id=' {$ user_id }' " );
135
197
136
198
if ($ query ->num_rows === 0 ) { //nothing to delete in the DB
137
199
$ this ->response (array ('status ' => 'not exists ' ), 200 );
@@ -140,95 +202,6 @@ function delete_delete(){
140
202
$ this ->db ->query ("DELETE from timezone where id=' {$ id }' " );
141
203
$ this ->response (array ('status ' => 'success ' ), 200 );
142
204
}
143
- }
144
- }
145
-
146
- function login_post (){
147
- $ username = $ this ->post ('username ' );
148
- $ password = $ this ->post ('password ' );
149
- $ password = md5 ($ password );
150
-
151
- $ query = $ this ->db ->query ("SELECT username, password FROM user WHERE username=' {$ username }' and password=' {$ password }' " );
152
- if ($ query ->num_rows > 0 ){
153
- $ this ->response (array ('status ' => 'successs ' ), 200 );
154
- }else {
155
- $ this ->response (array ('status ' => 'fail ' ), 200 );
156
- }
157
- // echo 'Total Results: ' . $query->num_rows();
158
- }
159
-
160
- function user_get ()
161
- {
162
- if (!$ this ->get ('id ' ))
163
- {
164
- $ this ->response (NULL , 400 );
165
- }
166
-
167
- // $user = $this->some_model->getSomething( $this->get('id') );
168
- $ users = array (
169
- 1 => array ('id ' => 1 , 'name ' => 'Some Guy ' , 'email ' => 'example1@example.com ' , 'fact ' => 'Loves swimming ' ),
170
- 2 => array ('id ' => 2 , 'name ' => 'Person Face ' , 'email ' => 'example2@example.com ' , 'fact ' => 'Has a huge face ' ),
171
- 3 => array ('id ' => 3 , 'name ' => 'Scotty ' , 'email ' => 'example3@example.com ' , 'fact ' => 'Is a Scott! ' , array ('hobbies ' => array ('fartings ' , 'bikes ' ))),
172
- );
173
-
174
- $ user = @$ users [$ this ->get ('id ' )];
175
-
176
- if ($ user )
177
- {
178
- $ this ->response ($ user , 200 ); // 200 being the HTTP response code
179
- }
180
-
181
- else
182
- {
183
- $ this ->response (array ('error ' => 'User could not be found ' ), 404 );
184
- }
185
- }
186
-
187
- function user_post ()
188
- {
189
- //$this->some_model->updateUser( $this->get('id') );
190
- $ message = array ('id ' => $ this ->get ('id ' ), 'name ' => $ this ->post ('name ' ), 'email ' => $ this ->post ('email ' ), 'message ' => 'ADDED! ' );
191
-
192
- $ this ->response ($ message , 200 ); // 200 being the HTTP response code
193
- }
194
-
195
- function user_delete ()
196
- {
197
- //$this->some_model->deletesomething( $this->get('id') );
198
- $ message = array ('id ' => $ this ->get ('id ' ), 'message ' => 'DELETED! ' );
199
-
200
- $ this ->response ($ message , 200 ); // 200 being the HTTP response code
201
- }
202
-
203
- function users_get ()
204
- {
205
- //$users = $this->some_model->getSomething( $this->get('limit') );
206
- $ users = array (
207
- array ('id ' => 1 , 'name ' => 'Some Guy ' , 'email ' => 'example1@example.com ' ),
208
- array ('id ' => 2 , 'name ' => 'Person Face ' , 'email ' => 'example2@example.com ' ),
209
- 3 => array ('id ' => 3 , 'name ' => 'Scotty ' , 'email ' => 'example3@example.com ' , 'fact ' => array ('hobbies ' => array ('fartings ' , 'bikes ' ))),
210
- );
211
-
212
- if ($ users )
213
- {
214
- $ this ->response ($ users , 200 ); // 200 being the HTTP response code
215
- }
216
-
217
- else
218
- {
219
- $ this ->response (array ('error ' => 'Couldn \'t find any users! ' ), 404 );
220
- }
221
- }
222
-
223
-
224
- public function send_post ()
225
- {
226
- var_dump ($ this ->request ->body );
227
- }
228
-
229
-
230
- public function send_put ()
231
- {
232
- var_dump ($ this ->put ('foo ' ));
205
+ // }
233
206
}
234
207
}
0 commit comments