Implement direct message receive and background services manager, take 1.
[openemr.git] / interface / super / edit_globals.php
blob1957913bca023b18fe11a142b129bb1a5a0213bc
1 <?php
2 // Copyright (C) 2010 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 require_once("../globals.php");
10 require_once("$srcdir/acl.inc");
11 require_once("$srcdir/formdata.inc.php");
12 require_once("$srcdir/globals.inc.php");
13 require_once("$srcdir/user.inc");
14 require_once("$srcdir/classes/CouchDB.class.php");
16 if ($_GET['mode'] != "user") {
17 // Check authorization.
18 $thisauth = acl_check('admin', 'super');
19 if (!$thisauth) die(xl('Not authorized'));
22 function checkCreateCDB(){
23 $globalsres = sqlStatement("SELECT gl_name, gl_index, gl_value FROM globals WHERE gl_name IN
24 ('couchdb_host','couchdb_user','couchdb_pass','couchdb_port','couchdb_dbase','document_storage_method')");
25 $options = array();
26 while($globalsrow = sqlFetchArray($globalsres)){
27 $GLOBALS[$globalsrow['gl_name']] = $globalsrow['gl_value'];
29 $directory_created = false;
30 if($GLOBALS['document_storage_method'] != 0){
31 // /documents/temp/ folder is required for CouchDB
32 if(!is_dir($GLOBALS['OE_SITE_DIR'] . '/documents/temp/')){
33 $directory_created = mkdir($GLOBALS['OE_SITE_DIR'] . '/documents/temp/',0777,true);
34 if(!$directory_created){
35 echo htmlspecialchars( xl("Failed to create temporary folder. CouchDB will not work."),ENT_NOQUOTES);
38 $couch = new CouchDB();
39 if(!$couch->check_connection()) {
40 echo "<script type='text/javascript'>alert('".addslashes(xl("CouchDB Connection Failed."))."');</script>";
41 return;
43 if($GLOBALS['couchdb_host'] || $GLOBALS['couchdb_port'] || $GLOBALS['couchdb_dbase']){
44 $couch->createDB($GLOBALS['couchdb_dbase']);
45 $couch->createView($GLOBALS['couchdb_dbase']);
48 return true;
51 /**
52 * Update background_services table for a specific service following globals save.
53 * @author EMR Direct
55 function updateBackgroundService($name,$active,$interval) {
56 //order important here: next_run change dependent on _old_ value of execute_interval so it comes first
57 $sql = 'UPDATE background_services SET active=?, '
58 . 'next_run = next_run + INTERVAL (? - execute_interval) MINUTE, execute_interval=? WHERE name=?';
59 return sqlStatement($sql,array($active,$interval,$interval,$name));
62 /**
63 * Make any necessary changes to background_services table when globals are saved.
64 * To prevent an unexpected service call during startup or shutdown, follow these rules:
65 * 1. Any "startup" operations should occur _before_ the updateBackgroundService() call.
66 * 2. Any "shutdown" operations should occur _after_ the updateBackgroundService() call. If these operations
67 * would cause errors in a running service call, it would be best to make the shutdown function itself
68 * a background service that is activated here, does nothing if active=1 or running=1 for the
69 * parent service, then deactivates itself by setting active=0 when it is done shutting the parent service
70 * down. This will prevent nonresponsiveness to the user by waiting for a service to finish.
71 * 3. If any "previous" values for globals are required for startup/shutdown logic, they need to be
72 * copied to a temp variable before the while($globalsrow...) loop.
73 * @author EMR Direct
75 function checkBackgroundServices(){
76 //load up any necessary globals
77 $bgservices = sqlStatement("SELECT gl_name, gl_index, gl_value FROM globals WHERE gl_name IN
78 ('phimail_enable','phimail_interval')");
79 while($globalsrow = sqlFetchArray($bgservices)){
80 $GLOBALS[$globalsrow['gl_name']] = $globalsrow['gl_value'];
83 //Set up phimail service
84 $phimail_active = $GLOBALS['phimail_enable'] ? '1' : '0';
85 $phimail_interval = max(0,(int)$GLOBALS['phimail_interval']);
86 updateBackgroundService('phimail',$phimail_active,$phimail_interval);
90 <html>
92 <head>
93 <?php
95 html_header_show();
97 // If we are saving user_specific globals.
99 if ($_POST['form_save'] && $_GET['mode'] == "user") {
100 $i = 0;
101 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
102 if (in_array($grpname, $USER_SPECIFIC_TABS)) {
103 foreach ($grparr as $fldid => $fldarr) {
104 if (in_array($fldid, $USER_SPECIFIC_GLOBALS)) {
105 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
106 $label = "global:".$fldid;
107 $fldvalue = trim(strip_escape_custom($_POST["form_$i"]));
108 setUserSetting($label,$fldvalue,$_SESSION['authId'],FALSE);
109 if ( $_POST["toggle_$i"] == "YES" ) {
110 removeUserSetting($label);
112 ++$i;
117 echo "<script type='text/javascript'>";
118 echo "parent.left_nav.location.reload();";
119 echo "parent.Title.location.reload();";
120 echo "if(self.name=='RTop'){";
121 echo "parent.RBot.location.reload();";
122 echo "}else{";
123 echo "parent.RTop.location.reload();";
124 echo "}";
125 echo "self.location.href='edit_globals.php?mode=user&unique=yes';";
126 echo "</script>";
129 // If we are saving main globals.
131 if ($_POST['form_save'] && $_GET['mode'] != "user") {
133 $i = 0;
134 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
135 foreach ($grparr as $fldid => $fldarr) {
136 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
137 if($fldtype == 'pwd'){
138 $pass = sqlQuery("SELECT gl_value FROM globals WHERE gl_name = '$fldid'");
139 $fldvalueold = $pass['gl_value'];
141 sqlStatement("DELETE FROM globals WHERE gl_name = '$fldid'");
143 if (substr($fldtype, 0, 2) == 'm_') {
144 if (isset($_POST["form_$i"])) {
145 $fldindex = 0;
146 foreach ($_POST["form_$i"] as $fldvalue) {
147 $fldvalue = formDataCore($fldvalue, true);
148 sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
149 "VALUES ( '$fldid', '$fldindex', '$fldvalue' )");
150 ++$fldindex;
154 else {
155 if (isset($_POST["form_$i"])) {
156 $fldvalue = formData("form_$i", "P", true);
158 else {
159 $fldvalue = "";
161 if($fldtype=='pwd')
162 $fldvalue = $fldvalue ? SHA1($fldvalue) : $fldvalueold;
163 if(fldvalue){
164 sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
165 "VALUES ( '$fldid', '0', '$fldvalue' )");
169 ++$i;
172 checkCreateCDB();
173 checkBackgroundServices();
174 echo "<script type='text/javascript'>";
175 echo "parent.left_nav.location.reload();";
176 echo "parent.Title.location.reload();";
177 echo "if(self.name=='RTop'){";
178 echo "parent.RBot.location.reload();";
179 echo "}else{";
180 echo "parent.RTop.location.reload();";
181 echo "}";
182 echo "self.location.href='edit_globals.php?unique=yes';";
183 echo "</script>";
187 <!-- supporting javascript code -->
188 <script type="text/javascript" src="../../library/dialog.js"></script>
189 <script type="text/javascript" src="../../library/js/jquery.1.3.2.js"></script>
190 <script type="text/javascript" src="../../library/js/common.js"></script>
191 <script type="text/javascript" src="../../library/js/fancybox/jquery.fancybox-1.2.6.js"></script>
192 <link rel="stylesheet" type="text/css" href="../../library/js/fancybox/jquery.fancybox-1.2.6.css" media="screen" />
194 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
195 <?php if ($_GET['mode'] == "user") { ?>
196 <title><?php xl('User Settings','e'); ?></title>
197 <?php } else { ?>
198 <title><?php xl('Global Settings','e'); ?></title>
199 <?php } ?>
201 <style>
202 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
203 tr.detail { font-size:10pt; }
204 td { font-size:10pt; }
205 input { font-size:10pt; }
206 </style>
208 </head>
210 <body class="body_top">
212 <?php if ($_GET['mode'] == "user") { ?>
213 <form method='post' name='theform' id='theform' action='edit_globals.php?mode=user' onsubmit='return top.restoreSession()'>
214 <?php } else { ?>
215 <form method='post' name='theform' id='theform' action='edit_globals.php' onsubmit='return top.restoreSession()'>
216 <?php } ?>
218 <?php if ($_GET['mode'] == "user") { ?>
219 <p><b><?php xl('Edit User Settings','e'); ?></b>
220 <?php } else { ?>
221 <p><b><?php xl('Edit Global Settings','e'); ?></b>
222 <?php } ?>
224 <ul class="tabNav">
225 <?php
226 $i = 0;
227 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
228 if ( $_GET['mode'] != "user" || ($_GET['mode'] == "user" && in_array($grpname, $USER_SPECIFIC_TABS)) ) {
229 echo " <li" . ($i ? "" : " class='current'") .
230 "><a href='/play/javascript-tabbed-navigation/'>" .
231 xl($grpname) . "</a></li>\n";
232 ++$i;
236 </ul>
238 <div class="tabContainer">
239 <?php
240 $i = 0;
241 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
242 if ( $_GET['mode'] != "user" || ($_GET['mode'] == "user" && in_array($grpname, $USER_SPECIFIC_TABS)) ) {
243 echo " <div class='tab" . ($i ? "" : " current") .
244 "' style='height:auto;width:97%;'>\n";
246 echo " <table>";
248 if ($_GET['mode'] == "user") {
249 echo "<tr>";
250 echo "<th>&nbsp</th>";
251 echo "<th>" . htmlspecialchars( xl('User Specific Setting'), ENT_NOQUOTES) . "</th>";
252 echo "<th>" . htmlspecialchars( xl('Default Setting'), ENT_NOQUOTES) . "</th>";
253 echo "<th>&nbsp</th>";
254 echo "<th>" . htmlspecialchars( xl('Set to Default'), ENT_NOQUOTES) . "</th>";
255 echo "</tr>";
258 foreach ($grparr as $fldid => $fldarr) {
259 if ( $_GET['mode'] != "user" || ($_GET['mode'] == "user" && in_array($fldid, $USER_SPECIFIC_GLOBALS)) ) {
260 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
262 // Most parameters will have a single value, but some will be arrays.
263 // Here we cater to both possibilities.
264 $glres = sqlStatement("SELECT gl_index, gl_value FROM globals WHERE " .
265 "gl_name = '$fldid' ORDER BY gl_index");
266 $glarr = array();
267 while ($glrow = sqlFetchArray($glres)) $glarr[] = $glrow;
269 // $fldvalue is meaningful only for the single-value cases.
270 $fldvalue = count($glarr) ? $glarr[0]['gl_value'] : $flddef;
272 // Collect user specific setting if mode set to user
273 $userSetting = "";
274 $settingDefault = "checked='checked'";
275 if ($_GET['mode'] == "user") {
276 $userSettingArray = sqlQuery("SELECT * FROM user_settings WHERE setting_user=? AND setting_label=?",array($_SESSION['authId'],"global:".$fldid));
277 $userSetting = $userSettingArray['setting_value'];
278 $globalValue = $fldvalue;
279 if (!empty($userSettingArray)) {
280 $fldvalue = $userSetting;
281 $settingDefault = "";
285 echo " <tr title='$flddesc'><td valign='top'><b>$fldname </b></td><td valign='top'>\n";
287 if (is_array($fldtype)) {
288 echo " <select name='form_$i' id='form_$i'>\n";
289 foreach ($fldtype as $key => $value) {
290 if ($_GET['mode'] == "user") {
291 if ($globalValue == $key) $globalTitle = $value;
293 echo " <option value='$key'";
294 if ($key == $fldvalue) echo " selected";
295 echo ">";
296 echo $value;
297 echo "</option>\n";
299 echo " </select>\n";
302 else if ($fldtype == 'bool') {
303 if ($_GET['mode'] == "user") {
304 if ($globalValue == 1) {
305 $globalTitle = htmlspecialchars( xl('Checked'), ENT_NOQUOTES);
307 else {
308 $globalTitle = htmlspecialchars( xl('Not Checked'), ENT_NOQUOTES);
311 echo " <input type='checkbox' name='form_$i' id='form_$i' value='1'";
312 if ($fldvalue) echo " checked";
313 echo " />\n";
316 else if ($fldtype == 'num') {
317 if ($_GET['mode'] == "user") {
318 $globalTitle = $globalValue;
320 echo " <input type='text' name='form_$i' id='form_$i' " .
321 "size='6' maxlength='15' value='$fldvalue' />\n";
324 else if ($fldtype == 'text') {
325 if ($_GET['mode'] == "user") {
326 $globalTitle = $globalValue;
328 echo " <input type='text' name='form_$i' id='form_$i' " .
329 "size='50' maxlength='255' value='$fldvalue' />\n";
331 else if ($fldtype == 'pwd') {
332 if ($_GET['mode'] == "user") {
333 $globalTitle = $globalValue;
335 echo " <input type='password' name='form_$i' " .
336 "size='50' maxlength='255' value='' />\n";
339 else if ($fldtype == 'pass') {
340 if ($_GET['mode'] == "user") {
341 $globalTitle = $globalValue;
343 echo " <input type='password' name='form_$i' " .
344 "size='50' maxlength='255' value='$fldvalue' />\n";
347 else if ($fldtype == 'lang') {
348 $res = sqlStatement("SELECT * FROM lang_languages ORDER BY lang_description");
349 echo " <select name='form_$i' id='form_$i'>\n";
350 while ($row = sqlFetchArray($res)) {
351 echo " <option value='" . $row['lang_description'] . "'";
352 if ($row['lang_description'] == $fldvalue) echo " selected";
353 echo ">";
354 echo xl($row['lang_description']);
355 echo "</option>\n";
357 echo " </select>\n";
360 else if ($fldtype == 'm_lang') {
361 $res = sqlStatement("SELECT * FROM lang_languages ORDER BY lang_description");
362 echo " <select multiple name='form_{$i}[]' id='form_{$i}[]' size='3'>\n";
363 while ($row = sqlFetchArray($res)) {
364 echo " <option value='" . $row['lang_description'] . "'";
365 foreach ($glarr as $glrow) {
366 if ($glrow['gl_value'] == $row['lang_description']) {
367 echo " selected";
368 break;
371 echo ">";
372 echo xl($row['lang_description']);
373 echo "</option>\n";
375 echo " </select>\n";
378 else if ($fldtype == 'css') {
379 if ($_GET['mode'] == "user") {
380 $globalTitle = $globalValue;
382 $themedir = "$webserver_root/interface/themes";
383 $dh = opendir($themedir);
384 if ($dh) {
385 echo " <select name='form_$i' id='form_$i'>\n";
386 while (false !== ($tfname = readdir($dh))) {
387 // Only show files that contain style_ as options
388 // Skip style_blue.css since this is used for
389 // lone scripts such as setup.php
390 // Also skip style_pdf.css which is for PDFs and not screen output
391 if (!preg_match("/^style_.*\.css$/", $tfname) ||
392 $tfname == 'style_blue.css' || $tfname == 'style_pdf.css')
393 continue;
394 echo "<option value='$tfname'";
395 if ($tfname == $fldvalue) echo " selected";
396 echo ">";
397 echo $tfname;
398 echo "</option>\n";
400 closedir($dh);
401 echo " </select>\n";
405 else if ($fldtype == 'hour') {
406 if ($_GET['mode'] == "user") {
407 $globalTitle = $globalValue;
409 echo " <select name='form_$i' id='form_$i'>\n";
410 for ($h = 0; $h < 24; ++$h) {
411 echo "<option value='$h'";
412 if ($h == $fldvalue) echo " selected";
413 echo ">";
414 if ($h == 0) echo "12 AM";
415 else if ($h < 12) echo "$h AM";
416 else if ($h == 12) echo "12 PM";
417 else echo ($h - 12) . " PM";
418 echo "</option>\n";
420 echo " </select>\n";
422 if ($_GET['mode'] == "user") {
423 echo " </td>\n";
424 echo "<td align='center' style='color:red;'>" . $globalTitle . "</td>\n";
425 echo "<td>&nbsp</td>";
426 echo "<td align='center'><input type='checkbox' value='YES' name='toggle_" . $i . "' id='toggle_" . $i . "' " . $settingDefault . "/></td>\n";
427 echo "<input type='hidden' id='globaldefault_" . $i . "' value='" . $globalValue . "'>\n";
428 echo "</tr>\n";
430 else {
431 echo " </td></tr>\n";
433 ++$i;
436 echo " </table>\n";
437 echo " </div>\n";
441 </div>
444 <input type='submit' name='form_save' value='<?php xl('Save','e'); ?>' />
445 </p>
446 </center>
448 </form>
450 </body>
452 <script language="JavaScript">
454 $(document).ready(function(){
455 tabbify();
456 enable_modals();
458 // Use the counter ($i) to make the form user friendly for user-specific globals use
459 <?php if ($_GET['mode'] == "user") { ?>
460 <?php for ($j = 0; $j <= $i; $j++) { ?>
461 $("#form_<?php echo $j ?>").change(function() {
462 $("#toggle_<?php echo $j ?>").attr('checked',false);
464 $("#toggle_<?php echo $j ?>").change(function() {
465 if ($('#toggle_<?php echo $j ?>').attr('checked')) {
466 var defaultGlobal = $("#globaldefault_<?php echo $j ?>").val();
467 $("#form_<?php echo $j ?>").val(defaultGlobal);
470 <?php } ?>
471 <?php } ?>
475 </script>
477 </html>