PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/install/civicrm.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 361 lines | 243 code | 44 blank | 74 comment | 49 complexity | 6f26b29060a6f52a5b1ba99e227c4232 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 5 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2019 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2019
  31. * $Id$
  32. * @param $filesDirectory
  33. */
  34. function civicrm_setup($filesDirectory) {
  35. global $crmPath, $sqlPath, $pkgPath, $tplPath;
  36. global $compileDir;
  37. // Setup classloader
  38. // This is needed to allow CiviCRM to be installed by drush.
  39. // TODO: move to civicrm.drush.inc drush_civicrm_install()
  40. global $crmPath;
  41. require_once $crmPath . '/CRM/Core/ClassLoader.php';
  42. CRM_Core_ClassLoader::singleton()->register();
  43. $sqlPath = $crmPath . DIRECTORY_SEPARATOR . 'sql';
  44. $tplPath = $crmPath . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'CRM' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR;
  45. if (!is_dir($filesDirectory)) {
  46. mkdir($filesDirectory, 0777);
  47. chmod($filesDirectory, 0777);
  48. }
  49. $scratchDir = $filesDirectory . DIRECTORY_SEPARATOR . 'civicrm';
  50. if (!is_dir($scratchDir)) {
  51. mkdir($scratchDir, 0777);
  52. }
  53. $compileDir = $scratchDir . DIRECTORY_SEPARATOR . 'templates_c' . DIRECTORY_SEPARATOR;
  54. if (!is_dir($compileDir)) {
  55. mkdir($compileDir, 0777);
  56. }
  57. $compileDir = addslashes($compileDir);
  58. }
  59. /**
  60. * @param string $name
  61. * @param $buffer
  62. */
  63. function civicrm_write_file($name, &$buffer) {
  64. $fd = fopen($name, "w");
  65. if (!$fd) {
  66. die("Cannot open $name");
  67. }
  68. fwrite($fd, $buffer);
  69. fclose($fd);
  70. }
  71. /**
  72. * @param $config
  73. */
  74. function civicrm_main(&$config) {
  75. global $sqlPath, $crmPath, $cmsPath, $installType;
  76. if ($installType == 'drupal') {
  77. $siteDir = isset($config['site_dir']) ? $config['site_dir'] : getSiteDir($cmsPath, $_SERVER['SCRIPT_FILENAME']);
  78. civicrm_setup($cmsPath . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . $siteDir . DIRECTORY_SEPARATOR . 'files'
  79. );
  80. }
  81. elseif ($installType == 'backdrop') {
  82. civicrm_setup($cmsPath . DIRECTORY_SEPARATOR . 'files');
  83. }
  84. elseif ($installType == 'wordpress') {
  85. $upload_dir = wp_upload_dir();
  86. $files_dirname = $upload_dir['basedir'];
  87. civicrm_setup($files_dirname);
  88. }
  89. $parts = explode(':', $config['mysql']['server']);
  90. if (empty($parts[1])) {
  91. $parts[1] = 3306;
  92. }
  93. $config['mysql']['server'] = implode(':', $parts);
  94. $dsn = "mysql://{$config['mysql']['username']}:{$config['mysql']['password']}@{$config['mysql']['server']}/{$config['mysql']['database']}?new_link=true";
  95. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm.mysql');
  96. if (!empty($config['loadGenerated'])) {
  97. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_generated.mysql', TRUE);
  98. }
  99. else {
  100. if (isset($config['seedLanguage'])
  101. and preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $config['seedLanguage'])
  102. and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql")
  103. and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql")
  104. ) {
  105. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql");
  106. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql");
  107. }
  108. else {
  109. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_data.mysql');
  110. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_acl.mysql');
  111. }
  112. }
  113. // generate backend settings file
  114. if ($installType == 'drupal') {
  115. $configFile = $cmsPath . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . $siteDir . DIRECTORY_SEPARATOR . 'civicrm.settings.php';
  116. }
  117. elseif ($installType == 'backdrop') {
  118. $configFile = $cmsPath . DIRECTORY_SEPARATOR . 'civicrm.settings.php';
  119. }
  120. elseif ($installType == 'wordpress') {
  121. $configFile = $files_dirname . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR . 'civicrm.settings.php';
  122. }
  123. $string = civicrm_config($config);
  124. civicrm_write_file($configFile,
  125. $string
  126. );
  127. }
  128. /**
  129. * @param $dsn
  130. * @param string $fileName
  131. * @param bool $lineMode
  132. */
  133. function civicrm_source($dsn, $fileName, $lineMode = FALSE) {
  134. global $crmPath;
  135. require_once "$crmPath/packages/DB.php";
  136. // CRM-19699 See also CRM_Core_DAO for PHP7 mysqli compatiblity.
  137. // Duplicated here because this is not using CRM_Core_DAO directly
  138. // and this function may be called directly from Drush.
  139. if (!defined('DB_DSN_MODE')) {
  140. define('DB_DSN_MODE', 'auto');
  141. }
  142. $db = DB::connect($dsn);
  143. if (PEAR::isError($db)) {
  144. die("Cannot open $dsn: " . $db->getMessage());
  145. }
  146. $db->query("SET NAMES utf8");
  147. $db->query("SET NAMES utf8");
  148. if (!$lineMode) {
  149. $string = file_get_contents($fileName);
  150. // change \r\n to fix windows issues
  151. $string = str_replace("\r\n", "\n", $string);
  152. //get rid of comments starting with # and --
  153. $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
  154. $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
  155. $queries = preg_split('/;\s*$/m', $string);
  156. foreach ($queries as $query) {
  157. $query = trim($query);
  158. if (!empty($query)) {
  159. $res = &$db->query($query);
  160. if (PEAR::isError($res)) {
  161. print_r($res);
  162. die("Cannot execute $query: " . $res->getMessage());
  163. }
  164. }
  165. }
  166. }
  167. else {
  168. $fd = fopen($fileName, "r");
  169. while ($string = fgets($fd)) {
  170. $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
  171. $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
  172. $string = trim($string);
  173. if (!empty($string)) {
  174. $res = &$db->query($string);
  175. if (PEAR::isError($res)) {
  176. die("Cannot execute $string: " . $res->getMessage());
  177. }
  178. }
  179. }
  180. }
  181. }
  182. /**
  183. * @param $config
  184. *
  185. * @return string
  186. */
  187. function civicrm_config(&$config) {
  188. global $crmPath, $comPath;
  189. global $compileDir;
  190. global $tplPath, $installType;
  191. // Ex: $extraSettings[] = '$civicrm_settings["domain"]["foo"] = "bar";';
  192. $extraSettings = array();
  193. $params = array(
  194. 'crmRoot' => $crmPath,
  195. 'templateCompileDir' => $compileDir,
  196. 'frontEnd' => 0,
  197. 'dbUser' => addslashes($config['mysql']['username']),
  198. 'dbPass' => addslashes($config['mysql']['password']),
  199. 'dbHost' => $config['mysql']['server'],
  200. 'dbName' => addslashes($config['mysql']['database']),
  201. );
  202. $params['baseURL'] = isset($config['base_url']) ? $config['base_url'] : civicrm_cms_base();
  203. if ($installType == 'drupal' && defined('VERSION')) {
  204. if (version_compare(VERSION, '8.0') >= 0) {
  205. $params['cms'] = 'Drupal';
  206. $params['CMSdbUser'] = addslashes($config['drupal']['username']);
  207. $params['CMSdbPass'] = addslashes($config['drupal']['password']);
  208. $params['CMSdbHost'] = $config['drupal']['host'] . ":" . !empty($config['drupal']['port']) ? $config['drupal']['port'] : "3306";
  209. $params['CMSdbName'] = addslashes($config['drupal']['database']);
  210. }
  211. elseif (version_compare(VERSION, '7.0-rc1') >= 0) {
  212. $params['cms'] = 'Drupal';
  213. $params['CMSdbUser'] = addslashes($config['drupal']['username']);
  214. $params['CMSdbPass'] = addslashes($config['drupal']['password']);
  215. $params['CMSdbHost'] = $config['drupal']['server'];
  216. $params['CMSdbName'] = addslashes($config['drupal']['database']);
  217. }
  218. elseif (version_compare(VERSION, '6.0') >= 0) {
  219. $params['cms'] = 'Drupal6';
  220. $params['CMSdbUser'] = addslashes($config['drupal']['username']);
  221. $params['CMSdbPass'] = addslashes($config['drupal']['password']);
  222. $params['CMSdbHost'] = $config['drupal']['server'];
  223. $params['CMSdbName'] = addslashes($config['drupal']['database']);
  224. }
  225. }
  226. elseif ($installType == 'drupal') {
  227. $params['cms'] = $config['cms'];
  228. $params['CMSdbUser'] = addslashes($config['cmsdb']['username']);
  229. $params['CMSdbPass'] = addslashes($config['cmsdb']['password']);
  230. $params['CMSdbHost'] = $config['cmsdb']['server'];
  231. $params['CMSdbName'] = addslashes($config['cmsdb']['database']);
  232. }
  233. elseif ($installType == 'backdrop') {
  234. $params['cms'] = 'Backdrop';
  235. $params['CMSdbUser'] = addslashes($config['backdrop']['username']);
  236. $params['CMSdbPass'] = addslashes($config['backdrop']['password']);
  237. $params['CMSdbHost'] = $config['backdrop']['server'];
  238. $params['CMSdbName'] = addslashes($config['backdrop']['database']);
  239. }
  240. else {
  241. $params['cms'] = 'WordPress';
  242. $params['CMSdbUser'] = addslashes(DB_USER);
  243. $params['CMSdbPass'] = addslashes(DB_PASSWORD);
  244. $params['CMSdbHost'] = DB_HOST;
  245. $params['CMSdbName'] = addslashes(DB_NAME);
  246. // CRM-12386
  247. $params['crmRoot'] = addslashes($params['crmRoot']);
  248. //CRM-16421
  249. $extraSettings[] = sprintf('$civicrm_paths[\'wp.frontend.base\'][\'url\'] = %s;', var_export(home_url() . '/', 1));
  250. $extraSettings[] = sprintf('$civicrm_paths[\'wp.backend.base\'][\'url\'] = %s;', var_export(admin_url(), 1));
  251. $extraSettings[] = sprintf('$civicrm_setting[\'URL Preferences\'][\'userFrameworkResourceURL\'] = %s;', var_export(plugin_dir_url(CIVICRM_PLUGIN_FILE) . 'civicrm', 1));
  252. }
  253. if ($extraSettings) {
  254. $params['extraSettings'] = "Additional settings generated by installer:\n" . implode("\n", $extraSettings);
  255. }
  256. else {
  257. $params['extraSettings'] = "";
  258. }
  259. $params['siteKey'] = md5(rand() . mt_rand() . rand() . uniqid('', TRUE) . $params['baseURL']);
  260. // Would prefer openssl_random_pseudo_bytes(), but I don't think it's universally available.
  261. $str = file_get_contents($tplPath . 'civicrm.settings.php.template');
  262. foreach ($params as $key => $value) {
  263. $str = str_replace('%%' . $key . '%%', $value, $str);
  264. }
  265. return trim($str);
  266. }
  267. /**
  268. * @return string
  269. */
  270. function civicrm_cms_base() {
  271. global $installType;
  272. // for drupal
  273. $numPrevious = 6;
  274. if (isset($_SERVER['HTTPS']) &&
  275. !empty($_SERVER['HTTPS']) &&
  276. strtolower($_SERVER['HTTPS']) != 'off'
  277. ) {
  278. $url = 'https://' . $_SERVER['HTTP_HOST'];
  279. }
  280. else {
  281. $url = 'http://' . $_SERVER['HTTP_HOST'];
  282. }
  283. $baseURL = $_SERVER['SCRIPT_NAME'];
  284. if ($installType == 'drupal' || $installType == 'backdrop') {
  285. //don't assume 6 dir levels, as civicrm
  286. //may or may not be in sites/all/modules/
  287. //lets allow to install in custom dir. CRM-6840
  288. global $cmsPath;
  289. $crmDirLevels = str_replace($cmsPath, '', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
  290. $baseURL = str_replace($crmDirLevels, '', str_replace('\\', '/', $baseURL));
  291. }
  292. elseif ($installType == 'wordpress') {
  293. $baseURL = str_replace($url, '', site_url());
  294. }
  295. else {
  296. for ($i = 1; $i <= $numPrevious; $i++) {
  297. $baseURL = dirname($baseURL);
  298. }
  299. }
  300. // remove the last directory separator string from the directory
  301. if (substr($baseURL, -1, 1) == DIRECTORY_SEPARATOR) {
  302. $baseURL = substr($baseURL, 0, -1);
  303. }
  304. // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze
  305. $baseURL = str_replace(DIRECTORY_SEPARATOR, '/', $baseURL);
  306. if ($baseURL != '/') {
  307. $baseURL .= '/';
  308. }
  309. return $url . $baseURL;
  310. }
  311. /**
  312. * @return string
  313. */
  314. function civicrm_home_url() {
  315. $drupalURL = civicrm_cms_base();
  316. return $drupalURL . 'index.php?q=civicrm';
  317. }