/** * Returns a list of mimetypes based off extension * * @param array|string $extensions * @return array|string */ static public function getMimeTypes( $extensions ) { $mimeTypes = cbGetMimeFromExt( $extensions ); if ( is_array( $extensions ) ) { if ( in_array( 'm4v', $extensions ) ) { $mimeTypes[] = 'video/mp4'; } } else { if ( $extensions == 'm4v' ) { $mimeTypes = 'video/mp4'; } } if ( is_array( $extensions ) ) { if ( in_array( 'mp3', $extensions ) ) { $mimeTypes[] = 'audio/mp3'; } } else { if ( $extensions == 'mp3' ) { $mimeTypes = 'audio/mp3'; } } if ( is_array( $extensions ) ) { if ( in_array( 'm4a', $extensions ) ) { $mimeTypes[] = 'audio/mp4'; } } else { if ( $extensions == 'm4a' ) { $mimeTypes = 'audio/mp4'; } } if ( is_array( $mimeTypes ) ) { $mimeTypes = array_unique( $mimeTypes ); } return $mimeTypes; }
/** * Returns the video mimetype from extension * * @return string */ public function mimeType() { if ( in_array( $this->domain(), array( 'youtube', 'youtu' ) ) ) { return 'video/youtube'; } static $cache = array(); $id = $this->extension(); if ( $id == 'm4v' ) { return 'video/mp4'; } if ( ! isset( $cache[$id] ) ) { $cache[$id] = cbGetMimeFromExt( $id ); } return $cache[$id]; }
/** * returns the mimetype of the supplied file or link * * @param string $value * @return string */ function getMimeType($value) { $domain = preg_replace('/^(?:(?:\\w+\\.)*)?(\\w+)\\..+$/', '\\1', parse_url($value, PHP_URL_HOST)); $extension = strtolower(pathinfo($domain ? $value : preg_replace('/[^-a-zA-Z0-9_.]/', '', $value), PATHINFO_EXTENSION)); if ($extension == 'mp3') { return 'audio/mp3'; } if ($extension == 'm4a') { return 'audio/mp4'; } return cbGetMimeFromExt($extension); }
/** * Returns the file mimetype from extension * * @return string */ public function mimeType() { static $cache = array(); $id = $this->extension(); if ( ! isset( $cache[$id] ) ) { $mimeType = $this->params()->get( 'mimetype' ); if ( ! $mimeType ) { $mimeType = cbGetMimeFromExt( $id ); } $cache[$id] = $mimeType; } return $cache[$id]; }
/** * Parsers a url for media information * * @param string $url * @return null|Registry */ public function attachment( $url ) { global $_CB_framework; $attachment = null; if ( $url && preg_match( $this->regexp['link'], $url ) ) { $cachePath = $_CB_framework->getCfg( 'absolute_path' ) . '/cache/activity_links'; $cache = $cachePath . '/' . md5( $url ); if ( file_exists( $cache ) ) { if ( ( ( $_CB_framework->getUTCNow() - filemtime( $cache ) ) / 3600 ) > 24 ) { $request = true; } else { $attachment = trim( file_get_contents( $cache ) ); $request = false; } } else { $request = true; } if ( $request ) { $client = @new \GuzzleHttp\Client(); try { $result = $client->get( $url ); if ( $result->getStatusCode() == 200 ) { $attachment['type'] = 'url'; $attachment['title'] = array(); $attachment['description'] = array(); $attachment['media'] = array( 'video' => array(), 'audio' => array(), 'image' => array() ); $attachment['url'] = $url; $document = @new \DOMDocument(); $body = (string) $result->getBody(); if ( function_exists( 'mb_convert_encoding' ) ) { $body = mb_convert_encoding( $body, 'HTML-ENTITIES', 'UTF-8' ); } else { $body = '<?xml encoding="UTF-8">' . $body; } @$document->loadHTML( $body ); $xpath = @new \DOMXPath( $document ); $paths = array( 'title' => array( '//meta[@name="og:title"]/@content', '//meta[@name="twitter:title"]/@content', '//meta[@name="title"]/@content', '//meta[@property="og:title"]/@content', '//meta[@property="twitter:title"]/@content', '//meta[@property="title"]/@content', '//title' ), 'description' => array( '//meta[@name="og:description"]/@content', '//meta[@name="twitter:description"]/@content', '//meta[@name="description"]/@content', '//meta[@property="og:description"]/@content', '//meta[@property="twitter:description"]/@content', '//meta[@property="description"]/@content' ), 'media' => array( 'video' => array( '//meta[@name="og:video"]/@content', '//meta[@name="og:video:url"]/@content', '//meta[@name="twitter:player"]/@content', '//meta[@property="og:video"]/@content', '//meta[@property="og:video:url"]/@content', '//meta[@property="twitter:player"]/@content', '//video/@src' ), 'audio' => array( '//meta[@name="og:audio"]/@content', '//meta[@name="og:audio:url"]/@content', '//meta[@property="og:audio"]/@content', '//meta[@property="og:audio:url"]/@content', '//audio/@src' ), 'image' => array( '//meta[@name="og:image"]/@content', '//meta[@name="og:image:url"]/@content', '//meta[@name="twitter:image"]/@content', '//meta[@name="image"]/@content', '//meta[@property="og:image"]/@content', '//meta[@property="og:image:url"]/@content', '//meta[@property="twitter:image"]/@content', '//meta[@property="image"]/@content', '//img/@src' ) ) ); foreach ( $paths as $item => $itemPaths ) { $attachment[$item] = array(); foreach ( $itemPaths as $subItem => $itemPath ) { if ( $item == 'media' ) { $attachment[$item][$subItem] = array(); $existing = array(); foreach ( $itemPath as $subItemPath ) { $nodes = @$xpath->query( $subItemPath ); if ( ( $nodes !== false ) && $nodes->length ) { foreach ( $nodes as $node ) { if ( preg_match( $this->regexp['link'], $node->nodeValue ) ) { if ( in_array( $node->nodeValue, $existing ) ) { continue; } $itemDomain = preg_replace( '/^(?:(?:\w+\.)*)?(\w+)\..+$/', '\1', parse_url( $node->nodeValue, PHP_URL_HOST ) ); $itemExt = strtolower( pathinfo( $node->nodeValue, PATHINFO_EXTENSION ) ); if ( in_array( $itemDomain, array( 'youtube', 'youtu' ) ) ) { $itemMimeType = 'video/youtube'; } else { if ( $itemExt == 'm4v' ) { $itemMimeType = 'video/mp4'; } elseif ( $itemExt == 'mp3' ) { $itemMimeType = 'audio/mp3'; } elseif ( $itemExt == 'm4a' ) { $itemMimeType = 'audio/mp4'; } else { $itemMimeType = cbGetMimeFromExt( $itemExt ); if ( $itemMimeType == 'application/octet-stream' ) { continue; } } } $attachment[$item][$subItem][] = array( 'url' => $node->nodeValue, 'mimetype' => $itemMimeType, 'extension' => $itemExt ); $existing[] = $node->nodeValue; } } } } } else { $nodes = @$xpath->query( $itemPath ); if ( ( $nodes !== false ) && $nodes->length ) { foreach ( $nodes as $node ) { if ( in_array( $node->nodeValue, $attachment[$item] ) ) { continue; } $attachment[$item][] = $node->nodeValue; } } } } } $urlDomain = preg_replace( '/^(?:(?:\w+\.)*)?(\w+)\..+$/', '\1', parse_url( $url, PHP_URL_HOST ) ); $urlExt = strtolower( pathinfo( $url, PATHINFO_EXTENSION ) ); if ( in_array( $urlDomain, array( 'youtube', 'youtu' ) ) ) { $urlMimeType = 'video/youtube'; } else { if ( $urlExt == 'm4v' ) { $urlMimeType = 'video/mp4'; } elseif ( $urlExt == 'mp3' ) { $urlMimeType = 'audio/mp3'; } elseif ( $urlExt == 'm4a' ) { $urlMimeType = 'audio/mp4'; } else { $urlMimeType = cbGetMimeFromExt( $urlExt ); } } if ( in_array( $urlDomain, array( 'youtube', 'youtu' ) ) || in_array( $urlExt, array( 'mp4', 'ogv', 'ogg', 'webm', 'm4v' ) ) ) { $attachment['media']['video'][] = array( 'url' => $url, 'mimetype' => $urlMimeType, 'extension' => $urlExt ); $attachment['type'] = 'video'; } elseif ( in_array( $urlExt, array( 'mp3', 'oga', 'ogg', 'weba', 'wav', 'm4a' ) ) ) { $attachment['media']['audio'][] = array( 'url' => $url, 'mimetype' => $urlMimeType, 'extension' => $urlExt ); $attachment['type'] = 'audio'; } elseif ( in_array( $urlExt, array( 'jpg', 'jpeg', 'gif', 'png' ) ) ) { $attachment['media']['image'][] = array( 'url' => $url, 'mimetype' => $urlMimeType, 'extension' => $urlExt ); $attachment['type'] = 'image'; } } } catch( \Exception $e ) {} $attachment = json_encode( $attachment ); if ( ! is_dir( $cachePath ) ) { $oldMask = @umask( 0 ); if ( @mkdir( $cachePath, 0755, true ) ) { @umask( $oldMask ); @chmod( $cachePath, 0755 ); } else { @umask( $oldMask ); } } file_put_contents( $cache, $attachment ); } if ( $attachment ) { $attachment = new Registry( $attachment ); $this->attachments[] = $attachment; } else { $attachment = null; } } return $attachment; }