PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/www/install/steps/functions.php

https://gitlab.com/florianocomercial/centreon
PHP | 245 lines | 168 code | 9 blank | 68 comment | 35 complexity | 5bf6a4039c2354f1b2ef1605010f64eb MD5 | raw file
  1. <?php
  2. /**
  3. * Checks if line is sql comment
  4. *
  5. * @param string $str
  6. * @return bool
  7. */
  8. function isSqlComment($str) {
  9. if (substr(trim($str), 0, 2) == "--") {
  10. return true;
  11. }
  12. return false;
  13. }
  14. /**
  15. * Get template
  16. *
  17. * @param string $dir directory of templates
  18. * @return Smarty
  19. */
  20. function getTemplate($dir) {
  21. require_once '../../../GPL_LIB/Smarty/libs/Smarty.class.php';
  22. $template = new Smarty();
  23. $template->compile_dir = "../../../GPL_LIB/SmartyCache/compile";
  24. $template->config_dir = "../../../GPL_LIB/SmartyCache/config";
  25. $template->cache_dir = "../../../GPL_LIB/SmartyCache/cache";
  26. $template->template_dir = $dir;
  27. $template->caching = 0;
  28. $template->compile_check = true;
  29. $template->force_compile = true;
  30. return $template;
  31. }
  32. /**
  33. * Connect to database with user root
  34. *
  35. * @return mixed
  36. */
  37. function myConnect() {
  38. $pass = "";
  39. if (isset($_SESSION['root_password']) && $_SESSION['root_password']) {
  40. $pass = $_SESSION['root_password'];
  41. }
  42. $host = "localhost";
  43. if (isset($_SESSION['ADDRESS']) && $_SESSION['ADDRESS']) {
  44. $host = $_SESSION['ADDRESS'];
  45. }
  46. $port = "3306";
  47. if (isset($_SESSION['DB_PORT']) && $_SESSION['DB_PORT']) {
  48. $port = $_SESSION['DB_PORT'];
  49. }
  50. return mysql_connect($host.':'.$port, 'root', $pass);
  51. }
  52. /**
  53. * Replace macros
  54. *
  55. * @param string $query
  56. * @return string
  57. */
  58. function replaceInstallationMacros($query) {
  59. while (preg_match('/@([a-zA-Z0-9_]+)@/', $query, $matches)) {
  60. $macroValue = "";
  61. if (isset($_SESSION[$matches[1]])) {
  62. $macroValue = $_SESSION[$matches[1]];
  63. }
  64. // Exception
  65. if ($matches[1] == 'MAILER') {
  66. $macroValue = '-MAILER-';
  67. }
  68. $query = preg_replace('/@'.$matches[1].'@/', $macroValue, $query);
  69. }
  70. $query = str_replace('-MAILER-', '@MAILER@', $query);
  71. return $query;
  72. }
  73. /**
  74. * Split queries
  75. *
  76. * @param string $file
  77. * @param string $delimiter
  78. * @param CentreonDB $connector
  79. * @param string $tmpFile | $tmpFile will store the number of executed queries sql script can be resumed from last failure
  80. * @return string | returns "0" if everything is ok, or returns error message
  81. */
  82. function splitQueries($file, $delimiter = ';', $connector = null, $tmpFile = "") {
  83. set_time_limit(0);
  84. $count = 0;
  85. $start = 0;
  86. $fileName = basename($file);
  87. if ($tmpFile != '' && is_file($tmpFile)) {
  88. $start = file_get_contents($tmpFile);
  89. }
  90. if (is_file($file) === true) {
  91. $file = fopen($file, 'r');
  92. if (is_resource($file) === true)
  93. {
  94. $query = array();
  95. $line = 0;
  96. while (feof($file) === false) {
  97. $line++;
  98. $currentLine = fgets($file);
  99. if (false == isSqlComment($currentLine)) {
  100. $query[] = $currentLine;
  101. }
  102. if (preg_match('~' . preg_quote($delimiter, '~') . '\s*$~iS', end($query)) === 1) {
  103. $query = trim(implode('', $query));
  104. $query = replaceInstallationMacros($query);
  105. $count++;
  106. if ($count > $start) {
  107. if (is_null($connector)) {
  108. if (mysql_query($query) === false) {
  109. fclose($file);
  110. return "$fileName Line $line:".mysql_error();
  111. }
  112. } else {
  113. try {
  114. $connector->query($query);
  115. } catch (Exception $e) {
  116. return "$fileName Line $line:".$e->getMessage();
  117. }
  118. }
  119. while (ob_get_level() > 0) {
  120. ob_end_flush();
  121. }
  122. flush();
  123. if ($tmpFile != '') {
  124. file_put_contents($tmpFile, $count);
  125. }
  126. }
  127. }
  128. if (is_string($query) === true) {
  129. $query = array();
  130. }
  131. }
  132. fclose($file);
  133. return "0";
  134. }
  135. }
  136. return _('File not found');
  137. }
  138. /**
  139. * Import file, mainly INSERT clauses
  140. *
  141. * @param string $file
  142. * @return void
  143. */
  144. function importFile($file) {
  145. mysql_query('BEGIN');
  146. if (false == splitQueries($file)) {
  147. $error = mysql_error();
  148. mysql_query('ROLLBACK');
  149. exitProcess(PROCESS_ID, 1, $error);
  150. }
  151. mysql_query('COMMIT');
  152. }
  153. /**
  154. * Exit process
  155. *
  156. * @param string $id | name of the process
  157. * @param int $result | 0 = ok, 1 = nok
  158. * @param string $msg | error message
  159. */
  160. function exitProcess($id, $result, $msg) {
  161. $msg = str_replace('"', '\"', $msg);
  162. $msg = str_replace('\\', '\\\\', $msg);
  163. echo '{
  164. "id" : "'.$id.'",
  165. "result" : "'.$result.'",
  166. "msg" : "'.$msg.'"
  167. }';
  168. @mysql_close();
  169. exit;
  170. }
  171. /**
  172. * Exit upgrade process
  173. *
  174. * @param int $result | 0 = ok, 1 = nok
  175. * @param string $current
  176. * @param string $next
  177. * @param string $msg | error message
  178. * @return void
  179. */
  180. function exitUpgradeProcess($result, $current, $next, $msg) {
  181. $msg = str_replace('"', '\"', $msg);
  182. $msg = str_replace('\\', '\\\\', $msg);
  183. echo '{
  184. "result" : "'.$result.'",
  185. "current" : "'.$current.'",
  186. "next" : "'.$next.'",
  187. "msg" : "'.$msg.'"
  188. }';
  189. exit;
  190. }
  191. /**
  192. * Get param lines from file
  193. *
  194. * @param string $varPath
  195. * @param string $objectType
  196. * @return array
  197. */
  198. function getParamLines($varPath, $objectType) {
  199. $contents = "";
  200. if ($handle = opendir($varPath)) {
  201. while (false !== ($object = readdir($handle))) {
  202. if ($object == $objectType) {
  203. $contents = file_get_contents($varPath.'/'.$object);
  204. }
  205. }
  206. closedir($handle);
  207. }
  208. $lines = explode("\n", $contents);
  209. return $lines;
  210. }
  211. /**
  212. * Set session variables
  213. *
  214. * @param array $conf_centreon
  215. * @return void
  216. */
  217. function setSessionVariables($conf_centreon) {
  218. $_SESSION['INSTALL_DIR_CENTREON'] = $conf_centreon['centreon_dir'];
  219. $_SESSION['CENTREON_ETC'] = $conf_centreon['centreon_etc'];
  220. $_SESSION['BIN_MAIL'] = $conf_centreon['mail'];
  221. $_SESSION['MONITORINGENGINE_USER'] = $conf_centreon['monitoring_user'];
  222. $_SESSION['MONITORINGENGINE_GROUP'] = $conf_centreon['monitoring_group'];
  223. $_SESSION['MONITORINGENGINE_ETC'] = $conf_centreon['monitoring_etc'];
  224. $_SESSION['MONITORINGENGINE_PLUGIN'] = $conf_centreon['plugin_dir'];
  225. $_SESSION['CENTREON_LOG'] = $conf_centreon['centreon_log'];
  226. $_SESSION['CENTREON_RRD_DIR'] = $conf_centreon['centreon_dir_rrd'];
  227. $_SESSION['MONITORING_INIT_SCRIPT'] = $conf_centreon['monitoring_init_script'];
  228. $_SESSION['MONITORING_BINARY'] = $conf_centreon['monitoring_binary'];
  229. $_SESSION['CENTREON_VARLIB'] = $conf_centreon['centreon_varlib'];
  230. $_SESSION['MONITORING_VAR_LOG'] = $conf_centreon['monitoring_varlog'];
  231. $_SESSION['BROKER_INIT_SCRIPT'] = $conf_centreon['broker_init_script'];
  232. $_SESSION['CENTREON_ENGINE_CONNECTORS'] = $conf_centreon['centreon_engine_connectors'];
  233. $_SESSION['CENTREON_ENGINE_LIB'] = $conf_centreon['centreon_engine_lib'];
  234. $_SESSION['CENTREONBROKER_CBMOD'] = $conf_centreon['centreonbroker_cbmod'];
  235. }
  236. ?>