Skip to content

Commit f6a93bb

Browse files
committed
feat(form): setting to enable captcha
useful to avoid captcha in trusted intranet Signed-off-by: Thierry Bugier <tbugier@teclib.com>
1 parent dada741 commit f6a93bb

File tree

6 files changed

+96
-76
lines changed

6 files changed

+96
-76
lines changed

front/form_profile.form.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,22 @@
3131

3232
include ('../../../inc/includes.php');
3333

34-
Session::checkRight("entity", UPDATE);
34+
Session::checkRight('entity', UPDATE);
3535

3636
// Check if plugin is activated...
3737
$plugin = new Plugin();
38-
if (!$plugin->isActivated("formcreator")) {
38+
if (!$plugin->isActivated('formcreator')) {
3939
Html::displayNotFoundError();
4040
}
4141

4242
$formFk = PluginFormcreatorForm::getForeignKeyField();
43-
if (isset($_POST["profiles_id"]) && isset($_POST[$formFk])) {
43+
if (isset($_POST['profiles_id']) && isset($_POST[$formFk])) {
4444
if (isset($_POST['access_rights'])) {
4545
$form = new PluginFormcreatorForm();
4646
$form->update([
4747
'id' => (int) $_POST[$formFk],
48-
'access_rights' => (int) $_POST['access_rights']
48+
'access_rights' => (int) $_POST['access_rights'],
49+
'is_captcha_enabled' => $_POST['is_captcha_enabled'],
4950
]);
5051
}
5152

@@ -54,7 +55,7 @@
5455
$formFk => (int) $_POST[$formFk],
5556
]);
5657

57-
foreach ($_POST["profiles_id"] as $profile_id) {
58+
foreach ($_POST['profiles_id'] as $profile_id) {
5859
if ($profile_id != 0) {
5960
$form_profile = new PluginFormcreatorForm_Profile();
6061
$form_profile->add([
@@ -63,7 +64,5 @@
6364
]);
6465
}
6566
}
66-
Html::back();
67-
} else {
68-
Html::back();
6967
}
68+
Html::back();

front/formdisplay.php

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -41,64 +41,63 @@
4141

4242
PluginFormcreatorForm::header();
4343

44-
if (isset($_REQUEST['id'])
45-
&& is_numeric($_REQUEST['id'])) {
44+
if (!isset($_REQUEST['id'])
45+
|| !is_numeric($_REQUEST['id'])) {
46+
$message = __("The form has been successfully saved!");
47+
Html::displayTitle($CFG_GLPI['root_doc']."/pics/ok.png", $message, $message);
48+
}
4649

47-
$criteria = [
48-
'id' => (int) $_REQUEST['id'],
49-
'is_active' => '1',
50-
'is_deleted'=> '0',
51-
];
52-
$form = new PluginFormcreatorForm();
53-
if (!$form->getFromDBByCrit($criteria)) {
54-
Html::displayNotFoundError();
55-
}
50+
$criteria = [
51+
'id' => (int) $_REQUEST['id'],
52+
'is_active' => '1',
53+
'is_deleted'=> '0',
54+
];
55+
$form = new PluginFormcreatorForm();
56+
if (!$form->getFromDBByCrit($criteria)) {
57+
Html::displayNotFoundError();
58+
}
5659

57-
if ($form->fields['access_rights'] != PluginFormcreatorForm::ACCESS_PUBLIC) {
58-
Session::checkLoginUser();
59-
if (!$form->checkEntity(true)) {
60-
Html::displayRightError();
61-
exit();
62-
}
60+
if ($form->fields['access_rights'] != PluginFormcreatorForm::ACCESS_PUBLIC) {
61+
Session::checkLoginUser();
62+
if (!$form->checkEntity(true)) {
63+
Html::displayRightError();
64+
exit();
6365
}
66+
}
6467

65-
if ($form->fields['access_rights'] == PluginFormcreatorForm::ACCESS_RESTRICTED) {
66-
$iterator = $DB->request(PluginFormcreatorForm_Profile::getTable(), [
67-
'WHERE' => [
68-
'profiles_id' => $_SESSION['glpiactiveprofile']['id'],
69-
'plugin_formcreator_forms_id' => $form->getID()
70-
],
71-
'LIMIT' => 1
72-
]);
73-
if (count($iterator) == 0) {
74-
Html::displayRightError();
75-
exit();
76-
}
68+
if ($form->fields['access_rights'] == PluginFormcreatorForm::ACCESS_RESTRICTED) {
69+
$iterator = $DB->request(PluginFormcreatorForm_Profile::getTable(), [
70+
'WHERE' => [
71+
'profiles_id' => $_SESSION['glpiactiveprofile']['id'],
72+
'plugin_formcreator_forms_id' => $form->getID()
73+
],
74+
'LIMIT' => 1
75+
]);
76+
if (count($iterator) == 0) {
77+
Html::displayRightError();
78+
exit();
7779
}
78-
if (($form->fields['access_rights'] == PluginFormcreatorForm::ACCESS_PUBLIC) && (!isset($_SESSION['glpiID']))) {
79-
// If user is not authenticated, create temporary user
80-
if (!isset($_SESSION['glpiname'])) {
81-
$_SESSION['formcreator_forms_id'] = $form->getID();
82-
$_SESSION['glpiname'] = 'formcreator_temp_user';
83-
$_SESSION['valid_id'] = session_id();
84-
$_SESSION['glpiactiveentities'] = [$form->fields['entities_id']];
85-
$subentities = getSonsOf('glpi_entities', $form->fields['entities_id']);
86-
$_SESSION['glpiactiveentities_string'] = (!empty($subentities))
87-
? "'" . implode("', '", $subentities) . "'"
88-
: "'" . $form->fields['entities_id'] . "'";
89-
}
80+
}
81+
if (($form->fields['access_rights'] == PluginFormcreatorForm::ACCESS_PUBLIC) && (!isset($_SESSION['glpiID']))) {
82+
// If user is not authenticated, create temporary user
83+
if (!isset($_SESSION['glpiname'])) {
84+
$_SESSION['formcreator_forms_id'] = $form->getID();
85+
$_SESSION['glpiname'] = 'formcreator_temp_user';
86+
$_SESSION['valid_id'] = session_id();
87+
$_SESSION['glpiactiveentities'] = [$form->fields['entities_id']];
88+
$subentities = getSonsOf('glpi_entities', $form->fields['entities_id']);
89+
$_SESSION['glpiactiveentities_string'] = (!empty($subentities))
90+
? "'" . implode("', '", $subentities) . "'"
91+
: "'" . $form->fields['entities_id'] . "'";
9092
}
93+
}
9194

92-
$form->displayUserForm();
95+
$form->displayUserForm();
9396

94-
// If user was not authenticated, remove temporary user
95-
if ($_SESSION['glpiname'] == 'formcreator_temp_user') {
96-
session_write_close();
97-
unset($_SESSION['glpiname']);
98-
}
99-
} else if (isset($_GET['answer_saved'])) {
100-
$message = __("The form has been successfully saved!");
101-
Html::displayTitle($CFG_GLPI['root_doc']."/pics/ok.png", $message, $message);
97+
// If user was not authenticated, remove temporary user
98+
if ($_SESSION['glpiname'] == 'formcreator_temp_user') {
99+
session_write_close();
100+
unset($_SESSION['glpiname']);
102101
}
103102

104103
PluginFormcreatorForm::footer();

inc/form.class.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,14 @@ public static function getSpecificValueToSelect($field, $name = '', $values = ''
286286
break;
287287

288288
case 'access_rights' :
289-
return Dropdown::showFromArray($name, [
290-
self::ACCESS_PUBLIC => __('Public access', 'formcreator'),
291-
self::ACCESS_PRIVATE => __('Private access', 'formcreator'),
292-
self::ACCESS_RESTRICTED => __('Restricted access', 'formcreator'),
293-
], [
294-
'value' => $values[$field],
295-
'display_emptychoice' => false,
296-
'display' => false
297-
]);
289+
return Dropdown::showFromArray(
290+
$name,
291+
self::getEnumAccessType(), [
292+
'value' => $values[$field],
293+
'display_emptychoice' => false,
294+
'display' => false
295+
]
296+
);
298297
break;
299298

300299
case 'language' :
@@ -608,8 +607,10 @@ public function showForm($ID, $options = []) {
608607
echo '<tr>';
609608
echo '<td>'.__('Default form in service catalog', 'formcreator').'</td>';
610609
echo '<td>';
611-
Dropdown::showYesNo("is_default", $this->fields["is_default"]);
610+
Dropdown::showYesNo('is_default', $this->fields['is_default']);
612611
echo '</td>';
612+
echo '<td></td>';
613+
echo '<td></td>';
613614
echo '</tr>';
614615

615616
if (!$this->canPurgeItem()) {
@@ -1252,7 +1253,8 @@ public function displayUserForm() : void {
12521253
}
12531254

12541255
// Captcha for anonymous forms
1255-
if ($this->fields['access_rights'] == PluginFormcreatorForm::ACCESS_PUBLIC) {
1256+
if ($this->fields['access_rights'] == PluginFormcreatorForm::ACCESS_PUBLIC
1257+
&& $this->fields['is_captcha_enabled'] != '0') {
12561258
$captchaTime = time();
12571259
$captchaId = md5($captchaTime . $this->getID());
12581260
$captcha = PluginFormcreatorCommon::getCaptcha($captchaId);
@@ -2173,11 +2175,11 @@ public static function footer() {
21732175
* Is the form accessible anonymously (without being logged in) ?
21742176
* @return boolean true if the form is accessible anonymously
21752177
*/
2176-
public function isPublicAccess() {
2178+
public function isPublicAccess() : bool {
21772179
if ($this->isNewItem()) {
21782180
return false;
21792181
}
2180-
return ($this->fields['access_rights'] == \PluginFormcreatorForm::ACCESS_PUBLIC);
2182+
return ($this->fields['access_rights'] == self::ACCESS_PUBLIC);
21812183
}
21822184

21832185
/**

inc/form_profile.class.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public function prepareInputForAdd($input) {
6464
|| empty($input['uuid'])) {
6565
$input['uuid'] = plugin_formcreator_getUuid();
6666
}
67-
6867
return $input;
6968
}
7069

@@ -82,16 +81,19 @@ public static function showForForm(CommonDBTM $item, $withtemplate = '') {
8281
echo "<form name='form_profiles_form' id='form_profiles_form'
8382
method='post' action=' ";
8483
echo Toolbox::getItemTypeFormURL(__CLASS__)."'>";
85-
echo "<table class ='tab_cadre_fixe'>";
84+
echo '<table class="tab_cadre_fixe">';
8685

8786
echo '<tr><th colspan="2">'._n('Access type', 'Access types', 1, 'formcreator').'</th>';
8887
echo '</tr>';
88+
89+
// Access type
90+
echo '<tr>';
8991
echo '<td>';
9092
Dropdown::showFromArray(
9193
'access_rights',
9294
PluginFormcreatorForm::getEnumAccessType(),
9395
[
94-
'value' => (isset($item->fields["access_rights"])) ? $item->fields["access_rights"] : 1,
96+
'value' => (isset($item->fields['access_rights'])) ? $item->fields['access_rights'] : '1',
9597
]
9698
);
9799
echo '</td>';
@@ -108,10 +110,20 @@ public static function showForForm(CommonDBTM $item, $withtemplate = '') {
108110
echo '<i class="fas fa-envelope"><i/>';
109111
echo '</a>';
110112
} else {
111-
echo __('Please active the form to view the link', 'formcreator');
113+
echo __('Please activate the form to view the link', 'formcreator');
112114
}
113115
echo '</td>';
114-
echo "</tr>";
116+
echo '</tr>';
117+
118+
// Captcha
119+
if ($item->fields["access_rights"] == PluginFormcreatorForm::ACCESS_PUBLIC) {
120+
echo '<tr>';
121+
echo '<td>' . __('Enable captcha', 'formcreator') . '</td>';
122+
echo '<td>';
123+
Dropdown::showYesNo('is_captcha_enabled', $item->fields['is_captcha_enabled']);
124+
echo '</td>';
125+
echo '</tr>';
126+
}
115127

116128
if ($item->fields["access_rights"] == PluginFormcreatorForm::ACCESS_RESTRICTED) {
117129
echo '<tr><th colspan="2">'.self::getTypeName(2).'</th></tr>';

install/mysql/plugin_formcreator_empty.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_forms` (
5353
`validation_required` tinyint(1) NOT NULL DEFAULT '0',
5454
`usage_count` int(11) NOT NULL DEFAULT '0',
5555
`is_default` tinyint(1) NOT NULL DEFAULT '0',
56+
`is_captcha_enabled` tinyint(1) NOT NULL DEFAULT '0',
5657
`show_rule` INT(11) NOT NULL DEFAULT '1' COMMENT 'Conditions setting to show the submit button',
5758
`uuid` varchar(255) DEFAULT NULL,
5859
PRIMARY KEY (`id`),

install/upgrade_to_2.11.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* ---------------------------------------------------------------------
3030
*/
3131
class PluginFormcreatorUpgradeTo2_11 {
32+
/** @var Migration */
3233
protected $migration;
3334

3435
/**
@@ -156,6 +157,7 @@ public function upgrade(Migration $migration) {
156157
// Merge targettickets_actors and targetchanges_actors
157158
$this->migrateTargetTicket_Actor();
158159
$this->migrateTargetChange_Actor();
160+
$this->addCaptchaOption();
159161
}
160162

161163
/**
@@ -288,4 +290,9 @@ public function migrateTargetChange_Actor() {
288290
$table = 'glpi_plugin_formcreator_targetchanges_actors';
289291
$this->migration->backupTables([$table]);
290292
}
293+
294+
public function addCaptchaOption() {
295+
$table = 'glpi_plugin_formcreator_forms';
296+
$this->migration->addField($table, 'is_captcha_enabled', 'bool', ['after' => 'is_default']);
297+
}
291298
}

0 commit comments

Comments
 (0)