PluginProbe ʕ •ᴥ•ʔ
Download Manager / trunk
Download Manager vtrunk
3.3.62 3.3.61 3.3.60 3.3.59 3.3.58 3.3.57 3.3.56 trunk 2.1.3 2.3.0 2.5.96 2.5.97 2.6.2 2.6.96 2.8.3 2.9.99 3.0.4 3.1.05 3.1.07 3.1.08 3.1.11 3.1.12 3.1.14 3.1.17 3.1.18 3.1.22 3.1.23 3.1.24 3.1.25 3.1.26 3.1.27 3.1.28 3.2.04 3.2.13 3.2.14 3.2.16 3.2.18 3.2.19 3.2.21 3.2.22 3.2.23 3.2.24 3.2.25 3.2.27 3.2.28 3.2.29 3.2.30 3.2.31 3.2.32 3.2.33 3.2.34 3.2.35 3.2.37 3.2.38 3.2.39 3.2.40 3.2.41 3.2.42 3.2.43 3.2.44 3.2.45 3.2.46 3.2.47 3.2.48 3.2.49 3.2.50 3.2.51 3.2.52 3.2.53 3.2.54 3.2.55 3.2.56 3.2.57 3.2.58 3.2.59 3.2.60 3.2.61 3.2.63 3.2.64 3.2.65 3.2.66 3.2.67 3.2.68 3.2.69 3.2.70 3.2.71 3.2.72 3.2.73 3.2.74 3.2.75 3.2.76 3.2.77 3.2.78 3.2.79 3.2.80 3.2.81 3.2.82 3.2.83 3.2.84 3.2.85 3.2.86 3.2.87 3.2.88 3.2.89 3.2.90 3.2.91 3.2.92 3.2.93 3.2.94 3.2.95 3.2.96 3.2.97 3.2.98 3.2.99 3.3.00 3.3.01 3.3.02 3.3.03 3.3.04 3.3.05 3.3.06 3.3.07 3.3.08 3.3.09 3.3.10 3.3.11 3.3.12 3.3.13 3.3.14 3.3.15 3.3.16 3.3.17 3.3.18 3.3.19 3.3.20 3.3.21 3.3.22 3.3.23 3.3.24 3.3.25 3.3.26 3.3.27 3.3.28 3.3.29 3.3.30 3.3.31 3.3.32 3.3.33 3.3.34 3.3.35 3.3.36 3.3.37 3.3.38 3.3.39 3.3.40 3.3.41 3.3.42 3.3.43 3.3.44 3.3.45 3.3.46 3.3.47 3.3.48 3.3.49 3.3.50 3.3.51 3.3.52 3.3.53 3.3.54 3.3.55
download-manager / src / AssetManager / Asset.php
download-manager / src / AssetManager Last commit date
views 1 week ago Asset.php 1 year ago AssetManager.php 1 year ago
Asset.php
286 lines
1 <?php
2
3 namespace WPDM\AssetManager;
4
5
6 use WPDM\__\__;
7 use WPDM\__\FileSystem;
8 use WPDM\__\Template;
9
10 class Asset
11 {
12 var $ID;
13 var $activities = array();
14 var $comments = array();
15 var $access = array();
16 var $links = array();
17 var $metadata = array();
18 var $path;
19 var $preview;
20 var $name;
21 var $type;
22 var $size;
23 var $temp_download_url;
24 var $dbtable;
25
26 function __construct($path = null){
27 global $wpdb;
28 $this->dbtable = "{$wpdb->prefix}ahm_assets";
29 if($path)
30 $this->init($path);
31 }
32
33 public function init($path){
34 global $wpdb;
35 if(!$this->get($path)) {
36 $wpdb->insert($this->dbtable, array('path' => $path));
37 $this->ID = $wpdb->insert_id;
38 $this->path = $path;
39 $this->preview = self::preview($this->path);
40 $this->name = basename($this->path);
41 $this->type = is_dir($this->path)?'dir':'file';
42 $this->size = wpdm_file_size($this->path);
43 $this->temp_download_url = home_url("/?asset={$this->ID}&key=".wp_create_nonce($this->path));
44 }
45 return $this;
46 }
47
48 function get($path_or_id){
49 global $wpdb;
50 $id = (int)$path_or_id;
51 $idcond = $id > 0 ? " or ID = '$id'":"";
52 $assetmeta = $wpdb->get_row("select * from {$wpdb->prefix}ahm_assets where path = '{$path_or_id}' {$idcond}");
53 if ($assetmeta){
54 $this->ID = $assetmeta->ID;
55 $this->activities = json_decode($assetmeta->activities);
56 $this->comments = json_decode($assetmeta->comments);
57 $this->access = json_decode($assetmeta->access);
58 $this->links = $this->getLinks();
59 $this->metadata = json_decode($assetmeta->metadata);
60 $this->path = $assetmeta->path;
61 $this->preview = self::preview($this->path);
62 $this->name = basename($this->path);
63 $this->type = is_dir($this->path)?'dir':'file';
64 $this->size = wpdm_file_size($this->path);
65 $this->temp_download_url = home_url("/?asset={$this->ID}&key=".wp_create_nonce($this->path));
66 return $this;
67 }
68 return null;
69 }
70
71 function resolveKey($key){
72 global $wpdb;
73 $key = esc_sql(esc_attr($key));
74 $asset_link = $wpdb->get_row("select * from {$wpdb->prefix}ahm_asset_links where asset_key='$key'");
75 $asset_id = $asset_link->asset_ID;
76 $assetmeta = $wpdb->get_row("select * from {$wpdb->prefix}ahm_assets where ID = '{$asset_id}'");
77 if ($assetmeta){
78 $this->ID = $assetmeta->ID;
79 $this->activities = json_decode($assetmeta->activities);
80 $this->comments = json_decode($assetmeta->comments);
81 $this->access = json_decode($asset_link->access);
82 $this->links = $this->getLinks();
83 $this->metadata = json_decode($assetmeta->metadata);
84 $this->path = $assetmeta->path;
85 $this->preview = self::preview($this->path);
86 $this->name = basename($this->path);
87 $this->type = is_dir($this->path)?'dir':'file';
88 $this->size = wpdm_file_size($this->path);
89 $this->temp_download_url = home_url("/?asset={$this->ID}&key=".wp_create_nonce($this->path));
90 return $this;
91 }
92 return null;
93 }
94
95 function newComment($comment, $user){
96 if(!is_array($this->comments)) $this->comments = array();
97 $userdata = get_userdata($user);
98 $avatar = get_avatar($user);
99 $comment = array( 'comment' => $comment, 'user' => $user, 'username' => $userdata->user_login, 'name' => $userdata->display_name, 'email' => $userdata->user_email, 'avatar' => $avatar, 'time' => time() );
100 array_unshift($this->comments, $comment);
101 return $this;
102 }
103
104 function newLink($access = array('roles' => array('guest'), 'users' => array())){
105 global $wpdb;
106 $asset_key = uniqid();
107 $wpdb->insert("{$wpdb->prefix}ahm_asset_links", array('asset_ID' => $this->ID, 'asset_key' => $asset_key, 'access' => serialize($access)));
108 $this->links = $this->getLinks();
109 return $this;
110 }
111
112 function getLinks(){
113 global $wpdb;
114 $links = $wpdb->get_results("select * from {$wpdb->prefix}ahm_asset_links where asset_ID = '{$this->ID}'");
115 foreach ($links as &$link){
116 $link->url = home_url('/wpdm-asset/'.$link->asset_key);
117 $link->access = json_decode($link->access);
118 }
119
120 return $links;
121 }
122
123 public static function getLink($ID){
124 global $wpdb;
125 $link = $wpdb->get_row("select * from {$wpdb->prefix}ahm_asset_links where ID = '{$ID}'");
126 $link->url = home_url('/wpdm-asset/'.$link->asset_key);
127 $link->access = json_decode($link->access);
128 if(!is_array($link->access)) $link->access = array();
129 if(!isset($link->access['roles'])) $link->access['roles'] = array();
130 if(!isset($link->access['users'])) $link->access['users'] = array();
131
132 return $link;
133 }
134
135 public static function updateLink($data, $ID){
136 global $wpdb;
137 return $wpdb->update("{$wpdb->prefix}ahm_asset_links", $data, array('ID' => $ID));
138 }
139
140 public static function deleteLink($ID){
141 global $wpdb;
142 return $wpdb->delete("{$wpdb->prefix}ahm_asset_links", array('ID' => $ID));
143 }
144
145 function newActivity($activity, $user){
146 if(!is_array($this->activities)) $this->activities = array();
147 $this->activities[] = array( 'activity' => $activity, 'user' => $user, 'time' => time() );
148 return $this;
149 }
150
151 function newMeta($name, $value){
152 $this->metadata[] = array( $name => $value );
153 return $this;
154 }
155
156 public static function preview($path){
157 global $current_user;
158 $ext = explode('.', $path);
159 $ext = end($ext);
160 $ext = strtolower($ext);
161 $url = str_replace(ABSPATH, home_url('/'), $path);
162 $accessible = ini_get('allow_url_fopen') && __::is_url($url) ? @get_headers($url) : [403];
163 $accessible = is_array($accessible) && substr_count($accessible[0], '403') ? false : true;
164 $relpath = str_replace(AssetManager::root(), "", $path);
165 if(!$accessible)
166 $url = home_url("/?wpdmfmdl={$relpath}");
167 $image = array('png', 'jpg', 'jpeg');
168 if(is_dir($path))
169 return "<img style='padding:20px;width: 128px' src='".WPDM_BASE_URL."assets/images/folder.svg"."' alt='Preview' />";
170 if(in_array($ext, $image))
171 return "<img style='padding:20px;' src='".wpdm_dynamic_thumb($path, array(400, 0), false)."' alt='Preview' />";
172 if($ext == 'svg')
173 return file_get_contents($path);
174 if($ext == 'mp3')
175 return do_shortcode("[audio src='$url']");
176 if($ext == 'mp4')
177 return do_shortcode("[video src='$url']");
178
179 $icon = \WPDM\__\FileSystem::fileTypeIcon($ext);
180
181 return "<img src='$icon' style='padding: 20px;width: 128px'/>";
182
183 }
184
185 public static function view($path){
186 global $current_user;
187 $ext = explode('.', $path);
188 $ext = end($ext);
189 $ext = strtolower($ext);
190 $url = str_replace(ABSPATH, home_url('/'), $path);
191 $accessible = __::is_url($url) ? get_headers($url) : [403];
192 $accessible = strstr($accessible[0], '403') ? false : true;
193 $relpath = str_replace(AssetManager::root(), "", $path);
194 $asset = new Asset($path);
195 if(!$accessible)
196 $url = $asset->temp_download_url;
197 $image = array('png', 'jpg', 'jpeg');
198 if(is_dir($path))
199 return $asset->dirViewer();
200 if(in_array($ext, $image))
201 return "<div class='wpdm-asset wpdm-asset-image'><img title='Click for fullscreen view' style='cursor: pointer' onclick='this.requestFullscreen();' src='{$asset->temp_download_url}' alt='Preview' /></div>";
202 if($ext == 'svg')
203 return "<div class='wpdm-asset wpdm-asset-svg'>".file_get_contents($path)."</div>";
204 if($ext == 'mp3')
205 return do_shortcode("<div class='wpdm-asset wpdm-asset-audio'>[audio src='$url']</div>");
206 if($ext == 'mp4')
207 return do_shortcode("<div class='wpdm-asset wpdm-asset-video'>[video src='$url']</div>");
208 $mime = wp_check_filetype($path);
209 if(strstr(wpdm_valueof($mime, 'type'), "text/")) {
210 $class = str_replace("text/", "", wpdm_valueof($mime, 'type'));
211 return "<pre class='wpdm-asset wpdm-asset-text'><code class='$class'>" . esc_attr(file_get_contents($path)) . "</code></pre>";
212 }
213 $icon = \WPDM\__\FileSystem::fileTypeIcon($ext);
214
215 return "<img src='$icon' style='padding: 20px;width: 128px'/>";
216
217 }
218
219 function hasAccess(){
220 global $current_user;
221 $roles = isset($this->access->roles) && is_array($this->access->roles) ? $this->access->roles : array();
222 $users = isset($this->access->users) && is_array($this->access->users) ? $this->access->users : array();
223 if(current_user_can('manage_options')) return true;
224 if(count(array_intersect($current_user->roles, $roles)) > 0) return true;
225 if(in_array('guest', $roles)) return true;
226 if(in_array($current_user->user_login, $users)) return true;
227 return false;
228 }
229
230 function dirViewer(){
231 $dirTree = $this->dirIterator($this->path);
232 ob_start();
233 include Template::locate("dir-viewer.php", __DIR__.'/views');
234 $viewer = ob_get_clean();
235 return $viewer;
236 }
237
238 private function dirIterator($path){
239 $iterator = new \DirectoryIterator( $path );
240 $data = [];
241 foreach ( $iterator as $node )
242 {
243 if ( $node->isDir() && !$node->isDot() )
244 {
245 $data["_".md5($node->getPathname())] = [ 'path' => $node->getPathname(), 'type' => 'dir', 'items' => $this->dirIterator( $node->getPathname() ) , 'name' => $node->getFilename() ];
246 }
247 else if ( $node->isFile() )
248 {
249 $data["_".md5($node->getPathname())] = [ 'path' => $node->getPathname(), 'type' => 'file' , 'name' => $node->getFilename() ];
250 }
251 }
252 return $data;
253 }
254
255 function updatePath($new_path){
256 global $wpdb;
257 $this->path = $new_path;
258 $this->name = basename($new_path);
259 $wpdb->update($this->dbtable, array('path' => $this->path ), array('ID' => $this->ID));
260 return $this;
261 }
262
263 function save(){
264 global $wpdb;
265 $data = array(
266 'activities' => json_encode($this->activities),
267 'comments' => json_encode($this->comments),
268 'access' => json_encode($this->access),
269 'metadata' => json_encode($this->metadata),
270 );
271 $wpdb->update($this->dbtable, $data, array('ID' => $this->ID));
272 return $this;
273 }
274
275 public static function delete($path_or_id){
276 global $wpdb;
277 $path_or_id = wpdm_sanitize_var($path_or_id);
278 $id = (int)$path_or_id;
279 return $wpdb->query("delete from {$wpdb->prefix}ahm_assets where ID='{$id}' or path='{$path_or_id}'");
280 }
281
282 function download(){
283 \WPDM\__\FileSystem::downloadFile($this->path, wp_basename($this->name));
284 }
285 }
286