PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/KatyMoon/cr
PHP | 248 lines | 160 code | 46 blank | 42 comment | 29 complexity | 63f439fcadbcf3cedcc588c54d4b5247 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.3 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2010 |
  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-2010
  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;
  67. $siteDir = isset( $config['site_dir'] ) ? $config['site_dir'] : getSiteDir( $cmsPath, $_SERVER['SCRIPT_FILENAME'] );
  68. civicrm_setup( $cmsPath . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR .
  69. $siteDir . DIRECTORY_SEPARATOR . 'files' );
  70. $dsn = "mysql://{$config['mysql']['username']}:{$config['mysql']['password']}@{$config['mysql']['server']}/{$config['mysql']['database']}?new_link=true";
  71. civicrm_source( $dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm.mysql' );
  72. if ( isset( $config['loadGenerated'] ) &&
  73. $config['loadGenerated'] ) {
  74. civicrm_source( $dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_generated.mysql', true );
  75. } else {
  76. if (isset($config['seedLanguage'])
  77. and preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $config['seedLanguage'])
  78. and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql")
  79. and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql" )) {
  80. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql");
  81. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql" );
  82. } else {
  83. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_data.mysql');
  84. civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_acl.mysql' );
  85. }
  86. }
  87. // generate backend settings file
  88. $configFile =
  89. $cmsPath . DIRECTORY_SEPARATOR .
  90. 'sites' . DIRECTORY_SEPARATOR .
  91. $siteDir . DIRECTORY_SEPARATOR .
  92. 'civicrm.settings.php';
  93. $string = civicrm_config( $config );
  94. civicrm_write_file( $configFile,
  95. $string );
  96. }
  97. function civicrm_source( $dsn, $fileName, $lineMode = false ) {
  98. global $crmPath;
  99. require_once "$crmPath/packages/DB.php";
  100. $db =& DB::connect( $dsn );
  101. if ( PEAR::isError( $db ) ) {
  102. die( "Cannot open $dsn: " . $db->getMessage( ) );
  103. }
  104. if ( ! $lineMode ) {
  105. $string = file_get_contents( $fileName );
  106. // change \r\n to fix windows issues
  107. $string = str_replace("\r\n", "\n", $string );
  108. //get rid of comments starting with # and --
  109. $string = preg_replace("/^#[^\n]*$/m", "\n", $string );
  110. $string = preg_replace("/^(--[^-]).*/m", "\n", $string );
  111. $queries = preg_split('/;$/m', $string);
  112. foreach ( $queries as $query ) {
  113. $query = trim( $query );
  114. if ( ! empty( $query ) ) {
  115. $res =& $db->query( $query );
  116. if ( PEAR::isError( $res ) ) {
  117. die( "Cannot execute $query: " . $res->getMessage( ) );
  118. }
  119. }
  120. }
  121. } else {
  122. $fd = fopen( $fileName, "r" );
  123. while ( $string = fgets( $fd ) ) {
  124. $string = preg_replace("/^#[^\n]*$/m", "\n", $string );
  125. $string = preg_replace("/^(--[^-]).*/m", "\n", $string );
  126. $string = trim( $string );
  127. if ( ! empty( $string ) ) {
  128. $res =& $db->query( $string );
  129. if ( PEAR::isError( $res ) ) {
  130. die( "Cannot execute $string: " . $res->getMessage( ) );
  131. }
  132. }
  133. }
  134. }
  135. }
  136. function civicrm_config( &$config ) {
  137. global $crmPath, $comPath;
  138. global $compileDir;
  139. global $tplPath;
  140. $params = array(
  141. 'crmRoot' => $crmPath,
  142. 'templateCompileDir' => $compileDir,
  143. 'frontEnd' => 0,
  144. 'dbUser' => $config['mysql']['username'],
  145. 'dbPass' => $config['mysql']['password'],
  146. 'dbHost' => $config['mysql']['server'],
  147. 'dbName' => $config['mysql']['database'],
  148. );
  149. $params['cms'] = 'Drupal';
  150. $params['baseURL'] = isset($config['base_url']) ? $config['base_url'] : civicrm_cms_base( );
  151. $params['CMSdbUser'] = $config['drupal']['username'];
  152. $params['CMSdbPass'] = $config['drupal']['password'];
  153. $params['CMSdbHost'] = $config['drupal']['server'];
  154. $params['CMSdbName'] = $config['drupal']['database'];
  155. $str = file_get_contents( $tplPath . 'civicrm.settings.php.tpl' );
  156. foreach ( $params as $key => $value ) {
  157. $str = str_replace( '%%' . $key . '%%', $value, $str );
  158. }
  159. return trim( $str );
  160. }
  161. function civicrm_cms_base( ) {
  162. global $installType;
  163. // for drupal
  164. $numPrevious = 6;
  165. // for standalone
  166. if ( $installType == 'standalone' ) {
  167. $numPrevious = 2;
  168. }
  169. if ( ! isset( $_SERVER['HTTPS'] ) ||
  170. strtolower( $_SERVER['HTTPS'] ) == 'off' ) {
  171. $url = 'http://' . $_SERVER['HTTP_HOST'];
  172. } else {
  173. $url = 'https://' . $_SERVER['HTTP_HOST'];
  174. }
  175. $baseURL = $_SERVER['SCRIPT_NAME'];
  176. if ( $installType == 'drupal' ) {
  177. //don't assume 6 dir levels, as civicrm
  178. //may or may not be in sites/all/modules/
  179. //lets allow to install in custom dir. CRM-6840
  180. global $cmsPath;
  181. $crmDirLevels = str_replace( $cmsPath, '', str_replace( '\\', '/', $_SERVER['SCRIPT_FILENAME'] ) );
  182. $baseURL = str_replace( $crmDirLevels, '', str_replace( '\\', '/', $baseURL ) );
  183. } else {
  184. for ( $i = 1; $i <= $numPrevious; $i++ ) {
  185. $baseURL = dirname( $baseURL );
  186. }
  187. }
  188. // remove the last directory separator string from the directory
  189. if ( substr( $baseURL, -1, 1 ) == DIRECTORY_SEPARATOR ) {
  190. $baseURL = substr( $baseURL, 0, -1 );
  191. }
  192. // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze
  193. $baseURL = str_replace( DIRECTORY_SEPARATOR, '/', $baseURL );
  194. if ( $baseURL != '/' ) {
  195. $baseURL .= '/';
  196. }
  197. return $url . $baseURL;
  198. }
  199. function civicrm_home_url( ) {
  200. $drupalURL = civicrm_cms_base( );
  201. return $drupalURL . 'index.php?q=civicrm';
  202. }