assert_options(ASSERT_ACTIVE, true);//允许使用assert()函数
assert_options(ASSERT_WARNING, false);//在assert失败时不输出警告信息
assert_options(ASSERT_BAIL, true);//assert失败后终止代码执行
assert_options(ASSERT_CALLBACK, 'getMsg');//assert失败后终止代码执行。
echo '开始:<br/>';
assert('mysql_query("")');
echo '测试成功!';
function getMsg(){
echo '出错啦!';
}
mixed assert_options ( int $what [, mixed $value ] ) — 设置 assert() 的各种控制选项,或者查询当前的设置
ASSERT_ACTIVE : 是否启用 assert() 断言, ini配置 assert.active,默认值 1
ASSERT_WARNING :是否为每个失败的断言产生一个 PHP 警告,ini配置 assert.warning,默认1
ASSERT_BAIL :是否在断言失败时中止执行,ini配置 assert.bail,默认值0
ASSERT_QUIET_EVAL :是否在断言表达式求值时禁用 error_reporting,ini配置assert.quiet_eval,默认值0
ASSERT_CALLBACK :断言失败时调用回调函数,ini配置assert.callback
assert_options(ASSERT_ACTIVE, true);//允许使用assert()函数
assert_options(ASSERT_WARNING, false);//在assert失败时不输出警告信息
assert_options(ASSERT_BAIL, true);//assert失败后终止代码执行
assert_options(ASSERT_CALLBACK, 'getMsg');//assert失败后终止代码执行。
echo '开始:<br/>';
assert(is_int(1.2));//检测结果为fales
echo '测试成功!';
function getMsg(){
echo '出错啦!';
}
bool dl( string $library ) — 获取 PHP 配置选项的值 载入指定的 PHP扩展
if(!extension_loaded('sqlite')){//测试指定的扩展是否已经激活
$prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX);
}
int gc_collect_cycles() — 强制收集所有现存的垃圾循环周期
void gc_disable ( void ) — 停用循环引用收集器
void gc_enable ( void ) — 激活循环引用收集器
bool gc_enabled ( void ) — 返回循环引用计数器的状态
string get_cfg_var ( string $option ) — 获取 PHP 配置选项的值获取 PHP 配置选项的值
string get_current_user ( void )— 获取当前 PHP 脚本所有者名称
array get_defined_constants ([ bool $categorize = false ] )— 返回所有常量的关联数组
array get_extension_funcs ( string $module_name )— 返回模块函数名称的数组
print_r(get_extension_funcs("xml"));
string get_include_path ( void ) — 获取当前的 include_path 配置选项
array get_included_files ( void )— 返回被 include 和 require 文件名的 array
include 'test1.php';
include_once 'test2.php';
require 'test3.php';
require_once 'test4.php';
$included_files = get_included_files();
foreach ($included_files as $filename){
echo "$filename\n";
}
array get_loaded_extensions ([ bool $zend_extensions = false ] )— 返回所有编译并加载模块名的 array
bool get_magic_quotes_gpc ( void )— 获取当前 magic_quotes_gpc 的配置选项设置
bool get_magic_quotes_runtime ( void ) — 获取当前 magic_quotes_runtime 配置选项的激活状态
string getenv ( string $varname ) — 获取一个环境变量的值
$ip = getenv('REMOTE_ADDR');
int getlastmod ( void )— 获取页面最后修改的时间
int getmygid ( void )— 获取当前 PHP 脚本拥有者的 GID
int getmyinode ( void )— 获取当前脚本的索引节点(inode)
int getmypid ( void )— 获取 PHP 进程的 ID
int getmyuid ( void )— 获取 PHP 脚本所有者的 UID
array getopt ( string $options [, array $longopts ] )— 从命令行参数列表中获取选项
array getrusage ([ int $who = 0 ] ) — 获取当前资源使用状况
array ini_get_all ([ string $extension [, bool $details = true ]] ) — 获取所有配置选项
print_r(ini_get_all("pcre"));
print_r(ini_get_all());
string ini_get ( string $varname ) — 获取一个配置选项的值
void ini_restore ( string $varname )— 恢复配置选项的默认值
string ini_set ( string $varname , string $newvalue )— 为一个配置选项设置值
main — 虚拟的 main()int memory_get_peak_usage ([ bool $real_usage = false ] )— 返回分配给 PHP 内存的峰值int memory_get_usage ([ bool $real_usage = false ] ) — 返回分配给 PHP 的内存量
string php_ini_loaded_file ( void ) — 取得已加载的 php.ini 文件的路径
string php_ini_scanned_files ( void )— 返回从额外 ini 目录里解析的 .ini 文件列表
string php_sapi_name ( void ) — 返回 web 服务器和 PHP 之间的接口类型
string php_uname ([ string $mode = "a" ] )— 返回运行 PHP 的系统的有关信息
'a':此为默认all。
's':操作系统名称
'n':主机名。例如: localhost.example.com。
'r':版本名称,例如: 5.1.2-RELEASE。
'v':版本信息。操作系统之间有很大的不同。
'm':机器类型。例如:i386。
bool phpcredits ([ int $flag = CREDITS_ALL ] ) — 打印 PHP 贡献者名单
CREDITS_ALL :所有的
CREDITS_DOCS : 文档组贡献名单
CREDITS_FULLPAGE : 常用于和其他标志进行组合。 表示需要打印包含其他标志表示信息的独立 HTML 页面。
CREDITS_GENERAL : 普遍名单:语言设计与理念、PHP作者以及 SAPI 模块
CREDITS_GROUP : 核心开发者名单
CREDITS_MODULES : PHP 扩展模块以及作者
CREDITS_SAPI : PHP 的服务器 API 模块以及作者
phpcredits(CREDITS_GROUP | CREDITS_DOCS | CREDITS_FULLPAGE);
bool phpinfo ([ int $what = INFO_ALL ] ) — 输出关于 PHP 配置的信息
string phpversion ([ string $extension ] ) — 获取当前的PHP版本
bool putenv ( string $setting )— 设置环境变量的值
void restore_include_path ( void ) — 还原 include_path 配置选项的值
string set_include_path ( string $new_include_path ) — 设置 include_path 配置选项
void set_time_limit ( int $seconds )— 设置脚本最大执行时间,从它本身开始计时,0表示不限时
string sys_get_temp_dir ( void ) — 返回用于临时文件的目录
mixed version_compare ( string $version1 , string $version2 [, string $operator ] ) — 对比两个PHP 规范化的版本数字字串
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
echo '我的PHP版本很高: ' . PHP_VERSION . "\n";
}
int zend_thread_id ( void ) — 返回当前线程的唯一识别符
string zend_version ( void ) — 获取当前 Zend 引擎的版本
代码如下所示:
//被察者抽象类
class Observed implements SplSubject{
protected $_name;
protected $_observers;
//实例化,生成一个观察者对象
public function __construct(){
$this->_observers = new SplObjectStorage();
}
// 添加观察者对象
public function attach(SplObserver $observer){
$this->_observers->attach($observer);
}
//删除观者对象
public function detach(SplObserver $observer){
$this->_observers->detach($observer);
}
//通知消息
public function notify(){
foreach($this->_observers as $observer){
$observer->showMessage($this);
}
}
//普通方法: 设置值
public function setName($name){
$this->_name = $name;
$this->notify();
}
//普通方法: 获取值
public function getName(){
return $this->_name;
}
//普通方法:设置年龄
public function setAge($age){
$this->age = $age;
foreach($this->_observers as $observer){
$observer->showAge($this->_name,$this->age);
}
}
}
// 观察者抽象类
class Observer implements SplObserver{
//显示消息提示
public function showMessage(SplSubject $obj){
$user = $obj->getName();
if($user==='admin'){
echo '您好, ',$user,'欢迎您进入管理后台<br/>';
}else{
echo "你好, '$user' 你已经被添加到了用户列表<br/>";
}
}
//这是继承父类的抽象方法
public function update(SplSubject $subject) {}
//显示个人年龄
public function showAge($name,$age){
echo "<script>alert('$name 的年龄是: $age')</script>";
}
}
$subject = new Observed(); //生成一个被观察者对象
$observer = new Observer(); //生成一个观察者对象
$subject->attach($observer);//把观察者传入到被观察中去
$subject->setName('张三'); //调用 setName 方法
/*
* 通过面的 setName 就会调用 $this->notify();
* 通过调用 $this->notify()就会调用 $observer->showMessage($this)方法,
* 即每个观察者对象的 showMessage($obj)方法;
*/
$subject->setName('admin');
$subject->setAge(24);
<?php
// AudioExif.class.php
// 用PHP进行音频文件头部信息的读取与写入
// 目前只支持 WMA 和 MP3 两种格式, 只支持常用的几个头部信息
//
// 写入信息支持: Title(名称), Artist(艺术家), Copyright(版权), Description (描述)
// Year(年代), Genre (流派), AlbumTitle (专辑标题)
// 其中 mp3 和 wma 略有不同, 具体返回的信息还可能更多, 但只有以上信息可以被写入
// mp3 还支持 Track (曲目编号写入)
// 对于 MP3 文件支持 ID3v1也支持ID3v2, 读取时优先 v2, 写入时总是会写入v1, 必要时写入v2
//
// 用法说明: (由于 wma 使用 Unicode 存取, 故还需要 mb_convert_encoding() 扩展
// 返回数据及写入数据均为 ANSI 编码, 即存什么就显示什么 (中文_GB2312)
//
// require ('AudioExif.class.php');
// $AE = new AudioExif;
// $file = '/path/to/test.mp3';
//
// 1. 检查文件是否完整 (only for wma, mp3始终返回 true)
//
// $AE->CheckSize($file);
//
// 2. 读取信息, 返回值由信息组成的数组, 键名解释参见上方
//
// print_r($AE->GetInfo($file));
//
// 3. 写入信息, 第二参数是一个哈希数组, 键->值, 支持的参见上方的, mp3也支持 Track
// 要求第一参数的文件路径可由本程序写入
// $pa = array('Title' => '新标题', 'AlbumTitle' => '新的专辑名称');
// $AE->SetInfo($file, $pa);
//
//
// 其它: 该插件花了不少时间搜集查找 wma及mp3 的文件格式说明文档与网页, 希望对大家有用.
// 其实网上已经有不少类似的程序, 但对 wma 实在太少了, 只能在 win 平台下通过 M$ 的
// API 来操作, 而 MP3 也很少有可以在 unix/linux 命令行操作的, 所以特意写了这个模块
//
// 如果发现 bug 或提交 patch, 或加以改进使它更加健壮, 请告诉我.
// (关于 ID3和Wma的文件格式及结构 在网上应该都可以找到参考资料)
//
if (!extension_loaded('mbstring'))
{
trigger_error('PHP Extension module `mbstring` is required for AudioExif', E_USER_WARNING);
return true;
}
// the Main Class
class AudioExif
{
// public vars
var $_wma = false;
var $_mp3 = false;
// Construct
function AudioExif()
{
// nothing to do
}
// check the filesize
function CheckSize($file)
{
$handler = &$this->_get_handler($file);
if (!$handler) return false;
return $handler->check_size($file);
}
// get the infomations
function GetInfo($file)
{
$handler = &$this->_get_handler($file);
if (!$handler) return false;
return $handler->get_info($file);
}
// write the infomations
function SetInfo($file, $pa)
{
if (!is_writable($file))
{
trigger_error('AudioExif: file `' . $file . '` can not been overwritten', E_USER_WARNING);
return false;
}
$handler = &$this->_get_handler($file);
if (!$handler) return false;
return $handler->set_info($file, $pa);
}
// private methods
function &_get_handler($file)
{
$ext = strtolower(strrchr($file, '.'));
$ret = false;
if ($ext == '.mp3')
{ // MP3
$ret = &$this->_mp3;
if (!$ret) $ret = new _Mp3Exif();
}
else if ($ext == '.wma')
{ // wma
$ret = &$this->_wma;
if (!$ret) $ret = new _WmaExif();
}
else
{ // unknown
trigger_error('AudioExif not supported `' . $ext . '` file.', E_USER_WARNING);
}
return $ret;
}
}
// DBCS => gb2312
function dbcs_gbk($str)
{
// strip the last ""
$str = substr($str, 0, -2);
return mb_convert_encoding($str, 'GBK', 'UCS-2LE');
}
// gb2312 => DBCS
function gbk_dbcs($str)
{
$str = mb_convert_encoding($str, 'UCS-2LE', 'GBK');
$str .= "";
return $str;
}
// file exif
class _AudioExif
{
var $fd;
var $head;
var $head_off;
var $head_buf;
// init the file handler
function _file_init($fpath, $write = false)
{
$mode = ($write ? 'rb+' : 'rb');
$this->fd = @fopen($fpath, $mode);
if (!$this->fd)
{
trigger_error('AudioExif: `' . $fpath . '` can not be opened with mode `' . $mode . '`', E_USER_WARNING);
return false;
}
$this->head = false;
$this->head_off = 0;
$this->head_buf = '';
return true;
}
// read buffer from the head_buf & move the off pointer
function _read_head_buf($len)
{
if ($len <= 0) return NULL;
$buf = substr($this->head_buf, $this->head_off, $len);
$this->head_off += strlen($buf);
return $buf;
}
// read one short value
function _read_head_short()
{
$ord1 = ord(substr($this->head_buf, $this->head_off, 1));
$ord2 = ord(substr($this->head_buf, $this->head_off+1, 1));
$this->head_off += 2;
return ($ord1 + ($ord2<<8));
}
// save the file head
function _file_save($head, $olen, $nlen = 0)
{
if ($nlen == 0) $nlen = strlen($head);
if ($nlen == $olen)
{
// shorter
flock($this->fd, LOCK_EX);
fseek($this->fd, 0, SEEK_SET);
fwrite($this->fd, $head, $nlen);
flock($this->fd, LOCK_UN);
}
else
{
// longer, buffer required
$stat = fstat($this->fd);
$fsize = $stat['size'];
// buf required (4096?) 应该不会 nlen - olen > 4096 吧
$woff = 0;
$roff = $olen;
// read first buffer
flock($this->fd, LOCK_EX);
fseek($this->fd, $roff, SEEK_SET);
$buf = fread($this->fd, 4096);
// seek to start
fseek($this->fd, $woff, SEEK_SET);
fwrite($this->fd, $head, $nlen);
$woff += $nlen;
// seek to woff & write the data
do
{
$buf2 = $buf;
$roff += 4096;
if ($roff < $fsize)
{
fseek($this->fd, $roff, SEEK_SET);
$buf = fread($this->fd, 4096);
}
// save last buffer
$len2 = strlen($buf2);
fseek($this->fd, $woff, SEEK_SET);
fwrite($this->fd, $buf2, $len2);
$woff += $len2;
}
while ($roff < $fsize);
ftruncate($this->fd, $woff);
flock($this->fd, LOCK_UN);
}
}
// close the file
function _file_deinit()
{
if ($this->fd)
{
fclose($this->fd);
$this->fd = false;
}
}
}
// wma class
class _WmaExif extends _AudioExif
{
var $items1 = array('Title', 'Artist', 'Copyright', 'Description', 'Reserved');
var $items2 = array('Year', 'Genre', 'AlbumTitle');
// check file size (length) maybe invalid file
function check_size($file)
{
$ret = false;
if (!$this->_file_init($file)) return true;
if ($this->_init_header())
{
$buf = fread($this->fd, 24);
$tmp = unpack('H32id/Vlen/H8unused', $buf);
if ($tmp['id'] == '3626b2758e66cf11a6d900aa0062ce6c')
{
$stat = fstat($this->fd);
$ret = ($stat['size'] == ($this->head['len'] + $tmp['len']));
}
}
$this->_file_deinit();
return $ret;
}
// set info (save the infos)
function set_info($file, $pa)
{
// check the pa
settype($pa, 'array');
if (!$this->_file_init($file, true)) return false;
if (!$this->_init_header())
{
$this->_file_deinit();
return false;
}
// parse the old header & generate the new header
$head_body = '';
$st_found = $ex_found = false;
$head_num = $this->head['num'];
while (($tmp = $this->_get_head_frame()) && ($head_num > 0))
{
$head_num--;
if ($tmp['id'] == '3326b2758e66cf11a6d900aa0062ce6c')
{ // Standard Info
// 1-4
$st_found = true;
$st_body1 = $st_body2 = '';
$lenx = unpack('v5', $this->_read_head_buf(10));
$tmp['len'] -= 34; // 10 + 24
for ($i = 0; $i < count($this->items1); $i++)
{
$l = $lenx[$i+1];
$k = $this->items1[$i];
$tmp['len'] -= $l;
$data = $this->_read_head_buf($l);
if (isset($pa[$k])) $data = gbk_dbcs($pa[$k]);
$st_body2 .= $data;
$st_body1 .= pack('v', strlen($data));
}
// left length
if ($tmp['len'] > 0) $st_body2 .= $this->_read_head_buf($tmp['len']);
// save to head_body
$head_body .= pack('H32VH8', $tmp['id'], strlen($st_body1 . $st_body2)+24, $tmp['unused']);
$head_body .= $st_body1 . $st_body2;
}
else if ($tmp['id'] == '40a4d0d207e3d21197f000a0c95ea850')
{ // extended info
$ex_found = true;
$inum = $this->_read_head_short();
$inum2 = $inum;
$tmp['len'] -= 26; // 24 + 2
$et_body = '';
while ($tmp['len'] > 0 && $inum > 0)
{
// attribute name
$nlen = $this->_read_head_short();
$nbuf = $this->_read_head_buf($nlen);
// the flag & value length
$flag = $this->_read_head_short();
$vlen = $this->_read_head_short();
$vbuf = $this->_read_head_buf($vlen);
// set the length
$tmp['len'] -= (6 + $nlen + $vlen);
$inum--;
// save the data?
$name = dbcs_gbk($nbuf);
$k = substr($name, 3);
if (in_array($k, $this->items2) && isset($pa[$k]))
{
$vbuf = gbk_dbcs($pa[$k]);
$vlen = strlen($vbuf);
unset($pa[$k]);
}
$et_body .= pack('v', $nlen) . $nbuf . pack('vv', $flag, $vlen) . $vbuf;
}
// new tag insert??
foreach ($this->items2 as $k)
{
if (isset($pa[$k]))
{
$inum2++;
$nbuf = gbk_dbcs('WM/' . $k);
$nlen = strlen($nbuf);
$vbuf = gbk_dbcs($pa[$k]);
$vlen = strlen($vbuf);
$et_body .= pack('v', $nlen) . $nbuf . pack('vv', 0, $vlen) . $vbuf;
}
}
// left buf?
if ($tmp['len'] > 0) $et_body .= $this->_read_head_buf($tmp['len']);
// save to head_body
$head_body .= pack('H32VH8v', $tmp['id'], strlen($et_body)+26, $tmp['unused'], $inum2);
$head_body .= $et_body;
}
else
{
// just keep other head frame
$head_body .= pack('H32VH8', $tmp['id'], $tmp['len'], $tmp['unused']);
if ($tmp['len'] > 24) $head_body .= $this->_read_head_buf($tmp['len']-24);
}
}
// st not found?
if (!$st_found)
{
$st_body1 = $st_body2 = '';
foreach ($this->items1 as $k)
{
$data = (isset($pa[$k]) ? gbk_dbcs($pa[$k]) : "");
$st_body1 .= pack('v', strlen($data));
$st_body2 .= $data;
}
// save to head_body
$head_body .= pack('H32Va4', '3326b2758e66cf11a6d900aa0062ce6c', strlen($st_body1 . $st_body2)+24, '');
$head_body .= $st_body1 . $st_body2;
$this->head['num']++;
}
// ex not found?
if (!$ex_found)
{
$inum = 0;
$et_body = '';
foreach ($this->items2 as $k)
{
$nbuf = gbk_dbcs('WM/' . $k);
$vbuf = (isset($pa[$k]) ? gbk_dbcs($pa[$k]) : "");
$et_body .= pack('v', strlen($nbuf)) . $nbuf . pack('vv', 0, strlen($vbuf)) . $vbuf;
$inum++;
}
$head_body .= pack('H32Va4v', '40a4d0d207e3d21197f000a0c95ea850', strlen($et_body)+26, '', $inum);
$head_body .= $et_body;
$this->head['num']++;
}
// after save
$new_len = strlen($head_body) + 30;
$old_len = $this->head['len'];
if ($new_len < $old_len)
{
$head_body .= str_repeat("", $old_len - $new_len);
$new_len = $old_len;
}
$tmp = $this->head;
$head_buf = pack('H32VVVH4', $tmp['id'], $new_len, $tmp['len2'], $tmp['num'], $tmp['unused']);
$head_buf .= $head_body;
$this->_file_save($head_buf, $old_len, $new_len);
// close the file & return
$this->_file_deinit();
return true;
}
// get info
function get_info($file)
{
$ret = array();
if (!$this->_file_init($file)) return false;
if (!$this->_init_header())
{
$this->_file_deinit();
return false;
}
// get the data from head_buf
$head_num = $this->head['num']; // num of head_frame
while (($tmp = $this->_get_head_frame()) && $head_num > 0)
{
$head_num--;
if ($tmp['id'] == '3326b2758e66cf11a6d900aa0062ce6c')
{ // Standard Info
$lenx = unpack('v*', $this->_read_head_buf(10));
for ($i = 1; $i <= count($this->items1); $i++)
{
$k = $this->items1[$i-1];
$ret[$k] = dbcs_gbk($this->_read_head_buf($lenx[$i]));
}
}
else if ($tmp['id'] == '40a4d0d207e3d21197f000a0c95ea850')
{ // Extended Info
$inum = $this->_read_head_short();
$tmp['len'] -= 26;
while ($inum > 0 && $tmp['len'] > 0)
{
// attribute name
$nlen = $this->_read_head_short();
$nbuf = $this->_read_head_buf($nlen);
// the flag & value length
$flag = $this->_read_head_short();
$vlen = $this->_read_head_short();
$vbuf = $this->_read_head_buf($vlen);
// update the XX
$tmp['len'] -= (6 + $nlen + $vlen);
$inum--;
$name = dbcs_gbk($nbuf);
$k = substr($name, 3);
if (in_array($k, $this->items2))
{ // all is string value (refer to falg for other tags)
$ret[$k] = dbcs_gbk($vbuf);
}
}
}
else
{ // skip only
if ($tmp['len'] > 24) $this->head_off += ($tmp['len'] - 24);
}
}
$this->_file_deinit();
return $ret;
}
// get the header?
function _init_header()
{
fseek($this->fd, 0, SEEK_SET);
$buf = fread($this->fd, 30);
if (strlen($buf) != 30) return false;
$tmp = unpack('H32id/Vlen/Vlen2/Vnum/H4unused', $buf);
if ($tmp['id'] != '3026b2758e66cf11a6d900aa0062ce6c')
return false;
$this->head_buf = fread($this->fd, $tmp['len'] - 30);
$this->head = $tmp;
return true;
}
// _get_head_frame()
function _get_head_frame()
{
$buf = $this->_read_head_buf(24);
if (strlen($buf) != 24) return false;
$tmp = unpack('H32id/Vlen/H8unused', $buf);
return $tmp;
}
}
// mp3 class (if not IDv2 then select IDv1)
class _Mp3Exif extends _AudioExif
{
var $head1;
var $genres = array('Blues','Classic Rock','Country','Dance','Disco','Funk','Grunge','Hip-Hop','Jazz','Metal','New Age','Oldies','Other','Pop','R&B','Rap','Reggae','Rock','Techno','Industrial','Alternative','Ska','Death Metal','Pranks','Soundtrack','Euro-Techno','Ambient','Trip-Hop','Vocal','Jazz+Funk','Fusion','Trance','Classical','Instrumental','Acid','House','Game','Sound Clip','Gospel','Noise','AlternRock','Bass','Soul','Punk','Space','Meditative','Instrumental Pop','Instrumental Rock','Ethnic','Gothic','Darkwave','Techno-Industrial','Electronic','Pop-Folk','Eurodance','Dream','Southern Rock','Comedy','Cult','Gangsta','Top 40','Christian Rap','Pop/Funk','Jungle','Native American','Cabaret','New Wave','Psychadelic','Rave','Showtunes','Trailer','Lo-Fi','Tribal','Acid Punk','Acid Jazz','Polka','Retro','Musical','Rock & Roll','Hard Rock','Unknown');
// MP3 always return true
function check_size($file)
{
return true;
}
// get info
function get_info($file)
{
if (!$this->_file_init($file)) return false;
$ret = false;
if ($this->_init_header())
{
$ret = ($this->head ? $this->_get_v2_info() : $this->_get_v1_info());
$ret['meta'] = $this->_get_meta_info();
}
$this->_file_deinit();
return $ret;
}
// set info
function set_info($file, $pa)
{
if (!$this->_file_init($file, true)) return false;
if ($this->_init_header())
{
// always save v1 info
$this->_set_v1_info($pa);
// set v2 first if need
$this->_set_v2_info($pa);
}
$this->_file_deinit();
return true;
}
// get the header information[v1+v2], call after file_init
function _init_header()
{
$this->head1 = false;
$this->head = false;
// try to get ID3v1 first
fseek($this->fd, -128, SEEK_END);
$buf = fread($this->fd, 128);
if (strlen($buf) == 128 && substr($buf, 0, 3) == 'TAG')
{
$tmp = unpack('a3id/a30Title/a30Artist/a30AlbumTitle/a4Year/a28Description/CReserved/CTrack/CGenre', $buf);
$this->head1 = $tmp;
}
// try to get ID3v2
fseek($this->fd, 0, SEEK_SET);
$buf = fread($this->fd, 10);
if (strlen($buf) == 10 && substr($buf, 0, 3) == 'ID3')
{
$tmp = unpack('a3id/Cver/Crev/Cflag/C4size', $buf);
$tmp['size'] = ($tmp['size1']<<21)|($tmp['size2']<<14)|($tmp['size3']<<7)|$tmp['size4'];
unset($tmp['size1'], $tmp['size2'], $tmp['size3'], $tmp['size4']);
$this->head = $tmp;
$this->head_buf = fread($this->fd, $tmp['size']);
}
return ($this->head1 || $this->head);
}
// get v1 info
function _get_v1_info()
{
$ret = array();
$tmpa = array('Title', 'Artist', 'Copyright', 'Description', 'Year', 'AlbumTitle');
foreach ($tmpa as $tmp)
{
$ret[$tmp] = $this->head1[$tmp];
if ($pos = strpos($ret[$tmp], ""))
$ret[$tmp] = substr($ret[$tmp], 0, $pos);
}
// count the Genre, [Track]
if ($this->head1['Reserved'] == 0) $ret['Track'] = $this->head1['Track'];
else $ret['Description'] .= chr($ret['Reserved']) . chr($ret['Track']);
// Genre_idx
$g = $this->head1['Genre'];
if (!isset($this->genres[$g])) $ret['Genre'] = 'Unknown';
else $ret['Genre'] = $this->genres[$g];
// return the value
$ret['ID3v1'] = 'yes';
return $ret;
}
// get v2 info
function _get_v2_info()
{
$ret = array();
$items = array( 'TCOP'=>'Copyright', 'TPE1'=>'Artist', 'TIT2'=>'Title', 'TRCK'=> 'Track',
'TCON'=>'Genre', 'COMM'=>'Description', 'TYER'=>'Year', 'TALB'=>'AlbumTitle');
while (true)
{
$buf = $this->_read_head_buf(10);
if (strlen($buf) != 10) break;
$tmp = unpack('a4fid/Nsize/nflag', $buf);
if ($tmp['size'] == 0) break;
$tmp['dat'] = $this->_read_head_buf($tmp['size']);
// 0x6000 (11000000 00000000)
if ($tmp['flag'] & 0x6000) continue;
// mapping the data
if ($k = $items[$tmp['fid']])
{ // If first char is "", just skip
if (substr($tmp['dat'], 0, 1) == "") $tmp['dat'] = substr($tmp['dat'], 1);
$ret[$k] = $tmp['dat'];
}
}
// reset the genre
if ($g = $ret['Genre'])
{
if (substr($g,0,1) == '(' && substr($g,-1,1) == ')') $g = substr($g, 1, -1);
if (is_numeric($g))
{
$g = intval($g);
$ret['Genre'] = (isset($this->genres[$g]) ? $this->genres[$g] : 'Unknown');
}
}
$ret['ID3v1'] = 'no';
return $ret;
}
// get meta info of MP3
function _get_meta_info()
{
// seek to the lead buf: 0xff
$off = 0;
if ($this->head) $off = $this->head['size'] + 10;
fseek($this->fd, $off, SEEK_SET);
while (!feof($this->fd))
{
$skip = ord(fread($this->fd, 1));
if ($skip == 0xff) break;
}
if ($skip != 0xff) return false;
$buf = fread($this->fd, 3);
if (strlen($buf) != 3) return false;
$tmp = unpack('C3', $buf);
if (($tmp[1] & 0xf0) != 0xf0) return false;
// get the meta info
$meta = array();
// get mpeg version
$meta['mpeg'] = ($tmp[1] & 0x08 ? 1 : 2);
$meta['layer'] = ($tmp[1] & 0x04) ? (($tmp[1] & 0x02) ? 1 : 2) : (($tmp[1] & 0x02) ? 3 : 0);
$meta['epro'] = ($tmp[1] & 0x01) ? 'no' : 'yes';
// bit rates
$bit_rates = array(
1 => array(
1 => array(0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0),
2 => array(0,32,48,56,64,80,96,112,128,160,192,224,256,320,384,0),
3 => array(0,32,40,48,56,64,80,96,112,128,160,192,224,256,320,0)
),
2 => array(
1 => array(0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0),
2 => array(0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0),
3 => array(0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0)
)
);
$i = $meta['mpeg'];
$j = $meta['layer'];
$k = ($tmp[2]>>4);
$meta['bitrate'] = $bit_rates[$i][$j][$k];
// sample rates <采样率>
$sam_rates = array(1=>array(44100,48000,32000,0), 2=>array(22050,24000,16000,0));
$meta['samrate'] = $sam_rates[$i][$k];
$meta["padding"] = ($tmp[2] & 0x02) ? 'on' : 'off';
$meta["private"] = ($tmp[2] & 0x01) ? 'on' : 'off';
// mode & mode_ext
$k = ($tmp[3]>>6);
$channel_modes = array('stereo', 'joint stereo', 'dual channel', 'single channel');
$meta['mode'] = $channel_modes[$k];
$k = (($tmp[3]>>4) & 0x03);
$extend_modes = array('MPG_MD_LR_LR', 'MPG_MD_LR_I', 'MPG_MD_MS_LR', 'MPG_MD_MS_I');
$meta['ext_mode'] = $extend_modes[$k];
$meta['copyright'] = ($tmp[3] & 0x08) ? 'yes' : 'no';
$meta['original'] = ($tmp[3] & 0x04) ? 'yes' : 'no';
$emphasis = array('none', '50/15 microsecs', 'rreserved', 'CCITT J 17');
$k = ($tmp[3] & 0x03);
$meta['emphasis'] = $emphasis[$k];
return $meta;
}
// set v1 info
function _set_v1_info($pa)
{
// ID3v1 (simpled)
$off = -128;
if (!($tmp = $this->head1))
{
$off = 0;
$tmp['id'] = 'TAG';
$tmp['Title'] = $tmp['Artist'] = $tmp['AlbumTitle'] = $tmp['Year'] = $tmp['Description'] = '';
$tmp['Reserved'] = $tmp['Track'] = $tmp['Genre'] = 0;
}
// basic items
$items = array('Title', 'Artist', 'Copyright', 'Description', 'Year', 'AlbumTitle');
foreach ($items as $k)
{
if (isset($pa[$k])) $tmp[$k] = $pa[$k];
}
// genre index
if (isset($pa['Genre']))
{
$g = 0;
foreach ($this->genres as $gtmp)
{
if (!strcasecmp($gtmp, $pa['Genre']))
break;
$g++;
}
$tmp['Genre'] = $g;
}
if (isset($pa['Track'])) $tmp['Track'] = intval($pa['Track']);
// pack the data
$buf = pack('a3a30a30a30a4a28CCC', $tmp['id'], $tmp['Title'], $tmp['Artist'], $tmp['AlbumTitle'],
$tmp['Year'], $tmp['Description'], 0, $tmp['Track'], $tmp['Genre']);
flock($this->fd, LOCK_EX);
fseek($this->fd, $off, SEEK_END);
fwrite($this->fd, $buf, 128);
flock($this->fd, LOCK_UN);
}
// set v2 info
function _set_v2_info($pa)
{
if (!$this->head)
{ // insert ID3
return; // 没有就算了
/**
$tmp = array('id'=>'ID3','ver'=>3,'rev'=>0,'flag'=>0);
$tmp['size'] = -10; // +10 => 0
$this->head = $tmp;
$this->head_buf = '';
$this->head_off = 0;
**/
}
$items = array( 'TCOP'=>'Copyright', 'TPE1'=>'Artist', 'TIT2'=>'Title', 'TRAC'=>'Track',
'TCON'=>'Genre', 'COMM'=>'Description', 'TYER'=>'Year', 'TALB'=>'AlbumTitle');
$head_body = '';
while (true)
{
$buf = $this->_read_head_buf(10);
if (strlen($buf) != 10) break;
$tmp = unpack('a4fid/Nsize/nflag', $buf);
if ($tmp['size'] == 0) break;
$data = $this->_read_head_buf($tmp['size']);
if (($k = $items[$tmp['fid']]) && isset($pa[$k]))
{
// the data should prefix by "" [replace]
$data = "" . $pa[$k];
unset($pa[$k]);
}
$head_body .= pack('a4Nn', $tmp['fid'], strlen($data), $tmp['flag']) . $data;
}
// reverse the items & set the new tags
$items = array_flip($items);
foreach ($pa as $k => $v)
{
if ($fid = $items[$k])
{
$head_body .= pack('a4Nn', $fid, strlen($v) + 1, 0) . "" . $v;
}
}
// new length
$new_len = strlen($head_body) + 10;
$old_len = $this->head['size'] + 10;
if ($new_len < $old_len)
{
$head_body .= str_repeat("", $old_len - $new_len);
$new_len = $old_len;
}
// count the size1,2,3,4, no include the header
// 较为变态的算法... :p (28bytes integer)
$size = array();
$nlen = $new_len - 10;
for ($i = 4; $i > 0; $i--)
{
$size[$i] = ($nlen & 0x7f);
$nlen >>= 7;
}
$tmp = $this->head;
//echo "old_len : $old_len new_len: $new_len ";
$head_buf = pack('a3CCCCCCC', $tmp['id'], $tmp['ver'], $tmp['rev'], $tmp['flag'],
$size[1], $size[2], $size[3], $size[4]);
$head_buf .= $head_body;
$this->_file_save($head_buf, $old_len, $new_len);
}
}
?>