Пример #1
0
/**
* Validate video type
*
* Function to work out what type of video has been requested and
* whether it is valid. Also fetches the video title.
*
* @since	2.0
*
* @uses     aye_set_general_defaults Get the default settings
*
* @param	string	$id				Video ID
* @param	string	$title_needed	Whether the title requires extracting
* @return	string					Array containing file details
*/
function aye_validate_id($id, $title_needed = false)
{
    $type = false;
    $title = false;
    $options = false;
    $options = aye_set_general_defaults();
    // Attempt to get the video type and title from cache
    if ($options['info_cache'] != 0) {
        $type = get_transient('aye_type_' . $id);
        $title = get_transient('aye_title_' . $id);
    }
    // Check if items are cached
    $cache = true;
    if (!$type or !$title) {
        $cache = false;
    }
    // Get video information if not cached
    if (!$cache) {
        $type = '-';
        if ($options['api'] != 0) {
            if ($options['api'] == 2 or $options['api'] == 4) {
                $https = 's';
            } else {
                $https = '';
            }
            // Check with YouTube API as to whether the ID is a video
            $id_check = aye_get_file('http' . $https . '://gdata.youtube.com/feeds/api/videos/' . $id . '?v=2', false);
            if ($id_check['rc'] == 0) {
                if ($id_check['response'] != 200) {
                    // Check with YouTube API as to whether the ID is a playlist
                    if (strtolower(substr($id, 0, 2)) == 'pl') {
                        $id = substr($id, 2);
                    }
                    $id_check = aye_get_file('http' . $https . '://gdata.youtube.com/feeds/api/playlists/' . $id . '?v=2', false);
                    if ($id_check['rc'] == 0) {
                        if ($id_check['response'] != 200) {
                            $type = '';
                        } else {
                            $type = 'p';
                        }
                    }
                } else {
                    $type = 'v';
                }
            }
        }
        // If no type has been assigned then an API error must have occured
        if ($type == '-') {
            if ($options['api'] == 1 or $options['api'] == 2) {
                // If reporting API errors, output it
                $type = sprintf(__('An error occurred accessing the YouTube API for video ID %s - %s', 'youtube-embed'), $id, $id_check['error']);
            } else {
                // If not reporting errors or API switched off, work out whether a video or playlist
                $type = '';
                if (strlen($id) == 11) {
                    $type = 'v';
                } else {
                    if (strlen($id) == 16) {
                        $type = 'p';
                    }
                }
            }
        }
        // Update the cache
        set_transient('aye_type_' . $id, $type, $options['info_cache'] * 3600);
        // If the video is valid extract the title from the XML
        if ($title_needed && $id_check['file'] != '') {
            // Find title in XML
            $tag_start = strpos($id_check['file'], '<title>', 1) + 7;
            if ($tag_start !== false) {
                $tag_end = strpos($id_check['file'], '</title>', $tag_start);
                if ($tag_end !== false) {
                    $title = substr($id_check['file'], $tag_start, $tag_end - $tag_start);
                }
            }
            // Update the cache
            set_transient('aye_title_' . $id, $title, $options['info_cache'] * 3600);
        }
    }
    if (!$title_needed) {
        return $type;
    }
    $return['type'] = $type;
    $return['title'] = $title;
    return $return;
}
Пример #2
0
/**
* Get XML transcript
*
* Return XML formatted YouTube transcript
*
* @since	2.0
*
* @uses		aye_error					Output an error
* @uses		aye_extract_id				Extract a video ID
* @uses		aye_get_file					Get a file
* @uses		aye_validate_id				Check the video ID is valid
*
* @param    string		$id				YouTube video ID
* @return	string						Transcript file in XML format
*/
function get_youtube_transcript_xml($id)
{
    // Extract the ID if a full URL has been specified
    $id = aye_extract_id($id);
    // Check what type of video it is and whether it's valid
    $embed_type = aye_validate_id($id);
    if ($embed_type != 'v') {
        if (strlen($embed_type) > 1) {
            echo aye_error($embed_type);
        } else {
            echo aye_error(sprintf(__('The YouTube ID of %s is invalid.', 'youtube-embed'), $id));
        }
        return;
    }
    // Get transcript file
    $filename = 'http://video.google.com/timedtext?lang=en&v=' . $id;
    $xml = aye_get_file($filename);
    // Check success and return appropriate output
    if ($xml['rc'] > 0) {
        echo aye_error(sprintf(__('Could not fetch the transcript file %s.', 'youtube-embed'), $id));
        return;
    } else {
        return $xml;
    }
}
/**
* Get YouTube Transcript
*
* Generate re-encoded YouTube transcript
*
* @since	2.0
*
* @uses     aye_extract_id              Extract the ID
* @uses		aye_get_file			    Get a file
* @uses     aye_set_general_defaults    Set default options
* @uses     aye_validate_id             Validate the ID
*
* @param	string	$id			        Video ID
* @return	string	$output		        Transcript output
*/
function aye_generate_transcript($id)
{
    // Extract the ID if a full URL has been specified
    $id = aye_extract_id($id);
    // Check what type of video it is and whether it's valid
    $embed_type = aye_validate_id($id);
    if ($embed_type != 'v') {
        if (strlen($embed_type) > 1) {
            return aye_error($embed_type);
        } else {
            return aye_error(sprintf(__('The YouTube ID of %s is invalid.', 'youtube-embed'), $id));
        }
    }
    // Get general options
    $general = aye_set_general_defaults();
    // Check to see if cache is available
    if ($general['transcript_cache'] != 0) {
        $cache_key = 'aye_transcript_' . $id;
        $output = get_transient($cache_key);
        if ($output !== false) {
            return $output;
        }
    }
    // Get transcript file
    $return = aye_get_file('http://video.google.com/timedtext?lang=en&v=' . $id);
    $xml = $return['file'];
    $output = '';
    // If transcript file exists, strip and output
    if ($return['rc'] == 0) {
        $output = "<!-- YouTube Embed v" . youtube_embed_version . " | http://www.artiss.co.uk/youtube-embed -->\n";
        $pos = 0;
        $eof = false;
        while (!$eof) {
            $text_start = strpos($xml, '<text ', $pos);
            if ($text_start !== false) {
                // Extract the start time
                $start_start = strpos($xml, 'start="', $text_start) + 7;
                $start_end = strpos($xml, '"', $start_start) - 1;
                $start = substr($xml, $start_start, $start_end - $start_start + 1);
                // Convert time format
                $start = str_pad(floor($start), 3, '0', STR_PAD_LEFT);
                $start = substr($start, 0, -2) . ':' . substr($start, -2, 2);
                // Now extract the text
                $text_start = strpos($xml, '>', $text_start) + 1;
                $text_end = strpos($xml, '</text>', $text_start) - 1;
                $text = substr($xml, $text_start, $text_end - $text_start + 1);
                // Now return the output
                $output .= "<div class=\"Transcript\"><span class=\"TranscriptTime\">" . $start . "</span> <span class=\"TranscriptText\">" . htmlspecialchars_decode($text) . "</span></div>\n";
                $pos = $text_end + 7;
            } else {
                $eof = true;
            }
        }
        $output .= "<!-- End of YouTube Embed code -->\n";
    }
    // Save the cache
    if ($general['transcript_cache'] != 0) {
        set_transient($cache_key, $output, 3600 * $general['transcript_cache']);
    }
    return $output;
}