-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
116 lines (102 loc) · 2.24 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
require 'vendor/autoload.php';
require_once 'consts.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->view(new \JsonApiView());
$app->add(new \JsonApiMiddleware());
$app->get(
'/',
function () use ($app) {
$app->render(
200,
array(
'msg' => 'welcome to PraiseAPi!',
)
);
}
);
$app->get(
'/message/',
function () use ($app) {
messageGet($app);
}
);
$app->post(
'/praise/',
function () use ($app) {
praiseAdd($app);
}
);
$app->get('/functest', 'selectMessages');
$app->post(
'/users/',
function () use ($app) {
if ($app->request->post('user') !== '') {
addUser($app);
}
}
);
function messageGet($app)
{
$msg = selectMessages();
$imageurl = getImageURL();
$app->render(
200,
array(
'user_name' => 'hoge',
'message' => $msg,
'image' => $imageurl
)
);
}
function praiseAdd($app)
{
$jsonPosted = $app->request->getBody();
$deserialize = json_decode($jsonPosted);
$ret = file_put_contents(__DIR__ . DS . MESSAGES_FILENAME, $deserialize->{'message'} . "\n", FILE_APPEND | LOCK_EX);
if ($ret === FALSE) {
$app->render(
500,
array(
'description' => 'insert error'
)
);
}
$app->render(
200,
array(
'user_name' => 'hoge'
)
);
}
$app->run();
function selectMessages()
{
// メッセージを全部呼びこむ
$messages = file(__DIR__ . DS . MESSAGES_FILENAME);
// メッセージ数確認
$messagesLength = count($messages);
// ランダム選択
$selected = rand(0, $messagesLength - 1);
return $messages[$selected];
}
function getImageURL()
{
$img = getImage();
$URL = $img;
return $URL;
}
function getImage()
{
$imagefiles = [];
$res_dir = opendir('./' . IMAGE_PATH);
while ($file_name = readdir($res_dir)) {
array_push($imagefiles, $file_name);
}
$imagelength = count($imagefiles);
$selected = rand(0, $imagelength - 1);
$imgfile = $imagefiles[$selected];
closedir($res_dir);
return $imgfile;
}