Location: PHPKode > projects > Filebased Guestbook > filebased-guestbook/include/shared.inc.php
<?php
// -----------------------------------------------------------------------------------------------
// Project:   Filebased guestbook.
// File:      shared.inc.php
// Topic:     shared resources
// Author:    hide@address.com
// -----------------------------------------------------------------------------------------------

            function getCOL($myFILE, $row, $arg) {         // $row->row; $arg=""->whole line, $arg=2->get arg 2

              $content = file($myFILE);                 // read file

              if($arg == ""):
                return $content[$row];
                break;
              else:
                $myLINE = explode("|",$content[$row-1]);
                return $myLINE[$arg];
              endif;
            }

            function evalMAXid($file){
              global $id;

              $content = file($file);
              $size = count($content);

              $maxID = 0;
              for($line=1; $line<=$size; $line++):
                $tmpID = getCOL($file, $line, "0");
                if($tmpID > $maxID) { $maxID = $tmpID; }
              endfor;

              return $maxID;
            }

            function show(){

              global $PHP_SELF, $pos, $HTTP_GET_VARS;
              global $s_head, $s_new, $s_text, $s_url, $s_author, $s_date, $s_pages, $s_itmes;
              global $s_forward, $s_back;
              global $conf;

              if(isset($pos) == 0)
                $pos = 0;      // start position for the page indexer
              ?>
              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRansitional//EN">
              <HTML>
              <HEAD>
              <TITLE><?php echo $s_head; ?></TITLE>
              </HEAD>

              <BODY style="font-family:arial; font-size:10px">
              <FORM name="switchlang" action="<?php echo basename($PHP_SELF); ?>" METHOD="get">
              <CENTER>
                <TABLE border="0" cellspacing="0" cellpadding="0" width="100%">
                  <TR>
                    <TD align="CENTER"><h2><?php echo $s_head; ?></h2>
                    <TR align="CENTER">
                      <TD>
                      <TABLE border="0" width="80%">
                        <TR valign="top">
                          <TD colspan="4" >
                            <TABLE width="100%" border="0" cellspacing="0" cellpadding="0">
                              <TR valign="bottom">
                                <TD style="font-family:arial; font-size:10px" width="100">
                                <a href="<?php echo basename($PHP_SELF)."?cmd=form"."&lang=".$HTTP_GET_VARS["lang"]; ?>">
                                <img src="./images/edit.gif" width="18" height="13" border="0"></a><?php echo $s_new; ?></TD>
                                <TD style="font-family:arial; font-size:10px" width="*" align="center">&nbsp;</TD>
                                <TD style="font-family:arial; font-size:10px" width="200" align="right">
                                <?php
                                if($conf[langfeature] == "yes"):
                                ?>
                               <SELECT name="lang" style="width:120; font:9pt" size=1 onChange="document.switchlang.submit();" >
                                  <option value="cz" <?php echo ($HTTP_GET_VARS['lang'] == 'cz' ? 'selected' : ''); ?>>Cesky</option>
                                  <option value="ge" <?php echo ($HTTP_GET_VARS['lang'] == 'ge' ? 'selected' : ''); ?>>Deutsch</option>
                                  <option value="uk" <?php echo ($HTTP_GET_VARS['lang'] == 'uk' ? 'selected' : ''); ?>>English</option>
                                  <option value="es" <?php echo ($HTTP_GET_VARS['lang'] == 'es' ? 'selected' : ''); ?>>Espa&ntilde;ol</option>
                                  <option value="fr" <?php echo ($HTTP_GET_VARS['lang'] == 'fr' ? 'selected' : ''); ?>>Fran&ccedil;ais</option>
                                  <option value="is" <?php echo ($HTTP_GET_VARS['lang'] == 'is' ? 'selected' : ''); ?>>Islandic</option>
                                  <option value="it" <?php echo ($HTTP_GET_VARS['lang'] == 'it' ? 'selected' : ''); ?>>Italiano</option>
                                  <option value="nl" <?php echo ($HTTP_GET_VARS['lang'] == 'nl' ? 'selected' : ''); ?>>Nederlands</option>
                                  <option value="no" <?php echo ($HTTP_GET_VARS['lang'] == 'no' ? 'selected' : ''); ?>>Norge</option>
                                  <option value="pl" <?php echo ($HTTP_GET_VARS['lang'] == 'pl' ? 'selected' : ''); ?>>Polski</option>                                  
                                  <option value="pt" <?php echo ($HTTP_GET_VARS['lang'] == 'pt' ? 'selected' : ''); ?>>Portugu&ecirc;s</option>
                                  <option value="br" <?php echo ($HTTP_GET_VARS['lang'] == 'br' ? 'selected' : ''); ?>>Portugu&ecirc;s (Brasil)</option>
                                  <option value="si" <?php echo ($HTTP_GET_VARS['lang'] == 'si' ? 'selected' : ''); ?>>Slovenski</option>
                                  <option value="se" <?php echo ($HTTP_GET_VARS['lang'] == 'se' ? 'selected' : ''); ?>>Svenska</option>
                                  <option value="sr" <?php echo ($HTTP_GET_VARS['lang'] == 'sr' ? 'selected' : ''); ?>>Serbian</option>
                                </SELECT>
                                <TD width="30" align="right">
                                <INPUT type=submit value=">>" style="width:20; font-size: 8pt" align="right" >
                                <?php
                                endif;
                                ?>
                                </TD>
                              </TR>
                            </TABLE>
                          </TD>
                        </TR>
                        <TR valign="top">

                          <TD colspan=5 height="1">
                          <hr width="100%" noshade size="1"></TD>
                          </TR>
                          <?php
                          $content = file($conf[f_data] );
                          $size = count($content);

                          if($pos >= $size) $pos = 0;               // break at manipulated inputs on $pos

                          for($i=$pos; $i<($conf[itemsperpage]+$pos); $i++):
                            if($i > $size-1) break;                 // break due to not existing contents
                              $text = explode("|",stripslashes($content[$size-$i-1]));
                              ?>
                              <TR valign="top">
                                <TD colspan=5>
                                <TABLE width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
                                  <TR>
                                    <TD width="100" style="font-size: 10pt" valign="top"><?php echo $s_text." ".$text[0]; ?>
                                    <TD width="85%" style="font-size: 10pt"><?php echo $text[7]; ?>
                                  <?php
                                  if(($text[6] != "http://") AND (!empty($text[6])) ):
                                  ?>
                                  <TR>
                                    <TD width="100" style="font-size: 10pt" valign="top"><?php echo $s_url; ?>
                                    <TD width="85%" style="font-size: 10pt">
                                    <font size="-1"><a href='<?php echo $text[6]; ?>'><?php echo $text[6]; ?></a></font>
                                  <?php
                                  endif;
                                  ?>
                                  <TR>
                                    <TD width="100" style="font-size: 10pt" valign="top"><?php echo $s_author; ?>
                                    <TD width="85%" style="font-size: 10pt">
                                    <font size="-1"><a href='mailto:<?php echo $text[3]; ?>'><?php echo $text[2]; ?></a></font>
                                  <TR>
                                    <TD width="100" style="font-size: 10pt" valign="top"><?php echo $s_date; ?>
                                    <TD width="85%" style="font-size: 10pt"><?php echo $text[1]; ?>
                                  <TR>
                                    <TD colspan="2"><hr width="100%" noshade size="1">
                                </TABLE>
                                </TD>
                              </TR>
                          <?php
                          endfor;
                          ?>

                        <!-- number of pages index -->
                        <TR>
                          <TD ALIGN="LEFT" valign="top" style="font-size: 8pt" width="30%"><?php echo $s_pages; ?>
                            <?php
                              for($j=0; $j<$size; $j++):
                                 if(!($j % $conf[itemsperpage] )):
                                   ?>
                                   <a href="<?php echo basename($PHP_SELF)."?pos=".$j."&lang=".$HTTP_GET_VARS[lang]; ?>"><?php echo (($j/$conf[itemsperpage])+1); ?></a>
                                   <?php
                                 endif;
                              endfor;
                              ?>
                              </TD>
                              <!-- forward / reverse -->
                              <TD ALIGN="center" valign="top" style="font-size: 8pt" width="30%"><?php echo $s_itmes; ?><?php echo $size;?></TD>
                              <TD ALIGN="right" valign="top" style="font-size: 8pt" width="30%">
                              <?php
                                if ($pos > 0):
                                  ?>
                                  <a href="<?php echo basename($PHP_SELF)."?pos=".($pos-$conf[itemsperpage] )."&lang=".$HTTP_GET_VARS[lang]; ?>"><?php echo $s_back; ?></a>
                                  <?php
                                endif;
                                if($size > $pos+$conf[itemsperpage] ):
                                  ?>
                                  <a href="<?php echo basename($PHP_SELF)."?pos=".($pos+$conf[itemsperpage] )."&lang=".$HTTP_GET_VARS[lang]; ?>"><?php echo $s_forward; ?></a>
                                  <?php
                                endif;
                                ?>
                            </TD>
                          </TR>
                          <TR>
                            <TD colspan="4" align="CENTER"><font size="-2"><a href='<?php echo $conf[c_email]; ?>'><?php echo $conf[c_credits]; ?></a></font>
                          </TR>
                        <!-- number of pages index -->

                      </TABLE>
                      </TD>
                    </TR>
                </TABLE>
              </CENTER>
              </BODY>
              </HTML>
              <?php
            }


            function form(){

              global $PHP_SELF, $pos, $go, $HTTP_GET_VARS;
              global $f_head, $f_title, $f_new, $f_text, $f_url, $f_author, $f_date, $f_pages, $f_itmes;
              global $f_country, $f_location, $f_email;
              global $f_forward, $f_back, $f_submit, $f_alert;
              global $author, $email, $beitrag, $ort, $land, $url;
              global $conf;

              if (!$go):
                ?>
                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRansitional//EN">
                <HTML>
                <HEAD>
                <TITLE><?php echo $f_head; ?></TITLE>
                </HEAD>

                <BODY style="font-family:arial; font-size:10pt">
                <CENTER>
                <TABLE border="0" cellspacing="0" cellpadding="0" width="100%">
                <TR>
                <TD>
                <CENTER>
                  <h2><?php echo $f_title; ?></h2>
                </CENTER>
                </TD>
                </TR>
                  <TR align="center">
                    <TD>
                    <TABLE border="0" width="400">
                      <form action="<?php echo basename($PHP_SELF)."?cmd=form"."&lang=".$HTTP_GET_VARS[lang]; ?>" method="post">
                      <TR>
                        <TD width="70"><?php echo $f_author; ?></TD>
                        <TD colspan="2">
                        <input type="Text" name="author">
                        </TD>
                      </TR>
                        <TR>
                        <TD valign=top width="70"><?php echo $f_text; ?></TD>
                        <TD colspan="2">
                        <textarea name="beitrag" cols="35" rows="10"></textarea>
                        </TD>
                      </TR>
                      <TR>
                        <TD width="70"><?php echo $f_location; ?></TD>
                        <TD colspan="2">
                        <input type="Text" name="ort">
                        </TD>
                      </TR>
                      <TR>
                        <TD width="70"><?php echo $f_country; ?></TD>
                        <TD colspan="2">
                        <input type="Text" name="land">
                        </TD>
                      </TR>
                      <TR>
                        <TD width="70"><?php echo $f_email; ?></TD>
                        <TD colspan="2">
                        <input type="Text" name="email">
                        </TD>
                      </TR>
                      <TR>
                        <TD width="70">URL:</TD>
                        <TD colspan="2">
                        <input type="Text" name="url" value="http://">
                        </TD>
                      </TR>
                      <TR>
                        <TD width="70">&nbsp;</TD>
                        <TD colspan="2">&nbsp;</TD>
                      </TR>
                      <TR>
                        <TD ALIGN="CENTER" width="70">&nbsp;</TD>
                        <TD align="left" colspan="2">
                        <INPUT type="Submit" value="<?php echo $f_submit; ?>" style="width:100; font:9pt">
                        <INPUT type="hidden" name="go" value=1>
                        </TD>
                      </TR>
                      </FORM>

                      <!-- start back button -->
                      <TR>
                      <TD align="CENTER" width="70">&nbsp;</TD>
                      <TD align="left">
                        <FORM method="get" action="<?php echo basename($PHP_SELF); ?>">
                          <INPUT TYPE="submit" value="<?php echo $f_back; ?>" style="width:100; font:9pt" >
                          <INPUT type="hidden" name="lang" value="<?php echo $HTTP_GET_VARS["lang"]; ?>" >
                        </FORM>
                      </TD>
                      </TR>
                      <!-- end back button -->

                      </TABLE>
                    </TD>
                  </TR>

                  </TABLE>

                  </CENTER>
                  </BODY>
                  </HTML>
                  <?php
                  else:
                       if($author=="" || $email=="" || $beitrag==""):

                     ?>
                     <script type="text/javascript">
                       window.alert('<?php echo $f_alert; ?>');
                       history.back(-1);
                     </script>
                     <?php

                     else:
                     // begin writing to the file
                     $datum = date("j.n.Y");
                     $content = file($conf[f_data] );
                     $size = count($content);

                     $id = evalMAXid($conf[f_data] )+1;

                     $beitrag = str_replace("|", "/", $beitrag);
                     $beitrag = str_replace("\n", "<br>", $beitrag);    // these 3 lines do not look sophisticated,
                     $beitrag = str_replace("\r", "", $beitrag);        // but it works properly
                     $beitrag = str_replace("\n", "", $beitrag);

                     $text = $id."|".$datum."|".$author."|".$email."|".$ort."|".$land."|".$url."|".$beitrag."|";
                     $content[$size] = $text;

                     $fp=@fopen($conf[f_data], "a");
                     if($fp <= 0):
                       ?>Error opening data file.<br><?php
                     endif;
                     fputs($fp, $content[$size]."\n");
                     fclose($fp);

                     if ($conf[feedback] == "yes") {
                         $title = "New Entry in myGBOOK";
                         $from =  "From: ".$email."\nX-Mailer: MyGBOOK";
                         @mail($conf[myemail], $title, $beitrag, $from);
                     };

                     // log authors ip/host, datetime and entry id
                     $log = new log_visitors("data/logauthors.txt", FALSE, $id );

                     ?>
                     <script type="text/javascript">
                       window.location.href="<?php echo basename($PHP_SELF)."?lang=".$HTTP_GET_VARS[lang]."&".md5(time()); ?>";
                     </script>
                     <?php
                   endif;
                 endif;
            }

            /**
            * authenticate versus the $authentication array or cookie
            * urs_at_circle_dot_ch, 16-1-2001
            *
            */
            function authenticate(){
                global $PHP_SELF, $HTTP_POST_VARS, $HTTP_COOKIE_VARS;
                global $conf, $authentication;

                // coming from login form
                if(strlen($HTTP_POST_VARS[sent] )) {
                  $login_ok = 0;
                  if (strlen($HTTP_POST_VARS[login] ) AND strlen($HTTP_POST_VARS[passwd] )) {
                    for($i=0; $i<count($authentication ); $i++) {
                       if ($authentication[$HTTP_POST_VARS[login]] == $HTTP_POST_VARS[passwd] ) {
                          if(!strlen($HTTP_POST_VARS[check_it])) $conf[cookielifetime] = "";
                          // an argument fix [(int)] by Scott Tillman <hide@address.com>
                          setcookie('login', $_POST[login], (int)$conf[cookielifetime], "/", "", 0 );
                          setcookie('passwd', $_POST[passwd], (int)$conf[cookielifetime], "/", "", 0 );
                          return 1; // authentication succeeded
                          $login_ok = 1;
                          break;
                      }
                    }
                  }
                  if(!$login_ok ) {
                    return 0;       // access denied
                  }
                // coming from anywhere else
                } else {
                  for($i=0; $i<count($authentication); $i++) {
                      if (($authentication[$HTTP_COOKIE_VARS[login]] == $HTTP_COOKIE_VARS[passwd] )
                                                            AND (strlen($HTTP_COOKIE_VARS[passwd]) ) ) {
                          return 1; // authentication succeeded
                          $login_ok = 1;
                          break;
                      }
                  }
                  if(!$login_ok ) {
                    return 0;       // access denied
                  }
                }
            }


            function LoginForm(){

              global $PHP_SELF, $login, $passwd, $loginform, $sent;

              $header = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE>login form</TITLE><link rel="stylesheet" href="style.php"></HEAD><BODY><CENTER>';
              $footer = '</CENTER></BODY></HTML>';

              echo $header;
              ?>
              <form method="post" action="<?php echo basename($PHP_SELF); ?>" name=loginform  enctype="multipart/form-data">
              <input type="hidden" name="cmd" value="admin">
              <table frame=none rules=none WIDTH="300">

              <tr>
                <td align="left">
                login:
                <td align="left">
                <input type="text" name="login" maxlength=50 size=10 style="width: 90px; font-size: 10px">
              <tr>
                <td align="left">
                password:
                <td align="left">
                <input type="password" name="passwd" maxlength=50 size=10 style="width: 90px; font-size: 10px">
              <?php
              /*
              <tr>
                <td align="left">
                Logininformation speichern.
                <td align="left">
                <input name="check_it" value="1" type="checkbox" onclick="cookieREMARK()" >
              */
               ?>
              <tr>
                <td align="left">&nbsp;
                <td align="left">&nbsp;
              <tr>
                <td align="left">
                &nbsp;
                <td align="left">
                <input type="submit" value="login" name="sent" style="width: 30px; heigth: 18px; font-size: 10px">
              </table>
              </form>

              <script type="text/javascript">
              <!--
              if (document.loginform) {
                 document.loginform.login.focus();
              }
              // -->
              </script>
              <?php
              echo $footer;
            }

            function redirect($cmd){

              global $PHP_SELF;
              ?>
              <SCRIPT type="text/javascript">
                window.location.href="<?php echo basename($PHP_SELF).$cmd; ?>";
              </SCRIPT>
              <?php
            }

            function getITEM($file, $row, $col){             // rows, cols, args

              $content = file($file);                      // read file
              $myROWS = count($content);                   // get number of lines

              for($i=0; $i<$myROWS; $i++):                 // split by seperator "|"
                $myLINES[$i] = explode("|",$content[$i]);
              endfor;
              $myCOLS = count($myLINES[0]);                // get number of arguments per line

            }

            function del(){

              global $PHP_SELF, $emaildel, $valid, $email, $conf;
              ?>
              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRansitional//EN">
              <HTML>
              <HEAD>
              <TITLE>Administration / Delete guestbook entries</TITLE>
              </HEAD>

              <BODY style="font-family:arial; font-size: 10px">
              <center>
              <h2>Administration / Delete guestbook entries</h2>
              <?php
              if(!$valid):
                $content = file($conf[f_data] );
                $myROWS = count($content);
                ?>
                <FORM action="<?php echo basename($PHP_SELF)."?cmd=admin&".md5(time()); ?>" method="POST" enctype="multipart/form-data">
                <TABLE align="center">
                <?php
                for($i=0; $i<$myROWS; $i++):                 // split by seperator "|"
                  $myLINES[$i] = explode("|",$content[$i]);
                  ?>
                  <tr>
                    <td align="left" valign="top">
                    <INPUT TYPE="checkbox" NAME="emaildel[<?php echo $myLINES[$i][0]; ?>]" VALUE="1">
                    <td align="left"><?php echo $myLINES[$i][7]; ?>
                  <tr>
                    <td colspan="2"><hr>
                  <?
                endfor;
                ?>
                  <tr>
                    <td colspan="2" align="center">
                  <INPUT TYPE="submit" NAME="submit" VALUE="delete">
                  <INPUT TYPE="hidden" name="valid" value="1">
                </TABLE>
                </FORM>
                <?
            else:
             ?>
             <center>
               <?php

               $fp = @fopen($conf[f_data], "r");
               if($fp <= 0):
                 ?>File not existing or damaged<br><?php
               else:
                 $nfp = @fopen($conf[f_data].".tmp","w");
                 while(feof($fp) == 0):
                   $line = chop(fgets($fp, 1000));
                   $items =  explode("|", $line);
                   if($line == "")
                     continue;
                   if($emaildel[$items[0]]):
                     echo $items; ?> erased. <br><?php
                   else:
                     fputs($nfp, $line."\n");
                   endif;
                 endwhile;
                 fclose($fp);
                 fclose($nfp);
                 unlink($conf[f_data] );
                 rename($conf[f_data].".tmp", $conf[f_data] );
                 ?>
                 <p>selection erased!<br>
                 <?php
               endif;
               ?>
               <br>
               <A HREF="<?php echo basename($PHP_SELF); ?>">back</A>
               <script type="text/javascript">
                 window.location.href="<?php echo basename($PHP_SELF); ?>";
               </script>
             </center>
             <?php
            endif;
            ?>
            </CENTER>
            </FORM>
            </BODY>
            </HTML>
             <?php
          }

          /**
          * log visitors host and time
          * hide@address.com, 21-1-2001
          */
          class log_visitors {
              var $visited;

              function log_visitors ($logfile="data/logfile.txt", $unique=TRUE, $str="default" ) {
                  global $REMOTE_HOST, $REMOTE_ADDR;

                  $this->logfile          = $logfile;                     // can be configured
                  $this->unique           = $unique;                      // log unique visits
                  $this->str              = $str;                         // any other additional log string
                  $this->cookielifetime   = 864000;                       // 24 hours
                  $this->currentdate      = date("d.m.Y H:i:s", time() ); // 21.01.2001 16:50:32
                  $this->host             = isset($REMOTE_HOST ) ? $REMOTE_HOST : @gethostbyaddr($REMOTE_ADDR );

                  $this->log_write();
              }

              function log_write () {
                  global $HTTP_COOKIE_VARS, $HTTP_GET_VARS;
                  if($this->unique ) {
                      $this->log_setcookie(1);
                  } else {
                      $this->log_setcookie(0);
                  }

                  if($HTTP_COOKIE_VARS[visited] < 1 ) {
                      $fp = fopen("$this->logfile", "a+" );
                      fwrite($fp, "$this->currentdate, $this->str, $this->host, $HTTP_GET_VARS[lang]\n",
                                              strlen("$this->currentdate, $this->str, $this->host, $HTTP_GET_VARS[lang]\n" ));
                      fclose($fp);
                  }
              }

              function log_setcookie ($val) {
                  setcookie('visited', $val, time()+$this->cookielifetime, "/", "", 0 );
              }

          } // end class

?>
Return current item: Filebased Guestbook