<?php
function smarty_modifier_t_cmd2($string)
{
    if (!OPENPNE_USE_CMD_TAG) {
        return $string;
    }
    $search = array("\r\n", "\r","&quot;");
    $replace = array("\n", "\n",'"');
    $string = str_replace($search, $replace, $string);
    $regexp = '/&lt;cmd\s+(?i:src)="([\w\.]+)"\s*([\s\w=",]+)\n(.*)\n&gt;\n/s';
//    $fp=fopen("e:\\debug.txt","w"); fwrite($fp,$string);fclose($fp);
    return preg_replace_callback($regexp, 'smarty_modifier_t_cmd2_callback', $string);
}

function smarty_modifier_t_cmd2_callback($matches)
{
    $param = array();
    $module=$matches[1];
    $tmp=split('[ ,]',$matches[2]);
    for($i=0;$i<count($tmp);$i++) {
        $ttmp =split("=",$tmp[$i]);
        if(count($ttmp)==2)
            $param[trim($ttmp[0])] = preg_replace('/"(\w+)"/','$1',$ttmp[1]);
    }
    $body = trim($matches[3],"\n\t ");
/*
 * $module
 * $param
 * $body
 *
**/
    $file = dirname(__FILE__) . '/../cmd_plugins/cmd_'.$module.'.php';
    if(is_readable($file)) {
      require_once $file;
      $cmdmain = 'cmd_'.$module.'_main';
      return $cmdmain($param,$body);
    }

    $result="module=$matches[1]\n";
    foreach($param as $key => $value)
      $result .= "$key = $value\n";
    $result .= "/$body/\n";
    return '<font color="red">' . $result . '</font>';

}
