portalcp_portalblock.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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_portalblock.php 31958 2012-10-26 05:11:05Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. require_once libfile('function/block');
  12. $op = in_array($_GET['op'], array('recommend', 'getblocklist', 'verifydata', 'verifieddata')) ? $_GET['op'] : 'getblocklist';
  13. $initemdata = $op === 'verifydata' || $op === 'verifieddata' ? true : false;
  14. $_GET['idtype'] = dhtmlspecialchars($_GET['idtype']);
  15. $_GET['id'] = intval($_GET['id']);
  16. $allowdiy = checkperm('allowdiy');
  17. if(!$allowdiy && !$admincp4 && !$admincp5 && !$admincp6) {
  18. showmessage('portal_nopermission', dreferer());
  19. }
  20. loadcache('diytemplatename');
  21. $pagebids = $tpls = $blocks = $tplpermissions = $wherearr = $blockfavorite = $topblocks = $blockdata = array();
  22. if(submitcheck('getblocklistsubmit') || submitcheck('verifieddatasubmit') || submitcheck('verifydatasubmit')) {
  23. if($allowdiy) {
  24. $tpls = array_keys($_G['cache']['diytemplatename']);
  25. } else {
  26. $permissions = getallowdiytemplate($_G['uid']);
  27. foreach($permissions as $value) {
  28. if($value['allowmanage'] || ($value['allowrecommend'] && empty($value['needverify']))) {
  29. $tpls[] = $value['targettplname'];
  30. }
  31. }
  32. }
  33. if(!$allowdiy) {
  34. foreach(C::t('common_block_permission')->fetch_all_by_uid($_G['uid']) as $bid => $value) {
  35. if($value['allowmanage'] == 1 || ($value['allowrecommend'] == 1 && $value['needverify'] == 0)) {
  36. $bids[$value['bid']] = intval($value['bid']);
  37. }
  38. }
  39. }
  40. if(!$allowdiy && empty($bids)) {
  41. showmessage('portal_nopermission', dreferer());
  42. }
  43. if(submitcheck('getblocklistsubmit')) {
  44. $updatebids = $_GET['bids'];
  45. $updatebids = array_map('intval', $updatebids);
  46. $updatebids = array_filter($updatebids);
  47. $updatebids = !$allowdiy ? array_intersect($bids, $updatebids) : $updatebids;
  48. if($updatebids) {
  49. C::t('common_block')->update_dateline_to_expired($updatebids, TIMESTAMP);
  50. }
  51. showmessage('portalcp_block_push_the_update_line', dreferer());
  52. } else if (submitcheck('verifydatasubmit')) {
  53. if(!in_array($_POST['optype'], array('pass', 'delete'))) {
  54. showmessage('select_a_option', dreferer());
  55. }
  56. $ids = $updatebids = array();
  57. if($_POST['ids']) {
  58. foreach(C::t('common_block_item_data')->fetch_all($_POST['ids']) as $value) {
  59. if($allowdiy || in_array($value['bid'], $bids)) {
  60. $ids[$value['dataid']] = intval($value['dataid']);
  61. $updatebids[$value['bid']] = $value['bid'];
  62. }
  63. }
  64. }
  65. if(empty($ids)) {
  66. showmessage('select_a_moderate_data', dreferer());
  67. }
  68. if($_POST['optype']=='pass') {
  69. C::t('common_block_item_data')->update($ids, array('isverified' => '1', 'verifiedtime' => $_G['timestamp']));
  70. if($updatebids) {
  71. C::t('common_block')->update_dateline_to_expired($updatebids, TIMESTAMP);
  72. }
  73. } elseif($_POST['optype']=='delete') {
  74. C::t('common_block_item_data')->delete($ids);
  75. }
  76. showmessage('operation_done', dreferer());
  77. } else if (submitcheck('verifieddatasubmit')) {
  78. $ids = array();
  79. if(!empty($_POST['ids'])) {
  80. foreach(C::t('common_block_item_data')->fetch_all($_POST['ids']) as $value) {
  81. if($allowdiy || in_array($value['bid'], $bids)) {
  82. $ids[$value['dataid']] = intval($value['dataid']);
  83. }
  84. }
  85. }
  86. if($ids) {
  87. C::t('common_block_item_data')->delete($ids);
  88. }
  89. $displayorder = array_map('intval', $_POST['displayorder']);
  90. foreach($displayorder as $dataid => $displayorder) {
  91. if($displayorder !== intval($_POST['olddisplayorder'][$dataid])) {
  92. C::t('common_block_item_data')->update($dataid, array('displayorder' => $displayorder));
  93. }
  94. }
  95. showmessage('do_success', dreferer());
  96. }
  97. } else {
  98. $perpage = $op == 'recommend' ? 16 : 30;
  99. $page = max(1,intval($_GET['page']));
  100. $start = ($page-1)*$perpage;
  101. if($start<0) $start = 0;
  102. $theurl = 'portal.php?mod=portalcp&ac=portalblock&op='.$op.'&idtype='.$_GET['idtype'].'&id='.$_GET['id'];
  103. $showfavorite = $page == 1 ? true : false;
  104. $multi = $fields = $leftjoin = '';
  105. $blockfavorite = block_get_favorite($_G['uid']);
  106. if($allowdiy) {
  107. $tpls = $_G['cache']['diytemplatename'];
  108. } else {
  109. $tplpermissions = getallowdiytemplate($_G['uid']);
  110. foreach($tplpermissions as $value) {
  111. if($value['allowmanage'] || ($value['allowrecommend'] && empty($value['needverify'])) || ($op=='recommend' && $value['allowrecommend'])) {
  112. $tpls[$value['targettplname']] = isset($_G['cache']['diytemplatename'][$value['targettplname']]) ? $_G['cache']['diytemplatename'][$value['targettplname']] : $value['targettplname'];
  113. }
  114. }
  115. $fields = ',bp.allowmanage,bp.allowrecommend,bp.needverify';
  116. $leftjoin = ' LEFT JOIN '.DB::table('common_block_permission').' bp ON b.bid=bp.bid';
  117. $wherearr[] = "bp.uid='$_G[uid]'";
  118. $wherearr[] = "(bp.allowmanage='1' OR (bp.allowrecommend='1'".($op == 'recommend' ? '' : "AND bp.needverify='0'")."))";
  119. }
  120. $hasinblocks = array();
  121. if($op == 'recommend' && in_array($_GET['idtype'], array('tid', 'gtid', 'blogid', 'picid', 'aid'), true) && ($_GET['id'] = dintval($_GET['id']))) {
  122. $hasinblocks = C::t('common_block')->fetch_all_recommended_block($_GET['id'], $_GET['idtype'], $wherearr, $leftjoin, $fields);
  123. }
  124. if($_GET['searchkey']) {
  125. $_GET['searchkey'] = trim($_GET['searchkey']);
  126. $showfavorite = false;
  127. if (preg_match('/^[#]?(\d+)$/', $_GET['searchkey'],$match)) {
  128. $bid = intval($match[1]);
  129. $wherearr[] = " (b.bid='$bid' OR b.name='$bid')";
  130. } else {
  131. $wherearr[] = " b.name LIKE '%".stripsearchkey($_GET['searchkey'])."%'";
  132. $perpage = 10000;
  133. }
  134. $_GET['searchkey'] = dhtmlspecialchars($_GET['searchkey']);
  135. $theurl .= '&searchkey='.$_GET['searchkey'];
  136. }
  137. if($_GET['targettplname']) {
  138. $showfavorite = false;
  139. $targettplname = trim($_GET['targettplname']);
  140. $pagebids = array_keys(C::t('common_template_block')->fetch_all_by_targettplname($targettplname));
  141. if(!empty($pagebids)) {
  142. $wherearr[] = "b.bid IN (".dimplode($pagebids).")";
  143. $perpage = 10000;
  144. } else {
  145. $wherearr[] = "b.bid='0'";
  146. }
  147. $_GET['targettplname'] = dhtmlspecialchars($_GET['targettplname']);
  148. $theurl .= '&targettplname='.$_GET['targettplname'];
  149. }
  150. if($op == 'recommend') {
  151. $rewhere = array();
  152. switch ($_GET['idtype']) {
  153. case 'tid' :
  154. $rewhere[] = "(blockclass='forum_thread' OR blockclass='forum_activity' OR blockclass='forum_trade')";
  155. break;
  156. case 'gtid' :
  157. $rewhere[] = "(blockclass='group_thread' OR blockclass='group_activity' OR blockclass='group_trade')";
  158. break;
  159. case 'blogid' :
  160. $rewhere[] = "blockclass ='space_blog'";
  161. break;
  162. case 'picid' :
  163. $rewhere[] = "blockclass ='space_pic'";
  164. break;
  165. case 'aid' :
  166. $rewhere[] = "blockclass ='portal_article'";
  167. break;
  168. }
  169. $wherearr = array_merge($rewhere, $wherearr);
  170. $where = $wherearr ? ' WHERE '.implode(' AND ', $wherearr) : '';
  171. if(($count = C::t('common_block')->count_by_where($where, $leftjoin))) {
  172. foreach(C::t('common_block')->fetch_all_by_where($where, $start, $perpage, $leftjoin, $fields) as $value) {
  173. $value = formatblockvalue($value);
  174. if(!$value['favorite'] || !$showfavorite) {
  175. $blocks[$value['bid']] = $value;
  176. }
  177. }
  178. if(!empty($blockfavorite) && $showfavorite) {
  179. $blocks = $blockfavorite + $blocks;
  180. }
  181. $theurl = $_G['inajax'] ? $theurl.'&getdata=yes' : $theurl;
  182. if($_G['inajax']) $_GET['ajaxtarget'] = 'itemeditarea';
  183. $multi = multi($count, $perpage, $page, $theurl);
  184. }
  185. } else {
  186. $where = empty($wherearr) ? '' : ' WHERE '.implode(' AND ', $wherearr);
  187. if(($count = C::t('common_block')->count_by_where($where, $leftjoin))) {
  188. foreach(C::t('common_block')->fetch_all_by_where($where, $initemdata ? 0 : $start, $initemdata ? 0 : $perpage, $leftjoin, $fields) as $value) {
  189. $value = formatblockvalue($value);
  190. if(!$value['favorite'] || !$showfavorite) {
  191. $blocks[$value['bid']] = $value;
  192. }
  193. }
  194. if(!empty($blockfavorite) && $showfavorite) {
  195. $blocks = $blockfavorite + $blocks;
  196. }
  197. $multi = $initemdata ? '' : multi($count, $perpage, $page, $theurl);
  198. }
  199. }
  200. if($blocks) {
  201. $losttpls = $alldata = array();
  202. $bids = array_keys($blocks);
  203. if($bids) {
  204. foreach(C::t('common_template_block')->fetch_all_by_bid($bids) as $value) {
  205. $alldata[] = $value;
  206. if(!isset($_G['cache']['diytemplatename'][$value['targettplname']])) {
  207. $losttpls[$value['targettplname']] = $value['targettplname'];
  208. }
  209. }
  210. if($losttpls) {
  211. $lostnames = getdiytplnames($losttpls);
  212. foreach($lostnames as $pre => $datas) {
  213. foreach($datas as $id => $name) {
  214. $_G['cache']['diytemplatename'][$pre.$id] = $tpls[$pre.$id] = $name;
  215. }
  216. }
  217. }
  218. foreach($alldata as $value) {
  219. $diyurl = block_getdiyurl($value['targettplname']);
  220. $diyurl = $diyurl['url'];
  221. $tplname = isset($_G['cache']['diytemplatename'][$value['targettplname']]) ? $_G['cache']['diytemplatename'][$value['targettplname']] : $value['targettplname'];
  222. if(!isset($tpls[$value['targettplname']])) {
  223. $tpls[$value['targettplname']] = $tplname;
  224. }
  225. $blocks[$value['bid']]['page'][$value['targettplname']] = $diyurl ? '<a href="'.$diyurl.'" target="_blank">'.$tplname.'</a>' : $tplname;
  226. }
  227. }
  228. if($initemdata) {
  229. $isverified = $op === 'verifieddata' ? 1 : 0;
  230. $count = C::t('common_block_item_data')->count_by_bid($bids, $isverified);
  231. $blockdata = $count ? C::t('common_block_item_data')->fetch_all_by_bid($bids, $isverified, $start, $perpage) : array();
  232. $multi = multi($count, $perpage, $page, $theurl);
  233. }
  234. }
  235. }
  236. include_once template("portal/portalcp_portalblock");
  237. function formatblockvalue($value) {
  238. global $blockfavorite;
  239. $value['name'] = empty($value['name']) ? '<strong>#'.$value['bid'].'</strong>' : $value['name'];
  240. $theclass = block_getclass($value['blockclass']);
  241. $value['blockclassname'] = $theclass['name'];
  242. $value['datasrc'] = $theclass['script'][$value['script']];
  243. $value['isrecommendable'] = block_isrecommendable($value);
  244. $value['perm'] = formatblockpermissoin($value);
  245. $value['favorite'] = isset($blockfavorite[$value['bid']]) ? true : false;
  246. return $value;
  247. }
  248. function formatblockpermissoin($block) {
  249. static $allowdiy = null;
  250. $allowdiy = isset($allowdiy) ? $allowdiy : checkperm('allowdiy');;
  251. $perm = array('allowproperty' => 0, 'allowdata'=> 0);
  252. $bid = !empty($block) ? $block['bid'] : 0;
  253. if(!empty($bid)) {
  254. if($allowdiy) {
  255. $perm = array('allowproperty' => 1, 'allowdata'=> 1);
  256. } else {
  257. if($block['allowmanage']) {
  258. $perm = array('allowproperty' => 1, 'allowdata'=> 1);
  259. }
  260. if ($block['allowrecommend'] && !$block['needverify']) {
  261. $perm['allowdata'] = 1;
  262. }
  263. }
  264. }
  265. return $perm;
  266. }
  267. function block_get_favorite($uid){
  268. static $allowdiy = null;
  269. $allowdiy = isset($allowdiy) ? $allowdiy : checkperm('allowdiy');
  270. $blockfavorite = $permission = array();
  271. $uid = intval($uid);
  272. if($uid) {
  273. foreach(C::t('common_block_favorite')->fetch_all_by_uid($uid) as $value) {
  274. $blockfavorite[$value['bid']] = $value['bid'];
  275. }
  276. }
  277. if(!empty($blockfavorite)) {
  278. $blocks = C::t('common_block')->fetch_all($blockfavorite);
  279. if(!$allowdiy) {
  280. $permission = C::t('common_block_permission')->fetch_all_by_uid($uid);
  281. }
  282. foreach($blocks as $bid => $value) {
  283. if(!$allowdiy && $permission[$bid]) {
  284. $value = array_merge($value, $permission[$bid]);
  285. }
  286. $value = formatblockvalue($value);
  287. $value['favorite'] = true;
  288. $blockfavorite[$value['bid']] = $value;
  289. }
  290. $blockfavorite = array_filter($blockfavorite, 'is_array');
  291. }
  292. return $blockfavorite;
  293. }
  294. ?>