PageRenderTime 25ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress/wp-content/plugins/thecartpress/admin/UploadFiles.php

http://ownerpress.googlecode.com/
PHP | 119 lines | 98 code | 5 blank | 16 comment | 16 complexity | b2aa2750bcfbde4dc279430329797213 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This file is part of TheCartPress.
  4. *
  5. * TheCartPress is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * TheCartPress is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with TheCartPress. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. require_once( dirname( dirname( __FILE__ ) ).'/daos/Orders.class.php' );
  19. $post_id = isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : 0;
  20. $error_upload = '';
  21. function tcp_upload_file( $post_id, $file ) {
  22. $rev_name = strrev( $_FILES['upload_file']['name'] );
  23. $i = strpos( $rev_name, '.' );
  24. $ext = strrev( substr( $rev_name, 0, $i ) );
  25. $settings = get_option( 'tcp_settings' );
  26. $downloadable_path = isset( $settings['downloadable_path'] ) ? trim( $settings['downloadable_path'] ) : '';
  27. if ( strlen( $settings['downloadable_path'] ) == 0 ) {
  28. wp_die( __( 'The path where the downloadable files must be saved is not set.', 'tcp' ) );
  29. return false;
  30. } else {
  31. $folder_path = $settings['downloadable_path'];
  32. global $wpdb;
  33. $folder_path .= '/' . $wpdb->prefix . 'tcp';
  34. if ( ! file_exists( $folder_path ) )
  35. if ( ! mkdir( $folder_path ) ) {
  36. $error_upload = __( 'Error creating the folder.', 'tcp' );
  37. return false;
  38. }
  39. $file_path = $folder_path . '/upload_' . $post_id . '.' . $ext;
  40. tcp_set_the_file( $post_id, $file_path );
  41. if ( move_uploaded_file( $_FILES['upload_file']['tmp_name'], $file_path ) ) {
  42. do_action( 'tcp_uploaded_file', $file_path );
  43. return true;
  44. } else {
  45. $error_upload = __( 'Error uploading the file.', 'tcp' );
  46. return false;
  47. }
  48. }
  49. }
  50. if ( $post_id ) {
  51. $file_path = tcp_get_the_file( $post_id );
  52. if ( isset( $_REQUEST['tcp_upload_virtual_file'] ) ) {
  53. if ( tcp_upload_file( $post_id, $_FILES['upload_file'] ) ) {?>
  54. <div id="message" class="updated"><p><?php
  55. printf (__( 'Upload completed, uploaded %f bytes', 'tcp' ), number_format( $_FILES['upload_file']['size'], 2 ) );
  56. ?></p></div><?php
  57. $file_path = __( 'recent uploaded', 'tcp' );
  58. } else {?>
  59. <div id="message" class="updated"><p><?php
  60. printf( __( 'Error, the upload has not been completed: %s', 'tcp' ), $error_upload );
  61. ?></p></div><?php
  62. }
  63. } elseif ( isset( $_REQUEST['tcp_delete_virtual_file'] ) ) {
  64. $file_path = tcp_get_the_file( $post_id );
  65. do_action( 'tcp_delete_upload_file', $file_path );
  66. if ( unlink( $file_path ) ) {?>
  67. <div id="message" class="updated"><p><?php
  68. _e( 'The file has been deleted succesfuly', 'tcp' );
  69. ?></p></div><?php
  70. } else {?>
  71. <div id="message" class="error"><p><?php
  72. _e( 'The file can not be deleted', 'tcp' );
  73. ?></p></div><?php
  74. }
  75. tcp_set_the_file( $post_id, '' );
  76. $file_path = '';
  77. }
  78. $post = get_post( $post_id );
  79. if ( $post ) : ?>
  80. <div class="wrap">
  81. <h2><?php echo __( 'Upload file for', 'tcp' );?>&nbsp;<i><?php echo $post->post_title;?></i></h2>
  82. <ul class="subsubsub">
  83. <li><a href="post.php?action=edit&post=<?php echo $post_id;?>"><?php _e( 'return to the product', 'tcp' );?></a></li>
  84. </ul><!-- subsubsub -->
  85. <div class="clear"></div>
  86. <form method="post" enctype="multipart/form-data">
  87. <input type="hidden" name="post_id" value="<?php echo $post_id;?>" />
  88. <?php if ( strlen( $file_path ) > 0 ) : ?>
  89. <table class="form-table"><tbody>
  90. <tr valign="top">
  91. <th scope="row"><label for=""><?php _e( 'File', 'tcp');?></label></th>
  92. <td><?php echo $file_path;?></td>
  93. </tr>
  94. </tbody>
  95. </table>
  96. <p>
  97. <input type="submit" id="tcp_delete_virtual_file" name="tcp_delete_virtual_file" value="<?php _e( 'delete file', 'tcp' );?>" class="button-primary"/>
  98. </p>
  99. <?php else : ?>
  100. <table class="form-table"><tbody>
  101. <tr valign="top">
  102. <th scope="row"><label for="upload_file"><?php _e( 'File', 'tcp');?></label></th>
  103. <td><input type="file" id="upload_file" name="upload_file" class="regular-text"/></td>
  104. </tr>
  105. </tbody>
  106. </table>
  107. <p>
  108. <input type="submit" id="tcp_upload_virtual_file" name="tcp_upload_virtual_file" value="<?php _e( 'upload file', 'tcp' );?>" class="button-primary" />
  109. </p>
  110. <?php endif;?>
  111. </form>
  112. </div>
  113. <?php endif;
  114. }?>