function TPortalDLAdmin()
{
    global $txt, $scripturl, $boarddir, $boardurl, $smcFunc, $context, $settings, $sourcedir;
    // check permissions
    if (isset($_POST['dl_useredit'])) {
        checkSession('post');
    } else {
        isAllowedTo('tp_dlmanager');
    }
    // add visual options to this section
    $dl_visual = explode(',', $context['TPortal']['dl_visual_options']);
    $dv = array('left', 'right', 'center', 'top', 'bottom', 'lower');
    foreach ($dv as $v => $val) {
        if (in_array($val, $dl_visual)) {
            $context['TPortal'][$val . 'panel'] = '1';
            $context['TPortal']['dl_' . $val] = '1';
        } else {
            $context['TPortal'][$val . 'panel'] = '0';
        }
    }
    if (in_array('showtop', $dl_visual)) {
        $context['TPortal']['showtop'] = true;
        $context['TPortal']['dl_top'] = true;
    } else {
        $context['TPortal']['showtop'] = false;
    }
    if ($context['TPortal']['hidebars_admin_only'] == '1') {
        tp_hidebars();
    }
    // fetch membergroups so we can quickly set permissions
    // dlmanager, dlupload, dlcreatetopic
    $context['TPortal']['perm_all_groups'] = get_grps();
    $context['TPortal']['perm_groups'] = tp_fetchpermissions(array('tp_dlmanager', 'tp_dlupload', 'tp_dlcreatetopic'));
    $context['TPortal']['boards'] = tp_fetchboards();
    $context['TPortal']['all_dlitems'] = array();
    $request = $smcFunc['db_query']('', '
		SELECT id, name	FROM {db_prefix}tp_dlmanager
		WHERE type = {string:type}
		ORDER BY name ASC', array('type' => 'dlitem'));
    if ($smcFunc['db_num_rows']($request) > 0) {
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $context['TPortal']['all_dlitems'][] = array('id' => $row['id'], 'name' => $row['name']);
        }
        $smcFunc['db_free_result']($request);
    }
    // Add in BBC editor before we call in template so the headers are there
    if ($context['TPortal']['dl_wysiwyg'] == 'bbc') {
        if ($context['TPortal']['dlsub'] == 'adminaddcat') {
            $context['TPortal']['editor_id'] = 'newdladmin_text';
            TP_prebbcbox($context['TPortal']['editor_id']);
        } else {
            $context['TPortal']['editor_id'] = 'tp_dl_introtext';
            TP_prebbcbox($context['TPortal']['editor_id'], $context['TPortal']['dl_introtext']);
        }
    }
    // any items from the ftp screen?
    if (!empty($_POST['ftpdlsend'])) {
        // new category?
        if (!empty($_POST['assign-ftp-newcat'])) {
            $newcat = true;
            $newcatname = $_POST['assign-ftp-newcat'];
            if (isset($_POST['assign-ftp-cat']) && $_POST['assign-ftp-cat'] > 0) {
                $newcatparent = $_POST['assign-ftp-cat'];
            } else {
                $newcatparent = 0;
            }
            if ($newcatname == '') {
                $newcatname = '-no name-';
            }
        } else {
            $newcat = false;
            $newcatname = '';
            $newcatnow = $_POST['assign-ftp-cat'];
            $newcatparent = 0;
        }
        // if new category create it first.
        if ($newcat) {
            $request = $smcFunc['db_insert']('INSERT', '{db_prefix}tp_dlmanager', array('name' => 'string', 'description' => 'string', 'icon' => 'string', 'category' => 'int', 'type' => 'string', 'downloads' => 'int', 'views' => 'int', 'file' => 'string', 'created' => 'int', 'last_access' => 'int', 'filesize' => 'int', 'parent' => 'int', 'access' => 'string', 'link' => 'string', 'author_id' => 'int', 'screenshot' => 'string', 'rating' => 'string', 'voters' => 'string', 'subitem' => 'int'), array($newcatname, '', '', 0, 'dlcat', 0, 0, '', 0, 0, 0, $newcatparent, '', '', $context['user']['id'], '', '', '', 0), array('id'));
            $newcatnow = $smcFunc['db_insert_id']($request);
        }
        // now go through each file and put it into the table.
        foreach ($_POST as $what => $value) {
            if (substr($what, 0, 19) == 'assign-ftp-checkbox') {
                $name = $value;
                $now = time();
                $fsize = filesize($boarddir . '/tp-downloads/' . $value);
                $smcFunc['db_insert']('INSERT', '{db_prefix}tp_dlmanager', array('name' => 'string', 'description' => 'string', 'icon' => 'string', 'category' => 'int', 'type' => 'string', 'downloads' => 'int', 'views' => 'int', 'file' => 'string', 'created' => 'int', 'last_access' => 'int', 'filesize' => 'int', 'parent' => 'int', 'access' => 'string', 'link' => 'string', 'author_id' => 'int', 'screenshot' => 'string', 'rating' => 'string', 'voters' => 'string', 'subitem' => 'int'), array($name, '', '', $newcatnow, 'dlitem', 1, 1, $value, $now, $now, $fsize, 0, '', '', $context['user']['id'], '', '', '', 0), array('id'));
            }
        }
        // done, set a value to make member aware of assigned category
        redirectexit('action=tpmod;dl=adminftp;ftpcat=' . $newcatnow);
    }
    // check for new category
    if (!empty($_POST['newdlsend'])) {
        // get the items
        $name = strip_tags($_POST['newdladmin_name']);
        // no html here
        if (empty($name)) {
            $name = $txt['tp-dlnotitle'];
        }
        $text = $_POST['newdladmin_text'];
        $parent = $_POST['newdladmin_parent'];
        $icon = $boardurl . '/tp-downloads/icons/' . $_POST['newdladmin_icon'];
        // special case, the access
        $dlgrp = array();
        foreach ($_POST as $what => $value) {
            if (substr($what, 0, 16) == 'newdladmin_group') {
                $vv = substr($what, 16);
                if ($vv != '-2') {
                    $dlgrp[] = $vv;
                }
            }
        }
        $access = implode(',', $dlgrp);
        // insert the category
        $request = $smcFunc['db_insert']('INSERT', '{db_prefix}tp_dlmanager', array('name' => 'string', 'description' => 'string', 'icon' => 'string', 'category' => 'int', 'type' => 'string', 'downloads' => 'int', 'views' => 'int', 'file' => 'string', 'created' => 'int', 'last_access' => 'int', 'filesize' => 'int', 'parent' => 'int', 'access' => 'string', 'link' => 'string', 'author_id' => 'int', 'screenshot' => 'string', 'rating' => 'string', 'voters' => 'string', 'subitem' => 'int'), array($name, $text, $icon, 0, 'dlcat', 0, 0, '', 0, 0, 0, $parent, $access, '', $context['user']['id'], '', '', '', 0), array('id'));
        $newcat = $smcFunc['db_insert_id']($request);
        redirectexit('action=tpmod;dl=admineditcat' . $newcat);
    }
    $myid = 0;
    // check if tag links are present
    if (isset($_POST['dladmin_itemtags'])) {
        $itemid = $_POST['dladmin_itemtags'];
        // get title
        $request = $smcFunc['db_query']('', '
			SELECT name FROM {db_prefix}tp_dlmanager 
			WHERE id = {int:item} LIMIT 1', array('item' => $itemid));
        $title = $smcFunc['db_fetch_row']($request);
        // remove old ones first
        $smcFunc['db_query']('', '
			DELETE FROM {db_prefix}tp_variables 
			WHERE value3 = {string:val3} 
			AND subtype2 = {int:sub}', array('val3' => 'dladmin_itemtags', 'sub' => $itemid));
        $alltags = array();
        foreach ($_POST as $what => $value) {
            // a tag from edit items
            if (substr($what, 0, 17) == 'dladmin_itemtags_') {
                $tag = substr($what, 17);
                $itemid = $value;
                // insert new one
                $href = '?action=tpmod;dl=item' . $itemid;
                $tg = '<span style="background: url(' . $settings['tp_images_url'] . '/glyph_download.png) no-repeat;" class="taglink">' . $title[0] . '</span>';
                if (!empty($tag)) {
                    $smcFunc['db_query']('INSERT', '{db_prefix}tp_variables', array('value1' => 'string', 'value2' => 'string', 'value3' => 'string', 'type' => 'string', 'value4' => 'string', 'value5' => 'int', 'subtype' => 'string', 'value7' => 'string', 'value8' => 'string', 'subtype2' => 'int'), array($href, $tg, 'dladmin_itemtags', '', 0, $tag, '', '', $itemid), array('id'));
                    $alltags[] = $tag;
                }
            }
        }
        $tg = implode(',', $alltags);
        $smcFunc['db_query']('', '
			UPDATE {db_prefix}tp_dlmanager 
			SET global_tag = {string:tag} 
			WHERE id = {int:item}', array('tag' => $tg, 'item' => $itemid));
        $myid = $itemid;
        $go = 2;
        $newgo = 2;
    }
    // check if tag links are present -categories
    if (isset($_POST['dladmin_cattags'])) {
        $itemid = $_POST['dladmin_cattags'];
        // get title
        $request = $smcFunc['db_query']('', '
			SELECT name FROM {db_prefix}tp_dlmanager 
			WHERE id = {int:item} LIMIT 1', array('item' => $itemid));
        $title = $smcFunc['db_fetch_row']($request);
        // remove old ones first
        $smcFunc['db_query']('', '
			DELETE FROM {db_prefix}tp_variables 
			WHERE value3 = {string:val3} 
			AND subtype2 = {int:sub}', array('val3' => 'dladmin_cattags', 'sub' => $itemid));
        foreach ($_POST as $what => $value) {
            // a tag from edit category
            if (substr($what, 0, 16) == 'dladmin_cattags_') {
                $tag = substr($what, 16);
                $itemid = $value;
                // insert new one
                $href = '?action=tpmod;dl=cat' . $itemid;
                $title = $title[0] . ' [' . strtolower($txt['tp-downloads']) . '] ';
                $smcFunc['db_query']('INSERT', '{db_prefix}tp_variables', array('value1' => 'string', 'value2' => 'string', 'value3' => 'string', 'type' => 'string', 'value4' => 'string', 'value5' => 'int', 'subtype' => 'string', 'value7' => 'string', 'value8' => 'string', 'subtype2' => 'int'), array($href, $title, 'dladmin_cattags', '', 0, $tag, '', '', $itemid), array('id'));
            }
        }
        $myid = $itemid;
        $go = 3;
        $newgo = 3;
    }
    // check for access value
    if (!empty($_POST['dlsend'])) {
        $admgrp = array();
        $groupset = false;
        $dlgrp = array();
        $dlset = false;
        $visual = array();
        $visualset = false;
        $creategrp = array();
        $dlmanager_grp = array();
        $dlupload_grp = array();
        $dlcreatetopic_grp = array();
        // Our settings array to send to updateTPSettings();
        $changeArray = array();
        foreach ($_POST as $what => $value) {
            if (substr($what, 0, 13) == 'dladmin_group') {
                $val = substr($what, 13);
                if ($val != '-2') {
                    $admgrp[] = $val;
                }
                $groupset = true;
                $id = $value;
            } elseif (substr($what, 0, 8) == 'tp_group') {
                if ($value != '-2') {
                    $dlgrp[] = $value;
                }
                $dlset = true;
            } elseif (substr($what, 0, 20) == 'tp_dl_visual_options') {
                if ($value != 'not') {
                    $visual[] = $value;
                }
                $visualset = true;
            } elseif (substr($what, 0, 11) == 'tp_dlboards') {
                $creategrp[] = $value;
            }
        }
        if ($groupset) {
            $dlaccess = implode(',', $admgrp);
            $smcFunc['db_query']('', '
				UPDATE {db_prefix}tp_dlmanager 
				SET access = {string:access} 
				WHERE id = {int:item}', array('access' => $dlaccess, 'item' => $id));
        }
        if (!empty($_POST['dlsettings'])) {
            $changeArray['dl_createtopic_boards'] = implode(',', $creategrp);
        }
        if ($dlset) {
            $changeArray['dl_approve_groups'] = implode(',', $dlgrp);
        }
        if ($visualset) {
            $changeArray['dl_visual_options'] = implode(',', $visual);
        }
        $go = 0;
        if (!empty($_FILES['qup_dladmin_text']['tmp_name']) && (file_exists($_FILES['qup_dladmin_text']['tmp_name']) || is_uploaded_file($_FILES['qup_dladmin_text']['tmp_name']))) {
            $name = TPuploadpicture('qup_dladmin_text', $context['user']['id'] . 'uid');
            tp_createthumb('tp-images/' . $name, 50, 50, 'tp-images/thumbs/thumb_' . $name);
        }
        if (!empty($_FILES['qup_blockbody']['tmp_name']) && (file_exists($_FILES['qup_dladmin_text']['tmp_name']) || is_uploaded_file($_FILES['qup_dladmin_text']['tmp_name']))) {
            $name = TPuploadpicture('qup_dladmin_text', $context['user']['id'] . 'uid');
            tp_createthumb('tp-images/' . $name, 50, 50, 'tp-images/thumbs/thumb_' . $name);
        }
        // a screenshot from edit item screen?
        if (!empty($_FILES['tp_dluploadpic_edit']['tmp_name']) && (file_exists($_FILES['tp_dluploadpic_edit']['tmp_name']) || is_uploaded_file($_FILES['tp_dluploadpic_edit']['tmp_name']))) {
            $shot = true;
        } else {
            $shot = false;
        }
        if ($shot) {
            $sid = $_POST['tp_dluploadpic_editID'];
            $sfile = 'tp_dluploadpic_edit';
            $uid = $context['user']['id'] . 'uid';
            $dim = '1800';
            $suf = 'jpg,gif,png';
            $dest = 'tp-images/dlmanager';
            $sname = TPuploadpicture($sfile, $uid, $dim, $suf, $dest);
            $screenshot = $sname;
            tp_createthumb($dest . '/' . $sname, $context['TPortal']['dl_screenshotsize'][0], $context['TPortal']['dl_screenshotsize'][1], $dest . '/thumb/' . $sname);
            tp_createthumb($dest . '/' . $sname, $context['TPortal']['dl_screenshotsize'][2], $context['TPortal']['dl_screenshotsize'][3], $dest . '/listing/' . $sname);
            tp_createthumb($dest . '/' . $sname, $context['TPortal']['dl_screenshotsize'][4], $context['TPortal']['dl_screenshotsize'][5], $dest . '/single/' . $sname);
            $smcFunc['db_query']('', '
				UPDATE {db_prefix}tp_dlmanager 
				SET screenshot = {string:ss} 
				WHERE id = {int:item}', array('ss' => $screenshot, 'item' => $sid));
            $uploaded = true;
        } else {
            $screenshot = '';
            $uploaded = false;
        }
        if (isset($_POST['tp_dluploadpic_link']) && !$uploaded) {
            $sid = $_POST['tp_dluploadpic_editID'];
            $screenshot = $_POST['tp_dluploadpic_link'];
            $smcFunc['db_query']('', '
				UPDATE {db_prefix}tp_dlmanager 
				SET screenshot = {string:ss} 
				WHERE id = {int:item}', array('ss' => $screenshot, 'item' => $sid));
        } else {
            $screenshot = '';
        }
        // a new file uploaded?
        if (!empty($_FILES['tp_dluploadfile_edit']['tmp_name']) && is_uploaded_file($_FILES['tp_dluploadfile_edit']['tmp_name'])) {
            $shot = true;
        } else {
            $shot = false;
        }
        if ($shot) {
            $sid = $_POST['tp_dluploadfile_editID'];
            $shotname = $_FILES['tp_dluploadfile_edit']['name'];
            $sname = strtr($shotname, 'ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ', 'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy');
            $sname = strtr($sname, array('Þ' => 'TH', 'þ' => 'th', 'Ð' => 'DH', 'ð' => 'dh', 'ß' => 'ss', 'Œ' => 'OE', 'œ' => 'oe', 'Æ' => 'AE', 'æ' => 'ae', 'µ' => 'u'));
            $sname = preg_replace(array('/\\s/', '/[^\\w_\\.\\-]/'), array('_', ''), $sname);
            $sname = time() . $sname;
            // check the size
            $dlfilesize = filesize($_FILES['tp_dluploadfile_edit']['tmp_name']);
            if ($dlfilesize > 1000 * $context['TPortal']['dl_max_upload_size']) {
                unlink($_FILES['tp_dluploadfile_edit']['tmp_name']);
                $error = $txt['tp-dlmaxerror'] . ' ' . $context['TPortal']['dl_max_upload_size'] . ' Kb<br /><br />' . $txt['tp-dlmaxerror2'] . ': ' . ceil($dlfilesize / 1000) . ' Kb';
                fatal_error($error);
            }
            // check the extension
            $allowed = explode(',', $context['TPortal']['dl_allowed_types']);
            $match = false;
            foreach ($allowed as $extension => $value) {
                $ext = '.' . $value;
                $extlen = strlen($ext);
                if (substr($sname, strlen($sname) - $extlen, $extlen) == $ext) {
                    $match = true;
                }
            }
            if (!$match) {
                unlink($_FILES['tp_dluploadfile_edit']['tmp_name']);
                $error = $txt['tp-dlexterror'] . ':<b> <br />' . $context['TPortal']['dl_allowed_types'] . '</b><br /><br />' . $txt['tp-dlexterror2'] . ': <b>' . $sname . '</b>';
                fatal_error($error);
            }
            $success2 = move_uploaded_file($_FILES['tp_dluploadfile_edit']['tmp_name'], $boarddir . '/tp-downloads/' . $sname);
            $smcFunc['db_query']('', '
				UPDATE {db_prefix}tp_dlmanager 
				SET file = {string:file} 
				WHERE id = {int:item}', array('file' => $sname, 'item' => $sid));
            $new_upload = true;
            // update filesize as well
            $value = filesize($boarddir . '/tp-downloads/' . $sname);
            if (!is_numeric($value)) {
                $value = 0;
            }
            $smcFunc['db_query']('', '
				UPDATE {db_prefix}tp_dlmanager 
				SET filesize = {int:size}
				WHERE id = {int:item}', array('size' => $value, 'item' => $sid));
            $myid = $sid;
            $go = 2;
        }
        // get all values from forms
        foreach ($_POST as $what => $value) {
            if (substr($what, 0, 12) == 'dladmin_name') {
                $id = substr($what, 12);
                // no html here
                $value = strip_tags($value);
                if (empty($value)) {
                    $value = '-no title-';
                }
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}tp_dlmanager 
					SET name = {string:name} 
					WHERE id = {int:item}', array('name' => $value, 'item' => $id));
            } elseif (substr($what, 0, 12) == 'dladmin_icon') {
                $id = substr($what, 12);
                if ($value != '') {
                    $val = $boardurl . '/tp-downloads/icons/' . $value;
                    $smcFunc['db_query']('', '
						UPDATE {db_prefix}tp_dlmanager 
						SET icon = {string:icon} 
						WHERE id = {int:item}', array('icon' => $val, 'item' => $id));
                }
            } elseif (substr($what, 0, 12) == 'dladmin_text') {
                $id = substr($what, 12);
                if (is_numeric($id)) {
                    // If we came from WYSIWYG then turn it back into BBC regardless.
                    if (!empty($_REQUEST[$what . '_mode']) && isset($_REQUEST[$what])) {
                        require_once $sourcedir . '/Subs-Editor.php';
                        $_REQUEST[$what] = html_to_bbc($_REQUEST[$what]);
                        // We need to unhtml it now as it gets done shortly.
                        $_REQUEST[$what] = un_htmlspecialchars($_REQUEST[$what]);
                        // We need this for everything else.
                        $value = $_POST[$what] = $_REQUEST[$what];
                    }
                    if (isset($_POST['dladmin_text' . $id . '_pure']) && isset($_POST['dladmin_text' . $id . '_choice'])) {
                        if ($_POST['dladmin_text' . $id . '_choice'] == 1) {
                            $value = $_POST['dladmin_text' . $id];
                        } else {
                            $value = $_POST['dladmin_text' . $id . '_pure'];
                        }
                    }
                    $smcFunc['db_query']('', '
						UPDATE {db_prefix}tp_dlmanager 
						SET description = {string:desc} 
						WHERE id = {int:item}', array('desc' => $value, 'item' => $id));
                }
            } elseif (substr($what, 0, 14) == 'dladmin_delete') {
                $id = substr($what, 14);
                $request = $smcFunc['db_query']('', '
						SELECT * FROM {db_prefix}tp_dlmanager 
						WHERE id = {int:item}', array('item' => $id));
                if ($smcFunc['db_num_rows']($request) > 0) {
                    $row = $smcFunc['db_fetch_assoc']($request);
                    if ($row['type'] == 'dlitem') {
                        $category = $row['category'];
                        if ($category > 0) {
                            $smcFunc['db_query']('', '
									UPDATE {db_prefix}tp_dlmanager 
									SET downloads = downloads - 1 
									WHERE id = {int:cat} LIMIT 1', array('cat' => $category));
                        }
                        // delete both screenshot and file
                        if (!empty($row['file']) && file_exists($boarddir . '/tp-downloads/' . $row['file'])) {
                            $succ = unlink($boarddir . '/tp-downloads/' . $row['file']);
                            if (!$succ) {
                                $err = $txt['tp-dlfilenotdel'] . ' (' . $row['file'] . ')';
                            }
                        }
                        if (!empty($row['screenshot']) && file_exists($boarddir . '/' . $row['screenshot'])) {
                            $succ2 = unlink($boarddir . '/' . $row['screenshot']);
                            if (!$succ2) {
                                $err .= '<br />' . $txt['tp-dlssnotdel'] . ' (' . $row['screenshot'] . ')';
                            }
                        }
                    }
                    $smcFunc['db_free_result']($request);
                }
                $smcFunc['db_query']('', '
					DELETE FROM {db_prefix}tp_dlmanager 
					WHERE id = {int:item}', array('item' => $id));
                if (isset($err)) {
                    fatal_error($err);
                }
                redirectexit('action=tpmod;dl=admincat' . $category);
            } elseif (substr($what, 0, 15) == 'dladmin_approve' && $value == 'ON') {
                $id = abs(substr($what, 15));
                $request = $smcFunc['db_query']('', '
					SELECT category FROM {db_prefix}tp_dlmanager 
					WHERE id = {int:item}', array('item' => $id));
                if ($smcFunc['db_num_rows']($request) > 0) {
                    $row = $smcFunc['db_fetch_row']($request);
                    $newcat = abs($row[0]);
                    $smcFunc['db_query']('', '
						UPDATE {db_prefix}tp_dlmanager 
						SET category = {int:cat} 
						WHERE id = {int:item}', array('cat' => $newcat, 'item' => $id));
                    $smcFunc['db_query']('', '
						DELETE FROM {db_prefix}tp_variables 
						WHERE type = {string:type} 
						AND value5 = {int:val5}', array('type' => 'dl_not_approved', 'val5' => $id));
                    $smcFunc['db_free_result']($request);
                }
            } elseif (substr($what, 0, 16) == 'dl_admin_approve' && $value == 'ON') {
                $id = abs(substr($what, 16));
                $request = $smcFunc['db_query']('', '
					SELECT category FROM {db_prefix}tp_dlmanager 
					WHERE id = {int:item}', array('item' => $id));
                if ($smcFunc['db_num_rows']($request) > 0) {
                    $row = $smcFunc['db_fetch_row']($request);
                    $newcat = abs($row[0]);
                    $smcFunc['db_query']('', '
						UPDATE {db_prefix}tp_dlmanager 
						SET category = {int:cat} 
						WHERE id = {int:item}', array('cat' => $newcat, 'item' => $id));
                    $smcFunc['db_query']('', '
						DELETE FROM {db_prefix}tp_variables 
						WHERE type = {string:type} 
						AND value5 = {int:val5}', array('type' => 'dl_not_approved', 'val5' => $id));
                    $smcFunc['db_free_result']($request);
                }
            } elseif (substr($what, 0, 16) == 'dladmin_category') {
                $id = substr($what, 16);
                // update, but not on negative values :)
                if ($value > 0) {
                    $smcFunc['db_query']('', '
						UPDATE {db_prefix}tp_dlmanager 
						SET category = {int:cat} 
						WHERE id = {int:item}', array('cat' => $value, 'item' => $id));
                }
            } elseif (substr($what, 0, 14) == 'dladmin_parent') {
                $id = substr($what, 14);
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}tp_dlmanager 
					SET parent = {int:parent}
					WHERE id = {int:item}', array('parent' => $value, 'item' => $id));
            } elseif (substr($what, 0, 15) == 'dladmin_subitem') {
                $id = substr($what, 15);
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}tp_dlmanager 
					SET subitem = {int:sub}
					WHERE id = {int:item}', array('sub' => $value, 'item' => $id));
            } elseif (substr($what, 0, 11) == 'tp_dlcatpos') {
                $id = substr($what, 11);
                if (!empty($_POST['admineditcatval'])) {
                    $myid = $_POST['admineditcatval'];
                    $go = 4;
                }
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}tp_dlmanager 
					SET downloads = {int:down}
					WHERE id = {int:item}', array('down' => $value, 'item' => $id));
            } elseif (substr($what, 0, 18) == 'dladmin_screenshot') {
                $id = substr($what, 18);
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}tp_dlmanager 
					SET screenshot = {string:ss} 
					WHERE id = {int:item}', array('ss' => $value, 'item' => $id));
            } elseif (substr($what, 0, 12) == 'dladmin_link') {
                $id = substr($what, 12);
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}tp_dlmanager 
					SET link = {string:link} 
					WHERE id = {int:item}', array('link' => $value, 'item' => $id));
            } elseif (substr($what, 0, 12) == 'dladmin_file' && !isset($new_upload)) {
                $id = substr($what, 12);
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}tp_dlmanager 
					SET file = {string:file}
					WHERE id = {int:item}', array('file' => $value, 'item' => $id));
                $myid = $id;
                $go = 2;
            } elseif (substr($what, 0, 12) == 'dladmin_size' && !isset($new_upload)) {
                $id = substr($what, 12);
                // check the actual size
                $name = $_POST['dladmin_file' . $id];
                $value = filesize($boarddir . '/tp-downloads/' . $name);
                if (!is_numeric($value)) {
                    $value = 0;
                }
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}tp_dlmanager 
					SET filesize = {int:size}
					WHERE id = {int:item}', array('size' => $value, 'item' => $id));
            } elseif ($what == 'tp_dl_allowed_types') {
                $changeArray['dl_allowed_types'] = $value;
                $go = 1;
            } elseif ($what == 'tp_dl_usescreenshot') {
                $changeArray['dl_usescreenshot'] = $value;
                $go = 1;
            } elseif (substr($what, 0, 20) == 'tp_dl_screenshotsize') {
                // which one
                $who = substr($what, 20);
                $result = $smcFunc['db_query']('', '
					SELECT value FROM {db_prefix}tp_settings 
					WHERE name = {string:name} LIMIT 1', array('name' => 'dl_screenshotsizes'));
                $row = $smcFunc['db_fetch_assoc']($result);
                $smcFunc['db_free_result']($result);
                $all = explode(',', $row['value']);
                $all[$who] = $value;
                $changeArray['dl_screenshotsizes'] = implode(',', $all);
                $go = 1;
            } elseif ($what == 'tp_dl_showfeatured') {
                $changeArray['dl_showfeatured'] = $value;
                $go = 1;
            } elseif ($what == 'tp_dl_wysiwyg') {
                $changeArray['dl_wysiwyg'] = $value;
                $go = 1;
            } elseif ($what == 'tp_dl_showrecent') {
                $changeArray['dl_showlatest'] = $value;
                $go = 1;
            } elseif ($what == 'tp_dl_showstats') {
                $changeArray['dl_showstats'] = $value;
                $go = 1;
            } elseif ($what == 'tp_dl_showcategorytext') {
                $changeArray['dl_showcategorylist'] = $value;
                $go = 1;
            } elseif ($what == 'tp_dl_featured') {
                $changeArray['dl_featured'] = $value;
                $go = 1;
            } elseif ($what == 'tp_dl_introtext') {
                if ($context['TPortal']['dl_wysiwyg'] == 'bbc') {
                    // If we came from WYSIWYG then turn it back into BBC regardless.
                    if (!empty($_REQUEST['tp_dl_introtext']) && isset($_REQUEST['tp_dl_introtext'])) {
                        require_once $sourcedir . '/Subs-Editor.php';
                        $_REQUEST['tp_dl_introtext'] = html_to_bbc($_REQUEST['tp_dl_introtext']);
                        // We need to unhtml it now as it gets done shortly.
                        $_REQUEST['tp_dl_introtext'] = un_htmlspecialchars($_REQUEST['tp_dl_introtext']);
                        // We need this for everything else.
                        $value = $_POST['tp_dl_introtext'] = $_REQUEST['tp_dl_introtext'];
                    }
                }
                $changeArray['dl_introtext'] = trim($value);
                $go = 1;
            } elseif ($what == 'tp_dluploadsize') {
                $changeArray['dl_max_upload_size'] = $value;
                $go = 1;
            } elseif ($what == 'tp_dl_approveonly') {
                $changeArray['dl_approve'] = $value;
                $go = 1;
            } elseif ($what == 'tp_dlallowupload') {
                $changeArray['dl_allow_upload'] = $value;
                $go = 1;
            } elseif ($what == 'tp_dl_fileprefix') {
                $changeArray['dl_fileprefix'] = $value;
                $go = 1;
            } elseif ($what == 'tp_dltheme') {
                $changeArray['dlmanager_theme'] = $value;
                $go = 1;
            }
        }
        // Update all the changes settings finally
        updateTPSettings($changeArray);
        // if we came from useredit screen..
        if (isset($_POST['dl_useredit'])) {
            redirectexit('action=tpmod;dl=useredit' . $_POST['dl_useredit']);
        }
        if (!empty($newgo)) {
            $go = $newgo;
        }
        // guess not, admin screen then
        if ($go == 1) {
            redirectexit('action=tpmod;dl=adminsettings');
        } elseif ($go == 2) {
            redirectexit('action=tpmod;dl=adminitem' . $myid);
        } elseif ($go == 3) {
            redirectexit('action=tpmod;dl=admineditcat' . $myid);
        } elseif ($go == 4) {
            redirectexit('action=tpmod;dl=admincat' . $myid);
        }
    }
    // ****************
    TP_dlgeticons();
    // get all themes
    $context['TPthemes'] = array();
    $request = $smcFunc['db_query']('', '
		SELECT value AS name, id_theme as ID_THEME
		FROM {db_prefix}themes
		WHERE variable = {string:var}
		AND id_member = {int:id_mem}
		ORDER BY value ASC', array('var' => 'name', 'id_mem' => 0));
    if ($smcFunc['db_num_rows']($request) > 0) {
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $context['TPthemes'][] = array('id' => $row['ID_THEME'], 'name' => $row['name']);
        }
        $smcFunc['db_free_result']($request);
    }
    // fetch all files from tp-downloads
    $context['TPortal']['tp-downloads'] = array();
    $count = 1;
    if ($handle = opendir($boarddir . '/tp-downloads')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != '.' && $file != '..' && $file != '.htaccess' && $file != 'icons') {
                $size = floor(filesize($boarddir . '/tp-downloads/' . $file) / 102.4) / 10;
                $context['TPortal']['tp-downloads'][$count] = array('id' => $count, 'file' => $file, 'size' => $size);
                $count++;
            }
        }
        closedir($handle);
    }
    // get all membergroups for permissions
    $context['TPortal']['dlgroups'] = get_grps(true, true);
    //fetch all categories
    $sorted = array();
    $context['TPortal']['linkcats'] = array();
    $srequest = $smcFunc['db_query']('', '
		SELECT id, name, description, icon, access, parent 
		FROM {db_prefix}tp_dlmanager 
		WHERE type = {string:type} ORDER BY downloads ASC', array('type' => 'dlcat'));
    if ($smcFunc['db_num_rows']($srequest) > 0) {
        while ($row = $smcFunc['db_fetch_assoc']($srequest)) {
            // for the linktree
            $context['TPortal']['linkcats'][$row['id']] = array('id' => $row['id'], 'name' => $row['name'], 'parent' => $row['parent']);
            $sorted[$row['id']] = array('id' => $row['id'], 'parent' => $row['parent'], 'name' => $row['name'], 'text' => $row['description'], 'icon' => $row['icon']);
        }
        $smcFunc['db_free_result']($srequest);
    }
    // sort them
    if (count($sorted) > 1) {
        $context['TPortal']['admuploadcats'] = chain('id', 'parent', 'name', $sorted);
    } else {
        $context['TPortal']['admuploadcats'] = $sorted;
    }
    $context['TPortal']['dl_admcats'] = array();
    $context['TPortal']['dl_admcats2'] = array();
    $context['TPortal']['dl_admitems'] = array();
    $context['TPortal']['dl_admcount'] = array();
    $context['TPortal']['dl_admsubmitted'] = array();
    $context['TPortal']['dl_allitems'] = array();
    // count items in each category
    $request = $smcFunc['db_query']('', '
		SELECT file, category 
		FROM {db_prefix}tp_dlmanager 
		WHERE type = {string:type}', array('type' => 'dlitem'));
    if ($smcFunc['db_num_rows']($request) > 0) {
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            if ($row['category'] < 0) {
                if (isset($context['TPortal']['dl_admsubmitted'][abs($row['category'])])) {
                    $context['TPortal']['dl_admsubmitted'][abs($row['category'])]++;
                } else {
                    $context['TPortal']['dl_admsubmitted'][abs($row['category'])] = 1;
                }
            } else {
                if (isset($context['TPortal']['dl_admcount'][$row['category']])) {
                    $context['TPortal']['dl_admcount'][$row['category']]++;
                } else {
                    $context['TPortal']['dl_admcount'][$row['category']] = 1;
                }
            }
            $context['TPortal']['dl_allitems'][] = $row['file'];
        }
        $smcFunc['db_free_result']($request);
    }
    // fetch all categories
    $admsub = substr($context['TPortal']['dlsub'], 5);
    if ($admsub == '') {
        $context['TPortal']['dl_title'] = $txt['tp-dladmin'];
        // fetch all categories with subcats
        $req = $smcFunc['db_query']('', '
			SELECT * FROM {db_prefix}tp_dlmanager 
			WHERE type = {string:type} 
			ORDER BY downloads ASC', array('type' => 'dlcat'));
        if ($smcFunc['db_num_rows']($req) > 0) {
            while ($brow = $smcFunc['db_fetch_assoc']($req)) {
                if (isset($context['TPortal']['dl_admcount'][$brow['id']])) {
                    $items = $context['TPortal']['dl_admcount'][$brow['id']];
                } else {
                    $items = 0;
                }
                if (isset($context['TPortal']['dl_admsubmitted'][$brow['id']])) {
                    $sitems = $context['TPortal']['dl_admsubmitted'][$brow['id']];
                } else {
                    $sitems = 0;
                }
                $context['TPortal']['admcats'][] = array('id' => $brow['id'], 'name' => $brow['name'], 'icon' => $brow['icon'], 'access' => $brow['access'], 'parent' => $brow['parent'], 'description' => $brow['description'], 'shortname' => $brow['link'], 'items' => $items, 'submitted' => $sitems, 'total' => $items + $sitems, 'href' => $scripturl . '?action=tpmod;dl=admincat' . $brow['id'], 'href2' => $scripturl . '?action=tpmod;dl=admineditcat' . $brow['id'], 'href3' => $scripturl . '?action=tpmod;dl=admindelcat' . $brow['id'], 'pos' => $brow['downloads']);
            }
            $smcFunc['db_free_result']($req);
        }
    } elseif (substr($admsub, 0, 3) == 'cat') {
        $cat = substr($admsub, 3);
        // get the parent first
        $request = $smcFunc['db_query']('', '
			SELECT parent, name, link 
			FROM {db_prefix}tp_dlmanager 
			WHERE type = {string:type}
			AND id = {int:item}', array('type' => 'dlcat', 'item' => $cat));
        if ($smcFunc['db_num_rows']($request) > 0) {
            $row = $smcFunc['db_fetch_assoc']($request);
            $catparent = abs($row['parent']);
            $catname = $row['name'];
            $catshortname = $row['link'];
            $smcFunc['db_free_result']($request);
        }
        // fetch items within a category
        $request = $smcFunc['db_query']('', '
			SELECT dl.*, dl.author_id as authorID,m.real_name as realName
			FROM ({db_prefix}tp_dlmanager AS dl, {db_prefix}members AS m)
			WHERE abs(dl.category) = {int:cat}
			AND dl.type = {string:type}
			AND dl.subitem = {int:sub}
			AND dl.author_id = m.id_member
			ORDER BY dl.id DESC', array('cat' => $cat, 'type' => 'dlitem', 'sub' => 0));
        if ($smcFunc['db_num_rows']($request) > 0) {
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                $context['TPortal']['dl_admitems'][] = array('id' => $row['id'], 'name' => $row['name'], 'icon' => $row['icon'], 'category' => abs($row['category']), 'file' => $row['file'], 'filesize' => floor($row['filesize'] / 1024), 'views' => $row['views'], 'authorID' => $row['authorID'], 'author' => '<a href="' . $scripturl . '?action=profile;u=' . $row['authorID'] . '">' . $row['realName'] . '</a>', 'created' => timeformat($row['created']), 'last_access' => timeformat($row['last_access']), 'description' => $row['description'], 'downloads' => $row['downloads'], 'sshot' => $row['screenshot'], 'link' => $row['link'], 'href' => $scripturl . '?action=tpmod;dl=adminitem' . $row['id'], 'approved' => $row['category'] < 0 ? '0' : '1', 'approve' => $scripturl . '?action=tpmod;dl=adminapprove' . $row['id']);
            }
            $smcFunc['db_free_result']($request);
        }
        // fetch all categories with subcats
        $request = $smcFunc['db_query']('', '
			SELECT * FROM {db_prefix}tp_dlmanager 
			WHERE type = {string:type}
			ORDER BY name ASC', array('type' => 'dlcat'));
        if ($smcFunc['db_num_rows']($request) > 0) {
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                if (isset($context['TPortal']['dl_admcount'][$row['id']])) {
                    $items = $context['TPortal']['dl_admcount'][$row['id']];
                } else {
                    $items = 0;
                }
                if (isset($context['TPortal']['dl_admsubmitted'][$row['id']])) {
                    $sitems = $context['TPortal']['dl_admsubmitted'][$row['id']];
                } else {
                    $sitems = 0;
                }
                $context['TPortal']['admcats'][] = array('id' => $row['id'], 'name' => $row['name'], 'pos' => $row['downloads'], 'icon' => $row['icon'], 'shortname' => $row['link'], 'access' => $row['access'], 'parent' => $row['parent'], 'description' => $row['description'], 'items' => $items, 'submitted' => $sitems, 'total' => $items + $sitems, 'href' => $scripturl . '?action=tpmod;dl=admincat' . $row['id'], 'href2' => $scripturl . '?action=tpmod;dl=admineditcat' . $row['id'], 'href3' => $scripturl . '?action=tpmod;dl=admindelcat' . $row['id']);
            }
            $smcFunc['db_free_result']($request);
        }
        // check to see if its child
        $parents = array();
        while ($catparent > 0) {
            $parents[$catparent] = array('id' => $catparent, 'name' => $context['TPortal']['linkcats'][$catparent]['name'], 'parent' => $context['TPortal']['linkcats'][$catparent]['parent']);
            $catparent = $context['TPortal']['linkcats'][$catparent]['parent'];
        }
        // make the linktree
        TPadd_linktree($scripturl . '?action=tpmod;dl=admin', $txt['tp-dladmin']);
        if (isset($parents)) {
            $parts = array_reverse($parents, TRUE);
            // add to the linktree
            foreach ($parts as $parent) {
                TPadd_linktree($scripturl . '?action=tpmod;dl=admincat' . $parent['id'], $parent['name']);
            }
        }
        // add to the linktree
        TPadd_linktree($scripturl . '?action=tpmod;dl=admincat' . $cat, $catname);
    } elseif ($context['TPortal']['dlsub'] == 'adminsubmission') {
        // check any submissions if admin
        $submitted = array();
        isAllowedTo('tp_dlmanager');
        $context['TPortal']['dl_admitems'] = array();
        $request = $smcFunc['db_query']('', '
			SELECT dl.id, dl.name, dl.file, dl.created, dl.filesize, dl.author_id as authorID, m.real_name as realName
			FROM ({db_prefix}tp_dlmanager AS dl, {db_prefix}members AS m)
			WHERE dl.type = {string:type}
			AND dl.category < 0
			AND dl.author_id = m.id_member', array('type' => 'dlitem'));
        if ($smcFunc['db_num_rows']($request) > 0) {
            $rows = $smcFunc['db_num_rows']($request);
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                $context['TPortal']['dl_admitems'][] = array('id' => $row['id'], 'name' => $row['name'], 'file' => $row['file'], 'filesize' => floor($row['filesize'] / 1024), 'href' => $scripturl . '?action=tpmod;dl=adminitem' . $row['id'], 'author' => '<a href="' . $scripturl . '?action=profile;u=' . $row['authorID'] . '">' . $row['realName'] . '</a>', 'date' => timeformat($row['created']));
                $submitted[] = $row['id'];
            }
            $smcFunc['db_free_result']($request);
        }
        // check that submissions link to downloads
        $request = $smcFunc['db_query']('', '
			SELECT id,value5 FROM {db_prefix}tp_variables 
			WHERE type = {string:type}', array('type' => 'dl_not_approved'));
        if ($smcFunc['db_num_rows']($request) > 0) {
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                $what = $row['id'];
                if (!in_array($row['value5'], $submitted)) {
                    $smcFunc['db_query']('', '
						DELETE FROM {db_prefix}tp_variables 
						WHERE id = {int:item}', array('item' => $what));
                }
            }
            $smcFunc['db_free_result']($request);
        }
    } elseif (substr($admsub, 0, 7) == 'editcat') {
        $context['TPortal']['dl_title'] = '<a href="' . $scripturl . '?action=tpmod;dl=admin">' . $txt['tp-dladmin'] . '</a>';
        $cat = substr($admsub, 7);
        // edit category
        $request = $smcFunc['db_query']('', '
			SELECT * FROM {db_prefix}tp_dlmanager 
			WHERE id = {int:item} 
			AND type = {string:type} LIMIT 1', array('item' => $cat, 'type' => 'dlcat'));
        if ($smcFunc['db_num_rows']($request) > 0) {
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                $context['TPortal']['admcats'][] = array('id' => $row['id'], 'name' => $row['name'], 'access' => $row['access'], 'shortname' => $row['link'], 'description' => $row['description'], 'icon' => $row['icon'], 'parent' => $row['parent']);
            }
            $smcFunc['db_free_result']($request);
        }
        if ($context['TPortal']['dl_wysiwyg'] == 'bbc') {
            $context['TPortal']['editor_id'] = 'dladmin_text' . $context['TPortal']['admcats'][0]['id'];
            TP_prebbcbox($context['TPortal']['editor_id'], $context['TPortal']['admcats'][0]['description']);
        }
    } elseif (substr($admsub, 0, 6) == 'delcat') {
        $context['TPortal']['dl_title'] = '<a href="' . $scripturl . '?action=tpmod;dl=admin">' . $txt['tp-dladmin'] . '</a>';
        $cat = substr($admsub, 6);
        // delete category and all item it's in
        $request = $smcFunc['db_query']('', '
			DELETE FROM {db_prefix}tp_dlmanager 
			WHERE type = {string:type}
			AND category = {int:cat}', array('type' => 'dlitem', 'cat' => $cat));
        $request = $smcFunc['db_query']('', '
			DELETE FROM {db_prefix}tp_dlmanager 
			WHERE id = {int:cat} LIMIT 1', array('cat' => $cat));
        redirectexit('action=tpmod;dl=admin');
    } elseif (substr($admsub, 0, 8) == 'settings') {
        $context['TPortal']['dl_title'] = $txt['tp-dlsettings'];
    } elseif (substr($admsub, 0, 4) == 'item') {
        $item = substr($admsub, 4);
        $request = $smcFunc['db_query']('', '
			SELECT * FROM {db_prefix}tp_dlmanager 
			WHERE id = {int:item} 
			AND type = {string:type} LIMIT 1', array('item' => $item, 'type' => 'dlitem'));
        if ($smcFunc['db_num_rows']($request) > 0) {
            $row = $smcFunc['db_fetch_assoc']($request);
            // is it actually a subitem?
            if ($row['subitem'] > 0) {
                redirectexit('action=tpmod;dl=adminitem' . $row['subitem']);
            }
            // Add in BBC editor before we call in template so the headers are there
            if ($context['TPortal']['dl_wysiwyg'] == 'bbc') {
                $context['TPortal']['editor_id'] = 'dladmin_text' . $item;
                TP_prebbcbox($context['TPortal']['editor_id'], $row['description']);
            }
            // get all items for a list
            $context['TPortal']['admitems'] = array();
            $itemlist = $smcFunc['db_query']('', '
				SELECT id, name FROM {db_prefix}tp_dlmanager 
				WHERE id != {int:item} 
				AND type = {string:type} 
				AND subitem = 0 
				ORDER BY name ASC', array('item' => $item, 'type' => 'dlitem'));
            if ($smcFunc['db_num_rows']($itemlist) > 0) {
                while ($ilist = $smcFunc['db_fetch_assoc']($itemlist)) {
                    $context['TPortal']['admitems'][] = array('id' => $ilist['id'], 'name' => $ilist['name']);
                }
            }
            // Any additional files then..?
            $subitem = $row['id'];
            $fdata = array();
            $fetch = $smcFunc['db_query']('', '
				SELECT id, name, file, downloads, filesize, created
				FROM {db_prefix}tp_dlmanager
				WHERE type = {string:type}
				AND subitem = {int:sub}', array('type' => 'dlitem', 'sub' => $subitem));
            if ($smcFunc['db_num_rows']($fetch) > 0) {
                while ($frow = $smcFunc['db_fetch_assoc']($fetch)) {
                    if ($context['TPortal']['dl_fileprefix'] == 'K') {
                        $ffs = ceil($row['filesize'] / 1000) . ' Kb';
                    } elseif ($context['TPortal']['dl_fileprefix'] == 'M') {
                        $ffs = ceil($row['filesize'] / 1000) / 1000 . ' Mb';
                    } elseif ($context['TPortal']['dl_fileprefix'] == 'G') {
                        $ffs = ceil($row['filesize'] / 1000000) / 1000 . ' Gb';
                    }
                    $fdata[] = array('id' => $frow['id'], 'name' => $frow['name'], 'file' => $frow['file'], 'href' => $scripturl . '?action=tpmod;dl=item' . $frow['id'], 'downloads' => $frow['downloads'], 'created' => $frow['created'], 'filesize' => $ffs);
                }
                $smcFunc['db_free_result']($fetch);
            }
            if (!empty($row['screenshot'])) {
                if (substr($row['screenshot'], 0, 10) == 'tp-images/') {
                    $sshot = $boardurl . '/' . $row['screenshot'];
                } else {
                    $sshot = $boardurl . '/tp-images/dlmanager/listing/' . $row['screenshot'];
                }
            }
            $context['TPortal']['dl_admitems'][] = array('id' => $row['id'], 'name' => $row['name'], 'icon' => $row['icon'], 'category' => $row['category'], 'file' => $row['file'], 'views' => $row['views'], 'authorID' => $row['author_id'], 'description' => $row['description'], 'created' => timeformat($row['created']), 'last_access' => timeformat($row['last_access']), 'filesize' => substr($row['file'], 14) != '- empty item -' ? floor(filesize($boarddir . '/tp-downloads/' . $row['file']) / 1024) : '0', 'downloads' => $row['downloads'], 'sshot' => !empty($sshot) ? $sshot : '', 'screenshot' => $row['screenshot'], 'link' => $row['link'], 'href' => $scripturl . '?action=tpmod;dl=adminitem' . $row['id'], 'approved' => $row['category'] < 0 ? '0' : '1', 'approve' => $scripturl . '?action=tpmod;dl=adminitem' . $row['id'], 'subitem' => $fdata);
            $authorID = $row['author_id'];
            $catparent = $row['category'];
            $itemname = $row['name'];
            $smcFunc['db_free_result']($request);
            $request = $smcFunc['db_query']('', '
				SELECT mem.real_name as realName 
				FROM {db_prefix}members as mem 
				WHERE mem.id_member = {int:id_mem}', array('id_mem' => $authorID));
            if ($smcFunc['db_num_rows']($request) > 0) {
                $row = $smcFunc['db_fetch_assoc']($request);
                $context['TPortal']['admcurrent']['member'] = $row['realName'];
                $smcFunc['db_free_result']($request);
            } else {
                $context['TPortal']['admcurrent']['member'] = '-' . $txt['guest_title'] . '-';
            }
        }
        // check to see if its child
        $parents = array();
        while ($catparent > 0) {
            $parents[$catparent] = array('id' => $catparent, 'name' => $context['TPortal']['linkcats'][$catparent]['name'], 'parent' => $context['TPortal']['linkcats'][$catparent]['parent']);
            $catparent = $context['TPortal']['linkcats'][$catparent]['parent'];
        }
        // make the linktree
        TPadd_linktree($scripturl . '?action=tpmod;dl=admin', $txt['tp-dldownloads']);
        if (isset($parents)) {
            $parts = array_reverse($parents, TRUE);
            // add to the linktree
            foreach ($parts as $parent) {
                TPadd_linktree($scripturl . '?action=tpmod;dl=admincat' . $parent['id'], $parent['name']);
            }
        }
        // add to the linktree
        TPadd_linktree($scripturl . '?action=tpmod;dl=adminitem' . $item, $itemname);
    }
    loadTemplate('TPdladmin');
    if (loadLanguage('TPmodules') == false) {
        loadLanguage('TPmodules', 'english');
    }
    if (loadLanguage('TPortalAdmin') == false) {
        loadLanguage('TPortalAdmin', 'english');
    }
    // setup admin tabs according to subaction
    $context['admin_area'] = 'tp_dlmanager';
    $context['admin_tabs'] = array('title' => $txt['tp-dlheader1'], 'help' => $txt['tp-dlheader2'], 'description' => $txt['tp-dlheader3'], 'tabs' => array());
    if (allowedTo('tp_dlmanager')) {
        $context['TPortal']['subtabs'] = array('admin' => array('text' => 'tp-dltabs4', 'url' => $scripturl . '?action=tpmod;dl=admin', 'active' => substr($context['TPortal']['dlsub'], 0, 5) == 'admin' && $context['TPortal']['dlsub'] != 'adminsettings' && $context['TPortal']['dlsub'] != 'adminaddcat' && $context['TPortal']['dlsub'] != 'adminftp' && $context['TPortal']['dlsub'] != 'adminsubmission'), 'settings' => array('text' => 'tp-dltabs1', 'url' => $scripturl . '?action=tpmod;dl=adminsettings', 'active' => $context['TPortal']['dlsub'] == 'adminsettings'), 'addcategory' => array('text' => 'tp-dltabs2', 'url' => $scripturl . '?action=tpmod;dl=adminaddcat', 'active' => $context['TPortal']['dlsub'] == 'adminaddcat'), 'upload' => array('text' => 'tp-dltabs3', 'url' => $scripturl . '?action=tpmod;dl=upload', 'active' => $context['TPortal']['dlsub'] == 'upload'), 'submissions' => array('text' => 'tp-dlsubmissions', 'url' => $scripturl . '?action=tpmod;dl=adminsubmission', 'active' => $context['TPortal']['dlsub'] == 'adminsubmission'), 'ftp' => array('text' => 'tp-dlftp', 'url' => $scripturl . '?action=tpmod;dl=adminftp', 'active' => $context['TPortal']['dlsub'] == 'adminftp'));
    }
    $context['template_layers'][] = 'tpadm';
    $context['template_layers'][] = 'subtab';
    TPadminIndex('');
    $context['current_action'] = 'admin';
}
function TPortalAdmin()
{
    global $scripturl, $sourcedir, $context;
    if (loadLanguage('TPortalAdmin') == false) {
        loadLanguage('TPortalAdmin', 'english');
    }
    if (loadLanguage('TPortal') == false) {
        loadLanguage('TPortal', 'english');
    }
    require_once $sourcedir . '/TPcommon.php';
    require_once $sourcedir . '/Subs-Post.php';
    $context['TPortal']['frontpage_visualopts_admin'] = array('left' => 0, 'right' => 0, 'center' => 0, 'top' => 0, 'bottom' => 0, 'lower' => 0, 'header' => 0, 'nolayer' => 0, 'sort' => 'date', 'sortorder' => 'desc');
    $w = explode(',', $context['TPortal']['frontpage_visual']);
    if (in_array('left', $w)) {
        $context['TPortal']['frontpage_visualopts_admin']['left'] = 1;
    }
    if (in_array('right', $w)) {
        $context['TPortal']['frontpage_visualopts_admin']['right'] = 1;
    }
    if (in_array('center', $w)) {
        $context['TPortal']['frontpage_visualopts_admin']['center'] = 1;
    }
    if (in_array('top', $w)) {
        $context['TPortal']['frontpage_visualopts_admin']['top'] = 1;
    }
    if (in_array('bottom', $w)) {
        $context['TPortal']['frontpage_visualopts_admin']['bottom'] = 1;
    }
    if (in_array('lower', $w)) {
        $context['TPortal']['frontpage_visualopts_admin']['lower'] = 1;
    }
    if (in_array('header', $w)) {
        $context['TPortal']['frontpage_visualopts_admin']['header'] = 1;
    }
    if (in_array('nolayer', $w)) {
        $context['TPortal']['frontpage_visualopts_admin']['nolayer'] = 1;
    }
    foreach ($w as $r) {
        if (substr($r, 0, 5) == 'sort_') {
            $context['TPortal']['frontpage_visualopts_admin']['sort'] = substr($r, 5);
        } elseif (substr($r, 0, 10) == 'sortorder_') {
            $context['TPortal']['frontpage_visualopts_admin']['sortorder'] = substr($r, 10);
        }
    }
    // call up the editor
    TPwysiwyg_setup();
    TPadd_linktree($scripturl . '?action=tpadmin', 'TP Admin');
    // some GET values set up
    $context['TPortal']['tpstart'] = isset($_GET['tpstart']) ? $_GET['tpstart'] : 0;
    // a switch to make it clear what is "forum" and not
    $context['TPortal']['not_forum'] = true;
    // get all member groups
    tp_groups();
    // get the layout schemes
    get_catlayouts();
    // get the categories
    get_catnames();
    if (isset($_GET['id'])) {
        $context['TPortal']['subaction_id'] = $_GET['id'];
    }
    // check POST values
    $return = do_postchecks();
    if (!empty($return)) {
        redirectexit('action=tpadmin;sa=' . $return);
    }
    $tpsub = '';
    if (isset($_GET['sa'])) {
        $context['TPortal']['subaction'] = $tpsub = $_GET['sa'];
        if (substr($_GET['sa'], 0, 11) == 'editarticle') {
            $tpsub = 'articles';
            $context['TPortal']['subaction'] = 'editarticle';
        } elseif (substr($_GET['sa'], 0, 11) == 'addarticle_') {
            $tpsub = 'articles';
            $context['TPortal']['subaction'] = $_GET['sa'];
        }
        do_subaction($tpsub);
    } elseif (isset($_GET['blktype']) || isset($_GET['addblock']) || isset($_GET['blockon']) || isset($_GET['blockoff']) || isset($_GET['blockleft']) || isset($_GET['blockright']) || isset($_GET['blockcenter']) || isset($_GET['blocktop']) || isset($_GET['blockbottom']) || isset($_GET['blockfront']) || isset($_GET['blocklower']) || isset($_GET['blockdelete']) || isset($_GET['blockedit']) || isset($_GET['addpos']) || isset($_GET['subpos'])) {
        $context['TPortal']['subaction'] = $tpsub = 'blocks';
        do_blocks($tpsub);
    } elseif (isset($_GET['linkon']) || isset($_GET['linkoff']) || isset($_GET['linkedit']) || isset($_GET['linkdelete']) || isset($_GET['linkdelete'])) {
        $context['TPortal']['subaction'] = $tpsub = 'linkmanager';
        do_menus($tpsub);
    } elseif (isset($_GET['catdelete']) || isset($_GET['artfeat']) || isset($_GET['artfront']) || isset($_GET['artdelete']) || isset($_GET['arton']) || isset($_GET['artoff']) || isset($_GET['artsticky']) || isset($_GET['artlock']) || isset($_GET['catcollapse'])) {
        $context['TPortal']['subaction'] = $tpsub = 'articles';
        do_articles($tpsub);
    } else {
        $context['TPortal']['subaction'] = $tpsub = 'overview';
        do_news($tpsub);
    }
    // done with all POST values, go to the correct screen
    $context['TPortal']['subtabs'] = '';
    if (in_array($tpsub, array('articles', 'addarticle_php', 'addarticle_html', 'addarticle_bbc', 'addarticle_import', 'strays', 'categories', 'addcategory'))) {
        $context['TPortal']['subtabs'] = array('categories' => array('lang' => true, 'text' => 'tp-tabs5', 'url' => $scripturl . '?action=tpadmin;sa=categories', 'active' => $tpsub == 'categories'), 'addcategory' => array('lang' => true, 'text' => 'tp-tabs6', 'url' => $scripturl . '?action=tpadmin;sa=addcategory', 'active' => $tpsub == 'addcategory'), 'articles' => array('lang' => true, 'text' => 'tp-articles', 'url' => $scripturl . '?action=tpadmin;sa=articles', 'active' => ($context['TPortal']['subaction'] == 'articles' || $context['TPortal']['subaction'] == 'editarticle') && $context['TPortal']['subaction'] != 'strays'), 'articles_nocat' => array('lang' => true, 'text' => 'tp-uncategorised', 'url' => $scripturl . '?action=tpadmin;sa=articles;sa=strays', 'active' => $context['TPortal']['subaction'] == 'strays'), 'addarticle' => array('lang' => true, 'text' => 'tp-tabs2', 'url' => $scripturl . '?action=tpadmin;sa=addarticle_html' . (isset($_GET['cu']) ? ';cu=' . $_GET['cu'] : ''), 'active' => $context['TPortal']['subaction'] == 'addarticle_html'), 'addarticle_php' => array('lang' => true, 'text' => 'tp-tabs3', 'url' => $scripturl . '?action=tpadmin;sa=addarticle_php' . (isset($_GET['cu']) ? ';cu=' . $_GET['cu'] : ''), 'active' => $context['TPortal']['subaction'] == 'addarticle_php'), 'addarticle_bbc' => array('lang' => true, 'text' => 'tp-addbbc', 'url' => $scripturl . '?action=tpadmin;sa=addarticle_bbc' . (isset($_GET['cu']) ? ';cu=' . $_GET['cu'] : ''), 'active' => $context['TPortal']['subaction'] == 'addarticle_bbc'), 'article_import' => array('lang' => true, 'text' => 'tp-addimport', 'url' => $scripturl . '?action=tpadmin;sa=addarticle_import' . (isset($_GET['cu']) ? ';cu=' . $_GET['cu'] : ''), 'active' => $context['TPortal']['subaction'] == 'addarticle_import'), 'clist' => array('lang' => true, 'text' => 'tp-tabs11', 'url' => $scripturl . '?action=tpadmin;sa=clist', 'active' => $tpsub == 'clist'));
    } elseif (in_array($tpsub, array('addcategory', 'categories', 'clist'))) {
        $context['TPortal']['subtabs'] = array('categories' => array('lang' => true, 'text' => 'tp-tabs5', 'url' => $scripturl . '?action=tpadmin;sa=categories', 'active' => $tpsub == 'categories'), 'addcategory' => array('lang' => true, 'text' => 'tp-tabs6', 'url' => $scripturl . '?action=tpadmin;sa=addcategory', 'active' => $tpsub == 'addcategory'), 'clist' => array('lang' => true, 'text' => 'tp-tabs11', 'url' => $scripturl . '?action=tpadmin;sa=clist', 'active' => $tpsub == 'clist'));
    } elseif (in_array($tpsub, array('blocks', 'panels'))) {
        $context['TPortal']['subtabs'] = array('blocks' => array('lang' => true, 'text' => 'tp-blocks', 'url' => $scripturl . '?action=tpadmin;sa=blocks', 'active' => $tpsub == 'blocks' && !isset($_GET['overview'])), 'panels' => array('lang' => true, 'text' => 'tp-panels', 'url' => $scripturl . '?action=tpadmin;sa=panels', 'active' => $tpsub == 'panels'), 'blockoverview' => array('lang' => true, 'text' => 'tp-blockoverview', 'url' => $scripturl . '?action=tpadmin;sa=blocks;overview', 'active' => $tpsub == 'blocks' && isset($_GET['overview'])));
    }
    // TP Admin menu layer
    $context['template_layers'][] = 'tpadm';
    // Shows subtab layer above for admin submenu links
    $context['template_layers'][] = 'subtab';
    loadTemplate('TPortalAdmin');
    TPadminIndex($tpsub);
}
Beispiel #3
0
function tpshout_admin()
{
    global $context, $scripturl, $txt, $smcFunc, $sourcedir;
    // check permissions
    isAllowedTo('tp_can_admin_shout');
    if (!isset($context['tp_panels'])) {
        $context['tp_panels'] = array();
    }
    if (isset($_GET['p']) && is_numeric($_GET['p'])) {
        $tpstart = $_GET['p'];
    } else {
        $tpstart = 0;
    }
    require_once $sourcedir . '/Subs-Post.php';
    loadtemplate('TPShout');
    $context['template_layers'][] = 'tpadm';
    $context['template_layers'][] = 'subtab';
    loadlanguage('TPortalAdmin');
    TPadminIndex('shout', true);
    $context['current_action'] = 'admin';
    if (isset($_REQUEST['send']) || isset($_REQUEST[$txt['tp-send']]) || isset($_REQUEST['tp_preview']) || isset($_REQUEST['TPadmin_blocks'])) {
        $go = 0;
        $changeArray = array();
        foreach ($_POST as $what => $value) {
            if (substr($what, 0, 18) == 'tp_shoutbox_remove') {
                $val = substr($what, 18);
                $smcFunc['db_query']('', '
					DELETE FROM {db_prefix}tp_shoutbox 
					WHERE id = {int:shout}', array('shout' => $val));
                $go = 2;
            } elseif (substr($what, 0, 18) == 'tp_shoutbox_hidden') {
                $val = substr($what, 18);
                if (!empty($_POST['tp_shoutbox_sticky' . $val])) {
                    $value = '1';
                } else {
                    $value = '';
                }
                if (!empty($_POST['tp_shoutbox_sticky_layout' . $val]) && is_numeric($_POST['tp_shoutbox_sticky_layout' . $val])) {
                    $svalue = $_POST['tp_shoutbox_sticky_layout' . $val];
                } else {
                    $svalue = '0';
                }
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}tp_shoutbox 
					SET value6 = "' . $value . '",value8 = "' . $svalue . '"
					WHERE id = {int:shout}', array('shout' => $val));
                $go = 2;
            } elseif ($what == 'tp_shoutsdelall' && $value == 'ON') {
                $smcFunc['db_query']('', '
					DELETE FROM {db_prefix}tp_shoutbox 
					WHERE type = {string:type}', array('type' => 'shoutbox'));
                $go = 2;
            } elseif ($what == 'tp_shoutsunstickall' && $value == 'ON') {
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}tp_shoutbox 
					SET value6 = "0", value8 = "0"
					WHERE 1');
                $go = 2;
            } elseif (substr($what, 0, 16) == 'tp_shoutbox_item') {
                $val = substr($what, 16);
                $bshout = $smcFunc['htmlspecialchars'](substr($value, 0, 300));
                preparsecode($bshout);
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}tp_shoutbox 
					SET value1 = {string:val1}
					WHERE id = {int:val}', array('val1' => $bshout, 'val' => $val));
                $go = 2;
            } else {
                $what = substr($what, 3);
                if ($what == 'shoutbox_smile') {
                    $changeArray['show_shoutbox_smile'] = $value;
                }
                if ($what == 'shoutbox_icons') {
                    $changeArray['show_shoutbox_icons'] = $value;
                }
                if ($what == 'shoutbox_height') {
                    $changeArray['shoutbox_height'] = $value;
                }
                if ($what == 'shoutbox_usescroll') {
                    $changeArray['shoutbox_usescroll'] = $value;
                }
                if ($what == 'shoutbox_scrollduration') {
                    if ($value > 5) {
                        $value = 5;
                    } elseif ($value < 1) {
                        $value = 1;
                    }
                    $changeArray['shoutbox_scrollduration'] = $value;
                }
                if ($what == 'shoutbox_limit') {
                    if (!is_numeric($value)) {
                        $value = 10;
                    }
                    $changeArray['shoutbox_limit'] = $value;
                }
                if ($what == 'shoutbox_refresh') {
                    if (empty($value)) {
                        $value = '0';
                    }
                    $changeArray['shoutbox_refresh'] = $value;
                }
                if ($what == 'show_profile_shouts') {
                    $changeArray['profile_shouts_hide'] = $value;
                }
                if ($what == 'shout_allow_links') {
                    $changeArray['shout_allow_links'] = $value;
                }
                if ($what == 'shoutbox_layout') {
                    $changeArray['shoutbox_layout'] = $value;
                }
                if ($what == 'shout_submit_returnkey') {
                    $changeArray['shout_submit_returnkey'] = $value;
                }
                if ($what == 'shoutbox_stitle') {
                    $changeArray['shoutbox_stitle'] = $value;
                }
            }
        }
        updateTPSettings($changeArray, true);
        if (empty($go)) {
            redirectexit('action=tpmod;shout=admin;settings');
        } else {
            redirectexit('action=tpmod;shout=admin');
        }
    }
    // get latest shouts for admin section
    // check that a member has been filtered
    if (isset($_GET['u'])) {
        $memID = $_GET['u'];
    }
    // check that a IP has been filtered
    if (isset($_GET['ip'])) {
        $ip = $_GET['ip'];
    }
    if (isset($_GET['s'])) {
        $single = $_GET['s'];
    }
    $context['TPortal']['admin_shoutbox_items'] = array();
    if (isset($memID)) {
        $shouts = $smcFunc['db_query']('', '
			SELECT COUNT(*) FROM {db_prefix}tp_shoutbox 
			WHERE type = {string:type} 
			AND value5 = {int:val5} 
			AND value7 = {int:val7}', array('type' => 'shoutbox', 'val5' => $memID, 'val7' => 0));
        $weh = $smcFunc['db_fetch_row']($shouts);
        $smcFunc['db_free_result']($shouts);
        $allshouts = $weh[0];
        $context['TPortal']['admin_shoutbox_items_number'] = $allshouts;
        $context['TPortal']['shoutbox_pageindex'] = 'Member ' . $memID . ' filtered (<a href="' . $scripturl . '?action=tpmod;shout=admin">' . $txt['remove'] . '</a>) <br />' . TPageIndex($scripturl . '?action=tpmod;shout=admin;u=' . $memID, $tpstart, $allshouts, 10, true);
        $request = $smcFunc['db_query']('', '
			SELECT * FROM {db_prefix}tp_shoutbox 
			WHERE type = {string:type} 
			AND value5 = {int:val5} 
			AND value7 = {int:val7} 
			ORDER BY value2 DESC LIMIT {int:start},10', array('type' => 'shoutbox', 'val5' => $memID, 'val7' => 0, 'start' => $tpstart));
    } elseif (isset($ip)) {
        $shouts = $smcFunc['db_query']('', '
			SELECT COUNT(*) FROM {db_prefix}tp_shoutbox 
			WHERE type = {string:type}
			AND value4 = {string:val4} 
			AND value7 = {int:val7}', array('type' => 'shoutbox', 'val4' => $ip, 'val7' => 0));
        $weh = $smcFunc['db_fetch_row']($shouts);
        $smcFunc['db_free_result']($shouts);
        $allshouts = $weh[0];
        $context['TPortal']['admin_shoutbox_items_number'] = $allshouts;
        $context['TPortal']['shoutbox_pageindex'] = 'IP ' . $ip . ' filtered (<a href="' . $scripturl . '?action=tpmod;shout=admin">' . $txt['remove'] . '</a>) <br />' . TPageIndex($scripturl . '?action=tpmod;shout=admin;ip=' . urlencode($ip), $tpstart, $allshouts, 10, true);
        $request = $smcFunc['db_query']('', '
			SELECT * FROM {db_prefix}tp_shoutbox 
			WHERE type = {string:type}
			AND value4 = {string:val4} 
			AND value7 = {int:val7} 
			ORDER BY value2 DESC LIMIT {int:start}, 10', array('type' => 'shoutbox', 'val4' => $ip, 'val7' => 0, 'start' => $tpstart));
    } elseif (isset($single)) {
        // check session
        checkSession('get');
        $context['TPortal']['shoutbox_pageindex'] = '';
        $request = $smcFunc['db_query']('', '
			SELECT * FROM {db_prefix}tp_shoutbox 
			WHERE type = {string:type} 
			AND value7 = {int:val7} 
			AND id = {int:shout}', array('type' => 'shoutbox', 'val7' => 0, 'shout' => $single));
    } else {
        $shouts = $smcFunc['db_query']('', '
			SELECT COUNT(*) FROM {db_prefix}tp_shoutbox 
			WHERE type = {string:type} 
			AND value7 = {int:val7}', array('type' => 'shoutbox', 'val7' => 0));
        $weh = $smcFunc['db_fetch_row']($shouts);
        $smcFunc['db_free_result']($shouts);
        $allshouts = $weh[0];
        $context['TPortal']['admin_shoutbox_items_number'] = $allshouts;
        $context['TPortal']['shoutbox_pageindex'] = TPageIndex($scripturl . '?action=tpmod;shout=admin', $tpstart, $allshouts, 10, true);
        $request = $smcFunc['db_query']('', '
			SELECT * FROM {db_prefix}tp_shoutbox 
			WHERE type = {string:type} 
			AND value7 = {int:val7} 
			ORDER BY value2 DESC LIMIT {int:start}, 10', array('type' => 'shoutbox', 'val7' => 0, 'start' => $tpstart));
    }
    if ($smcFunc['db_num_rows']($request) > 0) {
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $context['TPortal']['admin_shoutbox_items'][] = array('id' => $row['id'], 'body' => html_entity_decode($row['value1'], ENT_QUOTES), 'poster' => $row['value3'], 'timestamp' => $row['value2'], 'time' => timeformat($row['value2']), 'ip' => $row['value4'], 'ID_MEMBER' => $row['value5'], 'sort_member' => '<a href="' . $scripturl . '?action=tpmod;shout=admin;u=' . $row['value5'] . '">' . $txt['tp-allshoutsbymember'] . '</a>', 'sticky' => $row['value6'], 'sticky_layout' => $row['value8'], 'sort_ip' => '<a href="' . $scripturl . '?action=tpmod;shout=admin;ip=' . $row['value4'] . '">' . $txt['tp-allshoutsbyip'] . '</a>', 'single' => isset($single) ? '<hr><a href="' . $scripturl . '?action=tpmod;shout=admin"><b>' . $txt['tp-allshouts'] . '</b></a>' : '');
        }
        $smcFunc['db_free_result']($request);
    }
    $context['TPortal']['subtabs'] = '';
    // setup menu items
    if (allowedTo('tp_can_admin_shout')) {
        $context['TPortal']['subtabs'] = array('shoutbox_settings' => array('text' => 'tp-settings', 'url' => $scripturl . '?action=tpmod;shout=admin;settings', 'active' => isset($_GET['action']) && ($_GET['action'] == 'tpmod' || $_GET['action'] == 'tpadmin') && isset($_GET['shout']) && $_GET['shout'] == 'admin' && isset($_GET['settings']) ? true : false), 'shoutbox' => array('text' => 'tp-tabs10', 'url' => $scripturl . '?action=tpmod;shout=admin', 'active' => isset($_GET['action']) && ($_GET['action'] == 'tpmod' || $_GET['action'] == 'tpadmin') && isset($_GET['shout']) && $_GET['shout'] == 'admin' && !isset($_GET['settings']) ? true : false));
        $context['admin_header']['tp_shout'] = $txt['tp_shout'];
    }
    // on settings screen?
    if (isset($_GET['settings'])) {
        $context['sub_template'] = 'tpshout_admin_settings';
    } else {
        $context['sub_template'] = 'tpshout_admin';
    }
    $context['page_title'] = 'Shoutbox admin';
    tp_hidebars();
}