PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/workflow/engine/methods/services/processHeartBeat_Ajax.php

https://bitbucket.org/ferOnti/processmaker
PHP | 341 lines | 218 code | 62 blank | 61 comment | 40 complexity | 854c15db68933baca9db6891dfe25eb2 MD5 | raw file
  1. <?php
  2. $request = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : "";
  3. G::LoadClass( 'serverConfiguration' );
  4. $oServerConf = &serverConf::getSingleton();
  5. $oServerConf->setHeartbeatProperty( 'HB_BEAT_URL', 'http://heartbeat.processmaker.com/syspmLicenseSrv/en/green/services/beat', 'HEART_BEAT_CONF' );
  6. switch ($request) {
  7. case 'processInformation':
  8. try {
  9. $heartBeatUrl = $oServerConf->getHeartbeatProperty( 'HB_BEAT_URL', 'HEART_BEAT_CONF' );
  10. //Test connection
  11. if (! (validateConnectivity( $heartBeatUrl ))) {
  12. $oServerConf->setHeartbeatProperty( 'HB_NEXT_BEAT_DATE', strtotime( "+1 day" ), 'HEART_BEAT_CONF' );
  13. throw new Exception( "Heartbeat::No connection" );
  14. }
  15. //Build Data to be sent
  16. $params = buildData();
  17. //Send the information
  18. postHeartBeat( $params );
  19. } catch (Exception $e) {
  20. G::pr( $e->getMessage() );
  21. }
  22. break;
  23. }
  24. function validateConnectivity ($url)
  25. {
  26. ini_set( 'allow_url_fopen', 1 );
  27. $sContent = file_get_conditional_contents( $url );
  28. $sw_connect = true;
  29. //if ($sContent == '' || $sContent === false || strpos ( $sContent, 'address location' ) === false ) { 4
  30. if ($sContent == '' || $sContent === false) {
  31. $sw_connect = false;
  32. }
  33. return $sw_connect;
  34. }
  35. function file_get_conditional_contents ($szURL)
  36. {
  37. $pCurl = curl_init();
  38. curl_setopt( $pCurl, CURLOPT_URL, $szURL );
  39. curl_setopt( $pCurl, CURLOPT_RETURNTRANSFER, true );
  40. curl_setopt( $pCurl, CURLOPT_HEADER, true );
  41. curl_setopt( $pCurl, CURLOPT_FOLLOWLOCATION, false );
  42. curl_setopt( $pCurl, CURLOPT_AUTOREFERER, true );
  43. //To avoid SSL error
  44. curl_setopt( $pCurl, CURLOPT_SSL_VERIFYHOST, 0 );
  45. curl_setopt( $pCurl, CURLOPT_SSL_VERIFYPEER, 0 );
  46. //To avoid timeouts
  47. curl_setopt( $pCurl, CURLOPT_CONNECTTIMEOUT, 10 );
  48. curl_setopt( $pCurl, CURLOPT_TIMEOUT, 20 );
  49. curl_setopt( $pCurl, CURLOPT_NOPROGRESS, false );
  50. curl_setopt( $pCurl, CURLOPT_VERBOSE, true );
  51. //Apply proxy settings
  52. $sysConf = System::getSystemConfiguration();
  53. if ($sysConf['proxy_host'] != '') {
  54. curl_setopt( $pCurl, CURLOPT_PROXY, $sysConf['proxy_host'] . ($sysConf['proxy_port'] != '' ? ':' . $sysConf['proxy_port'] : '') );
  55. if ($sysConf['proxy_port'] != '') {
  56. curl_setopt( $pCurl, CURLOPT_PROXYPORT, $sysConf['proxy_port'] );
  57. }
  58. if ($sysConf['proxy_user'] != '') {
  59. curl_setopt( $pCurl, CURLOPT_PROXYUSERPWD, $sysConf['proxy_user'] . ($sysConf['proxy_pass'] != '' ? ':' . $sysConf['proxy_pass'] : '') );
  60. }
  61. curl_setopt( $pCurl, CURLOPT_HTTPHEADER, array ('Expect:'
  62. ) );
  63. }
  64. $szContents = curl_exec( $pCurl );
  65. $aInfo = curl_getinfo( $pCurl );
  66. $curl_session = curl_getinfo( $pCurl, CURLINFO_HTTP_CODE );
  67. $headers = curl_getinfo( $pCurl );
  68. $header = substr( $szContents, 0, $headers['header_size'] );
  69. $content = substr( $szContents, $headers['header_size'] );
  70. if ($aInfo['http_code'] === 200) {
  71. return $content;
  72. }
  73. return false;
  74. }
  75. function buildData ()
  76. {
  77. require_once ("classes/model/Users.php");
  78. G::LoadClass( "serverConfiguration" );
  79. G::LoadClass( "system" );
  80. $oServerConf = &serverConf::getSingleton();
  81. $os = '';
  82. if (file_exists( '/etc/redhat-release' )) {
  83. $fnewsize = filesize( '/etc/redhat-release' );
  84. $fp = fopen( '/etc/redhat-release', 'r' );
  85. $os = trim( fread( $fp, $fnewsize ) );
  86. fclose( $fp );
  87. }
  88. $os .= " (" . PHP_OS . ")";
  89. $params = array ();
  90. $params['ip'] = getenv( 'SERVER_ADDR' );
  91. $oServerConf->setHeartbeatProperty( 'HB_BEAT_INDEX', intval( $oServerConf->getHeartbeatProperty( 'HB_BEAT_INDEX', 'HEART_BEAT_CONF' ) ) + 1, 'HEART_BEAT_CONF' );
  92. $params['index'] = $oServerConf->getHeartbeatProperty( 'HB_BEAT_INDEX', 'HEART_BEAT_CONF' ); //$this->index;
  93. $params['beatType'] = is_null( $oServerConf->getHeartbeatProperty( 'HB_BEAT_TYPE', 'HEART_BEAT_CONF' ) ) ? "starting" : $oServerConf->getHeartbeatProperty( 'HB_BEAT_TYPE', 'HEART_BEAT_CONF' ); //1;//$this->beatType;
  94. $params['date'] = date( 'Y-m-d H:i:s' );
  95. $params['host'] = getenv( 'SERVER_NAME' );
  96. $params['os'] = $os;
  97. $params['webserver'] = getenv( 'SERVER_SOFTWARE' );
  98. $params['php'] = phpversion();
  99. $params['pmVersion'] = System::getVersion();
  100. if (class_exists( 'pmLicenseManager' )) {
  101. $params['pmProduct'] = 'PMEE';
  102. } else {
  103. $params['pmProduct'] = 'PMCE';
  104. }
  105. $params['logins'] = $oServerConf->logins;
  106. $params['workspaces'] = serialize( $oServerConf->getWSList() );
  107. $params['plugins'] = serialize( $oServerConf->getPluginsList() );
  108. $params['dbVersion'] = $oServerConf->getDBVersion();
  109. //$params ['errors'] = serialize( $oServerConf->errors );
  110. if ($licInfo = $oServerConf->getProperty( 'LICENSE_INFO' )) {
  111. $params['license'] = serialize( $licInfo );
  112. }
  113. ///////
  114. $criteria = new Criteria( "workflow" );
  115. $criteria->addSelectColumn( "COUNT(USERS.USR_UID) AS USERS_NUMBER" );
  116. $criteria->add( UsersPeer::USR_UID, null, Criteria::ISNOTNULL );
  117. $rs = UsersPeer::doSelectRS( $criteria );
  118. $rs->setFetchmode( ResultSet::FETCHMODE_ASSOC );
  119. $rs->next();
  120. $row = $rs->getRow();
  121. $params["users"] = $row["USERS_NUMBER"];
  122. ///////
  123. $ee = null;
  124. if (file_exists( PATH_PLUGINS . "enterprise" . PATH_SEP . "VERSION" )) {
  125. $ee = trim( file_get_contents( PATH_PLUGINS . "enterprise" . PATH_SEP . "VERSION" ) );
  126. } else {
  127. $pluginRegistry = &PMPluginRegistry::getSingleton();
  128. $details = $pluginRegistry->getPluginDetails( "enterprise.php" );
  129. $ee = (! ($details == null)) ? $details->iVersion : null;
  130. }
  131. $params["ee"] = $ee;
  132. ///////
  133. $addonNumber = 0;
  134. $addonEnabledNumber = 0;
  135. $pluginRegistry = &PMPluginRegistry::getSingleton();
  136. $arrayAddon = array ();
  137. if (file_exists( PATH_DATA_SITE . "ee" )) {
  138. $arrayAddon = unserialize( trim( file_get_contents( PATH_DATA_SITE . "ee" ) ) );
  139. $arrayAddon["enterprise"] = array ("sFilename" => "enterprise-1.tar"
  140. );
  141. }
  142. foreach ($arrayAddon as $addon) {
  143. $sFileName = substr( $addon["sFilename"], 0, strpos( $addon["sFilename"], "-" ) );
  144. if (file_exists( PATH_PLUGINS . $sFileName . ".php" )) {
  145. $addonDetails = $pluginRegistry->getPluginDetails( $sFileName . ".php" );
  146. $enabled = 0;
  147. if ($addonDetails) {
  148. $enabled = ($addonDetails->enabled) ? 1 : 0;
  149. }
  150. if ($enabled == 1) {
  151. $addonEnabledNumber = $addonEnabledNumber + 1;
  152. }
  153. $addonNumber = $addonNumber + 1;
  154. }
  155. }
  156. $params["addonNumber"] = $addonNumber;
  157. $params["addonEnabledNumber"] = $addonEnabledNumber;
  158. ///////
  159. $licenseID = null;
  160. $licenseType = null;
  161. $licenseDomainWorkspace = null;
  162. $licenseNumber = 0;
  163. if (file_exists( PATH_PLUGINS . "enterprise" . PATH_SEP . "class.pmLicenseManager.php" ) && class_exists( 'enterprisePlugin' )) {
  164. $licenseManager = &pmLicenseManager::getSingleton();
  165. preg_match( "/^license_(.*).dat$/", $licenseManager->file, $matches );
  166. $licenseID = $matches[1];
  167. $licenseType = $licenseManager->type;
  168. $licenseDomainWorkspace = $licenseManager->info["DOMAIN_WORKSPACE"];
  169. ///////
  170. $criteria = new Criteria( "workflow" );
  171. $criteria->addSelectColumn( "COUNT(LICENSE_MANAGER.LICENSE_UID) AS LICENSE_NUMBER" );
  172. $criteria->add( LicenseManagerPeer::LICENSE_UID, null, Criteria::ISNOTNULL );
  173. $rs = LicenseManagerPeer::doSelectRS( $criteria );
  174. $rs->setFetchmode( ResultSet::FETCHMODE_ASSOC );
  175. $rs->next();
  176. $row = $rs->getRow();
  177. ///////
  178. $licenseNumber = ($row["LICENSE_NUMBER"] > 0) ? $row["LICENSE_NUMBER"] : count( glob( PATH_DATA_SITE . "licenses" . PATH_SEP . "*.dat" ) );
  179. }
  180. $params["licenseID"] = $licenseID;
  181. $params["licenseType"] = $licenseType;
  182. $params["licenseDomainWorkspace"] = $licenseDomainWorkspace;
  183. $params["licenseNumber"] = $licenseNumber;
  184. ///////
  185. return $params;
  186. }
  187. function postHeartBeat ($params)
  188. {
  189. if (is_array( $params )) {
  190. //No matter what happens with the result let's set the nextBeat to 2 hours from now
  191. G::LoadClass( 'serverConfiguration' );
  192. $oServerConf = & serverConf::getSingleton();
  193. $oServerConf->setHeartbeatProperty( 'HB_NEXT_BEAT_DATE', strtotime( "+2 hour" ), 'HEART_BEAT_CONF' );
  194. $nextBeatDate = $oServerConf->getHeartbeatProperty( 'HB_NEXT_BEAT_DATE', 'HEART_BEAT_CONF' );
  195. $heartBeatUrl = $oServerConf->getHeartbeatProperty( 'HB_BEAT_URL', 'HEART_BEAT_CONF' );
  196. $ch = curl_init();
  197. curl_setopt( $ch, CURLOPT_URL, $heartBeatUrl );
  198. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  199. curl_setopt( $ch, CURLOPT_HEADER, true );
  200. curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, false );
  201. curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
  202. //To avoid SSL error
  203. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
  204. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
  205. curl_setopt( $ch, CURLOPT_POST, 1 );
  206. curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
  207. //To avoid timeouts
  208. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
  209. curl_setopt( $ch, CURLOPT_TIMEOUT, 20 );
  210. //Apply proxy settings
  211. $sysConf = System::getSystemConfiguration();
  212. if ($sysConf['proxy_host'] != '') {
  213. curl_setopt( $ch, CURLOPT_PROXY, $sysConf['proxy_host'] . ($sysConf['proxy_port'] != '' ? ':' . $sysConf['proxy_port'] : '') );
  214. if ($sysConf['proxy_port'] != '') {
  215. curl_setopt( $ch, CURLOPT_PROXYPORT, $sysConf['proxy_port'] );
  216. }
  217. if ($sysConf['proxy_user'] != '') {
  218. curl_setopt( $ch, CURLOPT_PROXYUSERPWD, $sysConf['proxy_user'] . ($sysConf['proxy_pass'] != '' ? ':' . $sysConf['proxy_pass'] : '') );
  219. }
  220. curl_setopt( $ch, CURLOPT_HTTPHEADER, array ('Expect:'
  221. ) );
  222. }
  223. $response = curl_exec( $ch );
  224. $curl_session = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
  225. $headers = curl_getinfo( $ch );
  226. $header = substr( $response, 0, $headers['header_size'] );
  227. $content = substr( $response, $headers['header_size'] );
  228. curl_close( $ch );
  229. if ($headers['http_code'] == 200) {
  230. $oServerConf->setHeartbeatProperty( 'HB_BEAT_TYPE', 'beat', 'HEART_BEAT_CONF' );
  231. $oServerConf->resetLogins();
  232. $oServerConf->setHeartbeatProperty( 'HB_NEXT_BEAT_DATE', strtotime( "+7 day" ), 'HEART_BEAT_CONF' );
  233. //Reset Errors
  234. } else {
  235. //Catch the error
  236. $oServerConf->setHeartbeatProperty( 'HB_NEXT_BEAT_DATE', strtotime( "+1 day" ), 'HEART_BEAT_CONF' );
  237. }
  238. }
  239. /*
  240. $ch = curl_init ();
  241. curl_setopt ( $ch, CURLOPT_URL, $heartBeatUrl );
  242. curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
  243. curl_setopt ( $ch, CURLOPT_HEADER, true );
  244. curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, false );
  245. curl_setopt ( $ch, CURLOPT_AUTOREFERER, true );
  246. //To avoid SSL error
  247. curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
  248. curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
  249. curl_setopt ( $ch, CURLOPT_POST, 1 );
  250. curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
  251. //To avoid timeouts
  252. curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
  253. curl_setopt ( $ch, CURLOPT_TIMEOUT, 20 );
  254. $response = curl_exec ( $ch );
  255. $curl_session = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  256. $headers = curl_getinfo ( $ch );
  257. $header = substr ( $response, 0, $headers ['header_size'] );
  258. $content = substr ( $response, $headers ['header_size'] );
  259. curl_close ( $ch );
  260. if ($headers ['http_code'] == 200) {
  261. $this->beatType = 'beat';
  262. $this->resetLogins ();
  263. $this->nextBeatDate = strtotime ( "+7 day" ); //next beat in 7 days
  264. //Reset Errors
  265. $this->errors=array();
  266. } else {
  267. //Catch the error
  268. $this->errors[]=$curl_session;
  269. $this->nextBeatDate = strtotime ( "+1 day" ); //retry in 30 mins
  270. }
  271. $this->saveSingleton ();
  272. */
  273. }