aboutsummaryrefslogtreecommitdiff
path: root/h-source/Application/Controllers/BaseController.php
blob: 4b1246f4ea97f5697edd13167ab75d31d4865c4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php 

// h-source, a web software to build a community of people that want to share their hardware information.
// Copyright (C) 2010  Antonio Gallo (h-source-copyright.txt)
//
// This file is part of h-source
//
// h-source is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// h-source is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with h-source.  If not, see <http://www.gnu.org/licenses/>.

if (!defined('EG')) die('Direct access not allowed!');

class BaseController extends Controller
{
	
	protected $lang;
	protected $islogged = 'no';
	protected $ismoderator = false;
	protected $isadmin = false;
	protected $querySanitized = true;
	protected $token = 'token';
	protected $_updating = 'no';

	protected $_configXml = null;

	protected $_topMenuClasses = array(
		"home"			=>	null,
		"hardware"		=>	null,
		"credits"		=>	null,
		"issues"		=>	null,
		"contact"		=>	null,
		"search"		=>	null,
		"news"			=>	null,
		"download"		=>	null,
		"help"			=>	null,
		"wiki"			=>	null,
		"faq"			=>	null,
		"project"		=>	null,
	);
	
	public function __construct($model, $controller, $queryString) {
		parent::__construct($model, $controller, $queryString);
		
		header("Cache-Control: no-cache");

		$this->model('BoxesModel');
		$this->model('HistoryModel');
		$this->model('ParamsModel');
		$this->model('UsersModel');
				
		$this->load('header');
		$this->load('footer','last');
		
		$this->session('registered');
		$this->s['registered']->checkStatus();
			
		$data['username'] = null;
		$data['user_groups'] = null;
		$data['islogged'] = 'no';
		$data['token'] = 'token';
		$data['ismoderator'] = false;
		$data['isadmin'] = false;
		
		if ($this->s['registered']->status['status'] === 'logged')
		{
			$data['username'] = $this->s['registered']->status['user'];
			$groups = $this->s['registered']->status['groups'];
			$groups[] = 'registered';
			$data['user_groups'] = implode(',',$groups);
			
			$data['islogged'] = 'yes';
			$this->islogged = $data['islogged'];
			
			$data['token'] = $this->s['registered']->status['token'];
			$this->token = $data['token'];
			
			$data['ismoderator'] = in_array('moderator',$this->s['registered']->status['groups']) ? true : false;
			$this->ismoderator = $data['ismoderator'];

			$data['isadmin'] = in_array('admin',$this->s['registered']->status['groups']) ? true : false;
			$this->isadmin = $data['isadmin'];
		}
		
		$data['lang'] = 'en';
		$this->lang = 'en';

		if (isset($this->_queryString[0]))
		{
			$lang = (strcmp($this->_queryString[0],'') !== 0) ? $this->_queryString[0] : 'en';
			$data['lang'] = Lang::sanitize($lang);
			$this->lang = $data['lang'];
			Lang::$current = $data['lang'];
		}

		$data['langIcon'] = Lang::getIcon(Lang::$current);
		$data['langLabel'] = Lang::getLabel(Lang::$current);
		
		//set desktop or mobile version
		Version::set();
		//subfolder of the Views folder where to look for view files
		Params::$viewSubfolder = Version::getViewSubfolder();

		$data['tm'] = $this->_topMenuClasses;
		
		$this->_queryString = $this->sanitizeQueryString($this->_queryString);

		$data['querySanitized']	= $this->querySanitized;
		$data['queryString'] = Url::createUrl($this->_queryString);

		//check ft they are updating the website
		$updating = $this->m['ParamsModel']->select('updating')->where(array('id_par'=>1))->toList('updating')->send();

		$data['updating_flag'] = 'no';
		if (count($updating)>0)
		{
			$data['updating_flag'] = $updating[0];
			$this->_updating = $data['updating_flag'];
		}

		//get the configuration xml
		$xmlRes = $this->m['ParamsModel']->select('boxes_xml')->where(array('id_par'=>1))->toList('boxes_xml')->send();
		if (count($xmlRes)>0)
		{
			$configXml = htmlspecialchars_decode($xmlRes[0],ENT_QUOTES);
			
			if (Website::$useXmlConfigFile)
			{
				$xmlConfigFile = rtrim(Website::$xmlConfigFileFolder,'/') . '/config.xml';
				if (@simplexml_load_file($xmlConfigFile))
				{
					$this->_configXml = simplexml_load_file($xmlConfigFile);
				}
			}
			else
			{
				if (@simplexml_load_string($configXml))
				{
					$this->_configXml = simplexml_load_string($configXml);
				}
			}
		}

		//elements of the top menu from the config.xml file
		//help link
		$mod = new BoxParser($this->getBox(array("top_menu","help_page_link")));
		$data["topMenuHelpLink"] = $mod->modules[0]->render($this->_topMenuClasses["help"]);
		//faq link
		$mod = new BoxParser($this->getBox(array("top_menu","faq_link")));
		$data["topMenuFaqLink"] = $mod->modules[0]->render($this->_topMenuClasses["faq"]);
		
		//link to the "discover your hardware" wiki page
		$data['discoverYourHardwareLink'] = $this->getModule(array('right_column','discover_your_hardware'));
		
		$this->append($data);
	}

	//get the right box from the configuration xml
	protected function getBox($xmlPath,$xmlString = null)
	{
		if (!isset($xmlString))
		{
			$xmlString = $this->_configXml;
		}

		if (is_array($xmlPath))
		{
			if (isset($xmlString))
			{
				$tempXml = $xmlString->{$xmlPath[0]};
				if (count($xmlPath) === 1)
				{
					if (isset($tempXml->{$this->lang}))
					{
						return $tempXml->{$this->lang}->asXml();
					}
					else if (isset($tempXml->{"en"}))
					{
						return $tempXml->{"en"}->asXml();
					}
					else
					{
						return null;
					}
				}
				else
				{
					array_shift($xmlPath);
					return $this->getBox($xmlPath,$tempXml);
				}
			}
		}
		return null;
	}

// 	get the HTML of a module from the xml configuration string
//	$xmlPath: array conitaining the path
	protected function getModule($xmlPath)
	{
		$tracksHelpLabel = null;
		$xml = $this->getBox($xmlPath);
		if ($xml)
		{
			$box_news = new BoxParser($xml);
			$tracksHelpLabel = $box_news->render();
		}
		return $tracksHelpLabel;
	}
	
	protected function right($lang = 'en')
	{
		$hard = new HardwareModel();
		
		$data['stat'] = $hard->clear()->select('type,count(*) AS numb')->where(array('-deleted'=>'no','cleared'=>'no'))->groupBy('type')->toList('type','aggregate.numb')->send();
		
		$logged = $this->s['registered']->getUsersLogged();
		
		$data['numbLogged'] = count($logged);

		//render the boxes inside the right column
		$data['htmlRightBox'] = $this->getModule(array('right_column'));

		$data['language_links'] = $this->buildLanguageLinks($this->lang);
		
		$data['lastModif'] = $this->m['HistoryModel']
								->clear()->select()
								->inner('hardware')
								->on('hardware.id_hard=history.id')
								->where(array('type'=>'hardware','gr'=>'registered','deleted'=>'no','cleared'=>'no',))
								->orderBy('id_history desc')
								->limit(5)
								->send();

		$this->append($data);
		$this->load('right');
	}
	
	protected function sanitizeQueryString($queryArray)
	{
		$resArray = array();
		foreach ($queryArray as $item)
		{
			if (preg_match('/^[a-zA-Z0-9\-\_\.\+\s]+$/',$item))
			{
				$resArray[] = sanitizeAll($item);
			}
			else
			{
				$this->querySanitized = false;
				return array('en');
			}
		}
		return $resArray;
	}
	
	protected function buildLanguageLinks($lang)
	{
		$status = $this->_queryString;
		$cPage = $this->querySanitized ? $this->currPage : $this->baseUrl."/home/index";
		
		$mobileDataRole = Version::get() === "mobile" ? "data-role='listview'" : null;
		
		$link = "<ul $mobileDataRole class='languages_link_box'>\n";
		foreach (Lang::$complete as $abbr => $fullName)
		{
			$linkClass = (strcmp($abbr,$lang) === 0) ? "class='current_lang'" : null;
			$status[0] = $abbr;
			$href = Url::createUrl($status);
			$fullNameArray = explode(',',$fullName);
			$text = "<img class='ui-li-icon' src='".$this->baseUrl."/Public/Img/Famfamfam/".$fullNameArray[0]."'><span>".$fullNameArray[1]."</span>";

			$mobileRel = Version::get() === "mobile" ? "rel='external'" : null;
			$link .= "\t<li><a $mobileRel $linkClass href='".$cPage.$href."'>$text</a></li>\n";
		}
		$link .= "</ul>\n";
		return $link;
	}
	
	protected function cleverLoad($file)
	{
		$fileInt = $file."_".$this->lang;
		if (file_exists(ROOT . DS . APPLICATION_PATH . DS . 'Views' . DS . Params::$viewSubfolder . DS . ucwords($this->controller) . DS . $fileInt . '.php'))
		{
			$this->load($fileInt);
		}
		else
		{
			$this->load($file);
		}
	}
	
}