-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
602b118
commit 9054665
Showing
3 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace app\components; | ||
use yii\base\Exception; | ||
use yii\web\UploadedFile; | ||
|
||
/** | ||
* Set as component? | ||
* | ||
* Class FtpPusher | ||
* @package app\components | ||
*/ | ||
class FtpPusher | ||
{ | ||
const LOCAL_STORAGE = __DIR__ . '/pushed/'; | ||
|
||
private $host; | ||
private $login; | ||
private $password; | ||
private $connection; | ||
|
||
private $files = [ | ||
//[0 => filename | ||
//1 => localpath | ||
//2 => remotepath | ||
//3 => uploaded?] | ||
]; | ||
|
||
public function __construct($host, $login, $password) | ||
{ | ||
$this->login = $login; | ||
$this->host = $host; | ||
$this->password = $password; | ||
} | ||
|
||
public function connectProcedure() | ||
{ | ||
if (!$this->connect()) { | ||
throw new Exception('Connection failed'); | ||
} | ||
if (!$this->login()) { | ||
$this->disconnect(); | ||
throw new Exception('Authorization failed'); | ||
} | ||
} | ||
|
||
private function connect() | ||
{ | ||
$this->connection = ftp_connect($this->host, 21, 30); | ||
|
||
return $this->connection !== false; | ||
} | ||
private function disconnect() | ||
{ | ||
ftp_close($this->connection); | ||
$this->connection = null; | ||
} | ||
|
||
private function login() | ||
{ | ||
return @ftp_login($this->connection, $this->login, $this->password); | ||
} | ||
|
||
public function push() | ||
{ | ||
$this->connectProcedure(); | ||
|
||
$response = true; | ||
foreach ($this->files as $key => $file) { | ||
if ($file[3]) { | ||
//already uploaded | ||
continue; | ||
} | ||
if (ftp_put($this->connection, $file[2] . $file[0], $file[1] . $file[0], FTP_ASCII)){ | ||
$this->files[$key][3] = true; | ||
} else { | ||
$this->files[$key][3] = false; | ||
$response = false; | ||
} | ||
} | ||
|
||
$this->disconnect(); | ||
|
||
return $response; | ||
} | ||
|
||
public function addFromString($content, $remotePath, $name) | ||
{ | ||
if (file_put_contents(self::LOCAL_STORAGE . $name, $content)) { | ||
$this->files[] = [ | ||
$name, | ||
self::LOCAL_STORAGE, | ||
$remotePath, | ||
false, | ||
]; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace app\components; | ||
|
||
/** | ||
* Class Pusher | ||
* @package app\components | ||
*/ | ||
abstract class Pusher | ||
{ | ||
private $transport; | ||
|
||
/** | ||
* @todo: create interface for transport | ||
* Pusher constructor. | ||
* @param $transport | ||
*/ | ||
public function __construct($transport) | ||
{ | ||
$this->transport = $transport; | ||
} | ||
|
||
/** | ||
* @param $content | ||
* @param $name | ||
* @param $extension | ||
* @return mixed | ||
*/ | ||
public function addFileByString($content, $name, $extension) | ||
{ | ||
|
||
} | ||
|
||
/**Add from file system | ||
* @return mixed | ||
*/ | ||
public function addFileByPath() | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* Send files | ||
* @return mixed | ||
*/ | ||
public function push() | ||
{ | ||
$this->transport->push(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters