+<?php
+/************************************************************************
+ * MXChange v0.2.1 Start: 04/04/2009 *
+ * =============== Last change: 04/04/2009 *
+ * *
+ * -------------------------------------------------------------------- *
+ * File : wrapper-functions.php *
+ * -------------------------------------------------------------------- *
+ * Short description : Wrapper functions *
+ * -------------------------------------------------------------------- *
+ * Kurzbeschreibung : Wrapper-Funktionen *
+ * -------------------------------------------------------------------- *
+ * $Revision:: $ *
+ * $Date:: $ *
+ * $Tag:: 0.2.1-FINAL $ *
+ * $Author:: $ *
+ * Needs to be in all Files and every File needs "svn propset *
+ * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
+ * -------------------------------------------------------------------- *
+ * Copyright (c) 2003 - 2008 by Roland Haeder *
+ * For more information visit: http://www.mxchange.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the Free Software *
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
+ * MA 02110-1301 USA *
+ ************************************************************************/
+
+// Some security stuff...
+if (!defined('__SECURITY')) {
+ $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
+ require($INC);
+} // END - if
+
+// Read a given file
+function readFromFile ($FQFN, $sqlPrepare = false) {
+ // Load the file
+ if (function_exists('file_get_contents')) {
+ // Use new function
+ $content = file_get_contents($FQFN);
+ } else {
+ // Fall-back to implode-file chain
+ $content = implode('', file($FQFN));
+ }
+
+ // Prepare SQL queries?
+ if ($sqlPrepare === true) {
+ // Remove some unwanted chars
+ $content = str_replace("\r", '', $content);
+ $content = str_replace("\n\n", "\n", $content);
+ } // END - if
+
+ // Return the content
+ return $content;
+}
+
+// Writes content to a file
+function writeToFile ($FQFN, $content) {
+ // Is the file writeable?
+ if ((isFileReadable($FQFN)) && (!is_writeable($FQFN)) && (!changeMode($FQFN, 0644))) {
+ // Not writeable!
+ DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("File %s not writeable.", basename($FQFN)));
+
+ // Failed! :(
+ return false;
+ } // END - if
+
+ // By default all is failed...
+ $return = false;
+
+ // Is the function there?
+ if (function_exists('file_put_contents')) {
+ // Write it directly
+ $return = file_put_contents($FQFN, $content);
+ } else {
+ // Write it with fopen
+ $fp = fopen($FQFN, 'w') or app_die(__FUNCTION__, __LINE__, "Cannot write file ".basename($FQFN).'!');
+ fwrite($fp, $content);
+ fclose($fp);
+
+ // Set CHMOD rights
+ $return = changeMode($FQFN, 0644);
+ }
+
+ // Return status
+ return $return;
+}
+
+// Clears the output buffer. This function does *NOT* backup sent content.
+function clearOutputBuffer () {
+ // Trigger an error on failure
+ if (!ob_end_clean()) {
+ // Failed!
+ debug_report_bug(__FUNCTION__.': Failed to clean output buffer.');
+ } // END - if
+}
+
+// Loads an include file and logs any missing files for debug purposes
+function loadInclude ($INC) {
+ // Add the path. This is why we need a trailing slash in config.php
+ $FQFN = constant('PATH') . $INC;
+
+ // Is the include file there?
+ if (!isIncludeReadable($INC)) {
+ // Not there so log it
+ debug_report_bug(sprintf("Include file %s not found.", $INC));
+ return false;
+ } // END - if
+
+ // Try to load it
+ require($FQFN);
+}
+
+// Loads an include file once
+function loadIncludeOnce ($INC) {
+ // Is it not loaded?
+ if (!isset($GLOBALS['load_once'][$INC])) {
+ // Then try to load it
+ loadInclude($INC);
+
+ // And mark it as loaded
+ $GLOBALS['load_once'][$INC] = "loaded";
+ } // END - if
+}
+
+// Checks wether an include file (non-FQFN better) is readable
+function isIncludeReadable ($INC) {
+ // Construct FQFN
+ $FQFN = constant('PATH') . $INC;
+
+ // Is it readable?
+ return isFileReadable($FQFN);
+}
+
+// Encode strings
+// @TODO Implement $compress
+function encodeString ($str, $compress = true) {
+ $str = urlencode(base64_encode(compileUriCode($str)));
+ return $str;
+}
+
+// Decode strings encoded with encodeString()
+// @TODO Implement $decompress
+function decodeString ($str, $decompress = true) {
+ $str = compileUriCode(base64_decode(urldecode(compileUriCode($str))));
+ return $str;
+}
+
+// Smartly adds slashes
+function smartAddSlashes ($unquoted) {
+ $unquoted = str_replace("\\", '', $unquoted);
+ return addslashes($unquoted);
+}
+
+// Decode entities in a nicer way
+function decodeEntities ($str) {
+ // Decode the entities to UTF-8 now
+ $decodedString = html_entity_decode($str, ENT_NOQUOTES, 'UTF-8');
+
+ // Return decoded string
+ return $decodedString;
+}
+
+// Merges an array together but only if both are arrays
+function merge_array ($array1, $array2) {
+ // Are both an array?
+ if ((is_array($array1)) && (is_array($array2))) {
+ // Merge all together
+ return array_merge($array1, $array2);
+ } elseif (is_array($array1)) {
+ // Return left array
+ DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("array2 is not an array. array != %s", gettype($array2)));
+ return $array1;
+ } elseif (is_array($array2)) {
+ // Return right array
+ DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("array1 is not an array. array != %s", gettype($array1)));
+ return $array2;
+ }
+
+ // Both are not arrays
+ debug_report_bug(__FUNCTION__.": No arrays provided!");
+}
+
+// Check if given FQFN is a readable file
+function isFileReadable ($FQFN) {
+ // Check all...
+ return ((file_exists($FQFN)) && (is_file($FQFN)) && (is_readable($FQFN)));
+}
+
+// Checks wether the given FQFN is a directory and not .,.. or .svn
+function isDirectory ($FQFN) {
+ // Generate baseName
+ $baseName = basename($FQFN);
+
+ // Check it
+ $isDirectory = ((is_dir($FQFN)) && ($baseName != '.') && ($baseName != '..') && ($baseName != '.svn'));
+
+ // Return the result
+ return $isDirectory;
+}
+
+// "Getter" for remote IP number
+function detectRemoteAddr () {
+ // Get remote ip from environment
+ $remoteAddr = determineRealRemoteAddress();
+
+ // Is removeip installed?
+ if (EXT_IS_ACTIVE('removeip')) {
+ // Then anonymize it
+ $remoteAddr = GET_ANONYMOUS_REMOTE_ADDR($remoteAddr);
+ } // END - if
+
+ // Return it
+ return $remoteAddr;
+}
+
+// "Getter" for remote hostname
+function detectRemoteHostname () {
+ // Get remote ip from environment
+ $remoteHost = getenv('REMOTE_HOST');
+
+ // Is removeip installed?
+ if (EXT_IS_ACTIVE('removeip')) {
+ // Then anonymize it
+ $remoteHost = GET_ANONYMOUS_REMOTE_HOST($remoteHost);
+ } // END - if
+
+ // Return it
+ return $remoteHost;
+}
+
+// "Getter" for user agent
+function detectUserAgent () {
+ // Get remote ip from environment
+ $userAgent = getenv('HTTP_USER_AGENT');
+
+ // Is removeip installed?
+ if (EXT_IS_ACTIVE('removeip')) {
+ // Then anonymize it
+ $userAgent = GET_ANONYMOUS_USER_AGENT($userAgent);
+ } // END - if
+
+ // Return it
+ return $userAgent;
+}
+
+// "Getter" for referer
+function detectReferer () {
+ // Get remote ip from environment
+ $referer = getenv('HTTP_REFERER');
+
+ // Is removeip installed?
+ if (EXT_IS_ACTIVE('removeip')) {
+ // Then anonymize it
+ $referer = GET_ANONYMOUS_REFERER($referer);
+ } // END - if
+
+ // Return it
+ return $referer;
+}
+
+// Check wether we are installing
+function isInstalling () {
+ $installing = ((isset($GLOBALS['mxchange_installing'])) || (REQUEST_ISSET_GET('installing')));
+ //* DEBUG: */ var_dump($installing);
+ return $installing;
+}
+
+// Check wether this script is installed
+function isInstalled () {
+ return (getConfig('MXCHANGE_INSTALLED') == 'Y');
+}
+
+// Check wether an admin is registered
+function isAdminRegistered () {
+ return (getConfig('ADMIN_REGISTERED') == 'Y');
+}
+
+// Checks wether the reset mode is active
+function isResetModeEnabled () {
+ // Now simply check it
+ return ((isset($GLOBALS['reset_enabled'])) && ($GLOBALS['reset_enabled'] === true));
+}
+
+// Checks wether the debug mode is enabled
+function isDebugModeEnabled () {
+ // Simply check it
+ return (getConfig('DEBUG_MODE') == 'Y');
+}
+
+// Checks wether the cache instance is valid
+function isCacheInstanceValid () {
+ return ((isset($GLOBALS['cache_instance'])) && (is_object($GLOBALS['cache_instance'])));
+}
+
+// Copies a file from source to destination and verifies if that goes fine.
+// This function should wrap the copy() command and make a nicer debug backtrace
+// even if there is no xdebug extension installed.
+function copyFileVerified ($source, $dest, $chmod = '') {
+ // Failed is the default
+ $status = false;
+
+ // Is the source file there?
+ if (!isFileReadable($source)) {
+ // Then abort here
+ debug_report_bug('Cannot read from source file ' . basename($source) . '.');
+ } // END - if
+
+ // Is the target directory there?
+ if (!isDirectory(dirname($dest))) {
+ // Then abort here
+ debug_report_bug('Cannot find directory ' . str_replace(constant('PATH'), '', dirname($dest)) . '.');
+ } // END - if
+
+ // Now try to copy it
+ if (!copy($source, $dest)) {
+ // Something went wrong
+ debug_report_bug('copy() has failed to copy the file.');
+ } // END - if
+
+ // If there are chmod rights set, apply them
+ if (!empty($chmod)) {
+ // Try to apply them
+ $status = changeMode($dest, $chmod);
+ } else {
+ // All fine
+ $status = true;
+ }
+
+ // All fine
+ return $status;
+}
+
+// Wrapper function for header()
+// Send a header but checks before if we can do so
+function sendHeader ($header) {
+ // Is the header already sent?
+ if (headers_sent()) {
+ // Then abort here
+ debug_report_bug('Headers already sent!');
+ } // END - if
+
+ // Send the header
+ header($header);
+}
+
+// Wrapper function for chmod()
+// @TODO Do some more sanity check here
+function changeMode ($FQFN, $mode) {
+ // Is the file/directory there?
+ if ((!isFile($FQFN)) && (!isDirectory($FQFN))) {
+ // Neither, so abort here
+ debug_report_bug('Cannot chmod() on ' . basename($FQFN) . '.');
+ } // END - if
+
+ // Try to set them
+ chmod($FQFN, $mode);
+}
+
+// Wrapper for $_POST['sel']
+function countPostSelection () {
+ return countSelection(REQUEST_POST('sel'));
+}
+
+// [EOF]
+?>