function_threadsort.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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_threadsort.php 36284 2016-12-12 00:47:50Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. function gettypetemplate($option, $optionvalue, $optionid) {
  12. global $_G;
  13. if(in_array($option['type'], array('number', 'text', 'email', 'calendar', 'image', 'url', 'range', 'upload', 'range'))) {
  14. if($option['type'] == 'calendar') {
  15. $showoption[$option['identifier']]['value'] = '<script type="text/javascript" src="'.$_G['setting']['jspath'].'calendar.js?'.VERHASH.'"></script><input type="text" name="typeoption['.$option['identifier'].']" tabindex="1" id="typeoption_'.$option['identifier'].'" style="width:'.$option['inputsize'].'px;" onchange="checkoption(\''.$option['identifier'].'\', \''.$option['required'].'\', \''.$option['type'].'\')" value="'.$optionvalue['value'].'" onclick="showcalendar(event, this, false)" '.$optionvalue['unchangeable'].' class="px"/>';
  16. } elseif($option['type'] == 'image') {
  17. $showoption[$option['identifier']]['value'] = '<button type="button" class="pn" onclick="uploadWindow(function (aid, url){updatesortattach(aid, url, \''.$_G['setting']['attachurl'].'forum\', \''.$option['identifier'].'\')})"><span>'.($optionvalue['value'] ? lang('forum/misc', 'sort_update') : lang('forum/misc', 'sort_upload')).'</span></button>
  18. <input type="hidden" name="typeoption['.$option['identifier'].'][aid]" id="sortaid_'.$option['identifier'].'" value="'.$optionvalue['value']['aid'].'" tabindex="1" />'.
  19. ($optionvalue['value']['aid'] ? '<input type="hidden" name="oldsortaid['.$option['identifier'].']" value="'.$optionvalue['value']['aid'].'" tabindex="1" />' : '').
  20. '<input type="hidden" name="typeoption['.$option['identifier'].'][url]" id="sortattachurl_'.$option['identifier'].'" '.($optionvalue['value']['url'] ? 'value="'.$optionvalue['value']['url'].'"' : '').'tabindex="1" />
  21. <div id="sortattach_image_'.$option['identifier'].'" class="ptn">';
  22. if($optionvalue['value']['url']) {
  23. $showoption[$option['identifier']]['value'] .= '<a href="'.$optionvalue['value']['url'].'" target="_blank"><img class="spimg" src="'.$optionvalue['value']['url'].'" alt="" /></a>';
  24. }
  25. $showoption[$option['identifier']]['value'] .= '</div>';
  26. } else {
  27. $showoption[$option['identifier']]['value'] = '<input type="text" name="typeoption['.$option['identifier'].']" id="typeoption_'.$option['identifier'].'" class="px" tabindex="1" style="width:'.$option['inputsize'].'px;" onBlur="checkoption(\''.$option['identifier'].'\', \''.$option['required'].'\', \''.$option['type'].'\', \''.intval($option['maxnum']).'\', \''.intval($option['minnum']).'\', \''.intval($option['maxlength']).'\')" value="'.($optionvalue['value'] ? $optionvalue['value'] : $option['defaultvalue']).'" '.$optionvalue['unchangeable'].' />';
  28. }
  29. } elseif(in_array($option['type'], array('radio', 'checkbox', 'select'))) {
  30. if($option['type'] == 'select') {
  31. if(empty($optionvalue['value'])) {
  32. $showoption[$option['identifier']]['value'] = '<span id="select_'.$option['identifier'].'"><select tabindex="1" onchange="changeselectthreadsort(this.value, \''.$optionid.'\');checkoption(\''.$option['identifier'].'\', \''.$option['required'].'\', \''.$option['type'].'\')" '.$optionvalue['unchangeable'].' class="ps">';
  33. $showoption[$option['identifier']]['value'] .= '<option value="0">'.lang('forum/template', 'please_select').'</option>';
  34. foreach($option['choices'] as $id => $value) {
  35. if(!$value['foptionid']) {
  36. $showoption[$option['identifier']]['value'] .= '<option value="'.$id.'">'.$value['content'].' '.(($value['level'] == 1) ? '' : '>').'</option>';
  37. }
  38. }
  39. $showoption[$option['identifier']]['value'] .= '</select></span>';
  40. } else {
  41. foreach($optionvalue['value'] as $selectedkey => $selectedvalue) {
  42. $showoption[$option['identifier']]['value'] = '<span id="select_'.$option['identifier'].'"><script type="text/javascript">changeselectthreadsort(\''.$selectedkey.'\', '.$optionid.');</script></span>';
  43. }
  44. }
  45. } elseif($option['type'] == 'radio') {
  46. foreach($option['choices'] as $id => $value) {
  47. $showoption[$option['identifier']]['value'] .= '<span class="fb"><input type="radio" name="typeoption['.$option['identifier'].']" id="typeoption_'.$option['identifier'].'" class="pr" value="'.$id.'" tabindex="1" onclick="checkoption(\''.$option['identifier'].'\', \''.$option['required'].'\', \''.$option['type'].'\')" '.$optionvalue['value'][$id].' '.$optionvalue['unchangeable'].' />'.$value.'</span>';
  48. }
  49. } elseif($option['type'] == 'checkbox') {
  50. foreach($option['choices'] as $id => $value) {
  51. $showoption[$option['identifier']]['value'] .= '<span class="fb"><input type="checkbox" name="typeoption['.$option['identifier'].'][]" id="typeoption_'.$option['identifier'].'" class="pc" tabindex="1" value="'.$id.'" onclick="checkoption(\''.$option['identifier'].'\', \''.$option['required'].'\', \''.$option['type'].'\')" '.$optionvalue['value'][$id][$id].' '.$optionvalue['unchangeable'].' />'.$value.'</span>';
  52. }
  53. }
  54. } elseif(in_array($option['type'], array('textarea'))) {
  55. $showoption[$option['identifier']]['value'] = '<span><textarea name="typeoption['.$option['identifier'].']" id="typeoption_'.$option['identifier'].'" class="pt" tabindex="1" rows="'.$option['rowsize'].'" cols="'.$option['colsize'].'" onBlur="checkoption(\''.$option['identifier'].'\', \''.$option['required'].'\', \''.$option['type'].'\', 0, 0'.($option['maxlength'] ? ', \'$option[maxlength]\'' : '').'" '.$optionvalue['unchangeable'].'>'.$optionvalue['value'].'</textarea><span>';
  56. }
  57. return $showoption;
  58. }
  59. function quicksearch($sortoptionarray) {
  60. global $_G;
  61. $quicksearch = array();
  62. if($sortoptionarray) {
  63. foreach($sortoptionarray as $optionid => $option) {
  64. if($option['search']) {
  65. $quicksearch[$optionid]['title'] = $option['title'];
  66. $quicksearch[$optionid]['identifier'] = $option['identifier'];
  67. $quicksearch[$optionid]['unit'] = $option['unit'];
  68. $quicksearch[$optionid]['type'] = $option['type'];
  69. $quicksearch[$optionid]['search'] = $option['search'];
  70. if(in_array($option['type'], array('radio', 'select', 'checkbox'))) {
  71. $quicksearch[$optionid]['choices'] = $option['choices'];
  72. } elseif(!empty($option['searchtxt'])) {
  73. $choices = array();
  74. $prevs = 'd';
  75. foreach($option['searchtxt'] as $choice) {
  76. $value = "$prevs|$choice";
  77. if($choice) {
  78. $quicksearch[$optionid]['choices'][$value] = $prevs == 'd' ? lang('forum/misc', 'lower').$choice.$option['unit'] : $prevs.'-'.$choice.$option['unit'];
  79. $prevs = $choice;
  80. }
  81. $max = $choice;
  82. }
  83. $value = "u|$choice";
  84. $quicksearch[$optionid]['choices'][$value] .= lang('forum/misc', 'higher').$max.$option['unit'];
  85. }
  86. }
  87. }
  88. }
  89. return $quicksearch;
  90. }
  91. function sortsearch($sortid, $sortoptionarray, $searchoption = array(), $selecturladd = array(), $sortfid = 0) {
  92. global $_G;
  93. $sortid = intval($sortid);
  94. $selectsql = '';
  95. $optionide = $searchsorttids = array();
  96. if($selecturladd) {
  97. foreach($sortoptionarray[$sortid] as $optionid => $option) {
  98. if(in_array($option['type'], array('checkbox', 'radio', 'select', 'range'))) {
  99. $optionide[$option['identifier']] = $option['type'];
  100. }
  101. }
  102. foreach($selecturladd as $fieldname => $value) {
  103. if($optionide[$fieldname] && $value != 'all') {
  104. if($optionide[$fieldname] == 'range') {
  105. $value = explode('|', $value);
  106. if($value[0] == 'd') {
  107. $sql = "$fieldname<".intval($value[1]);
  108. } elseif($value[0] == 'u') {
  109. $sql = "$fieldname>".intval($value[1]);
  110. } else {
  111. $sql = "($fieldname BETWEEN ".intval($value[0])." AND ".intval($value[1]).")";
  112. }
  113. } elseif($optionide[$fieldname] == 'checkbox') {
  114. $sql = '('.DB::field($fieldname, $value).
  115. ' OR '.DB::field($fieldname, "$value\t%", 'like').
  116. ' OR '.DB::field($fieldname, "%\t$value", 'like').
  117. ' OR '.DB::field($fieldname, "%\t$value\t%", 'like').')';
  118. } elseif($optionide[$fieldname] == 'select') {
  119. $subvalues = $currentchoices = array();
  120. if(!empty($_G['forum_optionlist'])) {
  121. foreach($_G['forum_optionlist'] as $subkey => $subvalue) {
  122. if($subvalue['identifier'] == $fieldname) {
  123. $currentchoices = $subvalue['choices'];
  124. break;
  125. }
  126. }
  127. }
  128. if(!empty($currentchoices)) {
  129. foreach($currentchoices as $subkey => $subvalue) {
  130. if(preg_match('/^'.$value.'\.'.'/i', $subkey) || preg_match('/^'.$value.'$'.'/i', $subkey)) {
  131. $subvalues[] = $subkey;
  132. }
  133. }
  134. }
  135. $sql = DB::field($fieldname, $subvalues);
  136. } else {
  137. $sql = DB::field($fieldname, $value);
  138. }
  139. $selectsql .= "AND $sql ";
  140. }
  141. }
  142. }
  143. if(!empty($searchoption) && is_array($searchoption)) {
  144. foreach($searchoption as $optionid => $option) {
  145. $fieldname = $sortoptionarray[$sortid][$optionid]['identifier'] ? $sortoptionarray[$sortid][$optionid]['identifier'] : 1;
  146. if($option['value']) {
  147. if(in_array($option['type'], array('number', 'radio'))) {
  148. $option['value'] = intval($option['value']);
  149. $exp = '=';
  150. if($option['condition']) {
  151. $exp = $option['condition'] == 1 ? '>' : '<';
  152. }
  153. $sql = DB::field($fieldname, $option['value'], $exp);
  154. } elseif($option['type'] == 'select') {
  155. $subvalues = $currentchoices = array();
  156. if(!empty($_G['forum_optionlist'])) {
  157. foreach($_G['forum_optionlist'] as $subkey => $subvalue) {
  158. if($subvalue['identifier'] == $fieldname) {
  159. $currentchoices = $subvalue['choices'];
  160. break;
  161. }
  162. }
  163. }
  164. if(!empty($currentchoices)) {
  165. foreach($currentchoices as $subkey => $subvalue) {
  166. if(preg_match('/^'.$option['value'].'/i', $subkey)) {
  167. $subvalues[] = $subkey;
  168. }
  169. }
  170. }
  171. $sql = DB::field($fieldname, $subvalues);
  172. } elseif($option['type'] == 'checkbox') {
  173. $sql = DB::field($fieldname, '%'.implode('%', $option['value']).'%', 'like');
  174. } elseif($option['type'] == 'range') {
  175. $value = explode('|', $option['value']);
  176. if($value[0] == 'd') {
  177. $sql = "$fieldname<".intval($value[1]);
  178. } elseif($value[0] == 'u') {
  179. $sql = "$fieldname>".intval($value[1]);
  180. } else {
  181. $sql = $value[0] || $value[1] ? "($fieldname BETWEEN ".intval($value[0])." AND ".intval($value[1]).")" : '';
  182. }
  183. } else {
  184. $sql = DB::field($fieldname, '%'.$option['value'].'%', 'like');
  185. }
  186. $selectsql .= "AND $sql ";
  187. }
  188. }
  189. }
  190. $searchsorttids = C::t('forum_optionvalue')->fetch_all_tid($sortid, "WHERE 1 $selectsql ".($sortfid ? "AND fid='$sortfid'" : ''));
  191. return $searchsorttids;
  192. }
  193. function showsorttemplate($sortid, $fid, $sortoptionarray, $templatearray, $threadlist, $threadids = array(), $sortmode = false) {
  194. global $_G;
  195. $searchtitle = $searchvalue = $searchunit = $stemplate = $searchtids = $sortlistarray = $skipaids = $sortdata = array();
  196. $sortthreadlist = array();
  197. foreach(C::t('forum_typeoptionvar')->fetch_all_by_search($sortid, $fid, $threadids) as $sortthread) {
  198. $optionid = $sortthread['optionid'];
  199. $sortid = $sortthread['sortid'];
  200. $tid = $sortthread['tid'];
  201. $arrayoption = $sortoptionarray[$sortid][$optionid];
  202. if($sortoptionarray[$sortid][$optionid]['subjectshow']) {
  203. $_G['optionvaluelist'][$sortid][$tid][$arrayoption['identifier']]['title'] = $arrayoption['title'];
  204. $_G['optionvaluelist'][$sortid][$tid][$arrayoption['identifier']]['unit'] = $arrayoption['unit'];
  205. if(in_array($arrayoption['type'], array('radio', 'checkbox', 'select'))) {
  206. if($arrayoption['type'] == 'checkbox') {
  207. foreach(explode("\t", $sortthread['value']) as $choiceid) {
  208. $sortthreadlist[$tid][$arrayoption['title']] .= $arrayoption['choices'][$choiceid].'&nbsp;';
  209. $_G['optionvaluelist'][$sortid][$tid][$arrayoption['identifier']]['value'] .= $arrayoption['choices'][$choiceid].'&nbsp;';
  210. }
  211. } elseif($arrayoption['type'] == 'select') {
  212. $sortthreadlist[$tid][$arrayoption['title']] = $_G['optionvaluelist'][$sortid][$tid][$arrayoption['identifier']]['value'] = $arrayoption['choices'][$sortthread['value']]['content'];
  213. } else {
  214. $sortthreadlist[$tid][$arrayoption['title']] = $_G['optionvaluelist'][$sortid][$tid][$arrayoption['identifier']]['value'] = $arrayoption['choices'][$sortthread['value']];
  215. }
  216. } elseif($arrayoption['type'] == 'image') {
  217. $imgoptiondata = dunserialize($sortthread['value']);
  218. if(empty($templatearray[$sortid])) {
  219. $maxwidth = $arrayoption['maxwidth'] ? 'width="'.$arrayoption['maxwidth'].'"' : '';
  220. $maxheight = $arrayoption['maxheight'] ? 'height="'.$arrayoption['maxheight'].'"' : '';
  221. $sortthreadlist[$tid][$arrayoption['title']] = $_G['optionvaluelist'][$sortid][$tid][$arrayoption['identifier']]['value'] = $imgoptiondata['url'] ? "<img src=\"$imgoptiondata[url]\" onload=\"thumbImg(this)\" $maxwidth $maxheight border=\"0\">" : '';
  222. } else {
  223. $sortthread['value'] = '';
  224. if($imgoptiondata['aid']) {
  225. $sortthread['value'] = getforumimg($imgoptiondata['aid'], 0, 120, 120);
  226. } elseif($imgoptiondata['url']) {
  227. $sortthread['value'] = $imgoptiondata['url'];
  228. }
  229. $sortthreadlist[$tid][$arrayoption['title']] = $_G['optionvaluelist'][$sortid][$tid][$arrayoption['identifier']]['value'] = $sortthread['value'] ? $sortthread['value'] : './static/image/common/nophotosmall.gif';
  230. }
  231. } else {
  232. $sortthreadlist[$tid][$arrayoption['title']] = $_G['optionvaluelist'][$sortid][$tid][$arrayoption['identifier']]['value'] = $sortthread['value'] ? $sortthread['value'] : $arrayoption['defaultvalue'];
  233. }
  234. $sortthreadlist[$tid]['sortid'] = $sortid;
  235. $sortthreadlist[$tid]['expiration'] = $sortthread['expiration'] && $sortthread['expiration'] <= TIMESTAMP ? 1 : 0;
  236. }
  237. }
  238. if($templatearray && $sortthreadlist) {
  239. foreach($threadlist as $thread) {
  240. $thread['digest'] = $thread['digest'] ? '&nbsp;<img src="'.$_G['style']['imgdir'].'/digest_'.$thread['digest'].'.gif" class="vm" alt="" title="" />' : '';
  241. if($thread['highlight']) {
  242. $thread['subject'] = '<span '.$thread['highlight'].'>'.$thread['subject'].'</span>';
  243. }
  244. if($thread['digest']) {
  245. $thread['subject'] .= ' '.$thread['digest'];
  246. }
  247. $sortdata[$thread['tid']]['subject'] = !$sortmode ? '<a href="forum.php?mod=viewthread&tid='.$thread['tid'].'">'.$thread['subject'].'</a>' : $thread['subject'];
  248. $sortdata[$thread['tid']]['author'] = '<a href="home.php?mod=space&uid='.$thread['authorid'].'" target="_blank">'.$thread['author'].'</a>';
  249. }
  250. foreach($sortoptionarray as $sortid => $optionarray) {
  251. foreach($optionarray as $option) {
  252. if($option['subjectshow']) {
  253. $searchtitle[$sortid][] = '/{('.$option['identifier'].')}/';
  254. $searchvalue[$sortid][] = '/\[('.$option['identifier'].')value\]/';
  255. $searchvalue[$sortid][] = '/{('.$option['identifier'].')_value}/';
  256. $searchunit[$sortid][] = '/\[('.$option['identifier'].')unit\]/';
  257. $searchunit[$sortid][] = '/{('.$option['identifier'].')_unit}/';
  258. }
  259. }
  260. }
  261. foreach($sortthreadlist as $tid => $option) {
  262. $sortid = $option['sortid'];
  263. $sortexpiration[$sortid][$tid] = $option['expiration'];
  264. $stemplate[$sortid][$tid] = preg_replace(
  265. array("/\{sortname\}/i", "/\{author\}/i", "/\{subject\}/i", "/\[url\](.+?)\[\/url\]/i"),
  266. array(
  267. '<a href="forum.php?mod=forumdisplay&fid='.$sortthreadlist[$tid]['fid'].'&filter=sortid&sortid='.$sortid.'">'.$_G['forum']['threadsorts']['types'][$sortid].'</a>',
  268. $sortdata[$tid]['author'],
  269. $sortdata[$tid]['subject'],
  270. "<a href=\"forum.php?mod=viewthread&tid=$tid\">\\1</a>"
  271. ), stripslashes($templatearray[$sortid]));
  272. $stemplate[$sortid][$tid] = preg_replace_callback($searchtitle[$sortid], create_function('$matches', 'return showlistoption($matches[1], \'title\', '.intval($tid).', '.intval($sortid).');'), $stemplate[$sortid][$tid]);
  273. $stemplate[$sortid][$tid] = preg_replace_callback($searchvalue[$sortid], create_function('$matches', 'return showlistoption($matches[1], \'value\', '.intval($tid).', '.intval($sortid).');'), $stemplate[$sortid][$tid]);
  274. $stemplate[$sortid][$tid] = preg_replace_callback($searchunit[$sortid], create_function('$matches', 'return showlistoption($matches[1], \'unit\', '.intval($tid).', '.intval($sortid).');'), $stemplate[$sortid][$tid]);
  275. }
  276. }
  277. $sortlistarray['template'] = $stemplate;
  278. $sortlistarray['expiration'] = $sortexpiration;
  279. return $sortlistarray;
  280. }
  281. function showsortmodetemplate($sortid, $fid, $sortoptionarray, $templatearray, $threadlist, $threadids = array(), &$verify = array()) {
  282. global $_G;
  283. $sorttemplate = $replaces = array();
  284. $sorttemplate['footer'] = $sorttemplate['body'] = $sorttemplate['header'] = '';
  285. if(strexists($templatearray[$sortid], '[loop]') && strexists($templatearray[$sortid], '[/loop]')) {
  286. preg_match('/^(.+?)\[loop\](.+?)\[\/loop\](.+?)$/s', $templatearray[$sortid], $r);
  287. $sorttemplate['header'] = stripslashes($r[1]);
  288. $templatearray[$sortid] = stripslashes($r[2]);
  289. $sorttemplate['footer'] = stripslashes($r[3]);
  290. }
  291. $rewritespace = is_array($_G['setting']['rewritestatus']) && in_array('home_space', $_G['setting']['rewritestatus']);
  292. $rewriteviewthread = is_array($_G['setting']['rewritestatus']) && in_array('forum_viewthread', $_G['setting']['rewritestatus']);
  293. $sortlistarray = showsorttemplate($sortid, $fid, $sortoptionarray, $templatearray, $threadlist, $threadids, true);
  294. foreach($threadlist as $thread) {
  295. foreach($thread as $k => $v) {
  296. $replaces['{'.$k.'}'] = $v;
  297. }
  298. $body = $sortlistarray['template'][$sortid][$thread['tid']];
  299. $replaces['{author_url}'] = $rewritespace ? rewriteoutput('home_space', 1, '', $thread['authorid']) : 'home.php?mod=space&amp;uid='.$thread['authorid'];
  300. $replaces['{lastposter_url}'] = $rewritespace ? rewriteoutput('home_space', 1, '', '', $thread['lastposter']) : 'home.php?mod=space&amp;username='.$thread['lastposterenc'];
  301. $replaces['{subject_url}'] = $rewriteviewthread ? rewriteoutput('forum_viewthread', 1, '', $thread['tid']) : 'forum.php?mod=viewthread&amp;tid='.$thread['tid'];
  302. $replaces['{lastpost_url}'] = 'forum.php?mod=redirect&tid='.$thread['tid'].'&goto=lastpost#lastpost';
  303. $replaces['{lastpost_url}'] = 'forum.php?mod=redirect&tid='.$thread['tid'].'&goto=lastpost#lastpost';
  304. $replaces['{avatar_small}'] = avatar($thread['authorid'], 'small', true);
  305. $replaces['{typename_url}'] = 'forum.php?mod=forumdisplay&fid='.$fid.'&filter=typeid&typeid='.$thread['tid'];
  306. $replaces['{attachment}'] = ($thread['attachment'] == 2 ? '<img src="'.STATICURL.'image/filetype/image_s.gif" align="absmiddle" />' :
  307. ($thread['attachment'] == 1 ? '<img src="'.STATICURL.'image/filetype/common.gif" align="absmiddle" />' : ''));
  308. $replaces['{author_verify}'] = $verify[$thread['authorid']] ? $verify[$thread['authorid']] : '';
  309. if($_G['forum']['ismoderator']) {
  310. if($thread['fid'] == $fid && $thread['displayorder'] <= 3 || $_G['adminid'] == 1) {
  311. $replaces['{modcheck}'] = '<input onclick="tmodclick(this)" type="checkbox" name="moderate[]" value="'.$thread['tid'].'" />';
  312. } else {
  313. $replaces['{modcheck}'] = '<input type="checkbox" disabled="disabled" />';
  314. }
  315. } else {
  316. $replaces['{modcheck}'] = '';
  317. }
  318. $body = str_replace(array_keys($replaces), $replaces, $body);
  319. $sorttemplate['body'] .= $body;
  320. }
  321. return $sorttemplate;
  322. }
  323. function showlistoption($var, $type, $tid, $sortid) {
  324. global $_G;
  325. if($_G['optionvaluelist'][$sortid][$tid][$var][$type]) {
  326. return $_G['optionvaluelist'][$sortid][$tid][$var][$type];
  327. } else {
  328. return '';
  329. }
  330. }
  331. function threadsortshow($sortid, $tid) {
  332. global $_G;
  333. loadcache(array('threadsort_option_'.$sortid, 'threadsort_template_'.$sortid));
  334. $sortoptionarray = $_G['cache']['threadsort_option_'.$sortid];
  335. $templatearray = $_G['cache']['threadsort_template_'.$sortid];
  336. $threadsortshow = $optiondata = $searchtitle = $searchvalue = $searchunit = $memberinfofield = $_G['forum_option'] = array();
  337. if($sortoptionarray) {
  338. foreach(C::t('forum_typeoptionvar')->fetch_all_by_tid_optionid($tid) as $option) {
  339. $optiondata[$option['optionid']]['value'] = $option['value'];
  340. $optiondata[$option['optionid']]['expiration'] = $option['expiration'] && $option['expiration'] <= TIMESTAMP ? 1 : 0;
  341. $sortdataexpiration = $option['expiration'];
  342. }
  343. foreach($sortoptionarray as $optionid => $option) {
  344. $_G['forum_option'][$option['identifier']]['title'] = $option['title'];
  345. $_G['forum_option'][$option['identifier']]['unit'] = $option['unit'];
  346. $_G['forum_option'][$option['identifier']]['type'] = $option['type'];
  347. if(($option['expiration'] && !$optiondata[$optionid]['expiration']) || empty($option['expiration'])) {
  348. if(!protectguard($option['protect'])) {
  349. if($option['type'] == 'checkbox') {
  350. $_G['forum_option'][$option['identifier']]['value'] = '';
  351. foreach(explode("\t", $optiondata[$optionid]['value']) as $choiceid) {
  352. $_G['forum_option'][$option['identifier']]['value'] .= $option['choices'][$choiceid].'&nbsp;';
  353. }
  354. } elseif($option['type'] == 'radio') {
  355. $_G['forum_option'][$option['identifier']]['value'] = $option['choices'][$optiondata[$optionid]['value']];
  356. } elseif($option['type'] == 'select') {
  357. $tmpchoiceid = $tmpidentifiervalue = array();
  358. foreach(explode('.', $optiondata[$optionid]['value']) as $choiceid) {
  359. $tmpchoiceid[] = $choiceid;
  360. $tmpidentifiervalue[] = $option['choices'][implode('.', $tmpchoiceid)];
  361. }
  362. $_G['forum_option'][$option['identifier']]['value'] = implode(' &raquo; ', $tmpidentifiervalue);
  363. unset($tmpchoiceid, $tmpidentifiervalue);
  364. } elseif($option['type'] == 'image') {
  365. $imgoptiondata = dunserialize($optiondata[$optionid]['value']);
  366. $threadsortshow['sortaids'][] = $imgoptiondata['aid'];
  367. if(empty($templatearray['viewthread'])) {
  368. $maxwidth = $option['maxwidth'] ? 'width="'.$option['maxwidth'].'"' : '';
  369. $maxheight = $option['maxheight'] ? 'height="'.$option['maxheight'].'"' : '';
  370. if(!defined('IN_MOBILE')) {
  371. $_G['forum_option'][$option['identifier']]['value'] = $imgoptiondata['url'] ? "<img src=\"".$imgoptiondata['url']."\" onload=\"thumbImg(this)\" $maxwidth $maxheight border=\"0\">" : '';
  372. } else {
  373. $_G['forum_option'][$option['identifier']]['value'] = $imgoptiondata['url'] ? "<a href=\"".$imgoptiondata['url']."\" target=\"_blank\">".lang('forum/misc', 'click_view')."</a>" : '';
  374. }
  375. } else {
  376. $_G['forum_option'][$option['identifier']]['value'] = $imgoptiondata['url'] ? $imgoptiondata['url'] : './static/image/common/nophoto.gif';
  377. }
  378. } elseif($option['type'] == 'url') {
  379. $_G['forum_option'][$option['identifier']]['value'] = $optiondata[$optionid]['value'] ? "<a href=\"".$optiondata[$optionid]['value']."\" target=\"_blank\">".$optiondata[$optionid]['value']."</a>" : '';
  380. } elseif($option['type'] == 'number') {
  381. $_G['forum_option'][$option['identifier']]['value'] = $optiondata[$optionid]['value'];
  382. } else {
  383. if($option['protect']['status'] && $optiondata[$optionid]['value']) {
  384. $optiondata[$optionid]['value'] = $option['protect']['mode'] == 1 ? '<image src="'.stringtopic($optiondata[$optionid]['value']).'">' : (!defined('IN_MOBILE') ? '<span id="sortmessage_'.$option['identifier'].'"><a href="###" onclick="ajaxget(\'forum.php?mod=misc&action=protectsort&tid='.$tid.'&optionid='.$optionid.'\', \'sortmessage_'.$option['identifier'].'\');return false;">'.lang('forum/misc', 'click_view').'</a></span>' : $optiondata[$optionid]['value']);
  385. $_G['forum_option'][$option['identifier']]['value'] = $optiondata[$optionid]['value'] ? $optiondata[$optionid]['value'] : $option['defaultvalue'];
  386. } elseif($option['type'] == 'textarea') {
  387. $_G['forum_option'][$option['identifier']]['value'] = $optiondata[$optionid]['value'] != '' ? nl2br($optiondata[$optionid]['value']) : '';
  388. } else {
  389. $_G['forum_option'][$option['identifier']]['value'] = $optiondata[$optionid]['value'] != '' ? $optiondata[$optionid]['value'] : $option['defaultvalue'];
  390. }
  391. }
  392. } else {
  393. if(empty($option['permprompt'])) {
  394. $_G['forum_option'][$option['identifier']]['value'] = lang('forum/misc', 'view_noperm');
  395. } else {
  396. $_G['forum_option'][$option['identifier']]['value'] = $option['permprompt'];
  397. }
  398. }
  399. } else {
  400. $_G['forum_option'][$option['identifier']]['value'] = lang('forum/misc', 'has_expired');
  401. }
  402. }
  403. $typetemplate = '';
  404. if($templatearray['viewthread']) {
  405. foreach($sortoptionarray as $option) {
  406. $searchtitle[] = '/{('.$option['identifier'].')}/';
  407. $searchvalue[] = '/\[('.$option['identifier'].')value\]/';
  408. $searchvalue[] = '/{('.$option['identifier'].')_value}/';
  409. $searchunit[] = '/\[('.$option['identifier'].')unit\]/';
  410. $searchunit[] = '/{('.$option['identifier'].')_unit}/';
  411. }
  412. $threadexpiration = $sortdataexpiration ? dgmdate($sortdataexpiration) : lang('forum/misc', 'never_expired');
  413. $typetemplate = preg_replace(array("/\{expiration\}/i"), array($threadexpiration), stripslashes($templatearray['viewthread']));
  414. $typetemplate = preg_replace_callback($searchtitle, "threadsortshow_callback_showoption_title1", $typetemplate);
  415. $typetemplate = preg_replace_callback($searchvalue, "threadsortshow_callback_showoption_value1", $typetemplate);
  416. $typetemplate = preg_replace_callback($searchunit, "threadsortshow_callback_showoption_unit1", $typetemplate);
  417. }
  418. }
  419. $threadsortshow['optionlist'] = !$optionexpiration ? $_G['forum_option'] : 'expire';
  420. $threadsortshow['typetemplate'] = $typetemplate;
  421. $threadsortshow['expiration'] = dgmdate($sortdataexpiration, 'd');
  422. return $threadsortshow;
  423. }
  424. function threadsortshow_callback_showoption_title1($matches) {
  425. return showoption($matches[1], 'title');
  426. }
  427. function threadsortshow_callback_showoption_value1($matches) {
  428. return showoption($matches[1], 'value');
  429. }
  430. function threadsortshow_callback_showoption_unit1($matches) {
  431. return showoption($matches[1], 'unit');
  432. }
  433. function showoption($var, $type) {
  434. global $_G;
  435. if($_G['forum_option'][$var][$type] != '') {
  436. return $_G['forum_option'][$var][$type];
  437. } else {
  438. return '';
  439. }
  440. }
  441. function protectguard($protect) {
  442. global $_G, $member_verifys;
  443. if(!isset($member_verifys) && $_G['setting']['verify']['enabled']) {
  444. $member_verifys = array();
  445. getuserprofile('verify1');
  446. foreach($_G['setting']['verify'] as $vid => $verify) {
  447. if($verify['available'] && $_G['member']['verify'.$vid] == 1) {
  448. $member_verifys[] = $vid;
  449. }
  450. }
  451. }
  452. $verifyflag = 0;
  453. if($_G['setting']['verify']['enabled'] && $protect['verify']) {
  454. if(array_intersect(explode("\t", $protect['verify']), $member_verifys)) {
  455. $verifyflag = 1;
  456. }
  457. }
  458. if(($protect['usergroup'] && strstr("\t".$protect['usergroup']."\t", "\t$_G[groupid]\t"))
  459. || (empty($protect['usergroup']) && empty($protect['verify']))
  460. || $verifyflag
  461. || $_G['forum_thread']['authorid'] == $_G['uid']) {
  462. return false;
  463. } else {
  464. return true;
  465. }
  466. }
  467. function sortthreadsortselectoption($sortid) {
  468. global $_G;
  469. if(empty($_G['cache']['threadsort_option_'.$sortid])) {
  470. return false;
  471. }
  472. foreach($_G['cache']['threadsort_option_'.$sortid] as $key => $value) {
  473. if($value['type'] == 'select' && !empty($value['choices'])) {
  474. $newsort = array();
  475. $level = 0;
  476. foreach($value['choices'] as $subkey => $subvalue) {
  477. $newsort[$subkey]['content'] = $subvalue;
  478. $newsort[$subkey]['foptionid'] = trim(substr($subkey, 0, strrpos($subkey, '.'))) ? trim(substr($subkey, 0, strrpos($subkey, '.'))) : '0';
  479. $newsort[$subkey]['count'] = count(explode('.', $subkey));
  480. $subkeyarr = explode('.', $subkey);
  481. if($countsubkeyarr = count($subkeyarr)) {
  482. $tmpkey = '';
  483. for($i = 0;$i < $countsubkeyarr;$i++) {
  484. $subkeyarr[$i] = trim($subkeyarr[$i]);
  485. if(isset($newsort[$tmpkey.$subkeyarr[$i]]['level'])) {
  486. if(($countsubkeyarr - $i) > $newsort[$tmpkey.$subkeyarr[$i]]['level']) {
  487. $newsort[$tmpkey.$subkeyarr[$i]]['level'] = $countsubkeyarr - $i;
  488. }
  489. } else {
  490. $newsort[$tmpkey.$subkeyarr[$i]]['level'] = $countsubkeyarr - $i;
  491. }
  492. $tmpkey .= $subkeyarr[$i].'.';
  493. }
  494. }
  495. $newsort[$subkey]['optionid'] = $subkey;
  496. }
  497. $_G['cache']['threadsort_option_'.$sortid][$key]['choices'] = $newsort;
  498. }
  499. }
  500. }
  501. function cmpchoicekey($stringa, $stringb) {
  502. $arraya = explode('.', $stringa);
  503. $arrayb = explode('.', $stringb);
  504. $counta = count($arraya);
  505. $countb = count($arrayb);
  506. if($counta == $countb) {
  507. foreach($arraya as $key => $value) {
  508. $valuea = intval(trim($value));
  509. $valueb = intval(trim($arrayb[$key]));
  510. if($valuea != $valueb) {
  511. return ($valuea < $valueb) ? -1 : 1;
  512. } else {
  513. continue;
  514. }
  515. }
  516. return 0;
  517. } else {
  518. return ($counta < $countb) ? -1 : 1;
  519. }
  520. }
  521. function threadsort_checkoption($sortid = 0, $unchangeable = 1) {
  522. global $_G;
  523. $_G['forum_selectsortid'] = $sortid ? intval($sortid) : '';
  524. loadcache(array('threadsort_option_'.$sortid));
  525. sortthreadsortselectoption($sortid);
  526. $_G['forum_optionlist'] = $_G['cache']['threadsort_option_'.$sortid];
  527. $_G['forum_checkoption'] = array();
  528. if(is_array($_G['forum_optionlist'])) {
  529. foreach($_G['forum_optionlist'] as $optionid => $option) {
  530. $_G['forum_checkoption'][$option['identifier']]['optionid'] = $optionid;
  531. $_G['forum_checkoption'][$option['identifier']]['title'] = $option['title'];
  532. $_G['forum_checkoption'][$option['identifier']]['type'] = $option['type'];
  533. $_G['forum_checkoption'][$option['identifier']]['required'] = $option['required'] ? 1 : 0;
  534. $_G['forum_checkoption'][$option['identifier']]['unchangeable'] = $_GET['action'] == 'edit' && $unchangeable && $option['unchangeable'] ? 1 : 0;
  535. $_G['forum_checkoption'][$option['identifier']]['maxnum'] = $option['maxnum'] ? intval($option['maxnum']) : '';
  536. $_G['forum_checkoption'][$option['identifier']]['minnum'] = $option['minnum'] ? intval($option['minnum']) : '';
  537. $_G['forum_checkoption'][$option['identifier']]['maxlength'] = $option['maxlength'] ? intval($option['maxlength']) : '';
  538. }
  539. }
  540. }
  541. function threadsort_optiondata($pid, $sortid, $sortoptionarray, $templatearray) {
  542. global $_G;
  543. $_G['forum_optiondata'] = $_G['forum_typetemplate'] = $_G['forum_option'] = $_G['forum_memberinfo'] = $searchcontent = array();
  544. $id = $_G['tid'];
  545. if($id) {
  546. foreach(C::t('forum_typeoptionvar')->fetch_all_by_tid_optionid($id) as $option) {
  547. $_G['forum_optiondata'][$option['optionid']] = $option['value'];
  548. $_G['forum_optiondata']['expiration'] = $option['expiration'];
  549. }
  550. }
  551. $_G['forum_optiondata']['expiration'] = $_G['forum_optiondata']['expiration'] ? dgmdate($_G['forum_optiondata']['expiration'], 'd') : '';
  552. foreach($sortoptionarray as $optionid => $option) {
  553. if($id) {
  554. $_G['forum_optionlist'][$optionid]['unchangeable'] = $sortoptionarray[$optionid]['unchangeable'] ? 'disabled' : '';
  555. if($sortoptionarray[$optionid]['type'] == 'radio') {
  556. $_G['forum_optionlist'][$optionid]['value'] = array($_G['forum_optiondata'][$optionid] => 'checked="checked"');
  557. } elseif($sortoptionarray[$optionid]['type'] == 'select') {
  558. $_G['forum_optionlist'][$optionid]['value'] = $_G['forum_optiondata'][$optionid] ? array($_G['forum_optiondata'][$optionid] => 'selected="selected"') : '';
  559. } elseif($sortoptionarray[$optionid]['type'] == 'checkbox') {
  560. foreach(explode("\t", $_G['forum_optiondata'][$optionid]) as $value) {
  561. $_G['forum_optionlist'][$optionid]['value'][$value] = array($value => 'checked="checked"');
  562. }
  563. } elseif($sortoptionarray[$optionid]['type'] == 'image') {
  564. $_G['forum_optionlist'][$optionid]['value'] = dunserialize($_G['forum_optiondata'][$optionid]);
  565. } else {
  566. $_G['forum_optionlist'][$optionid]['value'] = $_G['forum_optiondata'][$optionid];
  567. }
  568. if(!isset($_G['forum_optiondata'][$optionid])) {
  569. C::t('forum_typeoptionvar')->insert(array(
  570. 'sortid' => $sortid,
  571. 'tid' => $id,
  572. 'fid' => $_G['fid'],
  573. 'optionid' => $optionid,
  574. ));
  575. }
  576. }
  577. if($templatearray['post']) {
  578. $_G['forum_option'][$option['identifier']]['title'] = $option['title'];
  579. $_G['forum_option'][$option['identifier']]['unit'] = $option['unit'];
  580. $_G['forum_option'][$option['identifier']]['description'] = $option['description'];
  581. $_G['forum_option'][$option['identifier']]['required'] = $option['required'] ? '*' : '';
  582. $_G['forum_option'][$option['identifier']]['tips'] = '<span id="check'.$option['identifier'].'"></span>';
  583. $showoption = gettypetemplate($option, $_G['forum_optionlist'][$optionid], $optionid);
  584. $_G['forum_option'][$option['identifier']]['value'] = $showoption[$option['identifier']]['value'];
  585. $searchcontent['title'][] = '/{('.$option['identifier'].')}/';
  586. $searchcontent['value'][] = '/\[('.$option['identifier'].')value\]/';
  587. $searchcontent['value'][] = '/{('.$option['identifier'].')_value}/';
  588. $searchcontent['unit'][] = '/\[('.$option['identifier'].')unit\]/';
  589. $searchcontent['unit'][] = '/{('.$option['identifier'].')_unit}/';
  590. $searchcontent['description'][] = '/\[('.$option['identifier'].')description\]/';
  591. $searchcontent['description'][] = '/{('.$option['identifier'].')_description}/';
  592. $searchcontent['required'][] = '/\[('.$option['identifier'].')required\]/';
  593. $searchcontent['required'][] = '/{('.$option['identifier'].')_required}/';
  594. $searchcontent['tips'][] = '/\[('.$option['identifier'].')tips\]/';
  595. $searchcontent['tips'][] = '/{('.$option['identifier'].')_tips}/';
  596. }
  597. }
  598. if($templatearray['post']) {
  599. $typetemplate = $templatearray['post'];
  600. foreach($searchcontent as $key => $content) {
  601. $typetemplate = preg_replace_callback($searchcontent[$key], create_function('$matches', 'return showoption($matches[1], \''.addslashes($key).'\');'), stripslashes($typetemplate));
  602. }
  603. $_G['forum_typetemplate'] = $typetemplate;
  604. }
  605. }
  606. function threadsort_validator($sortoption, $pid) {
  607. global $_G, $var;
  608. $postaction = $_G['tid'] && $pid ? "edit&tid=$_G[tid]&pid=$pid" : 'newthread';
  609. $_G['forum_optiondata'] = array();
  610. foreach($_G['forum_checkoption'] as $var => $option) {
  611. if($_G['forum_checkoption'][$var]['required'] && ($sortoption[$var] === '' && $_G['forum_checkoption'][$var]['type'] != 'number')) {
  612. showmessage('threadtype_required_invalid', "forum.php?mod=post&action=$postaction&fid=$_G[fid]&sortid=".$_G['forum_selectsortid'], array('typetitle' => $_G['forum_checkoption'][$var]['title']));
  613. } elseif($sortoption[$var] && ($_G['forum_checkoption'][$var]['type'] == 'number' && !is_numeric($sortoption[$var]) || $_G['forum_checkoption'][$var]['type'] == 'email' && !isemail($sortoption[$var]))){
  614. showmessage('threadtype_format_invalid', "forum.php?mod=post&action=$postaction&fid=$_G[fid]&sortid=".$_G['forum_selectsortid'], array('typetitle' => $_G['forum_checkoption'][$var]['title']));
  615. } elseif($sortoption[$var] && $_G['forum_checkoption'][$var]['maxlength'] && strlen($sortoption[$var]) > $_G['forum_checkoption'][$var]['maxlength']) {
  616. showmessage('threadtype_toolong_invalid', "forum.php?mod=post&action=$postaction&fid=$_G[fid]&sortid=".$_G['forum_selectsortid'], array('typetitle' => $_G['forum_checkoption'][$var]['title']));
  617. } elseif($sortoption[$var] && (($_G['forum_checkoption'][$var]['maxnum'] && $sortoption[$var] > $_G['forum_checkoption'][$var]['maxnum']) || ($_G['forum_checkoption'][$var]['minnum'] && $sortoption[$var] < $_G['forum_checkoption'][$var]['minnum']))) {
  618. showmessage('threadtype_num_invalid', "forum.php?mod=post&action=$postaction&fid=$_G[fid]&sortid=".$_G['forum_selectsortid'], array('typetitle' => $_G['forum_checkoption'][$var]['title']));
  619. } elseif($sortoption[$var] && $_G['forum_checkoption'][$var]['unchangeable'] && !($_G['tid'] && $pid)) {
  620. showmessage('threadtype_unchangeable_invalid', "forum.php?mod=post&action=$postaction&fid=$_G[fid]&sortid=".$_G['forum_selectsortid'], array('typetitle' => $_G['forum_checkoption'][$var]['title']));
  621. } elseif($sortoption[$var] && ($_G['forum_checkoption'][$var]['type'] == 'select')) {
  622. if($_G['forum_optionlist'][$_G['forum_checkoption'][$var]['optionid']]['choices'][$sortoption[$var]]['level'] != 1) {
  623. showmessage('threadtype_select_invalid', "forum.php?mod=post&action=$postaction&fid=$_G[fid]&sortid=".$_G['forum_selectsortid'], array('typetitle' => $_G['forum_checkoption'][$var]['title']));
  624. }
  625. }
  626. if($_G['forum_checkoption'][$var]['type'] == 'checkbox') {
  627. $sortoption[$var] = $sortoption[$var] ? implode("\t", $sortoption[$var]) : '';
  628. } elseif($_G['forum_checkoption'][$var]['type'] == 'url') {
  629. $sortoption[$var] = $sortoption[$var] ? (substr(strtolower($sortoption[$var]), 0, 4) == 'www.' ? 'http://'.$sortoption[$var] : $sortoption[$var]) : '';
  630. }
  631. if($_G['forum_checkoption'][$var]['type'] == 'image') {
  632. if($sortoption[$var]['aid']) {
  633. $_GET['attachnew'][$sortoption[$var]['aid']] = $sortoption[$var];
  634. }
  635. $sortoption[$var] = serialize($sortoption[$var]);
  636. } elseif($_G['forum_checkoption'][$var]['type'] == 'select') {
  637. $sortoption[$var] = censor(trim($sortoption[$var]));
  638. } else {
  639. $sortoption[$var] = dhtmlspecialchars(censor(trim($sortoption[$var])));
  640. }
  641. $_G['forum_optiondata'][$_G['forum_checkoption'][$var]['optionid']] = $sortoption[$var];
  642. }
  643. return $_G['forum_optiondata'];
  644. }
  645. function getsortedoptionlist() {
  646. global $_G;
  647. $forum_optionlist = $_G['forum_optionlist'];
  648. foreach($_G['forum_optionlist'] as $key => $value) {
  649. $choicesarr = $value['choices'];
  650. uksort($choicesarr, 'cmpchoicekey');
  651. $forum_optionlist[$key]['choices'] = $choicesarr;
  652. }
  653. $forum_optionlist = optionlistxml($forum_optionlist, 's');
  654. $forum_optionlist = '<?xml version="1.0" encoding="'.CHARSET.'"?>'."".'<forum_optionlist>'.$forum_optionlist.'</forum_optionlist>';
  655. return $forum_optionlist;
  656. }
  657. function optionlistxml($input, $pre = '') {
  658. $str = '';
  659. foreach($input as $key => $value) {
  660. $key = $pre.strval($key);
  661. if(is_array($value)) {
  662. $str .= "<$key>";
  663. $str .= optionlistxml($value, $pre);
  664. $str .="</$key>";
  665. } else {
  666. if(is_bool($value)) {
  667. $value = ($value == true) ? 'true' : 'false';
  668. }
  669. $value = str_replace("\r\n", '<br>', $value);
  670. if(dhtmlspecialchars($value) != $value) {
  671. $str .= "<$key><![CDATA[$value]]></$key>";
  672. } else {
  673. $str .= "<$key>$value</$key>";
  674. }
  675. }
  676. }
  677. return $str;
  678. }
  679. ?>