/wp-content/plugins/buddypress-group-documents/include/admin-uploads.php

https://github.com/voidit/nycga2 · PHP · 121 lines · 77 code · 15 blank · 29 comment · 17 complexity · a2919cc0a0276230789cebd4287b27a1 MD5 · raw file

  1. <?php
  2. /* Admin Uploads
  3. * This allows the site administrator to upload multiple files quickly,
  4. * or to upload large files via FTP and bypass normal HTTP restrictions
  5. */
  6. /*
  7. * bp_group_bulk_uploads()
  8. *
  9. * This checks for any files in the bulk "uploads" plugin folder
  10. * if there are any files, it displays them below the normal plugin settings area
  11. * the site admin can then select a group, name, and description, and move the file.
  12. * the actual file is moved out of the uploads folder, and a database record is created.
  13. */
  14. function bp_group_documents_bulk_uploads(){
  15. /* this is normally taken care of with AJAX, but it is here as well in case
  16. something goes wrong (no javascript) and a normal form submit occurs */
  17. bp_group_documents_check_uploads_submit();
  18. //array to hold file names
  19. $files = array();
  20. $dh = opendir(BP_GROUP_DOCUMENTS_ADMIN_UPLOAD_PATH);
  21. if( $dh ) {
  22. //read files
  23. while( false !== ($file = readdir( $dh ))) {
  24. if( $file != '.' && $file != '..' ) {
  25. $files[] = $file;
  26. }
  27. }
  28. if( !empty( $files ) ) { ?>
  29. <hr />
  30. <div id="bp-group-documents-bulk-message"></div>
  31. <h2><?php _e('Bulk File Uploads','bp-group-documents'); ?></h2>
  32. <div id="bp-group-documents-bulk-upload">
  33. <div class="doc-list">
  34. <?php foreach( $files as $file ) { ?>
  35. <div class="doc-single">
  36. <form method="post" class="bp-group-documents-admin-upload" action="">
  37. <input type="hidden" name="file" value="<?php echo $file; ?>" />
  38. <div class="file"><strong><?php echo $file; ?></strong></div>
  39. <div class="group"><select name="group">
  40. <option value="0"><?php _e('Select Group...','bp-group-documents'); ?></option>
  41. <?php
  42. $groups_list = BP_Groups_Group::get_alphabetically();
  43. $groups_list = $groups_list['groups'];
  44. foreach( $groups_list as $group ) {
  45. echo "<option value='{$group->id}'>" . stripslashes($group->name) . "</option>\n";
  46. }
  47. ?>
  48. </select></div>
  49. <div class="name"><input type="text" name="name" /></div>
  50. <div class="description"><textarea name="description"></textarea></div>
  51. <div class="submit"><input type="submit" value="<?php _e('Move File','bp-group-documents'); ?>" /></div>
  52. </form>
  53. <div class="clear"></div>
  54. </div>
  55. <?php }
  56. echo '</div></div>';
  57. }
  58. closedir($dh);
  59. }
  60. }
  61. add_action('bp_group_documents_admin_end','bp_group_documents_bulk_uploads');
  62. /*
  63. * bp_group_documents_check_uploads_submit()
  64. *
  65. * this function does the actual heavy-lifting of the moving the files
  66. * it first check if any files have been submitted, then validates the data,
  67. * moves the file, and displays applicable messages
  68. *
  69. * be sure all message end with a period. I'm finding extra junk returned
  70. * with ajax responses, so the period designates the end of a message.
  71. */
  72. function bp_group_documents_check_uploads_submit($msg_fmt = true) {
  73. //if user is submitting form
  74. if( isset( $_POST['file'] ) && isset( $_POST['group'] ) ) {
  75. if( '0' == $_POST['group'] ) {
  76. _e('You must choose a group for the file.','bp-group-documents');
  77. return false;
  78. }
  79. //get rid of extra slashes
  80. if ( get_magic_quotes_gpc() ) {
  81. $_POST = array_map( 'stripslashes_deep', $_POST );
  82. }
  83. //create and populate a shiney new object
  84. $document = new BP_Group_Documents();
  85. $document->user_id = get_current_user_id();
  86. $document->group_id = $_POST['group'];
  87. $document->file = apply_filters('bp_group_documents_filename_in',$_POST['file']);
  88. if( $_POST['name'] ) {
  89. $document->name = $_POST['name'];
  90. } else {
  91. $document->name = $_POST['file'];
  92. }
  93. $document->description = apply_filters('bp_group_documents_description_in', $_POST['description']);
  94. $current_path = WP_PLUGIN_DIR . '/buddypress-group-documents/uploads/' . $_POST['file'];
  95. if( rename( $current_path, $document->get_path(0,1))) {
  96. if( $document->save(false)) {//passing false tells it not to look for uplaods
  97. _e('Document moved successfully.','bp-group-documents');
  98. do_action('bp_group_documents_admin_upload_success', $document);
  99. } else {
  100. _e('There was a problem saving the file info.','bp-group-documents');
  101. }
  102. } else {
  103. _e('There was a problem moving the file.','bp-group-documents');
  104. }
  105. }
  106. }