uc_file.api.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the File Downloads module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Gives clearance to a user to download a file.
  12. *
  13. * By default the uc_file module can implement 3 restrictions on downloads: by
  14. * number of IP addresses downloaded from, by number of downloads, and by a set
  15. * expiration date. Developers wishing to add further restrictions can do so by
  16. * implementing this hook. After the 3 aforementioned restrictions are checked,
  17. * the uc_file module will check for implementations of this hook.
  18. *
  19. * @param $user
  20. * The drupal user object that has requested the download
  21. * @param $file_download
  22. * The file download object as defined as a row from the uc_file_users table
  23. * that grants the user the download
  24. *
  25. * @return
  26. * TRUE or FALSE depending on whether the user is to be permitted download of
  27. * the requested files. When a implementation returns FALSE it should set an
  28. * error message in Drupal using drupal_set_message() to inform customers of
  29. * what is going on.
  30. */
  31. function hook_uc_download_authorize($user, $file_download) {
  32. if (!$user->status) {
  33. drupal_set_message(t("This account has been banned and can't download files anymore."), 'error');
  34. return FALSE;
  35. }
  36. else {
  37. return TRUE;
  38. }
  39. }
  40. /**
  41. * Performs actions on file products.
  42. *
  43. * The uc_file module comes with a file manager (found at Administer » Store
  44. * administration » Products » View file downloads) that provides some basic
  45. * functionality: deletion of multiple files and directories, and upload of
  46. * single files (those looking to upload multiple files should just directly
  47. * upload them to their file download directory then visit the file manager
  48. * which automatically updates new files found in its directory). Developers
  49. * that need to create more advanced actions with this file manager can do so
  50. * by using this hook.
  51. *
  52. * @param $op
  53. * The operation being taken by the hook, possible ops defined below.
  54. * - info: Called before the uc_file module builds its list of possible file
  55. * actions. This op is used to define new actions that will be placed in
  56. * the file action select box.
  57. * - insert: Called after uc_file discovers a new file in the file download
  58. * directory.
  59. * - form: When any defined file action is selected and submitted to the form
  60. * this function is called to render the next form. Because this is called
  61. * whenever a module-defined file action is selected, the variable
  62. * $args['action'] can be used to define a new form or append to an existing
  63. * form.
  64. * - upload: After a file has been uploaded, via the file manager's built in
  65. * file upload function, and moved to the file download directory this op
  66. * can perform any remaining operations it needs to perform on the file
  67. * before its placed into the uc_files table.
  68. * - upload_validate: This op is called to validate the uploaded file that
  69. * was uploaded via the file manager's built in file upload function. At
  70. * this point, the file has been uploaded to PHP's temporary directory.
  71. * Files passing this upload validate function will be moved into the file
  72. * downloads directory.
  73. * - validate: This op is called to validate the file action form.
  74. * - submit: This op is called to submit the file action form.
  75. * @param $args
  76. * A keyed array of values that varies depending on the op being performed,
  77. * possible values defined below.
  78. * - info: None.
  79. * - insert:
  80. * - file_object: The file object of the newly discovered file.
  81. * - form:
  82. * - action: The file action being performed as defined by the key in the
  83. * array sent by hook_uc_file_action($op = 'info').
  84. * - file_ids: The file ids (as defined in the uc_files table) of the
  85. * selected files to perform the action on.
  86. * - upload:
  87. * - file_object: The file object of the file moved into file downloads
  88. * directory.
  89. * - form_id: The form_id variable of the form_submit function.
  90. * - form_values: The form_values variable of the form_submit function.
  91. * - upload_validate:
  92. * - file_object: The file object of the file that has been uploaded into
  93. * PHP's temporary upload directory.
  94. * - form_id: The form_id variable of the form_validate function.
  95. * - form_values: The form_values variable of the form_validate function.
  96. * - validate:
  97. * - form_id: The form_id variable of the form_validate function.
  98. * - form_values: The form_values variable of the form_validate function.
  99. * - submit:
  100. * - form_id: The form_id variable of the form_submit function.
  101. * - form_values: The form_values variable of the form_submit function.
  102. *
  103. * @return
  104. * The return value of hook depends on the op being performed, possible return
  105. * values defined below:
  106. * - info: The associative array of possible actions to perform. The keys are
  107. * unique strings that defines the actions to perform. The values are the
  108. * text to be displayed in the file action select box.
  109. * - insert: None.
  110. * - form: This op should return an array of drupal form elements as defined
  111. * by the drupal form API.
  112. * - upload: None.
  113. * - upload_validate: None.
  114. * - validate: None.
  115. * - submit: None.
  116. */
  117. function hook_uc_file_action($op, $args) {
  118. switch ($op) {
  119. case 'info':
  120. return array('uc_image_watermark_add_mark' => 'Add Watermark');
  121. case 'insert':
  122. // Automatically adds watermarks to any new files that are uploaded to
  123. // the file download directory.
  124. _add_watermark($args['file_object']->uri);
  125. break;
  126. case 'form':
  127. if ($args['action'] == 'uc_image_watermark_add_mark') {
  128. $form['watermark_text'] = array(
  129. '#type' => 'textfield',
  130. '#title' => t('Watermark text'),
  131. );
  132. $form['actions'] = array('#type' => 'actions');
  133. $form['actions']['submit_watermark'] = array(
  134. '#type' => 'submit',
  135. '#value' => t('Add watermark'),
  136. );
  137. }
  138. return $form;
  139. case 'upload':
  140. _add_watermark($args['file_object']->uri);
  141. break;
  142. case 'upload_validate':
  143. // Given a file path, function checks if file is valid JPEG.
  144. if (!_check_image($args['file_object']->uri)) {
  145. form_set_error('upload', t('Uploaded file is not a valid JPEG'));
  146. }
  147. break;
  148. case 'validate':
  149. if ($args['form_values']['action'] == 'uc_image_watermark_add_mark') {
  150. if (empty($args['form_values']['watermark_text'])) {
  151. form_set_error('watermar_text', t('Must fill in text'));
  152. }
  153. }
  154. break;
  155. case 'submit':
  156. if ($args['form_values']['action'] == 'uc_image_watermark_add_mark') {
  157. foreach ($args['form_values']['file_ids'] as $file_id) {
  158. $filename = db_query("SELECT filename FROM {uc_files} WHERE fid = :fid", array(':fid' => $file_id))->fetchField();
  159. // Function adds watermark to image.
  160. _add_watermark($filename);
  161. }
  162. }
  163. break;
  164. }
  165. }
  166. /**
  167. * Makes changes to a file before it is downloaded by the customer.
  168. *
  169. * Stores, either for customization, copy protection or other reasons, might
  170. * want to send customized downloads to customers. This hook will allow this
  171. * to happen. Before a file is opened to be transferred to a customer, this
  172. * hook will be called to make any altercations to the file that will be used
  173. * to transfer the download to the customer. This, in effect, will allow a
  174. * developer to create a new, personalized, file that will get transferred to
  175. * a customer.
  176. *
  177. * @param $file_user
  178. * The file_user object (i.e. an object containing a row from the
  179. * uc_file_users table) that corresponds with the user download being
  180. * accessed.
  181. * @param $ip
  182. * The IP address from which the customer is downloading the file.
  183. * @param $fid
  184. * The file id of the file being transferred.
  185. * @param $file
  186. * The file path of the file to be transferred.
  187. *
  188. * @return
  189. * The path of the new file to transfer to customer.
  190. */
  191. function hook_uc_file_transfer_alter($file_user, $ip, $fid, $file) {
  192. // For large files this might be too memory intensive.
  193. $file_data = file_get_contents($file) . " [insert personalized data]";
  194. $new_file = tempnam(file_directory_temp(), 'tmp');
  195. file_put_contents($new_file, $file_data);
  196. return $new_file;
  197. }
  198. /**
  199. * @} End of "addtogroup hooks".
  200. */