Skip to content

Commit d1c8646

Browse files
committed
rewritten api using Slim Framework
1 parent d26cd82 commit d1c8646

12 files changed

+164
-139
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# dependencies
88
/node_modules
99
/bower_components
10+
/api/vendor
1011

1112
# misc
1213
/.sass-cache

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ install:
1616
- npm install -g bower
1717
- npm install
1818
- bower install
19+
- cd api/ && composer install
1920

2021
before_script:
2122
- ember server --live-reload=false & # Start a server so we can hit the fake API from integration tests

Brocfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ trees.push(
8383
trees.push(
8484
pickFiles('api', {
8585
srcDir: '/',
86-
destDir: '/'
86+
destDir: '/api'
8787
})
8888
);
8989

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ You could check for an attack like this by using an development tool for your br
2323
Requirements
2424
------------
2525

26-
Croodle is designed to have as few as possible requirements on the server it is running on. Croodle runs on almost every web space with PHP. Croodle stores the data in textfiles, so there is no need for a database server like mySQL.
26+
Croodle is designed to have as few as possible requirements on the server it is running on. Croodle runs on almost every web space with PHP >= 5.3. Croodle stores the data in textfiles, so there is no need for a database server like mySQL.
2727

2828
Due to security reasons you should have SSL encryption enabled and provide a valid certificate.
2929

api/api.php

-133
This file was deleted.

api/classes/datahandler.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
/*
44
* handles the data for api.php
55
*/
6-
class datahandler {
6+
class Datahandler {
77
// (string) folder to store data in relative to position of api.php
88
// webserver has to have write access to this folder
99
// must end with a slash
10-
const DATA_FOLDER = 'data/';
10+
const DATA_FOLDER = '../data/';
1111

1212
// (int) length of ids used to identify data
1313
const ID_LENGTH = 10;

api/composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"slim/slim": "~2.0"
4+
}
5+
}

api/composer.lock

+63
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/index.php

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/*
3+
* RESTful API used by ember data for data storage
4+
*/
5+
6+
require 'vendor/autoload.php';
7+
require 'classes/datahandler.php';
8+
9+
function pollIdIsValid($pollId) {
10+
return preg_match('/[^A-Za-z0-9]/', $pollId) === 0;
11+
}
12+
13+
$app = new \Slim\Slim();
14+
15+
/*
16+
* default response headers
17+
*/
18+
$app->response->headers->set('Content-Type', 'application/json; charset=utf-8');
19+
// prevent Internet Explorer from caching AJAX requests
20+
$app->expires('-1');
21+
22+
$app->get('/polls/:id', function ($pollId) use ($app) {
23+
if(!pollIdIsValid($pollId)) {
24+
$app->halt(400, 'requested id must only contain letters and numbers');
25+
}
26+
27+
$datahandler = new Datahandler();
28+
$data = $datahandler->get($pollId);
29+
30+
if ($data === false) {
31+
// there was no data with this id or it could not be readen
32+
$app->response->setStatus(404);
33+
}
34+
else {
35+
$app->response->setBody($data);
36+
}
37+
});
38+
39+
$app->post('/polls', function() use ($app) {
40+
$datahandler = new Datahandler();
41+
$pollCreatedId = $datahandler->writePoll(
42+
$app->request->getBody()
43+
);
44+
45+
if (!$pollCreatedId) {
46+
$app->halt(500, 'saving poll failed');
47+
}
48+
49+
$app->response->setBody(
50+
$datahandler->get($pollCreatedId)
51+
);
52+
});
53+
54+
$app->post('/users', function() use ($app) {
55+
$datahandler = new Datahandler();
56+
57+
// get poll id
58+
$dataObject = json_decode(
59+
$app->request->getBody()
60+
);
61+
$pollId = $dataObject->user->poll;
62+
if (!pollIdIsValid($pollId)) {
63+
$app->halt(400, 'poll id must only contain letters and numbers');
64+
}
65+
66+
// write user
67+
$userCreatedId = $datahandler->writeUser($pollId, $app->request->getBody());
68+
69+
if ($userCreatedId === false) {
70+
$app->halt(500, 'saving user failed');
71+
}
72+
73+
// add user id to user object
74+
$dataObject->user->id = $userCreatedId;
75+
76+
$app->response->setBody(
77+
json_encode($dataObject)
78+
);
79+
});
80+
81+
$app->notFound(function () use ($app) {
82+
// die("verdammte schieße...");
83+
$app->halt(404, "verdammte scheiße\n" . $app->request->getResourceUri() . "\n");
84+
});
85+
86+
$app->run();

app/adapters/application.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default DS.RESTAdapter.extend({
1313
// remove leading and trailing slash
1414
.replace(/\/$/, '')
1515
// add api.php
16-
.concat('/api.php?')
16+
.concat('/api/index.php')
1717
// remove leading slash
1818
.replace(/^\//g, '')
1919
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@
4747
"ember-radio-button": "^0.1.2",
4848
"express": "^4.8.5",
4949
"glob": "^5.0.3",
50-
"node-phpcgi": "0.3.1"
50+
"node-phpcgi": "jelhan/node-phpcgi#entry-point"
5151
}
5252
}

server/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ module.exports = function(app) {
2020
/* use node-phpcgi to handle api */
2121
var phpcgi = require('node-phpcgi')({
2222
documentRoot: __dirname.substring(0, __dirname.length - 6) + '/dist',
23+
includePath: '/api/index.php',
24+
entryPoint: '/api/index.php'
2325
});
2426
app.use(phpcgi);
2527

0 commit comments

Comments
 (0)