You are here

lockr.module in Lockr 7

Same filename and directory in other branches
  1. 7.3 lockr.module
  2. 7.2 lockr.module

Hook implementations and callbacks for lockr.

File

lockr.module
View source
<?php

/**
 * @file
 * Hook implementations and callbacks for lockr.
 */
use Lockr\Exception\ClientException;
use Lockr\Exception\ServerException;

/**
 * Include our autoloader.
 */
require_once __DIR__ . '/autoload.php';

/**
 * Implements hook_ctools_plugin_directory().
 */
function lockr_ctools_plugin_directory($module, $plugin) {
  if ($module === 'key') {
    return "plugins/{$plugin}";
  }
}

/**
 * Implements hook_init().
 */
function lockr_init() {
  if (!variable_get('lockr_partner')) {
    $partner = lockr_get_partner();
    if ($partner) {
      variable_set('lockr_partner', $partner['name']);
      variable_set('lockr_cert', $partner['cert']);
    }
  }
}

/**
 * Implements hook_menu().
 */
function lockr_menu() {
  $items = array();
  $items['admin/config/system/lockr'] = array(
    'title' => t('Lockr'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'lockr_admin_form',
    ),
    'access arguments' => array(
      'administer keys',
    ),
    'file' => 'lockr.admin.inc',
  );
  $items['admin/config/system/lockr/login'] = array(
    'title' => t('Login to Lockr'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'lockr_login_form',
    ),
    'access arguments' => array(
      'administer keys',
    ),
    'file' => 'lockr.login.inc',
  );
  return $items;
}

/**
 * Returns the detected partner, if available.
 */
function lockr_get_partner() {
  if (defined('PANTHEON_BINDING')) {
    return array(
      'name' => 'pantheon',
      'title' => t('Pantheon'),
      'description' => t('Our system has detected that your website is hosted on one of our supported providers. ' . 'Please fill out the fields below to continue installation of Lockr and securing your API and encryption keys.'),
      'cert' => '/srv/bindings/' . PANTHEON_BINDING . '/certs/binding.pem',
    );
  }
  return NULL;
}

/**
 * Returns the Lockr site client.
 */
function lockr_site_client() {
  $client =& drupal_static(__FUNCTION__, NULL);
  if ($client === NULL) {
    $base_client = lockr_client();
    if ($base_client === FALSE) {
      return FALSE;
    }
    $client = new \Lockr\SiteClient($base_client);
  }
  return $client;
}

/**
 * Returns the Lockr key client.
 */
function lockr_key_client() {
  $client =& drupal_static(__FUNCTION__, NULL);
  if ($client === NULL) {
    $base_client = lockr_client();
    if ($base_client === FALSE) {
      return FALSE;
    }
    $client = new \Lockr\KeyClient($base_client);
  }
  return $client;
}

/**
 * Returns the Lockr client for this site.
 */
function lockr_client() {
  $client =& drupal_static(__FUNCTION__, NULL);
  if ($client === NULL) {
    $partner = variable_get('lockr_partner');
    $cert = variable_get('lockr_cert');
    if (!$partner || !$cert) {
      return FALSE;
    }
    $client = \Lockr\Lockr::create(new \Lockr\Partner($cert, $partner));
  }
  return $client;
}

/**
 * Returns if this site is currently registered with Lockr.
 *
 * @return bool
 * TRUE if this site is registered, FALSE if not.
 */
function lockr_check_registration() {
  $client = lockr_site_client();
  try {
    if ($client) {
      return $client
        ->exists();
    }
    else {
      return FALSE;
    }
  } catch (ServerException $e) {
    return FALSE;
  } catch (ClientException $e) {
    return FALSE;
  }
}

/**
 * Gets a key from Lockr.
 *
 * @param string $key_name
 * The key name.
 *
 * @return string | FALSE
 * Returns the key value, or FALSE on failure.
 */
function _lockr_get_key($key_name, $encoded) {
  $client = lockr_key_client();
  try {
    if ($client) {
      return $client
        ->encrypted($encoded)
        ->get($key_name);
    }
    else {
      return FALSE;
    }
  } catch (\Exception $e) {
    return FALSE;
  }
}

/**
 * Sets a key value in lockr.
 *
 * @param string $key_name
 * The key name.
 * @param string $key_value
 * The key value.
 * @param string $key_label
 * The key label.
 * @param string|bool $old_name
 * The old key name if it changed.
 *
 * @return bool
 * TRUE if they key set successfully, FALSE if not.
 */
function _lockr_set_key($key_name, $key_value, $key_label, $encoded = null) {
  $client = lockr_key_client();
  if ($client === FALSE) {
    return FALSE;
  }
  $client = $client
    ->encrypted();
  try {
    return $client
      ->set($key_name, $key_value, $key_label, $encoded);
  } catch (ClientException $e) {
    $body = $e
      ->getMessage();
    $data = json_decode($body, TRUE);
    if (isset($data['title']) && ($data['title'] = 'Not paid')) {
      drupal_set_message(t('NOTE: Key was not set. ' . 'Please go to <a href="@link">Lockr</a> and add a payment method.', array(
        '@link' => 'https://lockr.io/user/add-card',
      )), 'error');
    }
  } catch (\Exception $e) {
  }
  return FALSE;
}

/**
 * Deletes a key from Lockr.
 *
 * @param string $key_name
 * The key name
 */
function _lockr_delete_key($key_name) {
  $client = lockr_key_client();
  if ($client) {
    $client
      ->delete($key_name);
  }
}

Functions

Namesort descending Description
lockr_check_registration Returns if this site is currently registered with Lockr.
lockr_client Returns the Lockr client for this site.
lockr_ctools_plugin_directory Implements hook_ctools_plugin_directory().
lockr_get_partner Returns the detected partner, if available.
lockr_init Implements hook_init().
lockr_key_client Returns the Lockr key client.
lockr_menu Implements hook_menu().
lockr_site_client Returns the Lockr site client.
_lockr_delete_key Deletes a key from Lockr.
_lockr_get_key Gets a key from Lockr.
_lockr_set_key Sets a key value in lockr.