Skip to content

Commit 8e10dcd

Browse files
committed
README update
1 parent 8113a7e commit 8e10dcd

File tree

14 files changed

+409
-14
lines changed

14 files changed

+409
-14
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# SocialPlaces Server
2+
3+
Symfony 4 Contacts example app with RESTFUL API
4+
5+
## Features
6+
7+
1. User can add new contacts
8+
2. Users can view contact list
9+
3. API accepts new contact
10+
11+
## Getting Started
12+
13+
1. `git clone https://github.com/adeadedoja/dami-social-places-server.git`
14+
2. `cd dami-social-places-server`
15+
3. `composer install`
16+
4. `php bin/console doctrine:migrations:migrate`
17+
5. `create a new database`
18+
5. `set up env variables`
19+
6. `php bin/console server:run`
20+
7. `npm install to install dependencies`
21+
8. `run 'gulp default'`
22+
23+
The above will get you a copy of the project up and running on your local machine for development and testing purposes.
24+
25+
## Dependencies
26+
27+
1. [MYSQL]
28+
2. [PHP]
29+
3. [Gulp]
30+
31+
## API Endpoints
32+
33+
34+
| EndPoint | Functionality |
35+
| --------------------------------------- | ------------------------------------:|
36+
| POST /api/contact/store | Store a new contact |
37+
38+
## Responses
39+
40+
The API responds with JSON data by default.
41+
42+
## License
43+
44+
This project is licensed under the MIT License - see the [LICENSE.md](https://opensource.org/licenses/MIT) file for details

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
"php": "^7.1.3",
66
"ext-ctype": "*",
77
"ext-iconv": "*",
8+
"google/recaptcha": "1.2",
89
"nelmio/cors-bundle": "^1.5",
910
"sensio/framework-extra-bundle": "^5.2",
11+
"symfony/apache-pack": "^1.0",
1012
"symfony/asset": "*",
1113
"symfony/console": "*",
1214
"symfony/expression-language": "*",

composer.lock

Lines changed: 70 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/packages/twig.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ twig:
22
paths: ['%kernel.project_dir%/templates']
33
debug: '%kernel.debug%'
44
strict_variables: '%kernel.debug%'
5+
globals:
6+
google_sitekey: '%env(GOOGLE_SITEKEY)%'

public/.htaccess

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Use the front controller as index file. It serves as a fallback solution when
2+
# every other rewrite/redirect fails (e.g. in an aliased environment without
3+
# mod_rewrite). Additionally, this reduces the matching process for the
4+
# start page (path "/") because otherwise Apache will apply the rewriting rules
5+
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
6+
DirectoryIndex index.php
7+
8+
# By default, Apache does not evaluate symbolic links if you did not enable this
9+
# feature in your server configuration. Uncomment the following line if you
10+
# install assets as symlinks or if you experience problems related to symlinks
11+
# when compiling LESS/Sass/CoffeScript assets.
12+
# Options FollowSymlinks
13+
14+
# Disabling MultiViews prevents unwanted negotiation, e.g. "/index" should not resolve
15+
# to the front controller "/index.php" but be rewritten to "/index.php/index".
16+
<IfModule mod_negotiation.c>
17+
Options -MultiViews
18+
</IfModule>
19+
20+
<IfModule mod_rewrite.c>
21+
RewriteEngine On
22+
23+
# Determine the RewriteBase automatically and set it as environment variable.
24+
# If you are using Apache aliases to do mass virtual hosting or installed the
25+
# project in a subdirectory, the base path will be prepended to allow proper
26+
# resolution of the index.php file and to redirect to the correct URI. It will
27+
# work in environments without path prefix as well, providing a safe, one-size
28+
# fits all solution. But as you do not need it in this case, you can comment
29+
# the following 2 lines to eliminate the overhead.
30+
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
31+
RewriteRule ^(.*) - [E=BASE:%1]
32+
33+
# Sets the HTTP_AUTHORIZATION header removed by Apache
34+
RewriteCond %{HTTP:Authorization} .
35+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
36+
37+
# Redirect to URI without front controller to prevent duplicate content
38+
# (with and without `/index.php`). Only do this redirect on the initial
39+
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
40+
# endless redirect loop (request -> rewrite to front controller ->
41+
# redirect -> request -> ...).
42+
# So in case you get a "too many redirects" error or you always get redirected
43+
# to the start page because your Apache does not expose the REDIRECT_STATUS
44+
# environment variable, you have 2 choices:
45+
# - disable this feature by commenting the following 2 lines or
46+
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
47+
# following RewriteCond (best solution)
48+
RewriteCond %{ENV:REDIRECT_STATUS} ^$
49+
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
50+
51+
# If the requested filename exists, simply serve it.
52+
# We only want to let Apache serve files and not directories.
53+
RewriteCond %{REQUEST_FILENAME} -f
54+
RewriteRule ^ - [L]
55+
56+
# Rewrite all other queries to the front controller.
57+
RewriteRule ^ %{ENV:BASE}/index.php [L]
58+
</IfModule>
59+
60+
<IfModule !mod_rewrite.c>
61+
<IfModule mod_alias.c>
62+
# When mod_rewrite is not available, we instruct a temporary redirect of
63+
# the start page to the front controller explicitly so that the website
64+
# and the generated links can still be used.
65+
RedirectMatch 307 ^/$ /index.php/
66+
# RedirectTemp cannot be used instead
67+
</IfModule>
68+
</IfModule>

social-places-server.zip

41.5 MB
Binary file not shown.

src/Controller/ContactController.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
1111
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1212
use Symfony\Component\Routing\Annotation\Route;
13+
use ReCaptcha\ReCaptcha;
1314
use Symfony\Component\HttpFoundation\Request;
1415
use Symfony\Component\Validator\Validator\ValidatorInterface;
1516

@@ -34,9 +35,21 @@ public function index(Request $request, ValidatorInterface $validator, MailContr
3435

3536
$form->handleRequest($request);
3637

37-
//checks if submitted and valid
38-
if ($form->isSubmitted() && $form->isValid()) {
38+
3939

40+
//checks if submitted and valid
41+
if ($form->isSubmitted() && $form->isValid()){
42+
43+
//captcha check
44+
if (!$this->captchaverify($request)){
45+
$this->addFlash(
46+
'error-flash',
47+
'Captcha Required'
48+
);
49+
return $this->render('contact/index.html.twig', [
50+
'form' => $form->createView(),
51+
]);
52+
}
4053
//checks errors in object
4154
$errors = $validator->validate($form);
4255

@@ -55,15 +68,17 @@ public function index(Request $request, ValidatorInterface $validator, MailContr
5568
$entityManager->flush();
5669

5770
//call sendMail method to send mail
58-
$mailController->sendMail($contact->getName(), $contact->getEmail());
71+
$mailController->sendMail($contact->getName(), $contact->getEmail(),'confirm');
72+
//call sendMail method to send mail to admin to alert of new contact
73+
$mailController->sendMail('admin', getenv('MAIL_ADMIN'),'contact-alert');
5974

6075
return $this->render('contact/success.html.twig', [
6176
'name' => ucfirst($contact->getName()),
6277
]);
6378
}
6479

6580
return $this->render('contact/index.html.twig', [
66-
'form' => $form->createView(),
81+
'form' => $form->createView()
6782
]);
6883
}
6984

@@ -78,4 +93,11 @@ public function contacts()
7893
'contacts' => $contacts,
7994
]);
8095
}
96+
97+
# get success response from recaptcha and return it to controller
98+
public function captchaverify($request){
99+
$recaptcha = new ReCaptcha(getenv('GOOGLE_SECRET'));
100+
$resp = $recaptcha->verify($request->request->get('g-recaptcha-response'), $request->getClientIp());
101+
return $resp->isSuccess();
102+
}
81103
}

src/Dami/SocialPlacesBundle/Controller/ContactController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ public function store(MailController $mailController, ApiController $apiControll
4141
$em->persist($contact);
4242
$em->flush();
4343

44-
//call sendMail method to send mail
45-
$mailController->sendMail($data['name'], $data['email']);
44+
//call sendMail method to send mail to client to confirm
45+
$mailController->sendMail($data['name'], $data['email'],'confirm');
46+
//call sendMail method to send mail to admin to alert of new contact
47+
$mailController->sendMail('admin', getenv('MAIL_ADMIN'),'contact-alert');
4648

4749
//return name for display
4850
return $apiController->respond([

src/Dami/SocialPlacesBundle/Controller/MailController.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,30 @@ class MailController extends AbstractController
1414
* @param array $recipient
1515
*
1616
*/
17-
public function sendMail($name, $recipient)
17+
public function sendMail($name, $recipient, $type)
1818
{
19+
if($type == 'confirm'){
20+
$view = 'contact-confirmation';
21+
$subject = getenv('MAIL_SUBJECT');
22+
}elseif($type == 'contact-alert'){
23+
$view = 'contact-alert';
24+
$subject = 'You Got a New Contact';
25+
}
26+
$url = getenv('BASE_URL')."/contacts";
1927
// Create the Transport
2028
$transport = (new \Swift_SmtpTransport(getenv('MAIL_HOST'), getenv('MAIL_PORT'),getenv('MAIL_ENCRYPTION')))->setUsername(getenv('MAIL_USERNAME'))->setPassword(getenv('MAIL_PASSWORD'));
2129

2230
// Create the Mailer using your created Transport
2331
$mailer = new \Swift_Mailer($transport);
2432

2533
// Create a message
26-
$message = (new \Swift_Message(getenv('MAIL_SUBJECT')))
27-
->setFrom(array(getenv('MAIL_SUBJECT') => getenv('MAIL_FROM')))
34+
$message = (new \Swift_Message($subject))
35+
->setFrom(array(getenv('MAIL_USERNAME') => getenv('MAIL_FROM')))
2836
->setTo(array($recipient => $name))
2937
->setBody(
3038
$this->renderView(
31-
"emails/contact-confirmation.html.twig",
32-
array('name' => ucfirst($name), 'year' => date('Y'))
39+
"emails/".$view.".html.twig",
40+
array('name' => ucfirst($name), 'year' => date('Y'), 'url' => $url)
3341
),
3442
'text/html'
3543
);

symfony.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@
137137
"swiftmailer/swiftmailer": {
138138
"version": "v6.1.2"
139139
},
140+
"symfony/apache-pack": {
141+
"version": "1.0",
142+
"recipe": {
143+
"repo": "github.com/symfony/recipes-contrib",
144+
"branch": "master",
145+
"version": "1.0",
146+
"ref": "c82bead70f9a4f656354a193df7bf0ca2114efa0"
147+
}
148+
},
140149
"symfony/asset": {
141150
"version": "v4.1.3"
142151
},

0 commit comments

Comments
 (0)