Exemplo n.º 1
0
 public static function getBaseRoute($path)
 {
     if (StringHandler::endsWith($path, "Locale")) {
         $path = StringHandler::replaceLast("Locale", "", $path);
     }
     return $path;
 }
Exemplo n.º 2
0
 /**
  * @param string $name
  *
  * @return $this
  */
 public function setName($name)
 {
     $this->name = trim(strtolower($name));
     $this->name = StringHandler::removeDiacritics($this->name);
     $this->name = preg_replace('#([^a-z])#', '_', $this->name);
     return $this;
 }
Exemplo n.º 3
0
 /**
  * @param string $name
  *
  * @return string $name
  */
 public static function cleanName($name)
 {
     $name = StringHandler::variablize($name);
     if (0 === preg_match("/^role_/i", $name)) {
         $name = "ROLE_" . $name;
     }
     return strtoupper($name);
 }
 public function validate($value, Constraint $constraint)
 {
     $value = StringHandler::variablize($value);
     /*
      * If value is already the node name
      * do nothing.
      */
     if (null !== $constraint->currentValue && $value == $constraint->currentValue) {
         return;
     }
     if (null !== $constraint->entityManager && null !== $constraint->customForm) {
         if (true === $this->nameExists($value, $constraint->customForm, $constraint->entityManager)) {
             $this->context->addViolation($constraint->message);
         }
     } else {
         $this->context->addViolation('UniqueCustomFormFieldNameValidator constraint requires a valid EntityManager');
     }
 }
 public function validate($value, Constraint $constraint)
 {
     $value = StringHandler::slugify($value);
     /*
      * If value is already the node name
      * do nothing.
      */
     if (null !== $constraint->currentValue && $value == $constraint->currentValue) {
         return;
     }
     if (null !== $constraint->entityManager) {
         if (true === $this->urlAliasExists($value, $constraint->entityManager)) {
             $this->context->addViolation($constraint->messageUrlAlias);
         } elseif (true === $this->nodeNameExists($value, $constraint->entityManager)) {
             $this->context->addViolation($constraint->message);
         }
     } else {
         $this->context->addViolation('UniqueNodeNameValidator constraint requires a valid EntityManager');
     }
 }
Exemplo n.º 6
0
 /**
  * @param array                           $data
  * @param RZ\Roadiz\Core\Entities\UrlAlias $ua
  *
  * @return boolean
  */
 private function editUrlAlias($data, UrlAlias $ua)
 {
     $testingAlias = StringHandler::slugify($data['alias']);
     if ($testingAlias != $ua->getAlias() && ($this->nodeNameExists($testingAlias) || $this->urlAliasExists($testingAlias))) {
         $msg = $this->getTranslator()->trans('url_alias.%alias%.no_update.already_exists', ['%alias%' => $data['alias']]);
         throw new EntityAlreadyExistsException($msg, 1);
     }
     if ($data['urlaliasId'] == $ua->getId()) {
         try {
             $ua->setAlias($data['alias']);
             $this->getService('em')->flush();
             return true;
         } catch (\Exception $e) {
             return false;
         }
     }
     return false;
 }
Exemplo n.º 7
0
 /**
  * Get controller class path for a given node.
  *
  * @param Node $node
  *
  * @return string
  */
 public function getControllerForNode(Node $node)
 {
     $currentClass = get_class($this);
     $refl = new \ReflectionClass($currentClass);
     $namespace = $refl->getNamespaceName() . '\\Controllers';
     /*
      * Determine if we look for a node-type named controller or
      * a node-named controller.
      */
     if (in_array($node->getNodeName(), static::$specificNodesControllers)) {
         return $namespace . '\\' . StringHandler::classify($node->getNodeName()) . 'Controller';
     } else {
         return $namespace . '\\' . StringHandler::classify($node->getNodeType()->getName()) . 'Controller';
     }
 }
Exemplo n.º 8
0
 private function updateDocument($data, $document)
 {
     $fs = new Filesystem();
     if (!empty($data['newDocument'])) {
         $uploadedFile = $data['newDocument'];
         if ($uploadedFile !== null && $uploadedFile->getError() == UPLOAD_ERR_OK && $uploadedFile->isValid()) {
             /*
              * In case file already exists
              */
             if ($fs->exists($document->getAbsolutePath())) {
                 $fs->remove($document->getAbsolutePath());
             }
             if (StringHandler::cleanForFilename($uploadedFile->getClientOriginalName()) == $document->getFilename()) {
                 $finder = new Finder();
                 $previousFolder = $document->getFilesFolder() . '/' . $document->getFolder();
                 if ($fs->exists($previousFolder)) {
                     $finder->files()->in($previousFolder);
                     // Remove Precious folder if it's empty
                     if ($finder->count() == 0) {
                         $fs->remove($previousFolder);
                     }
                 }
                 $document->setFolder(substr(hash("crc32b", date('YmdHi')), 0, 12));
             }
             $document->setFilename($uploadedFile->getClientOriginalName());
             $document->setMimeType($uploadedFile->getMimeType());
             $this->getService('em')->flush();
             $uploadedFile->move(Document::getFilesFolder() . '/' . $document->getFolder(), $document->getFilename());
             return $document;
         }
     }
     return $document;
 }
Exemplo n.º 9
0
 /**
  * Return a ZipArchive of requested font.
  *
  * @param Symfony\Component\HttpFoundation\Request $request
  * @param int                                      $fontId
  *
  * @return Symfony\Component\HttpFoundation\Response
  */
 public function downloadAction(Request $request, $fontId)
 {
     $this->validateAccessForRole('ROLE_ACCESS_FONTS');
     $font = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Font', (int) $fontId);
     if ($font !== null) {
         // Prepare File
         $file = tempnam("tmp", "zip");
         $zip = new \ZipArchive();
         $zip->open($file, \ZipArchive::OVERWRITE);
         if ("" != $font->getEOTFilename()) {
             $zip->addFile($font->getEOTAbsolutePath(), $font->getEOTFilename());
         }
         if ("" != $font->getSVGFilename()) {
             $zip->addFile($font->getSVGAbsolutePath(), $font->getSVGFilename());
         }
         if ("" != $font->getWOFFFilename()) {
             $zip->addFile($font->getWOFFAbsolutePath(), $font->getWOFFFilename());
         }
         if ("" != $font->getWOFF2Filename()) {
             $zip->addFile($font->getWOFF2AbsolutePath(), $font->getWOFF2Filename());
         }
         if ("" != $font->getOTFFilename()) {
             $zip->addFile($font->getOTFAbsolutePath(), $font->getOTFFilename());
         }
         // Close and send to users
         $zip->close();
         $filename = StringHandler::slugify($font->getName() . ' ' . $font->getReadableVariant()) . '.zip';
         $response = new Response(file_get_contents($file), Response::HTTP_OK, ['content-control' => 'private', 'content-type' => 'application/zip', 'content-length' => filesize($file), 'content-disposition' => 'attachment; filename=' . $filename]);
         unlink($file);
         return $response;
     } else {
         return $this->throw404();
     }
 }
Exemplo n.º 10
0
 /**
  * Returns an option array for creating a Symfony Form
  * according to a node-type field.
  *
  * @param  NodesSources  $nodeSource
  * @param  NodeTypeField $field
  *
  * @return array
  */
 public function getFormOptionsFromFieldType(NodeTypeField $field)
 {
     $label = $field->getLabel();
     $devName = '{{ nodeSource.' . StringHandler::camelCase($field->getName()) . ' }}';
     switch ($field->getType()) {
         case NodeTypeField::ENUM_T:
             return ['label' => $label, 'placeholder' => 'choose.value', 'required' => false, 'attr' => ['data-desc' => $field->getDescription(), 'data-dev-name' => $devName]];
         case NodeTypeField::DATETIME_T:
             return ['date_widget' => 'single_text', 'date_format' => 'yyyy-MM-dd', 'label' => $label, 'required' => false, 'attr' => ['data-desc' => $field->getDescription(), 'data-dev-name' => $devName, 'class' => 'rz-datetime-field'], 'placeholder' => ['hour' => 'hour', 'minute' => 'minute']];
         case NodeTypeField::DATE_T:
             return ['widget' => 'single_text', 'format' => 'yyyy-MM-dd', 'label' => $label, 'required' => false, 'attr' => ['data-desc' => $field->getDescription(), 'data-dev-name' => $devName, 'class' => 'rz-date-field'], 'placeholder' => ''];
         case NodeTypeField::INTEGER_T:
             return ['label' => $label, 'required' => false, 'constraints' => [new Type('integer')], 'attr' => ['data-desc' => $field->getDescription(), 'data-dev-name' => $devName]];
         case NodeTypeField::EMAIL_T:
             return ['label' => $label, 'required' => false, 'constraints' => [new \Symfony\Component\Validator\Constraints\Email()], 'attr' => ['data-desc' => $field->getDescription(), 'data-dev-name' => $devName]];
         case NodeTypeField::DECIMAL_T:
             return ['label' => $label, 'required' => false, 'constraints' => [new Type('double')], 'attr' => ['data-desc' => $field->getDescription(), 'data-dev-name' => $devName]];
         case NodeTypeField::COLOUR_T:
             return ['label' => $label, 'required' => false, 'attr' => ['data-desc' => $field->getDescription(), 'data-dev-name' => $devName, 'class' => 'colorpicker-input']];
         case NodeTypeField::GEOTAG_T:
             return ['label' => $label, 'required' => false, 'attr' => ['data-desc' => $field->getDescription(), 'data-dev-name' => $devName, 'class' => 'rz-geotag-field']];
         case NodeTypeField::MARKDOWN_T:
             return ['label' => $label, 'required' => false, 'attr' => ['class' => 'markdown_textarea', 'data-desc' => $field->getDescription(), 'data-min-length' => $field->getMinLength(), 'data-max-length' => $field->getMaxLength(), 'data-dev-name' => $devName]];
         default:
             return ['label' => $label, 'required' => false, 'attr' => ['data-desc' => $field->getDescription(), 'data-min-length' => $field->getMinLength(), 'data-max-length' => $field->getMaxLength(), 'data-dev-name' => $devName]];
     }
 }
Exemplo n.º 11
0
 /**
  * @param string $name
  *
  * @return $this
  */
 public function setName($name)
 {
     $this->name = StringHandler::slugify($name);
     return $this;
 }
Exemplo n.º 12
0
 /**
  * @param string $filename
  *
  * @return $this
  */
 public function setFilename($filename)
 {
     $this->filename = StringHandler::cleanForFilename($filename);
     return $this;
 }
Exemplo n.º 13
0
 /**
  * @param string $tagName
  *
  * @return $this
  */
 public function setTagName($tagName)
 {
     $this->dirtyTagName = $tagName;
     $this->tagName = StringHandler::slugify($tagName);
     return $this;
 }
Exemplo n.º 14
0
 /**
  * @param string $svgFilename
  *
  * @return $this
  */
 public function setSVGFilename($svgFilename)
 {
     $this->svgFilename = StringHandler::cleanForFilename($svgFilename);
     return $this;
 }
Exemplo n.º 15
0
 public function searchAction(Request $request)
 {
     /*
      * Validate
      */
     if (true !== ($notValid = $this->validateRequest($request, 'GET'))) {
         return new Response(json_encode($notValid), Response::HTTP_OK, ['content-type' => 'application/javascript']);
     }
     $this->validateAccessForRole('ROLE_ACCESS_TAGS');
     $responseArray = ['statusCode' => Response::HTTP_NOT_FOUND, 'status' => 'danger', 'responseText' => 'No tags found'];
     if ($request->get('search') != "") {
         $responseArray = [];
         $pattern = strip_tags($request->get('search'));
         $tags = $this->getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Tag')->searchBy($pattern, [], [], 10);
         if (0 === count($tags)) {
             /*
              * Try again using tag slug
              */
             $pattern = StringHandler::slugify($pattern);
             $tags = $this->getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Tag')->searchBy($pattern, [], [], 10);
         }
         foreach ($tags as $tag) {
             $responseArray[] = $tag->getHandler()->getFullPath();
         }
     }
     return new Response(json_encode($responseArray), Response::HTTP_OK, ['content-type' => 'application/javascript']);
 }
Exemplo n.º 16
0
 /**
  * @param string $alias
  *
  * @return $this
  */
 public function setAlias($alias)
 {
     $this->alias = StringHandler::slugify($alias);
     return $this;
 }
Exemplo n.º 17
0
 /**
  * Get common options for your node-type field form components.
  *
  * @param  NodeTypeField $field
  *
  * @return array
  */
 public function getDefaultOptions(NodeTypeField $field)
 {
     $label = $field->getLabel();
     $devName = '{{ nodeSource.' . StringHandler::camelCase($field->getName()) . ' }}';
     $options = ['label' => $label, 'required' => false, 'attr' => ['data-field-group' => null !== $field->getGroupName() && '' != $field->getGroupName() ? $field->getGroupName() : 'default', 'data-dev-name' => $devName, 'autocomplete' => 'off']];
     if ('' !== $field->getDescription()) {
         $options['attr']['data-desc'] = $field->getDescription();
     }
     if ($field->getMinLength() > 0) {
         $options['attr']['data-min-length'] = $field->getMinLength();
     }
     if ($field->getMaxLength() > 0) {
         $options['attr']['data-max-length'] = $field->getMaxLength();
     }
     return $options;
 }
Exemplo n.º 18
0
 /**
  * Return a ZipArchive of requested folder.
  *
  * @param Symfony\Component\HttpFoundation\Request $request
  * @param int                                      $folderId
  *
  * @return Symfony\Component\HttpFoundation\Response
  */
 public function downloadAction(Request $request, $folderId)
 {
     $this->validateAccessForRole('ROLE_ACCESS_DOCUMENTS');
     $folder = $this->getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Folder', (int) $folderId);
     if ($folder !== null) {
         // Prepare File
         $file = tempnam("tmp", "zip");
         $zip = new \ZipArchive();
         $zip->open($file, \ZipArchive::OVERWRITE);
         $documents = $this->getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Document')->findBy(['folders' => [$folder]]);
         foreach ($documents as $document) {
             $zip->addFile($document->getAbsolutePath(), $document->getFilename());
         }
         // Close and send to users
         $zip->close();
         $filename = StringHandler::slugify($folder->getName()) . '.zip';
         $response = new Response(file_get_contents($file), Response::HTTP_OK, ['content-type' => 'application/zip', 'content-length' => filesize($file), 'content-disposition' => 'attachment; filename=' . $filename]);
         unlink($file);
         return $response;
     } else {
         return $this->throw404();
     }
 }
Exemplo n.º 19
0
 /**
  * @dataProvider encodeWithSecretNoSaltProvider
  */
 public function testEncodeWithSecretNoSalt($input, $secret)
 {
     $this->setExpectedException('RZ\\Roadiz\\Core\\Exceptions\\EmptySaltException');
     $code = StringHandler::encodeWithSecret($input, $secret);
     // Assert
     $this->assertEquals($input, StringHandler::decodeWithSecret($code, $secret));
 }
Exemplo n.º 20
0
 /**
  * Sets the value of overrideLocale.
  *
  * @param string $overrideLocale the override locale
  *
  * @return self
  */
 public function setOverrideLocale($overrideLocale)
 {
     $this->overrideLocale = StringHandler::slugify($overrideLocale);
     return $this;
 }
Exemplo n.º 21
0
 /**
  * @return string
  */
 public function getSetterName()
 {
     return StringHandler::camelCase('set ' . $this->getName());
 }
Exemplo n.º 22
0
 /**
  * Return the current Theme
  *
  * @return \RZ\Roadiz\Core\Entities\Theme
  */
 public function getTheme()
 {
     if (null === $this->theme) {
         $className = static::getCalledClass();
         while (!StringHandler::endsWith($className, "App")) {
             $className = get_parent_class($className);
             if ($className === false) {
                 $className = "";
                 break;
             }
             if (strpos($className, "\\") !== 0) {
                 $className = "\\" . $className;
             }
         }
         $this->theme = $this->getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Theme')->findOneByClassName($className);
     }
     return $this->theme;
 }
Exemplo n.º 23
0
 /**
  * Generate a form-builder for contact forms.
  *
  * For example in your contact page controller :
  *
  * <pre>
  * use RZ\Roadiz\CMS\Controllers\EntryPointsController;
  *
  * ...
  *
  * $formBuilder = EntryPointsController::getContactFormBuilder(
  *     $request,
  *     true
  * );
  * $formBuilder->add('email', 'email', array(
  *                 'label'=>$this->getTranslator()->trans('your.email')
  *             ))
  *             ->add('name', 'text', array(
  *                 'label'=>$this->getTranslator()->trans('your.name')
  *             ))
  *             ->add('message', 'textarea', array(
  *                 'label'=>$this->getTranslator()->trans('your.message')
  *             ))
  *             ->add('callMeBack', 'checkbox', array(
  *                 'label'=>$this->getTranslator()->trans('call.me.back'),
  *                 'required' => false
  *             ))
  *             ->add('send', 'submit', array(
  *                 'label'=>$this->getTranslator()->trans('send.contact.form')
  *             ));
  * $form = $formBuilder->getForm();
  * $this->assignation['contactForm'] = $form->createView();
  *
  * </pre>
  *
  * Add session messages to your assignations
  *
  * <pre>
  * // Get session messages
  * $this->assignation['session']['messages'] = $this->getService('session')->getFlashBag()->all();
  * </pre>
  *
  * Then in your contact page Twig template
  *
  * <pre>
  * {#
  *  # Display contact errors
  *  #}
  * {% if session.messages|length %}
  *     {% for type, msgs in session.messages %}
  *         {% for msg in msgs %}
  *             <div data-uk-alert class="uk-alert
  *                                       uk-alert-{% if type == "confirm" %}success
  *                                       {% elseif type == "warning" %}warning{% else %}danger{% endif %}">
  *                 <a href="" class="uk-alert-close uk-close"></a>
  *                 <p>{{ msg }}</p>
  *             </div>
  *         {% endfor %}
  *     {% endfor %}
  * {% endif %}
  * {#
  *  # Display contact form
  *  #}
  * {% form_theme contactForm 'forms.html.twig' %}
  * {{ form(contactForm) }}
  * </pre>
  *
  * @param Symfony\Component\HttpFoundation\Request $request             Contact page request
  * @param boolean                                  $redirect            Redirect to contact page after sending?
  * @param string                                   $customRedirectUrl   Redirect to a custom url
  * @param string                                   $customEmailReceiver Send contact form to a custom email (or emails)
  * @param string                                   $customEmailSubject  Customize email subject
  *
  * @return Symfony\Component\Form\FormBuilder
  */
 public static function getContactFormBuilder(Request $request, $redirect = true, $customRedirectUrl = null, $customEmailReceiver = null, $customEmailSubject = null)
 {
     $action = Kernel::getService('urlGenerator')->generate('contactFormLocaleAction', ['_locale' => $request->getLocale()]);
     $builder = Kernel::getService('formFactory')->createBuilder('form', null, ['intention' => static::CONTACT_FORM_TOKEN_INTENTION, 'attr' => ['id' => 'contactForm']])->setMethod('POST')->setAction($action)->add('_action', 'hidden', ['data' => 'contact']);
     if (true === $redirect) {
         if (null !== $customRedirectUrl) {
             $builder->add('_redirect', 'hidden', ['data' => strip_tags($customRedirectUrl)]);
         } else {
             $builder->add('_redirect', 'hidden', ['data' => strip_tags($request->getURI())]);
         }
     }
     if (null !== $customEmailReceiver) {
         $builder->add('_emailReceiver', 'hidden', ['data' => StringHandler::encodeWithSecret($customEmailReceiver, Kernel::getService('config')['security']['secret'])]);
     }
     if (null !== $customEmailSubject) {
         $builder->add('_emailSubject', 'hidden', ['data' => StringHandler::encodeWithSecret($customEmailSubject, Kernel::getService('config')['security']['secret'])]);
     }
     return $builder;
 }