* * 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\Application; use Silex\Api\ControllerProviderInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; class ApiControllerProvider implements ControllerProviderInterface { public function connect(Application $app) { $controllers = $app['controllers_factory']; $controllers->get('/v1/schema', function() { return new \Symfony\Component\HttpFoundation\Response('Success!'); }); $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; } }