Merge pull request #15817 from colemanw/Fix
[civicrm-core.git] / tools / bin / scripts / cli.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
5b71fd5f 4 | CiviCRM version 5 |
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 |
11 | Version 3, 19 November 2007. |
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 |
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 +--------------------------------------------------------------------+
24*/
25
26/**
27 * A PHP shell script
b7c0a88f 28 *
29 * On drupal if you have a symlink to your civi module, don't forget to create a new file - settings_location.php
30 * Enter the following code (substitute the actual location of your <drupal root>/sites directory)
31 * <?php
32 * define( 'CIVICRM_CONFDIR', '/var/www/drupal.6/sites' );
33 * ?>
6a488035 34 */
bed96570 35$include_path = "../packages/:" . get_include_path();
6a488035 36set_include_path($include_path);
a1a55b61
EM
37
38/**
39 * Class civicrm_CLI
40 */
6a488035
TO
41class civicrm_CLI {
42
43 /**
44 * constructor
45 */
46 function __construct() {
47 // $include_path = "packages/" . get_include_path( );
48 // set_include_path( $include_path );
49 require_once 'Console/Getopt.php';
50 $shortOptions = "s:u:p:k:";
b7c0a88f 51 $longOptions = ['site=', 'user', 'pass'];
6a488035
TO
52
53 $getopt = new Console_Getopt();
54 $args = $getopt->readPHPArgv();
55 array_shift($args);
56 list($valid, $this->args) = $getopt->getopt2($args, $shortOptions, $longOptions);
57
b7c0a88f 58 $vars = [
6a488035
TO
59 'user' => 'u',
60 'pass' => 'p',
61 'key' => 'k',
62 'site' => 's',
b7c0a88f 63 ];
6a488035
TO
64
65 foreach ($vars as $var => $short) {
66 $$var = NULL;
67 foreach ($valid as $v) {
68 if ($v[0] == $short || $v[0] == "--$var") {
69 $$var = $v[1];
70 break;
71 }
72 }
73 if (!$$var) {
74 die("\nUsage: $ php5 " . $_SERVER['PHP_SELF'] . " -k key -u user -p password -s yoursite.org\n");
75 }
76 }
77 $this->site = $site;
78 $this->key = $key;
79 $this->setEnv();
80 $this->authenticate($user, $pass);
81 }
82
a1a55b61
EM
83 /**
84 * @param $user
85 * @param $pass
86 */
6a488035
TO
87 function authenticate($user, $pass) {
88 session_start();
89 require_once 'CRM/Core/Config.php';
f3a87cf4 90 // Does calling this do anything here?
91 CRM_Core_Config::singleton();
6a488035
TO
92
93 // this does not return on failure
94 // require_once 'CRM/Utils/System.php';
95 // CRM_Utils_System::authenticateScript( true );
96 CRM_Utils_System::authenticateScript(TRUE, $user, $pass);
97 }
98
99 function setEnv() {
100 // so the configuration works with php-cli
101 $_SERVER['PHP_SELF'] = "/index.php";
102 $_SERVER['HTTP_HOST'] = $this->site;
103 $_REQUEST['key'] = $this->key;
b7c0a88f 104 require_once("./civicrm.config.php");
6a488035
TO
105 }
106}
107
108
109//$cli=new civicrm_cli ();
110