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

453 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. if (!is_dir($workDir.'/data/tplcache')) {
  310. mkdir($workDir.'/data/tplcache', 0777);
  311. }
  312. //common.inc.php
  313. $configStr1 = str_replace("~dbtype~",$dbtype,$configStr1);
  314. $configStr1 = str_replace("~dbhost~",$dbhost,$configStr1);
  315. $configStr1 = str_replace("~dbname~",$dbname,$configStr1);
  316. $configStr1 = str_replace("~dbuser~",$dbuser,$configStr1);
  317. $configStr1 = str_replace("~dbpwd~",$dbpwd,$configStr1);
  318. $configStr1 = str_replace("~dbprefix~",$dbprefix,$configStr1);
  319. $configStr1 = str_replace("~dblang~",$dblang,$configStr1);
  320. $fp = fopen($workDir."/data/common.inc.php","w") or die("error,check /data writeable");
  321. fwrite($fp,$configStr1);
  322. fclose($fp);
  323. $cookieencode = RandEncode(26);
  324. $baseurl = "http://127.0.0.1:8088";
  325. $indexUrl = "/";
  326. $cmspath = "";
  327. $webname = "DedeBIZ本地测试开发站点";
  328. $adminmail = "admin@dedebiz.com";
  329. $fp = fopen($workDir."/install/config.cache.inc.php","r");
  330. $configStr2 = fread($fp,filesize($workDir."/install/config.cache.inc.php"));
  331. fclose($fp);
  332. $configStr2 = str_replace("~baseurl~",$baseurl,$configStr2);
  333. $configStr2 = str_replace("~basepath~",$cmspath,$configStr2);
  334. $configStr2 = str_replace("~indexurl~",$indexUrl,$configStr2);
  335. $configStr2 = str_replace("~cookieEncode~",$cookieencode,$configStr2);
  336. $configStr2 = str_replace("~webname~",$webname,$configStr2);
  337. $configStr2 = str_replace("~adminmail~",$adminmail,$configStr2);
  338. $fp = fopen($workDir.'/data/config.cache.inc.php','w');
  339. fwrite($fp,$configStr2);
  340. fclose($fp);
  341. $fp = fopen($workDir.'/data/config.cache.bak.php','w');
  342. fwrite($fp,$configStr2);
  343. fclose($fp);
  344. $query = '';
  345. $fp = fopen($workDir.'/install/sql-dftables.txt','r');
  346. while(!feof($fp))
  347. {
  348. $line = rtrim(fgets($fp,1024));
  349. if(preg_match("#;$#", $line))
  350. {
  351. $query .= $line."\n";
  352. $query = str_replace('#@__',$dbprefix,$query);
  353. $query = preg_replace('/character set (.*?) /i','',$query);
  354. $query = str_replace('unsigned','',$query);
  355. $query = str_replace('TYPE=MyISAM','',$query);
  356. $query = preg_replace ('/TINYINT\(([\d]+)\)/i','INTEGER',$query);
  357. $query = preg_replace ('/mediumint\(([\d]+)\)/i','INTEGER',$query);
  358. $query = preg_replace ('/smallint\(([\d]+)\)/i','INTEGER',$query);
  359. $query = preg_replace('/int\(([\d]+)\)/i','INTEGER',$query);
  360. $query = preg_replace('/auto_increment/i','PRIMARY KEY AUTOINCREMENT',$query);
  361. $query = preg_replace('/, KEY(.*?)MyISAM;/','',$query);
  362. $query = preg_replace('/, KEY(.*?);/',');',$query);
  363. $query = preg_replace('/, UNIQUE KEY(.*?);/',');',$query);
  364. $query = preg_replace('/set\(([^\)]*?)\)/','varchar',$query);
  365. $query = preg_replace('/enum\(([^\)]*?)\)/','varchar',$query);
  366. if ( preg_match("/PRIMARY KEY AUTOINCREMENT/",$query) )
  367. {
  368. $query = preg_replace('/,([\t\s ]+)PRIMARY KEY \(`([0-9a-zA-Z]+)`\)/i','',$query);
  369. $query = str_replace(', PRIMARY KEY (`id`)','',$query);
  370. }
  371. @$db->exec($query);
  372. $query='';
  373. } else if(!preg_match("#^(\/\/|--)#", $line))
  374. {
  375. $query .= $line;
  376. }
  377. }
  378. fclose($fp);
  379. //导入默认数据
  380. $query = '';
  381. $fp = fopen($workDir.'/install/sql-dfdata.txt','r');
  382. while(!feof($fp))
  383. {
  384. $line = rtrim(fgets($fp, 1024));
  385. if(preg_match("#;$#", $line))
  386. {
  387. $query .= $line;
  388. $query = str_replace('#@__',$dbprefix,$query);
  389. $query = str_replace("\'","\"",$query);
  390. $query = str_replace('\t\n\n',"",$query);
  391. $query = str_replace('\t\n',"",$query);
  392. @$db->exec($query);
  393. $query='';
  394. } else if(!preg_match("#^(\/\/|--)#", $line))
  395. {
  396. $query .= $line;
  397. }
  398. }
  399. fclose($fp);
  400. //更新配置
  401. $cquery = "UPDATE `{$dbprefix}sysconfig` SET value='{$baseurl}' WHERE varname='cfg_basehost';";
  402. $db->exec($cquery);
  403. $cquery = "UPDATE `{$dbprefix}sysconfig` SET value='{$cmspath}' WHERE varname='cfg_cmspath';";
  404. $db->exec($cquery);
  405. $cquery = "UPDATE `{$dbprefix}sysconfig` SET value='{$indexUrl}' WHERE varname='cfg_indexurl';";
  406. $db->exec($cquery);
  407. $cquery = "UPDATE `{$dbprefix}sysconfig` SET value='{$cookieencode}' WHERE varname='cfg_cookie_encode';";
  408. $db->exec($cquery);
  409. $cquery = "UPDATE `{$dbprefix}sysconfig` SET value='{$webname}' WHERE varname='cfg_webname';";
  410. $db->exec($cquery);
  411. $cquery = "UPDATE `{$dbprefix}sysconfig` SET value='{$adminmail}' WHERE varname='cfg_adminemail';";
  412. $db->exec($cquery);
  413. $adminuser = "admin";
  414. $adminpwd = "admin";
  415. //增加管理员帐号
  416. $pfd = "pwd";
  417. $apwd = substr(md5($adminpwd),5,20);
  418. $upwd = md5($adminpwd);
  419. if (function_exists('password_hash')) {
  420. $pfd = "pwd_new";
  421. $apwd = password_hash($adminpwd, PASSWORD_BCRYPT);
  422. $upwd = password_hash($adminpwd, PASSWORD_BCRYPT);
  423. }
  424. //增加管理员帐号
  425. $adminquery = "INSERT INTO `{$dbprefix}admin` (`id`,`usertype`,`userid`,`$pfd`,`uname`,`tname`,`email`,`typeid`,`logintime`,`loginip`) VALUES (1,10,'$adminuser','".$apwd."','admin','','',0,'".time()."','127.0.0.1');";
  426. $db->exec($adminquery);
  427. //关连前台会员帐号
  428. $adminquery = "INSERT INTO `{$dbprefix}member` (`mid`,`mtype`,`userid`,`{$pfd}`,`uname`,`sex`,`rank`,`money`,`email`,`scores`,`matt`,`face`,`safequestion`,`safeanswer`,`jointime`,`joinip`,`logintime`,`loginip`) VALUES ('1','个人','$adminuser','".$upwd."','$adminuser','男','100','0','','10000','10','','0','','".time()."','','0',''); ";
  429. $db->exec($adminquery);
  430. $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`) VALUES ('1','1','男','{$adminuser}','','','','','0','0','1980-01-01','1','0','0','160','0','0','0','0','0','0','0','0','','','','','0'); ";
  431. $db->exec($adminquery);
  432. $adminquery = "INSERT INTO `{$dbprefix}member_tj` (`mid`,`article`,`album`,`archives`,`homecount`,`pagecount`,`feedback`,`friend`,`stow`) VALUES ('1','0','0','0','0','0','0','0','0'); ";
  433. $db->exec($adminquery);
  434. $adminquery = "INSERT INTO `{$dbprefix}member_space` (`mid`,`pagesize`,`matt`,`spacename`,`spacelogo`,`spacestyle`,`sign`,`spacenews`) VALUES ('1','10','0','{$adminuser}的空间','','person','',''); ";
  435. $db->exec($adminquery);
  436. DedeCli::write("admin user:admin");
  437. DedeCli::write("admin password:admin");
  438. if (phpversion() < "5.4") {
  439. die("DedeBIZ:command web server not support\n\n");
  440. }
  441. // 写入程序安装锁
  442. file_put_contents($workDir.'/install/install_lock.txt', 'ok');
  443. echo "Start Dev Server For DedeBIZ\n\r";
  444. echo "Open http://localhost:8088\n\r";
  445. passthru(PHP_BINARY . ' -S localhost:8088 -t' . escapeshellarg('./'));
  446. exit;
  447. } else {
  448. echo $helpStr;
  449. }