------------------------------------------------------------------------------------------------------------------------
MOD name: Link Mod
Version: 3.0
Description: Changes Torrent Links from URL's to website names and allows editing of existing links.
Written by : DemonSpawn
Difficulty : Easy
Effected Files: admin.php, functions.php, index.php, lang-*.php

Thanks to
Munk for the sorting code and a few minor fixes

First off copy the 3 images from this archive to your torrentflux images directory
------------------------------------------------------------------------------------------------------------------------
MYSQL UPDATE  ---  NOTE: THIS WILL REMOVE ALL LINKS 
------------------------------------------------------------------------------------------------------------------------

DROP TABLE IF EXISTS `tf_links`;
CREATE TABLE `tf_links` (
  `lid` int(10) NOT NULL auto_increment,
  `url` varchar(255) NOT NULL default '',
  `sitename` varchar(255) NOT NULL default 'Old Link',
  `sort_order` tinyint(3) unsigned default '0',
  PRIMARY KEY  (`lid`)
) TYPE=MyISAM;

------------------------------------------------------------------------------------------------------------------------
OPEN FILE
------------------------------------------------------------------------------------------------------------------------
index.php
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
        $arLinks = GetLinks();
        foreach($arLinks as $link)
        {
            $arURL = parse_url($link);
            echo "<a href=\"".$link."\" target=\"_blank\"><img src=\"images/arrow.gif\" width=9 height=9 title=\"".$link."\" border=0 align=\"baseline\">".$arURL['host']."</a><br>\n";
        }
------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
------------------------------------------------------------------------------------------------------------------------
        $arLinks = GetLinks();
        foreach($arLinks as $link)
        {
            echo "<a href=\"".$link['url']."\" target=\"_blank\"><img src=\"images/arrow.gif\" width=9 height=9 title=\"".$link['url']."\" border=0 align=\"baseline\">".$link['sitename']."</a><br>\n";
        }
------------------------------------------------------------------------------------------------------------------------
OPEN FILE
------------------------------------------------------------------------------------------------------------------------
functions.php
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
//**********************************************************************************
// START FUNCTIONS HERE
//**********************************************************************************
------------------------------------------------------------------------------------------------------------------------
AFTER ADD
------------------------------------------------------------------------------------------------------------------------
function getLinkSortOrder($lid)
{
    global $db;

    // Get Current sort order index of link with this link id:
    $sql="SELECT sort_order FROM tf_links WHERE lid=$lid";
    $rtnValue=$db->GetOne($sql);
    showError($db,$sql);

    return $rtnValue;
}
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
//*********************************************************
function getLink($lid)
------------------------------------------------------------------------------------------------------------------------
BEFORE ADD
------------------------------------------------------------------------------------------------------------------------
//*********************************************************
function getSite($lid)
{
    global $cfg, $db;

    $rtnValue = "";

    $sql = "SELECT sitename FROM tf_links WHERE lid=".$lid;
    $rtnValue = $db->GetOne($sql);

    return $rtnValue;
}
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
    $sql = "delete from tf_links where lid=".$lid;
------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
------------------------------------------------------------------------------------------------------------------------
    // Get Current sort order index of link with this link id:
    $idx=getLinkSortOrder($lid);

    // Fetch all link ids and their sort orders where the sort order is greater
    // than the one we're removing - we need to shuffle each sort order down
    // one:
    $sql="SELECT sort_order, lid FROM tf_links ";
    $sql.="WHERE sort_order > $idx ORDER BY sort_order ASC";
    $result=$db->Execute($sql);
    showError($db,$sql);
    $arLinks=$result->GetAssoc();

    // Decrement the sort order of each link:
    foreach($arLinks as $sid=>$this_lid){
        $sql="UPDATE tf_links SET sort_order=sort_order-1 WHERE lid=$this_lid";
        $db->Execute($sql);
        showError($db,$sql);
    }

    // Finally delete the link:
    $sql = "DELETE FROM tf_links WHERE lid=".$lid;
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
// ***************************************************************************
// addNewLink - Add New Link
------------------------------------------------------------------------------------------------------------------------
BEFORE ADD
------------------------------------------------------------------------------------------------------------------------
//**************************************************************************
// alterLink()
// This function updates the database and alters the selected links values

function alterLink($lid,$newLink,$newSite)
    {
    global $cfg, $db;

    $sql = "UPDATE tf_links SET url='".$newLink."',`sitename`='".$newSite."' WHERE `lid` = ".$lid." LIMIT 1";
    $db->Execute($sql);
    showError($db,$sql);
}
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
function addNewLink($newLink)
------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
------------------------------------------------------------------------------------------------------------------------
function addNewLink($newLink,$newSite)
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
    $rec = array('url'=>$newLink);
------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
------------------------------------------------------------------------------------------------------------------------
    // Link sort order index:
    $idx=-1;

    // Get current highest link index:
    $sql="SELECT sort_order FROM tf_links ORDER BY sort_order DESC";
    $result=$db->SelectLimit($sql, 1);
    showError($db, $sql);

    if($result->fields === false){
        // No links currently in db:
        $idx=0;
    } else {
        $idx=$result->fields["sort_order"]+1;
    }

    $rec = array(
        'url'=>$newLink,
        'sitename'=>$newSite,
        'sort_order'=>$idx
    );
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
    $link_array = $db->GetAssoc("SELECT lid, url FROM tf_links ORDER BY lid");
------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
------------------------------------------------------------------------------------------------------------------------
    $link_array = $db->GetAssoc("SELECT lid, url, sitename, sort_order FROM tf_links ORDER BY sort_order");
------------------------------------------------------------------------------------------------------------------------
OPEN FILE
------------------------------------------------------------------------------------------------------------------------
admin.php
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
// addLink -- adding a link
------------------------------------------------------------------------------------------------------------------------
BEFORE ADD
------------------------------------------------------------------------------------------------------------------------
// editLink -- Changing a link
//****************************************************************************
function editLink($lid,$newLink,$newSite)
{
    if(!empty($newLink)){
        if(strpos($newLink, "http://" ) !== 0 && strpos($newLink, "https://" ) !== 0 && strpos($newLink, "ftp://" ) !== 0){
            $newLink = "http://".$newLink;
        }
        empty($newSite) && $newSite = $newLink;
        global $cfg;
        $oldLink=getLink($lid);
        $oldSite=getSite($lid);
        alterLink($lid,$newLink,$newSite);
        AuditAction($cfg["constants"]["admin"], "Change Link: ".$oldSite." [".$oldLink."] -> ".$newSite." [".$newLink."]");
    }
    header("location: admin.php?op=editLinks");

}

//****************************************************************************
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
function addLink($newLink)
{
    if(!empty($newLink)){
------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
------------------------------------------------------------------------------------------------------------------------
function addLink($newLink,$newSite)
{
    if(!empty($newLink)){
        if(strpos($newLink, "http://" ) !== 0 && strpos($newLink, "https://" ) !== 0 && strpos($newLink, "ftp://" ) !== 0){
            $newLink = "http://".$newLink;
        }
        empty($newSite) && $newSite = $newLink;
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
    addNewLink($newLink);
    AuditAction($cfg["constants"]["admin"], "New "._LINKS_MENU.": ".$newLink);
------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
------------------------------------------------------------------------------------------------------------------------
    addNewLink($newLink,$newSite);
    AuditAction($cfg["constants"]["admin"], "New "._LINKS_MENU.": ".$newSite." [".$newLink."]");
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
//****************************************************************************
// addRSS -- adding a RSS link
------------------------------------------------------------------------------------------------------------------------
BEFORE ADD
------------------------------------------------------------------------------------------------------------------------
//****************************************************************************
// moveLink -- moving a link up or down in the list of links
//****************************************************************************
function moveLink($lid, $direction){
    global $db, $cfg;
    if  (!isset($lid) && !isset($direction)&& $direction !== "up" && $direction !== "down" ) {
        header("location: admin.php?op=editLinks");
    }
    $idx=getLinkSortOrder($lid);
    $position=array("up"=>-1, "down"=>1);
    $new_idx=$idx+$position[$direction];
    $sql="UPDATE tf_links SET sort_order=$idx WHERE sort_order=$new_idx";
    $db->Execute($sql);
    showError($db, $sql);
    $sql="UPDATE tf_links SET sort_order=$new_idx WHERE lid=$lid";
    $db->Execute($sql);
    showError($db, $sql);
    header("Location: admin.php?op=editLinks");
}
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
    AuditAction($cfg["constants"]["admin"], _DELETE." Link: ".getLink($lid));
------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
------------------------------------------------------------------------------------------------------------------------
    AuditAction($cfg["constants"]["admin"], _DELETE." Link: ".getSite($lid)." [".getLink($lid)."]");
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
    echo "<tr><td bgcolor=\"".$cfg["table_header_bg"]."\" background=\"themes/".$cfg["theme"]."/images/bar.gif\">";
    echo "<img src=\"images/properties.png\" width=18 height=13 border=0>&nbsp;&nbsp;<font class=\"title\">"._ADMINEDITLINKS."</font>";
------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
------------------------------------------------------------------------------------------------------------------------
    echo "<tr><td colspan=\"2\" bgcolor=\"".$cfg["table_header_bg"]."\" background=\"themes/".$cfg["theme"]."/images/bar.gif\">";
    echo "<img src=\"images/properties.png\" width=18 height=13 border=0>&nbsp;&nbsp;<font class=\"title\">"._ADMINEDITLINKS."</font>";
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
    echo "</td></tr><tr><td align=\"center\">";
------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
------------------------------------------------------------------------------------------------------------------------
    echo "</td></tr><tr><td colspan=2 align=\"center\">";
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
    <input type="Text" size="50" maxlength="255" name="newLink">
------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
------------------------------------------------------------------------------------------------------------------------
    <input type="Text" size="30" maxlength="255" name="newLink">
    <?php echo _FULLSITENAME ?>:
    <input type="Text" size="30" maxlength="255" name="newSite">
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
    $arLid = Array_Keys($arLinks);
    $inx = 0;
------------------------------------------------------------------------------------------------------------------------
ADD AFTER
------------------------------------------------------------------------------------------------------------------------
    $link_count = count($arLinks);
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
    echo "<tr><td><a href=\"admin.php?op=deleteLink&lid=".$lid."\"><img src=\"images/delete_on.gif\" width=16 height=16 border=0 title=\""._DELETE." ".$lid."\" align=\"absmiddle\"></a>&nbsp;";
    echo "<a href=\"".$link."\" target=\"_blank\">".$link."</a></td></tr>\n";
------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
------------------------------------------------------------------------------------------------------------------------
        if ( isset($_GET["edit"]) && $_GET["edit"] == $link['lid'])
        {
            echo "<tr><td colspan=\"2\">";
?>
  <form action="admin.php?op=editLink" method="post">
  <?php echo _FULLURLLINK ?>:
  <input type="Text" size="30" maxlength="255" name="editLink" value="<?php echo $link['url'] ?>">
  <?php echo _FULLSITENAME ?>:
  <input type="Text" size="30" maxlength="255" name="editSite" value="<?php echo $link['sitename'] ?>">
  <input type="hidden" name="lid" value="<?php echo $lid ?>">
  <input type="Submit" value="<?php echo _UPDATE ?>"><br>
  </form>
<?php
        }
        else
        {
            echo "<tr><td>";
            echo "<a href=\"admin.php?op=deleteLink&lid=".$lid."\"><img src=\"images/delete_on.gif\" width=16 height=16 border=0 title=\""._DELETE." ".$lid."\" align=\"absmiddle\"></a>&nbsp;";
            echo "<a href=\"admin.php?op=editLinks&edit=".$lid."\"><img src=\"images/edit.gif\" width=16 height=16 border=0 title=\""._EDIT." ".$lid."\" align=\"absmiddle\"></a>&nbsp;";
            echo "<a href=\"".$link['url']."\" target=\"_blank\">".$link['sitename']."</a></td>\n";
            echo "<td align=right width='36'>";

            if ($inx > 1 ){
                // Only put an 'up' arrow if this isn't the first entry:
                echo "<a href='admin.php?op=moveLink&amp;direction=up&amp;lid=".$lid."'>";
                echo "<img src='images/uparrow.png' width='16' height='16' ";
                echo "border='0' title='Move link up' align='absmiddle' alt='Up'></a>";
            }

            // If only one link, just put in a space in each cell:
            echo ($inx==1 ? "<img src='images/blank.gif' width='16' align='absmiddle'>" : "");
            echo "&nbsp;";

            if ($inx != count($arLinks)) {
                // Only put a 'down' arrow if this isn't the last item:
                echo "<a href='admin.php?op=moveLink&amp;direction=down&amp;lid=".$lid."'>";
                echo "<img src='images/downarrow.png' width='16' height='16' ";
                echo "border='0' title='Move link down' align='absmiddle' alt='Down'></a>";
            }
            echo "</td></tr>";
        }
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
        deleteRSS($rid);
    break;
------------------------------------------------------------------------------------------------------------------------
AFTER ADD
------------------------------------------------------------------------------------------------------------------------
    case "editLink":        
        $lid = getRequestVar('lid');
        $editLink = getRequestVar('editLink');
        $editSite = getRequestVar('editSite');
        editLink($lid,$editLink,$editSite);
    break;
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
        addLink($newLink);
------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
------------------------------------------------------------------------------------------------------------------------
        $newSite = getRequestVar('newSite');
        addLink($newLink,$newSite);
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
    case "deleteLink":
------------------------------------------------------------------------------------------------------------------------
BEFORE ADD
------------------------------------------------------------------------------------------------------------------------
    case "moveLink":
        $lid = getRequestVar('lid');
        $direction = getRequestVar('direction');
        moveLink($lid, $direction);
    break;
------------------------------------------------------------------------------------------------------------------------
OPEN
------------------------------------------------------------------------------------------------------------------------
language/lang-english.php
------------------------------------------------------------------------------------------------------------------------
FIND
------------------------------------------------------------------------------------------------------------------------
?>
------------------------------------------------------------------------------------------------------------------------
BEFORE ADD
------------------------------------------------------------------------------------------------------------------------
define("_FULLSITENAME", "Site Name");
------------------------------------------------------------------------------------------------------------------------
SAVE AND CLOSE FILES
------------------------------------------------------------------------------------------------------------------------
EOF
