3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 function civicrm_setup($filesDirectory) {
36 global $crmPath, $sqlPath, $pkgPath, $tplPath;
39 $pkgPath = $crmPath . DIRECTORY_SEPARATOR
. 'packages';
40 set_include_path($crmPath . PATH_SEPARATOR
.
41 $pkgPath . PATH_SEPARATOR
.
45 $sqlPath = $crmPath . DIRECTORY_SEPARATOR
. 'sql';
46 $tplPath = $crmPath . DIRECTORY_SEPARATOR
. 'templates' . DIRECTORY_SEPARATOR
. 'CRM' . DIRECTORY_SEPARATOR
. 'common' . DIRECTORY_SEPARATOR
;
48 if (!is_dir($filesDirectory)) {
49 mkdir($filesDirectory, 0777);
50 chmod($filesDirectory, 0777);
53 $scratchDir = $filesDirectory . DIRECTORY_SEPARATOR
. 'civicrm';
54 if (!is_dir($scratchDir)) {
55 mkdir($scratchDir, 0777);
58 $compileDir = $scratchDir . DIRECTORY_SEPARATOR
. 'templates_c' . DIRECTORY_SEPARATOR
;
59 if (!is_dir($compileDir)) {
60 mkdir($compileDir, 0777);
62 $compileDir = addslashes($compileDir);
69 function civicrm_write_file($name, &$buffer) {
70 $fd = fopen($name, "w");
72 die("Cannot open $name");
81 function civicrm_main(&$config) {
82 global $sqlPath, $crmPath, $cmsPath, $installType;
84 if ($installType == 'drupal') {
85 $siteDir = isset($config['site_dir']) ?
$config['site_dir'] : getSiteDir($cmsPath, $_SERVER['SCRIPT_FILENAME']);
86 civicrm_setup($cmsPath . DIRECTORY_SEPARATOR
. 'sites' . DIRECTORY_SEPARATOR
.
87 $siteDir . DIRECTORY_SEPARATOR
. 'files'
90 elseif ($installType == 'wordpress') {
91 civicrm_setup(WP_PLUGIN_DIR
. DIRECTORY_SEPARATOR
. 'files');
94 $dsn = "mysql://{$config['mysql']['username']}:{$config['mysql']['password']}@{$config['mysql']['server']}/{$config['mysql']['database']}?new_link=true";
96 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. 'civicrm.mysql');
98 if (!empty($config['loadGenerated'])) {
99 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. 'civicrm_generated.mysql', TRUE);
102 if (isset($config['seedLanguage'])
103 and preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $config['seedLanguage'])
104 and file_exists($sqlPath . DIRECTORY_SEPARATOR
. "civicrm_data.{$config['seedLanguage']}.mysql")
105 and file_exists($sqlPath . DIRECTORY_SEPARATOR
. "civicrm_acl.{$config['seedLanguage']}.mysql")
107 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. "civicrm_data.{$config['seedLanguage']}.mysql");
108 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. "civicrm_acl.{$config['seedLanguage']}.mysql");
111 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. 'civicrm_data.mysql');
112 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. 'civicrm_acl.mysql');
116 // generate backend settings file
117 if ($installType == 'drupal') {
118 $configFile = $cmsPath . DIRECTORY_SEPARATOR
. 'sites' . DIRECTORY_SEPARATOR
. $siteDir . DIRECTORY_SEPARATOR
. 'civicrm.settings.php';
120 elseif ($installType == 'wordpress') {
121 $configFile = $cmsPath . DIRECTORY_SEPARATOR
. 'civicrm.settings.php';
124 $string = civicrm_config($config);
125 civicrm_write_file($configFile,
134 * @param bool $lineMode
136 function civicrm_source($dsn, $fileName, $lineMode = FALSE) {
139 require_once "$crmPath/packages/DB.php";
141 $db = DB
::connect($dsn);
142 if (PEAR
::isError($db)) {
143 die("Cannot open $dsn: " . $db->getMessage());
145 $db->query("SET NAMES utf8");
147 $db->query("SET NAMES utf8");
150 $string = file_get_contents($fileName);
152 // change \r\n to fix windows issues
153 $string = str_replace("\r\n", "\n", $string);
155 //get rid of comments starting with # and --
157 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
158 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
160 $queries = preg_split('/;\s*$/m', $string);
161 foreach ($queries as $query) {
162 $query = trim($query);
163 if (!empty($query)) {
164 $res = &$db->query($query);
165 if (PEAR
::isError($res)) {
167 die("Cannot execute $query: " . $res->getMessage());
173 $fd = fopen($fileName, "r");
174 while ($string = fgets($fd)) {
175 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
176 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
178 $string = trim($string);
179 if (!empty($string)) {
180 $res = &$db->query($string);
181 if (PEAR
::isError($res)) {
182 die("Cannot execute $string: " . $res->getMessage());
194 function civicrm_config(&$config) {
195 global $crmPath, $comPath;
197 global $tplPath, $installType;
200 'crmRoot' => $crmPath,
201 'templateCompileDir' => $compileDir,
203 'dbUser' => addslashes($config['mysql']['username']),
204 'dbPass' => addslashes($config['mysql']['password']),
205 'dbHost' => $config['mysql']['server'],
206 'dbName' => addslashes($config['mysql']['database']),
209 $params['baseURL'] = isset($config['base_url']) ?
$config['base_url'] : civicrm_cms_base();
210 if ($installType == 'drupal') {
211 if (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']);
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']);
227 $params['cms'] = 'WordPress';
228 $params['CMSdbUser'] = addslashes(DB_USER
);
229 $params['CMSdbPass'] = addslashes(DB_PASSWORD
);
230 $params['CMSdbHost'] = DB_HOST
;
231 $params['CMSdbName'] = addslashes(DB_NAME
);
234 $params['crmRoot'] = addslashes($params['crmRoot']);
237 $params['siteKey'] = md5(uniqid('', TRUE) . $params['baseURL']);
239 $str = file_get_contents($tplPath . 'civicrm.settings.php.template');
240 foreach ($params as $key => $value) {
241 $str = str_replace('%%' . $key . '%%', $value, $str);
249 function civicrm_cms_base() {
255 if (isset($_SERVER['HTTPS']) &&
256 !empty($_SERVER['HTTPS']) &&
257 strtolower($_SERVER['HTTPS']) != 'off'
259 $url = 'https://' . $_SERVER['HTTP_HOST'];
262 $url = 'http://' . $_SERVER['HTTP_HOST'];
265 $baseURL = $_SERVER['SCRIPT_NAME'];
267 if ($installType == 'drupal') {
268 //don't assume 6 dir levels, as civicrm
269 //may or may not be in sites/all/modules/
270 //lets allow to install in custom dir. CRM-6840
272 $crmDirLevels = str_replace($cmsPath, '', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
273 $baseURL = str_replace($crmDirLevels, '', str_replace('\\', '/', $baseURL));
275 elseif ($installType == 'wordpress') {
276 $baseURL = str_replace($url, '', site_url());
279 for ($i = 1; $i <= $numPrevious; $i++
) {
280 $baseURL = dirname($baseURL);
284 // remove the last directory separator string from the directory
285 if (substr($baseURL, -1, 1) == DIRECTORY_SEPARATOR
) {
286 $baseURL = substr($baseURL, 0, -1);
289 // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze
290 $baseURL = str_replace(DIRECTORY_SEPARATOR
, '/', $baseURL);
292 if ($baseURL != '/') {
296 return $url . $baseURL;
302 function civicrm_home_url() {
303 $drupalURL = civicrm_cms_base();
304 return $drupalURL . 'index.php?q=civicrm';