forked from imtube/webtorrent-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebtorrent.php
More file actions
executable file
·261 lines (204 loc) · 8.89 KB
/
Copy pathwebtorrent.php
File metadata and controls
executable file
·261 lines (204 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/*
Plugin Name: WebTorrent
Description: WebTorrent is a streaming torrent client for the web browser. Visit http://webtorrent.io/
Version: 0.1.2
Author: Rune Piselli
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Domain Path: /languages
Text Domain: my-toolset
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
function webtorrent_scripts() {
wp_enqueue_script('webtorrent_js', plugin_dir_url(__FILE__) . 'js/webtorrent.min.js');
wp_enqueue_script('moment_js', plugin_dir_url(__FILE__) . 'js/moment.min.js');
wp_enqueue_style( 'webtorrent_css', plugin_dir_url(__FILE__) . 'css/style.css');
}
add_action( 'wp_enqueue_scripts', 'webtorrent_scripts' );
add_action( 'admin_menu', 'webtorrent_menu' );
function webtorrent_menu() {
add_options_page( 'WebTorrent Options', 'WebTorrent', 'manage_options', 'webtorrent', 'webtorrent_options' );
}
function webtorrent_options() {
if (!current_user_can('manage_options'))
{
wp_die( __('You do not have sufficient permissions to access this page.') );
}
$hidden_field_name = 'mt_submit_hidden';
if( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) {
check_admin_referer( 'webtorrent-plugin-settings');
$torrent_link_enabled = filter_var($_POST['torrent_link_enabled'], FILTER_VALIDATE_BOOLEAN);
$show_seed_leech_info = filter_var($_POST['show_seed_leech_info'], FILTER_VALIDATE_BOOLEAN);
$show_download_info = filter_var($_POST['show_download_info'], FILTER_VALIDATE_BOOLEAN);
$torrents_directory = sanitize_text_field($_POST['torrents_directory']);
update_option( 'torrent_link_enabled', $torrent_link_enabled);
update_option( 'show_seed_leech_info', $show_seed_leech_info);
update_option( 'show_download_info', $show_download_info);
update_option( 'torrents_directory', $torrents_directory);
?>
<div class="updated"><p><strong><?php _e('settings saved.', 'menu-test' ); ?></strong></p></div>
<?php
}
$torrent_link_enabled = get_option('torrent_link_enabled', true);
$show_seed_leech_info = get_option('show_seed_leech_info', true);
$show_download_info = get_option('show_download_info', true);
$torrents_directory = get_option('torrents_directory');
if(empty($torrents_directory)){
$torrents_directory = "torrents";
}
// Now display the settings editing screen
echo '<div class="wrap">';
// header
echo "<h2>" . __( 'Webtorrent Settings', 'menu-test' ) . "</h2>";
// settings form
?>
<form name="form1" method="post" action="">
<input type="hidden" name="<?php echo $hidden_field_name?>" value="Y">
<p><?php _e("Show torrent link:", 'menu-test' ); ?>
<input type="radio" id="torrent_link_enabled" name="torrent_link_enabled" <?php if($torrent_link_enabled == true) echo 'checked="checked"'; ?> value="true" />yes
<input type="radio" id="torrent_link_enabled" name="torrent_link_enabled" <?php if($torrent_link_enabled == false) echo 'checked="checked"'; ?> value="false" />no
</p>
<p><?php _e("Show seed leech info:", 'menu-test' ); ?>
<input type="radio" id="show_seed_leech_info" name="show_seed_leech_info" <?php if($show_seed_leech_info == true) echo 'checked="checked"'; ?> value="true" />yes
<input type="radio" id="show_seed_leech_info" name="show_seed_leech_info" <?php if($show_seed_leech_info == false) echo 'checked="checked"'; ?> value="false" />no
</p>
<p><?php _e("Show download info:", 'menu-test' ); ?>
<input type="radio" id="show_download_info" name="show_download_info" <?php if($show_download_info == true) echo 'checked="checked"'; ?> value="true" />yes
<input type="radio" id="show_download_info" name="show_download_info" <?php if($show_download_info == false) echo 'checked="checked"'; ?> value="false" />no
</p>
<p><?php _e("Torrents directory:", 'menu-test' ); ?>
<input type="text" id="torrents_directory" name="torrents_directory" value="<?php echo esc_attr($torrents_directory) ?>"/>Torrents directory relative to the uploads directory
</p>
<p class="submit">
<input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
</p>
<?php wp_nonce_field( 'webtorrent-plugin-settings' );?>
</form>
</div>
<?php
}
function webtorrent_shortcode($atts){
$torrent_link_enabled = get_option('torrent_link_enabled', true);
$show_seed_leech_info = get_option('show_seed_leech_info', true);
$show_download_info = get_option('show_download_info', true);
$torrents_directory = get_option('torrents_directory');
if(empty($torrents_directory)){
$torrents_directory = "torrents";
}
$webtorrent_atts = shortcode_atts( array(
'torrent_link_enabled' => $torrent_link_enabled,
'show_seed_leech_info' => $show_seed_leech_info,
'show_download_info' => $show_download_info,
'torrents_directory' => $torrents_directory,
'url' => 'none',
'file' => 'none',
), $atts );
$torrentId = $webtorrent_atts['url'];
if($webtorrent_atts['file'] <> 'none' && validate_file( $webtorrent_atts['file'])== false){
$uploads = wp_upload_dir();
$upload_url = $uploads['baseurl'];
$torrentId = $upload_url.'/'.$torrents_directory.'/'.$webtorrent_atts['file'];
}
$path=explode("?",$torrentId);
$filename=basename($path[0]);
ob_start();
?>
<div id="webtorrent">
<div id="output">
<div id="progressBar"></div>
<!-- The video player will be added here -->
</div>
<!-- Statistics -->
<div id="status">
<?php if($webtorrent_atts['show_seed_leech_info']=="true") { ?>
<div>
<span class="show-leech">Downloading </span>
<span class="show-seed">Seeding </span>
<?php if($webtorrent_atts['torrent_link_enabled']=="true") { ?>
<code class="webtorrent">
<!-- Informative link to the torrent file -->
<a id="torrentLink" href="<?php echo esc_url( $torrentId )?>"><?php echo esc_attr($filename) ?></a>
</code>
<?php }?>
<span class="show-leech"> from </span>
<span class="show-seed"> to </span>
<code id="numPeers" class="webtorrent">0 peers</code>.
</div>
<?php }?>
<?php if($webtorrent_atts['show_download_info']=="true") { ?>
<div>
<code id="downloaded" class="webtorrent"></code>
of <code id="total" class="webtorrent"></code>
— <span id="remaining"></span><br/>
↘<code id="downloadSpeed" class="webtorrent">0 b/s</code>
/ ↗<code id="uploadSpeed" class="webtorrent">0 b/s</code>
</div>
<?php } ?>
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function( $ ) {
var torrentId = '<?php echo esc_url( $torrentId )?>'
var client = new WebTorrent()
// HTML elements
var $body = document.body
var $progressBar = document.querySelector('#progressBar')
var $numPeers = document.querySelector('#numPeers')
var $downloaded = document.querySelector('#downloaded')
var $total = document.querySelector('#total')
var $remaining = document.querySelector('#remaining')
var $uploadSpeed = document.querySelector('#uploadSpeed')
var $downloadSpeed = document.querySelector('#downloadSpeed')
// Download the torrent
client.add(torrentId, function (torrent) {
// Stream the file in the browser
torrent.files[0].appendTo('#output')
// Trigger statistics refresh
torrent.on('done', onDone)
setInterval(onProgress, 500)
onProgress()
// Statistics
function onProgress () {
// Peers
$numPeers.innerHTML = torrent.numPeers + (torrent.numPeers === 1 ? ' peer' : ' peers')
// Progress
var percent = Math.round(torrent.progress * 100 * 100) / 100
$progressBar.style.width = percent + '%'
$downloaded.innerHTML = prettyBytes(torrent.downloaded)
$total.innerHTML = prettyBytes(torrent.length)
// Remaining time
var remaining
if (torrent.done) {
remaining = 'Done.'
} else {
remaining = moment.duration(torrent.timeRemaining / 1000, 'seconds').humanize()
remaining = remaining[0].toUpperCase() + remaining.substring(1) + ' remaining.'
}
$remaining.innerHTML = remaining
// Speed rates
$downloadSpeed.innerHTML = prettyBytes(torrent.downloadSpeed) + '/s'
$uploadSpeed.innerHTML = prettyBytes(torrent.uploadSpeed) + '/s'
}
function onDone () {
$body.className += ' is-seed'
onProgress()
}
})
});
// Human readable bytes util
function prettyBytes(num) {
var exponent, unit, neg = num < 0, units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
if (neg) num = -num
if (num < 1) return (neg ? '-' : '') + num + ' B'
exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), units.length - 1)
num = Number((num / Math.pow(1000, exponent)).toFixed(2))
unit = units[exponent]
return (neg ? '-' : '') + num + ' ' + unit
}
</script>
<?php
return ob_get_clean();
}
add_shortcode('webtorrent', 'webtorrent_shortcode');
?>