plugin.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_app_class('admin','admin',0);
  4. pc_base::load_sys_class('form', '', 0);
  5. pc_base::load_sys_func('plugin');
  6. class plugin extends admin {
  7. private $db,$db_var;
  8. function __construct() {
  9. parent::__construct();
  10. $this->db = pc_base::load_model('plugin_model');
  11. $this->db_var = pc_base::load_model('plugin_var_model');
  12. pc_base::load_app_func('global');
  13. }
  14. /**
  15. * 应用配置信息
  16. */
  17. public function init() {
  18. $show_validator = true;
  19. $show_dialog = true;
  20. if($pluginfo = $this->db->select('','*','','disable DESC,listorder DESC')) {
  21. foreach ($pluginfo as $_k=>$_r) {
  22. if(file_exists(PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$_r['dir'].DIRECTORY_SEPARATOR.$_r['dir'].'.class.php')){
  23. $pluginfo[$_k]['url'] = 'plugin.php?id='.$_r['dir'];
  24. } else {
  25. $pluginfo[$_k]['url'] = '';
  26. }
  27. $pluginfo[$_k]['dir'] = $_r['dir'].'/';
  28. }
  29. }
  30. include $this->admin_tpl('plugin_list');
  31. }
  32. /**
  33. * 应用导入\安装
  34. */
  35. public function import() {
  36. if(!isset($_GET['dir'])) {
  37. $plugnum = 1;
  38. $installsdir = array();
  39. if($installs_pluginfo = $this->db->select()) {
  40. foreach ($installs_pluginfo as $_r) {
  41. $installsdir[] = $_r['dir'];
  42. }
  43. }
  44. $pluginsdir = dir(PC_PATH.'plugin');
  45. while (false !== ($entry = $pluginsdir->read())) {
  46. $config_file = '';
  47. $plugin_data = array();
  48. if(!in_array($entry, array('.', '..')) && is_dir(PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$entry) && !in_array($entry, $installsdir) && !$this->db->get_one(array('identification'=>$entry))) {
  49. $config_file = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$entry.DIRECTORY_SEPARATOR.'plugin_'.$entry.'.cfg.php';
  50. if(file_exists($config_file)) {
  51. $plugin_data = @require($config_file);
  52. $pluginfo[$plugnum]['name'] = $plugin_data['plugin']['name'];
  53. $pluginfo[$plugnum]['version'] = $plugin_data['plugin']['version'];
  54. $pluginfo[$plugnum]['copyright'] = $plugin_data['plugin']['copyright'];
  55. $pluginfo[$plugnum]['dir'] = $entry;
  56. $plugnum++;
  57. }
  58. }
  59. }
  60. include $this->admin_tpl('plugin_list_import');
  61. } else {
  62. $dir = trim($_GET['dir']);
  63. $license = 0;
  64. $config_file = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.'plugin_'.$dir.'.cfg.php';
  65. if(file_exists($config_file)) {
  66. $plugin_data = @require($config_file);
  67. $license = ($plugin_data['license'] == '' || !isset($plugin_data['license'])) ? 0 : 1;
  68. }
  69. if(empty($_GET['license']) && $license) {
  70. $submit_url = '?m=admin&c=plugin&a=import&dir='.$dir.'&license=1&pc_hash='. $_SESSION['pc_hash'].'&menuid='.$_GET['menuid'];
  71. } else {
  72. $submit_url = '?m=admin&c=plugin&a=install&dir='.$dir.'&pc_hash='. $_SESSION['pc_hash'].'&menuid='.$_GET['menuid'];
  73. }
  74. $show_header = 0;
  75. include $this->admin_tpl('plugin_import_confirm');
  76. }
  77. }
  78. /**
  79. * 应用删除程序
  80. */
  81. public function delete() {
  82. if(isset($_POST['dosubmit'])) {
  83. $pluginid = intval($_POST['pluginid']);
  84. $plugin_data = $this->db->get_one(array('pluginid'=>$pluginid));
  85. $op_status = FALSE;
  86. $dir = $plugin_data['dir'];
  87. $config_file = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.'plugin_'.$dir.'.cfg.php';
  88. if(file_exists($config_file)) {
  89. $plugin_data = @require($config_file);
  90. }
  91. $filename = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.$plugin_data['plugin']['uninstallfile'];
  92. if(file_exists($filename)) {
  93. @include_once $filename;
  94. } else {
  95. showmessage(L('plugin_lacks_uninstall_file','','plugin'),HTTP_REFERER);
  96. }
  97. if($op_status) {
  98. $this->db->delete(array('pluginid'=>$pluginid));
  99. $this->db_var->delete(array('pluginid'=>$pluginid));
  100. delcache($dir,'plugins');
  101. delcache($dir.'_var','plugins');
  102. $this->set_hook_cache();
  103. if($plugin_data['plugin']['iframe']) {
  104. pc_base::load_sys_func('dir');
  105. if(!dir_delete(PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$dir)) {
  106. showmessage(L('plugin_uninstall_success_no_delete','','plugin'),'?m=admin&c=plugin');
  107. }
  108. }
  109. showmessage(L('plugin_uninstall_success','','plugin'),'?m=admin&c=plugin');
  110. } else {
  111. showmessage(L('plugin_uninstall_fail','','plugin'),'?m=admin&c=plugin');
  112. }
  113. } else {
  114. $show_header = 0;
  115. $pluginid = intval($_GET['pluginid']);
  116. $plugin_data = $this->db->get_one(array('pluginid'=>$pluginid));
  117. include $this->admin_tpl('plugin_delete_confirm');
  118. }
  119. }
  120. /**
  121. * 应用安装
  122. */
  123. public function install() {
  124. $op_status = FALSE;
  125. $dir = trim($_GET['dir']);
  126. $config_file = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.'plugin_'.$dir.'.cfg.php';
  127. if(file_exists($config_file)) {
  128. $plugin_data = @require($config_file);
  129. } else {
  130. showmessage(L('plugin_config_not_exist','','plugin'));
  131. }
  132. $app_status = app_validity_check($plugin_data['appid']);
  133. if($app_status != 2){
  134. $app_msg = $app_status == '' ? L('plugin_not_exist_or_pending','','plugin') : ($app_status == 0 || $app_status == 1 ? L('plugin_developing','','plugin') : L('plugin_be_locked','','plugin'));
  135. showmessage($app_msg);
  136. }
  137. if($plugin_data['version'] && $plugin_data['version']!=pc_base::load_config('version', 'pc_version')) {
  138. showmessage(L('plugin_incompatible','','plugin'));
  139. }
  140. if($plugin_data['dir'] == '' || $plugin_data['identification'] == '' || $plugin_data['identification']!=$plugin_data['dir']) {
  141. showmessage(L('plugin_lack_of_necessary_configuration_items','','plugin'));
  142. }
  143. if(!pluginkey_check($plugin_data['identification'])) {
  144. showmessage(L('plugin_illegal_id','','plugin'));
  145. }
  146. if(is_array($plugin_data['plugin_var'])) {
  147. foreach($plugin_data['plugin_var'] as $config) {
  148. if(!pluginkey_check($config['fieldname'])) {
  149. showmessage(L('plugin_illegal_variable','','plugin'));
  150. }
  151. }
  152. }
  153. if($this->db->get_one(array('identification'=>$plugin_data['identification']))) {
  154. showmessage(L('plugin_duplication_name','','plugin'));
  155. };
  156. $filename = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.$plugin_data['plugin']['installfile'];
  157. if(file_exists($filename)) {
  158. @include_once $filename;
  159. }
  160. if($op_status) {
  161. //向插件表中插入数据
  162. $plugin = array('name'=>new_addslashes($plugin_data['plugin']['name']),'identification'=>$plugin_data['identification'],'appid'=>$plugin_data['appid'],'description'=>new_addslashes($plugin_data['plugin']['description']),'dir'=>$plugin_data['dir'],'copyright'=>new_addslashes($plugin_data['plugin']['copyright']),'setting'=>array2string($plugin_data['plugin']['setting']),'iframe'=>array2string($plugin_data['plugin']['iframe']),'version'=>$plugin_data['plugin']['version'],'disable'=>'0');
  163. $pluginid = $this->db->insert($plugin,TRUE);
  164. //向插件变量表中插入数据
  165. if(is_array($plugin_data['plugin_var'])) {
  166. foreach($plugin_data['plugin_var'] as $config) {
  167. $plugin_var = array();
  168. $plugin_var['pluginid'] = $pluginid;
  169. foreach($config as $_k => $_v) {
  170. if(!in_array($_k, array('title','description','fieldname','fieldtype','setting','listorder','value','formattribute'))) continue;
  171. if($_k == 'setting') $_v = array2string($_v);
  172. $plugin_var[$_k] = $_v;
  173. }
  174. $this->db_var->insert($plugin_var);
  175. }
  176. }
  177. plugin_install_stat($plugin_data['appid']);
  178. setcache($plugin_data['identification'], $plugin,'plugins');
  179. $this->set_var_cache($pluginid);
  180. showmessage(L('plugin_install_success','','plugin'),'?m=admin&c=plugin');
  181. } else {
  182. showmessage(L('plugin_install_fail','','plugin'),'?m=admin&c=plugin');
  183. }
  184. }
  185. /**
  186. * 应用升级
  187. */
  188. public function upgrade() {
  189. //TODO
  190. }
  191. /**
  192. * 应用排序
  193. */
  194. public function listorder() {
  195. if(isset($_POST['dosubmit'])) {
  196. foreach($_POST['listorders'] as $pluginid => $listorder) {
  197. $this->db->update(array('listorder'=>$listorder),array('pluginid'=>$pluginid));
  198. }
  199. $this->set_hook_cache();
  200. showmessage(L('operation_success'),'?m=admin&c=plugin');
  201. } else {
  202. showmessage(L('operation_failure'),'?m=admin&c=plugin');
  203. }
  204. }
  205. public function design() {
  206. if(isset($_POST['dosubmit'])) {
  207. $data['identification'] = $_POST['info']['identification'];
  208. $data['realease'] = date('YMd',SYS_TIME);
  209. $data['dir'] = $_POST['info']['identification'];
  210. $data['appid'] = '';
  211. $data['plugin'] = array(
  212. 'version' => '0.0.2',
  213. 'name' => $_POST['info']['name'],
  214. 'copyright' => $_POST['info']['copyright'],
  215. 'description' => "",
  216. 'installfile' => 'install.php',
  217. 'uninstallfile' => 'uninstall.php',
  218. );
  219. $filepath = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$data['identification'].DIRECTORY_SEPARATOR.'plugin_'.$data['identification'].'.cfg.php';
  220. pc_base::load_sys_func('dir');
  221. dir_create(dirname($filepath));
  222. $data = "<?php\nreturn ".var_export($data, true).";\n?>";
  223. if(pc_base::load_config('system', 'lock_ex')) {
  224. $file_size = file_put_contents($filepath, $data, LOCK_EX);
  225. } else {
  226. $file_size = file_put_contents($filepath, $data);
  227. }
  228. echo 'success';
  229. } else {
  230. include $this->admin_tpl('plugin_design');
  231. }
  232. }
  233. /**
  234. * 应用中心
  235. * Enter description here ...
  236. */
  237. public function appcenter() {
  238. $data = array();
  239. $p = intval($_GET[p]) ? intval($_GET[p]) : 1;
  240. $s = 8;
  241. $data = file_get_contents('http://open.phpcms.cn/index.php?m=open&c=api&a=get_applist&s='.$s.'&p='.$p);
  242. $data = array_iconv(json_decode($data, true),'utf-8',CHARSET);
  243. $recommed_data = file_get_contents('http://open.phpcms.cn/index.php?m=open&c=api&a=get_recommed_applist&s=5&p=1');
  244. $recommed_data = array_iconv(json_decode($recommed_data, true),'utf-8',CHARSET);
  245. $focus_data = file_get_contents('http://open.phpcms.cn/index.php?m=open&c=api&a=get_app_focus&num=3');
  246. $focus_data = array_iconv(json_decode($focus_data, true),'utf-8',CHARSET);
  247. $pages = $data['pages'];
  248. $pre_page = $p <= 1 ? 1 : $p - 1;
  249. $next_page = $p >= $pages ? $pages : $p + 1;
  250. $pages = '<a class="a1">'.$data['total'].L('plugin_item','','plugin').'</a> <a href="?m=admin&c=plugin&a=appcenter&p=1">'.L('plugin_firstpage').'</a> <a href="?m=admin&c=plugin&a=appcenter&p='.$pre_page.'">'.L('plugin_prepage').'</a> <a href="?m=admin&c=plugin&a=appcenter&p='.$next_page.'">'.L('plugin_nextpage').'</a> <a href="?m=admin&c=plugin&a=appcenter&p='.$pages.'">'.L('plugin_lastpage').'</a>';
  251. $show_header = 1;
  252. include $this->admin_tpl('plugin_appcenter');
  253. }
  254. /**
  255. * 显示应用详情
  256. */
  257. public function appcenter_detail() {
  258. $data = array();
  259. $id = intval($_GET['id']);
  260. $data = file_get_contents('http://open.phpcms.cn/index.php?m=open&c=api&a=get_detail_byappid&id='.$id);
  261. $data = array_iconv(json_decode($data, true),'utf-8',CHARSET);
  262. extract($data);
  263. if($appname) {
  264. include $this->admin_tpl('plugin_appcenter_detail');
  265. } else {
  266. showmessage(L('plugin_not_exist_or_pending','','plugin'));
  267. }
  268. }
  269. /**
  270. * 在线安装
  271. */
  272. public function install_online() {
  273. $data = array();
  274. $id = intval($_GET['id']);
  275. $data = file_get_contents('http://open.phpcms.cn/index.php?m=open&c=api&a=get_detail_byappid&id='.$id);
  276. $data = array_iconv(json_decode($data, true),'utf-8',CHARSET);
  277. //如果为iframe类型应用,无需下载压缩包,之间创建插件文件夹
  278. if(!empty($data['iframe'])) {
  279. $appdirname = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$data['appenname'];
  280. if(!file_exists($appdirname)) {
  281. if(!mkdir($appdirname)) {
  282. showmessage(L('plugin_mkdir_fail', '', 'plugin'));
  283. } else {
  284. //创建安装、配置文件
  285. $installdata = <<<EOF
  286. <?php
  287. defined('IN_PHPCMS') or exit('No permission resources.');
  288. \$op_status = TRUE;
  289. ?>
  290. EOF;
  291. $uninstallres = @file_put_contents($appdirname.DIRECTORY_SEPARATOR.'uninstall.php', $installdata);
  292. $installres = @file_put_contents($appdirname.DIRECTORY_SEPARATOR.'install.php', $installdata);
  293. $cfgdata = <<<EOF
  294. <?php
  295. return array (
  296. 'identification' => '$data[appenname]',
  297. 'dir' => '$data[appenname]',
  298. 'appid' => '$data[id]',
  299. 'plugin'=> array(
  300. 'version' => '1.0',
  301. 'name' => '$data[appname]',
  302. 'copyright' => 'phpcms team',
  303. 'description' =>'$data[description]',
  304. 'installfile' => 'install.php',
  305. 'uninstallfile' => 'uninstall.php',
  306. 'iframe' => array('width'=>'960','height'=>'640','url'=>'$data[iframe]'),
  307. ),
  308. 'plugin_var'=> array( array('title'=>'宽度','description'=>'','fieldname'=>'width','fieldtype'=>'text','value'=>'960','formattribute'=>'style="width:50px"','listorder'=>'1',), array('title'=>'高度','description'=>'','fieldname'=>'height','fieldtype'=>'text','value'=>'640','formattribute'=>'style="width:50px"','listorder'=>'2',),
  309. ),
  310. );
  311. ?>
  312. EOF;
  313. $cfgres = @file_put_contents($appdirname.DIRECTORY_SEPARATOR.'plugin_'.$data['appenname'].'.cfg.php', $cfgdata);
  314. //检查配置文件是否写入成功
  315. if($installres*$uninstallres*$cfgres > 0) {
  316. showmessage(L('plugin_configure_success', '', 'plugin'), 'index.php?m=admin&c=plugin&a=import&dir='.$data['appenname']);
  317. } else {
  318. showmessage(L('plugin_install_fail', '', 'plugin'));
  319. }
  320. }
  321. } else {
  322. showmessage(L('plugin_allready_exists', '', 'plugin'));
  323. }
  324. } else {
  325. //远程压缩包地址
  326. $upgradezip_url = $data['downurl'];
  327. if(empty($upgradezip_url)) {
  328. showmessage(L('download_fail', '', 'plugin'), 'index.php?m=admin&c=plugin&a=appcenter');
  329. }
  330. //创建缓存文件夹
  331. if(!file_exists(CACHE_PATH.'caches_open')) {
  332. @mkdir(CACHE_PATH.'caches_open');
  333. }
  334. //保存到本地地址
  335. $upgradezip_path = CACHE_PATH.'caches_open'.DIRECTORY_SEPARATOR.$data['id'].'.zip';
  336. //解压路径
  337. $upgradezip_source_path = CACHE_PATH.'caches_open'.DIRECTORY_SEPARATOR.$data['id'];
  338. //下载压缩包
  339. @file_put_contents($upgradezip_path, @file_get_contents($upgradezip_url));
  340. //解压缩
  341. pc_base::load_app_class('pclzip', 'upgrade', 0);
  342. $archive = new PclZip($upgradezip_path);
  343. if($archive->extract(PCLZIP_OPT_PATH, $upgradezip_source_path, PCLZIP_OPT_REPLACE_NEWER) == 0) {
  344. die("Error : ".$archive->errorInfo(true));
  345. }
  346. //删除压缩包
  347. @unlink($upgradezip_path);
  348. //拷贝gbk/upload文件夹到根目录
  349. $copy_from = $upgradezip_source_path.DIRECTORY_SEPARATOR.CHARSET;
  350. //动态程序路径
  351. $copy_to_pcpath = PC_PATH.'plugin';
  352. //静态程序路径
  353. $copy_to_staticspath = PHPCMS_PATH.'statics'.DIRECTORY_SEPARATOR.'plugin';
  354. //应用文件夹名称
  355. $appdirname = $data['appenname'];
  356. $this->copyfailnum = 0;
  357. $this->copydir($copy_from.DIRECTORY_SEPARATOR.'phpcms'.DIRECTORY_SEPARATOR.'plugin', $copy_to_pcpath, $_GET['cover']);
  358. $this->copydir($copy_from.DIRECTORY_SEPARATOR.'statics'.DIRECTORY_SEPARATOR.'plugin', $copy_to_staticspath, $_GET['cover']);
  359. $this->deletedir($copy_from);
  360. //检查文件操作权限,是否复制成功
  361. if($this->copyfailnum > 0) {
  362. showmessage(L('download_fail', '', 'plugin'), 'index.php?m=admin&c=plugin&a=appcenter');
  363. } else {
  364. showmessage(L('download_success', '', 'plugin'), 'index.php?m=admin&c=plugin&a=import&dir='.$appdirname);
  365. }
  366. }
  367. }
  368. /**
  369. * 异步方式调用详情
  370. * Enter description here ...
  371. */
  372. public function public_appcenter_ajx_detail() {
  373. $id = intval($_GET['id']);
  374. $data = file_get_contents('http://open.phpcms.cn/index.php?m=open&c=api&a=get_detail_byappid&id='.$id);
  375. //$data = json_decode($data, true);
  376. echo $_GET['jsoncallback'].'(',$data,')';
  377. exit;
  378. }
  379. /**
  380. * 配置应用.
  381. */
  382. public function config() {
  383. if(isset($_POST['dosubmit'])) {
  384. $pluginid = intval($_POST['pluginid']);
  385. foreach ($_POST['info'] as $_k => $_v) {
  386. $this->db_var->update(array('value'=>$_v),array('pluginid'=>$pluginid,'fieldname'=>$_k));
  387. }
  388. $this->set_var_cache($pluginid);
  389. showmessage(L('operation_success'),HTTP_REFERER);
  390. } else {
  391. $pluginid = intval($_GET['pluginid']);
  392. $plugin_menus = array();
  393. $info = $this->db->get_one(array('pluginid'=>$pluginid));
  394. extract($info);
  395. if(!isset($_GET['module'])) {
  396. $plugin_menus[] =array('name'=>L('plugin_desc','','plugin'),'url'=>'','status'=>'1');
  397. if($disable){
  398. if($info_var = $this->db_var->select(array('pluginid'=>$pluginid),'*','','listorder ASC,id DESC')) {
  399. $plugin_menus[] =array('name'=>L('plugin_config','','plugin'),'url'=>'','status'=>'0');
  400. $form = $this->creatconfigform($info_var);
  401. }
  402. $meun_total = count($plugin_menus);;
  403. $setting = string2array($setting);
  404. if(is_array($setting)) {
  405. foreach($setting as $m) {
  406. $plugin_menus[] = array('name'=>$m['menu'],'extend'=>1,'url'=>$m['name']);
  407. $mods[] = $m['name'];
  408. }
  409. }
  410. }
  411. include $this->admin_tpl('plugin_setting');
  412. } else {
  413. define('PLUGIN_ID', $identification);
  414. $plugin_module = trim($_GET['module']);
  415. $plugin_admin_path = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$identification.DIRECTORY_SEPARATOR.'plugin_admin.class.php';
  416. if (file_exists($plugin_admin_path)) {
  417. include $plugin_admin_path;
  418. $plugin_admin = new plugin_admin($pluginid);
  419. call_user_func(array($plugin_admin, $plugin_module));
  420. }
  421. }
  422. }
  423. }
  424. /**
  425. * 开启/关闭插件
  426. * Enter description here ...
  427. */
  428. public function status() {
  429. $disable = intval($_GET['disable']);
  430. $pluginid = intval($_GET['pluginid']);
  431. $this->db->update(array('disable'=>$disable),array('pluginid'=>$pluginid));
  432. $this->set_cache($pluginid);
  433. showmessage(L('operation_success'),HTTP_REFERER);
  434. }
  435. /**
  436. * 设置字段缓存
  437. * @param int $pluginid
  438. */
  439. private function set_var_cache($pluginid) {
  440. if($info = $this->db_var->select(array('pluginid'=>$pluginid))) {
  441. $plugin_data = $this->db->get_one(array('pluginid'=>$pluginid));
  442. foreach ($info as $_value) {
  443. $plugin_vars[$_value['fieldname']] = $_value['value'];
  444. }
  445. setcache($plugin_data['identification'].'_var', $plugin_vars,'plugins');
  446. }
  447. }
  448. /**
  449. * 设置缓存
  450. * @param int $pluginid
  451. */
  452. private function set_cache($pluginid) {
  453. if($info = $this->db->get_one(array('pluginid'=>$pluginid))) {
  454. setcache($info['identification'], $info,'plugins');
  455. }
  456. $this->set_hook_cache();
  457. }
  458. /**
  459. * 设置hook缓存
  460. */
  461. function set_hook_cache() {
  462. if($info = $this->db->select(array('disable'=>1),'*','','listorder DESC')) {
  463. foreach($info as $i) {
  464. $id = $i['identification'];
  465. $hook_file = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$id.DIRECTORY_SEPARATOR.'hook.class.php';
  466. if(file_exists($hook_file)) {
  467. $hook[$i['appid']] = $i['identification'];
  468. }
  469. }
  470. }
  471. setcache('hook',$hook,'plugins');
  472. }
  473. /**
  474. * 创建配置表单
  475. * @param array $data
  476. */
  477. private function creatconfigform($data) {
  478. if(!is_array($data) || empty($data)) return false;
  479. foreach ($data as $r) {
  480. $form .= '<tr><th width="120">'.$r['title'].'</th><td class="y-bg">'.$this->creatfield($r).'</td></tr>';
  481. }
  482. return $form;
  483. }
  484. /**
  485. * 创建配置表单字段
  486. * @param array $data
  487. */
  488. private function creatfield($data) {
  489. extract($data);
  490. $fielda_array = array('text','radio','checkbox','select','datetime','textarea');
  491. if(in_array($fieldtype, $fielda_array)) {
  492. if($fieldtype == 'text') {
  493. return '<input type="text" name="info['.$fieldname.']" id="'.$fieldname.'" value="'.$value.'" class="input-text" '.$formattribute.' > '.' '.$description;
  494. } elseif($fieldtype == 'checkbox') {
  495. return form::checkbox(string2array($setting),$value,"name='info[$fieldname]' $formattribute",'',$fieldname).' '.$description;
  496. } elseif($fieldtype == 'radio') {
  497. return form::radio(string2array($setting),$value,"name='info[$fieldname]' $formattribute",'',$fieldname).' '.$description;
  498. } elseif($fieldtype == 'select') {
  499. return form::select(string2array($setting),$value,"name='info[$fieldname]' $formattribute",'',$fieldname).' '.$description;
  500. } elseif($fieldtype == 'datetime') {
  501. return form::date("info[$fieldname]",$value,$isdatetime,1).' '.$description;
  502. } elseif($fieldtype == 'textarea') {
  503. return '<textarea name="info['.$fieldname.']" id="'.$fieldname.'" '.$formattribute.'>'.$value.'</textarea>'.' '.$description;
  504. }
  505. }
  506. }
  507. /**
  508. * 执行SQL
  509. * @param string $sql 要执行的sql语句
  510. */
  511. private function _sql_execute($sql) {
  512. $sqls = $this->_sql_split($sql);
  513. if(is_array($sqls)) {
  514. foreach($sqls as $sql) {
  515. if(trim($sql) != '') {
  516. $this->db->query($sql);
  517. }
  518. }
  519. } else {
  520. $this->db->query($sqls);
  521. }
  522. return true;
  523. }
  524. /**
  525. * 分割SQL语句
  526. * @param string $sql 要执行的sql语句
  527. */
  528. private function _sql_split($sql) {
  529. $database = pc_base::load_config('database');
  530. $db_charset = $database['default']['charset'];
  531. if($this->db->version() > '4.1' && $db_charset) {
  532. $sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=".$db_charset,$sql);
  533. }
  534. $sql = str_replace("\r", "\n", $sql);
  535. $ret = array();
  536. $num = 0;
  537. $queriesarray = explode(";\n", trim($sql));
  538. unset($sql);
  539. foreach($queriesarray as $query) {
  540. $ret[$num] = '';
  541. $queries = explode("\n", trim($query));
  542. $queries = array_filter($queries);
  543. foreach($queries as $query) {
  544. $str1 = substr($query, 0, 1);
  545. if($str1 != '#' && $str1 != '-') $ret[$num] .= $query;
  546. }
  547. $num++;
  548. }
  549. return($ret);
  550. }
  551. private function copydir($dirfrom, $dirto, $cover='') {
  552. //如果遇到同名文件无法复制,则直接退出
  553. if(is_file($dirto)){
  554. die(L('have_no_pri').$dirto);
  555. }
  556. //如果目录不存在,则建立之
  557. if(!file_exists($dirto)){
  558. mkdir($dirto);
  559. }
  560. $handle = opendir($dirfrom); //打开当前目录
  561. //循环读取文件
  562. while(false !== ($file = readdir($handle))) {
  563. if($file != '.' && $file != '..'){ //排除"."和"."
  564. //生成源文件名
  565. $filefrom = $dirfrom.DIRECTORY_SEPARATOR.$file;
  566. //生成目标文件名
  567. $fileto = $dirto.DIRECTORY_SEPARATOR.$file;
  568. if(is_dir($filefrom)){ //如果是子目录,则进行递归操作
  569. $this->copydir($filefrom, $fileto, $cover);
  570. } else { //如果是文件,则直接用copy函数复制
  571. if(!empty($cover)) {
  572. if(!copy($filefrom, $fileto)) {
  573. $this->copyfailnum++;
  574. echo L('copy').$filefrom.L('to').$fileto.L('failed')."<br />";
  575. }
  576. } else {
  577. if(fileext($fileto) == 'html' && file_exists($fileto)) {
  578. } else {
  579. if(!copy($filefrom, $fileto)) {
  580. $this->copyfailnum++;
  581. echo L('copy').$filefrom.L('to').$fileto.L('failed')."<br />";
  582. }
  583. }
  584. }
  585. }
  586. }
  587. }
  588. }
  589. private function deletedir($dirname){
  590. $result = false;
  591. if(! is_dir($dirname)){
  592. echo " $dirname is not a dir!";
  593. exit(0);
  594. }
  595. $handle = opendir($dirname); //打开目录
  596. while(($file = readdir($handle)) !== false) {
  597. if($file != '.' && $file != '..'){ //排除"."和"."
  598. $dir = $dirname.DIRECTORY_SEPARATOR.$file;
  599. //$dir是目录时递归调用deletedir,是文件则直接删除
  600. is_dir($dir) ? $this->deletedir($dir) : unlink($dir);
  601. }
  602. }
  603. closedir($handle);
  604. $result = rmdir($dirname) ? true : false;
  605. return $result;
  606. }
  607. }
  608. ?>