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