Esempio n. 1
0
/**
 * Render a code box.
 *
 * @param  string			The data type (e.g. file extension) we are rendering.
 * @param  tempcode		Contents (code) to render.
 * @param  boolean		Whether to show line numbers.
 * @param  boolean		Whether what we have came from inside a semihtml tag
 * @param  boolean		Whether what we have came from semihtml mode
 * @return array			A pair: The tempcode for the code box, and the title of the box
 */
function do_code_box($type, $embed, $numbers = true, $in_semihtml = false, $is_all_semihtml = false)
{
    $_embed = mixed();
    $title = mixed();
    if (file_exists(get_file_base() . '/sources/geshi/' . filter_naughty(strtolower($type)) . '.php') || file_exists(get_file_base() . '/sources_custom/geshi/' . filter_naughty($type) . '.php')) {
        $evaluated = $embed->evaluate();
        if ($in_semihtml || $is_all_semihtml) {
            require_code('comcode_from_html');
            $evaluated = semihtml_to_comcode($evaluated, true);
        }
        require_code('geshi');
        if (class_exists('GeSHi')) {
            require_code('developer_tools');
            destrictify();
            $geshi = new GeSHi($evaluated, strtolower($type));
            $geshi->set_header_type(GESHI_HEADER_DIV);
            if ($numbers) {
                $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
            }
            require_lang('comcode');
            $title = do_lang_tempcode('comcode:CODE_IN_LANGUAGE', escape_html($type));
            require_code('xhtml');
            $_embed = xhtmlise_html($geshi->parse_code());
            restrictify();
        }
    } else {
        switch (strtolower($type)) {
            case 'php':
                if (!function_exists('highlight_string')) {
                    break;
                }
                $evaluated = $embed->evaluate();
                if ($in_semihtml || $is_all_semihtml) {
                    require_code('comcode_from_html');
                    $evaluated = semihtml_to_comcode($evaluated);
                }
                if (strpos($evaluated, '<' . '?php') === false) {
                    $strip = true;
                    $evaluated = "<" . "?php\n" . $evaluated . "\n?" . ">";
                } else {
                    $strip = false;
                }
                require_code('xhtml');
                if (defined('HIPHOP_PHP')) {
                    $h_result = nl2br(escape_html($evaluated));
                } else {
                    ob_start();
                    highlight_string($evaluated);
                    $h_result = ob_get_contents();
                    ob_end_clean();
                }
                $_embed = xhtmlise_html($h_result);
                if ($strip) {
                    $_embed = str_replace('&lt;?php<br />', '', $_embed);
                    $_embed = str_replace('?&gt;', '', $_embed);
                }
                $title = do_lang_tempcode('PHP_CODE');
                break;
        }
    }
    return array($_embed, $title);
}
Esempio n. 2
0
 /**
  * Handle hooks supported bot commands. Note multiple bots may support the same commands, and all respond. It is recommended all bots support the command 'help'.
  *
  * @param  AUTO_LINK		The ID of the chat room
  * @param  string			The command used. This is just the chat message, so you can encode and recognise your own parameter scheme if you like.
  * @return ?string		Bot reply (NULL: bot does not handle the command)
  */
 function handle_commands($room_id, $string)
 {
     require_code('developer_tools');
     destrictify();
     if ($string == '((SHAKE))') {
         return NULL;
     }
     if (file_exists(get_custom_file_base() . '/sources_custom/programe')) {
         if (get_value('octavius_installed') !== '1') {
             disable_php_memory_limit();
             if (function_exists('set_time_limit')) {
                 @set_time_limit(600);
             }
             $GLOBALS['SITE_DB']->query("DROP TABLE bot", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("DROP TABLE bots", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("DROP TABLE conversationlog", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("DROP TABLE dstore", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("DROP TABLE gmcache", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("DROP TABLE gossip", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("DROP TABLE patterns", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("DROP TABLE templates", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("DROP TABLE thatindex", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("DROP TABLE thatstack", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("CREATE TABLE bot (\n\t\t\t\t  id int(11) NOT NULL auto_increment,\n\t\t\t\t  bot tinyint(4) NOT NULL default '0',\n\t\t\t\t  name varchar(255) NOT NULL default '',\n\t\t\t\t  value text NOT NULL,\n\t\t\t\t  PRIMARY KEY  (id),\n\t\t\t\t  KEY botname (bot,name)\n\t\t\t\t) TYPE=MyISAM", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("CREATE TABLE bots (\n\t\t\t\t  id tinyint(3) unsigned NOT NULL auto_increment,\n\t\t\t\t  botname varchar(255) NOT NULL default '',\n\t\t\t\t  PRIMARY KEY  (botname),\n\t\t\t\t  KEY id (id)\n\t\t\t\t) TYPE=MyISAM", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("CREATE TABLE conversationlog (\n\t\t\t\t  bot tinyint(3) unsigned NOT NULL default '0',\n\t\t\t\t  id int(11) NOT NULL auto_increment,\n\t\t\t\t  input text,\n\t\t\t\t  response text,\n\t\t\t\t  uid varchar(255) default NULL,\n\t\t\t\t  enteredtime timestamp(14) NOT NULL,\n\t\t\t\t  PRIMARY KEY  (id),\n\t\t\t\t  KEY botid (bot)\n\t\t\t\t) TYPE=MyISAM", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("CREATE TABLE dstore (\n\t\t\t\t  uid varchar(255) default NULL,\n\t\t\t\t  name text,\n\t\t\t\t  value text,\n\t\t\t\t  enteredtime timestamp(14) NOT NULL,\n\t\t\t\t  id int(11) NOT NULL auto_increment,\n\t\t\t\t  PRIMARY KEY  (id),\n\t\t\t\t  KEY nameidx (name(40))\n\t\t\t\t) TYPE=MyISAM", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("CREATE TABLE gmcache (\n\t\t\t\t  id int(11) NOT NULL auto_increment,\n\t\t\t\t  bot tinyint(3) unsigned NOT NULL default '0',\n\t\t\t\t  template int(11) NOT NULL default '0',\n\t\t\t\t  inputstarvals text,\n\t\t\t\t  thatstarvals text,\n\t\t\t\t  topicstarvals text,\n\t\t\t\t  patternmatched text,\n\t\t\t\t  inputmatched text,\n\t\t\t\t  combined text NOT NULL,\n\t\t\t\t  PRIMARY KEY  (id),\n\t\t\t\t  KEY combined (bot,combined(255))\n\t\t\t\t) TYPE=MyISAM", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("CREATE TABLE gossip (\n\t\t\t\t  bot tinyint(3) unsigned NOT NULL default '0',\n\t\t\t\t  gossip text,\n\t\t\t\t  id int(11) NOT NULL auto_increment,\n\t\t\t\t  PRIMARY KEY  (id),\n\t\t\t\t  KEY botidx (bot)\n\t\t\t\t) TYPE=MyISAM", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("CREATE TABLE patterns (\n\t\t\t\t  bot tinyint(3) unsigned NOT NULL default '0',\n\t\t\t\t  id int(11) NOT NULL auto_increment,\n\t\t\t\t  word varchar(255) default NULL,\n\t\t\t\t  ordera tinyint(4) NOT NULL default '0',\n\t\t\t\t  parent int(11) NOT NULL default '0',\n\t\t\t\t  isend tinyint(4) NOT NULL default '0',\n\t\t\t\t  PRIMARY KEY  (id),\n\t\t\t\t  KEY wordparent (parent,word),\n\t\t\t\t  KEY botid (bot)\n\t\t\t\t) TYPE=MyISAM", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("CREATE TABLE templates (\n\t\t\t\t  bot tinyint(3) unsigned NOT NULL default '0',\n\t\t\t\t  id int(11) NOT NULL default '0',\n\t\t\t\t  template text NOT NULL,\n\t\t\t\t  pattern varchar(255) default NULL,\n\t\t\t\t  that varchar(255) default NULL,\n\t\t\t\t  topic varchar(255) default NULL,\n\t\t\t\t  PRIMARY KEY  (id),\n\t\t\t\t  KEY bot (id)\n\t\t\t\t) TYPE=MyISAM", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("CREATE TABLE thatindex (\n\t\t\t\t  uid varchar(255) default NULL,\n\t\t\t\t  enteredtime timestamp(14) NOT NULL,\n\t\t\t\t  id int(11) NOT NULL auto_increment,\n\t\t\t\t  PRIMARY KEY  (id)\n\t\t\t\t) TYPE=MyISAM", NULL, NULL, true);
             $GLOBALS['SITE_DB']->query("CREATE TABLE thatstack (\n\t\t\t\t  thatid int(11) NOT NULL default '0',\n\t\t\t\t  id int(11) NOT NULL auto_increment,\n\t\t\t\t  value varchar(255) default NULL,\n\t\t\t\t  enteredtime timestamp(14) NOT NULL,\n\t\t\t\t  PRIMARY KEY  (id)\n\t\t\t\t) TYPE=MyISAM", NULL, NULL, true);
             $fp = "";
             $templatesinserted = 0;
             $depth = array();
             $whaton = "";
             $pattern = "";
             $topic = "";
             $that = "";
             $template = "";
             $startupwhich = "";
             $splitterarray = array();
             $inputarray = array();
             $genderarray = array();
             $personarray = array();
             $person2array = array();
             require_code('programe/botloaderfuncs');
             loadstartup();
             makesubscode();
             set_value('octavius_installed', '1');
         }
         require_code('programe/respond');
         $response = replybotname(str_replace('?', '.', $string), get_session_id(), 'octavius');
         restrictify();
         if (is_null($response) || $response->response == '') {
             return NULL;
         }
         return '[html]' . $response->response . '[/html]';
     }
     // Eliza...
     // setup initial variables and values
     $kwarray = array();
     $vararray = array();
     $resparray = array();
     $priarray = array();
     $wordarray = array();
     $kwcount = 0;
     $varcount = 0;
     $respcount = 0;
     $syncount = 0;
     mt_srand((double) microtime() * 1000000);
     // load knowledge file
     $lines_array = file(get_custom_file_base() . "/sources_custom/hooks/modules/chat_bots/knowledge.txt");
     $count = count($lines_array);
     // This for loop goes through the entire knowledge file and places
     // the elements into arrays.  This later allows us to pull the information
     // (ie. key words, variances on the keywords, and responses) out of the
     // arrays.
     for ($x = 0; $x < $count; $x++) {
         $lines_array[$x] = trim($lines_array[$x]);
         $lines_array[$x] = ereg_replace("[\\]", "", $lines_array[$x]);
         if (strstr($lines_array[$x], "key:")) {
             eregi("key: (.*)", $lines_array[$x], $kw);
             $kwarray[$kwcount] = strtoupper($kw[1]);
             $currentkw = $kwcount;
             $kwcount++;
             $varcount = 0;
             // reset varcount to null
             $respcount = 0;
             // reset respcount to null
             $pricount = 0;
             // reset pricount to null
         } else {
             if (strstr($lines_array[$x], "var:")) {
                 eregi("var: (.*)", $lines_array[$x], $variance);
                 $vararray[$currentkw][$varcount] = strtoupper($variance[1]);
                 $varcurrent = $varcount;
                 $varcount++;
                 $respcount = 0;
             } else {
                 if (strstr($lines_array[$x], "pri:")) {
                     eregi("pri: (.*)", $lines_array[$x], $priority);
                     $priarray[$currentkw] = $priority[1];
                 } else {
                     if (strstr($lines_array[$x], "resp:")) {
                         eregi("resp: (.*)", $lines_array[$x], $response);
                         $resparray[$currentkw][$varcurrent][$respcount] = $response[1];
                         $respcount++;
                     } else {
                         if (strstr($lines_array[$x], "syn:")) {
                             eregi("syn: (.*)", $lines_array[$x], $synonym);
                             $synonymarray[$syncount] = strtoupper($synonym[1]);
                             $syncount++;
                         } else {
                             if (strstr($lines_array[$x], "goto:")) {
                                 eregi("goto: (.*)", $lines_array[$x], $goto);
                                 $goto = strtoupper($goto[1]);
                                 // find the keyword
                                 for ($zcount = 0; $zcount < count($kwarray); $zcount++) {
                                     // if the keyword already exists
                                     if (eregi($goto, $kwarray[$zcount])) {
                                         // then we assign properties of the keyword
                                         $vararray[$currentkw][0] = $kwarray[$currentkw];
                                         $resparray[$currentkw] = $resparray[$zcount];
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $y = 0;
     $z = 0;
     $v = 0;
     $bestpriority = -2;
     $originalstring = $string;
     if (!$string) {
         $string = "hello";
     }
     $string = strtoupper($string);
     // Figures out what word in the string has the most priority.
     // It can then check words to the left/right of this word depending
     // upon settings in the knowledge.txt file.
     while ($y < count($kwarray)) {
         // remove beginning and trailing white space, breaks, etc
         $string = trim($string);
         // remove puncuation from string
         $string = ereg_replace('[!?,.]', '', $string);
         // split the string up into seperate words
         $wordarray = explode(" ", $string);
         while ($v < count($wordarray)) {
             if (eregi($wordarray[$v] . "\$", $kwarray[$y])) {
                 // find which word holds the most weight in the sentance
                 if ($bestpriority == -2) {
                     $bestpriority = $y;
                 } else {
                     if ($priarray[$bestpriority] < $priarray[$y]) {
                         $bestpriority = $y;
                     }
                 }
             }
             $v++;
         }
         $v = 0;
         $y++;
     }
     // find the variance with the most matching words
     $vcount = 0;
     while ($vcount < count($vararray[$bestpriority])) {
         if (strstr($vararray[$bestpriority][$vcount], "@")) {
             eregi("@(.*)", $vararray[$bestpriority][$vcount], $syn);
             // fix this
             $syn = $syn[1];
             for ($x = 0; $x < count($synonymarray); $x++) {
                 if (eregi($syn, strtoupper($synonymarray[$x]))) {
                     $sarray = explode(" ", $synonymarray[$x]);
                     for ($f = 0; $f < count($sarray); $f++) {
                         $newstring = ereg_replace("@(.*)\$", $sarray[$f], $vararray[$bestpriority][$vcount]);
                         // works to this point
                         if (eregi($newstring . "\$", $string)) {
                             $varray = explode(" ", $vararray[$bestpriority][$vcount]);
                             if (count($varray) > $pvarray) {
                                 $bestvariance = $vcount;
                                 $pvarray = count($varray);
                             }
                         }
                     }
                 }
             }
         } else {
             if (ereg($vararray[$bestpriority][$vcount], $string)) {
                 $varray = explode(" ", $vararray[$bestpriority][$vcount]);
                 if (count($varray) > $pvarray) {
                     $bestvariance = $vcount;
                     $pvarray = count($varray);
                 }
             }
         }
         $vcount++;
     }
     // Using the bestpriority (aka the keyword (key:) with the most weight in the sentence)
     // and the bestvariance (aka, the variance (var:) phrase that most fits the context of
     // the original sentence, we form a response.
     if (count($resparray[$bestpriority][$bestvariance]) > 1) {
         $random = mt_rand(0, count($resparray[$bestpriority][$bestvariance]) - 1);
     } else {
         $random = 0;
     }
     $response = $resparray[$bestpriority][$bestvariance][$random];
     if ($response == "") {
         $response = "Sorry, I don't understand what you're trying to say.";
     }
     $originalstring = ereg_replace("[\\]", "", $originalstring);
     restrictify();
     return $response;
 }
Esempio n. 3
0
/**
 * Get the block object for a given block codename.
 *
 * @param  ID_TEXT		The block name
 * @param  ?array			The block parameter map (NULL: no parameters)
 * @return mixed			Either the block object, or the string output of a miniblock
 */
function do_block_hunt_file($codename, $map = NULL)
{
    global $BLOCKS_AT_CACHE;
    $codename = filter_naughty_harsh($codename);
    $file_base = get_file_base();
    global $_REQUIRED_CODE;
    if (isset($BLOCKS_AT_CACHE[$codename]) && !in_safe_mode() && $BLOCKS_AT_CACHE[$codename] == 'sources_custom/blocks' || !isset($BLOCKS_AT_CACHE[$codename]) && is_file($file_base . '/sources_custom/blocks/' . $codename . '.php')) {
        if (!isset($_REQUIRED_CODE['blocks/' . $codename])) {
            require_once $file_base . '/sources_custom/blocks/' . $codename . '.php';
        }
        $_REQUIRED_CODE['blocks/' . $codename] = 1;
        if (!isset($BLOCKS_AT_CACHE[$codename])) {
            $BLOCKS_AT_CACHE[$codename] = 'sources_custom/blocks';
            if (function_exists('persistant_cache_set')) {
                persistant_cache_set('BLOCKS_AT', $BLOCKS_AT_CACHE, true);
            }
        }
    } elseif (isset($BLOCKS_AT_CACHE[$codename]) && $BLOCKS_AT_CACHE[$codename] == 'sources/blocks' || !isset($BLOCKS_AT_CACHE[$codename]) && is_file($file_base . '/sources/blocks/' . $codename . '.php')) {
        if (!isset($_REQUIRED_CODE['blocks/' . $codename])) {
            require_once $file_base . '/sources/blocks/' . $codename . '.php';
        }
        $_REQUIRED_CODE['blocks/' . $codename] = 1;
        if (!isset($BLOCKS_AT_CACHE[$codename])) {
            $BLOCKS_AT_CACHE[$codename] = 'sources/blocks';
            if (function_exists('persistant_cache_set')) {
                persistant_cache_set('BLOCKS_AT', $BLOCKS_AT_CACHE, true);
            }
        }
    } else {
        if (isset($BLOCKS_AT_CACHE[$codename]) && !in_safe_mode() && $BLOCKS_AT_CACHE[$codename] == 'sources_custom/miniblocks' || !isset($BLOCKS_AT_CACHE[$codename]) && is_file($file_base . '/sources_custom/miniblocks/' . $codename . '.php')) {
            require_code('developer_tools');
            destrictify();
            ob_start();
            if (defined('HIPHOP_PHP')) {
                require 'sources_custom/miniblocks/' . $codename . '.php';
            } else {
                require $file_base . '/sources_custom/miniblocks/' . $codename . '.php';
            }
            $object = ob_get_contents();
            if ($GLOBALS['XSS_DETECT']) {
                ocp_mark_as_escaped($object);
            }
            ob_end_clean();
            restrictify();
            if (!isset($BLOCKS_AT_CACHE[$codename])) {
                $BLOCKS_AT_CACHE[$codename] = 'sources_custom/miniblocks';
                if (function_exists('persistant_cache_set')) {
                    persistant_cache_set('BLOCKS_AT', $BLOCKS_AT_CACHE, true);
                }
            }
        } elseif (isset($BLOCKS_AT_CACHE[$codename]) && $BLOCKS_AT_CACHE[$codename] == 'sources/miniblocks' || !isset($BLOCKS_AT_CACHE[$codename]) && is_file($file_base . '/sources/miniblocks/' . $codename . '.php')) {
            require_code('developer_tools');
            destrictify();
            ob_start();
            if (defined('HIPHOP_PHP')) {
                require 'sources/miniblocks/' . $codename . '.php';
            } else {
                require $file_base . '/sources/miniblocks/' . $codename . '.php';
            }
            $object = ob_get_contents();
            if ($GLOBALS['XSS_DETECT']) {
                ocp_mark_as_escaped($object);
            }
            ob_end_clean();
            restrictify();
            if (!isset($BLOCKS_AT_CACHE[$codename])) {
                $BLOCKS_AT_CACHE[$codename] = 'sources/miniblocks';
                if (function_exists('persistant_cache_set')) {
                    persistant_cache_set('BLOCKS_AT', $BLOCKS_AT_CACHE, true);
                }
            }
        } elseif (is_null($map) || !array_key_exists('failsafe', $map) || $map['failsafe'] != '1') {
            $temp = paragraph(do_lang_tempcode('MISSING_BLOCK_FILE', escape_html($codename)), '90dfdlksds8d7dyddssdds', 'error_marker');
            return $temp->evaluate();
        } else {
            $object = '';
        }
        return $object;
    }
    $_object = object_factory('Block_' . $codename);
    return $_object;
}