Pluf Framework

Pluf Framework Git Source Tree


Root/src/Pluf/Utils.php

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser 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
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * This file contains several utility classes.
 * These classes have only static methods.
 */
 
class Pluf_Utils
{
    /**
     * Produces a random string.
     *
     * @param int Length of the random string to be generated.
     * @return string Random string
     */
    static function getRandomString($len=35)
    {
        $string = '';
        $chars = '0123456789abcdefghijklmnopqrstuvwxyz'
            .'ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&*()+=-_}{[]><?/';
        $lchars = strlen($chars);
        $i = 0;
        while ($i<$len) {
            $string .= substr($chars, mt_rand(0, $lchars-1), 1);
            $i++;
        }
        return $string;
    }
 
    /**
     * Produces a random password.
     *
     * The random password generator avoid characters that can be
     * confused like 0,O,o,1,l,I.
     *
     * @param int Length of the password (8)
     * @return string Password
     */
    static function getPassword($len=8)
    {
        $string = '';
        $chars = '23456789abcdefghijkmnpqrstuvwxyz'
            .'ABCDEFGHJKLMNPQRSTUVWXYZ';
        $lchars = strlen($chars);
        $i = 0;
        while ($i<$len) {
            $string .= substr($chars, mt_rand(0, $lchars-1), 1);
            $i++;
        }
        return $string;
    }
 
    /**
     * Clean the name of a file to only have alphanumeric characters.
     *
     * @param string Name
     * @return string Clean name
     */
    static function cleanFileName($name)
    {
        return mb_ereg_replace("/\015\012|\015|\012|\s|[^A-Za-z0-9\.\-\_]/", '_', $name);
    }
 
    static function prettySize($size)
    {
        $mb = 1024*1024;
        if ($size > $mb) {
            $mysize = sprintf('%01.2f', $size/$mb).' '. __('MB');
        } elseif ($size >= 1024) {
            $mysize = sprintf('%01.2f', $size/1024).' '.__('kB');
        } else {
            $mysize = sprintf('%d', $size).' '.__('bytes');
        }
        return $mysize;
    }
 
    /**
     * RFC(2)822 Email Parser
     *
     * By Cal Henderson <cal@iamcal.com>
     * This code is licensed under a Creative Commons
     * Attribution-ShareAlike 2.5 License
     *
     * Comments were stripped, check the source for the way this
     * parser is built. It is a very interesting reading.
     *
     * @param string Email
     * @return bool Is email
     */
    static function isValidEmail($email)
    {
        $email = trim($email);
        $n = split(' ', $email);
        if (count($n) > 1) {
            return false;
        }
        $no_ws_ctl = "[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]";
        $alpha = "[\\x41-\\x5a\\x61-\\x7a]";
        $digit = "[\\x30-\\x39]";
        $cr = "\\x0d";
        $lf = "\\x0a";
        $crlf = "($cr$lf)";
        $obs_char = "[\\x00-\\x09\\x0b\\x0c\\x0e-\\x7f]";
        $obs_text = "($lf*$cr*($obs_char$lf*$cr*)*)";
        $text = "([\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f]|$obs_text)";
        $obs_qp = "(\\x5c[\\x00-\\x7f])";
        $quoted_pair = "(\\x5c$text|$obs_qp)";
        $wsp = "[\\x20\\x09]";
        $obs_fws = "($wsp+($crlf$wsp+)*)";
        $fws = "((($wsp*$crlf)?$wsp+)|$obs_fws)";
        $ctext = "($no_ws_ctl|[\\x21-\\x27\\x2A-\\x5b\\x5d-\\x7e])";
        $ccontent = "($ctext|$quoted_pair)";
        $comment = "(\\x28($fws?$ccontent)*$fws?\\x29)";
        $cfws = "(($fws?$comment)*($fws?$comment|$fws))";
        $cfws = "$fws*";
        $atext = "($alpha|$digit|[\\x21\\x23-\\x27\\x2a\\x2b\\x2d\\x2f\\x3d\\x3f\\x5e\\x5f\\x60\\x7b-\\x7e])";
        $atom = "($cfws?$atext+$cfws?)";
        $qtext = "($no_ws_ctl|[\\x21\\x23-\\x5b\\x5d-\\x7e])";
        $qcontent = "($qtext|$quoted_pair)";
        $quoted_string = "($cfws?\\x22($fws?$qcontent)*$fws?\\x22$cfws?)";
        $word = "($atom|$quoted_string)";
        $obs_local_part = "($word(\\x2e$word)*)";
        $obs_domain = "($atom(\\x2e$atom)*)";
        $dot_atom_text = "($atext+(\\x2e$atext+)*)";
        $dot_atom = "($cfws?$dot_atom_text$cfws?)";
        $dtext = "($no_ws_ctl|[\\x21-\\x5a\\x5e-\\x7e])";
        $dcontent = "($dtext|$quoted_pair)";
        $domain_literal = "($cfws?\\x5b($fws?$dcontent)*$fws?\\x5d$cfws?)";
        $local_part = "($dot_atom|$quoted_string|$obs_local_part)";
        $domain = "($dot_atom|$domain_literal|$obs_domain)";
        $addr_spec = "($local_part\\x40$domain)";
 
        $done = 0;
        while(!$done){
            $new = preg_replace("!$comment!", '', $email);
            if (strlen($new) == strlen($email)){
                $done = 1;
            }
            $email = $new;
        }
        return preg_match("!^$addr_spec$!", $email) ? true : false;
    }
 
    /**
     * Validate an url.
     * Only the structure is checked, no check of availability of the
     * url is performed. It is a really basic validation.
     */
    static function isValidUrl($url)
    {
        $ip = '(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.'
            .'(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}';
        $dom = '([a-z0-9\.\-]+)';
        return (preg_match('!^(http|https|ftp|gopher)\://('.$ip.'|'.$dom.')!i', $url)) ? true : false;
    }
 
 
    /**
     * Convert a whatever separated list of items and returns an array
     * of them.
     *
     * @param string Items.
     * @param string Separator (',')
     * @return array Items.
     */
    static function itemsToArray($items, $sep=',')
    {
        $_t = explode($sep, $items);
        $res = array();
        foreach ($_t as $item) {
            $item = trim($item);
            if (strlen($item) > 0) {
                $res[] = $item;
            }
        }
        return $res;
    }
 
    /**
     * Run an external program capturing both stdout and stderr.
     *
     * @credits dk at brightbyte dot de
     * @see proc_open
     *
     * @param string Command to run (will be passed to proc_open)
     * @param &int Return code of the command
     * @return mixed false in case of error or output string
     */
    public static function runExternal($cmd, &$code)
    {
        $descriptorspec = array(
               // stdin is a pipe that the child will read from
               0 => array('pipe', 'r'), 
               // stdout is a pipe that the child will write to
               1 => array('pipe', 'w'),
               // stderr is a file to write to
               2 => array('pipe', 'w'));
        $pipes= array();
        $process = proc_open($cmd, $descriptorspec, $pipes);
        $output= '';
        if (!is_resource($process)) return false;
        fclose($pipes[0]); //close child's input imidiately
        stream_set_blocking($pipes[1], false);
        stream_set_blocking($pipes[2], false);
        $todo = array($pipes[1], $pipes[2]);
        while (true) {
            $read = array();
            if(!feof($pipes[1])) $read[] = $pipes[1];
            if(!feof($pipes[2])) $read[] = $pipes[2];
            if (!$read) break;
            $write = $ex = array();
            $ready= stream_select($read, $write, $ex, 2);
            if ($ready === false) {
                break; // should never happen - something died
            }
            foreach ($read as $r) {
                $s = fread($r,1024);
                $output .= $s;
            }
        }
        fclose($pipes[1]);
        fclose($pipes[2]);
        $code = proc_close($process);
        return $output;
    }
}

Archive Download this file

Branches

Tags

Number of commits:
Page rendered in 0.09247s using 11 queries.