Location: PHPKode > scripts > Link Collector > link-collector/links.php
<?php
/***********************************************************************
*
*    ***  links.php PROGRAM LISTING                 ***
*    ***  How to use is outlined at ownlinksite.com ***
*
* 07.01.2002 V 1.0 Beta
* 22.04.2003 V 1.1 Stable
* 26.07.2003 V 2.0 Major update
* 30.09.2003 V 2.1 Some small bug fixes
*
* Place the code in a sub-directory called ownlinksite and name it
* links.php.
* Also, put pagetransform.php and linkcollector.php in the same directory.
*
* This program transfers a template link page into an actual, working
* php-based link page, as described in the tutorial how_to.php
*
* All rights reserverd J.P.C.D.S., Inc.
* 
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
* 
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
***********************************************************************/

class links  {

    var $_n_days_links;                // Number of days with links (from today and older)
    var $_n_days_picks;                // Number of days with picks
    var $_categories_links;            // Array with all textlink categories
    var $_categories_picks;            // Array with all pick categories

    var $_ok_files = array();  // Paths to already downloaded, OK link and pick files


function links($n_days_links=0,$n_days_picks=0,$categories_links=0,$categories_picks=0)  {
/***********************************************************************
* Constructor
***********************************************************************/

    // The parameters not passed as arguments should be read from file
    if(!$n_days_links)  {
        $this->_n_days_links    = $this->binary_read('ownlinksite/pagedata/n_days_links.txt');
    } else {
        $this->_n_days_links    = $n_days_links;
    }
    if(!$n_days_picks)  {
        $this->_n_days_picks    = $this->binary_read('ownlinksite/pagedata/n_days_picks.txt');
    } else {
        $this->_n_days_picks    = $n_days_picks;
    }
    if(!is_array($categories_links))  {
        $this->_categories_links = array_keys(unserialize($this->binary_read('ownlinksite/pagedata/links_each_cat.txt')));
    } else {
        $this->_categories_links = $categories_links;
    }
    if(!is_array($categories_picks))  {
        $this->_categories_picks = array_keys(unserialize($this->binary_read('ownlinksite/pagedata/picks_each_cat.txt')));
    } else {
        $this->_categories_picks = $categories_picks;
    }

    // Check for available links and picks (those downloaded before)
    $this->downloaded_files();
}

function check_downloaded_files($timestamp_start,$links_or_picks='links')  {
/***********************************************************************
* Fills in _ok_files for links or picks
***********************************************************************/

    if($links_or_picks=='links')  {
        $n_days = $this->_n_days_links;
        $prefix  = 'x';
        $categories_links = '_categories_links';
    } else { // Asume picks
        $n_days = $this->_n_days_picks;
        $prefix  = 'y';
        $categories_links = '_categories_picks';
    }
    
    // Check for available files, and fill in if missing
    if(is_array($this->$categories_links)) {
    foreach($this->$categories_links as $id_category)  {
        if(isset(  $link_files)) unset($link_files);    // Empty
        if(isset($no_link_files)) unset($no_link_files); // Empty

        for($day_of_interest=0; $day_of_interest<$n_days; $day_of_interest++)  {
            $day_index = $this->day_index($timestamp_start,$n_days,$day_of_interest);

            $file = "ownlinksite/links/$prefix"."_$id_category"."_$day_index.txt";
            
            // Use if it exists. For picks, the picks themselves must also exist.
            if(file_exists($file) && 
            ($links_or_picks=='links' || $this->all_picks_ok($file,$id_category,$day_index)))  {
                $link_files["$prefix$day_of_interest"]      = $file; // OK file
            } else { 
                $no_link_files  ["$prefix$day_of_interest"] = '';  // Cannot use this one
            }
        }

        // If any day of interest for this category does not have a file: Use another
        if(isset($link_files))  {
        if(isset($no_link_files) && is_array($link_files))  {
            $link_copy = $link_files;
            foreach($no_link_files as $filename=>$file)  {
                $value = array_shift($link_copy);
                $no_link_files[$filename] = $value;
                $link_copy[] = $value; // Make sure it does not run dry
            }
            $this->_ok_files[$links_or_picks][$id_category] = array_merge($link_files,$no_link_files);
        } else {
            $this->_ok_files[$links_or_picks][$id_category] = $link_files; // Best, we have all!
        }
        }
    }
    }
}

function downloaded_files()  {
/***********************************************************************
* Creates an array holding all available x-files and y-files,
* using the same several times when necessary
***********************************************************************/
    $timestamp_start = $this->read_value('timestamp_start');
    
    $this->check_downloaded_files($timestamp_start,'links');
    $this->check_downloaded_files($timestamp_start,'picks');    
}

function all_picks_ok($file,$id_category,$day_index)  {
/***********************************************************************
* 23.12.2003 Checks if all picks this file points to exist
***********************************************************************/

    // Determine how many picks this category should contain
    $n_picks = count(unserialize($this->binary_read($file)));
    
    // Return fals if any of those picks have not been downloaded
    for($i=0; $i<$n_picks; $i++)  {
        $pick_file = "ownlinksite/picks/y_$id_category"."_$i"."_$day_index.jpg";
        //print "<br>pick_file = $pick_file";
        if(!file_exists($pick_file)) return false;
    }
    return true;
}


function binary_read($url)  {
/***********************************************************************
* Uses the binary reader fread to grab and return web content or file
***********************************************************************/
    $x = '';
    if($fp = fopen($url,"rb"))  {
        while(!feof($fp)) {
            $x.= fread($fp,1024);  // Read 1024 bytes at a time
        }
        fclose($fp);
        return $x;  // Success, return result
    } else {
        return 0;  // Could not open, return false
    }
}

function read_value($name)  {
/***********************************************************************
* Returns the content of ownlinksite/pagedata/$name.txt
***********************************************************************/
    $file = "ownlinksite/pagedata/$name.txt";
    if(file_exists($file))  {
        return trim($this->binary_read($file));
    } else {
        return 0;
    }
}

function day_index($timestamp_start,$n_days,$day_of_interest=0)  {
/***********************************************************************
* Returns the day index for for the day of interest
* Ex.: a for day 0, b for day 1. If 3 days are included, it starts on a
*      again on the 4. day
*
* $days_from_start = The timestamp (in seconds) when the client started
*                    using ownlinksite.
* $n_days          = The number of days the client has information from 
*                  = on his web. (ex.: Every second day over 6 days is 3)
* $day_of_interest  = Number of days since the day of intrest (ex. 0=today,
*                    1=yesterday)
***********************************************************************/
    // 26 days possible, but use only $n_days of them. First time, start with a.
    $indexes = explode(',','a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z');
    
    // Number of days from start to day of interest, rounded off up or down to nearest whole day
    $days_from_start = (int)((time()-$timestamp_start)/(3600*24) + .5) - $day_of_interest;
    
    // Go up or down n_days at a time until the day is within acceptable limits
    while ($days_from_start < 0)          {
        $days_from_start += $n_days;
    }
    while ($days_from_start > $n_days-1)  {
        $days_from_start -= $n_days;
    }
    return $indexes[$days_from_start];
}

function create_date()  {
/***********************************************************************
* Create a date on the correct format
***********************************************************************/
    // Rember between calls, to avoid reading file more than once
    static $date_format;
    
    // Return false if previous calls concluded no date format exists
    if($date_format==-1 || strlen($date_format>1)) return 0;
    
    // Return the format directly if read during previous calls
    if(strlen($date_format)>1)                    return $date_format;
    
    // Read the data format from file if not read before
    if($date_format_fromfile=$this->read_value('date_format'))  {
        $date_format = $date_format_fromfile;
        return $date_format;
    } else {  // No date format found, probaly not using dates on page
        $date_format = -1; // Use this value to remember we have checked
        return 0;
    }
}

function date_and_links_to_arrays()  {
/***********************************************************************
* Puts dates and links into an array which is easy to convert to the
* arrays date, x and y
***********************************************************************/

    // Give values to the date array
    for($i=0; $i<$this->_n_days_links; $i++)  {
        // Put value on date variable if dates are used on page
        if($date_format=$this->create_date()) {
            $date_name  = 'date'.$i;
            $date_arr[$date_name] = date($date_format, (time()-24*3600*$i));
        }
    }
    if(!isset($date_arr)) $date_arr = array(0);  // Avoid error if dates are not in use
    
    
    // Give values to the main array
    foreach($this->_ok_files as $links_or_picks => $value1)  {
        foreach($value1 as $id_category => $value2)  {
            if(is_array($value2))  {
                foreach($value2 as $array_name => $file)  {
                    $xy_arr[$array_name][$id_category] = unserialize($this->binary_read($file));
                }
            }
        }
    }
    return array($date_arr,$xy_arr);
}
} // END CLASS links

    $links = new links($n_days_links,$n_days_picks,$categories_links,$categories_picks);
    list($date_arr,$xy_arr) = $links->date_and_links_to_arrays(); // Get values for variables
    unset($links);  // Free the memory as soon as possible
    
    // Put the dates into variables
    if(is_array($date_arr))  {
        foreach($date_arr as $array_name => $value)  {
        $$array_name = $value;
        }
    }
    
    // Put the x and y variables into arrays
    if(is_array($xy_arr))  {
        foreach($xy_arr as $array_name => $value)  {
        $$array_name = $value;
        }
    }
?>
Return current item: Link Collector