You are here

function drush_facebook_php_sdk in Facebook Autopost 7

Command to download the Facebook PHP SDK library.

1 string reference to 'drush_facebook_php_sdk'
fb_autopost_drush_command in ./fb_autopost.drush.inc
Implements hook_drush_command().

File

./fb_autopost.drush.inc, line 48
drush integration for Facebook Autopost.

Code

function drush_facebook_php_sdk() {
  $args = func_get_args();
  if (!empty($args[0])) {
    $path = $args[0];
  }
  else {
    $path = 'sites/all/libraries';
  }

  // Create the path if it does not exist.
  if (!is_dir($path)) {
    drush_op('mkdir', $path);
    drush_log(dt('Directory @path was created', array(
      '@path' => $path,
    )), 'notice');
  }

  // Set the directory to the download location.
  $olddir = getcwd();
  chdir($path);
  $filename = basename(FBAUTOPOST_DOWNLOAD_URI);

  // Remove any existing Facebook PHP SDK library directory.
  if (is_dir(FBAUTOPOST_DIRNAME)) {
    drush_delete_dir(FBAUTOPOST_DIRNAME, TRUE);
    drush_log(dt('A existing Facebook PHP SDK library was deleted from @path', array(
      '@path' => $path,
    )), 'notice');
  }

  // Remove any existing Facebook PHP SDK library zip archive.
  if (is_file($filename)) {
    drush_delete_dir($filename, TRUE);
  }

  // Download the zip archive.
  drush_download_file(FBAUTOPOST_DOWNLOAD_URI);
  if (is_file($filename)) {

    // Decompress the zip archive.
    $extracted_files = drush_tarball_extract($filename, FALSE, TRUE);

    // Get the last extracted file and get the initial folder.
    $matches = array();
    if (preg_match("@^[^/]*/@", end($extracted_files), $matches)) {
      drush_move_dir(rtrim($matches[0], '/'), FBAUTOPOST_DIRNAME);
    }
  }
  if (is_dir(FBAUTOPOST_DIRNAME)) {
    drush_log(dt('Facebook PHP SDK library has been installed in @path', array(
      '@path' => $path,
    )), 'success');
  }
  else {
    drush_log(dt('Drush was unable to install the Facebook PHP SDK library to @path', array(
      '@path' => $path,
    )), 'error');
  }

  // Set working directory back to the previous working directory.
  chdir($olddir);
}