Skip to content

Instantly share code, notes, and snippets.

@acrolink
Created November 28, 2021 12:14
Show Gist options
  • Select an option

  • Save acrolink/aa5de9792e51f0f996d7afbc1cfdba0d to your computer and use it in GitHub Desktop.

Select an option

Save acrolink/aa5de9792e51f0f996d7afbc1cfdba0d to your computer and use it in GitHub Desktop.

Revisions

  1. acrolink renamed this gist Nov 28, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. acrolink created this gist Nov 28, 2021.
    136 changes: 136 additions & 0 deletions my_excel.module
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,136 @@
    <?php

    function my_excel_permission()
    {
    return array(
    'Use Excel API' => array(
    'title' => t('Use Excel API'),
    'description' => t(''),
    ),
    );
    }

    function my_excel_menu()
    {
    $items = array();

    $items['excel/generate-key'] = array(
    'title' => t('Generate a key for Excel'), //page title
    'description' => 'A form to mess around with.',
    'page callback' => 'my_excel_custompage',
    'page arguments' => array(),
    'access arguments' => array('Use Excel API'),
    'type' => MENU_NORMAL_ITEM,
    );

    return $items;
    }

    function my_excel_custompage()
    {
    return theme('my_excel_custom_page_template');
    }

    function mybs_theme()
    {
    return array(
    'my_excel_custom_page_template' => array(
    'template' => 'my-excel',
    'arguments' => array(),
    ),
    );
    }

    function my_excel_form($form, &$form_state)
    {

    $form = array();
    // Blank output field which we will fill using AJAX.
    $form['output'] = array(
    '#prefix' => '<div id="output">' . t('Your key here..'),
    '#suffix' => '</div><div class="info amiri">' . t('Copy & Paste inside Excel\'s SMS Sender > Settings.') . '</div>',
    '#markup' => '',
    );

    // AJAX-enabled submit button.
    $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Generate an Excel Key'),
    '#prefix' => '<div id="gen">',
    '#suffix' => '</div>',
    '#ajax' => array(
    'callback' => 'my_excel_form_ajax_callback',
    'effect' => 'fade',
    ),
    '#attached' => array(
    'js' => array(
    drupal_get_path('module', 'my_excel') . '/my_excel.js' => array(
    'type' => 'file',
    ),
    ),
    'css' => array(
    drupal_get_path('module', 'my_excel') . '/my_excel.css' => array(
    'type' => 'file',
    ),
    ),
    ),
    );

    return $form;
    }

    /**
    * AJAX callback function for mymodule_form().
    */
    function my_excel_form_ajax_callback($form, &$form_state)
    {
    global $user;
    $user_id = $user->uid;
    $mi_nokkel = my_excel_create_api_key($user_id);

    return array(
    '#type' => 'ajax',
    '#commands' => array(
    'command' => 'special_effects',
    ajax_command_replace("#output", render($form['output']['value'] = "<div id='output'>" . $mi_nokkel . "</div>")),
    ajax_command_invoke(null, "myJavascriptFunction", array($myCustomParameters)),
    ),
    );
    }

    function my_excel_create_api_key($user_id)
    {

    $passord = my_excel_generateRandomString(16);
    $user = user_load($user_id);
    $obj = entity_metadata_wrapper('user', $user);

    // A higher "cost" is more secure but consumes more processing power
    $cost = 10;

    // Create a random salt
    $salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');

    // Prefix information about the hash so PHP knows how to verify it later.
    // "$2a$" Means we're using the Blowfish algorithm. The following two digits are the cost parameter.
    $salt = sprintf("$2a$%02d$", $cost) . $salt;

    // Hash the password with the salt
    $hash = crypt($passord, $salt);

    $obj->field_api_secret = $hash;
    $obj->save();

    return $passord;
    }

    function my_excel_generateRandomString($length = 16)
    {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
    $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
    }