", $text);
return $text;
}
function mysql2date($dateformatstring, $mysqlstring, $use_b2configmonthsdays = 1) {
global $month, $weekday;
$m = $mysqlstring;
if (empty($m)) {
return false;
}
$i = mktime(substr($m,11,2),substr($m,14,2),substr($m,17,2),substr($m,5,2),substr($m,8,2),substr($m,0,4));
if (!empty($month) && !empty($weekday) && $use_b2configmonthsdays) {
$datemonth = $month[date('m', $i)];
$dateweekday = $weekday[date('w', $i)];
$dateformatstring = ' '.$dateformatstring;
$dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit(substr($dateweekday, 0, 3)), $dateformatstring);
$dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
$dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
$dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit(substr($datemonth, 0, 3)), $dateformatstring);
$dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
}
$j = @date($dateformatstring, $i);
if (!$j) {
// for debug purposes
// echo $i." ".$mysqlstring;
}
return $j;
}
function current_time($type, $gmt = 0) {
switch ($type) {
case 'mysql':
if ($gmt) $d = gmdate('Y-m-d H:i:s');
else $d = gmdate('Y-m-d H:i:s', (time() + (get_settings('gmt_offset') * 3600)));
return $d;
break;
case 'timestamp':
if ($gmt) $d = time();
else $d = time() + (get_settings('gmt_offset') * 3600);
return $d;
break;
}
}
function date_i18n($dateformatstring, $unixtimestamp) {
global $month, $weekday;
$i = $unixtimestamp;
if ((!empty($month)) && (!empty($weekday))) {
$datemonth = $month[date('m', $i)];
$dateweekday = $weekday[date('w', $i)];
$dateformatstring = ' '.$dateformatstring;
$dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit(substr($dateweekday, 0, 3)), $dateformatstring);
$dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
$dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
$dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit(substr($datemonth, 0, 3)), $dateformatstring);
$dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
}
$j = @date($dateformatstring, $i);
return $j;
}
function get_weekstartend($mysqlstring, $start_of_week) {
$my = substr($mysqlstring,0,4);
$mm = substr($mysqlstring,8,2);
$md = substr($mysqlstring,5,2);
$day = mktime(0,0,0, $md, $mm, $my);
$weekday = date('w',$day);
$i = 86400;
while ($weekday > get_settings('start_of_week')) {
$weekday = date('w',$day);
$day = $day - 86400;
$i = 0;
}
$week['start'] = $day + 86400 - $i;
$week['end'] = $day + 691199;
return $week;
}
function get_lastpostdate($timezone = 'server') {
global $tableposts, $cache_lastpostdate, $pagenow, $wpdb;
$add_seconds_blog = get_settings('gmt_offset') * 3600;
$add_seconds_server = date('Z');
$now = current_time('mysql', 1);
if ( !isset($cache_lastpostdate[$timezone]) ) {
switch(strtolower($timezone)) {
case 'gmt':
$lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $tableposts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
break;
case 'blog':
$lastpostdate = $wpdb->get_var("SELECT post_date FROM $tableposts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
break;
case 'server':
$lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $tableposts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
break;
}
$cache_lastpostdate[$timezone] = $lastpostdate;
} else {
$lastpostdate = $cache_lastpostdate[$timezone];
}
return $lastpostdate;
}
function get_lastpostmodified($timezone = 'server') {
global $tableposts, $cache_lastpostmodified, $pagenow, $wpdb;
$add_seconds_blog = get_settings('gmt_offset') * 3600;
$add_seconds_server = date('Z');
$now = current_time('mysql', 1);
if ( !isset($cache_lastpostmodified[$timezone]) ) {
switch(strtolower($timezone)) {
case 'gmt':
$lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $tableposts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
break;
case 'blog':
$lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $tableposts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
break;
case 'server':
$lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $tableposts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
break;
}
$lastpostdate = get_lastpostdate($timezone);
if ($lastpostdate > $lastpostmodified) {
$lastpostmodified = $lastpostdate;
}
$cache_lastpostmodified[$timezone] = $lastpostmodified;
} else {
$lastpostmodified = $cache_lastpostmodified[$timezone];
}
return $lastpostmodified;
}
function user_pass_ok($user_login,$user_pass) {
global $cache_userdata;
if ( empty($cache_userdata[$user_login]) ) {
$userdata = get_userdatabylogin($user_login);
} else {
$userdata = $cache_userdata[$user_login];
}
return (md5($user_pass) == $userdata->user_pass);
}
function get_currentuserinfo() { // a bit like get_userdata(), on steroids
global $user_login, $userdata, $user_level, $user_ID, $user_nickname, $user_email, $user_url, $user_pass_md5, $cookiehash;
// *** retrieving user's data from cookies and db - no spoofing
if (isset($_COOKIE['wordpressuser_' . $cookiehash]))
$user_login = $_COOKIE['wordpressuser_' . $cookiehash];
$userdata = get_userdatabylogin($user_login);
$user_level = $userdata->user_level;
$user_ID = $userdata->ID;
$user_nickname = $userdata->user_nickname;
$user_email = $userdata->user_email;
$user_url = $userdata->user_url;
$user_pass_md5 = md5($userdata->user_pass);
}
function get_userdata($userid) {
global $wpdb, $cache_userdata, $tableusers;
if ( empty($cache_userdata[$userid]) ) {
$user = $wpdb->get_row("SELECT * FROM $tableusers WHERE ID = '$userid'");
$user->user_nickname = stripslashes($user->user_nickname);
$user->user_firstname = stripslashes($user->user_firstname);
$user->user_lastname = stripslashes($user->user_lastname);
$user->user_description = stripslashes($user->user_description);
$cache_userdata[$userid] = $user;
} else {
$user = $cache_userdata[$userid];
}
return $user;
}
function get_userdatabylogin($user_login) {
global $tableusers, $cache_userdata, $wpdb;
if ( empty($cache_userdata["$user_login"]) ) {
$user = $wpdb->get_row("SELECT * FROM $tableusers WHERE user_login = '$user_login'");
$cache_userdata["$user_login"] = $user;
} else {
$user = $cache_userdata["$user_login"];
}
return $user;
}
function get_userid($user_login) {
global $tableusers, $cache_userdata, $wpdb;
if ( empty($cache_userdata["$user_login"]) ) {
$user_id = $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_login = '$user_login'");
$cache_userdata["$user_login"] = $user_id;
} else {
$user_id = $cache_userdata["$user_login"];
}
return $user_id;
}
function get_usernumposts($userid) {
global $tableposts, $tablecomments, $wpdb;
return $wpdb->get_var("SELECT COUNT(*) FROM $tableposts WHERE post_author = '$userid'");
}
// examine a url (supposedly from this blog) and try to
// determine the post ID it represents.
function url_to_postid($url = '') {
global $wpdb, $tableposts;
$siteurl = get_settings('home');
// Take a link like 'http://example.com/blog/something'
// and extract just the '/something':
$uri = preg_replace("#$siteurl#i", '', $url);
// on failure, preg_replace just returns the subject string
// so if $uri and $siteurl are the same, they didn't match:
if ($uri == $siteurl)
return 0;
// First, check to see if there is a 'p=N' to match against:
preg_match('#[?&]p=(\d+)#', $uri, $values);
$p = intval($values[1]);
if ($p) return $p;
// Match $uri against our permalink structure
$permalink_structure = get_settings('permalink_structure');
// Matt's tokenizer code
$rewritecode = array(
'%year%',
'%monthnum%',
'%day%',
'%hour%',
'%minute%',
'%second%',
'%postname%',
'%post_id%'
);
$rewritereplace = array(
'([0-9]{4})?',
'([0-9]{1,2})?',
'([0-9]{1,2})?',
'([0-9]{1,2})?',
'([0-9]{1,2})?',
'([0-9]{1,2})?',
'([_0-9a-z-]+)?',
'([0-9]+)?'
);
// Turn the structure into a regular expression
$matchre = str_replace('/', '/?', $permalink_structure);
$matchre = str_replace($rewritecode, $rewritereplace, $matchre);
// Extract the key values from the uri:
preg_match("#$matchre#",$uri,$values);
// Extract the token names from the structure:
preg_match_all("#%(.+?)%#", $permalink_structure, $tokens);
for($i = 0; $i < count($tokens[1]); $i++) {
$name = $tokens[1][$i];
$value = $values[$i+1];
// Create a variable named $year, $monthnum, $day, $postname, or $post_id:
$$name = $value;
}
// If using %post_id%, we're done:
if (intval($post_id)) return intval($post_id);
// Otherwise, build a WHERE clause, making the values safe along the way:
if ($year) $where .= " AND YEAR(post_date) = '" . intval($year) . "'";
if ($monthnum) $where .= " AND MONTH(post_date) = '" . intval($monthnum) . "'";
if ($day) $where .= " AND DAYOFMONTH(post_date) = '" . intval($day) . "'";
if ($hour) $where .= " AND HOUR(post_date) = '" . intval($hour) . "'";
if ($minute) $where .= " AND MINUTE(post_date) = '" . intval($minute) . "'";
if ($second) $where .= " AND SECOND(post_date) = '" . intval($second) . "'";
if ($postname) $where .= " AND post_name = '" . $wpdb->escape($postname) . "' ";
// Run the query to get the post ID:
$id = intval($wpdb->get_var("SELECT ID FROM $tableposts WHERE 1 = 1 " . $where));
return $id;
}
/* Options functions */
function get_settings($setting) {
global $wpdb, $cache_settings;
if (strstr($_SERVER['REQUEST_URI'], 'install.php')) {
return false;
}
if ( (empty($cache_settings)) ) {
$settings = get_alloptions();
$cache_settings = $settings;
} else {
$settings = $cache_settings;
}
if ('home' == $setting && '' == $settings->home) return $settings->siteurl;
if (!isset($settings->$setting)) {
return false;
} else {
return stripslashes($settings->$setting);
}
}
function get_alloptions() {
global $tableoptions, $wpdb;
$options = $wpdb->get_results("SELECT option_name, option_value FROM $tableoptions");
if ($options) {
foreach ($options as $option) {
// "When trying to design a foolproof system,
// never underestimate the ingenuity of the fools :)"
if ('siteurl' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
if ('home' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
if ('category_base' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
$all_options->{$option->option_name} = $option->option_value;
}
}
return $all_options;
}
function update_option($option_name, $newvalue) {
global $wpdb, $tableoptions, $cache_settings;
$newvalue = stripslashes($newvalue);
$newvalue = trim($newvalue); // I can't think of any situation we wouldn't want to trim
$newvalue = $wpdb->escape($newvalue);
$wpdb->query("UPDATE $tableoptions SET option_value = '$newvalue' WHERE option_name = '$option_name'");
$cache_settings = get_alloptions(); // Re cache settings
return true;
}
// thx Alex Stapleton, http://alex.vort-x.net/blog/
function add_option($name, $value='') {
// Adds an option if it doesn't already exist
global $wpdb, $tableoptions;
if(!get_settings($name)) {
$name = $wpdb->escape($name);
$value = $wpdb->escape($value);
$wpdb->query("INSERT INTO $tableoptions (option_name, option_value) VALUES ('$name', '$value')");
if($wpdb->insert_id) {
global $cache_settings;
$cache_settings->{$name} = $value;
}
}
return;
}
function delete_option($name) {
global $wpdb, $tableoptions, $tableoptiongroup_options;
// Get the ID, if no ID then return
$option_id = $wpdb->get_var("SELECT option_id FROM $tableoptions WHERE option_name = '$name'");
if (!$option_id) return false;
$wpdb->query("DELETE FROM $tableoptiongroup_options WHERE option_id = '$option_id'");
$wpdb->query("DELETE FROM $tableoptions WHERE option_name = '$name'");
return true;
}
function get_postdata($postid) {
global $post, $tableposts, $wpdb;
$post = $wpdb->get_row("SELECT * FROM $tableposts WHERE ID = '$postid'");
$postdata = array (
'ID' => $post->ID,
'Author_ID' => $post->post_author,
'Date' => $post->post_date,
'Content' => $post->post_content,
'Excerpt' => $post->post_excerpt,
'Title' => $post->post_title,
'Category' => $post->post_category,
'Lat' => $post->post_lat,
'Lon' => $post->post_lon,
'post_status' => $post->post_status,
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_password' => $post->post_password,
'to_ping' => $post->to_ping,
'pinged' => $post->pinged,
'post_name' => $post->post_name
);
return $postdata;
}
function get_commentdata($comment_ID,$no_cache=0,$include_unapproved=false) { // less flexible, but saves DB queries
global $postc,$id,$commentdata,$tablecomments, $wpdb;
if ($no_cache) {
$query = "SELECT * FROM $tablecomments WHERE comment_ID = '$comment_ID'";
if (false == $include_unapproved) {
$query .= " AND comment_approved = '1'";
}
$myrow = $wpdb->get_row($query, ARRAY_A);
} else {
$myrow['comment_ID']=$postc->comment_ID;
$myrow['comment_post_ID']=$postc->comment_post_ID;
$myrow['comment_author']=$postc->comment_author;
$myrow['comment_author_email']=$postc->comment_author_email;
$myrow['comment_author_url']=$postc->comment_author_url;
$myrow['comment_author_IP']=$postc->comment_author_IP;
$myrow['comment_date']=$postc->comment_date;
$myrow['comment_content']=$postc->comment_content;
$myrow['comment_karma']=$postc->comment_karma;
if (strstr($myrow['comment_content'], '')) {
$myrow['comment_type'] = 'trackback';
} elseif (strstr($myrow['comment_content'], '')) {
$myrow['comment_type'] = 'pingback';
} else {
$myrow['comment_type'] = 'comment';
}
}
return $myrow;
}
function get_catname($cat_ID) {
global $tablecategories, $cache_catnames, $wpdb;
if ( !$cache_catnames ) {
$results = $wpdb->get_results("SELECT * FROM $tablecategories") or die('Oops, couldn\'t query the db for categories.');
foreach ($results as $post) {
$cache_catnames[$post->cat_ID] = $post->cat_name;
}
}
$cat_name = $cache_catnames[$cat_ID];
return $cat_name;
}
function gzip_compression() {
global $gzip_compressed;
if (strstr($_SERVER['PHP_SELF'], 'wp-admin')) return true;
if (!$gzip_compressed) {
$phpver = phpversion(); //start gzip compression
if($phpver >= "4.0.4pl1") {
if(extension_loaded("zlib")) {
ob_start("ob_gzhandler");
}
} else if($phpver > "4.0") {
if(strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
if(extension_loaded("zlib")) {
$do_gzip_compress = TRUE;
ob_start();
ob_implicit_flush(0);
header("Content-Encoding: gzip");
}
}
} //end gzip compression - that piece of script courtesy of the phpBB dev team
$gzip_compressed=1;
}
}
// functions to count the page generation time (from phpBB2)
// ( or just any time between timer_start() and timer_stop() )
function timer_start() {
global $timestart;
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$timestart = $mtime;
return true;
}
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 = '') {
$debug = false;
include_once (ABSPATH . WPINC . '/class-xmlrpc.php');
include_once (ABSPATH . WPINC . '/class-xmlrpcs.php');
$f = new xmlrpcmsg('weblogUpdates.ping',
array(new xmlrpcval(get_settings('blogname'), 'string'),
new xmlrpcval(get_settings('home') ,'string')));
$c = new xmlrpc_client($path, $server, 80);
$r = $c->send($f);
if ('0' != $r) {
if ($debug) {
echo "Response Object Dump:
\n";
print_r($r);
echo "
\n";
}
$v = @phpxmlrpc_decode($r->value());
if (!$r->faultCode()) {
$result['message'] = "";
$result['message'] = $result['message'] . $v["message"] . "
\n";
$result['message'] = $result['message'] . "
";
} else {
$result['err'] = $r->faultCode();
$result['message'] = "\n";
}
if ($debug) print '' . $result['message'] . '
';
}
}
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) {
$uri = parse_url($service);
weblog_ping($uri['host'], $uri['path']);
}
}
}
add_action('publish_post', 'generic_ping');
// Send a Trackback
function trackback($trackback_url, $title, $excerpt, $ID) {
global $wpdb, $tableposts;
$title = urlencode(stripslashes($title));
$excerpt = urlencode(stripslashes($excerpt));
$blog_name = urlencode(stripslashes(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'] . " HTTP/1.0\r\n";
$http_request .= 'Host: '.$trackback_url['host']."\r\n";
$http_request .= 'Content-Type: application/x-www-form-urlencoded'."\r\n";
$http_request .= 'Content-Length: '.strlen($query_string)."\r\n";
$http_request .= "\r\n";
$http_request .= $query_string;
$fs = @fsockopen($trackback_url['host'], 80);
@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);
$wpdb->query("UPDATE $tableposts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = '$ID'");
$wpdb->query("UPDATE $tableposts SET to_ping = REPLACE(to_ping, '$tb_url', '') WHERE ID = '$ID'");
return $result;
}
// trackback - reply
function trackback_response($error = 0, $error_message = '') {
if ($error) {
echo '\n";
echo "\n";
echo "1\n";
echo "$error_message\n";
echo "";
} else {
echo '\n";
echo "\n";
echo "0\n";
echo "";
}
die();
}
function make_url_footnote($content) {
preg_match_all('/