<?php
 
/*******************************************************************************
 
 *                      PHP Simple Links Exchange Class
 
 *******************************************************************************
 
 *      Author:     Denys Matuzenko
 
 *      Email:      info (_at_) brutka.com
 
 *      Website:    http://www.brutka.com
 
 *                  http://www.matuzenko.com
 
 
 
 *      File:       LinkExchange.class.php
 
 *      Version:    1.00
 
 *      Copyright:  (c) 2006 - Denys Matuzenko 
 
 *                  You are free to use, distribute, and modify this software 
 
 *                  under the terms of the GNU General Public License.  See the
 
 *                  included license.txt file.
 
 *      
 
 *******************************************************************************
 
 *  VERION HISTORY:
 
 *  
 
 *      v1.0.0 [08.04.2006] - Initial Version
 
 *
 
 *******************************************************************************
 
 
 
 *  DESCRIPTION:
 
 *  This class provides simple mechanism of managing links exchnage with your site
 
 *  including adding new links, updating and deleting existing and allowing you 
 
 *    to check if the backlink to your site is present on target web page.
 
 *    Class was created for own use, if you have thoughts how to improve it 
 
 *    I can do this free of charge and send you improved class.
 
 */
 
 
 
 
 
define ("DB_USER", "");
 
define ("DB_PASS", "");
 
define ("DB_NAME", "");
 
define ("DB_HOST", "localhost");
 
define ("MY_URL", "http://www.YOURDOMAIN.com");
 
 
class LinkExchange_class {
 
    var $counter;
 
 
    function LinkExchange_class() {
 
        $this->connectToBase();
 
    }
 
    function getCategoriesList () {
 
        $result = mysql_query ("SELECT category_id, name FROM link_exchange_categories ORDER BY category_id");
 
        $categories = array ();
 
        $this->counter = 0;
 
        while ($row = mysql_fetch_array ($result)) $categories[$this->counter++] = $row;
 
        return $categories;
 
    }
 
        
 
    function addCategory ($add_name) {
 
        mysql_query ("INSERT INTO link_exchange_categories (name) VALUES ('$add_name')");
 
    }
 
    
 
    function addLink ($add_link, $add_back_link, $add_category) {
 
        mysql_query ("INSERT INTO link_exchange_links (category_id, link_source, backlink_address, date_added) VALUES($add_category, '$add_link', '{$add_back_link}', now())");
 
    }
 
    
 
    function getLinksOfCategory ($get_category) {
 
        $result = mysql_query ("SELECT * FROM link_exchange_links WHERE category_id={$get_category}");
 
        $this->counter = 0;
 
        $links = array ();
 
        while ($row = mysql_fetch_array ($result)) $links[$this->counter++] = $row;
 
        return $links;
 
    }
 
    
 
    function removeLink ($remove_link) {
 
        mysql_query ("DELETE FROM link_exchange_links WHERE link_id={$remove_link}");
 
    }
 
    
 
    function updateLink ($link_id, $new_source, $new_backlink, $new_category) {
 
        mysql_query ("UPDATE link_exchange_links SET category_id={$new_category}, link_source='{$new_source}', backlink_address='{$new_backlink}', date_checked=NULL WHERE link_id={$link_id}");
 
    }
 
    
 
    function checkLinks ($check_type) {
 
        $sql = "SELECT link_exchange_links.* FROM link_exchange_links";
 
        if ($check_type == 'confirmed') $sql .= " WHERE backlink_present=1";
 
        if ($check_type == 'not_confirmed') $sql .= " backlink_present=0";
 
        $result = mysql_query ($sql);
 
        while ($row = mysql_fetch_array($result)) {
 
            echo '<tr><td>'.$row['link_id'].'</td><td><textarea cols="80" rows="2" readonly="readonly">'.$row['link_source'].'</textarea></td><td><input type="text" size="60" value="'.$row['backlink_address'].'" readonly="readonly" /></td><td>';
 
            if ($row['backlink_address'] == '') echo '-'; else {
 
                $contents = file_get_contents ($row['backlink_address']);
 
                if ($contents == FALSE) echo 'Could not open';
 
                else {
 
                    $present = 0;
 
                    if( (stristr($contents, '<a href="'.MY_URL) === FALSE) && (stristr($contents, "<a href='" .MY_URL) === FALSE)) {
 
                        echo 'Link NOT present';
 
                    } else {
 
                        $present = 1;
 
                        echo 'Link present';
 
                    }
 
                    mysql_query ("UPDATE link_exchange_links SET backlink_present={$present}, date_checked=now() WHERE link_id= " . $row['link_id']);
 
                }
 
            }
 
            echo '</td></tr>';
 
        }
 
    }
 
    
 
    function makeCheckReport ($category_id) {
 
        $where = ($category_id==0) ? '': ' WHERE link_exchange_categories.category_id='.$category_id;
 
        $result = mysql_query ("SELECT link_id, link_source, backlink_address, backlink_present, date_added, date_checked, name, link_exchange_categories.category_id, link_id FROM link_exchange_links INNER JOIN link_exchange_categories ON link_exchange_categories.category_id=link_exchange_links.category_id".$where." ORDER BY link_exchange_links.category_id, date_added DESC");
 
        $this->counter = 0;
 
        $report = array ();
 
        while ($row = mysql_fetch_array ($result)) $report[$this->counter++] = $row;
 
        return $report;
 
    }
 
    
 
    function connectToBase () {
 
        mysql_connect (DB_HOST, DB_USER, DB_PASS);
 
        mysql_select_db (DB_NAME);
 
    }
 
    
 
    function setupTables () {
 
        mysql_query ("CREATE TABLE link_exchange_categories (category_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255))");
 
        mysql_query ("CREATE TABLE link_exchange_links (link_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, category_id INT NOT NULL, link_source TEXT, backlink_address VARCHAR(255), backlink_present TINYINT NOT NULL DEFAULT 0, date_added DATE, date_checked DATE default NULL)");
 
        
 
    }
 
    
 
}
 
 
 
 
 
?>
 
 |