Improved Code Sniffing (#928)
[openemr.git] / ccdaservice / ssmanager.php
blobddb2ee43d0f4ee416e1516b0844214f60866d934
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()
27 if( !socket_status( 'localhost', '6661', 'status' ) ){
28 server_logit( 1, "Execute C-CDA Service Start", 0, "Task" );
29 execInBackground( '' );
30 sleep( 2 );
31 if( socket_status( 'localhost', '6661', 'status' ) )
32 server_logit( 1, "Service Status : Started." );
33 else
34 server_logit( 1, "Service Status : Failed Start." );
35 return true;
36 } else{
37 server_logit( 1, "Service Status : Alive.", 0, "Sanity Check" );
38 return true;
41 function service_shutdown($soft=1)
43 if( socket_status( 'localhost', '6661', 'status' ) ){
44 // shut down service- this can take a few seconds on windows so throw up notice to user.
45 flush();
46 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" />';
47 ob_flush();
48 flush();
49 server_logit( 1, "C-CDA Service shutdown request", 0, "Task" );
50 if(!IS_WINDOWS){
51 chdir(dirname(__FILE__));
52 $cmd = 'pkill -f "nodejs serveccda.njs"';
53 exec( $cmd . " > /dev/null &" );
55 else{
56 chdir(dirname(__FILE__));
57 $cmd = 'node unservice';
58 pclose( popen( $cmd, "r" ) );
60 sleep( 1 );
61 if( !socket_status( 'localhost', '6661', 'status' ) ){
62 server_logit( 1, "Service Status : " . $soft ? "Process Terminated" : "Terminated and Disabled." );
63 if($soft > 1) return true; // Just terminate process/service and allow background to restart. Restart if you will.
64 $service_name = 'ccdaservice';
65 // with ccdaservice and background service and running = 1 bs will not attempt restart of service while still available/active.
66 // not sure if needed but here it is anyway. Otherwise, service is disabled.
67 $sql = 'UPDATE background_services SET running = ?, active = ? WHERE name = ?';
68 $res = sqlStatementNoLog($sql, array($soft, $soft, $service_name));
69 return true;
71 else{
72 server_logit( 1, "Service Status : Failed Shutdown." );
73 return false;
75 } else{
76 server_logit( 1, "Service Status : Not active.", 0, "Shutdown Request" );
77 return true;
80 function execInBackground( $cmd )
82 if( IS_WINDOWS ){
83 chdir(dirname(__FILE__));
84 $cmd = 'node winservice';
85 pclose( popen( $cmd, "r" ) );
86 } else{
87 chdir(dirname(__FILE__));
88 $cmd = 'nodejs serveccda.njs';
89 exec( $cmd . " > /dev/null &" );
92 function socket_status( $ip, $port, $data )
94 $output = "";
95 $socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
96 if( $socket === false ){
97 server_logit( 1, "Creation of Socket Failed. Start/Restart Service" );
98 return false;
100 $result = socket_connect( $socket, $ip, $port );
101 if( $result === false ){
102 socket_close( $socket );
103 server_logit( 1, "Service Not Running" );
104 return false;
106 $data = $data . "\r\n";
107 $out = socket_write( $socket, $data, strlen( $data ) );
109 $line = "";
110 $line = socket_read( $socket, 1024, PHP_NORMAL_READ );
111 $output .= $line;
112 } while( $line != "\r" );
113 $output = substr( trim( $output ), 0, strlen( $output ) - 3 );
114 socket_close( $socket );
115 return true;
117 function service_command( $ip, $port, $doaction )
119 $output = "";
120 $socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
121 if( $socket === false ){
122 server_logit( 1, "Service not resident." );
123 return false;
125 $result = socket_connect( $socket, $ip, $port );
126 if( $result === false ){
127 socket_close( $socket );
128 server_logit( 1, "Service Not Running." );
129 return false;
131 $doaction = $doaction . "\r\n";
132 $out = socket_write( $socket, $doaction, strlen( $doaction ) );
134 $line = "";
135 $line = socket_read( $socket, 1024, PHP_NORMAL_READ );
136 $output .= $line;
137 } while( $line != "\r" );
138 $output = substr( trim( $output ), 0, strlen( $output ) - 3 );
139 socket_close( $socket );
140 return true;
142 function server_logit( $success, $text, $pid = 0, $event = "ccdaservice-manager" )
144 $pid = isset($_SESSION['pid'])?$_SESSION['pid']:$pid;
145 $event = isset($_SESSION['ptName']) ? ('Ccda Access: ' . $_SESSION['ptName']) : "Ccda Service Access";
146 $where = isset($_SESSION['ptName']) ? "Portal Patient" : 'OpenEMR: ' . $_SESSION['authUser'];
148 newEvent( $event, "Service_Manager", $where, $success, $text, $pid,'server','s2','s3' );