PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/michaelmcandrew/lbc
PHP | 270 lines | 187 code | 42 blank | 41 comment | 42 complexity | 2b9e95ca2e166790318c331458c3ead6 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.1 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  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-2011
  31. * $Id$
  32. *
  33. */
  34. function civicrm_setup( $filesDirectory ) {
  35. global $crmPath, $sqlPath, $pkgPath, $tplPath;
  36. global $compileDir;
  37. $pkgPath = $crmPath . DIRECTORY_SEPARATOR . 'packages';
  38. set_include_path( $crmPath . PATH_SEPARATOR .
  39. $pkgPath . PATH_SEPARATOR .
  40. get_include_path( ) );
  41. $sqlPath = $crmPath . DIRECTORY_SEPARATOR . 'sql';
  42. $tplPath = $crmPath . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'CRM' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR;
  43. if ( ! is_dir( $filesDirectory ) ) {
  44. mkdir( $filesDirectory, 0777 );
  45. chmod( $filesDirectory, 0777 );
  46. }
  47. $scratchDir = $filesDirectory . DIRECTORY_SEPARATOR . 'civicrm';
  48. if ( ! is_dir( $scratchDir ) ) {
  49. mkdir( $scratchDir, 0777 );
  50. }
  51. $compileDir = $scratchDir . DIRECTORY_SEPARATOR . 'templates_c' . DIRECTORY_SEPARATOR;
  52. if ( ! is_dir( $compileDir ) ) {
  53. mkdir( $compileDir, 0777 );
  54. }
  55. $compileDir = addslashes( $compileDir );
  56. }
  57. function civicrm_write_file( $name, &$buffer ) {
  58. $fd = fopen( $name, "w" );
  59. if ( ! $fd ) {
  60. die( "Cannot open $name" );
  61. }
  62. fputs( $fd, $buffer );
  63. fclose( $fd );
  64. }
  65. function civicrm_main( &$config ) {
  66. global $sqlPath, $crmPath, $cmsPath, $installType;
  67. if ( $installType == 'drupal' ) {
  68. $siteDir = isset( $config['site_dir'] ) ? $config['site_dir'] : getSiteDir( $cmsPath, $_SERVER['SCRIPT_FILENAME'] );
  69. civicrm_setup( $cmsPath . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR .
  70. $siteDir . DIRECTORY_SEPARATOR . 'files' );
  71. } elseif ( $installType == 'wordpress' ) {
  72. civicrm_setup( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'files' );
  73. }
  74. $dsn = "mysql://{$config['mysql']['username']}:{$config['mysql']['password']}@{$config['mysql']['server']}/{$config['mysql']['database']}?new_link=true";
  75. civicrm_source( $dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm.mysql' );
  76. if ( isset( $config['loadGenerated'] ) &&
  77. $config['loadGenerated'] ) {
  78. civicrm_source( $dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_generated.mysql', true );
  79. } else {
  80. if (isset($config['seedLanguage'])
  81. and preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $config['seedLanguage'])
  82. and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql")
  83. and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql" )) {
  84. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql");
  85. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql" );
  86. } else {
  87. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_data.mysql');
  88. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_acl.mysql' );
  89. }
  90. }
  91. // generate backend settings file
  92. if ( $installType == 'drupal' ) {
  93. $configFile =
  94. $cmsPath . DIRECTORY_SEPARATOR .
  95. 'sites' . DIRECTORY_SEPARATOR .
  96. $siteDir . DIRECTORY_SEPARATOR .
  97. 'civicrm.settings.php';
  98. } elseif( $installType == 'wordpress' ) {
  99. $configFile =
  100. $cmsPath . DIRECTORY_SEPARATOR .
  101. 'civicrm.settings.php';
  102. }
  103. $string = civicrm_config( $config );
  104. civicrm_write_file( $configFile,
  105. $string );
  106. }
  107. function civicrm_source( $dsn, $fileName, $lineMode = false ) {
  108. global $crmPath;
  109. require_once "$crmPath/packages/DB.php";
  110. $db = DB::connect( $dsn );
  111. if ( PEAR::isError( $db ) ) {
  112. die( "Cannot open $dsn: " . $db->getMessage( ) );
  113. }
  114. if ( ! $lineMode ) {
  115. $string = file_get_contents( $fileName );
  116. // change \r\n to fix windows issues
  117. $string = str_replace("\r\n", "\n", $string );
  118. //get rid of comments starting with # and --
  119. $string = preg_replace("/^#[^\n]*$/m", "\n", $string );
  120. $string = preg_replace("/^(--[^-]).*/m", "\n", $string );
  121. $queries = preg_split('/;\s*$/m', $string);
  122. foreach ( $queries as $query ) {
  123. $query = trim( $query );
  124. if ( ! empty( $query ) ) {
  125. $res =& $db->query( $query );
  126. if ( PEAR::isError( $res ) ) {
  127. print_r( $res );
  128. die( "Cannot execute $query: " . $res->getMessage( ) );
  129. }
  130. }
  131. }
  132. } else {
  133. $fd = fopen( $fileName, "r" );
  134. while ( $string = fgets( $fd ) ) {
  135. $string = preg_replace("/^#[^\n]*$/m", "\n", $string );
  136. $string = preg_replace("/^(--[^-]).*/m", "\n", $string );
  137. $string = trim( $string );
  138. if ( ! empty( $string ) ) {
  139. $res =& $db->query( $string );
  140. if ( PEAR::isError( $res ) ) {
  141. die( "Cannot execute $string: " . $res->getMessage( ) );
  142. }
  143. }
  144. }
  145. }
  146. }
  147. function civicrm_config( &$config ) {
  148. global $crmPath, $comPath;
  149. global $compileDir;
  150. global $tplPath, $installType;
  151. $params = array(
  152. 'crmRoot' => $crmPath,
  153. 'templateCompileDir' => $compileDir,
  154. 'frontEnd' => 0,
  155. 'dbUser' => $config['mysql']['username'],
  156. 'dbPass' => $config['mysql']['password'],
  157. 'dbHost' => $config['mysql']['server'],
  158. 'dbName' => $config['mysql']['database'],
  159. );
  160. $params['baseURL'] = isset($config['base_url']) ? $config['base_url'] : civicrm_cms_base( );
  161. if ( $installType == 'drupal' &&
  162. version_compare(VERSION, '7.0-rc1') >= 0 ) {
  163. $params['cms'] = 'Drupal';
  164. $params['CMSdbUser'] = $config['drupal']['username'];
  165. $params['CMSdbPass'] = $config['drupal']['password'];
  166. $params['CMSdbHost'] = $config['drupal']['server'];
  167. $params['CMSdbName'] = $config['drupal']['database'];
  168. } elseif ( $installType == 'drupal' &&
  169. version_compare(VERSION, '6.0') >= 0 ) {
  170. $params['cms'] = 'Drupal6';
  171. $params['CMSdbUser'] = $config['drupal']['username'];
  172. $params['CMSdbPass'] = $config['drupal']['password'];
  173. $params['CMSdbHost'] = $config['drupal']['server'];
  174. $params['CMSdbName'] = $config['drupal']['database'];
  175. } else {
  176. $params['cms'] = 'WordPress';
  177. $params['CMSdbUser'] = DB_USER;
  178. $params['CMSdbPass'] = DB_PASSWORD;
  179. $params['CMSdbHost'] = DB_HOST;
  180. $params['CMSdbName'] = DB_NAME;
  181. }
  182. $params['siteKey'] = md5(uniqid( '', true ) . $params['baseURL']);
  183. $str = file_get_contents( $tplPath . 'civicrm.settings.php.tpl' );
  184. foreach ( $params as $key => $value ) {
  185. $str = str_replace( '%%' . $key . '%%', $value, $str );
  186. }
  187. return trim( $str );
  188. }
  189. function civicrm_cms_base( ) {
  190. global $installType;
  191. // for drupal
  192. $numPrevious = 6;
  193. if ( ! isset( $_SERVER['HTTPS'] ) ||
  194. strtolower( $_SERVER['HTTPS'] ) == 'off' ) {
  195. $url = 'http://' . $_SERVER['HTTP_HOST'];
  196. } else {
  197. $url = 'https://' . $_SERVER['HTTP_HOST'];
  198. }
  199. $baseURL = $_SERVER['SCRIPT_NAME'];
  200. if ( $installType == 'drupal' ) {
  201. //don't assume 6 dir levels, as civicrm
  202. //may or may not be in sites/all/modules/
  203. //lets allow to install in custom dir. CRM-6840
  204. global $cmsPath;
  205. $crmDirLevels = str_replace( $cmsPath, '', str_replace( '\\', '/', $_SERVER['SCRIPT_FILENAME'] ) );
  206. $baseURL = str_replace( $crmDirLevels, '', str_replace( '\\', '/', $baseURL ) );
  207. } else if ( $installType == 'wordpress' ) {
  208. $baseURL = str_replace( $url, '', site_url() );
  209. } else {
  210. for ( $i = 1; $i <= $numPrevious; $i++ ) {
  211. $baseURL = dirname( $baseURL );
  212. }
  213. }
  214. // remove the last directory separator string from the directory
  215. if ( substr( $baseURL, -1, 1 ) == DIRECTORY_SEPARATOR ) {
  216. $baseURL = substr( $baseURL, 0, -1 );
  217. }
  218. // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze
  219. $baseURL = str_replace( DIRECTORY_SEPARATOR, '/', $baseURL );
  220. if ( $baseURL != '/' ) {
  221. $baseURL .= '/';
  222. }
  223. return $url . $baseURL;
  224. }
  225. function civicrm_home_url( ) {
  226. $drupalURL = civicrm_cms_base( );
  227. return $drupalURL . 'index.php?q=civicrm';
  228. }