aboutsummaryrefslogtreecommitdiff
path: root/h-source/Application/Controllers/BaseController.php
blob: b201165480d0fcf538344786725364b9ceacdb1b (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
<?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 program 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.
// 
// This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

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

class BaseController extends Controller
{
	
	protected $lang;
	protected $ismoderator;
	protected $querySanitized = true;
	
	protected $_topMenuClasses = array(
		"home"			=>	null,
		"hardware"		=>	null,
		"credits"		=>	null,
		"issues"		=>	null,
		"contact"		=>	null,
		"search"		=>	null,
		"news"			=>	null,
		"download"		=>	null,
		"help"			=>	null,
	);
	
	public function __construct($model, $controller, $queryString) {
		parent::__construct($model, $controller, $queryString);
		
		header("Cache-Control: no-cache");
		
		$this->model('BoxesModel');
				
		$this->load('header');
		$this->load('footer','last');
		
		$this->session('registered');
		$this->s['registered']->checkStatus();
			
		$data['username'] = null;
		$data['islogged'] = 'no';
		$data['token'] = 'token';
		$data['ismoderator'] = false;
		$this->ismoderator = false;
		
		if ($this->s['registered']->status['status'] === 'logged')
		{
			$data['username'] = $this->s['registered']->status['user'];
			$data['islogged'] = 'yes';
			$data['token'] = $this->s['registered']->status['token'];
			$data['ismoderator'] = in_array('moderator',$this->s['registered']->status['groups']) ? true : false;
			$this->ismoderator = $data['ismoderator'];
		}
		
		$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['tm'] = $this->_topMenuClasses;
		
// 		print_r($this->_queryString);
		$this->_queryString = $this->sanitizeQueryString($this->_queryString);
		
		$this->append($data);
		
	}
	
	protected function right($lang = 'en')
	{
		$hard = new HardwareModel();
		
		$data['stat'] = $hard->clear()->select('type,count(*) AS numb')->where(array('-deleted'=>'no'))->groupBy('type')->toList('type','aggregate.numb')->send();
		
		$logged = $this->s['registered']->getUsersLogged();
		
		$data['numbLogged'] = count($logged);

		// 		get the right column container
		$this->m['BoxesModel']->setWhereQueryClause(array('title'=>'right_bottom'));
		$boxes = $this->m['BoxesModel']->getAll('boxes');

		if (count($boxes) > 0)
		{
			$xml = htmlspecialchars_decode($boxes[0]['boxes']['message'],ENT_QUOTES);

			$box_news = new BoxParser($xml);
			$data['htmlRightBox'] = $box_news->render();
		}
		else
		{
			$data['htmlRightBox'] = null;
		}
		
		$data['language_links'] = $this->buildLanguageLinks($this->lang);
		
// 		print_r($this->_queryString);
		
		$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";
		$link = "<ul 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 src='".$this->baseUrl."/Public/Img/Famfamfam/".$fullNameArray[0]."'><span>".$fullNameArray[1]."</span>";
			$link .= "\t<li><a $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 . ucwords($this->controller) . DS . $fileInt . '.php'))
		{
			$this->load($fileInt);
		}
		else
		{
			$this->load($file);
		}
	}
	
}