/** * Add CAPTCHA image to form with a CAPTCHA field * * @param AbstractController $controller * @param Application $application * @return void */ public static function addCaptcha(AbstractController $controller, Application $application) { if ($controller->hasView() && null !== $controller->view()->form && $controller->view()->form !== false && $controller->view()->form instanceof \Pop\Form\Form && null !== $controller->view()->form->getElement('captcha')) { $captcha = new \Phire\Captcha\Model\Captcha($application->module('phire-captcha')['config']); $captcha->createToken(); $controller->view()->form->getElement('captcha')->setToken($captcha->token, 'Enter Code'); } }
/** * Constructor method to instantiate the form object * * @param mixed $id * @param array $captchaConfig * @throws \Pop\Form\Exception * @return self */ public function __construct($id, $captchaConfig = []) { $form = is_numeric($id) ? Table\Forms::findById($id) : Table\Forms::findBy(['name' => $id]); if (!isset($form->id)) { throw new \Pop\Form\Exception('That form does not exist.'); } if (!class_exists('Phire\\Fields\\Model\\Field')) { throw new \Pop\Form\Exception('The phire-fields module is not installed or active.'); } $fieldGroups = []; $submitAttributes = []; $formAttributes = []; $this->filter = (bool) $form->filter; if (!empty($form->submit_attributes)) { $attribs = explode('" ', $form->submit_attributes); foreach ($attribs as $attrib) { $attAry = explode('=', $attrib); $att = trim($attAry[0]); $val = str_replace('"', '', trim($attAry[1])); $submitAttributes[$att] = $val; } } if (!empty($form->attributes)) { $attribs = explode('" ', $form->attributes); foreach ($attribs as $attrib) { $attAry = explode('=', $attrib); $att = trim($attAry[0]); $val = str_replace('"', '', trim($attAry[1])); $formAttributes[$att] = $val; } } $sql = \Phire\Fields\Table\Fields::sql(); $sql->select()->where('models LIKE :models'); $sql->select()->orderBy('order', 'ASC'); $value = $sql->getDbType() == \Pop\Db\Sql::SQLITE ? '%Phire\\Forms\\Model\\Form%' : '%Phire\\\\Forms\\\\Model\\\\Form%'; $fields = \Phire\Fields\Table\Fields::execute((string) $sql, ['models' => $value]); foreach ($fields->rows() as $field) { if (null !== $field->group_id) { $fieldGroups[$field->group_id] = []; } } $fieldGroups[0] = []; $fieldGroups[-1] = []; foreach ($fields->rows() as $field) { $field->validators = unserialize($field->validators); $field->models = unserialize($field->models); if (null !== $field->group_id) { foreach ($field->models as $model) { if (null === $model['type_value'] || $form->id == $model['type_value']) { $fieldConfig = \Phire\Fields\Event\Field::createFieldConfig($field); if (strpos($fieldConfig['label'], '<span class="editor-link-span">') !== false) { $fieldConfig['label'] = str_replace('<span class="editor-link-span">', '<span style="display: none;" class="editor-link-span">', $fieldConfig['label']); } $fieldGroups[$field->group_id]['field_' . $field->id] = $fieldConfig; break; } } } else { if (null === $field->group_id) { foreach ($field->models as $model) { if (null === $model['type_value'] || $form->id == $model['type_value']) { $fieldConfig = \Phire\Fields\Event\Field::createFieldConfig($field); if (strpos($fieldConfig['label'], '<span class="editor-link-span">') !== false) { $fieldConfig['label'] = str_replace('<span class="editor-link-span">', '<span style="display: none;" class="editor-link-span">', $fieldConfig['label']); } $fieldGroups[0]['field_' . $field->id] = $fieldConfig; break; } } } } } if ($form->use_csrf) { $fieldGroups[-1]['csrf'] = ['type' => 'csrf']; } if ($form->use_captcha) { if (class_exists('Phire\\Captcha\\Model\\Captcha')) { $captcha = new \Phire\Captcha\Model\Captcha($captchaConfig); $captcha->createToken(); $fieldGroups[-1]['captcha'] = ['type' => 'captcha', 'label' => 'Enter Code', 'token' => $captcha->token]; } else { $fieldGroups[-1]['captcha'] = ['type' => 'captcha', 'label' => 'Please Solve: ']; } } $fieldGroups[-1]['id'] = ['type' => 'hidden', 'value' => $form->id]; $fieldGroups[-1]['submit'] = ['type' => 'submit', 'label' => ' ', 'value' => !empty($form->submit_value) ? $form->submit_value : 'SUBMIT', 'attributes' => $submitAttributes]; parent::__construct($fieldGroups, null, $form->method); foreach ($formAttributes as $attrib => $value) { $this->setAttribute($attrib, $value); } }