~chroot64bit/zivios/gentoo-experimental

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
 * Copyright (c) 2008 Zivios, LLC.
 *
 * This file is part of Zivios.
 *
 * Zivios 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 3 of the License, or
 * (at your option) any later version.
 *
 * Zivios 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 Zivios.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package		Zivios
 * @copyright	Copyright (c) 2008 Zivios, LLC. (http://www.zivios.org)
 * @license		http://www.zivios.org/legal/license
 * @version		$Id: Util.php 1065 2008-09-10 06:33:08Z mhashmi $
 * @subpackage  Core
 **/

class Zivios_Util
{
	public static function htmlClean($var)
	{
		return trim(chop(strip_tags($var)));
	}

	public static function makeLowerCase($value)
	{

		if (is_array($value)) {
			$retarray= array();
			foreach ($value as $val) {
				$retarray[] = trim(strtolower($val));
			}
			return $retarray;
		}
		else {
			return trim(strtolower($value));
		}
	}

	public static function formatSize($size, $unit=null, $retstring=null, $si=true)
	{
		if ($si === true) {
			$sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
			$mod   = 1000;
		} else {
			$sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
			$mod   = 1024;
		}

		$ii = count($sizes) - 1;
		$unit = array_search((string) $unit, $sizes);

		if ($unit === null || $unit === false)
			$unit = $ii;

		if ($retstring === null)
			$retstring = '%01.2f %s';

		$i = 0;
		while ($unit != $i && $size >= 1024 && $i < $ii) {
			$size /= $mod;
			$i++;
		}
		return sprintf($retstring, $size, $sizes[$i]);
	}

	public static function parseQuotedValue($str,$name)
	{
		$matches = array();
		$hello = preg_match_all("/ $name \'(.+?)\'/",$str,$matches);
		return $matches[1][0];

	}
	public static function parseValue($str,$name)
	{
		$matches = array();
		$boo = preg_match_all("/ $name (.+?) /",$str,$matches);
		return $matches[1][0];
	}

	public static function magicParseValue($str,$name)
	{
		$matches = array();
		$return = preg_match_all("/ $name \'(.+?)\'| $name (\w.+?) /",$str,$matches);
		if ($matches[1][0] == '') {
			return $matches[2][0];
		} else return $matches[1][0];

	}

    public static function renderTmplToCfg($template,$valarray)
    {
        $file = file_get_contents($template);
        $matches = array();

        preg_match_all("/\%\%\%(.+?)\%\%\%/",$file,$matches);

        foreach ($matches[1] as $match) {
            $tosubs = strtolower($match);
            $value = $valarray[$tosubs];
            $file = preg_replace("/\%\%\%$match\%\%\%/",$value,$file);
        }

        return $file;
    }

    /**
     * Generates a random string of specified length.
     *
     * NOTE: if you call this function outside the Zivios bootloader process,
     * ensure your seed the randomizer.
     *
     * @param int $len
     * @return string $str
     */
	public static function randomString($len)
	{
		$str = '';
		$i=0;
	    while($i<$len) {
	        $str.=chr((rand()%26)+97);
	        $i++;
	    }
	    return $str;
	}

    public static function autoPopulateFromRows($obj,$rows)
    {
        foreach ($rows as $key => $value) {
            $obj->$key = $value;
        }
    }
}