国内流行的内容管理系统(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.

446 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) 2022, DedeBIZ.COM
  9. * @license https://www.dedebiz.com/license
  10. * @link https://www.dedebiz.com
  11. */
  12. define('DEDE_ENVIRONMENT', 'production');
  13. define('DEBUG_LEVEL', FALSE); //如果设置为TRUE则会打印执行SQL的时间和标签加载时间方便调试
  14. //切换工作目录到/src
  15. $workDir = dirname(__FILE__) . "/src";
  16. chdir($workDir);
  17. if (substr(php_sapi_name(), 0, 3) === 'cgi') {
  18. die("DedeBIZ:needs php-cli to run\n\n");
  19. }
  20. $helpStr = "
  21. NAME:
  22. DedeBIZ Cli Tools
  23. USAGE:
  24. php ./dedebiz command [arguments...]
  25. COMMANDS:
  26. serv,s Run cli web server for DedeBIZ
  27. make,m Make DedeBIZ HTML
  28. update,u Update to latest system
  29. help,h Shows a list of commands or help
  30. quick,q Quick start a development environment
  31. WEBSITE:
  32. https://www.dedebiz.com/help/
  33. ";
  34. //将选项转化为SQL IN参数
  35. function Option2SQLin($str = "")
  36. {
  37. $str = preg_replace("#[^0-9-,]#", "", $str);
  38. $strs = explode(",", $str);
  39. foreach ($strs as $key => $si) {
  40. if (preg_match("#-#", $si)) {
  41. $tstart = 0;
  42. $tend = 0;
  43. $tss = explode("-", $si);
  44. if (intval($tss[0]) > intval($tss[1])) {
  45. $tstart = intval($tss[1]);
  46. $tend = intval($tss[0]);
  47. } else {
  48. $tstart = intval($tss[0]);
  49. $tend = intval($tss[1]);
  50. }
  51. $tmpArr = array();
  52. for ($i = $tstart; $i <= $tend; $i++) {
  53. $tmpArr[] = $i;
  54. }
  55. $strs[$key] = implode(",", $tmpArr);
  56. }
  57. }
  58. return implode(",", $strs);
  59. }
  60. function RandEncode($length=26)
  61. {
  62. $chars='abcdefghigklmnopqrstuvwxwyABCDEFGHIGKLMNOPQRSTUVWXWY0123456789';
  63. $rnd_cookieEncode='';
  64. $length = rand(28,32);
  65. $max = strlen($chars) - 1;
  66. for($i = 0; $i < $length; $i++) {
  67. $rnd_cookieEncode .= $chars[mt_rand(0, $max)];
  68. }
  69. return $rnd_cookieEncode;
  70. }
  71. if (count($argv) > 1 && ($argv[1] == "serv" || $argv[1] == "s")) {
  72. //PHP5.4以下不支持内建服务器
  73. //用于开发调试
  74. if (phpversion() < "5.4") {
  75. die("DedeBIZ:command web server not support\n\n");
  76. }
  77. echo "Start Dev Server For DedeBIZ\n\r";
  78. echo "Open http://localhost:8088\n\r";
  79. passthru(PHP_BINARY . ' -S localhost:8088 -t' . escapeshellarg('./'));
  80. } else if (count($argv) > 1 && ($argv[1] == "make" || $argv[1] == "m")) {
  81. if (!file_exists($workDir . "/system/common.inc.php")) {
  82. DedeCli::error("Check your root path is right");
  83. exit;
  84. }
  85. require_once($workDir . "/system/common.inc.php");
  86. require_once(DEDEINC . "/libraries/cli.class.php");
  87. //一个命令行的生成工具
  88. if (count($argv) > 2 && ($argv[2] == "arc" || $argv[2] == "a")) {
  89. //生成文档
  90. //make arc typeid=1
  91. $t1 = ExecTime();
  92. $addsql = "1=1";
  93. $typeid = Option2SQLin(DedeCli::getOption("typeid"));
  94. if (!empty($typeid)) {
  95. $addsql .= " AND typeid IN(" . $typeid . ")";
  96. }
  97. $aid = Option2SQLin(DedeCli::getOption("aid"));
  98. if (!empty($aid)) {
  99. $addsql .= " AND id IN(" . $typeid . ")";
  100. }
  101. $tt = $dsql->GetOne("SELECT COUNT(id) as dd FROM `#@__arctiny` WHERE " . $addsql);
  102. $total = intval($tt['dd']);
  103. $dsql->Execute('out', "SELECT id FROM `#@__arctiny` WHERE " . $addsql . " ORDER BY typeid ASC");
  104. $i = 0;
  105. while ($row = $dsql->GetObject('out')) {
  106. $id = $row->id;
  107. $ac = new Archives($id);
  108. $rurl = $ac->MakeHtml(0);
  109. DedeCli::showProgress(ceil(($i / $total) * 100), 100, $i, $total);
  110. $i++;
  111. }
  112. DedeCli::write("Make archive html successfull");
  113. $queryTime = ExecTime() - $t1;
  114. DedeCli::write($queryTime);
  115. exit;
  116. } else if (count($argv) > 2 && ($argv[2] == "list" || $argv[2] == "l")) {
  117. //生成栏目
  118. $addsql = "1=1";
  119. $typeid = Option2SQLin(DedeCli::getOption("typeid"));
  120. if (!empty($typeid)) {
  121. $addsql .= " AND id IN(" . $typeid . ")";
  122. }
  123. $dsql->Execute('out', "SELECT id,channeltype FROM `#@__arctype` WHERE " . $addsql);
  124. $i = 0;
  125. while ($row = $dsql->GetObject('out')) {
  126. if ($row->channeltype > 0) {
  127. $lv = new ListView($row->id);
  128. } else {
  129. $lv = new SgListView($row->id);
  130. }
  131. $lv->CountRecord();
  132. DedeCli::write("Start make list html[id:{$row->id}]");
  133. $lv->MakeHtml('', '', 0);
  134. }
  135. exit;
  136. } else if (count($argv) > 2 && ($argv[2] == "index" || $argv[2] == "i")) {
  137. //生成首页
  138. $position = DedeCli::getOption("position");
  139. if (empty($position)) {
  140. $position = "../index.html";
  141. }
  142. if (!preg_match("#\.html$#", $position)) {
  143. DedeCli::error("position must end with .html");
  144. exit;
  145. }
  146. $homeFile = DEDEINC . "/" . $position;
  147. $homeFile = str_replace("\\", "/", $homeFile);
  148. $homeFile = str_replace("//", "/", $homeFile);
  149. $row = $dsql->GetOne("SELECT * FROM `#@__homepageset`");
  150. $templet = $row['templet'];
  151. $templet = str_replace("{style}", $cfg_df_style, $templet);
  152. $pv = new PartView();
  153. $GLOBALS['_arclistEnv'] = 'index';
  154. $pv->SetTemplet($cfg_basedir . $cfg_templets_dir . "/" . $templet);
  155. $pv->SaveToHtml($homeFile);
  156. DedeCli::write("Make index html successfull");
  157. } else if (count($argv) > 2 && ($argv[2] == "auto" || $argv[2] == "o")) {
  158. //自动生成
  159. function OptimizeData($dsql)
  160. {
  161. global $cfg_dbprefix;
  162. $tptables = array("{$cfg_dbprefix}archives", "{$cfg_dbprefix}arctiny");
  163. $dsql->SetQuery("SELECT maintable,addtable FROM `#@__channeltype` ");
  164. $dsql->Execute();
  165. while ($row = $dsql->GetObject()) {
  166. $addtable = str_replace('#@__', $cfg_dbprefix, $row->addtable);
  167. if ($addtable != '' && !in_array($addtable, $tptables)) $tptables[] = $addtable;
  168. }
  169. $tptable = '';
  170. foreach ($tptables as $t) $tptable .= ($tptable == '' ? "`{$t}`" : ",`{$t}`");
  171. $dsql->ExecuteNoneQuery(" OPTIMIZE TABLE $tptable; ");
  172. }
  173. $start = empty(DedeCli::getOption("start"))? "-1 day" : DedeCli::getOption("start");
  174. $start = strtotime($start);
  175. if (!$start) {
  176. DedeCli::error("start is empty");
  177. exit;
  178. }
  179. //1.生成首页
  180. $pv = new PartView();
  181. $row = $pv->dsql->GetOne("SELECT * FROM `#@__homepageset` ");
  182. $templet = str_replace("{style}", $cfg_df_style, $row['templet']);
  183. $homeFile = DEDEINC . '/' . $row['position'];
  184. $homeFile = str_replace("\\", '/', $homeFile);
  185. $homeFile = preg_replace("#\/{1,}#", '/', $homeFile);
  186. if ($row['showmod'] == 1) {
  187. $pv->SetTemplet($cfg_basedir . $cfg_templets_dir . '/' . $templet);
  188. $pv->SaveToHtml($homeFile);
  189. $pv->Close();
  190. } else {
  191. if (file_exists($homeFile)) @unlink($homeFile);
  192. }
  193. DedeCli::write("Make index html successfull");
  194. //2.生成栏目
  195. $query = "SELECT DISTINCT typeid From `#@__arctiny` WHERE senddate >=" . $start . " AND arcrank>-1";
  196. $dsql->SetQuery($query);
  197. $dsql->Execute();
  198. $typeids = array();
  199. while ($row = $dsql->GetArray()) {
  200. $typeids[$row['typeid']] = 1;
  201. }
  202. if (count($typeids) > 0) {
  203. foreach ($typeids as $k => $v) {
  204. $vs = array();
  205. $vs = GetParentIds($k);
  206. if (!isset($typeidsok[$k])) {
  207. $typeidsok[$k] = 1;
  208. }
  209. foreach ($vs as $k => $v) {
  210. if (!isset($typeidsok[$v])) {
  211. $typeidsok[$v] = 1;
  212. }
  213. }
  214. }
  215. foreach ($typeidsok as $tt=> $k) {
  216. $row = $dsql->GetOne("SELECT id,channeltype FROM `#@__arctype` WHERE id=".$tt);
  217. if ($row['channeltype'] > 0) {
  218. $lv = new ListView($tt);
  219. } else {
  220. $lv = new SgListView($tt);
  221. }
  222. $lv->CountRecord();
  223. DedeCli::write("Start make list html[id:{$tt}]");
  224. $lv->MakeHtml('', '', 0);
  225. }
  226. DedeCli::write("Make list html successfull");
  227. }
  228. //生成文档
  229. $tt = $dsql->GetOne("SELECT COUNT(id) as dd FROM `#@__arctiny` WHERE senddate >=" . $start . " AND arcrank>-1");
  230. $total = intval($tt['dd']);
  231. $dsql->Execute('out', "SELECT id FROM `#@__arctiny` WHERE senddate >=" . $start . " AND arcrank>-1 ORDER BY typeid ASC");
  232. $i = 0;
  233. while ($row = $dsql->GetObject('out')) {
  234. $id = $row->id;
  235. $ac = new Archives($id);
  236. $rurl = $ac->MakeHtml(0);
  237. DedeCli::showProgress(ceil(($i / $total) * 100), 100);
  238. $i++;
  239. }
  240. DedeCli::write("Make archives html successfull");
  241. //优化数据
  242. OptimizeData($dsql);
  243. DedeCli::write("Optimize data successfull");
  244. } else {
  245. $helpStr = "
  246. USAGE:
  247. php ./dedebiz make action [arguments...]
  248. ACTIONS:
  249. index,i Make Index html
  250. --position index html position,default: ../index.html(relative include dir)
  251. arc,a Make Archive htmls
  252. --typeid type id
  253. --aid archive id
  254. list,l Make List htmls
  255. --typeid type id
  256. auto,o Auto Make htmls
  257. --start start time(format:2012-03-12)
  258. WEBSITE:
  259. https://www.dedebiz.com/help/";
  260. DedeCli::write($helpStr);
  261. exit;
  262. }
  263. } else if (count($argv) > 1 && ($argv[1] == "update" || $argv[1] == "u")) {
  264. define("DEDEINC", $workDir."/system");
  265. require_once(DEDEINC."/dedehttpdown.class.php");
  266. require_once(DEDEINC . "/libraries/cli.class.php");
  267. //更新系统
  268. $latestURL = "https://cdn.dedebiz.com/release/latest.txt";
  269. $del = new DedeHttpDown();
  270. $del->OpenUrl($latestURL);
  271. $remoteVerStr = $del->GetHtml();
  272. $commStr = file_get_contents(DEDEINC."/common.inc.php");
  273. preg_match("#_version_detail = '([\d\.]+)'#", $commStr, $matchs);
  274. $cfg_version_detail = $localVerStr = $matchs[1];
  275. if (version_compare($localVerStr, $remoteVerStr, '>=')) {
  276. DedeCli::error("latest version,don't need to update");
  277. exit;
  278. }
  279. $fileHashURL = "https://cdn.dedebiz.com/release/{$cfg_version_detail}.json";
  280. $del = new DedeHttpDown();
  281. $del->OpenUrl($fileHashURL);
  282. $filelist = $del->GetJSON();
  283. $offFiles = array();
  284. //TODO 命令行自动更新
  285. } else if (count($argv) > 1 && ($argv[1] == "quick" || $argv[1] == "q")){
  286. define("DEDEINC", $workDir."/system");
  287. require_once(DEDEINC . "/libraries/cli.class.php");
  288. //快速开始一个用于开发的DedeBIZ环境,基于SQLite无其他依赖
  289. if (file_exists($workDir."/data/DedeBIZ.db")) {
  290. DedeCli::write("development environment has inited");
  291. echo "Start Dev Server For DedeBIZ\n\r";
  292. echo "Open http://localhost:8088\n\r";
  293. passthru(PHP_BINARY . ' -S localhost:8088 -t' . escapeshellarg('./'));
  294. exit;
  295. }
  296. //初始化安装一个开发环境
  297. $db = new SQLite3($workDir.'/data/DedeBIZ.db');
  298. $fp = fopen($workDir."/install/common.inc.php","r");
  299. $configStr1 = fread($fp,filesize($workDir."/install/common.inc.php"));
  300. fclose($fp);
  301. @chmod($workDir."/data",0777);
  302. $dbtype = "sqlite";
  303. $dbhost = "";
  304. $dbname = "DedeBIZ";
  305. $dbuser = "";
  306. $dbpwd = "";
  307. $dbprefix = "dede_";
  308. $dblang = "utf8";
  309. mkdir($workDir.'/data/tplcache', 0777);
  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 = "DedeBIZ本地测试开发站点";
  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`,`scores` ,`matt` ,`face`,`safequestion`,`safeanswer` ,`jointime` ,`joinip` ,`logintime` ,`loginip` )
  420. VALUES ('1','个人','$adminuser','".md5($adminpwd)."','$adminuser','男','100','0','','10000','10','','0','','".time()."','','0',''); ";
  421. $db->exec($adminquery);
  422. $adminquery = "INSERT INTO `{$dbprefix}member_person` (`mid`,`onlynet`,`sex`,`uname`,`qq`,`msn`,`tel`,`mobile`,`place`,`oldplace`,`birthday`,`star`,`income`,`education`,`height`,`bodytype`,`blood`,`vocation`,`smoke`,`marital`,`house` ,`drink`,`datingtype`,`language`,`nature`,`lovemsg`,`address`,`uptime`)
  423. VALUES ('1', '1', '男', '{$adminuser}', '', '', '', '', '0', '0','1980-01-01', '1', '0', '0', '160', '0', '0', '0', '0', '0', '0','0', '0', '', '', '', '','0'); ";
  424. $db->exec($adminquery);
  425. $adminquery = "INSERT INTO `{$dbprefix}member_tj` (`mid`,`article`,`album`,`archives`,`homecount`,`pagecount`,`feedback`,`friend`,`stow`)
  426. VALUES ('1','0','0','0','0','0','0','0','0'); ";
  427. $db->exec($adminquery);
  428. $adminquery = "Insert Into `{$dbprefix}member_space`(`mid` ,`pagesize` ,`matt` ,`spacename` ,`spacelogo` ,`spacestyle`, `sign` ,`spacenews`)
  429. Values('1','10','0','{$adminuser}的空间','','person','',''); ";
  430. $db->exec($adminquery);
  431. if (phpversion() < "5.4") {
  432. die("DedeBIZ:command web server not support\n\n");
  433. }
  434. // 写入程序安装锁
  435. file_put_contents($workDir.'/install/install_lock.txt', 'ok');
  436. echo "Start Dev Server For DedeBIZ\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. }