<commit_before><?php
namespace Concrete\Block\ExpressForm;

use Concrete\Controller\Element\Attribute\KeyList;
use Concrete\Core\Attribute\Category\ExpressCategory;
use Concrete\Core\Attribute\Type;
use Concrete\Core\Block\BlockController;
use Concrete\Core\Entity\Attribute\Key\ExpressKey;
use Concrete\Core\Entity\Express\Control\AttributeKeyControl;
use Concrete\Core\Entity\Express\Entity;
use Concrete\Core\Entity\Express\Entry;
use Concrete\Core\Entity\Express\FieldSet;
use Concrete\Core\Entity\Express\Form;
use Concrete\Core\Express\Form\Control\SaveHandler\SaveHandlerInterface;
use Concrete\Core\Express\Form\Renderer;
use Concrete\Core\Express\Form\Validator;
use Concrete\Core\File\FileProviderInterface;
use Concrete\Core\File\Set\Set;
use Concrete\Core\Http\ResponseAssetGroup;
use Concrete\Core\Routing\Redirect;
use Concrete\Core\Tree\Node\Node;
use Concrete\Core\Tree\Node\NodeType;
use Concrete\Core\Tree\Node\Type\Category;
use Concrete\Core\Tree\Type\ExpressEntryResults;
use Doctrine\ORM\Id\UuidGenerator;
use Symfony\Component\HttpFoundation\JsonResponse;

class Controller extends BlockController
{
    protected $btInterfaceWidth = 640;
    protected $btCacheBlockOutput = true;
    protected $btInterfaceHeight = 480;
    protected $btTable = 'btExpressForm';
    protected $entityManager;

    const FORM_RESULTS_CATEGORY_NAME = 'Forms';

    /**
     * Used for localization. If we want to localize the name/description we have to include this.
     *
     * @return string
     */
    public function getBlockTypeDescription()
    {
        return t("Build simple forms and surveys.");
    }

    public function getBlockTypeName()
    {
        return t("Express Form");
    }

    protected function clearSessionControls()
    {
        $session = \Core::make('session');
        $session->remove('block.express_form.new');
    }

    public function add()
    {
        $c = \Page::getCurrentPage();
        $this->set('formName', $c->getCollectionName());
        $this->set('submitLabel', t('Submit'));
        $this->set('thankyouMsg', t('Thanks!'));
        $this->edit();
        $this->set('resultsFolder', $this->get('formResultsRootFolderNodeID'));
    }

    public function action_form_success($bID = null)
    {
        if ($this->bID == $bID) {
            $this->set('success', $this->thankyouMsg);
            $this->view();
        }
    }

    public function delete()
    {
        parent::delete();
        $entity = $this->getFormEntity()->getEntity();
        $entityManager = \Core::make('database/orm')->entityManager();
        $entityManager->remove($entity);
        $entityManager->flush();
    }


    public function action_submit($bID = null)
    {
        if ($this->bID == $bID) {
            $entityManager = \Core::make('database/orm')->entityManager();
            $form = $this->getFormEntity();
            if (is_object($form)) {
                $e = \Core::make('error');
                $validator = new Validator($e, $this->request);
                $validator->validate($form);
                if ($this->displayCaptcha) {
                    $captcha = \Core::make('helper/validation/captcha');
                    if (!$captcha->check()) {
                        $e->add(t('Incorrect captcha code.'));
                    }
                }
                $this->set('error', $e);
            }
        }

        if (isset($e) && !$e->has()) {

            $entity = $form->getEntity();
            $entry = new Entry();
            $entry->setEntity($entity);
            $entityManager->persist($entry);
            foreach ($form->getControls() as $control) {
                $type = $control->getControlType();
                $saver = $type->getSaveHandler($control);
                if ($saver instanceof SaveHandlerInterface) {
                    $saver->saveFromRequest($control, $entry, $this->request);
                }
            }

            $values = $entity->getAttributeKeyCategory()->getAttributeValues($entry);

            if ($this->addFilesToSet) {
                $set = Set::getByID($this->addFilesToSet);
                if (is_object($set)) {
                    foreach($values as $value) {
                        $value = $value->getValueObject();
                        if ($value instanceof FileProviderInterface) {
                            $files = $value->getFileObjects();
                            foreach($files as $file) {
                                $set->addFileToSet($file);
                            }
                        }
                    }
                }
            }

            if ($this->notifyMeOnSubmission) {
                if (\Config::get('concrete.email.form_block.address') && strstr(Config::get('concrete.email.form_block.address'), '@')) {
                    $formFormEmailAddress = \Config::get('concrete.email.form_block.address');
                } else {
                    $adminUserInfo = \UserInfo::getByID(USER_SUPER_ID);
                    $formFormEmailAddress = $adminUserInfo->getUserEmail();<commit_msg>adding antispam to form<commit_after><?php
namespace Concrete\Block\ExpressForm;

use Concrete\Controller\Element\Attribute\KeyList;
use Concrete\Core\Attribute\Category\ExpressCategory;
use Concrete\Core\Attribute\Type;
use Concrete\Core\Block\BlockController;
use Concrete\Core\Entity\Attribute\Key\ExpressKey;
use Concrete\Core\Entity\Express\Control\AttributeKeyControl;
use Concrete\Core\Entity\Express\Entity;
use Concrete\Core\Entity\Express\Entry;
use Concrete\Core\Entity\Express\FieldSet;
use Concrete\Core\Entity\Express\Form;
use Concrete\Core\Express\Form\Control\SaveHandler\SaveHandlerInterface;
use Concrete\Core\Express\Form\Renderer;
use Concrete\Core\Express\Form\Validator;
use Concrete\Core\File\FileProviderInterface;
use Concrete\Core\File\Set\Set;
use Concrete\Core\Http\ResponseAssetGroup;
use Concrete\Core\Routing\Redirect;
use Concrete\Core\Tree\Node\Node;
use Concrete\Core\Tree\Node\NodeType;
use Concrete\Core\Tree\Node\Type\Category;
use Concrete\Core\Tree\Type\ExpressEntryResults;
use Doctrine\ORM\Id\UuidGenerator;
use Symfony\Component\HttpFoundation\JsonResponse;

class Controller extends BlockController
{
    protected $btInterfaceWidth = 640;
    protected $btCacheBlockOutput = true;
    protected $btInterfaceHeight = 480;
    protected $btTable = 'btExpressForm';
    protected $entityManager;

    const FORM_RESULTS_CATEGORY_NAME = 'Forms';

    /**
     * Used for localization. If we want to localize the name/description we have to include this.
     *
     * @return string
     */
    public function getBlockTypeDescription()
    {
        return t("Build simple forms and surveys.");
    }

    public function getBlockTypeName()
    {
        return t("Express Form");
    }

    protected function clearSessionControls()
    {
        $session = \Core::make('session');
        $session->remove('block.express_form.new');
    }

    public function add()
    {
        $c = \Page::getCurrentPage();
        $this->set('formName', $c->getCollectionName());
        $this->set('submitLabel', t('Submit'));
        $this->set('thankyouMsg', t('Thanks!'));
        $this->edit();
        $this->set('resultsFolder', $this->get('formResultsRootFolderNodeID'));
    }

    public function action_form_success($bID = null)
    {
        if ($this->bID == $bID) {
            $this->set('success', $this->thankyouMsg);
            $this->view();
        }
    }

    public function delete()
    {
        parent::delete();
        $entity = $this->getFormEntity()->getEntity();
        $entityManager = \Core::make('database/orm')->entityManager();
        $entityManager->remove($entity);
        $entityManager->flush();
    }


    public function action_submit($bID = null)
    {
        if ($this->bID == $bID) {
            $entityManager = \Core::make('database/orm')->entityManager();
            $form = $this->getFormEntity();
            if (is_object($form)) {
                $e = \Core::make('error');
                $validator = new Validator($e, $this->request);
                $validator->validate($form);
                if ($this->displayCaptcha) {
                    $captcha = \Core::make('helper/validation/captcha');
                    if (!$captcha->check()) {
                        $e->add(t('Incorrect captcha code.'));
                    }
                }
                $this->set('error', $e);
            }
        }

        if (isset($e) && !$e->has()) {

            $entity = $form->getEntity();
            $entry = new Entry();
            $entry->setEntity($entity);
            $entityManager->persist($entry);
            foreach ($form->getControls() as $control) {
                $type = $control->getControlType();
                $saver = $type->getSaveHandler($control);
                if ($saver instanceof SaveHandlerInterface) {
                    $saver->saveFromRequest($control, $entry, $this->request);
                }
            }

            $values = $entity->getAttributeKeyCategory()->getAttributeValues($entry);

            // Check antispam
            $antispam = \Core::make('helper/validation/antispam');
            $submittedData = '';
            foreach($values as $value) {
                $submittedData .= $value->getAttributeKey()->getAttributeKeyDisplayName() . "\r\n";
                $submittedData .= $value->getValue('displaySanitized') . "\r\n\r\n";
            }

            if (!$antispam->check($submittedData, 'form_block')) {
                // Remove the entry and silently fail.
                $entityManager->refresh($entry);
                $entityManager->remove($entry);
                $entityManager->flush();
                $c = \Page::getCurrentPage();
                $r = Redirect::to($c->getCollectionPath());
                $r->setTargetUrl($r->getTargetUrl() . '#form' . $this->bID);
            }

            if ($this->addFilesToSet) {
                $set = Set::getByID($this->addFilesToSet);
                if (is_object($set)) {
                    foreach($values as $value) {
                        $value = $value->getValueObject();
                        if ($value instanceof FileProviderInterface) {
                            $files = $value->getFileObjects();
                            foreach($files as $file) {
                                $set->addFileToSet($file);
                            }
                        }
                    }
                }
            }

            if ($this->notifyMeOnSubmission) {
                if (\Config::get('concrete.email.form_block.address') && strstr(Config::get('concrete.email.form_block.address'), '@')) {
                    $formFormEmailAddress = \Config::get('concrete.email.form_block.address');
                } else {
                    $adminUserInfo = \UserInfo::getByID(USER_SUPER_ID);
                    $formFormEmailAddress = $adminUserInfo->getUserEmail();
{'fraction_non_alphanumeric': 0.14096155593928927, 'fraction_numerical': 0.0010906116513678089, 'mean_word_length': 2.1172804532577905, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3513551', 'n_tokens_mistral': 3056, 'n_tokens_neox': 2952, 'n_words': 501}