PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/security.php

https://bitbucket.org/ishishmarev/violin
PHP | 135 lines | 87 code | 34 blank | 14 comment | 25 complexity | 649d6587b66d6bf3abd6d9d89ab3736c MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /*
  3. * dl-file.php
  4. *
  5. * Protect uploaded files with login.
  6. *
  7. * @link http://wordpress.stackexchange.com/questions/37144/protect-wordpress-uploads-if-user-is-not-logged-in
  8. *
  9. * @author hakre <http://hakre.wordpress.com/>
  10. * @license GPL-3.0+
  11. * @registry SPDX
  12. */
  13. define('FFMPEG_PATH', '/home/user/bin');
  14. define('FREE_VIDEOS_DIR_NAME', 'free_videos');
  15. define('FREE_VIDEO_LENGTH', '120'); // продолжительность видео в секундах
  16. require_once('wp-load.php');
  17. $file_path = $_GET['file'];
  18. $post_select_sql = "
  19. SELECT pm.post_id
  20. FROM {$wpdb->prefix}postmeta pm
  21. WHERE pm.meta_value = '{$file_path}'
  22. ";
  23. $post_id = $wpdb->get_var($post_select_sql);
  24. $media_post = get_post($post_id);
  25. $post_parent = get_post($media_post->post_parent);
  26. while ($post_parent->post_type !== 0 && $post_parent->post_type === 'attachment') {
  27. $media_post = $post_parent;
  28. $post_parent = get_post($post_parent->post_parent);
  29. }
  30. $is_not_free = (bool) get_post_meta( $media_post->ID, 'it_not_free', true );
  31. list($basedir, $baseurl) = array_values( array_intersect_key( wp_upload_dir(), array('basedir' => 1, 'baseurl' => 1) ) );
  32. if ( // Если видео платное и пользователь не является редактором или админом
  33. $is_not_free
  34. && ! (current_user_can('editor') || current_user_can('administrator'))
  35. ) {
  36. $free_videos_path = rtrim( $basedir,'/' ) . '/' . FREE_VIDEOS_DIR_NAME;
  37. $file_name = end( explode('/', $file_path) );
  38. $not_free_file_name = rtrim( $basedir,'/' ) . '/' . $file_path;
  39. $free_file_name = 'FREE__' . $file_name;
  40. $free_file_path = $free_videos_path . '/' . $free_file_name;
  41. $free_file_url = rtrim( $baseurl,'/' ) . '/' . FREE_VIDEOS_DIR_NAME . '/' . $free_file_name;
  42. if( !is_dir( $free_videos_path ) ){
  43. mkdir( $free_videos_path );
  44. }
  45. if ( !file_exists($free_file_path) ) {
  46. $command = escapeshellcmd(FFMPEG_PATH . '/' . 'ffmpeg' . ' -i "' . $not_free_file_name . '" -ss 00:00:00 -codec copy -t ' . FREE_VIDEO_LENGTH . ' "' . $free_file_path . '"');
  47. $command_result = shell_exec($command);
  48. }
  49. $current_user_id = get_current_user_id();
  50. if ($current_user_id) { // Если пользователь не аноним
  51. $current_user_expire_service = get_user_meta($current_user_id, 'register_service_expire', true);
  52. $current_time = current_time( 'mysql' );
  53. if (strtotime($current_user_expire_service) < strtotime($current_time)) { // Если у пользователя истек срок действия услуги
  54. header('Location: ' . $free_file_url, true, 301);
  55. die();
  56. } else {
  57. $file = rtrim( $basedir,'/' ) . '/' . $file_path;
  58. }
  59. } else {
  60. header('Location: ' . $free_file_url, true, 301);
  61. die();
  62. }
  63. } else {
  64. $file = rtrim( $basedir,'/' ) . '/' . $file_path;
  65. }
  66. if (!$basedir || !is_file($file)) {
  67. status_header(404);
  68. die('404 &#8212; File not found.');
  69. }
  70. $mime = wp_check_filetype($file);
  71. if( false === $mime[ 'type' ] && function_exists( 'mime_content_type' ) )
  72. $mime[ 'type' ] = mime_content_type( $file );
  73. if( $mime[ 'type' ] )
  74. $mimetype = $mime[ 'type' ];
  75. else
  76. $mimetype = 'image/' . substr( $file, strrpos( $file, '.' ) + 1 );
  77. header( 'Content-Type: ' . $mimetype ); // always send this
  78. if ( false === strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS' ) )
  79. header( 'Content-Length: ' . filesize( $file ) );
  80. $last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) );
  81. $etag = '"' . md5( $last_modified ) . '"';
  82. header( "Last-Modified: $last_modified GMT" );
  83. header( 'ETag: ' . $etag );
  84. header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' );
  85. // Support for Conditional GET
  86. $client_etag = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) : false;
  87. if( ! isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) )
  88. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = false;
  89. $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
  90. // If string is empty, return 0. If not, attempt to parse into a timestamp
  91. $client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;
  92. // Make a timestamp for our most recent modification...
  93. $modified_timestamp = strtotime($last_modified);
  94. if ( ( $client_last_modified && $client_etag )
  95. ? ( ( $client_modified_timestamp >= $modified_timestamp) && ( $client_etag == $etag ) )
  96. : ( ( $client_modified_timestamp >= $modified_timestamp) || ( $client_etag == $etag ) )
  97. ) {
  98. status_header( 304 );
  99. exit;
  100. }
  101. header("X-Accel-Redirect: /play-videos/" . $file_path);