Cheetah
cmd.php
Go to the documentation of this file.
1 <?php
2 
8 define('CH_INSTALL_DO_NOT_EXIT_ON_ERROR', 1);
9 
10 // include necessary files to perform install
11 $_REQUEST['action'] = 'empty';
12 $aPathInfo = pathinfo(__FILE__);
13 require_once($aPathInfo['dirname'] . '/index.php');
14 
19 {
20  protected $_sHeaderPath;
21  protected $_aSiteConfig;
22  protected $_isQuiet = false;
23  protected $_aReturnCodes = array(
24  'success' => array ('code' => 0, 'msg' => 'Success.'),
25  'already installed' => array ('code' => 0, 'msg' => 'Script is already installed. Can\'t perform install.'),
26  'requirements failed' => array ('code' => 2, 'msg' => 'Requirements aren\'t met.'),
27  'permissions failed' => array ('code' => 3, 'msg' => 'Folders and/or files permissions aren\'t correct.'),
28  'create config failed' => array ('code' => 4, 'msg' => 'Form data was not submitted.'),
29  'lang compile failed' => array ('code' => 5, 'msg' => 'Language compilation failed.'),
30  );
31 
32  public function __construct()
33  {
34  $aPathInfo = pathinfo(__FILE__);
35 
36  $this->_aSiteConfig = array (
37  'site_url' => 'localhost',
38  'site_dir' => str_replace('/install', '/', $aPathInfo['dirname']),
39  'php_path' => '/usr/bin/php',
40  // form data below
41  'site_config' => true,
42  'db_name' => 'test',
43  'db_user' => 'root',
44  'db_password' => 'root',
45  'site_title' => 'Cheetah',
46  'site_email' => 'no-reply@example.com',
47  'admin_email' => 'admin@example.com',
48  'admin_username' => 'admin',
49  'admin_password' => 'cheetah',
50  );
51 
52  $this->_sHeaderPath = $this->_aSiteConfig['site_dir'] . 'inc/header.inc.php';
53  }
54 
55  public function main()
56  {
57  // set neccessary options
58 
59  $a = getopt('hq', $this->getOptions());
60 
61  if (isset($a['h']))
62  $this->finish($this->_aReturnCodes['success']['code'], $this->getHelp());
63 
64  if (isset($a['q']))
65  $this->_isQuiet = true;
66 
67  $this->_aSiteConfig = array_merge($this->_aSiteConfig, $a);
68 
69  // initialize environment
70 
71  $this->init();
72 
73  // peform install
74 
75  $this->checkRequirements();
76  $this->checkPermissions();
77  $this->createSiteConfig();
78  $this->compileLanguage();
79 
80  $this->finish($this->_aReturnCodes['success']['code'], $this->_aReturnCodes['success']['msg']);
81  }
82 
83  protected function getOptions()
84  {
85  $a = array ();
86  foreach ($this->_aSiteConfig as $sKey => $sValue)
87  if ('site_config' != $sKey)
88  $a[] = "$sKey::";
89  return $a;
90  }
91 
92  protected function getHelp()
93  {
94  $s = "Usage: php cmd.php [options]\n";
95 
96  $s .= str_pad("\t -h", 35) . "Print this help\n";
97  $s .= str_pad("\t -q", 35) . "Quiet\n";
98 
99  foreach ($this->_aSiteConfig as $sKey => $sVal)
100  if ('site_config' != $sKey)
101  $s .= str_pad("\t --{$sKey}=<value>", 35) . "Default value: {$sVal}\n";
102 
103  $s .= "\n";
104  $s .= "Return codes:\n";
105  foreach ($this->_aReturnCodes as $r)
106  $s .= str_pad("\t {$r['code']}", 5) . "{$r['msg']}\n";
107 
108  return $s;
109  }
110 
111  protected function finish($iCode, $sMsg)
112  {
113  if (!$this->_isQuiet)
114  fwrite($iCode ? STDERR : STDOUT, $sMsg . "\n");
115 
116  exit($iCode);
117  }
118 
119  protected function init()
120  {
121  // skip this test if script is already installed
122  if (file_exists($this->_sHeaderPath))
123  $this->finish($this->_aReturnCodes['already installed']['code'], $this->_aReturnCodes['already installed']['msg']);
124  }
125 
126  public function checkRequirements()
127  {
128  if (!empty($GLOBALS['aErrors']))
129  $this->finish($this->_aReturnCodes['requirements failed']['code'], $this->_aReturnCodes['requirements failed']['msg']);
130  }
131 
132  public function checkPermissions()
133  {
134  $sError = '';
135  $sErrorMsg = checkPreInstallPermission($sError);
136 
137  if ($sErrorMsg)
138  $this->finish($this->_aReturnCodes['permissions failed']['code'], $this->_aReturnCodes['permissions failed']['msg']);
139  }
140 
141  public function createSiteConfig()
142  {
143  $this->_setSiteConfigVars();
145 
146  if (!file_exists($this->_sHeaderPath))
147  $this->finish($this->_aReturnCodes['create config failed']['code'], $this->_aReturnCodes['create config failed']['msg']);
148  }
149 
150  public function compileLanguage()
151  {
153  require_once($GLOBALS['aPathInfo']['dirname'] . '/../inc/header.inc.php');
154  require_once(CH_DIRECTORY_PATH_INC . 'design.inc.php');
156 
157  $sLang = db_value("SELECT `Name` FROM `sys_localization_languages` LIMIT 1");
158  if (empty($sLang) || !file_exists($GLOBALS['aPathInfo']['dirname'] . '/../langs/lang-' . $sLang . '.php'))
159  $this->finish($this->_aReturnCodes['lang compile failed']['code'], $this->_aReturnCodes['lang compile failed']['msg']);
160  }
161 
162  protected function _setSiteConfigVars()
163  {
164  $aSiteConfigMapVars = array (
165  'site_url' => &$this->_aSiteConfig['site_url'],
166  'dir_root' => &$this->_aSiteConfig['site_dir'],
167  'dir_php' => &$this->_aSiteConfig['php_path'],
168 
169  // db conf
170  'sql_file' => eval($GLOBALS['aDbConf']['sql_file']['def_exp']),
171  'db_host' => $GLOBALS['aDbConf']['db_host']['def'],
172  'db_port' => $GLOBALS['aDbConf']['db_port']['def'],
173  'db_sock' => $GLOBALS['aDbConf']['db_sock']['def'],
174  'db_name' => &$this->_aSiteConfig['db_name'],
175  'db_user' => &$this->_aSiteConfig['db_user'],
176  'db_password' => &$this->_aSiteConfig['db_password'],
177 
178  // general
179  'site_title' => &$this->_aSiteConfig['site_title'],
180  'site_desc' => '',
181  'site_email' => &$this->_aSiteConfig['admin_email'],
182  'notify_email' => &$this->_aSiteConfig['site_email'],
183  'bug_report_email' => &$this->_aSiteConfig['admin_email'],
184  'admin_username' => &$this->_aSiteConfig['admin_username'],
185  'admin_password' => &$this->_aSiteConfig['admin_password'],
186  );
187 
188  foreach ($aSiteConfigMapVars as $k => $v)
189  $_POST[$k] = $_REQUEST[$k] = $v;
190  }
191 }
192 
194 $o->main();
195 
ChWsbInstallCmd\$_aSiteConfig
$_aSiteConfig
Definition: cmd.php:21
ChWsbInstallCmd\createSiteConfig
createSiteConfig()
Definition: cmd.php:141
ChWsbInstallCmd\$_aReturnCodes
$_aReturnCodes
Definition: cmd.php:23
$aPathInfo
$aPathInfo
Definition: cmd.php:12
ChWsbInstallCmd\$_sHeaderPath
$_sHeaderPath
Definition: cmd.php:20
ChWsbInstallCmd\checkRequirements
checkRequirements()
Definition: cmd.php:126
ChWsbInstallCmd\init
init()
Definition: cmd.php:119
ChWsbInstallCmd\_setSiteConfigVars
_setSiteConfigVars()
Definition: cmd.php:162
php
$sMsg
$sMsg
Definition: actions.inc.php:22
ChWsbInstallCmd\getHelp
getHelp()
Definition: cmd.php:92
ChWsbInstallCmd
Definition: cmd.php:19
performInstallLanguages
performInstallLanguages()
Definition: index.php:516
checkPreInstallPermission
checkPreInstallPermission(&$sError)
Definition: index.php:971
exit
exit
Definition: cart.php:21
genInstallationProcessPage
genInstallationProcessPage($sErrorMessage='')
Definition: index.php:566
ChWsbInstallCmd\compileLanguage
compileLanguage()
Definition: cmd.php:150
ChWsbInstallCmd\$_isQuiet
$_isQuiet
Definition: cmd.php:22
$_REQUEST
$_REQUEST['action']
Definition: cmd.php:11
global
if(!defined("GLOBAL_MODULE")) define("GLOBAL_MODULE" global
Definition: header.inc.php:25
ChWsbInstallCmd\main
main()
Definition: cmd.php:55
$tmpl
$tmpl
Definition: params.inc.php:87
ChWsbInstallCmd\checkPermissions
checkPermissions()
Definition: cmd.php:132
$s
$s
Definition: embed.php:13
ChWsbInstallCmd\finish
finish($iCode, $sMsg)
Definition: cmd.php:111
db_value
db_value($query, $bindings=[], $error_checking=true, $index=0)
Definition: db.inc.php:98
$oSysTemplate
$oSysTemplate
Definition: params.inc.php:22
$sError
$sError
Definition: index.php:425
ChWsbInstallCmd\__construct
__construct()
Definition: cmd.php:32
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
$o
$o
Definition: cmd.php:193
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10
ChWsbInstallCmd\getOptions
getOptions()
Definition: cmd.php:83