Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright Tech To The People http:tttp.eu (c) 2008 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | | | |
9 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
10 | | under the terms of the GNU Affero General Public License | | |
c73475ea | 11 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
6a488035 TO |
12 | | | |
13 | | CiviCRM is distributed in the hope that it will be useful, but | | |
14 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
16 | | See the GNU Affero General Public License for more details. | | |
17 | | | | |
18 | | You should have received a copy of the GNU Affero General Public | | |
c73475ea WA |
19 | | License and the CiviCRM Licensing Exception along | |
20 | | with this program; if not, contact CiviCRM LLC | | |
6a488035 TO |
21 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
22 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
23 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
24 | +--------------------------------------------------------------------+ | |
d25dd0ee | 25 | */ |
6a488035 TO |
26 | |
27 | /** | |
28 | * This files provides several classes for doing command line work with | |
29 | * CiviCRM. civicrm_cli is the base class. It's used by cli.php. | |
30 | * | |
31 | * In addition, there are several additional classes that inherit | |
32 | * civicrm_cli to do more precise functions. | |
33 | * | |
5396af74 | 34 | */ |
6a488035 TO |
35 | |
36 | /** | |
37 | * base class for doing all command line operations via civicrm | |
38 | * used by cli.php | |
5396af74 | 39 | */ |
6a488035 TO |
40 | class civicrm_cli { |
41 | // required values that must be passed | |
42 | // via the command line | |
43 | var $_required_arguments = array('action', 'entity'); | |
44 | var $_additional_arguments = array(); | |
45 | var $_entity = NULL; | |
46 | var $_action = NULL; | |
47 | var $_output = FALSE; | |
48 | var $_joblog = FALSE; | |
49 | var $_config; | |
50 | ||
51 | // optional arguments | |
52 | var $_site = 'localhost'; | |
53 | var $_user = NULL; | |
2d8f9c75 | 54 | var $_password = NULL; |
6a488035 TO |
55 | |
56 | // all other arguments populate the parameters | |
57 | // array that is passed to civicrm_api | |
58 | var $_params = array('version' => 3); | |
59 | ||
60 | var $_errors = array(); | |
61 | ||
4e87860d EM |
62 | /** |
63 | * @return bool | |
64 | */ | |
6a488035 TO |
65 | public function initialize() { |
66 | if (!$this->_accessing_from_cli()) { | |
67 | return FALSE; | |
68 | } | |
69 | if (!$this->_parseOptions()) { | |
70 | return FALSE; | |
71 | } | |
72 | if (!$this->_bootstrap()) { | |
73 | return FALSE; | |
74 | } | |
75 | if (!$this->_validateOptions()) { | |
76 | return FALSE; | |
77 | } | |
78 | return TRUE; | |
79 | } | |
80 | ||
cd5823ae EM |
81 | /** |
82 | * Ensure function is being run from the cli. | |
83 | * | |
84 | * @return bool | |
85 | */ | |
6a488035 TO |
86 | public function _accessing_from_cli() { |
87 | if (PHP_SAPI === 'cli') { | |
88 | return TRUE; | |
89 | } | |
90 | else { | |
91 | trigger_error("cli.php can only be run from command line.", E_USER_ERROR); | |
92 | } | |
93 | } | |
94 | ||
4e87860d EM |
95 | /** |
96 | * @return bool | |
97 | */ | |
6a488035 TO |
98 | public function callApi() { |
99 | require_once 'api/api.php'; | |
100 | ||
101 | // CRM-9822 -'execute' action always goes thru Job api and always writes to log | |
102 | if ($this->_action != 'execute' && $this->_joblog) { | |
103 | require_once 'CRM/Core/JobManager.php'; | |
104 | $facility = new CRM_Core_JobManager(); | |
105 | $facility->setSingleRunParams($this->_entity, $this->_action, $this->_params, 'From Cli.php'); | |
106 | $facility->executeJobByAction($this->_entity, $this->_action); | |
107 | } | |
108 | else { | |
109 | // CRM-9822 cli.php calls don't require site-key, so bypass site-key authentication | |
110 | $this->_params['auth'] = FALSE; | |
111 | $result = civicrm_api($this->_entity, $this->_action, $this->_params); | |
112 | } | |
113 | ||
114 | if ($result['is_error'] != 0) { | |
115 | $this->_log($result['error_message']); | |
116 | return FALSE; | |
117 | } | |
118 | elseif ($this->_output) { | |
119 | print_r($result['values']); | |
120 | } | |
121 | return TRUE; | |
122 | } | |
123 | ||
4e87860d EM |
124 | /** |
125 | * @return bool | |
126 | */ | |
6a488035 TO |
127 | private function _parseOptions() { |
128 | $args = $_SERVER['argv']; | |
129 | // remove the first argument, which is the name | |
130 | // of this script | |
131 | array_shift($args); | |
132 | ||
133 | while (list($k, $arg) = each($args)) { | |
134 | // sanitize all user input | |
135 | $arg = $this->_sanitize($arg); | |
136 | ||
137 | // if we're not parsing an option signifier | |
138 | // continue to the next one | |
139 | if (!preg_match('/^-/', $arg)) { | |
140 | continue; | |
141 | } | |
142 | ||
143 | // find the value of this arg | |
144 | if (preg_match('/=/', $arg)) { | |
145 | $parts = explode('=', $arg); | |
56fdfc52 | 146 | $arg = $parts[0]; |
6a488035 TO |
147 | $value = $parts[1]; |
148 | } | |
149 | else { | |
150 | if (isset($args[$k + 1])) { | |
151 | $next_arg = $this->_sanitize($args[$k + 1]); | |
152 | // if the next argument is not another option | |
153 | // it's the value for this argument | |
154 | if (!preg_match('/^-/', $next_arg)) { | |
155 | $value = $next_arg; | |
156 | } | |
157 | } | |
158 | } | |
159 | ||
160 | // parse the special args first | |
161 | if ($arg == '-e' || $arg == '--entity') { | |
162 | $this->_entity = $value; | |
163 | } | |
164 | elseif ($arg == '-a' || $arg == '--action') { | |
165 | $this->_action = $value; | |
166 | } | |
167 | elseif ($arg == '-s' || $arg == '--site') { | |
168 | $this->_site = $value; | |
169 | } | |
170 | elseif ($arg == '-u' || $arg == '--user') { | |
171 | $this->_user = $value; | |
172 | } | |
173 | elseif ($arg == '-p' || $arg == '--password') { | |
174 | $this->_password = $value; | |
175 | } | |
176 | elseif ($arg == '-o' || $arg == '--output') { | |
177 | $this->_output = TRUE; | |
178 | } | |
179 | elseif ($arg == '-j' || $arg == '--joblog') { | |
180 | $this->_joblog = TRUE; | |
181 | } | |
182 | else { | |
c8c10d52 | 183 | foreach ($this->_additional_arguments as $short => $long) { |
6a488035 TO |
184 | if ($arg == '-' . $short || $arg == '--' . $long) { |
185 | $property = '_' . $long; | |
186 | $this->$property = $value; | |
187 | continue; | |
188 | } | |
189 | } | |
190 | // all other arguments are parameters | |
191 | $key = ltrim($arg, '--'); | |
192 | $this->_params[$key] = isset($value) ? $value : NULL; | |
193 | } | |
194 | } | |
195 | return TRUE; | |
196 | } | |
197 | ||
4e87860d EM |
198 | /** |
199 | * @return bool | |
200 | */ | |
6a488035 TO |
201 | private function _bootstrap() { |
202 | // so the configuration works with php-cli | |
203 | $_SERVER['PHP_SELF'] = "/index.php"; | |
204 | $_SERVER['HTTP_HOST'] = $this->_site; | |
205 | $_SERVER['REMOTE_ADDR'] = "127.0.0.1"; | |
ddd885e6 | 206 | $_SERVER['SERVER_SOFTWARE'] = NULL; |
56fdfc52 | 207 | $_SERVER['REQUEST_METHOD'] = 'GET'; |
ddd885e6 | 208 | |
6a488035 TO |
209 | // SCRIPT_FILENAME needed by CRM_Utils_System::cmsRootPath |
210 | $_SERVER['SCRIPT_FILENAME'] = __FILE__; | |
ddd885e6 | 211 | |
6a488035 TO |
212 | // CRM-8917 - check if script name starts with /, if not - prepend it. |
213 | if (ord($_SERVER['SCRIPT_NAME']) != 47) { | |
214 | $_SERVER['SCRIPT_NAME'] = '/' . $_SERVER['SCRIPT_NAME']; | |
215 | } | |
216 | ||
217 | $civicrm_root = dirname(__DIR__); | |
218 | chdir($civicrm_root); | |
a9e80afc | 219 | require_once 'civicrm.config.php'; |
6a488035 | 220 | // autoload |
481a74f4 | 221 | if (!class_exists('CRM_Core_ClassLoader')) { |
c1f3c6da BS |
222 | require_once $civicrm_root . '/CRM/Core/ClassLoader.php'; |
223 | } | |
6a488035 TO |
224 | CRM_Core_ClassLoader::singleton()->register(); |
225 | ||
226 | $this->_config = CRM_Core_Config::singleton(); | |
4e87860d | 227 | |
84f65b2e KW |
228 | // HTTP_HOST will be 'localhost' unless overwritten with the -s argument. |
229 | // Now we have a Config object, we can set it from the Base URL. | |
230 | if ($_SERVER['HTTP_HOST'] == 'localhost') { | |
a9e80afc | 231 | $_SERVER['HTTP_HOST'] = preg_replace( |
56fdfc52 TO |
232 | '!^https?://([^/]+)/$!i', |
233 | '$1', | |
234 | $this->_config->userFrameworkBaseURL); | |
84f65b2e | 235 | } |
6a488035 TO |
236 | |
237 | $class = 'CRM_Utils_System_' . $this->_config->userFramework; | |
238 | ||
239 | $cms = new $class(); | |
323696fa | 240 | if (!CRM_Utils_System::loadBootstrap(array(), FALSE, FALSE, $civicrm_root)) { |
6a488035 TO |
241 | $this->_log(ts("Failed to bootstrap CMS")); |
242 | return FALSE; | |
243 | } | |
244 | ||
245 | if (strtolower($this->_entity) == 'job') { | |
246 | if (!$this->_user) { | |
247 | $this->_log(ts("Jobs called from cli.php require valid user as parameter")); | |
248 | return FALSE; | |
249 | } | |
250 | } | |
251 | ||
252 | if (!empty($this->_user)) { | |
22e263ad | 253 | if (!CRM_Utils_System::authenticateScript(TRUE, $this->_user, $this->_password, TRUE, FALSE, FALSE)) { |
bec3fc7c BS |
254 | $this->_log(ts("Failed to login as %1. Wrong username or password.", array('1' => $this->_user))); |
255 | return FALSE; | |
256 | } | |
6a488035 TO |
257 | if (!$cms->loadUser($this->_user)) { |
258 | $this->_log(ts("Failed to login as %1", array('1' => $this->_user))); | |
259 | return FALSE; | |
260 | } | |
261 | } | |
262 | ||
263 | return TRUE; | |
264 | } | |
265 | ||
4e87860d EM |
266 | /** |
267 | * @return bool | |
268 | */ | |
6a488035 TO |
269 | private function _validateOptions() { |
270 | $required = $this->_required_arguments; | |
271 | while (list(, $var) = each($required)) { | |
272 | $index = '_' . $var; | |
273 | if (empty($this->$index)) { | |
274 | $missing_arg = '--' . $var; | |
275 | $this->_log(ts("The %1 argument is required", array(1 => $missing_arg))); | |
276 | $this->_log($this->_getUsage()); | |
277 | return FALSE; | |
278 | } | |
279 | } | |
280 | return TRUE; | |
281 | } | |
282 | ||
4e87860d EM |
283 | /** |
284 | * @param $value | |
285 | * | |
286 | * @return string | |
287 | */ | |
6a488035 TO |
288 | private function _sanitize($value) { |
289 | // restrict user input - we should not be needing anything | |
290 | // other than normal alpha numeric plus - and _. | |
291 | return trim(preg_replace('#^[^a-zA-Z0-9\-_=/]$#', '', $value)); | |
292 | } | |
293 | ||
4e87860d EM |
294 | /** |
295 | * @return string | |
296 | */ | |
6a488035 TO |
297 | private function _getUsage() { |
298 | $out = "Usage: cli.php -e entity -a action [-u user] [-s site] [--output] [PARAMS]\n"; | |
299 | $out .= " entity is the name of the entity, e.g. Contact, Event, etc.\n"; | |
300 | $out .= " action is the name of the action e.g. Get, Create, etc.\n"; | |
301 | $out .= " user is an optional username to run the script as\n"; | |
302 | $out .= " site is the domain name of the web site (for Drupal multi site installs)\n"; | |
303 | $out .= " --output will print the result from the api call\n"; | |
304 | $out .= " PARAMS is one or more --param=value combinations to pass to the api\n"; | |
305 | return ts($out); | |
306 | } | |
307 | ||
4e87860d EM |
308 | /** |
309 | * @param $error | |
310 | */ | |
6a488035 TO |
311 | private function _log($error) { |
312 | // fixme, this should call some CRM_Core_Error:: function | |
313 | // that properly logs | |
314 | print "$error\n"; | |
315 | } | |
96025800 | 316 | |
6a488035 TO |
317 | } |
318 | ||
319 | /** | |
320 | * class used by csv/export.php to export records from | |
321 | * the database in a csv file format. | |
5396af74 | 322 | */ |
6a488035 TO |
323 | class civicrm_cli_csv_exporter extends civicrm_cli { |
324 | var $separator = ','; | |
325 | ||
4e87860d | 326 | /** |
4e87860d | 327 | */ |
5396af74 | 328 | public function __construct() { |
6a488035 TO |
329 | $this->_required_arguments = array('entity'); |
330 | parent::initialize(); | |
331 | } | |
332 | ||
5396af74 | 333 | public function run() { |
6a488035 TO |
334 | $out = fopen("php://output", 'w'); |
335 | fputcsv($out, $this->columns, $this->separator, '"'); | |
336 | ||
337 | $this->row = 1; | |
338 | $result = civicrm_api($this->_entity, 'Get', $this->_params); | |
a9e80afc | 339 | $first = TRUE; |
6a488035 | 340 | foreach ($result['values'] as $row) { |
22e263ad | 341 | if ($first) { |
6a488035 TO |
342 | $columns = array_keys($row); |
343 | fputcsv($out, $columns, $this->separator, '"'); | |
a9e80afc | 344 | $first = FALSE; |
6a488035 | 345 | } |
0d5a3f9d JL |
346 | //handle values returned as arrays (i.e. custom fields that allow multiple selections) by inserting a control character |
347 | foreach ($row as &$field) { | |
22e263ad | 348 | if (is_array($field)) { |
0d5a3f9d | 349 | //convert to string |
a9e80afc | 350 | $field = implode($field, CRM_Core_DAO::VALUE_SEPARATOR) . CRM_Core_DAO::VALUE_SEPARATOR; |
0d5a3f9d JL |
351 | } |
352 | } | |
6a488035 TO |
353 | fputcsv($out, $row, $this->separator, '"'); |
354 | } | |
355 | fclose($out); | |
356 | echo "\n"; | |
357 | } | |
96025800 | 358 | |
6a488035 TO |
359 | } |
360 | ||
361 | /** | |
362 | * base class used by both civicrm_cli_csv_import | |
363 | * and civicrm_cli_csv_deleter to add or delete | |
364 | * records based on those found in a csv file | |
365 | * passed to the script. | |
5396af74 | 366 | */ |
6a488035 TO |
367 | class civicrm_cli_csv_file extends civicrm_cli { |
368 | var $header; | |
369 | var $separator = ','; | |
370 | ||
4e87860d | 371 | /** |
4e87860d | 372 | */ |
5396af74 | 373 | public function __construct() { |
a9e80afc | 374 | $this->_required_arguments = array('entity', 'file'); |
6a488035 TO |
375 | $this->_additional_arguments = array('f' => 'file'); |
376 | parent::initialize(); | |
377 | } | |
378 | ||
cd5823ae EM |
379 | /** |
380 | * Run CLI function. | |
381 | */ | |
5396af74 | 382 | public function run() { |
6a488035 TO |
383 | $this->row = 1; |
384 | $handle = fopen($this->_file, "r"); | |
385 | ||
386 | if (!$handle) { | |
387 | die("Could not open file: " . $this->_file . ". Please provide an absolute path.\n"); | |
388 | } | |
389 | ||
390 | //header | |
391 | $header = fgetcsv($handle, 0, $this->separator); | |
392 | // In case fgetcsv couldn't parse the header and dumped the whole line in 1 array element | |
393 | // Try a different separator char | |
394 | if (count($header) == 1) { | |
395 | $this->separator = ";"; | |
396 | rewind($handle); | |
397 | $header = fgetcsv($handle, 0, $this->separator); | |
398 | if (count($header) == 1) { | |
399 | die("Invalid file format for " . $this->_file . ". It must be a valid csv with separator ',' or ';'\n"); | |
400 | } | |
401 | } | |
402 | ||
403 | $this->header = $header; | |
404 | while (($data = fgetcsv($handle, 0, $this->separator)) !== FALSE) { | |
405 | // skip blank lines | |
4f99ca55 TO |
406 | if (count($data) == 1 && is_null($data[0])) { |
407 | continue; | |
a9e80afc | 408 | } |
6a488035 TO |
409 | $this->row++; |
410 | $params = $this->convertLine($data); | |
411 | $this->processLine($params); | |
412 | } | |
413 | fclose($handle); | |
6a488035 TO |
414 | } |
415 | ||
416 | /* return a params as expected */ | |
4e87860d EM |
417 | /** |
418 | * @param $data | |
419 | * | |
420 | * @return array | |
421 | */ | |
5396af74 | 422 | public function convertLine($data) { |
6a488035 TO |
423 | $params = array(); |
424 | foreach ($this->header as $i => $field) { | |
2d8f9c75 | 425 | //split any multiselect data, denoted with CRM_Core_DAO::VALUE_SEPARATOR |
0d5a3f9d | 426 | if (strpos($data[$i], CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE) { |
a9e80afc | 427 | $data[$i] = explode(CRM_Core_DAO::VALUE_SEPARATOR, $data[$i]); |
0d5a3f9d JL |
428 | $data[$i] = array_combine($data[$i], $data[$i]); |
429 | } | |
6a488035 TO |
430 | $params[$field] = $data[$i]; |
431 | } | |
432 | $params['version'] = 3; | |
433 | return $params; | |
434 | } | |
96025800 | 435 | |
6a488035 TO |
436 | } |
437 | ||
438 | /** | |
439 | * class for processing records to add | |
440 | * used by csv/import.php | |
441 | * | |
5396af74 | 442 | */ |
6a488035 | 443 | class civicrm_cli_csv_importer extends civicrm_cli_csv_file { |
4e87860d | 444 | /** |
c490a46a | 445 | * @param array $params |
4e87860d | 446 | */ |
5396af74 | 447 | public function processline($params) { |
6a488035 TO |
448 | $result = civicrm_api($this->_entity, 'Create', $params); |
449 | if ($result['is_error']) { | |
450 | echo "\nERROR line " . $this->row . ": " . $result['error_message'] . "\n"; | |
451 | } | |
452 | else { | |
453 | echo "\nline " . $this->row . ": created " . $this->_entity . " id: " . $result['id'] . "\n"; | |
454 | } | |
455 | } | |
96025800 | 456 | |
6a488035 TO |
457 | } |
458 | ||
459 | /** | |
460 | * class for processing records to delete | |
461 | * used by csv/delete.php | |
462 | * | |
5396af74 | 463 | */ |
6a488035 | 464 | class civicrm_cli_csv_deleter extends civicrm_cli_csv_file { |
4e87860d | 465 | /** |
c490a46a | 466 | * @param array $params |
4e87860d | 467 | */ |
5396af74 | 468 | public function processline($params) { |
6a488035 TO |
469 | $result = civicrm_api($this->_entity, 'Delete', $params); |
470 | if ($result['is_error']) { | |
471 | echo "\nERROR line " . $this->row . ": " . $result['error_message'] . "\n"; | |
0db6c3e1 TO |
472 | } |
473 | else { | |
6a488035 TO |
474 | echo "\nline " . $this->row . ": deleted\n"; |
475 | } | |
476 | } | |
96025800 | 477 | |
6a488035 | 478 | } |