portalcp_diy.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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: portalcp_diy.php 33949 2013-09-05 02:16:25Z laoguozhang $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. $op = in_array($_GET['op'], array('style', 'diy', 'image', 'export', 'import', 'blockclass')) ? $_GET['op'] : '';
  12. if (submitcheck('uploadsubmit')) {
  13. $topicid = intval($_POST['topicid']);
  14. if($topicid) {
  15. $topic = C::t('portal_topic')->fetch($topicid);
  16. if(empty($topic)) {
  17. topic_upload_error('diy_topic_noexist');
  18. }
  19. }
  20. topic_checkperm($topic);
  21. $upload = new discuz_upload();
  22. $upload->init($_FILES['attach'], 'portal');
  23. $attach = $upload->attach;
  24. if(!$upload->error()) {
  25. $upload->save();
  26. }
  27. if($upload->error()) {
  28. topic_upload_error($attach, $upload->error());
  29. } else {
  30. if($attach['isimage']) {
  31. require_once libfile('class/image');
  32. $image = new image();
  33. $attach['thumb'] = $image->Thumb($attach['target'], '', '80', '50');
  34. }
  35. if(getglobal('setting/ftp/on')) {
  36. if(ftpcmd('upload', 'portal/'.$attach['attachment'])) {
  37. if($attach['thumb']) {
  38. ftpcmd('upload', 'portal/'.getimgthumbname($attach['attachment']));
  39. }
  40. $attach['remote'] = 1;
  41. } else {
  42. if(getglobal('setting/ftp/mirror')) {
  43. @unlink($attach['target']);
  44. @unlink(getimgthumbname($attach['target']));
  45. topic_upload_error($attach, 'diy_remote_upload_failed');
  46. }
  47. }
  48. }
  49. $setarr = array(
  50. 'uid' => $_G['uid'],
  51. 'username' => $_G['username'],
  52. 'filename' => $attach['name'],
  53. 'filepath' => $attach['attachment'],
  54. 'size' => $attach['size'],
  55. 'thumb' => $attach['thumb'],
  56. 'remote' => $attach['remote'],
  57. 'dateline' => $_G['timestamp'],
  58. 'topicid' => $topicid
  59. );
  60. $setarr['picid'] = C::t('portal_topic_pic')->insert($setarr, true);
  61. topic_upload_show($topicid);
  62. }
  63. } elseif (submitcheck('diysubmit')) {
  64. require_once libfile('function/portalcp');
  65. $tpldirectory = getstr($_POST['tpldirectory'], 80);
  66. $template = getstr($_POST['template'], 50);
  67. if(dsign($tpldirectory.$template) !== $_POST['diysign']) {
  68. showmessage('diy_sign_invalid');
  69. }
  70. $tpldirectory = ($tpldirectory) ? $tpldirectory : $_G['cache']['style_default']['tpldir'];
  71. $savemod = getstr($_POST['savemod'], 1);
  72. $recover = getstr($_POST['recover'], 1);
  73. $optype = getstr($_POST['optype'],10);
  74. tpl_checkperm($template);
  75. list($template, $clonefile) = explode(':', $template);
  76. list($mod,$file) = explode('/', $template);
  77. $targettplname = $template;
  78. if ($savemod == '1' && !empty($clonefile)) {
  79. $targettplname = $template.'_'.$clonefile;
  80. }
  81. $istopic = $iscategory = $isarticle = false;
  82. if($template == 'portal/portal_topic_content') {
  83. $template = gettopictplname($clonefile);
  84. $istopic = true;
  85. } elseif ($template == 'portal/list') {
  86. $template = getportalcategorytplname($clonefile);
  87. $iscategory = true;
  88. } elseif ($template == 'portal/view') {
  89. $template = getportalarticletplname($clonefile, $template);
  90. $isarticle = true;
  91. }
  92. if(($istopic || $iscategory || $isarticle) && strpos($template, ':') !== false) {
  93. list($tpldirectory, $template) = explode(':', $template);
  94. }
  95. $checktpl = checkprimaltpl($tpldirectory.':'.$template);
  96. if($checktpl !== true) {
  97. showmessage($checktpl);
  98. }
  99. if($optype == 'canceldiy') {
  100. @unlink(DISCUZ_ROOT.'./data/diy/'.$tpldirectory.'/'.$targettplname.'_diy_preview.htm');
  101. if($targettplname == $template) @unlink(DISCUZ_ROOT.'./data/diy/'.$tpldirectory.'/'.$targettplname.'_'.$clonefile.'_diy_preview.htm');
  102. showmessage('do_success');
  103. }
  104. if ($recover == '1') {
  105. $file = './data/diy/'.$tpldirectory.'/'.$targettplname.'.htm';
  106. if (is_file($file.'.bak')) {
  107. copy ($file.'.bak', $file);
  108. } else {
  109. showmessage('diy_backup_noexist');
  110. }
  111. } else {
  112. $templatedata = array();
  113. checksecurity($_POST['spacecss']);
  114. $templatedata['spacecss'] = preg_replace("/(\<|\>)/is", '', $_POST['spacecss']);
  115. $style = empty($_POST['style'])?'':preg_replace("/[^0-9a-z]/i", '', $_POST['style']);
  116. if($style) {
  117. $cssfile = DISCUZ_ROOT.'./static/topic/'.$style.'/style.css';
  118. if(!file_exists($cssfile)) {
  119. showmessage('theme_does_not_exist');
  120. } else {
  121. $templatedata['style'] = "static/topic/$style/style.css";
  122. }
  123. }
  124. $layoutdata = getstr($_POST['layoutdata'],0,0,0,0,1);
  125. require_once libfile('class/xml');
  126. $templatedata['layoutdata'] = xml2array($layoutdata);
  127. if (empty($templatedata['layoutdata'])) showmessage('diy_data_format_invalid');
  128. $r = save_diy_data($tpldirectory, $template, $targettplname, $templatedata, true, $optype);
  129. include_once libfile('function/cache');
  130. updatecache('diytemplatename');
  131. if ($r && $optype != 'savecache') {
  132. if (!$iscategory && !$istopic && empty($savemod) && !empty($clonefile)) {
  133. $delfile = DISCUZ_ROOT.'./data/diy/'.$tpldirectory.'/'.$template.'_'.$clonefile.'.htm';
  134. if (file_exists($delfile)) {
  135. unlink($delfile);
  136. @unlink($delfile.'.bak');
  137. C::t('common_template_block')->delete_by_targettplname("{$template}_{$clonefile}", $tpldirectory);
  138. C::t('common_diy_data')->delete("{$template}_{$clonefile}", $tpldirectory);
  139. include_once libfile('function/cache');
  140. updatecache('diytemplatename');
  141. }
  142. }
  143. }
  144. }
  145. $tourl = empty($_POST['gobackurl']) || strpos($_POST['gobackurl'],'op=add') != false || strpos($_POST['gobackurl'],'&diy=yes') != false ?
  146. str_replace('&diy=yes','',$_SERVER['HTTP_REFERER']) : $_POST['gobackurl'];
  147. $tourl = preg_replace('/[\?|&]preview=yes/', '', $tourl);
  148. showmessage('do_success', $tourl,array('rejs'=>$_POST['rejs']));
  149. }
  150. if($op == 'blockclass') {
  151. loadcache('blockclass');
  152. } elseif($op == 'style') {
  153. if(!$_G['group']['allowmanagetopic'] && !$_G['group']['allowdiy'] && !$_G['group']['allowaddtopic']) {
  154. showmessage('group_nopermission', NULL, array('grouptitle' => $_G['group']['grouptitle']), array('login' => 1));
  155. }
  156. $themes = gettheme('topic');
  157. } elseif ($op == 'diy' || $op == 'image') {
  158. $topicid = intval($_GET['topicid']);
  159. $topic = C::t('portal_topic')->fetch($topicid);
  160. topic_checkperm($topic);
  161. $perpage = 6;
  162. $page = max(1, intval($_GET['page']));
  163. $start= ($page-1) * $perpage;
  164. $list = array();
  165. if ($topicid) {
  166. $count = C::t('portal_topic_pic')->count_by_topicid($topicid);
  167. if (!empty($count)) {
  168. foreach(C::t('portal_topic_pic')->fetch_all_by_topicid($topicid, $start, $perpage) as $value) {
  169. $value['pic'] = pic_get($value['filepath'], 'portal', $value['thumb'], $value['remote']);
  170. $list[] = $value;
  171. }
  172. }
  173. $multi= multi($count, $perpage, $page, "portal.php?mod=portalcp&ac=diy&op=image&topicid=$topicid");
  174. }
  175. } elseif ($op == 'delete') {
  176. $topicid = intval($_GET['topicid']);
  177. $topic = C::t('portal_topic')->fetch($topicid);
  178. topic_checkperm($topic);
  179. $picid = intval($_GET['picid']);
  180. } elseif ($op == 'export') {
  181. if (submitcheck('exportsubmit')) {
  182. $tpl = $_POST['tpl'];
  183. $tpldirectory = $_POST['tpldirectory'];
  184. $frame = $_POST['frame'];
  185. $type = $_POST['type'];
  186. if (!empty($tpl)) {
  187. tpl_checkperm($tpl);
  188. list($tpl,$id) = explode(':', $tpl);
  189. $tplname = $id ? $tpl.'_'.$id : $tpl;
  190. $diydata = C::t('common_diy_data')->fetch($tplname, $tpldirectory);
  191. if(empty($diydata) && $id) $diydata = C::t('common_diy_data')->fetch($tpl, $tpldirectory);
  192. if ($diydata) {
  193. $filename = $diydata['targettplname'];
  194. $diycontent = dunserialize($diydata['diycontent']);
  195. if (empty($diycontent)) showmessage('diy_no_export_data');
  196. if ($frame) {
  197. $area = '';
  198. $filename = $frame;
  199. $framedata = array();
  200. foreach ($diycontent['layoutdata'] as $key => $value) {
  201. $framedata = getobjbyname($frame, $value);
  202. if ($framedata) {
  203. $area = $key;
  204. getframeblock(array($framedata['type'].'`'.$frame => $framedata['content']));
  205. break;
  206. }
  207. }
  208. } else {
  209. foreach ($diycontent['layoutdata'] as $key => $value) {
  210. if (!empty($value)) getframeblock($value);
  211. }
  212. }
  213. $diycontent['blockdata'] = block_export($_G['curtplbid']);
  214. if ($frame) {
  215. $diycontent['spacecss'] = getcssdata($diycontent['spacecss']);
  216. $diycontent['layoutdata'] = array();
  217. $area = empty($area) ? 'diy1' : $area;
  218. $diycontent['layoutdata'][$area][$framedata['type'].'`'.$frame] = $framedata['content'] ? $framedata['content'] : array();
  219. }
  220. dheader('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  221. dheader('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  222. dheader('Cache-Control: no-cache, must-revalidate');
  223. dheader('Pragma: no-cache');
  224. dheader('Content-Encoding: none');
  225. if ($type == 'txt') {
  226. $str = serialize($diycontent);
  227. dheader('Content-Length: '.strlen($str));
  228. dheader('Content-Disposition: attachment; filename='.$filename.'.txt');
  229. dheader('Content-Type: text/plant');
  230. } else {
  231. require_once libfile('class/xml');
  232. $str = array2xml($diycontent, true);
  233. dheader('Content-Length: '.strlen($str));
  234. dheader('Content-Disposition: attachment; filename='.$filename.'.xml');
  235. dheader('Content-Type: text/xml');
  236. }
  237. echo $str;
  238. exit();
  239. } else {
  240. showmessage('diy_export_no_data','/');
  241. }
  242. } else {
  243. showmessage('diy_export_tpl_invalid','/');
  244. }
  245. }
  246. showmessage('diy_operation_invalid','/');
  247. } elseif ($op == 'import') {
  248. $tpl = $_POST['tpl'] ? $_POST['tpl'] : $_GET['tpl'];
  249. tpl_checkperm($tpl);
  250. if (submitcheck('importsubmit')) {
  251. $isinner = false;
  252. $filename = '';
  253. if($_POST['importfilename']) {
  254. $filename = DISCUZ_ROOT.'./template/default/portal/diyxml/'.$_POST['importfilename'].'.xml';
  255. $isinner = true;
  256. } else {
  257. $upload = new discuz_upload();
  258. $upload->init($_FILES['importfile'], 'temp');
  259. $attach = $upload->attach;
  260. if(!$upload->error()) {
  261. $upload->save();
  262. }
  263. if($upload->error()) {
  264. showmessage($upload->error(),'portal.php',array('status'=>$upload->error()));
  265. } else {
  266. $filename = $attach['target'];
  267. }
  268. }
  269. if($filename) {
  270. $arr = import_diy($filename);
  271. if(!$isinner) {
  272. @unlink($filename);
  273. }
  274. if (!empty($arr)) {
  275. $search = array('/\<script/i', '/\<\/script\>/i', "/\r/", "/\n/", '/(\[script [^>]*?)(src=)(.*?\[\/script\])/');
  276. $replace = array('[script', '[/script]', '', '', '$1[src=]$3');
  277. $arr['css'] = str_replace(array("\r","\n"),array(''),$arr['css']);
  278. $jsarr = array('status'=>1,'css'=>$arr['css'],'bids'=>implode(',',$arr['mapping']));
  279. foreach ($arr['html'] as $key => $value) {
  280. $value = preg_replace($search,$replace,$value);
  281. $jsarr['html'][$key] = $value;
  282. }
  283. showmessage('do_success','portal.php',$jsarr);
  284. } else {
  285. showmessage('do_success','portal.php',array('status'=>0));
  286. }
  287. }
  288. }
  289. $xmlarr = array();
  290. if ($_GET['type'] == 1) {
  291. $xmlfilepath = DISCUZ_ROOT.'./template/default/portal/diyxml/';
  292. if(($dh = @opendir($xmlfilepath))) {
  293. while(($file = @readdir($dh)) !== false) {
  294. if(fileext($file) == 'xml') {
  295. $xmlarr[substr($file, 0, -4)] = getdiyxmlname($file, $xmlfilepath);
  296. }
  297. }
  298. closedir($dh);
  299. }
  300. arsort($xmlarr);
  301. }
  302. } else {
  303. showmessage('undefined_action');
  304. }
  305. include_once template("portal/portalcp_diy");
  306. function topic_upload_error($attach, $msg='') {
  307. echo '<script>';
  308. echo 'parent.document.getElementById(\'uploadmsg\').innerHTML = \''.$attach['name'].' '.lang('home/template', 'upload_error').$msg.'\';';
  309. echo '</script>';
  310. exit();
  311. }
  312. function topic_upload_show($topicid) {
  313. echo '<script>';
  314. echo 'parent.ajaxget("portal.php?mod=portalcp&ac=diy&op=image&topicid='.$topicid.'&", "diyimages");';
  315. echo 'parent.document.uploadpic.attach.value = \'\';';
  316. echo 'Util.toggleEle(\'upload\')';
  317. echo '</script>';
  318. exit();
  319. }
  320. function tpl_checkperm($tpl) {
  321. global $_G;
  322. list($file,$id) = explode(':', $tpl);
  323. if ($file == 'portal/portal_topic_content') {
  324. $topicid = max(0,intval($id));
  325. $topic = C::t('portal_topic')->fetch($topicid);
  326. topic_checkperm($topic);
  327. } elseif($file == 'portal/list'){
  328. $catid = max(0,intval($id));
  329. $category = $_G['cache']['portalcategory'][$catid];
  330. category_checkperm($category);
  331. } else {
  332. if(!$_G['group']['allowdiy']) {
  333. showmessage('diy_nopermission');
  334. }
  335. }
  336. }
  337. function category_checkperm($category) {
  338. global $_G;
  339. if(empty($category)) {
  340. showmessage('topic_not_exist');
  341. }
  342. if($_G['group']['allowdiy']) return true;
  343. if(!$_G['group']['allowdiy'] && (!$_G['group']['allowaddtopic'] || $_G['uid'] != $topic['uid'])) {
  344. showmessage('topic_edit_nopermission');
  345. }
  346. }
  347. function topic_checkperm($topic) {
  348. global $_G;
  349. if(empty($topic)) {
  350. showmessage('topic_not_exist');
  351. }
  352. if(!$_G['group']['allowmanagetopic'] && (!$_G['group']['allowaddtopic'] || $_G['uid'] != $topic['uid'])) {
  353. showmessage('topic_edit_nopermission');
  354. }
  355. }
  356. function gettopictplname($topicid) {
  357. $topicid = max(0,intval($topicid));
  358. $topic = C::t('portal_topic')->fetch($topicid);
  359. return !empty($topic) && !empty($topic['primaltplname']) ? $topic['primaltplname'] : getglobal('cache/style_default/tpldir').':portal/portal_topic_content';
  360. }
  361. function getportalcategorytplname($catid) {
  362. global $_G;
  363. $catid = max(0,intval($catid));
  364. $category = $_G['cache']['portalcategory'][$catid];
  365. return !empty($category) && !empty($category['primaltplname']) ? $category['primaltplname'] : getglobal('cache/style_default/tpldir').':portal/list';
  366. }
  367. function getportalarticletplname($catid, $primaltplname = ''){
  368. if(($catid = intval($catid))) {
  369. if(($category = C::t('portal_category')->fetch($catid))) {
  370. $primaltplname = $category['articleprimaltplname'];
  371. }
  372. if(empty($primaltplname)) {
  373. $primaltplname = getglobal('cache/style_default/tpldir').':portal/view';
  374. C::t('portal_category')->update($catid, array('articleprimaltplname' => $primaltplname));
  375. }
  376. }
  377. return $primaltplname;
  378. }
  379. function getdiyxmlname($filename, $path) {
  380. $content = @file_get_contents($path.$filename);
  381. $name = $filename;
  382. if($content) {
  383. preg_match("/\<\!\-\-\[name\](.+?)\[\/name\]\-\-\>/i", trim($content), $mathes);
  384. if(!empty($mathes[1])) {
  385. preg_match("/^\{lang (.+?)\}$/", $mathes[1], $langs);
  386. if(!empty($langs[1])) {
  387. $name = lang('portalcp', $langs[1]);
  388. } else {
  389. $name = dhtmlspecialchars($mathes[1]);
  390. }
  391. }
  392. }
  393. return $name;
  394. }
  395. ?>