Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // $Id: music_dropbox.module
- function music_dropbox_menu() {
- $items['uploadtest'] = array(
- 'title' => 'Upload a file to my Dropbox',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('music_dropbox_form'),
- 'access callback' => TRUE,
- );
- return $items;
- }
- function music_dropbox_form($form, &$form_state) {
- global $user;
- // enctype="multipart/form-data" required by browsers to handle files.
- $form = array(
- '#attributes' => array('enctype' => "multipart/form-data"),
- );
- // This is the first form element. It's a textfield with a label, "Name"
- $form['title'] = array(
- '#type' => 'textfield',
- '#title' => t('Title'),
- );
- $form['description'] = array(
- '#type' => 'textarea',
- '#title' => t('Description'),
- );
- $form['file'] = array(
- '#type' => 'file',
- '#title' => t('File Upload'),
- '#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
- );
- $form['media_type'] = array(
- '#type' => 'select',
- '#title' => t('Media Type'),
- '#options' => array(
- 'documents' => t('Documents'),
- 'video' => t('Video'),
- 'image' => t('Image'),
- 'track' => t('Track'),
- )
- );
- $form['price'] = array(
- '#type' => 'textfield',
- '#title' => t('Price £'),
- );
- $form['keywords'] = array(
- '#type' => 'textarea',
- '#title' => t('Keywords'),
- );
- $form['dest'] = array(
- '#type' => 'hidden',
- '#value' => $user->name,
- );
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => 'Submit',
- );
- return $form;
- }
- /**
- * Submit the dropbox_send_form. This uploads the file to a temporary
- * directory, transfers the file to Dropbox, and sends a notification email
- * to the destination user.
- *
- * @param $form
- * @param $form_state
- * @return unknown_type
- */
- function music_dropbox_send_form_submit($form, &$form_state) {
- global $user;
- $destination_user = $form_state['values']['destination_user'];
- $file = file_save_upload('upload', array());
- if (is_object($file)) {
- $error = dropbox_file_put($destination_user, $file, '/' . variable_get('site_name', 'Drupal') . '/' . strtr($form_state['values']['mail'], "@", "-"));
- if (!$error) {
- $params = array();
- $params['destination_user'] = $destination_user;
- $params['name'] = $form_state['values']['name'];
- $params['mail'] = strtr($form_state['values']['mail'], "@", "-");
- drupal_mail('dropbox', 'send_notify', $destination_user->name . ' <' . $destination_user->mail . '>', user_preferred_language($destination_user), $params, $form_state['values']['mail']);
- drupal_set_message(t('%filename has successfully been sent.', array('%filename' => $file->filename)));
- file_delete($file->filepath);
- }
- else {
- form_set_error('upload', t('Failed to upload the file. Dropbox returned the following error: !dropbox-error', array('!dropbox-error' => $error)));
- }
- }
- else {
- form_set_error('upload', t('Failed to upload the file. Please try again.'));
- }
- }
- function dropbox_file_put($account, $file, $path) {
- global $user;
- $dropbox_email = 'example';
- $dropbox_password = 'password';
- module_load_include('php', 'music_dropbox', 'DropboxUploader');
- $dropboxUploader = new DropboxUploader($dropbox_email, $dropbox_password);
- $dropboxUploader->setCaCertificateFile(drupal_get_path('module', 'music_dropbox') . '/Thawte_Premium_Server_CA.pem');
- try {
- $dropboxUploader->upload(file_directory_path('temporary') . '/' . $file->filename, $path);
- }
- catch (Exception $e) {
- return $e->getMessage();
- }
- if ($user->uid == 0) {
- flood_register_event('dropbox_send');
- }
- return FALSE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement