public function vxSearchSubstance()
 {
     $err = array();
     $err['too_common'] = 0;
     $stage = 0;
     $query_verified = array();
     $query_task = array();
     $query_common = array();
     $style_search_highlight = '<span class="text_matched">\\1</span>';
     $stop_words = array('the', '的', '我');
     if (isset($_GET['q'])) {
         $query = trim($_GET['q']);
         if (strlen($query) > 0) {
             if (get_magic_quotes_gpc()) {
                 $query = stripslashes($query);
             }
             $stage = 1;
         }
     }
     if ($stage == 1) {
         $query_hash = md5($query);
         $query_splitted = explode(' ', $query);
         foreach ($query_splitted as $query_keyword) {
             if (!in_array($query_keyword, $query_verified)) {
                 $query_verified[] = $query_keyword;
                 if (in_array($query_keyword, $stop_words)) {
                     $query_common[] = $query_keyword;
                 } else {
                     $query_task[] = $query_keyword;
                 }
             }
         }
         $count_verified = count($query_verified);
         $count_task = count($query_task);
         $count_common = count($query_common);
         if ($count_task > 0) {
             $stage = 2;
         } else {
             if ($count_common > 0) {
                 $stage = 3;
             }
         }
     }
     if ($stage == 2) {
         if ($result_a = $this->cl->load('k_search_' . $query_hash)) {
             $time_start = microtime_float();
             $result_a = unserialize($result_a);
             $count_result = count($result_a);
         } else {
             $time_start = microtime_float();
             // get topics
             $i = 0;
             /* FIX: WHAT A DIRTY HACK! */
             $mysql_ver = mysql_get_server_info();
             $mysql_ver_major = intval(substr($mysql_ver, 0, 1));
             if ($mysql_ver_major > 4) {
                 $sql = "SELECT DISTINCT tpc_id, tpc_uid, tpc_title, tpc_description, tpc_content, tpc_uid, tpc_lasttouched, usr_nick FROM babel_topic LEFT JOIN (babel_post, babel_user) ON (babel_topic.tpc_id = babel_post.pst_tid AND babel_topic.tpc_uid = babel_user.usr_id) WHERE (";
                 foreach ($query_task as $task) {
                     $task = mysql_real_escape_string($task, $this->db);
                     $i++;
                     if ($i == 1) {
                         $sql = $sql . '(';
                     } else {
                         $sql = $sql . ' OR (';
                     }
                     $sql = $sql . "tpc_title LIKE '%{$task}%'";
                     $sql = $sql . " OR tpc_description LIKE '%{$task}%'";
                     $sql = $sql . " OR tpc_content LIKE '%{$task}%'";
                     $sql = $sql . ')';
                     $sql = $sql . ' OR (';
                     $sql = $sql . "pst_content LIKE '%{$task}%'";
                     $sql = $sql . ')';
                 }
                 $sql = $sql . ")";
                 $sql = $sql . " ORDER BY tpc_created DESC";
             } else {
                 $sql = "SELECT DISTINCT tpc_id, tpc_title, tpc_description, tpc_content, tpc_uid, tpc_lasttouched, usr_nick FROM babel_topic, babel_post, babel_user WHERE (";
                 foreach ($query_task as $task) {
                     $task = mysql_real_escape_string($task, $this->db);
                     $i++;
                     if ($i == 1) {
                         $sql = $sql . '(';
                     } else {
                         $sql = $sql . ' OR (';
                     }
                     $sql = $sql . "tpc_title LIKE '%{$task}%'";
                     $sql = $sql . " OR tpc_description LIKE '%{$task}%'";
                     $sql = $sql . " OR tpc_content LIKE '%{$task}%'";
                     $sql = $sql . ')';
                     $sql = $sql . ' OR (';
                     $sql = $sql . "pst_content LIKE '%{$task}%'";
                     $sql = $sql . ')';
                 }
                 $sql = $sql . ")";
                 $sql = $sql . " AND (tpc_uid = usr_id AND tpc_id = pst_tid)";
                 $sql = $sql . " ORDER BY tpc_created DESC";
             }
             $rs = mysql_query($sql, $this->db);
             $count_matched = mysql_num_rows($rs);
             // get ads
             if (KIJIJI_LEGACY_API_SEARCH_ENABLED) {
                 if ($x = $this->cl->load('k_search_ads_' . $query_hash)) {
                     $x = simplexml_load_string($x);
                     $count_ads = $x->Body->return_ad_count;
                 } else {
                     if (preg_match('/[a-z0-9]/i', $query)) {
                         $count_ads = 0;
                     } else {
                         $req_kijiji =& new HTTP_Request("http://shanghai.kijiji.com.cn/classifieds/ClassiApiSearchAdExCommand");
                         $req_kijiji_input = '<?xml version="1.0" encoding="UTF-8" ?><SOAP:Envelope xmlns:SOAP="http://www.w3.org/2003/05/soap-envelope" ><SOAP:Header ><m:command xmlns:m="http://www.kijiji.com/soap">search_ad_ex</m:command><m:version xmlns:m="http://www.kijiji.com/soap">1</m:version></SOAP:Header><SOAP:Body><m:search_options xmlns:m="http://www.kijiji.com/soap">					<sub_area_id></sub_area_id><neighborhood_id></neighborhood_id><date_duration>40</date_duration><category_id></category_id><load_image>true</load_image><keyword>' . $query . '</keyword><return_ad_count>100</return_ad_count></m:search_options></SOAP:Body></SOAP:Envelope>';
                         $req_kijiji->setMethod(HTTP_REQUEST_METHOD_POST);
                         $req_kijiji->addPostData("xml", $req_kijiji_input);
                         if (!PEAR::isError($req_kijiji->sendRequest())) {
                             $rt_kijiji = $req_kijiji->getResponseBody();
                             $rt_kijiji = str_replace('SOAP:', '', $rt_kijiji);
                             $rt_kijiji = str_replace('m:', '', $rt_kijiji);
                         } else {
                             $rt_kijiji = '';
                         }
                         if ($rt_kijiji != '') {
                             $this->cl->save($rt_kijiji, 'k_search_ads_' . $query_hash);
                             $x = simplexml_load_string($rt_kijiji);
                             $count_ads = $x->Body->return_ad_count;
                         } else {
                             $count_ads = 0;
                         }
                     }
                 }
             } else {
                 $count_ads = 0;
             }
             // total
             $count_result = $count_ads + $count_matched;
             // the remix
             if ($count_result > 0) {
                 $result_a = array();
                 // db
                 $unique_a = array();
                 if ($count_matched > 0) {
                     while ($Topic = mysql_fetch_object($rs)) {
                         $Result = null;
                         $Result->title = $Topic->tpc_title;
                         $Result->type = 0;
                         $Result->author = $Topic->usr_nick;
                         $Result->excerpt = make_excerpt_topic($Topic, $query_task, $style_search_highlight);
                         $Result->url = '/topic/view/' . $Topic->tpc_id . '.html';
                         $Result->timestamp = $Topic->tpc_lasttouched;
                         $Result->uid = $Topic->tpc_uid;
                         $Result->hash = sha1($Result->title . $Result->author . $Result->excerpt);
                         if (!in_array($Result->hash, $unique_a)) {
                             $result_a[$Result->timestamp] = $Result;
                             $unique_a[$Result->timestamp] = $Result->hash;
                         }
                     }
                     mysql_free_result($rs);
                     krsort($result_a, SORT_NUMERIC);
                 } else {
                     mysql_free_result($rs);
                 }
                 // xml
                 if ($count_ads > 0) {
                     for ($i = 0; $i < $count_ads; $i++) {
                         $Result = null;
                         $Result->title = strval($x->Body->ad[$i]->title);
                         $Result->type = 1;
                         $Result->author = '客齐集';
                         $Result->excerpt = make_excerpt_ad($x->Body->ad[$i]->description, $query_task, $style_search_highlight);
                         $Result->url = strval($x->Body->ad[$i]->view_ad_url);
                         $Result->timestamp = format_api_date($x->Body->ad[$i]->start_date);
                         $Result->uid = 0;
                         $Result->hash = sha1($Result->title . $Result->author . $Result->excerpt);
                         if (isset($x->Body->ad[$i]->img_url[0])) {
                             $_excerpt = '<img src="' . strval($x->Body->ad[$i]->img_url[0]) . '" width="75" height="75" class="thumbnail" align="left" />' . $Result->excerpt;
                             $Result->excerpt = $_excerpt;
                         }
                         if (!in_array($Result->hash, $unique_a)) {
                             $result_a[$Result->timestamp] = $Result;
                             $unique_a[$Result->timestamp] = $Result->hash;
                         }
                     }
                     krsort($result_a, SORT_NUMERIC);
                 }
                 $this->cl->save(serialize($result_a), 'k_search_' . $query_hash);
             }
         }
         // page
         $p = array();
         $p['base'] = '/q/' . implode('+', $query_task) . '/';
         $p['ext'] = '.html';
         $p['items'] = $count_result;
         $p['size'] = BABEL_MSG_PAGE;
         $p['span'] = BABEL_PG_SPAN;
         if ($p['items'] % $p['size'] == 0) {
             $p['total'] = $p['items'] / $p['size'];
         } else {
             $p['total'] = floor($p['items'] / $p['size']) + 1;
         }
         if (isset($_GET['p'])) {
             $p['cur'] = intval($_GET['p']);
         } else {
             $p['cur'] = 1;
         }
         if ($p['cur'] < 1) {
             $p['cur'] = 1;
         }
         if ($p['cur'] > $p['total']) {
             $p['cur'] = $p['total'];
         }
         if ($p['cur'] - $p['span'] >= 1) {
             $p['start'] = $p['cur'] - $p['span'];
         } else {
             $p['start'] = 1;
         }
         if ($p['cur'] + $p['span'] <= $p['total']) {
             $p['end'] = $p['cur'] + $p['span'];
         } else {
             $p['end'] = $p['total'];
         }
         $p['offset'] = ($p['cur'] - 1) * $p['size'];
         if ($count_result > $p['size']) {
             $result_b = array_slice($result_a, $p['offset'], $p['size'], true);
         } else {
             $result_b = $result_a;
         }
         $time_end = microtime_float();
         $time_elapsed = $time_end - $time_start;
     }
     echo '<div id="main"><div class="blank" align="left">';
     _v_ico_map();
     echo ' <a href="/">' . Vocabulary::site_name . '</a> &gt; ' . Vocabulary::site_name . ' ' . Vocabulary::action_search . '</div><div class="blank" align="left"><span class="text_large"><img src="' . CDN_IMG . 'ico_search.gif" class="home" align="absmiddle" />' . Vocabulary::site_name . ' ' . Vocabulary::action_search . '</span><form action="/search.php" method="get">';
     if ($stage == 2) {
         $query_return = make_single_return($query);
         echo '<input type="text" name="q" id="k_search_q" onmouseover="this.focus()" class="search" value="' . $query_return . '"/>';
     } else {
         echo '<input type="text" name="q" id="k_search_q" onmouseover="this.focus()" class="search" />';
     }
     switch ($stage) {
         case 2:
             printf("<span class=\"tip\" style=\"display: block; margin-top: 5px; margin-bottom: 10px;\">" . Vocabulary::site_name . " 搜索为你找到了 %d 条匹配“%s”的结果,耗时 %.3f 秒</span>", $count_result, make_plaintext(implode(' ', $query_verified)), $time_elapsed);
             break;
         case 3:
             echo '<span class="tip" style="display: block; margin-top: 5px; margin-bottom: 10px;">你所查询的关键字“' . implode(' ', $query_common) . '”太常见</span>';
             break;
         case 0:
         case 1:
         default:
             echo '<span class="tip" style="display: block; margin-top: 5px; margin-bottom: 10px;"></span>';
             break;
     }
     echo '<input type="image" src="' . CDN_IMG . 'graphite/search.gif" /></form></div>';
     if ($stage == 2 && $count_result > 0) {
         echo '<table width="100%" border="0" cellpadding="0" cellspacing="2" class="board">';
         if (DICT_API_ENABLED == 'yes') {
             if (preg_match('/[a-z0-9]/i', $query)) {
                 $d = new Net_Dict();
                 $d->setCache(true, 'file', array('cache_dir' => BABEL_PREFIX . '/cache/dict/'));
                 $defs_a = $d->define($query, 'web1913');
                 if (!PEAR::isError($defs_a)) {
                     if (count($defs_a) > 0) {
                         echo '<tr><td colspan="2" height="18" class="shead">&nbsp;';
                         echo format_def(mb_convert_encoding($defs_a[0]['definition'], 'UTF-8', 'GBK'));
                         if (preg_match('/^[a-zA-Z]+$/', $query)) {
                             echo '<span class="tip_i"><small> ... learn more on <a href="http://www.dict.org/bin/Dict?Form=Dict2&Database=web1913&Query=' . urlencode(strtolower($query)) . '" target="_blank" class="t">dict.org</a></small></span>';
                         }
                         echo '</td></tr>';
                     } else {
                         $this->vxSearchSubstanceSpell($query, $d);
                     }
                 } else {
                     $this->vxSearchSubstanceSpell($query, $d, 1);
                 }
             }
         }
         if ($p['total'] > 1) {
             echo '<tr><td align="left" height="30" class="hf" colspan="2" style="border-bottom: 1px solid #CCC;">';
             $this->vxDrawPages($p);
             echo '</td></tr>';
         }
         $j = 0;
         foreach ($result_b as $Result) {
             $j++;
             if ($j == 1) {
                 echo '<tr><td colspan="2" height="10"></td></tr>';
             }
             if ($Result->type == 1) {
                 $img = 'gray';
             } else {
                 if ($Result->uid == $this->User->usr_id) {
                     $dot = 'green';
                 } else {
                     $dot = 'gray';
                 }
             }
             echo '<tr><td width="24" height="18" valign="top" align="center" class="star"><img src="' . CDN_IMG . 'dot_' . $dot . '.png" /></td>';
             if ($Result->type == 1) {
                 $_target = '_blank';
             } else {
                 $_target = '_self';
             }
             echo '<td height="18" class="star"><a href="' . $Result->url . '" class="blue" target="' . $_target . '">' . make_plaintext($Result->title) . '</a>';
             if ($Result->author != '') {
                 echo ' <span class="tip_i">by</span> <a href="/u/' . urlencode($Result->author) . '">' . make_plaintext($Result->author) . '</a>';
             }
             echo '</td></tr>';
             if (strlen($Result->excerpt) > 0) {
                 echo '<tr><td width="24"></td><td class="hf"><span class="excerpt">';
                 echo $Result->excerpt;
                 echo '</span></td></tr>';
             }
             echo '<tr><td width="24"></td><td valign="top"><span class="tip"><span class="green">';
             if ($Result->type == 0) {
                 echo $_SERVER['SERVER_NAME'] . $Result->url;
             } else {
                 echo $Result->url;
             }
             echo ' - ' . date('Y年n月j日', $Result->timestamp) . '</span></td></tr>';
             echo '<tr><td colspan="2" height="10"></td></tr>';
         }
         if ($p['total'] > 1) {
             echo '<tr><td align="left" height="30" class="hf" colspan="2" style="border-top: 1px solid #CCC;">';
             $this->vxDrawPages($p);
             echo '</td></tr>';
         }
         echo '</table>';
     } else {
         if (isset($query)) {
             if (DICT_API_ENABLED == 'yes') {
                 if (preg_match('/[a-z0-9]/i', $query)) {
                     $d = new Net_Dict();
                     $d->setCache(true, 'file', array('cache_dir' => BABEL_PREFIX . '/cache/dict/'));
                     $defs_a = @$d->define($query, 'web1913');
                     if (!PEAR::isError($defs_a)) {
                         if (count($defs_a) > 0) {
                             echo '<table width="100%" border="0" cellpadding="0" cellspacing="2" class="board">';
                             echo '<tr><td class="shead">&nbsp;' . format_def(mb_convert_encoding($defs_a[0]['definition'], 'UTF-8', 'GBK'));
                             if (preg_match('/^[a-zA-Z]+$/', $query)) {
                                 echo '<span class="tip_i"><small> ... learn more on <a href="http://www.dict.org/bin/Dict?Form=Dict2&Database=web1913&Query=' . urlencode(strtolower($query)) . '" target="_blank" class="t">dict.org</a></small></span>';
                             }
                             echo '</td></tr>';
                             echo '</table>';
                         } else {
                             $this->vxSearchSubstanceSpell($query, $d, 0);
                         }
                     } else {
                         $this->vxSearchSubstanceSpell($query, $d, 0);
                     }
                 }
             }
         }
     }
     echo '</div>';
 }
 $customer->setLastName($row['l_name']);
 // Create the conversation
 $conversation = new \HelpScout\model\Conversation();
 $conversation->setSubject($row['reason'] . ': ' . $row['subject']);
 $conversation->setCreatedAt(format_api_date($row['date']));
 $conversation->setMailbox($mailbox);
 $conversation->setCustomer($customer);
 $conversation->setType('email');
 $tags = array('backlog', strtolower($row['reason']));
 if ($row['extension'] == 'Yes') {
     $tags[] = strtolower($row['extension_name']);
 }
 $conversation->setTags($tags);
 // A conversation must have at least one thread
 $thread = new \HelpScout\model\thread\Customer();
 $thread->setCreatedAt(format_api_date($row['date']));
 $body = '<strong>Website: </strong>' . $row['website'] . "\r\n";
 if ($row['extension'] == 'Yes') {
     $body .= '<strong>Extension: </strong>' . $row['extension_name'] . "\r\n";
 }
 $body .= "\r\n\r\n" . $row['body'];
 $thread->setBody($body);
 $thread->setStatus('active');
 // Add attachments if they exist.
 if (!empty($attachments)) {
     $thread->setAttachments($attachments);
 }
 // Create by: required
 $thread->setCreatedBy($customer);
 $conversation->setThreads(array($thread));
 $conversation->setCreatedBy($customer);