This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.php
200 lines (156 loc) · 7.1 KB
/
index.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
<?php
/*
* Download Manager Plugin for WolfCMS <http://www.wolfcms.org>
* Copyright (C) 2011 Shannon Brooks <http://www.brooksworks.com>
*/
// security measure
if (!defined('IN_CMS')) { exit(); }
Plugin::setInfos(array(
'id' => 'downloads',
'title' => __('Download Manager'),
'description' => __('Provides interface to manage downloadable files.'),
'version' => '0.0.3',
'license' => 'GPL',
'author' => 'Shannon Brooks',
'website' => 'http://www.brooksworks.com/',
'require_wolf_version' => '0.7.2'
));
// define paths
define ('DOWNLOADS_PLUGIN_ROOT',CORE_ROOT.'/plugins/downloads');
// watch for download requests
Observer::observe('page_requested', 'downloads_catch_click');
// Add the plugin's tab and controller
Plugin::addController('downloads', __('Download Manager'),'downloads_view,downloads_new,downloads_edit,downloads_delete,downloads_settings');
// Load the class into the system.
AutoLoader::addFile('Download', CORE_ROOT.'/plugins/downloads/Download.php');
AutoLoader::addFile('DownloadTag', CORE_ROOT.'/plugins/downloads/DownloadTag.php');
AutoLoader::addFile('DownloadTagConnection', CORE_ROOT.'/plugins/downloads/DownloadTagConnection.php');
// redirect urls already set up
function downloads_catch_click($args) {
// check for download
if (preg_match('#^/download-file/(\d+)#i',$args,$matches)) {
// update the click count of the banner
$id = (int)$matches[1];
if (!$download = Download::findById($id)) return $args;
$download->downloads++;
$download->save();
// redirect to the requested url
header ('HTTP/1.1 301 Moved Permanently', true);
header ('Location: /'.Plugin::getSetting('download_uri','downloads').'/'.$download->filename);
exit;
}
// check for download by name
if (preg_match('#^/download-file/(.*)$#i',$args,$matches)) {
// update the click count of the banner
$filename = $matches[1];
if (!$download = Download::findByFilename($filename)) return $args;
$download->downloads++;
$download->save();
// redirect to the requested url
header ('HTTP/1.1 301 Moved Permanently', true);
header ('Location: /'.Plugin::getSetting('download_uri','downloads').'/'.$download->filename);
exit;
}
// no click so keep going
return $args;
}
// return download search results in array
function downloadSearch($terms,$limit=10,$offset=0,$order='name',$expired=false,$inactive=false) {
$where = '1';
// show expired downloads?
if ($expired === false) $where .= " AND ( `downloads`.`expires` > NOW() || `downloads`.`expires` IS NULL )";
// show inactive downloads?
if ($inactive === false) $where .= " AND `downloads`.`active` = '1'";
$order = strtolower($order);
$order = in_array($order,explode(',','id,name,filename,active,downloads,expires,created,updated')) && !empty($order) ? $order : 'name' ;
$order = $order == 'downloads' ? 'downloads.'.$order.' DESC' : 'downloads.'.$order.' ASC';
if (! empty($terms)) {
$querys = preg_replace('/[^a-z0-9 %]/i',' ',$terms);
$querys = strstr($querys,' ') !== false ? explode(' ',$querys) : array($querys);
$querys = preg_replace(array('/ing$/i','/ed$/i','/s$/i'),'',$querys);
foreach ($querys as $query) {
if (strstr($query,'%') === false && !empty($query)) $query = "%{$query}%";
if (!empty($query)) $where .= " AND ( downloads.name LIKE ".Record::escape($query)." OR downloads.description LIKE ".Record::escape($query)." OR downloads.keywords LIKE ".Record::escape($query)." ) ";
}
}
if (!$results = Download::findAll(array('where'=>$where,'limit'=>$limit,'offset'=>$offset,'order'=>$order))) return false;
$count = Record::countFrom('Download',$where);
return array('downloads'=>$results,'count'=>$count);
}
// output a download link by download id
function downloadLinkById($download_id,$text=null) {
// get download
if (!$download = Download::findById($download_id)) return "<span class=\"download-broken\" title=\"broken download link\">[".(!empty($text) ? $text : 'file not found')."]</span>";
// return download link
return "<a class=\"download-link\" href=\"/download-file/{$download->id}/{$download->filename}\" target=\"_blank\" rel=\"nofollow\">".(!empty($text) ? $text : $download->name)."</a>";
}
// output a download link by download id
function downloadBoxesAll($text=null) {
$return = '';
// get download
if (!$downloads = Download::findAll()) return "<div><span class=\"download-broken\" title=\"broken download link\">[file not found]</span></div>";
// return download links
foreach($downloads as $download){
$return .= downloadBoxFormat($download);
}
return $return;
}
function downloadBoxById($download_id) {
// get download
if (!$download = Download::findById($download_id)) return "<div><span class=\"download-broken\" title=\"broken download link\">[file not found]</span></div>";
// return download box
return downloadBoxFormat($download);
}
// output a download player for mp3 files
function downloadPlayerById($download_id,$text=null) {
// get download
if (!$download = Download::findById($download_id)) return "<span class=\"download-broken\" title=\"broken download link\">[".(!empty($text) ? $text : 'file not found')."]</span>";
// return download link
return downloadBoxFormat($download,true);
}
// depreciated
function downloadLinksByTag($tag) {
return downloadBoxesByTag($tag);
}
// returns a list of downloads in an un-ordered list
function downloadListByTag($tag) {
if (!$downloads = DownloadTagConnection::findAllByTagName($tag)) return "<span class=\"download-broken\" title=\"no downloads found\">[tag \"{$tag}\" has no downloads]</span>";
$return = '<ul>';
foreach ($downloads as $download) $return .= '<li>'.downloadLinkById($download->id).'</li>';
return $return.'</ul>';
}
// returns a list of downloads in formatted boxes
function downloadBoxesByTag($tag) {
if (!$downloads = DownloadTagConnection::findAllByTagName($tag)) return "<span class=\"download-broken\" title=\"no downloads found\">[tag \"{$tag}\" has no downloads]</span>";
$return = '';
foreach ($downloads as $download) $return .= downloadBoxFormat($download);
return $return;
}
function downloadBoxFormat($download,$player='') {
$modified = ($download->created == $download->updated ? 'File was created ' : 'File was last updated ').date('F j, Y',strtotime($download->updated));
$description = !empty($download->description) ? "<div class=\"download-description\">".nl2br($download->description)."</div>" : '<!-- no description -->';
if ($player === true) {
$playerid = 'dp'.rand();
$player = <<<HTML
<div class="download-player"><div id="{$playerid}"></div></div>
<script type="text/javascript">
<!--
$(function(){
AudioPlayer.embed("{$playerid}", { soundFile: "/download-file/{$download->id}/{$download->filename}" });
});
//-->
</script>
HTML;
}
$return = <<<HTML
<div class="download">
<div class="download-name">{$download->name}</div>
{$description}
{$player}
<div class="download-link-container"><a class="download-link" href="/download-file/{$download->id}/{$download->filename}" target="_blank" rel="nofollow">Download File</a></div>
<div class="download-info">{$modified} and has been downloaded {$download->downloads} times.</div>
</div>
HTML;
return $return;
}
?>