minor changes and internationalization for prior commit
[openemr.git] / ccdaservice / ssmanager.php
blob20573ba14e501e9aaedf288024ee163914b94a2c
1 <?php
2 /**
4 * Copyright (C) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
6 * LICENSE: This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @package OpenEMR
20 * @author Jerry Padgett <sjpadgett@gmail.com>
21 * @link http://www.open-emr.org
23 require_once ( dirname( __FILE__ ) . "/../library/log.inc" );
25 function runCheck(){
26 if( !socket_status( 'localhost', '6661', 'status' ) ){
27 server_logit( 1, "Execute C-CDA Service Start", 0, "Task" );
28 execInBackground( '' );
29 sleep( 2 );
30 if( socket_status( 'localhost', '6661', 'status' ) )
31 server_logit( 1, "Service Status : Started." );
32 else
33 server_logit( 1, "Service Status : Failed Start." );
34 return true;
35 } else{
36 server_logit( 1, "Service Status : Alive.", 0, "Sanity Check" );
37 return true;
40 function service_shutdown($soft=1){
41 if( socket_status( 'localhost', '6661', 'status' ) ){
42 // shut down service- this can take a few seconds on windows so throw up notice to user.
43 flush();
44 echo '<h3 style="position: absolute; top: 25%; left: 42%">'. xlt("Shutting Down Service ...") . '</h3><img style="position: absolute; top: 40%; left: 45%; width: 125px; height: 125px" src="../../portal/sign/assets/loading.gif" />';
45 ob_flush(); flush();
46 server_logit( 1, "C-CDA Service shutdown request", 0, "Task" );
47 if(!IS_WINDOWS){
48 chdir(dirname(__FILE__));
49 $cmd = 'pkill -f "nodejs serveccda.njs"';
50 exec( $cmd . " > /dev/null &" );
52 else{
53 chdir(dirname(__FILE__));
54 $cmd = 'node unservice';
55 pclose( popen( $cmd, "r" ) );
57 sleep( 1 );
58 if( !socket_status( 'localhost', '6661', 'status' ) ){
59 server_logit( 1, "Service Status : " . $soft ? "Process Terminated" : "Terminated and Disabled." );
60 if($soft > 1) return true; // Just terminate process/service and allow background to restart. Restart if you will.
61 $service_name = 'ccdaservice';
62 // with ccdaservice and background service and running = 1 bs will not attempt restart of service while still available/active.
63 // not sure if needed but here it is anyway. Otherwise, service is disabled.
64 $sql = 'UPDATE background_services SET running = ?, active = ? WHERE name = ?';
65 $res = sqlStatementNoLog($sql, array($soft, $soft, $service_name));
66 return true;
68 else{
69 server_logit( 1, "Service Status : Failed Shutdown." );
70 return false;
72 } else{
73 server_logit( 1, "Service Status : Not active.", 0, "Shutdown Request" );
74 return true;
77 function execInBackground( $cmd ){
78 if( IS_WINDOWS ){
79 chdir(dirname(__FILE__));
80 $cmd = 'node winservice';
81 pclose( popen( $cmd, "r" ) );
82 } else{
83 chdir(dirname(__FILE__));
84 $cmd = 'nodejs serveccda.njs';
85 exec( $cmd . " > /dev/null &" );
88 function socket_status( $ip, $port, $data ){
89 $output = "";
90 $socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
91 if( $socket === false ){
92 server_logit( 1, "Creation of Socket Failed. Start/Restart Service" );
93 return false;
95 $result = socket_connect( $socket, $ip, $port );
96 if( $result === false ){
97 socket_close( $socket );
98 server_logit( 1, "Service Not Running" );
99 return false;
101 $data = $data . "\r\n";
102 $out = socket_write( $socket, $data, strlen( $data ) );
104 $line = "";
105 $line = socket_read( $socket, 1024, PHP_NORMAL_READ );
106 $output .= $line;
107 } while( $line != "\r" );
108 $output = substr( trim( $output ), 0, strlen( $output ) - 3 );
109 socket_close( $socket );
110 return true;
112 function service_command( $ip, $port, $doaction ){
113 $output = "";
114 $socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
115 if( $socket === false ){
116 server_logit( 1, "Service not resident." );
117 return false;
119 $result = socket_connect( $socket, $ip, $port );
120 if( $result === false ){
121 socket_close( $socket );
122 server_logit( 1, "Service Not Running." );
123 return false;
125 $doaction = $doaction . "\r\n";
126 $out = socket_write( $socket, $doaction, strlen( $doaction ) );
128 $line = "";
129 $line = socket_read( $socket, 1024, PHP_NORMAL_READ );
130 $output .= $line;
131 } while( $line != "\r" );
132 $output = substr( trim( $output ), 0, strlen( $output ) - 3 );
133 socket_close( $socket );
134 return true;
136 function server_logit( $success, $text, $pid = 0, $event = "ccdaservice-manager" ){
137 $pid = isset($_SESSION['pid'])?$_SESSION['pid']:$pid;
138 $event = isset($_SESSION['ptName']) ? ('Ccda Access: ' . $_SESSION['ptName']) : "Ccda Service Access";
139 $where = isset($_SESSION['ptName']) ? "Portal Patient" : 'OpenEMR: ' . $_SESSION['authUser'];
141 newEvent( $event, "Service_Manager", $where, $success, $text, $pid,'server','s2','s3' );