Ant.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. 'class' => 'btn btn-md btn-link',
  36. 'href' => $this->getLink('ant', [
  37. 'path' => $path,
  38. 'typeName' => $typeName,
  39. 'primaryKey' => $primaryKey,
  40. 'primaryKeyField' => $pkField
  41. ])
  42. ], "Uruchom '{$label}'")
  43. ]);
  44. $tmpls = $this->getAntUrlActionTemplates($path);
  45. if (!empty($tmpls)) {
  46. foreach ($tmpls as $tmpl => $labelTmpl) {
  47. echo UI::h('div', [ 'style' => "padding-left: 50px" ], [
  48. UI::h('a', [
  49. 'href' => $this->getLink('ant', [
  50. 'path' => $path,
  51. 'template' => $tmpl,
  52. 'typeName' => $typeName,
  53. 'primaryKey' => $primaryKey,
  54. 'primaryKeyField' => $pkField
  55. ]),
  56. 'class' => 'btn btn-sm btn-link',
  57. ], '<i class="glyphicon glyphicon-arrow-right"></i> ' . $labelTmpl)
  58. ]);
  59. }
  60. }
  61. }
  62. UI::endTag('ul');
  63. } catch (Exception $e) {
  64. UI::alert('danger', $e->getMessage());
  65. DBG::log($e);
  66. }
  67. UI::endContainer();
  68. UI::dol();
  69. }
  70. public function outputAction() {// index.php?_route=UrlAction_Ant&_task=output & path={$path} & file={$file}";
  71. $path = V::get('path', '', $_GET);
  72. if (!$path) throw new Exception("Missing Ant path!");
  73. $file = V::get('file', '', $_GET);
  74. if (!$file) throw new Exception("Missing Ant file!");
  75. // TODO: allow auth by token?
  76. throw new Exception("TODO: return output file '$file' from ant task '{$path}'");
  77. }
  78. public function antAction() {
  79. UI::gora();
  80. UI::startContainer();
  81. try {
  82. $path = V::get('path', '', $_GET);
  83. if (!$path) throw new Exception("Missing Ant path!");
  84. $typeName = V::get('typeName', '', $_GET);
  85. if (!$typeName) throw new Exception("Missing typeName");
  86. $primaryKey = V::get('primaryKey', '', $_GET);
  87. if (!$primaryKey) throw new Exception("Missing primaryKey");
  88. $pkField = V::get('primaryKeyField', '', $_GET);
  89. if (!$pkField) throw new Exception("Missing primaryKeyField");
  90. // list($nsPrefix, $objectName) = explode(':', $typeName);// TODO: get wfs prefix from typeName
  91. $parsedInfo = Core_AclHelper::parseTypeName($typeName);
  92. DBG::log($parsedInfo, '', "Ant parseTypeName({$typeName})");
  93. $nsPrefix = $parsedInfo['prefix'];
  94. $objectName = $parsedInfo['name'];
  95. $template = V::get('template', '', $_GET);
  96. // TODO: validate missing ...
  97. DBG::log($_GET, 'array', '$_GET');
  98. DBG::log([ 'msg'=>'$path', '$path'=>$path]);
  99. DBG::log([ 'msg'=>'$primaryKey', '$primaryKey'=>$primaryKey]);
  100. DBG::log([ 'msg'=>'$typeName', '$typeName'=>$typeName]);
  101. $taskList = $this->getAntUrlActionList();
  102. if (!array_key_exists($path, $taskList)) throw new Exception("Ant path not exists! '{$path}'");
  103. echo UI::h('h3', [], [
  104. "Druk",
  105. '<br>',
  106. UI::h('small', [], $taskList[$path])
  107. ]);
  108. echo UI::h('hr');
  109. $webRootUrl = Request::getPathUri() . "schema/ant-url_action/{$path}";// TODO: security - only for test
  110. $outputFunctionUrl = $this->getLink('output') . "&path={$path}&file=";
  111. DBG::log([ 'msg'=>'$webRootUrl', '$webRootUrl'=>$webRootUrl]);
  112. DBG::log([ 'msg'=>'$outputFunctionUrl', '$outputFunctionUrl'=>$outputFunctionUrl]);
  113. $testUrl = Request::getPathUri() . "wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$typeName}&SRSNAME=EPSG:3003&featureID={$objectName}.{$primaryKey}";// &REQUEST=GetFeature
  114. // $testUrl = Request::getPathUri() . "wfs-data.php" ;// &REQUEST=GetFeature
  115. $cryptedPass = base64_encode(User::getLogin() . ":" . Crypt::decrypt($_SESSION['ADM_PASS_HASH']));
  116. $uniqID = date("Y-m-d-H_i_s") . substr(md5(time()), 0, 6);// TODO: uniq id for every request
  117. // $cmd = "{$this->pathUrlActions}{$path}/do_build.sh";
  118. $cmd = "cd {$this->pathUrlActions}{$path} && ant -S";
  119. $cmd .= " -DoutputFunctionUrl='{$outputFunctionUrl}'";
  120. $cmd .= " -DwebRootUrl='{$webRootUrl}'";
  121. // $cmd .= " -Durl='{$testUrl}'"; nie mozna tego uzywac, bo nadpisuje property w funkcji build_get_wfs.xml
  122. $cmd .= " -DpasswordBase64Basic=\"{$cryptedPass}\"";
  123. $cmd .= " -Duuid=\"{$uniqID}\"";
  124. $cmd .= " -Dphp_session_id=\"" . session_id() . "\"";
  125. $cmd .= " -Dxpath={$pkField}";
  126. $cmd .= " -Dxpath_value={$primaryKey}";
  127. $cmd .= " -Dapi_url=".Request::getPathUri()."wfs-data.php"; //potrzebuje ten parametr do dzialania w AMS itp
  128. $cmd .= " -DtypeName=\"{$typeName}\"";
  129. if ($template) $cmd .= " {$template} ";
  130. $cmd .= " 2>&1";
  131. $pathCmd = [];
  132. $pathCmd[] = '/opt/local/bin/ant';
  133. $pathCmd[] = '/bin';
  134. $pathCmd[] = '/usr/bin';
  135. $pathCmd[] = '/usr/local/bin';
  136. $pathCmd[] = '/opt/local/bin';
  137. $pathCmd[] = '/sbin';
  138. $pathCmd[] = '/usr/sbin';
  139. $pathCmd[] = '/opt/local/sbin/skrypty';
  140. $pathCmd[] = '/opt/local/var/macports/software';
  141. $pathCmd[] = '/Applications/Server.app/Contents/ServerRoot/usr/bin';
  142. $pathCmd[] = '/Applications/Server.app/Contents/ServerRoot/usr/sbin';
  143. $cmd = 'export PATH=' . implode(':', $pathCmd) . "\n" .
  144. 'JAVA_HOME="/usr/bin/java"' . "\n" .
  145. 'MAVEN_OPTS="-Xms256m -Xmx512m"' . "\n" .
  146. $cmd;
  147. $useShellExec = true;
  148. if ($useShellExec) $out = shell_exec($cmd);
  149. else V::exec($cmd, $out, $ret);
  150. DBG::log([ 'msg'=>"cmd and returns({$ret})", 'output'=>$out, 'cmd'=>str_replace(APP_PATH_ROOT, 'SE', $cmd) ]);
  151. if (empty($out)) throw new Exception("Empty output");
  152. if ($useShellExec) $out = explode("\n", $out);
  153. $outputType = 'HTML';
  154. $html = []; $startRead = false;
  155. foreach ($out as $line) {
  156. if (!$startRead) {
  157. if ('OUTPUT__TYPE__XML' == $line) $outputType = 'XML';
  158. if ('OUTPUT__TYPE__HTML' == $line) $outputType = 'HTML';
  159. if ('OUTPUT__START' == $line) {
  160. $startRead = true;
  161. continue;
  162. }
  163. } else {
  164. if ('<!DOCTYPE' == substr($line, 0, strlen('<!DOCTYPE'))) continue;
  165. if ('OUTPUT__END' == $line) {
  166. break;
  167. }
  168. $html[]= $line;
  169. }
  170. }
  171. if (empty($html)) UI::alert('danger', "Empty output!");
  172. else {
  173. if ('XML' == $outputType) {
  174. echo UI::h('pre', [], htmlspecialchars(implode("\n", $html)));
  175. } else if ('HTML' == $outputType) {
  176. echo UI::h('div', ['class'=>"container", 'style'=>"padding:12px; border:1px solid #ddd"], $html);
  177. }
  178. }
  179. } catch (Exception $e) {
  180. UI::alert('danger', $e->getMessage());
  181. DBG::log($e);
  182. }
  183. UI::endContainer();
  184. UI::dol();
  185. }
  186. }