Ant.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('UI');
  4. Lib::loadClass('DBG');
  5. Lib::loadClass('Request');
  6. Lib::loadClass('Crypt');
  7. Lib::loadClass('Route_Ant');
  8. class Route_UrlAction_Ant extends Route_Ant {// @doc @see Route_Ant
  9. public function defaultAction() {
  10. UI::gora();
  11. UI::menu();
  12. UI::startContainer();
  13. UI::tag('h1', [], 'Ant');
  14. try {
  15. $taskList = $this->getAntUrlActionList();
  16. // $featureID = V::get('featureID', '', $_GET);
  17. $primaryKey = V::get('primaryKey', '', $_GET, 'word');
  18. $typeName = V::get('typeName', '', $_GET, 'word');
  19. if (empty($typeName)) throw new Exception("Missing typeName");
  20. if (empty($primaryKey)) throw new Exception("Missing primaryKey");
  21. $parsedInfo = Core_AclHelper::parseTypeName($typeName);
  22. DBG::log($parsedInfo, '', "Ant parseTypeName({$typeName})");
  23. $acl = ACL::getAclByNamespace("{$parsedInfo['url']}/{$parsedInfo['name']}");
  24. $pkField = $acl->getPrimaryKeyField();
  25. UI::startTag('ul');
  26. foreach ($taskList as $path => $label) {
  27. echo UI::h('li', [], [
  28. UI::h('a', [
  29. 'href' => $this->getLink('ant', [
  30. 'path' => $path,
  31. 'typeName' => $typeName,
  32. 'primaryKey' => $primaryKey,
  33. 'primaryKeyField' => $pkField
  34. ])
  35. ], "Uruchom '{$label}'")
  36. ]);
  37. }
  38. UI::endTag('ul');
  39. } catch (Exception $e) {
  40. UI::alert('danger', $e->getMessage());
  41. DBG::log($e);
  42. }
  43. UI::endContainer();
  44. UI::dol();
  45. }
  46. public function outputAction() {// index.php?_route=UrlAction_Ant&_task=output & path={$path} & file={$file}";
  47. $path = V::get('path', '', $_GET);
  48. if (!$path) throw new Exception("Missing Ant path!");
  49. $file = V::get('file', '', $_GET);
  50. if (!$file) throw new Exception("Missing Ant file!");
  51. // TODO: allow auth by token?
  52. throw new Exception("TODO: return output file '$file' from ant task '{$path}'");
  53. }
  54. public function antAction() {
  55. UI::gora();
  56. UI::startContainer();
  57. try {
  58. $path = V::get('path', '', $_GET);
  59. if (!$path) throw new Exception("Missing Ant path!");
  60. $typeName = V::get('typeName', '', $_GET);
  61. if (!$typeName) throw new Exception("Missing typeName");
  62. $primaryKey = V::get('primaryKey', '', $_GET);
  63. if (!$primaryKey) throw new Exception("Missing primaryKey");
  64. $pkField = V::get('primaryKeyField', '', $_GET);
  65. if (!$pkField) throw new Exception("Missing primaryKeyField");
  66. // list($nsPrefix, $objectName) = explode(':', $typeName);// TODO: get wfs prefix from typeName
  67. $parsedInfo = Core_AclHelper::parseTypeName($typeName);
  68. DBG::log($parsedInfo, '', "Ant parseTypeName({$typeName})");
  69. $nsPrefix = $parsedInfo['prefix'];
  70. $objectName = $parsedInfo['name'];
  71. // TODO: validate missing ...
  72. DBG::log($_GET, 'array', '$_GET');
  73. DBG::log([ 'msg'=>'$path', '$path'=>$path]);
  74. DBG::log([ 'msg'=>'$primaryKey', '$primaryKey'=>$primaryKey]);
  75. DBG::log([ 'msg'=>'$typeName', '$typeName'=>$typeName]);
  76. $taskList = $this->getAntUrlActionList();
  77. if (!array_key_exists($path, $taskList)) throw new Exception("Ant path not exists! '{$path}'");
  78. echo UI::h('h3', [], [
  79. "Druk",
  80. '<br>',
  81. UI::h('small', [], $taskList[$path])
  82. ]);
  83. echo UI::h('hr');
  84. $webRootUrl = Request::getPathUri() . "schema/ant-url_action/{$path}";// TODO: security - only for test
  85. $outputFunctionUrl = $this->getLink('output') . "&path={$path}&file=";
  86. DBG::log([ 'msg'=>'$webRootUrl', '$webRootUrl'=>$webRootUrl]);
  87. DBG::log([ 'msg'=>'$outputFunctionUrl', '$outputFunctionUrl'=>$outputFunctionUrl]);
  88. $testUrl = Request::getPathUri() . "wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME={$typeName}&SRSNAME=EPSG:3003&featureID={$objectName}.{$primaryKey}";
  89. $cryptedPass = base64_encode(User::getLogin() . ":" . Crypt::decrypt($_SESSION['ADM_PASS_HASH']));
  90. $uniqID = session_id();// TODO: uniq id for every request
  91. // $cmd = "{$this->pathUrlActions}{$path}/do_build.sh";
  92. $cmd = "cd {$this->pathUrlActions}{$path} && ant -S";
  93. $cmd .= " -DoutputFunctionUrl='{$outputFunctionUrl}'";
  94. $cmd .= " -DwebRootUrl='{$webRootUrl}'";
  95. $cmd .= " -Durl='{$testUrl}'";
  96. $cmd .= " -DpasswordBase64Basic=\"{$cryptedPass}\"";
  97. $cmd .= " -Duuid=\"{$uniqID}\"";
  98. $cmd .= " -Dxpath={$pkField}";
  99. $cmd .= " -Dxpath_value={$primaryKey}";
  100. $cmd .= " -DtypeName=\"{$typeName}\"";
  101. $cmd .= " 2>&1";
  102. $pathCmd = [];
  103. $pathCmd[] = '/opt/local/bin/ant';
  104. $pathCmd[] = '/bin';
  105. $pathCmd[] = '/usr/bin';
  106. $pathCmd[] = '/usr/local/bin';
  107. $pathCmd[] = '/opt/local/bin';
  108. $pathCmd[] = '/sbin';
  109. $pathCmd[] = '/usr/sbin';
  110. $pathCmd[] = '/opt/local/sbin/skrypty';
  111. $pathCmd[] = '/opt/local/var/macports/software';
  112. $pathCmd[] = '/Applications/Server.app/Contents/ServerRoot/usr/bin';
  113. $pathCmd[] = '/Applications/Server.app/Contents/ServerRoot/usr/sbin';
  114. $cmd = 'export PATH=' . implode(':', $pathCmd) . "\n" .
  115. 'JAVA_HOME="/usr/bin/java"' . "\n" .
  116. 'MAVEN_OPTS="-Xms256m -Xmx512m"' . "\n" .
  117. $cmd;
  118. $useShellExec = true;
  119. if ($useShellExec) $out = shell_exec($cmd);
  120. else V::exec($cmd, $out, $ret);
  121. DBG::log([ 'msg'=>"cmd and returns({$ret})", 'output'=>$out, 'cmd'=>str_replace(APP_PATH_ROOT, 'SE', $cmd) ]);
  122. if (empty($out)) throw new Exception("Empty output");
  123. if ($useShellExec) $out = explode("\n", $out);
  124. $html = []; $startRead = false;
  125. foreach ($out as $line) {
  126. if (!$startRead) {
  127. if ('OUTPUT__START' == $line) {
  128. $startRead = true;
  129. continue;
  130. }
  131. } else {
  132. if ('<!DOCTYPE' == substr($line, 0, strlen('<!DOCTYPE'))) continue;
  133. if ('OUTPUT__END' == $line) {
  134. break;
  135. }
  136. $html[]= $line;
  137. }
  138. }
  139. if (empty($html)) UI::alert('danger', "Empty output!");
  140. else {
  141. // echo UI::h('pre', [], htmlspecialchars(implode("\n", $html)));
  142. echo UI::h('div', ['class'=>"container", 'style'=>"padding:12px; border:1px solid #ddd"], $html);
  143. }
  144. } catch (Exception $e) {
  145. UI::alert('danger', $e->getMessage());
  146. DBG::log($e);
  147. }
  148. UI::endContainer();
  149. UI::dol();
  150. }
  151. }