Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
1f4ea726 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
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 | /** | |
b081365f | 29 | * This api exposes CiviCRM system functionality. |
6a488035 | 30 | * |
b081365f | 31 | * Includes caching, logging, and checking system functionality. |
6a488035 | 32 | * |
b081365f | 33 | * @package CiviCRM_APIv3 |
6a488035 TO |
34 | */ |
35 | ||
36 | /** | |
9d32e6f7 | 37 | * Flush all system caches. |
6a488035 | 38 | * |
cf470720 TO |
39 | * @param array $params |
40 | * Input parameters. | |
c28e1768 CW |
41 | * - triggers: bool, whether to drop/create SQL triggers; default: FALSE |
42 | * - session: bool, whether to reset the CiviCRM session data; default: FALSE | |
6a488035 | 43 | * |
c23f45d3 | 44 | * @return array |
6a488035 TO |
45 | */ |
46 | function civicrm_api3_system_flush($params) { | |
47 | CRM_Core_Invoke::rebuildMenuAndCaches( | |
48 | CRM_Utils_Array::value('triggers', $params, FALSE), | |
49 | CRM_Utils_Array::value('session', $params, FALSE) | |
50 | ); | |
51 | return civicrm_api3_create_success(); | |
52 | } | |
53 | ||
11e09c59 | 54 | /** |
9d32e6f7 | 55 | * Adjust Metadata for Flush action. |
6a488035 | 56 | * |
0aa0303c EM |
57 | * The metadata is used for setting defaults, documentation & validation. |
58 | * | |
cf470720 | 59 | * @param array $params |
b081365f | 60 | * Array of parameters determined by getfields. |
6a488035 | 61 | */ |
9b873358 | 62 | function _civicrm_api3_system_flush_spec(&$params) { |
b2ed8e73 CW |
63 | $params['triggers'] = array( |
64 | 'title' => 'Triggers', | |
65 | 'description' => 'rebuild triggers (boolean)', | |
66 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
67 | ); | |
68 | $params['session'] = array( | |
69 | 'title' => 'Sessions', | |
70 | 'description' => 'refresh sessions (boolean)', | |
71 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
72 | ); | |
9ef501da | 73 | } |
6a488035 | 74 | |
9ef501da | 75 | /** |
9d32e6f7 EM |
76 | * System.Check API specification (optional). |
77 | * | |
9ef501da TO |
78 | * This is used for documentation and validation. |
79 | * | |
cf470720 TO |
80 | * @param array $spec |
81 | * Description of fields supported by this API call. | |
9d32e6f7 | 82 | * |
9ef501da TO |
83 | * @see http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards |
84 | */ | |
85 | function _civicrm_api3_system_check_spec(&$spec) { | |
e5e4a5b2 CW |
86 | $spec['id'] = array( |
87 | 'title' => 'ID', | |
88 | 'description' => 'Not a real identifier - do not use', | |
89 | 'type' => CRM_Utils_Type::T_INT, | |
90 | ); | |
91 | $spec['name'] = array( | |
92 | 'title' => 'Name', | |
93 | 'description' => 'Unique identifier', | |
94 | 'type' => CRM_Utils_Type::T_STRING, | |
95 | ); | |
96 | $spec['title'] = array( | |
97 | 'title' => 'Title', | |
98 | 'description' => 'Short title text', | |
99 | 'type' => CRM_Utils_Type::T_STRING, | |
100 | ); | |
101 | $spec['message'] = array( | |
102 | 'title' => 'Message', | |
103 | 'description' => 'Long description html', | |
104 | 'type' => CRM_Utils_Type::T_STRING, | |
105 | ); | |
106 | $spec['help'] = array( | |
107 | 'title' => 'Help', | |
108 | 'description' => 'Optional extra help (html string)', | |
109 | 'type' => CRM_Utils_Type::T_STRING, | |
110 | ); | |
111 | $spec['severity'] = array( | |
112 | 'title' => 'Severity', | |
d1fa280a CW |
113 | 'description' => 'Psr\Log\LogLevel string', |
114 | 'type' => CRM_Utils_Type::T_STRING, | |
0e1b333f | 115 | 'options' => array_combine(CRM_Utils_Check::getSeverityList(), CRM_Utils_Check::getSeverityList()), |
d1fa280a CW |
116 | ); |
117 | $spec['severity_id'] = array( | |
118 | 'title' => 'Severity ID', | |
e5e4a5b2 CW |
119 | 'description' => 'Integer representation of Psr\Log\LogLevel', |
120 | 'type' => CRM_Utils_Type::T_INT, | |
0e1b333f | 121 | 'options' => CRM_Utils_Check::getSeverityList(), |
e5e4a5b2 | 122 | ); |
fc8d84da N |
123 | $spec['is_visible'] = array( |
124 | 'title' => 'is visible', | |
e5e4a5b2 | 125 | 'description' => '0 if message has been hidden by the user', |
a1c17079 | 126 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
097c681e | 127 | ); |
e5e4a5b2 CW |
128 | $spec['hidden_until'] = array( |
129 | 'title' => 'Hidden_until', | |
130 | 'description' => 'When will hidden message be visible again?', | |
131 | 'type' => CRM_Utils_Type::T_DATE, | |
132 | ); | |
232624b1 | 133 | } |
9ef501da TO |
134 | |
135 | /** | |
b081365f | 136 | * System Check API. |
9ef501da TO |
137 | * |
138 | * @param array $params | |
9d32e6f7 | 139 | * |
a6c01b45 | 140 | * @return array |
72b3a70c | 141 | * API result descriptor; return items are alert codes/messages |
9ef501da TO |
142 | * @see civicrm_api3_create_success |
143 | * @see civicrm_api3_create_error | |
144 | * @throws API_Exception | |
145 | */ | |
146 | function civicrm_api3_system_check($params) { | |
e918c16d | 147 | // array(array('name'=> $, 'severity'=>$, ...)) |
7c6fb573 | 148 | $id = 1; |
e5e4a5b2 CW |
149 | $returnValues = $fields = array(); |
150 | _civicrm_api3_system_check_spec($fields); | |
e918c16d NM |
151 | |
152 | // array(CRM_Utils_Check_Message) | |
e5e4a5b2 | 153 | $messages = CRM_Utils_Check::checkAll(); |
e918c16d | 154 | |
7c6fb573 NM |
155 | foreach ($messages as $msg) { |
156 | $returnValues[] = $msg->toArray() + array('id' => $id++); | |
46a903fb NM |
157 | } |
158 | ||
e5e4a5b2 | 159 | return _civicrm_api3_basic_array_get('systemCheck', $params, $returnValues, "id", array_keys($fields)); |
e2bef985 | 160 | } |
161 | ||
162 | /** | |
9d32e6f7 EM |
163 | * Log entry to system log table. |
164 | * | |
c490a46a | 165 | * @param array $params |
e2bef985 | 166 | * |
167 | * @return array | |
168 | */ | |
169 | function civicrm_api3_system_log($params) { | |
170 | $log = new CRM_Utils_SystemLogger(); | |
9d32e6f7 | 171 | // This part means fields with separate db storage are accepted as params which kind of seems more intuitive to me |
e2bef985 | 172 | // because I felt like not doing this required a bunch of explanation in the spec function - but perhaps other won't see it as helpful? |
22e263ad | 173 | if (!isset($params['context'])) { |
e2bef985 | 174 | $params['context'] = array(); |
175 | } | |
176 | $specialFields = array('contact_id', 'hostname'); | |
22e263ad TO |
177 | foreach ($specialFields as $specialField) { |
178 | if (isset($params[$specialField]) && !isset($params['context'])) { | |
e2bef985 | 179 | $params['context'][$specialField] = $params[$specialField]; |
180 | } | |
181 | } | |
182 | $returnValues = $log->log($params['level'], $params['message'], $params['context']); | |
183 | return civicrm_api3_create_success($returnValues, $params, 'System', 'Log'); | |
184 | } | |
185 | ||
186 | /** | |
d1b0d05e EM |
187 | * Metadata for log function. |
188 | * | |
c490a46a | 189 | * @param array $params |
e2bef985 | 190 | */ |
4f8ccea0 | 191 | function _civicrm_api3_system_log_spec(&$params) { |
e2bef985 | 192 | $params['level'] = array( |
193 | 'title' => 'Log Level', | |
194 | 'description' => 'Log level as described in PSR3 (info, debug, warning etc)', | |
195 | 'type' => CRM_Utils_Type::T_STRING, | |
196 | 'api.required' => TRUE, | |
197 | ); | |
198 | $params['message'] = array( | |
199 | 'title' => 'Log Message', | |
200 | 'description' => 'Standardised message string, you can also ', | |
201 | 'type' => CRM_Utils_Type::T_STRING, | |
202 | 'api.required' => TRUE, | |
203 | ); | |
204 | $params['context'] = array( | |
205 | 'title' => 'Log Context', | |
206 | 'description' => 'An array of additional data to store.', | |
207 | 'type' => CRM_Utils_Type::T_LONGTEXT, | |
208 | 'api.default' => array(), | |
209 | ); | |
210 | $params['contact_id'] = array( | |
211 | 'title' => 'Log Contact ID', | |
212 | 'description' => 'Optional ID of relevant contact', | |
213 | 'type' => CRM_Utils_Type::T_INT, | |
214 | ); | |
215 | $params['hostname'] = array( | |
216 | 'title' => 'Log Hostname', | |
217 | 'description' => 'Optional name of host', | |
218 | 'type' => CRM_Utils_Type::T_STRING, | |
219 | ); | |
220 | } | |
42bf9336 | 221 | |
91f1889e | 222 | /** |
d1b0d05e | 223 | * System.Get API. |
91f1889e | 224 | * |
24a70b66 | 225 | * @param array $params |
d1b0d05e | 226 | * |
24a70b66 | 227 | * @return array |
91f1889e TO |
228 | */ |
229 | function civicrm_api3_system_get($params) { | |
5bbcc823 | 230 | $config = CRM_Core_Config::singleton(); |
91f1889e TO |
231 | $returnValues = array( |
232 | array( | |
5bbcc823 TO |
233 | 'version' => CRM_Utils_System::version(), // deprecated in favor of civi.version |
234 | 'uf' => CIVICRM_UF, // deprecated in favor of cms.type | |
235 | 'php' => array( | |
236 | 'version' => phpversion(), | |
40278a8e | 237 | 'time' => time(), |
5bbcc823 | 238 | 'tz' => date_default_timezone_get(), |
d7aad22f | 239 | 'sapi' => php_sapi_name(), |
5bbcc823 TO |
240 | 'extensions' => get_loaded_extensions(), |
241 | 'ini' => _civicrm_api3_system_get_redacted_ini(), | |
242 | ), | |
243 | 'mysql' => array( | |
244 | 'version' => CRM_Core_DAO::singleValueQuery('SELECT @@version'), | |
40278a8e | 245 | 'time' => CRM_Core_DAO::singleValueQuery('SELECT unix_timestamp()'), |
7163c128 | 246 | 'vars' => _civicrm_api3_system_get_redacted_mysql(), |
5bbcc823 TO |
247 | ), |
248 | 'cms' => array( | |
594260ad | 249 | 'version' => $config->userSystem->getVersion(), |
5bbcc823 TO |
250 | 'type' => CIVICRM_UF, |
251 | 'modules' => CRM_Core_Module::collectStatuses($config->userSystem->getModules()), | |
252 | ), | |
253 | 'civi' => array( | |
254 | 'version' => CRM_Utils_System::version(), | |
255 | 'dev' => (bool) CRM_Utils_System::isDevelopment(), | |
256 | 'components' => array_keys(CRM_Core_Component::getEnabledComponents()), | |
257 | 'extensions' => preg_grep( | |
258 | '/^uninstalled$/', | |
259 | CRM_Extension_System::singleton()->getManager()->getStatuses(), | |
260 | PREG_GREP_INVERT | |
261 | ), | |
41ccb6fc C |
262 | 'multidomain' => CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_domain') > 1, |
263 | 'settings' => _civicrm_api3_system_get_redacted_settings(), | |
5bbcc823 TO |
264 | 'exampleUrl' => CRM_Utils_System::url('civicrm/example', NULL, TRUE, NULL, FALSE), |
265 | ), | |
d7aad22f C |
266 | 'http' => array( |
267 | 'software' => CRM_Utils_Array::value('SERVER_SOFTWARE', $_SERVER), | |
268 | 'forwarded' => !empty($_SERVER['HTTP_X_FORWARDED_FOR']) || !empty($_SERVER['X_FORWARDED_PROTO']), | |
269 | 'port' => (empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == 80 || $_SERVER['SERVER_PORT'] == 443) ? 'Standard' : 'Nonstandard', | |
270 | ), | |
271 | 'os' => array( | |
272 | 'type' => php_uname('s'), | |
273 | 'release' => php_uname('r'), | |
274 | 'version' => php_uname('v'), | |
275 | 'machine' => php_uname('m'), | |
276 | ), | |
91f1889e TO |
277 | ), |
278 | ); | |
5bbcc823 | 279 | |
91f1889e TO |
280 | return civicrm_api3_create_success($returnValues, $params, 'System', 'get'); |
281 | } | |
5bbcc823 TO |
282 | |
283 | /** | |
284 | * Generate a sanitized/anonymized/redacted dump of the PHP configuration. | |
285 | * | |
286 | * Some INI fields contain site-identifying information (SII) -- e.g. URLs, | |
287 | * hostnames, file paths, IP addresses, passwords, or free-form comments | |
288 | * could be used to identify a site or gain access to its resources. | |
289 | * | |
290 | * A number of INI fields have been examined to determine whether they | |
291 | * contain SII. Approved fields are put in a whitelist; all other fields | |
292 | * are redacted. | |
293 | * | |
294 | * Redaction hides the substance of a field but does not completely omit | |
295 | * all information. Consider the field 'mail.log' - setting this field | |
296 | * has a functional effect (it enables or disables the logging behavior) | |
297 | * and also points to particular file. Empty values (FALSE/NULL/0/"") | |
298 | * will pass through redaction, but all other values will be replaced | |
299 | * by a string (eg "REDACTED"). This roughly indicates whether the | |
300 | * option is enabled/disabled without giving away its content. | |
301 | * | |
302 | * @return array | |
303 | */ | |
304 | function _civicrm_api3_system_get_redacted_ini() { | |
305 | static $whitelist = NULL; | |
306 | if ($whitelist === NULL) { | |
7163c128 | 307 | $whitelist = _civicrm_api3_system_get_whitelist(__DIR__ . '/System/ini-whitelist.txt'); |
5bbcc823 TO |
308 | } |
309 | ||
310 | $inis = ini_get_all(NULL, FALSE); | |
311 | $result = array(); | |
312 | foreach ($inis as $k => $v) { | |
313 | if (empty($v) || in_array($k, $whitelist)) { | |
314 | $result[$k] = $v; | |
315 | } | |
316 | else { | |
317 | $result[$k] = 'REDACTED'; | |
318 | } | |
319 | } | |
320 | ||
321 | return $result; | |
322 | } | |
7163c128 TO |
323 | |
324 | /** | |
325 | * Generate ae sanitized/anonymized/redacted dump of MySQL configuration. | |
326 | * | |
327 | * @return array | |
328 | * @see _civicrm_api3_system_get_redacted_ini | |
329 | */ | |
330 | function _civicrm_api3_system_get_redacted_mysql() { | |
331 | static $whitelist = NULL; | |
332 | if ($whitelist === NULL) { | |
333 | $whitelist = _civicrm_api3_system_get_whitelist(__DIR__ . '/System/mysql-whitelist.txt'); | |
334 | } | |
335 | ||
336 | $inis = ini_get_all(NULL, FALSE); | |
337 | $result = array(); | |
338 | $dao = CRM_Core_DAO::executeQuery('SHOW VARIABLES'); | |
339 | while ($dao->fetch()) { | |
340 | if (empty($dao->Variable_name) || in_array($dao->Variable_name, $whitelist)) { | |
341 | $result[$dao->Variable_name] = $dao->Value; | |
342 | } | |
343 | else { | |
344 | $result[$dao->Variable_name] = 'REDACTED'; | |
345 | } | |
346 | } | |
347 | ||
348 | return $result; | |
349 | } | |
350 | ||
db01bf2f | 351 | /** |
352 | * Get redacted settings. | |
353 | * | |
354 | * @return array | |
355 | * @throws CiviCRM_API3_Exception | |
356 | */ | |
41ccb6fc C |
357 | function _civicrm_api3_system_get_redacted_settings() { |
358 | static $whitelist = NULL; | |
359 | if ($whitelist === NULL) { | |
360 | $whitelist = _civicrm_api3_system_get_whitelist(__DIR__ . '/System/setting-whitelist.txt'); | |
361 | } | |
362 | ||
363 | $apiResult = civicrm_api3('Setting', 'get', array()); | |
364 | $result = array(); | |
365 | foreach ($apiResult['values'] as $settings) { | |
366 | foreach ($settings as $key => $value) { | |
367 | if (in_array($key, $whitelist)) { | |
368 | $result[$key] = $value; | |
369 | } | |
370 | } | |
371 | } | |
372 | ||
373 | return $result; | |
374 | } | |
375 | ||
7163c128 TO |
376 | /** |
377 | * Read a whitelist. | |
378 | * | |
379 | * @param string $whitelistFile | |
380 | * Name of a file. Each line is a field name. Comments begin with "#". | |
381 | * @return array | |
382 | */ | |
383 | function _civicrm_api3_system_get_whitelist($whitelistFile) { | |
384 | $whitelist = array_filter( | |
385 | explode("\n", file_get_contents($whitelistFile)), | |
386 | function ($k) { | |
387 | return !empty($k) && !preg_match('/^\s*#/', $k); | |
388 | } | |
389 | ); | |
390 | return $whitelist; | |
391 | } | |
d7ea7150 | 392 | |
393 | /** | |
394 | * Update log table structures. | |
395 | * | |
396 | * This updates the engine type if defined in the hook and changes the field type | |
397 | * for log_conn_id to reflect CRM-18193. | |
398 | */ | |
399 | function civicrm_api3_system_updatelogtables() { | |
400 | $schema = new CRM_Logging_Schema(); | |
401 | $schema->updateLogTableSchema(); | |
49186f94 AS |
402 | return civicrm_api3_create_success(1); |
403 | } | |
404 | ||
405 | /** | |
406 | * Update indexes. | |
407 | * | |
408 | * This adds any indexes that exist in the schema but not the database. | |
409 | */ | |
410 | function civicrm_api3_system_updateindexes() { | |
f4f835ed JP |
411 | list($missingIndices) = CRM_Core_BAO_SchemaHandler::getMissingIndices(); |
412 | CRM_Core_BAO_SchemaHandler::createMissingIndices($missingIndices); | |
49186f94 | 413 | return civicrm_api3_create_success(1); |
d7ea7150 | 414 | } |
cb721356 JP |
415 | |
416 | /** | |
417 | * Creates missing log tables. | |
418 | * | |
419 | * CRM-20838 - This adds any missing log tables into the database. | |
420 | */ | |
421 | function civicrm_api3_system_createmissinglogtables() { | |
422 | $schema = new CRM_Logging_Schema(); | |
423 | $missingLogTables = $schema->getMissingLogTables(); | |
424 | if (!empty($missingLogTables)) { | |
425 | foreach ($missingLogTables as $tableName) { | |
426 | $schema->fixSchemaDifferencesFor($tableName, NULL, FALSE); | |
427 | } | |
428 | } | |
429 | return civicrm_api3_create_success(1); | |
430 | } |