Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
freedom_util.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  * Function Utilities for freedom
9  *
10  * @author Anakeen 2000
11  * @version $Id: freedom_util.php,v 1.119 2009/01/20 14:30:39 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage
15  */
16 /**
17  */
18 
19 include_once ("FDL/Lib.Util.php");
20 //
21 // ------------------------------------------------------
22 // construction of a sql disjonction
23 // ------------------------------------------------------
24 function GetSqlCond2($Table, $column)
25 // ------------------------------------------------------
26 
27 {
28  $sql_cond = "";
29  if (count($Table) > 0) {
30  $sql_cond = "(($column = '$Table[0]') ";
31  for ($i = 1; $i < count($Table); $i++) {
32  $sql_cond = $sql_cond . "OR ($column = '$Table[$i]') ";
33  }
34  $sql_cond = $sql_cond . ")";
35  }
36 
37  return $sql_cond;
38 }
39 
40 function GetSqlCond($Table, $column, $integer = false)
41 // ------------------------------------------------------
42 
43 {
44  $sql_cond = "";
45  if (count($Table) > 0) {
46  if ($integer) { // for integer type
47  $sql_cond = "$column in (";
48  $sql_cond.= implode(",", $Table);
49  $sql_cond.= ")";
50  } else { // for text type
51  foreach ($Table as & $v) $v = pg_escape_string($v);
52  $sql_cond = "$column in ('";
53  $sql_cond.= implode("','", $Table);
54  $sql_cond.= "')";
55  }
56  }
57 
58  return $sql_cond;
59 }
60 /**
61  * return first element of array
62  * @param array $a
63  * @return string the first, false is empty
64  */
65 function first($a)
66 {
67  if (count($a) == 0) return false;
68  reset($a);
69  return current($a);
70 }
71 
72 function notEmpty($a)
73 {
74  return (!empty($a));
75 }
76 /**
77  * function use by Doc::getOOoValue()
78  * use to convert html to xhtml
79  * @param string $lt the < character
80  * @param string $tag the tag name
81  * @param string $attr all attributes of tag
82  * @param string $gt the > tag
83  * @return string the new tag
84  */
85 function toxhtmltag($lt, $tag, $attr, $gt)
86 {
87  // print "\ntoxhtmltag($tag,$attr)\n ";
88  if ($tag == "font") return '';
89  elseif (strpos($tag, ':') > 0) {
90  return strtolower($lt . 'xhtml:span' . $gt);
91  } else {
92  $attr = str_replace(':=', '=', $attr);
93  return strtolower($lt . "xhtml:" . $tag . $attr . $gt);
94  }
95 }
96 /**
97  * function use by Doc::getOOoValue()
98  * use to trap XML parsing error : raise exception
99  * @param int $errno error number
100  * @param string $errstr error message
101  * @param string $errfile
102  * @param string $errline error line
103  * @return bool
104  */
105 function HandleXmlError($errno, $errstr, $errfile, $errline)
106 {
107  if ($errno == E_WARNING && (substr_count($errstr, "DOMDocument::loadXML()") > 0)) {
108  throw new DOMException($errstr);
109  } else return false;
110 }
111 /**
112  * optimize for speed : memorize object for future use
113  * @global array $_GLOBALS["gdocs"]
114  * @name $gdocs
115  */
116 /**
117  * return document object in type concordance
118  * @param string $dbaccess database specification
119  * @param int $id identificator of the object
120  * @param bool $latest if true set to latest revision of doc
121  * @global array optimize for speed
122  *
123  * @return Doc object
124  */
125 function new_Doc($dbaccess, $id = '', $latest = false)
126 {
127 
128  global $gdocs; // optimize for speed
129  if ($dbaccess == "") {
130  // don't test if file exist or must be searched in include_path
132  }
133  // print("doctype:".$res["doctype"]);
134  $classname = "";
135  if (($id == '')) {
136  include_once ("FDL/Class.DocFile.php");
137  $doc = new DocFile($dbaccess);
138 
139  return ($doc);
140  }
141  $fromid = "";
142  $gen = ""; // path GEN or not
143  if (!is_numeric($id)) $id = getIdFromName($dbaccess, $id);
144  elseif ($latest) {
145  $lid = getLatestDocId($dbaccess, $id);
146  if ($lid > 0) {
147  $id = $lid;
148  $latest = false;
149  }
150  }
151  $id = intval($id);
152  if ($id > 0) {
153  if (isset($gdocs[$id]) && ((!$latest) || ($gdocs[$id]->locked != - 1))) {
154  $doc = $gdocs[$id];
155  if (($doc->doctype != 'W') || (!isset($doc->doc))) {
156  $doc = $gdocs[$id]; // optimize for speed
157  // if ($doc->id != $id) print_r2("<b>Error $id /".$doc->id."</b>");
158  if ($doc->id == $id) {
159  $doc->cached = 1;
160  return $doc;
161  } else unset($gdocs[$id]);
162  }
163  }
164 
165  $fromid = getFromId($dbaccess, $id);
166  if ($fromid > 0) {
167  $classname = "Doc$fromid";
168  $gen = getGen($dbaccess);
169  } else if ($fromid == - 1) $classname = "DocFam";
170  }
171 
172  if ($classname != "") {
173  include_once ("FDL$gen/Class.$classname.php");
174  $doc = new $classname($dbaccess, $id);
175  if ($latest && $doc->locked == - 1) {
176  $tl = getLatestTDoc($dbaccess, $doc->initid);
177  $doc->Affect($tl);
178  $id = $doc->id;
179  }
180 
181  if (($id > 0) && (count($gdocs) < MAXGDOCS)) {
182  if (($doc->doctype != 'C') || (count($doc->attributes->attr) > 0)) {
183  $doc->iscached = 1;
184  $gdocs[$id] = & $doc;
185  }
186  //print_r2("<b>use cache $id /".$doc->id."</b>");
187 
188  }
189  return ($doc);
190  } else {
191  include_once ("FDL/Class.DocFile.php");
192  $doc = new DocFile($dbaccess, $id);
193 
194  return ($doc);
195  }
196 }
197 /**
198  * create a new document object in type concordance
199  *
200  * the document is set with default values and default profil of the family
201  * @param string $dbaccess database specification
202  * @param string $fromid identificator of the family document (the number or internal name)
203  * @param bool $control if false don't control the user hability to create this kind of document
204  * @param bool $defaultvalues if false not affect default values
205  * @return Doc may be return false if no hability to create the document
206  */
207 function createDoc($dbaccess, $fromid, $control = true, $defaultvalues = true, $temporary = false)
208 {
209 
210  if (!is_numeric($fromid)) $fromid = getFamIdFromName($dbaccess, $fromid);
211  if ($fromid > 0) {
212  include_once ("FDL/Class.DocFam.php");
213  $cdoc = new DocFam($dbaccess, $fromid);
214 
215  if ($control) {
216  $err = $cdoc->control('create');
217  if ($err != "") return false;
218  }
219 
220  $classname = "Doc" . $fromid;
221  $GEN = getGen($dbaccess);
222  include_once ("FDL$GEN/Class.$classname.php");
223  $doc = new $classname($dbaccess);
224 
225  $doc->revision = "0";
226  $doc->fileref = "0";
227  $doc->doctype = $doc->defDoctype; // it is a new document (not a familly)
228  $doc->cprofid = "0"; // NO CREATION PROFILE ACCESS
229  $doc->fromid = $fromid;
230  if (!$temporary) {
231  $doc->setProfil($cdoc->cprofid); // inherit from its familly
232  $doc->setCvid($cdoc->ccvid); // inherit from its familly
233  $doc->wid = $cdoc->wid;
234  }
235  $doc->icon = $cdoc->icon; // inherit from its familly
236  $doc->usefor = $cdoc->usefor; // inherit from its familly
237  $doc->forumid = $cdoc->forumid; // inherit from its familly
238  $doc->atags = $cdoc->atags;
239  if ($defaultvalues) $doc->setDefaultValues($cdoc->getDefValues());
240  $doc->ApplyMask();
241  return ($doc);
242  }
243  return new_Doc($dbaccess);
244 }
245 /**
246  * create a temporary document object in type concordance
247  *
248  * the document is set with default values and has no profil
249  * the create privilege is not tested in this case
250  * @param string $dbaccess database specification
251  * @param string $fromid identificator of the family document (the number or internal name)
252  * @return Doc may be return false if no hability to create the document
253  */
254 function createTmpDoc($dbaccess, $fromid)
255 {
256  $d = createDoc($dbaccess, $fromid, false, true, true);
257  if ($d) {
258  $d->doctype = 'T'; // tag has temporary document
259  $d->profid = 0; // no privilege
260 
261  }
262  return $d;
263 }
264 /**
265  * return from id for document (not for family (use @see getFamFromId() instead)
266  * @param string $dbaccess database specification
267  * @param int $id identificator of the object
268  *
269  * @return int false if error occured (return -1 if family document )
270  */
272 {
273  if (!($id > 0)) return false;
274  if (!is_numeric($id)) return false;
276  $fromid = false;
277 
278  $result = pg_query($dbid, sprintf("select fromid from docfrom where id=%d", $id));
279  if ($result) {
280  if (pg_numrows($result) > 0) {
281  $arr = pg_fetch_array($result, 0, PGSQL_ASSOC);
282  $fromid = $arr["fromid"];
283  }
284  }
285 
286  return $fromid;
287 }
288 /**
289  * return from name for document (not for family (use @see getFamFromId() instead)
290  * @param string $dbaccess database specification
291  * @param int $id identificator of the object
292  *
293  * @return string false if error occured (return -1 if family document )
294  */
296 {
297 
298  if (!($id > 0)) return false;
299  if (!is_numeric($id)) return false;
301  $fromname = false;
302  $result = pg_query($dbid, sprintf("SELECT name from docfam where id=(select fromid from docfrom where id=%d)", $id));
303 
304  if (pg_numrows($result) > 0) {
305  $arr = pg_fetch_array($result, 0, PGSQL_ASSOC);
306  $fromname = $arr["name"];
307  }
308 
309  return $fromname;
310 }
311 /**
312  * return from id for family document
313  * @param string $dbaccess database specification
314  * @param int $id identificator of the object
315  *
316  * @return int false if error occured
317  */
319 {
320 
321  if (!($id > 0)) return false;
322  if (!is_numeric($id)) return false;
324  $fromid = false;
325  $result = pg_query($dbid, "select fromid from docfam where id=$id;");
326 
327  if (pg_numrows($result) > 0) {
328  $arr = pg_fetch_array($result, 0, PGSQL_ASSOC);
329  $fromid = intval($arr["fromid"]);
330  }
331 
332  return $fromid;
333 }
334 /**
335  * return document table value
336  * @param string $dbaccess database specification
337  * @param int $id identificator of the object
338  * @param array $sqlfilters add sql supply condition
339  *
340  * @return array false if error occured
341  */
342 function getTDoc($dbaccess, $id, $sqlfilters = array() , $result = array())
343 {
344  global $action;
345  global $SQLDELAY, $SQLDEBUG;
346 
347  if (!is_numeric($id)) $id = getIdFromName($dbaccess, $id);
348  if (!($id > 0)) return false;
350  $table = "doc";
351  $fromid = getFromId($dbaccess, $id);
352  if ($fromid > 0) $table = "doc$fromid";
353  else if ($fromid == - 1) $table = "docfam";
354 
355  $sqlcond = "";
356  if (count($sqlfilters) > 0) $sqlcond = "and (" . implode(") and (", $sqlfilters) . ")";
357  if (count($result) == 0) {
358  $userid = $action->user->id;
359  $sql = "select *,getuperm($userid,profid) as uperm from only $table where id=$id $sqlcond;";
360  } else {
361  $scol = implode($result, ",");
362  $sql = "select $scol from only $table where id=$id $sqlcond;";
363  }
364 
365  if ($SQLDEBUG) $sqlt1 = microtime(); // to test delay of request
366  $result = pg_query($dbid, $sql);
367  if ($SQLDEBUG) {
368  global $TSQLDELAY;
369  $SQLDELAY+= microtime_diff(microtime() , $sqlt1); // to test delay of request
370  $TSQLDELAY[] = array(
371  "t" => sprintf("%.04f", microtime_diff(microtime() , $sqlt1)) ,
372  "s" => $sql
373  );
374  }
375  if (($result) && (pg_numrows($result) > 0)) {
376  $arr = pg_fetch_array($result, 0, PGSQL_ASSOC);
377 
378  return $arr;
379  }
380  return false;
381 }
382 
383 /**
384  * return the value of an doc array item
385  *
386  * @param array &$t the array where get value
387  * @param string $k the index of the value
388  * @param string $d default value if not found or if it is empty
389  * @return string
390  */
391 function getv(&$t, $k, $d = "")
392 {
393  if (isset($t[$k]) && ($t[$k] != "")) return $t[$k];
394  if (strpos($t["attrids"], "£$k") !== 0) {
395 
396  $tvalues = explode("£", $t["values"]);
397  $tattrids = explode("£", $t["attrids"]);
398  foreach ($tattrids as $ka => $va) {
399  if ($va != "") {
400  if (!isset($t[$va])) $t[$va] = $tvalues[$ka];
401  if ($va == $k) {
402  if ($tvalues[$ka] != "") return $tvalues[$ka];
403  break;
404  }
405  }
406  }
407  }
408  return $d;
409 }
410 /**
411  * complete all values of an doc array item
412  *
413  * @param array &$t the array where get value
414  * @return void
415  */
416 function getvs(&$t)
417 {
418  $tvalues = explode("£", $t["values"]);
419  $tattrids = explode("£", $t["attrids"]);
420  foreach ($tattrids as $ka => $va) {
421  if ($va != "") {
422  if (!isset($t[$va])) $t[$va] = $tvalues[$ka];
423  }
424  }
425  return $t;
426 }
427 /**
428  * use to usort attributes
429  * @param BasicAttribute $a
430  * @param BasicAttribute $b
431  */
432 function tordered($a, $b)
433 {
434  if (isset($a->ordered) && isset($b->ordered)) {
435  if ($a->ordered == $b->ordered) return 0;
436  if ($a->ordered > $b->ordered) return 1;
437  return -1;
438  }
439  if (isset($a->ordered)) return 1;
440  if (isset($b->ordered)) return -1;
441  return 0;
442 }
443 
444 function cmp_cvorder3($a, $b)
445 {
446  if ($a["cv_order"] == $b["cv_order"]) {
447  return 0;
448  }
449  return ($a["cv_order"] < $b["cv_order"]) ? -1 : 1;
450 }
451 /**
452  * control privilege for a document in the array form
453  * the array must provide from getTdoc
454  * the function is equivalent of Doc::Control
455  * @param array $tdoc document
456  * @param string $aclname identificator of the privilege to test
457  * @return bool true if current user has privilege
458  */
459 function controlTdoc($tdoc, $aclname)
460 {
461  static $_ODocCtrol = false;
462  static $_Ocuid = false; // current user
463  if (!$_ODocCtrol) {
464  $cd = new DocCtrl();
465  $_ODocCtrol = $cd;
466  $_Ocuid = $cd->userid;
467  }
468 
469  if (($tdoc["profid"] <= 0) || ($_Ocuid == 1)) return true;
470  if (!isset($tdoc["uperm"])) {
472  $result = @pg_query($dbid, sprintf("select getuperm(%d,%d) as uperm", $_Ocuid, $tdoc['profid']));
473  if ($result && (pg_numrows($result) > 0)) {
474  $arr = pg_fetch_array($result, 0, PGSQL_ASSOC);
475  $tdoc["uperm"] = $arr["uperm"];
476  }
477  }
478  $err = $_ODocCtrol->ControlUp($tdoc["uperm"], $aclname);
479 
480  return ($err == "");
481 }
482 /**
483  * get document object from array document values
484  * @param string $dbaccess database specification
485  * @param array $v values of document
486  * @return Doc the document object
487  */
488 function getDocObject($dbaccess, $v, $k = 0)
489 {
490  static $_OgetDocObject;
491 
492  if ($v["doctype"] == "C") {
493  if (!isset($_OgetDocObject[$k]["family"])) $_OgetDocObject[$k]["family"] = new DocFam($dbaccess);
494  $_OgetDocObject[$k]["family"]->Affect($v, true);
495  $v["fromid"] = "family";
496  } else {
497  if (!isset($_OgetDocObject[$k][$v["fromid"]])) $_OgetDocObject[$k][$v["fromid"]] = createDoc($dbaccess, $v["fromid"], false, false);
498  }
499  $_OgetDocObject[$k][$v["fromid"]]->Affect($v, true);
500  $_OgetDocObject[$k][$v["fromid"]]->nocache = true;
501  return $_OgetDocObject[$k][$v["fromid"]];
502 }
503 /**
504  * return the next document in sql select ressources
505  * use with "ITEM" type searches direct in QueryDb
506  * return Doc the next doc (false if the end)
507  */
508 function getNextDbObject($dbaccess, $res)
509 {
510  $tdoc = pg_fetch_array($res, NULL, PGSQL_ASSOC);
511  if ($tdoc === false) return false;
512  return getDocObject($dbaccess, $tdoc, intval($res));
513 }
514 /**
515  * return the next document in sql select ressources
516  * use with "ITEM" type searches with getChildDoc
517  * return Doc the next doc (false if the end)
518  */
519 function getNextDoc($dbaccess, &$tres)
520 {
521  $n = current($tres);
522  if ($n === false) return false;
523  $tdoc = pg_fetch_array($n, NULL, PGSQL_ASSOC);
524  if ($tdoc === false) {
525  $n = next($tres);
526  if ($n === false) return false;
527  $tdoc = pg_fetch_array($n, NULL, PGSQL_ASSOC);
528  if ($tdoc === false) return false;
529  }
530  return getDocObject($dbaccess, $tdoc, intval(current($tres)));
531 }
532 /**
533  * count returned document in sql select ressources
534  * @param array $tres of ressources
535  * return Doc the next doc (false if the end)
536  */
537 function countDocs(&$tres)
538 {
539  $n = 0;
540  foreach ($tres as $res) $n+= pg_num_rows($res);
541  reset($tres);
542  return $n;
543 }
544 /**
545  * return the identificator of a family from internal name
546  *
547  * @param string $dbaccess database specification
548  * @param string $name internal family name
549  * @return int 0 if not found
550  */
552 {
553  include_once ("FDL/Class.DocFam.php");
554  global $tFamIdName;
555  if (!isset($tFamIdName)) {
556  $tFamIdName = array();
557  $q = new QueryDb($dbaccess, "DocFam");
558  $ql = $q->Query(0, 0, "TABLE");
559  while (list($k, $v) = each($ql)) {
560  if ($v["name"] != "") $tFamIdName[$v["name"]] = $v["id"];
561  }
562  }
563  if (isset($tFamIdName[$name])) return $tFamIdName[$name];
564  if (isset($tFamIdName[strtoupper($name) ])) return $tFamIdName[strtoupper($name) ];
565  return 0;
566 }
567 /**
568  * return the identificator of a document from a search with title
569  *
570  * @param string $dbaccess database specification
571  * @param string $name logical name
572  * @param string $famid must be set to increase speed search
573  * @param boolean $only set to true to not search in subfamilies
574  * @return int 0 if not found, return negative first id found if multiple (name must be unique)
575  */
576 function getIdFromTitle($dbaccess, $title, $famid = "", $only = false)
577 {
578  if ($famid && (!is_numeric($famid))) $famid = getFamIdFromName($dbaccess, $famid);
579  if ($famid > 0) {
580  $fromonly = ($only) ? "only" : "";
581  $err = simpleQuery($dbaccess, sprintf("select id from $fromonly doc%d where title='%s' and locked != -1", $famid, pg_escape_string($title)) , $id, true, true);
582  } else {
583  $err = simpleQuery($dbaccess, sprintf("select id from docread where title='%s' and locked != -1", pg_escape_string($title)) , $id, true, true);
584  }
585 
586  return $id;
587 }
588 /**
589  * return the identificator of a document from its logical name
590  *
591  * @param string $dbaccess database specification
592  * @param string $name logical name
593  * @param string $famid must be set to increase speed search
594  * @return int 0 if not found, return negative first id found if multiple (name must be unique)
595  */
597 {
598  static $first = true;
600  $id = false;
601 
602  if ($first) {
603  @pg_prepare($dbid, "getidfromname", 'select id from docname where name=$1');
604  $first = false;
605  }
606  // $result = pg_query($dbid,"select id from docname where name='$name';");
607  $result = pg_execute($dbid, "getidfromname", array(
608  trim($name)
609  ));
610  $n = pg_numrows($result);
611  if ($n > 0) {
612  $arr = pg_fetch_array($result, ($n - 1) , PGSQL_ASSOC);
613  $id = $arr["id"];
614  }
615  return $id;
616 }
617 /**
618  * return the logical name of a document from its initial identificator
619  *
620  * @param string $dbaccess database specification
621  * @param string $id initial identificator
622  *
623  * @return string empty if not found
624  */
626 {
627  static $first = true;
629  $id = intval($id);
630  // $result = pg_query($dbid,"select name from docname where id=$id;");
631  if ($first) {
632  @pg_prepare($dbid, "getNameFromId", 'select name from docname where id=$1');
633  $first = false;
634  }
635  $result = pg_execute($dbid, "getNameFromId", array(
636  $id
637  ));
638  $n = pg_numrows($result);
639  if ($n > 0) {
640  $arr = pg_fetch_array($result, ($n - 1) , PGSQL_ASSOC);
641  $name = $arr["name"];
642  }
643  return $name;
644 }
646 {
647 
648  global $tFamIdName;
649 
650  if (!isset($tFamIdName)) getFamIdFromName($action->GetParam("FREEDOM_DB") , "-");
651 
652  reset($tFamIdName);
653  while (list($k, $v) = each($tFamIdName)) {
654  $action->lay->set("IDFAM_$k", $v);
655  }
656 }
657 /**
658  * return freedom user document in concordance with what user id
659  * @param string $dbaccess database specification
660  * @param int $userid what user identificator
661  * @return Doc the user document
662  */
663 function getDocFromUserId($dbaccess, $userid)
664 {
665  if ($userid == "") return false;
666  include_once ("FDL/Lib.Dir.php");
667  $tdoc = array();
668  $user = new User("", $userid);
669  if (!$user->isAffected()) return false;
670  if ($user->isgroup == "Y") {
671  $filter = array(
672  "us_whatid = '$userid'"
673  );
674  $tdoc = getChildDoc($dbaccess, 0, 0, "ALL", $filter, 1, "LIST", getFamIdFromName($dbaccess, "IGROUP"));
675  } else {
676  $filter = array(
677  "us_whatid = '$userid'"
678  );
679  $tdoc = getChildDoc($dbaccess, 0, 0, "ALL", $filter, 1, "LIST", getFamIdFromName($dbaccess, "IUSER"));
680  }
681  if (count($tdoc) == 0) return false;
682  return $tdoc[0];
683 }
684 
685 function getFamTitle(&$tdoc)
686 {
687  $r = $tdoc["name"] . '#title';
688  $i = _($r);
689  if ($i != $r) return $i;
690  return $tdoc['title'];
691 }
692 /**
693  * verify in database if document is fixed
694  * @return bool
695  */
697 {
698  $tdoc = getTDoc($dbaccess, $id, array() , array(
699  "locked"
700  ));
701  if (!$tdoc) return null;
702  return ($tdoc["locked"] == - 1);
703 }
704 
705 function ComputeVisibility($vis, $fvis, $ffvis='')
706 {
707  if ($vis == "I") return $vis;
708  if ($fvis == "H") return $fvis;
709  if (($fvis == "R") && ($vis == "W")) return $fvis;
710  if (($fvis == "R") && ($vis == "O")) return "H";
711  if (($fvis == "O") && ($vis == "W")) return $fvis;
712  if (($fvis == "R") && ($vis == "U")) return $fvis;
713  if (($fvis == "S") && (($vis == "W") || ($vis == "O"))) return $fvis;
714  if ($fvis == "I") return $fvis;
715  if ($fvis == 'U') {
716  if ($ffvis && ($vis == 'W' || $vis=='O')) {
717  if ($ffvis=='S') return 'S';
718  if ($ffvis=='R') return 'R';
719  }
720  }
721 
722  return $vis;
723 }
724 /**
725  * return doc array of latest revision of initid
726  *
727  * @param string $dbaccess database specification
728  * @param string $initid initial identificator of the document
729  * @param array $sqlfilters add sql supply condition
730  * @return array values array if found. False if initid not avalaible
731  */
732 function getLatestTDoc($dbaccess, $initid, $sqlfilters = array() , $fromid = false)
733 {
734  global $action;
735 
736  if (!($initid > 0)) return false;
738  $table = "doc";
739  if (!$fromid) $fromid = getFromId($dbaccess, $initid);
740  if (!$fromid) {
741  $err = simpleQuery($dbaccess, sprintf("select fromid from docread where initid=%d and locked != -1", $initid) , $tf, true);
742  if (count($tf) > 0) $fromid = $tf[0];
743  }
744  if ($fromid > 0) $table = "doc$fromid";
745  else if ($fromid == - 1) $table = "docfam";
746 
747  $sqlcond = "";
748  if (count($sqlfilters) > 0) $sqlcond = "and (" . implode(") and (", $sqlfilters) . ")";
749 
750  $userid = $action->user->id;
751  if ($userid) {
752  $result = @pg_query($dbid, "select *,getuperm($userid,profid) as uperm from only $table where initid=$initid and doctype != 'T' and locked != -1 $sqlcond;");
753  if ($result && (pg_numrows($result) > 0)) {
754  if (pg_numrows($result) > 1) addWarningMsg(sprintf("document %d : multiple alive revision", $initid));
755 
756  $arr = pg_fetch_array($result, 0, PGSQL_ASSOC);
757 
758  return $arr;
759  }
760  }
761  return false;
762 }
763 /**
764  * return identificators according to latest revision
765  * the order is not the same as parameters. The key of result containt initial id
766  *
767  * @param string $dbaccess database specification
768  * @param array $ids array of document identificators
769  * @return array identificator relative to latest revision. if one or several documents document not exists the identificator not appear in result so the array count of result can be lesser than parameter
770  */
771 function getLatestDocIds($dbaccess, $ids)
772 {
773  if (!is_array($ids)) return null;
774 
776  foreach ($ids as $k => $v) $ids[$k] = intval($v);
777  $sids = implode($ids, ",");
778  $sql = sprintf("SELECT id,initid from docread where initid in (SELECT initid from docread where id in (%s)) and locked != -1;", $sids);
779  $result = @pg_query($dbid, $sql);
780  if ($result) {
781  $arr = pg_fetch_all($result);
782  $tlids = array();
783  foreach ($arr as $v) $tlids[$v["initid"]] = $v["id"];
784  return $tlids;
785  }
786  return null;
787 }
788 /**
789  * return latest id of document from its initid
790  *
791  * @param string $dbaccess database specification
792  * @param array $ids array of document identificators
793  * @return array identificator relative to latest revision. if one or several documents document not exists the identificator not appear in result so the array count of result can be lesser than parameter
794  */
795 function getLatestDocId($dbaccess, $initid)
796 {
797  if (is_array($ids)) return null;
798  // first more quick if alive
799  $err = simpleQuery($dbaccess, sprintf("select id from docread where initid='%d' and locked != -1", $initid) , $id, true, true);
800  if (($err == '') && ($id > 0)) return $id;
801  // second for zombie document
802  $err = simpleQuery($dbaccess, sprintf("select id from docread where initid='%d' order by id desc limit 1", $initid) , $id, true, true);
803  if ($err == '') return $id;
804  return null;
805 }
806 /**
807  * return doc array of specific revision of document initid
808  *
809  * @param string $dbaccess database specification
810  * @param string $initid initial identificator of the document
811  * @param int $rev revision number
812  * @return array values array if found. False if initid not avalaible
813  */
814 function getRevTDoc($dbaccess, $initid, $rev)
815 {
816  global $action;
817 
818  if (!($initid > 0)) return false;
820  $table = "doc";
821  $fromid = getFromId($dbaccess, $initid);
822  if ($fromid > 0) $table = "doc$fromid";
823  else if ($fromid == - 1) $table = "docfam";
824 
825  $userid = $action->user->id;
826  $result = pg_query($dbid, "select *,getuperm($userid,profid) as uperm from only $table where initid=$initid and revision=$rev;");
827  if (pg_numrows($result) > 0) {
828  $arr = pg_fetch_array($result, 0, PGSQL_ASSOC);
829 
830  return $arr;
831  }
832  return false;
833 }
834 /**
835  * return really latest revision number
836  * use only for debug mode
837  *
838  * @param string $dbaccess database specification
839  * @param int $initid initial identificator of the document
840  * @param int $fromid family identicator of document
841  * @return int latest revision if found. False if initid not available
842  */
843 function getLatestRevisionNumber($dbaccess, $initid, $fromid = 0)
844 {
845  global $action;
846 
847  $initid = intval($initid);
848  if (!($initid > 0)) return false;
850  $table = "doc";
851  if (!$fromid) $fromid = getFromId($dbaccess, $initid);
852  if ($fromid > 0) $table = "doc$fromid";
853  else if ($fromid == - 1) $table = "docfam";
854 
855  $result = @pg_query($dbid, "SELECT revision from only $table where initid=$initid order by revision desc limit 1;");
856  if ($result && (pg_numrows($result) > 0)) {
857  $arr = pg_fetch_array($result, 0, PGSQL_ASSOC);
858  return $arr['revision'];
859  }
860  return false;
861 }
862 /**
863  * Create default folder for a family with default constraint
864  *
865  * @param Doc $Doc the family object document
866  * @return int id of new folder (false if error)
867  */
869 {
870  $dir = createDoc($doc->dbaccess, getFamIdFromName($doc->dbaccess, "DIR"));
871  $err = $dir->Add();
872  if ($err != "") return false;
873  $dir->setValue("BA_TITLE", sprintf(_("root for %s") , $doc->title));
874  $dir->setValue("BA_DESC", _("default folder"));
875  $dir->setValue("FLD_ALLBUT", "1");
876  $dir->setValue("FLD_FAM", $doc->title . "\n" . _("folder") . "\n" . _("search"));
877  $dir->setValue("FLD_FAMIDS", $doc->id . "\n" . getFamIdFromName($doc->dbaccess, "DIR") . "\n" . getFamIdFromName($doc->dbaccess, "SEARCH"));
878  $dir->setValue("FLD_SUBFAM", "yes\nyes\nyes");
879  $dir->Modify();
880  $fldid = $dir->id;
881  return $fldid;
882 }
883 /**
884  * get personal profil
885  *
886  * return the profil named "PERSONAL-PROFIL-<$uid>"
887  * the document return is a folder profil that can be use also for "normal" documents
888  * @return PDir may be return false if no hability to create the document
889  */
890 function getMyProfil($dbaccess, $create = true)
891 {
892  global $action;
893  $uid = $action->user->id;
894  $pname = sprintf("PERSONAL-PROFIL-%d", $uid);
895  $p = new_doc($dbaccess, $pname);
896  if (!$p->isAffected()) {
897  if ($create) {
898  $p = createDoc($dbaccess, "PDIR");
899  $p->name = $pname;
900  $p->setValue("ba_title", sprintf(_("Personal profile for %s %s") , $action->user->firstname, $action->user->lastname));
901  $p->setValue("prf_desc", sprintf(_("Only %s %s can view and edit") , $action->user->firstname, $action->user->lastname));
902 
903  $err = $p->Add();
904  if ($err == "") {
905  $err = $p->setControl(); //activate the profile
906 
907  }
908  } else {
909  $p = false;
910  }
911  }
912  return $p;
913 }
914 
915 function xt_innerXML(&$node)
916 {
917  if (!$node) return false;
918  $document = $node->ownerDocument;
919  $nodeAsString = $document->saveXML($node);
920  preg_match('!<.*?>(.*)</.*?>!s', $nodeAsString, $match);
921  return $match[1];
922 }
923 function cleanhtml($html)
924 {
925  $html = preg_replace("/<\/?span[^>]*>/s", "", $html);
926  $html = preg_replace("/<\/?font[^>]*>/s", "", $html);
927  $html = preg_replace("/<\/?meta[^>]*>/s", "", $html);
928  $html = preg_replace("/<style[^>]*>.*?<\/style>/s", "", $html);
929  $html = preg_replace("/<([^>]*) style=\"[^\"]*\"/s", "<\\1", $html);
930  $html = preg_replace("/<([^>]*) class=\"[^\"]*\"/s", "<\\1", $html);
931  return $html;
932 }
933 ?>
← centre documentaire © anakeen - published under CC License - Dynacase