<?php

/**
 *
 * INDEXU
 * Copyright(C), Nicecoder, 2000-2006, All Rights Reserved.
 *
 * INDEXU(tm) is protected by Indonesia and International copyright laws.
 * Unauthorized use or distribution of INDEXU(tm) is strictly prohibited,
 * violators will be prosecuted. To obtain a license for using INDEXU(tm),
 * please register at Nicecoder home page at http://www.nicecoder.com
 *
 * Author:
 *    Dody Rachmat Wicaksono (dody@nicecoder.com)
 *    M. Zuber (zubby@nicecoder.com)
 *
 */

  /*===================================================
    main
  ===================================================*/

  include "application.php";

  RemoveFilter("gzip_compression");
  RemoveFilter("runtime");
  RemoveFilter("start_session");
  RunPreFilter(__FILE__);

  header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  header ("Cache-Control: no-cache, must-revalidate");
  header ("Pragma: no-cache");

  if ($cmd == 'fetch_meta') {
    if (!preg_match('|^http(s)?://[\w-]+\.[\w-]+(\S+)?$|i', $url)) {
      RunPostFilter(__FILE__);
    }
    $urls = array($url, 'http://www.alexa.com/data/details/main?url='.$url);
    $contents = HttpMultiRequest($urls);
    preg_match('/<title>([^>]*)<\/title>/si', $contents[0]['html'], $match);
    $info['title'] = trim(strip_tags($match[1]));
    preg_match_all('/<[\s]*meta[\s]*name="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents[0]['html'], $match);
    for ($i = 0; $i < count($match[1]); $i++) {
      if (strtolower($match[1][$i]) == "keywords") {
        $info['keywords'] = trim(strip_tags($match[2][$i]));
      }
      if (strtolower($match[1][$i]) == "description") {
        $info['description'] = trim(strip_tags($match[2][$i]));
      }
    }
    preg_match('|<br>([^>]*)\[at\]([^>]*)<p></p>|', $contents[1]['html'], $match);
    $info['email'] = str_replace(' [at] ', '@', trim(strip_tags($match[0])));
    $response = JSEncode($info);
    echo $response;
  }

  if ($cmd == 'get_fields') {
    include "key.php";
    if ($hash != '') {
      RunPostFilter(__FILE__);
    }
    $fields['N/A'] = "N/A";
    if ($tbl) {
      $query = "show fields from $tbl";
      $result_field = @$dbConn->Execute($query);
      $fields = array();
      while ($row_field = $result_field->FetchRow($result_field))  {
        $fields[$row_field['Field']] = $row_field['Field'];
      }
    }
    echo JSEncode($fields);
  }

  RunPostFilter(__FILE__);

?>