avail = $this->get_avail(); $this->cache_dir = trailingslashit( apply_filters( 'adobe_xmp_cache_dir', dirname( __FILE__ ) . '/cache/' ) ); require_once ( dirname ( __FILE__ ) . '/lib/shortcode.php' ); } public function get_avail() { $ret = array(); $ret[ 'media' ][ 'ngg' ] = class_exists( 'nggdb' ) && method_exists( 'nggdb', 'find_image' ) ? true : false; return $ret; } public function get_xmp( $pid ) { if ( isset( $this->cache_xmp[ $pid ] ) ) { return $this->cache_xmp[ $pid ]; } if ( is_string( $pid ) && substr( $pid, 0, 4 ) == 'ngg-' ) { return $this->cache_xmp[ $pid ] = $this->get_ngg_xmp( substr( $pid, 4 ), false ); } else { return $this->cache_xmp[ $pid ] = $this->get_media_xmp( $pid, false ); } } public function get_ngg_xmp( $pid ) { $xmp_arr = array(); if ( ! empty( $this->avail[ 'media' ][ 'ngg' ] ) ) { global $nggdb; $image = $nggdb->find_image( $pid ); if ( ! empty( $image->imagePath ) ) { $xmp_raw = $this->get_xmp_raw( $image->imagePath ); if ( ! empty( $xmp_raw ) ) { $xmp_arr = $this->get_xmp_array( $xmp_raw ); } } } return $xmp_arr; } public function get_media_xmp( $pid ) { $xmp_arr = array(); if ( $filepath = get_attached_file( $pid ) ) { $xmp_raw = $this->get_xmp_raw( get_attached_file( $pid ) ); if ( ! empty( $xmp_raw ) ) { $xmp_arr = $this->get_xmp_array( $xmp_raw ); } } return $xmp_arr; } public function get_xmp_raw( $filepath ) { $start_tag = 'cache_dir . md5( $filepath ) . '.xml'; $xmp_raw = null; if ( $this->use_cache && file_exists( $cache_file ) && filemtime( $cache_file ) > filemtime( $filepath ) && $cache_fh = fopen( $cache_file, 'rb' ) ) { $xmp_raw = fread( $cache_fh, filesize( $cache_file ) ); fclose( $cache_fh ); } elseif ( $file_fh = fopen( $filepath, 'rb' ) ) { $chunk = ''; $file_size = filesize( $filepath ); while ( ( $file_pos = ftell( $file_fh ) ) < $file_size && $file_pos < $this->max_size ) { $chunk .= fread( $file_fh, $this->chunk_size ); if ( false !== ( $end_pos = strpos( $chunk, $end_tag ) ) ) { if ( false !== ( $start_pos = strpos( $chunk, $start_tag ) ) ) { $xmp_raw = substr( $chunk, $start_pos, $end_pos - $start_pos + strlen( $end_tag ) ); if ( $this->use_cache && $cache_fh = fopen( $cache_file, 'wb' ) ) { fwrite( $cache_fh, $xmp_raw ); fclose( $cache_fh ); } } break; // Stop reading after finding the xmp data. } } fclose( $file_fh ); } return $xmp_raw; } public function get_xmp_array( $xmp_raw ) { $xmp_arr = array(); foreach ( array( 'Creator Email' => ']+?CiEmailWork="([^"]*)"', 'Owner Name' => ']+?aux:OwnerName="([^"]*)"', 'Creation Date' => ']+?xmp:CreateDate="([^"]*)"', 'Modification Date' => ']+?xmp:ModifyDate="([^"]*)"', 'Label' => ']+?xmp:Label="([^"]*)"', 'Credit' => ']+?photoshop:Credit="([^"]*)"', 'Source' => ']+?photoshop:Source="([^"]*)"', 'Headline' => ']+?photoshop:Headline="([^"]*)"', 'City' => ']+?photoshop:City="([^"]*)"', 'State' => ']+?photoshop:State="([^"]*)"', 'Country' => ']+?photoshop:Country="([^"]*)"', 'Country Code' => ']+?Iptc4xmpCore:CountryCode="([^"]*)"', 'Location' => ']+?Iptc4xmpCore:Location="([^"]*)"', 'Title' => '\s*\s*(.*?)\s*<\/rdf:Alt>\s*<\/dc:title>', 'Description' => '\s*\s*(.*?)\s*<\/rdf:Alt>\s*<\/dc:description>', 'Creator' => '\s*\s*(.*?)\s*<\/rdf:Seq>\s*<\/dc:creator>', 'Keywords' => '\s*\s*(.*?)\s*<\/rdf:Bag>\s*<\/dc:subject>', 'Hierarchical Keywords' => '\s*\s*(.*?)\s*<\/rdf:Bag>\s*<\/lr:hierarchicalSubject>' ) as $key => $regex ) { /** * Get a single text string. */ $xmp_arr[ $key ] = preg_match( '/' . $regex . '/is', $xmp_raw, $match ) ? $match[1] : ''; /** * If string contains a list, then re-assign the variable as an array with the list elements. */ $xmp_arr[ $key ] = preg_match_all( '/]*>([^>]*)<\/rdf:li>/is', $xmp_arr[ $key ], $match ) ? $match[1] : $xmp_arr[ $key ]; /** * Hierarchical keywords need to be split into a third dimension. */ if ( ! empty( $xmp_arr[ $key ] ) && $key == 'Hierarchical Keywords' ) { foreach ( $xmp_arr[ $key ] as $li => $val ) { $xmp_arr[ $key ][ $li ] = explode( '|', $val ); } unset ( $li, $val ); } } return $xmp_arr; } } global $adobeXMP; $adobeXMP =& adobeXMPforWP::get_instance(); }