19 include_once (
'Class.DbObj.php');
20 include_once (
'Class.QueryDb.php');
21 include_once (
'Class.Action.php');
22 include_once (
'Class.Layout.php');
23 include_once (
'Class.Param.php');
24 include_once (
'Class.User.php');
25 include_once (
'Class.Permission.php');
26 include_once (
'Class.Style.php');
27 include_once (
'Class.ParamDef.php');
28 include_once (
'Lib.Http.php');
29 include_once (
'Lib.Common.php');
33 return (!((isset($var[
"global"]) && ($var[
"global"] ==
'Y'))));
66 create table application ( id int not null,
68 name varchar(20) not null,
71 access_free varchar(20),
82 create index application_idx1 on application(id);
83 create index application_idx2 on application(name);
84 create sequence SEQ_ID_APPLICATION start 10;
114 $this->log->debug(
"Entering : Set application to $name");
118 $query->criteria =
"name";
120 $query->string =
"'" . pg_escape_string(
$name) .
"'";
124 $this->log->debug(
"Set application to $name");
126 $this->log->debug(
"Parent not set");
134 if ($this->name ==
"") {
135 printf(
"Application name %s not found",
$name);
137 }
else Redirect($this, $this->name,
"");
140 if ($_SERVER[
'HTTP_HOST'] !=
"") Header(
"Location: " . $_SERVER[
'HTTP_REFERER']);
143 header(
'HTTP/1.0 503 Application unavalaible');
144 printf(
"Fail to find application %s.",
$name);
152 if (is_object($this->parent) && isset($this->parent->session)) {
153 $this->
session = $this->parent->session;
154 if (isset($this->parent->user) && is_object($this->parent->user)) {
155 $this->
user = $this->parent->user;
159 if ($session !=
"") $this->
SetSession($session);
161 $this->param =
new Param($this->dbaccess);
162 if ($this->
session) $sessparam = $this->
session->read(
"sessparam" . $this->
id,
false);
165 $this->param->buffer = $sessparam;
169 $this->param->SetKey($this->
id, isset($this->
user->id) ? $this->user->id :
false, $this->style->name);
170 if ($this->
session) $this->
session->register(
"sessparam" . $this->
id, $this->param->buffer);
172 if (!$this->rootdir) $this->rootdir = $this->Getparam(
"CORE_PUBDIR");
173 if ($this->available ==
"N") {
175 return sprintf(_(
"Application %s (%s) not available") , $this->name, _($this->short_name));
177 $this->permission = null;
188 if (is_object($this->
session)) {
189 if ($this->
session->userid != 0) {
190 $this->log->debug(
"Get user on " . $this->
GetParam(
"CORE_DB"));
193 $this->log->debug(
"User not set ");
200 if ($this->
Exists($this->name))
return "Ce nom d'application existe deja...";
201 if ($this->name ==
"CORE") {
204 $res = $this->
exec_query(
"select nextval ('seq_id_application')");
206 $this->
id = $arr[
"nextval"];
212 if ($this->dbid == - 1)
return FALSE;
213 if ($this->
Exists($this->name, $this->
id))
return "Ce nom d'application existe deja...";
216 function Exists($app_name, $id_application =
'')
218 $this->log->debug(
"Exists $app_name ?");
223 if ($id_application !=
'') {
224 $query->basic_elem->sup_where = array(
226 "id!=$id_application"
229 $query->criteria =
"name";
231 $query->string =
"'" . $app_name .
"'";
236 return (
$query->nb > 0) ?
$r[0][
"id"] :
false;
243 private function stripRootDir($pathname)
245 if (substr($pathname, 0, strlen($this->rootdir) - 1) == $this->rootdir) {
246 $pathname = substr(
$location, strlen($this->rootdir) + 1);
256 private function resolveRessourceLocation($ref)
258 if ($this->rootdir ==
'') {
259 $this->rootdir = $this->
GetParam(
"CORE_PUBDIR");
262 if (strstr($ref,
'../') !==
false) {
271 if (preg_match(
'/^(?P<appname>[a-z][a-z0-9_-]*):(?P<filename>.*)$/i', $ref, $m)) {
272 $location = sprintf(
'%s/%s/Layout/%s', $this->rootdir, $m[
'appname'], $m[
'filename']);
274 return sprintf(
'%s/Layout/%s', $m[
'appname'], $m[
'filename']);
280 sprintf(
"%s/Layout/%s", $this->name, $ref)
282 if (is_file(sprintf(
"%s/%s", $this->rootdir, $filename))) {
287 $pUrl = parse_url($ref);
288 if (isset($pUrl[
'scheme']) || isset($pUrl[
'query'])) {
304 $ret = $this->parent->AddRessourceRef(
$type, $ref, $needparse);
310 $ressourceLocation =
'';
312 $ressourceLocation = $this->
GetParam(
"CORE_STANDURL") .
"&app=CORE&action=CORE_CSS&session=" . $this->
session->id .
"&layout=" . $ref .
"&type=" .
$type;
314 $location = $this->resolveRessourceLocation($ref);
320 if ($ressourceLocation ==
'') {
326 $this->jsref[$ref] = $ressourceLocation;
327 } elseif (
$type ==
'css') {
328 $this->cssref[$ref] = $ressourceLocation;
333 return $ressourceLocation;
350 $this->parent->AddJsCode($code);
352 $this->jscode[] = $code;
358 if ($code ==
"")
return;
361 $this->parent->AddLogMsg($code, $cut);
364 if (is_array($code)) {
366 $logmsg[] = json_encode($code);
367 }
else $logmsg[] = strftime(
"%H:%M - ") . str_replace(
"\n",
"\\n", addslashes(mb_substr($code, 0, $cut)));
369 $suser = sprintf(
"%s %s [%d] - ", $this->
user->firstname, $this->user->lastname, $this->user->id);
370 if (is_array($code)) $code = print_r($code,
true);
371 $this->log->info($suser . $code);
376 if (($code ==
"") || ($code ==
"-"))
return;
379 $this->parent->AddWarningMsg($code);
383 $logmsg[] = str_replace(
"\n",
"\\n", addslashes($code));
385 }
else print
"$code\n";
391 return ($this->parent->GetJsRef());
393 return ($this->jsref);
400 return ($this->parent->GetJsCode());
402 return ($this->jscode);
408 return ($this->
session->read(
"logmsg", array()));
413 $this->
session->unregister(
"logmsg");
417 return ($this->
session->read(
"warningmsg", array()));
422 $this->
session->unregister(
"warningmsg");
429 $this->parent->AddCssCode($code);
431 $this->csscode[] = $code;
437 return ($this->parent->GetCssRef());
439 return ($this->cssref);
446 return ($this->parent->GetCssCode());
448 return ($this->csscode);
460 if (!isset($this->
user) || !is_object($this->
user)) {
461 $this->log->warning(
"Action {$this->parent->name}:{$this->name} requires authentification");
464 if ($this->
user->id == 1)
return true;
465 if ($app_name ==
"") {
467 $acl =
new Acl($this->dbaccess);
468 if (!$acl->Set($acl_name, $this->id)) {
469 $this->log->warning(
"Acl $acl_name not available for App $this->name");
472 if (!$this->permission) {
479 "id_user" => $this->
user->id,
480 "id_application" => $this->id
486 return ($this->permission->HasPrivilege($acl->id));
489 if (!is_numeric($app_name)) $appid = $this->
GetIdFromName($app_name);
490 else $appid = $app_name;
492 $wperm =
new Permission($this->dbaccess, array(
496 if ($wperm->isAffected()) {
497 $acl =
new Acl($this->dbaccess);
498 if (!$acl->Set($acl_name, $appid)) {
499 $this->log->warning(
"Acl $acl_name not available for App $this->name");
502 return ($wperm->HasPrivilege($acl->id));
512 if (isset($this->
user)) $pstyle =
new Param($this->dbaccess, array(
517 else $pstyle =
new Param($this->dbaccess, array(
522 if (!$pstyle->isAffected()) $pstyle =
new Param($this->dbaccess, array(
531 $this->style->Set($this);
536 $this->style->Set($this);
538 if (
"Y" == $this->style->parsable) {
539 $this->
AddCssRef(
"$style:gen.css",
true);
541 $this->
AddCssRef(
"STYLE/$style/Layout/gen.css");
544 $this->
AddCssRef(
"WHAT/Layout/size-$size.css");
550 $this->parent->SetLayoutVars($lay);
556 if ($this->parent ==
"") {
559 return ($this->parent->GetRootApp());
576 $url = $this->style->GetImageUrl(
$img,
"");
578 if (
$size !== null)
return 'resizeimg.php?img=' . $url .
'&size=' .
$size;
583 if (file_exists($this->rootdir .
"/" . $this->name .
"/Images/" .
$img)) {
584 $url = $this->name .
"/Images/" .
$img;
585 if (
$size !== null)
return 'resizeimg.php?img=' . $url .
'&size=' .
$size;
588 if (($this->childof !=
"") && (file_exists($this->rootdir .
"/" . $this->childof .
"/Images/" . $img))) {
589 $url = $this->childof .
"/Images/" .
$img;
590 if (
$size !== null)
return 'resizeimg.php?img=' . $url .
'&size=' .
$size;
592 }
else if (file_exists($this->rootdir .
"/Images/" . $img)) {
593 $url =
"Images/" .
$img;
594 if (
$size !== null)
return 'resizeimg.php?img=' . $url .
'&size=' .
$size;
599 if ($this->parent !=
"") {
600 $url = $this->parent->getImageUrl($img);
601 if (
$size !== null)
return 'resizeimg.php?img=' . $url .
'&size=' .
$size;
613 $im = imagecreatefromgif($image);
614 $idx = imagecolorexact($im, $fcol[0], $fcol[1], $fcol[2]);
615 imagecolorset($im, $idx, $newcol[0], $newcol[1], $newcol[2]);
623 $ttf = explode(
":", $imgf);
628 if ($url == $this->noimage)
return $url;
631 if (count(
$tf) != 2)
return $url;
633 $fcol = explode(
",",
$tf[0]);
634 if (count($fcol) != 3)
return $url;
636 if (substr(
$tf[1], 0, 1) ==
'#') $col =
$tf[1];
638 $ncol[0] = hexdec(substr($col, 1, 2));
639 $ncol[1] = hexdec(substr($col, 3, 2));
640 $ncol[2] = hexdec(substr($col, 5, 2));
642 $cdir =
'img-cache/';
643 $rcdir = $this->rootdir .
'/' . $cdir;
644 if (!is_dir($rcdir)) mkdir($rcdir);
646 $uimg = $cdir . $this->name .
'-' . $fcol[0] .
'.' . $fcol[1] .
'.' . $fcol[2] .
'_' .
$ncol[0] .
'.' .
$ncol[1] .
'.' .
$ncol[2] .
'.' .
$img;
647 $cimg = $this->rootdir .
'/' . $uimg;
648 if (file_exists($cimg))
return $uimg;
656 if (strstr($layname,
'..')) {
660 $file = $this->style->GetLayoutFile($layname,
"");
663 $laydir = $this->rootdir .
"/" . $this->name .
"/Layout/";
664 $file = $laydir . $layname;
665 if (file_exists(
$file)) {
669 $file = $this->rootdir .
"/" . $this->childof .
"/Layout/$layname";
672 if ($this->parent !=
"")
return ($this->parent->GetLayoutFile($layname));
678 $root = $this->Getparam(
"CORE_PUBDIR");
679 $file = $root .
"/" . $this->name .
"/Layout/" . $layname;
680 if (file_exists(
$file)) {
681 $file = $this->style->GetLayoutFile($layname,
$file);
684 if ($this->parent !=
"")
return ($this->parent->GetLayoutFile($layname));
690 if (is_array($val)) {
691 if (isset($val[
"global"]) && $val[
"global"] ==
"Y")
$type =
PARAM_GLB;
693 $this->param->Set($key, $val[
"val"],
$type, $this->
id);
695 $this->param->Set($key, $val,
PARAM_APP, $this->
id);
707 $this->param->Set($key, $val,
PARAM_USER . $this->
user->id, $this->id);
712 $pdef =
new ParamDef($this->dbaccess, $key);
713 if (!$pdef->isAffected()) {
716 $pdef->isstyle =
"N";
720 $pdef->kind =
"text";
723 if (is_array($val)) {
724 if (isset($val[
"kind"])) $pdef->kind = $val[
"kind"];
725 if (isset($val[
"user"]) && $val[
"user"] ==
"Y") $pdef->isuser =
"Y";
726 if (isset($val[
"style"]) && $val[
"style"] ==
"Y") $pdef->isstyle =
"Y";
727 if (isset($val[
"descr"])) $pdef->descr = $val[
"descr"];
728 if (isset($val[
"global"]) && $val[
"global"] ==
"Y") $pdef->isglob =
"Y";
731 if ($pdef->isAffected()) $pdef->Modify();
736 $this->param->SetVolatile($key, $val);
741 if (!isset($this->param))
return ($default);
742 $z = $this->param->Get($key,
"z");
744 if ($this->
hasParent())
return $this->parent->GetParam($key, $default);
754 if (is_array($tparam)) {
756 while (list($k, $v) = each($tparam)) {
760 if ($this->param->Get($k) ==
"") $this->
SetParam($k, $v);
771 $list = $this->param->buffer;
773 $list2 = $this->parent->GetAllParam();
774 $list = array_merge($this->param->buffer, $list2);
783 $this->log->info(
"Init : $name");
785 global $app_desc, $app_acl, $action_desc;
789 $action_desc = array();
790 include (
"{$name}/{$name}.app");
791 $action_desc_ini = $action_desc;
792 if (
sizeof($app_desc) > 0) {
794 $this->log->debug(
"InitApp : new application ");
797 foreach ($app_desc as $k => $v) {
809 $this->available =
"Y";
810 foreach ($app_desc as $k => $v) {
814 $this->param =
new Param();
815 $this->param->SetKey($this->
id, isset($this->
user->id) ? $this->user->id :
ANONYMOUS_ID);
818 $this->log->info(
"can't init $name");
822 $action_desc = $action_desc_ini;
824 $acl =
new Acl($this->dbaccess);
825 $acl->Init($this, $app_acl, $update);
827 if ($this->childof !=
"") {
830 $action_desc = array();
831 include (
"{$this->childof}/{$this->childof}.app");
833 $acl =
new Acl($this->dbaccess);
834 $acl->Init($this, $app_acl, $update);
837 $action->Init($this, $action_desc,
false);
842 $action->Init($this, array_merge($action_desc, $action_desc_ini) , $update);
847 include (
"{$name}/{$name}_init.php");
850 if ($update) $this->param->DelStatic($this->
id);
852 if (isset($app_const)) $this->
InitAllParam($app_const, $update);
857 if (file_exists(
GetParam(
"CORE_PUBDIR",
DEFAULT_PUBDIR) .
"/{$this->childof}/{$this->childof}_init.php")) {
858 include (
"{$this->childof}/{$this->childof}_init.php");
860 $this->
InitAllParam(array_filter($app_const,
"f_paramglog") ,
true);
875 $this->log->info(
"No {$name}/{$name}.app available");
890 $query->AddQuery(
"available = 'Y'");
891 $allapp =
$query->Query();
893 while (list($k,
$app) = each($allapp)) {
896 $application->Set(
$app->name, $this->parent);
897 $application->UpdateApp();
903 $acl =
new Acl($this->dbaccess);
904 $acl->DelAppAcl($this->
id);
906 $this->log->debug(
"Delete {$this->name}");
908 $query->basic_elem->sup_where = array(
909 "id_application = {$this->id}"
915 while (list($k, $v) = each(
$list)) {
916 $this->log->debug(
" Delete action {$v->name} ");
925 $param->DelAll($this->
id);
930 function Text($code, $args = NULL)
932 if ($code ==
"")
return "";
940 $query->AddQuery(
"available = 'Y'");
941 $allapp =
$query->Query();
942 $acl =
new Acl($this->dbaccess);
944 while (list($k, $v) = each($allapp)) {
949 $privileges = $acl->getDefaultAcls($v->id);
951 while (list($k2, $aclid) = each($privileges)) {
967 $query->AddQuery(
"name = '" . pg_escape_string(trim(
$name)) .
"'");
969 if (is_array(
$app))
return $app[0][
"id"];
975 return (is_object($this->parent) && ($this->parent !== $this));