Ant.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. echo UI::hButtonPost("odśwież", [
  20. 'class' => "btn btn-link",
  21. 'form.class' => "pull-right",
  22. 'title' => "Wczytaj ponownie listę dostępnych funkcji",
  23. 'data' => [
  24. 'forceUpdate' => '1',
  25. ]
  26. ]);
  27. UI::tag('h1', [], 'Ant');
  28. try {
  29. $taskList = $this->getAntUrlActionList();
  30. // $featureID = V::get('featureID', '', $_GET);
  31. $primaryKey = V::get('primaryKey', '', $_GET, 'word');
  32. $typeName = V::get('typeName', '', $_GET, 'word');
  33. if (empty($typeName)) throw new Exception("Missing typeName");
  34. if (empty($primaryKey)) throw new Exception("Missing primaryKey");
  35. $parsedInfo = Core_AclHelper::parseTypeName($typeName);
  36. DBG::log($parsedInfo, '', "Ant parseTypeName({$typeName})");
  37. $acl = ACL::getAclByNamespace("{$parsedInfo['url']}/{$parsedInfo['name']}");
  38. $pkField = $acl->getPrimaryKeyField();
  39. UI::startTag('ul');
  40. foreach ($taskList as $path => $label) {
  41. echo UI::h('li', [], [
  42. UI::h('a', [
  43. 'class' => 'btn btn-md btn-link',
  44. 'href' => $this->getLink('ant', [
  45. 'path' => $path,
  46. 'typeName' => $typeName,
  47. 'primaryKey' => $primaryKey,
  48. 'primaryKeyField' => $pkField
  49. ])
  50. ], "Uruchom '{$label}'")
  51. ]);
  52. $tmpls = $this->getAntUrlActionTemplates($path);
  53. if (!empty($tmpls)) {
  54. foreach ($tmpls as $tmpl => $labelTmpl) {
  55. echo UI::h('div', [ 'style' => "padding-left: 50px" ], [
  56. UI::h('a', [
  57. 'href' => $this->getLink('ant', [
  58. 'path' => $path,
  59. 'template' => $tmpl,
  60. 'typeName' => $typeName,
  61. 'primaryKey' => $primaryKey,
  62. 'primaryKeyField' => $pkField
  63. ]),
  64. // TODO: 'onclick' => 'return p5UI_runWPSAsyncJob(event, this)',
  65. 'class' => 'btn btn-sm btn-link',
  66. ], '<i class="glyphicon glyphicon-arrow-right"></i> ' . $labelTmpl)
  67. ]);
  68. }
  69. }
  70. }
  71. UI::endTag('ul');
  72. } catch (Exception $e) {
  73. UI::alert('danger', $e->getMessage());
  74. DBG::log($e);
  75. }
  76. UI::endContainer();
  77. UI::dol();
  78. }
  79. public function outputAction() {// index.php?_route=UrlAction_Ant&_task=output & path={$path} & file={$file}";
  80. $path = V::get('path', '', $_GET);
  81. if (!$path) throw new Exception("Missing Ant path!");
  82. $file = V::get('file', '', $_GET);
  83. if (!$file) throw new Exception("Missing Ant file!");
  84. // TODO: allow auth by token?
  85. throw new Exception("TODO: return output file '$file' from ant task '{$path}'");
  86. }
  87. public function antAction() {
  88. UI::gora();
  89. UI::startContainer();
  90. try {
  91. $path = V::get('path', '', $_GET);
  92. if (!$path) throw new Exception("Missing Ant path!");
  93. $typeName = V::get('typeName', '', $_GET);
  94. if (!$typeName) throw new Exception("Missing typeName");
  95. $primaryKey = V::get('primaryKey', '', $_GET);
  96. if (!$primaryKey) throw new Exception("Missing primaryKey");
  97. $pkField = V::get('primaryKeyField', '', $_GET);
  98. if (!$pkField) throw new Exception("Missing primaryKeyField");
  99. // list($nsPrefix, $objectName) = explode(':', $typeName);// TODO: get wfs prefix from typeName
  100. $parsedInfo = Core_AclHelper::parseTypeName($typeName);
  101. DBG::log($parsedInfo, '', "Ant parseTypeName({$typeName})");
  102. $nsPrefix = $parsedInfo['prefix'];
  103. $objectName = $parsedInfo['name'];
  104. $template = V::get('template', '', $_GET);
  105. $confirmAntfile = V::get('confirmAntfile', '', $_GET);
  106. $confirmAntfileTarget = V::get('confirmAntfileTarget', '', $_GET);
  107. // TODO: validate missing ...
  108. DBG::log($_GET, 'array', '$_GET');
  109. DBG::log([ 'msg'=>'$path', '$path'=>$path]);
  110. DBG::log([ 'msg'=>'$primaryKey', '$primaryKey'=>$primaryKey]);
  111. DBG::log([ 'msg'=>'$typeName', '$typeName'=>$typeName]);
  112. $taskList = $this->getAntUrlActionList();
  113. if (!array_key_exists($path, $taskList)) throw new Exception("Ant path not exists! '{$path}'");
  114. echo UI::h('h3', [], [
  115. "Druk",
  116. '<br>',
  117. UI::h('small', [], $taskList[$path])
  118. ]);
  119. echo UI::h('hr');
  120. $webRootUrl = Request::getPathUri() . "schema/ant-url_action/{$path}";// TODO: security - only for test
  121. $outputFunctionUrl = $this->getLink('output') . "&path={$path}&file=";
  122. $antFunctionUrl = $this->getLink('ant') . "&path={$path}&file={$file}&template={$template}&typeName={$typeName}&primaryKey={$primaryKey}&primaryKeyField={$pkField}"; // &confirmAntfile={$confirmAntfile}&confirmAntfileTarget={$confirmAntfileTarget} sdo confirmacji potrzebne - wzglednie przetwarzac z tresci output URL_TASK - ale potrzebne parametry analogiczne aby chodzily
  123. DBG::log([ 'msg'=>'$webRootUrl', '$webRootUrl'=>$webRootUrl]);
  124. DBG::log([ 'msg'=>'$outputFunctionUrl', '$outputFunctionUrl'=>$outputFunctionUrl]);
  125. $testUrl = Request::getPathUri() . "wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$typeName}&SRSNAME=EPSG:3003&featureID={$objectName}.{$primaryKey}";// &REQUEST=GetFeature
  126. // $testUrl = Request::getPathUri() . "wfs-data.php" ;// &REQUEST=GetFeature
  127. $cryptedPass = base64_encode(User::getLogin() . ":" . Crypt::decrypt($_SESSION['ADM_PASS_HASH']));
  128. $uniqID = date("Y-m-d-H_i_s") . substr(md5(time()), 0, 6);// TODO: uniq id for every request
  129. // $cmd = "{$this->pathUrlActions}/{$path}/do_build.sh";
  130. $cmd = "cd {$this->pathUrlActions}/{$path} && ant -S";
  131. // ant -silent, -S print nothing but task outputs and build failures
  132. // ant -D<property>=<value> use value for given property
  133. // ant -propertyfile <name> load all properties from file with -D properties taking precedence
  134. // TODO: mv to build.properties file @see https://dzone.com/tutorials/java/ant/ant-properties-file.html
  135. // ant -propertyfile build.properties
  136. $cmd .= " -DoutputFunctionUrl='{$outputFunctionUrl}'";
  137. $cmd .= " -DantFunctionUrl='{$antFunctionUrl}'";
  138. if( strlen($confirmAntfile) > 0 ) $cmd .= " -DconfirmAntfile={$confirmAntfile}";
  139. if( strlen($confirmAntfileTarget) > 0 ) $cmd .= " -DconfirmAntfileTarget={$confirmAntfileTarget}";
  140. $cmd .= " -DwebRootUrl='{$webRootUrl}'";
  141. // $cmd .= " -Durl='{$testUrl}'"; nie mozna tego uzywac, bo nadpisuje property w funkcji build_get_wfs.xml
  142. $cmd .= " -DpasswordBase64Basic=\"{$cryptedPass}\"";
  143. $cmd .= " -Duuid=\"{$uniqID}\"";
  144. $cmd .= " -Dphp_session_id=\"" . session_id() . "\"";
  145. $cmd .= " -Dxpath={$pkField}";
  146. $cmd .= " -Dxpath_value={$primaryKey}";
  147. $cmd .= " -Dtemplate={$template}";
  148. $cmd .= " -Dapi_url=".Request::getPathUri()."wfs-data.php"; //potrzebuje ten parametr do dzialania w AMS itp
  149. $cmd .= " -DtypeName=\"{$typeName}\"";
  150. if ($template) $cmd .= " {$template} ";
  151. $cmd .= " 2>&1";
  152. $pathCmd = [];
  153. $pathCmd[] = '/opt/local/bin/ant';
  154. $pathCmd[] = '/bin';
  155. $pathCmd[] = '/usr/bin';
  156. $pathCmd[] = '/usr/local/bin';
  157. $pathCmd[] = '/opt/local/bin';
  158. $pathCmd[] = '/sbin';
  159. $pathCmd[] = '/usr/sbin';
  160. $pathCmd[] = '/opt/local/sbin/skrypty';
  161. $pathCmd[] = '/opt/local/var/macports/software';
  162. $pathCmd[] = '/Applications/Server.app/Contents/ServerRoot/usr/bin';
  163. $pathCmd[] = '/Applications/Server.app/Contents/ServerRoot/usr/sbin';
  164. $cmd = 'export PATH=' . implode(':', $pathCmd) . "\n" .
  165. 'JAVA_HOME="/usr/bin/java"' . "\n" .
  166. 'MAVEN_OPTS="-Xms256m -Xmx512m"' . "\n" .
  167. $cmd;
  168. $useShellExec = true;
  169. if (User::isAdmin()) {
  170. echo UI::h('details', [], [
  171. UI::h('summary', [ 'style' => "padding:3px 8px;border:1px solid #ccc;background-color:#282c34;color:#de6b74;font-size:small" ], "DBG: command"),
  172. UI::h('div', [ 'style' => "padding:12px; background:#282c34;" ], [
  173. UI::h('pre', [], $cmd),
  174. ]),
  175. ]);
  176. }
  177. if ($useShellExec) $out = shell_exec($cmd);
  178. else V::exec($cmd, $out, $ret);
  179. DBG::log([ 'msg'=>"cmd and returns({$ret})", 'output'=>$out, 'cmd'=>str_replace(APP_PATH_ROOT, 'SE', $cmd) ]);
  180. if (empty($out)) throw new Exception("Empty output");
  181. if ($useShellExec) $out = explode("\n", $out);
  182. $outputType = 'HTML';
  183. $html = []; $startRead = false;
  184. foreach ($out as $line) {
  185. if (!$startRead) {
  186. if ('OUTPUT__TYPE__XML' == $line) $outputType = 'XML';
  187. if ('OUTPUT__TYPE__HTML' == $line) $outputType = 'HTML';
  188. if ('OUTPUT__START' == $line) {
  189. $startRead = true;
  190. continue;
  191. }
  192. } else {
  193. if ('<!DOCTYPE' == substr($line, 0, strlen('<!DOCTYPE'))) continue;
  194. if ('OUTPUT__END' == $line) {
  195. break;
  196. }
  197. $html[]= $line;
  198. }
  199. }
  200. if (empty($html)) UI::alert('danger', "Empty output!");
  201. else {
  202. if ('XML' == $outputType) {
  203. echo UI::h('pre', [], htmlspecialchars(implode("\n", $html)));
  204. } else if ('HTML' == $outputType) {
  205. echo UI::h('div', ['class'=>"container", 'style'=>"padding:12px; border:1px solid #ddd"], $html);
  206. }
  207. }
  208. } catch (Exception $e) {
  209. UI::alert('danger', $e->getMessage());
  210. DBG::log($e);
  211. }
  212. UI::endContainer();
  213. UI::dol();
  214. }
  215. }