some portal work
[openemr.git] / library / classes / RXList.class.php
blobd5e9f1cd92928882a6061935829ea129a004fe97
1 <?php
2 /**
3 * RXList.class.php
5 * Revision 1.6 2019/02/18 avanss llc
6 * replaced rxlist API with rxnav.nlm.nih.gov
8 * Revision 1.5 2016/02/016 sherwin gaddis
9 * fix broken url
11 * Revision 1.4 2008/05/09 20:10:28 cfapress
12 * Changes to handle to HTML returned by rxlist.com
14 * Revision 1.3 2007/06/19 02:34:45 sunsetsystems
15 * drug lookup fix from Sam Rajan
17 * Revision 1.2 2005/12/27 00:45:19 sunsetsystems
18 * fix broken url
20 * Revision 1.1.1.3 2005/06/23 05:25:49 drbowen
21 * Initial import.
23 * Revision 1.1.1.2 2005/03/28 00:44:54 drbowen
24 * Initial import.
26 * Revision 1.1.1.1 2005/03/09 19:59:43 wpennington
27 * First import of OpenEMR
29 * Revision 1.2 2002/08/06 13:49:08 rufustfirefly
30 * updated rxlist class for changes in RxList.com
32 * Revision 1.1 2002/07/08 14:42:25 rufustfirefly
33 * RxList.com prescription "module" for formulary
35 * @package OpenEMR
36 * @link https://www.open-emr.org
37 * @author rufustfirefly
38 * @author wpennington
39 * @author drbowen
40 * @author sunsetsystems
41 * @author cfapress
42 * @author sherwin gaddis
43 * @author avanss llc
44 * @author Brady Miller <brady.g.miller@gmail.com>
45 * @author Jerry Padgett <sjpadgett@gmail.com>
46 * @author Copyright (c) 2002 rufustfirefly
47 * @author Copyright (c) 2005 wpennington
48 * @author Copyright (c) 2005 drbowen
49 * @author Copyright (c) 2005-2007 sunsetsystems
50 * @author Copyright (c) 2008 cfapress
51 * @author Copyright (c) 2016 sherwin gaddis
52 * @author Copyright (c) 2019 avanss llc
53 * @author Copyright (c) 2019 Brady Miller <brady.g.miller@gmail.com>
54 * @author Copyright (c) 2019 Jerry Padgett <sjpadgett@gmail.com>
55 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
58 use OpenEMR\Common\Http\oeHttp;
60 class RxList
63 function getPage($query)
65 $url = "https://rxnav.nlm.nih.gov/REST/Prescribe/drugs";
66 $response = oeHttp::get($url, ['name'=>$query]);
67 $buffer = $response->body();
68 return $buffer ? $buffer : false;
69 } // end function RxList::getPage
71 function get_list($query)
73 $page = RxList::getPage($query);
74 $tokens = RxList::parse2tokens($page);
75 $hash = RxList::tokens2hash($tokens);
76 foreach ($hash as $index => $data) {
77 unset($my_data);
78 foreach ($data as $k => $v) {
79 $my_data[$k] = $v;
82 $rxcui = '';
84 if (trim($my_data['rxcui']) !== '') {
85 $rxcui = " / ".trim($my_data['rxcui']);
88 $synonym = '';
89 if (trim($my_data['synonym']) !== '') {
90 $synonym = " == (".trim($my_data['synonym']).$rxcui.")";
93 $list[trim($my_data['name']).$synonym] =
94 trim($my_data['name']);
97 return $list;
98 } // end function RxList::get_list
100 /* break the web page into a collection of TAGS
101 * such as <input ..> or <img ... >
103 function parse2tokens($page)
105 $pos = 0;
106 $token = 0;
107 unset($tokens);
108 $in_token = false;
109 while ($pos < strlen($page)) {
110 switch (substr($page, $pos, 1)) {
111 case "<":
112 if ($in_token) {
113 $token++;
114 $in_token = false;
117 $tokens[$token] .= substr($page, $pos, 1);
118 $in_token = true;
119 break;
121 case ">":
122 $tokens[$token] .= substr($page, $pos, 1);
123 $in_token = false;
124 $token++;
125 break;
127 default:
128 $tokens[$token] .= substr($page, $pos, 1);
129 $in_token = false;
130 break;
131 } // end decide what to do
132 $pos++;
133 } // end looping through string
134 return $tokens;
135 } // end function RxList::parse2tokens
137 function tokens2hash($tokens)
139 $record = false;
140 $current = 0;
141 unset($hash);
142 $hash = [];
143 unset($all);
144 for ($pos=0; $pos<count($tokens); $pos++) {
145 if (!(strpos($tokens[$pos], "<name>") === false) && $pos !== 3) {
146 // found a brand line 'token'
147 $type = "name";
148 $record = $pos;
149 $ending = "</name>";
152 if (!(strpos($tokens[$pos], "<synonym>") === false)) {
153 // found a generic line 'token'
154 $type = "synonym";
155 //print "generic_name record start at $pos<BR>\n";
156 $ending = "</synonym>";
157 $record = $pos;
160 if (!(strpos($tokens[$pos], "<rxcui>") === false)) {
161 // found a drug-class 'token'
162 $type = "rxcui";
163 $ending = "</rxcui>";
164 $record = $pos;
167 if (isset($hash["synonym"])) {
168 // Reset record
169 $record = false;
170 // Add hash to all
171 $all[] = $hash;
172 // Unset hash and type
173 unset($hash);
174 $type = "";
175 $ending = "";
178 if ($pos === ($record + 1) and ($ending != "")) {
179 $my_pos = strpos(strtoupper($tokens[$pos]), "<");
180 $hash[$type] = substr($tokens[$pos], 0, $my_pos);
181 $hash[$type] = str_replace("&amp;", "&", $hash[$type]);
182 //print "hash[$type] = ".htmlentities($hash[$type])."<BR>\n";
183 $type = "";
184 $ending = "";
186 } // end looping
187 return $all;
188 } // end function RxList::tokens2hash
189 } // end class RxList