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
|
<?php
/**
*
* PHP version 5.4
*
* @category Mageia
* @package Mageia\Web\www\Pinq
* @author rda <rda@mageia.org>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL-2+
* @link http://www.mageia.org/
*
*/
/**
*/
class Pinq_Controller
{
/**
* @param string $dir root application directory
* @param array $server $_SERVER array
*
*/
public function __construct($dir, $server)
{
$this->_app_root = $dir;
$this->_server = $server;
}
/**
*/
public function init()
{
$this->uri = $this->_server['REQUEST_URI'];
if (strpos('?', $this->uri) !== false) {
$uri = explode('?', $this->uri);
$this->query = $uri[1];
$this->uri = $uri[0];
}
}
/**
* @param array $routes
*
* @return boolean
*/
public function run($routes = null)
{
// static, image files are expected to be served directly by the server.
// detect path language; if not set, redirect to best fallback language (English for now), end
$this->lang = $this->get_request_language($this->uri);
if (!$this->lang_is_managed($this->lang)) {
// TODO
}
// delegate to declared routes/apps
$re = '
/
\/(([a-zA-Z\-\_]{2,5})\/)?
(.*)
/x';
if (preg_match_all($re, $this->uri, $matches)) {
$this->_lang = $matches[2][0];
$this->_path = $matches[3][0];
}
if (is_array($routes) && count($routes) > 0) {
foreach ($routes as $r => $app) {
$re = '/' . $r . '/';
if (preg_match_all($re, $this->uri, $m)) {
return $this->delegate_to($app);
}
}
}
// delegate to local script
// TODO look at local code at $uri, and decide if we can load it and decorate it.
// finally, act as we used to before
if ($this->fallback_to_previous_mode($this->uri, $this->lang)) {
return true;
}
// if nothing matched, well...
return $this->delegate_to('error', array('code' => '404'));
}
/**
* @param string $uri
*
* @return string
*/
function get_request_language($uri)
{
$tu = explode('/', $uri);
$lang = $tu[1];
$sl = strlen($lang);
if ($this->lang_exists($lang)) {
return $lang;
}
return null;
}
/**
*/
function lang_exists($lang)
{
// is it a valid language code? ll[-cc] or ll[_CC]?
return true;
}
/**
*/
function lang_is_managed($lang)
{
// in managed languages array?
return true;
}
/**
* @param string $app_name
*
* @return boolean
*/
private function delegate_to($app_name, $options = null)
{
$app_cont = realpath(sprintf('%s/apps/%s/%s_app.php', $this->_app_root, $app_name, $app_name));
if (!file_exists($app_cont)) {
throw new Exception(sprintf('Expected "%s" app, found no definition file.', $app_name));
}
include $app_cont;
$a = new $app_name($this, $options);
return $a->run();
}
/**
* @return boolean true if fallback happened
*/
function fallback_to_previous_mode($uri, $lang)
{
$alt_uri = sprintf(
'/%s/%s',
'en',
substr($uri, strlen($lang) + 2)
);
$test_uris = array(
/*
$uri,
$uri . 'index.php',
$uri . 'index.html',
/**/
$alt_uri,
$alt_uri . 'index.php',
$alt_uri . 'index.html'
);
$found = false;
foreach ($test_uris as $inc_uri) {
$real_file = realpath($this->_app_root . $inc_uri);
if (file_exists($real_file) && !is_dir($real_file)) {
$found = true;
break;
}
}
if ($found) {
chdir(dirname($real_file));
require $real_file;
return true;
}
return false;
}
}
|