Install.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_Install extends RouteBase {
  4. public function handleAuth() {
  5. if (!User::logged()) {
  6. throw new HttpException('Unauthorized', 401);
  7. }
  8. if (!User::isAdmin()) {
  9. throw new HttpException('Unauthorized - only for administrators', 401);
  10. }
  11. }
  12. public function defaultAction() {
  13. SE_Layout::gora();
  14. SE_Layout::menu();
  15. $this->menu();
  16. SE_Layout::dol();
  17. }
  18. private function menu() {
  19. $serversList = $this->fetchActiveLicences();
  20. ?>
  21. <div class="jumbotron">
  22. <div class="container">
  23. <form class="form-inline" method="POST">
  24. <input type="hidden" name="_task" value="createApp" />
  25. <label>Przygotuj kod źródłowy do aktualizacji na serwerze klienta:</label>
  26. <select class="form-control" name="licence_id">
  27. <?php foreach ($serversList as $srv) : ?>
  28. <option value="<?php echo $srv->ID; ?>">[<?php echo $srv->ID; ?>] <?php echo $srv->domain; ?></option>
  29. <?php endforeach; ?>
  30. </select>
  31. <button type="submit" id="fldSbmtBtn" class="btn btn-primary" autocomplete="off">
  32. Generuj
  33. </button>
  34. </form>
  35. </div>
  36. </div>
  37. <script type="text/javascript">
  38. jQuery(document).ready(function () {
  39. jQuery('#fldSbmtBtn').on('click', function () {
  40. jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
  41. jQuery(this).parent().submit();
  42. })
  43. });
  44. </script>
  45. <?php
  46. }
  47. public function createAppAction() {
  48. $args = array();
  49. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  50. SE_Layout::gora();
  51. SE_Layout::menu();
  52. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  53. try {
  54. $appLicenceInfo = $this->_fetchAppLicenceInfo($args['licence_id']);
  55. } catch (Exception $e) {
  56. $this->_endWithException($e);
  57. }
  58. //$this->generateApp($args['licence_id']);
  59. ?>
  60. <div class="jumbotron">
  61. <div class="container">
  62. <h3>Generowanie aplikacji dla licencji <?php echo $appLicenceInfo->ID; ?></h3>
  63. <p>Licencja dla domen: <?php echo implode(', ', $appLicenceInfo->domains); ?></p>
  64. <p>Katalog z zakodowanymi plikami: <?php echo $appLicenceInfo->installFolderName; ?></p>
  65. <form class="form-inline" method="POST">
  66. <input type="hidden" name="_task" value="createApp" />
  67. <input type="hidden" name="licence_id" value="<?php echo $appLicenceInfo->ID; ?>" />
  68. <input type="hidden" name="_generateEncryptedSource" value="1" />
  69. <button type="submit" id="fldSbmtBtn" class="btn btn-primary" autocomplete="off">
  70. Generuj
  71. </button>
  72. </form>
  73. <?php if ($appLicenceInfo->installFolderGitExists) : ?>
  74. <br>
  75. <div class="alert alert-info">
  76. Katalog istnieje i zawiera już repozytorium git:
  77. <br> - <a href="index.php?_route=Install&_task=gitResetHard&licence_id=<?php echo $appLicenceInfo->ID; ?>"
  78. target="_blank"
  79. class="btn btn-xs btn-default">aktualizuj werjsę (git reset --hard, git pull, set SE/VERSION)</a>
  80. <br> - <a href="index.php?_route=Install&_task=encodeSource&licence_id=<?php echo $appLicenceInfo->ID; ?>"
  81. target="_blank"
  82. class="btn btn-xs btn-default">encode files</a>
  83. <br> send files:
  84. <pre>
  85. ssh server@biuro.galeriaprzymorze.eu 'rm -rf ~/se.encrypted.upgrade/SE'
  86. # scp -r SE server@biuro.galeriaprzymorze.eu:~/se.encrypted.upgrade/
  87. rsync --archive --verbose --update --times --compress --one-file-system --omit-dir-times --no-g --no-perms SE/ server@biuro.galeriaprzymorze.eu:~/se.encrypted.upgrade/SE/
  88. ssh server@biuro.galeriaprzymorze.eu 'ln -s /Library/Server/Web/Data/Sites/Default/SE/config ~/se.encrypted.upgrade/SE/config'
  89. ssh server@biuro.galeriaprzymorze.eu 'ln -s ~/se.encrypted.upgrade/SE /Library/Server/Web/Data/Sites/Default/se.encrypted.upgrade'
  90. https://biuro.galeriaprzymorze.eu/se.encrypted.upgrade/
  91. </pre>
  92. </ul>
  93. </div>
  94. <?php endif; ?>
  95. </div>
  96. </div>
  97. <script type="text/javascript">
  98. jQuery(document).ready(function () {
  99. jQuery('#fldSbmtBtn').on('click', function () {
  100. jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
  101. jQuery(this).parent().submit();
  102. })
  103. });
  104. </script>
  105. <?php
  106. $generateEncryptedSource = (1 == V::get('_generateEncryptedSource', 0, $_REQUEST, 'int'));
  107. if ($generateEncryptedSource) {
  108. echo '<div class="container">';
  109. echo '<h4>' . "Generowanie..." . '</h4>';
  110. echo '<div style="border:1px solid silver; max-height:400px; overflow-y:scroll">';
  111. try {
  112. $this->generateApp($appLicenceInfo);
  113. } catch (Exception $e) {
  114. echo '</div></div>';// .container/ scroll
  115. $this->_endWithException($e);
  116. }
  117. echo '</div>';// .container
  118. ?>
  119. <div class="alert alert-success">
  120. <strong>Gotowe</strong> Aplikacja znajduje się w katalogu <?php echo $appLicenceInfo->installFolderName; ?>
  121. </div>
  122. <?php
  123. }
  124. SE_Layout::dol();
  125. }
  126. public function gitResetHardAction() {
  127. $args = array();
  128. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  129. SE_Layout::gora();
  130. SE_Layout::menu();
  131. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  132. try {
  133. $appLicenceInfo = $this->_fetchAppLicenceInfo($args['licence_id']);
  134. $this->_gitResetHard($appLicenceInfo);
  135. } catch (Exception $e) {
  136. $this->_endWithException($e);
  137. }
  138. SE_Layout::dol();
  139. }
  140. public function _gitResetHard($appLicenceInfo) {
  141. if (empty($appLicenceInfo->ID)) throw new Exception("Nie wybrano serwera/licencji.");
  142. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found");
  143. $installPath = $appLicenceInfo->installPath;
  144. if (empty($installPath)) throw new Exception("Install path not found");
  145. $cmds = array();
  146. $cmds[] = "cd {$installPath} && git reset --hard";
  147. $cmds[] = "cd {$installPath} && git pull";
  148. $cmds[] = "cd {$installPath} && echo `git show-ref --head|head -1|head -c 8` > SE/VERSION ";
  149. foreach ($cmds as $cmd) {
  150. $out = ''; $ret = '';
  151. exec($cmd, $out, $ret);
  152. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  153. }
  154. }
  155. public function encodeSourceAction() {
  156. $args = array();
  157. $args['licence_id'] = V::get('licence_id', 0, $_REQUEST, 'int');
  158. SE_Layout::gora();
  159. SE_Layout::menu();
  160. //$this->menu($args['licence_id']);// TODO: GO BACK BTN
  161. try {
  162. $appLicenceInfo = $this->_fetchAppLicenceInfo($args['licence_id']);
  163. $this->_encodeSource($appLicenceInfo);
  164. } catch (Exception $e) {
  165. $this->_endWithException($e);
  166. }
  167. SE_Layout::dol();
  168. }
  169. public function _encodeSource($appLicenceInfo) {
  170. if (empty($appLicenceInfo->ID)) throw new Exception("Nie wybrano serwera/licencji.");
  171. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found");
  172. $installPath = $appLicenceInfo->installPath;
  173. if (empty($installPath)) throw new Exception("Install path not found");
  174. $phpVersion = '5.5';
  175. if ('1' == V::get('DBG_ENCODER_HELP', '', $_REQUEST)) {// encoder help
  176. $cmd = "cd {$installPath}/SE && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder --help ";
  177. $out = ''; $ret = '';
  178. exec($cmd, $out, $ret);
  179. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  180. exit;
  181. }
  182. {
  183. $phpFiles = array();
  184. $skipPhpFiles = array();
  185. $skipPhpFiles[] = 'bash_sync_perms.php';
  186. $skipPhpFiles[] = '.config.php';
  187. $skipPhpFiles[] = 'se-lib/V.php';
  188. $skipPhpFiles[] = 'se-lib/Lib.php';
  189. $skipPhpFiles[] = 'se-lib/DB.php';
  190. $skipPhpFiles[] = 'se-lib/User.php';
  191. $skipPhpFiles[] = 'se-lib/UserProfile.php';
  192. $skipPhpFiles[] = 'se-lib/Config.php';
  193. $skipPhpFiles[] = 'se-lib/Config/INI.php';
  194. $skipPhpFiles[] = 'se-lib/FoldersConfig.php';
  195. $skipPhpFiles[] = 'se-lib/FileUploader.php';
  196. $skipPhpFiles[] = 'se-lib/Route/Budget.php';
  197. $skipPhpFiles[] = 'se-lib/Route/FixCrmProcesInitIdx.php';
  198. $skipPhpFiles[] = 'se-lib/Route/FixProjectPath.php';
  199. $skipPhpFiles[] = 'se-lib/Route/FixZasobPath.php';
  200. //$skipPhpFiles[] = 'se-lib/Route/Install.php';
  201. $skipPhpFiles[] = 'se-lib/Route/Msgs.php';
  202. $skipPhpFiles[] = 'superedit-DB_PROCEDURES_CREATE.php';
  203. //$skipPhpFiles[] = '';
  204. $cmd = "cd {$installPath}/SE && find . -name '*.php' ";
  205. $out = ''; $ret = '';
  206. exec($cmd, $out, $ret);
  207. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  208. if (0 !== $ret) throw new Exception("Error at find php files");
  209. if (empty($out)) throw new Exception("No php files found");
  210. foreach ($out as $phpFilePath) {
  211. $phpFilePath = ('./' == substr($phpFilePath, 0, 2))? substr($phpFilePath, 2) : $phpFilePath;
  212. if (in_array($phpFilePath, $skipPhpFiles)) continue;
  213. if ('schema/' == substr($phpFilePath, 0, 7)) continue;
  214. $phpFiles[] = $phpFilePath;
  215. }
  216. DBG::_(true, true, 'phpFiles', $phpFiles);
  217. if (empty($phpFiles)) throw new Exception("No php files to encode");
  218. }
  219. {
  220. $cmd = "cd {$installPath}/SE && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder --phpversion {$phpVersion} -b- ";
  221. foreach ($appLicenceInfo->domains as $domain) {
  222. $cmd .= " --domain {$domain} ";
  223. }
  224. $cmd .= " " . implode(" ", $phpFiles);
  225. $out = ''; $ret = '';
  226. exec($cmd, $out, $ret);
  227. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  228. if (0 !== $ret) throw new Exception("Error at encode files");
  229. if (empty($out)) throw new Exception("No output for encode files command");
  230. }
  231. }
  232. public function _fetchAppLicenceInfo($idLicence) {
  233. $idLicence = intval($idLicence);
  234. if (empty($idLicence)) throw new Exception("Nie wybrano serwera/licencji.");
  235. //DBG::_(true, true, 'idLicence', $idLicence, __CLASS__, __FUNCTION__, __LINE__);
  236. $appLicenceInfo = new stdClass();
  237. $appLicenceInfo->ID = $idLicence;
  238. $appLicenceInfo->domains = $this->fetchDomainsByLicenceId($idLicence);
  239. $installRootPath = '/Library/Server/Web/Data/Sites/Default/PLIKI/SES_PROCESY5_A';
  240. $appLicenceInfo->installFolderName = "{$idLicence}_upgrade_SE_source_encrypted";
  241. $appLicenceInfo->installPath = "{$installRootPath}/{$appLicenceInfo->installFolderName}";
  242. //DBG::_(true, true, 'appLicenceInfo', $appLicenceInfo, __CLASS__, __FUNCTION__, __LINE__);
  243. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found.");
  244. $appLicenceInfo->installFolderExists = file_exists("{$appLicenceInfo->installPath}/SE");
  245. $appLicenceInfo->installFolderGitExists = file_exists("{$appLicenceInfo->installPath}/.git");
  246. return $appLicenceInfo;
  247. }
  248. public function generateApp($appLicenceInfo) {
  249. if (empty($appLicenceInfo->ID)) throw new Exception("Nie wybrano serwera/licencji.");
  250. if (empty($appLicenceInfo->domains)) throw new Exception("Domains not found");
  251. $installPath = $appLicenceInfo->installPath;
  252. if (empty($installPath)) throw new Exception("Install path not found");
  253. $cmds = array();
  254. $cmds[] = "if [ -d {$installPath} ] ; then rm -rf '{$installPath}'; fi";
  255. $cmds[] = "mkdir {$installPath}";
  256. $cmds[] = "cd {$installPath} && git clone git@biuro.biall-net.pl:plabudda/se.git .";
  257. $cmds[] = "cd {$installPath} && echo `git show-ref --head|head -1|head -c 8` > SE/VERSION ";
  258. //echo'<pre>cmds: ';print_r($cmds);echo'</pre>';
  259. foreach ($cmds as $cmd) {
  260. $out = ''; $ret = '';
  261. exec($cmd, $out, $ret);
  262. echo'<pre>cmd: '. $cmd . ': (return:'.$ret.')'."\n";print_r($out);echo'</pre>';
  263. }
  264. $this->_encodeSource($appLicenceInfo);
  265. // 1763: $exec='cd '.$installer_dir.' && /Applications/SourceGuardian.app/Contents/MacOS/sgencoder -b-
  266. // '.INSTALL_SES_PROCESY_A::get_same_domains_for_install($h->SERVER_ADDRESS_SHORT).'
  267. // -r *.php
  268. // -x superedit-DB_PROCEDURES_CREATE.php
  269. // -x INI.php
  270. // -x .config_base_structure.php
  271. // ';
  272. //
  273. // INSTALL_SES_PROCESY_A::get_same_domains_for_install($h->SERVER_ADDRESS_SHORT):
  274. // $res2=DB::query("select SERVER_ADDRESS_SHORT from SES_PROCESY5_A where SERVER_ADDRESS_IP='".$h->SERVER_ADDRESS_IP."'");
  275. // while($h2=DB::fetch($res2)) {
  276. // $domain[]=' --domain '.$h2->SERVER_ADDRESS_SHORT;
  277. // ssh server@biuro.galeriaprzymorze.eu: PHP 5.5.20
  278. // ssh server@biuro.biall-net.pl
  279. // cd /Users/plabudda/procesy5-install-galeriaprzymorze.eu/
  280. // sudo chown -R server:admin SE/
  281. // /Applications/SourceGuardian.app/Contents/MacOS/sgencoder --phpversion 5.5 -b- --domain galeriaprzymorze.eu -r SE/*.php SE/se-lib/*.php SE/se-lib/*/*.php SE/se-lib/*/*/*.php SE/procesy/*.php SE/odt2xhtml/*.php -x superedit-DB_PROCEDURES_CREATE.php -x INI.php -x .config_base_structure.php
  282. }
  283. public function fetchActiveLicences() {
  284. $activeLic = array();
  285. $db = DB::getDB();
  286. $sql = "select l.`ID`
  287. , l.`SERVER_ADDRESS`
  288. , l.`SERVER_ADDRESS_SHORT` as domain -- domain for sgencoder
  289. , l.`SERVER_ADDRESS_IP`
  290. from `SES_PROCESY5_A` l
  291. where 1=1
  292. -- TODO: and l.`A_STATUS` in('NORMAL','WAITING')
  293. order by l.`ID` DESC
  294. ";
  295. $res = $db->query($sql);
  296. while ($r = $db->fetch($res)) {
  297. $activeLic[$r->ID] = $r;
  298. }
  299. return $activeLic;
  300. }
  301. public function fetchDomainsByLicenceId($licenceId) {
  302. $domains = array();
  303. $db = DB::getDB();
  304. $sql = "select g.`SERVER_ADDRESS_SHORT`, g.`SERVER_ADDRESS`
  305. from `SES_PROCESY5_A` g
  306. where g.`SERVER_ADDRESS_IP`=(select l.`SERVER_ADDRESS_IP`
  307. from `SES_PROCESY5_A` l
  308. where l.`ID`='{$licenceId}'
  309. -- TODO: and l.`A_STATUS` in('NORMAL','WAITING')
  310. )
  311. -- TODO: and g.`A_STATUS` in('NORMAL','WAITING')
  312. ";
  313. $res = $db->query($sql);
  314. while ($r = $db->fetch($res)) {
  315. $domains[] = $r->SERVER_ADDRESS_SHORT;
  316. if ($r->SERVER_ADDRESS != $r->SERVER_ADDRESS_SHORT) {
  317. $domains[] = $r->SERVER_ADDRESS;
  318. }
  319. }
  320. return $domains;
  321. }
  322. public function _endWithException($e) {
  323. ?>
  324. <div class="container">
  325. <div class="alert alert-danger">
  326. <?php echo $e->getMessage(); ?>
  327. </div>
  328. <p>Wróć do <a href="index.php?_route=Install">menu</a></p>
  329. </div>
  330. <?php
  331. SE_Layout::dol();
  332. exit;
  333. }
  334. }