* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ /** * This file is part of test. * * @file ApiControllerProvider.php * * R. Eric Wheeler * * 7/8/16 / 10:11 AM * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Sikofitt\Controller; use ReCaptcha\ReCaptcha; use Silex\Api\ControllerProviderInterface; use Silex\Application; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class ApiControllerProvider implements ControllerProviderInterface { /** * {@inheritdoc} * * @param Application $app * * @return mixed */ public function connect(Application $app) { $controllers = $app['controllers_factory']; $controllers->get('/v1/schema', function () use ($app) { $response = new Response(file_get_contents($app->getDataDirectory() . '/schema/schema.v1.json'), Response::HTTP_OK); $response->headers->set('Content-Type', 'application/schema+json'); return $response; }); $controllers->match('/v1/message', function (Request $request) use ($app) { /*$app->mail(\Swift_Message::newInstance() ->setSubject('[YourSite] Feedback') ->setFrom(array('noreply@yoursite.com')) ->setTo(array('feedback@yoursite.com')) ->setBody($request->get('message'))); */ return new Response('This is the message.'); })->method('GET|POST'); $controllers->post('/v1/captcha', function (Request $request) use ($app) { $captcha = new ReCaptcha('6LcvmSQTAAAAAITkvYJjgLar1LqGGLz-ic0ZMiXo'); $valid = $captcha->verify( $request->request->get('g-recaptcha-response'), $request->server->get('REMOTE_ADDR') ); if ($valid->isSuccess()) { $return = [ 'valid' => true, 'message' => [ 'email' => null !== $app->config('app.email') ? $app->config('app.email') : 'No email has been set in the configuration. Please let the owner know.', 'phone' => null !== $app->config('app.phone') ? $app->config('app.phone') : 'No phone has been set in the configuration. Please let the owner know.', ], ]; } else { $errorCodes = [ 'missing-input-secret' => 'The secret parameter is missing.', 'invalid-input-secret' => 'The secret parameter is invalid or malformed.', 'missing-input-response' =>'The response parameter is missing.', 'invalid-input-response' => 'The response parameter is invalid or malformed.' ]; foreach ($valid->getErrorCodes() as $code) { if (array_key_exists($code, $errorCodes)) { $errors[] = $errorCodes[$code]; } } if (!isset($errors)) { $errors[] = 'An unknown error occurred.'; } $return = [ 'valid' => false, 'message' => $errors, ]; } return new JsonResponse(json_encode($return)); }); return $controllers; } }