dbtable = "{$wpdb->prefix}ahm_assets";
if($path)
$this->init($path);
}
public function init($path){
global $wpdb;
if(!$this->get($path)) {
$wpdb->insert($this->dbtable, array('path' => $path));
$this->ID = $wpdb->insert_id;
$this->path = $path;
$this->preview = self::preview($this->path);
$this->name = basename($this->path);
$this->type = is_dir($this->path)?'dir':'file';
$this->size = wpdm_file_size($this->path);
$this->temp_download_url = home_url("/?asset={$this->ID}&key=".wp_create_nonce($this->path));
}
return $this;
}
function get($path_or_id){
global $wpdb;
$id = (int)$path_or_id;
$idcond = $id > 0 ? " or ID = '$id'":"";
$assetmeta = $wpdb->get_row("select * from {$wpdb->prefix}ahm_assets where path = '{$path_or_id}' {$idcond}");
if ($assetmeta){
$this->ID = $assetmeta->ID;
$this->activities = maybe_unserialize($assetmeta->activities);
$this->comments = maybe_unserialize($assetmeta->comments);
$this->access = maybe_unserialize($assetmeta->access);
$this->links = array();
$this->metadata = maybe_unserialize($assetmeta->metadata);
$this->path = $assetmeta->path;
$this->preview = self::preview($this->path);
$this->name = basename($this->path);
$this->type = is_dir($this->path)?'dir':'file';
$this->size = wpdm_file_size($this->path);
$this->temp_download_url = home_url("/?asset={$this->ID}&key=".wp_create_nonce($this->path));
return $this;
}
return null;
}
public static function preview($path){
global $current_user;
$ext = explode('.', $path);
$ext = end($ext);
$ext = strtolower($ext);
$url = str_replace(ABSPATH, home_url('/'), $path);
$accessible = strstr($url, "://") ? get_headers($url) : false;
$accessible = strstr($accessible[0], '403') ? false : true;
$relpath = str_replace(AssetManager::root(), "", $path);
if(!$accessible)
$url = home_url("/?wpdmfmdl={$relpath}");
$image = array('png', 'jpg', 'jpeg');
if(is_dir($path))
return "";
if(in_array($ext, $image))
return "
";
if($ext == 'svg')
return file_get_contents($path);
if($ext == 'mp3')
return do_shortcode("[audio src='$url']");
if($ext == 'mp4')
return do_shortcode("[video src='$url']");
$icon = file_exists(WPDM_BASE_DIR."assets/file-type-icons/{$ext}.svg" ) ? WPDM_BASE_URL."assets/file-type-icons/{$ext}.svg" : WPDM_BASE_URL."assets/file-type-icons/unknown.svg";
return "
";
}
public static function view($path){
global $current_user;
$ext = explode('.', $path);
$ext = end($ext);
$ext = strtolower($ext);
$url = str_replace(ABSPATH, home_url('/'), $path);
$accessible = get_headers($url);
$accessible = strstr($accessible[0], '403') ? false : true;
$relpath = str_replace(AssetManager::root(), "", $path);
$asset = new Asset($path);
if(!$accessible)
$url = $asset->temp_download_url;
$image = array('png', 'jpg', 'jpeg');
if(is_dir($path))
return $asset->dirViewer();
if(in_array($ext, $image))
return "
" . esc_attr(file_get_contents($path)) . "{$script}";
}
$icon = file_exists(WPDM_BASE_DIR."assets/file-type-icons/{$ext}.svg" ) ? WPDM_BASE_URL."assets/file-type-icons/{$ext}.svg" : WPDM_BASE_URL."assets/file-type-icons/unknown.svg";
return "