You are here

phantomjs_capture.module in PhantomJS Capture 7

Same filename and directory in other branches
  1. 8 phantomjs_capture.module

Defines the administration interface and utility functions to use PhantomJS and test screen shot capture functions.

File

phantomjs_capture.module
View source
<?php

/**
 * @file
 * Defines the administration interface and utility functions to use PhantomJS
 * and test screen shot capture functions.
 */

/**
 * Implements hook_menu().
 */
function phantomjs_capture_menu() {
  $items = array();
  $items['admin/config/user-interface/phantomjs_capture'] = array(
    'title' => 'PhantomJS Screen capture',
    'description' => 'Screen capture with PhantomJS',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'phantomjs_capture_admin_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  return $items;
}

/**
 * The administration form.
 *
 * It allows site admins to enter information about the systems installation of
 * PhantomJS.
 *
 * @return array
 *   Drupal form array with the administration form.
 */
function phantomjs_capture_admin_form() {
  $form = array();
  $form['phantomjs_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('PhantomJS settings'),
    '#collapsible' => FALSE,
  );
  $form['phantomjs_settings']['phantomjs_capture_binary'] = array(
    '#type' => 'textfield',
    '#required' => TRUE,
    '#title' => t('Path to phantomJS'),
    '#description' => t('This module requries that you install PhantomJS on your server and enter the path to the executable. The program is not include in the module due to linces and operation system constrains. See !url for information about download.', array(
      '!url' => l('PhantomJs.org', 'http://phantomjs.org/'),
    )),
    '#default_value' => variable_get('phantomjs_capture_binary', _phantomjs_capture_get_binary()),
  );
  $form['phantomjs_settings']['phantomjs_capture_dest'] = array(
    '#type' => 'textfield',
    '#required' => TRUE,
    '#title' => t('Default destination'),
    '#description' => t('The default destination for screenshots captures with PhantomJS'),
    '#default_value' => variable_get('phantomjs_capture_dest', 'phantomjs'),
  );
  $form['phantomjs_settings']['phantomjs_capture_script'] = array(
    '#type' => 'textfield',
    '#required' => TRUE,
    '#title' => t('PhantomJS capture script'),
    '#description' => t('The script used by PhantomJS to capture the screen. It captures full HD images (1920 x 1080).'),
    '#default_value' => variable_get('phantomjs_capture_script', drupal_get_path('module', 'phantomjs_capture') . '/js/phantomjs_capture.js'),
  );
  $form['phantomjs_capture_test'] = array(
    '#type' => 'fieldset',
    '#title' => t('Phantom JS test'),
    '#description' => t('You can use the form in this section to test your installation of PhantomJS.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );
  $form['phantomjs_capture_test']['url'] = array(
    '#type' => 'textfield',
    '#title' => t('URL'),
    '#description' => t('Absolute URL to the homepage that should be capture (it has to be a complet URL with http://).'),
    '#default_value' => 'http://www.google.com',
  );
  $form['phantomjs_capture_test']['format'] = array(
    '#type' => 'select',
    '#title' => 'File format',
    '#options' => array(
      '.png' => 'png',
      '.jpg' => 'jpg',
      '.pdf' => 'pdf',
    ),
  );
  $form['phantomjs_capture_test']['result'] = array(
    '#prefix' => '<div id="phantomjs-capture-test-result">',
    '#suffix' => '</div>',
    '#markup' => '',
  );
  $form['phantomjs_capture_test']['button'] = array(
    '#type' => 'button',
    '#value' => t('Capture'),
    "#ajax" => array(
      "callback" => "phantomjs_capture_test_submit",
      "wrapper" => "phantomjs-capture-test-result",
      "method" => 'replace',
      "effect" => "fade",
    ),
  );
  return system_settings_form($form);
}

/**
 * Ajax callback for the test PhantomJS form on the administration page.
 *
 * @param $form
 * @param $form_state
 * @return array
 */
function phantomjs_capture_test_submit($form, $form_state) {

  // Build urls and destination.
  $url = $form_state['values']['phantomjs_capture_test']['url'];
  $file = 'test' . $form_state['values']['phantomjs_capture_test']['format'];
  $dest = file_default_scheme() . '://' . variable_get('phantomjs_capture_dest', 'phantomjs');

  // Get the screen shot and create success/error message.
  $output = '<div class="messages status"><ul><li>' . t('File has been generated. You can get it !url', array(
    '!url' => l(t('here'), file_create_url($dest . '/' . $file)),
  )) . '.</ul></li></div>';
  if (!phantomjs_capture_screen($url, $dest, $file)) {
    $output = '<div class="messages error"><ul><li>' . t('The address entered could not be retrieved.') . '</ul></li></div>';
  }

  // Return.
  return array(
    'phantomjs_capture_test' => array(
      'result' => array(
        '#prefix' => '<div id="phantomjs-capture-test-result">',
        '#suffix' => '</div>',
        '#markup' => $output,
      ),
    ),
  );
}

/**
 * Validation of the administration form.
 *
 * It tests that the locations given exists and that the PhantomJS binary is
 * executable (by getting its version number).
 */
function phantomjs_capture_admin_form_validate(&$form, &$form_state) {

  // Check that PhantomJS exists.
  if (!file_exists($form_state['values']['phantomjs_capture_binary'])) {
    form_set_error('phantomjs_capture_binary', t('PhantomJS was not found at the location given.'));
  }
  else {

    // Only show version message on "Save configuration" submit.
    if ($form_state['clicked_button']['#value'] == t('Save configuration')) {
      drupal_set_message(t('PhantomJS version @version found.', array(
        '@version' => _phantomjs_capture_get_version($form_state['values']['phantomjs_capture_binary']),
      )));
    }
  }

  // Check that destination can be created.
  $dest = file_default_scheme() . '://' . $form_state['values']['phantomjs_capture_dest'];
  if (!file_prepare_directory($dest, FILE_CREATE_DIRECTORY)) {
    form_set_error('phantomjs_capture_dest', t('The path was not writeable or could not be created.'));
  }

  // Check that capture script exists.
  if (!file_exists($form_state['values']['phantomjs_capture_script'])) {
    form_set_error('phantomjs_capture_script', t('PhantomJS script was not found at the location given.'));
  }

  // Remove test form.
  unset($form_state['values']['phantomjs_capture_test']);
}

/**
 * Returns the version number of the currently install PhantomJS.
 *
 * @param string $binary
 *   Optional absolute path with the PhantomJS binary. If not given the default
 *   location is used.
 *
 * @return string|boolean
 *   If PhantomJS is found and executable the version number is returned else
 *   FALSE is returned.
 */
function _phantomjs_capture_get_version($binary = NULL) {

  // If the binary is not given try the default path.
  if (is_null($binary)) {
    $binary = _phantomjs_capture_get_binary();
    if (!$binary) {
      drupal_set_message(t('PhantomJS binary was not found. Plase intall PhantomJS on the system.'));
      return FALSE;
    }
  }

  // Execute PhantomJS to get its version, if PhantomJS was found.
  $output = array();
  exec($binary . ' -v', $output);
  return $output[0];
}

/**
 * Returns the absolute path with the binary to the installed PhantomJS.
 *
 * @return string|boolean
 *   The executable PhantomJS binary or FALSE if not found.
 */
function _phantomjs_capture_get_binary() {
  $binary = variable_get('phantomjs_capture_binary', '/usr/local/bin/phantomjs');
  if (!file_exists($binary)) {
    return FALSE;
  }
  return $binary;
}

/**
 * Captures a screen shot using PhantomJS by calling the program.
 *
 * @param string $url
 *   The URL to render the screen shot from.
 * @param string $dest
 *   The destination for the rendered file (e.g. public://phantomjs).
 * @param string $filename
 *   The filename to store the file as in the destination.
 * @param string $element
 *   The id of the DOM element to render in the document.
 *
 * @return bool
 *   Returns TRUE if the screen shot was taken else FALSE on error.
 */
function phantomjs_capture_screen($url, $dest, $filename, $element = NULL) {

  // Get PhantomJS binary.
  $binary = _phantomjs_capture_get_binary();
  if (!$binary) {
    drupal_set_message(t('PhantomJS binary was not found. Plase intall PhantomJS on the system.'));
    return FALSE;
  }

  // Check that destination is writable.
  if (!file_prepare_directory($dest, FILE_CREATE_DIRECTORY)) {
    drupal_set_message(t('The PhantomJS destination path (@dest) was not writeable or could not be created.', array(
      '@dest' => $dest,
    )));
    return FALSE;
  }

  // Get absolute path to PhantomJS script and the destination file.
  $script = realpath(variable_get('phantomjs_capture_script', drupal_get_path('module', 'phantomjs_capture') . '/js/phantomjs_capture.js'));
  $dest = drupal_realpath($dest . '/' . $filename);

  // Run PhantomJS to create the screen shot.
  $output = array();
  if ($element) {
    exec($binary . ' ' . $script . ' "' . $url . '" ' . $dest . ' ' . escapeshellarg($element), $output);
  }
  else {
    exec($binary . ' ' . $script . ' "' . $url . '" ' . $dest, $output);
  }

  // Check that PhantomJS was able to load the page.
  if ($output[0] == '500') {
    return FALSE;
  }
  return TRUE;
}

Functions

Namesort descending Description
phantomjs_capture_admin_form The administration form.
phantomjs_capture_admin_form_validate Validation of the administration form.
phantomjs_capture_menu Implements hook_menu().
phantomjs_capture_screen Captures a screen shot using PhantomJS by calling the program.
phantomjs_capture_test_submit Ajax callback for the test PhantomJS form on the administration page.
_phantomjs_capture_get_binary Returns the absolute path with the binary to the installed PhantomJS.
_phantomjs_capture_get_version Returns the version number of the currently install PhantomJS.