Location: PHPKode > projects > phpMyOrdering > phpMyOrdering-0.1.7-alpha/phpMyPurchasing-0.1.2-alpha/scripts/classes/class.libraries.php
<?PHP
/*
 * phpMyOrdering - All rights reserved.
 *
 * Author:       Jason Gerfen
 * Email:        <hide@address.com>
 *
 * Description:  class.libraries.php - Miscellaneous functions
 *
 */

class MiscFunctions
{

 function GenDate()
 {
  return date( "Y-m-d" );
 }

 function GenTime()
 {
  return time();
 }
 
 function GenTimeRead()
 {
  return date( "G:i:s A" );
 }

 function GenSub30Date( $date )
 {
  return mktime( 0, 0, 0, date( "m" ) - 1, date( "d" ), date( "Y" ) );
 }
	
	function arr2json( $array )
 {
  foreach( $array as $key => $value ) $json[] = $key . ':' . php2js( $value );
  if( count( $json ) > 0 ) return '{' . implode( ',', $json ) . '}';
  else return '';
 }

 function php2js( $value )
 {
  if( is_array( $value ) ) return arr2json( $val );
  if( is_string( $value ) ) return '"' . addslashes( $value ) . '"';
  if( is_bool( $value ) ) return 'Boolean(' . (int) $value . ')';
  if( is_null( $value ) ) return '""';
  return $value;
 }

 function getDefaults()
 {
  global $defined,$handles;
  $dbconn = $handles['db']->dbConnect( $defined['dbhost'], $defined['username'], $defined['password'], $defined['dbname'] );
  $sql = "SELECT * FROM `configuration`";
		if(($return = $handles['db']->dbQuery($handles['val']->ValidateSQL($sql,$dbconn),$dbconn))!==-1) {
   if($handles['db']->dbNumRows($return)>0) {
  	 $data = $handles['db']->dbArrayResultsAssoc($return);
    $val['email'] = $data[0]['txtOptionsEmail'];
    $val['timeout'] = $data[0]['txtOptionsTimeout'];
    $val['txtGMapAPIKey'] = $data[0]['txtGMapAPIKey'];
   }
		}
  $handles['db']->dbFixTable("configuration", $dbconn);
  $handles['db']->dbFreeData($dbconn);
  $handles['db']->dbCloseConn($dbconn);
  return $val;
 }
	
 function getAuthentication()
 {
  global $defined,$handles;
  $dbconn = $handles['db']->dbConnect( $defined['dbhost'], $defined['username'], $defined['password'], $defined['dbname'] );
  $sql = "SELECT * FROM `authentication`";
		if(($return = $handles['db']->dbQuery($handles['val']->ValidateSQL($sql,$dbconn),$dbconn))!==-1) {
   if($handles['db']->dbNumRows($return)>0) {
  	 $data = $handles['db']->dbArrayResultsAssoc($return);
    $val['ldapuser'] = $data[0]['txtAuthUsername'];
    if(function_exists("mcrypt_encrypt")) {
     $cipher = new Cipher(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
     $val['ldappass'] = $cipher->decrypt($data[0]['txtAuthPassword'],$data[0]['key'],$data[0]['salt']);
    } else {
     $val['ldappass'] = $data[0]['txtAuthPassword'];
    }
    $val['ldapdomain'] = $data[0]['txtAuthDomain'];
    $val['ldapserv'] = $data[0]['txtAuthServers'];
    $val['ldapport'] = $data[0]['txtAuthPort'];
    $val['binddn'] = $data[0]['txtAuthBindDN'];
    $val['basedn'] = $data[0]['txtAuthBaseDN'];
   }
		}
  $handles['db']->dbFixTable("authentication", $dbconn);
  $handles['db']->dbFreeData($dbconn);
  $handles['db']->dbCloseConn($dbconn);
  return $val;
 }

 function getAccessList()
 {
  global $defined,$handles;
  $dbconn = $handles['db']->dbConnect( $defined['dbhost'], $defined['username'], $defined['password'], $defined['dbname'] );
  $sql = "SELECT * FROM `access-list`";
		if(($return = $handles['db']->dbQuery($handles['val']->ValidateSQL($sql,$dbconn),$dbconn))!==-1) {
   if($handles['db']->dbNumRows($return)>0) {
  	 $data = $handles['db']->dbArrayResultsAssoc($return);
   }
		}
  $handles['db']->dbFixTable("access-list", $dbconn);
  $handles['db']->dbFreeData($dbconn);
  $handles['db']->dbCloseConn($dbconn);
  return $data[0];
 }

 /*
  * Generate a simple drop down menu from array
  * @array => Nested array of data to use in form
  * @selected => Option to show if $_POST or $_GET processed
  * @name => Name of drop down menu
  */
 function GenDropMenuWSelected( $array, $selected, $name )
 { //echo "<pre>"; print_r( $array ); echo "</pre>";
  if( count( $array[0] ) !== 0 ) {
   $list .= "<select name=\"". $name . "\" style=\"width: 100%\">";
   if( !empty( $selected ) ) {
    $list .= "<option value=\"" . $selected . "\">" . $selected . "</option>";
   }
   $list .= "<option>---------------</option>";
   foreach( $array as $key => $value ) {
    $value['name'] = $this->TrimString( $value['name'], 25 );
    $list .= "<option value=\"" . $value['name'] . "\">" . $value['name'] . "</option>";
   }
   $list .= "</select>";
  }
  return $list;
 }
 
 /*
  * Perform nested array in_array() function
  */
 function array_search_recursive( $needle, $haystack )
 { //echo "$needle<pre>"; print_r( $haystack ); echo "</pre><hr>";
  if( count( $haystack ) !== 0 ) {
   foreach( $haystack as $k => $v ) {
    if( $v['subnet-name'] === $needle ) { 
     return TRUE;
     break;
    }
   }
  }
 }
 
 /*
  * Perform nested array in_array() function
  */
 function array_search_recursive_2( $needle, $haystack, $group )
 { //echo "<pre>"; print_r( $needle ); echo "</pre>";
   //echo "<pre>"; print_r( $haystack ); echo "</pre>";
  if( count( $haystack ) !== 0 ) {
   foreach( $haystack as $k => $v ) {
    if( ( $v['allowed'] === $needle ) && ( $group !== $v['allowed'] ) ) {
     return TRUE;
     break;
    }
   }
  }
 }
 
	function GenDropMenuURL( $table, $field, $order, $id )
 {
  global $defined;
  $db = new dbConn();
  $val = new ValidateStrings();
  if( ( empty( $table ) ) || ( empty( $field ) ) ) {
   return -1;
  }
  $conn = $db->dbConnect( $defined['dbhost'], $defined['username'], $defined['password'], $defined['dbname'] );
  if( empty( $order ) ) {
   $query = "SELECT `$field` FROM `$table`";
  } else {
   $query = "SELECT `$field` FROM `$table` ORDER BY `$order`";
  }
  $query = $val->ValidateSQL( $query, $conn );
  if( ( $value = $db->dbQuery( $query, $conn ) ) === -1 ) {
   return -1;
  }
  if( ( $db->dbNumRows( $value ) === -1 ) || ( $db->dbNumRows( $value ) === 0 ) ) {
   return -1;
  } else {
   $list = "<option name=\"----------\" value=\"----------\">----------</option>";
   foreach( $db->dbArrayResults( $value ) as $key => $val ) {
    foreach( $val as $key => $val ) {
				 if( strlen( $val ) >= 40 ) {
					 $list .= "<option value=\"" . $_SERVER['PHP_SELF'] . "?" . $id . "=" . $val . "\">" . substr_replace( $val, '...', 40 ) . "</option>";
					} else {
      $list .= "<option value=\"" . $_SERVER['PHP_SELF'] . "?" . $id . "=" . $val . "\">$val</option>";
					}
    }
   }
   $data = $list;
  }
  $db->dbFreeData( $conn );
  $db->dbCloseConn( $conn );
  return $data;
 }
	
 function DetArrayType( $array ) {
  $val = 0;
  $keys = array_keys( $array[0] );
  foreach( $keys as $key ) {
   if( !is_int( $key ) ) {
    $val = 1;
   }
  }
  return $val;
 }

 /*
  * return a reversed dns lookup
  */
 function GenRevAddr( $ip ) {
  $adapter = @dns_get_record( implode( '.', array_reverse( explode( '.', $ip ) ) ) . '.in-addr.arpa.', DNS_PTR );
  if( empty( $adapter ) ) {
   $adapter = "ERROR DETERMINING REVERSE HOST RECORD";
  }
  return $adapter;
 }

 /*
  * trim string length
  */
 function TrimString( $string, $len )
 { //echo "STRING: " . $string . " LENGTH: " . strlen( $string ) . " ALLOWED LENGTH: " . $len . " FIXED: " . substr( $string, 0, $len - 5 ) . "<br>";
  $strlen = strlen( $string );
		if( $strlen < $len ) {
		 return $string;
		} else {
   return substr( $string, 0, $len - 5 ) . "...";
		}
 }

 /*
  * apply re-index of array keys
  */
 function ReIndexArray( $array ) {
  $index = 1;
  if( count( $array ) === 0 ) {
		 return;
		}
		foreach( $array as $keys => $values ) {
   $data[$index] = $values;
   $index++;
  }
  return $data;
 }

 function DetDup( $array ) {
  if( count( $array ) === 0 ) {
   $data = -1;
  } else {
   for( $x = 0; $x < count( $array ); $x++ ) {
    foreach( $array[$x] as $key => $value ) {
     if( @array_search( $array[$x]['ordernum'], $data ) === FALSE ) {
      $data[] = $array[$x]['ordernum'];
     }
    }
   }
  }
  return $data;
 }

 function EliminiateDuplicates( $array )
 {
  foreach( $array as $key => $value ) {
   $arrayOfArrays[$key] = "'" . serialize( $value ) . "'";
  }
  $arrayOfArrays = array_unique( $arrayOfArrays );
  foreach( $arrayOfArrays as $key => $value ) {
   $arrayOfArrays[$key] = unserialize( trim( $value, "'" ) );
  }
  sort( $arrayOfArrays );
  return $arrayOfArrays;
 }


 function ExitApplication($token) {
  global $handles;
  if (!empty($token)) {
   $handles['sessions']->destroy($token);
  }
  return;
 }

 function CleanUpVars( $array, $single )
 {
  if( !empty( $single ) ) { unset( $single ); }
  if( count( $array < 0 ) ) {
   for( $i = 0; $i < count( $array ); $i++ ) {
    unset( $array[$i] );
   }
  }
 }
	
	
	function parseToXML( $htmlStr )
 { 
  $xmlStr = str_replace( '<', '&lt;', $htmlStr ); 
  $xmlStr = str_replace( '>', '&gt;', $xmlStr ); 
  $xmlStr = str_replace( '"', '&quot;', $xmlStr ); 
  $xmlStr = str_replace( "'", '&#39;', $xmlStr ); 
  $xmlStr = str_replace( "&", '&amp;', $xmlStr ); 
  return $xmlStr;
 }
	
	function ArrayDimension( $array, $option = NULL ) {
	 $img = new ImageGallery();
  if( !is_array( $array ) ) {
   return 0;
  } elseif( !$array ) {
   return 1;
  } else {
   switch( strtolower( $option ) ) {
   case 'first':
    $keys = array_keys( $array );
    return $img->ArrayDimension( $array[$keys[0]], $option ) + 1;
   case 'last':
    $keys = array_keys( $array );
    return $img->ArrayDimension( $array[$keys[count( $keys ) - 1]], $option ) + 1;
   case 'min':
    foreach( $array as $key => $val ) {
     $dems[$key] = $img->ArrayDimension( $val, $option );
    }
    return min( $dems ) + 1;
   case 'max':
   default:
    foreach( $array as $key => $val ) {
     $dems[$key] = $img->ArrayDimension( $val, $option );
    }
    return max( $dems ) + 1;
   }
  }
 }
	
	function SafeReadFile( $path, $file )
	{
  if( ( !empty( $file ) ) || ( !empty( $path ) ) ) {
   if( is_dir( $path ) ) {
    if( ( $handle = @fopen( $path . $file, "r" ) ) !== FALSE ) {
     if( @flock( $handle, LOCK_EX ) ) {
      while( !feof( $handle ) ) {
						 $data .= @fread( $handle, filesize( $path. $file ) );
       @flock( $handle, LOCK_UN );
      }
     } else {
      $data = -4;
     }
    } else {
     $data = -3;
    }
   } else {
    $data = -2;
   }
  } else {
   $data = -1;
  }
  return $data;	
	}
	
 function SafeWriteFile( $path, $file, $string )
 {
  if( ( !empty( $path ) ) && ( !empty( $file ) ) && ( !empty( $string ) ) ) {
   if( is_dir( $path ) ) {
    if( ( $handle = @fopen( $path . $file, "w+b" ) ) !== FALSE ) {
     if( @flock( $handle, LOCK_EX ) ) {
      if( @fwrite( $handle, $string ) !== FALSE ) {
       $data = 0;
       @flock( $handle, LOCK_UN );
      } else {
       $data = -5;
      }
     } else {
      $data = -4;
     }
    } else {
     $data = -3;
    }
   } else {
    $data = -2;
   }
  } else {
   $data = -1;
  }
  return $data;
 }
 
}
?>
Return current item: phpMyOrdering