/** * Returns locale independent base name of the given path. * * @param string $name The new file name * * @return string containing */ protected function getName($name) { $name = parent::getName($name); // This fixes any URL encoded filename and sanitize it $name = strtolower(urldecode($name)); // Replace spaces with a dash $name = preg_replace('!\\s+!', '-', $name); // Remove odd characters return preg_replace('/[^A-Za-z0-9\\-_\\.]/', '', $name); }
public function testGetOriginalName() { $file = new UploadedFile( __DIR__.'/Fixtures/test.gif', 'original.gif', 'image/gif', filesize(__DIR__.'/Fixtures/test.gif'), null ); $this->assertEquals('test.gif', $file->getName()); }
/** * Handle the form submission * * @param Request $request * * @Route("/addFormAnswerAction", name="victoire_contact_form_result") * @return array */ public function addFormAnswerAction(Request $request) { $emailSend = false; $regexErrors = []; if ($request->getMethod() != "POST" && $request->getMethod() != "PUT") { throw $this->createNotFoundException(); } $_taintedValues = $this->getRequest()->request->all()['cms_form_content']; /** @var WidgetForm $widget */ $widget = $this->get('doctrine.orm.entity_manager')->getRepository('VictoireWidgetFormBundle:WidgetForm')->find($_taintedValues['id']); foreach ($_taintedValues['questions'] as $question) { if (in_array($question['type'], array("text", "textarea", "email")) && !empty($question[0])) { $data[] = array('label' => $question["label"], 'value' => $question[0]); if (isset($question['regex']) && !empty($question['regex'])) { $regex = $question['regex']; $regexTitle = null; $regex = "/" . $regex . "/"; $isValid = preg_match($regex, $question[0]); if (isset($question['regexTitle']) && !empty($question['regexTitle'])) { $regexTitle = $question['regexTitle']; } if ($isValid !== 1) { $regexErrors[] = $regexTitle; } } } elseif (in_array($question['type'], array("checkbox", "radio")) && !empty($question['proposal'][0])) { $checkboxValues = $question['proposal']; $data[] = array('label' => $question["label"], 'value' => implode(', ', $checkboxValues)); } elseif ($question['type'] == "date" && !empty($question['Day']) && !empty($question['Month']) && !empty($question['Year'])) { $label = $question["label"]; $data[] = array('label' => $label, 'value' => $question['Day'] . " " . $question['Month'] . " " . $question['Year']); } else { if ($question['type'] == "boolean") { $label = "victoire_widget_form.boolean.false"; if (!empty($question[0])) { $label = "victoire_widget_form.boolean.true"; } $data[] = array('label' => $question["label"], 'value' => $this->get('translator')->trans($label)); } } } ///////////////////////// SEND EMAIL TO ADMIN (set in the form or default one) ////////////////////////////////////////// //$isSpam = $this->testForSpam($taintedValues, $request); $mailer = 'mailer'; $subject = $widget->getTitle(); $targetEmail = $widget->getTargetEmail() ? $widget->getTargetEmail() : $this->container->getParameter('victoire_widget_form.default_email_address'); if ($errors = $this->get('validator')->validateValue($widget->getTargetEmail(), new EmailConstraint())) { try { $from = array($this->container->getParameter('victoire_widget_form.default_email_address') => $this->container->getParameter('victoire_widget_form.default_email_label')); array_push($data, array('label' => 'ip', 'value' => $_SERVER['REMOTE_ADDR'])); $body = $this->renderView('VictoireWidgetFormBundle::managerMailTemplate.html.twig', array('title' => $widget->getTitle(), 'url' => $request->headers->get('referer'), 'data' => $data)); if (sizeof($regexErrors) == 0) { $emailSend = true; $this->createAndSendMail($subject, $from, $targetEmail, $body, 'text/html', null, array(), $mailer); } } catch (\Exception $e) { echo $e->getTraceAsString(); } } ///////////////////////// AUTOANSWER (if email field exists and is filled properly) ////////////////////////////////////////// $email = null; foreach ($_taintedValues['questions'] as $question) { if ($question['label'] == "Email" || $question['label'] == "email") { $email = $question[0]; } } if ($widget->isAutoAnswer() === true && $email) { if ($errors = $this->get('validator')->validateValue($widget->getTargetEmail(), new EmailConstraint())) { try { $urlizer = new Urlizer(); $body = $widget->getMessage(); preg_match_all("/{{(.*?)}}/", $body, $variables); foreach ($variables[1] as $index => $variable) { $pattern = "/" . $variables[0][$index] . "/"; foreach ($_taintedValues["questions"] as $_question) { //Allow exact and urlized term (ex: for a field named Prénom => prenom, Prénom, Prenom are ok) if ($_question['label'] === $variable || $urlizer->urlize($_question['label']) === $urlizer->urlize($variable)) { switch ($_question['type']) { case 'radio': $body = preg_replace($pattern, $_question["proposal"][0], $body); break; case 'checkbox': $body = preg_replace($pattern, implode(', ', $_question["proposal"]), $body); break; case 'date': $body = preg_replace($pattern, $_question['Day'] . " " . $_question['Month'] . " " . $_question['Year'], $body); break; default: //text, textarea $replacement = $_question[0]; $body = preg_replace($pattern, $replacement, $body); } } } //If we didn't found the variable in any field, we cleanup by removing the variable in the body to not appear like buggy to the final user $body = preg_replace($pattern, "", $body); } //Send an email to the customer AND to the specified email target $from = array($this->container->getParameter('victoire_widget_form.default_email_address') => $this->container->getParameter('victoire_widget_form.default_email_label')); $body = $this->renderView('VictoireWidgetFormBundle::customerMailTemplate.html.twig', array('message' => $body)); $attachments = array(); foreach (array('attachmentUrl', 'attachmentUrl2', 'attachmentUrl3', 'attachmentUrl4', 'attachmentUrl5', 'attachmentUrl6', 'attachmentUrl7') as $field) { $getAttachment = 'get' . ucfirst($field); /** @var Media $attachment */ if ($attachment = $widget->{$getAttachment}()) { $filePath = $this->container->getParameter('kernel.root_dir') . '/../web' . $attachment->getUrl(); $attachment = new UploadedFile($filePath, $attachment->getName()); $attachments[] = $attachment; } } if (sizeof($regexErrors) == 0) { $emailSend = true; $this->createAndSendMail($widget->getSubject(), $from, $email, $body, 'text/html', $widget->getTargetemail(), $attachments, $mailer); } } catch (\Exception $exc) { echo $exc->getTraceAsString(); } } } ///////////////////////// BUILD REDIRECT URL ACCORDING TO SUCCESS CALLBACK ///////////////////////////////////// $redirectUrl = null; if ($emailSend) { if ($widget->getSuccessCallback() == 'notification') { $message = $widget->getSuccessMessage() != "" ? $widget->getSuccessMessage() : $this->get('translator')->trans('victoire_widget_form.alert.send.email.success.label'); $this->container->get('appventus_alertifybundle.helper.alertifyhelper')->congrat($message); } else { if ($link = $widget->getLink()) { $redirectUrl = $this->get('victoire_widget.twig.link_extension')->victoireLinkUrl($link->getParameters()); } } } else { if ($widget->getErrorNotification() == true) { $message = $widget->getErrorMessage() != "" ? $widget->getErrorMessage() : $this->get('translator')->trans('victoire_widget_form.alert.send.email.error.label'); $this->container->get('appventus_alertifybundle.helper.alertifyhelper')->scold($message); } } foreach ($regexErrors as $key => $error) { if ($error != '') { $this->container->get('appventus_alertifybundle.helper.alertifyhelper')->scold($error); } } $redirectUrl = $redirectUrl ?: $request->headers->get('referer'); return $this->redirect($redirectUrl); }
/** * Handle the form submission * * @param Request $request * * @Route("/addFormAnswerAction", name="patrimea_result") * @return array */ public function addFormAnswerAction(Request $request) { $emailSend = false; $regexErrors = []; if ($request->getMethod() != "POST" && $request->getMethod() != "PUT") { throw $this->createNotFoundException(); } $taintedValues = $this->getRequest()->request->all()['cms_form_content']; foreach ($taintedValues['questions'] as $question) { if (in_array($question['type'], array("text", "textarea")) && !empty($question[0])) { $data[] = array('label' => $question["label"], 'value' => $question[0]); if (isset($question['regex']) && !empty($question['regex'])) { $regex = $question['regex']; $regexTitle = null; $regex = "/" . $regex . "/"; $isValid = preg_match($regex, $question[0]); if (isset($question['regexTitle']) && !empty($question['regexTitle'])) { $regexTitle = $question['regexTitle']; } if ($isValid !== 1) { $regexErrors[] = $regexTitle; } } } elseif (in_array($question['type'], array("checkbox", "radio")) && !empty($question['proposal'][0])) { $checkboxValues = $question['proposal']; $data[] = array('label' => $question["label"], 'value' => implode(', ', $checkboxValues)); } elseif ($question['type'] == "date" && !empty($question['Day']) && !empty($question['Month']) && !empty($question['Year'])) { $label = $question["label"]; $data[] = array('label' => $label, 'value' => $question['Day'] . " " . $question['Month'] . " " . $question['Year']); } else { if ($question['type'] == "boolean") { $label = "victoire_widget_form.boolean.false"; if (!empty($question[0])) { $label = "victoire_widget_form.boolean.true"; } $data[] = array('label' => $question["label"], 'value' => $this->get('translator')->trans($label)); } } } $isSpam = $this->testValues($taintedValues, $request); $mailer = 'mailer'; $subject = $taintedValues['title']; if (isset($taintedValues['targetEmail']) && !empty($taintedValues['targetEmail'])) { $targetEmail = !empty($taintedValues['targetEmail']) ? $taintedValues['targetEmail'] : $this->container->getParameter('victoire_widget_form.default_email_address'); if ($errors = $this->get('validator')->validateValue($taintedValues['targetEmail'], new EmailConstraint())) { try { $to = $targetEmail; $from = array($this->container->getParameter('victoire_widget_form.default_email_address') => $this->container->getParameter('victoire_widget_form.default_email_label')); array_push($data, array('label' => 'ip', 'value' => $_SERVER['REMOTE_ADDR'])); $body = $this->renderView('VictoireWidgetFormBundle::managerMailTemplate.html.twig', array('title' => $taintedValues['title'], 'url' => $request->headers->get('referer'), 'data' => $data)); if (sizeof($regexErrors) == 0) { $emailSend = true; $this->createAndSendMail($subject, $from, $to, $body, 'text/html', null, array(), $mailer); } } catch (Exception $exc) { echo $exc->getTraceAsString(); } } } $email = null; foreach ($taintedValues['questions'] as $question) { if ($question['label'] == "Email" || $question['label'] == "email") { $email = $question[0]; } } if (!empty($taintedValues['autoAnswer']) && $taintedValues['autoAnswer'] == true && !empty($email)) { if ($errors = $this->get('validator')->validateValue($taintedValues['targetEmail'], new EmailConstraint())) { try { $body = $taintedValues['message']; preg_match_all("/{{.*?}}/", $body, $variables); foreach ($variables[0] as $variable) { if (!empty($taintedValues["questions"][$this->slugify($variable)])) { if (in_array($taintedValues["questions"][$this->slugify($variable)]['type'], array("text", "textarea")) && !empty($taintedValues["questions"][$this->slugify($variable)][0])) { $body = preg_replace("/{$variable}/", $taintedValues["questions"][$this->slugify($variable)][0], $body); } elseif ($taintedValues["questions"][$this->slugify($variable)]['type'] == "radio" && !empty($taintedValues["questions"][$this->slugify($variable)]["proposal"][0])) { $body = preg_replace("/{$variable}/", $taintedValues["questions"][$this->slugify($variable)]["proposal"][0], $body); } elseif ($taintedValues["questions"][$this->slugify($variable)]['type'] == "checkbox" && !empty($taintedValues["questions"][$this->slugify($variable)]["proposal"])) { $body = preg_replace("/{$variable}/", implode(', ', $taintedValues["questions"][$this->slugify($variable)]['proposal']), $body); } elseif ($taintedValues["questions"][$this->slugify($variable)]['type'] == "date" && !empty($taintedValues["questions"][$this->slugify($variable)]['Day']) && !empty($taintedValues["questions"][$this->slugify($variable)]['Month']) && !empty($taintedValues["questions"][$this->slugify($variable)]['Year'])) { $body = preg_replace("/{$variable}/", $taintedValues["questions"][$this->slugify($variable)]['Day'] . " " . $taintedValues["questions"][$this->slugify($variable)]['Month'] . " " . $taintedValues["questions"][$this->slugify($variable)]['Year'], $body); } $body = preg_replace("/{$variable}/", "", $body); } } //Send an email to the customer AND to the specified email target $to = $email; if ($this->container->getParameter('victoire_widget_form.default_bcc_email_address', null)) { $replyTo = $this->container->getParameter('victoire_widget_form.default_bcc_email_address'); } $from = array($this->container->getParameter('victoire_widget_form.default_email_address') => $this->container->getParameter('victoire_widget_form.default_email_label')); $subject = $taintedValues['subject']; $body = $this->renderView('VictoireWidgetFormBundle::customerMailTemplate.html.twig', array('message' => $body)); $em = $this->getDoctrine()->getManager(); $mediaRepo = $em->getRepository('\\Victoire\\Bundle\\MediaBundle\\Entity\\Media'); $attachments = array(); foreach (array('attachmentUrl', 'attachmentUrl2', 'attachmentUrl3', 'attachmentUrl4', 'attachmentUrl5', 'attachmentUrl6', 'attachmentUrl7') as $field) { if (!empty($taintedValues[$field])) { $file = $mediaRepo->findOneById($taintedValues[$field]); $filePath = $this->container->getParameter('kernel.root_dir') . '/../web' . $file->getUrl(); $file = new UploadedFile($filePath, $file->getName()); $attachments[] = $file; } } if (sizeof($regexErrors) == 0) { $emailSend = true; $this->createAndSendMail($subject, $from, $to, $body, 'text/html', null, $attachments, $mailer); } } catch (Exception $exc) { echo $exc->getTraceAsString(); } } } if ($emailSend) { if ($taintedValues['successNotification'] == true) { $message = $taintedValues['successMessage'] != "" ? $taintedValues['successMessage'] : $this->get('translator')->trans('victoire_widget_form.alert.send.email.success.label'); $this->container->get('appventus_alertifybundle.helper.alertifyhelper')->congrat($message); } } else { if ($taintedValues['errorNotification'] == true) { $message = $taintedValues['errorMessage'] != "" ? $taintedValues['errorMessage'] : $this->get('translator')->trans('victoire_widget_form.alert.send.email.error.label'); $this->container->get('appventus_alertifybundle.helper.alertifyhelper')->scold($message); } } foreach ($regexErrors as $key => $error) { if ($error != '') { $this->container->get('appventus_alertifybundle.helper.alertifyhelper')->scold($error); } } $referer = $this->getRequest()->headers->get('referer'); return $this->redirect($referer); }