Location: PHPKode > scripts > Link Collector > link-collector/linkbase.php
<?  //  pagetransform.php
/***********************************************************************
*
*    ***  pagetransform.php PROGRAM LISTING         ***
*    ***  How to use is outlined at ownlinksite.com ***
*
* 27.12.2002 V 1.0 Beta
* 12.04.2003 V 1.1 Stable
* 19.07.2003 V 2.0 Major update
* 21.09.2003 V 2.1 Some small bug fixes
*
* Place the code in a sub-directory called ownlinksite and name it
* pagetransform.php.
* Also, put links.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 pagetransform  {

    // URL to where the OwnLinkSite database resides:
    var $_url_linkbase = 'ownlinksite.com/pagetransform_base.php';

    var $_msg_arr;  // Errors, warnings and messages created in this
                    // program, to be displayed to user

function pagetransform()  {
/***********************************************************************
* Constructor. When the object is created, do the whole job.
***********************************************************************/

}

function check_dirs()  {
/***********************************************************************
* Creates directories pagedata,links and picks if they do not exist already
***********************************************************************/

    $dirs = array('pagedata','links','picks');

    foreach($dirs as $directory)  {
        if(!is_dir($directory)) { // Does not exist, create it
            if(!mkdir($directory, 0777))  { // Could not create, give warning
                $this->_msg_arr[] = "<b>Warning:</b>Tried to create directory 
                                    <b>$directory</b>, but did not succseed.
                                    Try creating it manually or change the permissions.
                                    This problem is explained in greater detail
                                    in the tutoril in ownlinksite.com";
        } else {
            $this->_msg_arr[] = "<b>Info:</b>Directory $directory created successfully";
            }
    } else {
        $this->_msg_arr[] = "<b>Info:</b>Directory $directory was there already, not created now";
        }
    }
}

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 {
        $this->_msg_arr[] = "<b>Warning:</b>Could not read $url";
        return 0;  // Could not open, return false
    }
}

function date_options()  {  
/***********************************************************************
* All different date formats the user may choose from.
* Returns an array with all formats.
***********************************************************************/
    return array('d-m','m-d','d-m-Y','M d.','D m-d','l m-d','D m-d-y',
                'D m-d-Y','D','l');
}

function date_dropdown()  {
/***********************************************************************
* Puts the dates listed in function date_options() into a dropdown
***********************************************************************/
    $date_options = $this->date_options();
    $x = '<select size="1" name="date_format">';
    foreach($date_options as $date_format)  {
        $x .= '<option value="'.$date_format.'">'.
              date($date_format, time()).'</option>';
    }
    $x .= '</select></p>';
    return $x;
}


function show_input_form()  {
/***********************************************************************
* Prints the form used to insert the template-HTML to be transformed
***********************************************************************/
    $x  = '<form method="POST" action="pagetransform.php">';
    
    $x .= '&nbsp;<p><b><font face="Verdana">'.
          'If you use dates on your page, select preferred format: '. $this->date_dropdown();
    $x .= 'If you want the finished page to be saved automatically,<br>please write its name here
          <input type="text" name="file_name" size="20" value="index.php">';
    $x .= '</p> <br>Paste your template HTML into the text area below&nbsp; ';
    if(isset($_GET['blocker'])) $x .= '<input type="hidden" name="blocker" value="'.$_GET['blocker'].'">';
    $x .= '&nbsp;<input type="submit" value="Submit">';
    $x .= '<p><textarea rows="16" name="page" cols="60"></textarea>';
    $x .= '</font></b></p></form>';
    
    return $x;
}

function show_code($text)  {
/***********************************************************************
* Transforms the text to make it possible to show it via browser
* Tags are replaced by their specialchrs equivalents, and new lines 
* like \n are replaced by <br>
***********************************************************************/
    $table_start = '<br></div><table width=100% border=1 cellpadding=0 cellspacing=0>
                    <tr><td bgcolor="#FFFFCC">';
    $table_stop  = '</br></td></tr></table>';

    return $table_start.nl2br(htmlspecialchars($text)).$table_stop;
}

function make_post_request($post_array) {
/***********************************************************************
//* 28.09.2003 
* Makes a POST request to the main OwnLinkSite server
* The main server then returns the complete php code.
***********************************************************************/
    $url = $this->_url_linkbase;
    $url = eregi_replace("^http://", "", $url);
    
//  Separate into Host and URI
    $host = substr($url, 0, strpos($url, "/"));
    $uri  = strstr($url, "/");

//  Form up the request body
    $post_vars = "";
    while (list($key, $val) = each($post_array)) {
      if ($post_vars) $post_vars.= "&";
      $post_vars.= $key."=".urlencode($val);
    }

    //  Text used in the request header:
    $header_text =
      "POST $uri HTTP/1.0\n".
      "Host: $host\n".
      "Content-Type: application/x-www-form-urlencoded\n".
      "Content-Length: ".strlen($post_vars)."\n\n".
      "$post_vars\n";

    //  Open the connection to the host
    $socket = fsockopen($host, 80);
    $x = '';
    fputs($socket, $header_text);
    while (!feof($socket)) {
        $x .= fgets($socket, 1024);
    }
    return $x;
}

function get_quasi_xml($x,$tag)  {
/***********************************************************************
* Some parameters return from the OwnLinkSite server as text between 
* tag-like brackets. This function isolates text of interest by
* stripping out everything between <tag> and </tag> in the text x 
***********************************************************************/
    return substr(strstr(substr($x, 0, strpos($x, "</$tag>")),
          "<$tag>"), strlen("<$tag>"));
}

function parameters_to_save()  {
/***********************************************************************
* Lists all the constants and arrays to be saved on the client's 
* harddisk (transferred from pagetransform_base.php) on the OwnLinkSite
* server. All arrays are stored serialized.
*
* n_days          = Number of days categories are shown from.
* date_format    = The date format used for all dates on page
* pick_b          = How broad the pix shall be on display, in pixels
* pick_h          = How tall the pixs are
* links_each_cat  = Array with number of links in each category 
* picks_each_cat    = Array with mumber of pix in each category. Note it is the
*                  displayed number which is stored, not the (much higher)
*                  number of pick links passed from main server.
* var            = Array containing all categories in use, like url, short, ..
* bookmarks      = Used as bookmark to know where to select records
*                  next time.
***********************************************************************/
    return array('n_days_links','n_days_picks',
                'date_format','pick_b','pick_h','user',
                'links_each_cat','picks_each_cat','var','bookmarks');
}

function save_to_file($x, $file)  {
/***********************************************************************
//* 20.09.2003
* Saves $x to file $file
***********************************************************************/
    if($fp = fopen($file, 'w'))  {
        fwrite($fp, $x);
        fclose($fp);
        return 1;
    } else {
        $dir_info = '';
        if(strlen(trim(eregi_replace("/[^/]*$",'',$file))) > 0)
            $dir_info = 'for directory '.eregi_replace("/[^/]*$",'',$file);
        $this->_msg_arr[] = "<b>Warning:</b>Could not open file $file and save
                            - please check access permissions ".$dir_info;
        return 0;
    }
}

function save_parameters($x)  {
/***********************************************************************
* Saves those parameters present in $x
***********************************************************************/
    foreach($this->parameters_to_save() as $par_name)  {
        $parameter = $this->get_quasi_xml($x,$par_name);
        if(strlen($parameter)!=0 && strlen($parameter) < strlen($x))  {
            $this->save_to_file($parameter,"pagedata/$par_name.txt");
        //  print "<br>Saved $par_name";
        }
    }
}

function messages_presentable()  {
/***********************************************************************
* Returns the messages in a neat table
***********************************************************************/

    $x  = '<div align="center">
          <table border="1" cellpadding="0" cellspacing="0" width="80%">
          <tr><td bgcolor="#FF00FF" align="center"><h2>Messages from pagetransform.php: </h2>
          </td></tr><tr><td>';

    if(is_array($this->_msg_arr))  { // Put some colors on the messages
        $x .= str_replace('Warning:','<font color="#0000FF">Warning:</font>',
              str_replace('Error:','<font color="#FF0000">Error:</font>',
              str_replace('</b>','</b><br>',implode('<br>',$this->_msg_arr))))
            .'</td></tr>';
    } else {
        $x .= '<b>None.</b> Everything went well</td></tr>';
    }
    $x .= '</table></div>';
    if(is_array($this->_msg_arr))  {
        return $x;
    } else {
        return 0;
    }
}

function transform_and_save()  {
/***********************************************************************
* Transfers the page and saves the result
***********************************************************************/

    $this->check_dirs();  // Create the directories pagedata, links, picks
    
    $x  = '<table width="100%" border="0" cellpadding="0" cellspacing="0">';
    $x .= '<tr><td bgcolor="#FFCC33" align="center"><br><h2>';
    $x .= '<font face="Verdana">Transforming the template to php-code</font>';
    $x .= '</h2></td></tr>';
  
    if(!isset($_POST['page'])) {  // The form not shown alrady, do it now
        $x .= '<tr><td align="center">'.$this->show_input_form().'</td></tr>';
    } else {
        // Date format and day of interest is saved directly
        $this->save_to_file($_POST['date_format'],'pagedata/date_format.txt');
        $this->save_to_file(0,'pagedata/day_of_interest.txt');  // Generally today
        
        $page  = stripslashes($this->make_post_request($_POST)); 
        
        
        $show=stripslashes($this->get_quasi_xml($page,'page'));

        if(strlen($_POST['file_name']) > 2)  {  // The user request auomatically saving the page
            if(!$this->save_to_file($show,'../'.$_POST['file_name']))  {  // Could not save
                $x .= '<tr><td align="left"><br><b><h3><font face="Verdana">'.
                      "<h2>Unable to save the file utomatically!</h2>
                      Please do it manually by saving this code
                      in the main directory and call it <i>$_POST[file_name]</i>
                      </font></h3></b>";
                $x .= $this->show_code($show);
            } else {

                $x .= '<tr><td align="left"><br><b><h3><font face="Verdana">';
                $x .= "The transformed file has been saved as $_POST[file_name]</h3>";
                $x .= "<b>Please run <a href=\"linkcollector.php\" target=\"_blank\">linkcollector.php</a>
                          to collect the first links and picks,
                      and have a look at your page!</b><br><br>";
            }
  
        } else {
      
            $x .= '<tr><td align="left"><br><b><h3><font face="Verdana">Put this 
                  code in the main directory and call it <i>index.php</i>
                  or whatever:</font></h3></b>';
            $x .= $this->show_code($show);            
        }

        $x .= '</td></tr>';
        $this->save_parameters($page); 
    }
    $x .= '<tr><td align="center"><b><h3><font face="Verdana">'.
          '<a href="index.php">home</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <a href="pagetransform.php">reset</a>'.
          '</font></h3></b></td></tr>';
    $x .= '</table>';
    
    if($this->messages_presentable()) $x .= $this->messages_presentable(); // Messages from this program
    if(isset($_POST['page'])) {
        if($this->get_quasi_xml($page,'message')) {
            $x .= $this->get_quasi_xml($page,'message');                  // Messages from server
        }
    }
    return $x;
}
} // END CLASS pagetransform.php



/*BLOCKER*/
    // Uncomment the if-line below to protect pagetransform.php from un-authorized use
    // After un-commenting, the program must be called with the blocker-parameter
    // (in addition to any other arguments to be passed) like so:
    // http://your-site.com/ownlinksite/pagetransform.php?blocker=avyinry
//    if($blocker != 'avyinry') exit();


    // Do the pagetransform
    $pagetransform = new pagetransform();
    print $pagetransform->transform_and_save();
?>
Return current item: Link Collector