| // +----------------------------------------------------------------------+ // // $Id: file_put_contents.php,v 1.1.1.1 2006/03/28 10:00:11 joel Exp $ if (!defined('FILE_USE_INCLUDE_PATH')) { define('FILE_USE_INCLUDE_PATH', 1); } if (!defined('LOCK_EX')) { define('LOCK_EX', 2); } if (!defined('FILE_APPEND')) { define('FILE_APPEND', 8); } /** * Replace file_put_contents() * * @category PHP * @package PHP_Compat * @link http://php.net/function.file_put_contents * @author Aidan Lister * @version $Revision: 1.1.1.1 $ * @internal resource_context is not supported * @since PHP 5 * @require PHP 4.0.0 (user_error) */ if (!function_exists('file_put_contents')) { function file_put_contents($filename, $content, $flags = null, $resource_context = null) { // If $content is an array, convert it to a string if (is_array($content)) { $content = implode('', $content); } // If we don't have a string, throw an error if (!is_scalar($content)) { user_error('file_put_contents() The 2nd parameter should be either a string or an array', E_USER_WARNING); return false; } // Get the length of data to write $length = strlen($content); // Check what mode we are using $mode = ($flags & FILE_APPEND) ? 'a' : 'wb'; // Check if we're using the include path $use_inc_path = ($flags & FILE_USE_INCLUDE_PATH) ? true : false; // Open the file for writing if (($fh = @fopen($filename, $mode, $use_inc_path)) === false) { user_error('file_put_contents() failed to open stream: Permission denied', E_USER_WARNING); return false; } // Attempt to get an exclusive lock $use_lock = ($flags & LOCK_EX) ? true : false ; if ($use_lock === true) { if (!flock($fh, LOCK_EX)) { return false; } } // Write to the file $bytes = 0; if (($bytes = @fwrite($fh, $content)) === false) { $errormsg = sprintf('file_put_contents() Failed to write %d bytes to %s', $length, $filename); user_error($errormsg, E_USER_WARNING); return false; } // Close the handle @fclose($fh); // Check all the data was written if ($bytes != $length) { $errormsg = sprintf('file_put_contents() Only %d of %d bytes written, possibly out of free disk space.', $bytes, $length); user_error($errormsg, E_USER_WARNING); return false; } // Return length return $bytes; } } // -------- taken from PEAR::PHP_Compat ----------------- // // TODO: What to do about the 'dirtree' part of the registry file? // if (!isset($argv[1])) { die('Usage: '.$argv[0]." \n"); } $pear_dir = $argv[1]; if (!is_dir($pear_dir)) { die("Please supply a valid pear directory\n"); } // // update pearcmd.php // $pearcmd_file = $pear_dir . DIRECTORY_SEPARATOR . 'pearcmd.php'; if (is_file($pearcmd_file)) { echo "Updating pearcmd.php\n"; $pearcmd = file_get_contents($pearcmd_file); $pearcmd = preg_replace("%if \('.+' != '@'\.'include_path'\.'@'\) {%", "if ('$pear_dir' != '@'.'include_path'.'@') {", $pearcmd); $pearcmd = preg_replace("% ini_set\('include_path', '.+'\);%", " ini_set('include_path', '$pear_dir');", $pearcmd); file_put_contents($pearcmd_file, $pearcmd) or die("Error writing to pearcmd.php\n"); } // // update registry // $registry = $pear_dir . DIRECTORY_SEPARATOR . '.registry'; if (is_dir($registry)) { foreach (glob($registry.DIRECTORY_SEPARATOR.'*.reg') as $file) { $register = unserialize(file_get_contents($file)); if (is_array($register['filelist'])) { $changed = false; foreach ($register['filelist'] as $key => $val) { if (is_array($val) && array_key_exists('installed_as',$val)) { $package_name = isset($register['package']) ? $register['package'] : $register['name']; switch ($val['role']) { case 'php': default: $new_installed_as = $pear_dir.DIRECTORY_SEPARATOR.$key; break; case 'data': $new_installed_as = $pear_dir.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.$package_name.DIRECTORY_SEPARATOR.$key; break; // 'doc' is in the 'docs' dir according to go-pear case 'doc': $new_installed_as = $pear_dir.DIRECTORY_SEPARATOR.'docs'.DIRECTORY_SEPARATOR.$package_name.DIRECTORY_SEPARATOR.$key; break; // 'test' is in 'tests' case 'test': $new_installed_as = $pear_dir.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.$package_name.DIRECTORY_SEPARATOR.$key; break; } //echo $val['installed_as'] . " -> " . $new_installed_as . "\n"; $register['filelist'][$key]['installed_as'] = $new_installed_as; $changed = true; } } if ($changed) { echo 'Updating '.$file."\n"; if (!file_put_contents($file, serialize($register))) { die("Error writing to file ".$file."\n"); } } } } } ?>