国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

471 linhas
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("DedeBIZ: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 DedeBIZ
  25. make,m Make DedeBIZ 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("DedeBIZ:command web server not support\n\n");
  74. }
  75. echo "Start Dev Server For DedeBIZ\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. // TODO 命令行自动更新
  283. } else if (count($argv) > 1 && ($argv[1] == "quick" || $argv[1] == "q")){
  284. define("DEDEINC", $workDir."/include");
  285. require_once(DEDEINC . "/cli.class.php");
  286. // 快速开始一个用于开发的DedeBIZ环境,基于SQLite无其他依赖
  287. if (file_exists($workDir."/data/DedeBIZ.db")) {
  288. DedeCli::write("development environment has inited");
  289. echo "Start Dev Server For DedeBIZ\n\r";
  290. echo "Open http://localhost:8088\n\r";
  291. passthru(PHP_BINARY . ' -S localhost:8088 -t' . escapeshellarg('./'));
  292. exit;
  293. }
  294. // 初始化安装一个开发环境
  295. $db = new SQLite3($workDir.'/data/DedeBIZ.db');
  296. $fp = fopen($workDir."/install/common.inc.php","r");
  297. $configStr1 = fread($fp,filesize($workDir."/install/common.inc.php"));
  298. fclose($fp);
  299. @chmod($workDir."/data",0777);
  300. $dbtype = "sqlite";
  301. $dbhost = "";
  302. $dbname = "DedeBIZ";
  303. $dbuser = "";
  304. $dbpwd = "";
  305. $dbprefix = "dede_";
  306. $dblang = "utf8";
  307. //common.inc.php
  308. $configStr1 = str_replace("~dbtype~",$dbtype,$configStr1);
  309. $configStr1 = str_replace("~dbhost~",$dbhost,$configStr1);
  310. $configStr1 = str_replace("~dbname~",$dbname,$configStr1);
  311. $configStr1 = str_replace("~dbuser~",$dbuser,$configStr1);
  312. $configStr1 = str_replace("~dbpwd~",$dbpwd,$configStr1);
  313. $configStr1 = str_replace("~dbprefix~",$dbprefix,$configStr1);
  314. $configStr1 = str_replace("~dblang~",$dblang,$configStr1);
  315. $fp = fopen($workDir."/data/common.inc.php","w") or die("error,check ../data writeable");
  316. fwrite($fp,$configStr1);
  317. fclose($fp);
  318. $cookieencode = RandEncode(26);
  319. $baseurl = "http://127.0.0.1:8088";
  320. $indexUrl = "/";
  321. $cmspath = "";
  322. $webname = "DedeBIZ本地测试开发站点";
  323. $adminmail = "admin@dedebiz.com";
  324. $fp = fopen($workDir."/install/config.cache.inc.php","r");
  325. $configStr2 = fread($fp,filesize($workDir."/install/config.cache.inc.php"));
  326. fclose($fp);
  327. $configStr2 = str_replace("~baseurl~",$baseurl,$configStr2);
  328. $configStr2 = str_replace("~basepath~",$cmspath,$configStr2);
  329. $configStr2 = str_replace("~indexurl~",$indexUrl,$configStr2);
  330. $configStr2 = str_replace("~cookieEncode~",$cookieencode,$configStr2);
  331. $configStr2 = str_replace("~webname~",$webname,$configStr2);
  332. $configStr2 = str_replace("~adminmail~",$adminmail,$configStr2);
  333. $fp = fopen($workDir.'/data/config.cache.inc.php','w');
  334. fwrite($fp,$configStr2);
  335. fclose($fp);
  336. $fp = fopen($workDir.'/data/config.cache.bak.php','w');
  337. fwrite($fp,$configStr2);
  338. fclose($fp);
  339. $query = '';
  340. $fp = fopen($workDir.'/install/sql-dftables.txt','r');
  341. while(!feof($fp))
  342. {
  343. $line = rtrim(fgets($fp,1024));
  344. if(preg_match("#;$#", $line))
  345. {
  346. $query .= $line."\n";
  347. $query = str_replace('#@__',$dbprefix,$query);
  348. $query = preg_replace('/character set (.*?) /i','',$query);
  349. $query = str_replace('unsigned','',$query);
  350. $query = str_replace('TYPE=MyISAM','',$query);
  351. $query = preg_replace ('/TINYINT\(([\d]+)\)/i','INTEGER',$query);
  352. $query = preg_replace ('/mediumint\(([\d]+)\)/i','INTEGER',$query);
  353. $query = preg_replace ('/smallint\(([\d]+)\)/i','INTEGER',$query);
  354. $query = preg_replace('/int\(([\d]+)\)/i','INTEGER',$query);
  355. $query = preg_replace('/auto_increment/i','PRIMARY KEY AUTOINCREMENT',$query);
  356. $query = preg_replace('/, KEY(.*?)MyISAM;/','',$query);
  357. $query = preg_replace('/, KEY(.*?);/',');',$query);
  358. $query = preg_replace('/, UNIQUE KEY(.*?);/',');',$query);
  359. $query = preg_replace('/set\(([^\)]*?)\)/','varchar',$query);
  360. $query = preg_replace('/enum\(([^\)]*?)\)/','varchar',$query);
  361. if ( preg_match("/PRIMARY KEY AUTOINCREMENT/",$query) )
  362. {
  363. $query = preg_replace('/,([\t\s ]+)PRIMARY KEY \(`([0-9a-zA-Z]+)`\)/i','',$query);
  364. $query = str_replace(', PRIMARY KEY (`id`)','',$query);
  365. }
  366. @$db->exec($query);
  367. $query='';
  368. } else if(!preg_match("#^(\/\/|--)#", $line))
  369. {
  370. $query .= $line;
  371. }
  372. }
  373. fclose($fp);
  374. //导入默认数据
  375. $query = '';
  376. $fp = fopen($workDir.'/install/sql-dfdata.txt','r');
  377. while(!feof($fp))
  378. {
  379. $line = rtrim(fgets($fp, 1024));
  380. if(preg_match("#;$#", $line))
  381. {
  382. $query .= $line;
  383. $query = str_replace('#@__',$dbprefix,$query);
  384. $query = str_replace("\'","\"",$query);
  385. $query = str_replace('\t\n\n',"",$query);
  386. $query = str_replace('\t\n',"",$query);
  387. @$db->exec($query);
  388. $query='';
  389. } else if(!preg_match("#^(\/\/|--)#", $line))
  390. {
  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. $adminquery = "INSERT INTO `{$dbprefix}admin` VALUES (1, 10, '$adminuser', '".substr(md5($adminpwd),5,20)."', 'admin', '', '', 0, '".time()."', '127.0.0.1');";
  412. $db->exec($adminquery);
  413. DedeCli::write("admin user:admin");
  414. DedeCli::write("admin password:admin");
  415. //关连前台会员帐号
  416. $adminquery = "INSERT INTO `{$dbprefix}member` (`mid`,`mtype`,`userid`,`pwd`,`uname`,`sex`,`rank`,`money`,`email`,
  417. `scores` ,`matt` ,`face`,`safequestion`,`safeanswer` ,`jointime` ,`joinip` ,`logintime` ,`loginip` )
  418. VALUES ('1','个人','$adminuser','".md5($adminpwd)."','$adminuser','男','100','0','','10000','10','','0','','".time()."','','0',''); ";
  419. $db->exec($adminquery);
  420. $adminquery = "INSERT INTO `{$dbprefix}member_person` (`mid`,`onlynet`,`sex`,`uname`,`qq`,`msn`,`tel`,`mobile`,`place`,`oldplace`,`birthday`,`star`,
  421. `income` , `education` , `height` , `bodytype` , `blood` , `vocation` , `smoke` , `marital` , `house` ,`drink` , `datingtype` , `language` , `nature` , `lovemsg` , `address`,`uptime`)
  422. VALUES ('1', '1', '男', '{$adminuser}', '', '', '', '', '0', '0','1980-01-01', '1', '0', '0', '160', '0', '0', '0', '0', '0', '0','0', '0', '', '', '', '','0'); ";
  423. $db->exec($adminquery);
  424. $adminquery = "INSERT INTO `{$dbprefix}member_tj` (`mid`,`article`,`album`,`archives`,`homecount`,`pagecount`,`feedback`,`friend`,`stow`)
  425. VALUES ('1','0','0','0','0','0','0','0','0'); ";
  426. $db->exec($adminquery);
  427. $adminquery = "Insert Into `{$dbprefix}member_space`(`mid` ,`pagesize` ,`matt` ,`spacename` ,`spacelogo` ,`spacestyle`, `sign` ,`spacenews`)
  428. Values('1','10','0','{$adminuser}的空间','','person','',''); ";
  429. $db->exec($adminquery);
  430. if (phpversion() < "5.4") {
  431. die("DedeBIZ:command web server not support\n\n");
  432. }
  433. echo "Start Dev Server For DedeBIZ\n\r";
  434. echo "Open http://localhost:8088\n\r";
  435. passthru(PHP_BINARY . ' -S localhost:8088 -t' . escapeshellarg('./'));
  436. exit;
  437. } else {
  438. echo $helpStr;
  439. }