You are here

dropbox_client.module in Dropbox Client 7.4

File

dropbox_client.module
View source
<?php

/**
 * Implements hook_menu().
 */
function dropbox_client_menu() {
  $items['dropbox_client/install'] = array(
    'title' => 'Install Dropbox for Drupal',
    'page callback' => 'dropbox_client_connect',
    'access callback' => 'user_is_logged_in',
    'type' => MENU_CALLBACK,
  );
  $items['dropbox_client/access'] = array(
    'title' => 'Access Dropbox for Drupal',
    'page callback' => 'dropbox_client_access',
    'access callback' => 'user_is_logged_in',
    'type' => MENU_CALLBACK,
  );
  $items['dropbox_client/success'] = array(
    'title' => 'Dropbox Client for Drupal',
    'page callback' => 'dropbox_client_success',
    'access callback' => 'user_is_logged_in',
  );
  $items['admin/config/content/dropbox_client/settings'] = array(
    'title' => 'DropBox Client Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dropbox_client_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  $items['dropbox/files'] = array(
    'title' => 'Dropbox Files',
    'page callback' => 'dropbox_files_list',
    'access callback' => array(
      'user_is_logged_in',
    ),
  );
  return $items;
}
function dropbox_files_list() {
  global $user;

  //print_r(variable_get( 'dropbox_client_website_oauth_token', 0 ));
  $content = '';
  $dpath = isset($_GET['dpath']) ? $_GET['dpath'] : '/';
  if ($user->uid && variable_get('dropbox_client_account_type', 0) == 2 && isset($user->data['dropbox']['token_secret'])) {
    $content = dropbox_client_metadata($dpath) . l('Link Dropbox to Website with another account', 'dropbox_client/install') . '</span>';
  }
  elseif (variable_get('dropbox_client_account_type', 0) == 1 && is_string(variable_get('dropbox_client_website_oauth_token', 0))) {
    $content = dropbox_client_metadata($dpath);
  }
  elseif (variable_get('dropbox_client_account_type', 0) == 2) {
    $content = l('Click here to add drop box folder', 'dropbox_client/install');
  }
  elseif (variable_get('dropbox_client_account_type', 0) == 1) {
    $content = l('Link Dropbox to Website with another account', 'dropbox_client/install');
  }
  return $content;
}
function dropbox_client_get_client() {
  global $user;
  module_load_include('php', 'dropbox_client', 'oauth');
  module_load_include('php', 'dropbox_client', 'dropbox');

  //Credential for App Dropbox Client For Drupal
  $params['key'] = variable_get('dropbox_client_key', FALSE);
  $params['secret'] = variable_get('dropbox_client_secret', FALSE);
  if (variable_get('dropbox_client_account_type', 0) == 1) {
    $params['access'] = array(
      'oauth_token' => urlencode(variable_get('dropbox_client_website_oauth_token', 0)),
      'oauth_token_secret' => urlencode(variable_get('dropbox_client_website_oauth_token_secret', 0)),
    );
  }
  else {
    if (variable_get('dropbox_client_account_type', 0) == 2) {
      $params['access'] = array(
        'oauth_token' => urlencode($user->data['dropbox']['oauth_token']),
        'oauth_token_secret' => urlencode($user->data['dropbox']['oauth_token_secret']),
      );
    }
  }
  if (isset($params['access'])) {
    $dropbox = new dropbox($params);
    return $dropbox;
  }
  else {
    return null;
  }
}
function dropbox_client_settings() {
  $form['dropbox_client_key'] = array(
    '#type' => 'textfield',
    '#default_value' => variable_get('dropbox_client_key', ''),
    '#title' => t('Dropbox client key'),
    '#description' => t('Enter your Dropbox app key. If you do not have an app key, you can create one at <a href="@url" target="_blank">Dropbox developers</a>', array(
      '@url' => 'https://www.dropbox.com/developers/apps/create',
    )),
  );
  $form['dropbox_client_secret'] = array(
    '#type' => 'textfield',
    '#default_value' => variable_get('dropbox_client_secret', ''),
    '#title' => t('Dropbox client key'),
    '#description' => t('Enter your Dropbox app secret. If you do not have an app secret, you can create one at <a href="@url" target="_blank">Dropbox developers</a>', array(
      '@url' => 'https://www.dropbox.com/developers/apps/create',
    )),
  );
  $form['dropbox_client_account_type'] = array(
    '#states' => array(
      'invisible' => array(
        array(
          'input[name="dropbox_client_key"]' => array(
            'value' => '',
          ),
        ),
        'or',
        array(
          'input[name="dropbox_client_secret"]' => array(
            'value' => '',
          ),
        ),
      ),
    ),
    '#type' => 'select',
    '#title' => t('Selected'),
    '#options' => array(
      0 => t('Select'),
      1 => t('Widesite Dropbox Account'),
      2 => t('Dropbox Account per User'),
    ),
    '#default_value' => variable_get('dropbox_client_account_type', 0),
    '#description' => t('Configure how Dropbox Will be integrated with Drupal. 1) Widesite dropbox account ideal to share files with all users
    2) Enable each user to use his own Dropbox Account '),
  );
  if (variable_get('dropbox_client_account_type', 0) === 1 && (is_string(variable_get('dropbox_client_website_token_secret', 0)) && is_string(variable_get('dropbox_client_website_oauth_token', 0)) && is_string(variable_get('dropbox_client_website_oauth_token_secret', 0)))) {
    $form['dropbox_install_widesite'] = array(
      '#type' => 'item',
      '#markup' => '<span>' . l('Link Dropbox to Website', 'dropbox_client/install') . '</span>',
    );
  }
  else {
    if (variable_get('dropbox_client_key', FALSE) && variable_get('dropbox_client_secret', FALSE) && variable_get('dropbox_client_account_type', 0) == 1) {
      $form['dropbox_install_widesite'] = array(
        '#type' => 'item',
        '#markup' => t('Dropbox Client for Website was configured successfully') . '<span>' . "  " . l('Link Dropbox to Website with another account', 'dropbox_client/install') . '</span>',
      );
    }
  }
  return system_settings_form($form);
}
function dropbox_client_success() {
  global $user;
  module_load_include('php', 'dropbox_client', 'oauth');
  module_load_include('php', 'dropbox_client', 'dropbox');

  //Credential for App Dropbox Client For Drupal
  $params['key'] = variable_get('dropbox_client_key', FALSE);
  $params['secret'] = variable_get('dropbox_client_secret', FALSE);
  $params['access'] = array(
    'oauth_token' => urlencode($_SESSION['oauth_token']),
    'oauth_token_secret' => urlencode($_SESSION['oauth_token_secret']),
  );
  $dropbox = new dropbox($params);
  $dbobj = $dropbox
    ->account();

  #$metadata = $dropbox->metadata('/');
  if (is_object($dbobj) and property_exists($dbobj, 'uid') and is_numeric($dbobj->uid)) {
    $_SESSION['dropbox_client_installed'] = TRUE;
    if (variable_get('dropbox_client_account_type', 0) == 1) {
      variable_set('dropbox_client_website_token_secret', $_SESSION['dropbox_client_token_secret']);
      variable_set('dropbox_client_website_oauth_token', $_SESSION['oauth_token']);
      variable_set('dropbox_client_website_oauth_token_secret', $_SESSION['oauth_token_secret']);
      drupal_goto('admin/config/content/dropbox_client/settings');
    }
    else {
      if (variable_get('dropbox_client_account_type', 0) == 2) {
        $dropbox_client = array(
          'token_secret' => $_SESSION['dropbox_client_token_secret'],
          'oauth_token' => $_SESSION['oauth_token'],
          'oauth_token_secret' => $_SESSION['oauth_token_secret'],
        );
        if (is_array($user->data)) {
          $user->data = array_merge($user->data, array(
            'dropbox' => $dropbox_client,
          ));
          user_save($user);
        }
        else {
          $user->data = array(
            'dropbox' => $dropbox_client,
          );
          user_save($user);
        }
        return "Dropbox Client For Drupal installed Successfully";
      }
    }
  }
  else {
    return "Error: Dropbox Client For Drupal can't be installed";
  }
}
function dropbox_client_access() {
  module_load_include('php', 'dropbox_client', 'oauth');
  module_load_include('php', 'dropbox_client', 'dropbox');

  //Credential for App Dropbox Client For Drupal
  $params['key'] = variable_get('dropbox_client_key', FALSE);
  $params['secret'] = variable_get('dropbox_client_secret', FALSE);
  $dropbox = new dropbox($params);
  $oauth = $dropbox
    ->get_access_token($_SESSION['dropbox_client_token_secret']);
  $_SESSION['oauth_token'] = $oauth['oauth_token'];
  $_SESSION['oauth_token_secret'] = $oauth['oauth_token_secret'];
  drupal_goto('dropbox_client/success');
}
function dropbox_client_connect() {
  module_load_include('php', 'dropbox_client', 'oauth');
  module_load_include('php', 'dropbox_client', 'dropbox');

  //Credential for App Dropbox Client For Drupal
  $params['key'] = variable_get('dropbox_client_key', FALSE);
  $params['secret'] = variable_get('dropbox_client_secret', FALSE);
  $dropbox = new dropbox($params);
  $data = $dropbox
    ->get_request_token(url("dropbox_client/access", array(
    'absolute' => TRUE,
  )));
  $_SESSION['dropbox_client_token_secret'] = $data['token_secret'];
  drupal_goto($data['redirect']);
}

/**
 * Define all blocks provided by the module.
 *
 * @return
 *   An associative array whose keys define the delta for each block and whose
 *   values contain the block descriptions. Each block description is itself an
 *   associative array, with the following key-value pairs:
 *   - 'info': (required)
 *   - 'cache': (optional)
 *   - 'properties': (optional)
 *   - 'weight': (optional)
 *   - 'status': (optional)
 *   - 'region': (optional)
 *   - 'visibility': (optional)
 *   - 'pages': (optional)
 */
function dropbox_client_block_info() {
  $blocks = array();

  /* $blocks[ 'dropbox_install' ] = array(
     'info' => t( 'Install Dropbox Client For Drupal' ),
     'cache' => DRUPAL_NO_CACHE,
     ); */
  $blocks['dropbox_files'] = array(
    'info' => t('Dropbox Files'),
    'cache' => DRUPAL_NO_CACHE,
  );
  $blocks['dropbox_upload'] = array(
    'info' => t('Dropbox Upload'),
    'cache' => DRUPAL_NO_CACHE,
  );
  $blocks['dropbox_search'] = array(
    'info' => t('Dropbox Search'),
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

/**
 * Return a rendered or renderable view of a block.
 *
 * @param $delta
 *   Which block to render. This is a unique identifier for the block
 *   within the module, defined in hook_block_info().
 *
 * @return
 *   An array containing the following elements:
 *   - subject: The default localized title of the block. If the block does not
 *     have a default title, this should be set to NULL.
 *   - content: The content of the block's body. This may be a renderable array
 *     (preferable) or a string containing rendered HTML content.
 */
function dropbox_client_block_view($delta = '') {
  global $user;
  $block = '';
  switch ($delta) {
    case 'dropbox_files':
      $block['subject'] = t('');
      $dpath = isset($_GET['dpath']) ? $_GET['dpath'] : '/';
      if ($user->uid && variable_get('dropbox_client_account_type', 0) == 2 && isset($user->data['dropbox']['token_secret'])) {
        $block['content'] = dropbox_client_metadata($dpath) . l('Link Dropbox to Website with another account', 'dropbox_client/install') . '</span>';
      }
      elseif (variable_get('dropbox_client_account_type', 0) == 1 && is_string(variable_get('dropbox_client_website_oauth_token', 0))) {
        $block['content'] = dropbox_client_metadata($dpath);
      }
      elseif (variable_get('dropbox_client_account_type', 0) == 2) {
        $block['content'] = l('Click here to add drop box folder', 'dropbox_client/install');
      }
      elseif (variable_get('dropbox_client_account_type', 0) == 1) {
        $content = l('Link Dropbox to Website with another account', 'dropbox_client/install');
      }
      break;
    case 'dropbox_upload':
      $block['subject'] = t('documents');
      if ($user->uid && variable_get('dropbox_client_account_type', 0) == 2 && isset($user->data['dropbox']['token_secret'])) {
        $block['content'] = drupal_get_form('dropbox_client_upload_form');
      }
      elseif (variable_get('dropbox_client_account_type', 0) == 1 && is_string(variable_get('dropbox_client_website_oauth_token', 0))) {
        $block['content'] = drupal_get_form('dropbox_client_upload_form');
      }
      break;
    case 'dropbox_search':
      $block['subject'] = t('search');
      if ($user->uid && variable_get('dropbox_client_account_type', 0) == 2 && isset($user->data['dropbox']['token_secret'])) {
        if (isset($_SESSION['dropbox_client_results'])) {
          $block['content'] = dropbox_client_search_results();
        }
        else {
          $block['content'] = drupal_get_form('dropbox_client_search_form');
        }
      }
      elseif (variable_get('dropbox_client_account_type', 0) == 1 && is_string(variable_get('dropbox_client_website_oauth_token', 0))) {
        if (isset($_SESSION['dropbox_client_results'])) {
          $block['content'] = dropbox_client_search_results();
        }
        else {
          $block['content'] = drupal_get_form('dropbox_client_search_form');
        }
      }
      break;
  }
  return $block;
}
function dropbox_client_metadata($path) {
  global $user, $base_path;
  $dropbox = dropbox_client_get_client();
  $dbobj = $dropbox
    ->account();
  $metadata = $dropbox
    ->metadata($path);
  $files = array();
  $destination = $_GET['q'];
  foreach ($metadata->contents as $content) {
    if ($content->is_dir) {
      array_push($files, array(
        'data' => '<a href="' . $base_path . $destination . '?dpath=' . check_plain($content->path) . '">' . check_plain(basename($content->path)) . "</a>",
        'class' => array(
          $content->icon,
        ),
      ));
    }
    else {
      $media = $dropbox
        ->media($content->path);
      $contentclass = isset($content->icon) ? array(
        $content->icon,
      ) : array(
        "page_white_default",
      );
      array_push($files, array(
        'data' => l(basename($content->path), $media->url, array(
          'attributes' => array(
            'target' => '_blank',
          ),
        )),
        'class' => array(
          $content->icon,
        ),
      ));
    }
  }
  $breadcrumb = explode("/", $path);
  $incremental_path = '';

  //Delete empty entries except home
  foreach ($breadcrumb as $i => $bread) {
    if ($i && empty($bread)) {
      unset($breadcrumb[$i]);
    }
    else {
      $breadcrumb[$i] = check_plain($breadcrumb[$i]);
    }
  }
  foreach ($breadcrumb as $i => $bread) {
    if (!$i) {
      if (count($breadcrumb) == 1) {
        $breadcrumb[$i] = t('Dropbox Home');
      }
      else {
        $path = '/';
        $breadcrumb[$i] = l(t('Dropbox Home'), $destination, array(
          'query' => array(
            'dpath' => $path,
          ),
        ));
      }
    }
    else {
      if (count($breadcrumb) > $i + 1) {
        $path .= "/" . $bread;
        $path = preg_replace("#(^|[^:])//+#", "\\1/", $path);
        $breadcrumb[$i] = l($bread, $destination, array(
          'query' => array(
            'dpath' => $path,
          ),
        ));
      }
      else {
        $path .= "/" . $bread;
        $path = preg_replace("#(^|[^:])//+#", "\\1/", $path);
        $breadcrumb[$i] = $bread;
      }
    }
  }
  $output = theme('breadcrumb', array(
    'breadcrumb' => $breadcrumb,
  ));
  return $output . theme('dropbox_file_list', array(
    'files' => $files,
  ));
}
function dropbox_client_theme($existing, $type, $theme, $path) {
  return array(
    'dropbox_file_list' => array(
      'variables' => array(
        'files' => NULL,
      ),
    ),
  );
}
function theme_dropbox_file_list($vars) {
  return theme('item_list', array(
    'items' => $vars['files'],
  ));
}
function dropbox_client_search_results() {
  $files = array();
  global $user, $base_path;
  foreach ($_SESSION['dropbox_client_results'] as $content) {
    $dropbox = dropbox_client_get_client();
    $destination = $_GET['q'];
    if ($content->is_dir) {
      array_push($files, array(
        'data' => '<a href="' . $base_path . $destination . '?dpath=' . $content->path . '">' . check_plain(basename($content->path)) . "</a>",
        'class' => array(
          $content->icon,
        ),
      ));
    }
    else {
      $media = $dropbox
        ->media($content->path);
      if (property_exists($media, 'url')) {
        $contentclass = isset($content->icon) ? array(
          $content->icon,
        ) : array(
          "page_white_default",
        );
        array_push($files, array(
          'data' => l(basename($content->path), $media->url),
          'class' => array(
            $content->icon,
          ),
        ));
      }
    }
  }
  unset($_SESSION['dropbox_client_results']);
  if (count($files)) {
    return theme('dropbox_file_list', array(
      'files' => $files,
    ));
  }
  else {
    return t('No results');
  }
}
function dropbox_client_search_form($form, &$form_state) {
  $form['search'] = array(
    '#type' => 'textfield',
    '#title' => t('Search'),
    '#size' => 20,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
  );
  return $form;
}
function dropbox_client_search_form_submit($form, &$form_state) {
  global $user;
  $dropbox = dropbox_client_get_client();
  $query = $form_state['values']['search'];
  $dpath = isset($_GET['dpath']) ? $_GET['dpath'] : '/';
  $results = $dropbox
    ->search($dpath, $query);
  $_SESSION['dropbox_client_results'] = $results;
}
function dropbox_client_upload_form($form, &$form_state) {
  $form['upload'] = array(
    '#title' => t('Upload file'),
    '#type' => 'file',
    '#size' => 20,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Upload'),
  );
  return $form;
}
function dropbox_client_upload_form_submit($form, &$form_state) {
  global $user;
  $dropbox = dropbox_client_get_client();

  //Process files
  $uploads_dir = dirname($_FILES['files']["tmp_name"]['upload']);
  $tmp_name = $_FILES['files']["tmp_name"]['upload'];
  $name = $_FILES['files']["name"]['upload'];
  $filename = "{$uploads_dir}/{$name}";
  move_uploaded_file($tmp_name, $filename);
  $dpath = isset($_GET['dpath']) ? $_GET['dpath'] : '/';
  $result = $dropbox
    ->add($dpath, $filename);
  if (property_exists($result, 'error')) {
    drupal_set_message($result->error);
  }
  else {
    drupal_goto($_GET['q'], array(
      'query' => array(
        'dpath' => $dpath,
      ),
    ));
  }
}