国内流行的内容管理系统(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. if (phpversion() < "5.4") {
  74. die("DedeBIZ:command web server not support\n\n");
  75. }
  76. echo "Start Dev Server For DedeBIZ\n\r";
  77. echo "Open http://localhost:8088\n\r";
  78. passthru(PHP_BINARY . ' -S localhost:8088 -t' . escapeshellarg('./'));
  79. } else if (count($argv) > 1 && ($argv[1] == "make" || $argv[1] == "m")) {
  80. if (!file_exists($workDir . "/system/common.inc.php")) {
  81. DedeCli::error("Check your root path is right");
  82. exit;
  83. }
  84. require_once($workDir . "/system/common.inc.php");
  85. require_once(DEDEINC . "/libraries/cli.class.php");
  86. //一个命令行的生成工具
  87. if (count($argv) > 2 && ($argv[2] == "arc" || $argv[2] == "a")) {
  88. //生成文档
  89. //make arc typeid=1
  90. $t1 = ExecTime();
  91. $addsql = "1=1";
  92. $typeid = Option2SQLin(DedeCli::getOption("typeid"));
  93. if (!empty($typeid)) {
  94. $addsql .= " AND typeid IN(" . $typeid . ")";
  95. }
  96. $aid = Option2SQLin(DedeCli::getOption("aid"));
  97. if (!empty($aid)) {
  98. $addsql .= " AND id IN(" . $typeid . ")";
  99. }
  100. $tt = $dsql->GetOne("SELECT COUNT(id) as dd FROM `#@__arctiny` WHERE " . $addsql);
  101. $total = intval($tt['dd']);
  102. $dsql->Execute('out', "SELECT id FROM `#@__arctiny` WHERE " . $addsql . " ORDER BY typeid ASC");
  103. $i = 0;
  104. while ($row = $dsql->GetObject('out')) {
  105. $id = $row->id;
  106. $ac = new Archives($id);
  107. $rurl = $ac->MakeHtml(0);
  108. DedeCli::showProgress(ceil(($i / $total) * 100), 100, $i, $total);
  109. $i++;
  110. }
  111. DedeCli::write("Make archive html successfull");
  112. $queryTime = ExecTime() - $t1;
  113. DedeCli::write($queryTime);
  114. exit;
  115. } else if (count($argv) > 2 && ($argv[2] == "list" || $argv[2] == "l")) {
  116. //生成栏目
  117. $addsql = "1=1";
  118. $typeid = Option2SQLin(DedeCli::getOption("typeid"));
  119. if (!empty($typeid)) {
  120. $addsql .= " AND id IN(" . $typeid . ")";
  121. }
  122. $dsql->Execute('out', "SELECT id,channeltype FROM `#@__arctype` WHERE " . $addsql);
  123. $i = 0;
  124. while ($row = $dsql->GetObject('out')) {
  125. if ($row->channeltype > 0) {
  126. $lv = new ListView($row->id);
  127. } else {
  128. $lv = new SgListView($row->id);
  129. }
  130. $lv->CountRecord();
  131. DedeCli::write("Start make list html[id:{$row->id}]");
  132. $lv->MakeHtml('', '', 0);
  133. }
  134. exit;
  135. } else if (count($argv) > 2 && ($argv[2] == "index" || $argv[2] == "i")) {
  136. //生成首页
  137. $position = DedeCli::getOption("position");
  138. if (empty($position)) {
  139. $position = "../index.html";
  140. }
  141. if (!preg_match("#\.html$#", $position)) {
  142. DedeCli::error("position must end with .html");
  143. exit;
  144. }
  145. $homeFile = DEDEINC . "/" . $position;
  146. $homeFile = str_replace("\\", "/", $homeFile);
  147. $homeFile = str_replace("//", "/", $homeFile);
  148. $row = $dsql->GetOne("SELECT * FROM `#@__homepageset`");
  149. $templet = $row['templet'];
  150. $templet = str_replace("{style}", $cfg_df_style, $templet);
  151. $pv = new PartView();
  152. $GLOBALS['_arclistEnv'] = 'index';
  153. $pv->SetTemplet($cfg_basedir . $cfg_templets_dir . "/" . $templet);
  154. $pv->SaveToHtml($homeFile);
  155. DedeCli::write("Make index html successfull");
  156. } else if (count($argv) > 2 && ($argv[2] == "auto" || $argv[2] == "o")) {
  157. //自动生成
  158. function OptimizeData($dsql)
  159. {
  160. global $cfg_dbprefix;
  161. $tptables = array("{$cfg_dbprefix}archives", "{$cfg_dbprefix}arctiny");
  162. $dsql->SetQuery("SELECT maintable,addtable FROM `#@__channeltype` ");
  163. $dsql->Execute();
  164. while ($row = $dsql->GetObject()) {
  165. $addtable = str_replace('#@__', $cfg_dbprefix, $row->addtable);
  166. if ($addtable != '' && !in_array($addtable, $tptables)) $tptables[] = $addtable;
  167. }
  168. $tptable = '';
  169. foreach ($tptables as $t) $tptable .= ($tptable == '' ? "`{$t}`" : ",`{$t}`");
  170. $dsql->ExecuteNoneQuery(" OPTIMIZE TABLE $tptable; ");
  171. }
  172. $start = empty(DedeCli::getOption("start"))? "-1 day" : DedeCli::getOption("start");
  173. $start = strtotime($start);
  174. if (!$start) {
  175. DedeCli::error("start is empty");
  176. exit;
  177. }
  178. //1.生成首页
  179. $pv = new PartView();
  180. $row = $pv->dsql->GetOne("SELECT * FROM `#@__homepageset` ");
  181. $templet = str_replace("{style}", $cfg_df_style, $row['templet']);
  182. $homeFile = DEDEINC . '/' . $row['position'];
  183. $homeFile = str_replace("\\", '/', $homeFile);
  184. $homeFile = preg_replace("#\/{1,}#", '/', $homeFile);
  185. if ($row['showmod'] == 1) {
  186. $pv->SetTemplet($cfg_basedir . $cfg_templets_dir . '/' . $templet);
  187. $pv->SaveToHtml($homeFile);
  188. $pv->Close();
  189. } else {
  190. if (file_exists($homeFile)) @unlink($homeFile);
  191. }
  192. DedeCli::write("Make index html successfull");
  193. //2.生成栏目
  194. $query = "SELECT DISTINCT typeid From `#@__arctiny` WHERE senddate >=" . $start . " AND arcrank>-1";
  195. $dsql->SetQuery($query);
  196. $dsql->Execute();
  197. $typeids = array();
  198. while ($row = $dsql->GetArray()) {
  199. $typeids[$row['typeid']] = 1;
  200. }
  201. if (count($typeids) > 0) {
  202. foreach ($typeids as $k => $v) {
  203. $vs = array();
  204. $vs = GetParentIds($k);
  205. if (!isset($typeidsok[$k])) {
  206. $typeidsok[$k] = 1;
  207. }
  208. foreach ($vs as $k => $v) {
  209. if (!isset($typeidsok[$v])) {
  210. $typeidsok[$v] = 1;
  211. }
  212. }
  213. }
  214. foreach ($typeidsok as $tt=> $k) {
  215. $row = $dsql->GetOne("SELECT id,channeltype FROM `#@__arctype` WHERE id=".$tt);
  216. if ($row['channeltype'] > 0) {
  217. $lv = new ListView($tt);
  218. } else {
  219. $lv = new SgListView($tt);
  220. }
  221. $lv->CountRecord();
  222. DedeCli::write("Start make list html[id:{$tt}]");
  223. $lv->MakeHtml('', '', 0);
  224. }
  225. DedeCli::write("Make list html successfull");
  226. }
  227. //生成文档
  228. $tt = $dsql->GetOne("SELECT COUNT(id) as dd FROM `#@__arctiny` WHERE senddate >=" . $start . " AND arcrank>-1");
  229. $total = intval($tt['dd']);
  230. $dsql->Execute('out', "SELECT id FROM `#@__arctiny` WHERE senddate >=" . $start . " AND arcrank>-1 ORDER BY typeid ASC");
  231. $i = 0;
  232. while ($row = $dsql->GetObject('out')) {
  233. $id = $row->id;
  234. $ac = new Archives($id);
  235. $rurl = $ac->MakeHtml(0);
  236. DedeCli::showProgress(ceil(($i / $total) * 100), 100);
  237. $i++;
  238. }
  239. DedeCli::write("Make archives html successfull");
  240. //优化数据
  241. OptimizeData($dsql);
  242. DedeCli::write("Optimize data successfull");
  243. } else {
  244. $helpStr = "
  245. USAGE:
  246. php ./dedebiz make action [arguments...]
  247. ACTIONS:
  248. index,i Make Index html
  249. --position index html position,default: ../index.html(relative include dir)
  250. arc,a Make Archive htmls
  251. --typeid type id
  252. --aid archive id
  253. list,l Make List htmls
  254. --typeid type id
  255. auto,o Auto Make htmls
  256. --start start time(format:2012-03-12)
  257. WEBSITE:
  258. https://www.dedebiz.com/help/";
  259. DedeCli::write($helpStr);
  260. exit;
  261. }
  262. } else if (count($argv) > 1 && ($argv[1] == "update" || $argv[1] == "u")) {
  263. define("DEDEINC", $workDir."/system");
  264. require_once(DEDEINC."/dedehttpdown.class.php");
  265. require_once(DEDEINC . "/libraries/cli.class.php");
  266. //更新系统
  267. $latestURL = "https://cdn.dedebiz.com/release/latest.txt";
  268. $del = new DedeHttpDown();
  269. $del->OpenUrl($latestURL);
  270. $remoteVerStr = $del->GetHtml();
  271. $commStr = file_get_contents(DEDEINC."/common.inc.php");
  272. preg_match("#_version_detail = '([\d\.]+)'#", $commStr, $matchs);
  273. $cfg_version_detail = $localVerStr = $matchs[1];
  274. if (version_compare($localVerStr, $remoteVerStr, '>=')) {
  275. DedeCli::error("latest version,don't need to update");
  276. exit;
  277. }
  278. $fileHashURL = "https://cdn.dedebiz.com/release/{$cfg_version_detail}.json";
  279. $del = new DedeHttpDown();
  280. $del->OpenUrl($fileHashURL);
  281. $filelist = $del->GetJSON();
  282. $offFiles = array();
  283. //TODO 命令行自动更新
  284. } else if (count($argv) > 1 && ($argv[1] == "quick" || $argv[1] == "q")){
  285. define("DEDEINC", $workDir."/system");
  286. require_once(DEDEINC . "/libraries/cli.class.php");
  287. //快速开始一个用于开发的DedeBIZ环境,基于SQLite无其他依赖
  288. if (file_exists($workDir."/data/DedeBIZ.db")) {
  289. DedeCli::write("development environment has inited");
  290. echo "Start Dev Server For DedeBIZ\n\r";
  291. echo "Open http://localhost:8088\n\r";
  292. passthru(PHP_BINARY . ' -S localhost:8088 -t' . escapeshellarg('./'));
  293. exit;
  294. }
  295. //初始化安装一个开发环境
  296. $db = new SQLite3($workDir.'/data/DedeBIZ.db');
  297. $fp = fopen($workDir."/install/common.inc.php","r");
  298. $configStr1 = fread($fp,filesize($workDir."/install/common.inc.php"));
  299. fclose($fp);
  300. @chmod($workDir."/data",0777);
  301. $dbtype = "sqlite";
  302. $dbhost = "";
  303. $dbname = "DedeBIZ";
  304. $dbuser = "";
  305. $dbpwd = "";
  306. $dbprefix = "dede_";
  307. $dblang = "utf8";
  308. if (!is_dir($workDir.'/data/tplcache')) {
  309. mkdir($workDir.'/data/tplcache', 0777);
  310. }
  311. //common.inc.php
  312. $configStr1 = str_replace("~dbtype~",$dbtype,$configStr1);
  313. $configStr1 = str_replace("~dbhost~",$dbhost,$configStr1);
  314. $configStr1 = str_replace("~dbname~",$dbname,$configStr1);
  315. $configStr1 = str_replace("~dbuser~",$dbuser,$configStr1);
  316. $configStr1 = str_replace("~dbpwd~",$dbpwd,$configStr1);
  317. $configStr1 = str_replace("~dbprefix~",$dbprefix,$configStr1);
  318. $configStr1 = str_replace("~dblang~",$dblang,$configStr1);
  319. $fp = fopen($workDir."/data/common.inc.php","w") or die("error,check /data writeable");
  320. fwrite($fp,$configStr1);
  321. fclose($fp);
  322. $cookieencode = RandEncode(26);
  323. $baseurl = "http://127.0.0.1:8088";
  324. $indexUrl = "/";
  325. $cmspath = "";
  326. $webname = "DedeBIZ本地测试开发站点";
  327. $adminmail = "admin@dedebiz.com";
  328. $fp = fopen($workDir."/install/config.cache.inc.php","r");
  329. $configStr2 = fread($fp,filesize($workDir."/install/config.cache.inc.php"));
  330. fclose($fp);
  331. $configStr2 = str_replace("~baseurl~",$baseurl,$configStr2);
  332. $configStr2 = str_replace("~basepath~",$cmspath,$configStr2);
  333. $configStr2 = str_replace("~indexurl~",$indexUrl,$configStr2);
  334. $configStr2 = str_replace("~cookieEncode~",$cookieencode,$configStr2);
  335. $configStr2 = str_replace("~webname~",$webname,$configStr2);
  336. $configStr2 = str_replace("~adminmail~",$adminmail,$configStr2);
  337. $fp = fopen($workDir.'/data/config.cache.inc.php','w');
  338. fwrite($fp,$configStr2);
  339. fclose($fp);
  340. $fp = fopen($workDir.'/data/config.cache.bak.php','w');
  341. fwrite($fp,$configStr2);
  342. fclose($fp);
  343. $query = '';
  344. $fp = fopen($workDir.'/install/sql-dftables.txt','r');
  345. while (!feof($fp))
  346. {
  347. $line = rtrim(fgets($fp,1024));
  348. if (preg_match("#;$#", $line))
  349. {
  350. $query .= $line."\n";
  351. $query = str_replace('#@__',$dbprefix,$query);
  352. $query = preg_replace('/character set (.*?) /i','',$query);
  353. $query = str_replace('unsigned','',$query);
  354. $query = str_replace('TYPE=MyISAM','',$query);
  355. $query = preg_replace ('/TINYINT\(([\d]+)\)/i','INTEGER',$query);
  356. $query = preg_replace ('/mediumint\(([\d]+)\)/i','INTEGER',$query);
  357. $query = preg_replace ('/smallint\(([\d]+)\)/i','INTEGER',$query);
  358. $query = preg_replace('/int\(([\d]+)\)/i','INTEGER',$query);
  359. $query = preg_replace('/auto_increment/i','PRIMARY KEY AUTOINCREMENT',$query);
  360. $query = preg_replace('/, KEY(.*?)MyISAM;/','',$query);
  361. $query = preg_replace('/, KEY(.*?);/',');',$query);
  362. $query = preg_replace('/, UNIQUE KEY(.*?);/',');',$query);
  363. $query = preg_replace('/set\(([^\)]*?)\)/','varchar',$query);
  364. $query = preg_replace('/enum\(([^\)]*?)\)/','varchar',$query);
  365. if (preg_match("/PRIMARY KEY AUTOINCREMENT/",$query)) {
  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. $query .= $line;
  373. }
  374. }
  375. fclose($fp);
  376. //导入默认数据
  377. $query = '';
  378. $fp = fopen($workDir.'/install/sql-dfdata.txt','r');
  379. while (!feof($fp))
  380. {
  381. $line = rtrim(fgets($fp, 1024));
  382. if (preg_match("#;$#", $line)) {
  383. $query .= $line;
  384. $query = str_replace('#@__',$dbprefix,$query);
  385. $query = str_replace("\'","\"",$query);
  386. $query = str_replace('\t\n\n',"",$query);
  387. $query = str_replace('\t\n',"",$query);
  388. @$db->exec($query);
  389. $query='';
  390. } else if (!preg_match("#^(\/\/|--)#", $line)) {
  391. $query .= $line;
  392. }
  393. }
  394. fclose($fp);
  395. //更新配置
  396. $cquery = "UPDATE `{$dbprefix}sysconfig` SET value='{$baseurl}' WHERE varname='cfg_basehost';";
  397. $db->exec($cquery);
  398. $cquery = "UPDATE `{$dbprefix}sysconfig` SET value='{$cmspath}' WHERE varname='cfg_cmspath';";
  399. $db->exec($cquery);
  400. $cquery = "UPDATE `{$dbprefix}sysconfig` SET value='{$indexUrl}' WHERE varname='cfg_indexurl';";
  401. $db->exec($cquery);
  402. $cquery = "UPDATE `{$dbprefix}sysconfig` SET value='{$cookieencode}' WHERE varname='cfg_cookie_encode';";
  403. $db->exec($cquery);
  404. $cquery = "UPDATE `{$dbprefix}sysconfig` SET value='{$webname}' WHERE varname='cfg_webname';";
  405. $db->exec($cquery);
  406. $cquery = "UPDATE `{$dbprefix}sysconfig` SET value='{$adminmail}' WHERE varname='cfg_adminemail';";
  407. $db->exec($cquery);
  408. $adminuser = "admin";
  409. $adminpwd = "admin";
  410. //增加管理员帐号
  411. $pfd = "pwd";
  412. $apwd = substr(md5($adminpwd),5,20);
  413. $upwd = md5($adminpwd);
  414. if (function_exists('password_hash')) {
  415. $pfd = "pwd_new";
  416. $apwd = password_hash($adminpwd, PASSWORD_BCRYPT);
  417. $upwd = password_hash($adminpwd, PASSWORD_BCRYPT);
  418. }
  419. //增加管理员帐号
  420. $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');";
  421. $db->exec($adminquery);
  422. //关连前台会员帐号
  423. $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',''); ";
  424. $db->exec($adminquery);
  425. $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'); ";
  426. $db->exec($adminquery);
  427. $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'); ";
  428. $db->exec($adminquery);
  429. $adminquery = "INSERT INTO `{$dbprefix}member_space` (`mid`,`pagesize`,`matt`,`spacename`,`spacelogo`,`spacestyle`,`sign`,`spacenews`) VALUES ('1','10','0','{$adminuser}的空间','','person','',''); ";
  430. $db->exec($adminquery);
  431. DedeCli::write("admin user:admin");
  432. DedeCli::write("admin password:admin");
  433. if (phpversion() < "5.4") {
  434. die("DedeBIZ:command web server not support\n\n");
  435. }
  436. //写入程序安装锁
  437. file_put_contents($workDir.'/install/install_lock.txt', 'ok');
  438. echo "Start Dev Server For DedeBIZ\n\r";
  439. echo "Open http://localhost:8088\n\r";
  440. passthru(PHP_BINARY . ' -S localhost:8088 -t' . escapeshellarg('./'));
  441. exit;
  442. } else {
  443. echo $helpStr;
  444. }
  445. ?>