dftk
[ class tree: dftk ] [ index: dftk ] [ all elements ]

Source for file dftk_misc_fct.php

Documentation is available at dftk_misc_fct.php


1 <?php
2 /*
3
4 Copyright (c)2003 DuckCorp(tm) and RtpNet(tm)
5
6
7
8 This file is part of DFTK.
9
10 DFTK is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 DFTK is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with DFTK; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25
26 /**
27 * @package dftk
28 * @subpackage dftk-utilityfct
29 * @author Duck <duck@DuckCorp.org>
30 * @author Rtp <rtp@rtp-net.org>
31 * @copyright Copyright (c)2003 DuckCorp(tm) and RtpNet(tm)
32 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
33 * @version 0.4.0
34 */
35
36
37 /**
38 * Display an array, for debugging purpose
39 *
40 * @access public
41 * @param array &$arr Array to display
42 */
43 function dftk_display_array(&$arr)
44 {
45 if (!is_array($arr))
46 return;
47
48 echo '<table cellpadding="0" cellspacing="0" border="1">';
49 foreach ($arr as $key1 => $elem1)
50 {
51 echo '<tr>';
52 echo '<td>'.$key1.'&nbsp;</td>';
53 if (is_array($elem1))
54 _dftk_display_array2($elem1);
55 else
56 echo '<td>'.$elem1.'&nbsp;</td>';
57 echo '</tr>';
58 }
59 echo '</table>';
60 }
61
62 /**
63 * Display a part of an array
64 *
65 * @access private
66 * @param array $arr2 Part of array to display
67 * @see display_array()
68 */
69 function _dftk_display_array2($arr2)
70 {
71 echo '<td>';
72 echo '<table cellpadding="0" cellspacing="0" border="1">';
73 foreach ($arr2 as $key => $elem)
74 {
75 echo '<tr>';
76 echo '<td>'.$key.'&nbsp;</td>';
77 if (is_array($elem))
78 _dftk_display_array2($elem);
79 else
80 {
81 if (is_bool($elem))
82 {
83 if ($elem)
84 echo '<td>#true#</td>';
85 else
86 echo '<td>#false#</td>';
87 }
88 else
89 echo '<td>'.htmlspecialchars($elem).'&nbsp;</td>';
90 }
91 echo '</tr>';
92 }
93 echo '</table>';
94 echo '</td>';
95 }
96
97 /**
98 * Crypt for unix usage
99 *
100 * @access public
101 * @param string $passwd Clear-text Password
102 * @return string
103 */
104 function dftk_unix_crypt($passwd)
105 {
106 $salt=substr(ereg_replace("[^a-zA-Z0-9./]", "", crypt(rand(10000000,99999999), rand(10,99))), 2, 2);
107 return crypt($passwd, "$1$".$salt);
108 }
109
110 /**
111 * Test if is an hexadecimal number
112 *
113 * @access public
114 * @param string $val Value to test
115 * @return boolean
116 */
117 function dftk_is_hexa($val)
118 {
119 if (preg_match("/^[0-9a-f]*$/i", $val)>0)
120 return true;
121 else
122 return false;
123 }
124
125 /**
126 * Delete a specified value in an array
127 *
128 * Delete each occurence of a specified value in an array
129 * and reindex it.
130 *
131 * @access public
132 * @param array $tab_orig Array to process
133 * @param mixed $value Value to delete
134 * @return array $tab Array processed
135 */
136 function dftk_array_del_value($tab_orig, $value)
137 {
138 $tab = array();
139
140 for ($i=0; $i<count($tab_orig); $i++)
141 if ($tab_orig[$i] != $value)
142 $tab[] = $tab_orig[$i];
143
144 return $tab;
145 }
146
147 /**
148 * Insert a value in a specified position in an array
149 *
150 * @access public
151 * @param array $tab_orig Array to process
152 * @param integer $n Position where to insert the value
153 * @param mixed $value Value to insert
154 * @return array $tab Array processed
155 */
156 function dftk_array_insert($tab_orig, $n, $value)
157 {
158 if ($n <= 0)
159 return array_merge(array($value), $tab_orig);
160 elseif ($n >= count($tab_orig))
161 return array_merge($tab_orig, array($value));
162 else
163 return array_merge(array_slice($tab_orig, 0, $n), array($value), array_slice($tab_orig, $n));
164 }
165
166 /**
167 * Insert a value in a sorted array
168 *
169 * This function ensure the array will stay sorted after each insert.
170 *
171 * @access public
172 * @param array $tab_orig Array to process
173 * @param mixed $value Value to insert
174 * @param string $fct Name of the sort function
175 * @return array $tab Array processed
176 */
177 function dftk_array_sorted_insert($tab_orig, $value, $fct)
178 {
179 $n = count($tab_orig);
180
181 for ($i=0; $i<count($tab_orig); $i++)
182 if ($fct($value, $tab_orig[$i]))
183 {
184 $n = $i;
185 break;
186 }
187
188 return dftk_array_insert($tab_orig, $n, $value);
189 }
190
191 ?>

Documentation generated on Sat, 6 Dec 2003 13:47:41 +0100 by phpDocumentor 1.2.3