<?php

require_once 'Horde.php';

/**
 * The Text_Filter_emoticons:: class finds emoticon strings ( :), etc.) in a
 * block of text and turns them into image links.
 *
 * Parameters:
 * <pre>
 * entities -- If true the html entity versions of the patterns will be used.
 * </pre>
 *
 * $Horde: framework/Text_Filter/Filter/emoticons.php,v 1.17.10.10 2006/07/24 04:48:17 slusarz Exp $
 *
 * Copyright 2003-2006 Marko Djukic <marko@oblo.com>
 *
 * See the enclosed file COPYING for license information (LGPL). If you did not
 * receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Marko Djukic <marko@oblo.com>
 * @package Horde_Text
 */
class Text_Filter_emoticons extends Text_Filter {

    /**
     * Filter parameters.
     *
     * @var array
     */
    var $_params = array('entities' => false);

    /**
     * Returns a hash with replace patterns.
     *
     * @return array  Patterns hash.
     */
    function getPatterns()
    {
        $regexp = array();
        $iconpath = $GLOBALS['registry']->getImageDir('horde') . '/emoticons';

        /* Loop through possible string emoticons and convert to graphics. */
        foreach ($this->getIcons() as $string => $icon) {
            $beg_pattern = '(^|\s)';
            $end_pattern = '(?=\s)';
            if ($this->_params['entities']) {
                $string = htmlspecialchars($string);
                $beg_pattern = '(^|\s|<br />|&nbsp;)';
                $end_pattern = '(?=\s|<br />|&nbsp;)';
            }

            /* Check for a smiley either immediately at the start of a line or
             * following a space. Use {} as the preg delimiters as this is not
             * found in any smiley. */
            $regexp['{' . $beg_pattern . preg_quote($string) . $end_pattern . '}'] = '\\1' . Horde::img($icon . '.png', $string, array('align' => 'middle', 'title' => $string), $iconpath) . (($this->_params['entities']) ? '\\2' : '');
        }

        return array('regexp' => $regexp);
    }

    /**
     * Returns a hash with emoticons and names.
     *
     * @return array  Patterns hash.
     */
    function getIcons()
    {
        /* List complex strings before simpler ones, otherwise for example :((
         * would be matched against :( before :(( is found. */
        return array(
            ':/' => 'frustrated', ':-/' => 'frustrated',
            // ':*>' => 'blush',
            ':e' => 'disappointed',
            '=:)$' => 'mrt',
            '#|' => 'hangover', '#-|' => 'hangover',
            ':-@' => 'shout',
            ':((' => 'bigfrown', ':C' => 'bigfrown',
            ':S' => 'dazed', ':-S' => 'dazed',
            'X@' => 'angry',
            'X(' => 'mad',
            // '>:)' => 'devil', '>:-)' => 'devil',
            // '>:p' => 'deviltongue', '>:-p' => 'deviltongue',
            // '>:p' => 'raspberry', '>:P' => 'raspberry',
            // '&)' => 'punk',
            // '&p' => 'punktongue',
            // '=&)' => 'punkmohawk',
            ':]' => 'grin',
            '#[' => 'hurt', '#(' => 'hurt', '#-[' => 'hurt', '#-(' => 'hurt',
            ':O' => 'embarrassed', ':-O' => 'embarrassed',
            ':[' => 'sad',
            // '>:@' => 'enraged',
            // ':&' => 'annoyed',
            '=(' => 'worried', '=-(' => 'worried',
            ':|=' => 'vampire',
            ':-(' => 'frown',
            ':D' => 'biggrin',
            '8)' => 'cool',
            // In English, 8PM occurs sufficiently often to specifically
            // search for and exclude
            '8p(?<![Mm]\s+)' => 'cooltongue', // '8Þ' => 'cooltongue',
            '8D' => 'coolgrin',
            ':p' => 'tongueout', ':P' => 'tongueout', // ':Þ' => 'tongueout',
            '?:(' => 'confused', '%-(' => 'confused',
            // ':)&' => 'love',
            'O;-)' => 'angelwink',
            ';]' => 'winkgrin',
            ';p' => 'winktongue', ';P' => 'winktongue', // ';Þ' => 'winktongue',
            ':|' => 'indifferent', ':-|' => 'indifferent',
            '!|' => 'tired', '!-I' => 'tired',
            '|I' => 'asleep', '|-I' => 'asleep',
            'O:)' => 'angel', 'O:-)' => 'angel',
            'O;)' => 'angelwink',
            ';-)' => 'wink',
            ':#)' => 'clown', ':o)' => 'clown',
            ':)' => 'smile',
            ';)' => 'wink',
            ':-)' => 'smile',
            ':@' => 'shout',
            ':(' => 'frown',
        );
    }

}
