Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Appmng/Class.Application.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  * Application Class
9  *
10  * @author Anakeen 2000
11  * @version $Id: Class.Application.php,v 1.64 2008/08/01 09:03:01 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 
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');
30 
31 function f_paramglog($var)
32 { // filter to select only not global
33  return (!((isset($var["global"]) && ($var["global"] == 'Y'))));
34 }
35 
36 class Application extends DbObj
37 {
38  public $fields = array(
39  "id",
40  "name",
41  "short_name",
42  "description",
43  "access_free",
44  "available",
45  "icon",
46  "displayable",
47  "with_frame",
48  "childof",
49  "objectclass",
50  "ssl",
51  "machine",
52  "iorder",
53  "tag"
54  );
55 
56  public $id_fields = array(
57  "id"
58  );
59  public $rootdir = '';
60  public $fulltextfields = array(
61  "name",
62  "short_name",
63  "description"
64  );
65  public $sqlcreate = '
66 create table application ( id int not null,
67  primary key (id),
68  name varchar(20) not null,
69  short_name text,
70  description text ,
71  access_free varchar(20),
72  available char,
73  icon text,
74  displayable char,
75  with_frame char,
76  childof varchar(20),
77  objectclass char,
78  ssl char,
79  machine text,
80  iorder int,
81  tag text);
82 create index application_idx1 on application(id);
83 create index application_idx2 on application(name);
84 create sequence SEQ_ID_APPLICATION start 10;
85 ';
86 
87  public $dbtable = "application";
88 
89  public $def = array(
90  "criteria" => "",
91  "order_by" => "name"
92  );
93 
94  public $criterias = array(
95  "name" => array(
96  "libelle" => "Nom",
97  "type" => "TXT"
98  )
99  );
100 
101  public $parent = "";
102 
103  public $param;
104  public $permission = null; // permission object
105  public $jsref = array();
106  public $jscode = array();
107  public $logmsg = array();
108 
109  public $cssref = array();
110  public $csscode = array();
111 
112  function Set($name, &$parent, $session = "", $autoinit = false)
113  {
114  $this->log->debug("Entering : Set application to $name");
115 
116  $query = new QueryDb($this->dbaccess, "Application");
117  $query->order_by = "";
118  $query->criteria = "name";
119  $query->operator = "=";
120  $query->string = "'" . pg_escape_string($name) . "'";
121  $list = $query->Query(0, 0, "TABLE");
122  if ($query->nb != 0) {
123  $this->affect($list[0]);
124  $this->log->debug("Set application to $name");
125  if (!isset($parent)) {
126  $this->log->debug("Parent not set");
127  }
128  } else {
129  if ($autoinit) {
130  // Init the database with the app file if it exists
131  $this->InitApp($name);
132  if ($parent != "") {
133  $this->parent = & $parent;
134  if ($this->name == "") {
135  printf("Application name %s not found", $name);
136  exit;
137  } else Redirect($this, $this->name, "");
138  } else {
139  global $_SERVER;
140  if ($_SERVER['HTTP_HOST'] != "") Header("Location: " . $_SERVER['HTTP_REFERER']);
141  }
142  } else {
143  header('HTTP/1.0 503 Application unavalaible');
144  printf("Fail to find application %s.", $name);
145  exit;
146  //throw new Exception(sprintf("Fail to find application %s",$name));
147 
148  }
149  }
150 
151  if ($this !== $parent) $this->parent = & $parent;
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;
156  }
157  }
158 
159  if ($session != "") $this->SetSession($session);
160 
161  $this->param = new Param($this->dbaccess);
162  if ($this->session) $sessparam = $this->session->read("sessparam" . $this->id, false);
163  if ($sessparam) {
164  $this->param->appid = $this->id;
165  $this->param->buffer = $sessparam;
166  $this->InitStyle(false);
167  } else {
168  $this->InitStyle();
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);
171  }
172  if (!$this->rootdir) $this->rootdir = $this->Getparam("CORE_PUBDIR");
173  if ($this->available == "N") {
174  // error
175  return sprintf(_("Application %s (%s) not available") , $this->name, _($this->short_name));
176  }
177  $this->permission = null;
178  }
179 
180  function Complete()
181  {
182  }
183 
184  function SetSession(&$session)
185  {
186  $this->session = $session;
187  // Set the user if possible
188  if (is_object($this->session)) {
189  if ($this->session->userid != 0) {
190  $this->log->debug("Get user on " . $this->GetParam("CORE_DB"));
191  $this->user = new User($this->GetParam("CORE_DB") , $this->session->userid);
192  } else {
193  $this->log->debug("User not set ");
194  }
195  }
196  }
197 
198  function PreInsert()
199  {
200  if ($this->Exists($this->name)) return "Ce nom d'application existe deja...";
201  if ($this->name == "CORE") {
202  $this->id = 1;
203  } else {
204  $res = $this->exec_query("select nextval ('seq_id_application')");
205  $arr = $this->fetch_array(0);
206  $this->id = $arr["nextval"];
207  }
208  }
209 
210  function PreUpdate()
211  {
212  if ($this->dbid == - 1) return FALSE;
213  if ($this->Exists($this->name, $this->id)) return "Ce nom d'application existe deja...";
214  }
215 
216  function Exists($app_name, $id_application = '')
217  {
218  $this->log->debug("Exists $app_name ?");
219  $query = new QueryDb($this->dbaccess, "application");
220  $query->order_by = "";
221  $query->criteria = "";
222 
223  if ($id_application != '') {
224  $query->basic_elem->sup_where = array(
225  "name='$app_name'",
226  "id!=$id_application"
227  );
228  } else {
229  $query->criteria = "name";
230  $query->operator = "=";
231  $query->string = "'" . $app_name . "'";
232  }
233 
234  $r = $query->Query(0, 0, "TABLE");
235 
236  return ($query->nb > 0) ? $r[0]["id"] : false;
237  }
238  /**
239  * Strip the pubdir/wpub directory from a file pathname
240  * @param string $pathname the file pathname
241  * @return string file pathname without the root dir
242  */
243  private function stripRootDir($pathname)
244  {
245  if (substr($pathname, 0, strlen($this->rootdir) - 1) == $this->rootdir) {
246  $pathname = substr($location, strlen($this->rootdir) + 1);
247  }
248 
249  return $pathname;
250  }
251  /**
252  * Try to resolve a JS/CSS reference to a supported location
253  * @param string $ref the JS/CSS reference
254  * @return string the resolved location of the reference or an empty string on failure
255  */
256  private function resolveRessourceLocation($ref)
257  {
258  if ($this->rootdir == '') {
259  $this->rootdir = $this->GetParam("CORE_PUBDIR");
260  }
261 
262  if (strstr($ref, '../') !== false) {
263  return '';
264  }
265  /* Resolve through getLayoutFile */
266  $location = $this->GetLayoutFile($ref);
267  if ($location != '') {
268  return $this->stripRootDir($location);
269  }
270  /* Try "APP:file.extension" notation */
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']);
273  if (is_file($location)) {
274  return sprintf('%s/Layout/%s', $m['appname'], $m['filename']);
275  }
276  }
277  /* Try hardcoded locations */
278  foreach (array(
279  $ref,
280  sprintf("%s/Layout/%s", $this->name, $ref)
281  ) as $filename) {
282  if (is_file(sprintf("%s/%s", $this->rootdir, $filename))) {
283  return $filename;
284  }
285  }
286  /* Detect URLs */
287  $pUrl = parse_url($ref);
288  if (isset($pUrl['scheme']) || isset($pUrl['query'])) {
289  return $ref;
290  }
291 
292  return '';
293  }
294  /**
295  * Add a ressource (JS/CSS) to the page
296  * @param string $type 'js' or 'css'
297  * @param string $ref the ressource reference
298  * @param boolean $needparse should the ressource be parsed (default false)
299  */
300  function AddRessourceRef($type, $ref, $needparse)
301  {
302  /* Try to attach the ressource to the parent app */
303  if ($this->hasParent()) {
304  $ret = $this->parent->AddRessourceRef($type, $ref, $needparse);
305  if ($ret !== '') {
306  return $ret;
307  }
308  }
309  /* Try to attach the ressource to the current app */
310  $ressourceLocation = '';
311  if ($needparse) {
312  $ressourceLocation = $this->GetParam("CORE_STANDURL") . "&app=CORE&action=CORE_CSS&session=" . $this->session->id . "&layout=" . $ref . "&type=" . $type;
313  } else {
314  $location = $this->resolveRessourceLocation($ref);
315  if ($location != '') {
316  $ressourceLocation = $location;
317  }
318  }
319 
320  if ($ressourceLocation == '') {
321  /* The ressource could not be resolved */
322  return '';
323  }
324 
325  if ($type == 'js') {
326  $this->jsref[$ref] = $ressourceLocation;
327  } elseif ($type == 'css') {
328  $this->cssref[$ref] = $ressourceLocation;
329  } else {
330  return '';
331  }
332 
333  return $ressourceLocation;
334  }
335 
336  function AddCssRef($ref, $needparse = false)
337  {
338  return $this->AddRessourceRef('css', $ref, $needparse);
339  }
340 
341  function AddJsRef($ref, $needparse = false)
342  {
343  return $this->AddRessourceRef('js', $ref, $needparse);
344  }
345 
346  function AddJsCode($code)
347  {
348  // Js Code are stored in the top level application
349  if ($this->hasParent()) {
350  $this->parent->AddJsCode($code);
351  } else {
352  $this->jscode[] = $code;
353  }
354  }
355 
356  function AddLogMsg($code, $cut = 80)
357  {
358  if ($code == "") return;
359  // Js Code are stored in the top level application
360  if ($this->hasParent()) {
361  $this->parent->AddLogMsg($code, $cut);
362  } else {
363  $logmsg = $this->session->read("logmsg", array());
364  if (is_array($code)) {
365  $code["stack"] = getDebugStack(4);
366  $logmsg[] = json_encode($code);
367  } else $logmsg[] = strftime("%H:%M - ") . str_replace("\n", "\\n", addslashes(mb_substr($code, 0, $cut)));
368  $this->session->register("logmsg", $logmsg);
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);
372  }
373  }
374  function AddWarningMsg($code)
375  {
376  if (($code == "") || ($code == "-")) return;
377  // Js Code are stored in the top level application
378  if ($this->hasParent()) {
379  $this->parent->AddWarningMsg($code);
380  } else {
381  if ($_SERVER['HTTP_HOST'] != "") {
382  $logmsg = $this->session->read("warningmsg", array());
383  $logmsg[] = str_replace("\n", "\\n", addslashes($code));
384  $this->session->register("warningmsg", $logmsg);
385  } else print "$code\n";
386  }
387  }
388  function GetJsRef()
389  {
390  if ($this->hasParent()) {
391  return ($this->parent->GetJsRef());
392  } else {
393  return ($this->jsref);
394  }
395  }
396 
397  function GetJsCode()
398  {
399  if ($this->hasParent()) {
400  return ($this->parent->GetJsCode());
401  } else {
402  return ($this->jscode);
403  }
404  }
405 
406  function GetLogMsg()
407  {
408  return ($this->session->read("logmsg", array()));
409  }
410 
411  function ClearLogMsg()
412  {
413  $this->session->unregister("logmsg");
414  }
415  function GetWarningMsg()
416  {
417  return ($this->session->read("warningmsg", array()));
418  }
419 
420  function ClearWarningMsg()
421  {
422  $this->session->unregister("warningmsg");
423  }
424 
425  function AddCssCode($code)
426  {
427  // Css Code are stored in the top level application
428  if ($this->hasParent()) {
429  $this->parent->AddCssCode($code);
430  } else {
431  $this->csscode[] = $code;
432  }
433  }
434  function GetCssRef()
435  {
436  if ($this->hasParent()) {
437  return ($this->parent->GetCssRef());
438  } else {
439  return ($this->cssref);
440  }
441  }
442 
443  function GetCssCode()
444  {
445  if ($this->hasParent()) {
446  return ($this->parent->GetCssCode());
447  } else {
448  return ($this->csscode);
449  }
450  }
451  /**
452  * Test permission for currennt user in current application
453  *
454  * @param string $acl_name acl name to test
455  * @param string $app_name application if test for other application
456  * @return bool true if permission granted
457  */
458  function HasPermission($acl_name, $app_name = "")
459  {
460  if (!isset($this->user) || !is_object($this->user)) {
461  $this->log->warning("Action {$this->parent->name}:{$this->name} requires authentification");
462  return FALSE;
463  }
464  if ($this->user->id == 1) return true; // admin can do everything
465  if ($app_name == "") {
466 
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");
470  return FALSE;
471  }
472  if (!$this->permission) {
473  $permission = new Permission($this->dbaccess, array(
474  $this->user->id,
475  $this->id
476  ));
477  if (!$permission->IsAffected()) { // case of no permission available
478  $permission->Affect(array(
479  "id_user" => $this->user->id,
480  "id_application" => $this->id
481  ));
482  }
483  $this->permission = & $permission;
484  }
485 
486  return ($this->permission->HasPrivilege($acl->id));
487  } else {
488  // test permission for other application
489  if (!is_numeric($app_name)) $appid = $this->GetIdFromName($app_name);
490  else $appid = $app_name;
491 
492  $wperm = new Permission($this->dbaccess, array(
493  $this->user->id,
494  $appid
495  ));
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");
500  return false;
501  } else {
502  return ($wperm->HasPrivilege($acl->id));
503  }
504  }
505  }
506  return false;
507  }
508 
509  function InitStyle($init = true)
510  {
511  if ($init == true) {
512  if (isset($this->user)) $pstyle = new Param($this->dbaccess, array(
513  "STYLE",
514  PARAM_USER . $this->user->id,
515  "1"
516  ));
517  else $pstyle = new Param($this->dbaccess, array(
518  "STYLE",
520  "1"
521  ));
522  if (!$pstyle->isAffected()) $pstyle = new Param($this->dbaccess, array(
523  "STYLE",
524  PARAM_APP,
525  "1"
526  ));
527 
528  $style = $pstyle->val;
529  $this->style = new Style($this->dbaccess, $style);
530 
531  $this->style->Set($this);
532  } else {
533  $style = $this->getParam("STYLE");
534  $this->style = new Style($this->dbaccess, $style);
535 
536  $this->style->Set($this);
537  }
538  if ("Y" == $this->style->parsable) {
539  $this->AddCssRef("$style:gen.css", true);
540  } else {
541  $this->AddCssRef("STYLE/$style/Layout/gen.css");
542  }
543  $size = $this->getParam("FONTSIZE", "normal");
544  $this->AddCssRef("WHAT/Layout/size-$size.css");
545  }
546 
547  function SetLayoutVars($lay)
548  {
549  if ($this->hasParent()) {
550  $this->parent->SetLayoutVars($lay);
551  }
552  }
553 
554  function GetRootApp()
555  {
556  if ($this->parent == "") {
557  return ($this);
558  } else {
559  return ($this->parent->GetRootApp());
560  }
561  }
562 
563  function GetImageFile($img)
564  {
565 
566  return $this->rootdir . "/" . $this->GetImageUrl($img);
567  }
568 
569  var $noimage = "CORE/Images/noimage.png";
570  function GetImageUrl($img, $detectstyle = true, $size = null)
571  {
572 
573  if ($img != "") {
574  // try style first
575  if ($detectstyle) {
576  $url = $this->style->GetImageUrl($img, "");
577  if ($url != "") {
578  if ($size !== null) return 'resizeimg.php?img=' . $url . '&size=' . $size;
579  return $url;
580  }
581  }
582  // try application
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;
586  return $url;
587  } else { // perhaps generic application
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;
591  return $url;
592  } else if (file_exists($this->rootdir . "/Images/" . $img)) {
593  $url = "Images/" . $img;
594  if ($size !== null) return 'resizeimg.php?img=' . $url . '&size=' . $size;
595  return $url;
596  }
597  }
598  // try in parent
599  if ($this->parent != "") {
600  $url = $this->parent->getImageUrl($img);
601  if ($size !== null) return 'resizeimg.php?img=' . $url . '&size=' . $size;
602  return $url;
603  }
604  }
605  return $this->noimage;
606  }
607 
608  function ImageFilterColor($image, $fcol, $newcol, $out = null)
609  {
610  if ($out === null) {
611  $out = getTmpDir() . "/i.gif";
612  }
613  $im = imagecreatefromgif($image);
614  $idx = imagecolorexact($im, $fcol[0], $fcol[1], $fcol[2]);
615  imagecolorset($im, $idx, $newcol[0], $newcol[1], $newcol[2]);
616  imagegif($im, $out);
617  imagedestroy($im);
618  }
619 
620  function GetFilteredImageUrl($imgf)
621  {
622 
623  $ttf = explode(":", $imgf);
624  $img = $ttf[0];
625  $filter = $ttf[1];
626 
627  $url = $this->GetImageUrl($img);
628  if ($url == $this->noimage) return $url;
629 
630  $tf = explode("|", $filter);
631  if (count($tf) != 2) return $url;
632 
633  $fcol = explode(",", $tf[0]);
634  if (count($fcol) != 3) return $url;
635 
636  if (substr($tf[1], 0, 1) == '#') $col = $tf[1];
637  else $col = $this->getParam($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));
641 
642  $cdir = 'img-cache/';
643  $rcdir = $this->rootdir . '/' . $cdir;
644  if (!is_dir($rcdir)) mkdir($rcdir);
645 
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;
649 
650  $this->ImageFilterColor($this->rootdir . '/' . $url, $fcol, $ncol, $cimg);
651  return $uimg;
652  }
653 
654  function GetLayoutFile($layname)
655  {
656  if (strstr($layname, '..')) {
657  return ""; // not authorized
658 
659  }
660  $file = $this->style->GetLayoutFile($layname, "");
661  if ($file != "") return $file;
662 
663  $laydir = $this->rootdir . "/" . $this->name . "/Layout/";
664  $file = $laydir . $layname; // default file
665  if (file_exists($file)) {
666  return ($file);
667  } else {
668  // perhaps generic application
669  $file = $this->rootdir . "/" . $this->childof . "/Layout/$layname";
670  if (file_exists($file)) return ($file);
671  }
672  if ($this->parent != "") return ($this->parent->GetLayoutFile($layname));
673  return ("");
674  }
675  function OldGetLayoutFile($layname)
676  {
677 
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);
682  return ($file);
683  }
684  if ($this->parent != "") return ($this->parent->GetLayoutFile($layname));
685  return ("");
686  }
687 
688  function SetParam($key, $val)
689  {
690  if (is_array($val)) {
691  if (isset($val["global"]) && $val["global"] == "Y") $type = PARAM_GLB;
692  else $type = PARAM_APP;
693  $this->param->Set($key, $val["val"], $type, $this->id);
694  } else { // old method
695  $this->param->Set($key, $val, PARAM_APP, $this->id);
696  }
697  }
698  /**
699  * set user parameter for current user
700  *
701  * @param string $key parameter identificator
702  * @param string $val value
703  * @return void
704  */
705  function SetParamU($key, $val)
706  {
707  $this->param->Set($key, $val, PARAM_USER . $this->user->id, $this->id);
708  }
709  function SetParamDef($key, $val)
710  {
711  // add new param definition
712  $pdef = new ParamDef($this->dbaccess, $key);
713  if (!$pdef->isAffected()) {
714  $pdef->name = $key;
715  $pdef->isuser = "N";
716  $pdef->isstyle = "N";
717  $pdef->isglob = "N";
718  $pdef->appid = $this->id;
719  $pdef->descr = "";
720  $pdef->kind = "text";
721  }
722 
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";
729  }
730 
731  if ($pdef->isAffected()) $pdef->Modify();
732  else $pdef->Add();
733  }
734  function SetVolatileParam($key, $val)
735  {
736  $this->param->SetVolatile($key, $val);
737  }
738 
739  function GetParam($key, $default = "")
740  {
741  if (!isset($this->param)) return ($default);
742  $z = $this->param->Get($key, "z");
743  if ($z == "z") {
744  if ($this->hasParent()) return $this->parent->GetParam($key, $default);
745  } else {
746  return ($z);
747  }
748 
749  return ($default);
750  }
751 
752  function InitAllParam($tparam, $update = false)
753  {
754  if (is_array($tparam)) {
755  reset($tparam);
756  while (list($k, $v) = each($tparam)) {
757  $this->SetParamDef($k, $v); // update definition
758  if ($update) {
759  // don't modify old parameters
760  if ($this->param->Get($k) == "") $this->SetParam($k, $v); // set only new parameters or static variable like VERSION
761 
762  } else {
763  $this->SetParam($k, $v);
764  }
765  }
766  }
767  }
768 
769  function GetAllParam()
770  {
771  $list = $this->param->buffer;
772  if ($this->hasParent()) {
773  $list2 = $this->parent->GetAllParam();
774  $list = array_merge($this->param->buffer, $list2);
775  }
776 
777  return ($list);
778  }
779 
780  function InitApp($name, $update = FALSE)
781  {
782 
783  $this->log->info("Init : $name");
784  if (file_exists($this->GetParam("CORE_PUBDIR", DEFAULT_PUBDIR) . "/{$name}/{$name}.app")) {
785  global $app_desc, $app_acl, $action_desc;
786  // init global array
787  $app_acl = array();
788  $app_desc = array();
789  $action_desc = array();
790  include ("{$name}/{$name}.app");
791  $action_desc_ini = $action_desc;
792  if (sizeof($app_desc) > 0) {
793  if (!$update) {
794  $this->log->debug("InitApp : new application ");
795  }
796  if ($update) {
797  foreach ($app_desc as $k => $v) {
798  switch ($k) {
799  case 'displayable':
800  case 'available':
801  break;
802 
803  default:
804  $this->$k = $v;
805  }
806  }
807  $this->Modify();
808  } else {
809  $this->available = "Y";
810  foreach ($app_desc as $k => $v) {
811  $this->$k = $v;
812  }
813  $this->Add();
814  $this->param = new Param();
815  $this->param->SetKey($this->id, isset($this->user->id) ? $this->user->id : ANONYMOUS_ID);
816  }
817  } else {
818  $this->log->info("can't init $name");
819  return false;
820  }
821 
822  $action_desc = $action_desc_ini;
823  // init acl
824  $acl = new Acl($this->dbaccess);
825  $acl->Init($this, $app_acl, $update);
826  // init father if has
827  if ($this->childof != "") {
828  // init ACL & ACTION
829  $app_acl = array();
830  $action_desc = array();
831  include ("{$this->childof}/{$this->childof}.app");
832  // init acl
833  $acl = new Acl($this->dbaccess);
834  $acl->Init($this, $app_acl, $update);
835  // init actions
836  $action = new Action($this->dbaccess);
837  $action->Init($this, $action_desc, false);
838  }
839  // init actions
840  $action = new Action($this->dbaccess);
841 
842  $action->Init($this, array_merge($action_desc, $action_desc_ini) , $update);
843  //----------------------------------
844  // init application constant
845  if (file_exists(GetParam("CORE_PUBDIR", DEFAULT_PUBDIR) . "/{$name}/{$name}_init.php")) {
846 
847  include ("{$name}/{$name}_init.php");
848  if ($this->param) {
849  // delete paramters that cannot be change after initialisation to be change now
850  if ($update) $this->param->DelStatic($this->id);
851  global $app_const;
852  if (isset($app_const)) $this->InitAllParam($app_const, $update);
853  }
854  }
855  //----------------------------------
856  // add init father application constant
857  if (file_exists(GetParam("CORE_PUBDIR", DEFAULT_PUBDIR) . "/{$this->childof}/{$this->childof}_init.php")) {
858  include ("{$this->childof}/{$this->childof}_init.php");
859  global $app_const;
860  $this->InitAllParam(array_filter($app_const, "f_paramglog") , true);
861  }
862 
863  if ($this->id > 1) {
864  $this->SetParamDef("APPNAME", array(
865  "val" => $name,
866  "kind" => "static"
867  )); // use by generic application
868  $this->SetParam("APPNAME", array(
869  "val" => $name,
870  "kind" => "static"
871  )); // use by generic application
872 
873  }
874  } else {
875  $this->log->info("No {$name}/{$name}.app available");
876  return false;
877  }
878  }
879 
880  function UpdateApp()
881  {
882  $name = $this->name;
883  $this->InitApp($name, TRUE);
884  }
885  // Update All available application
886  function UpdateAllApp()
887  {
888 
889  $query = new QueryDb($this->dbaccess, $this->dbtable);
890  $query->AddQuery("available = 'Y'");
891  $allapp = $query->Query();
892 
893  while (list($k, $app) = each($allapp)) {
894  $application = new Application($this->dbaccess, $app->id);
895 
896  $application->Set($app->name, $this->parent);
897  $application->UpdateApp();
898  }
899  }
900  function DeleteApp()
901  {
902  // delete acl
903  $acl = new Acl($this->dbaccess);
904  $acl->DelAppAcl($this->id);
905  // delete actions
906  $this->log->debug("Delete {$this->name}");
907  $query = new QueryDb("", "Action");
908  $query->basic_elem->sup_where = array(
909  "id_application = {$this->id}"
910  );
911  $list = $query->Query();
912 
913  if ($query->nb > 0) {
914  reset($list);
915  while (list($k, $v) = each($list)) {
916  $this->log->debug(" Delete action {$v->name} ");
917  $v->Delete();
918  }
919  }
920  unset($query);
921 
922  unset($list);
923  // delete params
924  $param = new Param($this->dbaccess);
925  $param->DelAll($this->id);
926  // delete application
927  $this->Delete();
928  }
929 
930  function Text($code, $args = NULL)
931  {
932  if ($code == "") return "";
933  return _("$code");
934  }
935  // Write default ACL when new user is created
936  function UpdateUserAcl($iduser)
937  {
938 
939  $query = new QueryDb($this->dbaccess, $this->dbtable);
940  $query->AddQuery("available = 'Y'");
941  $allapp = $query->Query();
942  $acl = new Acl($this->dbaccess);
943 
944  while (list($k, $v) = each($allapp)) {
945  $permission = new Permission($this->dbaccess);
946  $permission->id_user = $iduser;
947  $permission->id_application = $v->id;
948 
949  $privileges = $acl->getDefaultAcls($v->id);
950 
951  while (list($k2, $aclid) = each($privileges)) {
952  $permission->id_acl = $aclid;
953  if (($permission->id_acl > 0) && (!$permission->Exists($permission->id_user, $v->id))) {
954  $permission->Add();
955  }
956  }
957  }
958  }
959  /**
960  * return id from name for an application
961  * @param string $name
962  * @return int (0 if not found)
963  */
965  {
966  $query = new QueryDb($this->dbaccess, $this->dbtable);
967  $query->AddQuery("name = '" . pg_escape_string(trim($name)) . "'");
968  $app = $query->Query(0, 0, "TABLE");
969  if (is_array($app)) return $app[0]["id"];
970  return 0;
971  }
972 
973  function hasParent()
974  {
975  return (is_object($this->parent) && ($this->parent !== $this));
976  }
977 }
978 ?>
← centre documentaire © anakeen - published under CC License - Dynacase