View source
<?php
function encrypt_submissions_menu() {
$items = array();
$items["encrypt-submissions/generate-keypair"] = array(
"page callback" => "encrypt_submissions_generate_keypair",
"access arguments" => array(
"access encrypt submissions",
),
"type" => MENU_CALLBACK,
);
$items["admin/settings/encrypt-submissions"] = array(
"title" => "Encrypt submissions",
"page callback" => "drupal_get_form",
"page arguments" => array(
"encrypt_submissions_admin_form",
),
"access arguments" => array(
"administer encrypt submissions",
),
"type" => MENU_NORMAL_ITEM,
);
return $items;
}
function encrypt_submissions_perm() {
$perms = array(
"administer encrypt submissions",
"access encrypt submissions",
);
return $perms;
}
function encrypt_submissions_check_library_files_exist($bool_notify_correct_installation = TRUE) {
$err = "";
$original_location = variable_get("encrypt_submissions_jcryption_location", "");
$jcryption_location = drupal_get_path("module", "encrypt_submissions") . "/jcryption/";
$js_file = "{$jcryption_location}/jquery.jcryption.js";
$php_file = "{$jcryption_location}/jcryption.php";
if (!file_exists($js_file) || !file_exists($php_file)) {
if (function_exists("libraries_get_path")) {
$jcryption_location = libraries_get_path("jcryption");
$js_file = "{$jcryption_location}/jquery.jcryption.js";
$php_file = "{$jcryption_location}/jcryption.php";
}
}
variable_set("encrypt_submissions_jcryption_location", $jcryption_location);
if ($original_location != $jcryption_location) {
unset($_SESSION["es_notified_correct"]);
}
if (!file_exists($js_file)) {
$err .= "<p>" . t("The file jquery.jcryption.js could not be found. It should be named correctly\n and located at: ") . $js_file . "</p>";
}
if (!file_exists($php_file)) {
$err .= "<p>" . t("The file jcryption.php could not be found. It should be named correctly\n and located at: ") . $php_file . "</p>";
}
if ($err) {
drupal_set_message($err, "error");
unset($_SESSION["es_notified_correct"]);
}
else {
if ($bool_notify_correct_installation && !isset($_SESSION["es_notified_correct"])) {
drupal_set_message(t("The jCryption library files appear to be installed correctly at {$jcryption_location}."));
$_SESSION["es_notified_correct"] = TRUE;
}
}
}
function encrypt_submissions_admin_form() {
$form = array();
encrypt_submissions_check_library_files_exist();
$form["encrypt_submissions_visibility"] = array(
"#title" => t("Enable Encrypt Submissions for specific forms"),
"#type" => "radios",
"#options" => array(
"only_listed" => t("Only the listed forms"),
"all_except" => t("All except those listed"),
),
"#default_value" => variable_get("encrypt_submissions_visibility", "only_listed"),
"#description" => t("Be aware that this module will slow down the submission of forms. It is unadvised\n to enable the module on forms which require quick submission times."),
);
$form["encrypt_submissions_forms"] = array(
"#title" => t("List form_id's, one per line"),
"#type" => "textarea",
"#default_value" => variable_get("encrypt_submissions_forms", ""),
"#description" => t("Some common form id's:\n <br> Login: <b>user_login</b> and <b>user_login_block</b>\n <br> Registration: <b>user_register_form</b>\n <br><br>\n <b>Don't forget-- if you want to encrypt login or registration submissions</b>,\n you must give anonymous users the Access Encrypted Submissions privilege!\n "),
);
$form["encrypt_submissions_encrypt_msg"] = array(
"#title" => t("Encrypt message"),
"#type" => "textfield",
"#default_value" => variable_get("encrypt_submissions_encrypt_msg", "Encrypting..."),
"#description" => t("This is the message the user sees while the form is performing\n the encryption operation. Enter ") . "<b>none</b>" . t(" if you do not wish any\n message to appear."),
);
$form["encrypt_submissions_disable_jquery144"] = array(
"#title" => t("Advanced - Do not load included jQuery 1.4.4"),
"#type" => "checkbox",
"#default_value" => variable_get("encrypt_submissions_disable_jquery144", 0),
"#description" => t("If checked, this module will NOT attempt to load the included\n jQuery 1.4.4 file. Only check this if you are positive you\n do not need it."),
);
$form["mark1"] = array(
"#type" => "markup",
"#markup" => "<p><i>" . t("Please note that the jCryption libraries were NOT programmed by \n this module's maintainer. They were mostly programmed by \n Daniel Griesser, and comprise several other libraries by different\n authors. Please visit http://www.jcryption.org/about for a \n full list of credits.") . "</i></p>",
);
return system_settings_form($form);
}
function encrypt_submissions_generate_keypair() {
$jcryption_location = variable_get("encrypt_submissions_jcryption_location", "");
$php_file = "{$jcryption_location}/jcryption.php";
if (!file_exists($php_file)) {
encrypt_submissions_check_library_files_exist();
$jcryption_location = variable_get("encrypt_submissions_jcryption_location", "");
$php_file = "{$jcryption_location}/jcryption.php";
}
require_once $php_file;
$keyLength = 256;
$jCryption = new jCryption();
$keys = $jCryption
->generateKeypair($keyLength);
$_SESSION["es_e"] = array(
"int" => $keys["e"],
"hex" => $jCryption
->dec2string($keys["e"], 16),
);
$_SESSION["es_d"] = array(
"int" => $keys["d"],
"hex" => $jCryption
->dec2string($keys["d"], 16),
);
$_SESSION["es_n"] = array(
"int" => $keys["n"],
"hex" => $jCryption
->dec2string($keys["n"], 16),
);
$_SESSION["es_token"] = md5(drupal_get_private_key() . $_SESSION["es_e"]);
echo '{"e":"' . $_SESSION["es_e"]["hex"] . '","n":"' . $_SESSION["es_n"]["hex"] . '","maxdigits":"' . intval($keyLength * 2 / 16 + 3) . '"}';
return;
}
function encrypt_submissions_form_alter(&$form, &$form_state, $form_id) {
$listed_form_ids = explode("\n", variable_get("encrypt_submissions_forms", ""));
foreach ($listed_form_ids as $key => $val) {
$listed_form_ids[$key] = trim($val);
}
$visibility = variable_get("encrypt_submissions_visibility", "only_listed");
if ($visibility == "all_except" && in_array($form_id, $listed_form_ids)) {
return;
}
else {
if ($visibility == "only_listed" && !in_array($form_id, $listed_form_ids)) {
return;
}
else {
if (!user_access("access encrypt submissions")) {
return;
}
$dom_form_id = $form["#id"];
$encrypt_msg = variable_get("encrypt_submissions_encrypt_msg", "Encrypting...");
$markup = "<span></span>";
if (trim(strtolower($encrypt_msg)) != "none") {
$markup = "<div id='encrypt_submissions_encrypt_msg_{$dom_form_id}'>\n <span id='encrypt_submissions_status_{$dom_form_id}'\n class='encrypt-submissions-status'>{$encrypt_msg} \n </span>\n </div>";
}
$form["encrypt_submissions_status"] = array(
"#type" => "markup",
"#dom" => $dom_form_id,
"#weight" => 999999,
"#value" => $markup,
"#after_build" => array(
"encrypt_submissions_add_form_css_js",
),
);
}
}
}
function encrypt_submissions_add_form_css_js($form_element, &$form_state) {
$form_id = $form_state["values"]["form_id"];
$dom_form_id = $form_element["#dom"];
drupal_add_css(drupal_get_path("module", "encrypt_submissions") . "/css/encrypt_submissions.css");
$jcryption_location = variable_get("encrypt_submissions_jcryption_location", "");
$js_file = "{$jcryption_location}/jquery.jcryption.js";
if (!file_exists($js_file)) {
encrypt_submissions_check_library_files_exist();
$jcryption_location = variable_get("encrypt_submissions_jcryption_location", "");
$js_file = "{$jcryption_location}/jquery.jcryption.js";
}
if (!variable_get("encrypt_submissions_disable_jquery144", 0)) {
drupal_add_js(drupal_get_path("module", "encrypt_submissions") . "/js/jq1.4.4/jquery.min.js", "encrypt_submissions");
}
drupal_add_js($js_file, "encrypt_submissions");
drupal_add_js(drupal_get_path("module", "encrypt_submissions") . "/js/encryption_submissions.js", "encrypt_submissions");
drupal_add_js(array(
'encrypt_submissions' => array(
'encryptForms' => array(
$form_id => $dom_form_id,
),
),
), 'setting');
return $form_element;
}
function encrypt_submissions_init() {
if (!user_access("access encrypt submissions")) {
return;
}
drupal_add_js(array(
'encrypt_submissions' => array(
'baseUrl' => $GLOBALS["base_url"],
),
), 'setting');
if (isset($_POST["jCryption"]) && isset($_SESSION["es_e"]) && isset($_SESSION["es_d"]) && isset($_SESSION["es_n"])) {
$jcryption_location = variable_get("encrypt_submissions_jcryption_location", "");
$php_file = "{$jcryption_location}/jcryption.php";
if (!file_exists($php_file)) {
encrypt_submissions_check_library_files_exist();
$jcryption_location = variable_get("encrypt_submissions_jcryption_location", "");
$php_file = "{$jcryption_location}/jcryption.php";
}
$test_es_token = md5(drupal_get_private_key() . $_SESSION["es_e"]);
if ($_SESSION["es_token"] != $test_es_token) {
drupal_set_message("Invalid encryption token. Rejecting submission.", "error");
return;
}
require_once $php_file;
$jCryption = new jCryption();
$var = $jCryption
->decrypt($_POST['jCryption'], $_SESSION["es_d"]["int"], $_SESSION["es_n"]["int"]);
unset($_SESSION["es_e"]);
unset($_SESSION["es_d"]);
unset($_SESSION["es_n"]);
unset($_SESSION["es_token"]);
parse_str($var, $result);
$_POST = $result;
}
}