Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. <?php
  2. // $Id: music_dropbox.module
  3.  
  4. function music_dropbox_menu() {
  5. $items['uploadtest'] = array(
  6. 'title' => 'Upload a file to my Dropbox',
  7. 'page callback' => 'drupal_get_form',
  8. 'page arguments' => array('music_dropbox_form'),
  9. 'access callback' => TRUE,
  10. );
  11. return $items;
  12. }
  13.  
  14. function music_dropbox_form($form, &$form_state) {
  15. global $user;
  16. // enctype="multipart/form-data" required by browsers to handle files.
  17. $form = array(
  18. '#attributes' => array('enctype' => "multipart/form-data"),
  19. );
  20. // This is the first form element. It's a textfield with a label, "Name"
  21. $form['title'] = array(
  22. '#type' => 'textfield',
  23. '#title' => t('Title'),
  24. );
  25. $form['description'] = array(
  26. '#type' => 'textarea',
  27. '#title' => t('Description'),
  28. );
  29. $form['file'] = array(
  30. '#type' => 'file',
  31. '#title' => t('File Upload'),
  32. '#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
  33. );
  34. $form['media_type'] = array(
  35. '#type' => 'select',
  36. '#title' => t('Media Type'),
  37. '#options' => array(
  38. 'documents' => t('Documents'),
  39. 'video' => t('Video'),
  40. 'image' => t('Image'),
  41. 'track' => t('Track'),
  42. )
  43. );
  44. $form['price'] = array(
  45. '#type' => 'textfield',
  46. '#title' => t('Price £'),
  47. );
  48. $form['keywords'] = array(
  49. '#type' => 'textarea',
  50. '#title' => t('Keywords'),
  51. );
  52. $form['dest'] = array(
  53. '#type' => 'hidden',
  54. '#value' => $user->name,
  55. );
  56. $form['submit'] = array(
  57. '#type' => 'submit',
  58. '#value' => 'Submit',
  59. );
  60. return $form;
  61. }
  62.  
  63. /**
  64. * Submit the dropbox_send_form. This uploads the file to a temporary
  65. * directory, transfers the file to Dropbox, and sends a notification email
  66. * to the destination user.
  67. *
  68. * @param $form
  69. * @param $form_state
  70. * @return unknown_type
  71. */
  72. function music_dropbox_send_form_submit($form, &$form_state) {
  73. global $user;
  74. $destination_user = $form_state['values']['destination_user'];
  75. $file = file_save_upload('upload', array());
  76. if (is_object($file)) {
  77. $error = dropbox_file_put($destination_user, $file, '/' . variable_get('site_name', 'Drupal') . '/' . strtr($form_state['values']['mail'], "@", "-"));
  78. if (!$error) {
  79. $params = array();
  80. $params['destination_user'] = $destination_user;
  81. $params['name'] = $form_state['values']['name'];
  82. $params['mail'] = strtr($form_state['values']['mail'], "@", "-");
  83. drupal_mail('dropbox', 'send_notify', $destination_user->name . ' <' . $destination_user->mail . '>', user_preferred_language($destination_user), $params, $form_state['values']['mail']);
  84. drupal_set_message(t('%filename has successfully been sent.', array('%filename' => $file->filename)));
  85. file_delete($file->filepath);
  86. }
  87. else {
  88. form_set_error('upload', t('Failed to upload the file. Dropbox returned the following error: !dropbox-error', array('!dropbox-error' => $error)));
  89. }
  90. }
  91. else {
  92. form_set_error('upload', t('Failed to upload the file. Please try again.'));
  93. }
  94. }
  95.  
  96.  
  97.  
  98. function dropbox_file_put($account, $file, $path) {
  99. global $user;
  100. $dropbox_email = 'example';
  101. $dropbox_password = 'password';
  102. module_load_include('php', 'music_dropbox', 'DropboxUploader');
  103.  
  104. $dropboxUploader = new DropboxUploader($dropbox_email, $dropbox_password);
  105. $dropboxUploader->setCaCertificateFile(drupal_get_path('module', 'music_dropbox') . '/Thawte_Premium_Server_CA.pem');
  106. try {
  107. $dropboxUploader->upload(file_directory_path('temporary') . '/' . $file->filename, $path);
  108. }
  109. catch (Exception $e) {
  110. return $e->getMessage();
  111. }
  112. if ($user->uid == 0) {
  113. flood_register_event('dropbox_send');
  114. }
  115. return FALSE;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement