3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright Tech To The People http:tttp.eu (c) 2008 |
7 +--------------------------------------------------------------------+
9 | CiviCRM is free software; you can copy, modify, and distribute it |
10 | under the terms of the GNU Affero General Public License |
11 | Version 3, 19 November 2007. |
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. |
18 | You should have received a copy of the GNU Affero General Public |
19 | License along with this program; if not, contact CiviCRM LLC |
20 | at info[AT]civicrm[DOT]org. If you have questions about the |
21 | GNU Affero General Public License or the licensing of CiviCRM, |
22 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
23 +--------------------------------------------------------------------+
27 * This files provides several classes for doing command line work with
28 * CiviCRM. civicrm_cli is the base class. It's used by cli.php.
30 * In addition, there are several additional classes that inherit
31 * civicrm_cli to do more precise functions.
36 * base class for doing all command line operations via civicrm
41 // required values that must be passed
42 // via the command line
43 var $_required_arguments = array('action', 'entity');
44 var $_additional_arguments = array();
52 var $_site = 'localhost';
54 var $_password = NULL;
56 // all other arguments populate the parameters
57 // array that is passed to civicrm_api
58 var $_params = array('version' => 3);
60 var $_errors = array();
62 public function initialize() {
63 if (!$this->_accessing_from_cli()) {
66 if (!$this->_parseOptions()) {
69 if (!$this->_bootstrap()) {
72 if (!$this->_validateOptions()) {
78 public function _accessing_from_cli() {
79 if (PHP_SAPI
=== 'cli') {
83 trigger_error("cli.php can only be run from command line.", E_USER_ERROR
);
87 public function callApi() {
88 require_once 'api/api.php';
90 // CRM-9822 -'execute' action always goes thru Job api and always writes to log
91 if ($this->_action
!= 'execute' && $this->_joblog
) {
92 require_once 'CRM/Core/JobManager.php';
93 $facility = new CRM_Core_JobManager();
94 $facility->setSingleRunParams($this->_entity
, $this->_action
, $this->_params
, 'From Cli.php');
95 $facility->executeJobByAction($this->_entity
, $this->_action
);
98 // CRM-9822 cli.php calls don't require site-key, so bypass site-key authentication
99 $this->_params
['auth'] = FALSE;
100 $result = civicrm_api($this->_entity
, $this->_action
, $this->_params
);
103 if ($result['is_error'] != 0) {
104 $this->_log($result['error_message']);
107 elseif ($this->_output
) {
108 print_r($result['values']);
113 private function _parseOptions() {
114 $args = $_SERVER['argv'];
115 // remove the first argument, which is the name
119 while (list($k, $arg) = each($args)) {
120 // sanitize all user input
121 $arg = $this->_sanitize($arg);
123 // if we're not parsing an option signifier
124 // continue to the next one
125 if (!preg_match('/^-/', $arg)) {
129 // find the value of this arg
130 if (preg_match('/=/', $arg)) {
131 $parts = explode('=', $arg);
136 if (isset($args[$k +
1])) {
137 $next_arg = $this->_sanitize($args[$k +
1]);
138 // if the next argument is not another option
139 // it's the value for this argument
140 if (!preg_match('/^-/', $next_arg)) {
146 // parse the special args first
147 if ($arg == '-e' ||
$arg == '--entity') {
148 $this->_entity
= $value;
150 elseif ($arg == '-a' ||
$arg == '--action') {
151 $this->_action
= $value;
153 elseif ($arg == '-s' ||
$arg == '--site') {
154 $this->_site
= $value;
156 elseif ($arg == '-u' ||
$arg == '--user') {
157 $this->_user
= $value;
159 elseif ($arg == '-p' ||
$arg == '--password') {
160 $this->_password
= $value;
162 elseif ($arg == '-o' ||
$arg == '--output') {
163 $this->_output
= TRUE;
165 elseif ($arg == '-j' ||
$arg == '--joblog') {
166 $this->_joblog
= TRUE;
169 while(list($short, $long) = each ($this->_additional_arguments
)) {
170 if ($arg == '-' . $short ||
$arg == '--' . $long) {
171 $property = '_' . $long;
172 $this->$property = $value;
176 // all other arguments are parameters
177 $key = ltrim($arg, '--');
178 $this->_params
[$key] = isset($value) ?
$value : NULL;
184 private function _bootstrap() {
185 // so the configuration works with php-cli
186 $_SERVER['PHP_SELF'] = "/index.php";
187 $_SERVER['HTTP_HOST'] = $this->_site
;
188 $_SERVER['REMOTE_ADDR'] = "127.0.0.1";
189 $_SERVER['SERVER_SOFTWARE'] = NULL;
190 $_SERVER['REQUEST_METHOD'] = 'GET';
192 // SCRIPT_FILENAME needed by CRM_Utils_System::cmsRootPath
193 $_SERVER['SCRIPT_FILENAME'] = __FILE__
;
195 // CRM-8917 - check if script name starts with /, if not - prepend it.
196 if (ord($_SERVER['SCRIPT_NAME']) != 47) {
197 $_SERVER['SCRIPT_NAME'] = '/' . $_SERVER['SCRIPT_NAME'];
200 $civicrm_root = dirname(__DIR__
);
201 chdir($civicrm_root);
202 require_once ('civicrm.config.php');
204 if ( !class_exists('CRM_Core_ClassLoader') ) {
205 require_once $civicrm_root . '/CRM/Core/ClassLoader.php';
207 CRM_Core_ClassLoader
::singleton()->register();
209 $this->_config
= CRM_Core_Config
::singleton();
211 // HTTP_HOST will be 'localhost' unless overwritten with the -s argument.
212 // Now we have a Config object, we can set it from the Base URL.
213 if ($_SERVER['HTTP_HOST'] == 'localhost') {
214 $_SERVER['HTTP_HOST'] = preg_replace(
215 '!^https?://([^/]+)/$!i',
217 $this->_config
->userFrameworkBaseURL
);
220 $class = 'CRM_Utils_System_' . $this->_config
->userFramework
;
223 if (!CRM_Utils_System
::loadBootstrap(array(), FALSE, FALSE, $civicrm_root)) {
224 $this->_log(ts("Failed to bootstrap CMS"));
228 if (strtolower($this->_entity
) == 'job') {
230 $this->_log(ts("Jobs called from cli.php require valid user as parameter"));
235 if (!empty($this->_user
)) {
236 if(!CRM_Utils_System
::authenticateScript(TRUE, $this->_user
, $this->_password
, TRUE, FALSE, FALSE)) {
237 $this->_log(ts("Failed to login as %1. Wrong username or password.", array('1' => $this->_user
)));
240 if (!$cms->loadUser($this->_user
)) {
241 $this->_log(ts("Failed to login as %1", array('1' => $this->_user
)));
249 private function _validateOptions() {
250 $required = $this->_required_arguments
;
251 while (list(, $var) = each($required)) {
253 if (empty($this->$index)) {
254 $missing_arg = '--' . $var;
255 $this->_log(ts("The %1 argument is required", array(1 => $missing_arg)));
256 $this->_log($this->_getUsage());
263 private function _sanitize($value) {
264 // restrict user input - we should not be needing anything
265 // other than normal alpha numeric plus - and _.
266 return trim(preg_replace('#^[^a-zA-Z0-9\-_=/]$#', '', $value));
269 private function _getUsage() {
270 $out = "Usage: cli.php -e entity -a action [-u user] [-s site] [--output] [PARAMS]\n";
271 $out .= " entity is the name of the entity, e.g. Contact, Event, etc.\n";
272 $out .= " action is the name of the action e.g. Get, Create, etc.\n";
273 $out .= " user is an optional username to run the script as\n";
274 $out .= " site is the domain name of the web site (for Drupal multi site installs)\n";
275 $out .= " --output will print the result from the api call\n";
276 $out .= " PARAMS is one or more --param=value combinations to pass to the api\n";
280 private function _log($error) {
281 // fixme, this should call some CRM_Core_Error:: function
282 // that properly logs
288 * class used by csv/export.php to export records from
289 * the database in a csv file format.
292 class civicrm_cli_csv_exporter
extends civicrm_cli
{
293 var $separator = ',';
295 function __construct() {
296 $this->_required_arguments
= array('entity');
297 parent
::initialize();
301 $out = fopen("php://output", 'w');
302 fputcsv($out, $this->columns
, $this->separator
, '"');
305 $result = civicrm_api($this->_entity
, 'Get', $this->_params
);
307 foreach ($result['values'] as $row) {
309 $columns = array_keys($row);
310 fputcsv($out, $columns, $this->separator
, '"');
313 //handle values returned as arrays (i.e. custom fields that allow multiple selections) by inserting a control character
314 foreach ($row as &$field) {
315 if(is_array($field)) {
317 $field = implode($field,CRM_Core_DAO
::VALUE_SEPARATOR
) . CRM_Core_DAO
::VALUE_SEPARATOR
;
320 fputcsv($out, $row, $this->separator
, '"');
328 * base class used by both civicrm_cli_csv_import
329 * and civicrm_cli_csv_deleter to add or delete
330 * records based on those found in a csv file
331 * passed to the script.
334 class civicrm_cli_csv_file
extends civicrm_cli
{
336 var $separator = ',';
338 function __construct() {
339 $this->_required_arguments
= array('entity','file');
340 $this->_additional_arguments
= array('f' => 'file');
341 parent
::initialize();
346 $handle = fopen($this->_file
, "r");
349 die("Could not open file: " . $this->_file
. ". Please provide an absolute path.\n");
353 $header = fgetcsv($handle, 0, $this->separator
);
354 // In case fgetcsv couldn't parse the header and dumped the whole line in 1 array element
355 // Try a different separator char
356 if (count($header) == 1) {
357 $this->separator
= ";";
359 $header = fgetcsv($handle, 0, $this->separator
);
360 if (count($header) == 1) {
361 die("Invalid file format for " . $this->_file
. ". It must be a valid csv with separator ',' or ';'\n");
365 $this->header
= $header;
366 while (($data = fgetcsv($handle, 0, $this->separator
)) !== FALSE) {
368 if(count($data) == 1 && is_null($data[0])) continue;
370 $params = $this->convertLine($data);
371 $this->processLine($params);
377 /* return a params as expected */
378 function convertLine($data) {
380 foreach ($this->header
as $i => $field) {
381 //split any multiselect data, denoted with CRM_Core_DAO::VALUE_SEPARATOR
382 if (strpos($data[$i], CRM_Core_DAO
::VALUE_SEPARATOR
) !== FALSE) {
383 $data[$i] = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,$data[$i]);
384 $data[$i] = array_combine($data[$i], $data[$i]);
386 $params[$field] = $data[$i];
388 $params['version'] = 3;
394 * class for processing records to add
395 * used by csv/import.php
399 class civicrm_cli_csv_importer
extends civicrm_cli_csv_file
{
400 function processline($params) {
401 $result = civicrm_api($this->_entity
, 'Create', $params);
402 if ($result['is_error']) {
403 echo "\nERROR line " . $this->row
. ": " . $result['error_message'] . "\n";
406 echo "\nline " . $this->row
. ": created " . $this->_entity
. " id: " . $result['id'] . "\n";
412 * class for processing records to delete
413 * used by csv/delete.php
417 class civicrm_cli_csv_deleter
extends civicrm_cli_csv_file
{
418 function processline($params) {
419 $result = civicrm_api($this->_entity
, 'Delete', $params);
420 if ($result['is_error']) {
421 echo "\nERROR line " . $this->row
. ": " . $result['error_message'] . "\n";
423 echo "\nline " . $this->row
. ": deleted\n";