function_plugin.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: function_plugin.php 36284 2016-12-12 00:47:50Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. require_once libfile('function/cloudaddons');
  12. function plugininstall($pluginarray, $installtype = '', $available = 0) {
  13. if(!$pluginarray || !$pluginarray['plugin']['identifier']) {
  14. return false;
  15. }
  16. $plugin = C::t('common_plugin')->fetch_by_identifier($pluginarray['plugin']['identifier']);
  17. if($plugin) {
  18. return false;
  19. }
  20. $pluginarray['plugin']['modules'] = dunserialize($pluginarray['plugin']['modules']);
  21. $pluginarray['plugin']['modules']['extra']['installtype'] = $installtype;
  22. if(updatepluginlanguage($pluginarray)) {
  23. $pluginarray['plugin']['modules']['extra']['langexists'] = 1;
  24. }
  25. if(!empty($pluginarray['intro'])) {
  26. if(!empty($pluginarray['intro'])) {
  27. require_once libfile('function/discuzcode');
  28. $pluginarray['plugin']['modules']['extra']['intro'] = discuzcode(strip_tags($pluginarray['intro']), 1, 0);
  29. }
  30. }
  31. if(!empty($pluginarray['uninstallfile'])) {
  32. $pluginarray['plugin']['modules']['extra']['uninstallfile'] = $pluginarray['uninstallfile'];
  33. }
  34. if(!empty($pluginarray['checkfile'])) {
  35. $pluginarray['plugin']['modules']['extra']['checkfile'] = $pluginarray['checkfile'];
  36. }
  37. if(!empty($pluginarray['enablefile'])) {
  38. $pluginarray['plugin']['modules']['extra']['enablefile'] = $pluginarray['enablefile'];
  39. }
  40. if(!empty($pluginarray['disablefile'])) {
  41. $pluginarray['plugin']['modules']['extra']['disablefile'] = $pluginarray['disablefile'];
  42. }
  43. $pluginarray['plugin']['modules'] = serialize($pluginarray['plugin']['modules']);
  44. $data = array();
  45. foreach($pluginarray['plugin'] as $key => $val) {
  46. if($key == 'directory') {
  47. $val .= (!empty($val) && substr($val, -1) != '/') ? '/' : '';
  48. } elseif($key == 'available') {
  49. $val = $available;
  50. }
  51. $data[$key] = $val;
  52. }
  53. $pluginid = C::t('common_plugin')->insert($data, true);
  54. if(is_array($pluginarray['var'])) {
  55. foreach($pluginarray['var'] as $config) {
  56. $data = array('pluginid' => $pluginid);
  57. foreach($config as $key => $val) {
  58. $data[$key] = $val;
  59. }
  60. C::t('common_pluginvar')->insert($data);
  61. }
  62. }
  63. if(!empty($dir) && !empty($pluginarray['importfile'])) {
  64. require_once libfile('function/importdata');
  65. foreach($pluginarray['importfile'] as $importtype => $file) {
  66. if(in_array($importtype, array('smilies', 'styles'))) {
  67. $files = explode(',', $file);
  68. foreach($files as $file) {
  69. if(file_exists($file = DISCUZ_ROOT.'./source/plugin/'.$dir.'/'.$file)) {
  70. $importtxt = @implode('', file($file));
  71. $imporfun = 'import_'.$importtype;
  72. $imporfun();
  73. }
  74. }
  75. }
  76. }
  77. }
  78. cloudaddons_installlog($pluginarray['plugin']['identifier'].'.plugin');
  79. cron_create($pluginarray['plugin']['identifier']);
  80. updatecache(array('plugin', 'setting', 'styles'));
  81. cleartemplatecache();
  82. dsetcookie('addoncheck_plugin', '', -1);
  83. return $pluginid;
  84. }
  85. function pluginupgrade($pluginarray, $installtype) {
  86. if(!$pluginarray || !$pluginarray['plugin']['identifier']) {
  87. return false;
  88. }
  89. $plugin = C::t('common_plugin')->fetch_by_identifier($pluginarray['plugin']['identifier']);
  90. if(!$plugin) {
  91. return false;
  92. }
  93. if(is_array($pluginarray['var'])) {
  94. $pluginvars = $pluginvarsnew = array();
  95. foreach(C::t('common_pluginvar')->fetch_all_by_pluginid($plugin['pluginid']) as $pluginvar) {
  96. $pluginvars[] = $pluginvar['variable'];
  97. }
  98. foreach($pluginarray['var'] as $config) {
  99. if(!in_array($config['variable'], $pluginvars)) {
  100. $data = array('pluginid' => $plugin[pluginid]);
  101. foreach($config as $key => $val) {
  102. $data[$key] = $val;
  103. }
  104. C::t('common_pluginvar')->insert($data);
  105. } else {
  106. $data = array();
  107. foreach($config as $key => $val) {
  108. if($key != 'value') {
  109. $data[$key] = $val;
  110. }
  111. }
  112. if($data) {
  113. C::t('common_pluginvar')->update_by_variable($plugin['pluginid'], $config['variable'], $data);
  114. }
  115. }
  116. $pluginvarsnew[] = $config['variable'];
  117. }
  118. $pluginvardiff = array_diff($pluginvars, $pluginvarsnew);
  119. if($pluginvardiff) {
  120. C::t('common_pluginvar')->delete_by_variable($plugin['pluginid'], $pluginvardiff);
  121. }
  122. }
  123. $langexists = updatepluginlanguage($pluginarray);
  124. $pluginarray['plugin']['modules'] = dunserialize($pluginarray['plugin']['modules']);
  125. $plugin['modules'] = dunserialize($plugin['modules']);
  126. if(!empty($plugin['modules']['system'])) {
  127. $pluginarray['plugin']['modules']['system'] = $plugin['modules']['system'];
  128. }
  129. $plugin['modules']['extra']['installtype'] = $installtype;
  130. $pluginarray['plugin']['modules']['extra'] = $plugin['modules']['extra'];
  131. if(!empty($pluginarray['intro']) || $langexists) {
  132. if(!empty($pluginarray['intro'])) {
  133. require_once libfile('function/discuzcode');
  134. $pluginarray['plugin']['modules']['extra']['intro'] = discuzcode(strip_tags($pluginarray['intro']), 1, 0);
  135. }
  136. $langexists && $pluginarray['plugin']['modules']['extra']['langexists'] = 1;
  137. }
  138. if(!empty($pluginarray['uninstallfile'])) {
  139. $pluginarray['plugin']['modules']['extra']['uninstallfile'] = $pluginarray['uninstallfile'];
  140. }
  141. if(!empty($pluginarray['checkfile'])) {
  142. $pluginarray['plugin']['modules']['extra']['checkfile'] = $pluginarray['checkfile'];
  143. }
  144. if(!empty($pluginarray['enablefile'])) {
  145. $pluginarray['plugin']['modules']['extra']['enablefile'] = $pluginarray['enablefile'];
  146. }
  147. if(!empty($pluginarray['disablefile'])) {
  148. $pluginarray['plugin']['modules']['extra']['disablefile'] = $pluginarray['disablefile'];
  149. }
  150. $pluginarray['plugin']['modules'] = serialize($pluginarray['plugin']['modules']);
  151. C::t('common_plugin')->update($plugin['pluginid'], array('version' => $pluginarray['plugin']['version'], 'modules' => $pluginarray['plugin']['modules']));
  152. cloudaddons_installlog($pluginarray['plugin']['identifier'].'.plugin');
  153. cron_create($pluginarray['plugin']['identifier']);
  154. updatecache(array('plugin', 'setting', 'styles'));
  155. cleartemplatecache();
  156. dsetcookie('addoncheck_plugin', '', -1);
  157. return true;
  158. }
  159. function modulecmp($a, $b) {
  160. return $a['displayorder'] > $b['displayorder'] ? 1 : -1;
  161. }
  162. function updatepluginlanguage($pluginarray) {
  163. global $_G;
  164. if(!$pluginarray['language']) {
  165. return false;
  166. }
  167. foreach(array('script', 'template', 'install', 'system') as $type) {
  168. loadcache('pluginlanguage_'.$type, 1);
  169. if($type != 'system') {
  170. if(!empty($pluginarray['language'][$type.'lang'])) {
  171. $_G['cache']['pluginlanguage_'.$type][$pluginarray['plugin']['identifier']] = $pluginarray['language'][$type.'lang'];
  172. }
  173. } else {
  174. if(!empty($_G['config']['plugindeveloper']) && @include(DISCUZ_ROOT.'./data/plugindata/'.$pluginarray['plugin']['identifier'].'.lang.php')) {
  175. if(!empty($systemlang[$pluginarray['plugin']['identifier']])) {
  176. $pluginarray['language']['systemlang'] = $systemlang[$pluginarray['plugin']['identifier']];
  177. }
  178. }
  179. foreach($pluginarray['language']['systemlang'] as $file => $vars) {
  180. foreach($vars as $key => $var) {
  181. $_G['cache']['pluginlanguage_system'][$file][$key] = $var;
  182. }
  183. }
  184. }
  185. savecache('pluginlanguage_'.$type, $_G['cache']['pluginlanguage_'.$type]);
  186. }
  187. return true;
  188. }
  189. function runquery($sql) {
  190. global $_G;
  191. $tablepre = $_G['config']['db'][1]['tablepre'];
  192. $dbcharset = $_G['config']['db'][1]['dbcharset'];
  193. $sql = str_replace(array(' cdb_', ' `cdb_', ' pre_', ' `pre_'), array(' {tablepre}', ' `{tablepre}', ' {tablepre}', ' `{tablepre}'), $sql);
  194. $sql = str_replace("\r", "\n", str_replace(array(' {tablepre}', ' `{tablepre}'), array(' '.$tablepre, ' `'.$tablepre), $sql));
  195. $ret = array();
  196. $num = 0;
  197. foreach(explode(";\n", trim($sql)) as $query) {
  198. $queries = explode("\n", trim($query));
  199. foreach($queries as $query) {
  200. $ret[$num] .= $query[0] == '#' || $query[0].$query[1] == '--' ? '' : $query;
  201. }
  202. $num++;
  203. }
  204. unset($sql);
  205. foreach($ret as $query) {
  206. $query = trim($query);
  207. if($query) {
  208. if(substr($query, 0, 12) == 'CREATE TABLE') {
  209. $name = preg_replace("/CREATE TABLE ([a-z0-9_]+) .*/is", "\\1", $query);
  210. DB::query(createtable($query, $dbcharset));
  211. } else {
  212. DB::query($query);
  213. }
  214. }
  215. }
  216. }
  217. function createtable($sql, $dbcharset) {
  218. $type = strtoupper(preg_replace("/^\s*CREATE TABLE\s+.+\s+\(.+?\).*(ENGINE|TYPE)\s*=\s*([a-z]+?).*$/isU", "\\2", $sql));
  219. $type = in_array($type, array('MYISAM', 'HEAP')) ? $type : 'MYISAM';
  220. return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).
  221. (DB::$db->version() > '4.1' ? " ENGINE=$type DEFAULT CHARSET=$dbcharset" : " TYPE=$type");
  222. }
  223. function updatetable($sql) {
  224. global $_G;
  225. $config = array(
  226. 'dbcharset' => $_G['config']['db']['1']['dbcharset'],
  227. 'charset' => $_G['config']['output']['charset'],
  228. 'tablepre' => $_G['config']['db']['1']['tablepre']
  229. );
  230. preg_match_all("/CREATE\s+TABLE.+?pre\_(.+?)\s*\((.+?)\)\s*(ENGINE|TYPE)\s*=\s*(\w+)/is", $sql, $matches);
  231. $newtables = empty($matches[1])?array():$matches[1];
  232. $newsqls = empty($matches[0])?array():$matches[0];
  233. if(empty($newtables) || empty($newsqls)) {
  234. return array(1);
  235. }
  236. foreach($newtables as $i => $newtable) {
  237. $newcols = updatetable_getcolumn($newsqls[$i]);
  238. if(!$query = DB::query("SHOW CREATE TABLE ".DB::table($newtable), 'SILENT')) {
  239. preg_match("/(CREATE TABLE .+?)\s*(ENGINE|TYPE)\s*=\s*(\w+)/is", $newsqls[$i], $maths);
  240. $maths[3] = strtoupper($maths[3]);
  241. if($maths[3] == 'MEMORY' || $maths[3] == 'HEAP') {
  242. $type = helper_dbtool::dbversion() > '4.1' ? " ENGINE=MEMORY".(empty($config['dbcharset'])?'':" DEFAULT CHARSET=$config[dbcharset]" ): " TYPE=HEAP";
  243. } else {
  244. $type = helper_dbtool::dbversion() > '4.1' ? " ENGINE=MYISAM".(empty($config['dbcharset'])?'':" DEFAULT CHARSET=$config[dbcharset]" ): " TYPE=MYISAM";
  245. }
  246. $usql = $maths[1].$type;
  247. $usql = str_replace("CREATE TABLE IF NOT EXISTS pre_", 'CREATE TABLE IF NOT EXISTS '.$config['tablepre'], $usql);
  248. $usql = str_replace("CREATE TABLE pre_", 'CREATE TABLE '.$config['tablepre'], $usql);
  249. if(!DB::query($usql, 'SILENT')) {
  250. return array(-1, $newtable);
  251. }
  252. } else {
  253. $value = DB::fetch($query);
  254. $oldcols = updatetable_getcolumn($value['Create Table']);
  255. $updates = array();
  256. $allfileds =array_keys($newcols);
  257. foreach ($newcols as $key => $value) {
  258. if($key == 'PRIMARY') {
  259. if($value != $oldcols[$key]) {
  260. if(!empty($oldcols[$key])) {
  261. $usql = "RENAME TABLE ".DB::table($newtable)." TO ".DB::table($newtable.'_bak');
  262. if(!DB::query($usql, 'SILENT')) {
  263. return array(-1, $newtable);
  264. }
  265. }
  266. $updates[] = "ADD PRIMARY KEY $value";
  267. }
  268. } elseif ($key == 'KEY') {
  269. foreach ($value as $subkey => $subvalue) {
  270. if(!empty($oldcols['KEY'][$subkey])) {
  271. if($subvalue != $oldcols['KEY'][$subkey]) {
  272. $updates[] = "DROP INDEX `$subkey`";
  273. $updates[] = "ADD INDEX `$subkey` $subvalue";
  274. }
  275. } else {
  276. $updates[] = "ADD INDEX `$subkey` $subvalue";
  277. }
  278. }
  279. } elseif ($key == 'UNIQUE') {
  280. foreach ($value as $subkey => $subvalue) {
  281. if(!empty($oldcols['UNIQUE'][$subkey])) {
  282. if($subvalue != $oldcols['UNIQUE'][$subkey]) {
  283. $updates[] = "DROP INDEX `$subkey`";
  284. $updates[] = "ADD UNIQUE INDEX `$subkey` $subvalue";
  285. }
  286. } else {
  287. $usql = "ALTER TABLE ".DB::table($newtable)." DROP INDEX `$subkey`";
  288. DB::query($usql, 'SILENT');
  289. $updates[] = "ADD UNIQUE INDEX `$subkey` $subvalue";
  290. }
  291. }
  292. } else {
  293. if(!empty($oldcols[$key])) {
  294. if(strtolower($value) != strtolower($oldcols[$key])) {
  295. $updates[] = "CHANGE `$key` `$key` $value";
  296. }
  297. } else {
  298. $i = array_search($key, $allfileds);
  299. $fieldposition = $i > 0 ? 'AFTER `'.$allfileds[$i-1].'`' : 'FIRST';
  300. $updates[] = "ADD `$key` $value $fieldposition";
  301. }
  302. }
  303. }
  304. if(!empty($updates)) {
  305. $usql = "ALTER TABLE ".DB::table($newtable)." ".implode(', ', $updates);
  306. if(!DB::query($usql, 'SILENT')) {
  307. return array(-1, $newtable);
  308. }
  309. }
  310. }
  311. }
  312. return array(1);
  313. }
  314. function updatetable_getcolumn($creatsql) {
  315. $creatsql = preg_replace("/ COMMENT '.*?'/i", '', $creatsql);
  316. preg_match("/\((.+)\)\s*(ENGINE|TYPE)\s*\=/is", $creatsql, $matchs);
  317. $cols = explode("\n", $matchs[1]);
  318. $newcols = array();
  319. foreach ($cols as $value) {
  320. $value = trim($value);
  321. if(empty($value)) continue;
  322. $value = updatetable_remakesql($value);
  323. if(substr($value, -1) == ',') $value = substr($value, 0, -1);
  324. $vs = explode(' ', $value);
  325. $cname = $vs[0];
  326. if($cname == 'KEY' || $cname == 'INDEX' || $cname == 'UNIQUE') {
  327. $name_length = strlen($cname);
  328. if($cname == 'UNIQUE') $name_length = $name_length + 4;
  329. $subvalue = trim(substr($value, $name_length));
  330. $subvs = explode(' ', $subvalue);
  331. $subcname = $subvs[0];
  332. $newcols[$cname][$subcname] = trim(substr($value, ($name_length+2+strlen($subcname))));
  333. } elseif($cname == 'PRIMARY') {
  334. $newcols[$cname] = trim(substr($value, 11));
  335. } else {
  336. $newcols[$cname] = trim(substr($value, strlen($cname)));
  337. }
  338. }
  339. return $newcols;
  340. }
  341. function updatetable_remakesql($value) {
  342. $value = trim(preg_replace("/\s+/", ' ', $value));
  343. $value = str_replace(array('`',', ', ' ,', '( ' ,' )', 'mediumtext'), array('', ',', ',','(',')','text'), $value);
  344. return $value;
  345. }
  346. function cron_create($pluginid, $filename = null, $name = null, $weekday = null, $day = null, $hour = null, $minute = null) {
  347. if(!ispluginkey($pluginid)) {
  348. return false;
  349. }
  350. $dir = DISCUZ_ROOT.'./source/plugin/'.$pluginid.'/cron';
  351. if(!file_exists($dir)) {
  352. return false;
  353. }
  354. $crondir = dir($dir);
  355. while($filename = $crondir->read()) {
  356. if(!in_array($filename, array('.', '..')) && preg_match("/^cron\_[\w\.]+$/", $filename)) {
  357. $content = file_get_contents($dir.'/'.$filename);
  358. preg_match("/cronname\:(.+?)\n/", $content, $r);$name = lang('plugin/'.$pluginid, trim($r[1]));
  359. preg_match("/week\:(.+?)\n/", $content, $r);$weekday = trim($r[1]) ? intval($r[1]) : -1;
  360. preg_match("/day\:(.+?)\n/", $content, $r);$day = trim($r[1]) ? intval($r[1]) : -1;
  361. preg_match("/hour\:(.+?)\n/", $content, $r);$hour = trim($r[1]) ? intval($r[1]) : -1;
  362. preg_match("/minute\:(.+?)\n/", $content, $r);$minute = trim($r[1]) ? trim($r[1]) : 0;
  363. $minutenew = explode(',', $minute);
  364. foreach($minutenew as $key => $val) {
  365. $minutenew[$key] = $val = intval($val);
  366. if($val < 0 || $var > 59) {
  367. unset($minutenew[$key]);
  368. }
  369. }
  370. $minutenew = array_slice(array_unique($minutenew), 0, 12);
  371. $minutenew = implode("\t", $minutenew);
  372. $filename = $pluginid.':'.$filename;
  373. $cronid = C::t('common_cron')->get_cronid_by_filename($filename);
  374. if(!$cronid) {
  375. return C::t('common_cron')->insert(array(
  376. 'available' => 1,
  377. 'type' => 'plugin',
  378. 'name' => $name,
  379. 'filename' => $filename,
  380. 'weekday' => $weekday,
  381. 'day' => $day,
  382. 'hour' => $hour,
  383. 'minute' => $minutenew,
  384. ), true);
  385. } else {
  386. C::t('common_cron')->update($cronid, array(
  387. 'name' => $name,
  388. 'weekday' => $weekday,
  389. 'day' => $day,
  390. 'hour' => $hour,
  391. 'minute' => $minutenew,
  392. ));
  393. return $cronid;
  394. }
  395. }
  396. }
  397. }
  398. function cron_delete($pluginid) {
  399. if(!ispluginkey($pluginid)) {
  400. return false;
  401. }
  402. $dir = DISCUZ_ROOT.'./source/plugin/'.$pluginid.'/cron';
  403. if(!file_exists($dir)) {
  404. return false;
  405. }
  406. $crondir = dir($dir);
  407. $count = 0;
  408. while($filename = $crondir->read()) {
  409. if(!in_array($filename, array('.', '..')) && preg_match("/^cron\_[\w\.]+$/", $filename)) {
  410. $filename = $pluginid.':'.$filename;
  411. $cronid = C::t('common_cron')->get_cronid_by_filename($filename);
  412. C::t('common_cron')->delete($cronid);
  413. $count++;
  414. }
  415. }
  416. return $count;
  417. }
  418. function domain_create($pluginid, $domain, $domainroot) {
  419. $plugin = C::t('common_plugin')->fetch_by_identifier($pluginid);
  420. if(!$plugin || !$plugin['available']) {
  421. return;
  422. }
  423. C::t('common_domain')->delete_by_id_idtype($plugin['pluginid'], 'plugin');
  424. $data = array(
  425. 'id' => $plugin['pluginid'],
  426. 'idtype' => 'plugin',
  427. 'domain' => $domain,
  428. 'domainroot' => $domainroot,
  429. );
  430. C::t('common_domain')->insert($data);
  431. require_once libfile('function/cache');
  432. updatecache('setting');
  433. }
  434. function domain_delete($pluginid) {
  435. $plugin = C::t('common_plugin')->fetch_by_identifier($pluginid);
  436. if(!$plugin || !$plugin['available']) {
  437. return;
  438. }
  439. C::t('common_domain')->delete_by_id_idtype($plugin['pluginid'], 'plugin');
  440. require_once libfile('function/cache');
  441. updatecache('setting');
  442. }
  443. ?>