forked from mRoca/slim-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
155 lines (120 loc) · 4.2 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
//session_start();
// Init Slim framework, other composer libs and PSR-0 autoloader
require_once './vendor/autoload.php';
//The config file
require_once './config.php';
//Add some helpers
require_once './app/lib/SlimBoilerplate/SlimHelpers.php';
// ########################################
// ############### Config #################
$app = new \Slim\Slim(array(
'mode' => $_SERVER['SERVER_NAME'] === 'server.production.com' ? 'production' : 'development',
'log.path' => './app/logs',
'templates.path' => './app/templates',
));
$app->configureMode('production', function () use ($app) {
$app->config(array(
'log.level' => \Slim\Log::WARN,
'log.enable' => true,
'debug' => false
));
});
$app->configureMode('development', function () use ($app) {
$app->config(array(
'log.enable' => false,
'debug' => true
));
});
//Use the layout class if not Ajax request
if (!$app->request()->isAjax()) {
$app->view(new SlimBoilerplate\Layout\LayoutView());
$app->notFound(function () use ($app) {
include("404.html");
});
}
// ########################################
// ###### DEFAULT LAYOUT CONFIG ###########
SlimBoilerplate\Layout\LayoutView::addCss('assets/css/vendor/normalize.min.css');
SlimBoilerplate\Layout\LayoutView::addCss('assets/css/style.css');
SlimBoilerplate\Layout\LayoutView::addJs('assets/js/vendor/modernizr-2.6.2-custom.min.js');
//SlimBoilerplate\Layout\LayoutView::setDefaultDescription('Slim PHP boilerplate');
// ########################################
// ############### ROUTES #################
// REST API
// Very simple Rest server for the "account" sql table
$restAccounts = new \SlimBoilerplate\Rest\RestServer('account');
$restAccounts->createRoutes($app);
// HTML
$app->get('/', function () use ($app) {
$app->render('home.php');
})->name('home');
$app->get('/error', function () use ($app) {
$app->render('error.php');
})->name('error');
$app->get('/contact', function () use ($app) {
$data = inputGet('success') ? array('success' => inputGet('success')) : array();
$app->render('contact.php', $data);
});
$app->post('/contact', function () use ($app) {
$error = '';
$message = 'Un nouveau message a été posté sur le site ' . SITE_NAME . '.<br /><br />';
$fields = array(
'Nom' => 'contact_name',
'Société' => 'contact_company',
'Email' => 'contact_email',
'Message' => 'contact_message',
);
foreach ($fields as $fullName => $name) {
if (!trim(inputPost($name))) {
$error .= "Le champ $fullName' est invalide.<br />";
}
if ($name === 'contact_email' && !valid_email(inputPost($name))) {
$error .= "L'adresse mail est invalide.<br />";
}
if ($name !== 'contact_message')
$message .= "$fullName : <strong>" . inputPost($name) . "</strong><br />";
else {
$message .= "<br />------------------ Message : --------------<br /><br />";
$message .= nl2br(strip_tags(inputPost($name))) . "<br />";
}
}
if (!$error) {
try {
$mailer = new SimpleMail();
$send = $mailer->setTo(CONTACT_EMAIL, SITE_NAME)
->setSubject("[" . SITE_NAME . "] Nouveau message de contact")
->setFrom(CONTACT_EMAIL, SITE_NAME)
->addMailHeader('Reply-To', inputPost('contact_email'), inputPost('contact_name'))
->addGenericHeader('X-Mailer', 'PHP/' . phpversion())
->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
->setMessage($message)
->setWrap(100)
->send();
if (!$send) {
$error = 'Erreur lors de l\'envoi du mail';
}
} catch (Exception $e) {
$error = $e->getMessage();
}
}
if ($error)
$app->render('contact.php', array('error' => $error));
else
$app->redirect(uri('contact?success=1'));
});
//Default controller for front pages
$app->get('/:page', function ($page) use ($app) {
$page = preg_replace('/[^0-9a-zA-Z\-_+]/', '', $page);
if (file_exists("./app/templates/front/$page.php")) {
$app->render("front/$page.php");
} else $app->notFound();
});
$app->get('/:folder/:page', function ($folder, $page) use ($app) {
$page = preg_replace('/[^0-9a-zA-Z\-_+]/', '', $page);
$folder = preg_replace('/[^0-9a-zA-Z\-_+]/', '', $folder);
if (file_exists("./app/templates/front/$folder/$page.php")) {
$app->render("front/$folder/$page.php");
} else $app->notFound();
});
$app->run();