Code archives/Miscellaneous/PHP-based BlitzMax module server

This code has been declared by its author to be Public Domain code.

Download source code

PHP-based BlitzMax module server by Fabian.
(Posted 20 years ago)
If you want to have your own ModServer, you first of all need an own Webserver, which must be able to execute PHP-scripts.

The BlitzMax module synchronization works over the http protocol, and it's best to use PHP scripts on the serverside. I suggest creating a new folder on a webserver where to store all the scripts and module data. If you'll use the code I post, the folder needs a subfolder called "data". This is where the actual modules are stored. Sometimes users enter the address of the modserver manually in a webbrowser, therefore you'll need an "index.php" file; you can use this one on your server:
<?PHP
Echo "<HTML>\n  <HEAD><TITLE>Modules</TITLE></HEAD>\n  <BODY STYLE=" ;
Echo '"background: #F9F9F9; font-family: Courier New;"' . ">\n    " ;
Echo '<TABLE STYLE="border-collapse: collapse">' . "\n      <TR><TH STYLE=" ;
Echo '"border: 2px solid #BABABA;">Modulescope</TH><TH STYLE="border: 2px solid #BABABA;"' ;
Echo ">Modulename</TH></TR>\n" ;
$dir = opendir ( 'data' ) ;
while ( $file = readdir ( $dir ) )
{
  if ( $file != '.' && $file != '..' )
  {
    $subdir = opendir ( 'data/' . $file ) ;
    while ( $subfile = readdir ( $subdir ) )
    {
      if ( $subfile != '.' && $subfile != '..' )
      {
        Echo '      <TR><TD STYLE="border: 2px solid #BABABA;">' . $file ;
        Echo '</TD><TD STYLE="border: 2px solid #BABABA;">' . $subfile . "</TD></TR>\n" ;
      }
    }
    closedir ( $subdir ) ;
  }
}
closedir ( $dir ) ;
Echo "    </TABLE>\n  </BODY>\n</HTML>" ;
?>

Then you need a file called "status.php", this will be called from the client side to check where and whether the server is present:
<?PHP
Echo "OK: status\n" ;
Echo "ModServer: <i>modserver-name</i>\n" ;
Echo "ServerUrl: <i>modserver-url</i>\n" ;
if ( $_GET [ 'mod' ] == '<i>yourscope</i>' && $_GET [ 'user' ] == '<i>yourusername</i>' && $_GET [ 'pass' ] == '<i>yourpassword</i>' )
{
  Echo "ServerPrivs: write\n\n" ;
}
else
{
  Echo "ServerPrivs: read\n\n" ;
}
?>

Next you need a "listmods.php" file, which will be called by the client to determinate which modules are on the server:
<?PHP
Echo "OK: listmods\n" ;
if ( $_GET [ 'mod' ] != '' && substr_count ( $_GET [ 'mod' ] , '/' ) == 0 )
{
if ( substr_count ( $_GET [ 'mod' ] , '.' ) == 0 )
{
  if ( is_dir ( 'data/' . $_GET [ 'mod' ] ) )
  {
    $dir = opendir ( 'data/' . $_GET [ 'mod' ] ) ;
    while ( $file = readdir ( $dir ) )
    {
      if ( $file != '.' && $file != '..' )
      {
if ( $_GET [ 'mod' ] == '<i>yourscope</i>' && $_GET [ 'user' ] == '<i>yourusername</i>' && $_GET [ 'pass' ] == '<i>yourpassword</i>' )
{
        Echo "ModPrivs: write\n" ;
}
else
{
        Echo "ModPrivs: read\n" ;
}
        Echo 'Module: ' . $_GET [ 'mod' ] . '.' . $file . "\n" ;
        $sub = fopen ( 'data/' . $_GET [ 'mod' ] . '/' . $file . '/.txt' , 'rb' ) ;
        while ( ! feof ( $sub ) )
        {
          Echo fread ( $sub , 1024 ) ;
        }
        fclose ( $sub ) ;
        Echo "\n" ;
      }
    }
    closedir ( $dir ) ;
  }
}
else if ( substr_count ( $_GET [ 'mod' ] , '.' ) == 1 )
{
  if ( is_dir ( 'data/' . str_replace ( '.' , '/' , $_GET [ 'mod' ] ) ) )
  {
$module = substr ( $_GET [ 'mod' ] , 0 , strpos ( $_GET [ 'mod' ] , '.' ) ) ;
if ( $module == '<i>yourscope</i>' && $_GET [ 'user' ] == '<i>yourusername</i>' && $_GET [ 'pass' ] == '<i>yourpassword</i>' )
{
        Echo "ModPrivs: write\n" ;
}
else
{
        Echo "ModPrivs: read\n" ;
}
        Echo 'Module: ' . $_GET [ 'mod' ] . "\n" ;
        $sub = fopen ( 'data/' . str_replace ( '.' , '/' , $_GET [ 'mod' ] ) . '/.txt' , 'rb' ) ;
        while ( ! feof ( $sub ) )
        {
          Echo fread ( $sub , 1024 ) ;
        }
        fclose ( $sub ) ;
        Echo "\n" ;
  }
}
}
?>

The next one is surely the most important one: the client calls the "getmod.php" file on the server to download a module:
<?PHP
$err = 1 ;
if ( substr_count ( $_GET [ 'mod' ] , '/' ) == 0 )
{
if ( strlen ( $_GET [ 'mod' ] ) >= 3 && substr_count ( $_GET [ 'mod' ] , '.' ) == 1 )
{
  if ( is_dir ( 'data/' . str_replace ( '.' , '/' , $_GET [ 'mod' ] ) ) )
  {
    $err = 0 ;
    Echo 'Module: ' . $_GET [ 'mod' ] . "\n" ;
    $file = fopen ( 'data/' . str_replace ( '.' , '/' , $_GET [ 'mod' ] ) . '/.txt' , 'rb' ) ;
    while ( ! feof ( $file ) )
    {
      Echo fread ( $file , 1024 ) ;
    }
    fclose ( $file ) ;
    Echo "\n" ;
    $file = fopen ( 'data/' . str_replace ( '.' , '/' , $_GET [ 'mod' ] ) . '/.zap' , 'rb' ) ;
    while ( ! feof ( $file ) )
    {
      Echo fread ( $file , 1024 ) ;
    }
    fclose ( $file ) ;
    Echo 'OK: getmod ' . $_GET [ 'mod' ] . "\n" ;
  }
}
if ( $err )
{
  Echo "ERR: Invalid module name\n" ;
}
}
?>

And the last file, called "putmod.php" is needed for you, to upload your modules:
<?PHP
$err = 1 ;
if ( $_GET [ 'mod' ] != '' && substr_count ( $_GET [ 'mod' ] , '.' ) == 1 )
{
if ( substr ( $_GET [ 'mod' ] , 0 , strpos ( $_GET [ 'mod' ] , '.' ) ) == '<i>yourscope</i>' && $_GET [ 'user' ] == '<i>yourusername</i>' && $_GET [ 'pass' ] == '<i>yourpassword</i>' )
{
  $err = 0 ;
  if ( ! is_dir ( 'data/' . str_replace ( '.' , '/' , $_GET [ 'mod' ] ) ) )
  {
    if ( ! is_dir ( 'data/' . substr ( $_GET [ 'mod' ] , 0 , strpos ( $_GET [ 'mod' ] , '.' ) ) ) )
    {
      if ( ! mkdir ( 'data/' . substr ( $_GET [ 'mod' ] , 0 , strpos ( $_GET [ 'mod' ] , '.' ) ) , 0755 ) )
      {
        $err = 1 ;
      }
    }
    if ( ! mkdir ( 'data/' . str_replace ( '.' , '/' , $_GET [ 'mod' ] ) , 0755 ) )
    {
      $err = 1 ;
    }
  }
}
}
if ( $err )
{
  Echo "ERR: Invalid module name\n" ;
}
else
{
  $data = '' ;
  $file = fopen ( 'php://input' , 'rb' ) ;
  while ( ! feof ( $file ) )
  {
    $data = $data . fread ( $file , 1024 ) ;
  }
  fclose ( $file ) ;
  $pos1 = strpos ( $data , "\r\n" ) + 2 ;
  $pos2 = strpos ( $data , "\r\n\r\n" ) + 2 ;
  $pos3 = $pos2 + 2 ;
  $pos4 = strlen ( $data ) ;
  $file = fopen ( 'data/' . str_replace ( '.' , '/' , $_GET [ 'mod' ] ) . '/.txt' , 'wb' ) ;
  fwrite ( $file , substr ( $data , $pos1 , $pos2 - $pos1 ) ) ;
  fclose ( $file ) ;
  $file = fopen ( 'data/' . str_replace ( '.' , '/' , $_GET [ 'mod' ] ) . '/.zap' , 'wb' ) ;
  fwrite ( $file , substr ( $data , $pos3 , $pos4 - $pos3 ) ) ;
  fclose ( $file ) ;
  Echo 'OK: putmod ' . $_GET [ 'mod' ] . "\n" ;
}
?>

Summarising: These files I posted you above should be with the specified names in a separate folder on your webserver. There needs to be a subfolder "data". If you replace yourscope, -username, -password with the actual scope, username, password, you can upload your modules typing this on command line
syncmods -u <i>yourusername</i> -p <i>yourpassword</i> <i>yourscope</i>
and others can download your modules by typing:
syncmods <i>yourscope</i>
On the downloading client, the username and password are ignored. A correct user name and password is only important to upload your modules.

I think, that's it. Then you should be able to host your own module server. But if you're having problems, just ask....
.