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 |