first commit

This commit is contained in:
2024-11-11 18:46:54 +01:00
commit a630d17338
25634 changed files with 4923715 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
<?php
// todo: change dir
require_once __DIR__ . '/../../config/config.inc.php';
#require_once __DIR__.'/../../config/config.inc.php';
$smarty = Context::getContext()->smarty;
$smarty->display(_PS_MODULE_DIR_ . 'freshmail/views/templates/plugin_info.tpl');

View File

@@ -0,0 +1,491 @@
<?php
namespace FreshMail;
use Configuration;
use Db;
use DbQuery;
use Exception;
use FreshMail\Api\Client\Service\RequestExecutor;
use FreshMail\ApiV2\Client;
use FreshMail\ApiV2\UnauthorizedException;
use FreshMail\Entity\AsyncJob;
use FreshMail\Entity\Email;
use FreshMail\Entity\EmailToSynchronize;
use FreshMail\Repository\AsyncJobs;
use FreshMail\Repository\EmailsSynchronized;
use Validate;
use ZipArchive;
require_once __DIR__ . '/../lib/freshmail-api/vendor/autoload.php';
class Freshmail
{
private $token = '';
protected $freshmailApiV2 = null;
protected $freshmailApiV3 = null;
const SYNC_SUCCESS_ID_STATUS = 2;
const SYNC_FAIL_ID_STATUS = 3;
public function __construct($token)
{
$this->token = $token;
$this->freshmailApiV2 = new Client($token);
$this->freshmailApiV3 = new FreshmailApiV3($token);
}
public function getLists()
{
try {
$list = $this->freshmailApiV2->doRequest('subscribers_list/lists');
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
if (!empty($list['lists'])) {
usort($list['lists'], function ($a, $b) {
return strcmp($a["name"], $b["name"]);
});
return $list['lists'];
}
return [];
}
public function addList($name, $description = '')
{
$data = [
'name' => $name,
'description' => $description,
'custom_fields' => [
[
'name' => \Context::getContext()->getTranslator()->trans('First name', [],'Admin.Global'),
'tag' => \Freshmail::NAME_TAG
],
]
];
try {
$result = $this->freshmailApiV2->doRequest('subscribers_list/create', $data);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
if ('OK' == $result['status']) {
return $result['hash'];
}
return false;
}
public function hasFieldWithTag($hashList, $tag) {
$fields = $this->getAllFieldsByIdHashList($hashList);
if(empty($fields)){
return false;
}
foreach ($fields as $field){
if($tag == $field['tag']) {
return true;
}
}
return false;
}
public function addFieldToList($hashList, $tag, $name) {
$fieldData = array(
'hash' => $hashList,
'name' => $name,
'tag' => $tag
);
$customFields = $this->getAllFieldsByIdHashList($hashList);
if (is_array($customFields) && !in_array($fieldData['tag'], array_column($customFields, 'tag'))) {
return $this->addField($fieldData);
}
return false;
}
public function addField($fields = array()) {
if(empty($fields)) {
return false;
}
try {
return $this->freshmailApiV2->doRequest('subscribers_list/addField', $fields);
} catch (Exception $e) {
throw new Exception('Wystąpił błąd podczas dodawania nowego pola!');
}
}
public function addSubscriber($data = null)
{
if (empty($data) || empty($data['email']) || empty($data['list'])) {
throw new Exception(Configuration::get('FRESHMAIL_SUBMISSION_FAILURE_MESSAGE'));
}
try {
$msg['response'] = $this->freshmailApiV2->doRequest('subscriber/add', $data);
$subscriber = new \FreshMail\Entity\Email();
$subscriber->email = $data['email'];
$subscriber->hash_list = $data['list'];
$subscriber->status = 'Active';
$subscriber->add_date = date('Y-m-d H:i:s');
$subscriber->last_synchronization = date('Y-m-d H:i:s');
$subscriber->save();
$synchronized = new \FreshMail\Entity\EmailsSynchronized();
$synchronized->email = $data['email'];
$synchronized->hash_list = $data['list'];
$synchronized->save();
if(!empty($data['custom_fields'])){
}
$msg['success'] = Configuration::get('FRESHMAIL_SUBMISSION_SUCCESS_MESSAGE');
} catch (Exception $exception) {
if ($exception->getCode() == 1304) {
$msg['error'] = Configuration::get('FRESHMAIL_ALREADY_SUBSCRIBED_MESSAGE');
} else {
$msg['error'] = Configuration::get('FRESHMAIL_SUBMISSION_FAILURE_MESSAGE');
}
}
return $msg;
}
public function addSubscribers($listHash, SubscriberCollection $collection, int $state, bool $confirm = false)
{
$subscribers = [];
foreach ($collection as $sub) {
$subscriber = [
'email' => $sub->email,
];
if (!empty($sub->custom_fields)) {
foreach ($sub->custom_fields as $name => $value){
if($this->hasFieldWithTag($listHash, $name)){
$subscriber['custom_fields'][$name] = $value;
}
}
}
$subscribers[] = $subscriber;
}
if (empty($subscribers)) {
return;
}
$data = [
'list' => $listHash,
'subscribers' => $subscribers,
'confirm' => (int)$confirm,
'state' => $state
];
return $this->freshmailApiV2->doRequest("subscriber/addMultiple", $data);
}
public function getAllFieldsByIdHashList($idHash = null)
{
if (empty($idHash)) {
return false;
}
$data = array('hash' => $idHash);
try {
$responseArray = $this->freshmailApiV2->doRequest('subscribers_list/getFields', $data);
if (isset($responseArray['fields']) && !empty($responseArray['fields'])) {
return $responseArray['fields'];
}
return array();
} catch (Exception $e) {
echo 'Code: ' . $e->getCode() . ' Message: ' . $e->getMessage() . "\n";
}
}
public function getAllHashList()
{
$res = $this->getAllList();
$hashList = array();
foreach ($res as $k => $v) {
$hashList[] = $v['key'];
}
return $hashList;
}
public function getAllList()
{
$data = array();
try {
$responseArray = $this->freshmailApiV2->doRequest('subscribers_list/lists', $data);
/*$response[0] = array(
'key' => 0,
'name' => '---'
);*/
usort($responseArray['lists'], function ($a, $b) {
return strtotime($a['creation_date']) < strtotime($b['creation_date']) ? 1 : -1;
});
$response = [];
foreach ($responseArray['lists'] as $k => $v) {
$response[] = array(
'key' => $v['subscriberListHash'],
'name' => $v['name'] . ' (' . $v['subscribers_number'] . ')'
);
}
return $response;
} catch (Exception $e) {
syslog(3, 'Code: ' . $e->getCode() . ' Message: ' . $e->getMessage());
}
}
public function getAllFieldHashByHashList($id = 0)
{
$res = $this->getAllFieldsByIdHashList($id);
$hashList = array();
foreach ($res as $k => $v) {
if (!empty($v) && isset($v['hash']) && isset($v['tag'])) {
$hashList[$v['hash']] = $v;
}
}
return $hashList;
}
public function check()
{
try {
$response = $this->freshmailApiV2->doRequest('ping');
}
catch (UnauthorizedException $e) {
$response['status'] = false;
}catch (Exception $e) {
$response['status'] = 'Error message: ' . $e->getMessage() . ', Error code: ' . $e->getCode() /*. ', HTTP code: ' . $response-> ->getHttpCode()*/;
}
return $response;
}
public function getFileFromJob($idJob)
{
$file = md5(time());
try {
$zipStream = $this->freshmailApiV2->doRequest("/rest/async_result/getFile", [
'id_job' => $idJob
], true);
Tools::checkTmpDir();
if (!mkdir(\Freshmail::TMP_DIR . $file)) {
throw new Exception('Error creating dir: ' . \Freshmail::TMP_DIR . $file);
}
file_put_contents(\Freshmail::TMP_DIR . $file . '.zip', $zipStream);
$zip = new ZipArchive;
if (TRUE === $zip->open(\Freshmail::TMP_DIR . $file . '.zip')) {
// Check if destination is writable
$zip->extractTo(\Freshmail::TMP_DIR . $file);
$zip->close();
} else {
throw new Exception('Error processing archive');
}
} catch (Exception $e) {
var_dump($e->getMessage());
}
return $file;
}
public function triggerExport($listHash)
{
$data = [
'list' => $listHash,
];
$response = $this->freshmailApiV2->doRequest("/rest/async_subscribers_list/export", $data);
if ('OK' == $response['status']) {
$aj = new AsyncJob();
$aj->hash_list = $listHash;
$aj->id_job = $response['data']['id_job'];
$aj->save();
return $aj;
}
return false;
}
public function getSubscriber($listHash, $email){
$uri = sprintf('subscriber/get/%s/%s', $listHash, $email);
try {
$response = $this->freshmailApiV2->doRequest($uri);
} catch (Exception $e) {
$response['status'] = 'Error message: '.$e->getMessage().', Error code: '.$e->getCode().', HTTP code: '.$this->freshmailApiV2->getHttpCode();
}catch (UnauthorizedException $e) {
$response['status'] = false;
}
return $response;
}
public function triggerSendSubscribers($hashList, SubscriberCollection $subscribers)
{
foreach ($subscribers as $subsciber) {
$ets = new EmailToSynchronize();
$ets->email = $subsciber->email;
$ets->name = $subsciber->name;
$ets->hash_list = $hashList;
try {
$ets->save();
} catch (Exception $e) {
}
}
$query = new DbQuery();
$query
->select('*')
->from('customer')
->where('newsletter = 1');
$db = Db::getInstance();
foreach (Db::getInstance()->executeS($query) as $customer) {
$db->insert(
'freshmail_emails_to_synchronize',
[
'hash_list' => $hashList,
'email' => $customer['email'],
'name' => $customer['name']
]
);
}
}
public function pingAsyncJobStatus($job)
{
if (is_int($job)) {
$job = (new AsyncJobs(Db::getInstance()))->findByIdJob($job);
}
if (!Validate::isLoadedObject($job)) {
return false;
}
/* if(!empty($job->parts)){
return true;
}*/
$response = $this->freshmailApiV2->doRequest("/rest/async_result/get", [
'id_job' => $job->id_job
]);
if ('OK' != $response['status']) {
return false;
}
if (!empty($response['data']['job_status'])
&& in_array($response['data']['job_status'], [self::SYNC_SUCCESS_ID_STATUS, self::SYNC_FAIL_ID_STATUS])
&& !empty($response['data']['parts'])) {
$job->parts = (int)$response['data']['parts'];
}
$job->job_status = (int)$response['data']['job_status'];
$job->last_sync = date('Y-m-d H:i:s');
$job->save();
}
public function getEmailsTemplates($directory_name = '')
{
$list = [];
$params = [];
if(!empty($directory_name)){
$params['directory_name'] = $directory_name;
}
try{
$response = $this->freshmailApiV2->doRequest("/rest/templates/lists", $params);
if(!empty($response['data'])){
$list = $response['data'];
}
} catch (Exception $e){}
return $list;
}
private static $templateCache = [];
private function getTemplate($hash){
if(!isset(self::$templateCache[$hash])){
try {
self::$templateCache[$hash] = $this->freshmailApiV2->doRequest("/rest/templates/template", ['hash' => $hash]);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
return self::$templateCache[$hash];
}
public function getTemplateHtml($hash){
$response = $this->getTemplate($hash);
if(isset($response['data']['content_reb'])){
return $response['data']['content_reb'];
}
return '';
}
public function getProductHtml($hash){
$response = $this->getTemplate($hash);
if(isset($response['data']['product'])){
return $response['data']['product'];
}
return '';
}
public function getForms($listHash = ''){
$response = $this->freshmailApiV2->doRequest("/rest/form_library/get");
foreach ($response['data'] as &$data){
if('popup' == $data['form_type']){
$data['list_name'] = 'Pop Up: '.$data['list_name'];
}
}
$result = $response['data'];
if(!empty($listHash)){
$result = $this->filterFormByListHash($result, $listHash);
}
return array_combine(array_column($result, 'id_hash'),$result);
}
private function filterFormByListHash($forms, $listHash){
$result = [];
foreach ($forms as $form){
if($listHash == $form['list_hash']){
$result[] = $form;
}
}
return $result;
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace FreshMail;
require_once _PS_MODULE_DIR_ . 'freshmail/lib/freshmail-api/vendor/autoload.php';
use FreshMail\Api\Client\FreshMailApiClient;
class FreshmailApiV3 extends FreshMailApiClient
{
private $bearerToken;
public function __construct(string $bearerToken)
{
$this->bearerToken = $bearerToken;
parent::__construct($bearerToken);
}
public function sendIntegrationInfo(){
$data = new class() implements \JsonSerializable {
public function jsonSerialize (){
return [
'type' => 'plugin',
'data' => [
'vendor' => 'PrestaShop',
'version' => _PS_VERSION_,
'ip' => $_SERVER['SERVER_ADDR'],
'url' => \Context::getContext()->shop->domain
]
];
}
};
try {
$response = $this->requestExecutor->post('integrations', $data);
\PrestaShopLogger::addLog('FM ( '.$this->bearerToken.' ) -> Success endpoint response code: '. $response->getStatusCode(), 1, null, null, null , true);
if( 200 == $response->getStatusCode() ){
return true;
}
} catch (\Exception $e){
\PrestaShopLogger::addLog('FM ( '.$this->bearerToken.' ) ->endpoint exception: '. $e->getMessage());
}
return false;
}
public function sendTransactionalEmail(TransactionalEmail $transactionalEmail){
try {
$response = $this->requestExecutor->post('messaging/emails', $transactionalEmail);
\PrestaShopLogger::addLog('FM ( '.$this->bearerToken.' ) -> Success endpoint response code: '. $response->getStatusCode(), 1, null, null, null , true);
if( 201 == $response->getStatusCode() ){
return true;
}
} catch (\Exception $e){
\PrestaShopLogger::addLog('FM ( '.$this->bearerToken.' ) ->endpoint exception: '. $e->getMessage());
}
return false;
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace FreshMail;
class FreshmailCode{
const ALREADY_SUBSCRIBED = 1304;
}

View File

@@ -0,0 +1,119 @@
<?php
namespace FreshMail;
use Configuration;
use Db;
use DbQuery;
use Exception;
use FreshMail\Api\Client\Service\RequestExecutor;
use FreshMail\ApiV2\Client;
use FreshMail\ApiV2\UnauthorizedException;
use FreshMail\Entity\AsyncJob;
use FreshMail\Entity\Email;
use FreshMail\Entity\EmailToSynchronize;
use Freshmail\Entity\FreshmailSetting;
use FreshMail\Repository\AsyncJobs;
use FreshMail\Repository\FreshmailSettings;
use FreshMail\Repository\Subscribers;
use Validate;
use ZipArchive;
require_once __DIR__ . '/../lib/freshmail-api/vendor/autoload.php';
class FreshmailList extends Freshmail
{
private $fmSettings = null;
public function __construct(FreshmailSetting $settings)
{
parent::__construct($settings->api_token);
$this->fmSettings = $settings;
}
public function addSubscriber($data = null)
{
if($data instanceof Subscriber){
if (empty($data->email) || empty($this->fmSettings->subscriber_list_hash)) {
throw new Exception(Configuration::get('FRESHMAIL_SUBMISSION_FAILURE_MESSAGE'));
}
$data->list = $this->fmSettings->subscriber_list_hash;
$data->confirm = $this->fmSettings->send_confirmation;
$data->state = (!$this->fmSettings->send_confirmation) ? 1 : 2;
if(!empty($data->custom_fields)){
foreach ($data->custom_fields as $name => $value){
if(!$this->hasField($name)){
unset($data->custom_fields[$name]);
}
}
}
try {
$msg['response'] = $this->freshmailApiV2->doRequest('subscriber/add', (array)$data);
$subscriber = new \FreshMail\Entity\Email();
$subscriber->email = $data->email;
$subscriber->hash_list = $data->list;
//$subscriber->status = 'Active';
$subscriber->add_date = date('Y-m-d H:i:s');
$subscriber->last_synchronization = date('Y-m-d H:i:s');
$subscriber->save();
$synchronized = new \FreshMail\Entity\EmailsSynchronized();
$synchronized->email = $data->email;
$synchronized->hash_list = $data->list;
$synchronized->save();
$msg['success'] = Configuration::get('FRESHMAIL_SUBMISSION_SUCCESS_MESSAGE');
} catch (Exception $exception) {
if ($exception->getCode() == 1304) {
$msg['error'] = Configuration::get('FRESHMAIL_ALREADY_SUBSCRIBED_MESSAGE');
} else {
$msg['error'] = Configuration::get('FRESHMAIL_SUBMISSION_FAILURE_MESSAGE');
}
}
return $msg;
} else {
return parent::addSubscriber($data);
}
}
public function deleteSubscriber($data = null)
{
if($data instanceof Subscriber){
if (empty($data->email) || empty($this->fmSettings->subscriber_list_hash)) {
throw new Exception(Configuration::get('FRESHMAIL_SUBMISSION_FAILURE_MESSAGE'));
}
$data->list = $this->fmSettings->subscriber_list_hash;
if(!empty($data->custom_fields)){
foreach ($data->custom_fields as $name => $value){
if(!$this->hasField($name)){
unset($data->custom_fields[$name]);
}
}
}
try {
$msg['response'] = $this->freshmailApiV2->doRequest('subscriber/delete', (array)$data);
$repository = new Subscribers(Db::getInstance());
$repository->deleteByEmailAndList($data->email, $data->list);
} catch (Exception $exception) {
return false;
}
return $msg;
}
}
public function hasField($tag)
{
return parent::hasFieldWithTag($this->fmSettings->subscriber_list_hash, $tag);
}
}

View File

@@ -0,0 +1,138 @@
<?php
namespace FreshMail;
use Configuration;
use FreshMail\Entity\Cart;
use FreshMail\Repository\AsyncJobs;
use FreshMail\Repository\FormRepository;
use FreshMail\Repository\FreshmailAbandonCartSettings;
use FreshMail\Repository\FreshmailSettings;
use FreshMail\Service\FormService;
use Validate;
trait Hooks
{
public function getHooks()
{
return [
'displayBackOfficeHeader',
'actionCustomerAccountAdd',
'actionDeleteGDPRCustomer',
'actionObjectCustomerDeleteAfter',
'actionObjectCartAddAfter',
'actionObjectCustomerUpdateBefore',
];
}
public function hookDisplayBackOfficeHeader($params)
{
// $this->context->controller->addCSS($this->_path . 'views/css/freshmail-core.css', 'all');
$ets = new \FreshMail\Repository\EmailToSynchronize(\Db::getInstance());
$list = $ets->getListToSync();
if(!empty($list[0])){
$this->context->smarty->assign([
'pendingSend' => true,
'sendUrl' => $this->context->link->getBaseLink(null,true).'modules/'.$this->name.'/cron/send_subscribers.php?hash='.$list[0]['hash_list'].'&token='.$this->getCronToken()
]);
}
$this->context->smarty->assign([
'base_url' => $this->context->link->getBaseLink(null,true),
]);
$aj = new AsyncJobs(\Db::getInstance());
$jobs = $aj->getRunningJobs();
if(!empty($jobs)){
Tools::asyncJobPing();
}
return $this->display(_PS_MODULE_DIR_ .'freshmail', 'views/templates/admin/header.tpl');
}
public function hookActionCustomerAccountAdd($params){
$customer = $params['newCustomer'];
if($customer->newsletter){
$this->addSubscriber($customer->email, $customer->firstname);
}
}
public function hookActionDeleteGDPRCustomer($customer)
{
$this->deleteSubscriber($customer['email']);
}
public function hookActionObjectCustomerDeleteAfter($params)
{
$this->deleteSubscriber($params['object']->email);
}
public function getFreshmailList() : FreshmailList
{
$freshmailSettings = (new FreshmailSettings())->findForShop($this->context->shop->id);
$fm = new FreshmailList($freshmailSettings);
if(empty($freshmailSettings->subscriber_list_hash) || !$fm->check()){
return false;
}
return $fm;
}
public function hookActionObjectCartAddAfter($params)
{
$cart = $params['object'];
if(
!(new FreshmailAbandonCartSettings(\Db::getInstance()))->findForShop($cart->id_shop)->enabled
){
return;
}
$fmCart = new Cart();
$fmCart->id_cart = $cart->id;
$fmCart->cart_token = sha1(time()).md5(time());
$fmCart->save();
}
public function hookActionObjectCustomerUpdateBefore($params)
{
$old = new \Customer($params['object']->id);
if($old->newsletter == $params['object']->newsletter){
return;
}
if(0 == $params['object']->newsletter){
$this->deleteSubscriber($params['object']->email);
} else {
$this->addSubscriber($params['object']->email, $params['object']->firstname);
}
}
private function deleteSubscriber($email){
if(empty($email) || !Validate::isEmail($email)){
return;
}
$fmList = $this->getFreshmailList();
if(empty($fmList)){
return;
}
$fmList->deleteSubscriber(new Subscriber($email));
}
private function addSubscriber($email, $name){
if(empty($email) || !Validate::isEmail($email)){
return;
}
$fmList = $this->getFreshmailList();
if(empty($fmList)){
return;
}
$subscriber = new Subscriber($email);
$subscriber->custom_fields[\Freshmail::NAME_TAG] = $name;
$fmList->addSubscriber($subscriber);
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace FreshMail;
use FreshMail\Repository\FormRepository;
trait HooksForms{
public function getHooksForm()
{
return [
'displayTop',
'displayLeftColumnProduct',
'displayRightColumnProduct',
'displayFooterProduct',
'displayHome',
'displayHeader',
'freshmailForm'
];
}
public function hookDisplayTop($params)
{
return $this->renderForms('displayTop');
}
public function hookDisplayLeftColumnProduct($params)
{
return $this->renderForms('displayLeftColumnProduct');
}
public function hookDisplayRightColumnProduct($params)
{
return $this->renderForms('displayRightColumnProduct');
}
public function hookDisplayFooterProduct($params)
{
return $this->renderForms('displayFooterProduct');
}
public function hookDisplayHome($params)
{
return $this->renderForms('displayHome');
}
public function hookDisplayHeader($params)
{
return
$this->renderForms('pop_up') .
$this->renderForms('displayHeader');
}
public function hookFreshmailForm($params)
{
return $this->renderForms('freshmailForm');
}
private function renderForms($hook)
{
$forms = (new FormRepository(\Db::getInstance()))->getByHooks(
$this->context->shop->id,
$hook
);
$fs = (new \FreshMail\Repository\FreshmailSettings(\Db::getInstance()))->findForShop($this->context->shop->id);
if(empty($fs->api_token)){
return '';
}
$html = '';
foreach($forms as $form){
$html .= $this->cache($fs->api_token, $form['form_hash']);
}
return $html;
}
private function cache($apiToken, $formHash){
$cache_file =_PS_CACHE_DIR_.'freshmailform_'.$formHash;
$form = '';
if (file_exists($cache_file) && (filemtime($cache_file) > (time() - \Freshmail::CACHE_FORM_LIFETIME ))) {
$form = file_get_contents($cache_file);
} else {
$fm = new \FreshMail\Freshmail($apiToken);
$fmForms = $fm->getForms();
if(!empty($fmForms[$formHash])){
$form = $fmForms[$formHash]['html_code'];
}
$tmp_file = _PS_CACHE_DIR_.md5( time().$cache_file );
file_put_contents($tmp_file, $form, LOCK_EX);
rename( $tmp_file, $cache_file);
}
return $form;
}
public function clearFormCache(){
foreach (scandir(_PS_CACHE_DIR_) as $file){
if(false !== strpos($file, 'freshmailform_')){
unlink(_PS_CACHE_DIR_. $file);
}
}
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace FreshMail;
use Validate;
class Subscriber
{
public $email = '';
public $custom_fields = [];
public $state = 1;
public $list = '';
public function __construct($email)
{
if (Validate::isEmail($email)) {
$this->email = $email;
}
}
public function addCustomField($name, $value)
{
$this->custom_fields[$name] = $value;
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace FreshMail;
use ArrayObject;
use Exception;
class SubscriberCollection extends ArrayObject
{
public function append($value)
{
if ($value instanceof Subscriber) {
parent::append($value); // TODO: Change the autogenerated stub
} else {
throw new Exception('value isn\'t Subscriber class');
}
}
}

View File

@@ -0,0 +1,283 @@
<?php
namespace FreshMail;
use Configuration;
use Context;
use Db;
use DbQuery;
use Exception;
use FreshMail\Entity\Email;
use FreshMail\Entity\EmailToSynchronize;
use Mail;
use PrestaShop\PrestaShop\Core\Addon\Module\ModuleManagerBuilder;
class Tools
{
public static function setShopSmtp($apiKey)
{
Configuration::updateValue('PS_MAIL_METHOD', 2);
Configuration::updateValue('PS_MAIL_SERVER', 'smtp.freshmail.com');
Configuration::updateValue('PS_MAIL_USER', 'smtp@freshmail.com');
Configuration::updateValue('PS_MAIL_PASSWD', $apiKey);
Configuration::updateValue('PS_MAIL_SMTP_ENCRYPTION', 'tls');
Configuration::updateValue('PS_MAIL_SMTP_PORT', '587');
}
public static function getSpecificPriceRules($idShop)
{
$query = new DbQuery();
$query
->select('id_specific_price_rule, name ')
->from('specific_price_rule', 's')
->where('id_shop = ' . (int)$idShop)
->orderBy('name');
return Db::getInstance()->executeS($query);
}
public static function sendWizardSuccessMail()
{
$context = Context::getContext();
$employee = Context::getContext()->employee;
return Mail::Send(
$context->language->id,
'wizard_success',
$context->getTranslator()->trans(
'Welcome!',
array(),
'Modules.Freshmail.Emails.'
),
array(
'{firstname}' => $employee->firstname,
'{lastname}' => $employee->lastname,
'{email}' => $employee->email,
),
$employee->email,
$employee->firstname . ' ' . $employee->lastname,
null,
null,
null,
null,
_PS_MODULE_DIR_ . 'freshmail/mails'
);
}
public static function checkTmpDir()
{
if (!is_dir(\Freshmail::TMP_DIR)) {
mkdir(\Freshmail::TMP_DIR);
}
if (!is_writeable(\Freshmail::TMP_DIR)) {
throw new Exception('Error: Directory ' . \Freshmail::TMP_DIR . ' not writeable by webserver.');
}
}
public static function addEmailsToSynchronize(int $idShop, $hashList)
{
$sql = str_replace(
['PREFIX_', '##HASH##', '##ID_SHOP##'],
[_DB_PREFIX_, pSQL($hashList), $idShop],
'INSERT IGNORE INTO PREFIX_freshmail_emails_to_synchronize (`email`, `name`, `hash_list`)
SELECT `email`, `firstname`, "##HASH##"
FROM PREFIX_customer
WHERE id_shop = ##ID_SHOP##
AND newsletter = 1
AND `email` NOT IN (SELECT email FROM PREFIX_freshmail_emails_synchronized WHERE hash_list = "##HASH##")
'
);
Db::getInstance()->execute($sql);
$moduleManagerBuilder = ModuleManagerBuilder::getInstance();
$moduleManager = $moduleManagerBuilder->build();
if ($moduleManager->isInstalled('ps_emailsubscription') && $moduleManager->isEnabled('ps_emailsubscription')) {
$sql = str_replace(
['PREFIX_', '##HASH##', '##ID_SHOP##'],
[_DB_PREFIX_, pSQL($hashList), $idShop],
'INSERT IGNORE INTO PREFIX_freshmail_emails_to_synchronize (`email`, `hash_list`)
SELECT `email`, "##HASH##"
FROM PREFIX_emailsubscription
WHERE id_shop = ##ID_SHOP##
AND active = 1
AND `email` NOT IN (SELECT email FROM PREFIX_freshmail_emails_synchronized WHERE hash_list = "##HASH##")
'
);
Db::getInstance()->execute($sql);
}
}
public static function getEmailsToSynchronize($hashList, $limit = 100)
{
$query = new DbQuery();
$query
->select('*, 1 as state')
->from('freshmail_emails_to_synchronize')
->where('hash_list = "' . pSQL($hashList) . '"');
if (!empty($limit)) {
$query->limit($limit);
}
return Db::getInstance()->executeS($query);
}
public static function convertToSubcriberCollection(array $data): SubscriberCollection
{
$collection = new SubscriberCollection();
foreach ($data as $item) {
$state = !empty($item['state']) ? (int)$item['state'] : 0;
$subscriber = new Subscriber($item['email']);
$subscriber->addCustomField('imie', $item['name'] );
$subscriber->state = $state;
$collection->append($subscriber);
}
return $collection;
}
public static function importSubscribersFromCsv($listHash, $dir)
{
$settings = (new \FreshMail\Repository\FreshmailSettings(Db::getInstance()))->getByHash($listHash);
foreach (scandir(\Freshmail::TMP_DIR . $dir) as $importFile) {
$filePath = \Freshmail::TMP_DIR . $dir . '/' . $importFile;
if (is_file($filePath)) {
$fh = fopen($filePath, 'r+');
while ($line = fgetcsv($fh, 0, ';')) {
Email::addBySimpleInsert($listHash, $line);
Email::updateStatusInShop($line, $settings);
}
}
}
}
public static function removeEmailsFromSynchronization($hashList, SubscriberCollection $subscribers)
{
foreach ($subscribers as $subscriber){
Db::getInstance()->delete(
'freshmail_emails_to_synchronize',
'hash_list = "'.pSQL($hashList).'" AND email = "'.pSQL($subscriber->email).'"',
1
);
}
}
public static function triggerSynchronization($listHash){
$url = Context::getContext()->link->getBaseLink().'modules/freshmail/cron/synchronize.php?hash='.$listHash;
self::sendRequest($url);
}
public static function asyncJobPing(){
$url = Context::getContext()->link->getBaseLink().'modules/freshmail/cron/synchronize_subscribers.php';
self::sendRequest($url);
}
public static function sendRequest($url){
$module = \Module::getInstanceByName('freshmail');
$ch = curl_init();
$glue = false !== strpos($url, '?') ? '&' : '?';
$url .= $glue . 'token='.$module->getCronToken();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,1);
curl_exec($ch);
curl_close($ch);
}
public static function getGuestEmailByToken($token)
{
$sql = 'SELECT `email`
FROM `'._DB_PREFIX_.'emailsubscription`
WHERE MD5(CONCAT( `email` , `newsletter_date_add`, \''.pSQL(Configuration::get('NW_SALT')).'\')) = \''.pSQL($token).'\'';
return Db::getInstance()->getValue($sql);
}
public static function getUserEmailByToken($token)
{
$sql = 'SELECT `email`
FROM `'._DB_PREFIX_.'customer`
WHERE MD5(CONCAT( `email` , `date_add`, \''.pSQL(Configuration::get('NW_SALT')).'\')) = \''.pSQL($token).'\'';
return Db::getInstance()->getValue($sql);
}
public static function checkDirPermission($dir)
{
$separator = substr($dir,-1) == '/' ? '' : '/';
$file = md5(time());
$result = file_put_contents($dir. $separator .$file, time());
if(!$result){
return false;
}
unlink($dir.'/'.$file);
return true;
}
public static function clearShopSettings($idShop)
{
$fs = (new \FreshMail\Repository\FreshmailSettings(Db::getInstance()))->findForShop($idShop);
$sql = [
'DELETE FROM PREFIX_freshmail_setting WHERE id_shop = '.(int)$idShop,
'DELETE FROM PREFIX_freshmail_form WHERE id_shop = '.(int)$idShop,
'DELETE FROM PREFIX_freshmail_async_job WHERE hash_list = "'.pSQL($fs->subscriber_list_hash).'"',
];
foreach ($sql as $query){
Db::getInstance()->execute(
str_replace( 'PREFIX_', _DB_PREFIX_, $query)
);
}
}
public static function writeEmailTpl($body, $lang)
{
$tpl = str_replace(
'[[BODY]]',
$body,
file_get_contents(_PS_MODULE_DIR_.'freshmail/views/templates/email/abandoned-cart.tpl')
);
if(!is_dir(_PS_MODULE_DIR_.'freshmail/mails/'.$lang.'/')){
try {
mkdir(_PS_MODULE_DIR_ . 'freshmail/mails/' . $lang . '/');
} catch (Exception $e){
throw new Exception(_PS_MODULE_DIR_.'freshmail/mails/'.$lang.'/abandoned-cart.html');
}
}
if( false === file_put_contents(_PS_MODULE_DIR_.'freshmail/mails/'.$lang.'/abandoned-cart.html', $tpl)){
throw new Exception(_PS_MODULE_DIR_.'freshmail/mails/'.$lang.'/abandoned-cart.html');
}
}
public static function is_cli()
{
if( defined('STDIN') )
{
return true;
}
if( empty($_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0)
{
return true;
}
return false;
}
public static function filterTplByCategory(&$list, $category){
foreach ($list as $k => $item){
if($category != $item['category']){
unset($list[$k]);
}
}
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace FreshMail;
use FreshMail\Sender\Email;
class TransactionalEmail implements \JsonSerializable{
private $recipient;
private $from;
private $subject;
private $content;
public function __construct(Email $recipient, Email $from, $subject, $content)
{
$this->recipient = $recipient;
$this->from = $from;
$this->subject = $subject;
$this->content = $content;
}
public function jsonSerialize()
{
return [
'recipients' => [
[
'email' => $this->recipient->email,
'name' => $this->recipient->name
]
],
'from' => [
'email' => $this->from->email,
'name' => $this->from->name,
],
'subject' => $this->subject,
'contents' => [
[
'type' => 'text/html',
'body' => $this->content
]
]
];
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace FreshMail\Discount;
use FreshMail\Entity\Cart;
abstract class AbstractDiscount{
protected $settings;
public function __construct(\Freshmail\Entity\AbandonedCartSettings $settings)
{
$this->settings = $settings;
}
abstract function apply(\Cart $cart, Cart $fmCart);
}

View File

@@ -0,0 +1,13 @@
<?php
namespace FreshMail\Discount;
use FreshMail\Entity\Cart;
class Custom extends AbstractDiscount{
function apply(\Cart $cart, Cart $fmCart){
$fmCart->discount_code = $this->settings->discount_code;
$fmCart->save();
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace FreshMail\Discount;
use FreshMail\Entity\Cart;
class None extends AbstractDiscount{
function apply(\Cart $cart, Cart $fmCart)
{
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace FreshMail\Discount;
use FreshMail\Entity\Cart;
use FreshMail\Repository\Carts;
class Percent extends AbstractDiscount{
function apply(\Cart $cart, Cart $fmCart){
if(!empty($fmCart->id_cart_rule)){
return;
}
$cartRule = new \CartRuleCore();
$cartRule->date_from = date("Y-m-d H:i:s");
$cartRule->date_to = date('Y-m-d H:i:s', strtotime('+'.$this->settings->discount_lifetime.' HOURS'));
$cartRule->free_shipping = false;
$cartRule->minimum_amount_currency = 1;
$cartRule->reduction_percent = (int)$this->settings->discount_percent;
$cartRule->reduction_tax = true;
$cartRule->reduction_currency = $cart->id_currency;
$cartRule->active = true;
$cartRule->partial_use = false;
$cartRule->code = 'fm_'.substr(md5(time()), 0,10);
foreach (\Language::getLanguages() as $lang)
{
$name = \Module::getInstanceByName('freshmail')->l('Discount for abandoned cart');
if(empty($name)){
$name = 'FreshMail';
}
$cartRule->name[$lang['id_lang']] = $name;
}
$cartRule->save();
$fmCart->id_cart_rule = $cartRule->id;
$fmCart->save();
}
}

View File

@@ -0,0 +1,175 @@
<?php
namespace FreshMail;
use Db;
use FreshMail\Entity\Hook;
use FreshMail\Installer\Tabs;
use Language;
use Module;
use Tab;
class Installer implements Interfaces\Installer
{
use Hooks;
use HooksForms;
private $module;
public function __construct(Module $module)
{
$this->module = $module;
}
public function install(): bool
{
$return = $this->installMenuAdmin();
foreach ($this->getHooks() as $hook) {
$return &= $this->module->registerHook($hook);
}
foreach ($this->getHooksForm() as $hook) {
$return &= $this->module->registerHook($hook);
}
return $return
&& $this->loadSQLFile(__DIR__ . '/../../install/install.sql')
// && $this->initData()
;
}
public function uninstall(): bool
{
$result = true;
foreach (Tabs::getTabs('extended') as $tab) {
$result &= $this->uninstallTab($tab['controller']);
}
return $result
&& $this->uninstallTab('AdminFreshmailWizard')
&& $this->uninstallTab('AdminFreshmail')
&& $this->loadSQLFile(__DIR__ . '/../../install/uninstall.sql');
}
public function installMenuAdmin()
{
return $this->installTab('AdminFreshmail', 'FRESHMAIL')
&& $this->installTab('AdminFreshmailWizard', 'AdminFreshmailWizard', false, true)
&& Tabs::install($this->module);
}
public function installTab($controllerClassName, $tabName, $tabParentControllerName = false, $withoutTab = false, $icon = '')
{
$idTab = (int)Tab::getIdFromClassName($controllerClassName);
if(!empty($idTab)){
return true;
}
$tab = new Tab();
$tab->active = 1;
$tab->class_name = $controllerClassName;
$tab->name = [];
$tab->icon = $icon;
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = $this->module->l($tabName);
}
if ($withoutTab) {
$tab->id_parent = -1;
} elseif ($tabParentControllerName) {
$tab->id_parent = (int)Tab::getIdFromClassName($tabParentControllerName);
} else {
$tab->id_parent = 0;
}
$tab->module = $this->module->name;
return $tab->add();
}
/**
* @param $controllerClassName
* @return bool
*/
public function uninstallTab($controllerClassName)
{
$idTab = (int)Tab::getIdFromClassName($controllerClassName);
if ($idTab) {
$tab = new Tab($idTab);
return $tab->delete();
}
return true;
}
public function loadSQLFile($sqlFile)
{
$sqlContent = file_get_contents($sqlFile);
$sqlContent = str_replace('PREFIX_', _DB_PREFIX_, $sqlContent);
$sqlRequests = preg_split("/;\s*[\r\n]+/", $sqlContent);
$result = true;
foreach ($sqlRequests as $request) {
if (!empty($request)) {
$result &= Db::getInstance()->execute(trim($request));
}
}
return $result;
}
public function initData(){
$hooks = [
'displayFooterProduct' => [
'title' => 'Product footer',
'description' => 'This hook adds new blocks under the product\'s description'
],
'displayHeader' => [
'title' => 'Added in the header of every page',
'description' => 'This hook adds new blocks in header'
],
'displayHome' => 'Displayed on the content of the home page.',
[
'title' => 'Homepage content',
'description' => 'This hook displays new elements on the homepage'
],
'displayRightColumnProduct' => [
'title' => 'New elements on the product page (right column)',
'description' => 'This hook displays new elements in the right-hand column of the product page'
],
'displayLeftColumnProduct' => [
'title' => 'New elements on the product page (left column)',
'description' => 'This hook displays new elements in the left-hand column of the product page'
],
'displayTop' => [
'title' => 'Top of pages',
'description' => 'This hook displays additional elements at the top of your pages'
],
'pop_up' => [
'title' => 'Pop up form',
'description' => 'It\'s only for pop up forms'
],
'freshmail_form' => [
'title' => 'Custom hook',
'description' => 'You can add this hook in every place You want'
]
];
$return = true;
foreach ($hooks as $hook => $item){
$obj = new Hook();
$obj->hook_name = $hook;
$obj->title = $this->module->l($item['title']);
$obj->description = $this->module->l($item['description']);
$return &= $obj->save();
}
return $return;
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace FreshMail\Installer;
use FreshMail\Interfaces\Installer;
use Module;
class InstallerFactory
{
public static function getInstaller(Module $module): Installer
{
return new \FreshMail\Installer($module);
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace FreshMail\Installer;
class Tabs
{
private static function base(){
return [
[ 'controller' => 'AdminFreshmailConfig', 'name' => 'Configuration', 'icon' => 'settings', ],
[ 'controller' => 'AdminFreshmailAbandonedCartConfig', 'name' => 'Abandoned cart', 'icon' => 'shopping_cart'],
];
}
private function extended(){
return [
[ 'controller' => 'AdminFreshmailSubscribers', 'name' => 'List of subscribers', 'icon' => 'list'],
[ 'controller' => 'AdminFreshmailFormConfig', 'name' => 'Forms', 'icon' => 'settings_applications'],
[ 'controller' => 'AdminFreshmailBirthday', 'name' => 'Birthday emails', 'icon' => 'settings_applications'],
];
}
public static function getTabs($type = 'base'){
$tabs = self::base();
if('extended' == $type){
$tabs = array_merge($tabs, self::extended());
}
return $tabs;
}
public static function install(\Module $module, $type = 'base'){
$tabs = self::getTabs($type);
$installer = InstallerFactory::getInstaller($module);
$result = true;
foreach ($tabs as $tab){
$parent = (isset($tab['parent'])) ? $tab['parent'] : 'AdminFreshmail';
$result &= $installer->installTab($tab['controller'], $tab['name'], $parent, false, $tab['icon']);
}
return $result;
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace FreshMail\Sender;
use Freshmail\Entity\AbandonedCartSettings;
use FreshMail\Entity\Cart;
use Freshmail\Entity\FreshmailSetting;
use FreshMail\Repository\FreshmailAbandonCartSettings;
use FreshMail\Sender\Service\CartDataCollector;
class AbstractSender{
protected $fmSettings = null;
protected $cartSettings = null;
public function __construct(FreshmailSetting $fmSettings, FreshmailAbandonCartSettings $cartSettings)
{
$this->fmSettings = $fmSettings;
$this->cartSettings = $cartSettings;
}
public static function getProductsLegacy(\Context $context, $products)
{
$context->smarty->assign('list', $products);
return $context->smarty->fetch(_PS_MODULE_DIR_ . 'freshmail/views/templates/email/order_conf_product_list.tpl');
}
public static function getProductsFM($html, $products){
$output = '';
foreach ($products as $product){
$output .= str_replace(
['{product_cover}', '{product_cover_big}' , '{product_name}', '{product_quantity}', '{unit_price_tax_incl}'],
[ $product['img'], $product['img_big'], $product['name'], $product['quantity'], $product['price_wt'] ],
$html
);
}
return $output;
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace FreshMail\Sender;
use FreshMail\Freshmail;
use FreshMail\FreshmailApiV3;
use FreshMail\Repository\Birthdays;
use FreshMail\TransactionalEmail;
class Birthday
{
private $bearer_token;
public function __construct($bearer_token)
{
$this->bearer_token = $bearer_token;
}
private function getMailHtml(\FreshMail\Entity\Birthday $birthday) : string
{
if(!empty($birthday->tpl)){
$fm = new Freshmail($this->bearer_token);
$html = $fm->getTemplateHtml($birthday->tpl);
if(!empty($html)){
return $html;
}
}
return '';
}
public function send(\Customer $customer, \FreshMail\Entity\Birthday $birthday) : bool
{
$shop = new \Shop($birthday->id_shop);
$html = str_replace(
['{firstname}', '{lastname}', '{content}', '{shop_url}'],
[$customer->firstname, $customer->lastname, $birthday->content[$customer->id_lang]],
$this->getMailHtml($birthday)
);
$recipient = new Email($customer->email, $customer->firstname);
$sender = new Email(\Configuration::get('PS_SHOP_EMAIL'), $shop->name);
$fmApi = new FreshmailApiV3($this->bearer_token);
return $fmApi->sendTransactionalEmail(
new TransactionalEmail($recipient, $sender, $birthday->email_subject[$customer->id_lang], $html)
);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace FreshMail\Sender;
class Email{
public $email;
public $name;
public $lastname;
public function __construct($email, $name = '', $lastname = '')
{
$this->email = $email;
$this->name = $name;
$this->lastname = $lastname;
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace FreshMail\Sender;
use FreshMail\Discount\Percent;
use Freshmail\Entity\AbandonedCartSettings;
use Freshmail\Entity\FreshmailSetting;
use FreshMail\Interfaces\Sender;
use FreshMail\Repository\FreshmailAbandonCartSettings;
class Factory {
public static function getSender(FreshmailSetting $fmSetting, FreshmailAbandonCartSettings $cartSettings) : Sender
{
if(empty($fmSetting->api_token)) {
return new Legacy($fmSetting, $cartSettings);
} else {
return new FmSender($fmSetting, $cartSettings);
}
}
}

View File

@@ -0,0 +1,115 @@
<?php
namespace FreshMail\Sender;
use FreshMail\Entity\Cart;
use Freshmail\Entity\FreshmailSetting;
use FreshMail\Freshmail;
use FreshMail\FreshmailApiV3;
use FreshMail\Interfaces\Sender;
use FreshMail\Repository\FreshmailAbandonCartSettings;
use FreshMail\Repository\FreshmailSettings;
use FreshMail\Sender\Service\CartDataCollector;
use FreshMail\TransactionalEmail;
class FmSender extends AbstractSender implements Sender{
private $freshmailApi = null;
public function __construct(FreshmailSetting $fmSettings, FreshmailAbandonCartSettings $cartSettings)
{
parent::__construct($fmSettings, $cartSettings);
$this->freshmailApi = new Freshmail($this->fmSettings->api_token);
}
public function send(\Cart $cart, Cart $fmCart, Email $email, CartDataCollector $cartDataCollector) : bool
{
$shop = new \Shop($cart->id_shop);
$fmApi = new FreshmailApiV3($this->fmSettings->api_token);
$cs = $this->cartSettings->findForShop($cart->id_shop);
$context = \Context::getContext();
$context->cart = $cart;
$context->shop = new \Shop($cart->id_shop);
$customer = $cartDataCollector::getCustomer($cart);
if (\Configuration::get('PS_LOGO_MAIL') !== false && file_exists(_PS_IMG_DIR_.\Configuration::get('PS_LOGO_MAIL', null, null, $cart->id_shop))) {
$logo = _PS_IMG_DIR_.\Configuration::get('PS_LOGO_MAIL', null, null, $cart->id_shop);
} else {
if (file_exists(_PS_IMG_DIR_.\Configuration::get('PS_LOGO', null, null, $cart->id_shop))) {
$logo = _PS_IMG_DIR_.\Configuration::get('PS_LOGO', null, null, $cart->id_shop);
}
}
$logo_url = '';
if(file_exists($logo)){
$logo_url = $context->shop->getBaseURL() . _PS_IMG_ . \Configuration::get('PS_LOGO', null, null, $cart->id_shop);
}
$replaceFrom = [
'{products_list}',
'{discount_code}',
'{shop_name}',
'{shop_url}',
'{shop_logo}',
'{cart_url}',
'{company_name}',
'{company_address}',
'{firstname}',
'{lastname}',
'{cartrule_validto}',
'{preheader}'
];
$productsHtml = AbstractSender::getProductsFM($this->getProductHtml($cart, $fmCart), $cartDataCollector::getProductList($cart, $context));
if(empty($productsHtml)){
$productsHtml = AbstractSender::getProductsLegacy($context, $cartDataCollector::getProductList($cart, $context));
}
$replaceTo = [
$productsHtml,
$cartDataCollector::getDiscountCode($fmCart),
$shop->name, //\Configuration::get('PS_SHOP_NAME'),
$context->shop->getBaseURL(), //$context->link->getPageLink('index', true, $cart->id_lang, null, false, $cart->id_shop),
$logo_url,
$cartDataCollector::getCartUrl($fmCart),
\Configuration::get('PS_SHOP_NAME', $cart->id_lang, null, $cart->id_shop),
$cartDataCollector::getShopAddress(),
$customer->firstname,
$customer->lastname,
$cartDataCollector::getDiscountValidTo($fmCart),
$cs->email_preheader[$cart->id_lang],
];
$html = str_replace(
$replaceFrom,
$replaceTo,
$this->getMailHtml($cart, $fmCart)
);
$sender = new Email(\Configuration::get('PS_SHOP_EMAIL'), $shop->name);
return $fmApi->sendTransactionalEmail(
new TransactionalEmail($email, $sender, $cs->email_subject[$cart->id_lang], $html)
);
}
private function getMailHtml(\Cart $cart, Cart $fmCart) : string
{
$cs = $this->cartSettings->findForShop($cart->id_shop);
if(!empty($cs->template_id_hash)){
return $this->freshmailApi->getTemplateHtml($cs->template_id_hash);
}
$lang = new \Language($cart->id_lang);
return file_get_contents(_PS_MODULE_DIR_.'freshmail/mails/'.$lang->iso_code.'/abandoned-cart.html');
}
private function getProductHtml(\Cart $cart, Cart $fmCart) : string
{
$cs = $this->cartSettings->findForShop($cart->id_shop);
if(!empty($cs->template_id_hash)){
return $this->freshmailApi->getProductHtml($cs->template_id_hash);
}
return '';
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace FreshMail\Sender;
use FreshMail\Entity\Cart;
use FreshMail\Repository\Carts;
use FreshMail\Sender\Service\CartDataCollector;
class Legacy extends AbstractSender implements \FreshMail\Interfaces\Sender{
public function send(\Cart $cart, Cart $fmCart, Email $email, CartDataCollector $cartDataCollector) : bool
{
$context = \Context::getContext();
$context->cart = $cart;
$context->shop = new \Shop($cart->id_shop);
$customer = new \Customer($cart->id_customer);
$vars = [
'{products_list}' => AbstractSender::getProductsLegacy($context, $cartDataCollector::getProductList($cart, $context)),
'{discount_code}' => $cartDataCollector::getDiscountCode($fmCart),
'{cart_url}' => $cartDataCollector::getCartUrl($fmCart),
'{company_name}' => \Configuration::get('PS_SHOP_NAME'),
'{company_address}' => $cartDataCollector::getShopAddress(),
'{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname,
'{cartrule_validto}' => $cartDataCollector::getDiscountValidTo($fmCart),
];
$cs = $this->cartSettings->findForShop($cart->id_shop);
return \Mail::send(
$cart->id_lang,
'abandoned-cart',
$cs->email_subject[$cart->id_lang],
$vars,
$email->email,
$email->name,
null,
null,
null,
null,
_PS_MODULE_DIR_.'freshmail/mails/'
);
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace FreshMail\Sender\Service;
use FreshMail\Entity\Cart;
use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter;
class CartData implements CartDataCollector
{
public static function getProductList(\Cart $cart, \Context $context)
{
$priceFormatter = new PriceFormatter();
$productList = $cart->getProducts();
foreach ($productList as &$prod) {
$prod['img'] = $context->link->getImageLink($prod['name'], \Product::getCover($prod['id_product'], $context)['id_image'], 'cart_default');
$prod['img_big'] = $context->link->getImageLink($prod['name'], \Product::getCover($prod['id_product'], $context)['id_image'], 'home_default');
$prod['price_wt'] = $priceFormatter->format($prod['price_wt'], new \Currency($cart->id_currency));
}
return $productList;
}
public static function getDiscountCode(Cart $fmCart)
{
$discountCode = '';
if (!empty($fmCart->id_cart_rule)) {
$discountCode = (new \CartRule($fmCart->id_cart_rule))->code;
} elseif (!empty($fmCart->discount_code)) {
$discountCode = $fmCart->discount_code;
}
return $discountCode;
}
public static function getDiscountValidTo(Cart $fmCart)
{
if (!empty($fmCart->id_cart_rule)) {
$id = $fmCart->id_cart_rule;
} elseif (!empty($fmCart->discount_code)) {
$id = \CartRule::getIdByCode($fmCart->discount_code);
}
if (empty($id)) {
return '';
}
$cr = new \CartRule($id);
if (!\Validate::isLoadedObject($cr)) {
return '';
}
return $cr->date_to;
}
public static function getCartUrl(Cart $fmCart)
{
return \Context::getContext()->link->getModuleLink('freshmail', 'restore', ['hash' => $fmCart->cart_token]);
}
public static function getShopAddress()
{
$city = \Configuration::get('PS_SHOP_CITY');
$postal = \Configuration::get('PS_SHOP_CODE');
$addr1 = \Configuration::get('PS_SHOP_ADDR1');
$addr2 = \Configuration::get('PS_SHOP_ADDR2');
return $city . ' ' . $postal . ' ' . $addr1 . ' ' . $addr2;
}
public static function getCustomer(\Cart $cart) : \Customer
{
return new \Customer($cart->id_customer);
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace FreshMail\Sender\Service;
use FreshMail\Entity\Cart;
interface CartDataCollector
{
public static function getProductList(\Cart $cart, \Context $context);
public static function getDiscountCode(Cart $fmCart);
public static function getDiscountValidTo(Cart $fmCart);
public static function getCartUrl(Cart $fmCart);
public static function getShopAddress();
public static function getCustomer(\Cart $cart) : \Customer;
}

View File

@@ -0,0 +1,80 @@
<?php
namespace FreshMail\Sender\Service;
use FreshMail\Entity\Cart;
use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter;
class MockCartData implements CartDataCollector
{
public static function getProductList(\Cart $cart, \Context $context)
{
$query = new \DbQuery();
$query->select('p.*, pl.*')
->from('product', 'p')
->leftJoin('product_lang', 'pl', 'p.id_product = pl.id_product AND pl.id_lang = '.(int)$context->language->id)
->leftJoin('product_sale', 'ps', 'p.id_product = ps.id_product')
->where('p.active = 1')
->where('p.available_for_order = 1')
->orderBy('ps.quantity desc')
->limit(1)
;
$productList = \Db::getInstance()->executeS($query);
$priceFormatter = new PriceFormatter();
foreach ($productList as &$prod) {
$prod['name'] = \Product::getProductName($prod['id_product']);
$prod['quantity'] = 1;
$prod['price_wt'] = $priceFormatter->format(
\Product::getPriceStatic((int)$prod['id_product'], true),
new \Currency($cart->id_currency)
);
$prod['img'] = $context->link->getImageLink($prod['name'], \Product::getCover($prod['id_product'], $context)['id_image'], 'cart_default');
$prod['img_big'] = $context->link->getImageLink($prod['name'], \Product::getCover($prod['id_product'], $context)['id_image'], 'home_default');
}
return $productList;
}
public static function getDiscountCode(Cart $fmCart)
{
return 'PROMO_CODE';
}
public static function getDiscountValidTo(Cart $fmCart)
{
return 'YYYY-MM-DD';
}
public static function getCartUrl(Cart $fmCart)
{
return \Context::getContext()->link->getModuleLink('freshmail', 'restore', ['hash' => $fmCart->cart_token]);
}
public static function getShopAddress()
{
$city = \Configuration::get('PS_SHOP_CITY');
$postal = \Configuration::get('PS_SHOP_CODE');
$addr1 = \Configuration::get('PS_SHOP_ADDR1');
$addr2 = \Configuration::get('PS_SHOP_ADDR2');
return $city . ' ' . $postal . ' ' . $addr1 . ' ' . $addr2;
}
public static function getCustomer(\Cart $cart) : \Customer
{
$query = new \DbQuery();
$query->select('DISTINCT(firstname)')->from('customer')->limit(100);
$firstame = \Db::getInstance()->executeS($query);
$query = new \DbQuery();
$query->select('DISTINCT(lastname)')->from('customer')->limit(100);
$lastname = \Db::getInstance()->executeS($query);
$customer = new \Customer();
$customer->firstname = $firstame[array_rand($firstame)]['firstname'];
$customer->lastname = $lastname[array_rand($lastname)]['lastname'];
return $customer;
}
}

View File

@@ -0,0 +1,26 @@
{
"name": "vendor_name/freshmail",
"description": "description",
"minimum-stability": "stable",
"license": "MIT",
"authors": [
{
"name": "FreshMail",
"email": "pomoc@freshmail.pl"
}
],
"config": {
"vendor-dir": "lib"
},
"autoload": {
"classmap": [
"classes",
"controllers",
"interfaces",
"src"
],
"psr-4": {
"FreshMail\\": "src/"
}
}
}

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>freshmail</name>
<displayName><![CDATA[Freshmail]]></displayName>
<categoryName><![CDATA[Newsletter & SMS]]></categoryName>
<additional_description>sadasdsad</additional_description>
<badges>
<badge label="Made by PrestaShop">
<![CDATA[https://medias2.prestastore.com/themes/prestastore/img/front/sprites/badges/made-by-prestashop.png]]></badge>
</badges>
<cover>
<small><![CDATA[https://medias2.prestastore.com/1295024-pprod/pages-not-found.jpg]]></small>
<big><![CDATA[https://medias2.prestastore.com/1295024-pbig/pages-not-found.jpg]]></big>
</cover>
<version><![CDATA[1.1.2]]></version>
<description><![CDATA[Adds a "Customers who bought this product also bought..." section to every product page.]]></description>
<author><![CDATA[PrestaShop]]></author>
<is_configurable>1</is_configurable>
<need_instance>1</need_instance>
<limited_countries></limited_countries>
</module>

View File

@@ -0,0 +1,13 @@
services:
_defaults:
public: true
FreshMail.Service.Form:
class: FreshMail\Service\FormService
arguments:
- '@FreshMail_Repository_Form'
public: true
FreshMail_Repository_Form:
class: FreshMail\Repository\FormRepository
public: true

View File

@@ -0,0 +1,7 @@
imports:
- { resource: ../common.yml }
services:
_defaults:
public: true

View File

@@ -0,0 +1,5 @@
your_route_name:
path: freshmail/demo
methods: [GET]
defaults:
_controller: 'FreshMail\Controller\Admin\DemoController::demoAction'

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>freshmail</name>
<displayName><![CDATA[FreshMail dla PrestaShop]]></displayName>
<version><![CDATA[3.10.7]]></version>
<description><![CDATA[Synchronizuje subskrybent&oacute;w newslettera i pozwala wsp&oacute;łpracować z narzędziami FreshMail]]></description>
<author><![CDATA[FreshMail]]></author>
<tab><![CDATA[front_office_features]]></tab>
<confirmUninstall><![CDATA[Jesteś pewien, że chcesz odinstalować?]]></confirmUninstall>
<is_configurable>1</is_configurable>
<need_instance>1</need_instance>
<limited_countries></limited_countries>
</module>

View File

@@ -0,0 +1,171 @@
<?php
require_once __DIR__.'/AdminFreshmailBase.php';
class AdminFreshmailAbandonedCartConfigController extends AdminFreshmailBaseController
{
const TPL_DIR = 'PrestaShop';
const TPL_CATEGORY = 3;
private $cart_config;
public function __construct()
{
$this->bootstrap = true;
parent::__construct();
$this->template = 'cart_config.tpl';
$this->override_folder = '/';
$this->cart_config = (new \FreshMail\Repository\FreshmailAbandonCartSettings(Db::getInstance()))->findForShop($this->context->shop->id);
}
public function run()
{
if(Tools::getIsset('ajax')){
return $this->ajax();
}
return parent::run();
}
private function ajax(){
$action = Tools::getValue('action');
$availableActions = ['set', 'getTpl', 'test'];
$result = [
'success' => false,
'message' => sprintf('Action %s nof found', $action)
];
if(in_array($action, $availableActions) && method_exists($this, $action)) {
$result = $this->$action();
}
die(json_encode($result));
}
public function getTpl()
{
parent::init();
$tplList = [];
if(!empty($this->freshmail) && $this->freshmail->check()){
$tplList = $this->freshmail->getEmailsTemplates(self::TPL_DIR);
\FreshMail\Tools::filterTplByCategory($tplList, self::TPL_CATEGORY);
}
die(
json_encode([
'template' => json_decode($this->cart_config->template),
'tpl_list' => $tplList
])
);
}
public function init(){
parent::init();
$idShop = $this->context->shop->id;
if (Configuration::get('PS_LOGO_MAIL') !== false && file_exists(_PS_IMG_DIR_.Configuration::get('PS_LOGO_MAIL', null, null, $idShop))) {
$logo = _PS_IMG_.Configuration::get('PS_LOGO_MAIL', null, null, $idShop);
} else {
if (file_exists(_PS_IMG_DIR_.Configuration::get('PS_LOGO', null, null, $idShop))) {
$logo = _PS_IMG_.Configuration::get('PS_LOGO', null, null, $idShop);
} else {
$logo = '';
}
}
$logo = $this->context->shop->getBaseURL(true).ltrim($logo, '/');
if(!Validate::isLoadedObject($this->cart_config)){
$idLang = Configuration::get('PS_LANG_DEFAULT');
$this->cart_config->email_preheader[$idLang] = $this->module->l('🛒 Complete your purchases!');
$this->cart_config->email_subject[$idLang] = $this->module->l('You have unfinished purchases in your cart.');
}
$this->context->smarty->assign([
'cart_config' => $this->cart_config,
'links' => $this->getLinks(),
'logo' => $logo,
'is_logged' => !empty($this->freshmail) && $this->freshmail->check(),
'id_lang' => Configuration::get('PS_LANG_DEFAULT')
]);
}
private function getLinks()
{
return [
'base_url' => $this->context->link->getBaseLink(null, true),
'cron_url' => $this->context->link->getBaseLink(null, true).'modules/freshmail/cron/abandoned_cart.php?token='.$this->module->getCronToken(),
'cron_cli' => _PS_MODULE_DIR_ . 'freshmail/cron/abandoned_cart.php'
];
}
private function set(){
$config = json_decode(Tools::getValue('config'));
$this->cart_config->enabled = $config->emails == 'true' ? 1 : 0;
$this->cart_config->id_shop = $this->context->shop->id;
$this->cart_config->template = Tools::getValue('template');
$this->cart_config->discount_percent = (int)$config->discount_percent_value;
$this->cart_config->discount_code = pSQL($config->discount_custom_value);
$this->cart_config->discount_type = pSQL($config->discount);
$this->cart_config->template_id_hash = pSQL($config->template_id_hash);
$this->cart_config->send_after = (int)$config->send_after;
$this->cart_config->discount_lifetime = (int)$config->discount_percent_livetime;
$this->cart_config->email_subject = pSQL($config->email_subject);
$this->cart_config->email_preheader = pSQL($config->email_preheader);
$this->cart_config->save();
try {
foreach (Language::getIsoIds() as $lang) {
\FreshMail\Tools::writeEmailTpl(rawurldecode(Tools::getValue('html')), $lang['iso_code']);
}
} catch (Exception $e){
return [
'success' => false,
'message' => $this->module->l('Unable to write file: '.$e->getMessage())
];
}
return [
'success' => true,
'message' => $this->module->l('Configuration saved')
];
}
private function test(){
$testEmail = Tools::getValue('email');
if(!Validate::isEmail($testEmail)){
return [
'success' => false,
'message' => sprintf('Email %s is not valid', $testEmail)
];
}
$abandonRepository = new \FreshMail\Repository\FreshmailAbandonCartSettings(Db::getInstance());
$activeAbandon = $abandonRepository->getActive();
$cartsRepository = new \FreshMail\Repository\Carts(Db::getInstance());
$settingsRepository = new \FreshMail\Repository\FreshmailSettings(Db::getInstance());
$cartService = new \FreshMail\Service\AbandonCartService($cartsRepository, $settingsRepository, $abandonRepository);
foreach ($activeAbandon as $abandon) {
$settings = new \Freshmail\Entity\AbandonedCartSettings($abandon['id_freshmail_cart_setting']);
$cart = new Cart();
$cart->id_shop = $this->context->shop->id;
$cart->id_lang = $this->context->language->id;
$cart->id_currency = Configuration::get('PS_CURRENCY_DEFAULT');
$fmCart = new \FreshMail\Entity\Cart();
$email = new \FreshMail\Sender\Email($testEmail);
$cartService->sendNotifications($cart, $fmCart, $email, new \FreshMail\Sender\Service\MockCartData());
}
return [
'success' => true,
'message' => $this->module->l('Test e-mail was sent')
];
}
}

View File

@@ -0,0 +1,167 @@
<?php
use FreshMail\ApiV2\UnauthorizedException;
use FreshMail\Repository\FreshmailSettings;
class AdminFreshmailAjaxController extends ModuleAdminController
{
private $settingRepository;
const NEW_LIST_KEY = 'create_new';
public function __construct()
{
$this->bootstrap = true;
parent::__construct();
$this->templateFile = _PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/wizard.tpl';
$this->settingRepository = new FreshmailSettings();
}
public function run()
{
if ($step = Tools::getValue('step')) {
if (method_exists($this, $step)) {
echo json_encode($this->$step());
}
}
$this->context->smarty->assign([
'is_wizard_available' => $this->isWizardAvailable(),
'links' => $this->getLinks(),
'specific_price_rules' => \FreshMail\Tools::getSpecificPriceRules($this->context->shop->id)
]);
return $this->context->smarty->fetch($this->templateFile);
}
private function getLinks()
{
return [
'settings' => $this->context->link->getAdminLink('AdminFreshmailWizard', true, [], ['step' => 'settings']),
'connect' => $this->context->link->getAdminLink('AdminFreshmailWizard', true, [], ['step' => 'connect']),
'save' => $this->context->link->getAdminLink('AdminFreshmailWizard', true, [], ['step' => 'save']),
'lists' => $this->context->link->getAdminLink('AdminFreshmailWizard', true, [], ['step' => 'getLists']),
'register_url' => 'https://app.freshmail.com/pl/auth/login',
];
}
private function isWizardAvailable()
{
return true;
}
private function settings()
{
$settings = $this->settingRepository->findForShop($this->context->shop->id);
$this->module->loadFreshmailApi();
$freshailApi = new \FreshMail\ApiV2\Client($settings->api_token);
$isLogged = false;
try {
$status = $freshailApi->doRequest('ping');
$isLogged = true;
} catch (UnauthorizedException $unauthorizedException) {
}
return [
'logged_in' => $isLogged,
'api_token' => $settings->api_token,
'smtp' => (bool)$settings->smtp,
'synchronize' => (bool)$settings->synchronize,
'id_specific_price_rule' => ((int)$settings->id_specific_price_rule > 0) ? (int)$settings->id_specific_price_rule : ''
];
}
private function connect()
{
$this->module->loadFreshmailApi();
$freshailApi = new \FreshMail\ApiV2\Client(Tools::getValue('api_token'));
try {
$freshailApi->doRequest('ping');
} catch (UnauthorizedException $unauthorizedException) {
header("HTTP/1.1 401 Unauthorized");
return [
'success' => false,
'message' => $this->l('Please provide a valid token')
];
}
$fs = $this->settingRepository->findForShop($this->context->shop->id);
$fs->api_token = Tools::getValue('api_token');
$fs->id_shop = $this->context->shop->id;
$fs->save();
return [
'success' => true,
'message' => $this->l('Correctly connected to the freshmail account')
];
}
private function getLists()
{
$fs = $this->settingRepository->findForShop($this->context->shop->id);
$freshmail = new \FreshMail\Freshmail($fs->api_token);
try {
$list = array_merge(
[[
'subscriberListHash' => self::NEW_LIST_KEY,
'name' => 'Prestashop - ' . $this->context->shop->name
]],
$freshmail->getLists()
);
return [
'success' => true,
'data' => $list
];
} catch (Exception $e) {
return [
'success' => false,
'message' => $this->l('Please provide a valid token')
];
}
// /rest/
}
private function save()
{
$fs = $this->settingRepository->findForShop($this->context->shop->id);
$fs->smtp = (Tools::getValue('smtp') == 'true') ? 1 : 0;
if (1 == $fs->smtp) {
\FreshMail\Tools::setShopSmtp($fs->api_token);
}
$fs->synchronize = (Tools::getValue('synchronize') == 'true') ? 1 : 0;
$listHash = '';
if (1 == $fs->synchronize) {
$listHash = Tools::getValue('synchronize_list');
if (self::NEW_LIST_KEY == Tools::getValue('synchronize_list', '')) {
$name = 'Prestashop - ' . $this->context->shop->name;
$description = \Context::getContext()->getTranslator()->trans('List from ', [], 'Modules.freshmail').$this->context->shop->name;
$listHash = (new \FreshMail\Freshmail($fs->api_token))->addList($name, $description);
}
}
$fs->subscriber_list_hash = $listHash;
$fs->wizard_completed = 1;
$fs->id_specific_price_rule = (int)Tools::getValue('id_specific_price_rule');
$fs->save();
return [
'success' => true,
'message' => $this->module->l('Configuration saved')
];
}
}

View File

@@ -0,0 +1,33 @@
<?php
use FreshMail\ApiV2\UnauthorizedException;
use FreshMail\Repository\FreshmailSettings;
class AdminFreshmailBaseController extends ModuleAdminController
{
protected $freshmail;
protected $freshmailSettings;
public function init()
{
$this->freshmailSettings = (new FreshmailSettings())->findForShop($this->context->shop->id);
if (!Validate::isLoadedObject(($this->freshmailSettings)) || 0 == $this->freshmailSettings->wizard_completed) {
$link = $this->context->link->getAdminLink('AdminModules', true, [], ['configure' => $this->module->name]);
Tools::redirectAdmin($link);
}
if(!empty($this->freshmailSettings->api_token)) {
$this->freshmail = new \FreshMail\Freshmail($this->freshmailSettings->api_token);
$response = $this->freshmail->check();
if (!$response['status']) {
$link = $this->context->link->getAdminLink('AdminFreshmailConfig', true, [], ['configure' => $this->module->name]);
Tools::redirectAdmin($link);
}
}
return parent::init(); // TODO: Change the autogenerated stub
}
}

View File

@@ -0,0 +1,196 @@
<?php
require_once __DIR__.'/AdminFreshmailBase.php';
class AdminFreshmailBirthdayController extends AdminFreshmailBaseController
{
const TPL_DIR = 'PrestaShop';
const TPL_CATEGORY = 4;
private $freshmailBirthday;
public function __construct()
{
$this->bootstrap = true;
$this->display = 'edit';
parent::__construct();
/*$this->template = 'cart_config.tpl';
$this->override_folder = '/';*/
$this->freshmailBirthday = (new \FreshMail\Repository\Birthdays(Db::getInstance()))->findForShop($this->context->shop->id);
}
public function init()
{
parent::init();
if(Tools::getIsset('send')){
require_once __DIR__.'/../../cron/birthday.php';
Tools::redirectAdmin($this->context->link->getAdminLink('AdminFreshmailBirthday').'&sent');
}
if(Tools::getIsset('sent')){
$this->confirmations[] = $this->module->l('Emails sent');
}
}
public function renderForm()
{
$this->submit_action = $this->display . $this->identifier;
$this->show_form_cancel_button = false;
$this->fields_form = [
'legend' => [
'title' => $this->module->l('Birthday e-mails'),
],
'input' => [
[
'type' => 'switch',
'label' => $this->module->l('Enable'),
'name' => 'birthday',
'required' => true,
'class' => 'switch prestashop-switch',
'is_bool' => true,
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->module->l('Enabled')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->module->l('Disabled')
]
],
],
],
'submit' => [
'name' => 'submitFreshmailBirthday',
'title' => $this->module->l('Save'),
'class' => 'btn btn-default pull-right'
],
];
$this->fields_value['birthday'] = $this->freshmailBirthday->enable && Validate::isLoadedObject($this->freshmailBirthday);
if($this->fields_value['birthday']){
$this->extendForm();
}
return parent::renderForm() . $this->extendedInfo();
}
public function postProcess()
{
if(isset($_POST['submitFreshmailBirthday'])){
$this->freshmailBirthday->enable = (bool)Tools::getValue('birthday');
if(isset($_POST['birthday_tpl'])){
$this->freshmailBirthday->tpl = Tools::getValue('birthday_tpl');
}
foreach (Language::getIDs(false) as $id_lang) {
if (isset($_POST['content_'.$id_lang])) {
$this->freshmailBirthday->content[$id_lang] = $_POST['content_'.$id_lang];
}
if (isset($_POST['email_subject_'.$id_lang])) {
$this->freshmailBirthday->email_subject[$id_lang] = $_POST['email_subject_'.$id_lang];
}
}
if(!Validate::isLoadedObject($this->freshmailBirthday)){
$this->freshmailBirthday->id_shop = $this->context->shop->id;
}
$this->freshmailBirthday->save();
}
return parent::postProcess(); // TODO: Change the autogenerated stub
}
public function run()
{
if(Tools::getIsset('ajax')){
return $this->ajax();
}
return parent::run();
}
private function extendForm(){
$this->warnings[] = $this->module->l('Full functionality requires setting a periodic task: ').' '
. $this->context->link->getBaseLink().'modules/freshmail/cron/birthday.php?token='.$this->module->getCronToken()
. '<br>' .$this->module->l('or use cli').': '. _PS_MODULE_DIR_ . 'freshmail/cron/birthday.php'
;
$this->fields_form['input'][] = [
'type' => 'select',
'label' => $this->module->l('Template'),
'desc' => $this->module->l('Lists of templates defined in the Freshmail application'),
'name' => 'birthday_tpl',
'multiple' => false,
'required' => true,
'options' => [
'query' => $this->getTpl(), //$lists,
'id' => 'id_hash',
'name' => 'name'
]
];
$this->fields_form['input'][] = [
'type' => 'text',
'label' => $this->module->l('Email subject'),
'name' => 'email_subject',
//'autoload_rte' => true,
'lang' => true,
];
$this->fields_form['input'][] = [
'type' => 'textarea',
'label' => $this->module->l('Description'),
'name' => 'content',
'autoload_rte' => true,
'lang' => true,
'hint' => $this->module->l('{content}')
];
$this->fields_form['buttons'] = [
[
'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminFreshmailBirthday').'&send',
'title' => $this->module->l("Send to today's birthday people"),
'icon' => 'process-icon-refresh'
]
];
$this->fields_value['birthday_tpl'] = $this->freshmailBirthday->tpl;
$this->fields_value['content'] = $this->freshmailBirthday->content;
$this->fields_value['email_subject'] = $this->freshmailBirthday->email_subject;
}
private function extendedInfo()
{
$templateFile = _PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/birthday.tpl';
return $this->context->smarty->fetch($templateFile);
}
public function getTpl()
{
parent::init();
$tplList = [
['id_hash' => '', 'name' => $this->module->l('Choose')]
];
if(!empty($this->freshmail) && $this->freshmail->check()){
$this->freshmail->getEmailsTemplates(self::TPL_DIR);
$tpls = $this->freshmail->getEmailsTemplates(self::TPL_DIR);
\FreshMail\Tools::filterTplByCategory($tpls, self::TPL_CATEGORY);
$tplList = array_merge($tplList, $tpls );
}
return $tplList;
}
}

View File

@@ -0,0 +1,203 @@
<?php
use FreshMail\Repository\FreshmailSettings;
require_once __DIR__ . '/AdminFreshmailBase.php';
class AdminFreshmailConfigController extends AdminFreshmailBaseController
{
const INTEGRATION_CREATED = 'FreshMail integration for Prestashop created successfully!';
const SUCCESS_UPDATE_API_KEYS = 'API keys update successfully!';
const FRESHMAIL_URL = 'http://freshmail.com';
public function __construct()
{
$this->bootstrap = true;
$this->lang = true;
$this->deleted = false;
$this->colorOnBackground = false;
$this->context = Context::getContext();
parent::__construct();
}
public function initContent()
{
parent::initContent();
if (Tools::isSubmit('resetSettings')) {
$this->resetSettings();
}
$freshmailSettings = (new FreshmailSettings())->findForShop($this->context->shop->id);
if(empty($freshmailSettings->api_token)){
$this->viewPreWizard();
return;
}
$this->viewConfig();
}
public function createTemplate($tpl_name)
{
$path = __DIR__ . '/../../views/templates/admin/';
return $this->context->smarty->createTemplate($path . $tpl_name, $this->context->smarty);
}
public function checkAccess($disable = false)
{
return true;
}
public function viewAccess($disable = false)
{
return true;
}
public function setMedia($isNewTheme = false)
{
parent::setMedia($isNewTheme);
$this->addJS(
array(
_PS_MODULE_DIR_ . 'freshmail/views/js/submitNewsletter.js?'.$this->module->version
)
);
}
public function displayAjaxSubmitNewsletter()
{
$this->ajax = true;
Configuration::updateValue('FRESHMAIL_NEWSLETTER_EMAIL', Tools::getValue('email'));
$result = array(
'link' => $this->context->link->getAdminLink('AdminFreshmailConfig') . '&conf=4',
'status' => 'OK'
);
echo json_encode($result);
}
private function submitEnterApiKeys(Freshmail\Entity\FreshmailSetting $fs, \FreshMail\Freshmail $fm){
$api_key = Tools::getValue('api_key');
if (empty($api_key)) {
$this->errors[] = $this->l('An error occurred while updating.');
return;
}
$listHash = Tools::getValue('subscriber_list_hash');
if(empty($listHash) || !in_array($listHash, $this->freshmail->getAllHashList())) {
$this->errors[] = $this->l('Please choose a subscribers list');
return;
}
$changeSMTP = !($fs->smtp == Tools::getValue('smtp'));
$changedList = !($fs->subscriber_list_hash == Tools::getValue('subscriber_list_hash'));
//$fs->api_token = $api_key;
$fs->id_specific_price_rule = (int)Tools::getValue('id_specific_price_rule');
$fs->smtp = (int)Tools::getValue('smtp');
$fs->synchronize = (int)Tools::getValue('synchronize');
$fs->send_confirmation = (int)Tools::getValue('send_confirmation');
$fs->subscriber_list_hash = Tools::getValue('subscriber_list_hash');
$fs->save();
if($changeSMTP){
\FreshMail\Tools::setShopSmtp($api_key);
}
if($changedList){
// check list has tag imie
if(!$fm->hasFieldWithTag($fs->subscriber_list_hash, Freshmail::NAME_TAG)){
$name = \Context::getContext()->getTranslator()->trans('First name', [],'Admin.Global');
$fm->addFieldToList($fs->subscriber_list_hash, Freshmail::NAME_TAG, $name) ;
}
\FreshMail\Tools::triggerSynchronization($fs->subscriber_list_hash);
}
$link = $this->context->link->getAdminLink('AdminFreshmailConfig') . '&conf=6';
Tools::redirectAdmin($link);
}
private function submitConnectToApi(\Freshmail\Entity\FreshmailSetting $fs){
$api = new \FreshMail\Freshmail($fs->api_token);
$response = $api->check();
if ($response && $response['status'] == 'OK') {
return true;
}
else {
$this->errors[] = $this->module->l('Error while connecting to FreshMail API');
}
}
private function resetSettings()
{
\FreshMail\Tools::clearShopSettings($this->context->shop->id);
$link = $this->context->link->getAdminLink('AdminModules', true, [], ['configure' => $this->module->name]);
Tools::redirectAdmin($link);
}
private function viewPreWizard(){
$links = [
'reset' => $this->context->link->getAdminLink('AdminFreshmailConfig', true, [], ['resetSettings' => 1]),
'base_url' => $this->context->link->getBaseLink(null, true),
];
$this->context->smarty->assign([
'links' => $links,
]);
$this->setTemplate('wizard_preview.tpl');
}
private function viewConfig(){
$this->initTabModuleList();
$this->initToolbar();
$this->initPageHeaderToolbar();
$this->addToolBarModulesListButton();
if(!\FreshMail\Tools::checkDirPermission(FreshMail::TMP_DIR)){
$this->errors[] = $this->l('Set temporary dir as writeable').' ('.FreshMail::TMP_DIR.')';
}
unset($this->toolbar_btn['save']);
$back = $this->context->link->getAdminLink('AdminDashboard');
$this->toolbar_btn['back'] = array(
'href' => $back,
'desc' => $this->l('Back to the dashboard'),
);
$freshmailSettings = (new FreshmailSettings())->findForShop($this->context->shop->id);
$fm = new FreshMail\Freshmail($freshmailSettings->api_token);
$helpArray = array(
'url_post' => self::$currentIndex . '&token=' . $this->token,
'module_templates' => _PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/',
'show_page_header_toolbar' => $this->show_page_header_toolbar,
'page_header_toolbar_title' => $this->page_header_toolbar_title,
'page_header_toolbar_btn' => $this->page_header_toolbar_btn,
'response' => null,
'specific_price_rules' => \FreshMail\Tools::getSpecificPriceRules($this->context->shop->id),
'subscribers_list' => $fm->getAllList(),
'freshmail_settings' => $freshmailSettings
);
if (Tools::isSubmit('submitEnterApiKeys')) {
$this->submitEnterApiKeys($freshmailSettings, $fm);
} elseif (Tools::isSubmit('submitConnectToApi')) {
if($this->submitConnectToApi($freshmailSettings)) {
$helpArray['success'] = $this->module->l(self::INTEGRATION_CREATED);
}
}
if (!empty($freshmailSettings->api_token)) {
$helpArray['showCheck'] = true;
} else {
$helpArray['showCheck'] = false;
}
$helpArray['FRESHMAIL_NEWSLETTER_EMAIL'] = Configuration::get('FRESHMAIL_NEWSLETTER_EMAIL');
$helpArray['FRESHMAIL_API_KEY'] = $freshmailSettings->api_token;
$this->context->smarty->assign($helpArray);
$this->setTemplate('api.tpl');
}
}

View File

@@ -0,0 +1,22 @@
<?php
class AdminFreshmailDashboardController extends ModuleAdminController
{
public function __construct()
{
$this->bootstrap = true;
$this->context = Context::getContext();
$this->className = 'AdminQpAllegroApi';
parent::__construct();
}
public function run()
{
return 'xxxxxx';
}
}

View File

@@ -0,0 +1,435 @@
<?php
use FreshMail\ApiV2\Client;
use FreshMail\Entity\Form;
use FreshMail\FreshmailApiV3;
use FreshMail\Service\FieldService;
use FreshMail\Service\FormService;
require_once __DIR__ . '/AdminFreshmailBase.php';
class AdminFreshmailFormConfigController extends AdminFreshmailBaseController
{
public static $UPDATE_SUCCESS = "Success!";
public static $ERROR = "Error!";
private $fmForms = [];
public function __construct()
{
$this->bootstrap = true;
$this->tpl_folder = 'freshmail_controller';
$this->lang = false;
$this->explicitSelect = true;
$this->context = Context::getContext();
$this->show_cancel_button = false;
$this->table = 'freshmail_form';
$this->identifier = 'id_freshmail_form';
$this->className = Form::class;
parent::__construct();
}
public function init()
{
parent::init();
if(Tools::getIsset('refresh')){
$this->module->clearFormCache();
}
$this->fmForms = $this->freshmail->getForms($this->freshmailSettings->subscriber_list_hash);
}
public function initContent()
{
$this->initTabModuleList();
$this->initToolbar();
parent::initContent();
}
public function initPageHeaderToolbar()
{
if (empty($this->display)) {
$this->page_header_toolbar_btn['refresh'] = array(
'href' => self::$currentIndex . '&refresh=1&token=' . $this->token,
'desc' => $this->module->l('Refresh cache'),
'icon' => 'process-icon-refresh'
);
$this->page_header_toolbar_btn['new_product'] = array(
'href' => self::$currentIndex . '&add' . $this->table . '=1&token=' . $this->token,
'desc' => $this->module->l('Add new form'),
'icon' => 'process-icon-new'
);
}
parent::initPageHeaderToolbar();
}
public function initToolbar()
{
switch ($this->display) {
default: // list
$this->toolbar_btn['new'] = array(
'href' => self::$currentIndex . '&add' . $this->table . '=1&token=' . $this->token,
'desc' => $this->module->l('Add New Form')
);
}
unset($this->toolbar_btn['back']);
}
public function renderList()
{
if(empty($this->freshmailSettings->api_token)){
$this->errors[] = $this->module->l('To use this feature You have to enable synchronization with FreshMail');
return;
}
$this->fields_list = array(
'id_freshmail_form' => array(
'title' => 'ID',
'align' => 'center',
'width' => 25
),
'form_hash' => array(
'title' => $this->module->l('Name'),
'width' => 'auto',
'callback' => 'getName'
),
'hook' => array(
'title' => $this->module->l('Hook'),
'width' => 'auto'
),
'active' => array(
'title' => $this->module->l('Active'),
'width' => 'auto'
),
);
// Adds an Edit button for each result
$this->addRowAction('edit');
// Adds a Delete button for each result
$this->addRowAction('delete');
$this->specificConfirmDelete = $this->l('Delete selected items?', array(), 'Admin.Notifications.Warning');
if (!($this->fields_list && is_array($this->fields_list))) {
return false;
}
$this->getList($this->context->language->id);
foreach ($this->_list as $k => $v) {
if (isset($v['active'])) {
if ((int)$this->_list[$k]['active'] == 0) {
$this->_list[$k]['active'] = $this->module->l('Disabled');
} else {
$this->_list[$k]['active'] = $this->module->l('Enabled');
}
if (isset($v['position']) && isset(self::$POSITION[$this->_list[$k]['position']])) {
$this->_list[$k]['position'] = $this->module->l(self::$POSITION[$this->_list[$k]['position']]);
}
}
}
// If list has 'active' field, we automatically create bulk action
if (isset($this->fields_list) && is_array($this->fields_list) && array_key_exists('active', $this->fields_list)
&& !empty($this->fields_list['active'])) {
if (!is_array($this->bulk_actions)) {
$this->bulk_actions = array();
}
}
$helper = new HelperList();
// Empty list is ok
if (!is_array($this->_list)) {
$this->displayWarning($this->module->l('Bad SQL query', 'Helper') . '<br />' . htmlspecialchars($this->_list_error));
return false;
}
$this->setHelperDisplay($helper);
$helper->simple_header = true;
$helper->_default_pagination = $this->_default_pagination;
$helper->_pagination = $this->_pagination;
$helper->tpl_vars = $this->getTemplateListVars();
// For compatibility reasons, we have to check standard actions in class attributes
foreach ($this->actions_available as $action) {
if (!in_array($action, $this->actions) && isset($this->$action) && $this->$action) {
$this->actions[] = $action;
}
}
$helper->has_value = false;
$helper->is_cms = $this->is_cms;
$helper->sql = $this->_listsql;
$list = $helper->generateList($this->_list, $this->fields_list);
return $list . $this->context->smarty->fetch(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/forms.tpl');
}
private function valid($action = 'add')
{
$this->requiredFields = array('name', 'position');
foreach ($this->requiredFields as $k => $v) {
if (!empty($v)) {
$value = Tools::getValue($v);
if (empty($value)) {
$title = ucfirst($this->fields_list[$v]['title']);
$label = $title . $this->module->l(' is required!') . ' -> ' . $v;
$this->errors[] = Tools::displayError($label);
}
}
}
if (is_array($this->errors) && count($this->errors)) {
$this->action = $action;
return false;
}
return true;
}
// This method generates the Add/Edit form
public function renderForm()
{
$hooks = [
[
'hook' => 'displayFooterProduct',
'title' => $this->module->l('Product footer'),
'description' => $this->module->l('This hook adds new blocks under the product\'s description')
],
[
'hook' => 'displayHeader',
'title' => $this->module->l('Added in the header of every page'),
'description' => $this->module->l('This hook adds new blocks in header')
],
[
'hook' => 'displayHome',
'title' => $this->module->l('Homepage content'),
'description' => $this->module->l('This hook displays new elements on the homepage')
],
[
'hook' => 'displayRightColumnProduct',
'title' => $this->module->l('New elements on the product page (right column)'),
'description' => $this->module->l('This hook displays new elements in the right-hand column of the product page')
],
[
'hook' => 'displayLeftColumnProduct',
'title' => $this->module->l('New elements on the product page (left column)'),
'description' => $this->module->l('This hook displays new elements in the left-hand column of the product page')
],
[
'hook' => 'displayTop',
'title' => $this->module->l('Top of pages'),
'description' => $this->module->l('This hook displays additional elements at the top of your pages')
],
[
'hook' => 'freshmailForm',
'title' => $this->module->l('Custom hook'),
'description' => $this->module->l('You can add this hook in every place You want')
]
];
foreach ($hooks as &$hook){
$hook['title'] = $hook['hook'] . ' | '.$hook['title'];
}
$this->fields_form = [
'legend' => [
'title' => $this->module->l('Forms and additional fields'),
],
'input' => [
[
'type' => 'select',
'label' => $this->module->l('Hook'),
'name' => 'hook',
'required' => true,
'desc' => $this->module->l('Used hook'),
'class' => 'fixed-width-xxl',
'options' => [
'query' => $hooks,
'id' => 'hook',
'name' => 'title'
],
],
[
'type' => 'select',
'label' => $this->module->l('Form in Freshmail'),
'name' => 'form_hash',
'required' => true,
'desc' => $this->module->l(''),
'class' => 'fixed-width-xxl',
'options' => [
'query' => $this->fmForms,
'id' => 'id_hash',
'name' => 'name'
],
],
[
'type' => 'switch',
'label' => $this->module->l('Enable form'),
'name' => 'active',
'required' => true,
'class' => 'switch prestashop-switch',
'is_bool' => true,
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->module->l('Enabled')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->module->l('Disabled')
]
],
],
],
'submit' => [
'title' => $this->module->l('Save'),
'class' => 'btn btn-default pull-right'
],
'buttons' => [
[
'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminFreshmailFormConfig'),
'title' => $this->module->l('Back to list'),
'icon' => 'process-icon-back'
]
]
];
return parent::renderForm();
}
public function displayEditLink($token = null, $id, $name = null)
{
$tpl = $this->createTemplate('helpers/list/list_action_edit.tpl');
if (!array_key_exists('Edit', self::$cache_lang)) {
self::$cache_lang['Edit'] = $this->module->l('Edit', 'Helper');
}
$href = self::$currentIndex . '&' . $this->identifier . '=' . $id . '&update' . $this->table . '&token=' . ($token != null ? $token : $this->token);
if ($this->display == 'view') {
$href = Context::getContext()->link->getAdminLink('AdminCustomers') . '&id_customer=' . (int)$id . '&updatecustomer';
}
$tpl->assign(array(
'href' => $href,
'action' => self::$cache_lang['Edit'],
'id' => $id
));
return $tpl->fetch();
}
public function checkAccess($disable = false)
{
return true;
}
public function viewAccess($disable = false)
{
return true;
}
public function displayAjax()
{
$return = array(
'hasError' => true,
'errors' => 'Error'
);
die(Tools::jsonEncode($return));
}
public function displayAjaxUpdateFieldsForm()
{
$this->ajax = true;
$hash = Tools::getValue('hash');
$fieldService = new FieldService(new \FreshMail\Repository\FieldRepository());
$fieldsApiArray = $this->freshmail->getAllFieldsByIdHashList($hash);
$idForm = Tools::getValue('id_form');
$fieldsDbArray = !empty($idForm) ? $fieldService->getFieldsByIdForm($idForm) : [];
$fields = [];
foreach ($fieldsDbArray as $key => $value) {
$fields[$value['hash']] = [
'id' => $value['id'],
'displayname' => $value['displayname'],
'hash' => $value['hash'],
'name' => $value['name'],
'tag' => $value['tag'],
'require_field' => $value['require_field'],
'include_field' => $value['include_field']
];
}
foreach ($fieldsApiArray as $key => $value) {
if (!isset($fields[$value['hash']])) {
$fields[$value['hash']] = $fieldsApiArray[$key];
}
}
if (!$this->checkExistField('email', $fields)) {
$fields[] = array(
'displayname' => "E-mail",
'hash' => $hash,
'name' => "E-mail",
'tag' => "email",
'require_field' => 1,
'include_field' => 1
);
}
$result = array(
'status' => 'OK',
'fields' => $fields
);
echo Tools::jsonEncode($result);
}
private function checkExistField($fieldName, $fields)
{
foreach ($fields as $key => $value) {
if ($fieldName == $value['tag']) {
return true;
}
}
return false;
}
public function beforeAdd($form)
{
$form->id_shop = $this->context->shop->id;
}
public function processDelete()
{
$id = (int)Tools::getValue('id_freshmail_form');
$form = new FreshMail\Entity\Form($id);
$form->delete();
Tools::redirectAdmin($this->context->link->getAdminLink('AdminFreshmailFormConfig') . '&conf=1');
}
public function getName($value, $row){
return isset($this->fmForms[$value]) ? $this->fmForms[$value]['name'] : '';
}
}

View File

@@ -0,0 +1,112 @@
<?php
use FreshMail\Repository\FreshmailSettings;
require_once __DIR__ . '/AdminFreshmailBase.php';
class AdminFreshmailSubscribersController extends AdminFreshmailBaseController
{
public function __construct()
{
$this->bootstrap = true;
$this->table = 'freshmail_list_email';
$this->className = 'FreshMail\Entity\Email';
$this->lang = false;
$this->deleted = false;
$this->colorOnBackground = false;
$this->multishop_context = Shop::CONTEXT_ALL;
$this->imageType = 'gif';
$this->fieldImageSettings = [
'name' => 'icon',
'dir' => 'os',
];
$this->display = 'list';
parent::__construct();
$this->fields_list = [
'email' => [
'title' => $this->module->l('Email'),
'maxlength' => 30,
'remove_onclick' => true
],
'add_date' => [
'title' => $this->module->l('Date add'),
],
'status' => [
'title' => $this->module->l('Subscriber status'),
'remove_onclick' => true
]
];
$hash = (new FreshmailSettings)->findForShop($this->context->shop->id)->subscriber_list_hash;
$this->_where = ' AND hash_list = "' . pSQL($hash) . '"';
}
public function init()
{
parent::init();
$freshmailSettings = (new FreshmailSettings())->findForShop($this->context->shop->id);
if(empty($freshmailSettings->api_token)){
$this->errors[] = $this->module->l('To use this feature You have to enable synchronization with FreshMail');
return;
}
if(!in_array($freshmailSettings->subscriber_list_hash, $this->freshmail->getAllHashList() )){
Tools::redirectAdmin(
$this->context->link->getAdminLink('AdminFreshmailConfig')
);
}
$ajr = new \FreshMail\Repository\AsyncJobs(Db::getInstance());
if(!empty($ajr->getRunningJobs($freshmailSettings->api_token))){
$this->warnings[] = $this->module->l('Synchronization is already pending');
}
if(Tools::getIsset('trigger_sync')){
if(empty($ajr->getRunningJobs($freshmailSettings->api_token))){
\FreshMail\Tools::triggerSynchronization($freshmailSettings->subscriber_list_hash);
$this->confirmations[] = $this->module->l('Synchronization has started');
}
}
}
public function renderList()
{
$freshmailSettings = (new FreshmailSettings())->findForShop($this->context->shop->id);
$this->context->smarty->assign([
'synchronization_url' => $this->context->link->getBaseLink(null, true).'modules/freshmail/cron/synchronize.php?hash='.$freshmailSettings->subscriber_list_hash . '&token='.Freshmail::getCronToken(),
'synchronization_cron' => _PS_MODULE_DIR_.'freshmail/cron/synchronize.php'
]);
return $this->context->smarty->fetch(_PS_MODULE_DIR_.$this->module->name. '/views/templates/admin/manage.tpl')
. parent::renderList()
. $this->context->smarty->fetch(_PS_MODULE_DIR_.$this->module->name. '/views/templates/admin/subscriber_list.tpl')
;
}
public function initPageHeaderToolbar()
{
if (!empty($this->display) && 'list' == $this->display) {
$this->page_header_toolbar_btn['trigger_sync'] = array(
'href' => self::$currentIndex . '&trigger_sync&token=' . $this->token,
'desc' => $this->module->l('Start synchronization'),
'icon' => 'process-icon-refresh',
);
}
parent::initPageHeaderToolbar();
}
public function initToolbar()
{
parent::initToolbar();
unset($this->toolbar_btn['new']);
}
}

View File

@@ -0,0 +1,222 @@
<?php
use FreshMail\ApiV2\UnauthorizedException;
use FreshMail\FreshmailApiV3;
use FreshMail\Repository\FreshmailSettings;
class AdminFreshmailWizardController extends ModuleAdminController
{
private $settingRepository;
const NEW_LIST_KEY = 'create_new';
public function __construct()
{
$this->bootstrap = true;
parent::__construct();
$this->templateFile = _PS_MODULE_DIR_ . $this->module->name . '/views/templates/admin/wizard.tpl';
$this->settingRepository = new FreshmailSettings();
}
public function run()
{
if ($step = Tools::getValue('step')) {
if (method_exists($this, $step)) {
echo json_encode($this->$step());
}
}
$this->context->smarty->assign([
'is_wizard_available' => $this->isWizardAvailable(),
'links' => $this->getLinks(),
'module_version' => $this->module->version,
'specific_price_rules' => \FreshMail\Tools::getSpecificPriceRules($this->context->shop->id)
]);
return $this->context->smarty->fetch($this->templateFile);
}
private function getLinks()
{
return [
'settings' => $this->context->link->getAdminLink('AdminFreshmailWizard', true, [], ['step' => 'settings']),
'connect' => $this->context->link->getAdminLink('AdminFreshmailWizard', true, [], ['step' => 'connect']),
'save' => $this->context->link->getAdminLink('AdminFreshmailWizard', true, [], ['step' => 'save']),
'lists' => $this->context->link->getAdminLink('AdminFreshmailWizard', true, [], ['step' => 'getLists']),
'register' => 'https://app.freshmail.com/pl/auth/login',
'help' => 'https://freshmail.pl/podrecznik-uzytkownika/',
'success_redirect' => $this->context->link->getAdminLink('AdminFreshmailConfig', true, [], ['step' => 'getLists']),
'abandon_carts_redirect' => $this->context->link->getAdminLink('AdminFreshmailAbandonedCartConfig', true, []),
'synchronize_link' => $this->context->link->getBaseLink(null, true).'modules/'.$this->module->name.'/cron/synchronize.php?token='.$this->module->getCronToken().'&hash=',
'activate_package' => $this->context->link->getAdminLink('AdminFreshmailWizard', true, [], ['step' => 'success']),
'base_url' => $this->context->link->getBaseLink(null, true),
];
}
private function isWizardAvailable()
{
return true;
}
private function settings()
{
$settings = $this->settingRepository->findForShop($this->context->shop->id);
$this->module->loadFreshmailApi();
$freshailApi = new \FreshMail\ApiV2\Client($settings->api_token);
$isLogged = false;
try {
$status = $freshailApi->doRequest('ping');
$isLogged = true;
} catch (UnauthorizedException $unauthorizedException) {
}
return [
'logged_in' => $isLogged,
'api_token' => $settings->api_token,
'smtp' => (bool)$settings->smtp,
'synchronize' => (bool)$settings->synchronize,
'id_specific_price_rule' => ((int)$settings->id_specific_price_rule > 0) ? (int)$settings->id_specific_price_rule : '',
'synchronize_list' => 'create_new'
];
}
private function connect()
{
$this->module->loadFreshmailApi();
$apiV2 = new \FreshMail\ApiV2\Client(Tools::getValue('api_token'));
try {
$apiV2->doRequest('ping');
} catch (UnauthorizedException $unauthorizedException) {
header("HTTP/1.1 401 Unauthorized");
return [
'success' => false,
'message' => $this->module->l('Please provide a valid token')
];
}
$fs = $this->settingRepository->findForShop($this->context->shop->id);
$fs->api_token = Tools::getValue('api_token');
$fs->id_shop = $this->context->shop->id;
$fs->save();
return [
'success' => true,
'message' => $this->module->l('Correctly connected to the freshmail account')
];
}
private function getLists()
{
$fs = $this->settingRepository->findForShop($this->context->shop->id);
$freshmail = new \FreshMail\Freshmail($fs->api_token);
try {
$list = array_merge(
[[
'subscriberListHash' => self::NEW_LIST_KEY,
'name' => 'Prestashop - ' . $this->context->shop->name
]],
$freshmail->getLists()
);
return [
'success' => true,
'data' => $list
];
} catch (Exception $e) {
return [
'success' => false,
'message' => $this->module->l('Please provide a valid token')
];
}
// /rest/
}
private function save()
{
$fs = $this->settingRepository->findForShop($this->context->shop->id);
if(empty($fs->api_token)){
\FreshMail\Installer\Tabs::install($this->module);
return $this->saveWithoutToken($fs);
}
\FreshMail\Installer\Tabs::install($this->module, 'extended');
return $this->saveWithToken($fs);
}
private function saveWithoutToken(\Freshmail\Entity\FreshmailSetting $fs)
{
$fs->wizard_completed = 1;
$fs->id_shop = $this->context->shop->id;
$fs->save();
return [
'success' => true,
'message' => $this->module->l('Configuration saved'),
'synchronize' => 0,
];
}
private function saveWithToken(\Freshmail\Entity\FreshmailSetting $fs)
{
$fs->smtp = (Tools::getValue('smtp') == 'true') ? 1 : 0;
if (1 == $fs->smtp) {
\FreshMail\Tools::setShopSmtp($fs->api_token);
}
$fs->synchronize = (Tools::getValue('synchronize') == 'true') ? 1 : 0;
$listHash = Tools::getValue('synchronize_list');
$fm = new \FreshMail\Freshmail($fs->api_token);
if (self::NEW_LIST_KEY == Tools::getValue('synchronize_list', '')) {
$name = 'Prestashop - ' . $this->context->shop->name;
$description = \Context::getContext()->getTranslator()->trans('List from ', [], 'Modules.freshmail').$this->context->shop->name;
$listHash = $fm->addList($name, $description);
}
if(!$fm->hasFieldWithTag($listHash, Freshmail::NAME_TAG)) {
$fm->addFieldToList($listHash, Freshmail::NAME_TAG, $this->trans('First name', [],'Admin.Global'));
}
if (1 == $fs->synchronize) {
}
$fs->subscriber_list_hash = $listHash;
$fs->wizard_completed = 1;
$fs->id_specific_price_rule = (int)Tools::getValue('id_specific_price_rule');
$fs->save();
$apiV3 = new FreshmailApiV3($fs->api_token);
try {
$apiV3->sendIntegrationInfo();
} catch (Exception $e) {
return [
'success' => false,
'message' => $e->getMessage()
];
}
// \FreshMail\Tools::sendWizardSuccessMail();
return [
'success' => true,
'message' => $this->module->l('Configuration saved'),
'synchronize' => (int)$fs->synchronize,
];
}
}

View File

@@ -0,0 +1,43 @@
<?php
class freshmailRestoreModuleFrontController extends ModuleFrontController
{
/** @var string controller name */
public $controller_name = 'Restore';
public function init()
{
parent::init();
$hash = Tools::getValue('hash');
$fmCart = (new \FreshMail\Repository\Carts(Db::getInstance()))->getByHash($hash);
$cart = new Cart($fmCart->id_cart);
if(!Validate::isLoadedObject($cart) || $cart->id_shop != $this->context->shop->id){
Tools::redirect('/');
}
$this->context->updateCustomer(new Customer($cart->id_customer));
$this->context->cookie->id_cart = (int) $cart->id;
$this->context->cookie->write();
$this->context->cart = $cart;
if(!empty($fmCart->id_cart_rule)){
$cartRule = new CartRule($fmCart->id_cart_rule);
} elseif(!empty($fmCart->discount_code)){
$cartRule = new CartRule(CartRule::getIdByCode($fmCart->discount_code));
}
if(Validate::isLoadedObject($cartRule)){
$this->context->cart->addCartRule($cartRule->id);
}
$this->context->cart->save();
Tools::redirect(
$this->context->link->getPageLink('cart', null, null, ['token' => Tools::getToken(false)])
);
}
}

View File

@@ -0,0 +1,43 @@
<?php
$flock = fopen(__FILE__, 'r');
if (!flock($flock, LOCK_EX | LOCK_NB)) {
die('Synchronization pending');
}
use FreshMail\Repository\FreshmailSettings;
require_once __DIR__ . '/../../../config/config.inc.php';
/*if (!Module::isInstalled('freshmail') || Tools::getValue('token') != Module::getInstanceByName('freshmail')->getCronToken() ) {
die('Bad token');
}*/
Module::getInstanceByName('freshmail');
// 1. utwórz listę
// 2. paczkuj na elementy
// 3. wyślij
// 4. usuń listy
$abandonRepository = new \FreshMail\Repository\FreshmailAbandonCartSettings(Db::getInstance());
$activeAbandon = $abandonRepository->getActive();
$cartsRepository = new \FreshMail\Repository\Carts(Db::getInstance());
$settingsRepository = new FreshmailSettings(Db::getInstance());
$cartService = new \FreshMail\Service\AbandonCartService($cartsRepository, $settingsRepository, $abandonRepository);
foreach ($activeAbandon as $abandon) {
$settings = new \Freshmail\Entity\AbandonedCartSettings($abandon['id_freshmail_cart_setting']);
$cart = new Cart();
$cart->id_shop = 1;
$cart->id_lang = Tools::getValue('id_lang') ? Tools::getValue('id_lang') : (int)Configuration::get('PS_LANG_DEFAULT');
$fmCart = new \FreshMail\Entity\Cart();
/* $fmCart = $cartsRepository->getByCart($cart);
$cartService->setDiscount($settings, $cart, $fmCart);*/
$email = new \FreshMail\Sender\Email('lukasz.kolanko@lizardmedia.pl', 'Łukasz');
//$email = 'lukasz.kolanko@lizardmedia.pl';(new Customer($cart->id_customer))->email;
$cartService->sendNotifications($cart, $fmCart, $email, new \FreshMail\Sender\Service\MockCartData());
}

View File

@@ -0,0 +1,61 @@
<?php
$flock = fopen(__FILE__, 'r');
if( !flock($flock, LOCK_EX | LOCK_NB )){
die('Synchronization pending');
}
use FreshMail\Repository\FreshmailSettings;
require_once __DIR__ . '/../../../config/config.inc.php';
if(!Module::isInstalled('freshmail') || !($module = Module::getInstanceByName('freshmail')) ){
die('Module isn\'t installed');
}
if(!\FreshMail\Tools::is_cli() && Tools::getValue('token') != $module->getCronToken() ) {
die('Bad token');
}
Module::getInstanceByName('freshmail');
global $kernel;
if(!$kernel){
require_once _PS_ROOT_DIR_.'/app/AppKernel.php';
$kernel = new \AppKernel('prod', false);
$kernel->boot();
}
// 1. utwórz listę
// 2. paczkuj na elementy
// 3. wyślij
// 4. usuń listy
$abandonRepository = new \FreshMail\Repository\FreshmailAbandonCartSettings(Db::getInstance());
$activeAbandon = $abandonRepository->getActive();
$cartsRepository = new \FreshMail\Repository\Carts(Db::getInstance());
$settingsRepository = new FreshmailSettings(Db::getInstance());
$cartService = new \FreshMail\Service\AbandonCartService($cartsRepository, $settingsRepository, $abandonRepository);
foreach ($activeAbandon as $abandon){
$settings = new \Freshmail\Entity\AbandonedCartSettings($abandon['id_freshmail_cart_setting']);
$idCarts = $cartsRepository->collectNotifyCarts($settings);
foreach ($idCarts as $c){
$cart = new Cart($c);
if($cart->orderExists()){
continue;
}
Context::getContext()->currency = new Currency($cart->id_currency);
$fmCart = $cartsRepository->getByCart($cart);
$cartService->setDiscount($settings, $cart, $fmCart);
$customer = new Customer($cart->id_customer);
$email = new \FreshMail\Sender\Email($customer->email, $customer->firstname.' '.$customer->lastname);
$cartService->sendNotifications($cart, $fmCart, $email, new \FreshMail\Sender\Service\CartData()) ;
}
}
//var_dump($activeAbandon);

View File

@@ -0,0 +1,52 @@
<?php
$flock = fopen(__FILE__, 'r');
if( !flock($flock, LOCK_EX | LOCK_NB )){
die('Synchronization pending');
}
use FreshMail\Repository\FreshmailSettings;
require_once __DIR__ . '/../../../config/config.inc.php';
/*if (!Module::isInstalled('freshmail') || Tools::getValue('token') != Module::getInstanceByName('freshmail')->getCronToken() ) {
die('Bad token');
}*/
Module::getInstanceByName('freshmail');
global $kernel;
if(!$kernel){
require_once _PS_ROOT_DIR_.'/app/AppKernel.php';
$kernel = new \AppKernel('prod', false);
$kernel->boot();
}
// 1. utwórz listę
// 2. paczkuj na elementy
// 3. wyślij
// 4. usuń listy
$abandonRepository = new \FreshMail\Repository\FreshmailAbandonCartSettings(Db::getInstance());
$activeAbandon = $abandonRepository->getActive();
$cartsRepository = new \FreshMail\Repository\Carts(Db::getInstance());
$settingsRepository = new FreshmailSettings(Db::getInstance());
$cartService = new \FreshMail\Service\AbandonCartService($cartsRepository, $settingsRepository, $abandonRepository);
foreach ($activeAbandon as $abandon){
$settings = new \Freshmail\Entity\AbandonedCartSettings($abandon['id_freshmail_cart_setting']);
$idCarts = [$_GET['id_cart']];
foreach ($idCarts as $c){
$cart = new Cart($c);
Context::getContext()->currency = new Currency($cart->id_currency);
$fmCart = $cartsRepository->getByCart($cart);
$cartService->setDiscount($settings, $cart, $fmCart);
$customer = new Customer($cart->id_customer);
$email = new \FreshMail\Sender\Email($customer->email, $customer->firstname.' '.$customer->lastname);
$cartService->sendNotifications($cart, $fmCart, $email, new \FreshMail\Sender\Service\CartData()) ;
}
}
//var_dump($activeAbandon);

View File

@@ -0,0 +1,56 @@
<?php
$flock = fopen(__FILE__, 'r');
if( !flock($flock, LOCK_EX | LOCK_NB )){
die('Synchronization pending');
}
use FreshMail\Repository\FreshmailSettings;
require_once __DIR__ . '/../../../config/config.inc.php';
if(!Module::isInstalled('freshmail') || !($module = Module::getInstanceByName('freshmail')) ){
die('Module isn\'t installed');
}
if(!\FreshMail\Tools::is_cli() && Tools::getValue('token') != $module->getCronToken() ) {
die('Bad token');
}
set_time_limit(0);
$date = date('Y-m-d', strtotime(Tools::getValue('birthdate', date('Y-m-d'))));
$repository = new \FreshMail\Repository\Birthdays(Db::getInstance());
$birthdays = [];
if(Tools::getValue('id_shop')){
$birthday = $repository->findForShop(Tools::getValue('id_shop'));
if(Validate::isLoadedObject($birthday) && $birthday->enable){
$birthdays[] = $birthday;
}
} else {
$key = \FreshMail\Entity\Birthday::$definition['primary'];
foreach ($repository->getActive() as $item) {
$birthdays[] = new \FreshMail\Entity\Birthday($item[$key]);
}
}
$service = new \FreshMail\Service\BirthdayService();
foreach ($birthdays as $birthday){
$query = new DbQuery();
$query->select('*')
->from('customer')
->where(sprintf('MONTH(birthday) = MONTH("%s")', $date))
->where(sprintf('DAY(birthday) = DAY("%s")', $date))
->where('id_shop = '.(int)$birthday->id_shop)
;
$customers = Db::getInstance()->executeS($query);
foreach ($customers as $c){
$service->sendNotifications(new Customer($c['id_customer']), $birthday);
}
}

View File

@@ -0,0 +1,61 @@
<?php
$flock = fopen(__FILE__, 'r');
if( !flock($flock, LOCK_EX | LOCK_NB )){
die('Synchronization pending');
}
use FreshMail\Repository\FreshmailSettings;
require_once __DIR__ . '/../../../config/config.inc.php';
if(!Module::isInstalled('freshmail') || !($module = Module::getInstanceByName('freshmail')) ){
die('Module isn\'t installed');
}
if(!\FreshMail\Tools::is_cli() && Tools::getValue('token') != $module->getCronToken() ) {
die('Bad token');
}
// 1. utwórz listę
// 2. paczkuj na elementy
// 3. wyślij
// 4. usuń listy
$hash = Tools::getValue('hash');
$fs = (new FreshmailSettings(Db::getInstance()))->getByHash($hash);
$ets = new \FreshMail\Repository\EmailToSynchronize(Db::getInstance());
$es = new \FreshMail\Repository\EmailsSynchronized(Db::getInstance());
if (Validate::isLoadedObject($fs)) {
set_time_limit(0);
$fm = new \FreshMail\Freshmail($fs->api_token);
$hash = Tools::getValue('hash');
while($ets->getCount($hash) > 0){
$subscribers = \FreshMail\Tools::convertToSubcriberCollection(\FreshMail\Tools::getEmailsToSynchronize($hash));
try {
$response = $fm->addSubscribers($hash, $subscribers, 1, false);
$es->addSubscribers($hash, $subscribers);
if(!empty($response['data']['not_inserted'])){
foreach ($response['data']['errors'] as $err ){
if($err['code'] != \FreshMail\FreshmailCode::ALREADY_SUBSCRIBED){
var_dump($err);
//save email
}
}
}
} catch (Exception $e){
}
\FreshMail\Tools::removeEmailsFromSynchronization($hash, $subscribers);
}
// \FreshMail\Tools::addEmailsToSynchronize($fs->id_shop, $hash);
}
echo ('Sending emails success');

View File

@@ -0,0 +1,70 @@
<?php
$flock = fopen(__FILE__, 'r');
if (!flock($flock, LOCK_EX | LOCK_NB)) {
die('Synchronization pending');
}
use FreshMail\Repository\FreshmailSettings;
require_once __DIR__ . '/../../../config/config.inc.php';
set_time_limit(0);
function synchronize($hash){
if($hash == AdminFreshmailWizardController::NEW_LIST_KEY){
foreach (Shop::getShops() as $shop ) {
if($shop['domain'] == Tools::getHttpHost()){
$fs = (new FreshmailSettings(Db::getInstance()))->findForShop((int)$shop['id_shop']);
$hash = $_GET['hash'] = $fs->subscriber_list_hash;
break;
}
}
}
$fs = (new FreshmailSettings(Db::getInstance()))->getByHash($hash);
if (!Validate::isLoadedObject($fs)) {
die('List doesn\'t exists');
}
\FreshMail\Tools::addEmailsToSynchronize($fs->id_shop, $fs->subscriber_list_hash);
require_once __DIR__.'/send_subscribers.php';
$fm = new \FreshMail\Freshmail($fs->api_token);
$fm->triggerExport($fs->subscriber_list_hash);
sleep(5);
require_once __DIR__.'/synchronize_subscribers.php';
}
if(!Module::isInstalled('freshmail') || !($module = Module::getInstanceByName('freshmail')) ){
die('Module isn\'t installed');
}
if(!\FreshMail\Tools::is_cli() && Tools::getValue('token') != $module->getCronToken() ) {
die('Bad token');
}
if(\FreshMail\Tools::is_cli()){
$query = new DbQuery();
$query->select('*')
->from(\Freshmail\Entity\FreshmailSetting::$definition['table'])
->where('synchronize = 1')
->where('wizard_completed = 1')
->where('subscriber_list_hash != ""');
foreach (Db::getInstance()->executeS($query) as $fs){
$_GET['hash'] = $fs['subscriber_list_hash'];
synchronize($fs['subscriber_list_hash']);
}
} else {
synchronize(Tools::getValue('hash'));
}
echo "synchronization done\n";

View File

@@ -0,0 +1,45 @@
<?php
use FreshMail\Entity\AsyncJob;
use FreshMail\Repository\AsyncJobs;
use FreshMail\Repository\FreshmailSettings;
require_once __DIR__ . '/../../../config/config.inc.php';
if(!Module::isInstalled('freshmail') || !($module = Module::getInstanceByName('freshmail')) ){
die('Module isn\'t installed');
}
if(!\FreshMail\Tools::is_cli() && Tools::getValue('token') != $module->getCronToken() ) {
die('Bad token');
}
$activeJobsRepository = new AsyncJobs(Db::getInstance());
$activeJobs = $activeJobsRepository->getRunningJobs();
$now = strtotime('now');
$cacheTime = AsyncJobs::TIME_SYNC_JOBS;
foreach ($activeJobs as $job) {
if ((strtotime($job['last_sync']) + $cacheTime) > $now) {
continue;
}
$fs = new \FreshMail\Freshmail($job['api_token']);
$aj = new AsyncJob($job['id_freshmail_async_job']);
$fs->pingAsyncJobStatus($aj);
}
set_time_limit(0);
foreach ( $activeJobsRepository->getSuccessJobs() as $readyJob ){
$fs = new \FreshMail\Freshmail($readyJob['api_token']);
$file = $fs->getFileFromJob($readyJob['id_job']);
\FreshMail\Service\AsyncJobService::updateJobFilename($readyJob['id_job'], $file);
(new \FreshMail\Repository\Subscribers(Db::getInstance()))->deleteByList($readyJob['hash_list']);
\FreshMail\Tools::importSubscribersFromCsv($readyJob['hash_list'], $file);
\FreshMail\Service\AsyncJobService::setJobAsFinished($readyJob['id_job']);
}

View File

@@ -0,0 +1,130 @@
<?php
use FreshMail\Hooks;
use FreshMail\Installer\InstallerFactory;
if (!defined('_PS_VERSION_')) {
exit;
}
require __DIR__ . '/lib/autoload.php';
class Freshmail extends Module
{
const TMP_DIR = _PS_MODULE_DIR_ . 'freshmail/tmp/';
const NAME_TAG = 'imie';
const CACHE_FORM_LIFETIME = 10 * 60;
use Hooks;
use \FreshMail\HooksForms;
public function __construct()
{
$this->name = "freshmail";
$this->tab = "front_office_features";
$this->version = "3.10.7";
$this->author = "FreshMail";
$this->need_instance = 1;
$this->bootstrap = true;
$this->displayName = $this->l('FreshMail for PrestaShop');
$this->description = $this->l('Synchronizes your newsletter subscribers and shop items with Freshmail.com');
/* $moduleManager = $this->get('prestashop.module.manager');
if($moduleManager && $moduleManager->isInstalled($this->name)){
$this->description .= ' ... <span>
<a class="module-read-more-list-btn url" href="/modules/freshmail/ajax.php" data-target="#module-modal-read-more-freshmail">Czytaj więcej</a>
</span>';
}*/
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?', 'freshmail');
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
parent::__construct();
$this->checkNativeSubscription();
}
public function install()
{
Configuration::updateValue('FRESHMAIL_SUBMISSION_SUCCESS_MESSAGE', $this->l('Your sign up request was successful! Please check your email inbox.'));
Configuration::updateValue('FRESHMAIL_SUBMISSION_FAILURE_MESSAGE', $this->l('Oops. Something went wrong. Please try again later.'));
Configuration::updateValue('FRESHMAIL_ALREADY_SUBSCRIBED_MESSAGE', $this->l('Given email address is already subscribed, thank you!'));
Configuration::updateValue('FRESHMAIL_INVALID_EMAIL_ADDRESS_MESSAGE', $this->l('Please provide a valid email address'));
Configuration::updateValue('FRESHMAIL_REQUIRED_FIELD_MISSING_MESSAGE', $this->l('Please fill all the required fields'));
Configuration::updateValue('FRESHMAIL_FORM_SUBSCRIBE_BUTTON', $this->l('Sign me up!'));
Configuration::updateValue('FRESHMAIL_SIGNUP_LABEL', $this->l('Sign me up for the newsletter'));
Configuration::updateValue('FRESHMAIL_ACCEPT_EMAILS_PARTNERS_LABEL', $this->l('I agree to receive email communications from partners'));
return parent::install()
&& InstallerFactory::getInstaller($this)->install();
}
public function uninstall()
{
return InstallerFactory::getInstaller($this)->uninstall()
&& parent::uninstall();
}
public function getContent()
{
$fs = (new \FreshMail\Repository\FreshmailSettings())->findForShop($this->context->shop->id);
if(!empty($fs->wizard_completed) && !empty($fs->api_token)) {
$link = $this->context->link->getAdminLink('AdminFreshmailConfig');
Tools::redirectAdmin($link);
}
$controller = new AdminFreshmailWizardController();
return $controller->run();
}
public function loadFreshmailApi()
{
require_once __DIR__ . '/lib/freshmail-api/vendor/autoload.php';
}
public static function getCronToken(){
return substr(Tools::encrypt('freshmail/index'), 0, 10);
}
private function checkNativeSubscription(){
if(!Module::isEnabled('ps_emailsubscription')){
return;
}
if (!Tools::isSubmit('submitNewsletter')
&& !($this->context->controller instanceof Ps_EmailsubscriptionVerificationModuleFrontController)
) {
return;
}
$fs = (new \FreshMail\Repository\FreshmailSettings(Db::getInstance()))->findForShop($this->context->shop->id);
$email = '';
if($this->context->controller instanceof Ps_EmailsubscriptionVerificationModuleFrontController){
$token = Tools::getValue('token');
$email = \FreshMail\Tools::getGuestEmailByToken($token);
if(empty($email)) {
$email = \FreshMail\Tools::getUserEmailByToken($token);
}
}
if(Tools::isSubmit('submitNewsletter')) {
$email = Tools::getValue('email');
}
if(empty($email)){
return;
}
$fm = new \FreshMail\Freshmail($fs->api_token);
$result = $fm->addSubscriber([
'email' => $email,
'list' => $fs->subscriber_list_hash,
'state' => (!$fs->send_confirmation ) ? 1 : 2,
'confirm' => $fs->send_confirmation
]);
}
}

View File

@@ -0,0 +1,35 @@
<?php
/*
* 2007-2016 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2016 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;

View File

@@ -0,0 +1,69 @@
{
"base": {
"type": "base",
"required": true,
"visible": true,
"styles": {
"background": "#333333",
"color": "#000"
}
},
"elements": {
"block_2": {
"type": "blockLogo",
"visible": true,
"styles": {
"background": "#FFFFFF",
"color": "#000",
"logo": "default",
"align": "left"
}
},
"block_1": {
"type": "blockText",
"visible": true,
"html": "<p>Spodobały Ci się te produkty?</p><p>{firstname}, w Twoim koszyku nadal znajdują się wybrane produkty. Zachęcamy do powrotu do sklepu i dokończenia transakcji nim inni zdecydują się na zakup tych samych produktów. </p>",
"styles": {
"background": "#FFFFFF",
"color": "#000"
}
},
"block_3": {
"type": "blockDiscount",
"visible": true,
"html": "<p>Nie dokończyłeś zakupów? Nic się nie stało, ponieważ teraz możesz to zrobić z rabatem! Kod ważny jest do {cartrule_validto}. Dotyczy produktów nieprzecenionych.</p>",
"styles": {
"background": "#FFFFFF",
"color": "#000000"
}
},
"block_4": {
"type": "blockProducts",
"visible": true,
"styles": {
"background": "#FFFFFF",
"color": "#000"
}
},
"block_5": {
"type": "blockCTA",
"required": true,
"visible": true,
"html": "<p style='padding:0;margin:0'>Dokończ zakupy</p>",
"styles": {
"background": "#FFFFFF",
"buttonbg": "#ffe807",
"color": "#644223"
}
},
"block_6": {
"type": "blockText",
"visible": true,
"html": "<p>Ta wiadomość została wysłana przez sklep internetowy <a href=\"{shop_url}\">{shop_url}</a> należący do {company_name} z siedzibą w: {company_address}.</p>",
"styles": {
"background": "#f9f9fc",
"color": "#363942"
}
}
}
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* 2007-2019 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@@ -0,0 +1,133 @@
CREATE TABLE IF NOT EXISTS `PREFIX_freshmail_setting`(
`id_freshmail_setting` INT NOT NULL AUTO_INCREMENT ,
`id_shop` INT NOT NULL ,
`api_token` VARCHAR(100) NOT NULL ,
`smtp` TINYINT(1) NOT NULL ,
`synchronize` TINYINT(1) NOT NULL ,
`wizard_completed` TINYINT(1) NOT NULL DEFAULT 0,
`send_confirmation` TINYINT(1) NOT NULL DEFAULT 0,
`subscriber_list_hash` char(10) COLLATE utf8_general_ci NOT NULL,
`id_specific_price_rule` int(10) UNSIGNED NOT NULL,
`date_add` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`date_upd` TIMESTAMP NULL,
KEY `id_freshmail_form` (`subscriber_list_hash`) USING BTREE,
PRIMARY KEY (`id_freshmail_setting`)
) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `PREFIX_freshmail_list_email`(
`id_freshmail_list_email` INT NOT NULL AUTO_INCREMENT ,
`email` VARCHAR (150),
`hash_list` char(10) COLLATE utf8_general_ci NOT NULL,
`last_synchronization` TIMESTAMP,
`add_date` TIMESTAMP,
`deletion_date` TIMESTAMP,
`status` VARCHAR (20),
`resigning_reason` VARCHAR (250),
PRIMARY KEY (`id_freshmail_list_email`),
CONSTRAINT `PREFIX_freshmail_list_email_ibfk_1` FOREIGN KEY (`hash_list`) REFERENCES `PREFIX_freshmail_setting` (`subscriber_list_hash`) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE (hash_list, email)
) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `PREFIX_freshmail_async_job` (
`id_freshmail_async_job` INT NOT NULL AUTO_INCREMENT ,
`id_job` INT NOT NULL ,
`hash_list` CHAR(10) NOT NULL ,
`parts` INT NOT NULL ,
`last_sync` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
`finished` TINYINT(1) DEFAULT 0,
`job_status` TINYINT(1) DEFAULT 0,
`filename` VARCHAR (50),
`date_add` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id_freshmail_async_job`)
) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `PREFIX_freshmail_emails_synchronized` (
`id_freshmail_emails_synchronized` INT NOT NULL AUTO_INCREMENT ,
`email` VARCHAR (150),
`hash_list` CHAR(10) NOT NULL ,
`date_add` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id_freshmail_emails_synchronized`),
UNIQUE (hash_list, email)
) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `PREFIX_freshmail_emails_to_synchronize` (
`id_freshmail_emails_to_synchronize` INT NOT NULL AUTO_INCREMENT ,
`email` VARCHAR (150),
`name` VARCHAR (150),
`hash_list` CHAR(10) NOT NULL ,
PRIMARY KEY (`id_freshmail_emails_to_synchronize`),
UNIQUE (hash_list, email)
) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `PREFIX_freshmail_form` (
`id_freshmail_form` int(11) NOT NULL AUTO_INCREMENT,
`id_shop` INT NOT NULL ,
`form_hash` char(10) COLLATE utf8_general_ci NOT NULL,
`hook` varchar(50) COLLATE utf8_general_ci NOT NULL,
`position` int(11) NOT NULL,
`active` tinyint(1) NOT NULL,
PRIMARY KEY (`id_freshmail_form`),
KEY `id_freshmail_form` (`form_hash`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `PREFIX_freshmail_cart_setting`(
`id_freshmail_cart_setting` INT NOT NULL AUTO_INCREMENT ,
`id_shop` INT NOT NULL ,
`enabled` TINYINT(1) NOT NULL DEFAULT 0,
`discount_type` ENUM ('none', 'percent', 'custom'),
`discount_percent` INT,
`discount_code` VARCHAR (50),
`discount_lifetime` INT,
`send_after` INT,
`template` TEXT,
`template_id_hash` VARCHAR(32),
`date_add` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`date_upd` TIMESTAMP NULL,
PRIMARY KEY (`id_freshmail_cart_setting`)
) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `PREFIX_freshmail_cart_setting_lang`(
`id_freshmail_cart_setting` INT NOT NULL AUTO_INCREMENT ,
`id_lang` INT NOT NULL ,
`email_subject` TEXT,
`email_preheader` TEXT,
PRIMARY KEY (`id_freshmail_cart_setting`, `id_lang`)
) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `PREFIX_freshmail_cart`(
`id_freshmail_cart` INT NOT NULL AUTO_INCREMENT ,
`id_cart` INT NOT NULL UNIQUE ,
`id_cart_rule` INT NULL,
`discount_code` VARCHAR(50) NULL,
`cart_token` VARCHAR (64),
`date_add` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`date_upd` TIMESTAMP NULL,
PRIMARY KEY (`id_freshmail_cart`)
) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `PREFIX_freshmail_cart_notify`(
`id` INT NOT NULL AUTO_INCREMENT ,
`id_freshmail_cart` INT NOT NULL,
`date_add` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`),
CONSTRAINT `PREFIX_freshmail_cart_ibfk_1` FOREIGN KEY (`id_freshmail_cart`) REFERENCES `PREFIX_freshmail_cart` (`id_freshmail_cart`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `PREFIX_freshmail_birthday`(
`id_freshmail_birthday` INT NOT NULL AUTO_INCREMENT ,
`id_shop` INT NOT NULL ,
`enable` TINYINT(1) NOT NULL DEFAULT 0 ,
`tpl` VARCHAR(32),
`date_add` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`date_upd` TIMESTAMP NULL,
PRIMARY KEY (`id_freshmail_birthday`)
) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `PREFIX_freshmail_birthday_lang`(
`id_freshmail_birthday` INT NOT NULL AUTO_INCREMENT ,
`id_lang` INT NOT NULL ,
`content` TEXT,
`email_subject` TEXT,
PRIMARY KEY (`id_freshmail_birthday`, `id_lang`)
) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

View File

@@ -0,0 +1,29 @@
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `PREFIX_freshmail_setting`;
DROP TABLE IF EXISTS `PREFIX_freshmail_list`;
DROP TABLE IF EXISTS `PREFIX_freshmail_mail`;
DROP TABLE IF EXISTS `PREFIX_freshmail_form`;
DROP TABLE IF EXISTS `PREFIX_freshmail_list_field`;
DROP TABLE IF EXISTS `PREFIX_freshmail_list_email`;
DROP TABLE IF EXISTS `PREFIX_freshmail_emails_to_synchronize`;
DROP TABLE IF EXISTS `PREFIX_freshmail_async_job`;
DROP TABLE IF EXISTS `PREFIX_freshmail_emails_synchronized`;
-- DROP TABLE IF EXISTS `PREFIX_freshmail_cart_setting`;
-- DROP TABLE IF EXISTS `PREFIX_freshmail_cart_setting_lang`;
-- DROP TABLE IF EXISTS `PREFIX_freshmail_cart`;
-- DROP TABLE IF EXISTS `PREFIX_freshmail_cart_notify`;
-- DROP TABLE IF EXISTS `PREFIX_freshmail_birthday`;
-- DROP TABLE IF EXISTS `PREFIX_freshmail_birthday_lang`;
SET FOREIGN_KEY_CHECKS = 1;

View File

@@ -0,0 +1,11 @@
<?php
namespace FreshMail\Interfaces;
interface Installer
{
public function install(): bool;
public function uninstall(): bool;
}

View File

@@ -0,0 +1,7 @@
<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit6209d2cc1bb420288929f962f08a3ac7::getLoader();

View File

@@ -0,0 +1,477 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Autoload;
/**
* ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
*
* $loader = new \Composer\Autoload\ClassLoader();
*
* // register classes with namespaces
* $loader->add('Symfony\Component', __DIR__.'/component');
* $loader->add('Symfony', __DIR__.'/framework');
*
* // activate the autoloader
* $loader->register();
*
* // to enable searching the include path (eg. for PEAR packages)
* $loader->setUseIncludePath(true);
*
* In this example, if you try to use a class in the Symfony\Component
* namespace or one of its children (Symfony\Component\Console for instance),
* the autoloader will first look for the class under the component/
* directory, and it will then fallback to the framework/ directory if not
* found before giving up.
*
* This class is loosely based on the Symfony UniversalClassLoader.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
* @see https://www.php-fig.org/psr/psr-0/
* @see https://www.php-fig.org/psr/psr-4/
*/
class ClassLoader
{
private $vendorDir;
// PSR-4
private $prefixLengthsPsr4 = array();
private $prefixDirsPsr4 = array();
private $fallbackDirsPsr4 = array();
// PSR-0
private $prefixesPsr0 = array();
private $fallbackDirsPsr0 = array();
private $useIncludePath = false;
private $classMap = array();
private $classMapAuthoritative = false;
private $missingClasses = array();
private $apcuPrefix;
private static $registeredLoaders = array();
public function __construct($vendorDir = null)
{
$this->vendorDir = $vendorDir;
}
public function getPrefixes()
{
if (!empty($this->prefixesPsr0)) {
return call_user_func_array('array_merge', array_values($this->prefixesPsr0));
}
return array();
}
public function getPrefixesPsr4()
{
return $this->prefixDirsPsr4;
}
public function getFallbackDirs()
{
return $this->fallbackDirsPsr0;
}
public function getFallbackDirsPsr4()
{
return $this->fallbackDirsPsr4;
}
public function getClassMap()
{
return $this->classMap;
}
/**
* @param array $classMap Class to filename map
*/
public function addClassMap(array $classMap)
{
if ($this->classMap) {
$this->classMap = array_merge($this->classMap, $classMap);
} else {
$this->classMap = $classMap;
}
}
/**
* Registers a set of PSR-0 directories for a given prefix, either
* appending or prepending to the ones previously set for this prefix.
*
* @param string $prefix The prefix
* @param array|string $paths The PSR-0 root directories
* @param bool $prepend Whether to prepend the directories
*/
public function add($prefix, $paths, $prepend = false)
{
if (!$prefix) {
if ($prepend) {
$this->fallbackDirsPsr0 = array_merge(
(array) $paths,
$this->fallbackDirsPsr0
);
} else {
$this->fallbackDirsPsr0 = array_merge(
$this->fallbackDirsPsr0,
(array) $paths
);
}
return;
}
$first = $prefix[0];
if (!isset($this->prefixesPsr0[$first][$prefix])) {
$this->prefixesPsr0[$first][$prefix] = (array) $paths;
return;
}
if ($prepend) {
$this->prefixesPsr0[$first][$prefix] = array_merge(
(array) $paths,
$this->prefixesPsr0[$first][$prefix]
);
} else {
$this->prefixesPsr0[$first][$prefix] = array_merge(
$this->prefixesPsr0[$first][$prefix],
(array) $paths
);
}
}
/**
* Registers a set of PSR-4 directories for a given namespace, either
* appending or prepending to the ones previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param array|string $paths The PSR-4 base directories
* @param bool $prepend Whether to prepend the directories
*
* @throws \InvalidArgumentException
*/
public function addPsr4($prefix, $paths, $prepend = false)
{
if (!$prefix) {
// Register directories for the root namespace.
if ($prepend) {
$this->fallbackDirsPsr4 = array_merge(
(array) $paths,
$this->fallbackDirsPsr4
);
} else {
$this->fallbackDirsPsr4 = array_merge(
$this->fallbackDirsPsr4,
(array) $paths
);
}
} elseif (!isset($this->prefixDirsPsr4[$prefix])) {
// Register directories for a new namespace.
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
} elseif ($prepend) {
// Prepend directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
(array) $paths,
$this->prefixDirsPsr4[$prefix]
);
} else {
// Append directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
$this->prefixDirsPsr4[$prefix],
(array) $paths
);
}
}
/**
* Registers a set of PSR-0 directories for a given prefix,
* replacing any others previously set for this prefix.
*
* @param string $prefix The prefix
* @param array|string $paths The PSR-0 base directories
*/
public function set($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr0 = (array) $paths;
} else {
$this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
}
}
/**
* Registers a set of PSR-4 directories for a given namespace,
* replacing any others previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param array|string $paths The PSR-4 base directories
*
* @throws \InvalidArgumentException
*/
public function setPsr4($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr4 = (array) $paths;
} else {
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
}
}
/**
* Turns on searching the include path for class files.
*
* @param bool $useIncludePath
*/
public function setUseIncludePath($useIncludePath)
{
$this->useIncludePath = $useIncludePath;
}
/**
* Can be used to check if the autoloader uses the include path to check
* for classes.
*
* @return bool
*/
public function getUseIncludePath()
{
return $this->useIncludePath;
}
/**
* Turns off searching the prefix and fallback directories for classes
* that have not been registered with the class map.
*
* @param bool $classMapAuthoritative
*/
public function setClassMapAuthoritative($classMapAuthoritative)
{
$this->classMapAuthoritative = $classMapAuthoritative;
}
/**
* Should class lookup fail if not found in the current class map?
*
* @return bool
*/
public function isClassMapAuthoritative()
{
return $this->classMapAuthoritative;
}
/**
* APCu prefix to use to cache found/not-found classes, if the extension is enabled.
*
* @param string|null $apcuPrefix
*/
public function setApcuPrefix($apcuPrefix)
{
$this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
}
/**
* The APCu prefix in use, or null if APCu caching is not enabled.
*
* @return string|null
*/
public function getApcuPrefix()
{
return $this->apcuPrefix;
}
/**
* Registers this instance as an autoloader.
*
* @param bool $prepend Whether to prepend the autoloader or not
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
if (null === $this->vendorDir) {
//no-op
} elseif ($prepend) {
self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders;
} else {
unset(self::$registeredLoaders[$this->vendorDir]);
self::$registeredLoaders[$this->vendorDir] = $this;
}
}
/**
* Unregisters this instance as an autoloader.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
if (null !== $this->vendorDir) {
unset(self::$registeredLoaders[$this->vendorDir]);
}
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
* @return bool|null True if loaded, null otherwise
*/
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
includeFile($file);
return true;
}
}
/**
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
*
* @return string|false The path if found, false otherwise
*/
public function findFile($class)
{
// class map lookup
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
return false;
}
if (null !== $this->apcuPrefix) {
$file = apcu_fetch($this->apcuPrefix.$class, $hit);
if ($hit) {
return $file;
}
}
$file = $this->findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
if (false === $file && defined('HHVM_VERSION')) {
$file = $this->findFileWithExtension($class, '.hh');
}
if (null !== $this->apcuPrefix) {
apcu_add($this->apcuPrefix.$class, $file);
}
if (false === $file) {
// Remember that this class does not exist.
$this->missingClasses[$class] = true;
}
return $file;
}
/**
* Returns the currently registered loaders indexed by their corresponding vendor directories.
*
* @return self[]
*/
public static function getRegisteredLoaders()
{
return self::$registeredLoaders;
}
private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
$subPath = $class;
while (false !== $lastPos = strrpos($subPath, '\\')) {
$subPath = substr($subPath, 0, $lastPos);
$search = $subPath . '\\';
if (isset($this->prefixDirsPsr4[$search])) {
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
foreach ($this->prefixDirsPsr4[$search] as $dir) {
if (file_exists($file = $dir . $pathEnd)) {
return $file;
}
}
}
}
}
// PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file;
}
}
// PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else {
// PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
}
if (isset($this->prefixesPsr0[$first])) {
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
}
}
}
// PSR-0 fallback dirs
foreach ($this->fallbackDirsPsr0 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
// PSR-0 include paths.
if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
return $file;
}
return false;
}
}
/**
* Scope isolated include.
*
* Prevents access to $this/self from included files.
*/
function includeFile($file)
{
include $file;
}

View File

@@ -0,0 +1,19 @@
Copyright (c) Nils Adermann, Jordi Boggiano
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,77 @@
<?php
// autoload_classmap.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'AbandonCartDiscount' => $baseDir . '/src/Interface/AbandonCartDiscount.php',
'AdminFreshmailAbandonedCartConfigController' => $baseDir . '/controllers/admin/AdminFreshmailAbandonedCartConfig.php',
'AdminFreshmailAjaxController' => $baseDir . '/controllers/admin/AdminFreshmailAjax.php',
'AdminFreshmailBaseController' => $baseDir . '/controllers/admin/AdminFreshmailBase.php',
'AdminFreshmailBirthdayController' => $baseDir . '/controllers/admin/AdminFreshmailBirthday.php',
'AdminFreshmailConfigController' => $baseDir . '/controllers/admin/AdminFreshmailConfig.php',
'AdminFreshmailDashboardController' => $baseDir . '/controllers/admin/AdminFreshmailDashboardController.php',
'AdminFreshmailFormConfigController' => $baseDir . '/controllers/admin/AdminFreshmailFormConfig.php',
'AdminFreshmailSubscribersController' => $baseDir . '/controllers/admin/AdminFreshmailSubscribers.php',
'AdminFreshmailWizardController' => $baseDir . '/controllers/admin/AdminFreshmailWizardController.php',
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
'FreshMail\\Discount\\AbstractDiscount' => $baseDir . '/classes/discount/AbstractDiscount.php',
'FreshMail\\Discount\\Custom' => $baseDir . '/classes/discount/Custom.php',
'FreshMail\\Discount\\None' => $baseDir . '/classes/discount/None.php',
'FreshMail\\Discount\\Percent' => $baseDir . '/classes/discount/Percent.php',
'FreshMail\\Entity\\AsyncJob' => $baseDir . '/src/Entity/AsyncJob.php',
'FreshMail\\Entity\\Birthday' => $baseDir . '/src/Entity/Birthday.php',
'FreshMail\\Entity\\Cart' => $baseDir . '/src/Entity/Cart.php',
'FreshMail\\Entity\\CartNotify' => $baseDir . '/src/Entity/CartNotify.php',
'FreshMail\\Entity\\Email' => $baseDir . '/src/Entity/Email.php',
'FreshMail\\Entity\\EmailToSynchronize' => $baseDir . '/src/Entity/EmailToSynchronize.php',
'FreshMail\\Entity\\EmailsSynchronized' => $baseDir . '/src/Entity/EmailsSynchronized.php',
'FreshMail\\Entity\\Form' => $baseDir . '/src/Entity/Form.php',
'FreshMail\\Entity\\Hook' => $baseDir . '/src/Entity/Hook.php',
'FreshMail\\Freshmail' => $baseDir . '/classes/Freshmail.php',
'FreshMail\\FreshmailApiV3' => $baseDir . '/classes/FreshmailApiV3.php',
'FreshMail\\FreshmailCode' => $baseDir . '/classes/FreshmailCode.php',
'FreshMail\\FreshmailList' => $baseDir . '/classes/FreshmailList.php',
'FreshMail\\Hooks' => $baseDir . '/classes/Hooks.php',
'FreshMail\\HooksForms' => $baseDir . '/classes/HooksForms.php',
'FreshMail\\Installer' => $baseDir . '/classes/installer/Installer.php',
'FreshMail\\Installer\\InstallerFactory' => $baseDir . '/classes/installer/InstallerFactory.php',
'FreshMail\\Installer\\Tabs' => $baseDir . '/classes/installer/Tabs.php',
'FreshMail\\Interfaces\\Installer' => $baseDir . '/interfaces/Installer.php',
'FreshMail\\Interfaces\\Sender' => $baseDir . '/src/Interface/Sender.php',
'FreshMail\\Repository\\AbstractRepository' => $baseDir . '/src/Repository/AbstractRepository.php',
'FreshMail\\Repository\\AsyncJobs' => $baseDir . '/src/Repository/AsyncJobs.php',
'FreshMail\\Repository\\Birthdays' => $baseDir . '/src/Repository/Birthdays.php',
'FreshMail\\Repository\\Carts' => $baseDir . '/src/Repository/Carts.php',
'FreshMail\\Repository\\EmailToSynchronize' => $baseDir . '/src/Repository/EmailToSynchronize.php',
'FreshMail\\Repository\\EmailsSynchronized' => $baseDir . '/src/Repository/EmailsSynchronized.php',
'FreshMail\\Repository\\FormRepository' => $baseDir . '/src/Repository/FormRepository.php',
'FreshMail\\Repository\\FreshmailAbandonCartSettings' => $baseDir . '/src/Repository/FreshmailAbandonCartSettings.php',
'FreshMail\\Repository\\FreshmailSettings' => $baseDir . '/src/Repository/FreshmailSettings.php',
'FreshMail\\Repository\\Subscribers' => $baseDir . '/src/Repository/Subscribers.php',
'FreshMail\\Sender\\AbstractSender' => $baseDir . '/classes/sender/AbstractSender.php',
'FreshMail\\Sender\\Birthday' => $baseDir . '/classes/sender/Birthday.php',
'FreshMail\\Sender\\Email' => $baseDir . '/classes/sender/Email.php',
'FreshMail\\Sender\\Factory' => $baseDir . '/classes/sender/Factory.php',
'FreshMail\\Sender\\FmSender' => $baseDir . '/classes/sender/FmSender.php',
'FreshMail\\Sender\\Legacy' => $baseDir . '/classes/sender/Legacy.php',
'FreshMail\\Sender\\Service\\CartData' => $baseDir . '/classes/sender/Service/CartData.php',
'FreshMail\\Sender\\Service\\CartDataCollector' => $baseDir . '/classes/sender/Service/CartDataCollector.php',
'FreshMail\\Sender\\Service\\MockCartData' => $baseDir . '/classes/sender/Service/MockCartData.php',
'FreshMail\\Service\\AbandonCartService' => $baseDir . '/src/Service/AbandonCartService.php',
'FreshMail\\Service\\AccountService' => $baseDir . '/src/Service/AccountService.php',
'FreshMail\\Service\\AsyncJobService' => $baseDir . '/src/Service/AsyncJobService.php',
'FreshMail\\Service\\AuthService' => $baseDir . '/src/Service/AuthService.php',
'FreshMail\\Service\\BirthdayService' => $baseDir . '/src/Service/BirthdayService.php',
'FreshMail\\Service\\FormService' => $baseDir . '/src/Service/FormService.php',
'FreshMail\\Subscriber' => $baseDir . '/classes/Subscriber.php',
'FreshMail\\SubscriberCollection' => $baseDir . '/classes/SubscriberCollection.php',
'FreshMail\\Tools' => $baseDir . '/classes/Tools.php',
'FreshMail\\TransactionalEmail' => $baseDir . '/classes/TransactionalEmail.php',
'Freshmail\\Entity\\AbandonedCartSettings' => $baseDir . '/src/Entity/AbandonedCartSettings.php',
'Freshmail\\Entity\\FreshmailEmail' => $baseDir . '/src/Entity/FreshmailEmail.php',
'Freshmail\\Entity\\FreshmailSetting' => $baseDir . '/src/Entity/FreshmailSetting.php',
'freshmailRestoreModuleFrontController' => $baseDir . '/controllers/front/restore.php',
);

View File

@@ -0,0 +1,9 @@
<?php
// autoload_namespaces.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);

View File

@@ -0,0 +1,10 @@
<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'FreshMail\\' => array($baseDir . '/src'),
);

View File

@@ -0,0 +1,55 @@
<?php
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit6209d2cc1bb420288929f962f08a3ac7
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
/**
* @return \Composer\Autoload\ClassLoader
*/
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit6209d2cc1bb420288929f962f08a3ac7', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit6209d2cc1bb420288929f962f08a3ac7', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit6209d2cc1bb420288929f962f08a3ac7::getInitializer($loader));
} else {
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
}
$loader->register(true);
return $loader;
}
}

View File

@@ -0,0 +1,103 @@
<?php
// autoload_static.php @generated by Composer
namespace Composer\Autoload;
class ComposerStaticInit6209d2cc1bb420288929f962f08a3ac7
{
public static $prefixLengthsPsr4 = array (
'F' =>
array (
'FreshMail\\' => 10,
),
);
public static $prefixDirsPsr4 = array (
'FreshMail\\' =>
array (
0 => __DIR__ . '/../..' . '/src',
),
);
public static $classMap = array (
'AbandonCartDiscount' => __DIR__ . '/../..' . '/src/Interface/AbandonCartDiscount.php',
'AdminFreshmailAbandonedCartConfigController' => __DIR__ . '/../..' . '/controllers/admin/AdminFreshmailAbandonedCartConfig.php',
'AdminFreshmailAjaxController' => __DIR__ . '/../..' . '/controllers/admin/AdminFreshmailAjax.php',
'AdminFreshmailBaseController' => __DIR__ . '/../..' . '/controllers/admin/AdminFreshmailBase.php',
'AdminFreshmailBirthdayController' => __DIR__ . '/../..' . '/controllers/admin/AdminFreshmailBirthday.php',
'AdminFreshmailConfigController' => __DIR__ . '/../..' . '/controllers/admin/AdminFreshmailConfig.php',
'AdminFreshmailDashboardController' => __DIR__ . '/../..' . '/controllers/admin/AdminFreshmailDashboardController.php',
'AdminFreshmailFormConfigController' => __DIR__ . '/../..' . '/controllers/admin/AdminFreshmailFormConfig.php',
'AdminFreshmailSubscribersController' => __DIR__ . '/../..' . '/controllers/admin/AdminFreshmailSubscribers.php',
'AdminFreshmailWizardController' => __DIR__ . '/../..' . '/controllers/admin/AdminFreshmailWizardController.php',
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
'FreshMail\\Discount\\AbstractDiscount' => __DIR__ . '/../..' . '/classes/discount/AbstractDiscount.php',
'FreshMail\\Discount\\Custom' => __DIR__ . '/../..' . '/classes/discount/Custom.php',
'FreshMail\\Discount\\None' => __DIR__ . '/../..' . '/classes/discount/None.php',
'FreshMail\\Discount\\Percent' => __DIR__ . '/../..' . '/classes/discount/Percent.php',
'FreshMail\\Entity\\AsyncJob' => __DIR__ . '/../..' . '/src/Entity/AsyncJob.php',
'FreshMail\\Entity\\Birthday' => __DIR__ . '/../..' . '/src/Entity/Birthday.php',
'FreshMail\\Entity\\Cart' => __DIR__ . '/../..' . '/src/Entity/Cart.php',
'FreshMail\\Entity\\CartNotify' => __DIR__ . '/../..' . '/src/Entity/CartNotify.php',
'FreshMail\\Entity\\Email' => __DIR__ . '/../..' . '/src/Entity/Email.php',
'FreshMail\\Entity\\EmailToSynchronize' => __DIR__ . '/../..' . '/src/Entity/EmailToSynchronize.php',
'FreshMail\\Entity\\EmailsSynchronized' => __DIR__ . '/../..' . '/src/Entity/EmailsSynchronized.php',
'FreshMail\\Entity\\Form' => __DIR__ . '/../..' . '/src/Entity/Form.php',
'FreshMail\\Entity\\Hook' => __DIR__ . '/../..' . '/src/Entity/Hook.php',
'FreshMail\\Freshmail' => __DIR__ . '/../..' . '/classes/Freshmail.php',
'FreshMail\\FreshmailApiV3' => __DIR__ . '/../..' . '/classes/FreshmailApiV3.php',
'FreshMail\\FreshmailCode' => __DIR__ . '/../..' . '/classes/FreshmailCode.php',
'FreshMail\\FreshmailList' => __DIR__ . '/../..' . '/classes/FreshmailList.php',
'FreshMail\\Hooks' => __DIR__ . '/../..' . '/classes/Hooks.php',
'FreshMail\\HooksForms' => __DIR__ . '/../..' . '/classes/HooksForms.php',
'FreshMail\\Installer' => __DIR__ . '/../..' . '/classes/installer/Installer.php',
'FreshMail\\Installer\\InstallerFactory' => __DIR__ . '/../..' . '/classes/installer/InstallerFactory.php',
'FreshMail\\Installer\\Tabs' => __DIR__ . '/../..' . '/classes/installer/Tabs.php',
'FreshMail\\Interfaces\\Installer' => __DIR__ . '/../..' . '/interfaces/Installer.php',
'FreshMail\\Interfaces\\Sender' => __DIR__ . '/../..' . '/src/Interface/Sender.php',
'FreshMail\\Repository\\AbstractRepository' => __DIR__ . '/../..' . '/src/Repository/AbstractRepository.php',
'FreshMail\\Repository\\AsyncJobs' => __DIR__ . '/../..' . '/src/Repository/AsyncJobs.php',
'FreshMail\\Repository\\Birthdays' => __DIR__ . '/../..' . '/src/Repository/Birthdays.php',
'FreshMail\\Repository\\Carts' => __DIR__ . '/../..' . '/src/Repository/Carts.php',
'FreshMail\\Repository\\EmailToSynchronize' => __DIR__ . '/../..' . '/src/Repository/EmailToSynchronize.php',
'FreshMail\\Repository\\EmailsSynchronized' => __DIR__ . '/../..' . '/src/Repository/EmailsSynchronized.php',
'FreshMail\\Repository\\FormRepository' => __DIR__ . '/../..' . '/src/Repository/FormRepository.php',
'FreshMail\\Repository\\FreshmailAbandonCartSettings' => __DIR__ . '/../..' . '/src/Repository/FreshmailAbandonCartSettings.php',
'FreshMail\\Repository\\FreshmailSettings' => __DIR__ . '/../..' . '/src/Repository/FreshmailSettings.php',
'FreshMail\\Repository\\Subscribers' => __DIR__ . '/../..' . '/src/Repository/Subscribers.php',
'FreshMail\\Sender\\AbstractSender' => __DIR__ . '/../..' . '/classes/sender/AbstractSender.php',
'FreshMail\\Sender\\Birthday' => __DIR__ . '/../..' . '/classes/sender/Birthday.php',
'FreshMail\\Sender\\Email' => __DIR__ . '/../..' . '/classes/sender/Email.php',
'FreshMail\\Sender\\Factory' => __DIR__ . '/../..' . '/classes/sender/Factory.php',
'FreshMail\\Sender\\FmSender' => __DIR__ . '/../..' . '/classes/sender/FmSender.php',
'FreshMail\\Sender\\Legacy' => __DIR__ . '/../..' . '/classes/sender/Legacy.php',
'FreshMail\\Sender\\Service\\CartData' => __DIR__ . '/../..' . '/classes/sender/Service/CartData.php',
'FreshMail\\Sender\\Service\\CartDataCollector' => __DIR__ . '/../..' . '/classes/sender/Service/CartDataCollector.php',
'FreshMail\\Sender\\Service\\MockCartData' => __DIR__ . '/../..' . '/classes/sender/Service/MockCartData.php',
'FreshMail\\Service\\AbandonCartService' => __DIR__ . '/../..' . '/src/Service/AbandonCartService.php',
'FreshMail\\Service\\AccountService' => __DIR__ . '/../..' . '/src/Service/AccountService.php',
'FreshMail\\Service\\AsyncJobService' => __DIR__ . '/../..' . '/src/Service/AsyncJobService.php',
'FreshMail\\Service\\AuthService' => __DIR__ . '/../..' . '/src/Service/AuthService.php',
'FreshMail\\Service\\BirthdayService' => __DIR__ . '/../..' . '/src/Service/BirthdayService.php',
'FreshMail\\Service\\FormService' => __DIR__ . '/../..' . '/src/Service/FormService.php',
'FreshMail\\Subscriber' => __DIR__ . '/../..' . '/classes/Subscriber.php',
'FreshMail\\SubscriberCollection' => __DIR__ . '/../..' . '/classes/SubscriberCollection.php',
'FreshMail\\Tools' => __DIR__ . '/../..' . '/classes/Tools.php',
'FreshMail\\TransactionalEmail' => __DIR__ . '/../..' . '/classes/TransactionalEmail.php',
'Freshmail\\Entity\\AbandonedCartSettings' => __DIR__ . '/../..' . '/src/Entity/AbandonedCartSettings.php',
'Freshmail\\Entity\\FreshmailEmail' => __DIR__ . '/../..' . '/src/Entity/FreshmailEmail.php',
'Freshmail\\Entity\\FreshmailSetting' => __DIR__ . '/../..' . '/src/Entity/FreshmailSetting.php',
'freshmailRestoreModuleFrontController' => __DIR__ . '/../..' . '/controllers/front/restore.php',
);
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit6209d2cc1bb420288929f962f08a3ac7::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit6209d2cc1bb420288929f962f08a3ac7::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit6209d2cc1bb420288929f962f08a3ac7::$classMap;
}, null, ClassLoader::class);
}
}

View File

@@ -0,0 +1,5 @@
{
"packages": [],
"dev": true,
"dev-package-names": []
}

View File

@@ -0,0 +1,18 @@
{
"require": {
"freshmail/rest-api": "^3.0",
"freshmail/php-api-client": "^1.2"
},
"autoload": {
"files" : [
"vendor-static/guzzlehttp/guzzle/src/functions_include.php",
"vendor-static/guzzlehttp/psr7/src/functions_include.php",
"vendor-static/guzzlehttp/promises/src/functions_include.php"
],
"psr-4": {
"GuzzleHttp6\\" : "vendor-static/guzzlehttp/guzzle/src",
"GuzzleHttp6\\Promise\\": "vendor-static/guzzlehttp/promises/src",
"GuzzleHttp6\\Psr7\\": "vendor-static/guzzlehttp/psr7/src"
}
}
}

View File

@@ -0,0 +1,763 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "9c8f8ca6aefc5761cb8e017c94223bc7",
"packages": [
{
"name": "freshmail/php-api-client",
"version": "1.2.1",
"source": {
"type": "git",
"url": "https://github.com/FreshMail/php-api-client.git",
"reference": "d66c94340729c026ffecca47af1d790029f98adb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/FreshMail/php-api-client/zipball/d66c94340729c026ffecca47af1d790029f98adb",
"reference": "d66c94340729c026ffecca47af1d790029f98adb",
"shasum": ""
},
"require": {
"ext-json": "*",
"guzzlehttp/guzzle": "^6.3",
"monolog/monolog": "^1.24|^2.0",
"myclabs/php-enum": "^1.7",
"php": "^7.0",
"psr/http-message": "^1.0",
"psr/log": "^1.1"
},
"require-dev": {
"symfony/var-dumper": "^4.3"
},
"type": "library",
"autoload": {
"psr-4": {
"FreshMail\\Api\\Client\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"description": "FreshMail API PHP Client",
"keywords": [
"api client",
"freshmail",
"freshmail.com",
"freshmail.pl",
"mail",
"transactional mail"
],
"time": "2020-05-29T20:20:13+00:00"
},
{
"name": "freshmail/rest-api",
"version": "v3.0.0",
"source": {
"type": "git",
"url": "https://github.com/FreshMail/REST-API.git",
"reference": "053b7ec839672c7c2bb78c7fd091af4bf0532c4f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/FreshMail/REST-API/zipball/053b7ec839672c7c2bb78c7fd091af4bf0532c4f",
"reference": "053b7ec839672c7c2bb78c7fd091af4bf0532c4f",
"shasum": ""
},
"require": {
"ext-curl": "*",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.5",
"monolog/monolog": "^1.25",
"php": ">=7.0",
"psr/log": "^1.1"
},
"type": "library",
"autoload": {
"psr-4": {
"FreshMail\\ApiV2\\": "src/FreshMail/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"GPL-3.0"
],
"authors": [
{
"name": "Tadeusz Kania",
"email": "tadeusz.kania@freshmail.pl",
"homepage": "https://freshmail.pl",
"role": "Developer"
},
{
"name": "Piotr Suszalski",
"email": "piotr.suszalski@freshmail.pl",
"homepage": "https://freshmail.pl",
"role": "Developer"
},
{
"name": "Grzegorz Gorczyca",
"email": "grzegorz.gorczyca@freshmail.pl",
"homepage": "https://freshmail.pl",
"role": "Developer"
},
{
"name": "Piotr Leżoń",
"email": "piotr.lezon@freshmail.pl",
"homepage": "https://freshmail.pl",
"role": "Developer"
}
],
"description": "A php library which implements the functionality of FreshMail REST API.",
"keywords": [
"freshmail",
"rest"
],
"time": "2020-05-20T13:27:31+00:00"
},
{
"name": "guzzlehttp/guzzle",
"version": "6.5.5",
"source": {
"type": "git",
"url": "https://github.com/guzzle/guzzle.git",
"reference": "9d4290de1cfd701f38099ef7e183b64b4b7b0c5e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/9d4290de1cfd701f38099ef7e183b64b4b7b0c5e",
"reference": "9d4290de1cfd701f38099ef7e183b64b4b7b0c5e",
"shasum": ""
},
"require": {
"ext-json": "*",
"guzzlehttp/promises": "^1.0",
"guzzlehttp/psr7": "^1.6.1",
"php": ">=5.5",
"symfony/polyfill-intl-idn": "^1.17.0"
},
"require-dev": {
"ext-curl": "*",
"phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.4 || ^7.0",
"psr/log": "^1.1"
},
"suggest": {
"psr/log": "Required for using the Log middleware"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "6.5-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp6\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"description": "Guzzle is a PHP HTTP client library",
"homepage": "http://guzzlephp.org/",
"keywords": [
"client",
"curl",
"framework",
"http",
"http client",
"rest",
"web service"
],
"time": "2020-06-16T21:01:06+00:00"
},
{
"name": "guzzlehttp/promises",
"version": "v1.3.1",
"source": {
"type": "git",
"url": "https://github.com/guzzle/promises.git",
"reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646",
"reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646",
"shasum": ""
},
"require": {
"php": ">=5.5.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.4-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp6\\Promise\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"description": "Guzzle promises library",
"keywords": [
"promise"
],
"time": "2016-12-20T10:07:11+00:00"
},
{
"name": "guzzlehttp/psr7",
"version": "1.6.1",
"source": {
"type": "git",
"url": "https://github.com/guzzle/psr7.git",
"reference": "239400de7a173fe9901b9ac7c06497751f00727a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/psr7/zipball/239400de7a173fe9901b9ac7c06497751f00727a",
"reference": "239400de7a173fe9901b9ac7c06497751f00727a",
"shasum": ""
},
"require": {
"php": ">=5.4.0",
"psr/http-message": "~1.0",
"ralouphie/getallheaders": "^2.0.5 || ^3.0.0"
},
"provide": {
"psr/http-message-implementation": "1.0"
},
"require-dev": {
"ext-zlib": "*",
"phpunit/phpunit": "~4.8.36 || ^5.7.27 || ^6.5.8"
},
"suggest": {
"zendframework/zend-httphandlerrunner": "Emit PSR-7 responses"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.6-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp6\\Psr7\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Tobias Schultze",
"homepage": "https://github.com/Tobion"
}
],
"description": "PSR-7 message implementation that also provides common utility methods",
"keywords": [
"http",
"message",
"psr-7",
"request",
"response",
"stream",
"uri",
"url"
],
"time": "2019-07-01T23:21:34+00:00"
},
{
"name": "monolog/monolog",
"version": "1.25.4",
"source": {
"type": "git",
"url": "https://github.com/Seldaek/monolog.git",
"reference": "3022efff205e2448b560c833c6fbbf91c3139168"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Seldaek/monolog/zipball/3022efff205e2448b560c833c6fbbf91c3139168",
"reference": "3022efff205e2448b560c833c6fbbf91c3139168",
"shasum": ""
},
"require": {
"php": ">=5.3.0",
"psr/log": "~1.0"
},
"provide": {
"psr/log-implementation": "1.0.0"
},
"require-dev": {
"aws/aws-sdk-php": "^2.4.9 || ^3.0",
"doctrine/couchdb": "~1.0@dev",
"graylog2/gelf-php": "~1.0",
"php-amqplib/php-amqplib": "~2.4",
"php-console/php-console": "^3.1.3",
"php-parallel-lint/php-parallel-lint": "^1.0",
"phpunit/phpunit": "~4.5",
"ruflin/elastica": ">=0.90 <3.0",
"sentry/sentry": "^0.13",
"swiftmailer/swiftmailer": "^5.3|^6.0"
},
"suggest": {
"aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB",
"doctrine/couchdb": "Allow sending log messages to a CouchDB server",
"ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)",
"ext-mongo": "Allow sending log messages to a MongoDB server",
"graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server",
"mongodb/mongodb": "Allow sending log messages to a MongoDB server via PHP Driver",
"php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib",
"php-console/php-console": "Allow sending log messages to Google Chrome",
"rollbar/rollbar": "Allow sending log messages to Rollbar",
"ruflin/elastica": "Allow sending log messages to an Elastic Search server",
"sentry/sentry": "Allow sending log messages to a Sentry server"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Monolog\\": "src/Monolog"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jordi Boggiano",
"email": "j.boggiano@seld.be",
"homepage": "http://seld.be"
}
],
"description": "Sends your logs to files, sockets, inboxes, databases and various web services",
"homepage": "http://github.com/Seldaek/monolog",
"keywords": [
"log",
"logging",
"psr-3"
],
"time": "2020-05-22T07:31:27+00:00"
},
{
"name": "myclabs/php-enum",
"version": "1.7.6",
"source": {
"type": "git",
"url": "https://github.com/myclabs/php-enum.git",
"reference": "5f36467c7a87e20fbdc51e524fd8f9d1de80187c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/myclabs/php-enum/zipball/5f36467c7a87e20fbdc51e524fd8f9d1de80187c",
"reference": "5f36467c7a87e20fbdc51e524fd8f9d1de80187c",
"shasum": ""
},
"require": {
"ext-json": "*",
"php": ">=7.1"
},
"require-dev": {
"phpunit/phpunit": "^7",
"squizlabs/php_codesniffer": "1.*",
"vimeo/psalm": "^3.8"
},
"type": "library",
"autoload": {
"psr-4": {
"MyCLabs\\Enum\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP Enum contributors",
"homepage": "https://github.com/myclabs/php-enum/graphs/contributors"
}
],
"description": "PHP Enum implementation",
"homepage": "http://github.com/myclabs/php-enum",
"keywords": [
"enum"
],
"time": "2020-02-14T08:15:52+00:00"
},
{
"name": "psr/http-message",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-message.git",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"time": "2016-08-06T14:39:51+00:00"
},
{
"name": "psr/log",
"version": "1.1.3",
"source": {
"type": "git",
"url": "https://github.com/php-fig/log.git",
"reference": "0f73288fd15629204f9d42b7055f72dacbe811fc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/0f73288fd15629204f9d42b7055f72dacbe811fc",
"reference": "0f73288fd15629204f9d42b7055f72dacbe811fc",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.1.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Log\\": "Psr/Log/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for logging libraries",
"homepage": "https://github.com/php-fig/log",
"keywords": [
"log",
"psr",
"psr-3"
],
"time": "2020-03-23T09:12:05+00:00"
},
{
"name": "ralouphie/getallheaders",
"version": "3.0.3",
"source": {
"type": "git",
"url": "https://github.com/ralouphie/getallheaders.git",
"reference": "120b605dfeb996808c31b6477290a714d356e822"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822",
"reference": "120b605dfeb996808c31b6477290a714d356e822",
"shasum": ""
},
"require": {
"php": ">=5.6"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^5 || ^6.5"
},
"type": "library",
"autoload": {
"files": [
"src/getallheaders.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ralph Khattar",
"email": "ralph.khattar@gmail.com"
}
],
"description": "A polyfill for getallheaders.",
"time": "2019-03-08T08:55:37+00:00"
},
{
"name": "symfony/polyfill-intl-idn",
"version": "v1.17.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-intl-idn.git",
"reference": "a57f8161502549a742a63c09f0a604997bf47027"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-intl-idn/zipball/a57f8161502549a742a63c09f0a604997bf47027",
"reference": "a57f8161502549a742a63c09f0a604997bf47027",
"shasum": ""
},
"require": {
"php": ">=5.3.3",
"symfony/polyfill-mbstring": "^1.3",
"symfony/polyfill-php72": "^1.10"
},
"suggest": {
"ext-intl": "For best performance"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.17-dev"
},
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
},
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Intl\\Idn\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Laurent Bassin",
"email": "laurent@bassin.info"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"idn",
"intl",
"polyfill",
"portable",
"shim"
],
"time": "2020-06-06T08:46:27+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.17.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "7110338d81ce1cbc3e273136e4574663627037a7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/7110338d81ce1cbc3e273136e4574663627037a7",
"reference": "7110338d81ce1cbc3e273136e4574663627037a7",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.17-dev"
},
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
},
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Mbstring\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for the Mbstring extension",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"mbstring",
"polyfill",
"portable",
"shim"
],
"time": "2020-06-06T08:46:27+00:00"
},
{
"name": "symfony/polyfill-php72",
"version": "v1.17.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-php72.git",
"reference": "f048e612a3905f34931127360bdd2def19a5e582"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-php72/zipball/f048e612a3905f34931127360bdd2def19a5e582",
"reference": "f048e612a3905f34931127360bdd2def19a5e582",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.17-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Php72\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"polyfill",
"portable",
"shim"
],
"time": "2020-05-12T16:47:27+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}

View File

@@ -0,0 +1,23 @@
<?php
$config = PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'declare_strict_types' => false,
'concat_space' => ['spacing'=>'one'],
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
'ordered_imports' => true,
// 'phpdoc_align' => ['align'=>'vertical'],
// 'native_function_invocation' => true,
])
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
->name('*.php')
)
;
return $config;

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,18 @@
FROM composer:latest as setup
RUN mkdir /guzzle
WORKDIR /guzzle
RUN set -xe \
&& composer init --name=guzzlehttp/test --description="Simple project for testing Guzzle scripts" --author="Márk Sági-Kazár <mark.sagikazar@gmail.com>" --no-interaction \
&& composer require guzzlehttp/guzzle
FROM php:7.3
RUN mkdir /guzzle
WORKDIR /guzzle
COPY --from=setup /guzzle /guzzle

View File

@@ -0,0 +1,19 @@
Copyright (c) 2011-2018 Michael Dowling, https://github.com/mtdowling <mtdowling@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,90 @@
Guzzle, PHP HTTP client
=======================
[![Latest Version](https://img.shields.io/github/release/guzzle/guzzle.svg?style=flat-square)](https://github.com/guzzle/guzzle/releases)
[![Build Status](https://img.shields.io/travis/guzzle/guzzle.svg?style=flat-square)](https://travis-ci.org/guzzle/guzzle)
[![Total Downloads](https://img.shields.io/packagist/dt/guzzlehttp/guzzle.svg?style=flat-square)](https://packagist.org/packages/guzzlehttp/guzzle)
Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and
trivial to integrate with web services.
- Simple interface for building query strings, POST requests, streaming large
uploads, streaming large downloads, using HTTP cookies, uploading JSON data,
etc...
- Can send both synchronous and asynchronous requests using the same interface.
- Uses PSR-7 interfaces for requests, responses, and streams. This allows you
to utilize other PSR-7 compatible libraries with Guzzle.
- Abstracts away the underlying HTTP transport, allowing you to write
environment and transport agnostic code; i.e., no hard dependency on cURL,
PHP streams, sockets, or non-blocking event loops.
- Middleware system allows you to augment and compose client behavior.
```php
$client = new \GuzzleHttp6\Client();
$response = $client->request('GET', 'https://api.github.com/repos/guzzle/guzzle');
echo $response->getStatusCode(); # 200
echo $response->getHeaderLine('content-type'); # 'application/json; charset=utf8'
echo $response->getBody(); # '{"id": 1420053, "name": "guzzle", ...}'
# Send an asynchronous request.
$request = new \GuzzleHttp6\Psr7\Request('GET', 'http://httpbin.org');
$promise = $client->sendAsync($request)->then(function ($response) {
echo 'I completed! ' . $response->getBody();
});
$promise->wait();
```
## Help and docs
- [Documentation](http://guzzlephp.org/)
- [Stack Overflow](http://stackoverflow.com/questions/tagged/guzzle)
- [Gitter](https://gitter.im/guzzle/guzzle)
## Installing Guzzle
The recommended way to install Guzzle is through
[Composer](http://getcomposer.org).
```bash
# Install Composer
curl -sS https://getcomposer.org/installer | php
```
Next, run the Composer command to install the latest stable version of Guzzle:
```bash
composer require guzzlehttp/guzzle
```
After installing, you need to require Composer's autoloader:
```php
require 'vendor/autoload.php';
```
You can then later update Guzzle using composer:
```bash
composer update
```
## Version Guidance
| Version | Status | Packagist | Namespace | Repo | Docs | PSR-7 | PHP Version |
|---------|------------|---------------------|--------------|---------------------|---------------------|-------|-------------|
| 3.x | EOL | `guzzle/guzzle` | `Guzzle` | [v3][guzzle-3-repo] | [v3][guzzle-3-docs] | No | >= 5.3.3 |
| 4.x | EOL | `guzzlehttp/guzzle` | `GuzzleHttp` | [v4][guzzle-4-repo] | N/A | No | >= 5.4 |
| 5.x | EOL | `guzzlehttp/guzzle` | `GuzzleHttp` | [v5][guzzle-5-repo] | [v5][guzzle-5-docs] | No | >= 5.4 |
| 6.x | Latest | `guzzlehttp/guzzle` | `GuzzleHttp` | [v6][guzzle-6-repo] | [v6][guzzle-6-docs] | Yes | >= 5.5 |
[guzzle-3-repo]: https://github.com/guzzle/guzzle3
[guzzle-4-repo]: https://github.com/guzzle/guzzle/tree/4.x
[guzzle-5-repo]: https://github.com/guzzle/guzzle/tree/5.3
[guzzle-6-repo]: https://github.com/guzzle/guzzle
[guzzle-3-docs]: http://guzzle3.readthedocs.org
[guzzle-5-docs]: http://guzzle.readthedocs.org/en/5.3/
[guzzle-6-docs]: http://guzzle.readthedocs.org/en/latest/

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,59 @@
{
"name": "guzzlehttp/guzzle",
"type": "library",
"description": "Guzzle is a PHP HTTP client library",
"keywords": [
"framework",
"http",
"rest",
"web service",
"curl",
"client",
"HTTP client"
],
"homepage": "http://guzzlephp.org/",
"license": "MIT",
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"require": {
"php": ">=5.5",
"ext-json": "*",
"symfony/polyfill-intl-idn": "^1.17.0",
"guzzlehttp/promises": "^1.0",
"guzzlehttp/psr7": "^1.6.1"
},
"require-dev": {
"ext-curl": "*",
"phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.4 || ^7.0",
"psr/log": "^1.1"
},
"suggest": {
"psr/log": "Required for using the Log middleware"
},
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "6.5-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp6\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"autoload-dev": {
"psr-4": {
"GuzzleHttp6\\Tests\\": "tests/"
}
}
}

View File

@@ -0,0 +1,504 @@
<?php
namespace GuzzleHttp6;
use Exception;
use GuzzleHttp6\Cookie\CookieJar;
use GuzzleHttp6\Exception\GuzzleException;
use GuzzleHttp6\Promise;
use GuzzleHttp6\Psr7;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
/**
* @method ResponseInterface get(string|UriInterface $uri, array $options = [])
* @method ResponseInterface head(string|UriInterface $uri, array $options = [])
* @method ResponseInterface put(string|UriInterface $uri, array $options = [])
* @method ResponseInterface post(string|UriInterface $uri, array $options = [])
* @method ResponseInterface patch(string|UriInterface $uri, array $options = [])
* @method ResponseInterface delete(string|UriInterface $uri, array $options = [])
* @method Promise\PromiseInterface getAsync(string|UriInterface $uri, array $options = [])
* @method Promise\PromiseInterface headAsync(string|UriInterface $uri, array $options = [])
* @method Promise\PromiseInterface putAsync(string|UriInterface $uri, array $options = [])
* @method Promise\PromiseInterface postAsync(string|UriInterface $uri, array $options = [])
* @method Promise\PromiseInterface patchAsync(string|UriInterface $uri, array $options = [])
* @method Promise\PromiseInterface deleteAsync(string|UriInterface $uri, array $options = [])
*/
class Client implements ClientInterface
{
/** @var array Default request options */
private $config;
/**
* Clients accept an array of constructor parameters.
*
* Here's an example of creating a client using a base_uri and an array of
* default request options to apply to each request:
*
* $client = new Client([
* 'base_uri' => 'http://www.foo.com/1.0/',
* 'timeout' => 0,
* 'allow_redirects' => false,
* 'proxy' => '192.168.16.1:10'
* ]);
*
* Client configuration settings include the following options:
*
* - handler: (callable) Function that transfers HTTP requests over the
* wire. The function is called with a Psr7\Http\Message\RequestInterface
* and array of transfer options, and must return a
* GuzzleHttp6\Promise\PromiseInterface that is fulfilled with a
* Psr7\Http\Message\ResponseInterface on success.
* If no handler is provided, a default handler will be created
* that enables all of the request options below by attaching all of the
* default middleware to the handler.
* - base_uri: (string|UriInterface) Base URI of the client that is merged
* into relative URIs. Can be a string or instance of UriInterface.
* - **: any request option
*
* @param array $config Client configuration settings.
*
* @see \GuzzleHttp6\RequestOptions for a list of available request options.
*/
public function __construct(array $config = [])
{
if (!isset($config['handler'])) {
$config['handler'] = HandlerStack::create();
} elseif (!is_callable($config['handler'])) {
throw new InvalidArgumentException('handler must be a callable');
}
// Convert the base_uri to a UriInterface
if (isset($config['base_uri'])) {
$config['base_uri'] = Psr7\uri_for($config['base_uri']);
}
$this->configureDefaults($config);
}
/**
* @param string $method
* @param array $args
*
* @return Promise\PromiseInterface
*/
public function __call($method, $args)
{
if (count($args) < 1) {
throw new InvalidArgumentException('Magic request methods require a URI and optional options array');
}
$uri = $args[0];
$opts = isset($args[1]) ? $args[1] : [];
return substr($method, -5) === 'Async'
? $this->requestAsync(substr($method, 0, -5), $uri, $opts)
: $this->request($method, $uri, $opts);
}
/**
* Asynchronously send an HTTP request.
*
* @param array $options Request options to apply to the given
* request and to the transfer. See \GuzzleHttp6\RequestOptions.
*
* @return Promise\PromiseInterface
*/
public function sendAsync(RequestInterface $request, array $options = [])
{
// Merge the base URI into the request URI if needed.
$options = $this->prepareDefaults($options);
return $this->transfer(
$request->withUri($this->buildUri($request->getUri(), $options), $request->hasHeader('Host')),
$options
);
}
/**
* Send an HTTP request.
*
* @param array $options Request options to apply to the given
* request and to the transfer. See \GuzzleHttp6\RequestOptions.
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function send(RequestInterface $request, array $options = [])
{
$options[RequestOptions::SYNCHRONOUS] = true;
return $this->sendAsync($request, $options)->wait();
}
/**
* Create and send an asynchronous HTTP request.
*
* Use an absolute path to override the base path of the client, or a
* relative path to append to the base path of the client. The URL can
* contain the query string as well. Use an array to provide a URL
* template and additional variables to use in the URL template expansion.
*
* @param string $method HTTP method
* @param string|UriInterface $uri URI object or string.
* @param array $options Request options to apply. See \GuzzleHttp6\RequestOptions.
*
* @return Promise\PromiseInterface
*/
public function requestAsync($method, $uri = '', array $options = [])
{
$options = $this->prepareDefaults($options);
// Remove request modifying parameter because it can be done up-front.
$headers = isset($options['headers']) ? $options['headers'] : [];
$body = isset($options['body']) ? $options['body'] : null;
$version = isset($options['version']) ? $options['version'] : '1.1';
// Merge the URI into the base URI.
$uri = $this->buildUri($uri, $options);
if (is_array($body)) {
$this->invalidBody();
}
$request = new Psr7\Request($method, $uri, $headers, $body, $version);
// Remove the option so that they are not doubly-applied.
unset($options['headers'], $options['body'], $options['version']);
return $this->transfer($request, $options);
}
/**
* Create and send an HTTP request.
*
* Use an absolute path to override the base path of the client, or a
* relative path to append to the base path of the client. The URL can
* contain the query string as well.
*
* @param string $method HTTP method.
* @param string|UriInterface $uri URI object or string.
* @param array $options Request options to apply. See \GuzzleHttp6\RequestOptions.
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function request($method, $uri = '', array $options = [])
{
$options[RequestOptions::SYNCHRONOUS] = true;
return $this->requestAsync($method, $uri, $options)->wait();
}
/**
* Get a client configuration option.
*
* These options include default request options of the client, a "handler"
* (if utilized by the concrete client), and a "base_uri" if utilized by
* the concrete client.
*
* @param string|null $option The config option to retrieve.
*
* @return mixed
*/
public function getConfig($option = null)
{
return $option === null
? $this->config
: (isset($this->config[$option]) ? $this->config[$option] : null);
}
/**
* @param string|null $uri
*
* @return UriInterface
*/
private function buildUri($uri, array $config)
{
// for BC we accept null which would otherwise fail in uri_for
$uri = Psr7\uri_for($uri === null ? '' : $uri);
if (isset($config['base_uri'])) {
$uri = Psr7\UriResolver::resolve(Psr7\uri_for($config['base_uri']), $uri);
}
if (isset($config['idn_conversion']) && ($config['idn_conversion'] !== false)) {
$idnOptions = ($config['idn_conversion'] === true) ? IDNA_DEFAULT : $config['idn_conversion'];
$uri = Utils::idnUriConvert($uri, $idnOptions);
}
return $uri->getScheme() === '' && $uri->getHost() !== '' ? $uri->withScheme('http') : $uri;
}
/**
* Configures the default options for a client.
*
* @param array $config
* @return void
*/
private function configureDefaults(array $config)
{
$defaults = [
'allow_redirects' => RedirectMiddleware::$defaultSettings,
'http_errors' => true,
'decode_content' => true,
'verify' => true,
'cookies' => false,
'idn_conversion' => true,
];
// Use the standard Linux HTTP_PROXY and HTTPS_PROXY if set.
// We can only trust the HTTP_PROXY environment variable in a CLI
// process due to the fact that PHP has no reliable mechanism to
// get environment variables that start with "HTTP_".
if (php_sapi_name() === 'cli' && getenv('HTTP_PROXY')) {
$defaults['proxy']['http'] = getenv('HTTP_PROXY');
}
if ($proxy = getenv('HTTPS_PROXY')) {
$defaults['proxy']['https'] = $proxy;
}
if ($noProxy = getenv('NO_PROXY')) {
$cleanedNoProxy = str_replace(' ', '', $noProxy);
$defaults['proxy']['no'] = explode(',', $cleanedNoProxy);
}
$this->config = $config + $defaults;
if (!empty($config['cookies']) && $config['cookies'] === true) {
$this->config['cookies'] = new CookieJar();
}
// Add the default user-agent header.
if (!isset($this->config['headers'])) {
$this->config['headers'] = ['User-Agent' => default_user_agent()];
} else {
// Add the User-Agent header if one was not already set.
foreach (array_keys($this->config['headers']) as $name) {
if (strtolower($name) === 'user-agent') {
return;
}
}
$this->config['headers']['User-Agent'] = default_user_agent();
}
}
/**
* Merges default options into the array.
*
* @param array $options Options to modify by reference
*
* @return array
*/
private function prepareDefaults(array $options)
{
$defaults = $this->config;
if (!empty($defaults['headers'])) {
// Default headers are only added if they are not present.
$defaults['_conditional'] = $defaults['headers'];
unset($defaults['headers']);
}
// Special handling for headers is required as they are added as
// conditional headers and as headers passed to a request ctor.
if (array_key_exists('headers', $options)) {
// Allows default headers to be unset.
if ($options['headers'] === null) {
$defaults['_conditional'] = [];
unset($options['headers']);
} elseif (!is_array($options['headers'])) {
throw new InvalidArgumentException('headers must be an array');
}
}
// Shallow merge defaults underneath options.
$result = $options + $defaults;
// Remove null values.
foreach ($result as $k => $v) {
if ($v === null) {
unset($result[$k]);
}
}
return $result;
}
/**
* Transfers the given request and applies request options.
*
* The URI of the request is not modified and the request options are used
* as-is without merging in default options.
*
* @param array $options See \GuzzleHttp6\RequestOptions.
*
* @return Promise\PromiseInterface
*/
private function transfer(RequestInterface $request, array $options)
{
// save_to -> sink
if (isset($options['save_to'])) {
$options['sink'] = $options['save_to'];
unset($options['save_to']);
}
// exceptions -> http_errors
if (isset($options['exceptions'])) {
$options['http_errors'] = $options['exceptions'];
unset($options['exceptions']);
}
$request = $this->applyOptions($request, $options);
/** @var HandlerStack $handler */
$handler = $options['handler'];
try {
return Promise\promise_for($handler($request, $options));
} catch (Exception $e) {
return Promise\rejection_for($e);
}
}
/**
* Applies the array of request options to a request.
*
* @param RequestInterface $request
* @param array $options
*
* @return RequestInterface
*/
private function applyOptions(RequestInterface $request, array &$options)
{
$modify = [
'set_headers' => [],
];
if (isset($options['headers'])) {
$modify['set_headers'] = $options['headers'];
unset($options['headers']);
}
if (isset($options['form_params'])) {
if (isset($options['multipart'])) {
throw new InvalidArgumentException('You cannot use '
. 'form_params and multipart at the same time. Use the '
. 'form_params option if you want to send application/'
. 'x-www-form-urlencoded requests, and the multipart '
. 'option to send multipart/form-data requests.');
}
$options['body'] = http_build_query($options['form_params'], '', '&');
unset($options['form_params']);
// Ensure that we don't have the header in different case and set the new value.
$options['_conditional'] = Psr7\_caseless_remove(['Content-Type'], $options['_conditional']);
$options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded';
}
if (isset($options['multipart'])) {
$options['body'] = new Psr7\MultipartStream($options['multipart']);
unset($options['multipart']);
}
if (isset($options['json'])) {
$options['body'] = json_encode($options['json']);
unset($options['json']);
// Ensure that we don't have the header in different case and set the new value.
$options['_conditional'] = Psr7\_caseless_remove(['Content-Type'], $options['_conditional']);
$options['_conditional']['Content-Type'] = 'application/json';
}
if (!empty($options['decode_content'])
&& $options['decode_content'] !== true
) {
// Ensure that we don't have the header in different case and set the new value.
$options['_conditional'] = Psr7\_caseless_remove(['Accept-Encoding'], $options['_conditional']);
$modify['set_headers']['Accept-Encoding'] = $options['decode_content'];
}
if (isset($options['body'])) {
if (is_array($options['body'])) {
$this->invalidBody();
}
$modify['body'] = Psr7\stream_for($options['body']);
unset($options['body']);
}
if (!empty($options['auth']) && is_array($options['auth'])) {
$value = $options['auth'];
$type = isset($value[2]) ? strtolower($value[2]) : 'basic';
switch ($type) {
case 'basic':
// Ensure that we don't have the header in different case and set the new value.
$modify['set_headers'] = Psr7\_caseless_remove(['Authorization'], $modify['set_headers']);
$modify['set_headers']['Authorization'] = 'Basic '
. base64_encode("$value[0]:$value[1]");
break;
case 'digest':
// @todo: Do not rely on curl
$options['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_DIGEST;
$options['curl'][CURLOPT_USERPWD] = "$value[0]:$value[1]";
break;
case 'ntlm':
$options['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_NTLM;
$options['curl'][CURLOPT_USERPWD] = "$value[0]:$value[1]";
break;
}
}
if (isset($options['query'])) {
$value = $options['query'];
if (is_array($value)) {
$value = http_build_query($value, null, '&', PHP_QUERY_RFC3986);
}
if (!is_string($value)) {
throw new InvalidArgumentException('query must be a string or array');
}
$modify['query'] = $value;
unset($options['query']);
}
// Ensure that sink is not an invalid value.
if (isset($options['sink'])) {
// TODO: Add more sink validation?
if (is_bool($options['sink'])) {
throw new InvalidArgumentException('sink must not be a boolean');
}
}
$request = Psr7\modify_request($request, $modify);
if ($request->getBody() instanceof Psr7\MultipartStream) {
// Use a multipart/form-data POST if a Content-Type is not set.
// Ensure that we don't have the header in different case and set the new value.
$options['_conditional'] = Psr7\_caseless_remove(['Content-Type'], $options['_conditional']);
$options['_conditional']['Content-Type'] = 'multipart/form-data; boundary='
. $request->getBody()->getBoundary();
}
// Merge in conditional headers if they are not present.
if (isset($options['_conditional'])) {
// Build up the changes so it's in a single clone of the message.
$modify = [];
foreach ($options['_conditional'] as $k => $v) {
if (!$request->hasHeader($k)) {
$modify['set_headers'][$k] = $v;
}
}
$request = Psr7\modify_request($request, $modify);
// Don't pass this internal value along to middleware/handlers.
unset($options['_conditional']);
}
return $request;
}
/**
* Throw Exception with pre-set message.
* @return void
* @throws InvalidArgumentException Invalid body.
*/
private function invalidBody()
{
throw new InvalidArgumentException('Passing in the "body" request '
. 'option as an array to send a POST request has been deprecated. '
. 'Please use the "form_params" request option to send a '
. 'application/x-www-form-urlencoded request, or the "multipart" '
. 'request option to send a multipart/form-data request.');
}
}

View File

@@ -0,0 +1,88 @@
<?php
namespace GuzzleHttp6;
use GuzzleHttp6\Exception\GuzzleException;
use GuzzleHttp6\Promise\PromiseInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
/**
* Client interface for sending HTTP requests.
*/
interface ClientInterface
{
/**
* @deprecated Will be removed in Guzzle 7.0.0
*/
const VERSION = '6.5.5';
/**
* Send an HTTP request.
*
* @param RequestInterface $request Request to send
* @param array $options Request options to apply to the given
* request and to the transfer.
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function send(RequestInterface $request, array $options = []);
/**
* Asynchronously send an HTTP request.
*
* @param RequestInterface $request Request to send
* @param array $options Request options to apply to the given
* request and to the transfer.
*
* @return PromiseInterface
*/
public function sendAsync(RequestInterface $request, array $options = []);
/**
* Create and send an HTTP request.
*
* Use an absolute path to override the base path of the client, or a
* relative path to append to the base path of the client. The URL can
* contain the query string as well.
*
* @param string $method HTTP method.
* @param string|UriInterface $uri URI object or string.
* @param array $options Request options to apply.
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function request($method, $uri, array $options = []);
/**
* Create and send an asynchronous HTTP request.
*
* Use an absolute path to override the base path of the client, or a
* relative path to append to the base path of the client. The URL can
* contain the query string as well. Use an array to provide a URL
* template and additional variables to use in the URL template expansion.
*
* @param string $method HTTP method
* @param string|UriInterface $uri URI object or string.
* @param array $options Request options to apply.
*
* @return PromiseInterface
*/
public function requestAsync($method, $uri, array $options = []);
/**
* Get a client configuration option.
*
* These options include default request options of the client, a "handler"
* (if utilized by the concrete client), and a "base_uri" if utilized by
* the concrete client.
*
* @param string|null $option The config option to retrieve.
*
* @return mixed
*/
public function getConfig($option = null);
}

View File

@@ -0,0 +1,321 @@
<?php
namespace GuzzleHttp6\Cookie;
use ArrayIterator;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
/**
* Cookie jar that stores cookies as an array
*/
class CookieJar implements CookieJarInterface
{
/** @var SetCookie[] Loaded cookie data */
private $cookies = [];
/** @var bool */
private $strictMode;
/**
* @param bool $strictMode Set to true to throw exceptions when invalid
* cookies are added to the cookie jar.
* @param array $cookieArray Array of SetCookie objects or a hash of
* arrays that can be used with the SetCookie
* constructor
*/
public function __construct($strictMode = false, $cookieArray = [])
{
$this->strictMode = $strictMode;
foreach ($cookieArray as $cookie) {
if (!($cookie instanceof SetCookie)) {
$cookie = new SetCookie($cookie);
}
$this->setCookie($cookie);
}
}
/**
* Create a new Cookie jar from an associative array and domain.
*
* @param array $cookies Cookies to create the jar from
* @param string $domain Domain to set the cookies to
*
* @return self
*/
public static function fromArray(array $cookies, $domain)
{
$cookieJar = new self();
foreach ($cookies as $name => $value) {
$cookieJar->setCookie(new SetCookie([
'Domain' => $domain,
'Name' => $name,
'Value' => $value,
'Discard' => true
]));
}
return $cookieJar;
}
/**
* @deprecated
*/
public static function getCookieValue($value)
{
return $value;
}
/**
* Evaluate if this cookie should be persisted to storage
* that survives between requests.
*
* @param SetCookie $cookie Being evaluated.
* @param bool $allowSessionCookies If we should persist session cookies
* @return bool
*/
public static function shouldPersist(
SetCookie $cookie,
$allowSessionCookies = false
)
{
if ($cookie->getExpires() || $allowSessionCookies) {
if (!$cookie->getDiscard()) {
return true;
}
}
return false;
}
/**
* Finds and returns the cookie based on the name
*
* @param string $name cookie name to search for
* @return SetCookie|null cookie that was found or null if not found
*/
public function getCookieByName($name)
{
// don't allow a non string name
if ($name === null || !is_scalar($name)) {
return null;
}
foreach ($this->cookies as $cookie) {
if ($cookie->getName() !== null && strcasecmp($cookie->getName(), $name) === 0) {
return $cookie;
}
}
return null;
}
public function toArray()
{
return array_map(function (SetCookie $cookie) {
return $cookie->toArray();
}, $this->getIterator()->getArrayCopy());
}
public function clear($domain = null, $path = null, $name = null)
{
if (!$domain) {
$this->cookies = [];
return;
} elseif (!$path) {
$this->cookies = array_filter(
$this->cookies,
function (SetCookie $cookie) use ($domain) {
return !$cookie->matchesDomain($domain);
}
);
} elseif (!$name) {
$this->cookies = array_filter(
$this->cookies,
function (SetCookie $cookie) use ($path, $domain) {
return !($cookie->matchesPath($path) &&
$cookie->matchesDomain($domain));
}
);
} else {
$this->cookies = array_filter(
$this->cookies,
function (SetCookie $cookie) use ($path, $domain, $name) {
return !($cookie->getName() == $name &&
$cookie->matchesPath($path) &&
$cookie->matchesDomain($domain));
}
);
}
}
public function clearSessionCookies()
{
$this->cookies = array_filter(
$this->cookies,
function (SetCookie $cookie) {
return !$cookie->getDiscard() && $cookie->getExpires();
}
);
}
public function setCookie(SetCookie $cookie)
{
// If the name string is empty (but not 0), ignore the set-cookie
// string entirely.
$name = $cookie->getName();
if (!$name && $name !== '0') {
return false;
}
// Only allow cookies with set and valid domain, name, value
$result = $cookie->validate();
if ($result !== true) {
if ($this->strictMode) {
throw new RuntimeException('Invalid cookie: ' . $result);
} else {
$this->removeCookieIfEmpty($cookie);
return false;
}
}
// Resolve conflicts with previously set cookies
foreach ($this->cookies as $i => $c) {
// Two cookies are identical, when their path, and domain are
// identical.
if ($c->getPath() != $cookie->getPath() ||
$c->getDomain() != $cookie->getDomain() ||
$c->getName() != $cookie->getName()
) {
continue;
}
// The previously set cookie is a discard cookie and this one is
// not so allow the new cookie to be set
if (!$cookie->getDiscard() && $c->getDiscard()) {
unset($this->cookies[$i]);
continue;
}
// If the new cookie's expiration is further into the future, then
// replace the old cookie
if ($cookie->getExpires() > $c->getExpires()) {
unset($this->cookies[$i]);
continue;
}
// If the value has changed, we better change it
if ($cookie->getValue() !== $c->getValue()) {
unset($this->cookies[$i]);
continue;
}
// The cookie exists, so no need to continue
return false;
}
$this->cookies[] = $cookie;
return true;
}
public function count()
{
return count($this->cookies);
}
public function getIterator()
{
return new ArrayIterator(array_values($this->cookies));
}
public function extractCookies(
RequestInterface $request,
ResponseInterface $response
)
{
if ($cookieHeader = $response->getHeader('Set-Cookie')) {
foreach ($cookieHeader as $cookie) {
$sc = SetCookie::fromString($cookie);
if (!$sc->getDomain()) {
$sc->setDomain($request->getUri()->getHost());
}
if (0 !== strpos($sc->getPath(), '/')) {
$sc->setPath($this->getCookiePathFromRequest($request));
}
$this->setCookie($sc);
}
}
}
/**
* Computes cookie path following RFC 6265 section 5.1.4
*
* @link https://tools.ietf.org/html/rfc6265#section-5.1.4
*
* @param RequestInterface $request
* @return string
*/
private function getCookiePathFromRequest(RequestInterface $request)
{
$uriPath = $request->getUri()->getPath();
if ('' === $uriPath) {
return '/';
}
if (0 !== strpos($uriPath, '/')) {
return '/';
}
if ('/' === $uriPath) {
return '/';
}
if (0 === $lastSlashPos = strrpos($uriPath, '/')) {
return '/';
}
return substr($uriPath, 0, $lastSlashPos);
}
public function withCookieHeader(RequestInterface $request)
{
$values = [];
$uri = $request->getUri();
$scheme = $uri->getScheme();
$host = $uri->getHost();
$path = $uri->getPath() ?: '/';
foreach ($this->cookies as $cookie) {
if ($cookie->matchesPath($path) &&
$cookie->matchesDomain($host) &&
!$cookie->isExpired() &&
(!$cookie->getSecure() || $scheme === 'https')
) {
$values[] = $cookie->getName() . '='
. $cookie->getValue();
}
}
return $values
? $request->withHeader('Cookie', implode('; ', $values))
: $request;
}
/**
* If a cookie already exists and the server asks to set it again with a
* null value, the cookie must be deleted.
*
* @param SetCookie $cookie
*/
private function removeCookieIfEmpty(SetCookie $cookie)
{
$cookieValue = $cookie->getValue();
if ($cookieValue === null || $cookieValue === '') {
$this->clear(
$cookie->getDomain(),
$cookie->getPath(),
$cookie->getName()
);
}
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace GuzzleHttp6\Cookie;
use Countable;
use IteratorAggregate;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Stores HTTP cookies.
*
* It extracts cookies from HTTP requests, and returns them in HTTP responses.
* CookieJarInterface instances automatically expire contained cookies when
* necessary. Subclasses are also responsible for storing and retrieving
* cookies from a file, database, etc.
*
* @link http://docs.python.org/2/library/cookielib.html Inspiration
*/
interface CookieJarInterface extends Countable, IteratorAggregate
{
/**
* Create a request with added cookie headers.
*
* If no matching cookies are found in the cookie jar, then no Cookie
* header is added to the request and the same request is returned.
*
* @param RequestInterface $request Request object to modify.
*
* @return RequestInterface returns the modified request.
*/
public function withCookieHeader(RequestInterface $request);
/**
* Extract cookies from an HTTP response and store them in the CookieJar.
*
* @param RequestInterface $request Request that was sent
* @param ResponseInterface $response Response that was received
*/
public function extractCookies(
RequestInterface $request,
ResponseInterface $response
);
/**
* Sets a cookie in the cookie jar.
*
* @param SetCookie $cookie Cookie to set.
*
* @return bool Returns true on success or false on failure
*/
public function setCookie(SetCookie $cookie);
/**
* Remove cookies currently held in the cookie jar.
*
* Invoking this method without arguments will empty the whole cookie jar.
* If given a $domain argument only cookies belonging to that domain will
* be removed. If given a $domain and $path argument, cookies belonging to
* the specified path within that domain are removed. If given all three
* arguments, then the cookie with the specified name, path and domain is
* removed.
*
* @param string|null $domain Clears cookies matching a domain
* @param string|null $path Clears cookies matching a domain and path
* @param string|null $name Clears cookies matching a domain, path, and name
*
* @return CookieJarInterface
*/
public function clear($domain = null, $path = null, $name = null);
/**
* Discard all sessions cookies.
*
* Removes cookies that don't have an expire field or a have a discard
* field set to true. To be called when the user agent shuts down according
* to RFC 2965.
*/
public function clearSessionCookies();
/**
* Converts the cookie jar to an array.
*
* @return array
*/
public function toArray();
}

View File

@@ -0,0 +1,94 @@
<?php
namespace GuzzleHttp6\Cookie;
use RuntimeException;
/**
* Persists non-session cookies using a JSON formatted file
*/
class FileCookieJar extends CookieJar
{
/** @var string filename */
private $filename;
/** @var bool Control whether to persist session cookies or not. */
private $storeSessionCookies;
/**
* Create a new FileCookieJar object
*
* @param string $cookieFile File to store the cookie data
* @param bool $storeSessionCookies Set to true to store session cookies
* in the cookie jar.
*
* @throws RuntimeException if the file cannot be found or created
*/
public function __construct($cookieFile, $storeSessionCookies = false)
{
parent::__construct();
$this->filename = $cookieFile;
$this->storeSessionCookies = $storeSessionCookies;
if (file_exists($cookieFile)) {
$this->load($cookieFile);
}
}
/**
* Saves the file when shutting down
*/
public function __destruct()
{
$this->save($this->filename);
}
/**
* Saves the cookies to a file.
*
* @param string $filename File to save
* @throws RuntimeException if the file cannot be found or created
*/
public function save($filename)
{
$json = [];
foreach ($this as $cookie) {
/** @var SetCookie $cookie */
if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) {
$json[] = $cookie->toArray();
}
}
$jsonStr = \GuzzleHttp6\json_encode($json);
if (false === file_put_contents($filename, $jsonStr, LOCK_EX)) {
throw new RuntimeException("Unable to save file {$filename}");
}
}
/**
* Load cookies from a JSON formatted file.
*
* Old cookies are kept unless overwritten by newly loaded ones.
*
* @param string $filename Cookie file to load.
* @throws RuntimeException if the file cannot be loaded.
*/
public function load($filename)
{
$json = file_get_contents($filename);
if (false === $json) {
throw new RuntimeException("Unable to load file {$filename}");
} elseif ($json === '') {
return;
}
$data = \GuzzleHttp6\json_decode($json, true);
if (is_array($data)) {
foreach (json_decode($json, true) as $cookie) {
$this->setCookie(new SetCookie($cookie));
}
} elseif (strlen($data)) {
throw new RuntimeException("Invalid cookie file: {$filename}");
}
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace GuzzleHttp6\Cookie;
use RuntimeException;
/**
* Persists cookies in the client session
*/
class SessionCookieJar extends CookieJar
{
/** @var string session key */
private $sessionKey;
/** @var bool Control whether to persist session cookies or not. */
private $storeSessionCookies;
/**
* Create a new SessionCookieJar object
*
* @param string $sessionKey Session key name to store the cookie
* data in session
* @param bool $storeSessionCookies Set to true to store session cookies
* in the cookie jar.
*/
public function __construct($sessionKey, $storeSessionCookies = false)
{
parent::__construct();
$this->sessionKey = $sessionKey;
$this->storeSessionCookies = $storeSessionCookies;
$this->load();
}
/**
* Saves cookies to session when shutting down
*/
public function __destruct()
{
$this->save();
}
/**
* Save cookies to the client session
*/
public function save()
{
$json = [];
foreach ($this as $cookie) {
/** @var SetCookie $cookie */
if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) {
$json[] = $cookie->toArray();
}
}
$_SESSION[$this->sessionKey] = json_encode($json);
}
/**
* Load the contents of the client session into the data array
*/
protected function load()
{
if (!isset($_SESSION[$this->sessionKey])) {
return;
}
$data = json_decode($_SESSION[$this->sessionKey], true);
if (is_array($data)) {
foreach ($data as $cookie) {
$this->setCookie(new SetCookie($cookie));
}
} elseif (strlen($data)) {
throw new RuntimeException("Invalid cookie data");
}
}
}

View File

@@ -0,0 +1,404 @@
<?php
namespace GuzzleHttp6\Cookie;
/**
* Set-Cookie object
*/
class SetCookie
{
/** @var array */
private static $defaults = [
'Name' => null,
'Value' => null,
'Domain' => null,
'Path' => '/',
'Max-Age' => null,
'Expires' => null,
'Secure' => false,
'Discard' => false,
'HttpOnly' => false
];
/** @var array Cookie data */
private $data;
/**
* Create a new SetCookie object from a string
*
* @param string $cookie Set-Cookie header string
*
* @return self
*/
public static function fromString($cookie)
{
// Create the default return array
$data = self::$defaults;
// Explode the cookie string using a series of semicolons
$pieces = array_filter(array_map('trim', explode(';', $cookie)));
// The name of the cookie (first kvp) must exist and include an equal sign.
if (empty($pieces[0]) || !strpos($pieces[0], '=')) {
return new self($data);
}
// Add the cookie pieces into the parsed data array
foreach ($pieces as $part) {
$cookieParts = explode('=', $part, 2);
$key = trim($cookieParts[0]);
$value = isset($cookieParts[1])
? trim($cookieParts[1], " \n\r\t\0\x0B")
: true;
// Only check for non-cookies when cookies have been found
if (empty($data['Name'])) {
$data['Name'] = $key;
$data['Value'] = $value;
} else {
foreach (array_keys(self::$defaults) as $search) {
if (!strcasecmp($search, $key)) {
$data[$search] = $value;
continue 2;
}
}
$data[$key] = $value;
}
}
return new self($data);
}
/**
* @param array $data Array of cookie data provided by a Cookie parser
*/
public function __construct(array $data = [])
{
$this->data = array_replace(self::$defaults, $data);
// Extract the Expires value and turn it into a UNIX timestamp if needed
if (!$this->getExpires() && $this->getMaxAge()) {
// Calculate the Expires date
$this->setExpires(time() + $this->getMaxAge());
} elseif ($this->getExpires() && !is_numeric($this->getExpires())) {
$this->setExpires($this->getExpires());
}
}
public function __toString()
{
$str = $this->data['Name'] . '=' . $this->data['Value'] . '; ';
foreach ($this->data as $k => $v) {
if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) {
if ($k === 'Expires') {
$str .= 'Expires=' . gmdate('D, d M Y H:i:s \G\M\T', $v) . '; ';
} else {
$str .= ($v === true ? $k : "{$k}={$v}") . '; ';
}
}
}
return rtrim($str, '; ');
}
public function toArray()
{
return $this->data;
}
/**
* Get the cookie name
*
* @return string
*/
public function getName()
{
return $this->data['Name'];
}
/**
* Set the cookie name
*
* @param string $name Cookie name
*/
public function setName($name)
{
$this->data['Name'] = $name;
}
/**
* Get the cookie value
*
* @return string
*/
public function getValue()
{
return $this->data['Value'];
}
/**
* Set the cookie value
*
* @param string $value Cookie value
*/
public function setValue($value)
{
$this->data['Value'] = $value;
}
/**
* Get the domain
*
* @return string|null
*/
public function getDomain()
{
return $this->data['Domain'];
}
/**
* Set the domain of the cookie
*
* @param string $domain
*/
public function setDomain($domain)
{
$this->data['Domain'] = $domain;
}
/**
* Get the path
*
* @return string
*/
public function getPath()
{
return $this->data['Path'];
}
/**
* Set the path of the cookie
*
* @param string $path Path of the cookie
*/
public function setPath($path)
{
$this->data['Path'] = $path;
}
/**
* Maximum lifetime of the cookie in seconds
*
* @return int|null
*/
public function getMaxAge()
{
return $this->data['Max-Age'];
}
/**
* Set the max-age of the cookie
*
* @param int $maxAge Max age of the cookie in seconds
*/
public function setMaxAge($maxAge)
{
$this->data['Max-Age'] = $maxAge;
}
/**
* The UNIX timestamp when the cookie Expires
*
* @return mixed
*/
public function getExpires()
{
return $this->data['Expires'];
}
/**
* Set the unix timestamp for which the cookie will expire
*
* @param int $timestamp Unix timestamp
*/
public function setExpires($timestamp)
{
$this->data['Expires'] = is_numeric($timestamp)
? (int)$timestamp
: strtotime($timestamp);
}
/**
* Get whether or not this is a secure cookie
*
* @return bool|null
*/
public function getSecure()
{
return $this->data['Secure'];
}
/**
* Set whether or not the cookie is secure
*
* @param bool $secure Set to true or false if secure
*/
public function setSecure($secure)
{
$this->data['Secure'] = $secure;
}
/**
* Get whether or not this is a session cookie
*
* @return bool|null
*/
public function getDiscard()
{
return $this->data['Discard'];
}
/**
* Set whether or not this is a session cookie
*
* @param bool $discard Set to true or false if this is a session cookie
*/
public function setDiscard($discard)
{
$this->data['Discard'] = $discard;
}
/**
* Get whether or not this is an HTTP only cookie
*
* @return bool
*/
public function getHttpOnly()
{
return $this->data['HttpOnly'];
}
/**
* Set whether or not this is an HTTP only cookie
*
* @param bool $httpOnly Set to true or false if this is HTTP only
*/
public function setHttpOnly($httpOnly)
{
$this->data['HttpOnly'] = $httpOnly;
}
/**
* Check if the cookie matches a path value.
*
* A request-path path-matches a given cookie-path if at least one of
* the following conditions holds:
*
* - The cookie-path and the request-path are identical.
* - The cookie-path is a prefix of the request-path, and the last
* character of the cookie-path is %x2F ("/").
* - The cookie-path is a prefix of the request-path, and the first
* character of the request-path that is not included in the cookie-
* path is a %x2F ("/") character.
*
* @param string $requestPath Path to check against
*
* @return bool
*/
public function matchesPath($requestPath)
{
$cookiePath = $this->getPath();
// Match on exact matches or when path is the default empty "/"
if ($cookiePath === '/' || $cookiePath == $requestPath) {
return true;
}
// Ensure that the cookie-path is a prefix of the request path.
if (0 !== strpos($requestPath, $cookiePath)) {
return false;
}
// Match if the last character of the cookie-path is "/"
if (substr($cookiePath, -1, 1) === '/') {
return true;
}
// Match if the first character not included in cookie path is "/"
return substr($requestPath, strlen($cookiePath), 1) === '/';
}
/**
* Check if the cookie matches a domain value
*
* @param string $domain Domain to check against
*
* @return bool
*/
public function matchesDomain($domain)
{
// Remove the leading '.' as per spec in RFC 6265.
// http://tools.ietf.org/html/rfc6265#section-5.2.3
$cookieDomain = ltrim($this->getDomain(), '.');
// Domain not set or exact match.
if (!$cookieDomain || !strcasecmp($domain, $cookieDomain)) {
return true;
}
// Matching the subdomain according to RFC 6265.
// http://tools.ietf.org/html/rfc6265#section-5.1.3
if (filter_var($domain, FILTER_VALIDATE_IP)) {
return false;
}
return (bool)preg_match('/\.' . preg_quote($cookieDomain, '/') . '$/', $domain);
}
/**
* Check if the cookie is expired
*
* @return bool
*/
public function isExpired()
{
return $this->getExpires() !== null && time() > $this->getExpires();
}
/**
* Check if the cookie is valid according to RFC 6265
*
* @return bool|string Returns true if valid or an error message if invalid
*/
public function validate()
{
// Names must not be empty, but can be 0
$name = $this->getName();
if (empty($name) && !is_numeric($name)) {
return 'The cookie name must not be empty';
}
// Check if any of the invalid characters are present in the cookie name
if (preg_match(
'/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5c\x7b\x7d\x7f]/',
$name
)) {
return 'Cookie name must not contain invalid characters: ASCII '
. 'Control characters (0-31;127), space, tab and the '
. 'following characters: ()<>@,;:\"/?={}';
}
// Value must not be empty, but can be 0
$value = $this->getValue();
if (empty($value) && !is_numeric($value)) {
return 'The cookie value must not be empty';
}
// Domains must not be empty, but can be 0
// A "0" is not a valid internet domain, but may be used as server name
// in a private network.
$domain = $this->getDomain();
if (empty($domain) && !is_numeric($domain)) {
return 'The cookie domain must not be empty';
}
return true;
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace GuzzleHttp6\Exception;
use Exception;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Exception when an HTTP error occurs (4xx or 5xx error)
*/
class BadResponseException extends RequestException
{
public function __construct(
$message,
RequestInterface $request,
ResponseInterface $response = null,
Exception $previous = null,
array $handlerContext = []
)
{
if (null === $response) {
@trigger_error(
'Instantiating the ' . __CLASS__ . ' class without a Response is deprecated since version 6.3 and will be removed in 7.0.',
E_USER_DEPRECATED
);
}
parent::__construct($message, $request, $response, $previous, $handlerContext);
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace GuzzleHttp6\Exception;
/**
* Exception when a client error is encountered (4xx codes)
*/
class ClientException extends BadResponseException
{
}

View File

@@ -0,0 +1,40 @@
<?php
namespace GuzzleHttp6\Exception;
use Exception;
use Psr\Http\Message\RequestInterface;
/**
* Exception thrown when a connection cannot be established.
*
* Note that no response is present for a ConnectException
*/
class ConnectException extends RequestException
{
public function __construct(
$message,
RequestInterface $request,
Exception $previous = null,
array $handlerContext = []
)
{
parent::__construct($message, $request, null, $previous, $handlerContext);
}
/**
* @return null
*/
public function getResponse()
{
return null;
}
/**
* @return bool
*/
public function hasResponse()
{
return false;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace GuzzleHttp6\Exception;
use Throwable;
if (interface_exists(Throwable::class)) {
interface GuzzleException extends Throwable
{
}
} else {
/**
* @method string getMessage()
* @method Throwable|null getPrevious()
* @method mixed getCode()
* @method string getFile()
* @method int getLine()
* @method array getTrace()
* @method string getTraceAsString()
*/
interface GuzzleException
{
}
}

View File

@@ -0,0 +1,7 @@
<?php
namespace GuzzleHttp6\Exception;
final class InvalidArgumentException extends \InvalidArgumentException implements GuzzleException
{
}

View File

@@ -0,0 +1,197 @@
<?php
namespace GuzzleHttp6\Exception;
use Exception;
use GuzzleHttp6\Promise\PromiseInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use function GuzzleHttp6\Psr7\get_message_body_summary;
/**
* HTTP Request exception
*/
class RequestException extends TransferException
{
/** @var RequestInterface */
private $request;
/** @var ResponseInterface|null */
private $response;
/** @var array */
private $handlerContext;
public function __construct(
$message,
RequestInterface $request,
ResponseInterface $response = null,
Exception $previous = null,
array $handlerContext = []
)
{
// Set the code of the exception if the response is set and not future.
$code = $response && !($response instanceof PromiseInterface)
? $response->getStatusCode()
: 0;
parent::__construct($message, $code, $previous);
$this->request = $request;
$this->response = $response;
$this->handlerContext = $handlerContext;
}
/**
* Wrap non-RequestExceptions with a RequestException
*
* @param RequestInterface $request
* @param Exception $e
*
* @return RequestException
*/
public static function wrapException(RequestInterface $request, Exception $e)
{
return $e instanceof RequestException
? $e
: new RequestException($e->getMessage(), $request, null, $e);
}
/**
* Factory method to create a new exception with a normalized error message
*
* @param RequestInterface $request Request
* @param ResponseInterface $response Response received
* @param Exception $previous Previous exception
* @param array $ctx Optional handler context.
*
* @return self
*/
public static function create(
RequestInterface $request,
ResponseInterface $response = null,
Exception $previous = null,
array $ctx = []
)
{
if (!$response) {
return new self(
'Error completing request',
$request,
null,
$previous,
$ctx
);
}
$level = (int)floor($response->getStatusCode() / 100);
if ($level === 4) {
$label = 'Client error';
$className = ClientException::class;
} elseif ($level === 5) {
$label = 'Server error';
$className = ServerException::class;
} else {
$label = 'Unsuccessful request';
$className = __CLASS__;
}
$uri = $request->getUri();
$uri = static::obfuscateUri($uri);
// Client Error: `GET /` resulted in a `404 Not Found` response:
// <html> ... (truncated)
$message = sprintf(
'%s: `%s %s` resulted in a `%s %s` response',
$label,
$request->getMethod(),
$uri,
$response->getStatusCode(),
$response->getReasonPhrase()
);
$summary = static::getResponseBodySummary($response);
if ($summary !== null) {
$message .= ":\n{$summary}\n";
}
return new $className($message, $request, $response, $previous, $ctx);
}
/**
* Get a short summary of the response
*
* Will return `null` if the response is not printable.
*
* @param ResponseInterface $response
*
* @return string|null
*/
public static function getResponseBodySummary(ResponseInterface $response)
{
return get_message_body_summary($response);
}
/**
* Obfuscates URI if there is a username and a password present
*
* @param UriInterface $uri
*
* @return UriInterface
*/
private static function obfuscateUri(UriInterface $uri)
{
$userInfo = $uri->getUserInfo();
if (false !== ($pos = strpos($userInfo, ':'))) {
return $uri->withUserInfo(substr($userInfo, 0, $pos), '***');
}
return $uri;
}
/**
* Get the request that caused the exception
*
* @return RequestInterface
*/
public function getRequest()
{
return $this->request;
}
/**
* Get the associated response
*
* @return ResponseInterface|null
*/
public function getResponse()
{
return $this->response;
}
/**
* Check if a response was received
*
* @return bool
*/
public function hasResponse()
{
return $this->response !== null;
}
/**
* Get contextual information about the error from the underlying handler.
*
* The contents of this array will vary depending on which handler you are
* using. It may also be just an empty array. Relying on this data will
* couple you to a specific handler, but can give more debug information
* when needed.
*
* @return array
*/
public function getHandlerContext()
{
return $this->handlerContext;
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace GuzzleHttp6\Exception;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
/**
* Exception thrown when a seek fails on a stream.
*/
class SeekException extends RuntimeException implements GuzzleException
{
private $stream;
public function __construct(StreamInterface $stream, $pos = 0, $msg = '')
{
$this->stream = $stream;
$msg = $msg ?: 'Could not seek the stream to position ' . $pos;
parent::__construct($msg);
}
/**
* @return StreamInterface
*/
public function getStream()
{
return $this->stream;
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace GuzzleHttp6\Exception;
/**
* Exception when a server error is encountered (5xx codes)
*/
class ServerException extends BadResponseException
{
}

View File

@@ -0,0 +1,7 @@
<?php
namespace GuzzleHttp6\Exception;
class TooManyRedirectsException extends RequestException
{
}

View File

@@ -0,0 +1,9 @@
<?php
namespace GuzzleHttp6\Exception;
use RuntimeException;
class TransferException extends RuntimeException implements GuzzleException
{
}

View File

@@ -0,0 +1,597 @@
<?php
namespace GuzzleHttp6\Handler;
use Exception;
use GuzzleHttp6\Exception\ConnectException;
use GuzzleHttp6\Exception\RequestException;
use GuzzleHttp6\Promise\FulfilledPromise;
use GuzzleHttp6\Promise\PromiseInterface;
use GuzzleHttp6\Psr7;
use GuzzleHttp6\Psr7\LazyOpenStream;
use GuzzleHttp6\TransferStats;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use RuntimeException;
use function GuzzleHttp6\debug_resource;
use function GuzzleHttp6\is_host_in_noproxy;
use function GuzzleHttp6\Promise\rejection_for;
use function GuzzleHttp6\Psr7\stream_for;
/**
* Creates curl resources from a request
*/
class CurlFactory implements CurlFactoryInterface
{
const CURL_VERSION_STR = 'curl_version';
const LOW_CURL_VERSION_NUMBER = '7.21.2';
/** @var array */
private $handles = [];
/** @var int Total number of idle handles to keep in cache */
private $maxHandles;
/**
* @param int $maxHandles Maximum number of idle handles.
*/
public function __construct($maxHandles)
{
$this->maxHandles = $maxHandles;
}
public function create(RequestInterface $request, array $options)
{
if (isset($options['curl']['body_as_string'])) {
$options['_body_as_string'] = $options['curl']['body_as_string'];
unset($options['curl']['body_as_string']);
}
$easy = new EasyHandle;
$easy->request = $request;
$easy->options = $options;
$conf = $this->getDefaultConf($easy);
$this->applyMethod($easy, $conf);
$this->applyHandlerOptions($easy, $conf);
$this->applyHeaders($easy, $conf);
unset($conf['_headers']);
// Add handler options from the request configuration options
if (isset($options['curl'])) {
$conf = array_replace($conf, $options['curl']);
}
$conf[CURLOPT_HEADERFUNCTION] = $this->createHeaderFn($easy);
$easy->handle = $this->handles
? array_pop($this->handles)
: curl_init();
curl_setopt_array($easy->handle, $conf);
return $easy;
}
public function release(EasyHandle $easy)
{
$resource = $easy->handle;
unset($easy->handle);
if (count($this->handles) >= $this->maxHandles) {
curl_close($resource);
} else {
// Remove all callback functions as they can hold onto references
// and are not cleaned up by curl_reset. Using curl_setopt_array
// does not work for some reason, so removing each one
// individually.
curl_setopt($resource, CURLOPT_HEADERFUNCTION, null);
curl_setopt($resource, CURLOPT_READFUNCTION, null);
curl_setopt($resource, CURLOPT_WRITEFUNCTION, null);
curl_setopt($resource, CURLOPT_PROGRESSFUNCTION, null);
curl_reset($resource);
$this->handles[] = $resource;
}
}
/**
* Completes a cURL transaction, either returning a response promise or a
* rejected promise.
*
* @param callable $handler
* @param EasyHandle $easy
* @param CurlFactoryInterface $factory Dictates how the handle is released
*
* @return PromiseInterface
*/
public static function finish(
callable $handler,
EasyHandle $easy,
CurlFactoryInterface $factory
)
{
if (isset($easy->options['on_stats'])) {
self::invokeStats($easy);
}
if (!$easy->response || $easy->errno) {
return self::finishError($handler, $easy, $factory);
}
// Return the response if it is present and there is no error.
$factory->release($easy);
// Rewind the body of the response if possible.
$body = $easy->response->getBody();
if ($body->isSeekable()) {
$body->rewind();
}
return new FulfilledPromise($easy->response);
}
private static function invokeStats(EasyHandle $easy)
{
$curlStats = curl_getinfo($easy->handle);
$curlStats['appconnect_time'] = curl_getinfo($easy->handle, CURLINFO_APPCONNECT_TIME);
$stats = new TransferStats(
$easy->request,
$easy->response,
$curlStats['total_time'],
$easy->errno,
$curlStats
);
call_user_func($easy->options['on_stats'], $stats);
}
private static function finishError(
callable $handler,
EasyHandle $easy,
CurlFactoryInterface $factory
)
{
// Get error information and release the handle to the factory.
$ctx = [
'errno' => $easy->errno,
'error' => curl_error($easy->handle),
'appconnect_time' => curl_getinfo($easy->handle, CURLINFO_APPCONNECT_TIME),
] + curl_getinfo($easy->handle);
$ctx[self::CURL_VERSION_STR] = curl_version()['version'];
$factory->release($easy);
// Retry when nothing is present or when curl failed to rewind.
if (empty($easy->options['_err_message'])
&& (!$easy->errno || $easy->errno == 65)
) {
return self::retryFailedRewind($handler, $easy, $ctx);
}
return self::createRejection($easy, $ctx);
}
private static function createRejection(EasyHandle $easy, array $ctx)
{
static $connectionErrors = [
CURLE_OPERATION_TIMEOUTED => true,
CURLE_COULDNT_RESOLVE_HOST => true,
CURLE_COULDNT_CONNECT => true,
CURLE_SSL_CONNECT_ERROR => true,
CURLE_GOT_NOTHING => true,
];
// If an exception was encountered during the onHeaders event, then
// return a rejected promise that wraps that exception.
if ($easy->onHeadersException) {
return rejection_for(
new RequestException(
'An error was encountered during the on_headers event',
$easy->request,
$easy->response,
$easy->onHeadersException,
$ctx
)
);
}
if (version_compare($ctx[self::CURL_VERSION_STR], self::LOW_CURL_VERSION_NUMBER)) {
$message = sprintf(
'cURL error %s: %s (%s)',
$ctx['errno'],
$ctx['error'],
'see https://curl.haxx.se/libcurl/c/libcurl-errors.html'
);
} else {
$message = sprintf(
'cURL error %s: %s (%s) for %s',
$ctx['errno'],
$ctx['error'],
'see https://curl.haxx.se/libcurl/c/libcurl-errors.html',
$easy->request->getUri()
);
}
// Create a connection exception if it was a specific error code.
$error = isset($connectionErrors[$easy->errno])
? new ConnectException($message, $easy->request, null, $ctx)
: new RequestException($message, $easy->request, $easy->response, null, $ctx);
return rejection_for($error);
}
private function getDefaultConf(EasyHandle $easy)
{
$conf = [
'_headers' => $easy->request->getHeaders(),
CURLOPT_CUSTOMREQUEST => $easy->request->getMethod(),
CURLOPT_URL => (string)$easy->request->getUri()->withFragment(''),
CURLOPT_RETURNTRANSFER => false,
CURLOPT_HEADER => false,
CURLOPT_CONNECTTIMEOUT => 150,
];
if (defined('CURLOPT_PROTOCOLS')) {
$conf[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
}
$version = $easy->request->getProtocolVersion();
if ($version == 1.1) {
$conf[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1;
} elseif ($version == 2.0) {
$conf[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_2_0;
} else {
$conf[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
}
return $conf;
}
private function applyMethod(EasyHandle $easy, array &$conf)
{
$body = $easy->request->getBody();
$size = $body->getSize();
if ($size === null || $size > 0) {
$this->applyBody($easy->request, $easy->options, $conf);
return;
}
$method = $easy->request->getMethod();
if ($method === 'PUT' || $method === 'POST') {
// See http://tools.ietf.org/html/rfc7230#section-3.3.2
if (!$easy->request->hasHeader('Content-Length')) {
$conf[CURLOPT_HTTPHEADER][] = 'Content-Length: 0';
}
} elseif ($method === 'HEAD') {
$conf[CURLOPT_NOBODY] = true;
unset(
$conf[CURLOPT_WRITEFUNCTION],
$conf[CURLOPT_READFUNCTION],
$conf[CURLOPT_FILE],
$conf[CURLOPT_INFILE]
);
}
}
private function applyBody(RequestInterface $request, array $options, array &$conf)
{
$size = $request->hasHeader('Content-Length')
? (int)$request->getHeaderLine('Content-Length')
: null;
// Send the body as a string if the size is less than 1MB OR if the
// [curl][body_as_string] request value is set.
if (($size !== null && $size < 1000000) ||
!empty($options['_body_as_string'])
) {
$conf[CURLOPT_POSTFIELDS] = (string)$request->getBody();
// Don't duplicate the Content-Length header
$this->removeHeader('Content-Length', $conf);
$this->removeHeader('Transfer-Encoding', $conf);
} else {
$conf[CURLOPT_UPLOAD] = true;
if ($size !== null) {
$conf[CURLOPT_INFILESIZE] = $size;
$this->removeHeader('Content-Length', $conf);
}
$body = $request->getBody();
if ($body->isSeekable()) {
$body->rewind();
}
$conf[CURLOPT_READFUNCTION] = function ($ch, $fd, $length) use ($body) {
return $body->read($length);
};
}
// If the Expect header is not present, prevent curl from adding it
if (!$request->hasHeader('Expect')) {
$conf[CURLOPT_HTTPHEADER][] = 'Expect:';
}
// cURL sometimes adds a content-type by default. Prevent this.
if (!$request->hasHeader('Content-Type')) {
$conf[CURLOPT_HTTPHEADER][] = 'Content-Type:';
}
}
private function applyHeaders(EasyHandle $easy, array &$conf)
{
foreach ($conf['_headers'] as $name => $values) {
foreach ($values as $value) {
$value = (string)$value;
if ($value === '') {
// cURL requires a special format for empty headers.
// See https://github.com/guzzle/guzzle/issues/1882 for more details.
$conf[CURLOPT_HTTPHEADER][] = "$name;";
} else {
$conf[CURLOPT_HTTPHEADER][] = "$name: $value";
}
}
}
// Remove the Accept header if one was not set
if (!$easy->request->hasHeader('Accept')) {
$conf[CURLOPT_HTTPHEADER][] = 'Accept:';
}
}
/**
* Remove a header from the options array.
*
* @param string $name Case-insensitive header to remove
* @param array $options Array of options to modify
*/
private function removeHeader($name, array &$options)
{
foreach (array_keys($options['_headers']) as $key) {
if (!strcasecmp($key, $name)) {
unset($options['_headers'][$key]);
return;
}
}
}
private function applyHandlerOptions(EasyHandle $easy, array &$conf)
{
$options = $easy->options;
if (isset($options['verify'])) {
if ($options['verify'] === false) {
unset($conf[CURLOPT_CAINFO]);
$conf[CURLOPT_SSL_VERIFYHOST] = 0;
$conf[CURLOPT_SSL_VERIFYPEER] = false;
} else {
$conf[CURLOPT_SSL_VERIFYHOST] = 2;
$conf[CURLOPT_SSL_VERIFYPEER] = true;
if (is_string($options['verify'])) {
// Throw an error if the file/folder/link path is not valid or doesn't exist.
if (!file_exists($options['verify'])) {
throw new InvalidArgumentException(
"SSL CA bundle not found: {$options['verify']}"
);
}
// If it's a directory or a link to a directory use CURLOPT_CAPATH.
// If not, it's probably a file, or a link to a file, so use CURLOPT_CAINFO.
if (is_dir($options['verify']) ||
(is_link($options['verify']) && is_dir(readlink($options['verify'])))) {
$conf[CURLOPT_CAPATH] = $options['verify'];
} else {
$conf[CURLOPT_CAINFO] = $options['verify'];
}
}
}
}
if (!empty($options['decode_content'])) {
$accept = $easy->request->getHeaderLine('Accept-Encoding');
if ($accept) {
$conf[CURLOPT_ENCODING] = $accept;
} else {
$conf[CURLOPT_ENCODING] = '';
// Don't let curl send the header over the wire
$conf[CURLOPT_HTTPHEADER][] = 'Accept-Encoding:';
}
}
if (isset($options['sink'])) {
$sink = $options['sink'];
if (!is_string($sink)) {
$sink = stream_for($sink);
} elseif (!is_dir(dirname($sink))) {
// Ensure that the directory exists before failing in curl.
throw new RuntimeException(sprintf(
'Directory %s does not exist for sink value of %s',
dirname($sink),
$sink
));
} else {
$sink = new LazyOpenStream($sink, 'w+');
}
$easy->sink = $sink;
$conf[CURLOPT_WRITEFUNCTION] = function ($ch, $write) use ($sink) {
return $sink->write($write);
};
} else {
// Use a default temp stream if no sink was set.
$conf[CURLOPT_FILE] = fopen('php://temp', 'w+');
$easy->sink = stream_for($conf[CURLOPT_FILE]);
}
$timeoutRequiresNoSignal = false;
if (isset($options['timeout'])) {
$timeoutRequiresNoSignal |= $options['timeout'] < 1;
$conf[CURLOPT_TIMEOUT_MS] = $options['timeout'] * 1000;
}
// CURL default value is CURL_IPRESOLVE_WHATEVER
if (isset($options['force_ip_resolve'])) {
if ('v4' === $options['force_ip_resolve']) {
$conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4;
} elseif ('v6' === $options['force_ip_resolve']) {
$conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V6;
}
}
if (isset($options['connect_timeout'])) {
$timeoutRequiresNoSignal |= $options['connect_timeout'] < 1;
$conf[CURLOPT_CONNECTTIMEOUT_MS] = $options['connect_timeout'] * 1000;
}
if ($timeoutRequiresNoSignal && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
$conf[CURLOPT_NOSIGNAL] = true;
}
if (isset($options['proxy'])) {
if (!is_array($options['proxy'])) {
$conf[CURLOPT_PROXY] = $options['proxy'];
} else {
$scheme = $easy->request->getUri()->getScheme();
if (isset($options['proxy'][$scheme])) {
$host = $easy->request->getUri()->getHost();
if (!isset($options['proxy']['no']) ||
!is_host_in_noproxy($host, $options['proxy']['no'])
) {
$conf[CURLOPT_PROXY] = $options['proxy'][$scheme];
}
}
}
}
if (isset($options['cert'])) {
$cert = $options['cert'];
if (is_array($cert)) {
$conf[CURLOPT_SSLCERTPASSWD] = $cert[1];
$cert = $cert[0];
}
if (!file_exists($cert)) {
throw new InvalidArgumentException(
"SSL certificate not found: {$cert}"
);
}
$conf[CURLOPT_SSLCERT] = $cert;
}
if (isset($options['ssl_key'])) {
if (is_array($options['ssl_key'])) {
if (count($options['ssl_key']) === 2) {
list($sslKey, $conf[CURLOPT_SSLKEYPASSWD]) = $options['ssl_key'];
} else {
list($sslKey) = $options['ssl_key'];
}
}
$sslKey = isset($sslKey) ? $sslKey : $options['ssl_key'];
if (!file_exists($sslKey)) {
throw new InvalidArgumentException(
"SSL private key not found: {$sslKey}"
);
}
$conf[CURLOPT_SSLKEY] = $sslKey;
}
if (isset($options['progress'])) {
$progress = $options['progress'];
if (!is_callable($progress)) {
throw new InvalidArgumentException(
'progress client option must be callable'
);
}
$conf[CURLOPT_NOPROGRESS] = false;
$conf[CURLOPT_PROGRESSFUNCTION] = function () use ($progress) {
$args = func_get_args();
// PHP 5.5 pushed the handle onto the start of the args
if (is_resource($args[0])) {
array_shift($args);
}
call_user_func_array($progress, $args);
};
}
if (!empty($options['debug'])) {
$conf[CURLOPT_STDERR] = debug_resource($options['debug']);
$conf[CURLOPT_VERBOSE] = true;
}
}
/**
* This function ensures that a response was set on a transaction. If one
* was not set, then the request is retried if possible. This error
* typically means you are sending a payload, curl encountered a
* "Connection died, retrying a fresh connect" error, tried to rewind the
* stream, and then encountered a "necessary data rewind wasn't possible"
* error, causing the request to be sent through curl_multi_info_read()
* without an error status.
*/
private static function retryFailedRewind(
callable $handler,
EasyHandle $easy,
array $ctx
)
{
try {
// Only rewind if the body has been read from.
$body = $easy->request->getBody();
if ($body->tell() > 0) {
$body->rewind();
}
} catch (RuntimeException $e) {
$ctx['error'] = 'The connection unexpectedly failed without '
. 'providing an error. The request would have been retried, '
. 'but attempting to rewind the request body failed. '
. 'Exception: ' . $e;
return self::createRejection($easy, $ctx);
}
// Retry no more than 3 times before giving up.
if (!isset($easy->options['_curl_retries'])) {
$easy->options['_curl_retries'] = 1;
} elseif ($easy->options['_curl_retries'] == 2) {
$ctx['error'] = 'The cURL request was retried 3 times '
. 'and did not succeed. The most likely reason for the failure '
. 'is that cURL was unable to rewind the body of the request '
. 'and subsequent retries resulted in the same error. Turn on '
. 'the debug option to see what went wrong. See '
. 'https://bugs.php.net/bug.php?id=47204 for more information.';
return self::createRejection($easy, $ctx);
} else {
$easy->options['_curl_retries']++;
}
return $handler($easy->request, $easy->options);
}
private function createHeaderFn(EasyHandle $easy)
{
if (isset($easy->options['on_headers'])) {
$onHeaders = $easy->options['on_headers'];
if (!is_callable($onHeaders)) {
throw new InvalidArgumentException('on_headers must be callable');
}
} else {
$onHeaders = null;
}
return function ($ch, $h) use (
$onHeaders,
$easy,
&$startingResponse
) {
$value = trim($h);
if ($value === '') {
$startingResponse = true;
$easy->createResponse();
if ($onHeaders !== null) {
try {
$onHeaders($easy->response);
} catch (Exception $e) {
// Associate the exception with the handle and trigger
// a curl header write error by returning 0.
$easy->onHeadersException = $e;
return -1;
}
}
} elseif ($startingResponse) {
$startingResponse = false;
$easy->headers = [$value];
} else {
$easy->headers[] = $value;
}
return strlen($h);
};
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace GuzzleHttp6\Handler;
use Psr\Http\Message\RequestInterface;
use RuntimeException;
interface CurlFactoryInterface
{
/**
* Creates a cURL handle resource.
*
* @param RequestInterface $request Request
* @param array $options Transfer options
*
* @return EasyHandle
* @throws RuntimeException when an option cannot be applied
*/
public function create(RequestInterface $request, array $options);
/**
* Release an easy handle, allowing it to be reused or closed.
*
* This function must call unset on the easy handle's "handle" property.
*
* @param EasyHandle $easy
*/
public function release(EasyHandle $easy);
}

View File

@@ -0,0 +1,46 @@
<?php
namespace GuzzleHttp6\Handler;
use GuzzleHttp6\Psr7;
use Psr\Http\Message\RequestInterface;
/**
* HTTP handler that uses cURL easy handles as a transport layer.
*
* When using the CurlHandler, custom curl options can be specified as an
* associative array of curl option constants mapping to values in the
* **curl** key of the "client" key of the request.
*/
class CurlHandler
{
/** @var CurlFactoryInterface */
private $factory;
/**
* Accepts an associative array of options:
*
* - factory: Optional curl factory used to create cURL handles.
*
* @param array $options Array of options to use with the handler
*/
public function __construct(array $options = [])
{
$this->factory = isset($options['handle_factory'])
? $options['handle_factory']
: new CurlFactory(3);
}
public function __invoke(RequestInterface $request, array $options)
{
if (isset($options['delay'])) {
usleep($options['delay'] * 1000);
}
$easy = $this->factory->create($request, $options);
curl_exec($easy->handle);
$easy->errno = curl_errno($easy->handle);
return CurlFactory::finish($this, $easy, $this->factory);
}
}

View File

@@ -0,0 +1,221 @@
<?php
namespace GuzzleHttp6\Handler;
use BadMethodCallException;
use GuzzleHttp6\Promise as P;
use GuzzleHttp6\Promise\Promise;
use GuzzleHttp6\Utils;
use Psr\Http\Message\RequestInterface;
/**
* Returns an asynchronous response using curl_multi_* functions.
*
* When using the CurlMultiHandler, custom curl options can be specified as an
* associative array of curl option constants mapping to values in the
* **curl** key of the provided request options.
*
* @property resource $_mh Internal use only. Lazy loaded multi-handle.
*/
class CurlMultiHandler
{
/** @var CurlFactoryInterface */
private $factory;
private $selectTimeout;
private $active;
private $handles = [];
private $delays = [];
private $options = [];
/**
* This handler accepts the following options:
*
* - handle_factory: An optional factory used to create curl handles
* - select_timeout: Optional timeout (in seconds) to block before timing
* out while selecting curl handles. Defaults to 1 second.
* - options: An associative array of CURLMOPT_* options and
* corresponding values for curl_multi_setopt()
*
* @param array $options
*/
public function __construct(array $options = [])
{
$this->factory = isset($options['handle_factory'])
? $options['handle_factory'] : new CurlFactory(50);
if (isset($options['select_timeout'])) {
$this->selectTimeout = $options['select_timeout'];
} elseif ($selectTimeout = getenv('GUZZLE_CURL_SELECT_TIMEOUT')) {
$this->selectTimeout = $selectTimeout;
} else {
$this->selectTimeout = 1;
}
$this->options = isset($options['options']) ? $options['options'] : [];
}
public function __get($name)
{
if ($name === '_mh') {
$this->_mh = curl_multi_init();
foreach ($this->options as $option => $value) {
// A warning is raised in case of a wrong option.
curl_multi_setopt($this->_mh, $option, $value);
}
// Further calls to _mh will return the value directly, without entering the
// __get() method at all.
return $this->_mh;
}
throw new BadMethodCallException();
}
public function __destruct()
{
if (isset($this->_mh)) {
curl_multi_close($this->_mh);
unset($this->_mh);
}
}
public function __invoke(RequestInterface $request, array $options)
{
$easy = $this->factory->create($request, $options);
$id = (int)$easy->handle;
$promise = new Promise(
[$this, 'execute'],
function () use ($id) {
return $this->cancel($id);
}
);
$this->addRequest(['easy' => $easy, 'deferred' => $promise]);
return $promise;
}
/**
* Ticks the curl event loop.
*/
public function tick()
{
// Add any delayed handles if needed.
if ($this->delays) {
$currentTime = Utils::currentTime();
foreach ($this->delays as $id => $delay) {
if ($currentTime >= $delay) {
unset($this->delays[$id]);
curl_multi_add_handle(
$this->_mh,
$this->handles[$id]['easy']->handle
);
}
}
}
// Step through the task queue which may add additional requests.
P\queue()->run();
if ($this->active &&
curl_multi_select($this->_mh, $this->selectTimeout) === -1
) {
// Perform a usleep if a select returns -1.
// See: https://bugs.php.net/bug.php?id=61141
usleep(250);
}
while (curl_multi_exec($this->_mh, $this->active) === CURLM_CALL_MULTI_PERFORM) ;
$this->processMessages();
}
/**
* Runs until all outstanding connections have completed.
*/
public function execute()
{
$queue = P\queue();
while ($this->handles || !$queue->isEmpty()) {
// If there are no transfers, then sleep for the next delay
if (!$this->active && $this->delays) {
usleep($this->timeToNext());
}
$this->tick();
}
}
private function addRequest(array $entry)
{
$easy = $entry['easy'];
$id = (int)$easy->handle;
$this->handles[$id] = $entry;
if (empty($easy->options['delay'])) {
curl_multi_add_handle($this->_mh, $easy->handle);
} else {
$this->delays[$id] = Utils::currentTime() + ($easy->options['delay'] / 1000);
}
}
/**
* Cancels a handle from sending and removes references to it.
*
* @param int $id Handle ID to cancel and remove.
*
* @return bool True on success, false on failure.
*/
private function cancel($id)
{
// Cannot cancel if it has been processed.
if (!isset($this->handles[$id])) {
return false;
}
$handle = $this->handles[$id]['easy']->handle;
unset($this->delays[$id], $this->handles[$id]);
curl_multi_remove_handle($this->_mh, $handle);
curl_close($handle);
return true;
}
private function processMessages()
{
while ($done = curl_multi_info_read($this->_mh)) {
$id = (int)$done['handle'];
curl_multi_remove_handle($this->_mh, $done['handle']);
if (!isset($this->handles[$id])) {
// Probably was cancelled.
continue;
}
$entry = $this->handles[$id];
unset($this->handles[$id], $this->delays[$id]);
$entry['easy']->errno = $done['result'];
$entry['deferred']->resolve(
CurlFactory::finish(
$this,
$entry['easy'],
$this->factory
)
);
}
}
private function timeToNext()
{
$currentTime = Utils::currentTime();
$nextTime = PHP_INT_MAX;
foreach ($this->delays as $time) {
if ($time < $nextTime) {
$nextTime = $time;
}
}
return max(0, $nextTime - $currentTime) * 1000000;
}
}

View File

@@ -0,0 +1,98 @@
<?php
namespace GuzzleHttp6\Handler;
use BadMethodCallException;
use Exception;
use GuzzleHttp6\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use function GuzzleHttp6\headers_from_lines;
use function GuzzleHttp6\normalize_header_keys;
/**
* Represents a cURL easy handle and the data it populates.
*
* @internal
*/
final class EasyHandle
{
/** @var resource cURL resource */
public $handle;
/** @var StreamInterface Where data is being written */
public $sink;
/** @var array Received HTTP headers so far */
public $headers = [];
/** @var ResponseInterface Received response (if any) */
public $response;
/** @var RequestInterface Request being sent */
public $request;
/** @var array Request options */
public $options = [];
/** @var int cURL error number (if any) */
public $errno = 0;
/** @var Exception Exception during on_headers (if any) */
public $onHeadersException;
/**
* Attach a response to the easy handle based on the received headers.
*
* @throws RuntimeException if no headers have been received.
*/
public function createResponse()
{
if (empty($this->headers)) {
throw new RuntimeException('No headers have been received');
}
// HTTP-version SP status-code SP reason-phrase
$startLine = explode(' ', array_shift($this->headers), 3);
$headers = headers_from_lines($this->headers);
$normalizedKeys = normalize_header_keys($headers);
if (!empty($this->options['decode_content'])
&& isset($normalizedKeys['content-encoding'])
) {
$headers['x-encoded-content-encoding']
= $headers[$normalizedKeys['content-encoding']];
unset($headers[$normalizedKeys['content-encoding']]);
if (isset($normalizedKeys['content-length'])) {
$headers['x-encoded-content-length']
= $headers[$normalizedKeys['content-length']];
$bodyLength = (int)$this->sink->getSize();
if ($bodyLength) {
$headers[$normalizedKeys['content-length']] = $bodyLength;
} else {
unset($headers[$normalizedKeys['content-length']]);
}
}
}
// Attach a response to the easy handle with the parsed headers.
$this->response = new Response(
$startLine[1],
$headers,
$this->sink,
substr($startLine[0], 5),
isset($startLine[2]) ? (string)$startLine[2] : null
);
}
public function __get($name)
{
$msg = $name === 'handle'
? 'The EasyHandle has been released'
: 'Invalid property: ' . $name;
throw new BadMethodCallException($msg);
}
}

View File

@@ -0,0 +1,207 @@
<?php
namespace GuzzleHttp6\Handler;
use Countable;
use Exception;
use GuzzleHttp6\Exception\RequestException;
use GuzzleHttp6\HandlerStack;
use GuzzleHttp6\Promise\PromiseInterface;
use GuzzleHttp6\Promise\RejectedPromise;
use GuzzleHttp6\TransferStats;
use InvalidArgumentException;
use OutOfBoundsException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use function GuzzleHttp6\describe_type;
use function GuzzleHttp6\Promise\promise_for;
use function GuzzleHttp6\Promise\rejection_for;
/**
* Handler that returns responses or throw exceptions from a queue.
*/
class MockHandler implements Countable
{
private $queue = [];
private $lastRequest;
private $lastOptions;
private $onFulfilled;
private $onRejected;
/**
* Creates a new MockHandler that uses the default handler stack list of
* middlewares.
*
* @param array $queue Array of responses, callables, or exceptions.
* @param callable $onFulfilled Callback to invoke when the return value is fulfilled.
* @param callable $onRejected Callback to invoke when the return value is rejected.
*
* @return HandlerStack
*/
public static function createWithMiddleware(
array $queue = null,
callable $onFulfilled = null,
callable $onRejected = null
)
{
return HandlerStack::create(new self($queue, $onFulfilled, $onRejected));
}
/**
* The passed in value must be an array of
* {@see Psr7\Http\Message\ResponseInterface} objects, Exceptions,
* callables, or Promises.
*
* @param array $queue
* @param callable $onFulfilled Callback to invoke when the return value is fulfilled.
* @param callable $onRejected Callback to invoke when the return value is rejected.
*/
public function __construct(
array $queue = null,
callable $onFulfilled = null,
callable $onRejected = null
)
{
$this->onFulfilled = $onFulfilled;
$this->onRejected = $onRejected;
if ($queue) {
call_user_func_array([$this, 'append'], $queue);
}
}
public function __invoke(RequestInterface $request, array $options)
{
if (!$this->queue) {
throw new OutOfBoundsException('Mock queue is empty');
}
if (isset($options['delay']) && is_numeric($options['delay'])) {
usleep($options['delay'] * 1000);
}
$this->lastRequest = $request;
$this->lastOptions = $options;
$response = array_shift($this->queue);
if (isset($options['on_headers'])) {
if (!is_callable($options['on_headers'])) {
throw new InvalidArgumentException('on_headers must be callable');
}
try {
$options['on_headers']($response);
} catch (Exception $e) {
$msg = 'An error was encountered during the on_headers event';
$response = new RequestException($msg, $request, $response, $e);
}
}
if (is_callable($response)) {
$response = call_user_func($response, $request, $options);
}
$response = $response instanceof Exception
? rejection_for($response)
: promise_for($response);
return $response->then(
function ($value) use ($request, $options) {
$this->invokeStats($request, $options, $value);
if ($this->onFulfilled) {
call_user_func($this->onFulfilled, $value);
}
if (isset($options['sink'])) {
$contents = (string)$value->getBody();
$sink = $options['sink'];
if (is_resource($sink)) {
fwrite($sink, $contents);
} elseif (is_string($sink)) {
file_put_contents($sink, $contents);
} elseif ($sink instanceof StreamInterface) {
$sink->write($contents);
}
}
return $value;
},
function ($reason) use ($request, $options) {
$this->invokeStats($request, $options, null, $reason);
if ($this->onRejected) {
call_user_func($this->onRejected, $reason);
}
return rejection_for($reason);
}
);
}
/**
* Adds one or more variadic requests, exceptions, callables, or promises
* to the queue.
*/
public function append()
{
foreach (func_get_args() as $value) {
if ($value instanceof ResponseInterface
|| $value instanceof Exception
|| $value instanceof PromiseInterface
|| is_callable($value)
) {
$this->queue[] = $value;
} else {
throw new InvalidArgumentException('Expected a response or '
. 'exception. Found ' . describe_type($value));
}
}
}
/**
* Get the last received request.
*
* @return RequestInterface
*/
public function getLastRequest()
{
return $this->lastRequest;
}
/**
* Get the last received request options.
*
* @return array
*/
public function getLastOptions()
{
return $this->lastOptions;
}
/**
* Returns the number of remaining items in the queue.
*
* @return int
*/
public function count()
{
return count($this->queue);
}
public function reset()
{
$this->queue = [];
}
private function invokeStats(
RequestInterface $request,
array $options,
ResponseInterface $response = null,
$reason = null
)
{
if (isset($options['on_stats'])) {
$transferTime = isset($options['transfer_time']) ? $options['transfer_time'] : 0;
$stats = new TransferStats($request, $response, $transferTime, $reason);
call_user_func($options['on_stats'], $stats);
}
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace GuzzleHttp6\Handler;
use GuzzleHttp6\RequestOptions;
use Psr\Http\Message\RequestInterface;
/**
* Provides basic proxies for handlers.
*/
class Proxy
{
/**
* Sends synchronous requests to a specific handler while sending all other
* requests to another handler.
*
* @param callable $default Handler used for normal responses
* @param callable $sync Handler used for synchronous responses.
*
* @return callable Returns the composed handler.
*/
public static function wrapSync(
callable $default,
callable $sync
)
{
return function (RequestInterface $request, array $options) use ($default, $sync) {
return empty($options[RequestOptions::SYNCHRONOUS])
? $default($request, $options)
: $sync($request, $options);
};
}
/**
* Sends streaming requests to a streaming compatible handler while sending
* all other requests to a default handler.
*
* This, for example, could be useful for taking advantage of the
* performance benefits of curl while still supporting true streaming
* through the StreamHandler.
*
* @param callable $default Handler used for non-streaming responses
* @param callable $streaming Handler used for streaming responses
*
* @return callable Returns the composed handler.
*/
public static function wrapStreaming(
callable $default,
callable $streaming
)
{
return function (RequestInterface $request, array $options) use ($default, $streaming) {
return empty($options['stream'])
? $default($request, $options)
: $streaming($request, $options);
};
}
}

View File

@@ -0,0 +1,558 @@
<?php
namespace GuzzleHttp6\Handler;
use Exception;
use GuzzleHttp6\Exception\ConnectException;
use GuzzleHttp6\Exception\RequestException;
use GuzzleHttp6\Promise\FulfilledPromise;
use GuzzleHttp6\Promise\PromiseInterface;
use GuzzleHttp6\Psr7;
use GuzzleHttp6\TransferStats;
use GuzzleHttp6\Utils;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use function GuzzleHttp6\debug_resource;
use function GuzzleHttp6\default_ca_bundle;
use function GuzzleHttp6\headers_from_lines;
use function GuzzleHttp6\is_host_in_noproxy;
use function GuzzleHttp6\normalize_header_keys;
use function GuzzleHttp6\Promise\rejection_for;
/**
* HTTP handler that uses PHP's HTTP stream wrapper.
*/
class StreamHandler
{
private $lastHeaders = [];
/**
* Sends an HTTP request.
*
* @param RequestInterface $request Request to send.
* @param array $options Request transfer options.
*
* @return PromiseInterface
*/
public function __invoke(RequestInterface $request, array $options)
{
// Sleep if there is a delay specified.
if (isset($options['delay'])) {
usleep($options['delay'] * 1000);
}
$startTime = isset($options['on_stats']) ? Utils::currentTime() : null;
try {
// Does not support the expect header.
$request = $request->withoutHeader('Expect');
// Append a content-length header if body size is zero to match
// cURL's behavior.
if (0 === $request->getBody()->getSize()) {
$request = $request->withHeader('Content-Length', '0');
}
return $this->createResponse(
$request,
$options,
$this->createStream($request, $options),
$startTime
);
} catch (InvalidArgumentException $e) {
throw $e;
} catch (Exception $e) {
// Determine if the error was a networking error.
$message = $e->getMessage();
// This list can probably get more comprehensive.
if (strpos($message, 'getaddrinfo') // DNS lookup failed
|| strpos($message, 'Connection refused')
|| strpos($message, "couldn't connect to host") // error on HHVM
|| strpos($message, "connection attempt failed")
) {
$e = new ConnectException($e->getMessage(), $request, $e);
}
$e = RequestException::wrapException($request, $e);
$this->invokeStats($options, $request, $startTime, null, $e);
return rejection_for($e);
}
}
private function invokeStats(
array $options,
RequestInterface $request,
$startTime,
ResponseInterface $response = null,
$error = null
)
{
if (isset($options['on_stats'])) {
$stats = new TransferStats(
$request,
$response,
Utils::currentTime() - $startTime,
$error,
[]
);
call_user_func($options['on_stats'], $stats);
}
}
private function createResponse(
RequestInterface $request,
array $options,
$stream,
$startTime
)
{
$hdrs = $this->lastHeaders;
$this->lastHeaders = [];
$parts = explode(' ', array_shift($hdrs), 3);
$ver = explode('/', $parts[0])[1];
$status = $parts[1];
$reason = isset($parts[2]) ? $parts[2] : null;
$headers = headers_from_lines($hdrs);
list($stream, $headers) = $this->checkDecode($options, $headers, $stream);
$stream = Psr7\stream_for($stream);
$sink = $stream;
if (strcasecmp('HEAD', $request->getMethod())) {
$sink = $this->createSink($stream, $options);
}
$response = new Psr7\Response($status, $headers, $sink, $ver, $reason);
if (isset($options['on_headers'])) {
try {
$options['on_headers']($response);
} catch (Exception $e) {
$msg = 'An error was encountered during the on_headers event';
$ex = new RequestException($msg, $request, $response, $e);
return rejection_for($ex);
}
}
// Do not drain when the request is a HEAD request because they have
// no body.
if ($sink !== $stream) {
$this->drain(
$stream,
$sink,
$response->getHeaderLine('Content-Length')
);
}
$this->invokeStats($options, $request, $startTime, $response, null);
return new FulfilledPromise($response);
}
private function createSink(StreamInterface $stream, array $options)
{
if (!empty($options['stream'])) {
return $stream;
}
$sink = isset($options['sink'])
? $options['sink']
: fopen('php://temp', 'r+');
return is_string($sink)
? new Psr7\LazyOpenStream($sink, 'w+')
: Psr7\stream_for($sink);
}
private function checkDecode(array $options, array $headers, $stream)
{
// Automatically decode responses when instructed.
if (!empty($options['decode_content'])) {
$normalizedKeys = normalize_header_keys($headers);
if (isset($normalizedKeys['content-encoding'])) {
$encoding = $headers[$normalizedKeys['content-encoding']];
if ($encoding[0] === 'gzip' || $encoding[0] === 'deflate') {
$stream = new Psr7\InflateStream(
Psr7\stream_for($stream)
);
$headers['x-encoded-content-encoding']
= $headers[$normalizedKeys['content-encoding']];
// Remove content-encoding header
unset($headers[$normalizedKeys['content-encoding']]);
// Fix content-length header
if (isset($normalizedKeys['content-length'])) {
$headers['x-encoded-content-length']
= $headers[$normalizedKeys['content-length']];
$length = (int)$stream->getSize();
if ($length === 0) {
unset($headers[$normalizedKeys['content-length']]);
} else {
$headers[$normalizedKeys['content-length']] = [$length];
}
}
}
}
}
return [$stream, $headers];
}
/**
* Drains the source stream into the "sink" client option.
*
* @param StreamInterface $source
* @param StreamInterface $sink
* @param string $contentLength Header specifying the amount of
* data to read.
*
* @return StreamInterface
* @throws RuntimeException when the sink option is invalid.
*/
private function drain(
StreamInterface $source,
StreamInterface $sink,
$contentLength
)
{
// If a content-length header is provided, then stop reading once
// that number of bytes has been read. This can prevent infinitely
// reading from a stream when dealing with servers that do not honor
// Connection: Close headers.
Psr7\copy_to_stream(
$source,
$sink,
(strlen($contentLength) > 0 && (int)$contentLength > 0) ? (int)$contentLength : -1
);
$sink->seek(0);
$source->close();
return $sink;
}
/**
* Create a resource and check to ensure it was created successfully
*
* @param callable $callback Callable that returns stream resource
*
* @return resource
* @throws RuntimeException on error
*/
private function createResource(callable $callback)
{
$errors = null;
set_error_handler(function ($_, $msg, $file, $line) use (&$errors) {
$errors[] = [
'message' => $msg,
'file' => $file,
'line' => $line
];
return true;
});
$resource = $callback();
restore_error_handler();
if (!$resource) {
$message = 'Error creating resource: ';
foreach ($errors as $err) {
foreach ($err as $key => $value) {
$message .= "[$key] $value" . PHP_EOL;
}
}
throw new RuntimeException(trim($message));
}
return $resource;
}
private function createStream(RequestInterface $request, array $options)
{
static $methods;
if (!$methods) {
$methods = array_flip(get_class_methods(__CLASS__));
}
// HTTP/1.1 streams using the PHP stream wrapper require a
// Connection: close header
if ($request->getProtocolVersion() == '1.1'
&& !$request->hasHeader('Connection')
) {
$request = $request->withHeader('Connection', 'close');
}
// Ensure SSL is verified by default
if (!isset($options['verify'])) {
$options['verify'] = true;
}
$params = [];
$context = $this->getDefaultContext($request);
if (isset($options['on_headers']) && !is_callable($options['on_headers'])) {
throw new InvalidArgumentException('on_headers must be callable');
}
if (!empty($options)) {
foreach ($options as $key => $value) {
$method = "add_{$key}";
if (isset($methods[$method])) {
$this->{$method}($request, $context, $value, $params);
}
}
}
if (isset($options['stream_context'])) {
if (!is_array($options['stream_context'])) {
throw new InvalidArgumentException('stream_context must be an array');
}
$context = array_replace_recursive(
$context,
$options['stream_context']
);
}
// Microsoft NTLM authentication only supported with curl handler
if (isset($options['auth'])
&& is_array($options['auth'])
&& isset($options['auth'][2])
&& 'ntlm' == $options['auth'][2]
) {
throw new InvalidArgumentException('Microsoft NTLM authentication only supported with curl handler');
}
$uri = $this->resolveHost($request, $options);
$context = $this->createResource(
function () use ($context, $params) {
return stream_context_create($context, $params);
}
);
return $this->createResource(
function () use ($uri, &$http_response_header, $context, $options) {
$resource = fopen((string)$uri, 'r', null, $context);
$this->lastHeaders = $http_response_header;
if (isset($options['read_timeout'])) {
$readTimeout = $options['read_timeout'];
$sec = (int)$readTimeout;
$usec = ($readTimeout - $sec) * 100000;
stream_set_timeout($resource, $sec, $usec);
}
return $resource;
}
);
}
private function resolveHost(RequestInterface $request, array $options)
{
$uri = $request->getUri();
if (isset($options['force_ip_resolve']) && !filter_var($uri->getHost(), FILTER_VALIDATE_IP)) {
if ('v4' === $options['force_ip_resolve']) {
$records = dns_get_record($uri->getHost(), DNS_A);
if (!isset($records[0]['ip'])) {
throw new ConnectException(
sprintf(
"Could not resolve IPv4 address for host '%s'",
$uri->getHost()
),
$request
);
}
$uri = $uri->withHost($records[0]['ip']);
} elseif ('v6' === $options['force_ip_resolve']) {
$records = dns_get_record($uri->getHost(), DNS_AAAA);
if (!isset($records[0]['ipv6'])) {
throw new ConnectException(
sprintf(
"Could not resolve IPv6 address for host '%s'",
$uri->getHost()
),
$request
);
}
$uri = $uri->withHost('[' . $records[0]['ipv6'] . ']');
}
}
return $uri;
}
private function getDefaultContext(RequestInterface $request)
{
$headers = '';
foreach ($request->getHeaders() as $name => $value) {
foreach ($value as $val) {
$headers .= "$name: $val\r\n";
}
}
$context = [
'http' => [
'method' => $request->getMethod(),
'header' => $headers,
'protocol_version' => $request->getProtocolVersion(),
'ignore_errors' => true,
'follow_location' => 0,
],
];
$body = (string)$request->getBody();
if (!empty($body)) {
$context['http']['content'] = $body;
// Prevent the HTTP handler from adding a Content-Type header.
if (!$request->hasHeader('Content-Type')) {
$context['http']['header'] .= "Content-Type:\r\n";
}
}
$context['http']['header'] = rtrim($context['http']['header']);
return $context;
}
private function add_proxy(RequestInterface $request, &$options, $value, &$params)
{
if (!is_array($value)) {
$options['http']['proxy'] = $value;
} else {
$scheme = $request->getUri()->getScheme();
if (isset($value[$scheme])) {
if (!isset($value['no'])
|| !is_host_in_noproxy(
$request->getUri()->getHost(),
$value['no']
)
) {
$options['http']['proxy'] = $value[$scheme];
}
}
}
}
private function add_timeout(RequestInterface $request, &$options, $value, &$params)
{
if ($value > 0) {
$options['http']['timeout'] = $value;
}
}
private function add_verify(RequestInterface $request, &$options, $value, &$params)
{
if ($value === true) {
// PHP 5.6 or greater will find the system cert by default. When
// < 5.6, use the Guzzle bundled cacert.
if (PHP_VERSION_ID < 50600) {
$options['ssl']['cafile'] = default_ca_bundle();
}
} elseif (is_string($value)) {
$options['ssl']['cafile'] = $value;
if (!file_exists($value)) {
throw new RuntimeException("SSL CA bundle not found: $value");
}
} elseif ($value === false) {
$options['ssl']['verify_peer'] = false;
$options['ssl']['verify_peer_name'] = false;
return;
} else {
throw new InvalidArgumentException('Invalid verify request option');
}
$options['ssl']['verify_peer'] = true;
$options['ssl']['verify_peer_name'] = true;
$options['ssl']['allow_self_signed'] = false;
}
private function add_cert(RequestInterface $request, &$options, $value, &$params)
{
if (is_array($value)) {
$options['ssl']['passphrase'] = $value[1];
$value = $value[0];
}
if (!file_exists($value)) {
throw new RuntimeException("SSL certificate not found: {$value}");
}
$options['ssl']['local_cert'] = $value;
}
private function add_progress(RequestInterface $request, &$options, $value, &$params)
{
$this->addNotification(
$params,
function ($code, $a, $b, $c, $transferred, $total) use ($value) {
if ($code == STREAM_NOTIFY_PROGRESS) {
$value($total, $transferred, null, null);
}
}
);
}
private function add_debug(RequestInterface $request, &$options, $value, &$params)
{
if ($value === false) {
return;
}
static $map = [
STREAM_NOTIFY_CONNECT => 'CONNECT',
STREAM_NOTIFY_AUTH_REQUIRED => 'AUTH_REQUIRED',
STREAM_NOTIFY_AUTH_RESULT => 'AUTH_RESULT',
STREAM_NOTIFY_MIME_TYPE_IS => 'MIME_TYPE_IS',
STREAM_NOTIFY_FILE_SIZE_IS => 'FILE_SIZE_IS',
STREAM_NOTIFY_REDIRECTED => 'REDIRECTED',
STREAM_NOTIFY_PROGRESS => 'PROGRESS',
STREAM_NOTIFY_FAILURE => 'FAILURE',
STREAM_NOTIFY_COMPLETED => 'COMPLETED',
STREAM_NOTIFY_RESOLVE => 'RESOLVE',
];
static $args = ['severity', 'message', 'message_code',
'bytes_transferred', 'bytes_max'];
$value = debug_resource($value);
$ident = $request->getMethod() . ' ' . $request->getUri()->withFragment('');
$this->addNotification(
$params,
function () use ($ident, $value, $map, $args) {
$passed = func_get_args();
$code = array_shift($passed);
fprintf($value, '<%s> [%s] ', $ident, $map[$code]);
foreach (array_filter($passed) as $i => $v) {
fwrite($value, $args[$i] . ': "' . $v . '" ');
}
fwrite($value, "\n");
}
);
}
private function addNotification(array &$params, callable $notify)
{
// Wrap the existing function if needed.
if (!isset($params['notification'])) {
$params['notification'] = $notify;
} else {
$params['notification'] = $this->callArray([
$params['notification'],
$notify
]);
}
}
private function callArray(array $functions)
{
return function () use ($functions) {
$args = func_get_args();
foreach ($functions as $fn) {
call_user_func_array($fn, $args);
}
};
}
}

Some files were not shown because too many files have changed in this diff Show More