国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

476 lines
17KB

  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * 命令行工具
  5. *
  6. * @version 2020年12月11日 tianya $
  7. * @package DedeBIZ.Command
  8. * @copyright Copyright (c) 2021, DedeBIZ.COM
  9. * @license https://www.dedebiz.com/license
  10. * @link https://www.dedebiz.com
  11. */
  12. // 切换工作目录到./src
  13. $workDir = dirname(__FILE__) . "/src";
  14. chdir($workDir);
  15. if (substr(php_sapi_name(), 0, 3) === 'cgi') {
  16. die("DedeCMSV6:needs php-cli to run\n\n");
  17. }
  18. $helpStr = "
  19. NAME:
  20. DedeBIZ Cli Tools
  21. USAGE:
  22. php ./dedebiz command [arguments...]
  23. COMMANDS:
  24. serv,s Run cli web server for DedeCMSV6
  25. make,m Make DedeCMSV6 HTML
  26. update,u Update to latest system
  27. help,h Shows a list of commands or help
  28. quick,q Quick start a development environment
  29. WEBSITE:
  30. https://www.dedebiz.com/help/
  31. ";
  32. // 将选项转化为SQL IN参数
  33. function Option2SQLin($str = "")
  34. {
  35. $str = preg_replace("#[^0-9-,]#", "", $str);
  36. $strs = explode(",", $str);
  37. foreach ($strs as $key => $si) {
  38. if (preg_match("#-#", $si)) {
  39. $tstart = 0;
  40. $tend = 0;
  41. $tss = explode("-", $si);
  42. if (intval($tss[0]) > intval($tss[1])) {
  43. $tstart = intval($tss[1]);
  44. $tend = intval($tss[0]);
  45. } else {
  46. $tstart = intval($tss[0]);
  47. $tend = intval($tss[1]);
  48. }
  49. $tmpArr = array();
  50. for ($i = $tstart; $i <= $tend; $i++) {
  51. $tmpArr[] = $i;
  52. }
  53. $strs[$key] = implode(",", $tmpArr);
  54. }
  55. }
  56. return implode(",", $strs);
  57. }
  58. function RandEncode($length=26)
  59. {
  60. $chars='abcdefghigklmnopqrstuvwxwyABCDEFGHIGKLMNOPQRSTUVWXWY0123456789';
  61. $rnd_cookieEncode='';
  62. $length = rand(28,32);
  63. $max = strlen($chars) - 1;
  64. for($i = 0; $i < $length; $i++) {
  65. $rnd_cookieEncode .= $chars[mt_rand(0, $max)];
  66. }
  67. return $rnd_cookieEncode;
  68. }
  69. if (count($argv) > 1 && ($argv[1] == "serv" || $argv[1] == "s")) {
  70. // PHP5.4以下不支持内建服务器
  71. // 用于开发调试
  72. if (phpversion() < "5.4") {
  73. die("DedeCMSV6:command web server not support\n\n");
  74. }
  75. echo "Start Dev Server For DedeCMSV6\n\r";
  76. echo "Open http://localhost:8088\n\r";
  77. passthru(PHP_BINARY . ' -S localhost:8088 -t' . escapeshellarg('./'));
  78. } else if (count($argv) > 1 && ($argv[1] == "make" || $argv[1] == "m")) {
  79. if (!file_exists($workDir . "/include/common.inc.php")) {
  80. DedeCli::error("Check your root path is right");
  81. exit;
  82. }
  83. require_once($workDir . "/include/common.inc.php");
  84. require_once(DEDEINC . "/cli.class.php");
  85. // 一个命令行的生成工具
  86. if (count($argv) > 2 && ($argv[2] == "arc" || $argv[2] == "a")) {
  87. // 生成文档
  88. // make arc typeid=1
  89. $t1 = ExecTime();
  90. $addsql = "1=1";
  91. $typeid = Option2SQLin(DedeCli::getOption("typeid"));
  92. if (!empty($typeid)) {
  93. $addsql .= " AND typeid IN(" . $typeid . ")";
  94. }
  95. $aid = Option2SQLin(DedeCli::getOption("aid"));
  96. if (!empty($aid)) {
  97. $addsql .= " AND id IN(" . $typeid . ")";
  98. }
  99. $tt = $dsql->GetOne("SELECT COUNT(id) as dd FROM `#@__arctiny` WHERE " . $addsql);
  100. $total = intval($tt['dd']);
  101. $dsql->Execute('out', "SELECT id FROM `#@__arctiny` WHERE " . $addsql . " ORDER BY typeid ASC");
  102. $i = 0;
  103. while ($row = $dsql->GetObject('out')) {
  104. $id = $row->id;
  105. $ac = new Archives($id);
  106. $rurl = $ac->MakeHtml(0);
  107. DedeCli::showProgress(ceil(($i / $total) * 100), 100);
  108. $i++;
  109. }
  110. DedeCli::write("Make archive html successfull");
  111. $queryTime = ExecTime() - $t1;
  112. DedeCli::write($queryTime);
  113. exit;
  114. } else if (count($argv) > 2 && ($argv[2] == "list" || $argv[2] == "l")) {
  115. // 生成栏目
  116. $addsql = "1=1";
  117. $typeid = Option2SQLin(DedeCli::getOption("typeid"));
  118. if (!empty($typeid)) {
  119. $addsql .= " AND id IN(" . $typeid . ")";
  120. }
  121. $dsql->Execute('out', "SELECT id,channeltype FROM `#@__arctype` WHERE " . $addsql);
  122. $i = 0;
  123. while ($row = $dsql->GetObject('out')) {
  124. if ($row->channeltype > 0) {
  125. $lv = new ListView($row->id);
  126. } else {
  127. $lv = new SgListView($row->id);
  128. }
  129. $lv->CountRecord();
  130. DedeCli::write("Start make list html[id:{$row->id}]");
  131. $lv->MakeHtml('', '', 0);
  132. }
  133. exit;
  134. } else if (count($argv) > 2 && ($argv[2] == "index" || $argv[2] == "i")) {
  135. // 生成首页
  136. $position = DedeCli::getOption("position");
  137. if (empty($position)) {
  138. $position = "../index.html";
  139. }
  140. if (!preg_match("#\.html$#", $position)) {
  141. DedeCli::error("position must end with .html");
  142. exit;
  143. }
  144. $homeFile = DEDEINC . "/" . $position;
  145. $homeFile = str_replace("\\", "/", $homeFile);
  146. $homeFile = str_replace("//", "/", $homeFile);
  147. $row = $dsql->GetOne("SELECT * FROM `#@__homepageset`");
  148. $templet = $row['templet'];
  149. $templet = str_replace("{style}", $cfg_df_style, $templet);
  150. $pv = new PartView();
  151. $GLOBALS['_arclistEnv'] = 'index';
  152. $pv->SetTemplet($cfg_basedir . $cfg_templets_dir . "/" . $templet);
  153. $pv->SaveToHtml($homeFile);
  154. DedeCli::write("Make index html successfull");
  155. } else if (count($argv) > 2 && ($argv[2] == "auto" || $argv[2] == "o")) {
  156. // 自动生成
  157. function OptimizeData($dsql)
  158. {
  159. global $cfg_dbprefix;
  160. $tptables = array("{$cfg_dbprefix}archives", "{$cfg_dbprefix}arctiny");
  161. $dsql->SetQuery("SELECT maintable,addtable FROM `#@__channeltype` ");
  162. $dsql->Execute();
  163. while ($row = $dsql->GetObject()) {
  164. $addtable = str_replace('#@__', $cfg_dbprefix, $row->addtable);
  165. if ($addtable != '' && !in_array($addtable, $tptables)) $tptables[] = $addtable;
  166. }
  167. $tptable = '';
  168. foreach ($tptables as $t) $tptable .= ($tptable == '' ? "`{$t}`" : ",`{$t}`");
  169. $dsql->ExecuteNoneQuery(" OPTIMIZE TABLE $tptable; ");
  170. }
  171. $start = empty(DedeCli::getOption("start"))? "-1 day" : DedeCli::getOption("start");
  172. $start = strtotime($start);
  173. if (!$start) {
  174. DedeCli::error("start is empty");
  175. exit;
  176. }
  177. // 1.生成首页
  178. $pv = new PartView();
  179. $row = $pv->dsql->GetOne("SELECT * FROM `#@__homepageset` ");
  180. $templet = str_replace("{style}", $cfg_df_style, $row['templet']);
  181. $homeFile = DEDEINC . '/' . $row['position'];
  182. $homeFile = str_replace("\\", '/', $homeFile);
  183. $homeFile = preg_replace("#\/{1,}#", '/', $homeFile);
  184. if ($row['showmod'] == 1) {
  185. $pv->SetTemplet($cfg_basedir . $cfg_templets_dir . '/' . $templet);
  186. $pv->SaveToHtml($homeFile);
  187. $pv->Close();
  188. } else {
  189. if (file_exists($homeFile)) @unlink($homeFile);
  190. }
  191. DedeCli::write("Make index html successfull");
  192. // 2.生成栏目
  193. $query = "SELECT DISTINCT typeid From `#@__arctiny` WHERE senddate >=" . $start . " AND arcrank>-1";
  194. $dsql->SetQuery($query);
  195. $dsql->Execute();
  196. $typeids = array();
  197. while ($row = $dsql->GetArray()) {
  198. $typeids[$row['typeid']] = 1;
  199. }
  200. if (count($typeids) > 0) {
  201. foreach ($typeids as $k => $v) {
  202. $vs = array();
  203. $vs = GetParentIds($k);
  204. if (!isset($typeidsok[$k])) {
  205. $typeidsok[$k] = 1;
  206. }
  207. foreach ($vs as $k => $v) {
  208. if (!isset($typeidsok[$v])) {
  209. $typeidsok[$v] = 1;
  210. }
  211. }
  212. }
  213. foreach ($typeidsok as $tt=> $k) {
  214. $row = $dsql->GetOne("SELECT id,channeltype FROM `#@__arctype` WHERE id=".$tt);
  215. if ($row['channeltype'] > 0) {
  216. $lv = new ListView($tt);
  217. } else {
  218. $lv = new SgListView($tt);
  219. }
  220. $lv->CountRecord();
  221. DedeCli::write("Start make list html[id:{$tt}]");
  222. $lv->MakeHtml('', '', 0);
  223. }
  224. DedeCli::write("Make list html successfull");
  225. }
  226. // 生成文档
  227. $tt = $dsql->GetOne("SELECT COUNT(id) as dd FROM `#@__arctiny` WHERE senddate >=" . $start . " AND arcrank>-1");
  228. $total = intval($tt['dd']);
  229. $dsql->Execute('out', "SELECT id FROM `#@__arctiny` WHERE senddate >=" . $start . " AND arcrank>-1 ORDER BY typeid ASC");
  230. $i = 0;
  231. while ($row = $dsql->GetObject('out')) {
  232. $id = $row->id;
  233. $ac = new Archives($id);
  234. $rurl = $ac->MakeHtml(0);
  235. DedeCli::showProgress(ceil(($i / $total) * 100), 100);
  236. $i++;
  237. }
  238. DedeCli::write("Make archives html successfull");
  239. // 优化数据
  240. OptimizeData($dsql);
  241. DedeCli::write("Optimize data successfull");
  242. } else {
  243. $helpStr = "
  244. USAGE:
  245. php ./dedebiz make action [arguments...]
  246. ACTIONS:
  247. index,i Make Index html
  248. --position index html position,default: ../index.html(relative include dir)
  249. arc,a Make Archive htmls
  250. --typeid type id
  251. --aid archive id
  252. list,l Make List htmls
  253. --typeid type id
  254. auto,o Auto Make htmls
  255. --start start time(format:2012-03-12)
  256. WEBSITE:
  257. https://www.dedebiz.com/help/";
  258. DedeCli::write($helpStr);
  259. exit;
  260. }
  261. } else if (count($argv) > 1 && ($argv[1] == "update" || $argv[1] == "u")) {
  262. define("DEDEINC", $workDir."/include");
  263. require_once(DEDEINC."/dedehttpdown.class.php");
  264. require_once(DEDEINC . "/cli.class.php");
  265. // 更新系统
  266. $latestURL = "https://cdn.dedebiz.com/release/latest.txt";
  267. $del = new DedeHttpDown();
  268. $del->OpenUrl($latestURL);
  269. $remoteVerStr = $del->GetHtml();
  270. $commStr = file_get_contents(DEDEINC."/common.inc.php");
  271. preg_match("#_version_detail = '([\d\.]+)'#", $commStr, $matchs);
  272. $cfg_version_detail = $localVerStr = $matchs[1];
  273. if (version_compare($localVerStr, $remoteVerStr, '>=')) {
  274. DedeCli::error("latest version,don't need to update");
  275. exit;
  276. }
  277. $fileHashURL = "https://cdn.dedebiz.com/release/{$cfg_version_detail}.json";
  278. $del = new DedeHttpDown();
  279. $del->OpenUrl($fileHashURL);
  280. $filelist = $del->GetJSON();
  281. $offFiles = array();
  282. // foreach ($filelist as $key => $ff) {
  283. // $offFiles[$ff->filename] = $ff->hash;
  284. // }
  285. var_dump($filelist);
  286. } else if (count($argv) > 1 && ($argv[1] == "quick" || $argv[1] == "q")){
  287. define("DEDEINC", $workDir."/include");
  288. require_once(DEDEINC . "/cli.class.php");
  289. // 快速开始一个用于开发的DedeCMSV6环境,基于SQLite无其他依赖
  290. if (file_exists($workDir."/data/dedecmsv6.db")) {
  291. DedeCli::write("development environment has inited");
  292. echo "Start Dev Server For DedeCMSV6\n\r";
  293. echo "Open http://localhost:8088\n\r";
  294. passthru(PHP_BINARY . ' -S localhost:8088 -t' . escapeshellarg('./'));
  295. exit;
  296. }
  297. // 初始化安装一个开发环境
  298. $db = new SQLite3($workDir.'/data/dedecmsv6.db');
  299. $fp = fopen($workDir."/install/common.inc.php","r");
  300. $configStr1 = fread($fp,filesize($workDir."/install/common.inc.php"));
  301. fclose($fp);
  302. @chmod($workDir."/data",0777);
  303. $dbtype = "sqlite";
  304. $dbhost = "";
  305. $dbname = "dedecmsv6";
  306. $dbuser = "";
  307. $dbpwd = "";
  308. $dbprefix = "dede_";
  309. $dblang = "utf8";
  310. //common.inc.php
  311. $configStr1 = str_replace("~dbtype~",$dbtype,$configStr1);
  312. $configStr1 = str_replace("~dbhost~",$dbhost,$configStr1);
  313. $configStr1 = str_replace("~dbname~",$dbname,$configStr1);
  314. $configStr1 = str_replace("~dbuser~",$dbuser,$configStr1);
  315. $configStr1 = str_replace("~dbpwd~",$dbpwd,$configStr1);
  316. $configStr1 = str_replace("~dbprefix~",$dbprefix,$configStr1);
  317. $configStr1 = str_replace("~dblang~",$dblang,$configStr1);
  318. $fp = fopen($workDir."/data/common.inc.php","w") or die("error,check ../data writeable");
  319. fwrite($fp,$configStr1);
  320. fclose($fp);
  321. $cookieencode = RandEncode(26);
  322. $baseurl = "http://127.0.0.1:8088";
  323. $indexUrl = "/";
  324. $cmspath = "";
  325. $webname = "DedeCMSV6本地测试开发站点";
  326. $adminmail = "admin@dedebiz.com";
  327. $fp = fopen($workDir."/install/config.cache.inc.php","r");
  328. $configStr2 = fread($fp,filesize($workDir."/install/config.cache.inc.php"));
  329. fclose($fp);
  330. $configStr2 = str_replace("~baseurl~",$baseurl,$configStr2);
  331. $configStr2 = str_replace("~basepath~",$cmspath,$configStr2);
  332. $configStr2 = str_replace("~indexurl~",$indexUrl,$configStr2);
  333. $configStr2 = str_replace("~cookieEncode~",$cookieencode,$configStr2);
  334. $configStr2 = str_replace("~webname~",$webname,$configStr2);
  335. $configStr2 = str_replace("~adminmail~",$adminmail,$configStr2);
  336. $fp = fopen($workDir.'/data/config.cache.inc.php','w');
  337. fwrite($fp,$configStr2);
  338. fclose($fp);
  339. $fp = fopen($workDir.'/data/config.cache.bak.php','w');
  340. fwrite($fp,$configStr2);
  341. fclose($fp);
  342. $query = '';
  343. $fp = fopen($workDir.'/install/sql-dftables.txt','r');
  344. while(!feof($fp))
  345. {
  346. $line = rtrim(fgets($fp,1024));
  347. if(preg_match("#;$#", $line))
  348. {
  349. $query .= $line."\n";
  350. $query = str_replace('#@__',$dbprefix,$query);
  351. $query = preg_replace('/character set (.*?) /i','',$query);
  352. $query = str_replace('unsigned','',$query);
  353. $query = str_replace('TYPE=MyISAM','',$query);
  354. $query = preg_replace ('/TINYINT\(([\d]+)\)/i','INTEGER',$query);
  355. $query = preg_replace ('/mediumint\(([\d]+)\)/i','INTEGER',$query);
  356. $query = preg_replace ('/smallint\(([\d]+)\)/i','INTEGER',$query);
  357. $query = preg_replace('/int\(([\d]+)\)/i','INTEGER',$query);
  358. $query = preg_replace('/auto_increment/i','PRIMARY KEY AUTOINCREMENT',$query);
  359. $query = preg_replace('/, KEY(.*?)MyISAM;/','',$query);
  360. $query = preg_replace('/, KEY(.*?);/',');',$query);
  361. $query = preg_replace('/, UNIQUE KEY(.*?);/',');',$query);
  362. $query = preg_replace('/set\(([^\)]*?)\)/','varchar',$query);
  363. $query = preg_replace('/enum\(([^\)]*?)\)/','varchar',$query);
  364. if ( preg_match("/PRIMARY KEY AUTOINCREMENT/",$query) )
  365. {
  366. $query = preg_replace('/,([\t\s ]+)PRIMARY KEY \(`([0-9a-zA-Z]+)`\)/i','',$query);
  367. $query = str_replace(', PRIMARY KEY (`id`)','',$query);
  368. }
  369. @$db->exec($query);
  370. $query='';
  371. } else if(!preg_match("#^(\/\/|--)#", $line))
  372. {
  373. $query .= $line;
  374. }
  375. }
  376. fclose($fp);
  377. //导入默认数据
  378. $query = '';
  379. $fp = fopen($workDir.'/install/sql-dfdata.txt','r');
  380. while(!feof($fp))
  381. {
  382. $line = rtrim(fgets($fp, 1024));
  383. if(preg_match("#;$#", $line))
  384. {
  385. $query .= $line;
  386. $query = str_replace('#@__',$dbprefix,$query);
  387. $query = str_replace("\'","\"",$query);
  388. $query = str_replace('\t\n\n',"",$query);
  389. $query = str_replace('\t\n',"",$query);
  390. @$db->exec($query);
  391. $query='';
  392. } else if(!preg_match("#^(\/\/|--)#", $line))
  393. {
  394. $query .= $line;
  395. }
  396. }
  397. fclose($fp);
  398. //更新配置
  399. $cquery = "Update `{$dbprefix}sysconfig` set value='{$baseurl}' where varname='cfg_basehost';";
  400. $db->exec($cquery);
  401. $cquery = "Update `{$dbprefix}sysconfig` set value='{$cmspath}' where varname='cfg_cmspath';";
  402. $db->exec($cquery);
  403. $cquery = "Update `{$dbprefix}sysconfig` set value='{$indexUrl}' where varname='cfg_indexurl';";
  404. $db->exec($cquery);
  405. $cquery = "Update `{$dbprefix}sysconfig` set value='{$cookieencode}' where varname='cfg_cookie_encode';";
  406. $db->exec($cquery);
  407. $cquery = "Update `{$dbprefix}sysconfig` set value='{$webname}' where varname='cfg_webname';";
  408. $db->exec($cquery);
  409. $cquery = "Update `{$dbprefix}sysconfig` set value='{$adminmail}' where varname='cfg_adminemail';";
  410. $db->exec($cquery);
  411. $adminuser = "admin";
  412. $adminpwd = "admin";
  413. //增加管理员帐号
  414. $adminquery = "INSERT INTO `{$dbprefix}admin` VALUES (1, 10, '$adminuser', '".substr(md5($adminpwd),5,20)."', 'admin', '', '', 0, '".time()."', '127.0.0.1');";
  415. $db->exec($adminquery);
  416. DedeCli::write("admin user:admin");
  417. DedeCli::write("admin password:admin");
  418. //关连前台会员帐号
  419. $adminquery = "INSERT INTO `{$dbprefix}member` (`mid`,`mtype`,`userid`,`pwd`,`uname`,`sex`,`rank`,`money`,`email`,
  420. `scores` ,`matt` ,`face`,`safequestion`,`safeanswer` ,`jointime` ,`joinip` ,`logintime` ,`loginip` )
  421. VALUES ('1','个人','$adminuser','".md5($adminpwd)."','$adminuser','男','100','0','','10000','10','','0','','".time()."','','0',''); ";
  422. $db->exec($adminquery);
  423. $adminquery = "INSERT INTO `{$dbprefix}member_person` (`mid`,`onlynet`,`sex`,`uname`,`qq`,`msn`,`tel`,`mobile`,`place`,`oldplace`,`birthday`,`star`,
  424. `income` , `education` , `height` , `bodytype` , `blood` , `vocation` , `smoke` , `marital` , `house` ,`drink` , `datingtype` , `language` , `nature` , `lovemsg` , `address`,`uptime`)
  425. VALUES ('1', '1', '男', '{$adminuser}', '', '', '', '', '0', '0','1980-01-01', '1', '0', '0', '160', '0', '0', '0', '0', '0', '0','0', '0', '', '', '', '','0'); ";
  426. $db->exec($adminquery);
  427. $adminquery = "INSERT INTO `{$dbprefix}member_tj` (`mid`,`article`,`album`,`archives`,`homecount`,`pagecount`,`feedback`,`friend`,`stow`)
  428. VALUES ('1','0','0','0','0','0','0','0','0'); ";
  429. $db->exec($adminquery);
  430. $adminquery = "Insert Into `{$dbprefix}member_space`(`mid` ,`pagesize` ,`matt` ,`spacename` ,`spacelogo` ,`spacestyle`, `sign` ,`spacenews`)
  431. Values('1','10','0','{$adminuser}的空间','','person','',''); ";
  432. $db->exec($adminquery);
  433. if (phpversion() < "5.4") {
  434. die("DedeCMSV6:command web server not support\n\n");
  435. }
  436. echo "Start Dev Server For DedeCMSV6\n\r";
  437. echo "Open http://localhost:8088\n\r";
  438. passthru(PHP_BINARY . ' -S localhost:8088 -t' . escapeshellarg('./'));
  439. exit;
  440. } else {
  441. echo $helpStr;
  442. }