Пример #1
0
/**
 * prepareMessage()
 *
 * Prepare incoming message data for storage
 */
function prepareMessage(&$name, $text)
{
    $message = array();
    // Parse text for commands and format accordingly
    $cmd = getCommand($text);
    // Perform some custom prefiltering
    $name = prefilterName($name);
    $text = $cmd == 'link' ? preFilterLink($text) : preFilterText($text);
    // HTML filter
    $filter = new lib_filter();
    $action = false;
    switch ($cmd) {
        case 'action':
            // Action
            $action = true;
            $text = $filter->go(mb_substr($text, 3));
            break;
        case 'link':
            // AutoLink
            // Grab the URL from the message
            $input = explode(' ', trim($text));
            $url = array_shift($input);
            if (mb_substr($url, 0, 4) == 'www.') {
                $url = 'http://' . $url;
            }
            $urlparts = @parse_url($url);
            if (array_key_exists('host', $urlparts)) {
                // Url is most likely valid (parse_url() is
                // not the best way to check this)
                if (count($input) > 0) {
                    // There is link text
                    $urltext = implode(' ', $input);
                } else {
                    // the url becomes the link text, and is shotened if necessary
                    $urltext = mb_strlen($url) > 40 ? str_shorten($url, 25) : $url;
                }
                $text = '<a href="' . htmlentities($url) . '"';
                $text .= 'title="[' . htmlentities($urlparts['host']) . ']" rel="external">';
                $text .= htmlentities($urltext) . '</a>';
            } else {
                // Url is most likely invalid
                return false;
            }
            break;
        default:
            // No command
            $text = $filter->go($text);
            break;
    }
    if (mb_strlen(trim($text)) == 0) {
        // Message text is invalid
        return false;
    }
    $message['action'] = $action;
    $message['time'] = time();
    $message['name'] = $name;
    $message['text'] = $text;
    return $message;
}
function strshort($string, $length, $append = '...')
{
    return str_shorten($string, $length, $append);
}