1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4/**
5 * Keep only the files that have a specific MIME type
6 *
7 * PHP versions 4 and 5
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA
22 *
23 * @category   File Formats
24 * @package    File_Archive
25 * @author     Vincent Lascaux <vincentlascaux@php.net>
26 * @copyright  1997-2005 The PHP Group
27 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL
28 * @version    CVS: $Id: MIME.php,v 1.5 2005/04/21 10:01:46 vincentlascaux Exp $
29 * @link       http://pear.php.net/package/File_Archive
30 */
31
32require_once "File/Archive/Predicate.php";
33require_once "MIME/Type.php";
34
35/**
36 * Keep only the files that have a specific MIME type
37 *
38 * @see        File_Archive_Predicate, File_Archive_Reader_Filter
39 */
40class File_Archive_Predicate_MIME extends File_Archive_Predicate
41{
42    var $mimes;
43
44    /**
45     * @param $extensions array or comma separated string of allowed extensions
46     */
47    function File_Archive_Predicate_MIME($mimes)
48    {
49        if (is_string($mimes)) {
50            $this->mimes = explode(",",$mimes);
51        } else {
52            $this->mimes = $mimes;
53        }
54    }
55    /**
56     * @see File_Archive_Predicate::isTrue()
57     */
58    function isTrue(&$source)
59    {
60        $sourceMIME = $source->getMIME();
61        foreach ($this->mimes as $mime) {
62            if (MIME_Type::isWildcard($mime)) {
63                $result = MIME_Type::wildcardMatch($mime, $sourceMIME);
64            } else {
65                $result = ($mime == $sourceMIME);
66            }
67            if ($result !== false) {
68                return $result;
69            }
70        }
71        return false;
72    }
73}
74
75?>