<?php
namespace App\Controller;
use App\Entity\App;
use App\Entity\AppSharing;
use App\Entity\Flow;
use App\Entity\Page;
use App\Form\FlowFormType;
use App\Service\AlgorithmService;
use App\Service\FlowService;
use App\Service\PageGeneratorService;
use App\Service\RestGeneratorService;
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Client\Browser;
use DeviceDetector\Parser\OperatingSystem;
use Doctrine\Persistence\ManagerRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Twig\Environment;
use Symfony\Component\Routing\Annotation\Route;
class WebController extends EasyAdminController {
public function __construct(ManagerRegistry $managerRegistry, PageGeneratorService $pageGeneratorService, TranslatorInterface $translator, RestGeneratorService $restGeneratorService) {
$this->doctrine = $managerRegistry;
$this->pageGeneratorService = $pageGeneratorService;
$this->translator = $translator;
$this->restGeneratorService = $restGeneratorService;
}
public function index(Request $request, FlowService $flowService, AlgorithmService $algorithmService )
{
$em = $this->doctrine->getManager();
$form = $this->createForm( FlowFormType::class, null, array('custom'=>1 ) );
$form->handleRequest( $request );
if ($form->isSubmitted()) {
$fuuid = $form['fuuid']->getData();
$query_input_data = $request->query->all();
$post_input_data = $request->request->all();
//$input_data = array('query'=>$query_input_data,'form'=>$post_input_data['flow_form'] );
$input_data = array_merge($post_input_data['flow_form'], $query_input_data);
$Flow = $em->getRepository(Flow::class)->findOneBy(['Fuuid'=>$fuuid]);
$Flow = $flowService->executeFlow($Flow, $input_data,'pl');
return $this->render('website/index.html.twig', [
'form' => $form->createView(),
'fuuid' => $fuuid,
]);
} else {
return $this->render('website/index.html.twig', [
'form' => $form->createView(),
'fuuid'=>null
]);
}
}
/**
* @Route("/webapp/{customer}/{appid}", name="share_webapp")
*/
public function webapp( Request $request, $customer='', $appid='') {
$em = $this->doctrine->getManager();
$basePath = $request->getSchemeAndHttpHost();
$App = $em->getRepository(App::class)->find($appid);
$Customer = $App->getCustomer();
$customerUuid = $Customer->getUuid();
$customerSlug = $Customer->getSlug();
$appId = $App->getId();
$appDirectoryPath = "$basePath/customer/{$customerSlug}-{$customerUuid}/apps/{$appId}/web/index.html";
return $this->render('website/webapp.html.twig',
[
'locale'=>'pl',
'path'=>$appDirectoryPath
]);
}
/**
* @Route("/r/{appid}/{code}", name="url_redirect")
*/
public function url_redirect(Request $request, $code='', $appid='') {
$em = $this->doctrine->getManager();
$data = $request->query->get( 'd' );
$pageid = $request->query->get( 'pageid' ); // landing page page id
$p = $request->query->get( 'p' ); // pageId directly in link
// User changes password using form.
$actionUrl = $request->request->get( 'applnkact' );
$emailUrl = $request->request->get( 'email' );
$passwordUrl = $request->request->get( 'new_password' );
$password2Url = $request->request->get( 'password2' );
$codeUrl = $request->request->get( 'prc' );
$codeLength = strlen($code);
if ($codeLength == 4) {
$AppSharing = $em->getRepository(AppSharing::class)->findOneBy(array('Code' => $code, 'App'=>$appid));
$linkCode = $AppSharing->getCode();
$pageId = $AppSharing->getPage()->getId();
} elseif ($codeLength == 8) {
$parentCode = substr($code, 0, 4);
$linkCode = substr($code, 4, 4);
$AppSharing = $em->getRepository(AppSharing::class)->findOneBy(array('Code' => $parentCode, 'App'=>$appid));
$appPagesArray = $AppSharing->getAppPages();
$pageId = $this->pageGeneratorService->findChildAppPage($appPagesArray, $linkCode);
}
if (empty($AppSharing)) {
return $this->render('website/redirect.html.twig',
['childpages'=>'', 'appid'=>$appid, 'code' => '', 'data' => '', 'title' => 'redirect_code_not_found_title', 'body' => 'redirect_code_not_found_full_message', 'link' => '', 'qr' => '', 'osFamily' => '', 'browser' => '', 'showAppstores' =>false, 'urlAppStore' => '', 'urlGooglePlay' => '', 'urlHuaweiAppGallery' => '']);
}
$App = $AppSharing->getApp();
$showLink = $AppSharing->getShowLink();
$showQr = $AppSharing->getShowQr();
$showAppstores = $AppSharing->getShowAppStores();
$webUrl = $AppSharing->getWebUrl();
$appSettings = $AppSharing->getApp()->getSettings();
$type = $AppSharing->getType();
$redirectMobile = $AppSharing->getRedirectMobile();
$redirectWeb = $AppSharing->getRedirectWeb();
$pages = $AppSharing->getPages();
$childpages = array();
$appIcon = $App->getIcon();
$clientLocale = strtolower(str_split($_SERVER['HTTP_ACCEPT_LANGUAGE'], 2)[0]);
$lang = $clientLocale; // todo - what if client locale is different than any in db ?
if ($pageid > 0) { // pageid is used in the mobile's page menu to access individual pages of the landing page
$Page = $em->getRepository(Page::class)->findOneBy(array('id'=>$pageid, 'App'=>$App));
}
foreach ($pages as $page) {
$childId = $page['Child'];
$childPage = $em->getRepository(Page::class)->find($childId);
$childpages[$childId] = $childPage->translate($clientLocale)->getName();
}
$this->translator->setLocale($clientLocale);
if (!empty($Page)) { // open a page with give pageid
$pageTitle = $Page->translate($clientLocale)->getName();
$pageBody = $Page->translate($clientLocale)->getBody();
return $this->render('website/redirect.html.twig',
['childpages'=>$childpages,
'appid'=>$appid,
'code' => $code,
'data' => $data,
'title' => $pageTitle,
'appIcon' => $appIcon,
'body' => $pageBody,
'link' => '',
'qr' => '',
'osFamily' => '',
'browser' => '',
'showAppstores' => false,
'urlAppStore' => '',
'urlGooglePlay' => '',
'urlHuaweiAppGallery' => '',
'locale'=>'pl']);
} else {
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$dd = new DeviceDetector($userAgent);
$dd->parse();
$osFamily = OperatingSystem::getOsFamily($dd->getOs('name'));
$browserFamily = Browser::getBrowserFamily($dd->getClient('name'));
$isSmartphone = $dd->isSmartphone();
$pageTitle = $AppSharing->translate($clientLocale)->getName();
$pageBody = $AppSharing->translate($clientLocale)->getBody();
$urlAppStore = (isset($appSettings['urlAppStore'])) ? $appSettings['urlAppStore'] : '';
$urlGooglePlay = (isset($appSettings['urlGooglePlay'])) ? $appSettings['urlGooglePlay'] : '';
$urlHuaweiAppGallery = (isset($appSettings['urlHuaweiAppGallery'])) ? $appSettings['urlHuaweiAppGallery'] : '';
$appTranslations = $App->translate($lang)->getTranslationStrings();
if (!empty($p)) {
$link = $webUrl . '?page_id=' . $p;
} else {
$link = $webUrl . '?page_id=' . $this->pageGeneratorService->buildPageId($pageId, 11, null);
}
if ($type == "db") {
$link .= '?c=' . $code;
} elseif ($type == "qr") {
$link .= '?d=' . $data;
}
if (($dd->isSmartphone()) && ($redirectMobile)) {
return $this->redirect($link);
} elseif ($redirectWeb) {
return $this->redirect($link);
}
if (!$showLink) {
$link = '';
}
if (($showQr) && (!$isSmartphone)) {
$appUserFilesPath = $this->pageGeneratorService->buildAppFilesPublicUrl($App);
$qr = $appUserFilesPath.'/qr/'.$linkCode.'.png';
} else {
$qr = '';
}
$externalDataArray = json_decode(base64_decode(strtr($data, '._-','+/=')), TRUE);
$inputData = $this->restGeneratorService->parseExternalData($externalDataArray);
$action = (isset($inputData['applnkact'])) ? $inputData['applnkact'] : null;
$prc = (isset($inputData['prc'])) ? $inputData['prc'] : '';
$email = (isset($inputData['email'])) ? $inputData['email'] : '';
if ($actionUrl == 'changepwd') {
$data = array('email'=>$emailUrl,'applnkact'=>'resetpwd','prc'=>$codeUrl, 'password'=>$passwordUrl, 'password2'=>$password2Url);
$Patient = $this->restGeneratorService->verifyPasswordResetCode($data, $App);
if (!empty($Patient)) {
$errors = $this->restGeneratorService->checkPassword($passwordUrl, $password2Url);
if (empty($errors)) {
$Patient->setPlainPassword($passwordUrl);
$this->restGeneratorService->encodePassword($Patient);
//$em->persist($Patient);
//$em->flush();
$message = 'password changed';
$status = 'success';
$pageBody = '<div class="alert alert-info">'.$message.'</div>';
} else {
$message = implode(' ', $errors);
$pageBody = '<div class="alert alert-danger">'.$message.'</div>';
}
}
} elseif ($action == 'veremail') {
$emailVerified = $this->restGeneratorService->verifyEmailCode($inputData, $App);
if ($emailVerified) {
$message = $this->restGeneratorService->getAppTranslation($appTranslations, 'account_email_verified_message'); // create verification success message
$pageBody = '<div class="alert alert-info">'.$message.'</div>';
} else {
$message = $this->restGeneratorService->getAppTranslation($appTranslations, 'form_validation_incorrect_form_data'); // create verification failure message
$pageBody = '<div class="alert alert-danger">'.$message.'</div>';
}
} elseif ($action == 'resetpwd') {
$label = $this->restGeneratorService->getAppTranslation($appTranslations, 'password_reset_button');
$form = $this->restGeneratorService->buildPasswordChangeForm($inputData, $pageId, $App, $lang);
$form[] = array('variableShortcode'=>'email', 'variableName'=>'', 'variableSuffix'=>'','variableDescription'=>'', 'required'=>true, 'variableType'=>'hidden', 'inputFieldSettings'=>[], 'value'=>$email);
$form[] = array('variableShortcode'=>'prc', 'variableName'=>'', 'variableSuffix'=>'','variableDescription'=>'', 'required'=>true, 'variableType'=>'hidden', 'inputFieldSettings'=>[], 'value'=>$prc);
$form[] = array('variableShortcode'=>'applnkact', 'variableName'=>'', 'variableSuffix'=>'','variableDescription'=>'', 'required'=>true, 'variableType'=>'hidden', 'inputFieldSettings'=>[], 'value'=>'changepwd');
$form[] = array('variableShortcode'=>'submit', 'variableName'=>$label , 'variableSuffix'=>'','variableDescription'=>'', 'required'=>true, 'variableType'=>'button', 'inputFieldSettings'=>['buttonAction'=>'submit'], 'value'=>'');
$pageBody = $this->pageGeneratorService->buildAppFormHtml('post', '', 'password_reset', $form);
}
return $this->render('website/redirect.html.twig',
['childpages'=>$childpages,
'appid'=>$appid,
'code' => $code,
'data' => $data,
'title' => $pageTitle,
'appIcon' => $appIcon,
'body' => $pageBody,
'link' => $link,
'qr' => $qr,
'osFamily' => $osFamily,
'browser' => $browserFamily,
'showAppstores' => $showAppstores,
'urlAppStore' => $urlAppStore,
'urlGooglePlay' => $urlGooglePlay,
'urlHuaweiAppGallery' => $urlHuaweiAppGallery
]);
}
}
}