PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/source/function/function_block.php

https://github.com/kuaileshike/upload
PHP | 1117 lines | 1042 code | 69 blank | 6 comment | 317 complexity | b9aae82551f7d4d575af2c4498cc443e MD5 | raw file
  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 32018 2012-10-31 06:49:32Z 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_replace("/([\$|\\\\])/", '\\\\$1', $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. $replacevalue = preg_replace("/([\$|\\\\])/", '\\\\$1', $replacevalue);
  347. $searcharr[] = '{title-title}';
  348. $replacearr[] = !empty($blockitem['fields']['fulltitle']) ? $blockitem['fields']['fulltitle'] : dhtmlspecialchars($replacevalue);
  349. $searcharr[] = '{alt-title}';
  350. $replacearr[] = !empty($blockitem['fields']['fulltitle']) ? $blockitem['fields']['fulltitle'] : dhtmlspecialchars($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_replace("/([\$|\\\\])/", '\\\\$1', $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. $_G['block_'.$bid][$order-1][$key] = $replacevalue;
  402. }
  403. }
  404. foreach($rtpl as $k=>$str_template) {
  405. if($str_template) {
  406. $str_template = preg_replace('/title=[\'"]{title}[\'"]/', 'title="{title-title}"', $str_template);
  407. $str_template = preg_replace('/alt=[\'"]{title}[\'"]/', 'alt="{alt-title}"', $str_template);
  408. $rvalue[$k] .= str_replace($searcharr, $replacearr, $str_template);
  409. $dynamicparts[$rkey[$k]] = array($rpattern[$k], $rvalue[$k]);
  410. }
  411. }
  412. }// foreach($block['itemlist'] as $itemid=>$blockitem) {
  413. foreach($dynamicparts as $value) {
  414. $template = preg_replace($value[0], $value[1], $template);
  415. }
  416. $template = stripslashes($template);
  417. }
  418. $template = preg_replace('/\s*\[(order\d*)=\w+\](.*?)\[\/\\1\]\s*/is', '', $template);
  419. $template = preg_replace('/\s*\[(index\d*)=\w+\](.*?)\[\/\\1\]\s*/is', '', $template);
  420. $template = preg_replace('/\s*\[(loop\d{0,1})\](.*?)\[\/\\1\]\s*/is', '', $template);
  421. return $template;
  422. }
  423. function block_showstyle($showstyle, $key) {
  424. $style = '';
  425. if(!empty($showstyle["{$key}_b"])) {
  426. $style .= 'font-weight: 900;';
  427. }
  428. if(!empty($showstyle["{$key}_i"])) {
  429. $style .= 'font-style: italic;';
  430. }
  431. if(!empty($showstyle["{$key}_u"])) {
  432. $style .= 'text-decoration: underline;';
  433. }
  434. if(!empty($showstyle["{$key}_c"])) {
  435. $style .= 'color: '.$showstyle["{$key}_c"].';';
  436. }
  437. return $style;
  438. }
  439. function block_setting($blockclass, $script, $values = array()) {
  440. global $_G;
  441. $return = array();
  442. $obj = block_script($blockclass, $script);
  443. if(!is_object($obj)) return $return;
  444. return block_makeform($obj->getsetting(), $values);
  445. }
  446. function block_makeform($blocksetting, $values){
  447. global $_G;
  448. static $randomid = 0, $calendar_loaded = false;
  449. $return = array();
  450. foreach($blocksetting as $settingvar => $setting) {
  451. $varname = in_array($setting['type'], array('mradio', 'mcheckbox', 'select', 'mselect')) ?
  452. ($setting['type'] == 'mselect' ? array('parameter['.$settingvar.'][]', $setting['value']) : array('parameter['.$settingvar.']', $setting['value']))
  453. : 'parameter['.$settingvar.']';
  454. $value = isset($values[$settingvar]) ? $values[$settingvar] : $setting['default'];
  455. $type = $setting['type'];
  456. $s = $comment = '';
  457. if(preg_match('/^([\w]+?)_[\w]+$/i', $setting['title'], $match)) {
  458. $langscript = $match[1];
  459. $setname = lang('block/'.$langscript, $setting['title']);
  460. $comment = lang('block/'.$langscript, $setting['title'].'_comment', array(), '');
  461. } else {
  462. $langscript = '';
  463. $setname = $setting['title'];
  464. }
  465. $check = array();
  466. if($type == 'radio') {
  467. $value ? $check['true'] = "checked" : $check['false'] = "checked";
  468. $value ? $check['false'] = '' : $check['true'] = '';
  469. $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>'.
  470. '<label for="randomid_'.(++$randomid).'" class="lb"><input type="radio" name="'.$varname.'" id="randomid_'.$randomid.'" class="pr" value="0" '.$check['false'].'>'.lang('core', 'no').'</label>';
  471. } elseif($type == 'text' || $type == 'password' || $type == 'number') {
  472. $s .= '<input type="'.$type.'" name="'.$varname.'" class="px" value="'.dhtmlspecialchars($value).'" />';
  473. } elseif($type == 'textarea') {
  474. $s .= '<textarea name="'.$varname.'" class="pt" rows="4" cols="40">'.dhtmlspecialchars($value).'</textarea>';
  475. } elseif($type == 'mtextarea') {
  476. $s .= '<textarea name="'.$varname.'" class="pt" rows="4" cols="40" onblur="blockCheckTag(this);">'.dhtmlspecialchars($value).'</textarea>';
  477. } elseif($type == 'select') {
  478. $s .= '<select name="'.$varname[0].'" class="ps">';
  479. foreach($varname[1] as $option) {
  480. $selected = $option[0] == $value ? ' selected="selected"' : '';
  481. $s .= '<option value="'.$option[0].'"'.$selected.'>'.($langscript ? lang('block/'.$langscript, $option[1]) : $option[1]).'</option>';
  482. }
  483. $s .= '</select>';
  484. } elseif($type == 'mradio') {
  485. if(is_array($varname)) {
  486. $radiocheck = array($value => ' checked');
  487. $s .= '<ul'.(empty($varname[2]) ? ' class="pr"' : '').'>';
  488. foreach($varname[1] as $varary) {
  489. if(is_array($varary) && !empty($varary)) {
  490. $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>';
  491. }
  492. }
  493. $s .= '</ul>';
  494. }
  495. } elseif($type == 'mcheckbox') {
  496. $s .= '<ul class="nofloat">';
  497. foreach($varname[1] as $varary) {
  498. if(is_array($varary) && !empty($varary)) {
  499. $checked = is_array($value) && in_array($varary[0], $value) ? ' checked' : '';
  500. $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>';
  501. }
  502. }
  503. $s .= '</ul>';
  504. } elseif($type == 'mselect') {
  505. $s .= '<select name="'.$varname[0].'" class="ps" multiple="multiple" size="10">';
  506. foreach($varname[1] as $option) {
  507. $selected = is_array($value) && in_array($option[0], $value) ? ' selected="selected"' : '';
  508. $s .= '<option value="'.$option[0].'"'.$selected.'>'.($langscript ? lang('block/'.$langscript, $option[1]) : $option[1]).'</option>';
  509. }
  510. $s .= '</select>';
  511. } elseif($type == 'calendar') {
  512. if(! $calendar_loaded) {
  513. $s .= "<script type=\"text/javascript\" src=\"{$_G[setting][jspath]}calendar.js?".VERHASH."\"></script>";
  514. $calendar_loaded = true;
  515. }
  516. $s .= '<input type="text" name="'.$varname.'" class="px" value="'.dhtmlspecialchars($value).'" onclick="showcalendar(event, this, true)" />';
  517. } elseif($type == 'district') {
  518. include_once libfile('function/profile');
  519. $elems = $vals = array();
  520. $districthtml = '';
  521. foreach($setting['value'] as $fieldid) {
  522. $elems[] = 'parameter['.$fieldid.']';
  523. $vals[$fieldid] = $values[$fieldid];
  524. if(!empty($values[$fieldid])) {
  525. $districthtml .= $values[$fieldid].'<input type="hidden" name="parameter['.$fieldid.']" value="'.$values[$fieldid].'" /> ';
  526. }
  527. }
  528. $containertype = strpos($setting['title'], 'birthcity') !== false ? 'birth' : 'reside';
  529. $containerid = 'randomid_'.(++$randomid);
  530. if($districthtml) {
  531. $s .= $districthtml;
  532. $s .= '&nbsp;&nbsp;<a href="javascript:;" onclick="showdistrict(\''.$containerid.'\', ['.dimplode($elems).'], '.count($elems).', \'\', \''.$containertype.'\'); return false;">'.lang('spacecp', 'profile_edit').'</a>';
  533. $s .= '<p id="'.$containerid.'"></p>';
  534. } else {
  535. $s .= "<div id=\"$containerid\">".showdistrict($vals, $elems, $containerid, null, $containertype).'</div>';
  536. }
  537. } elseif($type == 'file') {
  538. $s .= '<input type="'.$type.'" name="'.$varname.'" class="pf" value="'.dhtmlspecialchars($value).'" />';
  539. } elseif($type == 'mfile') {
  540. $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>';
  541. $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 />';
  542. $s .= '<input type="text" name="'.$varname.'" id="'.$settingvar.'_remote" class="px" value="'.dhtmlspecialchars($value).'" />';
  543. $s .= '<input type="file" name="'.$settingvar.'" id="'.$settingvar.'_upload" class="pf" value="" style="display:none" />';
  544. } else {
  545. $s .= $type;
  546. }
  547. $return[] = array('title' => $setname, 'html' => $s, 'comment'=>$comment);
  548. }
  549. return $return;
  550. }
  551. function block_updateitem($bid, $items=array()) {
  552. global $_G;
  553. $block = $_G['block'][$bid];
  554. if(!$block) {
  555. if(!($block = C::t('common_block')->fetch($bid))) {
  556. return false;
  557. }
  558. $_G['block'][$bid] = $block;
  559. }
  560. $block['shownum'] = max($block['shownum'], 1);
  561. $showlist = array();
  562. $archivelist = array();
  563. $prelist = array();
  564. $oldvalue = $fixedvalue = $fixedkeys = array();
  565. foreach(C::t('common_block_item')->fetch_all_by_bid($bid, true) as $value) {
  566. $key = $value['idtype'].'_'.$value['id'];
  567. if($value['itemtype'] == '1') {
  568. $fixedvalue[$value['displayorder']][] = $value;
  569. $fixedkeys[$key] = 1;
  570. continue;
  571. } elseif(!isset($oldvalue[$key])) {
  572. $oldvalue[$key] = $value;
  573. } else {
  574. $archivelist[$value['itemid']] = 1;
  575. }
  576. }
  577. $processkeys = array();
  578. $itemcount = count($items);
  579. for($k = 0; $k < $itemcount; $k++) {
  580. $v = $items[$k];
  581. $key = $v['idtype'].'_'.$v['id'];
  582. if(isset($fixedkeys[$key])) {
  583. $items[$k] = null;
  584. } elseif(isset($oldvalue[$key]) && !isset($processkeys[$key])) {
  585. if($oldvalue[$key]['itemtype'] == '2') {
  586. $items[$k] = $oldvalue[$key];
  587. } else {
  588. $items[$k]['itemid'] = $oldvalue[$key]['itemid'];
  589. }
  590. unset($oldvalue[$key]);
  591. $processkeys[$key] = 1;
  592. } elseif(isset($processkeys[$key])) {
  593. unset($item[$k]);
  594. }
  595. }
  596. $items = array_filter($items);
  597. foreach($oldvalue as $value) {
  598. $archivelist[$value['itemid']] = 1;
  599. }
  600. for($i = 1; $i <= $block['shownum']; $i++) {
  601. $jump = false;
  602. if(isset($fixedvalue[$i])) {
  603. foreach($fixedvalue[$i] as $value) {
  604. if($value['startdate'] > TIMESTAMP) {
  605. $prelist[] = $value;
  606. } elseif((!$value['startdate'] || $value['startdate'] <= TIMESTAMP)
  607. && (!$value['enddate'] || $value['enddate'] > TIMESTAMP)) {
  608. $showlist[] = $value;
  609. $jump = true;
  610. } else {
  611. $archivelist[$value['itemid']] = 1;
  612. }
  613. }
  614. }
  615. if(!$jump) {
  616. $curitem = array();
  617. if(!($curitem = array_shift($items))) {
  618. break;
  619. }
  620. $curitem['displayorder'] = $i;
  621. $curitem['makethumb'] = 0;
  622. if($block['picwidth'] && $block['picheight'] && $curitem['picflag']) { //picflag=0为url地址
  623. $thumbpath = empty($curitem['thumbpath']) ? block_thumbpath($block, $curitem) : $curitem['thumbpath'];
  624. if($_G['setting']['ftp']['on']) {
  625. if(empty($ftp) || empty($ftp->connectid)) {
  626. $ftp = & discuz_ftp::instance();
  627. $ftp->connect();
  628. }
  629. if($ftp->ftp_size($thumbpath) > 0) {
  630. $curitem['makethumb'] = 1;
  631. $curitem['picflag'] = 2;
  632. }
  633. } else if(file_exists($_G['setting']['attachdir'].$thumbpath)) {
  634. $curitem['makethumb'] = 1;
  635. $curitem['picflag'] = 1;
  636. }
  637. $curitem['thumbpath'] = $thumbpath;
  638. }
  639. if(is_array($curitem['fields'])) {
  640. $curitem['fields'] = serialize($curitem['fields']);
  641. }
  642. $showlist[] = $curitem;
  643. }
  644. }
  645. foreach($items as $value) {
  646. if(!empty($value['itemid'])) {
  647. $archivelist[$value['itemid']] = 1;
  648. }
  649. }
  650. if($archivelist) {
  651. $delids = array_keys($archivelist);
  652. C::t('common_block_item')->delete_by_itemid_bid($delids, $bid);
  653. block_delete_pic($bid, $delids);
  654. }
  655. $inserts = $itemlist = array();
  656. $itemlist = array_merge($showlist, $prelist);
  657. C::t('common_block_item')->insert_batch($bid, $itemlist);
  658. $showlist = array_filter($showlist);
  659. return $showlist;
  660. }
  661. function block_thumbpath($block, $item) {
  662. global $_G;
  663. $hash = md5($item['pic'].'-'.$item['picflag'].':'.$block['picwidth'].'|'.$block['picheight']);
  664. return 'block/'.substr($hash, 0, 2).'/'.$hash.'.jpg';
  665. }
  666. function block_getclass($classname, $getstyle=false) {
  667. global $_G;
  668. if(!isset($_G['cache']['blockclass'])) {
  669. loadcache('blockclass');
  670. }
  671. $theclass = array();
  672. list($c1, $c2) = explode('_', $classname);
  673. if(is_array($_G['cache']['blockclass']) && isset($_G['cache']['blockclass'][$c1]['subs'][$classname])) {
  674. $theclass = $_G['cache']['blockclass'][$c1]['subs'][$classname];
  675. if($getstyle && !isset($theclass['style'])) {
  676. foreach(C::t('common_block_style')->fetch_all_by_blockclass($classname) as $value) {
  677. $value['template'] = !empty($value['template']) ? (array)(dunserialize($value['template'])) : array();
  678. $value['fields'] = !empty($value['fields']) ? (array)(dunserialize($value['fields'])) : array();
  679. $key = 'blockstyle_'.$value['styleid'];
  680. $_G[$key] = $value;
  681. $theclass['style'][$value['styleid']] = $value;
  682. }
  683. $_G['cache']['blockclass'][$c1]['subs'][$classname] = $theclass;
  684. }
  685. }
  686. return $theclass;
  687. }
  688. function block_getdiyurl($tplname, $diymod = false) {
  689. $mod = $id = $script = $url = '';
  690. $flag = 0;
  691. if (empty ($tplname)) {
  692. $flag = 2;
  693. } else {
  694. list($script,$tpl) = explode('/',$tplname);
  695. if (!empty($tpl)) {
  696. $arr = array();
  697. preg_match_all('/(.*)\_(\d{1,9})/', $tpl,$arr);
  698. $mod = empty($arr[1][0]) ? $tpl : $arr[1][0];
  699. $id = max(intval($arr[2][0]),0);
  700. if($script == 'ranklist') {
  701. $script = 'misc';
  702. $mod = 'ranklist&type='.$mod;
  703. } else {
  704. switch ($mod) {
  705. case 'index' :
  706. $mod = 'index';
  707. break;
  708. case 'discuz' :
  709. $flag = 0;
  710. if($id){
  711. $mod = 'index&gid='.$id;
  712. } else {
  713. $mod = 'index';
  714. }
  715. break;
  716. case 'space_home' :
  717. $mod = 'space';
  718. break;
  719. case 'forumdisplay' :
  720. $flag = $id ? 0 : 1;
  721. $mod .= '&fid='.$id;
  722. break;
  723. case 'viewthread' :
  724. $flag = $id ? 0 : 1;
  725. $mod = 'forumdisplay&fid='.$id;
  726. break;
  727. case 'list' :
  728. $flag = $id ? 0 : 1;
  729. $mod .= '&catid='.$id;
  730. break;
  731. case 'portal_topic_content' :
  732. $flag = $id ? 0 : 1;
  733. $mod = 'topic&topicid='.$id;
  734. break;
  735. case 'view' :
  736. $flag = $id ? 0 : 1;
  737. $mod .= '&aid='.$id;
  738. break;
  739. default :
  740. break;
  741. }
  742. }
  743. }
  744. $url = empty($mod) || $flag == '1' ? '' : $script.'.php?mod='.$mod.($diymod?'&diy=yes':'');
  745. }
  746. return array('url'=>$url,'flag'=>$flag);
  747. }
  748. function block_clear() {
  749. $uselessbids = $usingbids = $bids = array();
  750. $bids = C::t('common_block')->fetch_all_bid_by_blocktype(0,1000);
  751. $usingbids = array_keys(C::t('common_template_block')->fetch_all_by_bid($bids));
  752. $uselessbids = array_diff($bids, $usingbids);
  753. if (!empty($uselessbids)) {
  754. C::t('common_block_item')->delete_by_bid($uselessbids);
  755. C::t('common_block_item_data')->delete_by_bid($uselessbids);
  756. C::t('common_block_favorite')->delete_by_bid($uselessbids);
  757. C::t('common_block_permission')->delete_by_bid_uid_inheritedtplname($uselessbids);
  758. C::t('common_block')->delete($uselessbids);
  759. C::t('common_block')->optimize();
  760. C::t('common_block_item')->optimize();
  761. block_delete_pic($uselessbids);
  762. }
  763. }
  764. function block_getstyle($styleids = array()) {
  765. global $_G;
  766. static $allowmem = null, $cachettl =null;
  767. if($allowmem === null) {
  768. $allowmem = ($cachettl = getglobal('setting/memory/diyblock')) !== null && memory('check');
  769. }
  770. $pre = 'blockstyle_';
  771. if(($ret = $styleids && !is_array($styleids) ? $styleids : false)) {
  772. if($_G[$pre.$ret]) {
  773. return $_G[$pre.$ret];
  774. } else {
  775. $styleids = (array)$styleids;
  776. }
  777. }
  778. $cacheprekey = 'blockstylecache_';
  779. $styleids = array_map('intval', $styleids);
  780. $styleids = array_unique($styleids);
  781. if($styleids) {
  782. if($allowmem) {
  783. if(($cachedata = memory('get', $styleids, $cacheprekey)) !== false) {
  784. foreach ($cachedata as $styleid => $style) {
  785. $_G[$pre.$styleid] = $style;
  786. }
  787. if(!($styleids = array_diff($styleids, array_keys($cachedata)))) {
  788. return $ret ? $_G[$pre.$ret] : true;
  789. }
  790. }
  791. }
  792. if($styleids) {
  793. foreach(C::t('common_block_style')->fetch_all($styleids) as $styleid => $value) {
  794. $value['template'] = !empty($value['template']) ? (array)(dunserialize($value['template'])) : array();
  795. $value['fields'] = !empty($value['fields']) ? (array)(dunserialize($value['fields'])) : array();
  796. $_G[$pre.$styleid] = $value;
  797. if($allowmem) {
  798. memory('set', $cacheprekey.$styleid, $_G[$pre.$styleid], $cachettl);
  799. }
  800. }
  801. }
  802. return $ret ? $_G[$pre.$ret] : true;
  803. }
  804. return array();
  805. }
  806. function blockclass_cache() {
  807. global $_G;
  808. $data = $dirs = $styles = $dataconvert = array();
  809. $dir = DISCUZ_ROOT.'/source/class/block/';
  810. $dh = opendir($dir);
  811. while(($filename=readdir($dh))) {
  812. if(is_dir($dir.$filename) && substr($filename,0,1) != '.') {
  813. $dirs[$filename] = $dir.$filename.'/';
  814. }
  815. }
  816. ksort($dirs);
  817. foreach($dirs as $name=>$dir) {
  818. $blockclass = $blockconvert = array();
  819. if(file_exists($dir.'blockclass.php')) {
  820. include_once($dir.'blockclass.php');
  821. }
  822. if(empty($blockclass['name'])) {
  823. $blockclass['name'] = $name;
  824. } else {
  825. $blockclass['name'] = dhtmlspecialchars($blockclass['name']);
  826. }
  827. $blockclass['subs'] = array();
  828. $dh = opendir($dir);
  829. while(($filename = readdir($dh))) {
  830. $match = $infos = $oneinfo = $fieldsconvert = array();
  831. $scriptname = $scriptclass = '';
  832. if(preg_match('/^(block_[\w]+)\.php$/i', $filename, $match)) {
  833. $scriptclass = $match[1];
  834. $scriptname = preg_replace('/^block_/i', '', $scriptclass);
  835. include_once $dir.$filename;
  836. if(class_exists($scriptclass, false)) {
  837. $obj = new $scriptclass();
  838. if(method_exists($obj, 'name') && method_exists($obj, 'blockclass') && method_exists($obj, 'fields')
  839. && method_exists($obj, 'getsetting') && method_exists($obj, 'getdata')) {
  840. if($scriptclass == 'block_xml') {
  841. foreach($obj->blockdata as $one) {
  842. $oneinfo['name'] = dhtmlspecialchars($one['data']['name']);
  843. $oneinfo['blockclass'] = array($one['id'], $oneinfo['name']);
  844. $oneinfo['fields'] = dhtmlspecialchars($one['data']['fields']);
  845. foreach($one['data']['style'] as $value) {
  846. $arr = array(
  847. 'blockclass'=>'xml_'.$one['id'],
  848. 'name' => dhtmlspecialchars($value['name']),
  849. );
  850. block_parse_template($value['template'], $arr);
  851. $styles[$arr['hash']] = $arr;
  852. }
  853. $infos[] = $oneinfo;
  854. }
  855. } else {
  856. $oneinfo['name'] = $obj->name();
  857. $oneinfo['blockclass'] = $obj->blockclass();
  858. $oneinfo['fields'] = $obj->fields();
  859. $infos[] = $oneinfo;
  860. }
  861. }
  862. if(method_exists($obj, 'fieldsconvert')) {
  863. $fieldsconvert = $obj->fieldsconvert();
  864. }
  865. }
  866. }
  867. foreach($infos as $info) {
  868. if($info['name'] && is_array($info['blockclass']) && $info['blockclass'][0] && $info['blockclass'][1]) {
  869. list($key, $title) = $info['blockclass'];
  870. $key = $name.'_'.$key;
  871. if(!isset($blockclass['subs'][$key])) {
  872. $blockclass['subs'][$key] = array(
  873. 'name' => $title,
  874. 'fields' => $info['fields'],
  875. 'script' => array()
  876. );
  877. }
  878. $blockclass['subs'][$key]['script'][$scriptname] = $info['name'];
  879. if(!isset($blockconvert[$key]) && !empty($fieldsconvert)) {
  880. $blockconvert[$key] = $fieldsconvert;
  881. }
  882. }
  883. }
  884. }
  885. if($blockclass['subs']) {
  886. $data[$name] = $blockclass;
  887. $blockstyle = array();
  888. if(file_exists($dir.'blockstyle.php')) {
  889. include_once($dir.'blockstyle.php');
  890. }
  891. if($blockstyle) {
  892. foreach($blockstyle as $value) {
  893. $arr = array(
  894. 'blockclass'=>$name.'_'.$value['blockclass'],
  895. 'name' => $value['name']
  896. );
  897. block_parse_template($value['template'], $arr);
  898. $styles[$arr['hash']] = $arr;
  899. }
  900. }
  901. }
  902. if(!empty($blockconvert)) {
  903. $dataconvert[$name] = $blockconvert;
  904. }
  905. }
  906. if($styles) {
  907. $hashes = array_keys($styles);
  908. foreach(C::t('common_block_style')->fetch_all_by_hash($hashes) as $value) {
  909. unset($styles[$value['hash']]);
  910. }
  911. if($styles) {
  912. C::t('common_block_style')->insert_batch($styles);
  913. }
  914. }
  915. savecache('blockclass', $data);
  916. savecache('blockconvert', $dataconvert);
  917. }
  918. function block_parse_template($str_template, &$arr) {
  919. $arr['makethumb'] = strexists($str_template, '{picwidth}') ? 1 : 0;
  920. $arr['getpic'] = strexists($str_template, '{pic}') ? 1 : 0;
  921. $arr['getsummary'] = strexists($str_template, '{summary}') ? 1 : 0;
  922. $arr['settarget'] = strexists($str_template, '{target}') ? 1 : 0;
  923. $arr['moreurl'] = strexists($str_template, '{moreurl}') ? 1 : 0;
  924. $fields = block_parse_fields($str_template);
  925. $arr['fields'] = serialize($fields);
  926. $template = array();
  927. $template['raw'] = $str_template;
  928. $template['header'] = $template['footer'] = '';
  929. $template['loop'] = $template['loopplus'] = $template['order'] = $template['orderplus'] = $template['index'] = $template['indexplus'] = array();
  930. $match = array();
  931. if(preg_match('/\[loop\](.*?)\[\/loop]/is', $str_template, $match)) {
  932. $template['loop'] = trim($match[1]);
  933. }
  934. $match = array();
  935. if(preg_match_all('/\[(loop\d)\](.*?)\[\/\\1]/is', $str_template, $match)) {
  936. foreach($match[1] as $key=>$value) {
  937. $content = trim($match[2][$key]);
  938. $k = intval(str_replace('loop', '', $value));
  939. $template['loopplus'][$k] = $content;
  940. }
  941. }
  942. $match = array();
  943. if(preg_match_all('/\[order=(\d+|odd|even)\](.*?)\[\/order]/is', $str_template, $match)) {
  944. foreach($match[1] as $key => $order) {
  945. $template['order'][$order] = trim($match[2][$key]);
  946. }
  947. }
  948. $match = array();
  949. if(preg_match_all('/\[(order\d+)=(\d+|odd|even)\](.*?)\[\/\\1]/is', $str_template, $match)) {
  950. foreach($match[1] as $key=>$value) {
  951. $content = trim($match[3][$key]);
  952. $order = $match[2][$key];
  953. $k = intval(str_replace('order', '', $value));
  954. $template['orderplus'][$k][$order] = $content;
  955. }
  956. }
  957. $match = array();
  958. if(preg_match_all('/\[index=(\d+)\](.*?)\[\/index]/is', $str_template, $match)) {
  959. foreach($match[1] as $key=>$order) {
  960. $template['index'][$order] = trim($match[2][$key]);
  961. }
  962. }
  963. $match = array();
  964. if(preg_match_all('/\[(index\d+)=(\d+)\](.*?)\[\/\\1]/is', $str_template, $match)) {
  965. foreach($match[1] as $key=>$value) {
  966. $content = trim($match[3][$key]);
  967. $order = intval($match[2][$key]);
  968. $k = intval(str_replace('index', '', $value));
  969. $template['indexplus'][$k][$order] = $content;
  970. }
  971. }
  972. $arr['template'] = serialize($template);
  973. $arr['hash'] = substr(md5($arr['blockclass'].'|'.$arr['template']), 8, 8);
  974. }
  975. function block_parse_fields($template) {
  976. $fields = array();
  977. if(preg_match_all('/\{(\w+)\}/', $template, $matches)) {
  978. foreach($matches[1] as $fieldname) {
  979. $fields[] = $fieldname;
  980. }
  981. $fields = array_unique($fields);
  982. $fields = array_diff($fields, array('picwidth', 'picheight', 'target', ''));
  983. $fields = array_values($fields);
  984. }
  985. return $fields;
  986. }
  987. function block_build_template($template) {
  988. if(! is_array($template)) {
  989. return $template;
  990. }
  991. if(!empty($template['raw'])) {
  992. return $template['raw'];
  993. }
  994. $str_template = $template['header'];
  995. if($template['loop']) {
  996. $str_template .= "\n[loop]\n{$template['loop']}\n[/loop]";
  997. }
  998. if(!empty($template['order']) && is_array($template['order'])) {
  999. foreach($template['order'] as $key=>$value) {
  1000. $str_template .= "\n[order={$key}]\n{$value}\n[/order]";
  1001. }
  1002. }
  1003. $str_template .= $template['footer'];
  1004. return $str_template;
  1005. }
  1006. function block_isrecommendable($block) {
  1007. return !empty($block) && in_array($block['blockclass'], array('forum_thread', 'group_thread', 'portal_article', 'space_pic', 'space_blog')) ? true : false;
  1008. }
  1009. function block_delete_pic($bid, $itemid = array()) {
  1010. global $_G;
  1011. if(!empty($bid)) {
  1012. $picids = array();
  1013. foreach(C::t('common_block_pic')->fetch_all_by_bid_itemid($bid, $itemid) as $value) {
  1014. $picids[$value['picid']] = $value['picid'];
  1015. if($value['picflag']) {
  1016. ftpcmd('delete', $value['pic']);
  1017. } else {
  1018. @unlink($_G['setting']['attachdir'].'/'.$value['pic']);
  1019. }
  1020. }
  1021. if(!empty($picids)) {
  1022. C::t('common_block_pic')->delete($picids);
  1023. }
  1024. }
  1025. }
  1026. function update_template_block($targettplname, $tpldirectory, $blocks) {
  1027. if(!empty($targettplname)) {
  1028. if(empty($blocks)) {
  1029. C::t('common_template_block')->delete_by_targettplname($targettplname, $tpldirectory);
  1030. } else {
  1031. $oldbids = array();
  1032. $oldbids = array_keys(C::t('common_template_block')->fetch_all_by_targettplname($targettplname, $tpldirectory));
  1033. $newaddbids = array_diff($blocks, $oldbids);
  1034. C::t('common_template_block')->delete_by_targettplname($targettplname, $tpldirectory);
  1035. if($tpldirectory === './template/default') {
  1036. C::t('common_template_block')->delete_by_targettplname($targettplname, '');
  1037. }
  1038. $blocks = array_unique($blocks);
  1039. C::t('common_template_block')->insert_batch($targettplname, $tpldirectory, $blocks);
  1040. if(!empty($newaddbids)) {
  1041. require_once libfile('class/blockpermission');
  1042. $tplpermission = & template_permission::instance();
  1043. $tplpermission->add_blocks($targettplname, $newaddbids);
  1044. }
  1045. }
  1046. }
  1047. }
  1048. ?>