// Written by: Prem Kumar Singh

<?PHP 

class yahoo_stocks {

    

    var $time = "+3"; //ATTENTION: daylight saving isn't (yet) respected

    var $refreshtime = "5"; // 0,25 because yahoo updates their values approx every 15sec!

    var $dbhost = "localhost";

    var $user = "group803";

    var $passwd = "SE#287";

    var $db = "group803";

    var $stocks_table = "STOCKS";

    var $keepfilesfor = 0;

    

   function get_stocks($stock)

    {

	    $mystock = mysql_real_escape_string($stock);

           
	    $q = "SELECT * FROM `$this->stocks_table` WHERE stock='$mystock' ";

            print("Funtion for :");
	    print($mystock);
	    print("  called");
	    print("\n");           


            //$mystock = mysql_real_escape_string($stock);

     	    $return = $this->generate_stock_array($stock);

		
		
                    $sql = "INSERT INTO `$this->stocks_table` (`stock` , `value` , `changepoints` , `open` , `intra_top` , `intra_down` , `date` , `time` )";

                    $sql .= "VALUES('${return['stock']}','${return['value']}','${return['changepoints']}','${return['open']}','${return['intra_top']}','${return['intra_down']}',";

                    $sql .= "'${return['date']}','${return['time']}')";

                    mysql_query($sql);

                    return $return;
 

   
      

    } 



    /**

     * yahoo_stocks::generate_stock_array() 

     * 

     * :: please don't use this function - Use get_stocks() instead

     * :: this function is for internal use in get_stocks()

     * 

     * @param  $stock 

     * @return 

     */

    function generate_stock_array($stock)

    {

        $open = fopen("http://finance.yahoo.com/d/quotes.csv?s=$stock&f=sl1d1t1c1ohgv&e=.csv", "r");

        $read = str_replace('"', '', trim(fread($open, 2000)));



        $data = explode(",", $read);

        fclose($open);


	/**

         * Converting yahoo's date to local date

         */

        $strtotime_date = strtotime(str_replace('"', '', "$data[2] $data[3]"));

        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'] = $data[0];

        $return['value'] = $data[1];

        $return['date'] = date("j.n.Y", $return['unixtime']);

        $return['time'] = date("G:i", $return['unixtime']);

        $return['changepoints'] = $data[4];

        $return['open'] = $data[5];

        $return['intra_top'] = $data[6];

        $return['intra_down'] = $data[7];

        $return['md5'] = md5($read);


        return $return;

    } 



/*
    function get_stock_bundle($syms)

    {

        foreach ($syms as $s) {

            $bundle[$s] = $this->generate_stock_array($s);

        } 



        return $bundle;

    } 

*/

       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);

    } 




    function my_close()

    {

        return @mysql_close();

    } 

} 





$period=390;  //total period for data collection in minutes
$interval=15*60;  //time between each query in seconds
$stocks = new yahoo_stocks();

$stocks->my_connect();//if you have no open mySQL connection (yet)
$foo = time()/60;
//print("Start time in min:\n")
//print $foo;
while(time()/60-$foo<$period)
{
$apple = $stocks->get_stocks("AAPL");

$cisco = $stocks->get_stocks("CSCO");

$msft = $stocks->get_stocks("MSFT");

$intel = $stocks->get_stocks("INTC");

$nokia = $stocks->get_stocks("NOK");

$oracle = $stocks->get_stocks("ORCL");

$dell = $stocks->get_stocks("DELL");

$canon = $stocks->get_stocks("CAJ");

$texasinst = $stocks->get_stocks("TXN");

$EMC = $stocks->get_stocks("EMC");

$motorola = $stocks->get_stocks("MOT");

$AMD = $stocks->get_stocks("AMD");

$johnjohn = $stocks->get_stocks("JNJ");

$pfizer = $stocks->get_stocks("PFE");

$glaxo = $stocks->get_stocks("GSK");

$novartis = $stocks->get_stocks("NVS");

$merck = $stocks->get_stocks("MRK");

$astra = $stocks->get_stocks("AZN");

$northrop = $stocks->get_stocks("NOC");

$amgen = $stocks->get_stocks("AMGN");

$sybase = $stocks->get_stocks("SY");

$abbott = $stocks->get_stocks("ABT");

$wyeth = $stocks->get_stocks("WYE");

$genentech = $stocks->get_stocks("DNA");

$alcon = $stocks->get_stocks("ACL");

$citigroup = $stocks->get_stocks("C");

$bofamerica = $stocks->get_stocks("BAC");

$hsbc = $stocks->get_stocks("HBC");

$berkshire = $stocks->get_stocks("BRK-A");

$agilent = $stocks->get_stocks("A");

$jpmorgan = $stocks->get_stocks("JPM");

$allianz = $stocks->get_stocks("AZ");

$wachovia = $stocks->get_stocks("WB");

$clic = $stocks->get_stocks("LFC");

$goldman = $stocks->get_stocks("GS");

$barclays = $stocks->get_stocks("BCS");

$axa = $stocks->get_stocks("AXA");

$exxon = $stocks->get_stocks("XOM");

$gamestop = $stocks->get_stocks("GME");

$chevron = $stocks->get_stocks("CVX");

$boeing = $stocks->get_stocks("B");

$opc = $stocks->get_stocks("OXY");

$impoil = $stocks->get_stocks("IMO");

$chinamobile = $stocks->get_stocks("CHL");

$atnt = $stocks->get_stocks("T");

$vodafone = $stocks->get_stocks("VOD");

$google = $stocks->get_stocks("GOOG");

$walmart = $stocks->get_stocks("WMT");

$verizon = $stocks->get_stocks("VZ");

$akamai = $stocks->get_stocks("AKAM");


//echo var_dump($stocks->get_stocks("^DJI"));//displays the kind of array this class returns
sleep($interval);
}
print(time()/60);

$stocks->my_close();



?> 