Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Lib.WCheck.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  * Util function for update and initialize application
9  *
10  * @author Anakeen 2005
11  * @version $Id: Lib.WCheck.php,v 1.21 2009/01/07 15:35:07 jerome 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 ("WHAT/Lib.System.php");
20 //---------------------------------------------------
21 function GetDbVersion($dbid, &$tmachine)
22 {
23  $tver = array();
24  $tmachine = array();
25  $rq = pg_exec($dbid, "select paramv.val, application.name, application.machine from paramv, application where paramv.name='VERSION' and paramv.appid=application.id");
26 
27  if ($rq === false) return GetDbOldVersion($dbid);
28 
29  for ($i = 0; $i < pg_numrows($rq); $i++) {
30  $row = pg_fetch_array($rq, $i);
31  $tver[$row["name"]] = $row["val"];
32  $tmachine[$row["name"]] = $row["machine"];
33  }
34 
35  return ($tver);
36 }
37 //---------------------------------------------------
39 {
40  print "GetDbOldVersion";
41  $tver = array();
42  $rq = pg_exec($dbid, "select param.val, application.name from param, application where param.name='VERSION' and param.key=application.id");
43 
44  for ($i = 0; $i < pg_numrows($rq); $i++) {
45  $row = pg_fetch_array($rq, $i);
46  $tver[$row["name"]] = $row["val"];
47  if ($row["name"] == "USERS") {
48  $tver["CORE"] = $row["val"];
49  $tver["ACCESS"] = $row["val"];
50  $tver["APPMNG"] = $row["val"];
51  $tver["AUTHENT"] = $row["val"];
52  }
53  }
54 
55  return ($tver);
56 }
57 //---------------------------------------------------
58 function GetFileVersion($topdir)
59 {
60 
61  $tver = array();
62  if ($dir = @opendir($topdir)) {
63  while (($file = readdir($dir)) !== false) {
64  $inifile = $topdir . "/$file/${file}_init.php";
65  if (@is_file($inifile)) {
66 
67  $fini = fopen($inifile, "r");
68  while (!feof($fini)) {
69  $line = fgets($fini, 256);
70  if (preg_match("/VERSION.*=>[ \t]*\"[ \t]*([0-9\.\-]+)/", $line, $reg)) {
71  if (isset($reg[1])) $tver[$file] = $reg[1];
72  }
73  }
74  fclose($fini);
75  }
76  }
77  closedir($dir);
78  }
79  return ($tver);
80 }
81 /**
82  * get iorder value in .app files
83  * @param string $topdir publish directory
84  * @return array of iorder
85  */
86 function getAppOrder($topdir)
87 {
88 
89  $tiorder = array();
90  if ($dir = @opendir($topdir)) {
91  while (($file = readdir($dir)) !== false) {
92  $inifile = $topdir . "/$file/${file}.app";
93  if (@is_file($inifile)) {
94  unset($app_desc);
95  include ($inifile);
96 
97  if (isset($app_desc)) {
98  if (isset($app_desc["iorder"])) $tiorder[$file] = $app_desc["iorder"];
99  }
100  }
101  }
102  closedir($dir);
103  }
104  return ($tiorder);
105 }
106 /** compare version like 1.2.3-4
107  * @param string $v1 version one
108  * @param string $v2 version two
109  * @return int 0 if equal -1 if ($v1<$v2) 1 if ($v2>$1)
110  */
111 function vercmp($v1, $v2)
112 {
113  return version_compare($v1, $v2);
114 }
115 
116 function version2float($ver)
117 {
118  if (preg_match_all('/([0-9]+)/', $ver, $matches)) {
119  $matches = ($matches[0]);
120  $sva = '';
121  $c = count($matches);
122  if ($c < 4) {
123  for ($i = 0; $i < (4 - $c); $i++) {
124  $matches[] = '0';
125  }
126  }
127  foreach ($matches as $k => $v) $sva.= sprintf("%02d", $v);
128  return floatval($sva);
129  }
130 }
131 
133 {
136 
137  $dbid = @pg_connect($dbaccess_core);
138 
139  if (!$dbid) {
140  $err = _("cannot access to core database service [service='$pgservice_core']");
141  exec("PGSERVICE=\"$pgservice_core\" psql -c '\q'", $out);
142  $err.= implode(",", $out);
143  } else {
144  pg_close($dbid);
145  }
146  return $err;
147 }
148 
149 function getCheckApp($pubdir, &$tapp, $version_override = array())
150 {
151  global $_SERVER;
152 
156 
158  $dbid = @pg_connect($dbaccess_core);
159 
160  if (!$dbid) {
161  $err = _("cannot access to core database service [service='$pgservice_core']");
162  exec("PGSERVICE=\"$pgservice_core\" psql -c '\q'", $out);
163  $err.= implode(",", $out);
164  } else {
165  $tvdb = GetDbVersion($dbid, $tmachine);
166  $tvfile = GetFileVersion("$pubdir");
167  pg_close($dbid);
168  /*
169  * Override versions of app for doing pmigr (post migr)
170  * scripts from WIFF
171  */
172  foreach ($tvdb as $appname => & $version) {
173  if (array_key_exists($appname, $version_override)) {
174  $version = $version_override[$appname];
175  }
176  }
177 
178  $ta = array_unique(array_merge(array_keys($tvdb) , array_keys($tvfile)));
179  foreach ($ta as $k => $v) {
180  if (($tmachine[$v] != "") && (gethostbyname($tmachine[$v]) != gethostbyname($_SERVER["HOSTNAME"]))) $chk[$v] = "?";
181  else if ($tvdb[$v] == $tvfile[$v]) {
182  $chk[$v] = "";
183  } else if ($tvdb[$v] == "") {
184  $chk[$v] = "I";
185  } else if ($tvfile[$v] == "") {
186  $chk[$v] = "D";
187  } else if (vercmp($tvdb[$v], $tvfile[$v]) == 1) {
188  $chk[$v] = "R";
189  } else {
190  $chk[$v] = "U";
191  }
192  $tapp[$v] = array(
193  "name" => $v,
194  "vdb" => $tvdb[$v],
195  "vfile" => $tvfile[$v],
196  "chk" => $chk[$v],
197  "machine" => $tmachine[$v]
198  );
199  }
200  }
201  return $err;
202 }
203 
204 function getCheckActions($pubdir, $tapp, &$tact, $version_override = array())
205 {
206 
207  $wsh = array(); // application update
208  $cmd = array(); // pre/post install
209  $dump = array();
210 
213 
214  $dbid = @pg_connect("service='$pgservice_core'");
215 
216  $tvdb = GetDbVersion($dbid, $tmachine);
217  $tiorder = getAppOrder($pubdir);
218  /*
219  * Override versions of app for doing pmigr (post migr)
220  * scripts from WIFF
221  */
222  foreach ($tvdb as $appname => & $version) {
223  if (array_key_exists($appname, $version_override)) {
224  $version = $version_override[$appname];
225  }
226  }
227 
228  foreach ($tiorder as $k => $v) {
229  $tapp[$k]["iorder"] = $v;
230  }
231  uasort($tapp, "cmpapp");
232  foreach ($tapp as $k => $v) {
233  $migr = array();
234  $pattern = preg_quote($k);
235  // search Migration file
236  if ($dir = @opendir("$pubdir/$k")) {
237  while (($file = readdir($dir)) !== false) {
238  if (preg_match("/{$pattern}_(?:migr|premigr)_([0-9\.]+)$/", $file, $reg)) {
239  if (($tvdb[$k] != "") && (vercmp($tvdb[$k], $reg[1]) < 0)) $migr[] = "$pubdir/$k/$file";
240  }
241  }
242  }
243  usort($migr, "cmpmigr");
244  $cmd = array_merge($cmd, $migr);
245  // search PRE install
246  if (($v["chk"] != "") && (is_file("$pubdir/$k/{$k}_post"))) {
247  if ($v["chk"] == "I") {
248  $cmd[] = "$pubdir/$k/{$k}_post " . $v["chk"];
249  }
250  }
251  switch ($v["chk"]) {
252  case "I":
253  $cmd[] = "$pubdir/wsh.php --api=appadmin --method=init --appname=$k";
254  $cmd[] = "$pubdir/wsh.php --api=appadmin --method=update --appname=$k";
255  break;
256 
257  case "U":
258  $cmd[] = "$pubdir/wsh.php --api=appadmin --method=update --appname=$k";
259  break;
260 
261  case "D":
262  $cmd[] = "#$pubdir/wsh.php --api=appadmin --method=delete --appname=$k";
263  break;
264 
265  case "R":
266  $cmd[] = "#rpm -Uvh $k-" . $v["vdb"];
267  break;
268  }
269  // search POST install
270  if (($v["chk"] != "") && (is_file("$pubdir/$k/{$k}_post"))) {
271  if ($v["chk"] == "I") {
272  $cmd[] = "$pubdir/$k/{$k}_post U";
273  } else {
274  if (($v["chk"] != "R") && ($v["chk"] != "?")) {
275  if ($v["chk"] == "D") $cmd[] = "#$pubdir/$k/{$k}_post " . $v["chk"];
276  else $cmd[] = "$pubdir/$k/{$k}_post " . $v["chk"];
277  }
278  }
279  }
280  // search Post Migration file
281  $migr = array();
282  if ($dir = @opendir("$pubdir/$k")) {
283  while (($file = readdir($dir)) !== false) {
284  if (preg_match("/{$pattern}_(?:pmigr|postmigr)_([0-9\.]+)$/", $file, $reg)) {
285 
286  if (($tvdb[$k] != "") && (vercmp($tvdb[$k], $reg[1]) < 0)) $migr[] = "$pubdir/$k/$file";
287  }
288  }
289  }
290  usort($migr, "cmpmigr");
291  $cmd = array_merge($cmd, $migr);
292  }
293 
294  $dump[] = "PGSERVICE=\"$pgservice_core\" pg_dump > " . getTmpDir() . "/" . uniqid($pgservice_core);
295  $dump[] = "PGSERVICE=\"$pgservice_freedom\" pg_dump -D > " . getTmpDir() . "/" . uniqid($pgservice_freedom);
296  // $dump[] = "/etc/rc.d/init.d/httpd stop";
297  $dump[] = "$pubdir/wstop";
298  $dump[] = "$pubdir/whattext";
299 
300  $tact = array_merge($dump, $cmd);
301 
302  $tact[] = "$pubdir/wsh.php --api=freedom_clean";
303  $tact[] = "$pubdir/wstart";
304  global $_SERVER;
305  if (empty($_GET['httpdrestart']) || ($_GET['httpdrestart'] != 'no')) {
306  if ($_SERVER['HTTP_HOST'] != "") $tact[] = "sudo $pubdir/admin/shttpd";
307  else $tact[] = "service httpd restart";
308  }
309 }
310 
311 function cmpapp($a, $b)
312 {
313  if (isset($a["iorder"]) && isset($b["iorder"])) {
314  if ($a["iorder"] > $b["iorder"]) return 1;
315  else if ($a["iorder"] < $b["iorder"]) return -1;
316  return 0;
317  }
318  if (isset($a["iorder"])) return -1;
319  if (isset($b["iorder"])) return 1;
320  return 0;
321 }
322 
323 function cmpmigr($migr_a, $migr_b)
324 {
325  $v_a = "";
326  $v_b = "";
327  preg_match("/_(?:p|post|pre)?migr_(?P<version>[0-9\.]+)$/", $migr_a, $v_a);
328  preg_match("/_(?:p|post|pre)?migr_(?P<version>[0-9\.]+)$/", $migr_b, $v_b);
329 
330  return version_compare($v_a['version'], $v_b['version']);
331 }
332 ?>
← centre documentaire © anakeen - published under CC License - Dynacase