Fix for issue #291
[openemr.git] / library / classes / RXList.class.php
blob4bbf769c1659a28ca1f5a79a15ed0e9b9bc3b74c
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 {
40 function getPage ( $query ) {
41 $url = "http://www.rxlist.com/script/main/srchcont_rxlist.asp?src=".
42 //$url = "http://www.rxlist.com/cgi/rxlist.cgi?drug=".
43 // $url = "http://129.250.146.18/cgi/rxlist.cgi?drug=".
44 urlencode($query);
46 if (!($fp = fopen($url, "r"))) {
47 // If we fail to get the page, return false
48 return false;
49 } else {
50 // Get the page
51 $buffer = 0;
52 while (!feof($fp)) {
53 $buffer .= fgets($fp, 4096);
55 fclose ($fp);
56 return $buffer;
57 } // end checking for successful open
58 } // end function RxList::getPage
60 function get_list ( $query ) {
61 $page = RxList::getPage($query);
62 $tokens = RxList::parse2tokens($page);
63 $hash = RxList::tokens2hash($tokens);
64 foreach ($hash AS $index => $data) {
65 unset($my_data);
66 foreach ($data AS $k => $v) {
67 $my_data[$k] = $v;
69 $list[trim($my_data[brand_name])." (".trim($my_data[generic_name]).")"] =
70 trim($my_data[brand_name]);
72 return $list;
73 } // end function RxList::get_list
75 /* break the web page into a collection of TAGS
76 * such as <input ..> or <img ... >
78 function parse2tokens( $page ) {
79 $pos = 0; $token = 0; unset ($tokens);
80 $in_token = false;
81 while ($pos < strlen($page)) {
82 switch(substr($page, $pos, 1)) {
83 case "<":
84 if ($in_token) {
85 $token++;
86 $in_token = false;
88 $tokens[$token] .= substr($page,$pos,1);
89 $in_token = true;
90 break;
92 case ">":
93 $tokens[$token] .= substr($page,$pos,1);
94 $in_token = false;
95 $token++;
96 break;
98 default:
99 $tokens[$token] .= substr($page,$pos,1);
100 $in_token = false;
101 break;
103 } // end decide what to do
104 $pos++;
105 } // end looping through string
106 return $tokens;
107 } // end function RxList::parse2tokens
110 /* WTF does this crap do? */
111 function tokens2hash ( $tokens ) {
112 $record = false; $current = 0; unset($hash); unset($all);
113 for ($pos=0; $pos<count($tokens); $pos++) {
115 if (!(strpos($tokens[$pos], "Brand Name") === false)){
116 // found a brand line 'token'
117 $type = "brand_name";
118 $record = $pos;
119 $ending = "</a>";
122 if (!(strpos($tokens[$pos], "Generic Name") === false)){
123 // found a generic line 'token'
124 $type = "generic_name";
125 //print "generic_name record start at $pos<BR>\n";
126 $ending = "</a>";
127 $record = $pos;
130 if (!(strpos($tokens[$pos], "Drug Class") === false)){
131 // found a drug-class 'token'
132 $type = "drug_class";
133 //print "drug_class record start at $pos<BR>\n";
134 $ending = "</font>";
135 $record = $pos;
138 // Handle the ending (assume when all fields are set
139 // that we're done)
141 // May 2008 - RXList doesn't always return all three types of 'tokens'
142 // for a drug, so I replaced the 'and's with 'or's -- JRM
143 //if (isset($hash["drug_class"]) and isset($hash["brand_name"]) and isset($hash["generic_name"])) {
144 if (isset($hash["drug_class"]) or isset($hash["brand_name"]) or isset($hash["generic_name"])) {
145 // Reset record
146 $record = false;
147 // Add hash to all
148 $all[] = $hash;
149 // Unset hash and type
150 unset($hash); $type = ""; $ending = "";
153 if ((($pos == ($record + 1)) or
154 ($pos == ($record + 2)) or
155 ($pos == ($record + 3)))
156 and ($ending != ""))
158 //print "tokens[$pos] = ".htmlentities($tokens[$pos])."<BR>\n";
159 if ((!(strpos(strtoupper($tokens[$pos]), "</A>" ) === false)) or
160 (!(strpos(strtoupper($tokens[$pos]), "</FONT>") === false)) or
161 (!(strpos(strtoupper($tokens[$pos]), "</TD>" ) === false)))
163 // Find where anchor is
164 $my_pos = strpos(strtoupper($tokens[$pos]), "<");
165 $hash[$type] = substr($tokens[$pos], 0, $my_pos);
166 $hash[$type] = str_replace("&amp;", "&", $hash[$type]);
167 //print "hash[$type] = ".htmlentities($hash[$type])."<BR>\n";
168 $type = ""; $ending = "";
171 } // end looping
172 return $all;
173 } // end function RxList::tokens2hash
175 } // end class RxList
177 } // end if not defined
179 // TEST CRAP HERE
180 //$page = RxList::getPage("http://129.250.146.18/cgi/rxlist.cgi?drug=lipitor");
181 $page = RxList::getPage("http://www.rxlist.com/cgi/rxlist.cgi?drug=lipitor");
182 $tokens = RxList::parse2tokens($page);
183 $hash = RxList::tokens2hash($tokens);
184 foreach ($hash AS $k => $v) {
185 print "<UL>k = ".htmlentities($k)." \n";
186 foreach ($v AS $key => $value) {
187 print "<LI>$key = $value\n";
189 print "</UL>\n";
192 print "<FORM>\n";
193 print html_form::select_widget("test", RxList::get_list($drug));
194 print "</FORM>\n";