<?PHP
 
/**
 * Class:             yahoo.stocks.class.php
 * Created and Modified by Shivangi C
 * -----------------------------------------------------------------------------
 * Configuration options
 * 
 * $time                - Difference in time between you and the yahoo Server (GMT)
 * $refreshtime            - How often a value may (at most) be checked for on the yahoo website (in minutes)
 *                           (Minimal value is 0,25 because yahoo updates the stocks approx every 15sec)
 * The following values are only necessary if the cache functionality is used
 * $dbhost                - mySQL database host
 * $user                - mySQL username
 * $passwd                - mySQL password
 * $db                    - mySQL database
 * $stocks_table        - mySQL table
 * 
 * -----------------------------------------------------------------------------

 *
 * Some tock keywords:
 * ^DJI         - Dow Jones
 * ^IXIC         - Nasdaq
 * ^GSPC         - S&P 500
 * ^STOXX50E     - Euro STOXX50
 * For more stock keywords visit http://finance.yahoo.com
 * -----------------------------------------------------------------------------
 */

class yahoo_stocks {
    /**
     * Time difference Between Yahoo and your local time;
     */
    //var $time = "+6"; //ATTENTION: daylight saving isn't (yet) respected
    //var $refreshtime = "5"; // 0,25 because yahoo updates their values approx every 15sec!
    var $dbhost = "software-ece.rutgers.edu";
    var $user = "group804";
    var $passwd = "EE#rt18";
    var $db = "group804";
    var $stocks_table = "stocks";
	var $Day_Prices = "DayPrice";
	var $Month_Prices= "MonthPrice";


    /**
     * how long should quote values be stored in mysql (0 = unlimited / otherwise time in days)
     */
    //var $keepfilesfor = 1;
   

    /**
     * yahoo_stocks :: generate_stock_array() 
     * :: ATTENTION - IF THE CACHE IS USED  - calling this
     * :: function will need an open mySQL connection to be present!
     * :: use my_connect() first
     * 
     * @param  $stock Stock shortcut
     * @param  $cache (y or n)
     * @return array ()
     */
    
  
    /**
     * yahoo_stocks::generate_stock_array() 
     * 
     * :: please don't use this function - Use generate_stock_array() instead
     * :: this function is for internal use in generate_stock_array()
     * 
     * @param  $stock 
     * @return 
     */
    function generate_stock_array($stock)
    {
		
        $open = fopen("http://ichart.yahoo.com/table.csv?s=$stock&d=04&e=9&f=2008&g=d&a=07&b=19&c=2004&ignore=.csv", "r");
        
        $read = str_replace('"', '', trim(fread($open, 2000000)));

        $data = explode(",", $read);
        fclose($open);
		
		$i=0;
		for ($cnt=0; $cnt<60; $cnt++)
		{
			
			$i=$i+6;
			print($i);print(" ");print($stock); print(" ");
			
			$newdata = explode("\n",$data[$i]);
			
			
			$strtotime_date = strtotime("$newdata[1]");
			
			if (eregi("^-.*", $this->time)) {
				$this->time = str_replace("-", "", $this->time);
				$return['unixtime'] = $strtotime_date - $this->time * 3600;
			} else {
				$this->time = str_replace("+", "", $this->time);
				$return['unixtime'] = $strtotime_date + $this->time * 3600;
			}
			
			$return['stock'] = $stock;
			$return['volume'] = $data[$i+6];
			$return['date'] = date("Y.n.j", $return['unixtime']);
			
			$return['close'] = $data[$i+4];
						
			$return['open'] = $data[$i+1];
			$return['intra_top'] = $data[$i+2];
			$return['intra_down'] = $data[$i+3];
			$return['md5'] = md5($read);
        	$sql4 = "INSERT IGNORE INTO `$this->Month_Prices` VALUES('${return['stock']}', '${return['date']}','${return['close']}', '${return['open']}', '${return['intra_top']}', '${return['intra_down']}') ";
			print($sql4);
			print("\n");
			$d = mysql_query($sql4);

		} 
    }


    function my_connect()
    {
        $this->conn = @mysql_connect($this->dbhost, $this->user, $this->passwd) or die("Can't connect to mySQLn");
        return mysql_select_db($this->db, $this->conn);
    } 

    /**
     * yahoo_stocks :: my_close() :: Disconnect from the mySQL server
     * 
     * @return TRUE ;)
     */
    function my_close()
    {
        return @mysql_close();
    } 
} 

//instantiate the class

$stocks = new yahoo_stocks();
$stocks->my_connect();//if you have no open mySQL connection (yet)
$stocks->generate_stock_array("^STOXX50E"); //Get data for Euro StoXX 50 and use cache (returns an array)
$stocks->generate_stock_array("GOOG");
$stocks->generate_stock_array("MSFT");
$stocks->generate_stock_array("MO");
$stocks->generate_stock_array("AAPL");
$stocks->generate_stock_array("AIG");
$stocks->generate_stock_array("BSC");
$stocks->generate_stock_array("AMZN");
$stocks->generate_stock_array("AMD");
$stocks->generate_stock_array("T");
$stocks->generate_stock_array("ADBE");
$stocks->generate_stock_array("ANF");
$stocks->generate_stock_array("GS");
$stocks->generate_stock_array("BARC.L");
$stocks->generate_stock_array("BKS");
$stocks->generate_stock_array("HPQ");
$stocks->generate_stock_array("MER");
$stocks->generate_stock_array("LMT");
$stocks->generate_stock_array("HON");
$stocks->generate_stock_array("TWX");
$stocks->generate_stock_array("GT");
$stocks->generate_stock_array("NYT");
$stocks->generate_stock_array("TXN");
$stocks->generate_stock_array("NATI");
$stocks->generate_stock_array("QCOM");
$stocks->generate_stock_array("AVR.F");
$stocks->generate_stock_array("LEH");
$stocks->generate_stock_array("VZ");
$stocks->generate_stock_array("VOD");
$stocks->generate_stock_array("S");
$stocks->generate_stock_array("IBM");
$stocks->generate_stock_array("YHOO");
$stocks->generate_stock_array("DIS");
$stocks->generate_stock_array("WMT");
$stocks->generate_stock_array("WAG");
$stocks->generate_stock_array("SI");
$stocks->generate_stock_array("BLK");
$stocks->generate_stock_array("XLNX");
$stocks->generate_stock_array("ADI");
$stocks->generate_stock_array("BAC");
$stocks->generate_stock_array("INTC");
$stocks->generate_stock_array("WB");
$stocks->generate_stock_array("LHL.F");
$stocks->generate_stock_array("TOA.F");
$stocks->generate_stock_array("DELL");
$stocks->generate_stock_array("TTM");
$stocks->generate_stock_array("INFY");
$stocks->generate_stock_array("TCS.NS");
$stocks->generate_stock_array("CTSH");
$stocks->generate_stock_array("ACN");
$stocks->generate_stock_array("CSCO");
$stocks->generate_stock_array("DFG");
$stocks->generate_stock_array("JPM");
$stocks->generate_stock_array("SNE");
$stocks->generate_stock_array("PHG");
$stocks->generate_stock_array("JNJ");
$stocks->generate_stock_array("SLB");
$stocks->generate_stock_array("C");
$stocks->generate_stock_array("DB");
$stocks->generate_stock_array("BSC");
//$dowjones = $stocks->generate_stock_array("^DJI");//Dow Jones and no cacheing
$stocks->generate_stock_array("UBS");
//echo var_dump($stocks->generate_stock_array("^DJI"));//displays the kind of array this class returns
$stocks->my_close();

?> 