-}
-
-function set_category_path($cat) {
- $cat->fullpath = '/' . $cat->category_nicename;
- $path = $cat->fullpath;
- $curcat = $cat;
- while ($curcat->category_parent != 0) {
- $curcat = get_category($curcat->category_parent);
- $path = '/' . $curcat->category_nicename . $path;
- }
-
- $cat->fullpath = $path;
-
- return $cat;
-}
-
-// Retrieves category data given a category ID or category object.
-// Handles category caching.
-function &get_category(&$category, $output = OBJECT) {
- global $wpdb;
-
- if ( empty($category) )
- return null;
-
- if ( is_object($category) ) {
- wp_cache_add($category->cat_ID, $category, 'category');
- $_category = $category;
- } else {
- if ( ! $_category = wp_cache_get($category, 'category') ) {
- $_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$category' LIMIT 1");
- wp_cache_add($category, $_category, 'category');
- }
- }
-
- $_category = apply_filters('get_category', $_category);
-
- if ( !isset($_category->fullpath) ) {
- $_category = set_category_path($_category);
- wp_cache_replace($_category->cat_ID, $_category, 'category');
- }
-
- if ( $output == OBJECT ) {
- return $_category;
- } elseif ( $output == ARRAY_A ) {
- return get_object_vars($_category);
- } elseif ( $output == ARRAY_N ) {
- return array_values(get_object_vars($_category));
- } else {
- return $_category;
- }
-}
-
-// Retrieves comment data given a comment ID or comment object.
-// Handles comment caching.
-function &get_comment(&$comment, $output = OBJECT) {
- global $comment_cache, $wpdb;
-
- if ( empty($comment) )
- return null;
-
- if ( is_object($comment) ) {
- if ( !isset($comment_cache[$comment->comment_ID]) )
- $comment_cache[$comment->comment_ID] = &$comment;
- $_comment = & $comment_cache[$comment->comment_ID];
- } else {
- if ( !isset($comment_cache[$comment]) ) {
- $_comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment' LIMIT 1");
- $comment_cache[$comment->comment_ID] = & $_comment;
- } else {
- $_comment = & $comment_cache[$comment];
- }
- }
-
- if ( $output == OBJECT ) {
- return $_comment;
- } elseif ( $output == ARRAY_A ) {
- return get_object_vars($_comment);
- } elseif ( $output == ARRAY_N ) {
- return array_values(get_object_vars($_comment));
- } else {
- return $_comment;
- }
-}
-
-function get_catname($cat_ID) {
- $category = &get_category($cat_ID);
- return $category->cat_name;
-}
-
-function get_all_category_ids() {
- global $wpdb;
-
- if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) {
- $cat_ids = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");
- wp_cache_add('all_category_ids', $cat_ids, 'category');
- }
-
- return $cat_ids;
-}
-
-function get_all_page_ids() {
- global $wpdb;
-
- if ( ! $page_ids = wp_cache_get('all_page_ids', 'pages') ) {
- $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_status='static'");
- wp_cache_add('all_page_ids', $page_ids, 'pages');
- }
-
- return $page_ids;
-}
-
-function gzip_compression() {
- if ( !get_settings('gzipcompression') ) return false;
-
- if ( extension_loaded('zlib') ) {
- ob_start('ob_gzhandler');
- }
-}
-
-
-// functions to count the page generation time (from phpBB2)
-// ( or just any time between timer_start() and timer_stop() )
-
-function timer_stop($display = 0, $precision = 3) { //if called like timer_stop(1), will echo $timetotal
- global $timestart, $timeend;
- $mtime = microtime();
- $mtime = explode(' ',$mtime);
- $mtime = $mtime[1] + $mtime[0];
- $timeend = $mtime;
- $timetotal = $timeend-$timestart;
- if ( $display )
- echo number_format($timetotal,$precision);
- return $timetotal;
-}
-
-function weblog_ping($server = '', $path = '') {
- global $wp_version;
- include_once (ABSPATH . WPINC . '/class-IXR.php');
-
- // using a timeout of 3 seconds should be enough to cover slow servers
- $client = new IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
- $client->timeout = 3;
- $client->useragent .= ' -- WordPress/'.$wp_version;
-
- // when set to true, this outputs debug messages by itself
- $client->debug = false;
- $home = trailingslashit( get_option('home') );
- if ( !$client->query('weblogUpdates.extendedPing', get_settings('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
- $client->query('weblogUpdates.ping', get_settings('blogname'), $home);
-}
-
-function generic_ping($post_id = 0) {
- $services = get_settings('ping_sites');
- $services = preg_replace("|(\s)+|", '$1', $services); // Kill dupe lines
- $services = trim($services);
- if ( '' != $services ) {
- $services = explode("\n", $services);
- foreach ($services as $service) {
- weblog_ping($service);
- }
- }
-
- return $post_id;
-}
-
-// Send a Trackback
-function trackback($trackback_url, $title, $excerpt, $ID) {
- global $wpdb, $wp_version;
-
- if ( empty($trackback_url) )
- return;
-
- $title = urlencode($title);
- $excerpt = urlencode($excerpt);
- $blog_name = urlencode(get_settings('blogname'));
- $tb_url = $trackback_url;
- $url = urlencode(get_permalink($ID));
- $query_string = "title=$title&url=$url&blog_name=$blog_name&excerpt=$excerpt";
- $trackback_url = parse_url($trackback_url);
- $http_request = 'POST ' . $trackback_url['path'] . ($trackback_url['query'] ? '?'.$trackback_url['query'] : '') . " HTTP/1.0\r\n";
- $http_request .= 'Host: '.$trackback_url['host']."\r\n";
- $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset='.get_settings('blog_charset')."\r\n";
- $http_request .= 'Content-Length: '.strlen($query_string)."\r\n";
- $http_request .= "User-Agent: WordPress/" . $wp_version;
- $http_request .= "\r\n\r\n";
- $http_request .= $query_string;
- if ( '' == $trackback_url['port'] )
- $trackback_url['port'] = 80;
- $fs = @fsockopen($trackback_url['host'], $trackback_url['port'], $errno, $errstr, 4);
- @fputs($fs, $http_request);
-/*
- $debug_file = 'trackback.log';
- $fp = fopen($debug_file, 'a');
- fwrite($fp, "\n*****\nRequest:\n\n$http_request\n\nResponse:\n\n");
- while(!@feof($fs)) {
- fwrite($fp, @fgets($fs, 4096));
- }
- fwrite($fp, "\n\n");
- fclose($fp);
-*/
- @fclose($fs);
-
- $tb_url = addslashes( $tb_url );
- $wpdb->query("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = '$ID'");
- return $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_url', '')) WHERE ID = '$ID'");
-}
-
-function make_url_footnote($content) {
- preg_match_all('/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches);
- $j = 0;
- for ($i=0; $i<count($matches[0]); $i++) {
- $links_summary = (!$j) ? "\n" : $links_summary;
- $j++;
- $link_match = $matches[0][$i];
- $link_number = '['.($i+1).']';
- $link_url = $matches[2][$i];
- $link_text = $matches[4][$i];
- $content = str_replace($link_match, $link_text.' '.$link_number, $content);
- $link_url = ((strtolower(substr($link_url,0,7)) != 'http://') && (strtolower(substr($link_url,0,8)) != 'https://')) ? get_settings('home') . $link_url : $link_url;
- $links_summary .= "\n".$link_number.' '.$link_url;
- }
- $content = strip_tags($content);
- $content .= $links_summary;
- return $content;
-}
-
-
-function xmlrpc_getposttitle($content) {
- global $post_default_title;
- if ( preg_match('/<title>(.+?)<\/title>/is', $content, $matchtitle) ) {
- $post_title = $matchtitle[0];
- $post_title = preg_replace('/<title>/si', '', $post_title);
- $post_title = preg_replace('/<\/title>/si', '', $post_title);
- } else {
- $post_title = $post_default_title;
- }
- return $post_title;
-}
-
-function xmlrpc_getpostcategory($content) {
- global $post_default_category;
- if ( preg_match('/<category>(.+?)<\/category>/is', $content, $matchcat) ) {
- $post_category = trim($matchcat[1], ',');
- $post_category = explode(',', $post_category);
- } else {
- $post_category = $post_default_category;
- }
- return $post_category;
-}
-
-function xmlrpc_removepostdata($content) {
- $content = preg_replace('/<title>(.+?)<\/title>/si', '', $content);
- $content = preg_replace('/<category>(.+?)<\/category>/si', '', $content);
- $content = trim($content);
- return $content;
-}
-
-function debug_fopen($filename, $mode) {
- global $debug;
- if ( $debug == 1 ) {
- $fp = fopen($filename, $mode);
- return $fp;
- } else {
- return false;
- }
-}
-
-function debug_fwrite($fp, $string) {
- global $debug;
- if ( $debug == 1 ) {
- fwrite($fp, $string);
- }
-}
-
-function debug_fclose($fp) {
- global $debug;
- if ( $debug == 1 ) {
- fclose($fp);
- }
-}
-
-function spawn_pinger() {
- global $wpdb, $wp_version;
- $doping = false;
- if ( $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE TRIM(to_ping) != '' LIMIT 1") )
- $doping = true;
-
- if ( $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_pingme' OR meta_key = '_encloseme' LIMIT 1") )
- $doping = true;
-
- if ( substr(php_sapi_name(), 0, 3) == 'cgi' )
- return $doping;
-
- if ( $doping ) {
- $ping_url = get_settings('siteurl') .'/wp-admin/execute-pings.php';
- $parts = parse_url($ping_url);
- $argyle = @ fsockopen($parts['host'], $_SERVER['SERVER_PORT'], $errno, $errstr, 0.01);
- if ( $argyle )
- fputs($argyle, "GET {$parts['path']}?time=".time()." HTTP/1.0\r\nHost: {$_SERVER['HTTP_HOST']}\r\nUser-Agent: WordPress/{$wp_version}\r\n\r\n");
- }
-}
-
-function do_enclose( $content, $post_ID ) {
- global $wp_version, $wpdb;
- include_once (ABSPATH . WPINC . '/class-IXR.php');
-
- $log = debug_fopen(ABSPATH . '/enclosures.log', 'a');
- $post_links = array();
- debug_fwrite($log, 'BEGIN '.date('YmdHis', time())."\n");
-
- $pung = get_enclosed( $post_ID );
-
- $ltrs = '\w';
- $gunk = '/#~:.?+=&%@!\-';
- $punc = '.:?\-';
- $any = $ltrs . $gunk . $punc;
-
- preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
-
- debug_fwrite($log, 'Post contents:');
- debug_fwrite($log, $content."\n");
-
- foreach($post_links_temp[0] as $link_test) :
- if ( !in_array($link_test, $pung) ) : // If we haven't pung it already
- $test = parse_url($link_test);
- if ( isset($test['query']) )
- $post_links[] = $link_test;
- elseif (($test['path'] != '/') && ($test['path'] != ''))
- $post_links[] = $link_test;
- endif;
- endforeach;
-
- foreach ($post_links as $url) :
- if ( $url != '' && !$wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE post_id = '$post_ID' AND meta_key = 'enclosure' AND meta_value LIKE ('$url%')") ) {
- if ( $headers = wp_get_http_headers( $url) ) {
- $len = (int) $headers['content-length'];
- $type = $wpdb->escape( $headers['content-type'] );
- $allowed_types = array( 'video', 'audio' );
- if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
- $meta_value = "$url\n$len\n$type\n";
- $wpdb->query( "INSERT INTO `$wpdb->postmeta` ( `post_id` , `meta_key` , `meta_value` )
- VALUES ( '$post_ID', 'enclosure' , '$meta_value')" );
- }
- }
- }
- endforeach;
-}
-
-function wp_get_http_headers( $url, $red = 1 ) {
- global $wp_version;
- @set_time_limit( 60 );
-
- if ( $red > 5 )
- return false;
-
- $parts = parse_url( $url );
- $file = $parts['path'] . ($parts['query'] ? '?'.$parts['query'] : '');
- $host = $parts['host'];
- if ( !isset( $parts['port'] ) )
- $parts['port'] = 80;
-
- $head = "HEAD $file HTTP/1.1\r\nHOST: $host\r\nUser-Agent: WordPress/" . $wp_version . "\r\n\r\n";
-
- $fp = @fsockopen($host, $parts['port'], $err_num, $err_msg, 3);
- if ( !$fp )
- return false;
-
- $response = '';
- fputs( $fp, $head );
- while ( !feof( $fp ) && strpos( $response, "\r\n\r\n" ) == false )
- $response .= fgets( $fp, 2048 );
- fclose( $fp );
- preg_match_all('/(.*?): (.*)\r/', $response, $matches);
- $count = count($matches[1]);
- for ( $i = 0; $i < $count; $i++) {
- $key = strtolower($matches[1][$i]);
- $headers["$key"] = $matches[2][$i];
- }
-
- preg_match('/.*([0-9]{3}).*/', $response, $return);
- $headers['response'] = $return[1]; // HTTP response code eg 204, 200, 404
-
- $code = $headers['response'];
- if ( ('302' == $code || '301' == $code) && isset($headers['location']) )
- return wp_get_http_headers( $headers['location'], ++$red );
-
- return $headers;
-}
-
-// Deprecated. Use the new post loop.
-function start_wp() {
- global $wp_query, $post;
-
- // Since the old style loop is being used, advance the query iterator here.
- $wp_query->next_post();
-
- setup_postdata($post);
-}
-
-// Setup global post data.
-function setup_postdata($post) {
- global $id, $postdata, $authordata, $day, $page, $pages, $multipage, $more, $numpages, $wp_query;
- global $pagenow;
-
- $id = $post->ID;
-
- $authordata = get_userdata($post->post_author);
-
- $day = mysql2date('d.m.y', $post->post_date);
- $currentmonth = mysql2date('m', $post->post_date);
- $numpages = 1;
- $page = get_query_var('page');
- if ( !$page )
- $page = 1;
- if ( is_single() || is_page() )
- $more = 1;
- $content = $post->post_content;
- if ( preg_match('/<!--nextpage-->/', $content) ) {
- if ( $page > 1 )
- $more = 1;
- $multipage = 1;
- $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
- $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
- $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
- $pages = explode('<!--nextpage-->', $content);
- $numpages = count($pages);
- } else {
- $pages[0] = $post->post_content;
- $multipage = 0;
- }
- return true;
-}
-
-// Setup global user vars. Used by set_current_user() for back compat.
-function setup_userdata($user_id = '') {
- global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_pass_md5, $user_identity;
-
- if ( '' == $user_id )
- $user = wp_get_current_user();
- else
- $user = new WP_User($user_id);
-
- if ( 0 == $user->ID )
- return;
-
- $userdata = $user->data;
- $user_login = $user->user_login;
- $user_level = $user->user_level;
- $user_ID = $user->ID;
- $user_email = $user->user_email;
- $user_url = $user->user_url;
- $user_pass_md5 = md5($user->user_pass);
- $user_identity = $user->display_name;
-}
-
-function is_new_day() {
- global $day, $previousday;
- if ( $day != $previousday ) {
- return(1);
- } else {
- return(0);
- }
-}
-
-// Filters: these are the core of WP's plugin architecture
-
-function merge_filters($tag) {
- global $wp_filter;
- if ( isset($wp_filter['all']) ) {
- foreach ($wp_filter['all'] as $priority => $functions) {
- if ( isset($wp_filter[$tag][$priority]) )
- $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], $wp_filter[$tag][$priority]);
- else
- $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], array());
- $wp_filter[$tag][$priority] = array_unique($wp_filter[$tag][$priority]);
- }
- }
-
- if ( isset($wp_filter[$tag]) )
- ksort( $wp_filter[$tag] );
-}
-
-function apply_filters($tag, $string) {
- global $wp_filter;