PHP Classes
elePHPant
Icontem

File: lib/functions.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Jaswinder  >  PHP My SVN Admin  >  lib/functions.php  >  Download  
File: lib/functions.php
Role: Auxiliary script
Content type: text/plain
Description: Class source
Class: PHP My SVN Admin
Manage SubVersion repositories
Author: By
Last change:
Date: 2 years ago
Size: 4,409 bytes
 

Contents

Class file image Download
<?php
/**
 * This file contains all common functions which can not (really) be part of complete any class in this package
 *
 * @package PHPMySVNAdmin
 * @author Jaswinder Rattanpal (www.rattanpal.com)
 * @version 1.0
 */
    /**
     * Wrapper around print_r() function
     * @param Mixed $var Variable to output in print_r()
     */
   
function printr($var){
        echo
'<pre>';
       
print_r($var);
        echo
'</echo>';
    }
   
/**
     * Run command using system() command
     * @param String $cmd Command to run
     */
   
function runCommand($cmd){
       
system($cmd);
    }
   
/**
     * use ob function to reuiq_once() a file
     * @repo String $file File to be included
     */
   
function obRequireOnce($file,&$data = array()){
       
ob_start();
        require_once(
$file);
       
$contents = ob_get_contents();
       
ob_end_clean();
        return
$contents;
    }
   
/**
     * Generate HTML Select field
     */
   
function genHtmlSelectTag($name,$options,$userKeyAsValue=false){
       
$str = '<select name="'.$name.'" id="'.$name.'">';
        foreach(
$options as $key=>$value){
           
$str .= "<option value='$key'>".($userKeyAsValue?$key:$value).'</option>';
        }
       
$str .= '</select>';
        return
$str;
    }
   
/**
     * Function to read ini file
     * Cannot use par_ini_file becuase SVN permissions file include # as comments and thos ehave been deprecated in parse_ini_file
     */
   
function read_ini_file($f, &$r)
    {
       
$null = "";
       
//$r=$null;
       
$first_char = "";
       
$sec=$null;
       
$comment_chars=";#";
       
$num_comments = "0";
       
$num_newline = "0";

       
//Read to end of file with the newlines still attached into $f
       
$f = @file($f);
        if (
$f === false) {
            return -
2;
        }
       
// Process all lines from 0 to count($f)
       
for ($i=0; $i<@count($f); $i++)
        {
           
$w=@trim($f[$i]);
           
$first_char = @substr($w,0,1);
            if (
$w)
            {
                if ((@
substr($w,0,1)=="[") and (@substr($w,-1,1))=="]") {
                   
$sec=@substr($w,1,@strlen($w)-2);
                   
$num_comments = 0;
                   
$num_newline = 0;
                }
                else if ((
stristr($comment_chars, $first_char) == true)) {
                   
//$r[$sec]["Comment_".$num_comments]=$w;
                    //$num_comments = $num_comments +1;
               
}
                else {
                   
// Look for the = char to allow us to split the section into key and value
                   
$w=@explode("=",$w);
                   
$k=@trim($w[0]);
                    unset(
$w[0]);
                   
$v=@trim(@implode("=",$w));
                   
// look for the new lines
                   
if ((@substr($v,0,1)=="\"") and (@substr($v,-1,1)=="\"")) {
                       
$v=@substr($v,1,@strlen($v)-2);
                    }
                   
$r[$sec][$k]=$v;
                }
            }
           
//else {
                //$r[$sec]["Newline_".$num_newline]=$w;
                //$num_newline = $num_newline +1;
            //}
       
}
        return
true;
    }
   
/**
     * Add INI file back in proper format (SVN Permissions file in this case)
     */
   
function write_php_ini($array, $file)
    {
       
$res = array();
        foreach(
$array as $key => $val)
        {
            if(
is_array($val))
            {
               
$res[] = "[$key]";
                foreach(
$val as $skey => $sval) $res[] = "$skey = ".(is_numeric($sval) ? $sval : $sval);
                
$res[] = "\r\n";
            }
            else
$res[] = "$key = ".(is_numeric($val) ? $val : $val);
        }
       
safefilerewrite($file, implode("\r\n", $res));
    }
   
//////
   
function safefilerewrite($fileName, $dataToSave)
    { if (
$fp = fopen($fileName, 'w'))
        {
           
$startTime = microtime();
            do
            {
$canWrite = flock($fp, LOCK_EX);
              
// If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
              
if(!$canWrite) usleep(round(rand(0, 100)*1000));
            } while ((!
$canWrite)and((microtime()-$startTime) < 1000));

           
//file was locked so now we can store information
           
if ($canWrite)
            {
fwrite($fp, $dataToSave);
               
flock($fp, LOCK_UN);
            }
           
fclose($fp);
        }

    }