forked from civicrm/civicrm-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
civicrm.install
232 lines (209 loc) · 7.99 KB
/
civicrm.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.4 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2013
* $Id$
*
*/
/**
* Implementation of hook_install()
*
* Drupal specific direct database install for civicrm
*/
function civicrm_install() {
global $base_url;
try {
$error = _civicrm_init_setting();
}
catch(Exception $e){
require_once DRUPAL_ROOT . '/includes/errors.inc';
$variables = _drupal_decode_exception($e);
$problems['install']['severity'] = REQUIREMENT_ERROR;
$problems['install']['description'] = $e->getMessage();
drupal_set_message($e->getMessage(), 'error');
watchdog_exception('civicrm', $e);
db_query("UPDATE civicrm SET status = 0 WHERE name = 'civicrm'");
}
// step 1: import databases
$success = _civicrm_import_sql('sql/civicrm.mysql');
if($success){
_civicrm_import_sql('sql/civicrm_data.zh_TW.mysql');
_civicrm_import_sql('sql/civicrm_acl.zh_TW.mysql');
}
// step 2: create file directory
// Oh we can't create civicrm directory here. Leave it for initialize auto create script.
$dir1 = file_build_uri('civicrm');
file_prepare_directory($dir1, FILE_CREATE_DIRECTORY);
$dir2 = file_build_uri('civicrm/templates_c');
file_prepare_directory($dir2, FILE_CREATE_DIRECTORY);
$dir3 = file_build_uri('civicrm/upload');
file_prepare_directory($dir3, FILE_CREATE_DIRECTORY);
$dir4 = file_build_uri('civicrm/persist');
file_prepare_directory($dir4, FILE_CREATE_DIRECTORY);
$dir5 = file_build_uri('civicrm/custom');
file_prepare_directory($dir5, FILE_CREATE_DIRECTORY);
drupal_set_message("Successful install CiviCRM");
}
/**
* Implementation of hook_uninstall( )
*/
function civicrm_uninstall() {
require_once 'civicrm.module';
if (!civicrm_initialize()) {
return;
}
require_once 'CRM/Core/Config.php';
$config = CRM_Core_Config::singleton();
require_once 'CRM/Core/DAO.php';
CRM_Core_DAO::dropAllTables();
}
function _civicrm_init_setting(){
global $crm_root;
if(empty($crm_root)){
$crm_root = DRUPAL_ROOT.base_path().drupal_get_path('module', 'civicrm');
$crm_root = preg_replace('/\/drupal$/', '', $crm_root);
$include_path = '.'.PATH_SEPARATOR.$crm_root.PATH_SEPARATOR.$crm_root.PATH_SEPARATOR.'packages'.PATH_SEPARATOR.get_include_path();
set_include_path( $include_path );
}
$civicrm_path = drupal_get_path('module', 'civicrm');
$conf_path = conf_path();
$setting = $conf_path.'/civicrm.settings.php';
if(file_exists($setting)){
// trying to require
require_once $setting;
$dsn = defined(CIVICRM_DSN) ? CIVICRM_DSN : NULL;
if(empty($dsn)){
throw new Exception(t('Could not detect database settings from civicrm.settings.php'));
}
}
else{
if(is_writable($conf_path)){
$db = Database::getConnection()->getConnectionOptions();
if ($db['host'] == '127.0.0.1') {
$db['host'] = 'localhost'; // use mysql socket instead
}
$params = array();
$params['%%baseURL%%'] = base_path();
$params['%%cms%%'] = 'Drupal';
$params['%%CMSdbUser%%'] = addslashes($db['username']);
$params['%%CMSdbPass%%'] = addslashes($db['password']);
$params['%%CMSdbHost%%'] = empty($db['port']) ? $db['host'] : $db['host'].':'.$db['port'];
$params['%%CMSdbName%%'] = addslashes($db['database']);
$params['%%dbUser%%'] = addslashes($db['username']);
$params['%%dbPass%%'] = addslashes($db['password']);
$params['%%dbHost%%'] = empty($db['port']) ? $db['host'] : $db['host'].':'.$db['port'];
$params['%%dbName%%'] = addslashes($db['database']);
$params['%%crmRoot%%'] = $crm_root.'/';
$params['%%templateCompileDir%%'] = drupal_realpath('public://')."/civicrm/templates_c/";
$params['%%siteKey%%'] = md5($base_url.microtime());
$filename = $crm_root.'/templates/CRM/common/civicrm.settings.php.template';
$setting_content = file_get_contents($filename);
$setting_content = str_replace(array_keys($params), $params, $setting_content);
$success = file_put_contents($setting, $setting_content);
if(!$success){
throw new Exception(t('CiviCRM settings does not exist'));
}
}
else{
throw new Exception(t('Drupal settings dir not writable'));
}
}
require_once conf_path().'/civicrm.settings.php';
require_once "$crm_root/packages/DB.php";
_civicrm_db_test();
}
/**
* Helper function for test civicrm if installed
*/
function _civicrm_db_test(){
// test if database exists
$db_crm = DB::connect(CIVICRM_DSN);
$pear = new PEAR;
if($pear->isError($db_crm)) {
throw new Exception(t('Database connection failed when enable civicrm'));
}
else{
$res = $db_crm->query("SHOW TABLES LIKE 'ciivcrm_contact'");
if($res->numRows()){
throw new Exception(t('CiviCRM already exists in database.'));
}
}
}
/**
* Helper function for import sql
*/
function _civicrm_import_sql($filename){
global $crm_root;
$dsn = CIVICRM_DSN;
$filename = $crm_root.'/'.$filename;
$db = DB::connect($dsn);
$pear = new PEAR;
if($pear->isError($db)) {
watchdog("civicrm", "Cannot open $dsn: " . $db->getMessage( ), NULL, WATCHDOG_ERROR);
return FALSE;
}
$string = file_get_contents( $filename );
// change \r\n to fix windows issues
$string = str_replace("\r\n", "\n", $string );
//get rid of comments starting with # and --
$string = preg_replace("/^#[^\n]*$/m", "\n", $string );
$string = preg_replace("/^(--[^-]).*/m", "\n", $string );
$db->query("/*!40101 SET NAMES utf8mb4 */");
$queries = preg_split('/;$/m', $string);
foreach ( $queries as $query ) {
$query = trim( $query );
if ( ! empty( $query ) ) {
$res = $db->query( $query );
if ( $pear->isError( $res ) ) {
watchdog("civicrm", "Cannot execute $query: " . $res->getMessage( ) , NULL, WATCHDOG_ERROR);
return FALSE;
}
}
}
return TRUE;
}
/**
* Upgrade from 6.x to 7.x
*/
function civicrm_update_7000(){
civicrm_initialize();
$query = CRM_Core_DAO::executeQuery("SELECT id, config_backend FROM civicrm_domain");
while ($query->fetch()) {
$config = unserialize($query->config_backend);
unset($config['userSystem']);
$serialized = serialize($config);
CRM_Core_DAO::executeQuery("UPDATE civicrm_domain SET config_backend = '$serialized' WHERE id = $query->id");
}
}
/**
* Update CiviCRM module weight
*/
function civicrm_update_7400(&$sandbox) {
db_query("UPDATE {system} SET weight = 100 WHERE name = 'civicrm'");
}