Support new security model in the formSubmit function
[openemr.git] / library / api.inc
blob274ae416f185938c39fa6628e05bd5f3711b2b95
1 <?php
2 //our api for 3rd party developers
3 include_once("../../globals.php");
4 include_once("{$GLOBALS['srcdir']}/sql.inc");
5 include_once("{$GLOBALS['srcdir']}/billing.inc");
6 include_once("{$GLOBALS['srcdir']}/formdata.inc.php");
8 $GLOBALS['form_exit_url'] = $GLOBALS['concurrent_layout'] ?
9         "$rootdir/patient_file/encounter/encounter_top.php" :
10         "$rootdir/patient_file/encounter/patient_encounter.php";
12 function formHeader ($title = "My Form")
14         ?>
15         <html>
16         <head>
17 <?php html_header_show();?>
18         <link rel=stylesheet href="<?php echo $GLOBALS['css_header']?>" type="text/css">
19         <title><?php echo $title?></title>
20         </head>
21         <body background="<?php echo $GLOBALS['backpic']?>" topmargin=0 rightmargin=0 leftmargin=2 bottommargin=0 marginwidth=2 marginheight=0>
22         <?php
25 function formFooter ()
27         ?>
28         </body>
29         </html>
30         <?php
33 // This function will escape the $values when using the new security method (ie. $sanitize_all_escapes is TRUE).
34 //   Otherwise, this function expects the $values to already be escaped(original and legacy behavior).
35 function formSubmit ($tableName, $values, $id, $authorized = "0")
37         $sql = "insert into $tableName set pid = {$_SESSION['pid']},groupname='".$_SESSION['authProvider']."',user='".$_SESSION['authUser']."',authorized=$authorized,activity=1, date = NOW(),";
38         foreach ($values as $key => $value)
39                 if (strpos($key,"openemr_net_cpt") === 0) {
40                         //code to auto add cpt code
41                         if (!empty($value)) {
42                                 $code_array = split(" ",$value,2);
43                                 
44                                 addBilling(date("Ymd"), 'CPT4', $code_array[0], $code_array[1], $_SESSION['pid'], $authorized, $_SESSION['authUserID']);
45                         }
46                                         
47                 }
48                 //case where key looks like "[a-zA-Z]*diagnosis[0-9]" which is special, it is used to auto add ICD codes
49                 
50                 elseif (strpos($key,"diagnosis") == (strlen($key) -10) && !(strpos($key,"diagnosis")=== false )) {
51                         //icd auto add ICD9-CM
52                         if (!empty($value)) {
53                                 $code_array = split(" ",$value,2);
54                                 addBilling(date("Ymd"), 'ICD9-M', $code_array[0], $code_array[1], $_SESSION['pid'], $authorized, $_SESSION['authUserID']);
55                         }
56                 }
57                 else {
58                         if (isset($sanitize_all_escapes) && $sanitize_all_escapes) {
59                                 // using new security method, so escape the key and values here
60                                 $sql .= " " . add_escape_custom($key) . " = '" . add_escape_custom($value) . "',";
61                         }
62                         else {
63                                 // original method (rely on code to escape values before using this function)
64                                 $sql .= " $key = '$value',";
65                         }
66                 }
67         $sql = substr($sql, 0, -1);
68         return sqlInsert($sql);
72 function formUpdate ($tableName, $values, $id, $authorized = "0")
74         $sql = "update $tableName set pid = {$_SESSION['pid']},groupname='".$_SESSION['authProvider']."',user='".$_SESSION['authUser']."',authorized=$authorized,activity=1, date = NOW(),";
75         foreach ($values as $key => $value)
76                 $sql .= " $key = '$value',";
77         $sql = substr($sql, 0, -1);
78         $sql .= " where id=$id";
79         
80         return sqlInsert($sql);
84 function formJump ($address = "0")
86         $returnurl = $GLOBALS['concurrent_layout'] ? 'encounter_top.php' : 'patient_encounter.php';
87         if ($address == "0")
88                 $address = "{$GLOBALS['rootdir']}/patient_file/encounter/$returnurl";
89         echo "\n<script language='Javascript'>top.restoreSession();window.location='$address';</script>\n";
90         exit;
93 function formFetch ($tableName, $id, $cols="*", $activity="1")
95         return sqlQuery ( "select $cols from `$tableName` where id='$id' and pid = '{$GLOBALS['pid']}' and activity like '$activity' order by date DESC LIMIT 0,1" ) ;
98 function formGetIds ($tableName, $cols = "*", $limit='all', $start=0, $activity = "1")
100         if($limit == "all")
101         {
103                 $sql = "select $cols from `$tableName` where pid like '$pid' ";
104                 if ($activity != "all")
105                         $sql .= "and activity like '$activity' ";
106                 $sql .= "order by date DESC";
107         }
108         else
109         {
110                 $sql = "select $cols from pnotes where pid like '$pid' ";
111                 $sql .= " AND deleted != 1 "; // exclude ALL deleted notes
112                 if ($activity != "all")
113                         $sql .= "and activity like '$activity' ";
114                 $sql .= "order by date DESC LIMIT $start, $limit";
115         }
117         $res = sqlStatement($sql);
118         
119         for ($iter = 0;$row = sqlFetchArray($res);$iter++)
120                 $all[$iter] = $row;
121         return $all;
124 function formDisappear ($tableName, $id)
126         if (sqlStatement("update `$tableName` set activity = '0' where id='$id' and pid='$pid'")) return true;
127         return false;
130 function formReappear ($tableName, $id)
132         if (sqlStatement("update `$tableName` set activity = '1' where id='$id' and pid='$pid'")) return true;
133         return false;