3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
33 * @param $filesDirectory
35 function civicrm_setup($filesDirectory) {
36 global $crmPath, $sqlPath, $pkgPath, $tplPath;
40 // This is needed to allow CiviCRM to be installed by drush.
41 // TODO: move to civicrm.drush.inc drush_civicrm_install()
43 require_once $crmPath . '/CRM/Core/ClassLoader.php';
44 CRM_Core_ClassLoader
::singleton()->register();
46 $sqlPath = $crmPath . DIRECTORY_SEPARATOR
. 'sql';
47 $tplPath = $crmPath . DIRECTORY_SEPARATOR
. 'templates' . DIRECTORY_SEPARATOR
. 'CRM' . DIRECTORY_SEPARATOR
. 'common' . DIRECTORY_SEPARATOR
;
49 if (!is_dir($filesDirectory)) {
50 mkdir($filesDirectory, 0777);
51 chmod($filesDirectory, 0777);
54 $scratchDir = $filesDirectory . DIRECTORY_SEPARATOR
. 'civicrm';
55 if (!is_dir($scratchDir)) {
56 mkdir($scratchDir, 0777);
59 $compileDir = $scratchDir . DIRECTORY_SEPARATOR
. 'templates_c' . DIRECTORY_SEPARATOR
;
60 if (!is_dir($compileDir)) {
61 mkdir($compileDir, 0777);
63 $compileDir = addslashes($compileDir);
70 function civicrm_write_file($name, &$buffer) {
71 $fd = fopen($name, "w");
73 die("Cannot open $name");
82 function civicrm_main(&$config) {
83 global $sqlPath, $crmPath, $cmsPath, $installType;
85 if ($installType == 'drupal') {
86 $siteDir = isset($config['site_dir']) ?
$config['site_dir'] : getSiteDir($cmsPath, $_SERVER['SCRIPT_FILENAME']);
87 civicrm_setup($cmsPath . DIRECTORY_SEPARATOR
. 'sites' . DIRECTORY_SEPARATOR
.
88 $siteDir . DIRECTORY_SEPARATOR
. 'files'
91 elseif ($installType == 'backdrop') {
92 civicrm_setup($cmsPath . DIRECTORY_SEPARATOR
. 'files');
94 elseif ($installType == 'wordpress') {
95 $upload_dir = wp_upload_dir();
96 $files_dirname = $upload_dir['basedir'];
97 civicrm_setup($files_dirname);
100 $parts = explode(':', $config['mysql']['server']);
101 if (empty($parts[1])) {
104 $config['mysql']['server'] = implode(':', $parts);
106 $dsn = "mysql://{$config['mysql']['username']}:{$config['mysql']['password']}@{$config['mysql']['server']}/{$config['mysql']['database']}?new_link=true";
107 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. 'civicrm.mysql');
109 if (!empty($config['loadGenerated'])) {
110 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. 'civicrm_generated.mysql', TRUE);
113 if (isset($config['seedLanguage'])
114 and preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $config['seedLanguage'])
115 and file_exists($sqlPath . DIRECTORY_SEPARATOR
. "civicrm_data.{$config['seedLanguage']}.mysql")
116 and file_exists($sqlPath . DIRECTORY_SEPARATOR
. "civicrm_acl.{$config['seedLanguage']}.mysql")
118 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. "civicrm_data.{$config['seedLanguage']}.mysql");
119 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. "civicrm_acl.{$config['seedLanguage']}.mysql");
122 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. 'civicrm_data.mysql');
123 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR
. 'civicrm_acl.mysql');
127 // generate backend settings file
128 if ($installType == 'drupal') {
129 $configFile = $cmsPath . DIRECTORY_SEPARATOR
. 'sites' . DIRECTORY_SEPARATOR
. $siteDir . DIRECTORY_SEPARATOR
. 'civicrm.settings.php';
131 elseif ($installType == 'backdrop') {
132 $configFile = $cmsPath . DIRECTORY_SEPARATOR
. 'civicrm.settings.php';
134 elseif ($installType == 'wordpress') {
135 $configFile = $files_dirname . DIRECTORY_SEPARATOR
. 'civicrm' . DIRECTORY_SEPARATOR
. 'civicrm.settings.php';
138 $string = civicrm_config($config);
139 civicrm_write_file($configFile,
147 * @param string $fileName
148 * @param bool $lineMode
150 function civicrm_source($dsn, $fileName, $lineMode = FALSE) {
153 require_once "$crmPath/packages/DB.php";
155 // CRM-19699 See also CRM_Core_DAO for PHP7 mysqli compatiblity.
156 // Duplicated here because this is not using CRM_Core_DAO directly
157 // and this function may be called directly from Drush.
158 if (!defined('DB_DSN_MODE')) {
159 define('DB_DSN_MODE', 'auto');
162 $db = DB
::connect($dsn);
163 if (PEAR
::isError($db)) {
164 die("Cannot open $dsn: " . $db->getMessage());
166 $db->query("SET NAMES utf8");
168 $db->query("SET NAMES utf8");
171 $string = file_get_contents($fileName);
173 // change \r\n to fix windows issues
174 $string = str_replace("\r\n", "\n", $string);
176 //get rid of comments starting with # and --
178 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
179 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
181 $queries = preg_split('/;\s*$/m', $string);
182 foreach ($queries as $query) {
183 $query = trim($query);
184 if (!empty($query)) {
185 $res = &$db->query($query);
186 if (PEAR
::isError($res)) {
188 die("Cannot execute $query: " . $res->getMessage());
194 $fd = fopen($fileName, "r");
195 while ($string = fgets($fd)) {
196 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
197 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
199 $string = trim($string);
200 if (!empty($string)) {
201 $res = &$db->query($string);
202 if (PEAR
::isError($res)) {
203 die("Cannot execute $string: " . $res->getMessage());
215 function civicrm_config(&$config) {
216 global $crmPath, $comPath;
218 global $tplPath, $installType;
221 'crmRoot' => $crmPath,
222 'templateCompileDir' => $compileDir,
224 'dbUser' => addslashes($config['mysql']['username']),
225 'dbPass' => addslashes($config['mysql']['password']),
226 'dbHost' => $config['mysql']['server'],
227 'dbName' => addslashes($config['mysql']['database']),
230 $params['baseURL'] = isset($config['base_url']) ?
$config['base_url'] : civicrm_cms_base();
231 if ($installType == 'drupal' && defined('VERSION')) {
232 if (version_compare(VERSION
, '8.0') >= 0) {
233 $params['cms'] = 'Drupal';
234 $params['CMSdbUser'] = addslashes($config['drupal']['username']);
235 $params['CMSdbPass'] = addslashes($config['drupal']['password']);
236 $params['CMSdbHost'] = $config['drupal']['host'] . ":" . !empty($config['drupal']['port']) ?
$config['drupal']['port'] : "3306";
237 $params['CMSdbName'] = addslashes($config['drupal']['database']);
239 elseif (version_compare(VERSION
, '7.0-rc1') >= 0) {
240 $params['cms'] = 'Drupal';
241 $params['CMSdbUser'] = addslashes($config['drupal']['username']);
242 $params['CMSdbPass'] = addslashes($config['drupal']['password']);
243 $params['CMSdbHost'] = $config['drupal']['server'];
244 $params['CMSdbName'] = addslashes($config['drupal']['database']);
246 elseif (version_compare(VERSION
, '6.0') >= 0) {
247 $params['cms'] = 'Drupal6';
248 $params['CMSdbUser'] = addslashes($config['drupal']['username']);
249 $params['CMSdbPass'] = addslashes($config['drupal']['password']);
250 $params['CMSdbHost'] = $config['drupal']['server'];
251 $params['CMSdbName'] = addslashes($config['drupal']['database']);
254 elseif ($installType == 'drupal') {
255 $params['cms'] = $config['cms'];
256 $params['CMSdbUser'] = addslashes($config['cmsdb']['username']);
257 $params['CMSdbPass'] = addslashes($config['cmsdb']['password']);
258 $params['CMSdbHost'] = $config['cmsdb']['server'];
259 $params['CMSdbName'] = addslashes($config['cmsdb']['database']);
261 elseif ($installType == 'backdrop') {
262 $params['cms'] = 'Backdrop';
263 $params['CMSdbUser'] = addslashes($config['backdrop']['username']);
264 $params['CMSdbPass'] = addslashes($config['backdrop']['password']);
265 $params['CMSdbHost'] = $config['backdrop']['server'];
266 $params['CMSdbName'] = addslashes($config['backdrop']['database']);
269 $params['cms'] = 'WordPress';
270 $params['CMSdbUser'] = addslashes(DB_USER
);
271 $params['CMSdbPass'] = addslashes(DB_PASSWORD
);
272 $params['CMSdbHost'] = DB_HOST
;
273 $params['CMSdbName'] = addslashes(DB_NAME
);
276 $params['crmRoot'] = addslashes($params['crmRoot']);
279 $params['siteKey'] = md5(rand() . mt_rand() . rand() . uniqid('', TRUE) . $params['baseURL']);
280 // Would prefer openssl_random_pseudo_bytes(), but I don't think it's universally available.
282 $str = file_get_contents($tplPath . 'civicrm.settings.php.template');
283 foreach ($params as $key => $value) {
284 $str = str_replace('%%' . $key . '%%', $value, $str);
292 function civicrm_cms_base() {
298 if (isset($_SERVER['HTTPS']) &&
299 !empty($_SERVER['HTTPS']) &&
300 strtolower($_SERVER['HTTPS']) != 'off'
302 $url = 'https://' . $_SERVER['HTTP_HOST'];
305 $url = 'http://' . $_SERVER['HTTP_HOST'];
308 $baseURL = $_SERVER['SCRIPT_NAME'];
310 if ($installType == 'drupal' ||
$installType == 'backdrop') {
311 //don't assume 6 dir levels, as civicrm
312 //may or may not be in sites/all/modules/
313 //lets allow to install in custom dir. CRM-6840
315 $crmDirLevels = str_replace($cmsPath, '', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
316 $baseURL = str_replace($crmDirLevels, '', str_replace('\\', '/', $baseURL));
318 elseif ($installType == 'wordpress') {
319 $baseURL = str_replace($url, '', site_url());
322 for ($i = 1; $i <= $numPrevious; $i++
) {
323 $baseURL = dirname($baseURL);
327 // remove the last directory separator string from the directory
328 if (substr($baseURL, -1, 1) == DIRECTORY_SEPARATOR
) {
329 $baseURL = substr($baseURL, 0, -1);
332 // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze
333 $baseURL = str_replace(DIRECTORY_SEPARATOR
, '/', $baseURL);
335 if ($baseURL != '/') {
339 return $url . $baseURL;
345 function civicrm_home_url() {
346 $drupalURL = civicrm_cms_base();
347 return $drupalURL . 'index.php?q=civicrm';