docker dev update
[openemr.git] / library / classes / RXList.class.php
blobf1c07c79429eda331aacd986df89f58dd15d05bf
1 <?php
2 // $Id$
3 // $Author$
4 //
5 // $Log$
7 // Revision 1.5 2016/02/016 sherwin gaddis
8 // fix broken url
10 // Revision 1.4 2008/05/09 20:10:28 cfapress
11 // Changes to handle to HTML returned by rxlist.com
13 // Revision 1.3 2007/06/19 02:34:45 sunsetsystems
14 // drug lookup fix from Sam Rajan
16 // Revision 1.2 2005/12/27 00:45:19 sunsetsystems
17 // fix broken url
19 // Revision 1.1.1.3 2005/06/23 05:25:49 drbowen
20 // Initial import.
22 // Revision 1.1.1.2 2005/03/28 00:44:54 drbowen
23 // Initial import.
25 // Revision 1.1.1.1 2005/03/09 19:59:43 wpennington
26 // First import of OpenEMR
28 // Revision 1.2 2002/08/06 13:49:08 rufustfirefly
29 // updated rxlist class for changes in RxList.com
31 // Revision 1.1 2002/07/08 14:42:25 rufustfirefly
32 // RxList.com prescription "module" for formulary
35 if (!defined('__CLASS_RXLIST_PHP__')) {
36 define('__CLASS_RXLIST_PHP__', true);
38 class RxList
41 function getPage($query)
43 $url = "http://www.rxlist.com/script/main/srchcont_rxlist.asp?src=".
44 //$url = "http://www.rxlist.com/cgi/rxlist.cgi?drug=".
45 // $url = "http://129.250.146.18/cgi/rxlist.cgi?drug=".
46 urlencode($query);
48 if (!($fp = fopen($url, "r"))) {
49 // If we fail to get the page, return false
50 return false;
51 } else {
52 // Get the page
53 $buffer = 0;
54 while (!feof($fp)) {
55 $buffer .= fgets($fp, 4096);
58 fclose($fp);
59 return $buffer;
60 } // end checking for successful open
61 } // end function RxList::getPage
63 function get_list($query)
65 $page = RxList::getPage($query);
66 $tokens = RxList::parse2tokens($page);
67 $hash = RxList::tokens2hash($tokens);
68 foreach ($hash as $index => $data) {
69 unset($my_data);
70 foreach ($data as $k => $v) {
71 $my_data[$k] = $v;
74 $list[trim($my_data[brand_name])." (".trim($my_data[generic_name]).")"] =
75 trim($my_data[brand_name]);
78 return $list;
79 } // end function RxList::get_list
81 /* break the web page into a collection of TAGS
82 * such as <input ..> or <img ... >
84 function parse2tokens($page)
86 $pos = 0;
87 $token = 0;
88 unset($tokens);
89 $in_token = false;
90 while ($pos < strlen($page)) {
91 switch (substr($page, $pos, 1)) {
92 case "<":
93 if ($in_token) {
94 $token++;
95 $in_token = false;
98 $tokens[$token] .= substr($page, $pos, 1);
99 $in_token = true;
100 break;
102 case ">":
103 $tokens[$token] .= substr($page, $pos, 1);
104 $in_token = false;
105 $token++;
106 break;
108 default:
109 $tokens[$token] .= substr($page, $pos, 1);
110 $in_token = false;
111 break;
112 } // end decide what to do
113 $pos++;
114 } // end looping through string
115 return $tokens;
116 } // end function RxList::parse2tokens
119 /* WTF does this crap do? */
120 function tokens2hash($tokens)
122 $record = false;
123 $current = 0;
124 unset($hash);
125 unset($all);
126 for ($pos=0; $pos<count($tokens); $pos++) {
127 if (!(strpos($tokens[$pos], "Brand Name") === false)) {
128 // found a brand line 'token'
129 $type = "brand_name";
130 $record = $pos;
131 $ending = "</a>";
134 if (!(strpos($tokens[$pos], "Generic Name") === false)) {
135 // found a generic line 'token'
136 $type = "generic_name";
137 //print "generic_name record start at $pos<BR>\n";
138 $ending = "</a>";
139 $record = $pos;
142 if (!(strpos($tokens[$pos], "Drug Class") === false)) {
143 // found a drug-class 'token'
144 $type = "drug_class";
145 //print "drug_class record start at $pos<BR>\n";
146 $ending = "</font>";
147 $record = $pos;
150 // Handle the ending (assume when all fields are set
151 // that we're done)
153 // May 2008 - RXList doesn't always return all three types of 'tokens'
154 // for a drug, so I replaced the 'and's with 'or's -- JRM
155 //if (isset($hash["drug_class"]) and isset($hash["brand_name"]) and isset($hash["generic_name"])) {
156 if (isset($hash["drug_class"]) or isset($hash["brand_name"]) or isset($hash["generic_name"])) {
157 // Reset record
158 $record = false;
159 // Add hash to all
160 $all[] = $hash;
161 // Unset hash and type
162 unset($hash);
163 $type = "";
164 $ending = "";
167 if ((($pos == ($record + 1)) or
168 ($pos == ($record + 2)) or
169 ($pos == ($record + 3)))
170 and ($ending != "")) {
171 //print "tokens[$pos] = ".htmlentities($tokens[$pos])."<BR>\n";
172 if ((!(strpos(strtoupper($tokens[$pos]), "</A>") === false)) or
173 (!(strpos(strtoupper($tokens[$pos]), "</FONT>") === false)) or
174 (!(strpos(strtoupper($tokens[$pos]), "</TD>") === false))) {
175 // Find where anchor is
176 $my_pos = strpos(strtoupper($tokens[$pos]), "<");
177 $hash[$type] = substr($tokens[$pos], 0, $my_pos);
178 $hash[$type] = str_replace("&amp;", "&", $hash[$type]);
179 //print "hash[$type] = ".htmlentities($hash[$type])."<BR>\n";
180 $type = "";
181 $ending = "";
184 } // end looping
185 return $all;
186 } // end function RxList::tokens2hash
187 } // end class RxList
189 } // end if not defined
191 // TEST CRAP HERE
192 //$page = RxList::getPage("http://129.250.146.18/cgi/rxlist.cgi?drug=lipitor");
193 $page = RxList::getPage("http://www.rxlist.com/cgi/rxlist.cgi?drug=lipitor");
194 $tokens = RxList::parse2tokens($page);
195 $hash = RxList::tokens2hash($tokens);
196 foreach ($hash AS $k => $v) {
197 print "<UL>k = ".htmlentities($k)." \n";
198 foreach ($v AS $key => $value) {
199 print "<LI>$key = $value\n";
201 print "</UL>\n";
204 print "<FORM>\n";
205 print html_form::select_widget("test", RxList::get_list($drug));
206 print "</FORM>\n";