Initial add of defaria.com
[clearscm.git] / defaria.com / php / id3.class.php
1 <?php\r
2 /***********************************************************\r
3 * Class:       ID3\r
4 * Version:     1.0\r
5 * Date:        Janeiro 2004\r
6 * Author:      Tadeu F. Oliveira\r
7 * Contact:     tadeu_fo@yahoo.com.br\r
8 * Use:         Extract ID3 Tag information from mp3 files\r
9 ************************************************************/\r
10 class ID3{\r
11   var $file_name        = '';\r
12   var $tags;\r
13   var $last_error_num   = 0;\r
14   var $tags_count       = 0;\r
15 \r
16   function hex2bin($data) {\r
17     $len = strlen ($data);\r
18 \r
19     for ($i = 0; $i < $len; $i += 2) {\r
20       $newdata .= pack ("C", hexdec (substr ($data, $i, 2)));\r
21     } // for\r
22 \r
23    return $newdata;\r
24   } // hex2bin\r
25    \r
26   function get_frame_size ($fourBytes) {\r
27     $tamanho [0] = str_pad (base_convert (substr ($fourBytes, 0, 2), 16, 2), 7, 0, STR_PAD_LEFT);\r
28     $tamanho [1] = str_pad (base_convert (substr ($fourBytes, 2, 2), 16, 2), 7, 0, STR_PAD_LEFT);\r
29     $tamanho [2] = str_pad (base_convert (substr ($fourBytes, 4, 2), 16, 2), 7, 0, STR_PAD_LEFT);\r
30     $tamanho [3] = str_pad (base_convert (substr ($fourBytes, 6, 2), 16, 2), 7, 0, STR_PAD_LEFT);\r
31     $total       = $tamanho [0] . $tamanho [1] . $tamanho [2] . $tamanho [3];\r
32 \r
33     $tamanho [0] = substr ($total,  0, 8);\r
34     $tamanho [1] = substr ($total,  8, 8);\r
35     $tamanho [2] = substr ($total, 16, 8);\r
36     $tamanho [3] = substr ($total, 24, 8);\r
37     \r
38     $total       = $tamanho [0] . $tamanho [1] . $tamanho [2] . $tamanho [3];\r
39     $total       = base_convert ($total, 2, 10);\r
40     return $total;\r
41   } // get_frame_size\r
42         \r
43   function extractTags ($text, &$tags) {\r
44     $size = -1;\r
45 \r
46     while ((strlen ($text) != 0) and ($size != 0)) {\r
47       //while there are tags to read and they have a meaning\r
48       $ID    = substr ($text, 0, 4);\r
49       $aux   = substr ($text, 4, 4);\r
50       $aux   = bin2hex ($aux);\r
51       $size  = $this->get_frame_size ($aux);\r
52       $flags = substr ($text, 8, 2);\r
53       $info  = substr ($text, 11, $size - 1);\r
54       \r
55       if ($size != 0){\r
56         $tags [$ID] = $info;\r
57         $this->tags_count++;\r
58       } // if\r
59 \r
60       $text = substr ($text, 10 + $size, strlen ($text));\r
61     } // while\r
62   } // extractTags\r
63    \r
64   function ID3 ($file_name) {\r
65     $this->file_name            = $file_name;\r
66     $this->last_error_num       = 0;\r
67   } // ID3\r
68    \r
69   function getInfo () {\r
70     if ($this->file_name != ''){\r
71       $mp3      = @fopen ($this->file_name, "r");\r
72       $header   = @fread ($mp3, 10);\r
73 \r
74       if (!$header) {\r
75         $this->last_error_num = 2;\r
76         return false;\r
77       } // if\r
78 \r
79       if (substr ($header, 0, 3) != "ID3") {\r
80         $this->last_error_num = 3;\r
81         return false;\r
82       } // if\r
83 \r
84       $header   = bin2hex ($header);\r
85       $version  = base_convert (substr ($header,  6, 2), 16, 10) . "." . \r
86                   base_convert (substr ($header,  8, 2), 16, 10);\r
87       $flags    = base_convert (substr ($header, 10, 2), 16,  2);\r
88       $flags    = str_pad ($flags, 8, 0, STR_PAD_LEFT);\r
89 \r
90       if ($flags [7] == 1){\r
91         // echo ('with Unsynchronisation<br>');\r
92       } // if\r
93 \r
94       if ($flags [6] == 1){\r
95         // echo ('with Extended header<br>');\r
96       } // if\r
97 \r
98       if ($flags [5] == 1){\r
99         // Esperimental tag\r
100         $this->last_error_num = 4;\r
101         return false;\r
102       } // if\r
103 \r
104       $total    = $this->get_frame_size (substr ($header, 12, 8));\r
105       $text     = @fread ($mp3,$total);\r
106 \r
107       fclose ($mp3);\r
108 \r
109       $this->extractTags ($text, $this->tags);\r
110     } else {\r
111       $this->last_error_num = 1; //file not set\r
112       return false;\r
113     } // if\r
114 \r
115     return true;\r
116   } // getInfo\r
117    \r
118   function getArtist () {\r
119     if (array_key_exists ('TPE1', $this->tags)) {\r
120       return $this->tags ['TPE1'];\r
121     } else {\r
122       $this->last_error_num = 5;\r
123       return false;\r
124     } // if\r
125   } // getArtist\r
126    \r
127   function getTrack () {\r
128     if (array_key_exists ('TRCK', $this->tags)) {\r
129       return $this->tags ['TRCK'];\r
130     } else {\r
131       $this->last_error_num = 5;\r
132       return false;\r
133     } // if\r
134   } // getTrack\r
135    \r
136   function getTitle () {\r
137     if (array_key_exists ('TIT2', $this->tags)) {\r
138         return $this->tags ['TIT2'];\r
139     } else {\r
140       $this->last_error_num = 5;\r
141       return false;\r
142     } // if\r
143   } // getTitle\r
144    \r
145   function getAlbum () {\r
146     if (array_key_exists ('TALB', $this->tags)) {\r
147       return $this->tags ['TALB'];\r
148     } else {\r
149       $this->last_error_num = 5;\r
150       return false;\r
151     } // if\r
152   } // getAlbum\r
153    \r
154   function getYear () {\r
155     if (array_key_exists ('TYER', $this->tags)) {\r
156       return $this->tags ['TYER'];\r
157     } else {\r
158       $this->last_error_num = 5;\r
159       return false;\r
160     } // if\r
161   } // getYear\r
162    \r
163   function getGender(){\r
164     if (array_key_exists ('TCON', $this->tags)) {\r
165       return $this->tags ['TCON'];\r
166     } else {\r
167       $this->last_error_num = 5;\r
168       return false;\r
169     } // if\r
170   } // getGender\r
171 } // ID3\r
172 ?>\r