Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Lib.Main.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Main first level function
9  *
10  * @author Anakeen 2002
11  * @version $Id: Lib.Common.php,v 1.50 2008/09/11 14:50:04 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage CORE
15  */
16 /**
17  */
18 include_once ("Lib.Common.php");
19 
20 function getMainAction($auth, &$action)
21 {
22  include_once ('Class.Action.php');
23  include_once ('Class.Application.php');
24  include_once ('Class.Session.php');
25  include_once ('Lib.Http.php');
26  include_once ('Class.Log.php');
27  include_once ('Class.Domain.php');
28  include_once ('Class.DbObj.php');
29  $indexphp = basename($_SERVER["SCRIPT_NAME"]);
30 
31  $log = new Log("", $indexphp);
32 
33  $CoreNull = "";
34  global $CORE_LOGLEVEL;
35 
36  global $_GET;
37  $standalone = GetHttpVars("sole", "Y");
38  $defaultapp = false;
39  if (!getHttpVars("app")) {
40  $defaultapp = true;
41  $_GET["app"] = "CORE";
42  if ($_SERVER["FREEDOM_ACCESS"]) {
43  $_GET["app"] = $_SERVER["FREEDOM_ACCESS"];
44  $_GET["action"] = "";
45  } else {
46  $defaultapp = false;
47  $_GET["action"] = "INVALID";
48  }
49  }
50 
51  if (isset($_COOKIE['freedom_param'])) $sess_num = $_COOKIE['freedom_param'];
52  else $sess_num = GetHttpVars('freedom_param'); //$_GET["session"];
53  $session = new Session();
54  if (!$session->Set($sess_num)) {
55  print "<B>:~((</B>";
56  exit;
57  };
58 
59  $core = new Application();
60  $core->Set("CORE", $CoreNull, $session);
61 
62  if ($core->user->login != $_SERVER['PHP_AUTH_USER']) {
63  // reopen a new session
64  $session->Set("");
65  $core->SetSession($session);
66  }
67  if ($defaultapp && $core->GetParam("CORE_START_APP")) {
68  $_GET["app"] = $core->GetParam("CORE_START_APP");
69  }
70  $limit = ini_get("memory_limit");
71  if (is_string($limit)) {
72  $limitNum = intval(substr($limit, 0, -1));
73  $multipli = 1;
74  if (strcmp(substr($limit, -1), "G") == 0) {
75  $multipli = 1000;
76  }
77  if ($limitNum >= 0 && ($limitNum * $multipli) < intval($core->GetParam("MEMORY_LIMIT", "64"))) {
78  ini_set("memory_limit", $core->GetParam("MEMORY_LIMIT", "64") . "M");
79  }
80  }
81  //$core->SetSession($session);
82  $CORE_LOGLEVEL = $core->GetParam("CORE_LOGLEVEL", "IWEF");
83  // ----------------------------------------
84  // Init PUBLISH URL from script name
85  $pattern = preg_quote($indexphp);
86  if (preg_match("|(.*)/$pattern|", $_SERVER['SCRIPT_NAME'], $reg)) {
87  // determine publish url (detect ssl require)
88  if ($_SERVER['HTTPS'] != 'on') $puburl = "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $reg[1];
89  else $puburl = "https://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $reg[1];
90  } else {
91  // it is not allowed
92  print "<B>:~(</B>";
93  exit;
94  }
95  $add_args = "";
96  if (array_key_exists('authtype', $_GET)) {
97  $add_args.= "&authtype=" . $_GET['authtype'];
98  }
99 
100  $urlindex = $core->getParam("CORE_URLINDEX");
101  if ($urlindex) $core->SetVolatileParam("CORE_EXTERNURL", $urlindex);
102  else $core->SetVolatileParam("CORE_EXTERNURL", $puburl . "/");
103 
104  $core->SetVolatileParam("CORE_PUBURL", "."); // relative links
105  $core->SetVolatileParam("CORE_ABSURL", $puburl . "/"); // absolute links
106  $core->SetVolatileParam("CORE_JSURL", "WHAT/Layout");
107  $core->SetVolatileParam("CORE_ROOTURL", "?sole=R$add_args&");
108  $core->SetVolatileParam("CORE_BASEURL", "?sole=A$add_args&");
109  $core->SetVolatileParam("CORE_SBASEURL", "?sole=A&freedom_param={$session->id}$add_args&");
110  $core->SetVolatileParam("CORE_STANDURL", "?sole=Y$add_args&");
111  $core->SetVolatileParam("CORE_SSTANDURL", "?sole=Y&freedom_param={$session->id}$add_args&");
112  $core->SetVolatileParam("CORE_ASTANDURL", "$puburl/$indexphp?sole=Y$add_args&"); // absolute links
113  // ----------------------------------------
114  // Init Application & Actions Objects
115  if (($standalone == "") || ($standalone == "N")) {
116  $action = new Action();
117  $action->Set("MAIN", $core, $session);
118  } else {
119  $appl = new Application();
120  $err = $appl->Set(getHttpVars("app") , $core, $session);
121  if ($err) {
122  print $err;
123  exit;
124  }
125 
126  if (($appl->machine != "") && ($_SERVER['SERVER_NAME'] != $appl->machine)) { // special machine to redirect
127  if (substr($_SERVER['REQUEST_URI'], 0, 6) == "http:/") {
128  $aquest = parse_url($_SERVER['REQUEST_URI']);
129  $aquest['host'] = $appl->machine;
130  $puburl = glue_url($aquest);
131  } else {
132  $puburl = "http://" . $appl->machine . $_SERVER['REQUEST_URI'];
133  }
134 
135  Header("Location: $puburl");
136  exit;
137  }
138  // ----------------------------------------
139  // test SSL mode needed or not
140  // redirect if needed
141  if ($appl->ssl == "Y") {
142  if ($_SERVER['HTTPS'] != 'on') {
143  // redirect to go to ssl http
144  $sslurl = "https://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
145  Header("Location: $sslurl");
146  exit;
147  }
148 
149  $core->SetVolatileParam("CORE_BGCOLOR", $core->GetParam("CORE_SSLBGCOLOR"));
150  }
151  // -----------------------------------------------
152  // now we are in correct protocol (http or https)
153  $action = new Action();
154  $action->Set(getHttpVars("action") , $appl, $session);
155 
156  if ($auth) {
157  $core_lang = $auth->getSessionVar('CORE_LANG');
158  if ($core_lang != '') {
159  $action->setParamU('CORE_LANG', $core_lang);
160  $auth->setSessionVar('CORE_LANG', '');
161  }
162  $action->auth = & $auth;
163  $core->SetVolatileParam("CORE_BASICAUTH", '&authtype=basic');
164  } else $core->SetVolatileParam("CORE_BASICAUTH", '');
165 
166  $nav = $_SERVER['HTTP_USER_AGENT'];
167  $pos = strpos($nav, "MSIE");
168  if ($action->Read("navigator", "") == "") {
169  if ($pos !== false) {
170  $action->Register("navigator", "EXPLORER");
171  if (preg_match("/MSIE ([0-9.]+).*/", $nav, $reg)) {
172  $action->Register("navversion", $reg[1]);
173  }
174  } else {
175  $action->Register("navigator", "NETSCAPE");
176  if (preg_match("|([a-zA-Z]+)/([0-9.]+).*|", $nav, $reg)) {
177  $action->Register("navversion", $reg[2]);
178  }
179  }
180  }
181 
182  $ISIE6 = false;
183  $ISIE7 = false;
184  $ISIE8 = false;
185  $ISAPPLEWEBKIT = false;
186  $ISSAFARI = false;
187  $ISCHROME = false;
188  if (preg_match('/MSIE ([0-9]+).*/', $nav, $match)) {
189  switch ($match[1]) {
190  case "6":
191  $ISIE6 = true;
192  break;
193 
194  case "7":
195  $ISIE7 = true;
196  break;
197 
198  case "8":
199  $ISIE8 = true;
200  break;
201  }
202  } elseif (preg_match('|\bAppleWebKit/(.*?)\b|', $nav, $match)) {
203  $ISAPPLEWEBKIT = true;
204  if (preg_match('|\bSafari/(.*?)\b|', $nav, $match)) {
205  $ISSAFARI = true;
206  if (preg_match('|\bChrome/(.*?)\b|', $nav, $match)) {
207  $ISCHROME = true;
208  }
209  }
210  }
211 
212  $core->SetVolatileParam("ISIE", ($action->read("navigator") == "EXPLORER"));
213  $core->SetVolatileParam("ISIE6", ($ISIE6 === true));
214  $core->SetVolatileParam("ISIE7", ($ISIE7 === true));
215  $core->SetVolatileParam("ISIE8", ($ISIE8 === true));
216  $core->SetVolatileParam("ISAPPLEWEBKIT", ($ISAPPLEWEBKIT === true));
217  $core->SetVolatileParam("ISSAFARI", ($ISSAFARI === true));
218  $core->SetVolatileParam("ISCHROME", ($ISCHROME === true));
219  // init for gettext
220  setLanguage($action->Getparam("CORE_LANG"));
221 
222  $action->log->debug("gettext init for " . $action->parent->name . $action->Getparam("CORE_LANG"));
223  }
224 }
225 /**
226  * execute action
227  * app and action http param
228  */
229 function executeAction(&$action, &$out = null)
230 {
231 
232  $standalone = GetHttpVars("sole", "Y");
233  if (($standalone == "Y") || ($standalone == "N") || ($standalone == "")) {
234  if ($out !== null) $out = $action->execute();
235  else echo ($action->execute());
236  } else if ($standalone == "R") {
237  $app = GetHttpVars("app", "CORE");
238  $act = GetHttpVars("action", "");
239  // compute others argument to propagate to redirect url
240  global $_GET;
241  $getargs = "";
242  while (list($k, $v) = each($_GET)) {
243  if (($k != "freedom_param") && ($k != "app") && ($k != "sole") && ($k != "action")) $getargs.= "&" . $k . "=" . $v;
244  }
245  redirect($action, "CORE", "MAIN&appd=${app}&actd=${act}" . urlencode($getargs) , $action->GetParam("CORE_STANDURL"));
246  } else if ($standalone == "A") {
247  if ((isset($action->parent)) && ($action->parent->with_frame != "Y")) {
248  // This document is not completed : does not contain header and footer
249  // HTML body result
250  // achieve action
251  $body = ($action->execute());
252  // write HTML header
253  $head = new Layout($action->GetLayoutFile("htmltablehead.xml") , $action);
254  // copy JS ref & code from action to header
255  $head->jsref = $action->parent->GetJsRef();
256  $head->jscode = $action->parent->GetJsCode();
257  $head->set("TITLE", _($action->parent->short_name));
258  if ($out !== null) {
259  $out = $head->gen();
260  $out.= $body;
261  $foot = new Layout($action->GetLayoutFile("htmltablefoot.xml") , $action);
262  $out.= $foot->gen();
263  } else {
264  echo ($head->gen());
265  // write HTML body
266  echo ($body);
267  // write HTML footer
268  $foot = new Layout($action->GetLayoutFile("htmltablefoot.xml") , $action);
269  echo ($foot->gen());
270  }
271  } else {
272  // This document is completed
273  if ($out !== null) $out = $action->execute();
274  else echo ($action->execute());
275  }
276  }
277 }
278 ?>
← centre documentaire © anakeen - published under CC License - Dynacase