[ PHPXref.com ] [ Generated: Sun Jul 20 20:09:57 2008 ] [ QwikiWiki 1.5.1 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/ -> _util.php (source)

   1  <?php
   2  // _util.php

   3  //

   4  //Copyright 2004/2005, David Barrett (www.quinthar.com) and TESTCo (www.testco.com).  All Rights Reserved.

   5  //

   6  // See LICENSE for the complete licensing details.

   7  
   8  // QWFatalError

   9  function QWFatalError( $message )
  10  {
  11      // Output the message and exit

  12      echo "<B>Fatal QwikiWiki Error</B>: $message<BR/>\n";
  13      exit;
  14  }
  15  
  16  // QWLockExists

  17  function QWLockExists( $lockPath )
  18  {
  19      // See if it exists and hasn't expired

  20      global $QW_CONFIG;
  21      return file_exists( $lockPath ) && (time() - filemtime( $lockPath )) < $QW_CONFIG['editLockfileExpiration'];
  22  }
  23  
  24  // QWCreateLockfile

  25  function QWCreateLockfile( $globalLockPath, $lockPath, $who )
  26  {
  27      // First acquire a lock on the globalLockPath by opening and block-locking

  28      $lockFP = fopen( $globalLockPath, 'w+' ) or QWFatalError( "Cannot lock '$globalLockPath' because it does not exist" );
  29      flock( $lockFP, LOCK_EX ) or QWFatalError( "Cannot lock '$globalLockPath' -- dunno why" );
  30      
  31      // Check to see if it exists

  32      global $QW_CONFIG;
  33      $alreadyExists = file_exists( $lockPath );
  34      $gotLock = false;
  35      if( !$alreadyExists || (time() - filemtime( $lockPath )) > $QW_CONFIG['editLockfileExpiration'] )
  36      {
  37          // Create the file

  38          $fp = fopen( $lockPath, 'w' );
  39          fwrite( $fp, $who );
  40          fclose( $fp );
  41          $gotLock = true;
  42      }
  43      
  44      // Release the global lock

  45      flock( $lockFP, LOCK_UN ) or QWFatalError( "Cannot unlock '$globalLockPath' -- dunno why" );
  46      fclose( $lockFP );
  47      
  48      // Return whether or not this successfully created a new file

  49      return $gotLock;
  50  }
  51  
  52  // QWHoldsLockfile

  53  function QWHoldsLockfile( $globalLockPath, $lockPath, $who )
  54  {
  55      // First acquire a lock on the globalLockPath by opening and block-locking

  56      $lockFP = fopen( $globalLockPath, 'w+' ) or QWFatalError( "Cannot lock '$globalLockPath' because it does not exist" );
  57      flock( $lockFP, LOCK_EX ) or QWFatalError( "Cannot lock '$globalLockPath' -- dunno why" );
  58      
  59      // Check to see if it exists

  60      $holdsIt = false;
  61      if( file_exists( $lockPath ) )
  62      {
  63          // Read it and see who holds it

  64          $fileArray = FILE( $lockPath );
  65          $holdsIt = ($fileArray[0] == $who);
  66      }
  67      
  68      // Release the global lock

  69      flock( $lockFP, LOCK_UN ) or QWFatalError( "Cannot unlock '$globalLockPath' -- dunno why" );
  70      fclose( $lockFP );
  71      
  72      // Return whether or not this lock is held by the indicated owner

  73      return $holdsIt;
  74  }
  75  
  76  // QWSafeGet

  77  // Access an array using an index that might not exist without producing an

  78  // "undefined index" warning

  79  function &QWSafeGet( &$varArray, $varIndex )
  80  {
  81      // If it's set, return the value, else return zero

  82      if( isset( $varArray[$varIndex] ) ) return $varArray[$varIndex];
  83      else                                return false;
  84  }
  85  
  86  // QWStripArraySlashes

  87  function QWStripArraySlashes( &$varArray )
  88  {
  89      // Walk across and strip it recursively

  90      if( count( $varArray ) ) foreach( $varArray as $name => $value )
  91      {
  92          if( is_array( $value ) ) QWStripArraySlashes( $value );
  93          else                     $varArray[$name] = stripslashes( $value );
  94      }
  95  }
  96  
  97  // QWClearCookies

  98  function QWClearCookies( &$array, $path )
  99  {
 100      // Loop across the named cookie array

 101      if( is_array( $array ) ) foreach( $array as $name => $value )
 102      {
 103          if( is_array( $value ) )
 104          {
 105              // If it's the first level, don't surround in brackets

 106              if( $path ) QWClearCookies( $value, "{$path}[{$name}]" );
 107              else        QWClearCookies( $value, "$name" );
 108          }
 109          else
 110          {
 111              // Erase this cookie

 112              if( $path ) setcookie( "{$path}[{$name}]", "", time()-3600 );
 113              else        setcookie( $name, "", time()-3600 );
 114          }
 115      }
 116  }
 117  
 118  // QWRedirect

 119  function QWRedirect( $relativeURL )
 120  {
 121      // Send the redirection header

 122      global $QW;
 123      $url = "$QW[homeLink]/$relativeURL";
 124      header("Location: $url" );
 125      echo "<P>Please wait, redirecting to <A HREF='$url'>here</A>.</P>";
 126  }
 127  
 128  // QWDisableCaching

 129  function QWDisableCaching( )
 130  {
 131      // Disable caching, but not all the way? (Allows for "back" to keep working fast)

 132      header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past

 133      header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified

 134  }
 135  
 136  // QWVerifyDirectory

 137  function QWVerifyDirectory( $directory )
 138  {
 139      // If it doesn't exist, create it and inheret from this directory's permissions

 140      if( !is_dir( $directory ) )
 141      {
 142          // Make a new backup directory

 143          $stats = stat( "." );
 144          $mode = $stats[2];
 145          if( !mkdir( $directory, $mode ) ) return "Error - Cannot create '$directory'";
 146      }
 147  }
 148  
 149  // QWGetRecentlyChangedFileList

 150  // Loop across the contents of a directory and create a list of files

 151  // that match the pattern, sorted by modifed date

 152  function QWGetRecentlyChangedFileList( $directory, $pattern )
 153  {
 154      // Get the dir and loop across

 155      $dir = dir( $directory );    
 156      while( $filename = $dir->read( ) )
 157          if( preg_match( $pattern, $filename ) )
 158              // Put into buckets by timestamp (potentially many changes in one second)

 159              $changeList[ filemtime( "$directory/$filename" ) ][] = $filename;
 160      $dir->close( );
 161          
 162      // Sort the list

 163      krsort( $changeList );
 164      foreach( $changeList as $timestamp => $fileList )
 165          foreach( $fileList as $filename )
 166              $outList[] = $filename;
 167      
 168      // Done

 169      return $outList;
 170  }
 171  
 172  // QWTruncateArrayInPlace

 173  function QWTruncateArrayInPlace( &$inout, $maxSize )
 174  {
 175      // Trim off anything greater than $maxSize;

 176      if( isset( $inout ) && is_array( $inout ) && count( $inout ) > $maxSize )
 177          // Update the reference

 178          $inout = array_slice( $inout, 0, $maxSize );
 179  }
 180  
 181  // QWUnduplicateArray

 182  function QWUnduplicateArray( &$in )
 183  {
 184      // Loop across and remove consecutive duplicates

 185      // Loop across and create a new array containing non-unique values

 186      if( isset( $in ) && is_array( $in ) )
 187          foreach( $in as $element )
 188              if( !isset( $out ) || ($element != end($out)) )            
 189                  $out[] = $element;
 190      // Done

 191      return $out;
 192  }
 193  
 194  // QWCollapseArray

 195  function QWCollapseArray( &$in, $separator, $finisher )
 196  {
 197      // Loop accrossa and create a string containing a commma-separated list of the array entries

 198      $out = "";
 199      if( isset( $in ) && is_array( $in ) && count( $in ) )
 200      {
 201          // Insert a comma after all but the last

 202          for( $c=0; $c<count($in)-1; ++$c ) $out .= $in[$c] . $separator;
 203          $out .= $finisher . $in[$c];
 204      }
 205      return $out;
 206  }
 207  
 208  // QWGetFileContents

 209  function QWGetFileContents( $path )
 210  {
 211      // Just read the file into a big string

 212      $fileArray = file( $path );
 213      $out = "";
 214      if( isset( $fileArray ) && is_array( $fileArray ) && count( $fileArray ) )
 215          foreach( $fileArray as $line )
 216              $out .= $line;
 217      return $out;
 218  }
 219  
 220  function QWValidPageName($page) {
 221      global $QW_CONFIG;
 222      $validPageName = preg_match($QW_CONFIG['pageNamePattern'], $page); 
 223      return $validPageName;
 224  }
 225  
 226  function QWValidEmail($email) {
 227      //fix to allow domain names with dash in they name

 228      $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
 229      return eregi($regexp, $email);
 230  }
 231  
 232  // QWGetFileList

 233  // Loop across the contents of a directory and create a list of files

 234  // that match the pattern, sorted by modifed date

 235  function QWGetFileList( $directory, $pattern )
 236  {
 237      // Get the dir and loop across

 238      $dir = dir( $directory );    
 239      while( $filename = $dir->read( ) )
 240          if( preg_match( $pattern, $filename ) )
 241              // Put into the list

 242              $outList[$filename] = $filename;
 243      $dir->close( );
 244      ksort( $outList );
 245      // Done

 246      return $outList;
 247  }
 248  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]