Skip to content

Commit a1244cf

Browse files
authored
Merge pull request #101 from WebFiori/dev
Improvements to Class `File` + Add SMTP Command + Run Query Command + SMTP Server
2 parents 72b51fc + 98b9480 commit a1244cf

File tree

5 files changed

+133
-28
lines changed

5 files changed

+133
-28
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"webfiori/ui":"2.2.2",
1414
"webfiori/jsonx":"2.1.0",
1515
"webfiori/http":"3.1.1",
16-
"webfiori/database":"0.3.7"
16+
"webfiori/database":"0.3.8"
1717
},
1818
"keywords": [
1919
"framework",

webfiori/framework/File.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*
3737
* @author Ibrahim
3838
*
39-
* @version 1.2.0
39+
* @version 1.2.1
4040
*/
4141
class File implements JsonI {
4242
/**
@@ -311,6 +311,54 @@ public function getAbsolutePath() {
311311

312312
return '';
313313
}
314+
/**
315+
* Split file raw data into chunks of fixed size.
316+
*
317+
* @param int $chunkSize The number of bytes in every chunk. If a negative
318+
* number is given, default value is used which is 1000.
319+
*
320+
* @param string $encodeOrDecode This parameter is used to base-64 decode or
321+
* encode file data. The parameter can have one of 3 values:
322+
* <ul>
323+
* <li>e: Encode the raw data of the file.</li>
324+
* <li>d: Decode the raw data of the file.</li>
325+
* <li>none: Return the raw data of the file as it is. This is the default value.</li>
326+
* </ul>
327+
* If any other value is given, the method will use 'e' since data is mostly
328+
* will be moved ss chunks.
329+
*
330+
* @return array The method will return an array that holds file data as
331+
* chunks.
332+
*
333+
* @since 1.2.1
334+
*/
335+
public function getChunks($chunkSize = 1000, $encodeOrDecode = 'e') {
336+
if ($chunkSize < 0) {
337+
$chunkSize = 1000;
338+
}
339+
340+
$data = $this->getRawData($encodeOrDecode);
341+
342+
if ($data === null) {
343+
return [];
344+
}
345+
$dataLen = strlen($data);
346+
$retVal = [];
347+
$index = 0;
348+
349+
while ($index < $dataLen) {
350+
$retVal[] = substr($data, $index, $chunkSize);
351+
$index += $chunkSize;
352+
}
353+
354+
$remainingChars = $dataLen - count($retVal) * $chunkSize;
355+
356+
if ($remainingChars > 0) {
357+
$retVal[] = substr($data, $index);
358+
}
359+
360+
return $retVal;
361+
}
314362
/**
315363
* Returns the directory at which the file exist on.
316364
*

webfiori/framework/cli/commands/AddCommand.php

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,27 +136,50 @@ private function _addLang() {
136136
private function _addSmtp() {
137137
$smtpConn = new SMTPAccount();
138138
$smtpConn->setServerAddress($this->getInput('SMTP Server address:', '127.0.0.1'));
139-
$smtpConn->setPort($this->getInput('Port number:', 465));
139+
$smtpConn->setPort(25);
140+
$addr = $smtpConn->getAddress();
141+
142+
if ($addr == 'smtp.outlook.com'
143+
|| $addr == 'outlook.office365.com'
144+
|| $addr == 'smtp.office365.com') {
145+
$smtpConn->setPort(587);
146+
} else if ($addr == 'smtp.gmail.com'
147+
|| $addr == 'smtp.mail.yahoo.com') {
148+
$smtpConn->setPort(465);
149+
}
150+
$smtpConn->setPort($this->getInput('Port number:', $smtpConn->getPort()));
140151
$smtpConn->setUsername($this->getInput('Username:'));
141152
$smtpConn->setPassword($this->getInput('Password:'));
142153
$smtpConn->setAddress($this->getInput('Sender email address:', $smtpConn->getUsername()));
143-
$smtpConn->setSenderName($this->getInput('Sender name:', 'WebFiori Framework'));
154+
$smtpConn->setSenderName($this->getInput('Sender name:', $smtpConn->getAddress()));
144155
$smtpConn->setAccountName($this->getInput('Give your connection a friendly name:', 'smtp-connection-'.count(WebFioriApp::getAppConfig()->getAccounts())));
145156
$this->println('Testing connection. This can take up to 1 minute...');
146157
$server = new SMTPServer($smtpConn->getServerAddress(), $smtpConn->getPort());
147158

148-
149-
if ($server->authLogin($smtpConn->getUsername(), $smtpConn->getPassword())) {
150-
$this->success('Connectd. Adding connection information...');
159+
try {
160+
if ($server->authLogin($smtpConn->getUsername(), $smtpConn->getPassword())) {
161+
$this->success('Connectd. Adding connection information...');
162+
ConfigController::get()->updateOrAddEmailAccount($smtpConn);
163+
$this->success('Connection information was stored in the class "'.APP_DIR_NAME.'\\AppConfig".');
164+
165+
166+
} else {
167+
$this->error('Unable to connect to SMTP server.');
168+
$this->println('Error Information: '.$server->getLastResponse());
169+
170+
$this->_confirmAdd($smtpConn);
171+
}
172+
} catch (Exception $ex) {
173+
$this->error('An exception with message "'.$ex->getMessage().'" was thrown while trying to connect.');
174+
$this->_confirmAdd($smtpConn);
175+
}
176+
177+
return 0;
178+
}
179+
private function _confirmAdd($smtpConn) {
180+
if ($this->confirm('Would you like to store connection information anyway?')) {
151181
ConfigController::get()->updateOrAddEmailAccount($smtpConn);
152182
$this->success('Connection information was stored in the class "'.APP_DIR_NAME.'\\AppConfig".');
153-
154-
return 0;
155-
} else {
156-
$this->error('Unable to connect to SMTP server.');
157-
$this->println('Error Information: '.$server->getLastResponse());
158-
159-
return -1;
160183
}
161184
}
162185
}

webfiori/framework/cli/commands/RunSQLQueryCommand.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,14 @@ private function queryOnSchema(DB $schema) {
261261
}
262262
/**
263263
*
264-
* @param type $schema
264+
* @param DB $schema
265265
* @param Table $tableObj
266266
*/
267267
private function tableQuery($schema, $tableObj) {
268268
$queryTypes = [
269269
'Create database table.',
270270
'Drop database table.',
271+
'Drop and create table.',
271272
'Add Column.',
272273
'Modify Column.',
273274
'Drop Column.'
@@ -287,6 +288,11 @@ private function tableQuery($schema, $tableObj) {
287288
$schema->table($tableObj->getNormalName())->createTable();
288289
} else if ($selectedQuery == 'Drop database table.') {
289290
$schema->table($tableObj->getNormalName())->drop();
290-
}
291+
} else if ($selectedQuery == 'Drop and create table.') {
292+
$schema->table($tableObj->getNormalName())->drop();
293+
$query1 = $schema->getLastQuery();
294+
$schema->table($tableObj->getNormalName())->createTable();
295+
$schema->setQuery($query1."\n".$schema->getLastQuery());
296+
}
291297
}
292298
}

webfiori/framework/mail/SMTPServer.php

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* @author Ibrahim
99
*
10-
* @version 1.0
10+
* @version 1.0.1
1111
*/
1212
class SMTPServer {
1313
const NL = "\r\n";
@@ -141,7 +141,8 @@ public function connect() {
141141
if (is_resource($this->serverCon)) {
142142
$this->_log('-', 0, $this->read());
143143
if ($this->getLastResponseCode() != 220) {
144-
throw new SMTPException('Server did not respond with code 220 during initial connection.');
144+
$this->_log('Connect', 0, 'Server did not respond with code 220 during initial connection.');
145+
throw new SMTPException($this->getLastLogEntry()['message']);
145146
}
146147
if ($this->sendHello()) {
147148
//We might need to switch to secure connection.
@@ -205,13 +206,35 @@ public function getLastSentCommand() {
205206
* commands which was sent to the server.
206207
*
207208
* @return array The array will hold sub-associative arrays. Each array
208-
* will have 3 indices, 'command', 'response-code' and 'response-message'
209+
* will have 4 indices, 'command', 'code', 'message' and 'time'
209210
*
210211
* @since 1.0
211212
*/
212213
public function getLog() {
213214
return $this->responseLog;
214215
}
216+
/**
217+
* Returns an array that contains last log entry.
218+
*
219+
* @return array The array will have 4 indices, 'command', 'code',
220+
* 'message' and 'time'.
221+
*
222+
* @since 1.0.1
223+
*/
224+
public function getLastLogEntry() {
225+
$entries = $this->getLog();
226+
$entriesCount = count($entries);
227+
228+
if ($entriesCount != 0) {
229+
return $entries[$entriesCount - 1];
230+
}
231+
return [
232+
'command' => '',
233+
'code' => 0,
234+
'message' => '',
235+
'time' => ''
236+
];
237+
}
215238
/**
216239
* Returns SMTP server port number.
217240
*
@@ -281,7 +304,8 @@ public function read() {
281304
$message = '';
282305

283306
while (!feof($this->serverCon)) {
284-
$str = stream_get_contents($this->serverCon);
307+
$str = fgets($this->serverCon);
308+
285309
if ($str !== false) {
286310
$message .= $str;
287311

@@ -290,6 +314,7 @@ public function read() {
290314
}
291315
} else {
292316
$this->_log('-', '0', 'Unable to read server response.');
317+
break;
293318
}
294319
}
295320
$this->_setLastResponseCode($message);
@@ -408,8 +433,9 @@ private function _getTransport() {
408433
private function _log($command, $code, $message) {
409434
$this->responseLog[] = [
410435
'command' => $command,
411-
'response-code' => $code,
412-
'response-message' => $message
436+
'code' => $code,
437+
'message' => $message,
438+
'time' => date('Y-m-d H:i:s')
413439
];
414440
}
415441
private function _parseHelloResponse($response) {
@@ -418,7 +444,7 @@ private function _parseHelloResponse($response) {
418444
$this->serverOptions = [];
419445

420446
foreach ($split as $part) {
421-
//Index 0 will usually hold server address
447+
//Index 0 will hold server address
422448
if ($index != 0) {
423449
$xPart = substr($part, 4);
424450
$this->serverOptions[] = $xPart;
@@ -484,19 +510,21 @@ private function _tryConnect($protocol) {
484510
]);
485511

486512

487-
$this->_log('Connect', 0, 'Trying to connect to the server using "stream_socket_client"...');
488-
$conn = stream_socket_client($protocol.$host.':'.$portNum, $err, $errStr, $timeout * 60, STREAM_CLIENT_CONNECT, $context);
513+
$this->_log('Connect', 0, 'Trying to open connection to the server using "stream_socket_client"...');
514+
$conn = @stream_socket_client($protocol.$host.':'.$portNum, $err, $errStr, $timeout * 60, STREAM_CLIENT_CONNECT, $context);
489515
} else {
490-
$this->_log('Connect', 0, 'Trying to connect to the server using "fsockopen"...');
516+
$this->_log('Connect', 0, 'Trying to open connection to the server using "fsockopen"...');
491517
$conn = fsockopen($protocol.$this->serverHost, $portNum, $err, $errStr, $timeout * 60);
492518
}
493519

494520
if (!is_resource($conn)) {
495521
if (strlen($errStr) == 0) {
496-
$this->_log('Connect', $err, 'Faild to connect due to unspecified error.');
522+
$this->_log('Connect', $err, 'Faild to open connection due to unspecified error.');
497523
} else {
498-
$this->_log('Connect', $err, 'Faild to connect: '.$errStr);
524+
$this->_log('Connect', $err, 'Faild to open connection: '.$errStr);
499525
}
526+
} else {
527+
$this->_log('Connect', 0, 'Connection opened.');
500528
}
501529

502530
return $conn;

0 commit comments

Comments
 (0)