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

File diff suppressed because it is too large Load diff

View file

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

View file

@ -19,43 +19,44 @@
* *
*/ */
if(!defined('B1GMAIL_INIT')) if (!defined('B1GMAIL_INIT')) {
die('Directly calling this file is not supported'); die('Directly calling this file is not supported');
}
/** /**
* ZIP class * ZIP class.
*
*/ */
class BMZIP class BMZIP
{ {
/** /**
* b1gZIP stream (used if b1gZIP is installed) * b1gZIP stream (used if b1gZIP is installed).
* *
* @var resource * @var resource
*/ */
var $_b1gzip_stream; private $_b1gzip_stream;
/** /**
* output stream * output stream.
* *
* @var resource * @var resource
*/ */
var $_fp; private $_fp;
/** /**
* central directory structure * central directory structure.
* *
* @var array * @var array
*/ */
var $_centralDirStruct; private $_centralDirStruct;
/** /**
* constructor * constructor.
* *
* @param resource $fp Output stream * @param resource $fp Output stream
*
* @return BMZIP * @return BMZIP
*/ */
function __construct($fp) public function __construct($fp)
{ {
// output stream // output stream
$this->_fp = $fp; $this->_fp = $fp;
@ -63,67 +64,67 @@ class BMZIP
// use b1gZIP? // use b1gZIP?
if (function_exists('b1gzip_create') if (function_exists('b1gzip_create')
&& function_exists('b1gzip_add') && function_exists('b1gzip_add')
&& function_exists('b1gzip_final')) && function_exists('b1gzip_final')) {
{
$this->_b1gzip_stream = b1gzip_create(); $this->_b1gzip_stream = b1gzip_create();
} } else {
else
{
$this->_b1gzip_stream = false; $this->_b1gzip_stream = false;
$this->_centralDirStruct = array(); $this->_centralDirStruct = [];
} }
} }
/** /**
* add a file to ZIP file * add a file to ZIP file.
* *
* @param string $fileName File name * @param string $fileName File name
* @param string $zipFileName File name in ZIP file * @param string $zipFileName File name in ZIP file
*
* @return bool * @return bool
*/ */
function AddFile($fileName, $zipFileName = false) public function AddFile($fileName, $zipFileName = false)
{ {
$fileFP = @fopen($fileName, 'rb'); $fileFP = @fopen($fileName, 'rb');
if($fileFP) if ($fileFP) {
{
$result = $this->AddFileByFP($fileFP, $fileName, $zipFileName); $result = $this->AddFileByFP($fileFP, $fileName, $zipFileName);
fclose($fileFP); fclose($fileFP);
return($result);
return $result;
} else {
return false;
} }
else
return(false);
} }
/** /**
* add a file to ZIP file by file pointer * add a file to ZIP file by file pointer.
* *
* @param resource $fileFP * @param resource $fileFP
* @param string $fileName * @param string $fileName
* @param string $zipFileName * @param string $zipFileName
*
* @return bool * @return bool
*/ */
function AddFileByFP($fileFP, $fileName, $zipFileName = false) public function AddFileByFP($fileFP, $fileName, $zipFileName = false)
{ {
if(!$zipFileName) if (!$zipFileName) {
$zipFileName = basename($fileName); $zipFileName = basename($fileName);
}
// read file // read file
fseek($fileFP, 0, SEEK_SET); fseek($fileFP, 0, SEEK_SET);
$fileData = ''; $fileData = '';
while(is_resource($fileFP) && !feof($fileFP)) while (is_resource($fileFP) && !feof($fileFP)) {
$fileData .= @fread($fileFP, 4096); $fileData .= @fread($fileFP, 4096);
}
$uncompressedSize = strlen($fileData); $uncompressedSize = strlen($fileData);
// use b1gZIP // use b1gZIP
if($this->_b1gzip_stream) if ($this->_b1gzip_stream) {
{
b1gzip_add($this->_b1gzip_stream, $fileData, $zipFileName); b1gzip_add($this->_b1gzip_stream, $fileData, $zipFileName);
return(true);
return true;
} }
// or own implementation // or own implementation
else else {
{
// compute crc32 // compute crc32
$crc32 = crc32($fileData); $crc32 = crc32($fileData);
$compressedData = gzcompress($fileData); $compressedData = gzcompress($fileData);
@ -136,18 +137,18 @@ class BMZIP
fwrite($this->_fp, $compressedData); fwrite($this->_fp, $compressedData);
} }
return(false); return false;
} }
/** /**
* begin file * begin file.
* *
* @param int $crc32 * @param int $crc32
* @param int $compressedSize * @param int $compressedSize
* @param int $uncompressedSize * @param int $uncompressedSize
* @param string $fileName * @param string $fileName
*/ */
function _beginFile($crc32, $compressedSize, $uncompressedSize, $fileName) private function _beginFile($crc32, $compressedSize, $uncompressedSize, $fileName)
{ {
// local header // local header
$header = pack('VvvvvvVVVvv', $header = pack('VvvvvvVVVvv',
@ -190,29 +191,27 @@ class BMZIP
} }
/** /**
* finish zip file * finish zip file.
* *
* @return int Size * @return int Size
*/ */
function Finish() public function Finish()
{ {
// use b1gZIP? // use b1gZIP?
if($this->_b1gzip_stream) if ($this->_b1gzip_stream) {
{
$zipData = b1gzip_final($this->_b1gzip_stream); $zipData = b1gzip_final($this->_b1gzip_stream);
fwrite($this->_fp, $zipData); fwrite($this->_fp, $zipData);
fseek($this->_fp, 0, SEEK_SET); fseek($this->_fp, 0, SEEK_SET);
return(strlen($zipData));
return strlen($zipData);
} }
// or own implementation // or own implementation
else else {
{
// write central dir struct // write central dir struct
$offset = ftell($this->_fp); $offset = ftell($this->_fp);
$dLength = 0; $dLength = 0;
foreach($this->_centralDirStruct as $item) foreach ($this->_centralDirStruct as $item) {
{
fwrite($this->_fp, $item); fwrite($this->_fp, $item);
$dLength += strlen($item); $dLength += strlen($item);
} }
@ -232,7 +231,8 @@ class BMZIP
// return // return
$len = ftell($this->_fp); $len = ftell($this->_fp);
fseek($this->_fp, 0, SEEK_SET); fseek($this->_fp, 0, SEEK_SET);
return($len);
return $len;
} }
} }
} }