Ant.php 6.4 KB

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