PageRenderTime 43ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Pn/Main.php

http://phpnow.googlecode.com/
PHP | 569 lines | 478 code | 68 blank | 23 comment | 43 complexity | 4c640b63c66f358691a95d8e463b741a MD5 | raw file
  1. <?php
  2. /*
  3. http://phpnow.org
  4. YinzCN_at_Gmail.com
  5. */
  6. error_reporting(E_ALL);
  7. // variables
  8. $PnDir = getcwd();
  9. $sysroot = env('SystemRoot');
  10. $htd_dir = env('htd_dir');
  11. $php_dir = env('php_dir');
  12. $htd_cfg = $htd_dir.'\conf\httpd.conf';
  13. $vhs_cfg = env('vhs_cfg');
  14. $php_ini = $php_dir.'\php-apache2handler.ini';
  15. // Load
  16. if (count($argv) > 1) {
  17. $a = implode(' ', $argv);
  18. $a = substr($a, strlen($argv[0]) + 1);
  19. $a = str_replace('`', '"', $a);
  20. eval($a);
  21. }
  22. exit;
  23. // end of Load
  24. // ??(?? $m, ???? $n);
  25. function quit($m, $n) {
  26. echo "\r\n ".$m."\r\n";
  27. exit($n);
  28. }
  29. function env($n) { return getenv($n); }
  30. function rpl($a, $b, $c) { return str_replace($a, $b, $c);}
  31. // ???? preg_replace()
  32. function regrpl($p, $r, $s) {
  33. $p = '/'.$p.'/im';
  34. $s = preg_replace($p, $r, $s);
  35. if ($s === NULL) quit('regrpl(): ??! ????????.', 1);
  36. return $s;
  37. }
  38. // ??????? $fn ???
  39. function rfile($fn) {
  40. if (file_exists($fn)) {
  41. $handle = fopen($fn, 'r');
  42. $c = fread($handle, filesize($fn));
  43. fclose($handle);
  44. return $c;
  45. } else {
  46. quit('?? '.$fn.' ???', 1);
  47. }
  48. }
  49. // ???? $c ??? $fn
  50. function wfile($fn, $c) {
  51. if (!is_writable($fn) && file_exists($fn))
  52. quit('?? '.$fn.' ???', 1);
  53. else {
  54. $handle = fopen($fn, 'w');
  55. if (fwrite($handle, $c) === FALSE)
  56. quit('???? '.$fn.' ??', 1);
  57. fclose($handle);
  58. }
  59. }
  60. // ???? $a ? $b
  61. function cp($a, $b) {
  62. $c = rfile($a);
  63. wfile($b, $c);
  64. }
  65. // ??? $fn ??????? $p ??? $r
  66. function frpl($fn, $p, $r) {
  67. global $htd_dir, $php_dir, $htd_cfg, $vhs_cfg, $php_ini;
  68. $s = rfile($fn);
  69. $p = '/'.$p.'/im';
  70. $s = preg_replace($p, $r, $s);
  71. wfile($fn, $s);
  72. }
  73. function chk_path($path) {
  74. $str = regrpl('[^\x80-\xff]+', '', $path);
  75. if (!$str) exit(0);
  76. echo "\r\n # ?????????????: ".$str."\r\n";
  77. echo "\r\n # ?? Apache + PHP ???????.";;
  78. echo "\r\n # ???????????????.\r\n";;
  79. exit(1);
  80. }
  81. // check port
  82. // ????????. netstat + tasklist
  83. function chk_port($port) {
  84. $s = shell_exec('netstat.exe -ano');
  85. $tok = strtok($s, ' ');
  86. $pid = NULL;
  87. while ($tok) {
  88. if ($tok == '0.0.0.0:'.$port) {
  89. for ($i=3; $i; $i--)
  90. $pid = rtrim(strtok(' '));
  91. if (is_numeric($pid))
  92. break;
  93. }
  94. $tok = strtok(' ');
  95. }
  96. $task = NULL;
  97. if (is_numeric($pid)) {
  98. $lst = array(
  99. 'WebThunder.exe' => 'Web ??',
  100. 'inetinfo.exe' => 'IIS',
  101. 'Thunder5.exe' => '??5',
  102. 'httpd.exe' => 'Apache 2.2',
  103. 'mysqld-nt.exe' => 'MySQL',
  104. 'mysqld.exe' => 'MySQL');
  105. $s = shell_exec('tasklist.exe /fi "pid eq '.$pid.'" /nh');
  106. $task = trim(strtok($s, ' '));
  107. $d = ' ';
  108. if (isset($lst[$task]))
  109. $d = ' "'.$lst[$task].'" ';
  110. quit(' ?? '.$port.' ??'.$d.'('.$task.' PID '.$pid.') ??!', 1);
  111. }
  112. }
  113. // change httpd port
  114. function chg_port($newport) {
  115. global $htd_cfg, $vhs_cfg;
  116. if (file_exists($htd_cfg)) {
  117. $c = rfile($htd_cfg);
  118. $c = regrpl('^([ \t]*Listen[ \t]+[^:]+):\d+(\r\n)', '$1:'.$newport.'$2', $c);
  119. $c = regrpl('^([ \t]*Listen)[ \t]+\d+(\r\n)', '$1 '.$newport.'$2', $c);
  120. $c = regrpl('^([ \t]*ServerName[ \t]+[^:]+):\d+(\r\n)', '$1:'.$newport.'$2', $c);
  121. wfile($htd_cfg, $c);
  122. }
  123. if (file_exists($vhs_cfg)) frpl($vhs_cfg, '(ServerName[ \t]+[^:]+):\d+', '$1:'.$newport);
  124. frpl('Pn/Config.cmd', '^(set htd_port)=\d+(\r\n)', '$1='.$newport.'$2');
  125. }
  126. // update config
  127. function upcfg() {
  128. global $htd_dir, $php_dir, $htd_cfg, $vhs_cfg, $php_ini, $PnDir, $sysroot;
  129. // php.ini
  130. $str = rfile($php_ini);
  131. $str = regrpl('[A-Z]:\\\\[^\\\\\r\n]+(\\\\Temp)', $sysroot.'$1', $str);
  132. $str = regrpl('(\.\.\\\\\.\.\\\\)[^\\\\]+(\\\\ext\\\\)', '$1'.$php_dir.'$2', $str);
  133. $str = regrpl('^(extension_dir)[ \t]+=[ \t]+"[^"]+"', '$1 = "..\\..\\'.$php_dir.'\\ext"', $str);
  134. wfile($php_ini, $str);
  135. // httpd.conf
  136. $str = rfile($htd_cfg);
  137. $str = regrpl('(php5_module "\.\.\/)[^\/]+(\/[^\/]+\.dll")', '$1'.$php_dir.'$2', $str);
  138. $str = regrpl('(PHPIniDir "\.\.\/)[^\/]+(\/")', '$1'.$php_dir.'$2', $str);
  139. wfile($htd_cfg, $str);
  140. // httpd-vhosts.conf
  141. $str = rfile($vhs_cfg);
  142. $str = regrpl('(open_basedir ")[^;]+(\\\\vhosts\\\\[^;]+;)', '$1'.$PnDir.'$2', $str);
  143. $str = regrpl('[A-Z]:\\\\[^\\\\]+\\\\Temp;', $sysroot.'\Temp;', $str);
  144. wfile($vhs_cfg, $str);
  145. }
  146. function apache_log_default() {
  147. global $htd_cfg;
  148. $str = rfile($htd_cfg);
  149. $str = regrpl('\r\nLogFormat.*comonvhost\r\nCustomLog.*comonvhost\r\n', '', $str);
  150. $str = regrpl('##(CustomLog.*common\r\n)', '$1', $str);
  151. wfile($htd_cfg, $str);
  152. }
  153. function apache_log_pipe() {
  154. global $htd_cfg;
  155. $str = rfile($htd_cfg);
  156. $str = regrpl('^([ ]*)(CustomLog.*common\r\n)', '$1##$2', $str);
  157. $str = regrpl('\r\nLogFormat.*comonvhost\r\nCustomLog.*comonvhost\r\n', '', $str);
  158. $str .= '
  159. LogFormat "%v %h %l %u %t \"%r\" %>s %b" comonvhost
  160. CustomLog "|bin/rotatelogs.exe logs/%Y-%m-%d_access_log 16M" comonvhost
  161. ';
  162. wfile($htd_cfg, $str);
  163. }
  164. function apache_log_dis() {
  165. global $htd_cfg;
  166. $str = rfile($htd_cfg);
  167. $str = regrpl('\r\nLogFormat.*comonvhost\r\nCustomLog.*comonvhost\r\n', '', $str);
  168. $str = regrpl('^[ ]*(CustomLog.*common\r\n)', '##$1', $str);
  169. wfile($htd_cfg, $str);
  170. }
  171. function vProxy_add($hName, $hAlias, $hPass)
  172. {
  173. global $htd_cfg, $vhs_cfg;
  174. frpl($htd_cfg, '^#(Load.*proxy_mod.*\r\n)', '$1');
  175. frpl($htd_cfg, '^#(Load.*proxy_http.*\r\n)', '$1');
  176. if (!$hAlias) $hAlias = '*.'.$hName;
  177. $str = rfile($vhs_cfg);
  178. $str.= '
  179. <VirtualHost *>
  180. ServerName '.$hName.':'.env('htd_port').'
  181. ServerAlias '.$hAlias.'
  182. ProxyPass / http://'.$hPass.'/
  183. ProxyPassReverse / http://'.$hPass.'/
  184. </VirtualHost>
  185. ';
  186. wfile($vhs_cfg, $str);
  187. }
  188. function vProxy_dis()
  189. {
  190. global $htd_cfg, $vhs_cfg;
  191. frpl($htd_cfg, '^(Load.*proxy_mod.*\r\n)', '#$1');
  192. frpl($htd_cfg, '^(Load.*proxy_http.*\r\n)', '#$1');
  193. $str = rfile($vhs_cfg);
  194. $str = rpl("\r\n", "\n", $str);
  195. $str = regrpl('\n?<VirtualHost \*>[^<]*ProxyPass [^<]*<\/VirtualHost>\n?', '', $str);
  196. $str = rpl("\n", "\r\n", $str);
  197. wfile($vhs_cfg, $str);
  198. }
  199. // ??????: $hn ???, $htdocs ????, $port ??, $hAlias ??, $lt ???? open_basedir
  200. function vhost_add($hn, $htdocs, $port, $hAlias, $lt) {
  201. global $vhs_cfg, $PnDir, $sysroot;
  202. $htdocs = trim($htdocs);
  203. $hn = trim($hn);
  204. if (regrpl('[\d]+', '', $hn) === '') quit(' # ?????????!', 1);
  205. if ($tmp = regrpl('[a-z0-9\.-]+', '', $hn)) quit(' # ????????? "'.$tmp.'"', 1);
  206. if ($port < 1 || $port > 65535) exit;
  207. $str = rfile($vhs_cfg);
  208. if (strpos($str, 'ServerName '.$hn.':')) quit(' # ??????!', 1);
  209. if (!$hAlias) $hAlias = '*.'.$hn;
  210. if (!$htdocs) {
  211. if (!file_exists('vhosts')) mkdir('vhosts');
  212. $tmp = 'vhosts/'.$hn;
  213. @mkdir($tmp);
  214. $htdocs = '../'.$tmp;
  215. $vhDir = $PnDir.'\\vhosts\\'.$hn;
  216. copy($PnDir.'/Pn/pn_info.ph_', $PnDir.'/'.$tmp.'/index.php');
  217. $vhDir_Cfg = '
  218. <Directory "'.$htdocs.'">
  219. Options -Indexes FollowSymLinks
  220. </Directory>';
  221. } else {
  222. $tmp = getcwd();
  223. chdir($htdocs);
  224. $vhDir = getcwd();
  225. chdir($tmp);
  226. $htdocs = rpl("\\", '/', $vhDir);
  227. if (!file_exists($htdocs.'/index.php'))
  228. copy($PnDir.'/Pn/pn_info.ph_', $htdocs.'/index.php');
  229. $vhDir_Cfg = '
  230. <Directory "'.$htdocs.'">
  231. Options -Indexes FollowSymLinks
  232. Allow from all
  233. AllowOverride All
  234. </Directory>';
  235. }
  236. $o_bdir = '';
  237. if (!($lt=='n' || $lt=='N'))
  238. $o_bdir = "\r\n".' php_admin_value open_basedir "'.$vhDir.';'.$sysroot.'\Temp;"';
  239. $str .= '
  240. <VirtualHost *>'.$vhDir_Cfg.'
  241. ServerAdmin admin@'.$hn.'
  242. DocumentRoot "'.$htdocs.'"
  243. ServerName '.$hn.':'.$port.'
  244. ServerAlias '.$hAlias.'
  245. ErrorLog logs/'.$hn.'-error_log'.$o_bdir.'
  246. </VirtualHost>
  247. ';
  248. wfile($vhs_cfg, $str);
  249. }
  250. // ??????
  251. function vhost_mod($vh, $n_hA, $hIndex) {
  252. global $htd_cfg, $vhs_cfg;
  253. $str = rfile($vhs_cfg);
  254. $Vhs = rvhs($str);
  255. // ???? $n ???? $vh
  256. if (regrpl('[0-9]+', '', $vh) === '') {
  257. $n = $vh;
  258. if (!isset($Vhs[$n])) quit(' # ?????? '.$n.' ?????!', 1);
  259. $vh = cuts($Vhs[$n], 'ServerName ', ':');
  260. } else {
  261. foreach ($Vhs as $i => $tmp)
  262. if (strpos($tmp, 'ServerName '.$vh.':'))
  263. $n = $i;
  264. if (!isset($n)) quit(' # ????? "'.$vh.'" ?????!', 1);
  265. }
  266. // ?????? (ServerAlias)
  267. $n_hA = trim($n_hA);
  268. if ($n_hA) {
  269. $hA = cuts($Vhs[$n], 'ServerAlias ', "\n"); // ????? ServerAlias
  270. if (substr_count($n_hA, '+'))
  271. $n_hA = rpl('+', ' '.$hA.' ', $n_hA);
  272. $n_hA = trim(regrpl('[ \t]+', ' ', $n_hA));
  273. $str = regrpl('(ServerAlias)[ \t]+'.quotemeta($hA)."(\r\n)", '$1 '.$n_hA.'$2', $str);
  274. }
  275. // ????????
  276. if (strpos($Vhs[$n], '../vhosts/') === FALSE) {
  277. $d_str = rpl("\r\n", '!', $str);
  278. $d_str = regrpl('.*(<VirtualHost [^<]+ServerName '.$vh.':[^<]+<\/VirtualHost>).*', '$1', $d_str);
  279. $vhDir = regrpl('.*DocumentRoot "([^!]+)"!.*', '$1', $d_str);
  280. $vhDir = rpl('/', '\/', $vhDir);
  281. $hIndex = strtolower($hIndex);
  282. if ($hIndex === 'y') {
  283. $hIndex_1 = '-';
  284. $hIndex_2 = '';
  285. $htd_cfg_str = rfile($htd_cfg);
  286. $htd_cfg_str = regrpl('^##(LoadModule.*autoindex.*\.so)', '$1', $htd_cfg_str);
  287. $htd_cfg_str = regrpl('^##?(Include.*autoindex.*)', '$1', $htd_cfg_str);
  288. wfile($htd_cfg, $htd_cfg_str);
  289. } elseif ($hIndex === 'n') {
  290. $hIndex_1 = '';
  291. $hIndex_2 = '-';
  292. }
  293. if (isset($hIndex_1)) {
  294. $str = regrpl('(\r\n<Directory "'.$vhDir.'">[^<]+Options )'.$hIndex_1.'(Indexes[^<]+<\/Directory>\r\n)', '$1'.$hIndex_2.'$2', $str);
  295. }
  296. }
  297. wfile($vhs_cfg, $str);
  298. }
  299. // ??????
  300. function vhost_del($vh) {
  301. global $vhs_cfg;
  302. $str = rfile($vhs_cfg);
  303. $Vhs = rvhs($str);
  304. // ???? $n ???? $vh
  305. if (regrpl('[0-9]+', '', $vh) === '') {
  306. $n = $vh;
  307. if (!isset($Vhs[$n])) quit(' # ?????? '.$n.' ?????!', 1);
  308. $vh = cuts($Vhs[$n], 'ServerName ', ':');
  309. } else {
  310. foreach ($Vhs as $i => $tmp)
  311. if (strpos($tmp, 'ServerName '.$vh.':'))
  312. $n = $i;
  313. if (!isset($n)) quit(' # ????? "'.$vh.'" ?????!', 1);
  314. }
  315. if ($vh == 'default')
  316. quit(' # ????????!', 1);
  317. $vhDir = cuts($Vhs[$n], 'DocumentRoot "', '"');
  318. $vhDir = rpl('/', '\/', $vhDir);
  319. $str = rpl("\r\n", "\n", $str);
  320. $str = regrpl('<(\/?Directory[^>]*)>', '[$1]', $str);
  321. $str = regrpl('\n?<VirtualHost[^<]+ServerName[ \t]+'.$vh.':[^<]+<\/VirtualHost>\n?', '', $str);
  322. $str = regrpl('\[(\/?Directory[^\]]*)\]', '<$1>', $str);
  323. $str = rpl("\n", "\r\n", $str);
  324. wfile($vhs_cfg, $str);
  325. quit(' # ???? "'.$vh.'" ???!', 0);
  326. }
  327. function showvhs() {
  328. global $vhs_cfg;
  329. $Vhs = rvhs(rfile($vhs_cfg));
  330. $str = '';
  331. for ($i=0; $i<count($Vhs); $i++) {
  332. $vh = str_pad(cuts($Vhs[$i], 'ServerName ', ':'), 18).'| ';
  333. $vh .= cuts($Vhs[$i], 'ServerAlias ', "\n");
  334. $P = cuts($Vhs[$i], 'DocumentRoot "', "\"\n");
  335. if (!$P) {
  336. $P = cuts($Vhs[$i], 'ProxyPass / http://', "/\n");
  337. if ($P) $P = '~'.$P;
  338. }
  339. else {
  340. $P = rpl('../vhosts/', 'vhosts/', $P);
  341. }
  342. if ($P) $vh = str_pad($vh, 42).' | '.str_pad($P, ' ', 20);
  343. $vh = str_pad($vh, (strlen($vh) < 71) ? 70 : 150).'|';
  344. $str .= ' |'.str_pad($i, 3, ' ', STR_PAD_LEFT).' | '.$vh."\r\n";
  345. }
  346. echo ' '.str_repeat('-', 78)."\r\n";
  347. echo ' | No.| ServerName ??? | ServerAlias ???? | ???? / ~???? |'."\r\n";
  348. echo ' '.str_repeat('-', 78)."\r\n";
  349. echo $str;
  350. echo ' '.str_repeat('-', 78);
  351. echo "\r\n";
  352. }
  353. // ?? $str ????????, ????
  354. function rvhs($str) {
  355. $Vhs = array();
  356. $str = regrpl('\s*\n\s*', "\n", $str);
  357. $str = regrpl('[ \t]+', ' ', $str);
  358. for ($i=0; $str=strstr($str, "\n<Vir"); $i++) {
  359. $p = strpos($str, "\n</Vir") + 14;
  360. $Vhs[$i] = substr($str, 1, $p);
  361. $str = substr($str, $p);
  362. }
  363. return $Vhs;
  364. }
  365. // ?? $str ?? $a ??, $z ??????
  366. function cuts($str, $a, $z) {
  367. $p0 = strpos($str, $a);
  368. if ($p0 === FALSE) return $p0;
  369. $p1 = strlen($a) + $p0;
  370. $p2 = strpos($str, $z, $p1);
  371. return substr($str, $p1, $p2 - $p1);
  372. }
  373. // check mysql connection
  374. function chk_mysql($port, $pwd) {
  375. dl('php_mysql.dll');
  376. for($n=0; $n<3; $n++) {
  377. $link = @mysql_connect('localhost:'.$port, 'root', $pwd);
  378. if ($link) {
  379. mysql_close($link);
  380. exit();
  381. }
  382. $errno = mysql_errno();
  383. if ($errno === 1045) exit($errno);
  384. echo ' # ???? MySQL, ???...'."\r\n";
  385. sleep(2);
  386. }
  387. exit($errno);
  388. }
  389. // ?????? / ??
  390. function cfg_bak($Arg) {
  391. global $htd_cfg, $vhs_cfg, $php_ini;
  392. $Arg = explode(' ', $Arg);
  393. dl('php_zip.dll');
  394. $Files = array(
  395. $htd_cfg,
  396. $vhs_cfg,
  397. $php_ini,
  398. env('myd_dir').'/my.ini',
  399. 'Pn/config.cmd');
  400. $zipfile = env('cfg_bak_zip');
  401. $zip = new ZipArchive;
  402. $zip->open($zipfile, ZIPARCHIVE::CREATE);
  403. if (!$zip->locateName($tmp = 'PHPnow_config_backup'))
  404. $zip->addFromString($tmp, '');
  405. // get Entries
  406. $Entries = array();
  407. for ($i=0; $i<$zip->numFiles; $i++)
  408. $Entries[$i] = $zip->getNameIndex($i);
  409. // get BakDirs
  410. $BakDirs = array();
  411. foreach ($Entries As $e) {
  412. if ($p = strpos($e, '/')) {
  413. $bakDir = substr($e, 0, $p);
  414. if (!in_array($bakDir, $BakDirs))
  415. array_push($BakDirs, $bakDir);
  416. }
  417. }
  418. // ????
  419. if ($Arg[0] === 'backup') {
  420. $bakDir = $Arg[1].'_'.gmdate('YmdHi', strtotime('+8 hour'));
  421. $tmp = $bakDir;
  422. for ($i=1; in_array($tmp.'/', $BakDirs); $i++)
  423. $tmp = $bakDir.'_'.$i;
  424. $bakDir = $tmp;
  425. foreach ($Files as $fn) {
  426. if (file_exists($fn))
  427. $zip->addFile($fn, $bakDir.'/'.basename($fn));
  428. }
  429. echo ' ?????? '.$zipfile.' -> '.$bakDir."\r\n";
  430. }
  431. // ??????
  432. if ($Arg[0] === 'restore') {
  433. $bakDir = $BakDirs[$Arg[1]];
  434. if ($bakDir) {
  435. foreach ($Files as $fn) {
  436. $c = $zip->getFromName($bakDir.'/'.basename($fn));
  437. if ($c) wfile($fn, $c);
  438. }
  439. }
  440. }
  441. // ????
  442. if ($Arg[0] === 'show') {
  443. if (count($BakDirs)) {
  444. foreach ($BakDirs as $n => $bakDir) {
  445. echo ' '.$n.' - '.$bakDir."\r\n";
  446. }
  447. } else {
  448. echo " **???????**\r\n";
  449. }
  450. }
  451. // ????
  452. if ($Arg[0] === 'delete') {
  453. $n = $Arg[1];
  454. if (isset($BakDirs[$n])) {
  455. $bakDir = $BakDirs[$n];
  456. foreach ($Entries As $e) {
  457. if (substr($e, 0, strlen($bakDir)) === $bakDir)
  458. $zip->deleteName($e);
  459. }
  460. echo ' ?? '.substr($bakDir, 0, -1).' ???';
  461. } else {
  462. echo(' ????! ?????? '.$n.' ??');
  463. }
  464. }
  465. $zip->close();
  466. }
  467. ?>