File: phpquote.class

package info (click to toggle)
myphpmoney 1.3RC3-13
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,160 kB
  • ctags: 884
  • sloc: php: 18,081; pascal: 2,106; sh: 327; xml: 239; makefile: 137; sql: 130
file content (233 lines) | stat: -rw-r--r-- 7,074 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
 * $Id: phpquote.class,v 1.10 2003/04/02 12:16:54 courou Exp $
 *
 * Author     : tobozo&#64;madchat.org
 * Modified   : courou&#64;users.sourceforge.net
 * Website    : http://allreponse.ath.cx
 *
 * Support    : http://sourceforge.net/projects/myphpmoney/
 * CVS        : http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/myphpmoney/
 *
 * PHPQUOTE
 *
 * Copyright 2001, Stephen C. Cook and Gear21.Inc.
 * See Standard GNU GPL License for Details.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * The Latest Version of PHPQUOTE can be downloaded from:
 * http://www.gear21.com/phpquote/
 *
 * Contact:  scook&#64;gear21.com
 * TODO: Multiple Quotes Section, More than basic INFO, error & timeout checking.
 */

$BasicPage="http://finance.yahoo.com/q?s=<TICKER_SYMBOLS>&d=v1&o=t";
$DetailedPage="http://finance.yahoo.com/q?s=<TICKER_SYMBOLS>&d=v1&o=t";

########################################################
## Class Definitions                                  ##
########################################################

class php_quote {
  var $Symbol;
  var $Last;
  var $Price;
  var $Change;
  var $PercChange;
  var $Volume;

  // Ajout courou du 27/03/2003
  // Afin d'eviter les WARNING php_hostconnect
  function connect_quote($symb) {
   Global $nb_err,$tools,$_VAR,$_MPM;
    $r='';
    $err = sprintf($_VAR['JS_PHP_HOSTCONNECT'],$_MPM['name'],$_VAR['MESS_CALENDAR_VERIF']);
    if (defined('PHP_HOSTCONNECT') && PHP_HOSTCONNECT == 1) {
      $r = $err;
    $f=false;
    }
    else if (!$tools->verif_connect()) {
      if (defined('PHP_HOSTCONNECT') && PHP_HOSTCONNECT == 1) {
         $r = $err;
      } else {
         $r = $_VAR['JS_PARSER_XML_6'];
      }
    $f=false;
    } else {
    $f=true;
    }
    if ($r != '' && $nb_err++ == '') XHTML_DisplayJs($r);
  return $f;
  }


  function get_single_quote($symb) {
    $nb_err=0;
    $QuotePage="http://finance.yahoo.com/q?s=<TICKER_SYMBOLS>&d=v1&o=t";
    # Prepare Symbol (might break this out into it's own function
    # to add more checking and security.
    $symb=strip_tags($symb);
    $symb=substr($symb,0,10);  #Change to 7 to allow canadian stocks
    $symb=trim($symb);
    #Modification courou du 22/01/2003
    $symb=str_replace("(","",$symb);
    #$symb=urlencode($symb);
    $yf_page=eregi_replace("<TICKER_SYMBOLS>",$symb,$QuotePage);

    #NEED TO CHECK FOR CONNECTION SUCCESS AND THEN VALID SYMBOL
    #If your Warning setting is set to visible, this will display
    # a warning if the file isn't available (404 or 403).
    #Check your php.ini or php3.ini file.

    if ($this->connect_quote($symb)) $f=@file($yf_page); else $f=false;
    if($f) {
      $x=0;
      $f_size=count($f);
      $tsymb=ereg_replace("\^","\\^",$symb);

      #Modification courou du 22/01/2003
      while( !eregi("No Symbol Entered",$f[$x])
         && !eregi(">$tsymb<",$f[$x]) && $x <= $f_size ) {
        $x++;
        }
      $qline=strip_tags($f[$x]);
      $t=explode("Chart",$qline);
      $single_space=ereg_replace(" +"," ",$t[0]);
      # If the above regex doesn't work, try:
      # $single_space=ereg_replace("[[:space:]]+"," ",$t[0]);

      #Modification courou du 22/01/2003
      #Now check for Unknown Symbol
      if(eregi("No Symbol Entered",$single_space) || eregi("No such ticker symbol",$single_space)) {
        $this->Symbol=$symb;
        $this->Last="Unkown Symbol";
        $this->Price="Unkown Symbol";
        $this->Change="N/A";
        $this->PercChange="N/A";
        $this->Volume="N/A";
      return(0);
      }
      else {
        $qa=explode(" ",$single_space);
        $this->Symbol=$qa[0];

        #Now, because they use a date after hours, we have to determine
        #If it's a time, or a date...
        $tc=substr($qa[1],0,1);
           if (ereg("[0-9]",$tc)) {
             $this->Last=$qa[1];
             $x=2;
           } else {
             $this->Last="$qa[1] $qa[2]";
             $x=3;
           }
        $this->Price=$qa[$x]; $x++;
        $this->Change=$qa[$x]; $x++;
        $this->PercChange=$qa[$x]; $x++;
        $this->Volume=$qa[$x]; $x++;
       return(1);
       }
    }
    else {
      $this->Symbol=$symb;
      $this->Last="Unavailable";
      $this->Price="Unavailable";
      $this->Change="N/A";
      $this->PercChange="N/A";
      $this->Volume="N/A";
      return(0);
      }
    }
  } ## end class


// class mpm_quote by tobozo (extended cache possibility and db handle)
class MPM_quote extends php_quote {

  var $Date;
  var $Delay = 1200; ## 20 minutes

  function MPM_quote() {
   global $_MPM,$db;
    $this->db= & $db;
    if(!is_object($db)) die ("no connexion");
    ## purger la base des entres obsoletes
  $this->db->query("DELETE FROM ".$_MPM['table'][7]." WHERE (TIME + $this->Delay)<".time());
  }

  function get_quote($symb) {
    if(empty($symb)) die ("no symbol");
    if($this->load($symb)) { // is in the db
      if($this->is_up_to_date($symb)) { // is up to date
        ## echo "is up to date ";
        return 1;
      }
      else { ## is not up to date, needs reload and update
        ## echo " is not up to date, needs reload and update ";
        $this->load_quote_from_yahoo($symb);
        $this->update_quote();
        return 0;
      }
    }
    else { ## is not in the db, needs insert
      ## echo " is not in the db, needs insert ";
      $this->load_quote_from_yahoo($symb);
      $this->insert_quote();
      return -1;
      }
    }

  function load($symb) {
   Global $_MPM;
    $query = "SELECT * FROM ".$_MPM['table'][7]." WHERE QUOTENAME=\"".trim($symb)."\"";
    $this->db->query($query);
      if($this->db->num_rows()>0) {
         ## echo " is in the db, load it up ";
         $this->db->next_record();
         $this->load_quote_from_sql();
         $this->Symbol = $symb;
      return 1;
      }
      else {
         ## echo " is not in the db, get it from yahoo ";
      return 0;
      }
   }

  function load_quote_from_sql() {
    $quote_ary = explode(";", $this->db->f('CACHE'));
    $this->Last       = $quote_ary[0];
    $this->Price      = $quote_ary[1];
    $this->Change     = $quote_ary[2];
    $this->PercChange = $quote_ary[3];
    $this->Volume     = $quote_ary[4];
    $this->Date       = $this->db->f("TIME");
  return 1;
  }

  function load_quote_from_yahoo($symb) {
    $this->Date = time();
  return $this->get_single_quote($symb);
  }

  function is_up_to_date($symb) {
    return (($this->Date + $this->Delay) > time()) ;
  }

  function update_quote() {
   Global $_MPM;
     $query = "UPDATE ".$_MPM['table'][7]." SET CACHE = '$this->Last;$this->Price;$this->Change;$this->PercChange;$this->Volume'
               WHERE QUOTENAME = '$this->Symbol'";
  $this->db->query($query);
  }

  function insert_quote() {
   Global $_MPM;
     if(ltrim(chop($this->Symbol!=""))) {
       $query = "INSERT INTO ".$_MPM['table'][7]." (QUOTENAME, TIME, CACHE)
                 VALUES ('$this->Symbol',".time().",'$this->Last;$this->Price;$this->Change;$this->PercChange;$this->Volume')";
     $this->db->query($query);
     }
  }
} ## end extended class