<?php
 // Crontab Curl Collection of Sticky Posts and Change Number 2 at WordPress Blog Postings - RJM Programming - April, 2015

 set_time_limit(0);
 $ourcat = "42";
 if (isset($_GET['category'])) {
   $ourcat = $_GET['category'];
 }

 $retval = "";

 $hostname = 'localhost'; 

 /*** mysql username ***/
 $username = 'username';

 /*** mysql ***/
 $password = 'password';

 $dbname = 'database_name';
 if (isset($_GET['dbname'])) $dbname = $_GET['dbname'];
 $tname = 'wp_options';
 $t2name = 'wp_posts';

 $link = mysql_connect($hostname, $username, $password);
 if (!$link) {
    die('Could not connect: ' . mysql_error());
 }
 mysql_select_db($dbname);
 $res = mysql_query("SELECT " . $t2name . ".ID FROM " . $t2name . " 
LEFT JOIN " . str_replace("_posts", "_term_relationships", $t2name) . " ON 
(" . $t2name . ".ID = " . str_replace("_posts", "_term_relationships", $t2name) . ".object_id) 
LEFT JOIN " . str_replace("_posts", "_term_taxonomy", $t2name) . " ON 
(" . str_replace("_posts", "_term_relationships", $t2name) . ".term_taxonomy_id = " . str_replace("_posts", "_term_taxonomy", $t2name) . ".term_taxonomy_id) 
WHERE " . $t2name . ".post_status = 'publish' 
AND " . str_replace("_posts", "_term_taxonomy", $t2name) . ".taxonomy = 'category' 
AND " . str_replace("_posts", "_term_taxonomy", $t2name) . ".term_id = " . $outcat . " 
AND datediff(CURDATE(), " . $t2name . ".post_date) > 8");
 if ($res == 0) {
  echo("<b>Error " . mysql_errno() . ": " . mysql_error() . "</b>");
 } else if (mysql_num_rows($res) == 0) {
  echo("<b>Query executed successfully</b>");
 } else {
  while (($r_array = mysql_fetch_row($res))) {
    $retval .= ($delim . $r_array[0]);
    $delim = ",";
  }
 }
 if ($retval != "") {
    file_put_contents("software_posts_list.php", $retval);
 }
 $res = mysql_query("SELECT " . $tname . ".option_value FROM " . $tname . " WHERE " . $tname . ".option_name='sticky_posts'");
 if ($res == 0) {
  echo("<b>Error " . mysql_errno() . ": " . mysql_error() . "</b>");
 } else if (mysql_num_rows($res) == 0) {
  echo("<b>Query executed successfully</b>");
 } else {
  while (($r_array = mysql_fetch_row($res))) {
    $retval = $r_array[0];
  }
 }
 if ($retval != "") {
    file_put_contents("sticky_posts_list.php", $retval);
    // a:2:{i:0;i:316;i:1;i:7666;}
    $colbits = explode(":", $retval);
    if (sizeof($colbits) >= 7) {
      $was = str_replace(";", "", str_replace("}", "", $colbits[6]));
      if (file_exists("software_posts_list.php")) {
       $slistcont = @file_get_contents("software_posts_list.php");
       $slist = explode(",", $slistcont);
       $isnow = $was;
       while ($isnow == $was) {
         $isnow = $slist[rand(0, (sizeof($slist) - 1))];
       }
       $newval = str_replace(";i:" . $was . ";", ";i:" . $isnow . ";", $retval);
       mysql_query("UPDATE " . $tname . " SET " . $tname . ".option_value='" . $newval . "' WHERE " . $tname . ".option_name='sticky_posts'");
       file_put_contents("sticky_posts_list_new.php", $newval);
      }
    }
 }
 mysql_close($link);
?>

