PageRenderTime 53ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/source/function/function_block.php

https://github.com/jinbo51/DiscuzX
PHP | 1121 lines | 1047 code | 68 blank | 6 comment | 318 complexity | 5441424a47c61cdc028e4f64f72ab734 MD5 | raw file
Possible License(s): BSD-3-Clause
  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_block.php 32895 2013-03-21 04:18:15Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. function block_script($blockclass, $script) {
  12. global $_G;
  13. $arr = explode('_', $blockclass);
  14. $dirname = $arr[0];
  15. $xmlid = null;
  16. if(strtoupper($dirname) == 'XML' && $script == 'xml' && intval($arr[1])) {
  17. $xmlid = intval($arr[1]);
  18. }
  19. $var = "blockscript_{$dirname}_{$script}";
  20. $script = 'block_'.$script;
  21. if(!isset($_G[$var]) || $xmlid) {
  22. if(@include_once libfile($script, 'class/block/'.$dirname)) {
  23. $_G[$var] = $xmlid ? new $script($xmlid) : new $script();
  24. } else {
  25. $_G[$var] = false;
  26. }
  27. }
  28. return $_G[$var];
  29. }
  30. function block_get_batch($parameter) {
  31. global $_G;
  32. $bids = $parameter && is_array($parameter) ? $parameter : ($parameter ? explode(',', $parameter) : array());
  33. $bids = array_map('intval', $bids);
  34. $bids = array_unique($bids);
  35. $styleids = array();
  36. if($bids) {
  37. if(C::t('common_block')->allowmem) {
  38. if(($cachedata = memory('get', $bids, 'blockcache_')) !== false) {
  39. foreach ($cachedata as $bid => $block) {
  40. $_G['block'][$bid] = $block;
  41. if($block['styleid']) {
  42. $styleids[$block['styleid']] = $block['styleid'];
  43. }
  44. }
  45. if($styleids) {
  46. block_getstyle($styleids);
  47. }
  48. if(!($bids = array_diff($bids, array_keys($cachedata)))) {
  49. return true;
  50. }
  51. }
  52. }
  53. $items = $prelist = array();
  54. foreach(C::t('common_block_item')->fetch_all_by_bid($bids) as $item) {
  55. if($item['itemtype'] == '1' && $item['enddate'] && $item['enddate'] < TIMESTAMP) {
  56. continue;
  57. } elseif($item['itemtype'] == '1' && (!$item['startdate'] || $item['startdate'] <= TIMESTAMP)) {
  58. if (!empty($items[$item['bid']][$item['displayorder']])) {
  59. $prelist[$item['bid']] = array();
  60. }
  61. $prelist[$item['bid']][$item['displayorder']] = $item;
  62. }
  63. $items[$item['bid']][$item['displayorder']] = $item;
  64. }
  65. foreach(C::t('common_block')->fetch_all($bids) as $bid => $block) {
  66. if(!empty($block['styleid']) && $block['styleid'] > 0) {
  67. $styleids[] = intval($block['styleid']);
  68. }
  69. if(!empty($items[$bid])) {
  70. ksort($items[$bid]);
  71. $newitem = array();
  72. if(!empty($prelist[$bid])) {
  73. $countpre = 0;
  74. foreach($items[$bid] as $position => $item) {
  75. if(empty($prelist[$bid][$position])) {
  76. if(isset($items[$bid][$position+$countpre])) {
  77. $newitem[$position+$countpre] = $item;
  78. }
  79. } else {
  80. if ($item['itemtype']=='1') {
  81. if ($prelist[$bid][$position]['startdate'] >= $item['startdate']) {
  82. $newitem[$position] = $prelist[$bid][$position];
  83. } else {
  84. $newitem[$position] = $item;
  85. }
  86. } else {
  87. $newitem[$position] = $prelist[$bid][$position];
  88. $countpre++;
  89. if(isset($items[$bid][$position+$countpre])) {
  90. $newitem[$position+$countpre] = $item;
  91. }
  92. }
  93. }
  94. }
  95. ksort($newitem);
  96. }
  97. $block['itemlist'] = empty($newitem) ? $items[$bid] : $newitem;
  98. }
  99. $block['param'] = $block['param'] ? dunserialize($block['param']) : array();
  100. $_G['block'][$bid] = $block;
  101. if(C::t('common_block')->allowmem) {
  102. memory('set', 'blockcache_'.$bid, $_G['block'][$bid], C::t('common_block')->cache_ttl);
  103. }
  104. }
  105. }
  106. if($styleids) {
  107. block_getstyle($styleids);
  108. }
  109. }
  110. function block_display_batch($bid) {
  111. echo block_fetch_content($bid);
  112. }
  113. function block_fetch_content($bid, $isjscall=false, $forceupdate=false) {
  114. global $_G;
  115. static $allowmem = null, $cachettl = null;
  116. if($allowmem === null) {
  117. $allowmem = ($cachettl = getglobal('setting/memory/diyblockoutput')) !== null && memory('check');
  118. }
  119. $str = '';
  120. $block = empty($_G['block'][$bid])?array():$_G['block'][$bid];
  121. if(!$block) {
  122. return;
  123. }
  124. if($forceupdate) {
  125. block_updatecache($bid, true);
  126. $block = $_G['block'][$bid];
  127. } elseif($block['cachetime'] > 0 && $_G['timestamp'] - $block['dateline'] > $block['cachetime']) {
  128. $block['cachetimerange'] = empty($block['cachetimerange']) ? (isset($_G['setting']['blockcachetimerange']) ? $_G['setting']['blockcachetimerange'] : '') : $block['cachetimerange'];
  129. $inrange = empty($block['cachetimerange']) ? true : false;
  130. if(!$inrange) {
  131. $block['cachetimerange'] = explode(',', $block['cachetimerange']);
  132. $hour = date('G', TIMESTAMP);
  133. if($block['cachetimerange'][0] <= $block['cachetimerange'][1]) {
  134. $inrange = $block['cachetimerange'][0] <= $hour && $block['cachetimerange'][1] >= $hour;
  135. } else {
  136. $inrange = !($block['cachetimerange'][1] < $hour && $block['cachetimerange'][0] > $hour);
  137. }
  138. }
  139. if($isjscall || $block['punctualupdate']) {
  140. block_updatecache($bid, true);
  141. $block = $_G['block'][$bid];
  142. } elseif((empty($_G['blockupdate']) || $block['dateline'] < $_G['blockupdate']['dateline']) && $inrange) {
  143. $_G['blockupdate'] = array('bid'=>$bid, 'dateline'=>$block['dateline']);
  144. }
  145. }
  146. $hidediv = $isjscall || $block['blocktype'];
  147. $_cache_key = 'blockcache_'.($isjscall ? 'js' : 'htm').'_'.$bid;
  148. if($allowmem && empty($block['hidedisplay']) && empty($block['nocache']) && ($str = memory('get', $_cache_key)) !== false) {
  149. } else {
  150. if($hidediv) {
  151. if($block['summary']) $str .= $block['summary'];
  152. $str .= block_template($bid);
  153. } else {
  154. if($block['title']) $str .= $block['title'];
  155. $str .= '<div id="portal_block_'.$bid.'_content" class="dxb_bc">';
  156. if($block['summary']) {
  157. $str .= "<div class=\"portal_block_summary\">$block[summary]</div>";
  158. }
  159. $str .= block_template($bid);
  160. $str .= '</div>';
  161. }
  162. if($allowmem && empty($block['hidedisplay']) && empty($block['nocache'])) {
  163. memory('set', $_cache_key, $str, C::t('common_block')->cache_ttl);
  164. }
  165. }
  166. if(!$hidediv) {
  167. $classname = !empty($block['classname']) ? $block['classname'].' ' : '';
  168. $div = "<div id=\"portal_block_$bid\" class=\"{$classname}block move-span\">";
  169. if(($_GET['diy'] === 'yes' || $_GET['inajax']) && check_diy_perm()) {
  170. $div .= "<div class='block-name'>$block[name] (ID:$bid)</div>";
  171. }
  172. $str = $div.$str."</div>";
  173. }
  174. if($block['blockclass'] == 'html_html' && $block['script'] == 'search') $str = strtr($str, array('{FORMHASH}'=>FORMHASH));
  175. return !empty($block['hidedisplay']) ? '' : $str;
  176. }
  177. function block_updatecache($bid, $forceupdate=false) {
  178. global $_G;
  179. if((isset($_G['block'][$bid]['cachetime']) && $_G['block'][$bid]['cachetime'] < 0) || !$forceupdate && discuz_process::islocked('block_update_cache', 5)) {
  180. return false;
  181. }
  182. C::t('common_block')->clear_cache($bid);
  183. $block = empty($_G['block'][$bid])?array():$_G['block'][$bid];
  184. if(!$block) {
  185. return false;
  186. }
  187. $obj = block_script($block['blockclass'], $block['script']);
  188. if(is_object($obj)) {
  189. C::t('common_block')->update($bid, array('dateline'=>TIMESTAMP));
  190. $_G['block'][$bid]['dateline'] = TIMESTAMP;
  191. $theclass = block_getclass($block['blockclass']);
  192. $thestyle = !empty($block['styleid']) ? block_getstyle($block['styleid']) : dunserialize($block['blockstyle']);
  193. if(in_array($block['blockclass'], array('forum_thread', 'group_thread', 'space_blog', 'space_pic', 'portal_article'))) {
  194. $datalist = array();
  195. $mapping = array('forum_thread'=>'tid', 'group_thread'=>'tid', 'space_blog'=>'blogid', 'space_blog'=>'picid', 'portal_article'=>'aid');
  196. $idtype = $mapping[$block['blockclass']];
  197. $bannedids = !empty($block['param']['bannedids']) ? explode(',', $block['param']['bannedids']) : array();
  198. $bannedsql = $bannedids ? ' AND id NOT IN ('.dimplode($bannedids).')' : '';
  199. $shownum = intval($block['shownum']);
  200. $titlelength = !empty($block['param']['titlelength']) ? intval($block['param']['titlelength']) : 40;
  201. $summarylength = !empty($block['param']['summarylength']) ? intval($block['param']['summarylength']) : 80;
  202. foreach(C::t('common_block_item_data')->fetch_all_by_bid($bid, 1, 0, $shownum * 2, $bannedids, false) as $value) {
  203. $value['title'] = cutstr($value['title'], $titlelength, '');
  204. $value['summary'] = cutstr($value['summary'], $summarylength, '');
  205. $value['itemtype'] = '3';
  206. $datalist[] = $value;
  207. $bannedids[] = intval($value['id']);
  208. }
  209. $leftnum = $block['shownum'] - count($datalist);
  210. if($leftnum > 0 && empty($block['isblank'])) {
  211. if($leftnum != $block['param']['items']) {
  212. $block['param']['items'] = $leftnum;
  213. $block['param']['bannedids'] = implode(',',$bannedids);
  214. }
  215. $return = $obj->getdata($thestyle, $block['param']);
  216. $return['data'] = array_merge($datalist, (array)$return['data']);
  217. } else {
  218. $return['data'] = $datalist;
  219. }
  220. } else {
  221. $return = $obj->getdata($thestyle, $block['param']);
  222. }
  223. if($return['data'] === null) {
  224. $_G['block'][$block['bid']]['summary'] = $return['html'];
  225. C::t('common_block')->update($bid, array('summary'=>$return['html']));
  226. } else {
  227. $_G['block'][$block['bid']]['itemlist'] = block_updateitem($bid, $return['data']);
  228. }
  229. } else {
  230. C::t('common_block')->update($bid, array('dateline'=>TIMESTAMP+999999, 'cachetime'=>0));
  231. $_G['block'][$bid]['dateline'] = TIMESTAMP+999999;
  232. }
  233. if(C::t('common_block')->allowmem) {
  234. memory('set', 'blockcache_'.$bid, $_G['block'][$bid], C::t('common_block')->cache_ttl);
  235. $styleid = $_G['block'][$bid]['styleid'];
  236. if($styleid && $_G['blockstyle_'.$styleid]) {
  237. memory('set', 'blockstylecache_'.$styleid, $_G['blockstyle_'.$styleid], C::t('common_block')->cache_ttl);
  238. }
  239. }
  240. discuz_process::unlock('block_update_cache');
  241. }
  242. function block_template($bid) {
  243. global $_G;
  244. $block = empty($_G['block'][$bid]) ? array() : $_G['block'][$bid];
  245. $theclass = block_getclass($block['blockclass'], false);
  246. $thestyle = !empty($block['styleid']) ? block_getstyle($block['styleid']) : dunserialize($block['blockstyle']);
  247. if(empty($block) || empty($theclass) || empty($thestyle)) {
  248. return false;
  249. }
  250. $template = block_build_template($thestyle['template']);
  251. if(!empty($block['itemlist'])) {
  252. if($thestyle['moreurl']) {
  253. $template = str_replace('{moreurl}', 'portal.php?mod=block&bid='.$bid, $template);
  254. }
  255. $fields = array('picwidth'=>array(), 'picheight'=>array(), 'target'=>array(), 'currentorder'=>array());
  256. if($block['hidedisplay']) {
  257. $fields = array_merge($fields, $theclass['fields']);
  258. } else {
  259. $thestyle['fields'] = !empty($thestyle['fields']) && is_array($thestyle['fields']) ? $thestyle['fields'] : block_parse_fields($template);
  260. foreach($thestyle['fields'] as $k) {
  261. if(isset($theclass['fields'][$k])) {
  262. $fields[$k] = $theclass['fields'][$k];
  263. }
  264. }
  265. }
  266. $order = 0;
  267. $dynamicparts = array();
  268. foreach($block['itemlist'] as $position=>$blockitem) {
  269. $itemid = $blockitem['itemid'];
  270. $order++;
  271. $rkey = $rpattern = $rvalue = $rtpl = array();
  272. $rkeyplug = false;
  273. if(isset($thestyle['template']['index']) && is_array($thestyle['template']['index']) && isset($thestyle['template']['index'][$order])) {
  274. $rkey[] = 'index_'.$order;
  275. $rpattern[] = '/\s*\[index='.$order.'\](.*?)\[\/index\]\s*/is';
  276. $rvalue[] = '';
  277. $rtpl[] = $thestyle['template']['index'][$order];
  278. }
  279. if(empty($rkey)) {
  280. $rkey[] = 'loop';
  281. $rpattern[] = '/\s*\[loop\](.*?)\[\/loop\]\s*/is';
  282. $rvalue[] = isset($dynamicparts['loop']) ? $dynamicparts['loop'][1] : '';
  283. if(is_array($thestyle['template']['order']) && isset($thestyle['template']['order'][$order])) {
  284. $rtpl[] = $thestyle['template']['order'][$order];
  285. } elseif(is_array($thestyle['template']['order']) && isset($thestyle['template']['order']['odd']) && ($order % 2 == 1)) {
  286. $rtpl[] = $thestyle['template']['order']['odd'];
  287. } elseif(is_array($thestyle['template']['order']) && isset($thestyle['template']['order']['even']) && ($order % 2 == 0)) {
  288. $rtpl[] = $thestyle['template']['order']['even'];
  289. } else {
  290. $rtpl[] = $thestyle['template']['loop'];
  291. }
  292. }
  293. if(!empty($thestyle['template']['indexplus'])) {
  294. foreach($thestyle['template']['indexplus'] as $k=>$v) {
  295. if(isset($v[$order])) {
  296. $rkey[] = 'index'.$k.'='.$order;
  297. $rkeyplug = true;
  298. $rpattern[] = '/\[index'.$k.'='.$order.'\](.*?)\[\/index'.$k.'\]/is';
  299. $rvalue[] = '';
  300. $rtpl[] = $v[$order];
  301. }
  302. }
  303. }
  304. if(empty($rkeyplug)) {
  305. if(!empty($thestyle['template']['loopplus'])) {
  306. foreach($thestyle['template']['loopplus'] as $k=>$v) {
  307. $rkey[] = 'loop'.$k;
  308. $rpattern[] = '/\s*\[loop'.$k.'\](.*?)\[\/loop'.$k.'\]\s*/is';
  309. $rvalue[] = isset($dynamicparts['loop'.$k]) ? $dynamicparts['loop'.$k][1] : '';
  310. if(is_array($thestyle['template']['orderplus'][$k]) && isset($thestyle['template']['orderplus'][$k][$order])) {
  311. $rtpl[] = $thestyle['template']['orderplus'][$k][$order];
  312. } elseif(is_array($thestyle['template']['orderplus'][$k]) && isset($thestyle['template']['orderplus'][$k]['odd']) && ($order % 2 == 1)) {
  313. $rtpl[] = $thestyle['template']['orderplus'][$k]['odd'];
  314. } elseif(is_array($thestyle['template']['orderplus'][$k]) && isset($thestyle['template']['orderplus'][$k]['even']) && ($order % 2 == 0)) {
  315. $rtpl[] = $thestyle['template']['orderplus'][$k]['even'];
  316. } else {
  317. $rtpl[] = $thestyle['template']['loopplus'][$k];
  318. }
  319. }
  320. }
  321. }
  322. $blockitem['fields'] = !empty($blockitem['fields']) ? $blockitem['fields'] : array();
  323. $blockitem['fields'] = is_array($blockitem['fields']) ? $blockitem['fields'] : dunserialize($blockitem['fields']);
  324. if(!empty($blockitem['showstyle'])) {
  325. $blockitem['fields']['showstyle'] = dunserialize($blockitem['showstyle']);
  326. }
  327. $blockitem = $blockitem['fields'] + $blockitem;
  328. $blockitem['picwidth'] = !empty($block['picwidth']) ? intval($block['picwidth']) : 'auto';
  329. $blockitem['picheight'] = !empty($block['picheight']) ? intval($block['picheight']) : 'auto';
  330. $blockitem['target'] = !empty($block['target']) ? ' target="_'.$block['target'].'"' : '';
  331. $blockitem['currentorder'] = $order;
  332. $blockitem['parity'] = $order % 2;
  333. $searcharr = $replacearr = array();
  334. $searcharr[] = '{parity}';
  335. $replacearr[] = $blockitem['parity'];
  336. foreach($fields as $key=>$field) {
  337. $replacevalue = $blockitem[$key];
  338. $field['datatype'] = !empty($field['datatype']) ? $field['datatype'] : '';
  339. if($field['datatype'] == 'int') {// int
  340. $replacevalue = intval($replacevalue);
  341. } elseif($field['datatype'] == 'string') {
  342. $replacevalue = preg_quote($replacevalue);
  343. } elseif($field['datatype'] == 'date') {
  344. $replacevalue = dgmdate($replacevalue, $block['dateuformat'] ? 'u' : $block['dateformat'], '9999', $block['dateuformat'] ? $block['dateformat'] : '');
  345. } elseif($field['datatype'] == 'title') {//title
  346. $searcharr[] = '{title-title}';
  347. $replacearr[] = preg_quote(!empty($blockitem['fields']['fulltitle']) ? $blockitem['fields']['fulltitle'] : dhtmlspecialchars($replacevalue));
  348. $searcharr[] = '{alt-title}';
  349. $replacearr[] = preg_quote(!empty($blockitem['fields']['fulltitle']) ? $blockitem['fields']['fulltitle'] : dhtmlspecialchars($replacevalue));
  350. $replacevalue = preg_quote($replacevalue);
  351. if($blockitem['showstyle'] && ($style = block_showstyle($blockitem['showstyle'], 'title'))) {
  352. $replacevalue = '<font style="'.$style.'">'.$replacevalue.'</font>';
  353. }
  354. } elseif($field['datatype'] == 'summary') {//summary
  355. $replacevalue = preg_quote($replacevalue);
  356. if($blockitem['showstyle'] && ($style = block_showstyle($blockitem['showstyle'], 'summary'))) {
  357. $replacevalue = '<font style="'.$style.'">'.$replacevalue.'</font>';
  358. }
  359. } elseif($field['datatype'] == 'pic') {
  360. if($blockitem['picflag'] == '1') {
  361. $replacevalue = $_G['setting']['attachurl'].$replacevalue;
  362. } elseif ($blockitem['picflag'] == '2') {
  363. $replacevalue = $_G['setting']['ftp']['attachurl'].$replacevalue;
  364. }
  365. if($blockitem['picflag'] && $block['picwidth'] && $block['picheight'] && $block['picwidth'] != 'auto' && $block['picheight'] != 'auto') {
  366. if($blockitem['makethumb'] == 1) {
  367. if($blockitem['picflag'] == '1') {
  368. $replacevalue = $_G['setting']['attachurl'].$blockitem['thumbpath'];
  369. } elseif ($blockitem['picflag'] == '2') {
  370. $replacevalue = $_G['setting']['ftp']['attachurl'].$blockitem['thumbpath'];
  371. }
  372. } elseif(!$_G['block_makethumb'] && !$blockitem['makethumb']) {
  373. C::t('common_block_item')->update($itemid, array('makethumb'=>2));
  374. require_once libfile('class/image');
  375. $image = new image();
  376. $thumbpath = block_thumbpath($block, $blockitem);
  377. if($_G['setting']['ftp']['on']) {
  378. $ftp = & discuz_ftp::instance();
  379. $ftp->connect();
  380. if($ftp->connectid && $ftp->ftp_size($thumbpath) > 0 || ($return = $image->Thumb($replacevalue, $thumbpath, $block['picwidth'], $block['picheight'], 2) && $ftp->upload($_G['setting']['attachurl'].'/'.$thumbpath, $thumbpath))) {
  381. $picflag = 1; //common_block_pic表中的picflag标识(0本地,1远程)
  382. $_G['block_makethumb'] = true;
  383. @unlink($_G['setting']['attachdir'].'./'.$thumbpath);
  384. }
  385. } elseif(file_exists($_G['setting']['attachdir'].$thumbpath) || ($return = $image->Thumb($replacevalue, $thumbpath, $block['picwidth'], $block['picheight'], 2))) {
  386. $picflag = 0; //common_block_pic表中的picflag标识(0本地,1远程)
  387. $_G['block_makethumb'] = true;
  388. }
  389. if($_G['block_makethumb']) {
  390. C::t('common_block_item')->update($itemid, array('makethumb'=>1, 'thumbpath' => $thumbpath));
  391. C::t('common_block')->clear_cache($block['bid']);
  392. $thumbdata = array('bid' => $block['bid'], 'itemid' => $itemid, 'pic' => $thumbpath, 'picflag' => $picflag, 'type' => '0');
  393. C::t('common_block_pic')->insert($thumbdata);
  394. }
  395. }
  396. }
  397. }
  398. $searcharr[] = '{'.$key.'}';
  399. $replacearr[] = $replacevalue;
  400. if($block['hidedisplay']) {
  401. if(strpos($replacevalue, "\\") !== false) {
  402. $replacevalue = str_replace(
  403. array('\.', '\\\\', '\+', '\*', '\?', '\[', '\^', '\]', '\$', '\(', '\)', '\{', '\}', '\=', '\!', '\<', '\>', '\|', '\:', '\-'),
  404. array('.', '\\', '+', '*', '?', '[', '^', ']', '$', '(', ')', '{', '}', '=', '!', '<', '>', '|', ':', '-'), $replacevalue);
  405. }
  406. $_G['block_'.$bid][$order-1][$key] = $replacevalue;
  407. }
  408. }
  409. foreach($rtpl as $k=>$str_template) {
  410. if($str_template) {
  411. $str_template = preg_replace('/title=[\'"]{title}[\'"]/', 'title="{title-title}"', $str_template);
  412. $str_template = preg_replace('/alt=[\'"]{title}[\'"]/', 'alt="{alt-title}"', $str_template);
  413. $rvalue[$k] .= str_replace($searcharr, $replacearr, $str_template);
  414. $dynamicparts[$rkey[$k]] = array($rpattern[$k], $rvalue[$k]);
  415. }
  416. }
  417. }// foreach($block['itemlist'] as $itemid=>$blockitem) {
  418. foreach($dynamicparts as $value) {
  419. $template = preg_replace($value[0], $value[1], $template);
  420. }
  421. $template = str_replace('\\', '&#92;', stripslashes($template));
  422. }
  423. $template = preg_replace('/\s*\[(order\d*)=\w+\](.*?)\[\/\\1\]\s*/is', '', $template);
  424. $template = preg_replace('/\s*\[(index\d*)=\w+\](.*?)\[\/\\1\]\s*/is', '', $template);
  425. $template = preg_replace('/\s*\[(loop\d{0,1})\](.*?)\[\/\\1\]\s*/is', '', $template);
  426. return $template;
  427. }
  428. function block_showstyle($showstyle, $key) {
  429. $style = '';
  430. if(!empty($showstyle["{$key}_b"])) {
  431. $style .= 'font-weight: 900;';
  432. }
  433. if(!empty($showstyle["{$key}_i"])) {
  434. $style .= 'font-style: italic;';
  435. }
  436. if(!empty($showstyle["{$key}_u"])) {
  437. $style .= 'text-decoration: underline;';
  438. }
  439. if(!empty($showstyle["{$key}_c"])) {
  440. $style .= 'color: '.$showstyle["{$key}_c"].';';
  441. }
  442. return $style;
  443. }
  444. function block_setting($blockclass, $script, $values = array()) {
  445. global $_G;
  446. $return = array();
  447. $obj = block_script($blockclass, $script);
  448. if(!is_object($obj)) return $return;
  449. return block_makeform($obj->getsetting(), $values);
  450. }
  451. function block_makeform($blocksetting, $values){
  452. global $_G;
  453. static $randomid = 0, $calendar_loaded = false;
  454. $return = array();
  455. foreach($blocksetting as $settingvar => $setting) {
  456. $varname = in_array($setting['type'], array('mradio', 'mcheckbox', 'select', 'mselect')) ?
  457. ($setting['type'] == 'mselect' ? array('parameter['.$settingvar.'][]', $setting['value']) : array('parameter['.$settingvar.']', $setting['value']))
  458. : 'parameter['.$settingvar.']';
  459. $value = isset($values[$settingvar]) ? $values[$settingvar] : $setting['default'];
  460. $type = $setting['type'];
  461. $s = $comment = '';
  462. if(preg_match('/^([\w]+?)_[\w]+$/i', $setting['title'], $match)) {
  463. $langscript = $match[1];
  464. $setname = lang('block/'.$langscript, $setting['title']);
  465. $comment = lang('block/'.$langscript, $setting['title'].'_comment', array(), '');
  466. } else {
  467. $langscript = '';
  468. $setname = $setting['title'];
  469. }
  470. $check = array();
  471. if($type == 'radio') {
  472. $value ? $check['true'] = "checked" : $check['false'] = "checked";
  473. $value ? $check['false'] = '' : $check['true'] = '';
  474. $s .= '<label for="randomid_'.(++$randomid).'" class="lb"><input type="radio" name="'.$varname.'" id="randomid_'.$randomid.'" class="pr" value="1" '.$check['true'].'>'.lang('core', 'yes').'</label>'.
  475. '<label for="randomid_'.(++$randomid).'" class="lb"><input type="radio" name="'.$varname.'" id="randomid_'.$randomid.'" class="pr" value="0" '.$check['false'].'>'.lang('core', 'no').'</label>';
  476. } elseif($type == 'text' || $type == 'password' || $type == 'number') {
  477. $s .= '<input type="'.$type.'" name="'.$varname.'" class="px" value="'.dhtmlspecialchars($value).'" />';
  478. } elseif($type == 'textarea') {
  479. $s .= '<textarea name="'.$varname.'" class="pt" rows="4" cols="40">'.dhtmlspecialchars($value).'</textarea>';
  480. } elseif($type == 'mtextarea') {
  481. $s .= '<textarea name="'.$varname.'" class="pt" rows="4" cols="40" onblur="blockCheckTag(this);">'.dhtmlspecialchars($value).'</textarea>';
  482. } elseif($type == 'select') {
  483. $s .= '<select name="'.$varname[0].'" class="ps">';
  484. foreach($varname[1] as $option) {
  485. $selected = $option[0] == $value ? ' selected="selected"' : '';
  486. $s .= '<option value="'.$option[0].'"'.$selected.'>'.($langscript ? lang('block/'.$langscript, $option[1]) : $option[1]).'</option>';
  487. }
  488. $s .= '</select>';
  489. } elseif($type == 'mradio') {
  490. if(is_array($varname)) {
  491. $radiocheck = array($value => ' checked');
  492. $s .= '<ul'.(empty($varname[2]) ? ' class="pr"' : '').'>';
  493. foreach($varname[1] as $varary) {
  494. if(is_array($varary) && !empty($varary)) {
  495. $s .= '<li'.($radiocheck[$varary[0]] ? ' class="checked"' : '').'><label for="randomid_'.(++$randomid).'"><input type="radio" name="'.$varname[0].'" id="randomid_'.$randomid.'" class="pr" value="'.$varary[0].'"'.$radiocheck[$varary[0]].'>'.($langscript ? lang('block/'.$langscript, $varary[1]) : $varary[1]).'</label></li>';
  496. }
  497. }
  498. $s .= '</ul>';
  499. }
  500. } elseif($type == 'mcheckbox') {
  501. $s .= '<ul class="nofloat">';
  502. foreach($varname[1] as $varary) {
  503. if(is_array($varary) && !empty($varary)) {
  504. $checked = is_array($value) && in_array($varary[0], $value) ? ' checked' : '';
  505. $s .= '<li'.($checked ? ' class="checked"' : '').'><label for="randomid_'.(++$randomid).'"><input type="checkbox" name="'.$varname[0].'[]" id="randomid_'.$randomid.'" class="pc" value="'.$varary[0].'"'.$checked.'>'.($langscript ? lang('block/'.$langscript, $varary[1]) : $varary[1]).'</label></li>';
  506. }
  507. }
  508. $s .= '</ul>';
  509. } elseif($type == 'mselect') {
  510. $s .= '<select name="'.$varname[0].'" class="ps" multiple="multiple" size="10">';
  511. foreach($varname[1] as $option) {
  512. $selected = is_array($value) && in_array($option[0], $value) ? ' selected="selected"' : '';
  513. $s .= '<option value="'.$option[0].'"'.$selected.'>'.($langscript ? lang('block/'.$langscript, $option[1]) : $option[1]).'</option>';
  514. }
  515. $s .= '</select>';
  516. } elseif($type == 'calendar') {
  517. if(! $calendar_loaded) {
  518. $s .= "<script type=\"text/javascript\" src=\"{$_G[setting][jspath]}calendar.js?".VERHASH."\"></script>";
  519. $calendar_loaded = true;
  520. }
  521. $s .= '<input type="text" name="'.$varname.'" class="px" value="'.dhtmlspecialchars($value).'" onclick="showcalendar(event, this, true)" />';
  522. } elseif($type == 'district') {
  523. include_once libfile('function/profile');
  524. $elems = $vals = array();
  525. $districthtml = '';
  526. foreach($setting['value'] as $fieldid) {
  527. $elems[] = 'parameter['.$fieldid.']';
  528. $vals[$fieldid] = $values[$fieldid];
  529. if(!empty($values[$fieldid])) {
  530. $districthtml .= $values[$fieldid].'<input type="hidden" name="parameter['.$fieldid.']" value="'.$values[$fieldid].'" /> ';
  531. }
  532. }
  533. $containertype = strpos($setting['title'], 'birthcity') !== false ? 'birth' : 'reside';
  534. $containerid = 'randomid_'.(++$randomid);
  535. if($districthtml) {
  536. $s .= $districthtml;
  537. $s .= '&nbsp;&nbsp;<a href="javascript:;" onclick="showdistrict(\''.$containerid.'\', ['.dimplode($elems).'], '.count($elems).', \'\', \''.$containertype.'\'); return false;">'.lang('spacecp', 'profile_edit').'</a>';
  538. $s .= '<p id="'.$containerid.'"></p>';
  539. } else {
  540. $s .= "<div id=\"$containerid\">".showdistrict($vals, $elems, $containerid, null, $containertype).'</div>';
  541. }
  542. } elseif($type == 'file') {
  543. $s .= '<input type="'.$type.'" name="'.$varname.'" class="pf" value="'.dhtmlspecialchars($value).'" />';
  544. } elseif($type == 'mfile') {
  545. $s .= '<label for="'.$settingvar.'way_remote"'.' class="lb"><input type="radio" name="'.$settingvar.'_chk" id="'.$settingvar.'way_remote" class="pr" onclick="showpicedit(\''.$settingvar.'\');" checked="checked">'.lang('portalcp', 'remote').'</label>';
  546. $s .= '<label for="'.$settingvar.'way_upload"'.' class="lb"><input type="radio" name="'.$settingvar.'_chk" id="'.$settingvar.'way_upload" class="pr" onclick="showpicedit(\''.$settingvar.'\');">'.lang('portalcp', 'upload').'</label><br />';
  547. $s .= '<input type="text" name="'.$varname.'" id="'.$settingvar.'_remote" class="px" value="'.dhtmlspecialchars($value).'" />';
  548. $s .= '<input type="file" name="'.$settingvar.'" id="'.$settingvar.'_upload" class="pf" value="" style="display:none" />';
  549. } else {
  550. $s .= $type;
  551. }
  552. $return[] = array('title' => $setname, 'html' => $s, 'comment'=>$comment);
  553. }
  554. return $return;
  555. }
  556. function block_updateitem($bid, $items=array()) {
  557. global $_G;
  558. $block = $_G['block'][$bid];
  559. if(!$block) {
  560. if(!($block = C::t('common_block')->fetch($bid))) {
  561. return false;
  562. }
  563. $_G['block'][$bid] = $block;
  564. }
  565. $block['shownum'] = max($block['shownum'], 1);
  566. $showlist = array();
  567. $archivelist = array();
  568. $prelist = array();
  569. $oldvalue = $fixedvalue = $fixedkeys = array();
  570. foreach(C::t('common_block_item')->fetch_all_by_bid($bid, true) as $value) {
  571. $key = $value['idtype'].'_'.$value['id'];
  572. if($value['itemtype'] == '1') {
  573. $fixedvalue[$value['displayorder']][] = $value;
  574. $fixedkeys[$key] = 1;
  575. continue;
  576. } elseif(!isset($oldvalue[$key])) {
  577. $oldvalue[$key] = $value;
  578. } else {
  579. $archivelist[$value['itemid']] = 1;
  580. }
  581. }
  582. $processkeys = array();
  583. $itemcount = count($items);
  584. for($k = 0; $k < $itemcount; $k++) {
  585. $v = $items[$k];
  586. $key = $v['idtype'].'_'.$v['id'];
  587. if(isset($fixedkeys[$key])) {
  588. $items[$k] = null;
  589. } elseif(isset($oldvalue[$key]) && !isset($processkeys[$key])) {
  590. if($oldvalue[$key]['itemtype'] == '2') {
  591. $items[$k] = $oldvalue[$key];
  592. } else {
  593. $items[$k]['itemid'] = $oldvalue[$key]['itemid'];
  594. }
  595. unset($oldvalue[$key]);
  596. $processkeys[$key] = 1;
  597. } elseif(isset($processkeys[$key])) {
  598. unset($items[$k]);
  599. }
  600. }
  601. $items = array_filter($items);
  602. foreach($oldvalue as $value) {
  603. $archivelist[$value['itemid']] = 1;
  604. }
  605. for($i = 1; $i <= $block['shownum']; $i++) {
  606. $jump = false;
  607. if(isset($fixedvalue[$i])) {
  608. foreach($fixedvalue[$i] as $value) {
  609. if($value['startdate'] > TIMESTAMP) {
  610. $prelist[] = $value;
  611. } elseif((!$value['startdate'] || $value['startdate'] <= TIMESTAMP)
  612. && (!$value['enddate'] || $value['enddate'] > TIMESTAMP)) {
  613. $showlist[] = $value;
  614. $jump = true;
  615. } else {
  616. $archivelist[$value['itemid']] = 1;
  617. }
  618. }
  619. }
  620. if(!$jump) {
  621. $curitem = array();
  622. if(!($curitem = array_shift($items))) {
  623. break;
  624. }
  625. $curitem['displayorder'] = $i;
  626. $curitem['makethumb'] = 0;
  627. if($block['picwidth'] && $block['picheight'] && $curitem['picflag']) { //picflag=0为url地址
  628. $thumbpath = empty($curitem['thumbpath']) ? block_thumbpath($block, $curitem) : $curitem['thumbpath'];
  629. if($_G['setting']['ftp']['on']) {
  630. if(empty($ftp) || empty($ftp->connectid)) {
  631. $ftp = & discuz_ftp::instance();
  632. $ftp->connect();
  633. }
  634. if($ftp->ftp_size($thumbpath) > 0) {
  635. $curitem['makethumb'] = 1;
  636. $curitem['picflag'] = 2;
  637. }
  638. } else if(file_exists($_G['setting']['attachdir'].$thumbpath)) {
  639. $curitem['makethumb'] = 1;
  640. $curitem['picflag'] = 1;
  641. }
  642. $curitem['thumbpath'] = $thumbpath;
  643. }
  644. if(is_array($curitem['fields'])) {
  645. $curitem['fields'] = serialize($curitem['fields']);
  646. }
  647. $showlist[] = $curitem;
  648. }
  649. }
  650. foreach($items as $value) {
  651. if(!empty($value['itemid'])) {
  652. $archivelist[$value['itemid']] = 1;
  653. }
  654. }
  655. if($archivelist) {
  656. $delids = array_keys($archivelist);
  657. C::t('common_block_item')->delete_by_itemid_bid($delids, $bid);
  658. block_delete_pic($bid, $delids);
  659. }
  660. $inserts = $itemlist = array();
  661. $itemlist = array_merge($showlist, $prelist);
  662. C::t('common_block_item')->insert_batch($bid, $itemlist);
  663. $showlist = array_filter($showlist);
  664. return $showlist;
  665. }
  666. function block_thumbpath($block, $item) {
  667. global $_G;
  668. $hash = md5($item['pic'].'-'.$item['picflag'].':'.$block['picwidth'].'|'.$block['picheight']);
  669. return 'block/'.substr($hash, 0, 2).'/'.$hash.'.jpg';
  670. }
  671. function block_getclass($classname, $getstyle=false) {
  672. global $_G;
  673. if(!isset($_G['cache']['blockclass'])) {
  674. loadcache('blockclass');
  675. }
  676. $theclass = array();
  677. list($c1, $c2) = explode('_', $classname);
  678. if(is_array($_G['cache']['blockclass']) && isset($_G['cache']['blockclass'][$c1]['subs'][$classname])) {
  679. $theclass = $_G['cache']['blockclass'][$c1]['subs'][$classname];
  680. if($getstyle && !isset($theclass['style'])) {
  681. foreach(C::t('common_block_style')->fetch_all_by_blockclass($classname) as $value) {
  682. $value['template'] = !empty($value['template']) ? (array)(dunserialize($value['template'])) : array();
  683. $value['fields'] = !empty($value['fields']) ? (array)(dunserialize($value['fields'])) : array();
  684. $key = 'blockstyle_'.$value['styleid'];
  685. $_G[$key] = $value;
  686. $theclass['style'][$value['styleid']] = $value;
  687. }
  688. $_G['cache']['blockclass'][$c1]['subs'][$classname] = $theclass;
  689. }
  690. }
  691. return $theclass;
  692. }
  693. function block_getdiyurl($tplname, $diymod = false) {
  694. $mod = $id = $script = $url = '';
  695. $flag = 0;
  696. if (empty ($tplname)) {
  697. $flag = 2;
  698. } else {
  699. list($script,$tpl) = explode('/',$tplname);
  700. if (!empty($tpl)) {
  701. $arr = array();
  702. preg_match_all('/(.*)\_(\d{1,9})/', $tpl,$arr);
  703. $mod = empty($arr[1][0]) ? $tpl : $arr[1][0];
  704. $id = max(intval($arr[2][0]),0);
  705. if($script == 'ranklist') {
  706. $script = 'misc';
  707. $mod = 'ranklist&type='.$mod;
  708. } else {
  709. switch ($mod) {
  710. case 'index' :
  711. $mod = 'index';
  712. break;
  713. case 'discuz' :
  714. $flag = 0;
  715. if($id){
  716. $mod = 'index&gid='.$id;
  717. } else {
  718. $mod = 'index';
  719. }
  720. break;
  721. case 'space_home' :
  722. $mod = 'space';
  723. break;
  724. case 'forumdisplay' :
  725. $flag = $id ? 0 : 1;
  726. $mod .= '&fid='.$id;
  727. break;
  728. case 'viewthread' :
  729. $flag = $id ? 0 : 1;
  730. $mod = 'forumdisplay&fid='.$id;
  731. break;
  732. case 'list' :
  733. $flag = $id ? 0 : 1;
  734. $mod .= '&catid='.$id;
  735. break;
  736. case 'portal_topic_content' :
  737. $flag = $id ? 0 : 1;
  738. $mod = 'topic&topicid='.$id;
  739. break;
  740. case 'view' :
  741. $flag = $id ? 0 : 1;
  742. $mod .= '&aid='.$id;
  743. break;
  744. default :
  745. break;
  746. }
  747. }
  748. }
  749. $url = empty($mod) || $flag == '1' ? '' : $script.'.php?mod='.$mod.($diymod?'&diy=yes':'');
  750. }
  751. return array('url'=>$url,'flag'=>$flag);
  752. }
  753. function block_clear() {
  754. $uselessbids = $usingbids = $bids = array();
  755. $bids = C::t('common_block')->fetch_all_bid_by_blocktype(0,1000);
  756. $usingbids = array_keys(C::t('common_template_block')->fetch_all_by_bid($bids));
  757. $uselessbids = array_diff($bids, $usingbids);
  758. if (!empty($uselessbids)) {
  759. C::t('common_block_item')->delete_by_bid($uselessbids);
  760. C::t('common_block_item_data')->delete_by_bid($uselessbids);
  761. C::t('common_block_favorite')->delete_by_bid($uselessbids);
  762. C::t('common_block_permission')->delete_by_bid_uid_inheritedtplname($uselessbids);
  763. C::t('common_block')->delete($uselessbids);
  764. C::t('common_block')->optimize();
  765. C::t('common_block_item')->optimize();
  766. block_delete_pic($uselessbids);
  767. }
  768. }
  769. function block_getstyle($styleids = array()) {
  770. global $_G;
  771. static $allowmem = null, $cachettl =null;
  772. if($allowmem === null) {
  773. $allowmem = ($cachettl = getglobal('setting/memory/diyblock')) !== null && memory('check');
  774. }
  775. $pre = 'blockstyle_';
  776. if(($ret = $styleids && !is_array($styleids) ? $styleids : false)) {
  777. if($_G[$pre.$ret]) {
  778. return $_G[$pre.$ret];
  779. } else {
  780. $styleids = (array)$styleids;
  781. }
  782. }
  783. $cacheprekey = 'blockstylecache_';
  784. $styleids = array_map('intval', $styleids);
  785. $styleids = array_unique($styleids);
  786. if($styleids) {
  787. if($allowmem) {
  788. if(($cachedata = memory('get', $styleids, $cacheprekey)) !== false) {
  789. foreach ($cachedata as $styleid => $style) {
  790. $_G[$pre.$styleid] = $style;
  791. }
  792. if(!($styleids = array_diff($styleids, array_keys($cachedata)))) {
  793. return $ret ? $_G[$pre.$ret] : true;
  794. }
  795. }
  796. }
  797. if($styleids) {
  798. foreach(C::t('common_block_style')->fetch_all($styleids) as $styleid => $value) {
  799. $value['template'] = !empty($value['template']) ? (array)(dunserialize($value['template'])) : array();
  800. $value['fields'] = !empty($value['fields']) ? (array)(dunserialize($value['fields'])) : array();
  801. $_G[$pre.$styleid] = $value;
  802. if($allowmem) {
  803. memory('set', $cacheprekey.$styleid, $_G[$pre.$styleid], $cachettl);
  804. }
  805. }
  806. }
  807. return $ret ? $_G[$pre.$ret] : true;
  808. }
  809. return array();
  810. }
  811. function blockclass_cache() {
  812. global $_G;
  813. $data = $dirs = $styles = $dataconvert = array();
  814. $dir = DISCUZ_ROOT.'/source/class/block/';
  815. $dh = opendir($dir);
  816. while(($filename=readdir($dh))) {
  817. if(is_dir($dir.$filename) && substr($filename,0,1) != '.') {
  818. $dirs[$filename] = $dir.$filename.'/';
  819. }
  820. }
  821. ksort($dirs);
  822. foreach($dirs as $name=>$dir) {
  823. $blockclass = $blockconvert = array();
  824. if(file_exists($dir.'blockclass.php')) {
  825. include_once($dir.'blockclass.php');
  826. }
  827. if(empty($blockclass['name'])) {
  828. $blockclass['name'] = $name;
  829. } else {
  830. $blockclass['name'] = dhtmlspecialchars($blockclass['name']);
  831. }
  832. $blockclass['subs'] = array();
  833. $dh = opendir($dir);
  834. while(($filename = readdir($dh))) {
  835. $match = $infos = $oneinfo = $fieldsconvert = array();
  836. $scriptname = $scriptclass = '';
  837. if(preg_match('/^(block_[\w]+)\.php$/i', $filename, $match)) {
  838. $scriptclass = $match[1];
  839. $scriptname = preg_replace('/^block_/i', '', $scriptclass);
  840. include_once $dir.$filename;
  841. if(class_exists($scriptclass, false)) {
  842. $obj = new $scriptclass();
  843. if(method_exists($obj, 'name') && method_exists($obj, 'blockclass') && method_exists($obj, 'fields')
  844. && method_exists($obj, 'getsetting') && method_exists($obj, 'getdata')) {
  845. if($scriptclass == 'block_xml') {
  846. foreach($obj->blockdata as $one) {
  847. $oneinfo['name'] = dhtmlspecialchars($one['data']['name']);
  848. $oneinfo['blockclass'] = array($one['id'], $oneinfo['name']);
  849. $oneinfo['fields'] = dhtmlspecialchars($one['data']['fields']);
  850. foreach($one['data']['style'] as $value) {
  851. $arr = array(
  852. 'blockclass'=>'xml_'.$one['id'],
  853. 'name' => dhtmlspecialchars($value['name']),
  854. );
  855. block_parse_template($value['template'], $arr);
  856. $styles[$arr['hash']] = $arr;
  857. }
  858. $infos[] = $oneinfo;
  859. }
  860. } else {
  861. $oneinfo['name'] = $obj->name();
  862. $oneinfo['blockclass'] = $obj->blockclass();
  863. $oneinfo['fields'] = $obj->fields();
  864. $infos[] = $oneinfo;
  865. }
  866. }
  867. if(method_exists($obj, 'fieldsconvert')) {
  868. $fieldsconvert = $obj->fieldsconvert();
  869. }
  870. }
  871. }
  872. foreach($infos as $info) {
  873. if($info['name'] && is_array($info['blockclass']) && $info['blockclass'][0] && $info['blockclass'][1]) {
  874. list($key, $title) = $info['blockclass'];
  875. $key = $name.'_'.$key;
  876. if(!isset($blockclass['subs'][$key])) {
  877. $blockclass['subs'][$key] = array(
  878. 'name' => $title,
  879. 'fields' => $info['fields'],
  880. 'script' => array()
  881. );
  882. }
  883. $blockclass['subs'][$key]['script'][$scriptname] = $info['name'];
  884. if(!isset($blockconvert[$key]) && !empty($fieldsconvert)) {
  885. $blockconvert[$key] = $fieldsconvert;
  886. }
  887. }
  888. }
  889. }
  890. if($blockclass['subs']) {
  891. $data[$name] = $blockclass;
  892. $blockstyle = array();
  893. if(file_exists($dir.'blockstyle.php')) {
  894. include_once($dir.'blockstyle.php');
  895. }
  896. if($blockstyle) {
  897. foreach($blockstyle as $value) {
  898. $arr = array(
  899. 'blockclass'=>$name.'_'.$value['blockclass'],
  900. 'name' => $value['name']
  901. );
  902. block_parse_template($value['template'], $arr);
  903. $styles[$arr['hash']] = $arr;
  904. }
  905. }
  906. }
  907. if(!empty($blockconvert)) {
  908. $dataconvert[$name] = $blockconvert;
  909. }
  910. }
  911. if($styles) {
  912. $hashes = array_keys($styles);
  913. foreach(C::t('common_block_style')->fetch_all_by_hash($hashes) as $value) {
  914. unset($styles[$value['hash']]);
  915. }
  916. if($styles) {
  917. C::t('common_block_style')->insert_batch($styles);
  918. }
  919. }
  920. savecache('blockclass', $data);
  921. savecache('blockconvert', $dataconvert);
  922. }
  923. function block_parse_template($str_template, &$arr) {
  924. $arr['makethumb'] = strexists($str_template, '{picwidth}') ? 1 : 0;
  925. $arr['getpic'] = strexists($str_template, '{pic}') ? 1 : 0;
  926. $arr['getsummary'] = strexists($str_template, '{summary}') ? 1 : 0;
  927. $arr['settarget'] = strexists($str_template, '{target}') ? 1 : 0;
  928. $arr['moreurl'] = strexists($str_template, '{moreurl}') ? 1 : 0;
  929. $fields = block_parse_fields($str_template);
  930. $arr['fields'] = serialize($fields);
  931. $template = array();
  932. $template['raw'] = $str_template;
  933. $template['header'] = $template['footer'] = '';
  934. $template['loop'] = $template['loopplus'] = $template['order'] = $template['orderplus'] = $template['index'] = $template['indexplus'] = array();
  935. $match = array();
  936. if(preg_match('/\[loop\](.*?)\[\/loop]/is', $str_template, $match)) {
  937. $template['loop'] = trim($match[1]);
  938. }
  939. $match = array();
  940. if(preg_match_all('/\[(loop\d)\](.*?)\[\/\\1]/is', $str_template, $match)) {
  941. foreach($match[1] as $key=>$value) {
  942. $content = trim($match[2][$key]);
  943. $k = intval(str_replace('loop', '', $value));
  944. $template['loopplus'][$k] = $content;
  945. }
  946. }
  947. $match = array();
  948. if(preg_match_all('/\[order=(\d+|odd|even)\](.*?)\[\/order]/is', $str_template, $match)) {
  949. foreach($match[1] as $key => $order) {
  950. $template['order'][$order] = trim($match[2][$key]);
  951. }
  952. }
  953. $match = array();
  954. if(preg_match_all('/\[(order\d+)=(\d+|odd|even)\](.*?)\[\/\\1]/is', $str_template, $match)) {
  955. foreach($match[1] as $key=>$value) {
  956. $content = trim($match[3][$key]);
  957. $order = $match[2][$key];
  958. $k = intval(str_replace('order', '', $value));
  959. $template['orderplus'][$k][$order] = $content;
  960. }
  961. }
  962. $match = array();
  963. if(preg_match_all('/\[index=(\d+)\](.*?)\[\/index]/is', $str_template, $match)) {
  964. foreach($match[1] as $key=>$order) {
  965. $template['index'][$order] = trim($match[2][$key]);
  966. }
  967. }
  968. $match = array();
  969. if(preg_match_all('/\[(index\d+)=(\d+)\](.*?)\[\/\\1]/is', $str_template, $match)) {
  970. foreach($match[1] as $key=>$value) {
  971. $content = trim($match[3][$key]);
  972. $order = intval($match[2][$key]);
  973. $k = intval(str_replace('index', '', $value));
  974. $template['indexplus'][$k][$order] = $content;
  975. }
  976. }
  977. $arr['template'] = serialize($template);
  978. $arr['hash'] = substr(md5($arr['blockclass'].'|'.$arr['template']), 8, 8);
  979. }
  980. function block_parse_fields($template) {
  981. $fields = array();
  982. if(preg_match_all('/\{(\w+)\}/', $template, $matches)) {
  983. foreach($matches[1] as $fieldname) {
  984. $fields[] = $fieldname;
  985. }
  986. $fields = array_unique($fields);
  987. $fields = array_diff($fields, array('picwidth', 'picheight', 'target', ''));
  988. $fields = array_values($fields);
  989. }
  990. return $fields;
  991. }
  992. function block_build_template($template) {
  993. if(! is_array($template)) {
  994. return $template;
  995. }
  996. if(!empty($template['raw'])) {
  997. return $template['raw'];
  998. }
  999. $str_template = $template['header'];
  1000. if($template['loop']) {
  1001. $str_template .= "\n[loop]\n{$template['loop']}\n[/loop]";
  1002. }
  1003. if(!empty($template['order']) && is_array($template['order'])) {
  1004. foreach($template['order'] as $key=>$value) {
  1005. $str_template .= "\n[order={$key}]\n{$value}\n[/order]";
  1006. }
  1007. }
  1008. $str_template .= $template['footer'];
  1009. return $str_template;
  1010. }
  1011. function block_isrecommendable($block) {
  1012. return !empty($block) && in_array($block['blockclass'], array('forum_thread', 'group_thread', 'portal_article', 'space_pic', 'space_blog')) ? true : false;
  1013. }
  1014. function block_delete_pic($bid, $itemid = array()) {
  1015. global $_G;
  1016. if(!empty($bid)) {
  1017. $picids = array();
  1018. foreach(C::t('common_block_pic')->fetch_all_by_bid_itemid($bid, $itemid) as $value) {
  1019. $picids[$value['picid']] = $value['picid'];
  1020. if($value['picflag']) {
  1021. ftpcmd('delete', $value['pic']);
  1022. } else {
  1023. @unlink($_G['setting']['attachdir'].'/'.$value['pic']);
  1024. }
  1025. }
  1026. if(!empty($picids)) {
  1027. C::t('common_block_pic')->delete($picids);
  1028. }
  1029. }
  1030. }
  1031. function update_template_block($targettplname, $tpldirectory, $blocks) {
  1032. if(!empty($targettplname)) {
  1033. if(empty($blocks)) {
  1034. C::t('common_template_block')->delete_by_targettplname($targettplname, $tpldirectory);
  1035. } else {
  1036. $oldbids = array();
  1037. $oldbids = array_keys(C::t('common_template_block')->fetch_all_by_targettplname($targettplname, $tpldirectory));
  1038. $newaddbids = array_diff($blocks, $oldbids);
  1039. C::t('common_template_block')->delete_by_targettplname($targettplname, $tpldirectory);
  1040. if($tpldirectory === './template/default') {
  1041. C::t('common_template_block')->delete_by_targettplname($targettplname, '');
  1042. }
  1043. $blocks = array_unique($blocks);
  1044. C::t('common_template_block')->insert_batch($targettplname, $tpldirectory, $blocks);
  1045. if(!empty($newaddbids)) {
  1046. require_once libfile('class/blockpermission');
  1047. $tplpermission = & template_permission::instance();
  1048. $tplpermission->add_blocks($targettplname, $newaddbids);
  1049. }
  1050. }
  1051. }
  1052. }
  1053. ?>