Skip to content

Commit

Permalink
Merge pull request #101 from WebFiori/dev
Browse files Browse the repository at this point in the history
Improvements to Class `File` + Add SMTP Command + Run Query Command + SMTP Server
  • Loading branch information
usernane authored Dec 5, 2021
2 parents 72b51fc + 98b9480 commit a1244cf
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 28 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"webfiori/ui":"2.2.2",
"webfiori/jsonx":"2.1.0",
"webfiori/http":"3.1.1",
"webfiori/database":"0.3.7"
"webfiori/database":"0.3.8"
},
"keywords": [
"framework",
Expand Down
50 changes: 49 additions & 1 deletion webfiori/framework/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* @author Ibrahim
*
* @version 1.2.0
* @version 1.2.1
*/
class File implements JsonI {
/**
Expand Down Expand Up @@ -311,6 +311,54 @@ public function getAbsolutePath() {

return '';
}
/**
* Split file raw data into chunks of fixed size.
*
* @param int $chunkSize The number of bytes in every chunk. If a negative
* number is given, default value is used which is 1000.
*
* @param string $encodeOrDecode This parameter is used to base-64 decode or
* encode file data. The parameter can have one of 3 values:
* <ul>
* <li>e: Encode the raw data of the file.</li>
* <li>d: Decode the raw data of the file.</li>
* <li>none: Return the raw data of the file as it is. This is the default value.</li>
* </ul>
* If any other value is given, the method will use 'e' since data is mostly
* will be moved ss chunks.
*
* @return array The method will return an array that holds file data as
* chunks.
*
* @since 1.2.1
*/
public function getChunks($chunkSize = 1000, $encodeOrDecode = 'e') {
if ($chunkSize < 0) {
$chunkSize = 1000;
}

$data = $this->getRawData($encodeOrDecode);

if ($data === null) {
return [];
}
$dataLen = strlen($data);
$retVal = [];
$index = 0;

while ($index < $dataLen) {
$retVal[] = substr($data, $index, $chunkSize);
$index += $chunkSize;
}

$remainingChars = $dataLen - count($retVal) * $chunkSize;

if ($remainingChars > 0) {
$retVal[] = substr($data, $index);
}

return $retVal;
}
/**
* Returns the directory at which the file exist on.
*
Expand Down
47 changes: 35 additions & 12 deletions webfiori/framework/cli/commands/AddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,27 +136,50 @@ private function _addLang() {
private function _addSmtp() {
$smtpConn = new SMTPAccount();
$smtpConn->setServerAddress($this->getInput('SMTP Server address:', '127.0.0.1'));
$smtpConn->setPort($this->getInput('Port number:', 465));
$smtpConn->setPort(25);
$addr = $smtpConn->getAddress();

if ($addr == 'smtp.outlook.com'
|| $addr == 'outlook.office365.com'
|| $addr == 'smtp.office365.com') {
$smtpConn->setPort(587);
} else if ($addr == 'smtp.gmail.com'
|| $addr == 'smtp.mail.yahoo.com') {
$smtpConn->setPort(465);
}
$smtpConn->setPort($this->getInput('Port number:', $smtpConn->getPort()));
$smtpConn->setUsername($this->getInput('Username:'));
$smtpConn->setPassword($this->getInput('Password:'));
$smtpConn->setAddress($this->getInput('Sender email address:', $smtpConn->getUsername()));
$smtpConn->setSenderName($this->getInput('Sender name:', 'WebFiori Framework'));
$smtpConn->setSenderName($this->getInput('Sender name:', $smtpConn->getAddress()));
$smtpConn->setAccountName($this->getInput('Give your connection a friendly name:', 'smtp-connection-'.count(WebFioriApp::getAppConfig()->getAccounts())));
$this->println('Testing connection. This can take up to 1 minute...');
$server = new SMTPServer($smtpConn->getServerAddress(), $smtpConn->getPort());


if ($server->authLogin($smtpConn->getUsername(), $smtpConn->getPassword())) {
$this->success('Connectd. Adding connection information...');
try {
if ($server->authLogin($smtpConn->getUsername(), $smtpConn->getPassword())) {
$this->success('Connectd. Adding connection information...');
ConfigController::get()->updateOrAddEmailAccount($smtpConn);
$this->success('Connection information was stored in the class "'.APP_DIR_NAME.'\\AppConfig".');


} else {
$this->error('Unable to connect to SMTP server.');
$this->println('Error Information: '.$server->getLastResponse());

$this->_confirmAdd($smtpConn);
}
} catch (Exception $ex) {
$this->error('An exception with message "'.$ex->getMessage().'" was thrown while trying to connect.');
$this->_confirmAdd($smtpConn);
}

return 0;
}
private function _confirmAdd($smtpConn) {
if ($this->confirm('Would you like to store connection information anyway?')) {
ConfigController::get()->updateOrAddEmailAccount($smtpConn);
$this->success('Connection information was stored in the class "'.APP_DIR_NAME.'\\AppConfig".');

return 0;
} else {
$this->error('Unable to connect to SMTP server.');
$this->println('Error Information: '.$server->getLastResponse());

return -1;
}
}
}
10 changes: 8 additions & 2 deletions webfiori/framework/cli/commands/RunSQLQueryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,14 @@ private function queryOnSchema(DB $schema) {
}
/**
*
* @param type $schema
* @param DB $schema
* @param Table $tableObj
*/
private function tableQuery($schema, $tableObj) {
$queryTypes = [
'Create database table.',
'Drop database table.',
'Drop and create table.',
'Add Column.',
'Modify Column.',
'Drop Column.'
Expand All @@ -287,6 +288,11 @@ private function tableQuery($schema, $tableObj) {
$schema->table($tableObj->getNormalName())->createTable();
} else if ($selectedQuery == 'Drop database table.') {
$schema->table($tableObj->getNormalName())->drop();
}
} else if ($selectedQuery == 'Drop and create table.') {
$schema->table($tableObj->getNormalName())->drop();
$query1 = $schema->getLastQuery();
$schema->table($tableObj->getNormalName())->createTable();
$schema->setQuery($query1."\n".$schema->getLastQuery());
}
}
}
52 changes: 40 additions & 12 deletions webfiori/framework/mail/SMTPServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @author Ibrahim
*
* @version 1.0
* @version 1.0.1
*/
class SMTPServer {
const NL = "\r\n";
Expand Down Expand Up @@ -141,7 +141,8 @@ public function connect() {
if (is_resource($this->serverCon)) {
$this->_log('-', 0, $this->read());
if ($this->getLastResponseCode() != 220) {
throw new SMTPException('Server did not respond with code 220 during initial connection.');
$this->_log('Connect', 0, 'Server did not respond with code 220 during initial connection.');
throw new SMTPException($this->getLastLogEntry()['message']);
}
if ($this->sendHello()) {
//We might need to switch to secure connection.
Expand Down Expand Up @@ -205,13 +206,35 @@ public function getLastSentCommand() {
* commands which was sent to the server.
*
* @return array The array will hold sub-associative arrays. Each array
* will have 3 indices, 'command', 'response-code' and 'response-message'
* will have 4 indices, 'command', 'code', 'message' and 'time'
*
* @since 1.0
*/
public function getLog() {
return $this->responseLog;
}
/**
* Returns an array that contains last log entry.
*
* @return array The array will have 4 indices, 'command', 'code',
* 'message' and 'time'.
*
* @since 1.0.1
*/
public function getLastLogEntry() {
$entries = $this->getLog();
$entriesCount = count($entries);

if ($entriesCount != 0) {
return $entries[$entriesCount - 1];
}
return [
'command' => '',
'code' => 0,
'message' => '',
'time' => ''
];
}
/**
* Returns SMTP server port number.
*
Expand Down Expand Up @@ -281,7 +304,8 @@ public function read() {
$message = '';

while (!feof($this->serverCon)) {
$str = stream_get_contents($this->serverCon);
$str = fgets($this->serverCon);

if ($str !== false) {
$message .= $str;

Expand All @@ -290,6 +314,7 @@ public function read() {
}
} else {
$this->_log('-', '0', 'Unable to read server response.');
break;
}
}
$this->_setLastResponseCode($message);
Expand Down Expand Up @@ -408,8 +433,9 @@ private function _getTransport() {
private function _log($command, $code, $message) {
$this->responseLog[] = [
'command' => $command,
'response-code' => $code,
'response-message' => $message
'code' => $code,
'message' => $message,
'time' => date('Y-m-d H:i:s')
];
}
private function _parseHelloResponse($response) {
Expand All @@ -418,7 +444,7 @@ private function _parseHelloResponse($response) {
$this->serverOptions = [];

foreach ($split as $part) {
//Index 0 will usually hold server address
//Index 0 will hold server address
if ($index != 0) {
$xPart = substr($part, 4);
$this->serverOptions[] = $xPart;
Expand Down Expand Up @@ -484,19 +510,21 @@ private function _tryConnect($protocol) {
]);


$this->_log('Connect', 0, 'Trying to connect to the server using "stream_socket_client"...');
$conn = stream_socket_client($protocol.$host.':'.$portNum, $err, $errStr, $timeout * 60, STREAM_CLIENT_CONNECT, $context);
$this->_log('Connect', 0, 'Trying to open connection to the server using "stream_socket_client"...');
$conn = @stream_socket_client($protocol.$host.':'.$portNum, $err, $errStr, $timeout * 60, STREAM_CLIENT_CONNECT, $context);
} else {
$this->_log('Connect', 0, 'Trying to connect to the server using "fsockopen"...');
$this->_log('Connect', 0, 'Trying to open connection to the server using "fsockopen"...');
$conn = fsockopen($protocol.$this->serverHost, $portNum, $err, $errStr, $timeout * 60);
}

if (!is_resource($conn)) {
if (strlen($errStr) == 0) {
$this->_log('Connect', $err, 'Faild to connect due to unspecified error.');
$this->_log('Connect', $err, 'Faild to open connection due to unspecified error.');
} else {
$this->_log('Connect', $err, 'Faild to connect: '.$errStr);
$this->_log('Connect', $err, 'Faild to open connection: '.$errStr);
}
} else {
$this->_log('Connect', 0, 'Connection opened.');
}

return $conn;
Expand Down

0 comments on commit a1244cf

Please sign in to comment.