File:  [mozdev] / cdn / www / b2-include / b2functions.php
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Fri Mar 7 17:24:51 2003 UTC (16 years, 5 months ago) by cdn
Branches: MAIN
CVS tags: HEAD
OOo

    1: <?php
    2: 
    3: /*
    4: if ( !isset($project) )
    5:   $project = 'cdn';
    6: */
    7: 
    8: /* new and improved ! now with more querystring stuff ! */
    9: 
   10: if (!isset($querystring_start)) {
   11: 	$querystring_start = '?';
   12: 	$querystring_equal = '=';
   13: 	$querystring_separator = '&amp;';
   14: }
   15: 
   16: if (!function_exists('_')) {
   17: 	function _($string) {
   18: 		return $string;
   19: 	}
   20: }
   21: 
   22: 
   23: /* functions... */
   24: 
   25: function get_currentuserinfo() { // a bit like get_userdata(), on steroids
   26: 	global $HTTP_COOKIE_VARS,$user_login,$userdata,$user_level,$user_ID,$user_nickname,$user_email,$user_url,$user_pass_md5;
   27: 	// *** retrieving user's data from cookies and db - no spoofing
   28: 	$user_login = $HTTP_COOKIE_VARS["cafeloguser"];
   29: 	$userdata = get_userdatabylogin($user_login);
   30: 	$user_level = $userdata["user_level"];
   31: 	$user_ID=$userdata['ID'];
   32: 	$user_nickname=$userdata["user_nickname"];
   33: 	$user_email=$userdata["user_email"];
   34: 	$user_url=$userdata["user_url"];
   35: 	$user_pass_md5=md5($userdata["user_pass"]);
   36: }
   37: 
   38: 
   39: 
   40: function dbconnect() {
   41: 	global $connexion, $server, $loginsql, $passsql, $base;
   42: 	$connexion = mysql_connect($server,$loginsql,$passsql) or die("Can't connect to the database server. MySQL said:<br />".mysql_error());
   43: 	$connexionbase = mysql_select_db("$base") or die("Can't connect to the database $base. MySQL said:<br />".mysql_error());
   44: 	return(($connexion && $connexionbase));
   45: }
   46: 
   47: 
   48: function mysql_oops($sql_query) {
   49: 	$error  = '<p>Oops, MySQL error!</p><p>Your query:<br />'.$query;
   50: 	$error .= '</p><p>MySQL said:<br />'.mysql_error().'</p>';
   51: 	die($error);
   52: }
   53: 
   54: 
   55: /***** Formatting functions *****/
   56: 
   57: function autobrize($content) {
   58: 	$content = preg_replace("/<br>\n/", "\n", $content);
   59: 	$content = preg_replace("/<br \/>\n/", "\n", $content);
   60: 	$content = preg_replace("/(\015\012)|(\015)|(\012)/", "<br />\n", $content);
   61: 	return($content);
   62: 	}
   63: function unautobrize($content) {
   64: 	$content = preg_replace("/<br>\n/", "\n", $content);   //for PHP versions before 4.0.5
   65: 	$content = preg_replace("/<br \/>\n/", "\n", $content);
   66: 	return($content);
   67: 	}
   68: 
   69: 
   70: function format_to_edit($content) {
   71: 	global $autobr;
   72: 	$content = stripslashes($content);
   73: 	if ($autobr) { $content = unautobrize($content); }
   74: 	$content = htmlspecialchars($content);
   75: 	return($content);
   76: 	}
   77: function format_to_post($content) {
   78: 	global $post_autobr,$comment_autobr;
   79: 	$content = addslashes($content);
   80: 	if ($post_autobr || $comment_autobr) { $content = autobrize($content); }
   81: 	return($content);
   82: 	}
   83: 
   84: 
   85: function zeroise($number,$threshold) { // function to add leading zeros when necessary
   86: 	$l=strlen($number);
   87: 	if ($l<$threshold)
   88: 		for ($i=0; $i<($threshold-$l); $i=$i+1) { $number='0'.$number;	}
   89: 	return($number);
   90: 	}
   91: 
   92: 
   93: function backslashit($string) {
   94: 	$string = preg_replace('/([a-z])/i', '\\\\\1', $string);
   95: 	return $string;
   96: }
   97: 
   98: 
   99: function mysql2date($dateformatstring, $mysqlstring, $use_b2configmonthsdays = 1) {
  100: 	global $month, $weekday;
  101: 	$m = $mysqlstring;
  102: 	if (empty($m)) {
  103: 		return false;
  104: 	}
  105: 	$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)); 
  106: 	if (!empty($month) && !empty($weekday) && $use_b2configmonthsdays) {
  107: 		$datemonth = $month[date('m', $i)];
  108: 		$dateweekday = $weekday[date('w', $i)];
  109: 		$dateformatstring = ' '.$dateformatstring;
  110: 		$dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit(substr($dateweekday, 0, 3)), $dateformatstring);
  111: 		$dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
  112: 		$dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
  113: 		$dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit(substr($datemonth, 0, 3)), $dateformatstring);
  114: 		$dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
  115: 	}
  116: 	$j = @date($dateformatstring, $i);
  117: 	if (!$j) {
  118: # // for debug purposes
  119: #		echo $i." ".$mysqlstring;
  120: 	}
  121: 	return $j;
  122: }
  123: 
  124: function addslashes_gpc($gpc) {
  125: 	if (!get_magic_quotes_gpc()) {
  126: 		$gpc = addslashes($gpc);
  127: 	}
  128: 	return($gpc);
  129: }
  130: 
  131: function date_i18n($dateformatstring, $unixtimestamp) {
  132: 	global $month, $weekday;
  133: 	$i = $unixtimestamp; 
  134: 	if ((!empty($month)) && (!empty($weekday))) {
  135: 		$datemonth = $month[date('m', $i)];
  136: 		$dateweekday = $weekday[date('w', $i)];
  137: 		$dateformatstring = ' '.$dateformatstring;
  138: 		$dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit(substr($dateweekday, 0, 3)), $dateformatstring);
  139: 		$dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
  140: 		$dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
  141: 		$dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit(substr($datemonth, 0, 3)), $dateformatstring);
  142: 		$dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
  143: 	}
  144: 	$j = @date($dateformatstring, $i);
  145: 	return $j;
  146: 	}
  147: 
  148: 
  149: 
  150: function get_weekstartend($mysqlstring, $start_of_week) {
  151: 	$my = substr($mysqlstring,0,4);
  152: 	$mm = substr($mysqlstring,8,2);
  153: 	$md = substr($mysqlstring,5,2);
  154: 	$day = mktime(0,0,0, $md, $mm, $my);
  155: 	$weekday = date('w',$day);
  156: 	$i = 86400;
  157: 	while ($weekday > $start_of_week) {
  158: 		$weekday = date('w',$day);
  159: 		$day = $day - 86400;
  160: 		$i = 0;
  161: 	}
  162: 	$week['start'] = $day + 86400 - $i;
  163: 	$week['end']   = $day + 691199;
  164: 	return ($week);
  165: }
  166: 
  167: function convert_chars($content,$flag="html") { // html/unicode entities output, defaults to html
  168: 	$newcontent = "";
  169: 
  170: 	global $convert_chars2unicode, $convert_entities2unicode, $leavecodealone, $use_htmltrans;
  171: 	global $b2_htmltrans, $b2_htmltranswinuni;
  172: 
  173: 	### this is temporary - will be replaced by proper config stuff
  174: 	$convert_chars2unicode = 1;
  175: 	if (($leavecodealone) || (!$use_htmltrans)) {
  176: 		$convert_chars2unicode = 0;
  177: 	}
  178: 	###
  179: 
  180: 
  181: 	// converts HTML-entities to their display values in order to convert them again later
  182: 
  183: 	$content = preg_replace("/<title>(.+?)<\/title>/","",$content);
  184: 	$content = preg_replace("/<category>(.+?)<\/category>/","",$content);
  185: 	
  186: #	$content = str_replace("&amp;","&#38;",$content);
  187: 	$content = strtr($content, $b2_htmltrans);
  188: 
  189: 	for ($i=0; $i<strlen($content); $i=$i+1) {
  190: 		$j = substr($content,$i,1);
  191: 		$jnext = substr($content,$i+1,1);
  192: 		$jord = ord($j);
  193: 		if ($convert_chars2unicode) {
  194: 			switch($flag) {
  195: 				case "unicode":
  196: 	//				$j = str_replace("&","&#38;",$j);
  197: 					if (($jord>=128) || ($j == "&") || (($jord>=128) && ($jord<=159))) {
  198: 						$j = "&#".$jord.";";
  199: 					}
  200: 					break;
  201: 				case "html":
  202: 					if (($jord>=128) || (($jord>=128) && ($jord<=159))) {
  203: 						$j = "&#".$jord.";"; // $j = htmlentities($j);
  204: 					} elseif (($j == "&") && ($jnext != "#")) {
  205: 						$j = "&amp;";
  206: 					}
  207: 					break;
  208: 				case "xml":
  209: 					if ($jord>=128) {
  210: 						$j = "&#".$jord.";"; // $j = htmlentities($j);
  211: 	//					$j = htmlentities($j);
  212: 					} elseif (($j == "&") && ($jnext != "#")) {
  213: 						$j = "&#38;";
  214: 					}
  215: 					break;
  216: 			}
  217: 		}
  218: 
  219: 		$newcontent .= $j;
  220: 	}
  221: 
  222: 	// now converting: Windows CP1252 => Unicode (valid HTML)
  223: 	// (if you've ever pasted text from MSWord, you'll understand)
  224: 
  225: 	$newcontent = strtr($newcontent, $b2_htmltranswinuni);
  226: 
  227: 	// you can delete these 2 lines if you don't like <br /> and <hr />
  228: 	$newcontent = str_replace("<br>","<br />",$newcontent);
  229: 	$newcontent = str_replace("<hr>","<hr />",$newcontent);
  230: 
  231: 	return($newcontent);
  232: }
  233: 
  234: function convert_bbcode($content) {
  235: 	global $b2_bbcode, $use_bbcode;
  236: 	if ($use_bbcode) {
  237: 		$content = preg_replace($b2_bbcode["in"], $b2_bbcode["out"], $content);
  238: 	}
  239: 	$content = convert_bbcode_email($content);
  240: 	return ($content);
  241: }
  242: 
  243: function convert_bbcode_email($content) {
  244: 	global $use_bbcode;
  245: 	$bbcode_email["in"] = array(
  246: 		'#\[email](.+?)\[/email]#eis',
  247: 		'#\[email=(.+?)](.+?)\[/email]#eis'
  248: 	);
  249: 	$bbcode_email["out"] = array(
  250: 		"'<a href=\"mailto:'.antispambot('\\1').'\">'.antispambot('\\1').'</a>'",		// E-mail
  251: 		"'<a href=\"mailto:'.antispambot('\\1').'\">\\2</a>'"
  252: 	);
  253: 
  254: 	$content = preg_replace($bbcode_email["in"], $bbcode_email["out"], $content);
  255: 	return ($content);
  256: }
  257: 
  258: function convert_gmcode($content) {
  259: 	global $b2_gmcode, $use_gmcode;
  260: 	if ($use_gmcode) {
  261: 		$content = preg_replace($b2_gmcode["in"], $b2_gmcode["out"], $content);
  262: 	}
  263: 	return ($content);
  264: }
  265: 
  266: function convert_smilies($content) {
  267: 	global $smilies_directory, $use_smilies;
  268: 	global $b2_smiliessearch, $b2_smiliesreplace;
  269: 	if ($use_smilies) {
  270: 		$content = str_replace($b2_smiliessearch, $b2_smiliesreplace, $content);
  271: 	}
  272: 	return ($content);
  273: }
  274: 
  275: function antispambot($emailaddy, $mailto=0) {
  276: 	$emailNOSPAMaddy = '';
  277: 	srand ((float) microtime() * 1000000);
  278: 	for ($i = 0; $i < strlen($emailaddy); $i = $i + 1) {
  279: 		$j = floor(rand(0, 1+$mailto));
  280: 		if ($j==0) {
  281: 			$emailNOSPAMaddy .= '&#'.ord(substr($emailaddy,$i,1)).';';
  282: 		} elseif ($j==1) {
  283: 			$emailNOSPAMaddy .= substr($emailaddy,$i,1);
  284: 		} elseif ($j==2) {
  285: 			$emailNOSPAMaddy .= '%'.zeroise(dechex(ord(substr($emailaddy, $i, 1))), 2);
  286: 		}
  287: 	}
  288: 	$emailNOSPAMaddy = str_replace('@','&#64;',$emailNOSPAMaddy);
  289: 	return $emailNOSPAMaddy;
  290: }
  291: 
  292: function make_clickable($text) { // original function: phpBB, extended here for AIM & ICQ
  293:     $ret = " " . $text;
  294:     $ret = preg_replace("#([\n ])([a-z]+?)://([^, <>{}\n\r]+)#i", "\\1<a href=\"\\2://\\3\" target=\"_blank\">\\2://\\3</a>", $ret);
  295:     $ret = preg_replace("#([\n ])aim:([^,< \n\r]+)#i", "\\1<a href=\"aim:goim?screenname=\\2\\3&message=Hello\">\\2\\3</a>", $ret);
  296:     $ret = preg_replace("#([\n ])icq:([^,< \n\r]+)#i", "\\1<a href=\"http://wwp.icq.com/scripts/search.dll?to=\\2\\3\">\\2\\3</a>", $ret);
  297:     $ret = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^,< \n\r]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\" target=\"_blank\">www.\\2.\\3\\4</a>", $ret);
  298:     $ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([^,< \n\r]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
  299:     $ret = substr($ret, 1);
  300:     return($ret);
  301: }
  302: 
  303: 
  304: function is_email($user_email) {
  305: 	$chars = "/^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}\$/i";
  306: 	if(strstr($user_email, '@') && strstr($user_email, '.')) {
  307: 		if (preg_match($chars, $user_email)) {
  308: 			return true;
  309: 		} else {
  310: 			return false;
  311: 		}
  312: 	} else {
  313: 		return false;
  314: 	}
  315: }
  316: 
  317: 
  318: function phpcurlme($string, $language = 'en') {
  319: 	// by Matt - http://www.photomatt.net/scripts/phpcurlme
  320: 
  321:     // This should take care of the single quotes
  322:     $string = preg_replace("/'([dmst])([ .,?!\)\/<])/i","&#8217;$1$2",$string);
  323:     $string = preg_replace("/'([lrv])([el])([ .,?!\)\/<])/i","&#8217;$1$2$3",$string);
  324:     $string = preg_replace("/([^=])(\s+)'([^ >])?(.*?)([^=])'(\s*)([^>&])/S","$1$2&#8216;$3$4$5&#8217;$6$7",$string);
  325: 
  326:     // time for the doubles
  327:     $string = preg_replace('/([^=])(\s+)"([^ >])?(.*?)([^=])"(\s*)([^>&])/S',"$1$2&#8220;$3$4$5&#8221;$6$7",$string);
  328:     // multi-paragraph
  329:     $string = preg_replace('/<p>"(.*)<\/p>/U',"<p>&#8220;$1</p>",$string);
  330: 
  331:     // not a quote, but whatever
  332:     $string = str_replace('---','&#8212;',$string);
  333:     $string = str_replace('--','&#8211;',$string);
  334:     return $string;
  335: }
  336: 
  337: 
  338: function strip_all_but_one_link($text, $mylink) {
  339: 	$match_link = '#(<a.+?href.+?'.'>)(.+?)(</a>)#';
  340: 	preg_match_all($match_link, $text, $matches);
  341: 	$count = count($matches[0]);
  342: 	for ($i=0; $i<$count; $i++) {
  343: 		if (!strstr($matches[0][$i], $mylink)) {
  344: 			$text = str_replace($matches[0][$i], $matches[2][$i], $text);
  345: 		}
  346: 	}
  347: 	return $text;
  348: }
  349: 
  350: 
  351: /***** // Formatting functions *****/
  352: 
  353: 
  354: 
  355: function get_lastpostdate() {
  356: 	global $tableposts, $cache_lastpostdate, $use_cache, $time_difference, $pagenow;
  357: 	if ((!isset($cache_lastpostdate)) OR (!$use_cache)) {
  358: 		$now = date("Y-m-d H:i:s",(time() + ($time_difference * 3600)));
  359: 		if ($pagenow != 'b2edit.php') {
  360: 			$showcatzero = 'post_category > 0 AND';
  361: 		} else {
  362: 			$showcatzero = '';
  363: 		}
  364: 		$sql = "SELECT * FROM $tableposts WHERE $showcatzero post_date <= '$now' ORDER BY post_date DESC LIMIT 1";
  365: 		$result = mysql_query($sql) or die("Your SQL query: <br />$sql<br /><br />MySQL said:<br />".mysql_error());
  366: 		$querycount++;
  367: 		$myrow = mysql_fetch_object($result);
  368: 		$lastpostdate = $myrow->post_date;
  369: 		$cache_lastpostdate = $lastpostdate;
  370: //		echo $lastpostdate;
  371: 	} else {
  372: 		$lastpostdate = $cache_lastpostdate;
  373: 	}
  374: 	return($lastpostdate);
  375: }
  376: 
  377: function user_pass_ok($user_login,$user_pass) {
  378: 	global $cache_userdata,$use_cache;
  379: 	if ((empty($cache_userdata[$user_login])) OR (!$use_cache)) {
  380: 		$userdata = get_userdatabylogin($user_login);
  381: 	} else {
  382: 		$userdata = $cache_userdata[$user_login];
  383: 	}
  384: 	return ($user_pass == $userdata['user_pass']);
  385: }
  386: 
  387: function get_userdata($userid) {
  388: 	global $tableusers,$querycount,$cache_userdata,$use_cache;
  389: 	if ((empty($cache_userdata[$userid])) OR (!$use_cache)) {
  390: 		$sql = "SELECT * FROM $tableusers WHERE ID = '$userid'";
  391: 		$result = mysql_query($sql) or die("Your SQL query: <br />$sql<br /><br />MySQL said:<br />".mysql_error());
  392: 		$myrow = mysql_fetch_array($result);
  393: 		$querycount++;
  394: 		$cache_userdata[$userid] = $myrow;
  395: 	} else {
  396: 		$myrow = $cache_userdata[$userid];
  397: 	}
  398: 	return($myrow);
  399: }
  400: 
  401: function get_userdata2($userid) { // for team-listing
  402: 	global $tableusers,$row;
  403: 	$user_data['ID'] = $userid;
  404: 	$user_data['user_login'] = $row->user_login;
  405: 	$user_data['user_firstname'] = $row->user_firstname;
  406: 	$user_data['user_lastname'] = $row->user_lastname;
  407: 	$user_data['user_nickname'] = $row->user_nickname;
  408: 	$user_data['user_level'] = $row->user_level;
  409: 	$user_data['user_email'] = $row->user_email;
  410: 	$user_data['user_url'] = $row->user_url;
  411: 	return($user_data);
  412: }
  413: 
  414: function get_userdatabylogin($user_login) {
  415: 	global $tableusers,$querycount,$cache_userdata,$use_cache;
  416: 	if ((empty($cache_userdata["$user_login"])) OR (!$use_cache)) {
  417: 		$sql = "SELECT * FROM $tableusers WHERE user_login = '$user_login'";
  418: 		$result = mysql_query($sql) or die("Your SQL query: <br />$sql<br /><br />MySQL said:<br />".mysql_error());
  419: 		if (!$result)	die($sql."<br /><br />".mysql_error());
  420: 		$myrow = mysql_fetch_array($result);
  421: 		$querycount++;
  422: 		$cache_userdata["$user_login"] = $myrow;
  423: 	} else {
  424: 		$myrow = $cache_userdata["$user_login"];
  425: 	}
  426: 	return($myrow);
  427: }
  428: 
  429: function get_userid($user_login) {
  430: 	global $tableusers,$querycount,$cache_userdata,$use_cache;
  431: 	if ((empty($cache_userdata["$user_login"])) OR (!$use_cache)) {
  432: 		$sql = "SELECT ID FROM $tableusers WHERE user_login = '$user_login'";
  433: 		$result = mysql_query($sql) or die("No user with the login <i>$user_login</i>");
  434: 		$myrow = mysql_fetch_array($result);
  435: 		$querycount++;
  436: 		$cache_userdata["$user_login"] = $myrow;
  437: 	} else {
  438: 		$myrow = $cache_userdata["$user_login"];
  439: 	}
  440: 	return($myrow[0]);
  441: }
  442: 
  443: function get_usernumposts($userid) {
  444: 	global $tableusers,$tablesettings,$tablecategories,$tableposts,$tablecomments,$querycount;
  445: 	$sql = "SELECT * FROM $tableposts WHERE post_author = $userid";
  446: 	$result = mysql_query($sql) or die("Your SQL query: <br />$sql<br /><br />MySQL said:<br />".mysql_error());
  447: 	$querycount++;
  448: 	return mysql_num_rows($result);
  449: }
  450: 
  451: function get_settings($setting) {
  452: 	global $tablesettings,$querycount,$cache_settings,$use_cache;
  453: 	if ((empty($cache_settings)) OR (!$use_cache)) {
  454: 		$sql = "SELECT * FROM $tablesettings";
  455: 		$result = mysql_query($sql) or die("Your SQL query: <br />$sql<br /><br />MySQL said:<br />".mysql_error());
  456: 		$querycount++;
  457: 		$myrow = mysql_fetch_object($result);
  458: 		$cache_settings = $myrow;
  459: 	} else {
  460: 		$myrow = $cache_settings;
  461: 	}
  462: 	return($myrow->$setting);
  463: }
  464: 
  465: function get_postdata($postid) {
  466: 	global $tableusers,$tablesettings,$tablecategories,$tableposts,$tablecomments,$querycount;
  467: 	$sql = "SELECT * FROM $tableposts WHERE ID = $postid";
  468: 	$result = mysql_query($sql) or die("Your SQL query: <br />$sql<br /><br />MySQL said:<br />".mysql_error());
  469: 	$querycount++;
  470: 	if (mysql_num_rows($result)) {
  471: 		$myrow = mysql_fetch_object($result);
  472: 		$postdata = array (
  473: 			'ID' => $myrow->ID, 
  474: 			'Author_ID' => $myrow->post_author, 
  475: 			'Date' => $myrow->post_date, 
  476: 			'Content' => $myrow->post_content, 
  477: 			'Title' => $myrow->post_title, 
  478: 			'Category' => $myrow->post_category, 
  479: 			);
  480: 		return($postdata);
  481: 	} else {
  482: 		return false;
  483: 	}
  484: }
  485: 
  486: function get_postdata2($postid=0) { // less flexible, but saves mysql queries
  487: 	global $row;
  488: 	$postdata = array (
  489: 		'ID' => $row->ID, 
  490: 		'Author_ID' => $row->post_author,
  491: 		'Date' => $row->post_date,
  492: 		'Content' => $row->post_content,
  493: 		'Title' => $row->post_title,
  494: 		'Category' => $row->post_category,
  495: #		'Notify' => $row->post_notifycomments,
  496: #		'Clickable' => $row->post_make_clickable,
  497: 		'Karma' => $row->post_karma // this isn't used yet
  498: 		);
  499: 	return($postdata);
  500: }
  501: 
  502: function get_commentdata($comment_ID,$no_cache=0) { // less flexible, but saves mysql queries
  503: 	global $rowc,$id,$commentdata,$tablecomments,$querycount;
  504: 	if ($no_cache) {
  505: 		$query="SELECT * FROM $tablecomments WHERE comment_ID = $comment_ID";
  506: 		$result=mysql_query($query);
  507: 		$querycount++;
  508: 		$myrow = mysql_fetch_array($result);
  509: 	} else {
  510: 		$myrow['comment_ID']=$rowc->comment_ID;
  511: 		$myrow['comment_post_ID']=$rowc->comment_post_ID;
  512: 		$myrow['comment_author']=$rowc->comment_author;
  513: 		$myrow['comment_author_email']=$rowc->comment_author_email;
  514: 		$myrow['comment_author_url']=$rowc->comment_author_url;
  515: 		$myrow['comment_author_IP']=$rowc->comment_author_IP;
  516: 		$myrow['comment_date']=$rowc->comment_date;
  517: 		$myrow['comment_content']=$rowc->comment_content;
  518: 		$myrow['comment_karma']=$rowc->comment_karma;
  519: 		if (strstr($myrow['comment_content'], '<trackback />')) {
  520: 			$myrow['comment_type'] = 'trackback';
  521: 		} elseif (strstr($myrow['comment_content'], '<pingback />')) {
  522: 			$myrow['comment_type'] = 'pingback';
  523: 		} else {
  524: 			$myrow['comment_type'] = 'comment';
  525: 		}
  526: 	}
  527: 	return($myrow);
  528: }
  529: 
  530: function get_catname($cat_ID) {
  531: 	global $tablecategories,$cache_catnames,$use_cache,$querycount;
  532: 	if ((!$cache_catnames) || (!$use_cache)) {
  533: 		$sql = "SELECT * FROM $tablecategories";
  534: 		$result = mysql_query($sql) or die('Oops, couldn\'t query the db for categories.');
  535: 		$querycount;
  536: 		while ($row = mysql_fetch_object($result)) {
  537: 			$cache_catnames[$row->cat_ID] = $row->cat_name;
  538: 		}
  539: 	}
  540: 	$cat_name = $cache_catnames[$cat_ID];
  541: 	return($cat_name);
  542: }
  543: 
  544: function profile($user_login) {
  545: 	global $user_data;
  546: 	echo "<a href=\"#\" OnClick=\"javascript:window.open('b2profile.php?user=".$user_data["user_login"]."','Profile','toolbar=0,status=1,location=0,directories=0,menuBar=1,scrollbars=1,resizable=0,width=480,height=320,left=100,top=100');\">$user_login</a>";
  547: }
  548: 
  549: function dropdown_categories($blog_ID=1) {
  550: 	global $postdata,$tablecategories,$mode,$querycount;
  551: 	$query="SELECT * FROM $tablecategories";
  552: 	$result=mysql_query($query);
  553: 	$querycount++;
  554: 	$width = ($mode=="sidebar") ? "100%" : "170px";
  555: 	echo '<select name="post_category" style="width:'.$width.';" tabindex="2" id="category">';
  556: 	while($row = mysql_fetch_object($result)) {
  557: 		echo "<option value=\"".$row->cat_ID."\"";
  558: 		if ($row->cat_ID == $postdata["Category"])
  559: 			echo " selected";
  560: 		echo ">".$row->cat_name."</option>";
  561: 	}
  562: 	echo "</select>";
  563: }
  564: 
  565: function touch_time($edit=1) {
  566: 	global $month, $postdata;
  567: 	echo $postdata['Date'];
  568: 	echo '<br /><br /><input type="checkbox" class="checkbox" name="edit_date" value="1" id="timestamp" /><label for="timestamp"> Edit timestamp</label><br />';
  569: 	
  570: 	$jj = ($edit) ? mysql2date('d', $postdata['Date']) : date('d');
  571: 	$mm = ($edit) ? mysql2date('m', $postdata['Date']) : date('m');
  572: 	$aa = ($edit) ? mysql2date('Y', $postdata['Date']) : date('Y');
  573: 	$hh = ($edit) ? mysql2date('H', $postdata['Date']) : date('H');
  574: 	$mn = ($edit) ? mysql2date('i', $postdata['Date']) : date('i');
  575: 	$ss = ($edit) ? mysql2date('s', $postdata['Date']) : date('s');
  576: 
  577: 	echo '<input type="text" name="jj" value="'.$jj.'" size="2" maxlength="2" />'."\n";
  578: 	echo "<select name=\"mm\">\n";
  579: 	for ($i=1; $i < 13; $i=$i+1) {
  580: 		echo "\t\t\t<option value=\"$i\"";
  581: 		if ($i == $mm)
  582: 		echo " selected";
  583: 		if ($i < 10) {
  584: 			$ii = "0".$i;
  585: 		} else {
  586: 			$ii = "$i";
  587: 		}
  588: 		echo ">".$month["$ii"]."</option>\n";
  589: 	} ?>
  590: </select>
  591: <input type="text" name="aa" value="<?php echo $aa ?>" size="4" maxlength="5" /> @ 
  592: <input type="text" name="hh" value="<?php echo $hh ?>" size="2" maxlength="2" /> : 
  593: <input type="text" name="mn" value="<?php echo $mn ?>" size="2" maxlength="2" /> : 
  594: <input type="text" name="ss" value="<?php echo $ss ?>" size="2" maxlength="2" />
  595: 	<?php
  596: }
  597: 
  598: function gzip_compression() {
  599: 	global $gzip_compressed;
  600: 		if (!$gzip_compressed) {
  601: 		$phpver = phpversion(); //start gzip compression
  602: 		if($phpver >= "4.0.4pl1") {
  603: 			if(extension_loaded("zlib")) { ob_start("ob_gzhandler"); }
  604: 		} else if($phpver > "4.0") {
  605: 			if(strstr($HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING'], 'gzip')) {
  606: 				if(extension_loaded("zlib")) { $do_gzip_compress = TRUE; ob_start(); ob_implicit_flush(0); header("Content-Encoding: gzip");  }
  607: 			}
  608: 		} //end gzip compression - that piece of script courtesy of the phpBB dev team
  609: 		$gzip_compressed=1;
  610: 	}
  611: }
  612: 
  613: function alert_error($msg) { // displays a warning box with an error message (original by KYank)
  614: 	global $$HTTP_SERVER_VARS;
  615: 	?>
  616: 	<html>
  617: 	<head>
  618: 	<script language="JavaScript">
  619: 	<!--
  620: 	alert("<?php echo $msg ?>");
  621: 	history.back();
  622: 	//-->
  623: 	</script>
  624: 	</head>
  625: 	<body>
  626: 	<!-- this is for non-JS browsers (actually we should never reach that code, but hey, just in case...) -->
  627: 	<?php echo $msg; ?><br />
  628: 	<a href="<?php echo $HTTP_SERVER_VARS["HTTP_REFERER"]; ?>">go back</a>
  629: 	</body>
  630: 	</html>
  631: 	<?php
  632: 	exit;
  633: }
  634: 
  635: function alert_confirm($msg) { // asks a question - if the user clicks Cancel then it brings them back one page
  636: 	?>
  637: 	<script language="JavaScript">
  638: 	<!--
  639: 	if (!confirm("<?php echo $msg ?>")) {
  640: 	history.back();
  641: 	}
  642: 	//-->
  643: 	</script>
  644: 	<?php
  645: }
  646: 
  647: function redirect_js($url,$title="...") {
  648: 	?>
  649: 	<script language="JavaScript">
  650: 	<!--
  651: 	function redirect() {
  652: 	window.location = "<?php echo $url; ?>";
  653: 	}
  654: 	setTimeout("redirect();", 100);
  655: 	//-->
  656: 	</script>
  657: 	<p>Redirecting you : <b><?php echo $title; ?></b><br />
  658: 	<br />
  659: 	If nothing happens, click <a href="<?php echo $url; ?>">here</a>.</p>
  660: 	<?php
  661: 	exit();
  662: }
  663: 
  664: // functions to count the page generation time (from phpBB2)
  665: // ( or just any time between timer_start() and timer_stop() )
  666: 
  667: 	function timer_start() {
  668: 		global $timestart;
  669: 		$mtime = microtime();
  670: 		$mtime = explode(" ",$mtime);
  671: 		$mtime = $mtime[1] + $mtime[0];
  672: 		$timestart = $mtime;
  673: 		return true;
  674: 	}
  675: 
  676: 	function timer_stop($display=0,$precision=3) { //if called like timer_stop(1), will echo $timetotal
  677: 		global $timestart,$timeend;
  678: 		$mtime = microtime();
  679: 		$mtime = explode(" ",$mtime);
  680: 		$mtime = $mtime[1] + $mtime[0];
  681: 		$timeend = $mtime;
  682: 		$timetotal = $timeend-$timestart;
  683: 		if ($display)
  684: 			echo number_format($timetotal,$precision);
  685: 		return($timetotal);
  686: 	}
  687: 
  688: 
  689: // pings Weblogs.com
  690: function pingWeblogs($blog_ID = 1) {
  691: 	// original function by Dries Buytaert for Drupal
  692: 	global $use_weblogsping, $blogname,$siteurl,$blogfilename;
  693: 	if ((!(($blogname=="my weblog") && ($siteurl=="http://example.com") && ($blogfilename=="b2.php"))) && (!preg_match("/localhost\//",$siteurl)) && ($use_weblogsping)) {
  694: 		$client = new xmlrpc_client("/RPC2", "rpc.weblogs.com", 80);
  695: 		$message = new xmlrpcmsg("weblogUpdates.ping", array(new xmlrpcval($blogname), new xmlrpcval($siteurl."/".$blogfilename)));
  696: 		$result = $client->send($message);
  697: 		if (!$result || $result->faultCode()) {
  698: 			return(false);
  699: 		}
  700: 		return(true);
  701: 	} else {
  702: 		return(false);
  703: 	}
  704: }
  705: 
  706: // pings Weblogs.com/rssUpdates
  707: function pingWeblogsRss($blog_ID = 1, $rss_url) {
  708: 	global $use_weblogsrssping, $blogname, $rss_url;
  709: 	if ($blogname != 'my weblog' && $rss_url != 'http://example.com/b2rdf.php' && $use_weblogsrssping) {
  710: 		$client = new xmlrpc_client('/RPC2', 'rssrpc.weblogs.com', 80);
  711: 		$message = new xmlrpcmsg('rssUpdate', array(new xmlrpcval($blogname), new xmlrpcval($rss_url)));
  712: 		$result = $client->send($message);
  713: 		if (!$result || $result->faultCode()) {
  714: 			return(false);
  715: 		}
  716: 		return(true);
  717: 	} else {
  718: 		return(false);
  719: 	}
  720: }
  721: 
  722: // pings CaféLog.com
  723: function pingCafelog($cafelogID,$title='',$p='') {
  724: 	global $use_cafelogping, $blogname, $siteurl, $blogfilename;
  725: 	if ((!(($blogname=="my weblog") && ($siteurl=="http://example.com") && ($blogfilename=="b2.php"))) && (!preg_match("/localhost\//",$siteurl)) && ($use_cafelogping) && ($cafelogID != '')) {
  726: 		$client = new xmlrpc_client("/xmlrpc.php", "cafelog.tidakada.com", 80);
  727: 		$message = new xmlrpcmsg("b2.ping", array(new xmlrpcval($cafelogID), new xmlrpcval($title), new xmlrpcval($p)));
  728: 		$result = $client->send($message);
  729: 		if (!$result || $result->faultCode()) {
  730: 			return(false);
  731: 		}
  732: 		return(true);
  733: 	} else {
  734: 		return(false);
  735: 	}
  736: }
  737: 
  738: // pings Blo.gs
  739: function pingBlogs($blog_ID="1") {
  740: 	global $use_blodotgsping, $blodotgsping_url, $use_rss, $blogname, $siteurl, $blogfilename;
  741: 	if ((!(($blogname=='my weblog') && ($siteurl=='http://example.com') && ($blogfilename=='b2.php'))) && (!preg_match('/localhost\//',$siteurl)) && ($use_blodotgsping)) {
  742: 		$url = ($blodotgsping_url == 'http://example.com') ? $siteurl.'/'.$blogfilename : $blodotgsping_url;
  743: 		$client = new xmlrpc_client('/', 'ping.blo.gs', 80);
  744: 		if ($use_rss) {
  745: 			$message = new xmlrpcmsg('weblogUpdates.extendedPing', array(new xmlrpcval($blogname), new xmlrpcval($url), new xmlrpcval($url), new xmlrpcval($siteurl.'/b2rss.xml')));
  746: 		} else {
  747: 			$message = new xmlrpcmsg('weblogUpdates.ping', array(new xmlrpcval($blogname), new xmlrpcval($url)));
  748: 		}
  749: 		$result = $client->send($message);
  750: 		if (!$result || $result->faultCode()) {
  751: 			return(false);
  752: 		}
  753: 		return(true);
  754: 	} else {
  755: 		return(false);
  756: 	}
  757: }
  758: 
  759: 
  760: // trackback - send
  761: function trackback($trackback_url, $title, $excerpt, $ID) {
  762: 	global $siteurl, $blogfilename, $blogname;
  763: 	global $querystring_start, $querystring_equal;
  764: 	$title = urlencode($title);
  765: 	$excerpt = urlencode(stripslashes($excerpt));
  766: 	$blog_name = urlencode($blogname);
  767: 	$url = urlencode($siteurl.'/'.$blogfilename.$querystring_start.'p'.$querystring_equal.$ID);
  768: 	$query_string = "title=$title&url=$url&blog_name=$blog_name&excerpt=$excerpt";
  769: 	if (strstr($trackback_url, '?')) {
  770: 		$trackback_url .= "&".$query_string;;
  771: 		$fp = @fopen($trackback_url, 'r');
  772: 		$result = @fread($fp, 4096);
  773: 		@fclose($fp);
  774: /* debug code
  775: 		$debug_file = 'trackback.log';
  776: 		$fp = fopen($debug_file, 'a');
  777: 		fwrite($fp, "\n*****\nTrackback URL query:\n\n$trackback_url\n\nResponse:\n\n");
  778: 		fwrite($fp, $result);
  779: 		fwrite($fp, "\n\n");
  780: 		fclose($fp);
  781: */
  782: 	} else {
  783: 		$trackback_url = parse_url($trackback_url);
  784: 		$http_request  = 'POST '.$trackback_url['path']." HTTP/1.0\r\n";
  785: 		$http_request .= 'Host: '.$trackback_url['host']."\r\n";
  786: 		$http_request .= 'Content-Type: application/x-www-form-urlencoded'."\r\n";
  787: 		$http_request .= 'Content-Length: '.strlen($query_string)."\r\n";
  788: 		$http_request .= "\r\n";
  789: 		$http_request .= $query_string;
  790: 		$fs = @fsockopen($trackback_url['host'], 80);
  791: 		@fputs($fs, $http_request);
  792: /* debug code
  793: 		$debug_file = 'trackback.log';
  794: 		$fp = fopen($debug_file, 'a');
  795: 		fwrite($fp, "\n*****\nRequest:\n\n$http_request\n\nResponse:\n\n");
  796: 		while(!@feof($fs)) {
  797: 			fwrite($fp, @fgets($fs, 4096));
  798: 		}
  799: 		fwrite($fp, "\n\n");
  800: 		fclose($fp);
  801: */
  802: 		@fclose($fs);
  803: 	}
  804: 	return $result;
  805: }
  806: 
  807: // trackback - reply
  808: function trackback_response($error = 0, $error_message = '') {
  809: 	if ($error) {
  810: 		echo '<?xml version="1.0" encoding="iso-8859-1"?'.">\n";
  811: 		echo "<response>\n";
  812: 		echo "<error>1</error>\n";
  813: 		echo "<message>$error_message</message>\n";
  814: 		echo "</response>";
  815: 	} else {
  816: 		echo '<?xml version="1.0" encoding="iso-8859-1"?'.">\n";
  817: 		echo "<response>\n";
  818: 		echo "<error>0</error>\n";
  819: 		echo "</response>";
  820: 	}
  821: 	die();
  822: }
  823: 
  824: // updates the RSS feed !
  825: function rss_update($blog_ID, $num_posts="", $file="./b2rss.xml") {
  826: 
  827: 	global $use_rss, $b2_version, $querystring_start, $querystring_equal, $querystring_separator;
  828: 	global $admin_email,$blogname,$siteurl,$blogfilename,$blogdescription,$posts_per_rss,$rss_language;
  829: 	global $tableposts,$postdata,$row;
  830: 
  831: 	if ($rss_language == '') {
  832: 		$rss_language = 'en';
  833: 	}
  834: 
  835: 	if ($use_rss) {
  836: 
  837: 		$num_posts = ($num_posts=="") ? $posts_per_rss : 5;
  838: 
  839: 		$date_now = gmdate("D, d M Y H:i:s")." GMT";
  840: 
  841: 		# let's build the rss file
  842: 		$rss = '';
  843: 
  844: 		$rss .= '<?xml version="1.0"?'.">\n";
  845: 		$rss .= "<!-- generator=\"b2/$b2_version\" -->\n";
  846: 		$rss .= "<rss version=\"0.92\">\n";
  847: 		$rss .= "\t<channel>\n";
  848: 		$rss .= "\t\t<title>".convert_chars(strip_tags(get_bloginfo("name")),"unicode")."</title>\n";
  849: 		$rss .= "\t\t<link>".convert_chars(strip_tags(get_bloginfo("url")),"unicode")."</link>\n";
  850: 		$rss .= "\t\t<description>".convert_chars(strip_tags(get_bloginfo("description")),"unicode")."</description>\n";
  851: 		$rss .= "\t\t<lastBuildDate>$date_now</lastBuildDate>\n";
  852: 		$rss .= "\t\t<docs>http://backend.userland.com/rss092</docs>\n";
  853: 		$rss .= "\t\t<managingEditor>$admin_email</managingEditor>\n";
  854: 		$rss .= "\t\t<webMaster>$admin_email</webMaster>\n";
  855: 		$rss .= "\t\t<language>$rss_language</language>\n";
  856: 		
  857: 		$now = date('Y-m-d H:i:s',(time() + ($time_difference * 3600)));
  858: 		$sql = "SELECT * FROM $tableposts WHERE post_date <= '$now' AND post_category > 0 ORDER BY post_date DESC LIMIT $num_posts";
  859: 		$result = mysql_query($sql) or die("Your SQL query: <br />$sql<br /><br />MySQL said:<br />".mysql_error());
  860: 
  861: 		while($row = mysql_fetch_object($result)) {
  862: 
  863: 			$id = $row->ID;
  864: 			$postdata=get_postdata2($id);
  865: 
  866: 			$rss .= "\t\t<item>\n";
  867: 			$rss .= "\t\t\t<title>".convert_chars(strip_tags(get_the_title()),"unicode")."</title>\n";
  868: 
  869: //		we could add some specific RSS here, but not yet. uncomment if you wish, it's functionnal
  870: //			$rss .= "\t\t\t<category>".convert_chars(strip_tags(get_the_category()),"unicode")."</category>\n";
  871: 
  872: 			$content = stripslashes($row->post_content);
  873: 			$content = explode("<!--more-->",$content);
  874: 			$content = $content[0];
  875: 			$rss .= "\t\t\t<description>".convert_chars(make_url_footnote($content),"unicode")."</description>\n";
  876: 
  877: 			$rss .= "\t\t\t<link>".htmlentities("$siteurl/$blogfilename".$querystring_start.'p'.$querystring_equal.$row->ID.$querystring_separator.'c'.$querystring_equal.'1')."</link>\n";
  878: 			$rss .= "\t\t</item>\n";
  879: 
  880: 		}
  881: 
  882: 		$rss .= "\t</channel>\n";
  883: 		$rss .= "</rss>";
  884: 
  885: 		$f=@fopen("$file","w+");
  886: 		if ($f) {
  887: 			@fwrite($f,$rss);
  888: 			@fclose($f);
  889: 
  890: 			return(true);
  891: 		} else {
  892: 			return(false);
  893: 		}
  894: 	} else {
  895: 		return(false);
  896: 	}
  897: }
  898: 
  899: function make_url_footnote($content) {
  900: 	global $siteurl;
  901: 	preg_match_all('/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches);
  902: 	$j = 0;
  903: 	for ($i=0; $i<count($matches[0]); $i++) {
  904: 		$links_summary = (!$j) ? "\n" : $links_summary;
  905: 		$j++;
  906: 		$link_match = $matches[0][$i];
  907: 		$link_number = '['.($i+1).']';
  908: 		$link_url = $matches[2][$i];
  909: 		$link_text = $matches[4][$i];
  910: 		$content = str_replace($link_match, $link_text.' '.$link_number, $content);
  911: 		$link_url = (strtolower(substr($link_url,0,7)) != 'http://') ? $siteurl.$link_url : $link_url;
  912: 		$links_summary .= "\n".$link_number.' '.$link_url;
  913: 	}
  914: 	$content = strip_tags($content);
  915: 	$content .= $links_summary;
  916: 	return($content);
  917: }
  918: 
  919: 
  920: function xmlrpc_getposttitle($content) {
  921: 	global $post_default_title;
  922: 	if (preg_match('/<title>(.+?)<\/title>/is', $content, $matchtitle)) {
  923: 		$post_title = $matchtitle[0];
  924: 		$post_title = preg_replace('/<title>/si', '', $post_title);
  925: 		$post_title = preg_replace('/<\/title>/si', '', $post_title);
  926: 	} else {
  927: 		$post_title = $post_default_title;
  928: 	}
  929: 	return($post_title);
  930: }
  931: 	
  932: function xmlrpc_getpostcategory($content) {
  933: 	global $post_default_category;
  934: 	if (preg_match('/<category>(.+?)<\/category>/is', $content, $matchcat)) {
  935: 		$post_category = $matchcat[0];
  936: 		$post_category = preg_replace('/<category>/si', '', $post_category);
  937: 		$post_category = preg_replace('/<\/category>/si', '', $post_category);
  938: 
  939: 	} else {
  940: 		$post_category = $post_default_category;
  941: 	}
  942: 	return($post_category);
  943: }
  944: 
  945: function xmlrpc_removepostdata($content) {
  946: 	$content = preg_replace('/<title>(.+?)<\/title>/si', '', $content);
  947: 	$content = preg_replace('/<category>(.+?)<\/category>/si', '', $content);
  948: 	$content = trim($content);
  949: 	return($content);
  950: }
  951: 
  952: function debug_fopen($filename, $mode) {
  953: 	global $debug;
  954: 	if ($debug == 1) {
  955: 		$fp = fopen($filename, $mode);
  956: 		return $fp;
  957: 	} else {
  958: 		return false;
  959: 	}
  960: }
  961: 
  962: function debug_fwrite($fp, $string) {
  963: 	global $debug;
  964: 	if ($debug == 1) {
  965: 		fwrite($fp, $string);
  966: 	}
  967: }
  968: 
  969: function debug_fclose($fp) {
  970: 	global $debug;
  971: 	if ($debug == 1) {
  972: 		fclose($fp);
  973: 	}
  974: }
  975: 
  976: function pingback($content, $post_ID) {
  977: 	// original code by Mort (http://mort.mine.nu:8080)
  978: 	global $siteurl, $blogfilename, $b2_version;
  979: 	$log = debug_fopen('./pingback.log', 'a');
  980: 	$post_links = array();
  981: 	debug_fwrite($log, 'BEGIN '.time()."\n");
  982: 
  983: 	// Variables
  984: 	$ltrs = '\w';
  985: 	$gunk = '/#~:.?+=&%@!\-';
  986: 	$punc = '.:?\-';
  987: 	$any = $ltrs.$gunk.$punc;
  988: 	$pingback_str_dquote = 'rel="pingback"';
  989: 	$pingback_str_squote = 'rel=\'pingback\'';
  990: 	$x_pingback_str = 'x-pingback: ';
  991: 	$pingback_href_original_pos = 27;
  992: 
  993: 	// Step 1
  994: 	// Parsing the post, external links (if any) are stored in the $post_links array
  995: 	// This regexp comes straigth from phpfreaks.com
  996: 	// http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
  997: 	preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
  998: 
  999: 	// Debug
 1000: 	debug_fwrite($log, 'Post contents:');
 1001: 	debug_fwrite($log, $content."\n");
 1002: 	
 1003: 	// Step 2.
 1004: 	// Walking thru the links array
 1005: 	// first we get rid of links pointing to sites, not to specific files
 1006: 	// Example:
 1007: 	// http://dummy-weblog.org
 1008: 	// http://dummy-weblog.org/
 1009: 	// http://dummy-weblog.org/post.php
 1010: 	// We don't wanna ping first and second types, even if they have a valid <link/>
 1011: 
 1012: 	foreach($post_links_temp[0] as $link_test){
 1013: 		$test = parse_url($link_test);
 1014: 		if (isset($test['query'])) {
 1015: 			$post_links[] = $link_test;
 1016: 		} elseif(($test['path'] != '/') && ($test['path'] != '')) {
 1017: 			$post_links[] = $link_test;
 1018: 		}
 1019: 	}
 1020: 
 1021: 	foreach ($post_links as $pagelinkedto){
 1022: 		debug_fwrite($log, 'Processing -- '.$pagelinkedto."\n\n");
 1023: 
 1024: 		$bits = parse_url($pagelinkedto);
 1025: 		if (!isset($bits['host'])) {
 1026: 			debug_fwrite($log, 'Couldn\'t find a hostname for '.$pagelinkedto."\n\n");
 1027: 			continue;
 1028: 		}
 1029: 		$host = $bits['host'];
 1030: 		$path = isset($bits['path']) ? $bits['path'] : '';
 1031: 		if (isset($bits['query'])) {
 1032: 			$path .= '?'.$bits['query'];
 1033: 		}
 1034: 		if (!$path) {
 1035: 			$path = '/';
 1036: 		}
 1037: 		$port = isset($bits['port']) ? $bits['port'] : 80;
 1038: 
 1039: 		// Try to connect to the server at $host
 1040: 		$fp = fsockopen($host, $port, $errno, $errstr, 30);
 1041: 		if (!$fp) {
 1042: 			debug_fwrite($log, 'Couldn\'t open a connection to '.$host."\n\n");
 1043: 			continue;
 1044: 		}
 1045: 
 1046: 		// Send the GET request
 1047: 		$request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: b2/$b2_version PHP/" . phpversion() . "\r\n\r\n";
 1048: 		ob_end_flush();
 1049: 		fputs($fp, $request);
 1050: 
 1051: 		// Start receiving headers and content
 1052: 		$contents = '';
 1053: 		$headers = '';
 1054: 		$gettingHeaders = true;
 1055: 		$found_pingback_server = 0;
 1056: 		while (!feof($fp)) {
 1057: 			$line = fgets($fp, 4096);
 1058: 			if (trim($line) == '') {
 1059: 				$gettingHeaders = false;
 1060: 			}
 1061: 			if (!$gettingHeaders) {
 1062: 				$contents .= trim($line)."\n";
 1063: 				$pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
 1064: 				$pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
 1065: 			} else {
 1066: 				$headers .= trim($line)."\n";
 1067: 				$x_pingback_header_offset = strpos(strtolower($headers), $x_pingback_str);
 1068: 			}
 1069: 			if ($x_pingback_header_offset) {
 1070: 				preg_match('#x-pingback: (.+)#is', $headers, $matches);
 1071: 				$pingback_server_url = trim($matches[1]);
 1072: 				debug_fwrite($log, "Pingback server found from X-Pingback header @ $pingback_server_url\n");
 1073: 				$found_pingback_server = 1;
 1074: 				break;
 1075: 			}
 1076: 			if ($pingback_link_offset_dquote || $pingback_link_offset_squote) {
 1077: 				$quote = ($pingback_link_offset_dquote) ? '"' : '\'';
 1078: 				$pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
 1079: 				$pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
 1080: 				$pingback_href_start = $pingback_href_pos+6;
 1081: 				$pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
 1082: 				$pingback_server_url_len = $pingback_href_end-$pingback_href_start;
 1083: 				$pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
 1084: 				debug_fwrite($log, "Pingback server found from Pingback <link /> tag @ $pingback_server_url\n");
 1085: 				$found_pingback_server = 1;
 1086: 				break;
 1087: 			}
 1088: 		}
 1089: 
 1090: 		if (!$found_pingback_server) {
 1091: 			debug_fwrite($log, "Pingback server not found\n\n*************************\n\n");
 1092: 			@fclose($fp);
 1093: 		} else {
 1094: 			debug_fwrite($log,"\n\nPingback server data\n");
 1095: 
 1096: 			// Assuming there's a "http://" bit, let's get rid of it
 1097: 			$host_clear = substr($pingback_server_url, 7);
 1098: 
 1099: 			//  the trailing slash marks the end of the server name
 1100: 			$host_end = strpos($host_clear, '/');
 1101: 
 1102: 			// Another clear cut
 1103: 			$host_len = $host_end-$host_start;
 1104: 			$host = substr($host_clear, 0, $host_len);
 1105: 			debug_fwrite($log, 'host: '.$host."\n");
 1106: 
 1107: 			// If we got the server name right, the rest of the string is the server path
 1108: 			$path = substr($host_clear,$host_end);
 1109: 			debug_fwrite($log, 'path: '.$path."\n\n");
 1110: 
 1111: 			 // Now, the RPC call
 1112: 			$method = 'pingback.ping';
 1113: 			debug_fwrite($log, 'Page Linked To: '.$pagelinkedto."\n");
 1114: 			debug_fwrite($log, 'Page Linked From: ');
 1115: 			$pagelinkedfrom = $siteurl.'/'.$blogfilename.'?p='.$post_ID.'&c=1';
 1116: 			debug_fwrite($log, $pagelinkedfrom."\n");
 1117: 
 1118: 			$client = new xmlrpc_client($path, $host, 80);
 1119: 			$message = new xmlrpcmsg($method, array(new xmlrpcval($pagelinkedfrom), new xmlrpcval($pagelinkedto)));
 1120: 			$result = $client->send($message);
 1121: 			if ($result){
 1122: 				if (!$result->value()){
 1123: 					debug_fwrite($log, $result->faultCode().' -- '.$result->faultString());
 1124: 				} else {
 1125: 					$value = xmlrpc_decode($result->value());
 1126: 					if (is_array($value)) {
 1127: 						$value_arr = '';
 1128: 						foreach($value as $blah) {
 1129: 							$value_arr .= $blah.' |||| ';
 1130: 						}
 1131: 						debug_fwrite($log, $value_arr);
 1132: 					} else {
 1133: 						debug_fwrite($log, $value);
 1134: 					}
 1135: 				}
 1136: 			}
 1137: 			@fclose($fp);
 1138: 		}
 1139: 	}
 1140: 
 1141: 	debug_fwrite($log, "\nEND: ".time()."\n****************************\n\r");
 1142: 	debug_fclose($log);
 1143: }
 1144: 
 1145: 
 1146: /*
 1147:  balanceTags
 1148:  
 1149:  Balances Tags of string using a modified stack.
 1150:  
 1151:  @param text      Text to be balanced
 1152:  @return          Returns balanced text
 1153:  @author          Leonard Lin (leonard@acm.org)
 1154:  @version         v1.1
 1155:  @date            November 4, 2001
 1156:  @license         GPL v2.0
 1157:  @notes           
 1158:  @changelog       
 1159:              1.2  ***TODO*** Make better - change loop condition to $text
 1160:              1.1  Fixed handling of append/stack pop order of end text
 1161:                   Added Cleaning Hooks
 1162:              1.0  First Version
 1163: */
 1164: 
 1165: function balanceTags($text, $is_comment = 0) {
 1166: 	global $use_balanceTags;
 1167: 	if ($use_balanceTags == 0) {
 1168: 		return($text);
 1169: 	}
 1170: 
 1171: 	$tagstack = array();
 1172: 	$stacksize = 0;
 1173: 	$tagqueue = '';
 1174: 	$newtext = '';
 1175: 
 1176: 	# b2 bug fix for comments - in case you REALLY meant to type '< !--'
 1177: 	$text = str_replace('< !--', '<    !--', $text);
 1178: 
 1179: 	# b2 bug fix for LOVE <3 (and other situations with '<' before a number)
 1180: 	$text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
 1181: 
 1182: 
 1183: 	while (preg_match("/<(\/?\w*)\s*([^>]*)>/",$text,$regex)) {
 1184: 		$newtext = $newtext . $tagqueue;
 1185: 
 1186: 		$i = strpos($text,$regex[0]);
 1187: 		$l = strlen($tagqueue) + strlen($regex[0]);
 1188: 
 1189: 		// clear the shifter
 1190: 		$tagqueue = '';
 1191: 
 1192: 		// Pop or Push
 1193: 		if ($regex[1][0] == "/") { // End Tag
 1194: 			$tag = strtolower(substr($regex[1],1));
 1195: 
 1196: 			// if too many closing tags
 1197: 			if($stacksize <= 0) { 
 1198: 				$tag = '';
 1199: 				//or close to be safe $tag = '/' . $tag;
 1200: 			}
 1201: 			// if stacktop value = tag close value then pop
 1202: 			else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag
 1203: 				$tag = '</' . $tag . '>'; // Close Tag
 1204: 				// Pop
 1205: 				array_pop ($tagstack);
 1206: 				$stacksize--;
 1207: 			} else { // closing tag not at top, search for it
 1208: 				for ($j=$stacksize-1;$j>=0;$j--) {
 1209: 					if ($tagstack[$j] == $tag) {
 1210: 					// add tag to tagqueue
 1211: 						for ($k=$stacksize-1;$k>=$j;$k--){
 1212: 							$tagqueue .= '</' . array_pop ($tagstack) . '>';
 1213: 							$stacksize--;
 1214: 						}
 1215: 						break;
 1216: 					}
 1217: 				}
 1218: 				$tag = '';
 1219: 			}
 1220: 		} else { // Begin Tag
 1221: 			$tag = strtolower($regex[1]);
 1222: 
 1223: 			// Tag Cleaning
 1224: 
 1225: 			// Push if not img or br or hr
 1226: 			if($tag != 'br' && $tag != 'img' && $tag != 'hr') {
 1227: 				$stacksize = array_push ($tagstack, $tag);
 1228: 			}
 1229: 
 1230: 			// Attributes
 1231: 			// $attributes = $regex[2];
 1232: 			$attributes = $regex[2];
 1233: 			if($attributes) {
 1234: 				// fix to avoid CSS defacements
 1235: 				if ($is_comment) {
 1236: 					$attributes = str_replace('style=', 'title=', $attributes);
 1237: 					$attributes = str_replace('class=', 'title=', $attributes);
 1238: 					$attributes = str_replace('id=', 'title=', $attributes);
 1239: 				}
 1240: 				$attributes = ' '.$attributes;
 1241: 			}
 1242: 
 1243: 			$tag = '<'.$tag.$attributes.'>';
 1244: 		}
 1245: 
 1246: 		$newtext .= substr($text,0,$i) . $tag;
 1247: 		$text = substr($text,$i+$l);
 1248: 	}  
 1249: 
 1250: 	// Clear Tag Queue
 1251: 	$newtext = $newtext . $tagqueue;
 1252: 
 1253: 	// Add Remaining text
 1254: 	$newtext .= $text;
 1255: 
 1256: 	// Empty Stack
 1257: 	while($x = array_pop($tagstack)) {
 1258: 		$newtext = $newtext . '</' . $x . '>'; // Add remaining tags to close      
 1259: 	}
 1260: 
 1261: 	# b2 fix for the bug with HTML comments
 1262: 	$newtext = str_replace("< !--","<!--",$newtext);
 1263: 	$newtext = str_replace("<    !--","< !--",$newtext);
 1264: 
 1265: 	return $newtext;
 1266: }
 1267: 
 1268: require_once(dirname(__FILE__).'/lj_update.php');
 1269: ?>

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>