minor bug fix
[openemr.git] / library / classes / RXList.class.php
blob2895504cbe78da47db6f9fcb95ab52afc9384772
1 <?php
2 // $Id$
3 // $Author$
4 //
5 // $Log$
6 // Revision 1.4 2008/05/09 20:10:28 cfapress
7 // Changes to handle to HTML returned by rxlist.com
8 //
9 // Revision 1.3 2007/06/19 02:34:45 sunsetsystems
10 // drug lookup fix from Sam Rajan
12 // Revision 1.2 2005/12/27 00:45:19 sunsetsystems
13 // fix broken url
15 // Revision 1.1.1.3 2005/06/23 05:25:49 drbowen
16 // Initial import.
18 // Revision 1.1.1.2 2005/03/28 00:44:54 drbowen
19 // Initial import.
21 // Revision 1.1.1.1 2005/03/09 19:59:43 wpennington
22 // First import of OpenEMR
24 // Revision 1.2 2002/08/06 13:49:08 rufustfirefly
25 // updated rxlist class for changes in RxList.com
27 // Revision 1.1 2002/07/08 14:42:25 rufustfirefly
28 // RxList.com prescription "module" for formulary
31 if (!defined('__CLASS_RXLIST_PHP__')) {
32 define('__CLASS_RXLIST_PHP__', true);
34 class RxList {
36 function getPage ( $query ) {
37 $url = "http://www.rxlist.com/cgi/rxlist.cgi?drug=".
38 // $url = "http://129.250.146.18/cgi/rxlist.cgi?drug=".
39 urlencode($query);
41 if (!($fp = fopen($url, "r"))) {
42 // If we fail to get the page, return false
43 return false;
44 } else {
45 // Get the page
46 $buffer = 0;
47 while (!feof($fp)) {
48 $buffer .= fgets($fp, 4096);
50 fclose ($fp);
51 return $buffer;
52 } // end checking for successful open
53 } // end function RxList::getPage
55 function get_list ( $query ) {
56 $page = RxList::getPage($query);
57 $tokens = RxList::parse2tokens($page);
58 $hash = RxList::tokens2hash($tokens);
59 foreach ($hash AS $index => $data) {
60 unset($my_data);
61 foreach ($data AS $k => $v) {
62 $my_data[$k] = $v;
64 $list[trim($my_data[brand_name])." (".trim($my_data[generic_name]).")"] =
65 trim($my_data[brand_name]);
67 return $list;
68 } // end function RxList::get_list
70 /* break the web page into a collection of TAGS
71 * such as <input ..> or <img ... >
73 function parse2tokens( $page ) {
74 $pos = 0; $token = 0; unset ($tokens);
75 $in_token = false;
76 while ($pos < strlen($page)) {
77 switch(substr($page, $pos, 1)) {
78 case "<":
79 if ($in_token) {
80 $token++;
81 $in_token = false;
83 $tokens[$token] .= substr($page,$pos,1);
84 $in_token = true;
85 break;
87 case ">":
88 $tokens[$token] .= substr($page,$pos,1);
89 $in_token = false;
90 $token++;
91 break;
93 default:
94 $tokens[$token] .= substr($page,$pos,1);
95 $in_token = false;
96 break;
98 } // end decide what to do
99 $pos++;
100 } // end looping through string
101 return $tokens;
102 } // end function RxList::parse2tokens
105 /* WTF does this crap do? */
106 function tokens2hash ( $tokens ) {
107 $record = false; $current = 0; unset($hash); unset($all);
108 for ($pos=0; $pos<count($tokens); $pos++) {
110 if (!(strpos($tokens[$pos], "Brand Name") === false)){
111 // found a brand line 'token'
112 $type = "brand_name";
113 $record = $pos;
114 $ending = "</a>";
117 if (!(strpos($tokens[$pos], "Generic Name") === false)){
118 // found a generic line 'token'
119 $type = "generic_name";
120 //print "generic_name record start at $pos<BR>\n";
121 $ending = "</a>";
122 $record = $pos;
125 if (!(strpos($tokens[$pos], "Drug Class") === false)){
126 // found a drug-class 'token'
127 $type = "drug_class";
128 //print "drug_class record start at $pos<BR>\n";
129 $ending = "</font>";
130 $record = $pos;
133 // Handle the ending (assume when all fields are set
134 // that we're done)
136 // May 2008 - RXList doesn't always return all three types of 'tokens'
137 // for a drug, so I replaced the 'and's with 'or's -- JRM
138 //if (isset($hash["drug_class"]) and isset($hash["brand_name"]) and isset($hash["generic_name"])) {
139 if (isset($hash["drug_class"]) or isset($hash["brand_name"]) or isset($hash["generic_name"])) {
140 // Reset record
141 $record = false;
142 // Add hash to all
143 $all[] = $hash;
144 // Unset hash and type
145 unset($hash); $type = ""; $ending = "";
148 if ((($pos == ($record + 1)) or
149 ($pos == ($record + 2)) or
150 ($pos == ($record + 3)))
151 and ($ending != ""))
153 //print "tokens[$pos] = ".htmlentities($tokens[$pos])."<BR>\n";
154 if ((!(strpos(strtoupper($tokens[$pos]), "</A>" ) === false)) or
155 (!(strpos(strtoupper($tokens[$pos]), "</FONT>") === false)) or
156 (!(strpos(strtoupper($tokens[$pos]), "</TD>" ) === false)))
158 // Find where anchor is
159 $my_pos = strpos(strtoupper($tokens[$pos]), "<");
160 $hash[$type] = substr($tokens[$pos], 0, $my_pos);
161 $hash[$type] = str_replace("&amp;", "&", $hash[$type]);
162 //print "hash[$type] = ".htmlentities($hash[$type])."<BR>\n";
163 $type = ""; $ending = "";
166 } // end looping
167 return $all;
168 } // end function RxList::tokens2hash
170 } // end class RxList
172 } // end if not defined
174 // TEST CRAP HERE
175 //$page = RxList::getPage("http://129.250.146.18/cgi/rxlist.cgi?drug=lipitor");
176 $page = RxList::getPage("http://www.rxlist.com/cgi/rxlist.cgi?drug=lipitor");
177 $tokens = RxList::parse2tokens($page);
178 $hash = RxList::tokens2hash($tokens);
179 foreach ($hash AS $k => $v) {
180 print "<UL>k = ".htmlentities($k)." \n";
181 foreach ($v AS $key => $value) {
182 print "<LI>$key = $value\n";
184 print "</UL>\n";
187 print "<FORM>\n";
188 print html_form::select_widget("test", RxList::get_list($drug));
189 print "</FORM>\n";