run php cs fix

This commit is contained in:
Sebijk 2022-02-21 21:04:55 +01:00
parent 6a8c9a281f
commit cb7b5bdadc
4 changed files with 1496 additions and 1473 deletions

View file

@ -19,420 +19,426 @@
*
*/
if(!defined('B1GMAIL_INIT'))
die('Directly calling this file is not supported');
if (!defined('B1GMAIL_INIT')) {
die('Directly calling this file is not supported');
}
if(!class_exists('BMHTTP'))
include(B1GMAIL_DIR . 'serverlib/http.class.php');
if (!class_exists('BMHTTP')) {
include B1GMAIL_DIR.'serverlib/http.class.php';
}
/**
* sms class
* sms class.
*/
class BMSMS
{
var $_userID;
var $_userObject;
private $_userID;
private $_userObject;
/**
* constructor
*
* @param int $userID User ID
* @param BMUser $userObject User object
* @return BMSMS
*/
function __construct($userID, &$userObject)
{
$this->_userID = (int)$userID;
$this->_userObject = &$userObject;
}
/**
* constructor.
*
* @param int $userID User ID
* @param BMUser $userObject User object
*
* @return BMSMS
*/
public function __construct($userID, &$userObject)
{
$this->_userID = (int) $userID;
$this->_userObject = &$userObject;
}
/**
* get outbox
*
* @return array
*/
function GetOutbox($sortColumn = 'id', $sortOrder = 'DESC')
{
global $db;
/**
* get outbox.
*
* @return array
*/
public function GetOutbox($sortColumn = 'id', $sortOrder = 'DESC')
{
global $db;
$result = array();
$res = $db->Query('SELECT id,`from`,`to`,`text`,price,`date` FROM {pre}smsend WHERE isSMS=1 AND user=? AND deleted=0 ORDER BY `' . $sortColumn . '` ' . $sortOrder . ' LIMIT 15',
$this->_userID);
while($row = $res->FetchArray(MYSQLI_ASSOC))
$result[$row['id']] = $row;
$res->Free();
$result = [];
$res = $db->Query('SELECT id,`from`,`to`,`text`,price,`date` FROM {pre}smsend WHERE isSMS=1 AND user=? AND deleted=0 ORDER BY `'.$sortColumn.'` '.$sortOrder.' LIMIT 15',
$this->_userID);
while ($row = $res->FetchArray(MYSQLI_ASSOC)) {
$result[$row['id']] = $row;
}
$res->Free();
return($result);
}
return $result;
}
/**
* delete sms outbox entry
*
* @param int $id ID
*/
function DeleteOutboxEntry($id)
{
global $db;
/**
* delete sms outbox entry.
*
* @param int $id ID
*/
public function DeleteOutboxEntry($id)
{
global $db;
$db->Query('UPDATE {pre}smsend SET `deleted`=1,`from`=?,`to`=?,`text`=? WHERE id=? AND user=?',
'', '', '',
$id, $this->_userID);
return($db->AffectedRows() == 1);
}
$db->Query('UPDATE {pre}smsend SET `deleted`=1,`from`=?,`to`=?,`text`=? WHERE id=? AND user=?',
'', '', '',
$id, $this->_userID);
/**
* check if $no conforms $pre-list
*
* @param string $no
* @param string $pre
* @return bool
*/
function PreOK($no, $pre)
{
if(trim($pre) != '')
{
$ok = false;
$entries = explode(':', $pre);
foreach($entries as $entry)
{
$entry = str_replace('+', '00', preg_replace('/[^0-9]/', '', $entry));
if(substr($no, 0, strlen($entry)) == $entry)
{
$ok = true;
break;
}
}
}
else
$ok = true;
return $db->AffectedRows() == 1;
}
return($ok);
}
/**
* check if $no conforms $pre-list.
*
* @param string $no
* @param string $pre
*
* @return bool
*/
public function PreOK($no, $pre)
{
if (trim($pre) != '') {
$ok = false;
$entries = explode(':', $pre);
foreach ($entries as $entry) {
$entry = str_replace('+', '00', preg_replace('/[^0-9]/', '', $entry));
if (substr($no, 0, strlen($entry)) == $entry) {
$ok = true;
break;
}
}
} else {
$ok = true;
}
/**
* get available SMS types
*
* @return array
*/
function GetTypes()
{
global $db;
return $ok;
}
if(is_object($this->_userObject))
{
$group = $this->_userObject->GetGroup();
$groupRow = $group->Fetch();
}
else
$groupRow = array('sms_sig' => '');
/**
* get available SMS types.
*
* @return array
*/
public function GetTypes()
{
global $db;
$result = array();
$res = $db->Query('SELECT id,titel,typ,std,price,gateway,flags,maxlength FROM {pre}smstypen ORDER BY titel ASC');
while($row = $res->FetchArray(MYSQLI_ASSOC))
$result[$row['id']] = array(
'id' => $row['id'],
'title' => $row['titel'],
'type' => $row['typ'],
'default' => $row['std'] == 1,
'price' => $row['price'],
'gateway' => $row['gateway'],
'flags' => $row['flags'],
'maxlength' => $row['maxlength'] - strlen($groupRow['sms_sig'])
);
$res->Free();
if (is_object($this->_userObject)) {
$group = $this->_userObject->GetGroup();
$groupRow = $group->Fetch();
} else {
$groupRow = ['sms_sig' => ''];
}
return($result);
}
$result = [];
$res = $db->Query('SELECT id,titel,typ,std,price,gateway,flags,maxlength FROM {pre}smstypen ORDER BY titel ASC');
while ($row = $res->FetchArray(MYSQLI_ASSOC)) {
$result[$row['id']] = [
'id' => $row['id'],
'title' => $row['titel'],
'type' => $row['typ'],
'default' => $row['std'] == 1,
'price' => $row['price'],
'gateway' => $row['gateway'],
'flags' => $row['flags'],
'maxlength' => $row['maxlength'] - strlen($groupRow['sms_sig']),
];
}
$res->Free();
/**
* get max SMS chars for user
*
* @return int
*/
function GetMaxChars($typeID = 0)
{
global $db;
return $result;
}
if(is_object($this->_userObject))
{
$group = $this->_userObject->GetGroup();
$groupRow = $group->Fetch();
}
else
$groupRow = array('sms_sig' => '');
/**
* get max SMS chars for user.
*
* @return int
*/
public function GetMaxChars($typeID = 0)
{
global $db;
$maxChars = 160;
if (is_object($this->_userObject)) {
$group = $this->_userObject->GetGroup();
$groupRow = $group->Fetch();
} else {
$groupRow = ['sms_sig' => ''];
}
if($typeID > 0)
{
$res = $db->Query('SELECT maxlength FROM {pre}smstypen WHERE id=?', $typeID);
while($row = $res->FetchArray(MYSQLI_ASSOC))
$maxChars = $row['maxlength'];
$res->Free();
}
$maxChars = 160;
return($maxChars - strlen($groupRow['sms_sig']));
}
if ($typeID > 0) {
$res = $db->Query('SELECT maxlength FROM {pre}smstypen WHERE id=?', $typeID);
while ($row = $res->FetchArray(MYSQLI_ASSOC)) {
$maxChars = $row['maxlength'];
}
$res->Free();
}
/**
* get default gateway or gateway specified by ID
*
* @param int $id ID (0 = default gateway)
* @return array
*/
function GetGateway($id = 0)
{
global $bm_prefs, $db;
return $maxChars - strlen($groupRow['sms_sig']);
}
if($id == 0)
$id = $bm_prefs['sms_gateway'];
/**
* get default gateway or gateway specified by ID.
*
* @param int $id ID (0 = default gateway)
*
* @return array
*/
public function GetGateway($id = 0)
{
global $bm_prefs, $db;
$res = $db->Query('SELECT id,titel,getstring,success,`user`,`pass` FROM {pre}smsgateways WHERE id=?',
$id);
if($res->RowCount() == 1)
{
$row = $res->FetchArray(MYSQLI_ASSOC);
$res->Free();
if ($id == 0) {
$id = $bm_prefs['sms_gateway'];
}
return(array(
'id' => $row['id'],
'title' => $row['titel'],
'getstring' => $row['getstring'],
'success' => $row['success'],
'user' => $row['user'],
'pass' => $row['pass']
));
}
return(false);
}
$res = $db->Query('SELECT id,titel,getstring,success,`user`,`pass` FROM {pre}smsgateways WHERE id=?',
$id);
if ($res->RowCount() == 1) {
$row = $res->FetchArray(MYSQLI_ASSOC);
$res->Free();
/**
* send SMS
*
* @param string $from From
* @param string $to To
* @param string $text Text
* @param int $type Type ID (0 = default type)
* @param bool $charge Charge user account?
* @param bool $putToOutbox Put SMS to outbox?
* @param int $outboxID ID of outbox entry, if already available
* @return bool
*/
function Send($from, $to, $text, $type = 0, $charge = true, $putToOutbox = true, $outboxID = 0)
{
global $bm_prefs, $db, $lang_user;
return [
'id' => $row['id'],
'title' => $row['titel'],
'getstring' => $row['getstring'],
'success' => $row['success'],
'user' => $row['user'],
'pass' => $row['pass'],
];
}
// module handler
ModuleFunction('OnSendSMS', array(&$text, &$type, &$from, &$to, &$this->_userObject));
return false;
}
// get type and type list
$types = $this->GetTypes();
if($type == 0)
{
foreach($types as $typeID=>$typeInfo)
if($typeInfo['default'])
$type = $typeID;
if($type == 0)
{
PutLog(sprintf('Default SMS type <%d> not found while trying to send SMS from <%s> to <%s> (userID: %d)',
$type,
$from,
$to,
$this->_userID),
PRIO_WARNING,
__FILE__,
__LINE__);
return(false);
}
}
if(isset($types[$type]))
$type = $types[$type];
else
{
PutLog(sprintf('SMS type <%d> not found while trying to send SMS from <%s> to <%s> (userID: %d)',
$type,
$from,
$to,
$this->_userID),
PRIO_WARNING,
__FILE__,
__LINE__);
return(false);
}
/**
* send SMS.
*
* @param string $from From
* @param string $to To
* @param string $text Text
* @param int $type Type ID (0 = default type)
* @param bool $charge Charge user account?
* @param bool $putToOutbox Put SMS to outbox?
* @param int $outboxID ID of outbox entry, if already available
*
* @return bool
*/
public function Send($from, $to, $text, $type = 0, $charge = true, $putToOutbox = true, $outboxID = 0)
{
global $bm_prefs, $db, $lang_user;
// crop text
if(_strlen($text) > $this->GetMaxChars($type['id']))
$text = _substr($text, 0, $this->GetMaxChars($type['id']));
// module handler
ModuleFunction('OnSendSMS', [&$text, &$type, &$from, &$to, &$this->_userObject]);
// check account balance
if($charge)
{
$balance = $this->_userObject->GetBalance();
if($balance < $type['price'])
{
PutLog(sprintf('Failed to send SMS from <%s> to <%s>: Not enough credits (userID: %d)',
$from,
$to,
$this->_userID),
PRIO_NOTE,
__FILE__,
__LINE__);
return(false);
}
}
// get type and type list
$types = $this->GetTypes();
if ($type == 0) {
foreach ($types as $typeID => $typeInfo) {
if ($typeInfo['default']) {
$type = $typeID;
}
}
if ($type == 0) {
PutLog(sprintf('Default SMS type <%d> not found while trying to send SMS from <%s> to <%s> (userID: %d)',
$type,
$from,
$to,
$this->_userID),
PRIO_WARNING,
__FILE__,
__LINE__);
// get gateway info
$gateway = $this->GetGateway($type['gateway']);
if(!$gateway)
{
PutLog(sprintf('Gateway <%d> not found while trying to send SMS with type <%d> from <%s> to <%s> (userID: %d)',
$type['gateway'],
$type['id'],
$from,
$to,
$this->_userID),
PRIO_WARNING,
__FILE__,
__LINE__);
return(false);
}
return false;
}
}
if (isset($types[$type])) {
$type = $types[$type];
} else {
PutLog(sprintf('SMS type <%d> not found while trying to send SMS from <%s> to <%s> (userID: %d)',
$type,
$from,
$to,
$this->_userID),
PRIO_WARNING,
__FILE__,
__LINE__);
// prepare formatted numbers
$fromPlus = preg_replace('/^00/', '+', $from);
$toPlus = preg_replace('/^00/', '+', $to);
return false;
}
// build GET string
$getString = $gateway['getstring'];
$getString = str_replace('%%user%%',
_urlencode($gateway['user']),
$getString);
$getString = str_replace('%%passwort%%',
_urlencode($gateway['pass']),
$getString);
$getString = str_replace('%%from%%',
urlencode(CharsetDecode($from, false, 'ISO-8859-1')),
$getString);
$getString = str_replace('%%fromPlus%%',
urlencode(CharsetDecode($fromPlus, false, 'ISO-8859-1')),
$getString);
$getString = str_replace('%%from_utf8%%',
urlencode(CharsetDecode($from, false, 'UTF-8')),
$getString);
$getString = str_replace('%%to%%',
_urlencode($to),
$getString);
$getString = str_replace('%%toPlus%%',
_urlencode($toPlus),
$getString);
$getString = str_replace('%%msg%%',
urlencode(CharsetDecode($text, false, 'ISO-8859-1')),
$getString);
$getString = str_replace('%%msg_utf8%%',
urlencode(CharsetDecode($text, false, 'UTF-8')),
$getString);
if($this->_userObject)
{
$getString = str_replace('%%usermail%%',
_urlencode($this->_userObject->_row['email']),
$getString);
$getString = str_replace('%%userid%%',
_urlencode($this->_userObject->_id),
$getString);
}
$getString = str_replace('%%typ%%',
_urlencode($type['type']),
$getString);
// crop text
if (_strlen($text) > $this->GetMaxChars($type['id'])) {
$text = _substr($text, 0, $this->GetMaxChars($type['id']));
}
// request!
$http = _new('BMHTTP', array($getString));
$result = $http->DownloadToString();
$success = (trim($gateway['success']) == '' && trim($result) == '')
|| (strpos(strtolower($result), strtolower($gateway['success'])) !== false);
// check account balance
if ($charge) {
$balance = $this->_userObject->GetBalance();
if ($balance < $type['price']) {
PutLog(sprintf('Failed to send SMS from <%s> to <%s>: Not enough credits (userID: %d)',
$from,
$to,
$this->_userID),
PRIO_NOTE,
__FILE__,
__LINE__);
// log
PutLog(sprintf('SMS success: %d; expected result: <%s>; gateway result: <%s> (userID: %d)',
$success,
$gateway['success'],
$result,
$this->_userID),
PRIO_DEBUG,
__FILE__,
__LINE__);
return false;
}
}
// ok?
if($success)
{
// status ID?
if(preg_match('/Status\:([0-9]*)/', $result, $reg) && is_array($reg) && isset($reg[1]))
$statusID = $reg[1];
else
$statusID = -1;
// get gateway info
$gateway = $this->GetGateway($type['gateway']);
if (!$gateway) {
PutLog(sprintf('Gateway <%d> not found while trying to send SMS with type <%d> from <%s> to <%s> (userID: %d)',
$type['gateway'],
$type['id'],
$from,
$to,
$this->_userID),
PRIO_WARNING,
__FILE__,
__LINE__);
// stats
Add2Stat('sms');
return false;
}
// charge, if requested
if($charge)
$outboxID = $this->_userObject->Debit($type['price']*-1, $lang_user['tx_sms']);
// prepare formatted numbers
$fromPlus = preg_replace('/^00/', '+', $from);
$toPlus = preg_replace('/^00/', '+', $to);
// put to outbox?
if($putToOutbox)
if($outboxID == 0)
{
$db->Query('INSERT INTO {pre}smsend(user,monat,price,isSMS,`from`,`to`,`text`,statusid,`date`) VALUES(?,?,?,?,?,?,?,?,?)',
$this->_userID,
(int)date('mY'),
0,
1,
$from,
$to,
$text,
$statusID,
time());
$outboxID = $db->InsertId();
}
else
$db->Query('UPDATE {pre}smsend SET isSMS=?,`from`=?,`to`=?,`text`=?,statusid=?,`date`=? WHERE id=? AND user=?',
1,
$from,
$to,
$text,
$statusID,
time(),
$outboxID,
$this->_userID);
// build GET string
$getString = $gateway['getstring'];
$getString = str_replace('%%user%%',
_urlencode($gateway['user']),
$getString);
$getString = str_replace('%%passwort%%',
_urlencode($gateway['pass']),
$getString);
$getString = str_replace('%%from%%',
urlencode(CharsetDecode($from, false, 'ISO-8859-1')),
$getString);
$getString = str_replace('%%fromPlus%%',
urlencode(CharsetDecode($fromPlus, false, 'ISO-8859-1')),
$getString);
$getString = str_replace('%%from_utf8%%',
urlencode(CharsetDecode($from, false, 'UTF-8')),
$getString);
$getString = str_replace('%%to%%',
_urlencode($to),
$getString);
$getString = str_replace('%%toPlus%%',
_urlencode($toPlus),
$getString);
$getString = str_replace('%%msg%%',
urlencode(CharsetDecode($text, false, 'ISO-8859-1')),
$getString);
$getString = str_replace('%%msg_utf8%%',
urlencode(CharsetDecode($text, false, 'UTF-8')),
$getString);
if ($this->_userObject) {
$getString = str_replace('%%usermail%%',
_urlencode($this->_userObject->_row['email']),
$getString);
$getString = str_replace('%%userid%%',
_urlencode($this->_userObject->_id),
$getString);
}
$getString = str_replace('%%typ%%',
_urlencode($type['type']),
$getString);
// module handler
ModuleFunction('AfterSendSMS', array(true, $result, $outboxID));
// request!
$http = _new('BMHTTP', [$getString]);
$result = $http->DownloadToString();
$success = (trim($gateway['success']) == '' && trim($result) == '')
|| (strpos(strtolower($result), strtolower($gateway['success'])) !== false);
// log
PutLog(sprintf('Sent SMS from <%s> to <%s> (type: %d; statusID: %d; charged: %d; userID: %d)',
$from,
$to,
$type['id'],
$statusID,
$charge ? $type['price'] : 0,
$this->_userID),
PRIO_NOTE,
__FILE__,
__LINE__);
return(true);
}
else
{
// module handler
ModuleFunction('AfterSendSMS', array(false, $result, $outboxID));
// log
PutLog(sprintf('SMS success: %d; expected result: <%s>; gateway result: <%s> (userID: %d)',
$success,
$gateway['success'],
$result,
$this->_userID),
PRIO_DEBUG,
__FILE__,
__LINE__);
// log
PutLog(sprintf('Failed to send SMS from <%s> to <%s> (type: %d; charged: 0; userID: %d)',
$from,
$to,
$type['id'],
$this->_userID),
PRIO_NOTE,
__FILE__,
__LINE__);
return(false);
}
}
// ok?
if ($success) {
// status ID?
if (preg_match('/Status\:([0-9]*)/', $result, $reg) && is_array($reg) && isset($reg[1])) {
$statusID = $reg[1];
} else {
$statusID = -1;
}
// stats
Add2Stat('sms');
// charge, if requested
if ($charge) {
$outboxID = $this->_userObject->Debit($type['price'] * -1, $lang_user['tx_sms']);
}
// put to outbox?
if ($putToOutbox) {
if ($outboxID == 0) {
$db->Query('INSERT INTO {pre}smsend(user,monat,price,isSMS,`from`,`to`,`text`,statusid,`date`) VALUES(?,?,?,?,?,?,?,?,?)',
$this->_userID,
(int) date('mY'),
0,
1,
$from,
$to,
$text,
$statusID,
time());
$outboxID = $db->InsertId();
} else {
$db->Query('UPDATE {pre}smsend SET isSMS=?,`from`=?,`to`=?,`text`=?,statusid=?,`date`=? WHERE id=? AND user=?',
1,
$from,
$to,
$text,
$statusID,
time(),
$outboxID,
$this->_userID);
}
}
// module handler
ModuleFunction('AfterSendSMS', [true, $result, $outboxID]);
// log
PutLog(sprintf('Sent SMS from <%s> to <%s> (type: %d; statusID: %d; charged: %d; userID: %d)',
$from,
$to,
$type['id'],
$statusID,
$charge ? $type['price'] : 0,
$this->_userID),
PRIO_NOTE,
__FILE__,
__LINE__);
return true;
} else {
// module handler
ModuleFunction('AfterSendSMS', [false, $result, $outboxID]);
// log
PutLog(sprintf('Failed to send SMS from <%s> to <%s> (type: %d; charged: 0; userID: %d)',
$from,
$to,
$type['id'],
$this->_userID),
PRIO_NOTE,
__FILE__,
__LINE__);
return false;
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -19,240 +19,243 @@
*
*/
if(!defined('B1GMAIL_INIT'))
die('Directly calling this file is not supported');
if (!defined('B1GMAIL_INIT')) {
die('Directly calling this file is not supported');
}
/**
* Basic UnZIP class
*
* Basic UnZIP class.
*/
class BMUnZIP
{
var $_fp;
var $_centralDirStruct;
private $_fp;
private $_centralDirStruct;
/**
* constructor
*
* @param resource $fp Input file stream
* @return BMUnZIP
*/
function __construct($fp)
{
$this->_fp = $fp;
$this->_readCentralDirStruct();
}
/**
* constructor.
*
* @param resource $fp Input file stream
*
* @return BMUnZIP
*/
public function __construct($fp)
{
$this->_fp = $fp;
$this->_readCentralDirStruct();
}
/**
* get ZIP file directory listing
*
* @return array
*/
function GetFileList()
{
return($this->_centralDirStruct);
}
/**
* get ZIP file directory listing.
*
* @return array
*/
public function GetFileList()
{
return $this->_centralDirStruct;
}
/**
* get ZIP file directory listing prepared for tree display
*
* @return array
*/
function GetFileTree()
{
$tree = array();
$folderNoCounter = count($this->_centralDirStruct);
/**
* get ZIP file directory listing prepared for tree display.
*
* @return array
*/
public function GetFileTree()
{
$tree = [];
$folderNoCounter = count($this->_centralDirStruct);
// add files
foreach($this->_centralDirStruct as $fileNo=>$file)
{
$file['parentID'] = -2;
$file['type'] = 'file';
$file['fileNo'] = $fileNo;
$file['baseName'] = basename($file['fileName']);
// add files
foreach ($this->_centralDirStruct as $fileNo => $file) {
$file['parentID'] = -2;
$file['type'] = 'file';
$file['fileNo'] = $fileNo;
$file['baseName'] = basename($file['fileName']);
$tree[] = $file;
}
$tree[] = $file;
}
// add folders
$again = true;
while($again)
{
$again = false;
// add folders
$again = true;
while ($again) {
$again = false;
foreach($tree as $id=>$item)
{
if($item['parentID'] != -2)
continue;
foreach ($tree as $id => $item) {
if ($item['parentID'] != -2) {
continue;
}
$parentFolderName = dirname($item['fileName']);
$parentFolderName = dirname($item['fileName']);
if($parentFolderName != '.' && $parentFolderName != '')
{
$parentID = -2;
if ($parentFolderName != '.' && $parentFolderName != '') {
$parentID = -2;
foreach($tree as $id2=>$item2)
{
if($item2['type'] == 'folder' && $item2['fileName'] == $parentFolderName)
{
$parentID = $item2['fileNo'];
break;
}
}
foreach ($tree as $id2 => $item2) {
if ($item2['type'] == 'folder' && $item2['fileName'] == $parentFolderName) {
$parentID = $item2['fileNo'];
break;
}
}
if($parentID == -2)
{
$folderNo = $folderNoCounter++;
$tree[] = array('type' => 'folder',
'fileName' => $parentFolderName,
'baseName' => basename($parentFolderName),
'fileNo' => $folderNo,
'parentID' => -2);
$parentID = $folderNo;
$again = true;
}
if ($parentID == -2) {
$folderNo = $folderNoCounter++;
$tree[] = ['type' => 'folder',
'fileName' => $parentFolderName,
'baseName' => basename($parentFolderName),
'fileNo' => $folderNo,
'parentID' => -2, ];
$parentID = $folderNo;
$again = true;
}
$tree[$id]['parentID'] = $parentID;
}
}
}
$tree[$id]['parentID'] = $parentID;
}
}
}
return($tree);
}
return $tree;
}
/**
* extract a file by file no
*
* @param int $fileNo File no (from GetFileList() array)
* @param resource $fp Output file stream
* @return bool
*/
function ExtractFile($fileNo, $fp = false, $sizeLimit = -1)
{
if(!isset($this->_centralDirStruct[$fileNo]))
return(false);
/**
* extract a file by file no.
*
* @param int $fileNo File no (from GetFileList() array)
* @param resource $fp Output file stream
*
* @return bool
*/
public function ExtractFile($fileNo, $fp = false, $sizeLimit = -1)
{
if (!isset($this->_centralDirStruct[$fileNo])) {
return false;
}
if(!in_array($this->_centralDirStruct[$fileNo]['compressionMethod'], array(8, 12)))
return(false);
if (!in_array($this->_centralDirStruct[$fileNo]['compressionMethod'], [8, 12])) {
return false;
}
fseek($this->_fp, $this->_centralDirStruct[$fileNo]['relativeOffset'], SEEK_SET);
fseek($this->_fp, $this->_centralDirStruct[$fileNo]['relativeOffset'], SEEK_SET);
$fileHeader = fread($this->_fp, 30);
$fileHeader = fread($this->_fp, 30);
$_fileHeader = @unpack('Vsignature/vversionNeeded/vflags/vcompressionMethod/vmTime/vmDate/Vcrc32/VcompressedSize/VuncompressedSize/vfileNameLength/vextraFieldLength',
$fileHeader);
$_fileHeader = @unpack('Vsignature/vversionNeeded/vflags/vcompressionMethod/vmTime/vmDate/Vcrc32/VcompressedSize/VuncompressedSize/vfileNameLength/vextraFieldLength',
$fileHeader);
if(!$_fileHeader || $_fileHeader['signature'] != 0x04034b50)
return(false);
if (!$_fileHeader || $_fileHeader['signature'] != 0x04034b50) {
return false;
}
fseek($this->_fp, $_fileHeader['fileNameLength']+$_fileHeader['extraFieldLength'], SEEK_CUR);
fseek($this->_fp, $_fileHeader['fileNameLength'] + $_fileHeader['extraFieldLength'], SEEK_CUR);
$_fileHeader = $this->_centralDirStruct[$fileNo];
$_fileHeader = $this->_centralDirStruct[$fileNo];
if($_fileHeader['compressedSize'] > 0)
{
$compressedData = fread($this->_fp, $_fileHeader['compressedSize']);
$uncompressedData = '';
if ($_fileHeader['compressedSize'] > 0) {
$compressedData = fread($this->_fp, $_fileHeader['compressedSize']);
$uncompressedData = '';
if($_fileHeader['compressionMethod'] == 8)
{
$uncompressedData = @gzinflate($compressedData);
}
else if($_fileHeader['compressionMethod'] == 12)
{
$uncompressedData = @bzdecompress($compressedData);
}
if ($_fileHeader['compressionMethod'] == 8) {
$uncompressedData = @gzinflate($compressedData);
} elseif ($_fileHeader['compressionMethod'] == 12) {
$uncompressedData = @bzdecompress($compressedData);
}
unset($compressedData);
unset($compressedData);
if(crc32($uncompressedData) != $_fileHeader['crc32'])
return(false);
if (crc32($uncompressedData) != $_fileHeader['crc32']) {
return false;
}
if($fp !== false)
fwrite($fp, $uncompressedData, $sizeLimit != -1 ? $sizeLimit : strlen($uncompressedData));
else
echo($uncompressedData);
}
else if($_fileHeader['crc32'] != 0)
{
return(false);
}
if ($fp !== false) {
fwrite($fp, $uncompressedData, $sizeLimit != -1 ? $sizeLimit : strlen($uncompressedData));
} else {
echo $uncompressedData;
}
} elseif ($_fileHeader['crc32'] != 0) {
return false;
}
return(true);
}
return true;
}
/**
* read central dir struct from ZIP file
*
*/
function _readCentralDirStruct()
{
$this->_centralDirStruct = array();
/**
* read central dir struct from ZIP file.
*/
private function _readCentralDirStruct()
{
$this->_centralDirStruct = [];
fseek($this->_fp, -22, SEEK_END);
$endOfCDS = fread($this->_fp, 22);
fseek($this->_fp, -22, SEEK_END);
$endOfCDS = fread($this->_fp, 22);
while(substr($endOfCDS, 0, 4) != pack('V', 0x06054b50))
{
fseek($this->_fp, -1, SEEK_CUR);
$endOfCDS = fgetc($this->_fp) . $endOfCDS;
fseek($this->_fp, -1, SEEK_CUR);
while (substr($endOfCDS, 0, 4) != pack('V', 0x06054b50)) {
fseek($this->_fp, -1, SEEK_CUR);
$endOfCDS = fgetc($this->_fp).$endOfCDS;
fseek($this->_fp, -1, SEEK_CUR);
if(ftell($this->_fp) < 2)
break;
}
if (ftell($this->_fp) < 2) {
break;
}
}
if(substr($endOfCDS, 0, 4) != pack('V', 0x06054b50))
{
if(DEBUG) trigger_error('File corrupt or not in ZIP format', E_USER_NOTICE);
return;
}
if (substr($endOfCDS, 0, 4) != pack('V', 0x06054b50)) {
if (DEBUG) {
trigger_error('File corrupt or not in ZIP format', E_USER_NOTICE);
}
// parse endOfCDS record
$_endOfCDS = @unpack('Vsignature/vdiskNo/vcdsStartDiskNo/vcdsDiskEntryCount/vcdsTotalEntryCount/VcdsSize/VcdsOffset/vcommentLength', $endOfCDS);
if(!$_endOfCDS)
{
if(DEBUG) trigger_error('File corrupt or not in ZIP format (eoCDS broken)', E_USER_NOTICE);
return;
}
return;
}
// seek to CDS offset
fseek($this->_fp, $_endOfCDS['cdsOffset'], SEEK_SET);
// parse endOfCDS record
$_endOfCDS = @unpack('Vsignature/vdiskNo/vcdsStartDiskNo/vcdsDiskEntryCount/vcdsTotalEntryCount/VcdsSize/VcdsOffset/vcommentLength', $endOfCDS);
if (!$_endOfCDS) {
if (DEBUG) {
trigger_error('File corrupt or not in ZIP format (eoCDS broken)', E_USER_NOTICE);
}
// read CDS entries
for($i=0; $i<$_endOfCDS['cdsDiskEntryCount'] && !feof($this->_fp); $i++)
{
$fileHeader = fread($this->_fp, 46);
return;
}
$_fileHeader = @unpack('Vsignature/vversion/vversionNeeded/vflags/vcompressionMethod/vmTime/vmDate/Vcrc32/VcompressedSize/VuncompressedSize/vfileNameLength/vextraFieldLength/vfileCommentLength/vdiskNumberStart/vinternalAttrs/VexternalAttrs/VrelativeOffset',
$fileHeader);
if(!$_fileHeader || $_fileHeader['signature'] != 0x02014b50)
{
if(DEBUG) trigger_error('File corrupt (CDS broken)');
return;
}
// seek to CDS offset
fseek($this->_fp, $_endOfCDS['cdsOffset'], SEEK_SET);
$_fileHeader['cdsOffset'] = ftell($this->_fp) - strlen($fileHeader);
// read CDS entries
for ($i = 0; $i < $_endOfCDS['cdsDiskEntryCount'] && !feof($this->_fp); ++$i) {
$fileHeader = fread($this->_fp, 46);
if($_fileHeader['fileNameLength'] > 0)
$_fileHeader['fileName'] = fread($this->_fp, $_fileHeader['fileNameLength']);
if($_fileHeader['extraFieldLength'] > 0)
$_fileHeader['extraField'] = fread($this->_fp, $_fileHeader['extraFieldLength']);
if($_fileHeader['fileCommentLength'] > 0)
$_fileHeader['fileComment'] = fread($this->_fp, $_fileHeader['fileCommentLength']);
$_fileHeader = @unpack('Vsignature/vversion/vversionNeeded/vflags/vcompressionMethod/vmTime/vmDate/Vcrc32/VcompressedSize/VuncompressedSize/vfileNameLength/vextraFieldLength/vfileCommentLength/vdiskNumberStart/vinternalAttrs/VexternalAttrs/VrelativeOffset',
$fileHeader);
if (!$_fileHeader || $_fileHeader['signature'] != 0x02014b50) {
if (DEBUG) {
trigger_error('File corrupt (CDS broken)');
}
if(substr($_fileHeader['fileName'], -1) == '/' && $_fileHeader['uncompressedSize'] == 0)
continue;
return;
}
$this->_centralDirStruct[] = $_fileHeader;
}
$_fileHeader['cdsOffset'] = ftell($this->_fp) - strlen($fileHeader);
// sort by filename
uasort($this->_centralDirStruct, array(&$this, '_sortHandler'));
}
if ($_fileHeader['fileNameLength'] > 0) {
$_fileHeader['fileName'] = fread($this->_fp, $_fileHeader['fileNameLength']);
}
if ($_fileHeader['extraFieldLength'] > 0) {
$_fileHeader['extraField'] = fread($this->_fp, $_fileHeader['extraFieldLength']);
}
if ($_fileHeader['fileCommentLength'] > 0) {
$_fileHeader['fileComment'] = fread($this->_fp, $_fileHeader['fileCommentLength']);
}
function _sortHandler($s1, $s2)
{
return(strcmp($s1['fileName'], $s2['fileName']));
}
if (substr($_fileHeader['fileName'], -1) == '/' && $_fileHeader['uncompressedSize'] == 0) {
continue;
}
$this->_centralDirStruct[] = $_fileHeader;
}
// sort by filename
uasort($this->_centralDirStruct, [&$this, '_sortHandler']);
}
private function _sortHandler($s1, $s2)
{
return strcmp($s1['fileName'], $s2['fileName']);
}
}

View file

@ -19,220 +19,220 @@
*
*/
if(!defined('B1GMAIL_INIT'))
die('Directly calling this file is not supported');
if (!defined('B1GMAIL_INIT')) {
die('Directly calling this file is not supported');
}
/**
* ZIP class
*
* ZIP class.
*/
class BMZIP
{
/**
* b1gZIP stream (used if b1gZIP is installed)
*
* @var resource
*/
var $_b1gzip_stream;
/**
* b1gZIP stream (used if b1gZIP is installed).
*
* @var resource
*/
private $_b1gzip_stream;
/**
* output stream
*
* @var resource
*/
var $_fp;
/**
* output stream.
*
* @var resource
*/
private $_fp;
/**
* central directory structure
*
* @var array
*/
var $_centralDirStruct;
/**
* central directory structure.
*
* @var array
*/
private $_centralDirStruct;
/**
* constructor
*
* @param resource $fp Output stream
* @return BMZIP
*/
function __construct($fp)
{
// output stream
$this->_fp = $fp;
/**
* constructor.
*
* @param resource $fp Output stream
*
* @return BMZIP
*/
public function __construct($fp)
{
// output stream
$this->_fp = $fp;
// use b1gZIP?
if(function_exists('b1gzip_create')
&& function_exists('b1gzip_add')
&& function_exists('b1gzip_final'))
{
$this->_b1gzip_stream = b1gzip_create();
}
else
{
$this->_b1gzip_stream = false;
$this->_centralDirStruct = array();
}
}
// use b1gZIP?
if (function_exists('b1gzip_create')
&& function_exists('b1gzip_add')
&& function_exists('b1gzip_final')) {
$this->_b1gzip_stream = b1gzip_create();
} else {
$this->_b1gzip_stream = false;
$this->_centralDirStruct = [];
}
}
/**
* add a file to ZIP file
*
* @param string $fileName File name
* @param string $zipFileName File name in ZIP file
* @return bool
*/
function AddFile($fileName, $zipFileName = false)
{
$fileFP = @fopen($fileName, 'rb');
if($fileFP)
{
$result = $this->AddFileByFP($fileFP, $fileName, $zipFileName);
fclose($fileFP);
return($result);
}
else
return(false);
}
/**
* add a file to ZIP file.
*
* @param string $fileName File name
* @param string $zipFileName File name in ZIP file
*
* @return bool
*/
public function AddFile($fileName, $zipFileName = false)
{
$fileFP = @fopen($fileName, 'rb');
if ($fileFP) {
$result = $this->AddFileByFP($fileFP, $fileName, $zipFileName);
fclose($fileFP);
/**
* add a file to ZIP file by file pointer
*
* @param resource $fileFP
* @param string $fileName
* @param string $zipFileName
* @return bool
*/
function AddFileByFP($fileFP, $fileName, $zipFileName = false)
{
if(!$zipFileName)
$zipFileName = basename($fileName);
return $result;
} else {
return false;
}
}
// read file
fseek($fileFP, 0, SEEK_SET);
$fileData = '';
while(is_resource($fileFP) && !feof($fileFP))
$fileData .= @fread($fileFP, 4096);
$uncompressedSize = strlen($fileData);
/**
* add a file to ZIP file by file pointer.
*
* @param resource $fileFP
* @param string $fileName
* @param string $zipFileName
*
* @return bool
*/
public function AddFileByFP($fileFP, $fileName, $zipFileName = false)
{
if (!$zipFileName) {
$zipFileName = basename($fileName);
}
// use b1gZIP
if($this->_b1gzip_stream)
{
b1gzip_add($this->_b1gzip_stream, $fileData, $zipFileName);
return(true);
}
// read file
fseek($fileFP, 0, SEEK_SET);
$fileData = '';
while (is_resource($fileFP) && !feof($fileFP)) {
$fileData .= @fread($fileFP, 4096);
}
$uncompressedSize = strlen($fileData);
// or own implementation
else
{
// compute crc32
$crc32 = crc32($fileData);
$compressedData = gzcompress($fileData);
unset($fileData);
$compressedData = substr($compressedData, 2, -4);
$compressedSize = strlen($compressedData);
// use b1gZIP
if ($this->_b1gzip_stream) {
b1gzip_add($this->_b1gzip_stream, $fileData, $zipFileName);
// write file header
$this->_beginFile($crc32, $compressedSize, $uncompressedSize, $zipFileName);
fwrite($this->_fp, $compressedData);
}
return true;
}
return(false);
}
// or own implementation
else {
// compute crc32
$crc32 = crc32($fileData);
$compressedData = gzcompress($fileData);
unset($fileData);
$compressedData = substr($compressedData, 2, -4);
$compressedSize = strlen($compressedData);
/**
* begin file
*
* @param int $crc32
* @param int $compressedSize
* @param int $uncompressedSize
* @param string $fileName
*/
function _beginFile($crc32, $compressedSize, $uncompressedSize, $fileName)
{
// local header
$header = pack('VvvvvvVVVvv',
0x04034b50,
0x0014,
0x0,
0x0008,
(date('H') << 11) | (date('i') << 5) | round(date('s')/2, 0),
(date('Y')-1980 << 9) | (date('m') << 5) | date('d'),
$crc32,
$compressedSize,
$uncompressedSize,
strlen($fileName),
0x0);
$offset = ftell($this->_fp);
fwrite($this->_fp, $header);
fwrite($this->_fp, $fileName);
// write file header
$this->_beginFile($crc32, $compressedSize, $uncompressedSize, $zipFileName);
fwrite($this->_fp, $compressedData);
}
// central dir struct entry
$entry = pack('VvvvvvvVVVvvvvvVV',
0x02014b50,
0x0,
0x0014,
0x0,
0x0008,
(date('H') << 11) | (date('i') << 5) | round(date('s')/2, 0),
(date('Y')-1980 << 9) | (date('m') << 5) | date('d'),
$crc32,
$compressedSize,
$uncompressedSize,
strlen($fileName),
0x0,
0x0,
0x0,
0x0,
32,
$offset);
$entry .= $fileName;
$this->_centralDirStruct[] = $entry;
}
return false;
}
/**
* finish zip file
*
* @return int Size
*/
function Finish()
{
// use b1gZIP?
if($this->_b1gzip_stream)
{
$zipData = b1gzip_final($this->_b1gzip_stream);
fwrite($this->_fp, $zipData);
fseek($this->_fp, 0, SEEK_SET);
return(strlen($zipData));
}
/**
* begin file.
*
* @param int $crc32
* @param int $compressedSize
* @param int $uncompressedSize
* @param string $fileName
*/
private function _beginFile($crc32, $compressedSize, $uncompressedSize, $fileName)
{
// local header
$header = pack('VvvvvvVVVvv',
0x04034b50,
0x0014,
0x0,
0x0008,
(date('H') << 11) | (date('i') << 5) | round(date('s') / 2, 0),
(date('Y') - 1980 << 9) | (date('m') << 5) | date('d'),
$crc32,
$compressedSize,
$uncompressedSize,
strlen($fileName),
0x0);
$offset = ftell($this->_fp);
fwrite($this->_fp, $header);
fwrite($this->_fp, $fileName);
// or own implementation
else
{
// write central dir struct
$offset = ftell($this->_fp);
$dLength = 0;
foreach($this->_centralDirStruct as $item)
{
fwrite($this->_fp, $item);
$dLength += strlen($item);
}
// central dir struct entry
$entry = pack('VvvvvvvVVVvvvvvVV',
0x02014b50,
0x0,
0x0014,
0x0,
0x0008,
(date('H') << 11) | (date('i') << 5) | round(date('s') / 2, 0),
(date('Y') - 1980 << 9) | (date('m') << 5) | date('d'),
$crc32,
$compressedSize,
$uncompressedSize,
strlen($fileName),
0x0,
0x0,
0x0,
0x0,
32,
$offset);
$entry .= $fileName;
$this->_centralDirStruct[] = $entry;
}
// write footer
$footer = pack('VvvvvVVv',
0x06054b50,
0x0,
0x0,
count($this->_centralDirStruct),
count($this->_centralDirStruct),
$dLength,
$offset,
0x0);
fwrite($this->_fp, $footer);
/**
* finish zip file.
*
* @return int Size
*/
public function Finish()
{
// use b1gZIP?
if ($this->_b1gzip_stream) {
$zipData = b1gzip_final($this->_b1gzip_stream);
fwrite($this->_fp, $zipData);
fseek($this->_fp, 0, SEEK_SET);
// return
$len = ftell($this->_fp);
fseek($this->_fp, 0, SEEK_SET);
return($len);
}
}
return strlen($zipData);
}
// or own implementation
else {
// write central dir struct
$offset = ftell($this->_fp);
$dLength = 0;
foreach ($this->_centralDirStruct as $item) {
fwrite($this->_fp, $item);
$dLength += strlen($item);
}
// write footer
$footer = pack('VvvvvVVv',
0x06054b50,
0x0,
0x0,
count($this->_centralDirStruct),
count($this->_centralDirStruct),
$dLength,
$offset,
0x0);
fwrite($this->_fp, $footer);
// return
$len = ftell($this->_fp);
fseek($this->_fp, 0, SEEK_SET);
return $len;
}
}
}