function cb_url($url, $title = false, $img = false, $chop = '')
 {
     global $config;
     if (is_array($url)) {
         list(, $url) = $url;
     }
     if ($img == true && (preg_match("~(\\[/?[\\w\\d]{1,10}\\])~", $chop) || preg_match("/(([^?&=\\[\\]]+?)\\.(png|gif|bmp|jpg|jpe|jpeg))/is", $url))) {
         return $url . $chop;
     }
     if (strtolower(substr($url, 0, 4)) == 'www.') {
         $url = "http://{$url}";
     }
     $specialchars = array('"', '>', '<', ''');
     foreach ($specialchars as $char) {
         $full = $url . $chop;
         if (($pos = strpos($full, $char)) !== false) {
             $url = substr($full, 0, $pos);
             $chop = substr($full, $pos);
             break;
         }
     }
     $pid = $this->noparse_id();
     $this->noparse[$pid] = $url;
     if ($title != false) {
         $ahref = '<a href="<!PID:' . $pid . '>" target="_blank">' . $title . '</a>';
         return $ahref;
     } elseif ($this->profile['reduceUrl'] == 1 && strxlen($url) >= $config['maxurllength']) {
         $prefix = ceil($config['maxurllength'] / 5);
         $suffix = strpos($url, '/', 8);
         $func = 'substr';
         if ($suffix === false) {
             $suffix = ceil($config['maxurllength'] / 3);
             $func = 'subxstr';
         }
         $newurl = $func($url, 0, $suffix + 1) . $config['maxurltrenner'] . subxstr($url, -$prefix);
         $pid2 = $this->noparse_id();
         $this->noparse[$pid2] = $newurl;
         $ahref = '<a href="<!PID:' . $pid . '>" target="_blank"><!PID:' . $pid2 . '></a>';
     } else {
         $ahref = '<a href="<!PID:' . $pid . '>" target="_blank"><!PID:' . $pid . '></a>';
     }
     return $ahref . $chop;
 }
 function cb_url($url, $title = false, $prefix = '', $suffix = '')
 {
     global $config;
     if (strtolower(substr($url, 0, 4)) == 'www.') {
         $url = "http://{$url}";
     }
     $pid = $this->noparse_id();
     $this->noparse[$pid] = $url;
     if ($title != false) {
         // Ein Titel wurde angegeben
         $ahref = '<a href="<!PID:' . $pid . '>" target="_blank">' . $title . '</a>';
         return $ahref;
     } elseif ($this->profile['reduceUrl'] == 1) {
         // Die URL wird als Titel genommen und gekürzt
         $before = ceil($config['maxurllength'] / 5);
         $after = strpos($url, '/', 8);
         $func = 'substr';
         if ($after === false) {
             $after = ceil($config['maxurllength'] / 3);
             $func = 'subxstr';
         }
         $newurl = $func($url, 0, $after + 1) . $config['maxurltrenner'] . subxstr($url, -$before);
         $pid2 = $this->noparse_id();
         $this->noparse[$pid2] = $newurl;
         $ahref = '<a href="<!PID:' . $pid . '>" target="_blank"><!PID:' . $pid2 . '></a>';
     } else {
         // Die URL wird ungekürzt als Titel genommen
         $ahref = '<a href="<!PID:' . $pid . '>" target="_blank"><!PID:' . $pid . '></a>';
     }
     return $prefix . $ahref . $suffix;
 }
function BoardSelect($board = 0)
{
    global $config, $my, $tpl, $db, $gpc, $lang, $scache, $plugins, $slog;
    $found = false;
    $sub_cache = $forum_cache = $last_cache = $forums = $cat = array();
    $categories_obj = $scache->load('categories');
    $cat_cache = $categories_obj->get();
    $memberdata_obj = $scache->load('memberdata');
    $memberdata = $memberdata_obj->get();
    $index_moderators = $scache->load('index_moderators');
    $mod_cache = $index_moderators->get();
    $prefix_obj = $scache->load('prefix');
    $prefix = $prefix_obj->get();
    ($code = $plugins->load('forums_query')) ? eval($code) : null;
    // Fetch Forums
    $result = $db->query("\n\tSELECT\n\t\tf.id, f.name, f.description, f.opt, f.optvalue, f.parent, f.topics, f.replies, f.last_topic, f.invisible,\n\t\tt.topic as l_topic, t.prefix AS l_prefix, t.id as l_tid, t.last as l_date, u.name AS l_uname, t.last_name AS l_name, f.id AS l_bid\n\tFROM {$db->pre}forums AS f\n\t\tLEFT JOIN {$db->pre}topics AS t ON f.last_topic=t.id\n\t\tLEFT JOIN {$db->pre}user AS u ON t.last_name=u.id\n\tORDER BY f.parent, f.position\n\t");
    $keys = array('l_prefix' => null, 'l_topic_full' => null, 'l_topic' => null, 'l_tid' => null, 'l_date' => null, 'l_uname' => null, 'l_name' => null, 'l_bid' => null);
    while ($row = $db->fetch_assoc($result)) {
        $row['name'] = $gpc->prepare($row['name']);
        $row['l_uname'] = $gpc->prepare($row['l_uname']);
        $row['l_name'] = $gpc->prepare($row['l_name']);
        $row['bid'] = $cat_cache[$row['parent']]['parent'];
        // Caching for Subforums
        if (!empty($row['bid'])) {
            $sub_cache[$row['bid']][] = $row;
        }
        // Caching the Forums
        if ($row['bid'] == $board) {
            $forum_cache[$row['parent']][] = $row;
        }
        $last_cache[$row['id']] = $row;
        ($code = $plugins->load('forums_caching')) ? eval($code) : null;
    }
    $cats = array();
    $hidden = 0;
    // Work with the chached data!
    foreach ($cat_cache as $cat) {
        $cat['forums'] = array();
        if (isset($forum_cache[$cat['id']]) == false) {
            continue;
        }
        foreach ($forum_cache[$cat['id']] as $forum) {
            $found = true;
            $forum['new'] = false;
            $forum['show'] = true;
            // Subforendaten vererben (Letzter Beitrag, Markierung)
            if (isset($sub_cache[$forum['id']])) {
                $substats = SubStats($forum['topics'], $forum['replies'], $forum['id'], $sub_cache);
                $forum['topics'] = $substats[0];
                $forum['replies'] = $substats[1];
            }
            // Letzter Beitrag
            $last = $last_cache[$forum['id']];
            if (isset($sub_cache[$forum['id']])) {
                foreach ($substats[2] as $last_bid) {
                    $sub = $last_cache[$last_bid];
                    if ($last['l_date'] < $sub['l_date'] && check_forumperm($sub)) {
                        $last = $sub;
                    }
                }
            }
            $forum = array_merge($forum, array_intersect_key($last, $keys));
            if (is_id($forum['l_name']) && isset($memberdata[$forum['l_name']])) {
                $forum['l_name'] = array($forum['l_uname'], $forum['l_name']);
            } else {
                $forum['l_name'] = array($forum['l_name'], 0);
            }
            // Rechte und Gelesensystem
            if ($forum['opt'] != 're') {
                if (!check_forumperm($forum)) {
                    if ($forum['invisible'] != 0) {
                        $forum['show'] = false;
                    }
                    $forum['foldimg'] = $tpl->img('cat_locked');
                    $forum['state'] = BOARD_STATE_LOCKED;
                    $forum['topics'] = '-';
                    $forum['replies'] = '-';
                    $forum['l_topic'] = false;
                    $forum['l_topic_full'] = '';
                } else {
                    if ($slog->isForumRead($forum['id'], $forum['l_date']) || $forum['topics'] < 1) {
                        $forum['foldimg'] = $tpl->img('cat_open');
                        $forum['state'] = BOARD_STATE_OLD;
                    } else {
                        $forum['foldimg'] = $tpl->img('cat_red');
                        $forum['state'] = BOARD_STATE_NEW;
                        $forum['new'] = true;
                    }
                    if (!empty($forum['l_topic'])) {
                        if (isset($prefix[$forum['id']][$forum['l_prefix']]) && $forum['l_prefix'] > 0) {
                            $lang->assign('prefix', $prefix[$forum['id']][$forum['l_prefix']]['value']);
                            $forum['l_prefix'] = $lang->phrase('showtopic_prefix_title');
                        } else {
                            $forum['l_prefix'] = '';
                        }
                        if (strxlen($forum['l_topic']) > $config['lasttopic_chars']) {
                            $forum['l_topic_full'] = $forum['l_prefix'] . $forum['l_topic'];
                            $forum['l_topic'] = subxstr($forum['l_topic'], 0, $config['lasttopic_chars']);
                            $forum['l_topic'] .= "...";
                        } else {
                            $forum['l_topic_full'] = '';
                        }
                        $forum['l_date'] = str_date($lang->phrase('dformat1'), times($forum['l_date']));
                    }
                }
            }
            $forum['topics'] = numbers($forum['topics']);
            $forum['replies'] = numbers($forum['replies']);
            // Moderatoren
            $forum['mod'] = array();
            if (isset($mod_cache[$forum['id']])) {
                $anz2 = count($mod_cache[$forum['id']]);
                for ($i = 0; $i < $anz2; $i++) {
                    if ($anz2 != $i + 1) {
                        $mod_cache[$forum['id']][$i]['sep'] = ', ';
                    } else {
                        $mod_cache[$forum['id']][$i]['sep'] = '';
                    }
                    $forum['mod'][] = $mod_cache[$forum['id']][$i];
                }
            }
            // Unterforen
            $forum['sub'] = array();
            if ($config['showsubfs']) {
                if (isset($sub_cache[$forum['id']])) {
                    $anz2 = count($sub_cache[$forum['id']]);
                    $sub = array();
                    for ($i = 0; $i < $anz2; $i++) {
                        $show = true;
                        $sub_cache[$forum['id']][$i]['new'] = false;
                        if ($sub_cache[$forum['id']][$i]['opt'] != 're') {
                            if (!check_forumperm($sub_cache[$forum['id']][$i])) {
                                if ($sub_cache[$forum['id']][$i]['invisible'] != 0) {
                                    $show = false;
                                } else {
                                    $sub_cache[$forum['id']][$i]['foldimg'] = $tpl->img('subcat_locked');
                                    $sub_cache[$forum['id']][$i]['state'] = BOARD_STATE_LOCKED;
                                }
                            } else {
                                if ($slog->isForumRead($sub_cache[$forum['id']][$i]['id'], $sub_cache[$forum['id']][$i]['l_date']) || $sub_cache[$forum['id']][$i]['topics'] < 1) {
                                    $sub_cache[$forum['id']][$i]['foldimg'] = $tpl->img('subcat_open');
                                    $sub_cache[$forum['id']][$i]['state'] = BOARD_STATE_OLD;
                                } else {
                                    $sub_cache[$forum['id']][$i]['foldimg'] = $tpl->img('subcat_red');
                                    $sub_cache[$forum['id']][$i]['state'] = BOARD_STATE_NEW;
                                    $sub_cache[$forum['id']][$i]['new'] = true;
                                }
                            }
                        } else {
                            $sub_cache[$forum['id']][$i]['foldimg'] = $tpl->img('subcat_redirect');
                            $sub_cache[$forum['id']][$i]['state'] = BOARD_STATE_WWW;
                        }
                        if ($show == true) {
                            $forum['sub'][] = $sub_cache[$forum['id']][$i];
                        }
                    }
                }
            }
            ($code = $plugins->load('forums_entry_prepared')) ? eval($code) : null;
            if ($forum['show'] == true) {
                $cat['forums'][] = $forum;
            } elseif ($forum['invisible'] != 2) {
                $hidden++;
            }
        }
        if (count($cat['forums']) > 0) {
            $cats[] = $cat;
        }
    }
    ($code = $plugins->load('forums_prepared')) ? eval($code) : null;
    $error_state = count($cats) == 0 && $board == 0;
    if (count($cats) > 0 || $error_state) {
        $tpl->globalvars(compact("cats", "board", "hidden", "error_state"));
        echo $tpl->parse("categories");
    }
    // Else: This is a forum without sub forums (that should be displayed)
    return $found;
}
 /**
  * Truncates a string to a certain length at the most sensible point.
  * First, if there's a '.' character near the end of the string, the string is truncated after this character.
  * If there is no '.', the string is truncated after the last ' ' character.
  * If the string is truncated, " ..." is appended.
  * If the string is already shorter than $length, it is returned unchanged.
  *
  * @static
  * @param string    string A string to be truncated.
  * @param int        length the maximum length the string should be truncated to
  * @return string    the truncated string
  */
 function iTrunc($string, $length)
 {
     if (strxlen($string) <= $length) {
         return $string;
     }
     $pos = strrpos($string, ".");
     if ($pos !== false && $pos >= $length - 4) {
         $string = substr($string, 0, $length - 4);
         $pos = strrpos($string, ".");
     }
     if ($pos !== false && $pos >= $length * 0.4) {
         return substr($string, 0, $pos + 1) . " ...";
     }
     $pos = strrpos($string, " ");
     if ($pos !== false && $pos >= $length - 4) {
         $string = substr($string, 0, $length - 4);
         $pos = strrpos($string, " ");
     }
     if ($pos !== false && $pos >= $length * 0.4) {
         return substr($string, 0, $pos) . " ...";
     }
     return subxstr($string, 0, $length - 4) . " ...";
 }