\s*\#', '{$threadfields_display}$0');
287 | else
288 | find_replace_templatesets('showthread', '#\\{\\$classic_header\\}#', '{$threadfields_display}{$classic_header}');
289 | find_replace_templatesets('forumdisplay_threadlist', '#\\{\\$threads\\}#', '{$threads}{$nullthreads}');
290 | $forumd_sorttpl = ($GLOBALS['mybb']->version_code >= 1827) ? 'forumdisplay_forumsort' : 'forumdisplay_threadlist';
291 | find_replace_templatesets($forumd_sorttpl, '#\\\\{\\$lang-\\>sort_by_subject\\}\\ #', '{$sort_by_prefix}{$lang->sort_by_subject} ');
292 | find_replace_templatesets($forumd_sorttpl, '#\\\\{\\$lang-\\>sort_by_views\\}\\ #', '{$lang->sort_by_views} '."\n".XTHREADS_INSTALL_TPLADD_EXTRASORT);
293 | find_replace_templatesets('forumdisplay_threadlist_sortrating', '#$#', '{$lang->sort_by_numratings} ');
294 | }
295 | $plugins->run_hooks('xthreads_activate_end');
296 | }
297 | function xthreads_deactivate() {
298 | global $db, $cache, $plugins;
299 | $plugins->run_hooks('xthreads_deactivate_start');
300 | $db->delete_query('tasks', 'file="xtaorphan_cleanup"');
301 | $cache->update_tasks();
302 |
303 | if(XTHREADS_MODIFY_TEMPLATES)
304 | xthreads_undo_template_edits();
305 | $plugins->run_hooks('xthreads_deactivate_end');
306 | }
307 |
308 | function xthreads_uninstall() {
309 | global $db, $cache, $mybb, $plugins;
310 |
311 | if(!empty($mybb->input['no'])) {
312 | admin_redirect(xthreads_admin_url('config', 'plugins'));
313 | exit;
314 | }
315 | if(empty($mybb->input['confirm_uninstall'])) {
316 | $link = 'index.php?confirm_uninstall=1&'.htmlspecialchars($_SERVER['QUERY_STRING']);
317 |
318 | $GLOBALS['page']->output_confirm_action($link, $GLOBALS['lang']->xthreads_confirm_uninstall);
319 | exit;
320 | } else
321 | unset($mybb->input['confirm_uninstall']);
322 |
323 | $plugins->run_hooks('xthreads_uninstall_start');
324 |
325 | $query = $db->simple_select('adminoptions', 'uid,permissions');
326 | while($adminopt = $db->fetch_array($query)) {
327 | $perms = @unserialize($adminopt['permissions']);
328 | if(empty($perms)) continue; // inherited or just messed up
329 | unset($perms['config']['threadfields']);
330 | $db->update_query('adminoptions', array('permissions' => $db->escape_string(serialize($perms))), 'uid='.$adminopt['uid']);
331 | }
332 | $db->free_result($query);
333 |
334 | if($db->table_exists('threadfields_data'))
335 | $db->write_query('DROP TABLE '.$db->table_prefix.'threadfields_data');
336 | if($db->table_exists('threadfields'))
337 | $db->write_query('DROP TABLE '.$db->table_prefix.'threadfields');
338 | if($db->table_exists('xtattachments')) {
339 | // remove attachments first
340 | require_once MYBB_ROOT.'inc/xthreads/xt_updatehooks.php';
341 | $query = $db->simple_select('xtattachments', 'aid,indir,attachname');
342 | while($xta = $db->fetch_array($query)) {
343 | xthreads_rm_attach_fs($xta);
344 | }
345 | $db->free_result($query);
346 | $db->write_query('DROP TABLE '.$db->table_prefix.'xtattachments');
347 | }
348 | // remove any indexes added on the threads table
349 | foreach(array(
350 | 'uid',
351 | 'lastposteruid',
352 | 'prefix',
353 | 'icon',
354 | ) as $afe) {
355 | if($afe == 'uid') continue; // we won't remove this from the above array
356 | $db->write_query('ALTER TABLE `'.$db->table_prefix.'threads` DROP KEY `xthreads_'.$afe.'`', true);
357 | }
358 |
359 | $fields = array(
360 | 'xthreads_grouping',
361 | 'xthreads_firstpostattop',
362 | 'xthreads_inlinesearch',
363 | 'xthreads_tplprefix',
364 | 'xthreads_langprefix',
365 | 'xthreads_allow_blankmsg',
366 | 'xthreads_nostatcount',
367 | 'xthreads_fdcolspan_offset',
368 | 'xthreads_settingoverrides',
369 | 'xthreads_postsperpage',
370 | 'xthreads_hideforum',
371 | 'xthreads_hidebreadcrumb',
372 | 'xthreads_defaultfilter',
373 | 'xthreads_addfiltenable', // legacy (uninstall w/o upgrade)
374 | //'xthreads_pull_firstpost',
375 | 'xthreads_wol_announcements',
376 | 'xthreads_wol_forumdisplay',
377 | 'xthreads_wol_newthread',
378 | 'xthreads_wol_attachment',
379 | 'xthreads_wol_newreply',
380 | 'xthreads_wol_showthread',
381 | );
382 | foreach($fields as $k => &$f)
383 | if(!$db->field_exists($f, 'forums'))
384 | unset($fields[$k]);
385 |
386 | if(!empty($fields)) {
387 | switch($db->type) {
388 | case 'sqlite3': case 'sqlite2': case 'sqlite':
389 | $db->alter_table_parse($db->table_prefix.'forums', 'DROP '.implode(', DROP COLUMN ', $fields).'');
390 | break;
391 | case 'pgsql':
392 | foreach($fields as &$f)
393 | $db->write_query('ALTER TABLE '.$db->table_prefix.'forums
394 | DROP COLUMN '.$f);
395 | break;
396 | default:
397 | $db->write_query('ALTER TABLE '.$db->table_prefix.'forums
398 | DROP COLUMN '.implode(', DROP COLUMN ', $fields));
399 | }
400 | }
401 |
402 | // remove any custom default sorts and reduce size of sorting column back to original
403 | $db->update_query('forums', array('defaultsortby' => ''), 'defaultsortby LIKE "tf_%" OR defaultsortby LIKE "tfa_%"');
404 | $db->write_query('ALTER TABLE `'.$db->table_prefix.'forums` MODIFY `defaultsortby` varchar(10) NOT NULL default \'\'');
405 | $cache->update_forums();
406 |
407 | xthreads_delete_datacache('threadfields');
408 |
409 | if(file_exists(MYBB_ROOT.'cache/xthreads.php'))
410 | @unlink(MYBB_ROOT.'cache/xthreads.php');
411 | if(file_exists(MYBB_ROOT.'cache/xthreads_evalcache.php'))
412 | @unlink(MYBB_ROOT.'cache/xthreads_evalcache.php');
413 |
414 | $db->delete_query('templates', 'title IN ("'.implode('","', array_keys(xthreads_new_templates())).'") AND sid=-2');
415 |
416 | // revert QuickThread modification
417 | if(function_exists('quickthread_uninstall')) {
418 | $tpl = $db->fetch_array($db->simple_select('templates', 'tid,template', 'title="forumdisplay_quick_thread" AND sid=-1', array('limit' => 1)));
419 | if($tpl && strpos($tpl['template'], '{$GLOBALS[\'extra_threadfields\']}') !== false) {
420 | $newtpl = preg_replace('~\\{\\$GLOBALS\\[\'extra_threadfields\'\\]\\}'."\r?(\n\t{0,3})?".'~is', '', $tpl['template'], 1);
421 | if($newtpl != $tpl['template'])
422 | $db->update_query('templates', array('template' => $db->escape_string($newtpl)), 'tid='.$tpl['tid']);
423 | }
424 | }
425 |
426 | // try to determine and remove stuff added to the custom moderation table
427 | $query = $db->simple_select('modtools', 'tid,threadoptions');
428 | while($tool = $db->fetch_array($query)) {
429 | $opts = unserialize($tool['threadoptions']);
430 | if(isset($opts['edit_threadfields'])) {
431 | unset($opts['edit_threadfields']);
432 | $db->update_query('modtools', array('threadoptions' => $db->escape_string(serialize($opts))), 'tid='.$tool['tid']);
433 | }
434 | }
435 |
436 | $plugins->run_hooks('xthreads_uninstall_end');
437 | }
438 |
439 | function xthreads_delete_datacache($key) {
440 | global $cache, $db;
441 | $cache->update($key, null);
442 | if(is_object($cache->handler) && method_exists($cache->handler, 'delete')) {
443 | $cache->handler->delete($key);
444 | }
445 | $db->delete_query('datacache', 'title="'.$db->escape_string($key).'"');
446 | }
447 |
448 | // rebuild threadfields cache on phptpl activation/deactivation
449 | function xthreads_plugins_phptpl_activate() { xthreads_plugins_phptpl_reparse(true); }
450 | function xthreads_plugins_phptpl_deactivate() { xthreads_plugins_phptpl_reparse(false); }
451 | function xthreads_plugins_phptpl_reparse($active) {
452 | if($GLOBALS['codename'] != 'phptpl' || !function_exists('phptpl_evalphp')) return;
453 |
454 | define('XTHREADS_ALLOW_PHP_THREADFIELDS_ACTIVATION', $active); // define is maybe safer?
455 | xthreads_buildtfcache();
456 | }
457 |
458 | function xthreads_plugins_quickthread_install() {
459 | if($GLOBALS['codename'] != 'quickthread' || !$GLOBALS['install_uninstall']) return;
460 | xthreads_plugins_quickthread_tplmod();
461 | }
462 | function xthreads_plugins_quickthread_tplmod() {
463 | if(!function_exists('quickthread_install')) return;
464 | global $db;
465 | $tpl = $db->fetch_array($db->simple_select('templates', 'tid,template', 'title="forumdisplay_quick_thread" AND sid=-1', array('limit' => 1)));
466 | if($tpl && strpos($tpl['template'], '{$GLOBALS[\'extra_threadfields\']}') === false) {
467 | $newtpl = preg_replace('~(\