<?
// Variables area
$mysqlhost "";
$mysqluser "";     // User has read-only access
$mysqluserpass "";
$mysqlsuper "";    // Super should only have 'insert', 'update', and 'delete' acccess
$mysqlsuperpass "";
$mysqldatabase "";
$mysqltableusers "";
$mysqltablefeeds "";

//
// Password & Account modification
//

   
if($PHP_SELF."".$_SERVER['QUERY_STRING'] == "settingsaccountupdate") {
    
mysql_connect($mysqlhost,$mysqluser,$mysqluserpass);
    
mysql_select_db($mysqldatabase);
     
$query "SELECT * FROM ".$mysqltableusers." WHERE active = 'yes' AND email = '".$_COOKIE['email']."' AND pass = '".sha1($_COOKIE['pass'])."'";
     
$result mysql_query($query);
     
$num_rows mysql_num_rows($result);
      while(
$row=mysql_fetch_array($result)) {
       
$ownerid $row['id'];
       
$name $row['name'];
       
$email $row['email'];
      }
      if(
$num_rows 0) {
// if both password fields are set
       
if(isset($_POST['pass1']) and ($_POST['pass2'])) {
// if both password fields are the same
        
if($_POST['pass1'] == $_POST['pass2']) {
        
mysql_connect($mysqlhost,$mysqlsuper,$mysqlsuperpass);
        
mysql_select_db($mysqldatabase);
         
$editquery "UPDATE `".$mysqltableusers."` SET `name`='".htmlspecialchars($_POST['name'])."', `email`='".htmlspecialchars($_POST['email'])."', `pass`='".sha1($_POST['pass1'])."', `active`='yes' where `id`=".$ownerid."";
         
$editresult mysql_query($editquery);
// kill off all the cookies so we dont get messages.
    
setcookie("status"""time()-3600);
    
setcookie("email"""time()-3600);
    
setcookie("pass"""time()-3600);
          echo(
"<br /><center><img src=\"mobilis.jpg\" /></center>
      <p />You have successfully changed your password and/or account information.<br /><a href=\"?\">You have to login again.</a><br />"
);
// if both password fields are different
        
} else {
         echo(
"<center><img src=\"mobilis.jpg\" /></center>
     <p />Sorry, but the passwords entered do no match. Please try again.<br /><a href=\"?settings\">Back</a><br />"
);
        }
// if neither password field is set
       
} else {
        
mysql_connect($mysqlhost,$mysqlsuper,$mysqlsuperpass);
        
mysql_select_db($mysqldatabase);
         
$editquery "UPDATE `".$mysqltableusers."` SET `name`='".htmlspecialchars($_POST['name'])."', `email`='".htmlspecialchars($_POST['email'])."', `active`='yes' where `id`=".$ownerid."";
         
$editresult mysql_query($editquery);
          echo(
"<a href=\"?\">Home</a> | <a href=\"?settings\">Edit Settings</a> | <a href=\"?logout\">Logout</a><br />
      <center><img src=\"mobilis.jpg\" /></center>
      <p />"
);
          echo(
"<center><img src=\"mobilis.jpg\" /></center>
      <p />Successfully changed your account information.<br /><a href=\"?settings\">Back to Settings</a>"
);
       }
      }
   } 

//
// Password validation & cookie setup
//
 
if($PHP_SELF."".$_SERVER['QUERY_STRING'] == "validate") {
   
mysql_connect($mysqlhost,$mysqluser,$mysqluserpass);
   
mysql_select_db($mysqldatabase);
    
$query "SELECT * FROM ".$mysqltableusers." WHERE active = 'yes' AND email = '".$_POST['email']."' AND pass = '".sha1($_POST['pass'])."'";
    
$result mysql_query($query);
    
$num_rows mysql_num_rows($result);
     if(
$num_rows 0) {
      
setcookie("status""loggedin"time()+60*60*24*365);
      
setcookie("email"$_POST['email'], time()+60*60*24*365);
      
setcookie("pass"$_POST['pass'], time()+60*60*24*365);
       
header("Location: ?");
     } else {
      echo(
"<br /><center><img src=\"mobilis.jpg\" /></center>
          <p />Invalid Login<br />Make sure you are logging in using the email you signed up with.<br /><a href=\"?\">Please try again.</a>"
);
     }
 }
//
// A way to kill the cookies
//
 
if ($PHP_SELF."".$_SERVER['QUERY_STRING'] == "logout") {
     
setcookie("status"""time()-3600);
     
setcookie("email"""time()-3600);
     
setcookie("pass"""time()-3600);
      
header("Location: ?");
 }
//
// Predefined functions here
//

// allow users to specify the maximum amount of characters displayed
 
function truncate($string,$length) {
  
$string=trim($string);
   if(
strlen($string)>$length) {
    
$string=substr($string,0,$length);
     
$n=0
      while(
substr($string,-1)!=chr(32)) {
       
$n++;
       
$string=substr($string,0,$length-$n);
      }
    
$string=trim($string)."...";
   }
  return 
$string;
 }

// main rss feed module
 
function module_custom($url,$title,$limit,$length) {
  
$count 0;
  
$feed fetch_rss($url);
   echo 
"<table><tr><th>".$title."</th></tr>\n";
    foreach (
array_slice($feed->items0$limit) as $item) {
     
$rowid = ($count 2) ? "2" "1";
      echo 
"
       <tr class=\"row"
.$rowid."\">
        <td class=\"tdr\"><a href=\""
.$item['link']."\">".truncate($item['title'], $length)."</a></td>
       </tr>\n
      "
;
     
$count++;
    }
  echo(
"</table>");
  echo(
"<p />\n");
 }

?>
<html>
 <head>
  <title>Mobilis</title>
  <style>
   body {
    font-size:small;
   }
   a {
   color:#000000;
   text-decoration:underline;
   }
   table a {
    text-decoration:none;
   }
   table {
    border:1px solid #000000;
    width:100%;
    background:#C3D9FF;
    font-size:small;
   }
   .row1 {
    background:#E8EEF7;
   }
   .row2 {
    background:#f5f5f5;
   }
   .tdl {
    white-space:nowrap;
    overflow:hidden;
   }
   .tdr {
    white-space:nowrap;
    overflow:hidden;
   }
  </style>
 </head>
 <body>
 <?
//
// Display login form if the status cookie isnt set
//
 
if($_COOKIE['status'] != "loggedin") {
  if(
$PHP_SELF."".$_SERVER['QUERY_STRING'] == "") {
   echo(
"$message<br />
     <center><img src=\"mobilis.jpg\" /></center>
     <p />
     <form method=\"post\" action=\"?validate\">
      <table>
       <tr>
        <th colspan=\"2\">Please Login</th>
       </tr>
       <tr class=\"row1\">
        <td>E-Mail:</td><td><input name=\"email\" id=\"email\" /></td>
       </tr>
       <tr class=\"row2\">
        <td>Password:</td><td><input name=\"pass\" id=\"pass\" type=\"password\" /></td>
       </tr>
       <tr class=\"row1\">
        <th colspan=\"2\"><input type=\"submit\" value=\"Submit\" /><input type=\"reset\" value=\"Reset\" /></th>
       </tr>
      </table>
     </form>
     <p />
     If you don't have an account, signup <b><a href=\"?signup\">here</a></b>.<br />
     You can view the source of Mobilis <b><a href=\"index.phps\">here</a></b>.<br />
     You can grab the SQL of the tables <b><a href=\"setup.sql\">here</a></b>.
   "
);
  }
  if(
$PHP_SELF."".$_SERVER['QUERY_STRING'] == "signup") {
   echo(
"
    <center><img src=\"mobilis.jpg\" /></center>
    <p />
   <form method=\"post\" action=\"?signupfinish\" />
    Blah blah blah blah, who reads those pesky terms of service or eula messages anyway.<br />
    All I ask is that you don't break it. But if you do, atleast help me figure out a way to fix it.<br />
    This software is released under the <a href=\"http://www.gnu.org/copyleft/gpl.html\" target=\"_blank\">GNU General Public License</a> and uses <a href=\"http://magpierss.sourceforge.net/\" target=\"_blank\">MagpieRSS</a> for its backend.<p />
    With all the legal mumbo jumbo out of the way I'll let you signup now.<p />
    <table>
     <tr><th colspan=\"2\">Signup Form</th></tr>
     <tr class=\"row1\"><td>What's your name?</td><td><input type=\"text\" name=\"name\" /></td></tr>
     <tr class=\"row2\"><td>What's your email?</td><td><input type=\"text\" name=\"email\" /></td></tr>
     <tr class=\"row1\"><td>What password do you want?</td><td><input type=\"password\" name=\"pass1\" /></td></tr>
     <tr class=\"row2\"><td>Repeat the password:</td><td><input type=\"password\" name=\"pass2\" /></td></tr>
     <tr class=\"row1\"><td colspan=\"2\" align=\"right\"><input type=\"submit\" value=\"Submit\" /></td></tr>
    </table>
   "
);
  }
  if(
$PHP_SELF."".$_SERVER['QUERY_STRING'] == "signupfinish") {
   if(isset(
$_POST['email']) and ($_POST['name']) and ($_POST['pass1']) and ($_POST['pass2'])) {
    if(
$_POST['pass1'] == $_POST['pass2']) {
     
mysql_connect($mysqlhost,$mysqlsuper,$mysqlsuperpass);
     
mysql_select_db($mysqldatabase);
      
$adduserquery "INSERT INTO `".$mysqltableusers."` (`id`, `email`, `name`, `pass`, `active`) VALUES('', '".$_POST['email']."', '".$_POST['name']."', '".sha1($_POST['pass1'])."','yes');";
      
$adduserresult mysql_query($adduserquery);

     
$query "SELECT * FROM ".$mysqltableusers." WHERE active = 'yes' AND email = '".$_POST['email']."' AND pass = '".sha1($_POST['pass1'])."'";
     
$result mysql_query($query);
     
$num_rows mysql_num_rows($result);
     while(
$row=mysql_fetch_array($result)) {
      
$ownerid $row['id'];
     }
      if(
$num_rows 0) {
       
$addquery "INSERT INTO `".$mysqltablefeeds."` (`id`, `owner`, `title`, `feed`, `limit`, `length`, `disabled`, `position`) VALUES('', '".$ownerid."', 'Default Feed - Slashdot', 'http://rss.slashdot.org/Slashdot/slashdot', '10', '128', 'no', '1');";
       
$addresult mysql_query($addquery) or die(mysql_error());
      }
    echo(
"
    <center><img src=\"mobilis.jpg\" /></center>
    <p />    
    Your account has been setup.<br /><b><a href=\"?\">You may login now.</a></b>"
);
    } else {
     echo(
"
    <center><img src=\"mobilis.jpg\" /></center>
    <p />
    The passwords specified don't match."
);
    }
   } else {
    echo(
"
    <center><img src=\"mobilis.jpg\" /></center>
    <p /> 
    All fields must be filled out."
);
   }
  }
 }
//
// Display stuff here
//
  
if ($_COOKIE['status']=="loggedin") {
// Homepage display
   
if($PHP_SELF."".$_SERVER['QUERY_STRING'] == "") {
   echo(
"<a href=\"?\">Home</a> | <a href=\"?settings\">Edit Settings</a> | <a href=\"?logout\">Logout</a><br />
    <center><img src=\"mobilis.jpg\" /></center>
    <p />"
);
    
mysql_connect($mysqlhost,$mysqluser,$mysqluserpass);
    
mysql_select_db($mysqldatabase);
     
$query "SELECT * FROM ".$mysqltableusers." WHERE active = 'yes' AND email = '".$_COOKIE['email']."' AND pass = '".sha1($_COOKIE['pass'])."'";
     
$result mysql_query($query);
     
$num_rows mysql_num_rows($result);
     while(
$row=mysql_fetch_array($result)) {
      
$ownerid $row['id'];
     }
      if(
$num_rows 0) {
        require_once 
'magpierss/rss_fetch.inc';
         
$result1 mysql_query("select * from ".$mysqltablefeeds." where owner='".$ownerid."' and disabled='no' order by position asc");
          while(
$r=mysql_fetch_array($result1)) {
           
module_custom($r['feed'],$r['title'],$r['limit'],$r['length']);
     }
      } else {
       echo(
"Sorry, the login credentials supplied are invalid.<br />
    <center><img src=\"mobilis.jpg\" /></center>
      <form method=\"post\" action=\"?validate\">
       <table>
        <tr>
         <th colspan=\"2\">Please Login</th>
        </tr>
        <tr class=\"row1\">
         <td>E-Mail:</td><td><input name=\"email\" id=\"email\" /></td>
        </tr>
        <tr class=\"row2\">
         <td>Password:</td><td><input name=\"pass\" id=\"pass\" type=\"password\" /></td>
        </tr>
        <tr class=\"row1\">
         <th colspan=\"2\"><input type=\"submit\" value=\"Submit\" /><input type=\"reset\" value=\"Reset\" /></th>
        </tr>
       </table>
      </form>
     "
);
      }
   }

//
// Settings display
//
   
if($PHP_SELF."".$_SERVER['QUERY_STRING'] == "settings") {
   echo(
"<a href=\"?\">Home</a> | <a href=\"?settings\">Edit Settings</a> | <a href=\"?logout\">Logout</a><br />
    <center><img src=\"mobilis.jpg\" /></center>
    <p />"
);
    
mysql_connect($mysqlhost,$mysqluser,$mysqluserpass);
    
mysql_select_db($mysqldatabase);
     
$query "SELECT * FROM ".$mysqltableusers." WHERE active = 'yes' AND email = '".$_COOKIE['email']."' AND pass = '".sha1($_COOKIE['pass'])."'";
     
$result mysql_query($query);
     
$num_rows mysql_num_rows($result);
      while(
$row=mysql_fetch_array($result)) {
       
$ownerid $row['id'];
       
$name $row['name'];
       
$email $row['email'];
      }
      if(
$num_rows 0) {
//
// Display RSS Feed Add Module
//
     
echo("
      <form method=\"post\" action=\"?settingsaddfeed\">
       <table cellspacing=\"1\" cellpadding=\"0\"><tr><th colspan=\"3\">Add RSS Feed</th></tr>
        <tr class=\"row1\"><td>&nbsp;<b>Title:</b></td><td colspan=\"2\"> <input type=\"text\" name=\"title\" /></td></tr>
        <tr class=\"row2\"><td>&nbsp;<b>URL:</b></td><td colspan=\"2\"> <input type=\"text\" name=\"feed\" /></td></tr>
        <tr class=\"row1\"><td colspan=\"3\">&nbsp;
            <b>Items:</b> <input type=\"text\" size=\"2\" name=\"items\" value=\"10\" />
            <b>Length:</b> <input type=\"text\" size=\"2\" name=\"length\" value=\"128\" />
            <b>Priority:</b> <input type=\"text\" size=\"2\" name=\"priority\" /></td>
        </tr>
        <tr class=\"row2\"><td colspan=\"3\" align=\"right\"><input type=\"submit\" value=\"Submit\" /></td></tr>
       </table>
      </form>"
); 
//
// Display RSS Feed Edit Module
//
      
echo("
       <table cellspacing=\"1\" cellpadding=\"0\"><tr><th colspan=\"4\">Edit RSS Feeds</th></tr>\n"
);
       
$displayresults mysql_query("select * from ".$mysqltablefeeds." where owner='".$ownerid."' order by position asc ");
       
$count 0;
        while(
$r=mysql_fetch_array($displayresults)) {
         
$rowid = ($count 2) ? "2" "1";
         
$disabled['1'] = $r['disabled'];
         if(
$disabled['1'] == "no"$disabled['2'] = "yes";
         if(
$disabled['1'] == "yes"$disabled['2'] = "no";
         echo(
"
      <form method=\"post\" action=\"?settingseditfeeds\">
        <input type=\"hidden\" name=\"id\" value=\""
.$r['id']."\" />
       <tr class=\"row"
.$rowid."\">
            <td colspan=\"4\">&nbsp;<b>Title:</b> <input type=\"text\" name=\"title\" value=\""
.$r['title']."\" size=\"15\" />
        <b>Delete:</b> <input type=\"checkbox\" name=\"del\" value=\"true\" />
        </td>
           </tr>
       <tr>
        <td class=\"row"
.$rowid."\" colspan=\"4\">&nbsp;<b>URL:</b> <input type=\"text\" name=\"url\" size=\"30\" value=\"".$r['feed']."\" /></td>
       </tr>
           <tr class=\"row"
.$rowid."\">
            <td align=\"right\"><b>Items:</b></td>
        <td><input type=\"text\" size=\"2\" name=\"limit\" value=\""
.$r['limit']."\" /></td>
        <td align=\"right\"><b>Length:</b></td>
        <td><input type=\"text\" size=\"3\" name=\"length\" value=\""
.$r['length']."\" />
            </td>
           </tr>       
           <tr class=\"row"
.$rowid."\">
            <td align=\"right\"><b>Priority:</b></td>
        <td><input type=\"text\" name=\"position\" value=\""
.$r['position']."\" size=\"2\" /></td>
        <td align=\"right\"><b>Disabled:</b></td>
        <td>
         <select name=\"disabled\">
              <option value=\""
.$disabled['1']."\">".$disabled['1']."</option>
              <option value=\""
.$disabled['2']."\">".$disabled['2']."</option>
             </select>
            </td>
           </tr>
           <tr class=\"row"
.$rowid."\">
            <td colspan=\"4\" align=\"right\"><input type=\"submit\" value=\"Submit\" /></td>
           </tr>
          </form>
       \n"
);
     
$count++;
        }
       echo(
"
      </table>
     <p />
       "
);


// 
// Account Setup Form
//
     
echo("
      <form action=\"?settingsaccountupdate\" method=\"post\">
       <table cellspacing=\"1\" cellpadding=\"0\">
        <tr>
     <th colspan=\"2\">Account Settings</th>
    </tr>
    <tr class=\"row1\">
     <td>&nbsp;<b>Display Name:</b> </td><td><input type=\"text\" name=\"name\" value=\""
.$name."\" /></td>
    </tr>
    <tr class=\"row2\">
     <td>&nbsp;<b>Email Address:</b> </td><td><input type=\"text\" name=\"email\" value=\""
.$email."\" /></td>
    </tr>
    <tr class=\"row1\">
     <td>&nbsp;<b>Password:</b> <td><input type=\"password\" name=\"pass1\" /></td>
    </tr>
    <tr class=\"row2\">
     <td>&nbsp;<b>Password (Verify):</b> <td><input type=\"password\" name=\"pass2\" /></td>     
        </tr>
    <tr class=\"row1\">
     <td colspan=\"2\" align=\"right\">
      <input type=\"submit\" value=\"Submit\" />
     </td>
    </tr>
       </table>
      </form>
     "
);

    }
   }
  
//
// Backend for adding feeds
//
   
if($PHP_SELF."".$_SERVER['QUERY_STRING'] == "settingsaddfeed") {
    
mysql_connect($mysqlhost,$mysqluser,$mysqluserpass);
    
mysql_select_db($mysqldatabase);
     
$query "SELECT * FROM ".$mysqltableusers." WHERE active = 'yes' AND email = '".$_COOKIE['email']."' AND pass = '".sha1($_COOKIE['pass'])."'";
     
$result mysql_query($query);
     
$num_rows mysql_num_rows($result);
      while(
$row=mysql_fetch_array($result)) {
       
$ownerid $row['id'];
       
$name $row['name'];
       
$email $row['email'];
      }
      if(
$num_rows 0) {
   
// Check to make sure a valid url was entered
    
if ((trim($_POST['feed']) == '') || !ereg("^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\._\?\,\'/\\\+&%\$#\=~\-])*$"$_POST['feed'])) {
     die(
"Please enter a valid url.");
    }
   
// Check to make sure the limit is a number
    
if (!is_numeric($_POST['items'])) {
        die(
'ERROR: Please enter a valid numerical item limit');
    }
   
// Check to make sure the length is a number
    
if (!is_numeric($_POST['length'])) {
        die(
'ERROR: Please enter a valid numerical length');
    }
   
// Check to make sure the priority is a number
    
if (!is_numeric($_POST['priority'])) {
        die(
'ERROR: Please enter a valid numerical priority');
    }
        
mysql_connect($mysqlhost,$mysqlsuper,$mysqlsuperpass);
        
mysql_select_db($mysqldatabase);
         
$addquery "INSERT INTO `".$mysqltablefeeds."` (`id`, `owner`, `title`, `feed`, `limit`, `length`, `disabled`, `position`) VALUES('', '".$ownerid."', '".htmlspecialchars($_POST['title'])."', '".htmlspecialchars($_POST['feed'])."', '".htmlspecialchars($_POST['items'])."', '".htmlspecialchars($_POST['length'])."', 'no', '".htmlspecialchars($_POST['priority'])."');";  
         
$addresult mysql_query($addquery) or die(mysql_error());
           echo(
"<center><img src=\"mobilis.jpg\" /></center>
       <p />Successfully added "
.$_POST['title']."!<br />
           <a href=\"?settings\">Back to Settings</a>"
);
      }
   }
//
// Backend for editing feeds
//
   
if($PHP_SELF."".$_SERVER['QUERY_STRING'] == "settingseditfeeds") {

   
// Check to make sure a valid url was entered
    
if ((trim($_POST['url']) == '') || !ereg("^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\._\?\,\'/\\\+&%\$#\=~\-])*$"$_POST['url'])) {
     die(
"Please enter a valid url.");
    }
   
// Check to make sure the limit is a number
    
if (!is_numeric($_POST['limit'])) {
        die(
'ERROR: Please enter a valid numerical item limit');
    }
   
// Check to make sure the length is a number
    
if (!is_numeric($_POST['length'])) {
        die(
'ERROR: Please enter a valid numerical length');
    }   
// Check to make sure the priority is a number
    
if (!is_numeric($_POST['position'])) {
        die(
'ERROR: Please enter a valid numerical priority');
    }

    
mysql_connect($mysqlhost,$mysqluser,$mysqluserpass);
    
mysql_select_db($mysqldatabase);
     
$query "SELECT * FROM ".$mysqltableusers." WHERE active = 'yes' AND email = '".$_COOKIE['email']."' AND pass = '".sha1($_COOKIE['pass'])."'";
     
$result mysql_query($query);
     
$num_rows mysql_num_rows($result);
      while(
$row=mysql_fetch_array($result)) {
       
$ownerid $row['id'];
       
$name $row['name'];
       
$email $row['email'];
      }
      if(
$num_rows 0) {
       if(isset(
$_POST['del'])) {
        
mysql_connect($mysqlhost,$mysqlsuper,$mysqlsuperpass);
        
mysql_select_db($mysqldatabase);
         
$delquery "DELETE FROM `".$mysqltablefeeds."` where id=".$_POST['id']." and owner=".$ownerid."";
         
$delresult mysql_query($delquery);
          echo(
"<center><img src=\"mobilis.jpg\" /></center>
      <p />Successfully deleted "
.$_POST['title']."!<br />
      <a href=\"?\">Back to Settings</a>"
);
       } else {
        
mysql_connect($mysqlhost,$mysqlsuper,$mysqlsuperpass);        
        
mysql_select_db($mysqldatabase);
         
$editquery "UPDATE `".$mysqltablefeeds."` SET `title`='".htmlspecialchars($_POST['title'])."', `feed`='".htmlspecialchars($_POST['url'])."', `limit`='".htmlspecialchars($_POST['limit'])."', `length`='".htmlspecialchars($_POST['length'])."', `disabled`='".$_POST['disabled']."', `position`='".$_POST['position']."' where `id`=".$_POST['id']." AND `owner`=".$ownerid."";
         
$editresult mysql_query($editquery);
      echo(
"<center><img src=\"mobilis.jpg\" /></center>
      <p />Successfully edited "
.$_POST['title']."!<br />
      <a href=\"?\">Back to Settings</a>"
);
       }
      }
   } 
  } 
?>
  <center>Mobilis <a href=\"http://www.gnu.org/copyleft/gpl.html\" target=\"_blank\">GNU/GPL Software</a>. Written for <a href=\"http://magpierss.sourceforge.net/\" target=\"_blank\">MagpieRSS</a>. Mobilis is written by <a href=\"http://www.occulti.com\">Occulti Design</a>.</center>
 </body>
</html>