function format_file_size($file_size = 0) {
//$file_size = intval($file_size);
if (!$file_size) {
return "n/a";
}
if (strlen($file_size) <= 9 && strlen($file_size) >= 7) {
$file_size = number_format($file_size / 1048576,1);
return $file_size." MB";
}
elseif (strlen($file_size) >= 10) {
$file_size = number_format($file_size / 1073741824,1);
return $file_size." GB";
}
else {
$file_size = number_format($file_size / 1024,1);
return $file_size." KB";
}
}
function get_remote_file_size($file_path) {
if (!CHECK_REMOTE_FILES) {
return 'n/a';
}
ob_start();
@readfile($file_path);
$file_data = ob_get_contents();
ob_end_clean();
return format_file_size(strlen($file_data));
}
function update_comment_count($image_id = 0, $user_id = 0) {
global $site_db, $user_table_fields;
if ($image_id) {
$sql = "SELECT COUNT(comment_id) AS comments
FROM ".COMMENTS_TABLE."
WHERE image_id = $image_id";
$countcomments = $site_db->query_firstrow($sql);
$sql = "UPDATE ".IMAGES_TABLE."
SET image_comments = ".$countcomments['comments']."
WHERE image_id = $image_id";
$site_db->query($sql);
}
if ($user_id != GUEST && $user_id && !empty($user_table_fields['user_comments'])) {
$sql = "SELECT COUNT(comment_id) AS comments
FROM ".COMMENTS_TABLE."
WHERE user_id = $user_id";
$countcomments = $site_db->query_firstrow($sql);
$sql = "UPDATE ".USERS_TABLE."
SET ".get_user_table_field("", "user_comments")." = ".$countcomments['comments']."
WHERE ".get_user_table_field("", "user_id")." = $user_id";
$site_db->query($sql);
}
}
function update_image_rating($image_id, $rating) {
global $site_db;
$sql = "SELECT cat_id, image_votes, image_rating
FROM ".IMAGES_TABLE."
WHERE image_id = $image_id";
$image_row = $site_db->query_firstrow($sql);
if (check_permission("auth_vote", $image_row['cat_id'])) {
$old_votes = $image_row['image_votes'];
$old_rating = $image_row['image_rating'];
$new_rating = (($old_rating * $old_votes) + $rating) / ($old_votes + 1);
$new_rating = sprintf("%.2f", $new_rating);
$sql = "UPDATE ".IMAGES_TABLE."
SET image_votes = ($old_votes + 1), image_rating = '$new_rating'
WHERE image_id = $image_id";
$site_db->query($sql);
}
}
function check_email($email) {
return (preg_match('/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+\.)+([0-9A-Z]){2,}$/i', $email)) ? 1 : 0;
}
function format_date($format, $timestamp) {
global $user_info;
$timezone_offset = (defined("TIME_OFFSET")) ? TIME_OFFSET : 0;
return date($format, $timestamp + (3600 * $timezone_offset));
}
function format_url($url) {
if (empty($url)) {
return '';
}
if (!preg_match("/^https?:\/\//i", $url)) {
$url = "http://".$url;
}
return htmlspecialchars($url);
}
function replace_url($text) {
$text = " ".$text." ";
$url_search_array = array(
"#([^]_a-z0-9-=\"'\/])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^, \(\)<>\n\r]*)?)#si",
"#([^]_a-z0-9-=\"'\/])([a-z]+?)://([^, \(\)<>\n\r]+)#si"
);
$url_replace_array = array(
"\\1www.\\2.\\3\\4",
"\\1\\2://\\3",
);
$text = preg_replace($url_search_array, $url_replace_array, $text);
if (strpos($text, "@")) {
$text = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)?[\w]+)#i", "\\1\\2@\\3", $text);
}
return substr($text, 1, -1);
}
function replace_badwords($text) {
global $config, $split_badwords;
if ($config['badword_list'] != "") {
if (!isset($split_badwords)) {
$badwords = trim($config['badword_list']);
$badwords = preg_replace("/[\n\r]/is", " ", $badwords);
$badwords = str_replace(","," ",$badwords);
$badwords = preg_quote($badwords);
$badwords = str_replace('/', '\\/', $badwords);
$split_badwords = preg_split("/\s+/", $badwords);
}
foreach ($split_badwords as $key => $val) {
if ($val != "") {
if (substr($val, 0, 2) == "\\{") {
$val = substr($val, 2, -2);
$text = trim(preg_replace("/([^A-Za-z])".$val."(?=[^A-Za-z])/si", "\\1".str_repeat($config['badword_replace_char'], strlen($val)), " $text "));
}
else {
$text = trim(preg_replace("/$val/si", str_repeat($config['badword_replace_char'], strlen($val)), " $text "));
}
}
}
}
return $text;
}
function format_text($text, $html = 0, $word_wrap = 0, $bbcode = 0, $bbcode_img = 0) {
if ($word_wrap && $text != "") {
$text = preg_replace("/([^\n\r ?&\.\/<>\"\\-]{".$word_wrap."})/i", " \\1\n", $text);
}
if ($html == 0 || $html == 2) {
$text = safe_htmlspecialchars($text);
}
// Replace { to prevent parsing in templates
global $site_template;
$text = preg_replace(
'='.preg_quote($site_template->start).'([A-Z0-9_]+)'.preg_quote($site_template->end).'=Usi',
'{\1}',
$text
);
if ($html !== 2) {
$text = nl2br(trim($text));
$text = replace_url($text);
}
if ($bbcode == 1) {
$search_array = array(
"/(\[)(list)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/list)(((=)(\\4)([^\"']*)(\\4]))|(\]))/siU",
"/(\[)(list)(])(.*)(\[\/list\])/siU",
"/(\[\*\])/siU",
"/(\[\/\*\])/siU",
"/(\[)(url)(=)(['\"]?)(www\.)([^\"']*)(\\4])(.*)(\[\/url\])/siU",
"/(\[)(url)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/url\])/siU",
"/(\[)(url)(])(www\.)([^\"]*)(\[\/url\])/siU",
"/(\[)(url)(])([^\"]*)(\[\/url\])/siU",
"/(\[)(code)(])(\r\n)*(.*)(\[\/code\])/siU",
"/javascript:/si",
"/about:/si"
);
$replace_array = array(
"
\\7
",
"",
"",
"",
"\\8",
"\\7",
"www.\\5",
"\\4",
"Code:
\\5
",
"java script:",
"about :"
);
$text = preg_replace($search_array, $replace_array, $text);
if (!$bbcode_img) {
$text = preg_replace("/(\[)(img)(])(\r\n)*([^\"]*)(\[\/img\])/siU", "\\5", $text);
}
else {
$text = preg_replace("/(\[)(img)(])(\r\n)*([^\"]*)(\[\/img\])/siU", "", $text);
}
$text = preg_replace("/(\[)(b)(])(\r\n)*([^\"]*)(\[\/b\])/siU", "\\5", $text);
$text = preg_replace("/(\[)(i)(])(\r\n)*([^\"]*)(\[\/i\])/siU", "\\5", $text);
$text = preg_replace("/(\[)(u)(])(\r\n)*([^\"]*)(\[\/u\])/siU", "\\5", $text);
$text = replace_badwords($text);
}
$text = str_replace("\\'", "'", $text);
return $text;
}
function utf8_to_htmlentities($source) {
// array used to figure what number to decrement from character order
// value
// according to number of characters used to map unicode to ascii by
// utf-8
$decrement = array();
$decrement[4] = 240;
$decrement[3] = 224;
$decrement[2] = 192;
$decrement[1] = 0;
// the number of bits to shift each charNum by
$shift = array();
$shift[1][0] = 0;
$shift[2][0] = 6;
$shift[2][1] = 0;
$shift[3][0] = 12;
$shift[3][1] = 6;
$shift[3][2] = 0;
$shift[4][0] = 18;
$shift[4][1] = 12;
$shift[4][2] = 6;
$shift[4][3] = 0;
$pos = 0;
$len = strlen($source);
$str = '';
while ($pos < $len) {
$asciiPos = ord(substr($source, $pos, 1));
if (($asciiPos >= 240) && ($asciiPos <= 255)) {
// 4 chars representing one unicode character
$thisLetter = substr($source, $pos, 4);
$pos += 4;
}
elseif (($asciiPos >= 224) && ($asciiPos <= 239)) {
// 3 chars representing one unicode character
$thisLetter = substr($source, $pos, 3);
$pos += 3;
}
else if (($asciiPos >= 192) && ($asciiPos <= 223)) {
// 2 chars representing one unicode character
$thisLetter = substr($source, $pos, 2);
$pos += 2;
}
else {
// 1 char (lower ascii)
$thisLetter = substr($source, $pos, 1);
$pos += 1;
}
// process the string representing the letter to a unicode entity
$thisLen = strlen($thisLetter);
$thisPos = 0;
$decimalCode = 0;
while ($thisPos < $thisLen) {
$thisCharOrd = ord(substr($thisLetter, $thisPos, 1));
if ($thisPos == 0) {
$charNum = intval($thisCharOrd - $decrement[$thisLen]);
$decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
} else {
$charNum = intval($thisCharOrd - 128);
$decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
}
$thisPos++;
}
if (($thisLen == 1) && ($decimalCode <= 128)) {
$encodedLetter = $thisLetter;
}
else {
$encodedLetter = '' . $decimalCode . ';';
}
$str .= $encodedLetter;
}
return $str;
}
function uni_to_utf8($char) {
$char = intval($char);
switch ($char) {
case ($char < 128) :
// its an ASCII char no encoding needed
return chr($char);
case ($char < 1 << 11) :
// its a 2 byte UTF-8 char
return chr(192 + ($char >> 6)) .
chr(128 + ($char & 63));
case ($char < 1 << 16) :
// its a 3 byte UTF-8 char
return chr(224 + ($char >> 12)) .
chr(128 + (($char >> 6) & 63)) .
chr(128 + ($char & 63));
case ($char < 1 << 21) :
// its a 4 byte UTF-8 char
return chr(240 + ($char >> 18)) .
chr(128 + (($char >> 12) & 63)) .
chr(128 + (($char >> 6) & 63)) .
chr(128 + ($char & 63));
case ($char < 1 << 26) :
// its a 5 byte UTF-8 char
return chr(248 + ($char >> 24)) .
chr(128 + (($char >> 18) & 63)) .
chr(128 + (($char >> 12) & 63)) .
chr(128 + (($char >> 6) & 63)) .
chr(128 + ($char & 63));
default:
// its a 6 byte UTF-8 char
return chr(252 + ($char >> 30)) .
chr(128 + (($char >> 24) & 63)) .
chr(128 + (($char >> 18) & 63)) .
chr(128 + (($char >> 12) & 63)) .
chr(128 + (($char >> 6) & 63)) .
chr(128 + ($char & 63));
}
}
function get_user_info($user_id = 0) {
global $site_db, $user_table_fields;
$user_info = 0;
if ($user_id != 0 && $user_id != GUEST) {
$sql = "SELECT *
FROM ".USERS_TABLE."
WHERE ".get_user_table_field("", "user_id")." = $user_id";
if ($user_info = $site_db->query_firstrow($sql)) {
foreach ($user_table_fields as $key => $val) {
if (isset($user_info[$val])) {
$user_info[$key] = $user_info[$val];
}
elseif (!isset($user_info[$key])) {
$user_info[$key] = "";
}
}
}
}
return $user_info;
}
function get_icq_status($uin) {
if (!is_numeric($uin)) return false;
$fp = @fsockopen('status.icq.com', 80, $errno, $errstr, 8);
if (!$fp) return false;
$request = "HEAD /online.gif?icq=$uin&img=3 HTTP/1.0\r\n"
."Host: status.icq.com\r\n"
."Connection: close\r\n\r\n";
fputs($fp, $request);
do {
$response = fgets($fp, 1024);
}
while (!feof($fp) && !stristr($response, 'Location'));
fclose($fp);
if (strstr($response, 'online1')) return 'online';
if (strstr($response, 'online0')) return 'offline';
if (strstr($response, 'online2')) return 'disabled';
return FALSE;
}
function add_to_lightbox($id) {
global $user_info, $site_db;
$id = intval($id);
if (!$id) {
return false;
}
$lightbox_ids = $user_info['lightbox_image_ids'];
$lightbox_array = explode(" ", $lightbox_ids);
if (!in_array($id, $lightbox_array)) {
$lightbox_ids .= " ".$id;
}
$user_info['lightbox_image_ids'] = trim($lightbox_ids);
$user_info['lightbox_lastaction'] = time();
$sql = "UPDATE ".LIGHTBOXES_TABLE."
SET lightbox_lastaction = ".$user_info['lightbox_lastaction'].", lightbox_image_ids = '".$user_info['lightbox_image_ids']."'
WHERE user_id = ".$user_info['user_id'];
return ($site_db->query($sql)) ? 1 : 0;
}
function remove_from_lightbox($id) {
global $user_info, $site_db;
$lightbox_array = explode(" ",$user_info['lightbox_image_ids']);
foreach ($lightbox_array as $key => $val) {
if ($val == $id) {
unset($lightbox_array[$key]);
}
}
$user_info['lightbox_image_ids'] = trim(implode(" ", $lightbox_array));
$user_info['lightbox_lastaction'] = time();
$sql = "UPDATE ".LIGHTBOXES_TABLE."
SET lightbox_lastaction = ".$user_info['lightbox_lastaction'].", lightbox_image_ids = '".$user_info['lightbox_image_ids']."'
WHERE user_id = ".$user_info['user_id'];
return ($site_db->query($sql)) ? 1 : 0;
}
function clear_lightbox() {
global $user_info, $site_db;
$current_time = time();
$sql = "UPDATE ".LIGHTBOXES_TABLE."
SET lightbox_image_ids = '', lightbox_lastaction = $current_time
WHERE user_id = ".$user_info['user_id'];
if ($site_db->query($sql)) {
$user_info['lightbox_image_ids'] = "";
$user_info['lightbox_lastaction'] = $current_time;
return true;
}
else {
return false;
}
}
function check_lightbox($id) {
global $user_info;
$lightbox_array = explode(" ", $user_info['lightbox_image_ids']);
return in_array($id, $lightbox_array);
}
function get_random_key($db_table = "", $db_column = "") {
global $site_db;
$key = md5(uniqid(microtime()));
if ($db_table != "" && $db_column != "") {
$i = 0;
while ($i == 0) {
$sql = "SELECT ".$db_column."
FROM ".$db_table."
WHERE ".$db_column." = '$key'";
if ($site_db->is_empty($sql)) {
$i = 1;
}
else {
$i = 0;
$key = md5(uniqid(microtime()));
}
}
}
return $key;
}
function get_subcat_ids($cid = 0, $cat_id = 0, $cat_parent_cache) {
global $subcat_ids;
if (!isset($cat_parent_cache[$cid])) {
return false;
}
foreach ($cat_parent_cache[$cid] as $key => $val) {
if (check_permission("auth_viewcat", $val)) {
$subcat_ids[$cat_id][] = $val;
get_subcat_ids($val, $cat_id, $cat_parent_cache);
}
}
return $subcat_ids;
}
function get_subcategories($parent_id) {
global $cat_parent_cache, $cat_cache, $site_sess, $config;
if (!isset($cat_parent_cache[$parent_id]) || $config['num_subcats'] < 1) {
return "";
}
$visible_cat_cache = array();
foreach ($cat_parent_cache[$parent_id] as $key => $val) {
if (check_permission("auth_viewcat", $val)) {
$visible_cat_cache[$key] = $val;
}
}
$num_subs = sizeof($visible_cat_cache);
$sub_cat_list = "";
$i = 1;
foreach ($visible_cat_cache as $subcat_id) {
if ($i <= $num_subs && $i <= $config['num_subcats']) {
$sub_url = $site_sess->url(ROOT_PATH."categories.php?".URL_CAT_ID."=".$subcat_id);
$sub_cat_list .= "".format_text($cat_cache[$subcat_id]['cat_name'], 2)."";
if ($i != $config['num_subcats'] && $i < $config['num_subcats'] && $i < $num_subs) {
$sub_cat_list .= ", ";
}
if ($i == $config['num_subcats'] && $i < $num_subs) {
$sub_cat_list .= " ...\n";
}
}
$i++;
}
return $sub_cat_list;
}
function get_categories($cat_id = 0) {
global $site_template, $site_db, $site_sess, $config, $lang;
global $cat_cache, $cat_parent_cache, $new_image_cache, $subcat_ids;
$cattable_width = ceil((intval($config['cat_table_width'])) / $config['cat_cells']);
if ((substr($config['cat_table_width'],-1)) == "%") {
$cattable_width .= "%";
}
if (!isset($cat_parent_cache[$cat_id])) {
return "";
}
$visible_cat_cache = array();
foreach ($cat_parent_cache[$cat_id] as $key => $val) {
if (check_permission("auth_viewcat", $val)) {
$visible_cat_cache[$key] = $val;
}
}
if (empty($visible_cat_cache)) {
return "";
}
$total = sizeof($visible_cat_cache);
$table_columns = (intval($config['cat_cells'])) ? intval($config['cat_cells']) : 2;
if ($total <= $table_columns) {
$table_rows = 1;
}
else {
$table_rows = $total / $table_columns;
if ($total >= $table_columns && !is_integer($table_rows)) {
$table_rows = intval($table_rows) + 1;
}
}
$categories = "\n\n\n\n";
$categories .= "\n";
$count = 0;
$count2 = 0;
foreach ($visible_cat_cache as $key => $category_id) {
$categories .= "\n\n";
$is_new = (isset($new_image_cache[$category_id]) && $new_image_cache[$category_id] > 0) ? 1 : 0;
$num_images = (isset($cat_cache[$category_id]['num_images'])) ? $cat_cache[$category_id]['num_images'] : 0;
$subcat_ids = array();
get_subcat_ids($category_id, $category_id, $cat_parent_cache);
if (isset($subcat_ids[$category_id])) {
foreach ($subcat_ids[$category_id] as $val) {
if (isset($new_image_cache[$val]) && $new_image_cache[$val] > 0) {
$is_new = 1;
}
if (isset($cat_cache[$val]['num_images'])) {
$num_images += $cat_cache[$val]['num_images'];
}
}
}
if (defined("SHOW_RANDOM_IMAGE") && SHOW_RANDOM_IMAGE == 0 || defined("SHOW_RANDOM_CAT_IMAGE") && SHOW_RANDOM_CAT_IMAGE == 0) {
$random_cat_image_file = "";
}
else {
$random_cat_image_file = get_random_image($category_id, 0, 1);
}
$site_template->register_vars(array(
"cat_id" => $category_id,
"cat_name" => format_text($cat_cache[$category_id]['cat_name'], 2),
"cat_description" => htmlspecialchars(format_text($cat_cache[$category_id]['cat_description'], 1)),
"cat_hits" => $cat_cache[$category_id]['cat_hits'],
"cat_is_new" => $is_new,
"lang_new" => $lang['new'],
"sub_cats" => get_subcategories($category_id),
"cat_url" => $site_sess->url(ROOT_PATH."categories.php?".URL_CAT_ID."=".$category_id),
"random_cat_image_file" => $random_cat_image_file,
"num_images" => $num_images
));
$categories .= $site_template->parse_template("category_bit");
$count++;
$count2++;
$categories .= " | \n \n";
if ($count == $table_rows && $count2 < sizeof($visible_cat_cache)) {
$categories .= " | \n";
$categories .= "\n";
$categories .= "\n";
$total = $total - $count2;
$table_columns = $table_columns - 1;
/*if ($total <= $table_columns && $table_columns > 1) {
$table_rows = 1;
}
else {
$table_rows = $total / $table_columns;
if ($total >= $table_columns && !is_integer($table_rows)) {
$table_rows = intval($table_rows) + 1;
}
}*/
$count = 0;
}
}
$categories .= " \n | \n
\n
\n";
return $categories;
}
function get_category_path($cat_id = 0, $detail_path = 0) {
global $site_sess, $config, $cat_cache, $url;
$parent_id = 1;
while ($parent_id) {
if (!isset($cat_cache[$cat_id]['cat_parent_id'])) {
return false;
}
$parent_id = $cat_cache[$cat_id]['cat_parent_id'];
if (empty($path)) {
if ($detail_path) {
$cat_url = ROOT_PATH."categories.php?".URL_CAT_ID."=".$cat_id;
if (preg_match("/".URL_PAGE."=([0-9]+)/", $url, $regs)) {
if (!empty($regs[1]) && $regs[1] != 1) {
$cat_url .= "&".URL_PAGE."=".$regs[1];
}
}
$path = "url($cat_url)."\" class=\"clickstream\">".format_text($cat_cache[$cat_id]['cat_name'], 2)."";
}
else {
$path = format_text($cat_cache[$cat_id]['cat_name'], 2);
}
}
else {
$path = "url(ROOT_PATH."categories.php?".URL_CAT_ID."=".$cat_id)."\" class=\"clickstream\">".format_text($cat_cache[$cat_id]['cat_name'], 2)."".$config['category_separator'].$path;
}
$cat_id = $parent_id;
} // end while
return $path;
}
function get_category_dropdown_bits($cat_id, $cid = 0, $depth = 1) {
global $site_db, $drop_down_cat_cache, $cat_cache;
if (!isset($drop_down_cat_cache[$cid])) {
return "";
}
$category_list = "";
foreach ($drop_down_cat_cache[$cid] as $key => $category_id) {
if (check_permission("auth_viewcat", $category_id)) {
$category_list .= "\n";
}
else {
$category_list .= ">".format_text($cat_cache[$category_id]['cat_name'], 2)."\n";
}
$category_list .= get_category_dropdown_bits($cat_id, $category_id, $depth + 1);
}
}
unset($drop_down_cat_cache[$cid]);
return $category_list;
}
function get_category_dropdown($cat_id, $jump = 0, $admin = 0, $i = 0) {
global $lang, $drop_down_cat_cache, $cat_parent_cache;
// $admin = 1 Main Cat (update/add cats)
// $admin = 2 All Cats (find/validate images...)
// $admin = 3 Select Cat (update/add image)
// $admin = 4 No Cat (check new images)
switch ($admin) {
case 1:
$category = "\n