Ejemplo n.º 1
0
function TPortalDLManager()
{
    global $txt, $scripturl, $boarddir, $boardurl, $context, $settings, $smcFunc;
    // assume its the frontpage initially
    $context['TPortal']['dlaction'] = 'main';
    // is even the manager active?
    if (!$context['TPortal']['show_download']) {
        fatal_error($txt['tp-dlmanageroff']);
    }
    $context['TPortal']['upshrinkpanel'] = '';
    // add visual options to thsi section
    $context['TPortal']['dl_visual'] = array();
    $dl_visual = explode(',', $context['TPortal']['dl_visual_options']);
    $dv = array('left', 'right', 'center', 'lower', 'top', 'bottom');
    foreach ($dv as $v => $val) {
        if ($context['TPortal'][$val . 'panel'] == 1) {
            if (in_array($val, $dl_visual)) {
                $context['TPortal'][$val . 'panel'] = '1';
            } else {
                $context['TPortal'][$val . 'panel'] = '0';
            }
        }
        $context['TPortal']['dl_visual'][$val] = true;
    }
    if (in_array('top', $dl_visual)) {
        $context['TPortal']['showtop'] = '1';
    } else {
        $context['TPortal']['showtop'] = '0';
    }
    // check that you can upload at all
    if (allowedTo('tp_dlupload')) {
        $context['TPortal']['can_upload'] = true;
    } else {
        $context['TPortal']['can_upload'] = false;
    }
    // fetch all files from tp-downloads
    if (isset($_GET['ftp']) && allowedTo('tp_dlmanager')) {
        TP_dlftpfiles();
    }
    // any uploads being sent?
    $context['TPortal']['uploads'] = array();
    if (isset($_FILES['tp-dluploadfile']['tmp_name']) || isset($_POST['tp-dluploadnot'])) {
        // skip the uplaod checks etc . if just an empty item
        if (!isset($_POST['tp-dluploadnot'])) {
            // check if uploaded quick-list picture
            if (isset($_FILES['qup_tp_dluploadtext']) && file_exists($_FILES['qup_tp_dluploadtext']['tmp_name'])) {
                $item_id = isset($_GET['dl']) ? $_GET['dl'] : 'upload';
                $name = TPuploadpicture('qup_tp_dluploadtext', $context['user']['id'] . 'uid');
                tp_createthumb('tp-images/' . $name, 50, 50, 'tp-images/thumbs/thumb_' . $name);
                redirectexit('action=tpmod;dl=' . $item_id);
            }
            // check that nothing happended
            if (!file_exists($_FILES['tp-dluploadfile']['tmp_name']) || !is_uploaded_file($_FILES['tp-dluploadfile']['tmp_name'])) {
                fatal_error($txt['tp-dluploadfailure']);
            }
            // first, can we upload at all?
            if (!$context['TPortal']['can_upload']) {
                unlink($_FILES['tp-dluploadfile']['tmp_name']);
                fatal_error($txt['tp-dluploadnotallowed']);
            }
        }
        // a file it is
        $title = isset($_POST['tp-dluploadtitle']) ? strip_tags($_POST['tp-dluploadtitle']) : $txt['tp-no_title'];
        if ($title == '') {
            $title = $txt['tp-no_title'];
        }
        $text = isset($_POST['tp_dluploadtext']) ? $_POST['tp_dluploadtext'] : '';
        $category = isset($_POST['tp-dluploadcat']) ? (int) $_POST['tp-dluploadcat'] : 0;
        // a screenshot?
        if (file_exists($_FILES['tp_dluploadpic']['tmp_name']) || is_uploaded_file($_FILES['tp_dluploadpic']['tmp_name'])) {
            $shot = true;
        } else {
            $shot = false;
        }
        $icon = !empty($_POST['tp_dluploadicon']) ? $boardurl . '/tp-downloads/icons/' . $_POST['tp_dluploadicon'] : '';
        if (!isset($_POST['tp-dluploadnot'])) {
            // process the file
            $filename = $_FILES['tp-dluploadfile']['name'];
            $name = strtr($filename, 'ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ', 'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy');
            $name = strtr($name, array('Þ' => 'TH', 'þ' => 'th', 'Ð' => 'DH', 'ð' => 'dh', 'ß' => 'ss', 'Œ' => 'OE', 'œ' => 'oe', 'Æ' => 'AE', 'æ' => 'ae', 'µ' => 'u'));
            $name = preg_replace(array('/\\s/', '/[^\\w_\\.\\-]/'), array('_', ''), $name);
        } else {
            $name = '- empty item -';
        }
        if (isset($_POST['tp-dlupload_ftpstray'])) {
            $name = '- empty item - ftp';
        }
        $status = 'normal';
        if (!isset($_POST['tp-dluploadnot'])) {
            // check the size
            $dlfilesize = filesize($_FILES['tp-dluploadfile']['tmp_name']);
            if ($dlfilesize > 1000 * $context['TPortal']['dl_max_upload_size']) {
                $status = 'maxsize';
                unlink($_FILES['tp-dluploadfile']['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);
            }
        } else {
            $dlfilesize = 0;
        }
        if (!isset($_POST['tp-dluploadnot'])) {
            // check the extension
            $allowed = explode(',', $context['TPortal']['dl_allowed_types']);
            $match = false;
            foreach ($allowed as $extension => $value) {
                $ext = '.' . $value;
                $extlen = strlen($ext);
                if (substr($name, strlen($name) - $extlen, $extlen) == $ext) {
                    $match = true;
                }
            }
            if (!$match) {
                $status = 'wrongtype';
                unlink($_FILES['tp-dluploadfile']['tmp_name']);
                $error = $txt['tp-dlexterror'] . ':<b> <br />' . $context['TPortal']['dl_allowed_types'] . '</b><br /><br />' . $txt['tp-dlexterror2'] . ': <b>' . $name . '</b>';
                fatal_error($error);
            }
        }
        // ok, go ahead
        if ($status == 'normal') {
            if (!isset($_POST['tp-dluploadnot'])) {
                // check that no other file exists with same name
                if (file_exists($boarddir . '/tp-downloads/' . $name)) {
                    $name = time() . $name;
                }
                $success = move_uploaded_file($_FILES['tp-dluploadfile']['tmp_name'], $boarddir . '/tp-downloads/' . $name);
            }
            if ($shot) {
                $sfile = 'tp_dluploadpic';
                $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);
            } else {
                if (isset($_POST['tp_dluploadpic_link'])) {
                    $screenshot = $_POST['tp_dluploadpic_link'];
                } else {
                    $screenshot = '';
                }
            }
            // insert it into the database
            $now = time();
            // if all uploads needs to be approved: set category to -category , but not for dl admins
            if ($context['TPortal']['dl_approve'] == '1' && !allowedTo('tp_dlmanager')) {
                $category = $category - $category - $category;
            }
            // get the category access
            $request = $smcFunc['db_query']('', '
				SELECT access FROM {db_prefix}tp_dlmanager WHERE id = {int:cat}', array('cat' => $category));
            if ($smcFunc['db_num_rows']($request) > 0) {
                $row = $smcFunc['db_fetch_assoc']($request);
                $acc = $row['access'];
            } else {
                $acc = '';
            }
            $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($title, $text, $icon, $category, 'dlitem', 0, 1, $name, $now, $now, $dlfilesize, 0, '', '', $context['user']['id'], $screenshot, '', '', 0), array('id'));
            $newitem = $smcFunc['db_insert_id']($request);
            // record the event
            if ($context['TPortal']['dl_approve'] == '1' && allowedTo('tp_dlmanager') || $context['TPortal']['dl_approve'] == '0') {
                tp_recordevent($now, $context['user']['id'], 'tp-createdupload', 'action=tpmod;dl=item' . $newitem, 'Uploaded new file.', $acc, $newitem);
            }
            // should we create a topic?
            if (isset($_POST['create_topic']) && (allowedTo('admin_forum') || !empty($context['TPortal']['dl_create_topic']))) {
                $sticky = false;
                $announce = false;
                // sticky and announce?
                if (isset($_POST['create_topic_sticky'])) {
                    $sticky = true;
                }
                if (isset($_POST['create_topic_announce']) && allowedTo('admin_forum')) {
                    $announce = true;
                }
                if (!empty($_POST['create_topic_board'])) {
                    $brd = $_POST['create_topic_board'];
                }
                if (isset($_POST['create_topic_body'])) {
                    $body = $_POST['create_topic_body'];
                }
                $body .= '[hr][b]' . $txt['tp-download'] . ':[/b][br]' . $scripturl . '?action=tpmod;dl=item' . $newitem;
                // ok, create the topic then
                $top = TP_createtopic($title, $body, 'theme', $brd, $sticky ? 1 : 0, $context['user']['id']);
                // go to announce screen?
                if ($top > 0) {
                    if ($announce) {
                        redirectexit('action=announce;sa=selectgroup;topic=' . $top);
                    } else {
                        redirectexit('topic=' . $top);
                    }
                }
            }
            // put this into submissions - id and type
            if ($category < 0) {
                $smcFunc['db_insert']('INSERT', '{db_prefix}tp_variables', array('value1' => 'string', 'value2' => 'string', 'value3' => 'string', 'type' => 'string', 'value4' => 'string', 'value5' => 'int'), array($title, $now, '', 'dl_not_approved', '', $newitem), array('id'));
                redirectexit('action=tpmod;sub=dlsubmitsuccess');
            } else {
                if (!isset($_POST['tp-dluploadnot'])) {
                    redirectexit('action=tpmod;dl=item' . $newitem);
                } else {
                    redirectexit('action=tpmod;dl=adminitem' . $newitem);
                }
            }
        }
    }
    // ok, on with the show :)
    TP_dluploadcats();
    TP_dlgeticons();
    // showing a category, or even a single item?
    $context['TPortal']['dlaction'] = '';
    if (isset($context['TPortal']['dlsub'])) {
        // a category?
        if (substr($context['TPortal']['dlsub'], 0, 3) == 'cat') {
            $context['TPortal']['dlcat'] = substr($context['TPortal']['dlsub'], 3);
            // check if its a number
            if (is_numeric($context['TPortal']['dlcat'])) {
                $context['TPortal']['dlaction'] = 'cat';
            } else {
                redirectexit('action=tpmod;dl');
            }
        } elseif (substr($context['TPortal']['dlsub'], 0, 4) == 'item') {
            $context['TPortal']['dlitem'] = substr($context['TPortal']['dlsub'], 4);
            if (is_numeric($context['TPortal']['dlitem'])) {
                $item = $context['TPortal']['dlitem'];
                $context['TPortal']['item'] = $item;
                $context['TPortal']['dlaction'] = 'item';
                $request = $smcFunc['db_query']('', '
						SELECT category, subitem 
						FROM {db_prefix}tp_dlmanager 
						WHERE id = {int:dl} AND type = {string:type} LIMIT 1', array('dl' => $item, 'type' => 'dlitem'));
                if ($smcFunc['db_num_rows']($request) > 0) {
                    $row = $smcFunc['db_fetch_assoc']($request);
                    $context['TPortal']['dlcat'] = $row['category'];
                    $smcFunc['db_free_result']($request);
                    // check that it is indeed a main item, if not : redirect to the main one.
                    if ($row['subitem'] > 0) {
                        redirectexit('action=tpmod;dl=item' . $row['subitem']);
                    }
                } else {
                    redirectexit('action=tpmod;dl');
                }
            } else {
                redirectexit('action=tpmod;dl');
            }
        } elseif ($context['TPortal']['dlsub'] == 'stats') {
            $context['TPortal']['dlaction'] = 'stats';
            $context['TPortal']['dlitem'] = '';
        } elseif ($context['TPortal']['dlsub'] == 'search') {
            $context['TPortal']['dlaction'] = 'search';
            $context['TPortal']['dlitem'] = '';
        } elseif ($context['TPortal']['dlsub'] == 'results') {
            $context['TPortal']['dlaction'] = 'results';
            $context['TPortal']['dlitem'] = '';
        } elseif ($context['TPortal']['dlsub'] == 'submission') {
            $context['TPortal']['dlaction'] = 'submission';
            $context['TPortal']['dlitem'] = '';
        } elseif (substr($context['TPortal']['dlsub'], 0, 3) == 'get') {
            $context['TPortal']['dlitem'] = substr($context['TPortal']['dlsub'], 3);
            if (is_numeric($context['TPortal']['dlitem'])) {
                $context['TPortal']['dlaction'] = 'get';
            } else {
                redirectexit('action=tpmod;dl');
            }
        } elseif (substr($context['TPortal']['dlsub'], 0, 6) == 'upload') {
            $context['TPortal']['dlitem'] = substr($context['TPortal']['dlsub'], 6);
            $context['TPortal']['dlaction'] = 'upload';
            // check your permission for uploading
            isAllowedTo('tp_dlupload');
            // Add in BBC editor before we call in template so the headers are there
            if ($context['TPortal']['dl_wysiwyg'] == 'bbc') {
                $context['TPortal']['editor_id'] = 'tp_dluploadtext';
                TP_prebbcbox($context['TPortal']['editor_id']);
            }
            TP_dlgeticons();
            // allow to attach this to another item
            $context['TPortal']['attachitems'] = array();
            if (allowedTo('dlmanager')) {
                // get all items for a list
                $itemlist = $smcFunc['db_query']('', '
					SELECT id, name FROM {db_prefix}tp_dlmanager 
					WHERE type = {string:type} AND subitem = {int:sub} ORDER BY name ASC', array('type' => 'dlitem', 'sub' => 0));
                if ($smcFunc['db_num_rows']($itemlist) > 0) {
                    while ($ilist = $smcFunc['db_fetch_assoc']($itemlist)) {
                        $context['TPortal']['attachitems'][] = array('id' => $ilist['id'], 'name' => $ilist['name']);
                    }
                    $smcFunc['db_free_result']($itemlist);
                }
            } else {
                // how about attaching to one of your own?
                // get all items for a list
                $itemlist = $smcFunc['db_query']('', '
					SELECT id,name FROM {db_prefix}tp_dlmanager 
					WHERE category > {int:cat} 
					AND type = {string:type} 
					AND subitem = {int:sub} 
					AND author_id = {int:auth}
					ORDER BY name ASC', array('cat' => 0, 'type' => 'dlitem', 'sub' => 0, 'auth' => $context['user']['id']));
                if (isset($itemlist) && $smcFunc['db_num_rows']($itemlist) > 0) {
                    while ($ilist = $smcFunc['db_fetch_assoc']($itemlist)) {
                        $context['TPortal']['attachitems'][] = array('id' => $ilist['id'], 'name' => $ilist['name']);
                    }
                    $smcFunc['db_free_result']($itemlist);
                }
            }
            $context['TPortal']['boards'] = array();
            // fetch all boards
            $request = $smcFunc['db_query']('', '
				SELECT b.ID_BOARD, b.name FROM {db_prefix}boards as b
				WHERE {query_see_board}');
            if ($smcFunc['db_num_rows']($request) > 0) {
                while ($row = $smcFunc['db_fetch_assoc']($request)) {
                    $context['TPortal']['boards'][] = array('id' => $row['ID_BOARD'], 'name' => $row['name']);
                }
                $smcFunc['db_free_result']($request);
            }
        } else {
            // check its really exists
            $what = $context['TPortal']['dlsub'];
            $request = $smcFunc['db_query']('', '
				SELECT id FROM {db_prefix}tp_dlmanager 
				WHERE link = {string:link} LIMIT 1', array('link' => $what));
            if (isset($request) && $smcFunc['db_num_rows']($request) > 0) {
                $row = $smcFunc['db_fetch_assoc']($request);
                $context['TPortal']['dlcat'] = $row['id'];
                $context['TPortal']['dlsub'] = 'cat' . $row['id'];
                $context['TPortal']['dlaction'] = 'cat';
                $smcFunc['db_free_result']($request);
            }
        }
    }
    // add to the linktree
    TPadd_linktree($scripturl . '?action=tpmod;dl=0', $txt['tp-downloads']);
    // set the title
    $context['page_title'] = $txt['tp-downloads'];
    $context['TPortal']['dl_title'] = $txt['tp-mainpage'];
    // load the dlmanager frontpage
    if ($context['TPortal']['dlaction'] == '') {
        $context['TPortal']['dlcats'] = array();
        $context['TPortal']['dlcatchilds'] = array();
        // add x most recent and feature the last one
        $context['TPortal']['dl_last_added'] = array();
        $context['TPortal']['dl_most_downloaded'] = array();
        $context['TPortal']['dl_week_downloaded'] = array();
        $mycats = array();
        dl_getcats();
        foreach ($context['TPortal']['dl_allowed_cats'] as $ca) {
            $mycats[] = $ca['id'];
        }
        // empty?
        if (sizeof($mycats) > 0) {
            $request = $smcFunc['db_query']('', '
				SELECT dlm.id, dlm.name, dlm.category, dlm.file, dlm.downloads, dlm.views,
					dlm.author_id as authorID, dlm.created, dlm.screenshot, dlm.filesize,
					dlcat.name AS catname, mem.real_name as realName, LEFT(dlm.description,100) as 	description
				FROM ({db_prefix}tp_dlmanager AS dlm, {db_prefix}members AS mem)
				LEFT JOIN {db_prefix}tp_dlmanager AS dlcat ON (dlcat.id = dlm.category)
				WHERE dlm.type = {string:type}
				AND dlm.category IN ({array_int:cat})
				AND dlm.author_id = mem.id_member
				ORDER BY dlm.created DESC LIMIT 6', array('type' => 'dlitem', 'cat' => $mycats));
            if ($smcFunc['db_num_rows']($request) > 0) {
                while ($row = $smcFunc['db_fetch_assoc']($request)) {
                    $fs = '';
                    if ($context['TPortal']['dl_fileprefix'] == 'K') {
                        $fs = ceil($row['filesize'] / 1000) . ' Kb';
                    } elseif ($context['TPortal']['dl_fileprefix'] == 'M') {
                        $fs = ceil($row['filesize'] / 1000) / 1000 . ' Mb';
                    } elseif ($context['TPortal']['dl_fileprefix'] == 'G') {
                        $fs = ceil($row['filesize'] / 1000000) / 1000 . ' Gb';
                    }
                    if ($context['TPortal']['dl_usescreenshot'] == 1) {
                        if (!empty($row['screenshot'])) {
                            $ico = $boardurl . '/tp-images/dlmanager/thumb/' . $row['screenshot'];
                        } else {
                            $ico = '';
                        }
                    } else {
                        $ico = '';
                    }
                    $context['TPortal']['dl_last_added'][] = array('id' => $row['id'], 'name' => $row['name'], 'category' => $row['category'], 'description' => $context['TPortal']['dl_wysiwyg'] == 'bbc' ? parse_bbc(trim(strip_tags($row['description']))) : $row['description'], 'file' => $row['file'], 'href' => $scripturl . '?action=tpmod;dl=item' . $row['id'], 'downloads' => $row['downloads'], 'views' => $row['views'], 'author' => '<a href="' . $scripturl . '?action=profile;u=' . $row['authorID'] . '">' . $row['realName'] . '</a>', 'authorID' => $row['authorID'], 'date' => timeformat($row['created']), 'screenshot' => $ico, 'catname' => $row['catname'], 'cathref' => $scripturl . '?action=tpmod;dl=cat' . $row['category'], 'filesize' => $fs);
                }
                $smcFunc['db_free_result']($request);
            }
            $request = $smcFunc['db_query']('', '
				SELECT dlm.id, dlm.name, dlm.category, dlm.file, dlm.downloads, dlm.views,
					dlm.author_id as authorID, dlm.created, dlm.filesize, dlcat.name AS catname, 
					mem.real_name as realName
				FROM ({db_prefix}tp_dlmanager AS dlm, {db_prefix}members AS mem)
				LEFT JOIN {db_prefix}tp_dlmanager AS dlcat ON dlcat.id = dlm.category
				WHERE dlm.type = {string:type}
				AND dlm.category IN ({array_string:cat})
				AND dlm.author_id = mem.id_member
				ORDER BY dlm.downloads DESC LIMIT 10', array('type' => 'dlitem', 'cat' => $mycats));
            if ($smcFunc['db_num_rows']($request) > 0) {
                while ($row = $smcFunc['db_fetch_assoc']($request)) {
                    $fs = '';
                    if ($context['TPortal']['dl_fileprefix'] == 'K') {
                        $fs = ceil($row['filesize'] / 1000) . ' Kb';
                    } elseif ($context['TPortal']['dl_fileprefix'] == 'M') {
                        $fs = ceil($row['filesize'] / 1000) / 1000 . ' Mb';
                    } elseif ($context['TPortal']['dl_fileprefix'] == 'G') {
                        $fs = ceil($row['filesize'] / 1000000) / 1000 . ' Gb';
                    }
                    $context['TPortal']['dl_most_downloaded'][] = array('id' => $row['id'], 'name' => $row['name'], 'category' => $row['category'], 'file' => $row['file'], 'href' => $scripturl . '?action=tpmod;dl=item' . $row['id'], 'downloads' => $row['downloads'], 'views' => $row['views'], 'author' => '<a href="' . $scripturl . '?action=profile;u=' . $row['authorID'] . '">' . $row['realName'] . '</a>', 'authorID' => $row['authorID'], 'date' => timeformat($row['created']), 'catname' => $row['catname'], 'cathref' => $scripturl . '?action=tpmod;dl=cat' . $row['category'], 'filesize' => $fs);
                }
                $smcFunc['db_free_result']($request);
            }
            // fetch most downloaded this week
            $now = time();
            $week = (int) date("W", $now);
            $year = (int) date("Y", $now);
            $request = $smcFunc['db_query']('', '
				SELECT dlm.id, dlm.name, dlm.category, dlm.file, data.downloads, dlm.views, 
					dlm.author_id as authorID, dlm.created, dlm.screenshot, dlm.filesize,
					dlcat.name AS catname, mem.real_name as realName
				FROM ({db_prefix}tp_dlmanager AS dlm, {db_prefix}tp_dldata AS data, {db_prefix}members AS mem)
				LEFT JOIN {db_prefix}tp_dlmanager AS dlcat ON dlcat.id = dlm.category
				WHERE dlm.type = {string:type}
				AND dlm.category IN ({array_string:cat})
				AND data.item = dlm.id
				AND data.year = {int:yr}
				AND data.week = {int:week}
				AND dlm.author_id = mem.id_member
				ORDER BY data.downloads DESC LIMIT 10', array('type' => 'dlitem', 'cat' => $mycats, 'yr' => $year, 'week' => $week));
            if ($smcFunc['db_num_rows']($request) > 0) {
                while ($row = $smcFunc['db_fetch_assoc']($request)) {
                    if ($context['TPortal']['dl_usescreenshot'] == 1) {
                        if (!empty($row['screenshot'])) {
                            $ico = $boardurl . '/tp-images/dlmanager/thumb/' . $row['screenshot'];
                        } else {
                            $ico = '';
                        }
                    } else {
                        $ico = '';
                    }
                    $fs = '';
                    if ($context['TPortal']['dl_fileprefix'] == 'K') {
                        $fs = ceil($row['filesize'] / 1000) . ' Kb';
                    } elseif ($context['TPortal']['dl_fileprefix'] == 'M') {
                        $fs = ceil($row['filesize'] / 1000) / 1000 . ' Mb';
                    } elseif ($context['TPortal']['dl_fileprefix'] == 'G') {
                        $fs = ceil($row['filesize'] / 1000000) / 1000 . ' Gb';
                    }
                    $context['TPortal']['dl_week_downloaded'][] = array('id' => $row['id'], 'name' => $row['name'], 'category' => $row['category'], 'file' => $row['file'], 'href' => $scripturl . '?action=tpmod;dl=item' . $row['id'], 'downloads' => $row['downloads'], 'views' => $row['views'], 'author' => '<a href="' . $scripturl . '?action=profile;u=' . $row['authorID'] . '">' . $row['realName'] . '</a>', 'authorID' => $row['authorID'], 'date' => timeformat($row['created']), 'screenshot' => $ico, 'catname' => $row['catname'], 'cathref' => $scripturl . '?action=tpmod;dl=cat' . $row['category'], 'filesize' => $fs);
                }
                $smcFunc['db_free_result']($request);
            }
        }
        // fetch the categories, the number of files
        $request = $smcFunc['db_query']('', '
			SELECT a.access AS access, a.icon AS icon, a.link AS shortname, a.description AS description,
				a.name AS name, a.id AS id, a.parent AS parent,
	  			if (a.id = b.category, count(*), 0) AS files, b.category AS subchild
			FROM ({db_prefix}tp_dlmanager AS a)
			LEFT JOIN {db_prefix}tp_dlmanager AS b ON (a.id = b.category)
			WHERE a.type = {string:type}
		  	GROUP BY a.id
			ORDER BY a.downloads ASC', array('type' => 'dlcat'));
        $fetched_cats = array();
        if ($smcFunc['db_num_rows']($request) > 0) {
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                $show = get_perm($row['access'], 'tp_dlmanager');
                if ($show && $row['parent'] == 0) {
                    $context['TPortal']['dlcats'][$row['id']] = array('id' => $row['id'], 'name' => $row['name'], 'parent' => $row['parent'], 'description' => $context['TPortal']['dl_wysiwyg'] == 'bbc' ? parse_bbc(trim(strip_tags($row['description']))) : $row['description'], 'access' => $row['access'], 'icon' => $row['icon'], 'href' => !empty($row['shortname']) ? $scripturl . '?action=tpmod;dl=' . $row['shortname'] : $scripturl . '?action=tpmod;dl=cat' . $row['id'], 'shortname' => !empty($row['shortname']) ? $row['shortname'] : $row['id'], 'files' => $row['files']);
                    $fetched_cats[] = $row['id'];
                } elseif ($show && $row['parent'] > 0) {
                    $context['TPortal']['dlcatchilds'][] = array('id' => $row['id'], 'name' => $row['name'], 'parent' => $row['parent'], 'href' => $scripturl . '?action=tpmod;dl=cat' . $row['id'], 'files' => $row['files']);
                }
            }
            $smcFunc['db_free_result']($request);
        }
        // add filecount to parent
        foreach ($context['TPortal']['dlcatchilds'] as $child) {
            if (isset($context['TPortal']['dlcats'][$child['parent']]) && $context['TPortal']['dlcats'][$child['parent']]['parent'] == 0) {
                $context['TPortal']['dlcats'][$child['parent']]['files'] = $context['TPortal']['dlcats'][$child['parent']]['files'] + $child['files'];
            }
        }
        // do we need the featured one?
        if (!empty($context['TPortal']['dl_featured'])) {
            // fetch the item data
            $item = $context['TPortal']['dl_featured'];
            $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 dl.type = {string:type}
					AND dl.id = {int:item}
					AND dl.author_id = m.id_member
					LIMIT 1', array('type' => 'dlitem', 'item' => $item));
            if ($smcFunc['db_num_rows']($request) > 0) {
                $row = $smcFunc['db_fetch_assoc']($request);
                if ($context['TPortal']['dl_fileprefix'] == 'K') {
                    $fs = ceil($row['filesize'] / 1000) . ' Kb';
                } elseif ($context['TPortal']['dl_fileprefix'] == 'M') {
                    $fs = ceil($row['filesize'] / 1000) / 1000 . ' Mb';
                } elseif ($context['TPortal']['dl_fileprefix'] == 'G') {
                    $fs = ceil($row['filesize'] / 1000000) / 1000 . ' Gb';
                }
                $rat = array();
                $rating_votes = 0;
                $rat = explode(',', $row['rating']);
                $rating_votes = count($rat);
                if ($row['rating'] == '') {
                    $rating_votes = 0;
                }
                $total = 0;
                foreach ($rat as $mm => $mval) {
                    $total = $total + $mval;
                }
                if ($rating_votes > 0 && $total > 0) {
                    $rating_average = floor($total / $rating_votes);
                } else {
                    $rating_average = 0;
                }
                $decideshot = !empty($row['screenshot']) ? $boardurl . '/' . $row['screenshot'] : '';
                // does it exist?
                if (file_exists($boarddir . '/tp-images/dlmanager/listing/' . $row['screenshot']) && !empty($row['screenshot'])) {
                    $decideshot = $boardurl . '/tp-images/dlmanager/listing/' . $row['screenshot'];
                }
                if ($context['user']['is_logged']) {
                    $can_rate = in_array($context['user']['id'], explode(',', $row['voters'])) ? false : true;
                } else {
                    $can_rate = false;
                }
                $context['TPortal']['featured'] = array('id' => $row['id'], 'name' => $row['name'], 'description' => $context['TPortal']['dl_wysiwyg'] == 'bbc' ? parse_bbc(trim(strip_tags($row['description']))) : $row['description'], 'category' => $row['category'], 'file' => $row['file'], 'href' => $scripturl . '?action=tpmod;dl=item' . $row['id'], 'downloads' => $row['downloads'], 'views' => $row['views'], 'link' => $row['link'], 'date_last' => $row['last_access'], 'author' => $row['realName'], 'authorID' => $row['authorID'], 'screenshot' => $row['screenshot'], 'sshot' => $decideshot, 'icon' => $row['icon'], 'created' => $row['created'], 'filesize' => $fs, 'subitem' => isset($fdata) ? $fdata : '', 'rating_votes' => $rating_votes, 'rating_average' => $rating_average, 'can_rate' => $can_rate);
            }
            $smcFunc['db_free_result']($request);
        }
        $context['TPortal']['dlheader'] = $txt['tp-downloads'];
    } elseif ($context['TPortal']['dlaction'] == 'cat') {
        // check if sorting is specified
        if (isset($_GET['dlsort']) && in_array($_GET['dlsort'], array('id', 'name', 'last_access', 'created', 'downloads', 'author_id'))) {
            $context['TPortal']['dlsort'] = $dlsort = $_GET['dlsort'];
        } else {
            $context['TPortal']['dlsort'] = $dlsort = 'id';
        }
        if (isset($_GET['asc'])) {
            $context['TPortal']['dlsort_way'] = $dlsort_way = 'asc';
        } else {
            $context['TPortal']['dlsort_way'] = $dlsort_way = 'desc';
        }
        $currentcat = $context['TPortal']['dlcat'];
        //fetch all  categories and its childs
        $context['TPortal']['dlcats'] = array();
        $context['TPortal']['dlcatchilds'] = array();
        $context['TPortal']['dl_week_downloaded'] = array();
        // fetch most downloaded this week
        $now = time();
        $week = (int) date("W", $now);
        $year = (int) date("Y", $now);
        $request = $smcFunc['db_query']('', '
			SELECT dlm.id, dlm.name, dlm.category, dlm.file, dlm.downloads, dlm.views, dlm.author_id as authorID, dlm.created, dlm.screenshot, dlm.filesize,
			dlcat.name AS catname, mem.real_name as realName
			FROM ({db_prefix}tp_dlmanager AS dlm, {db_prefix}tp_dldata AS data, {db_prefix}members AS mem)
			LEFT JOIN {db_prefix}tp_dlmanager AS dlcat ON dlcat.id=dlm.category
			WHERE dlm.type = {string:type}
			AND (dlm.category = {int:cat} OR dlm.parent = {int:cat})
			AND data.item = dlm.id
			AND data.year = {int:year}
			AND data.week = {int:week}
			AND dlm.author_id = mem.id_member
			ORDER BY dlm.downloads DESC LIMIT 10', array('type' => 'dlitem', 'cat' => $currentcat, 'year' => $year, 'week' => $week));
        if ($smcFunc['db_num_rows']($request) > 0) {
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                $fs = '';
                if ($context['TPortal']['dl_fileprefix'] == 'K') {
                    $fs = ceil($row['filesize'] / 1000) . ' Kb';
                } elseif ($context['TPortal']['dl_fileprefix'] == 'M') {
                    $fs = ceil($row['filesize'] / 1000) / 1000 . ' Mb';
                } elseif ($context['TPortal']['dl_fileprefix'] == 'G') {
                    $fs = ceil($row['filesize'] / 1000000) / 1000 . ' Gb';
                }
                $context['TPortal']['dl_week_downloaded'][] = array('id' => $row['id'], 'name' => $row['name'], 'category' => $row['category'], 'file' => $row['file'], 'href' => $scripturl . '?action=tpmod;dl=item' . $row['id'], 'downloads' => $row['downloads'], 'views' => $row['views'], 'author' => '<a href="' . $scripturl . '?action=profile;u=' . $row['authorID'] . '">' . $row['realName'] . '</a>', 'authorID' => $row['authorID'], 'date' => timeformat($row['created']), 'screenshot' => !empty($row['screenshot']) ? $row['screenshot'] : '', 'catname' => $row['catname'], 'cathref' => $scripturl . '?action=tpmod;dl=cat' . $row['category'], 'filesize' => $fs);
            }
            $smcFunc['db_free_result']($request);
        }
        // add x most recent and feature the last one
        $context['TPortal']['dl_last_added'] = dl_recentitems(5, 'date', 'array', $context['TPortal']['dlcat']);
        $context['TPortal']['dl_most_downloaded'] = dl_recentitems(5, 'downloads', 'array', $context['TPortal']['dlcat']);
        // do we have access then?
        $request = $smcFunc['db_query']('', '
			SELECT parent, access, name 
			FROM {db_prefix}tp_dlmanager 
			WHERE id = {int:cat}', array('cat' => $currentcat));
        if ($smcFunc['db_num_rows']($request) > 0) {
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                $currentname = $row['name'];
                $context['page_title'] = $row['name'];
                $catparent = $row['parent'];
                if (!get_perm($row['access'], 'tp_dlmanager')) {
                    // if a guest, make them login/register
                    if ($context['user']['is_guest']) {
                        redirectexit('action=login');
                    } else {
                        redirectexit('action=tpmod;dl');
                    }
                }
            }
            $smcFunc['db_free_result']($request);
        } else {
            redirectexit('action=tpmod;dl');
        }
        $request = $smcFunc['db_query']('', '
			SELECT a.access AS access, a.icon AS icon,	a.link AS shortname, a.description AS description,
				a.name AS name,	a.id AS id, a.parent AS parent, if (a.id = b.category, count(*), 0) AS files,
		  		b.category AS subchild
			FROM ({db_prefix}tp_dlmanager AS a)
			LEFT JOIN {db_prefix}tp_dlmanager AS b
		  		ON a.id = b.category
			WHERE a.type = {string:type}
			AND a.parent = {int:cat}
		  	GROUP BY a.id
		  	ORDER BY a.downloads ASC', array('type' => 'dlcat', 'cat' => $currentcat));
        $context['TPortal']['dlchildren'] = array();
        if ($smcFunc['db_num_rows']($request) > 0) {
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                $show = get_perm($row['access'], 'tp_dlmanager');
                if ($show && $row['parent'] == $currentcat) {
                    $context['TPortal']['dlcats'][] = array('id' => $row['id'], 'name' => $row['name'], 'parent' => $row['parent'], 'description' => $context['TPortal']['dl_wysiwyg'] == 'bbc' ? parse_bbc(trim(strip_tags($row['description']))) : $row['description'], 'access' => $row['access'], 'icon' => $row['icon'], 'href' => !empty($row['shortname']) ? $scripturl . '?action=tpmod;dl=' . $row['shortname'] : $scripturl . '?action=tpmod;dl=cat' . $row['id'], 'shortname' => !empty($row['shortname']) ? $row['shortname'] : $row['id'], 'files' => $row['files']);
                } elseif ($show && $row['parent'] != $currentcat) {
                    $context['TPortal']['dlchildren'][] = $row['id'];
                    $context['TPortal']['dlcatchilds'][] = array('id' => $row['id'], 'name' => $row['name'], 'parent' => $row['parent'], 'href' => !empty($row['shortname']) ? $scripturl . '?action=tpmod;dl=' . $row['shortname'] : $scripturl . '?action=tpmod;dl=cat' . $row['id'], 'shortname' => !empty($row['shortname']) ? $row['shortname'] : $row['id'], 'files' => $row['files']);
                }
            }
            $smcFunc['db_free_result']($request);
        }
        // get any items in the category
        $context['TPortal']['dlitem'] = array();
        $start = 0;
        if (isset($_GET['p']) && !is_numeric($_GET['p'])) {
            fatal_error('Attempt to specify a non-integer value!');
        } elseif (isset($_GET['p']) && is_numeric($_GET['p'])) {
            $start = $_GET['p'];
        }
        // get total count
        $request = $smcFunc['db_query']('', '
				SELECT COUNT(*) FROM {db_prefix}tp_dlmanager 
				WHERE type = {string:type} 
				AND category = {int:cat} 
				AND subitem = {int:sub}', array('type' => 'dlitem', 'cat' => $currentcat, 'sub' => 0));
        $row = $smcFunc['db_fetch_row']($request);
        $rows2 = $row[0];
        $request = $smcFunc['db_query']('', '
				SELECT dl.id, LEFT(dl.description, 400) as ingress,dl.name, dl.category, dl.file, 
					dl.downloads, dl.views, dl.link, dl.created, dl.last_access, 
					dl.author_id as authorID, dl.icon, dl.screenshot, dl.filesize, mem.real_name as realName 
				FROM {db_prefix}tp_dlmanager as dl
				LEFT JOIN {db_prefix}members as mem ON (dl.author_id=mem.id_member)
				WHERE dl.type = {string:type} 
				AND dl.category = {int:cat} 
				AND dl.subitem = {int:sub} 
				ORDER BY dl.' . $dlsort . ' ' . $dlsort_way . ' LIMIT {int:start}, 10', array('type' => 'dlitem', 'cat' => $currentcat, 'sub' => 0, 'start' => $start));
        if ($smcFunc['db_num_rows']($request) > 0) {
            // set up the sorting links
            $context['TPortal']['sortlinks'] = '<span class="smalltext">' . $txt['tp-sortby'] . ': ';
            $what = array('id', 'name', 'downloads', 'last_access', 'created', 'authorID');
            foreach ($what as $v) {
                if ($context['TPortal']['dlsort'] == $v) {
                    $context['TPortal']['sortlinks'] .= '<a href="' . $scripturl . '?action=tpmod;dl=cat' . $currentcat . ';dlsort=' . $v . ';';
                    if ($context['TPortal']['dlsort_way'] == 'asc') {
                        $context['TPortal']['sortlinks'] .= 'desc;p=' . $start . '">' . $txt['tp-' . $v] . ' <img src="' . $settings['tp_images_url'] . '/TPsort_up.gif" alt="" /></a> &nbsp;|&nbsp; ';
                    } else {
                        $context['TPortal']['sortlinks'] .= 'asc;p=' . $start . '">' . $txt['tp-' . $v] . ' <img src="' . $settings['tp_images_url'] . '/TPsort_down.gif" alt="" /></a> &nbsp;|&nbsp; ';
                    }
                } else {
                    $context['TPortal']['sortlinks'] .= '<a href="' . $scripturl . '?action=tpmod;dl=cat' . $currentcat . ';dlsort=' . $v . ';desc;p=' . $start . '">' . $txt['tp-' . $v] . '</a> &nbsp;|&nbsp; ';
                }
            }
            $context['TPortal']['sortlinks'] = substr($context['TPortal']['sortlinks'], 0, strlen($context['TPortal']['sortlinks']) - 15);
            $context['TPortal']['sortlinks'] .= '</span>';
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                if (substr($row['screenshot'], 0, 16) == 'tp-images/Image/') {
                    $decideshot = $boardurl . '/' . $row['screenshot'];
                } else {
                    $decideshot = $boardurl . '/tp-images/dlmanager/thumb/' . $row['screenshot'];
                }
                if ($context['TPortal']['dl_fileprefix'] == 'K') {
                    $fs = ceil($row['filesize'] / 1000) . ' Kb';
                } elseif ($context['TPortal']['dl_fileprefix'] == 'M') {
                    $fs = ceil($row['filesize'] / 1000) / 1000 . ' Mb';
                } elseif ($context['TPortal']['dl_fileprefix'] == 'G') {
                    $fs = ceil($row['filesize'] / 1000000) / 1000 . ' Gb';
                }
                if ($context['TPortal']['dl_usescreenshot'] == 1) {
                    if (!empty($row['screenshot'])) {
                        $ico = $boardurl . '/tp-images/dlmanager/thumb/' . $row['screenshot'];
                    } else {
                        $ico = '';
                    }
                } else {
                    $ico = $row['icon'];
                }
                $context['TPortal']['dlitem'][] = array('id' => $row['id'], 'name' => $row['name'], 'category' => $row['category'], 'file' => $row['file'], 'description' => '', 'href' => $scripturl . '?action=tpmod;dl=item' . $row['id'], 'dlhref' => $scripturl . '?action=tpmod;dl=get' . $row['id'], 'downloads' => $row['downloads'], 'views' => $row['views'], 'link' => $row['link'], 'created' => $row['created'], 'date_last' => $row['last_access'], 'author' => '<a href="' . $scripturl . '?action=profile;u=' . $row['authorID'] . '">' . $row['realName'] . '</a>', 'authorID' => $row['authorID'], 'screenshot' => $row['screenshot'], 'sshot' => $decideshot, 'icon' => $ico, 'date' => $row['created'], 'filesize' => $fs, 'ingress' => $context['TPortal']['dl_wysiwyg'] == 'bbc' ? parse_bbc(trim(strip_tags($row['ingress']))) : $row['ingress']);
            }
            $smcFunc['db_free_result']($request);
        }
        if (isset($context['TPortal']['mystart'])) {
            $mystart = $context['TPortal']['mystart'];
        }
        $currsorting = '';
        if (!empty($dlsort)) {
            $currsorting .= ';dlsort=' . $dlsort;
        }
        if (!empty($dlsort_way)) {
            $currsorting .= ';' . $dlsort_way;
        }
        // construct a pageindex
        $context['TPortal']['pageindex'] = TPageIndex($scripturl . '?action=tpmod;dl=cat' . $currentcat . $currsorting, $mystart, $rows2, 10);
        // check backwards for parents
        $done = 0;
        $context['TPortal']['parents'] = array();
        while ($catparent > 0 || $done < 2) {
            if (!empty($context['TPortal']['cats'][$catparent])) {
                $context['TPortal']['parents'][] = array('id' => $catparent, 'name' => $context['TPortal']['cats'][$catparent]['name'], 'parent' => $context['TPortal']['cats'][$catparent]['parent']);
                $catparent = $context['TPortal']['cats'][$catparent]['parent'];
            } else {
                $catparent = 0;
            }
            if ($catparent == 0) {
                $done++;
            }
        }
        // make the linktree
        if (sizeof($context['TPortal']['parents']) > 0) {
            $parts = array_reverse($context['TPortal']['parents']);
            // add to the linktree
            foreach ($parts as $par) {
                TPadd_linktree($scripturl . '?action=tpmod;dl=cat' . $par['id'], $par['name']);
            }
        }
        // add to the linktree
        TPadd_linktree($scripturl . '?action=tpmod;dl=cat' . $currentcat, $currentname);
        $context['TPortal']['dlheader'] = $currentname;
    } elseif ($context['TPortal']['dlaction'] == 'tptag') {
        $context['TPortal']['dlsort'] = $dlsort = 'id';
        $context['TPortal']['dlsort_way'] = $dlsort_way = 'desc';
        // get any items in the category
        $context['TPortal']['dlitem'] = array();
        $start = 0;
        if (isset($_GET['p']) && !is_numeric($_GET['p'])) {
            fatal_error($txt['tp-dlnonint']);
        } elseif (isset($_GET['p']) && is_numeric($_GET['p'])) {
            $start = $_GET['p'];
        }
        // get total count
        $request = $smcFunc['db_query']('', '
			SELECT COUNT(*) FROM {db_prefix}tp_dlmanager 
			WHERE type = {string:type}
			AND subitem = {int:sub}', array('type' => 'dlitem', 'sub' => 0));
        $row = $smcFunc['db_fetch_row']($request);
        $rows2 = $row[0];
        $request = $smcFunc['db_query']('', '
			SELECT id, name, category, file, downloads, views, link, created,
				last_access, author_id as authorID, icon, screenshot, filesize,
				global_tag 
			FROM {db_prefix}tp_dlmanager 
			WHERE type = {string:type} 
			AND subitem = {int:sub} LIMIT {int:start}, 10', array('type' => 'dlitem', 'sub' => 0, 'start' => $start));
        if ($smcFunc['db_num_rows']($request) > 0) {
            if (substr($row['screenshot'], 0, 16) == 'tp-images/Image/') {
                $decideshot = $boardurl . '/' . $row['screenshot'];
            } else {
                $decideshot = $boardurl . '/tp-images/dlmanager/thumb/' . $row['screenshot'];
            }
            // set up the sorting links
            $context['TPortal']['sortlinks'] = '';
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                if ($context['TPortal']['dl_fileprefix'] == 'K') {
                    $fs = ceil($row['filesize'] / 1000) . ' Kb';
                } elseif ($context['TPortal']['dl_fileprefix'] == 'M') {
                    $fs = ceil($row['filesize'] / 1000) / 1000 . ' Mb';
                } elseif ($context['TPortal']['dl_fileprefix'] == 'G') {
                    $fs = ceil($row['filesize'] / 1000000) / 1000 . ' Gb';
                }
                $context['TPortal']['dlitem'][] = array('id' => $row['id'], 'name' => $row['name'], 'category' => $row['category'], 'file' => $row['file'], 'description' => '', 'href' => $scripturl . '?action=tpmod;dl=item' . $row['id'], 'dlhref' => $scripturl . '?action=tpmod;dl=get' . $row['id'], 'downloads' => $row['downloads'], 'views' => $row['views'], 'link' => $row['link'], 'created' => $row['created'], 'date_last' => $row['last_access'], 'author' => '', 'authorID' => $row['authorID'], 'screenshot' => $row['screenshot'], 'sshot' => $decideshot, 'icon' => $row['icon'], 'date' => $row['created'], 'filesize' => $fs);
            }
            $smcFunc['db_free_result']($request);
        }
        if (isset($context['TPortal']['mystart'])) {
            $mystart = $context['TPortal']['mystart'];
        }
        $context['TPortal']['dlheader'] = '';
    } elseif ($context['TPortal']['dlaction'] == 'item') {
        //fetch the category
        $cat = $context['TPortal']['dlcat'];
        $context['TPortal']['dlcats'] = array();
        $catname = '';
        $catdesc = '';
        $request = $smcFunc['db_query']('', '
			SELECT id, name, parent, icon, access, link 
			FROM {db_prefix}tp_dlmanager 
			WHERE id = {int:cat}
			AND type = {string:type} LIMIT 1', array('cat' => $cat, 'type' => 'dlcat'));
        if ($smcFunc['db_num_rows']($request) > 0) {
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                $catshortname = $row['link'];
                $catname = $row['name'];
                $catparent = $row['parent'];
                $firstparent = $row['parent'];
                // check if you are allowed in here
                $show = get_perm($row['access'], 'tp_dlmanager');
                if (!$show) {
                    redirectexit('action=tpmod;dl');
                }
            }
            $smcFunc['db_free_result']($request);
        }
        // set the title
        $context['TPortal']['dl_title'] = $catname;
        $context['TPortal']['parents'] = array();
        // check backwards for parents
        $done = 0;
        while ($catparent > 0 || $done < 2) {
            if (!empty($context['TPortal']['cats'][$catparent])) {
                $context['TPortal']['parents'][] = array('id' => $catparent, 'shortname' => $catshortname, 'name' => $context['TPortal']['cats'][$catparent]['name'], 'parent' => $context['TPortal']['cats'][$catparent]['parent']);
                $catparent = $context['TPortal']['cats'][$catparent]['parent'];
            } else {
                $catparent = 0;
            }
            if ($catparent == 0) {
                $done++;
            }
        }
        // make the linktree
        if (sizeof($context['TPortal']['parents']) > 0) {
            $parts = array_reverse($context['TPortal']['parents'], TRUE);
            // add to the linktree
            foreach ($parts as $parent) {
                if (!empty($parent['shortname'])) {
                    TPadd_linktree($scripturl . '?action=tpmod;dl=' . $parent['shortname'], $parent['name']);
                } else {
                    TPadd_linktree($scripturl . '?action=tpmod;dl=cat' . $parent['id'], $parent['name']);
                }
            }
        }
        // fetch the item data
        $item = $context['TPortal']['item'] = $item;
        $context['TPortal']['dlitem'] = array();
        $request = $smcFunc['db_query']('', '
			SELECT dl.*, dl.author_id as authorID, m.real_name as realName
			FROM ({db_prefix}tp_dlmanager AS dl)
			LEFT JOIN {db_prefix}members AS m ON (m.id_member = dl.author_id)
			WHERE dl.type = {string:type}
			AND dl.id = {int:item}
			LIMIT 1', array('type' => 'dlitem', 'item' => $item));
        if ($smcFunc['db_num_rows']($request) > 0) {
            $rows = $smcFunc['db_num_rows']($request);
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                $subitem = $row['id'];
                $fetch = $smcFunc['db_query']('', '
					SELECT id, name, file, downloads, filesize, created, views
					FROM {db_prefix}tp_dlmanager
					WHERE type = {string:type}
					AND subitem = {int:sub}
					ORDER BY id DESC', array('type' => 'dlitem', 'sub' => $subitem));
                if ($smcFunc['db_num_rows']($fetch) > 0) {
                    $fdata = array();
                    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=get' . $frow['id'], 'href2' => $scripturl . '?action=tpmod;dl=item' . $frow['id'], 'downloads' => $frow['downloads'], 'views' => $frow['views'], 'created' => $frow['created'], 'filesize' => $ffs);
                    }
                    $smcFunc['db_free_result']($fetch);
                }
                if ($context['TPortal']['dl_fileprefix'] == 'K') {
                    $fs = ceil($row['filesize'] / 1000) . ' Kb';
                } elseif ($context['TPortal']['dl_fileprefix'] == 'M') {
                    $fs = ceil($row['filesize'] / 1000) / 1000 . ' Mb';
                } elseif ($context['TPortal']['dl_fileprefix'] == 'G') {
                    $fs = ceil($row['filesize'] / 1000000) / 1000 . ' Gb';
                }
                $rat = array();
                $rating_votes = 0;
                $rat = explode(',', $row['rating']);
                $rating_votes = count($rat);
                if ($row['rating'] == '') {
                    $rating_votes = 0;
                }
                $total = 0;
                foreach ($rat as $mm => $mval) {
                    $total = $total + $mval;
                }
                if ($rating_votes > 0 && $total > 0) {
                    $rating_average = floor($total / $rating_votes);
                } else {
                    $rating_average = 0;
                }
                $bigshot = $decideshot = !empty($row['screenshot']) ? $boardurl . '/' . $row['screenshot'] : '';
                // does it exist?
                if (file_exists($boarddir . '/tp-images/dlmanager/listing/' . $row['screenshot']) && !empty($row['screenshot'])) {
                    $decideshot = $boardurl . '/tp-images/dlmanager/listing/' . $row['screenshot'];
                }
                if (file_exists($boarddir . '/tp-images/dlmanager/' . $row['screenshot']) && !empty($row['screenshot'])) {
                    $bigshot = $boardurl . '/tp-images/dlmanager/' . $row['screenshot'];
                }
                if ($context['user']['is_logged']) {
                    $can_rate = in_array($context['user']['id'], explode(',', $row['voters'])) ? false : true;
                } else {
                    $can_rate = false;
                }
                $context['TPortal']['dlitem'][] = array('id' => $row['id'], 'name' => $row['name'], 'description' => $context['TPortal']['dl_wysiwyg'] == 'bbc' ? parse_bbc(trim(strip_tags($row['description']))) : $row['description'], 'category' => $row['category'], 'file' => $row['file'], 'href' => $scripturl . '?action=tpmod;dl=get' . $row['id'], 'downloads' => $row['downloads'], 'views' => $row['views'], 'link' => $row['link'], 'date_last' => $row['last_access'], 'author' => $row['realName'], 'authorID' => $row['authorID'], 'screenshot' => $row['screenshot'], 'sshot' => $decideshot, 'bigshot' => $bigshot, 'icon' => $row['icon'], 'created' => $row['created'], 'filesize' => $fs, 'subitem' => isset($fdata) ? $fdata : '', 'rating_votes' => $rating_votes, 'rating_average' => $rating_average, 'can_rate' => $can_rate, 'global_tag' => $row['global_tag']);
                $author = $row['authorID'];
                $parent_cat = $row['category'];
                $views = $row['views'];
                $itemname = $row['name'];
                $itemid = $row['id'];
                $context['page_title'] = $row['name'];
            }
            $smcFunc['db_free_result']($request);
            TPadd_linktree($scripturl . '?action=tpmod;dl=cat' . $parent_cat, $catname);
            TPadd_linktree($scripturl . '?action=tpmod;dl=item' . $itemid, $itemname);
            // update the views and last access!
            $views++;
            $now = time();
            $year = (int) date("Y", $now);
            $week = (int) date("W", $now);
            // update weekly views
            $req = $smcFunc['db_query']('', '
				SELECT id FROM {db_prefix}tp_dldata 
				WHERE year = {int:year} 
				AND week = {int:week} 
				AND item = {int:item}', array('year' => $year, 'week' => $week, 'item' => $itemid));
            if ($smcFunc['db_num_rows']($req) > 0) {
                $row = $smcFunc['db_fetch_assoc']($req);
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}tp_dldata 
					SET views = views + 1 
					WHERE id = {int:item}', array('item' => $row['id']));
            } else {
                $smcFunc['db_insert']('INSERT', '{db_prefix}tp_dldata', array('week' => 'int', 'year' => 'int', 'views' => 'int', 'item' => 'int'), array($week, $year, 1, $itemid), array('id'));
            }
            $smcFunc['db_query']('', '
				UPDATE {db_prefix}tp_dlmanager 
				SET views = {int:views}, last_access = {int:last}
				WHERE id = {int:item}', array('views' => $views, 'last' => $now, 'item' => $itemid));
            $context['TPortal']['dlheader'] = $itemname;
        }
    } elseif ($context['TPortal']['dlaction'] == 'get') {
        TPdownloadme();
    } elseif ($context['TPortal']['dlaction'] == 'stats') {
        TPdlstats();
    } elseif ($context['TPortal']['dlaction'] == 'results') {
        TPdlresults();
    } elseif ($context['TPortal']['dlaction'] == 'search') {
        TPdlsearch();
    }
    // For wireless, we use the Wireless template...
    if (WIRELESS) {
        loadTemplate('TPwireless');
        if ($context['TPortal']['dlaction'] == 'item' || $context['TPortal']['dlaction'] == 'cat') {
            $what = $context['TPortal']['dlaction'];
        } else {
            $what = 'main';
        }
        $context['sub_template'] = WIRELESS_PROTOCOL . '_tp_dl_' . $what;
    } else {
        loadTemplate('TPdlmanager');
    }
}
Ejemplo n.º 2
0
function TPortal_module()
{
    global $context, $scripturl, $txt;
    switch ($context['TPortal']['moduleblock']) {
        case 'dl-stats':
            dl_recentitems('8', 'date', 'echo');
            break;
        case 'dl-stats2':
            dl_recentitems('8', 'downloads', 'echo');
            break;
        case 'dl-stats3':
            dl_recentitems('8', 'views', 'echo');
            break;
        case 'dl-stats4':
            $it = array();
            $it = dl_recentitems('1', 'date', 'array');
            if (sizeof($it) > 0) {
                foreach ($it as $item) {
                    echo '
					<img src="' . $item['icon'] . '" align="right" style="margin-left: 4px; " alt="" />
						<a href="' . $item['href'] . '"><b>' . $item['name'] . '</b></a>
						<p class="smalltext">' . $txt['tp-uploadedby'] . ' <b>' . $item['author'] . '</b> <br />( ' . $item['date'] . ')<br />
						' . $txt['tp-downloads'] . '/' . $txt['tp-itemviews'] . ': <b>' . $item['downloads'] . ' / ' . $item['views'] . '</b></p>';
                }
            }
            break;
        case 'dl-stats5':
            $it = array();
            $it = dl_recentitems('1', 'downloads', 'array');
            if (sizeof($it) > 0) {
                foreach ($it as $item) {
                    echo '
					<img src="' . $item['icon'] . '" align="right" style="margin-left: 4px; " alt="" />
						<a href="' . $item['href'] . '"><b>' . $item['name'] . '</b></a>
						<p class="smalltext">' . $txt['tp-uploadedby'] . ' <b>' . $item['author'] . '</b> <br />( ' . $item['date'] . ')<br />
						' . $txt['tp-downloads'] . '/' . $txt['tp-itemviews'] . ': <b>' . $item['downloads'] . ' / ' . $item['views'] . '</b></p>';
                }
            }
            break;
        case 'dl-stats6':
            $it = array();
            $it = dl_recentitems('1', 'views', 'array');
            if (sizeof($it) > 0) {
                foreach ($it as $item) {
                    echo '
					<img src="' . $item['icon'] . '" align="right" style="margin-left: 4px; " alt="" />
						<a href="' . $item['href'] . '"><b>' . $item['name'] . '</b></a>
						<p class="smalltext">' . $txt['tp-uploadedby'] . ' <b>' . $item['author'] . '</b> <br />( ' . $item['date'] . ')<br />
						' . $txt['tp-downloads'] . '/' . $txt['tp-itemviews'] . ': <b>' . $item['downloads'] . ' / ' . $item['views'] . '</b></p>';
                }
            }
            break;
        case 'dl-stats7':
            $it = array();
            $it = art_recentitems('5', 'date');
            if (sizeof($it) > 0) {
                foreach ($it as $item) {
                    echo '<span class="smalltext"><a title="' . $item['date'] . '" href="' . $scripturl . '?page=' . $item['id'] . '">' . $item['subject'] . '</a>
						</span><br />';
                }
            }
            break;
        case 'dl-stats8':
            $it = array();
            $it = art_recentitems('5', 'views');
            if (sizeof($it) > 0) {
                foreach ($it as $item) {
                    echo '<span class="smalltext"><a title="' . $item['views'] . ' ' . $txt['tp-views'] . '" href="' . $scripturl . '?page=' . $item['id'] . '">' . $item['subject'] . '</a>
						</span><br />';
                }
            }
            break;
        case 'dl-stats9':
            $it = array();
            $it = art_recentitems('5', 'comments');
            if (sizeof($it) > 0) {
                foreach ($it as $item) {
                    echo '<span class="smalltext"><a title="' . $item['comments'] . '" href="' . $scripturl . '?page=' . $item['id'] . '">' . $item['subject'] . '</a>
						(' . $item['comments'] . ')<br /></span>';
                }
            }
            break;
    }
}