' . mysql_errno() . ': ' . mysql_error() . '
'; } //------------------------------------------------------------------------------------------------------ // Connect with MySQL server and select database // Arguments: server name, user name, user password, database name // function db_open($db_server, $db_user, $db_pass, $db_name) { $status = true; if(!@mysql_connect($db_server, $db_user, $db_pass)) { sql_error(); $status = false; } else if(!@mysql_select_db($db_name)) { sql_error(); $status = false; } return $status; } //------------------------------------------------------------------------------------------------------ // Convert timestamp to YYYY-MM-DD HH:MM:SS // Arguments: timestamp (YYYYMMDDHHMMSS) // function timeStamp($ts) { return substr($ts, 0, 4) . '-' . substr($ts, 4, 2) . '-' . substr($ts, 6, 2) . ' ' . substr($ts, 8, 2) . ':' . substr($ts, 10, 2) . ':' . substr($ts, 12); } //------------------------------------------------------------------------------------------------------ // Cut text to a specified length // Arguments: text, length // function cutString($str, $length) { if(strlen($str) > $length) { $words = explode(' ', $str); $wCnt = count($words); if($wCnt == 1) { $str = substr($str, 0, $length) . '...'; } else { $str = ''; $cnt = 0; while($cnt < $wCnt && strlen($str) < $length) { $str .= trim($words[$cnt++]) . ' '; } if($cnt < $wCnt) $str .= '...'; } } return $str; } //------------------------------------------------------------------------------------------------------ // Search text between start-tag and end-tag // Arguments: text, start-tag, end-tag // function searchCode($str, $pStart, $pEnd) { $matches = array(); $a = strlen($pStart); $b = strlen($pEnd); do { $y = 0; if(strlen($str) > strlen($pStart) + strlen($pEnd)) { $s = strtolower($str); $x = strpos($s, $pStart); $y = strpos($s, $pEnd, $x + $a); $z = strpos($s, $pStart, $x + $a); if($x >= 0 && $y) { while($z && $z < $y) { $y = strpos($s, $pEnd, $y + $b); $z = strpos($s, $pStart, $z + $a); } $y += $b; $matches[] = substr($str, $x, $y - $x); $str = substr($str, $y); } } } while($x >= 0 && $y > 0); return $matches; } //------------------------------------------------------------------------------------------------------ // Convert text between start-tag and end-tag to base64 // Arguments: text, start-tag, end-tag // function encodeString($str, $pStart, $pEnd) { $matches = searchCode($str, $pStart, $pEnd); for($i = 0; $i < count($matches); $i++) { $str = str_replace($matches[$i], chr(1) . base64_encode($matches[$i]) . chr(2), $str); } return $str; } //------------------------------------------------------------------------------------------------------ // Decode base64-text // Arguments: text // function decodeString($str) { if(preg_match_all('/' . chr(1) . '([^' . chr(2) . ']+)' . chr(2) . '/', $str, $m)) { for($i = 0; $i < count($m[0]); $i++) { $str = str_replace($m[0][$i], base64_decode($m[1][$i]), $str); } } return $str; } //------------------------------------------------------------------------------------------------------ // Delete repeated characters (more than 3 times) // Arguments: text // function checkRepeats($str) { $newstr = substr($str, 0, 3); for($i = 3; $i < strlen($str); $i++) { if($str[$i] != $str[$i-1] || $str[$i] != $str[$i-2] || $str[$i] != $str[$i-3]) $newstr .= $str[$i]; } return $newstr; } //------------------------------------------------------------------------------------------------------ // Replace long words with image // Arguments: text, max. word length // function checkLongWords($str, $wordLength) { global $imgPath; if($wordLength && strlen($str) > $wordLength) { $html = array(); if(preg_match_all('/<[a-z\/][^>]+>/i', $str, $m)) { for($i = 0; $i < count($m[0]); $i++) { $html[$i] = $m[0][$i]; $str = str_replace($m[0][$i], " &HTML$i; ", $str); } } $str = str_replace("\r\n", "\n", $str); $str = str_replace("\r", "\n", $str); $lines = explode("\n", $str); $str = ''; for($i = 0; $i < count($lines); $i++) { $words = explode(' ', $lines[$i]); for($j = 0; $j < count($words); $j++) { $word = function_exists('html_entity_decode') ? html_entity_decode($words[$j]) : $words[$j]; if(strlen($word) > $wordLength && !preg_match('/&#\d{1,6};/', $words[$j])) { if(preg_match('%^(ftp|https?)://%i', $word)) { $str .= "" . substr($word, 0, $wordLength) . '...' . ''; } else $str .= ''; } else $str .= $words[$j] . ' '; } if($i < count($lines) - 1) $str .= "\n"; } if(preg_match_all('/ &HTML(\d+); /', $str, $m)) { for($i = 0; $i < count($m[0]); $i++) { $str = str_replace($m[0][$i], $html[$m[1][$i]], $str); } } } return $str; } //------------------------------------------------------------------------------------------------------ // Add image size and replace oversized images with thumbnail and invalid images with icon // Arguments: text, max. image width // function checkImages($str, $maxWidth) { global $imgPath; if(preg_match_all('/ src="([^">]+)"/i', $str, $m)) { for($i = 0; $i < count($m[0]); $i++) { list($width, $height, $type) = @getimagesize($m[1][$i]); if(!$width || $type < 1 || $type > 3) { $width = 17; $height = 14; $img = $imgPath . 'noimage.gif'; } else if($width > $maxWidth) { $perc = $maxWidth / $width; $width = round($width * $perc); $height = round($height * $perc); $img = $imgPath . "thumbnail.php?width=$width&height=$height&file=" . urlencode($m[1][$i]); } else $img = $m[1][$i]; if($img != $imgPath . 'nourl.gif' && $img != $imgPath . 'nohtml.gif') { $size = 'width="' . $width . '" height="' . $height . '"'; $str = str_replace($m[0][$i], ' src="' . $img . '" ' . $size, $str); } } } return $str; } //------------------------------------------------------------------------------------------------------ // Check if text contains HTML links or URLs // Arguments: text, replace text (optional) // function checkLinks($str, $replace = '') { $link1 = '.*?<\/a>'; $link2 = '