3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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);
65 function civicrm_write_file($name, &$buffer) {
66 $fd = fopen($name, "w");
68 die("Cannot open $name");
74 function civicrm_main(&$config) {
75 global $sqlPath, $crmPath, $cmsPath, $installType;
77 if ($installType == 'drupal') {
78 $siteDir = isset($config['site_dir']) ?
$config['site_dir'] : getSiteDir($cmsPath, $_SERVER['SCRIPT_FILENAME']);
79 civicrm_setup($cmsPath . DIRECTORY_SEPARATOR
. 'sites' . DIRECTORY_SEPARATOR
.
80 $siteDir . DIRECTORY_SEPARATOR
. 'files'
83 elseif ($installType == 'wordpress') {
84 civicrm_setup(WP_PLUGIN_DIR
. DIRECTORY_SEPARATOR
. 'files');
87 $dsn = "mysql://{$config['mysql']['username']}:{$config['mysql']['password']}@{$config['mysql']['server']}/{$config['mysql']['database']}?new_link=true";
89 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. 'civicrm.mysql');
91 if (!empty($config['loadGenerated'])) {
92 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. 'civicrm_generated.mysql', TRUE);
95 if (isset($config['seedLanguage'])
96 and preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $config['seedLanguage'])
97 and file_exists($sqlPath . DIRECTORY_SEPARATOR
. "civicrm_data.{$config['seedLanguage']}.mysql")
98 and file_exists($sqlPath . DIRECTORY_SEPARATOR
. "civicrm_acl.{$config['seedLanguage']}.mysql")
100 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. "civicrm_data.{$config['seedLanguage']}.mysql");
101 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. "civicrm_acl.{$config['seedLanguage']}.mysql");
104 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. 'civicrm_data.mysql');
105 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. 'civicrm_acl.mysql');
109 // generate backend settings file
110 if ($installType == 'drupal') {
111 $configFile = $cmsPath . DIRECTORY_SEPARATOR
. 'sites' . DIRECTORY_SEPARATOR
. $siteDir . DIRECTORY_SEPARATOR
. 'civicrm.settings.php';
113 elseif ($installType == 'wordpress') {
114 $configFile = $cmsPath . DIRECTORY_SEPARATOR
. 'civicrm.settings.php';
117 $string = civicrm_config($config);
118 civicrm_write_file($configFile,
124 function civicrm_source($dsn, $fileName, $lineMode = FALSE) {
127 require_once "$crmPath/packages/DB.php";
129 $db = DB
::connect($dsn);
130 if (PEAR
::isError($db)) {
131 die("Cannot open $dsn: " . $db->getMessage());
135 $string = file_get_contents($fileName);
137 // change \r\n to fix windows issues
138 $string = str_replace("\r\n", "\n", $string);
140 //get rid of comments starting with # and --
142 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
143 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
145 $queries = preg_split('/;\s*$/m', $string);
146 foreach ($queries as $query) {
147 $query = trim($query);
148 if (!empty($query)) {
149 $res = &$db->query($query);
150 if (PEAR
::isError($res)) {
152 die("Cannot execute $query: " . $res->getMessage());
158 $fd = fopen($fileName, "r");
159 while ($string = fgets($fd)) {
160 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
161 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
163 $string = trim($string);
164 if (!empty($string)) {
165 $res = &$db->query($string);
166 if (PEAR
::isError($res)) {
167 die("Cannot execute $string: " . $res->getMessage());
174 function civicrm_config(&$config) {
175 global $crmPath, $comPath;
177 global $tplPath, $installType;
180 'crmRoot' => $crmPath,
181 'templateCompileDir' => $compileDir,
183 'dbUser' => addslashes($config['mysql']['username']),
184 'dbPass' => addslashes($config['mysql']['password']),
185 'dbHost' => $config['mysql']['server'],
186 'dbName' => addslashes($config['mysql']['database']),
189 $params['baseURL'] = isset($config['base_url']) ?
$config['base_url'] : civicrm_cms_base();
190 if ($installType == 'drupal') {
191 if (version_compare(VERSION
, '7.0-rc1') >= 0) {
192 $params['cms'] = 'Drupal';
193 $params['CMSdbUser'] = addslashes($config['drupal']['username']);
194 $params['CMSdbPass'] = addslashes($config['drupal']['password']);
195 $params['CMSdbHost'] = $config['drupal']['server'];
196 $params['CMSdbName'] = addslashes($config['drupal']['database']);
198 elseif (version_compare(VERSION
, '6.0') >= 0) {
199 $params['cms'] = 'Drupal6';
200 $params['CMSdbUser'] = addslashes($config['drupal']['username']);
201 $params['CMSdbPass'] = addslashes($config['drupal']['password']);
202 $params['CMSdbHost'] = $config['drupal']['server'];
203 $params['CMSdbName'] = addslashes($config['drupal']['database']);
207 $params['cms'] = 'WordPress';
208 $params['CMSdbUser'] = addslashes(DB_USER
);
209 $params['CMSdbPass'] = addslashes(DB_PASSWORD
);
210 $params['CMSdbHost'] = DB_HOST
;
211 $params['CMSdbName'] = addslashes(DB_NAME
);
214 $params['crmRoot'] = addslashes($params['crmRoot']);
217 $params['siteKey'] = md5(uniqid('', TRUE) . $params['baseURL']);
219 $str = file_get_contents($tplPath . 'civicrm.settings.php.template');
220 foreach ($params as $key => $value) {
221 $str = str_replace('%%' . $key . '%%', $value, $str);
226 function civicrm_cms_base() {
232 if (isset($_SERVER['HTTPS']) &&
233 !empty($_SERVER['HTTPS']) &&
234 strtolower($_SERVER['HTTPS']) != 'off'
236 $url = 'https://' . $_SERVER['HTTP_HOST'];
239 $url = 'http://' . $_SERVER['HTTP_HOST'];
242 $baseURL = $_SERVER['SCRIPT_NAME'];
244 if ($installType == 'drupal') {
245 //don't assume 6 dir levels, as civicrm
246 //may or may not be in sites/all/modules/
247 //lets allow to install in custom dir. CRM-6840
249 $crmDirLevels = str_replace($cmsPath, '', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
250 $baseURL = str_replace($crmDirLevels, '', str_replace('\\', '/', $baseURL));
252 elseif ($installType == 'wordpress') {
253 $baseURL = str_replace($url, '', site_url());
256 for ($i = 1; $i <= $numPrevious; $i++
) {
257 $baseURL = dirname($baseURL);
261 // remove the last directory separator string from the directory
262 if (substr($baseURL, -1, 1) == DIRECTORY_SEPARATOR
) {
263 $baseURL = substr($baseURL, 0, -1);
266 // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze
267 $baseURL = str_replace(DIRECTORY_SEPARATOR
, '/', $baseURL);
269 if ($baseURL != '/') {
273 return $url . $baseURL;
276 function civicrm_home_url() {
277 $drupalURL = civicrm_cms_base();
278 return $drupalURL . 'index.php?q=civicrm';