BaseCms.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. <?php
  2. /* cms发布设置
  3. * 自定义cmsApp要求
  4. * 类名必须驼峰命名法不能用下划线
  5. */
  6. namespace plugin\release\cms;
  7. use skycaiji\admin\model\DbCommon;
  8. abstract class BaseCms extends \skycaiji\admin\event\ReleaseBase{
  9. public $release;//发布对象数据
  10. public $releConfig;//发布配置
  11. public $cmsDb;//cms数据库配置
  12. protected $db;//数据库对象
  13. public $cmsApp;//当前Cms app
  14. public $cmsPath;//当前Cms路径
  15. public $_params;//发布设置cms绑定参数
  16. protected $paramHtmlList=array();//参数对应的html代码
  17. public function __construct(){
  18. parent::__construct();
  19. if(config('app_debug')!=true){
  20. config('exception_tmpl',config('app_path').'/public/release_exception.tpl');//定义cms错误模板,ajax出错时方便显示
  21. }
  22. }
  23. public function init($cmsPath=null,$release=null){
  24. $cmsDb=array();//cms程序数据库配置
  25. if(empty($release)){
  26. $release=array();
  27. }
  28. if(!empty($cmsPath)){
  29. //通过cms名和路径加载配置
  30. $release['config']['cms']['name']=$this->cms_name($cmsPath);
  31. $release['config']['cms']['path']=$cmsPath;
  32. }
  33. if(!empty($release)){
  34. //通过已入库的发布设置加载配置
  35. $releConfig=$release['config'];
  36. if(strpos($releConfig['cms']['path'], '@')!==false){
  37. //路径中指定了cms
  38. list($cmsPath,$cmsPathName)=explode('@', $releConfig['cms']['path']);//指定程序名
  39. $releConfig['cms']['path']=$cmsPath;//换成有效的路径
  40. $releConfig['cms']['name']=$cmsPathName;//重新设置cms
  41. }
  42. exception('111:!'.$releConfig['cms']['name']."====".$releConfig['cms']['path']);
  43. $cmsDb=$this->cmsDb($releConfig['cms']['name'], $releConfig['cms']['path']);
  44. exception('11dd1:!'.json_encode($cmsDb));
  45. $this->releConfig=$releConfig;//发布数据库配置
  46. $this->release=$release;
  47. $this->cmsPath=$release['config']['cms']['path'];
  48. }else{
  49. exception('发布错误:配置加载失败!');
  50. }
  51. if(empty($cmsDb)||empty($cmsDb['db_name'])){
  52. //数据库不为空情况下需判断db_name
  53. exception('发布错误:没有数据库配置'.$cmsDb['db_name']."fuck");
  54. }
  55. $cmsDb['db_type']=empty($cmsDb['db_type'])?'mysql':strtolower($cmsDb['db_type']);
  56. $cmsDb['db_port']=$cmsDb['db_port']>0?$cmsDb['db_port']:3306;//设置默认端口
  57. $cmsDb['fields_strict']=false;//允许字段不存在
  58. $this->cmsDb=$cmsDb;//cms程序配置
  59. //实例化数据库
  60. try {
  61. $mdb=new DbCommon($cmsDb);
  62. $this->db=$mdb->db();
  63. }catch (\Exception $ex){
  64. exception('发布错误:'.$ex->getMessage());
  65. }
  66. $this->cmsApp=get_class($this);//类名
  67. $this->init_extend();//自定义执行操作
  68. }
  69. /*扩展初始化*/
  70. public function init_extend(){}
  71. /**
  72. * 参照thinkphp5数据库操作
  73. * @return Ambigous <\think\db\Query, NULL>
  74. */
  75. public function db(){
  76. return $this->db;
  77. }
  78. /**
  79. * 导出数据
  80. * @param unknown $collFields 采集到的字段数据
  81. */
  82. public function runExport($collFields){
  83. //数据库编码
  84. $dbCharset=strtolower($this->cmsDb['db_charset']);
  85. if(empty($dbCharset)||$dbCharset=='utf-8'||$dbCharset=='utf8'){
  86. //不转码
  87. $dbCharset=null;
  88. }
  89. //转换cms参数
  90. $cmsParams=array();
  91. foreach ($this->releConfig['cms_app']['param'] as $cmsParam=>$paramVal){
  92. if(strcasecmp('custom:',$paramVal)==0){
  93. //自定义
  94. $paramVal=$this->releConfig['cms_app']['custom'][$cmsParam];
  95. }elseif(preg_match('/^field\:(.+)$/i', $paramVal,$collField)){
  96. //采集器字段
  97. $paramVal=$this->get_field_val($collFields[$collField[1]]);
  98. }
  99. if(!empty($dbCharset)){
  100. //转码
  101. $paramVal=$this->utf8_to_charset($dbCharset, $paramVal);
  102. }
  103. $cmsParams[$cmsParam]=$paramVal;
  104. }
  105. if(!empty($this->_params)){
  106. //验证参数
  107. $errorMsg=false;//错误信息
  108. foreach ($this->_params as $pkey=>$pval){
  109. if($pval['require']){
  110. //必填项
  111. if(empty($cmsParams[$pkey])){
  112. $errorMsg='未获取到“'.$pval['name'].'”';
  113. break;
  114. }
  115. }
  116. }
  117. if(!empty($errorMsg)){
  118. return array('id'=>0,'error'=>$errorMsg);//返回错误信息
  119. }
  120. }
  121. return $this->runImport($cmsParams);
  122. }
  123. /**
  124. * 导入数据
  125. * @param unknown $params cms参数
  126. */
  127. public abstract function runImport($params);
  128. public function runTest($collFields){
  129. //事务回滚仅对InnoDB类型表起作用
  130. $this->db()->startTrans();//开启事务
  131. $this->runExport($collFields);
  132. $this->db()->rollback();//回滚事务
  133. }
  134. /*运行绑定*/
  135. public function runBind(){}
  136. /*运行检测*/
  137. public function runCheck($config){
  138. //设置了参数是进行验证
  139. if(empty($this->_params)){
  140. return;
  141. }
  142. foreach ($this->_params as $pkey=>$pval){
  143. //必填参数
  144. if($pval['require']){
  145. if($config['param'][$pkey]==''){
  146. //没有值
  147. $this->error($pval['name'].'不能为空');
  148. }elseif('custom:'==$config['param'][$pkey]){
  149. //自定义
  150. if(!isset($config['custom'][$pkey])||preg_match('/^\s*$/', $config['custom'][$pkey])){
  151. //自定义不能为空
  152. $this->error($pval['name'].'不能为空');
  153. }
  154. }
  155. }
  156. }
  157. }
  158. /*显示绑定模板*/
  159. public function tplBind(){
  160. if(!empty($this->_params)){
  161. //设置了参数
  162. $paramTags=array();
  163. foreach ($this->_params as $paramKey=>$paramVal){
  164. $paramTags[$paramKey]=$this->convert_param2html($paramKey, $paramVal);
  165. }
  166. $this->assign('paramTags',$paramTags);
  167. $this->assign('_params',$this->_params);
  168. $tpl=$this->fetch(config('plugin_path').'/release/view/cms/BaseCms.html');
  169. return $tpl->getContent();
  170. }else{
  171. $sltCollField=$this->param_option_fields();
  172. $this->assign('sltCollField',$sltCollField);
  173. //没有参数调用模板,复杂需求下可使用模板
  174. $tpl=$this->fetch(config('plugin_path').'/release/view/cms/'.$this->cmsApp.'.html');
  175. return $tpl->getContent();
  176. }
  177. }
  178. /*获取cms配置,可在app中自定义该方法*/
  179. public function cmsDb($cmsName,$cmsPath){
  180. $cmsDb=array();
  181. if(!empty($cmsName)&&!empty($cmsPath)){
  182. $method='l'.$cmsName;
  183. if(method_exists($this, $method)){
  184. $cmsDb=$this->$method($cmsPath);
  185. }
  186. }
  187. // exception("fucks:".json_encode($cmsDb));
  188. // print_r($cmsDb);
  189. return $cmsDb;
  190. }
  191. /*获取cms名称*/
  192. public function cms_name($cmsPath){
  193. $acms=controller('admin/Rcms','event');
  194. return $acms->cms_name($cmsPath);//cms名称
  195. }
  196. /*转换参数成html标签*/
  197. public function convert_param2html($paramKey,$paramVal){
  198. if(!isset($this->paramHtmlList[$paramKey])){
  199. $html='';
  200. $tag=strtolower($paramVal['tag']);//html标签
  201. $func=null;//函数
  202. $options=array();//选项
  203. if(is_string($paramVal['option'])&&preg_match('/^function\:(.*)$/i',$paramVal['option'],$func)){
  204. //函数名
  205. $func=trim($func[1]);
  206. }elseif(!empty($paramVal['option'])){
  207. if(is_string($paramVal['option'])){
  208. //选项转换成数组
  209. $options=explode('|', $paramVal['option']);
  210. $optionList=array();
  211. foreach ($options as $option){
  212. if(strpos($option, '=')!==false){
  213. //有=号
  214. list($optionKey,$optionVal)=explode('=', $option);
  215. $optionList[$optionKey]=$optionVal;
  216. }
  217. }
  218. $options=$optionList;
  219. }elseif(is_array($paramVal['option'])){
  220. $options=$paramVal['option'];
  221. }
  222. }
  223. if('select'==$tag){
  224. $html.='<div class="input-group input-select-custom"><div class="input-group-btn"><select name="_cms_app_param_" class="form-control"><option value="">不选择</option>';
  225. if(!empty($func)){
  226. //调用函数
  227. if(method_exists($this, $func)){
  228. $funcData=$this->$func();
  229. if(is_array($funcData)){
  230. //返回的是数组
  231. foreach ($funcData as $fdk=>$fdv){
  232. $html.="<option value=\"{$fdk}\">{$fdv}</option>";
  233. }
  234. }else{
  235. //返回的是字符串
  236. $html.=$funcData;
  237. }
  238. }
  239. }elseif(!empty($options)){
  240. //选项
  241. foreach ($options as $optionKey=>$optionVal){
  242. $html.="<option value=\"{$optionKey}\">{$optionVal}</option>";
  243. }
  244. }
  245. $html.='<option value="custom:">自定义</option></select></div>'
  246. .'<input class="form-control" style="display:none;" name="cms_app[custom]['.$paramKey.']" /></div>';
  247. }elseif(in_array($tag,array('input','text','number'))){
  248. $html.='<input type="'.($tag=='input'?'text':$tag).'" name="_cms_app_param_" class="form-control" value="" />';
  249. }elseif('radio'==$tag){
  250. $html.='<label class="radio-inline"><input type="radio" name="_cms_app_param_" value="1" /> 是</label>';
  251. $html.='<label class="radio-inline"><input type="radio" name="_cms_app_param_" value="0" /> 否</label>';
  252. }elseif('textarea'==$tag){
  253. $html.='<textarea name="_cms_app_param_" class="form-control"></textarea>';
  254. }
  255. $this->paramHtmlList[$paramKey]=str_replace('_cms_app_param_', 'cms_app[param]['.$paramKey.']', $html);
  256. }
  257. return $this->paramHtmlList[$paramKey];
  258. }
  259. /*采集器字段选项*/
  260. public function param_option_fields(){
  261. if(empty($this->release)){
  262. return null;
  263. }
  264. $mtask=model('Task');
  265. $taskData=$mtask->getById($this->release['task_id']);
  266. if(empty($taskData)){
  267. return null;
  268. }
  269. $acms=controller('admin/Rcms','event');
  270. $collFields=$acms->get_coll_fields($taskData['id'],$taskData['module']);
  271. $sltCollField='';
  272. foreach($collFields as $collField){
  273. $sltCollField.="<option value=\"field:{$collField}\">采集字段:{$collField}</option>";
  274. }
  275. return $sltCollField;
  276. }
  277. /*引入文件必须用include,include_once在多个实例化后会失效*/
  278. public function cms_db_discuz($cmsPath){
  279. $dbFile=realpath($cmsPath.'/config/config_global.php');
  280. //转换成thinkphp数据库配置
  281. include $dbFile;//导入本地cms配置文件
  282. $_config=$_config?$_config:array();
  283. $cmsDb=array(
  284. 'db_type' => 'mysql',
  285. 'db_user' => $_config['db'][$_config['server']['id']]['dbuser'],
  286. 'db_pwd' => $_config['db'][$_config['server']['id']]['dbpw'],
  287. 'db_host' => $_config['db'][$_config['server']['id']]['dbhost'],
  288. 'db_port' => 3306,
  289. 'db_name' => $_config['db'][$_config['server']['id']]['dbname'],
  290. 'db_charset' => $_config['db'][$_config['server']['id']]['dbcharset'],
  291. 'db_prefix' => $_config['db'][$_config['server']['id']]['tablepre']
  292. );
  293. return $cmsDb;
  294. }
  295. public function cms_db_wordpress($cmsPath){
  296. $dbFile=realpath($cmsPath.'/wp-config.php');
  297. $configTxt=file_get_contents($dbFile);
  298. $cmsDb=array(
  299. 'db_user' => 'DB_USER',
  300. 'db_pwd' => 'DB_PASSWORD',
  301. 'db_host' => 'DB_HOST',
  302. 'db_name' => 'DB_NAME',
  303. 'db_charset' => 'DB_CHARSET',
  304. );
  305. //匹配定义的参数
  306. foreach ($cmsDb as $dbKey=>$dbDefine){
  307. if(preg_match('/define\s*\(\s*[\'\"]\s*'.$dbDefine.'\s*[\'\"]\s*\,\s*[\'\"](?P<val>[^\'\"]*?)[\'\"]\s*\)/i', $configTxt,$dbDefineVal)){
  308. $cmsDb[$dbKey]=$dbDefineVal['val'];
  309. }else{
  310. $cmsDb[$dbKey]='';
  311. }
  312. }
  313. //匹配表前缀
  314. if(preg_match('/\$table_prefix\s*=\s*[\'\"](?P<val>[^\'\"]*?)[\'\"]/i', $configTxt,$tablePre)){
  315. $cmsDb['db_prefix']=$tablePre['val'];
  316. }else{
  317. $cmsDb['db_prefix']='';
  318. }
  319. $cmsDb['db_type']='mysql';
  320. $cmsDb['db_port']=3306;
  321. return $cmsDb;
  322. }
  323. public function cms_db_empirecms($cmsPath){
  324. $dbFile=realpath($cmsPath.'/e/config/config.php');
  325. define('InEmpireCMS', true);//必须定义才能引入配置
  326. include $dbFile;
  327. $ecms_config=$ecms_config?$ecms_config:array();
  328. $cmsDb=array(
  329. 'db_type' => 'mysql',
  330. 'db_user' => $ecms_config['db']['dbusername'],
  331. 'db_pwd' => $ecms_config['db']['dbpassword'],
  332. 'db_host' => $ecms_config['db']['dbserver'],
  333. 'db_port' => $ecms_config['db']['dbport'],
  334. 'db_name' => $ecms_config['db']['dbname'],
  335. 'db_charset' => $ecms_config['db']['setchar'],
  336. 'db_prefix' => $ecms_config['db']['dbtbpre']
  337. );
  338. return $cmsDb;
  339. }
  340. public function cms_db_dedecms($cmsPath){
  341. $dbFile=realpath($cmsPath.'/data/common.inc.php');
  342. $cfg_dbuser=null;$cfg_dbpwd=null;$cfg_dbhost=null;$cfg_dbname=null;$cfg_db_language=null;$cfg_dbprefix=null;
  343. include $dbFile;
  344. $cmsDb=array(
  345. 'db_type' => 'mysql',
  346. 'db_user' => $cfg_dbuser,
  347. 'db_pwd' => $cfg_dbpwd,
  348. 'db_host' => $cfg_dbhost,
  349. 'db_port' => 3306,
  350. 'db_name' => $cfg_dbname,
  351. 'db_charset' => $cfg_db_language,
  352. 'db_prefix' => $cfg_dbprefix
  353. );
  354. return $cmsDb;
  355. }
  356. public function cms_db_phpcms($cmsPath){
  357. $dbFile=realpath($cmsPath.'/caches/configs/database.php');
  358. $config=include $dbFile;
  359. $cmsDb=array(
  360. 'db_type' => 'mysql',
  361. 'db_user' => $config['default']['username'],
  362. 'db_pwd' => $config['default']['password'],
  363. 'db_host' => $config['default']['hostname'],
  364. 'db_port' => $config['default']['port'],
  365. 'db_name' => $config['default']['database'],
  366. 'db_charset' => $config['default']['charset'],
  367. 'db_prefix' => $config['default']['tablepre']
  368. );
  369. return $cmsDb;
  370. }
  371. public function cms_db_metinfo($cmsPath){
  372. $dbFile=realpath($cmsPath.'/config/config_db.php');
  373. $config=parse_ini_file($dbFile);
  374. $cmsDb=array(
  375. 'db_type' => 'mysql',
  376. 'db_user' => $config['con_db_id'],
  377. 'db_pwd' => $config['con_db_pass'],
  378. 'db_host' => $config['con_db_host'],
  379. 'db_port' => 3306,
  380. 'db_name' => $config['con_db_name'],
  381. 'db_charset' => $config['db_charset'],
  382. 'db_prefix' => $config['tablepre']
  383. );
  384. return $cmsDb;
  385. }
  386. public function cms_db_emlog($cmsPath){
  387. $configFile=realpath($cmsPath.'/config.php');
  388. $cmsDb=array('db_type'=>'mysql','db_charset'=>'utf8');
  389. if(file_exists($configFile)){
  390. $configFile=file_get_contents($configFile);
  391. if($configFile){
  392. $dbKeys=array('db_host'=>'DB_HOST','db_user'=>'DB_USER','db_pwd'=>'DB_PASSWD','db_prefix'=>'DB_PREFIX','db_name'=>'DB_NAME');
  393. foreach($dbKeys as $k=>$v){
  394. if(preg_match('/define\s*\(\s*[\'\"]'.$v.'[\'\"]\s*,\s*[\'\"](?P<val>[^\'\"]+)[\'\"]\s*\)/i',$configFile,$val)){
  395. $cmsDb[$k]=$val['val'];
  396. }
  397. }
  398. }
  399. }
  400. return $cmsDb;
  401. }
  402. public function cms_db_drupal($cmsPath){
  403. $dbFile=realpath($cmsPath.'/sites/default/settings.php');
  404. $cmsDb=array();
  405. if(file_exists($dbFile)){
  406. $dbFile=file_get_contents($dbFile);
  407. if(preg_match('/\$databases\s*\=\s*array[\s\S]+?\)\s*\;/i',$dbFile,$dbFile)){
  408. $dbFile=$dbFile[0];
  409. $dbParams=array('db_host'=>'host','db_user'=>'username','db_pwd'=>'password','db_port'=>'port','db_name'=>'database','db_prefix'=>'prefix');
  410. foreach ($dbParams as $k=>$v){
  411. if(preg_match('/\''.$v.'\'\s*\=\s*\>\s*[\'\"](?P<val>.*)[\'\"]/i',$dbFile,$dbMatch)){
  412. $cmsDb[$k]=$dbMatch['val'];
  413. }
  414. }
  415. $cmsDb['db_charset']='utf8';
  416. }
  417. }
  418. return $cmsDb;
  419. }
  420. public function cms_db_chanzhi($cmsPath){
  421. $dbFile=realpath($cmsPath.'/../system/config/my.php');
  422. $cmsDb=array();
  423. if(file_exists($dbFile)){
  424. $dbFile=file_get_contents($dbFile);
  425. $dbParams=array('db_host'=>'host','db_user'=>'user','db_pwd'=>'password','db_port'=>'port','db_name'=>'name','db_prefix'=>'prefix');
  426. foreach ($dbParams as $k=>$v){
  427. if(preg_match('/\$config->db->'.$v.'\s*=\s*[\'\"](?P<val>.*)[\'\"]/i',$dbFile,$dbMatch)){
  428. $cmsDb[$k]=$dbMatch['val'];
  429. }
  430. }
  431. $cmsDb['db_charset']='utf8';
  432. }
  433. return $cmsDb;
  434. }
  435. public function cms_db_feifei($cmsPath){
  436. $dbFile=realpath($cmsPath.'/Runtime/Conf/config.php');
  437. $cmsDb=array();
  438. if(file_exists($dbFile)){
  439. $dbFile=include $dbFile;
  440. if(is_array($dbFile)){
  441. $cmsDb['db_host']=$dbFile['db_host'];
  442. $cmsDb['db_user']=$dbFile['db_user'];
  443. $cmsDb['db_pwd']=$dbFile['db_pwd'];
  444. $cmsDb['db_charset']=$dbFile['db_charset'];
  445. $cmsDb['db_port']=$dbFile['db_port'];
  446. $cmsDb['db_name']=$dbFile['db_name'];
  447. $cmsDb['db_prefix']=$dbFile['db_prefix'];
  448. }
  449. }
  450. return $cmsDb;
  451. }
  452. public function cms_db_hybbs($cmsPath){
  453. $dbFile=realpath($cmsPath.'/Conf/config.php');
  454. $cmsDb=array();
  455. if(file_exists($dbFile)){
  456. $dbFile=include $dbFile;
  457. if(is_array($dbFile)){
  458. $cmsDb['db_host']=$dbFile['SQL_IP'];
  459. $cmsDb['db_user']=$dbFile['SQL_USER'];
  460. $cmsDb['db_pwd']=$dbFile['SQL_PASS'];
  461. $cmsDb['db_charset']=$dbFile['SQL_CHARSET'];
  462. $cmsDb['db_port']=$dbFile['SQL_PORT'];
  463. $cmsDb['db_name']=$dbFile['SQL_NAME'];
  464. $cmsDb['db_prefix']=$dbFile['SQL_PREFIX'];
  465. $this->siteurl=rtrim($dbFile['DOMAIN_NAME'],'\/\\').'/';
  466. }
  467. }
  468. return $cmsDb;
  469. }
  470. public function cms_db_sdcms($cmsPath){
  471. $dbFile=realpath($cmsPath.'/config.php');
  472. $cmsDb=array();
  473. if(file_exists($dbFile)){
  474. $dbFile=include $dbFile;
  475. if(is_array($dbFile)){
  476. $dbFile=$dbFile['DEFAULT_DB'];
  477. $cmsDb['db_host']=$dbFile['DB_HOST'];
  478. $cmsDb['db_user']=$dbFile['DB_USER'];
  479. $cmsDb['db_pwd']=$dbFile['DB_PASS'];
  480. $cmsDb['db_port']=$dbFile['DB_PORT'];
  481. $cmsDb['db_name']=$dbFile['DB_BASE'];
  482. $cmsDb['db_prefix']=$dbFile['DB_PREFIX'];
  483. }
  484. }
  485. return $cmsDb;
  486. }
  487. public function cms_db_catfish($cmsPath){
  488. $dbFile=realpath($cmsPath.'/application/database.php');
  489. $cmsDb=array();
  490. if(file_exists($dbFile)){
  491. $dbFile=include $dbFile;
  492. if(is_array($dbFile)){
  493. $cmsDb['db_host']=$dbFile['hostname'];
  494. $cmsDb['db_user']=$dbFile['username'];
  495. $cmsDb['db_pwd']=$dbFile['password'];
  496. $cmsDb['db_charset']=$dbFile['charset'];
  497. $cmsDb['db_port']=$dbFile['hostport'];
  498. $cmsDb['db_name']=$dbFile['database'];
  499. $cmsDb['db_prefix']=$dbFile['prefix'];
  500. }
  501. }
  502. return $cmsDb;
  503. }
  504. public function cms_db_pboot($cmsPath){
  505. $dbFile=realpath($cmsPath.'/config/database.php');
  506. $cmsDb=array();
  507. if(file_exists($dbFile)){
  508. $dbFile=include $dbFile;
  509. $dbFile=$dbFile['database'];
  510. if(is_array($dbFile)){
  511. //使用sqlite必须开启pdo_sqlite
  512. $cmsDb['db_type']=stripos($dbFile['type'], 'sqlite')!==false?'sqlite':'mysql';
  513. $cmsDb['db_name']=$cmsDb['db_type']=='sqlite'?($cmsPath.$dbFile['dbname']):$dbFile['dbname'];
  514. $cmsDb['db_host']=$dbFile['host'];
  515. $cmsDb['db_user']=$dbFile['user'];
  516. $cmsDb['db_pwd']=$dbFile['passwd'];
  517. $cmsDb['db_charset']='utf8';
  518. $cmsDb['db_port']=$dbFile['port'];
  519. $cmsDb['db_prefix']='ay_';//固定的前缀
  520. }
  521. }
  522. return $cmsDb;
  523. }
  524. public function cms_db_typecho($cmsPath){
  525. $configFile=realpath($cmsPath.'/config.inc.php');
  526. $cmsDb=array();
  527. if(file_exists($configFile)){
  528. $configFile=file_get_contents($configFile);
  529. if($configFile){
  530. if(preg_match('/\s*new\s*Typecho_Db\s*\([^,\(\)]+,\s*[\'\"](?P<pre>[^\'\"]+)[\'\"]/i',$configFile,$prefix)){
  531. //匹配前缀
  532. $cmsDb['db_prefix']=$prefix['pre'];
  533. }
  534. if(preg_match('/\$db->addServer\s*\((?P<db>[\s\S]+?)\)\s*,/i',$configFile,$db)){
  535. //匹配数组
  536. $db=$db['db'];
  537. $dbKeys=array('db_host'=>'host','db_user'=>'user','db_pwd'=>'password','db_charset'=>'charset','db_port'=>'port','db_name'=>'database');
  538. foreach($dbKeys as $k=>$v){
  539. if(preg_match('/[\'\"]'.$v.'[\'\"]\s*=\s*>\s*[\'\"](?P<val>[^\'\"]+)[\'\"]/i',$db,$val)){
  540. $cmsDb[$k]=$val['val'];
  541. }
  542. }
  543. }
  544. }
  545. }
  546. return $cmsDb;
  547. }
  548. public function cms_db_maccms($cmsPath){
  549. $config=include $cmsPath.'/application/database.php';
  550. $cmsDb=array(
  551. 'db_type' => $config['type'],
  552. 'db_user' => $config['username'],
  553. 'db_pwd' => $config['password'],
  554. 'db_host' => $config['hostname'],
  555. 'db_port' => $config['hostport'],
  556. 'db_name' => $config['database'],
  557. 'db_charset' => $config['charset'],
  558. 'db_prefix' => $config['prefix']
  559. );
  560. return $cmsDb;
  561. }
  562. public function cms_db_yzmcms($cmsPath){
  563. $config=include $cmsPath.'/common/config/config.php';
  564. $cmsDb=array(
  565. 'db_type' => 'mysql',
  566. 'db_user' => $config['db_user'],
  567. 'db_pwd' => $config['db_pwd'],
  568. 'db_host' => $config['db_host'],
  569. 'db_port' => $config['db_port'],
  570. 'db_name' => $config['db_name'],
  571. 'db_charset' => 'utf8',
  572. 'db_prefix' => $config['db_prefix']
  573. );
  574. return $cmsDb;
  575. }
  576. public function cms_db_xiunobbs($cmsPath){
  577. $dbFile=realpath($cmsPath.'/conf/conf.php');
  578. //转换成thinkphp数据库配置
  579. $config=include $dbFile;
  580. $config=$config['db'][$config['db']['type']]['master'];
  581. $cmsDb=array(
  582. 'db_type' => 'mysql',
  583. 'db_user' => $config['user'],
  584. 'db_pwd' => $config['password'],
  585. 'db_host' => $config['host'],
  586. 'db_port' => 3306,
  587. 'db_name' => $config['name'],
  588. 'db_charset' => $config['charset'],
  589. 'db_prefix' => $config['tablepre']
  590. );
  591. return $cmsDb;
  592. }
  593. public function cms_db_hadsky($cmsPath){
  594. $dbFile=realpath($cmsPath.'/puyuetian/mysql/config.php');
  595. //转换成thinkphp数据库配置
  596. $_G=null;
  597. include $dbFile;
  598. $config=$_G['MYSQL'];
  599. if(preg_match('/set names (\w+)/i', $config['CHARSET'],$charset)){
  600. $config['CHARSET']=$charset[1];
  601. }else{
  602. $config['CHARSET']='utf8';
  603. }
  604. $cmsDb=array(
  605. 'db_type' => 'mysql',
  606. 'db_user' => $config['USERNAME'],
  607. 'db_pwd' => $config['PASSWORD'],
  608. 'db_host' => $config['LOCATION'],
  609. 'db_port' => 3306,
  610. 'db_name' => $config['DATABASE'],
  611. 'db_charset' => $config['CHARSET'],
  612. 'db_prefix' => $config['PREFIX']
  613. );
  614. return $cmsDb;
  615. }
  616. public function cms_db_mipcms($cmsPath){
  617. $dbFile=realpath($cmsPath.'/app/database.php');
  618. //转换成thinkphp数据库配置
  619. $config=include $dbFile;
  620. $cmsDb=array(
  621. 'db_type' => $config['type'],
  622. 'db_user' => $config['username'],
  623. 'db_pwd' => $config['password'],
  624. 'db_host' => $config['hostname'],
  625. 'db_port' => $config['hostport'],
  626. 'db_name' => $config['database'],
  627. 'db_charset' => $config['charset'],
  628. 'db_prefix' => $config['prefix']
  629. );
  630. return $cmsDb;
  631. }
  632. public function cms_db_zblog($cmsPath){
  633. $dbFile=realpath($cmsPath.'/zb_users/c_option.php');
  634. //转换成thinkphp数据库配置
  635. $config=include $dbFile;
  636. $cmsDb=array(
  637. 'db_type' => $config['ZC_DATABASE_TYPE'],
  638. 'db_user' => $config['ZC_MYSQL_USERNAME'],
  639. 'db_pwd' => $config['ZC_MYSQL_PASSWORD'],
  640. 'db_host' => $config['ZC_MYSQL_SERVER'],
  641. 'db_port' => $config['ZC_MYSQL_PORT'],
  642. 'db_name' => $config['ZC_MYSQL_NAME'],
  643. 'db_charset' => $config['ZC_MYSQL_CHARSET'],
  644. 'db_prefix' => $config['ZC_MYSQL_PRE']
  645. );
  646. return $cmsDb;
  647. }
  648. public function cms_db_twcms($cmsPath){
  649. $dbFile=realpath($cmsPath.'/twcms/config/config.inc.php');
  650. //转换成thinkphp数据库配置
  651. include_once $dbFile;//导入本地cms配置文件
  652. $config=$_ENV['_config'][db];
  653. $cmsDb=array(
  654. 'db_type' => $config['type'],
  655. 'db_user' => $config['master']['user'],
  656. 'db_pwd' => $config['master']['password'],
  657. 'db_host' => $config['master']['host'],
  658. 'db_port' => 3306,
  659. 'db_name' => $config['master']['name'],
  660. 'db_charset' => $config['master']['charset'],
  661. 'db_prefix' => $config['master']['tablepre']
  662. );
  663. return $cmsDb;
  664. }
  665. public function cms_db_destoon($cmsPath){
  666. define('IN_DESTOON',true);
  667. $dbFile=realpath($cmsPath.'/config.inc.php');
  668. //转换成thinkphp数据库配置
  669. $CFG=null;
  670. include $dbFile;//导入本地cms配置文件
  671. $cmsDb=array(
  672. 'db_type' => $CFG['database'],
  673. 'db_user' => $CFG['db_user'],
  674. 'db_pwd' => $CFG['db_pass'],
  675. 'db_host' => $CFG['db_host'],
  676. 'db_port' => 3306,
  677. 'db_name' => $CFG['db_name'],
  678. 'db_charset' => $CFG['db_charset'],
  679. 'db_prefix' => $CFG['tb_pre']
  680. );
  681. $this->siteurl=$CFG['url'];
  682. return $cmsDb;
  683. }
  684. }
  685. ?>