1<?php
2/**
3 * eGgroupWare setup - show/return the header.inc.php
4 *
5 * @link http://www.egroupware.org
6 * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
7 * @package setup
8 * @copyright (c) 2007-16 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
9 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
10 * @version $Id$
11 */
12
13use EGroupware\Api;
14
15/**
16 * setup command: show/return the header.inc.php
17 *
18 * Has no constructor, as we have no arguments beside the header admin user and password,
19 * which get set via setup_cmd::set_header_secret($user,$pw)
20 */
21class setup_cmd_showheader extends setup_cmd
22{
23	/**
24	 * Allow to run this command via setup-cli
25	 */
26	const SETUP_CLI_CALLABLE = true;
27
28	/**
29	 * Constructor
30	 *
31	 * @param boolean $data=true true: send only the remote_hash, domain and webserver_url,
32	 *                           false: the complete header vars, plus install_id and webserver_url from the config table,
33	 *                           null:  only the header vars
34	 */
35	function __construct($data=true,$header_admin_user=null,$header_admin_password=null)
36	{
37		if (!is_array($data))
38		{
39			$data = array(
40				'hash_only' => $data,
41				'header_admin_user' => $header_admin_user,
42				'header_admin_password' => $header_admin_password,
43			);
44		}
45		//echo __CLASS__.'::__construct()'; _debug_array($data);
46		admin_cmd::__construct($data);
47	}
48
49	/**
50	 * show/return the header.inc.php
51	 *
52	 * @param boolean $check_only =false only run the checks (and throw the exceptions), but not the command itself
53	 * @return string serialized $GLOBALS defined in the header.inc.php
54	 * @throws Exception(lang('Wrong credentials to access the header.inc.php file!'),2);
55	 * @throws Exception('header.inc.php not found!');
56	 */
57	function exec($check_only=false)
58	{
59		if ($this->remote_id && $check_only) return true;	// cant check for the remote site locally!
60
61		$this->_check_header_access();
62
63		if ($check_only) return true;
64
65		$egw_info_backup = $GLOBALS['egw_info'];
66		$GLOBALS['egw_info'] = array (
67			'flags' => array(
68				'noapi' => true,
69			),
70		);
71		if (!($header = file_get_contents(EGW_SERVER_ROOT.'/header.inc.php')))
72		{
73			throw new Exception('header.inc.php not found!');
74		}
75		eval(str_replace(array('<?php','perfgetmicrotime'),array('','perfgetmicrotime2'),$header));
76
77		// unset the flags, they are not part of  the header
78		unset($GLOBALS['egw_info']['flags']);
79
80		// include the api version of this instance
81		$GLOBALS['egw_info']['server']['versions']['phpgwapi'] = $egw_info_backup['server']['versions']['phpgwapi'];
82
83		// fetching the install id's stored in the database
84		foreach($GLOBALS['egw_domain'] as &$data)
85		{
86			if (!is_null($this->hash_only))
87			{
88				$data += $this->_fetch_config($data);
89			}
90			try {
91				// it's saver to only send the remote_hash and not install_id and config_pw
92				$data['remote_hash'] = admin_cmd::remote_hash($data['install_id'],$data['config_passwd']);
93			}
94			catch(Exception $e) {
95				if ($data['install_id']) $data['error'] .= $e->getMessage();
96			}
97			if ($this->hash_only)
98			{
99				$data = array(
100					'remote_hash'   => $data['remote_hash'],
101					'webserver_url' => $data['webserver_url'],
102					'install_id'     => $data['install_id'],
103				)+($data['error'] ? array(
104					'error' => $data['error'],
105				) : array());
106			}
107		}
108		if ($this->hash_only)
109		{
110			$ret = array('egw_domain' => $GLOBALS['egw_domain']);
111		}
112		else
113		{
114			$ret = array(
115				'egw_info' => $GLOBALS['egw_info'],
116				'egw_domain' => $GLOBALS['egw_domain'],
117				'EGW_SERVER_ROOT' => EGW_SERVER_ROOT,
118				'EGW_INCLUDE_ROOT' => EGW_INCLUDE_ROOT,
119			);
120		}
121		$GLOBALS['egw_info'] = $egw_info_backup;
122
123		return $ret;
124	}
125
126	/**
127	 * Fetch the install_id, and webserver_url of a domain from the DB
128	 *
129	 * @param array $data with values for keys 'db_name', 'db_host', 'db_port', 'db_user', 'db_pass', 'db_type'
130	 * @return array with values for keys install_id, webserver_url
131	 */
132	private function _fetch_config(array $data)
133	{
134		$db = new Api\Db();
135
136		ob_start();		// not available db connection echos a lot grab ;-)
137		$err_rep = error_reporting(0);
138
139		$config = array();
140		try {
141			$db->connect($data['db_name'],$data['db_host'],$data['db_port'],$data['db_user'],$data['db_pass'],$data['db_type']);
142			$db->set_app('phpgwapi');
143			$db->select('egw_config','config_name,config_value',array(
144				'config_name'=>array('install_id','webserver_url','account_repository','allow_remote_admin','mail_suffix'),
145				'config_app'=>'phpgwapi',
146			),__LINE__,__FILE__);
147			while (($row = $db->row(true)))
148			{
149				$config[$row['config_name']] = $row['config_value'];
150			}
151		}
152		catch (Exception $e) {
153			$config['error'] = strip_tags($e->getMessage());
154		}
155		error_reporting($err_rep);
156		ob_end_clean();
157
158		// restoring the db connection, seems to be necessary when we run via remote execution
159		$this->restore_db();
160
161		return $config;
162	}
163
164	/**
165	 * Saving the object to the database, reimplemented to only the save the command if it runs remote
166	 *
167	 * @param boolean $set_modifier =true set the current user as modifier or 0 (= run by the system)
168	 * @return boolean true on success, false otherwise
169	 */
170	function save($set_modifier=true)
171	{
172		if ($this->remote_id)
173		{
174			return parent::save($set_modifier);
175		}
176		return true;
177	}
178}
179