Install.php 14 KB

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