-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3podcasts.php
232 lines (187 loc) · 5.44 KB
/
s3podcasts.php
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
<?php
/*
Plugin Name: S3 Podcasts
Description: A simple plugin to upload podcasts to your Amazon S3 instance and use in your Wordpress blog
Author: Bruno Leite
Author URI: http:/bruno-leite.net
Version: 0.1
*/
session_start();
add_action('admin_menu', 's3podcasts_setup_menu');
add_action('plugins_loaded', 'get_user_info');
function get_user_info(){
$current_user = wp_get_current_user();
if (!($current_user instanceof WP_User)) {
return;
}
$_SESSION['user_login'] = $current_user->user_login;
return true;
}
function s3podcasts_setup_menu(){
add_menu_page('S3 Podcasts', 'S3 Podcasts', 'manage_options', 's3podcasts-list', 'listPodcasts', 'http://www.myiconfinder.com/uploads/iconsets/20-20-5c3ca6ab08d98e197e0778a5c2e17599-microphone.png');
add_submenu_page('s3podcasts-list', 'S3 Podcasts', 'S3 Podcasts', 'manage_options', 's3podcasts-list', 'listPodcasts' );
add_submenu_page('s3podcasts-list', 'Adicionar podcast', 'Adicionar podcast', 'manage_options', 's3podcasts-add', 'addPodcasts' );
}
function listPodcasts(){
include_once 'list.php';
}
function addPodcasts(){
include_once 'add.php';
}
require_once('aws/aws-autoloader.php');
use Aws\S3\S3Client;
define(ACCESS_KEY, 'PUT YOUR ACCESS KEY HERE');
define(SECRET_KEY, 'PUT YOUR SECRET KEY HERE');
/**
* Function to handle post from add.php form
*
* @return bool
*/
function addPodcast() {
// First check if the file appears on the _FILES array
if (empty($_FILES['podcast']['name']) || empty($_POST['title'])) {
$_SESSION['s3podcastsmsg'] = "Erro ao fazer o upload do podcast: favor preencher os campos obrigatórios do formulário!";
return false;
}
// file
$fileName = $_FILES['podcast']['name'];
$fileSource = $_FILES['podcast']['tmp_name'];
// podcast data array
$podcastData = array(
'title' => strlen($_POST['title']) < 3 ? 'Meu podcast sem título' : $_POST['title'],
'user' => $_SESSION['user_login'],
'filename' => $_FILES['podcast']['name'],
'date' => date('Y-m-d H:i:s')
);
if (!empty($_POST['description'])) {
$podcastData['description'] = $_POST['description'];
}
// Instantiate an Amazon S3 client.
$s3 = S3Client::factory([
'version' => 'latest',
'region' => 'sa-east-1',
'credentials' => [
'key' => ACCESS_KEY,
'secret' => SECRET_KEY
]
]);
// 5 minutes
set_time_limit(300);
try {
// put object to s3 bucket
$response = $s3->putObject(array(
'Bucket' => 'podcasts-aic',
'Key' => $fileName,
'SourceFile' => $fileSource
));
// store object url
$podcastData['url'] = $response['ObjectURL'];
} catch (Aws\Exception\S3Exception $e) {
$_SESSION['s3podcastsmsg'] = "Erro ao fazer o upload do podcast: " . $e->getMessage();
return false;
}
try {
global $wpdb;
// insert podcast data to database
$wpdb->insert('wp_s3podcasts', $podcastData);
} catch (Exception $e) {
$_SESSION['s3podcastsmsg'] = "Erro ao fazer o upload do podcast: " . $e->getMessage();
return false;
}
$_SESSION['s3podcastsmsg'] = "Podcast postado com sucesso, endereco <a href='{$response['ObjectURL']}'>{$response['ObjectURL']}</a>";
return true;
}
/**
* Function to remove podcast
*
* @return bool
*/
function deletePodcast($id) {
session_start();
global $wpdb;
// check if the record exist
$podcast = $wpdb->get_row("SELECT id, filename FROM wp_s3podcasts WHERE id = $id");
try {
// delete object if exist
if (!empty($podcast)) {
$wpdb->delete('wp_s3podcasts', array('id' => $id));
}
} catch (Exception $e) {
$_SESSION['s3podcastsmsg'] = "Erro ao excluir o podcast: " . $e->getMessage();
return false;
}
// Instantiate an Amazon S3 client.
$s3 = S3Client::factory([
'version' => 'latest',
'region' => 'sa-east-1',
'credentials' => [
'key' => ACCESS_KEY,
'secret' => SECRET_KEY
]
]);
// curl_setopt($ch, CURLOPT_TIMEOUT, 30);
set_time_limit(300);
try {
// put object to s3 bucket
$response = $s3->deleteObject(array(
'Bucket' => 'podcasts-aic',
'Key' => $podcast->filename
));
} catch (Aws\Exception\S3Exception $e) {
$_SESSION['s3podcastsmsg'] = "Erro ao excluir o podcast: " . $e->getMessage();
return false;
}
$_SESSION['s3podcastsmsg'] = "O podcast foi exluído com sucesso!";
return true;
}
/**
* Function to init the plugin
*
* @return bool
*/
function initPlugin() {
global $wpdb;
// check if wp_s3podcasts table exist
$result = $wpdb->get_results("SHOW TABLES LIKE 'wp_s3podcasts'");
// if wp_s3podcasts table does not exist, them create it
if (empty($result)) {
$result = $wpdb->get_results("
CREATE TABLE `wp_s3podcasts` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(100) NOT NULL,
`description` TEXT NULL,
`url` VARCHAR(255) NOT NULL,
`filename` VARCHAR(200) NOT NULL,
`user` VARCHAR(60) NOT NULL,
`date` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM;
");
}
return true;
}
// init the plugin
initPlugin();
// handle form post from add.php file
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
addPodcast();
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['action'])) {
// single delete
if (!empty($_GET['id'])) {
deletePodcast($_GET['id']);
}
// mass delete
if (!empty($_GET['media'])) {
// get ids
$ids = $_GET['media'];
// delete selected podcasts
foreach ($ids as $id) {
deletePodcast($id);
}
}
header('Location: ' . $_SERVER['HTTP_REFERER']);
}