From 911623fe97919d44feac0a5cc2d4a3030dab2334 Mon Sep 17 00:00:00 2001 From: tuyennn Date: Sat, 30 Nov 2019 11:43:20 +0700 Subject: [PATCH 1/7] [TASK] Add debug configuration --- Model/Config.php | 20 ++++++++--- Model/Instagram.php | 74 +++++++++++++++++++++++++++++++++------- etc/adminhtml/system.xml | 4 +++ etc/config.xml | 1 + 4 files changed, 83 insertions(+), 16 deletions(-) diff --git a/Model/Config.php b/Model/Config.php index 5349cef..4edac6d 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -24,6 +24,7 @@ class Config const XML_PATH_INSTAGRAM_PASSWORD = 'auto_instagram_post/general/password'; const XML_PATH_DEFAULT_IMAGE = 'auto_instagram_post/general/upload_image_id'; const XML_PATH_ENABLE_OBSERVER = 'auto_instagram_post/general/enabled_observer'; + const XML_PATH_ENABLE_DEBUG = 'auto_instagram_post/general/enable_debug'; const XML_PATH_COMMENT_ENABLED = 'auto_instagram_post/comment_hashtag/enabled'; const XML_PATH_COMMENT_PRODUCT_DESCRIPTION = 'auto_instagram_post/comment_hashtag/product_description'; @@ -184,11 +185,9 @@ public function getDefaultImage($store = null) return ''; } - $imagePath = $this->directoryList->getPath('media') + return $this->directoryList->getPath('media') . DIRECTORY_SEPARATOR . $uploadDir . DIRECTORY_SEPARATOR . $image; - - return $imagePath; } /** @@ -199,13 +198,26 @@ public function getDefaultImage($store = null) */ public function isObserverEnabled($store = null) { - return (bool)$this->scopeConfig->getValue( + return $this->scopeConfig->isSetFlag( self::XML_PATH_ENABLE_OBSERVER, ScopeInterface::SCOPE_STORE, $store ); } + /** + * @param null $store + * @return bool + */ + public function isDebugEnabled($store = null) + { + return $this->scopeConfig->isSetFlag( + self::XML_PATH_ENABLE_DEBUG, + ScopeInterface::SCOPE_STORE, + $store + ); + } + /** * Check if Hashtag was enabled * diff --git a/Model/Instagram.php b/Model/Instagram.php index 7e9a48d..a363219 100644 --- a/Model/Instagram.php +++ b/Model/Instagram.php @@ -24,19 +24,69 @@ class Instagram const STATUS_OK = 'ok'; const STATUS_FAIL = 'fail'; - protected $username; // Instagram username - protected $password; // Instagram password - protected $debug; // Debug - - protected $uuid; // UUID - protected $deviceId; // Device ID - protected $usernameId; // Username ID - protected $token; // _csrftoken - protected $isLoggedIn = false; // Session status - protected $rankToken; // Rank token - protected $IGDataPath; // Data storage path + /** + * @var string + */ + protected $username; + + /** + * @var string + */ + protected $password; + + /** + * @var bool + */ + protected $debug; + + /** + * @var string + */ + protected $uuid; + + /** + * @var string + */ + protected $deviceId; + + /** + * @var string + */ + protected $usernameId; + + /** + * @var string + */ + protected $token; + + /** + * @var bool + */ + protected $isLoggedIn = false; + + /** + * @var string + */ + protected $rankToken; + + /** + * @var string + */ + protected $IGDataPath; + + /** + * @var DirectoryList + */ protected $directoryList; + + /** + * @var Config + */ protected $config; + + /** + * @var LoggerInterface + */ protected $logger; @@ -63,8 +113,8 @@ public function __construct( $this->directoryList = $directoryList; + $this->debug = $this->config->isDebugEnabled(); $this->IGDataPath = $this->directoryList->getPath('var') . DIRECTORY_SEPARATOR; - } public function generateDeviceId() diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 8f6fedd..cfbf0e6 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -55,6 +55,10 @@ 1 + + + Magento\Config\Model\Config\Source\Yesno + diff --git a/etc/config.xml b/etc/config.xml index f2108aa..1111813 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -7,6 +7,7 @@ 1 + 0 0 From cb15068d5c7f546deed3da6549256c28e40f62ac Mon Sep 17 00:00:00 2001 From: tuyennn Date: Sat, 30 Nov 2019 11:56:47 +0700 Subject: [PATCH 2/7] [TASK] Refactoring --- Api/Data/ItemInterface.php | 3 +++ Api/Data/ItemSearchResultInterface.php | 3 +++ Api/ItemRepositoryInterface.php | 3 +++ Api/WorkerInterface.php | 3 +++ Block/Adminhtml/Product.php | 1 - Block/Adminhtml/System/Config/Hashtag.php | 1 - Block/Adminhtml/System/Config/Hashtag/Activation.php | 1 - Block/Adminhtml/System/Config/TestConnection.php | 1 - Controller/Adminhtml/Manage/Index.php | 1 - Controller/Adminhtml/Manage/MassPost.php | 1 - Controller/Adminhtml/Manage/Post.php | 1 - Controller/Adminhtml/System/Config/TestConnection.php | 1 - Cron/SchedulePost.php | 1 - Helper/Data.php | 3 +-- Model/Config.php | 1 - Model/Config/Backend/Cron.php | 1 - Model/Config/Backend/Hashtag.php | 1 - Model/Config/Backend/Image.php | 1 - Model/ImageProcessor.php | 1 - Model/Instagram.php | 2 -- Model/Instagram/Worker.php | 1 - Model/Item.php | 1 - Model/Logger.php | 1 - Model/ResourceModel/Item.php | 1 - Model/ResourceModel/Item/Collection.php | 1 - Model/Serialize/Serializer.php | 3 +-- Model/Source/PostStatus.php | 1 - Observer/Catalog/ProductSaveAfter.php | 1 - Plugin/Model/Menu/BuilderPlugin.php | 1 - Plugin/Ui/DataProvider/Product/DataProviderPlugin.php | 1 - Ui/Component/Listing/Columns/PostStatusOptions.php | 3 +-- Ui/DataProvider/Product/DataProvider.php | 1 - 32 files changed, 15 insertions(+), 32 deletions(-) diff --git a/Api/Data/ItemInterface.php b/Api/Data/ItemInterface.php index 87dd6f1..66578e8 100644 --- a/Api/Data/ItemInterface.php +++ b/Api/Data/ItemInterface.php @@ -2,6 +2,9 @@ namespace GhoSter\AutoInstagramPost\Api\Data; +/** + * Interface ItemInterface + */ interface ItemInterface { diff --git a/Api/Data/ItemSearchResultInterface.php b/Api/Data/ItemSearchResultInterface.php index bd3702c..12541c6 100644 --- a/Api/Data/ItemSearchResultInterface.php +++ b/Api/Data/ItemSearchResultInterface.php @@ -4,6 +4,9 @@ use Magento\Framework\Api\SearchResultsInterface; +/** + * Interface ItemSearchResultInterface + */ interface ItemSearchResultInterface extends SearchResultsInterface { diff --git a/Api/ItemRepositoryInterface.php b/Api/ItemRepositoryInterface.php index 12c4eb7..e06c293 100644 --- a/Api/ItemRepositoryInterface.php +++ b/Api/ItemRepositoryInterface.php @@ -5,6 +5,9 @@ use Magento\Framework\Api\SearchCriteriaInterface; use GhoSter\AutoInstagramPost\Api\Data\ItemInterface; +/** + * Interface ItemRepositoryInterface + */ interface ItemRepositoryInterface { diff --git a/Api/WorkerInterface.php b/Api/WorkerInterface.php index 8761bf6..cbc7e0d 100644 --- a/Api/WorkerInterface.php +++ b/Api/WorkerInterface.php @@ -5,6 +5,9 @@ use Magento\Catalog\Model\Product; use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection; +/** + * Interface WorkerInterface + */ interface WorkerInterface { diff --git a/Block/Adminhtml/Product.php b/Block/Adminhtml/Product.php index 2a3f589..96d454f 100644 --- a/Block/Adminhtml/Product.php +++ b/Block/Adminhtml/Product.php @@ -6,7 +6,6 @@ /** * Class Product - * @package GhoSter\AutoInstagramPost\Block\Adminhtml */ class Product extends \Magento\Backend\Block\Widget\Container { diff --git a/Block/Adminhtml/System/Config/Hashtag.php b/Block/Adminhtml/System/Config/Hashtag.php index b4d4ca0..17b2d01 100644 --- a/Block/Adminhtml/System/Config/Hashtag.php +++ b/Block/Adminhtml/System/Config/Hashtag.php @@ -7,7 +7,6 @@ /** * Class Hashtag - * @package GhoSter\AutoInstagramPost\Block\Adminhtml\System\Config */ class Hashtag extends AbstractFieldArray { diff --git a/Block/Adminhtml/System/Config/Hashtag/Activation.php b/Block/Adminhtml/System/Config/Hashtag/Activation.php index 1a89022..381364a 100644 --- a/Block/Adminhtml/System/Config/Hashtag/Activation.php +++ b/Block/Adminhtml/System/Config/Hashtag/Activation.php @@ -8,7 +8,6 @@ /** * Class Activation - * @package GhoSter\AutoInstagramPost\Block\Adminhtml\System\Config\Hashtag */ class Activation extends Select { diff --git a/Block/Adminhtml/System/Config/TestConnection.php b/Block/Adminhtml/System/Config/TestConnection.php index 6ffb405..0914862 100644 --- a/Block/Adminhtml/System/Config/TestConnection.php +++ b/Block/Adminhtml/System/Config/TestConnection.php @@ -8,7 +8,6 @@ /** * Class TestConnection - * @package GhoSter\AutoInstagramPost\Block\Adminhtml\System\Config */ class TestConnection extends Field { diff --git a/Controller/Adminhtml/Manage/Index.php b/Controller/Adminhtml/Manage/Index.php index ec0c2c3..2d6af63 100644 --- a/Controller/Adminhtml/Manage/Index.php +++ b/Controller/Adminhtml/Manage/Index.php @@ -10,7 +10,6 @@ /** * Class Index - * @package GhoSter\AutoInstagramPost\Controller\Adminhtml\Manage */ class Index extends ProductController { diff --git a/Controller/Adminhtml/Manage/MassPost.php b/Controller/Adminhtml/Manage/MassPost.php index 59eea5b..8e16a56 100644 --- a/Controller/Adminhtml/Manage/MassPost.php +++ b/Controller/Adminhtml/Manage/MassPost.php @@ -13,7 +13,6 @@ /** * Class MassPost - * @package GhoSter\AutoInstagramPost\Controller\Adminhtml\Manage */ class MassPost extends Action { diff --git a/Controller/Adminhtml/Manage/Post.php b/Controller/Adminhtml/Manage/Post.php index 84fba0e..8fc1563 100644 --- a/Controller/Adminhtml/Manage/Post.php +++ b/Controller/Adminhtml/Manage/Post.php @@ -12,7 +12,6 @@ /** * Class Post - * @package GhoSter\AutoInstagramPost\Controller\Adminhtml\Manage */ class Post extends Action { diff --git a/Controller/Adminhtml/System/Config/TestConnection.php b/Controller/Adminhtml/System/Config/TestConnection.php index 8dab4dd..887105f 100644 --- a/Controller/Adminhtml/System/Config/TestConnection.php +++ b/Controller/Adminhtml/System/Config/TestConnection.php @@ -12,7 +12,6 @@ /** * Class TestConnection - * @package GhoSter\AutoInstagramPost\Controller\Adminhtml\System\Config */ class TestConnection extends Action { diff --git a/Cron/SchedulePost.php b/Cron/SchedulePost.php index fe23c2d..8287728 100644 --- a/Cron/SchedulePost.php +++ b/Cron/SchedulePost.php @@ -10,7 +10,6 @@ /** * Class SchedulePost - * @package GhoSter\AutoInstagramPost\Cron */ class SchedulePost { diff --git a/Helper/Data.php b/Helper/Data.php index 2bb2f6d..049f226 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -18,7 +18,6 @@ /** * Class Data - * @package GhoSter\AutoInstagramPost\Helper */ class Data extends AbstractHelper { @@ -265,7 +264,7 @@ public function getInstagramPostDescription($product) $html = ''; $stringTemplate = $this->config->getInstagramPostTemplate(); - if (preg_match_all("~\{\{\s*(.*?)\s*\}\}~", $stringTemplate, $matches)) { + if (preg_match_all("~{{\s*(.*?)\s*}}~", $stringTemplate, $matches)) { if (isset($matches[1]) && is_array($matches[1])) { foreach ($matches[1] as $key => $match) { $addOnSpace = $key !== 0 ? self::SPACE_STRING : ''; diff --git a/Model/Config.php b/Model/Config.php index 4edac6d..02a77c3 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -15,7 +15,6 @@ /** * Class Config - * @package GhoSter\AutoInstagramPost\Model */ class Config { diff --git a/Model/Config/Backend/Cron.php b/Model/Config/Backend/Cron.php index c90f13b..beb948a 100644 --- a/Model/Config/Backend/Cron.php +++ b/Model/Config/Backend/Cron.php @@ -10,7 +10,6 @@ /** * Class Cron - * @package GhoSter\AutoInstagramPost\Model\Config\Backend */ class Cron extends ConfigValue { diff --git a/Model/Config/Backend/Hashtag.php b/Model/Config/Backend/Hashtag.php index 92e6bb9..231b890 100644 --- a/Model/Config/Backend/Hashtag.php +++ b/Model/Config/Backend/Hashtag.php @@ -6,7 +6,6 @@ /** * Class Hashtag - * @package GhoSter\AutoInstagramPost\Model\Config\Backend */ class Hashtag extends ArraySerialized { diff --git a/Model/Config/Backend/Image.php b/Model/Config/Backend/Image.php index 58e04b0..63ac4e8 100644 --- a/Model/Config/Backend/Image.php +++ b/Model/Config/Backend/Image.php @@ -4,7 +4,6 @@ /** * Class Image - * @package GhoSter\AutoInstagramPost\Model\Config\Backend */ class Image extends \Magento\Config\Model\Config\Backend\Image { diff --git a/Model/ImageProcessor.php b/Model/ImageProcessor.php index 3922c63..f9150d0 100644 --- a/Model/ImageProcessor.php +++ b/Model/ImageProcessor.php @@ -11,7 +11,6 @@ /** * Class ImageProcessor - * @package GhoSter\AutoInstagramPost\Model */ class ImageProcessor { diff --git a/Model/Instagram.php b/Model/Instagram.php index a363219..d1b20e6 100644 --- a/Model/Instagram.php +++ b/Model/Instagram.php @@ -10,8 +10,6 @@ * Class Instagram * * Rewrite Code From : https://github.com/mgp25/Instagram-API - * - * @package GhoSter\AutoInstagramPost\Model */ //@codingStandardsIgnoreFile class Instagram diff --git a/Model/Instagram/Worker.php b/Model/Instagram/Worker.php index d897d3a..7dd8413 100644 --- a/Model/Instagram/Worker.php +++ b/Model/Instagram/Worker.php @@ -15,7 +15,6 @@ /** * Class Worker - * @package GhoSter\AutoInstagramPost\Model\Instagram */ class Worker implements WorkerInterface { diff --git a/Model/Item.php b/Model/Item.php index 1ad1e87..b0cc2d5 100644 --- a/Model/Item.php +++ b/Model/Item.php @@ -7,7 +7,6 @@ /** * Class Item - * @package GhoSter\AutoInstagramPost\Model */ class Item extends AbstractModel implements ItemInterface { diff --git a/Model/Logger.php b/Model/Logger.php index 8d958c5..b9945c4 100644 --- a/Model/Logger.php +++ b/Model/Logger.php @@ -8,7 +8,6 @@ /** * Class Logger - * @package GhoSter\AutoInstagramPost\Model */ class Logger { diff --git a/Model/ResourceModel/Item.php b/Model/ResourceModel/Item.php index f7141c1..6c54c1f 100644 --- a/Model/ResourceModel/Item.php +++ b/Model/ResourceModel/Item.php @@ -8,7 +8,6 @@ /** * Class Item - * @package GhoSter\AutoInstagramPost\Model\ResourceModel */ class Item extends AbstractDb { diff --git a/Model/ResourceModel/Item/Collection.php b/Model/ResourceModel/Item/Collection.php index e38bfcd..d429a2b 100644 --- a/Model/ResourceModel/Item/Collection.php +++ b/Model/ResourceModel/Item/Collection.php @@ -6,7 +6,6 @@ /** * Class Collection - * @package GhoSter\AutoInstagramPost\Model\ResourceModel\Item */ class Collection extends AbstractCollection { diff --git a/Model/Serialize/Serializer.php b/Model/Serialize/Serializer.php index bc8c8f9..f03eef5 100644 --- a/Model/Serialize/Serializer.php +++ b/Model/Serialize/Serializer.php @@ -4,7 +4,6 @@ /** * Class Serializer - * @package GhoSter\AutoInstagramPost\Model\Serialize */ //@codingStandardsIgnoreFile class Serializer @@ -92,4 +91,4 @@ private function _isSerialized($value, &$result = null) } return true; } -} \ No newline at end of file +} diff --git a/Model/Source/PostStatus.php b/Model/Source/PostStatus.php index 1a94ccd..7d0104e 100644 --- a/Model/Source/PostStatus.php +++ b/Model/Source/PostStatus.php @@ -8,7 +8,6 @@ /** * Class PostStatus - * @package GhoSter\AutoInstagramPost\Model\Source */ class PostStatus extends AbstractSource implements SourceInterface, OptionSourceInterface { diff --git a/Observer/Catalog/ProductSaveAfter.php b/Observer/Catalog/ProductSaveAfter.php index 38f90d0..2b680d6 100644 --- a/Observer/Catalog/ProductSaveAfter.php +++ b/Observer/Catalog/ProductSaveAfter.php @@ -15,7 +15,6 @@ /** * Class ProductSaveAfter - * @package GhoSter\AutoInstagramPost\Observer\Catalog */ class ProductSaveAfter implements ObserverInterface { diff --git a/Plugin/Model/Menu/BuilderPlugin.php b/Plugin/Model/Menu/BuilderPlugin.php index aea3b19..0d4e59d 100644 --- a/Plugin/Model/Menu/BuilderPlugin.php +++ b/Plugin/Model/Menu/BuilderPlugin.php @@ -8,7 +8,6 @@ /** * Class BuilderPlugin - * @package GhoSter\AutoInstagramPost\Plugin\Model\Menu */ class BuilderPlugin { diff --git a/Plugin/Ui/DataProvider/Product/DataProviderPlugin.php b/Plugin/Ui/DataProvider/Product/DataProviderPlugin.php index 9cc3393..536830a 100644 --- a/Plugin/Ui/DataProvider/Product/DataProviderPlugin.php +++ b/Plugin/Ui/DataProvider/Product/DataProviderPlugin.php @@ -8,7 +8,6 @@ /** * Class DataProviderPlugin - * @package GhoSter\AutoInstagramPost\Plugin\Ui\DataProvider\Product */ class DataProviderPlugin { diff --git a/Ui/Component/Listing/Columns/PostStatusOptions.php b/Ui/Component/Listing/Columns/PostStatusOptions.php index 4903d4e..649dce4 100644 --- a/Ui/Component/Listing/Columns/PostStatusOptions.php +++ b/Ui/Component/Listing/Columns/PostStatusOptions.php @@ -6,9 +6,8 @@ /** * Class PostStatusOptions - * @package GhoSter\AutoInstagramPost\Ui\Component\Listing\Columns + * @codeCoverageIgnore */ -// @codingStandardsIgnoreFile class PostStatusOptions extends Column { diff --git a/Ui/DataProvider/Product/DataProvider.php b/Ui/DataProvider/Product/DataProvider.php index ec3da7a..76e8871 100644 --- a/Ui/DataProvider/Product/DataProvider.php +++ b/Ui/DataProvider/Product/DataProvider.php @@ -6,7 +6,6 @@ /** * Class DataProvider - * @package GhoSter\AutoInstagramPost\Ui\DataProvider\Product */ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider { From 14e34a89e1e4855c9d45345b635c50e7cf1794b5 Mon Sep 17 00:00:00 2001 From: tuyennn Date: Sat, 30 Nov 2019 12:09:12 +0700 Subject: [PATCH 3/7] [TASK] Refactoring --- Block/Adminhtml/Product.php | 2 ++ Block/Adminhtml/System/Config/Hashtag.php | 2 ++ Block/Adminhtml/System/Config/Hashtag/Activation.php | 2 ++ Block/Adminhtml/System/Config/TestConnection.php | 2 ++ Controller/Adminhtml/Manage/Index.php | 2 ++ Controller/Adminhtml/Manage/MassPost.php | 2 ++ Controller/Adminhtml/Manage/Post.php | 2 ++ Controller/Adminhtml/System/Config/TestConnection.php | 6 +++++- Cron/SchedulePost.php | 2 ++ Helper/Data.php | 2 ++ Model/Config.php | 2 ++ Model/Config/Backend/Cron.php | 2 ++ Model/Config/Backend/Hashtag.php | 2 ++ Model/Config/Backend/Image.php | 2 ++ Model/ImageProcessor.php | 2 ++ Model/Instagram/Worker.php | 2 ++ Model/Item.php | 2 ++ Model/Logger.php | 2 ++ Model/ResourceModel/Item.php | 2 ++ Model/ResourceModel/Item/Collection.php | 2 ++ Model/Serialize/Serializer.php | 2 ++ Model/Source/PostStatus.php | 2 ++ Observer/Catalog/ProductSaveAfter.php | 2 ++ Plugin/Model/Menu/BuilderPlugin.php | 2 ++ Plugin/Ui/DataProvider/Product/DataProviderPlugin.php | 2 ++ Ui/Component/Listing/Columns/PostStatusOptions.php | 2 ++ Ui/Component/Listing/Columns/ProductActions.php | 6 +++++- Ui/DataProvider/Product/DataProvider.php | 2 ++ 28 files changed, 62 insertions(+), 2 deletions(-) diff --git a/Block/Adminhtml/Product.php b/Block/Adminhtml/Product.php index 96d454f..ed60680 100644 --- a/Block/Adminhtml/Product.php +++ b/Block/Adminhtml/Product.php @@ -5,6 +5,8 @@ use Magento\Eav\Setup\EavSetup; /** + * Product Widget Container + * * Class Product */ class Product extends \Magento\Backend\Block\Widget\Container diff --git a/Block/Adminhtml/System/Config/Hashtag.php b/Block/Adminhtml/System/Config/Hashtag.php index 17b2d01..db5f465 100644 --- a/Block/Adminhtml/System/Config/Hashtag.php +++ b/Block/Adminhtml/System/Config/Hashtag.php @@ -6,6 +6,8 @@ use GhoSter\AutoInstagramPost\Block\Adminhtml\System\Config\Hashtag\Activation; /** + * Hashtag Field + * * Class Hashtag */ class Hashtag extends AbstractFieldArray diff --git a/Block/Adminhtml/System/Config/Hashtag/Activation.php b/Block/Adminhtml/System/Config/Hashtag/Activation.php index 381364a..93dca31 100644 --- a/Block/Adminhtml/System/Config/Hashtag/Activation.php +++ b/Block/Adminhtml/System/Config/Hashtag/Activation.php @@ -7,6 +7,8 @@ use Magento\Config\Model\Config\Source\Enabledisable; /** + * Activation Select Element + * * Class Activation */ class Activation extends Select diff --git a/Block/Adminhtml/System/Config/TestConnection.php b/Block/Adminhtml/System/Config/TestConnection.php index 0914862..fa9eb13 100644 --- a/Block/Adminhtml/System/Config/TestConnection.php +++ b/Block/Adminhtml/System/Config/TestConnection.php @@ -7,6 +7,8 @@ use Magento\Framework\Data\Form\Element\AbstractElement; /** + * TestConnection Field + * * Class TestConnection */ class TestConnection extends Field diff --git a/Controller/Adminhtml/Manage/Index.php b/Controller/Adminhtml/Manage/Index.php index 2d6af63..34de98c 100644 --- a/Controller/Adminhtml/Manage/Index.php +++ b/Controller/Adminhtml/Manage/Index.php @@ -9,6 +9,8 @@ use Magento\Backend\Model\View\Result\Page; /** + * Manager Index Controller + * * Class Index */ class Index extends ProductController diff --git a/Controller/Adminhtml/Manage/MassPost.php b/Controller/Adminhtml/Manage/MassPost.php index 8e16a56..52a5a57 100644 --- a/Controller/Adminhtml/Manage/MassPost.php +++ b/Controller/Adminhtml/Manage/MassPost.php @@ -12,6 +12,8 @@ use GhoSter\AutoInstagramPost\Model\Config as InstagramConfig; /** + * Manager MassPost Controller + * * Class MassPost */ class MassPost extends Action diff --git a/Controller/Adminhtml/Manage/Post.php b/Controller/Adminhtml/Manage/Post.php index 8fc1563..029ef6f 100644 --- a/Controller/Adminhtml/Manage/Post.php +++ b/Controller/Adminhtml/Manage/Post.php @@ -11,6 +11,8 @@ use GhoSter\AutoInstagramPost\Model\Instagram\Worker as InstagramWorker; /** + * Manager Post Controller + * * Class Post */ class Post extends Action diff --git a/Controller/Adminhtml/System/Config/TestConnection.php b/Controller/Adminhtml/System/Config/TestConnection.php index 887105f..6186d1a 100644 --- a/Controller/Adminhtml/System/Config/TestConnection.php +++ b/Controller/Adminhtml/System/Config/TestConnection.php @@ -11,6 +11,8 @@ use Psr\Log\LoggerInterface; /** + * Test Connection Controller + * * Class TestConnection */ class TestConnection extends Action @@ -92,7 +94,9 @@ public function execute() $responseData = [ 'success' => $status, - 'message' => $status ? __('Connection Success') : __('Unauthorized Instagram Account, check your user/password settings') + 'message' => $status + ? __('Connection Success') + : __('Unauthorized Instagram Account, check your user/password settings') ]; } else { diff --git a/Cron/SchedulePost.php b/Cron/SchedulePost.php index 8287728..808b173 100644 --- a/Cron/SchedulePost.php +++ b/Cron/SchedulePost.php @@ -9,6 +9,8 @@ use Psr\Log\LoggerInterface; /** + * Cron for SchedulePost + * * Class SchedulePost */ class SchedulePost diff --git a/Helper/Data.php b/Helper/Data.php index 049f226..2725cbf 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -17,6 +17,8 @@ use GhoSter\AutoInstagramPost\Model\Config as InstagramConfig; /** + * AutoInstagramPost Data Helper + * * Class Data */ class Data extends AbstractHelper diff --git a/Model/Config.php b/Model/Config.php index 02a77c3..1432df4 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -14,6 +14,8 @@ use GhoSter\AutoInstagramPost\Model\Serialize\Serializer; /** + * AutoInstagramPost Config + * * Class Config */ class Config diff --git a/Model/Config/Backend/Cron.php b/Model/Config/Backend/Cron.php index beb948a..cb347fb 100644 --- a/Model/Config/Backend/Cron.php +++ b/Model/Config/Backend/Cron.php @@ -9,6 +9,8 @@ use GhoSter\AutoInstagramPost\Model\Config as InstagramConfig; /** + * Config Value for Cron + * * Class Cron */ class Cron extends ConfigValue diff --git a/Model/Config/Backend/Hashtag.php b/Model/Config/Backend/Hashtag.php index 231b890..ad12577 100644 --- a/Model/Config/Backend/Hashtag.php +++ b/Model/Config/Backend/Hashtag.php @@ -5,6 +5,8 @@ use Magento\Config\Model\Config\Backend\Serialized\ArraySerialized; /** + * Config Value for Hashtag + * * Class Hashtag */ class Hashtag extends ArraySerialized diff --git a/Model/Config/Backend/Image.php b/Model/Config/Backend/Image.php index 63ac4e8..35603b1 100644 --- a/Model/Config/Backend/Image.php +++ b/Model/Config/Backend/Image.php @@ -3,6 +3,8 @@ namespace GhoSter\AutoInstagramPost\Model\Config\Backend; /** + * Config Value for Image + * * Class Image */ class Image extends \Magento\Config\Model\Config\Backend\Image diff --git a/Model/ImageProcessor.php b/Model/ImageProcessor.php index f9150d0..672bb31 100644 --- a/Model/ImageProcessor.php +++ b/Model/ImageProcessor.php @@ -10,6 +10,8 @@ use GhoSter\AutoInstagramPost\Model\Config as InstagramConfig; /** + * AutoInstagramPost ImageProcessor + * * Class ImageProcessor */ class ImageProcessor diff --git a/Model/Instagram/Worker.php b/Model/Instagram/Worker.php index 7dd8413..14b34da 100644 --- a/Model/Instagram/Worker.php +++ b/Model/Instagram/Worker.php @@ -14,6 +14,8 @@ use Psr\Log\LoggerInterface; /** + * Instagram Worker + * * Class Worker */ class Worker implements WorkerInterface diff --git a/Model/Item.php b/Model/Item.php index b0cc2d5..0650bde 100644 --- a/Model/Item.php +++ b/Model/Item.php @@ -6,6 +6,8 @@ use GhoSter\AutoInstagramPost\Api\Data\ItemInterface; /** + * Item Model + * * Class Item */ class Item extends AbstractModel implements ItemInterface diff --git a/Model/Logger.php b/Model/Logger.php index b9945c4..e424ef2 100644 --- a/Model/Logger.php +++ b/Model/Logger.php @@ -7,6 +7,8 @@ use GhoSter\AutoInstagramPost\Model\ItemFactory; /** + * Logger for AutoInstagramPost + * * Class Logger */ class Logger diff --git a/Model/ResourceModel/Item.php b/Model/ResourceModel/Item.php index 6c54c1f..bdeb221 100644 --- a/Model/ResourceModel/Item.php +++ b/Model/ResourceModel/Item.php @@ -7,6 +7,8 @@ use Magento\Framework\Stdlib\DateTime\DateTime; /** + * Instagram Item ResourceModel + * * Class Item */ class Item extends AbstractDb diff --git a/Model/ResourceModel/Item/Collection.php b/Model/ResourceModel/Item/Collection.php index d429a2b..e519acb 100644 --- a/Model/ResourceModel/Item/Collection.php +++ b/Model/ResourceModel/Item/Collection.php @@ -5,6 +5,8 @@ use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; /** + * Instagram Item Collection + * * Class Collection */ class Collection extends AbstractCollection diff --git a/Model/Serialize/Serializer.php b/Model/Serialize/Serializer.php index f03eef5..57e031b 100644 --- a/Model/Serialize/Serializer.php +++ b/Model/Serialize/Serializer.php @@ -3,6 +3,8 @@ namespace GhoSter\AutoInstagramPost\Model\Serialize; /** + * Serializer for deprecated Magento version + * * Class Serializer */ //@codingStandardsIgnoreFile diff --git a/Model/Source/PostStatus.php b/Model/Source/PostStatus.php index 7d0104e..2d691c0 100644 --- a/Model/Source/PostStatus.php +++ b/Model/Source/PostStatus.php @@ -7,6 +7,8 @@ use Magento\Framework\Data\OptionSourceInterface; /** + * Source PostStatus + * * Class PostStatus */ class PostStatus extends AbstractSource implements SourceInterface, OptionSourceInterface diff --git a/Observer/Catalog/ProductSaveAfter.php b/Observer/Catalog/ProductSaveAfter.php index 2b680d6..58941a3 100644 --- a/Observer/Catalog/ProductSaveAfter.php +++ b/Observer/Catalog/ProductSaveAfter.php @@ -14,6 +14,8 @@ use GhoSter\AutoInstagramPost\Helper\Data as InstagramHelper; /** + * Observer for Product + * * Class ProductSaveAfter */ class ProductSaveAfter implements ObserverInterface diff --git a/Plugin/Model/Menu/BuilderPlugin.php b/Plugin/Model/Menu/BuilderPlugin.php index 0d4e59d..6694e19 100644 --- a/Plugin/Model/Menu/BuilderPlugin.php +++ b/Plugin/Model/Menu/BuilderPlugin.php @@ -7,6 +7,8 @@ use GhoSter\AutoInstagramPost\Model\Config as InstagramConfig; /** + * Plugin for Menu + * * Class BuilderPlugin */ class BuilderPlugin diff --git a/Plugin/Ui/DataProvider/Product/DataProviderPlugin.php b/Plugin/Ui/DataProvider/Product/DataProviderPlugin.php index 536830a..162329e 100644 --- a/Plugin/Ui/DataProvider/Product/DataProviderPlugin.php +++ b/Plugin/Ui/DataProvider/Product/DataProviderPlugin.php @@ -7,6 +7,8 @@ use GhoSter\AutoInstagramPost\Ui\DataProvider\Product\DataProvider; /** + * Data Provider for Grid + * * Class DataProviderPlugin */ class DataProviderPlugin diff --git a/Ui/Component/Listing/Columns/PostStatusOptions.php b/Ui/Component/Listing/Columns/PostStatusOptions.php index 649dce4..41a4dee 100644 --- a/Ui/Component/Listing/Columns/PostStatusOptions.php +++ b/Ui/Component/Listing/Columns/PostStatusOptions.php @@ -5,6 +5,8 @@ use Magento\Ui\Component\Listing\Columns\Column; /** + * PostStatusOptions Renderer + * * Class PostStatusOptions * @codeCoverageIgnore */ diff --git a/Ui/Component/Listing/Columns/ProductActions.php b/Ui/Component/Listing/Columns/ProductActions.php index 9e5d325..4f40056 100644 --- a/Ui/Component/Listing/Columns/ProductActions.php +++ b/Ui/Component/Listing/Columns/ProductActions.php @@ -8,6 +8,8 @@ use GhoSter\AutoInstagramPost\Model\Config as InstagramConfig; /** + * Product Actions Renderer + * * Class ProductActions * * @api @@ -71,7 +73,9 @@ public function prepareDataSource(array $dataSource) 'label' => $item['is_posted_to_instagram'] === true ? __('RePost') : __('Post'), 'confirm' => [ 'title' => __('Post "${ $.$data.name }" to Instagram'), - 'message' => $item['is_posted_to_instagram'] === true ? __('Are you sure you wan\'t to re-post this product to Instagram?') : __('Are you sure you wan\'t to post this product to Instagram?') + 'message' => $item['is_posted_to_instagram'] === true + ? __('Are you sure you wan\'t to re-post this product to Instagram?') + : __('Are you sure you wan\'t to post this product to Instagram?') ], 'hidden' => false, ]; diff --git a/Ui/DataProvider/Product/DataProvider.php b/Ui/DataProvider/Product/DataProvider.php index 76e8871..f9cbfcb 100644 --- a/Ui/DataProvider/Product/DataProvider.php +++ b/Ui/DataProvider/Product/DataProvider.php @@ -5,6 +5,8 @@ use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; /** + * DataProvider for Grid + * * Class DataProvider */ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider From fc7754d2503a07e043c64710ba17fd6f6733e0b2 Mon Sep 17 00:00:00 2001 From: tuyennn Date: Mon, 22 Jun 2020 23:11:10 +0700 Subject: [PATCH 4/7] [Issue-35]: Something went wrong while uploading to Instagram - API changed --- Model/Instagram.php | 1581 ++++---------------------------------- composer.json | 5 +- etc/adminhtml/system.xml | 1 + 3 files changed, 142 insertions(+), 1445 deletions(-) diff --git a/Model/Instagram.php b/Model/Instagram.php index d1b20e6..46ba684 100644 --- a/Model/Instagram.php +++ b/Model/Instagram.php @@ -2,21 +2,26 @@ namespace GhoSter\AutoInstagramPost\Model; -use Magento\Framework\App\Filesystem\DirectoryList; +use Exception; use GhoSter\AutoInstagramPost\Model\Config as InstagramConfig; +use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\Exception\FileSystemException; use Psr\Log\LoggerInterface; /** - * Class Instagram - * - * Rewrite Code From : https://github.com/mgp25/Instagram-API + * Class Instagram API to communicate with Instagram + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPCS.Magento2.Files.LineLength.MaxExceeded) */ -//@codingStandardsIgnoreFile class Instagram { + const API_DOMAIN = 'https://i.instagram.com/'; const API_URL = 'https://i.instagram.com/api/v1/'; const USER_AGENT = 'Instagram 8.2.0 Android (18/4.3; 320dpi; 720x1280; Xiaomi; HM 1SW; armani; qcom; en_US)'; const IG_SIG_KEY = '55e91155636eaa89ba5ed619eb4645a4daf1103f2161dbfe6fd94d5ea7716095'; + /** + * phpcs:disable Generic.Files.LineLength.TooLong + */ const EXPERIMENTS = 'ig_android_progressive_jpeg,ig_creation_growth_holdout,ig_android_report_and_hide,ig_android_new_browser,ig_android_enable_share_to_whatsapp,ig_android_direct_drawing_in_quick_cam_universe,ig_android_huawei_app_badging,ig_android_universe_video_production,ig_android_asus_app_badging,ig_android_direct_plus_button,ig_android_ads_heatmap_overlay_universe,ig_android_http_stack_experiment_2016,ig_android_infinite_scrolling,ig_fbns_blocked,ig_android_white_out_universe,ig_android_full_people_card_in_user_list,ig_android_post_auto_retry_v7_21,ig_fbns_push,ig_android_feed_pill,ig_android_profile_link_iab,ig_explore_v3_us_holdout,ig_android_histogram_reporter,ig_android_anrwatchdog,ig_android_search_client_matching,ig_android_high_res_upload_2,ig_android_new_browser_pre_kitkat,ig_android_2fac,ig_android_grid_video_icon,ig_android_white_camera_universe,ig_android_disable_chroma_subsampling,ig_android_share_spinner,ig_android_explore_people_feed_icon,ig_explore_v3_android_universe,ig_android_media_favorites,ig_android_nux_holdout,ig_android_search_null_state,ig_android_react_native_notification_setting,ig_android_ads_indicator_change_universe,ig_android_video_loading_behavior,ig_android_black_camera_tab,liger_instagram_android_univ,ig_explore_v3_internal,ig_android_direct_emoji_picker,ig_android_prefetch_explore_delay_time,ig_android_business_insights_qe,ig_android_direct_media_size,ig_android_enable_client_share,ig_android_promoted_posts,ig_android_app_badging_holdout,ig_android_ads_cta_universe,ig_android_mini_inbox_2,ig_android_feed_reshare_button_nux,ig_android_boomerang_feed_attribution,ig_android_fbinvite_qe,ig_fbns_shared,ig_android_direct_full_width_media,ig_android_hscroll_profile_chaining,ig_android_feed_unit_footer,ig_android_media_tighten_space,ig_android_private_follow_request,ig_android_inline_gallery_backoff_hours_universe,ig_android_direct_thread_ui_rewrite,ig_android_rendering_controls,ig_android_ads_full_width_cta_universe,ig_video_max_duration_qe_preuniverse,ig_android_prefetch_explore_expire_time,ig_timestamp_public_test,ig_android_profile,ig_android_dv2_consistent_http_realtime_response,ig_android_enable_share_to_messenger,ig_explore_v3,ig_ranking_following,ig_android_pending_request_search_bar,ig_android_feed_ufi_redesign,ig_android_video_pause_logging_fix,ig_android_default_folder_to_camera,ig_android_video_stitching_7_23,ig_android_profanity_filter,ig_android_business_profile_qe,ig_android_search,ig_android_boomerang_entry,ig_android_inline_gallery_universe,ig_android_ads_overlay_design_universe,ig_android_options_app_invite,ig_android_view_count_decouple_likes_universe,ig_android_periodic_analytics_upload_v2,ig_android_feed_unit_hscroll_auto_advance,ig_peek_profile_photo_universe,ig_android_ads_holdout_universe,ig_android_prefetch_explore,ig_android_direct_bubble_icon,ig_video_use_sve_universe,ig_android_inline_gallery_no_backoff_on_launch_universe,ig_android_image_cache_multi_queue,ig_android_camera_nux,ig_android_immersive_viewer,ig_android_dense_feed_unit_cards,ig_android_sqlite_dev,ig_android_exoplayer,ig_android_add_to_last_post,ig_android_direct_public_threads,ig_android_prefetch_venue_in_composer,ig_android_bigger_share_button,ig_android_dv2_realtime_private_share,ig_android_non_square_first,ig_android_video_interleaved_v2,ig_android_follow_search_bar,ig_android_last_edits,ig_android_video_download_logging,ig_android_ads_loop_count_universe,ig_android_swipeable_filters_blacklist,ig_android_boomerang_layout_white_out_universe,ig_android_ads_carousel_multi_row_universe,ig_android_mentions_invite_v2,ig_android_direct_mention_qe,ig_android_following_follower_social_context'; const SIG_KEY_VERSION = '4'; const STATUS_OK = 'ok'; @@ -95,6 +100,7 @@ class Instagram * @param InstagramConfig $config * @param LoggerInterface $logger * @param array $data + * @throws FileSystemException */ public function __construct( DirectoryList $directoryList, @@ -115,36 +121,22 @@ public function __construct( $this->IGDataPath = $this->directoryList->getPath('var') . DIRECTORY_SEPARATOR; } + /** + * @return string + */ public function generateDeviceId() { - // This has 10 million possible hash subdivisions per clock second. + // phpcs:disable Magento2.Security.InsecureFunction $megaRandomHash = md5(number_format(microtime(true), 7, '', '')); return 'android-' . substr($megaRandomHash, 16); } - /** - * Checks whether supplied UUID is valid or not. - * - * @param string $uuid UUID to check. - * - * @return bool - */ - public static function isValidUUID( - $uuid - ) { - if (!is_string($uuid)) { - return false; - } - return (bool)preg_match('#^[a-f\d]{8}-(?:[a-f\d]{4}-){3}[a-f\d]{12}$#D', $uuid); - } /** * Set the user. Manage multiple accounts. * - * @param string $username - * Your Instagram username. - * @param string $password - * Your Instagram password. + * @param string $username Your Instagram username. + * @param string $password Your Instagram password. */ public function setUser($username, $password) { @@ -153,17 +145,28 @@ public function setUser($username, $password) $this->uuid = $this->generateUUID(true); + // phpcs:ignore Magento2.Functions.DiscouragedFunction if ((file_exists($this->IGDataPath . "$this->username-cookies.dat")) + // phpcs:ignore Magento2.Functions.DiscouragedFunction && (file_exists($this->IGDataPath . "$this->username-userId.dat")) + // phpcs:ignore Magento2.Functions.DiscouragedFunction && (file_exists($this->IGDataPath . "$this->username-token.dat")) ) { $this->isLoggedIn = true; + // phpcs:ignore Magento2.Functions.DiscouragedFunction $this->usernameId = trim(file_get_contents($this->IGDataPath . "$username-userId.dat")); $this->rankToken = $this->usernameId . '_' . $this->uuid; + // phpcs:ignore Magento2.Functions.DiscouragedFunction $this->token = trim(file_get_contents($this->IGDataPath . "$username-token.dat")); } } + /** + * phpcs:disable Magento2.Functions.DiscouragedFunction + * + * @param bool $keepDashes + * @return string|string[] + */ public function generateUUID($keepDashes = true) { $uuid = sprintf( @@ -177,6 +180,7 @@ public function generateUUID($keepDashes = true) mt_rand(0, 0xffff), mt_rand(0, 0xffff) ); + return $keepDashes ? $uuid : str_replace('-', '', $uuid); } @@ -190,8 +194,11 @@ public function isLoggedIn() $username = isset($account['username']) ? ($account['username']) : null; if (!empty($username)) { + // phpcs:ignore Magento2.Functions.DiscouragedFunction if ((file_exists($this->IGDataPath . "$username-cookies.dat")) + // phpcs:ignore Magento2.Functions.DiscouragedFunction && (file_exists($this->IGDataPath . "$username-userId.dat")) + // phpcs:ignore Magento2.Functions.DiscouragedFunction && (file_exists($this->IGDataPath . "$username-token.dat")) ) { $this->isLoggedIn = true; @@ -204,18 +211,20 @@ public function isLoggedIn() /** * Login to Instagram. * - * @param bool $force - * Force login to Instagram, this will create a new session + * @param bool $force Force login to Instagram, this will create a new session * * @return bool - * Login data - * @throws \Exception + * @throws Exception */ public function login($force = false) { if (!$this->isLoggedIn || $force) { - $fetch = $this->request('si/fetch_headers/?challenge_type=signup&guid=' . $this->generateUUID(false), null, - true); + $fetch = $this->request( + 'si/fetch_headers/?challenge_type=signup&guid=' . $this->generateUUID(false), + null, + true + ); + preg_match('#Set-Cookie: csrftoken=([^;]+)#', $fetch[0], $token); $data = [ @@ -230,16 +239,18 @@ public function login($force = false) $login = $this->request('accounts/login/', $this->generateSignature(json_encode($data)), true); - if ($login[1]['status'] === 'fail') { + if ($login[1]['status'] === self::STATUS_FAIL) { return false; } $this->isLoggedIn = true; $this->usernameId = $login[1]['logged_in_user']['pk']; + // phpcs:ignore Magento2.Functions.DiscouragedFunction file_put_contents($this->IGDataPath . $this->username . '-userId.dat', $this->usernameId); $this->rankToken = $this->usernameId . '_' . $this->uuid; preg_match('#Set-Cookie: csrftoken=([^;]+)#', $login[0], $match); $this->token = $match[1]; + // phpcs:ignore Magento2.Functions.DiscouragedFunction file_put_contents($this->IGDataPath . $this->username . '-token.dat', $this->token); $this->syncFeatures(); @@ -260,16 +271,20 @@ public function login($force = false) } /** + * + * phpcs:disable Magento2.Functions.DiscouragedFunction + * * @param $endpoint * @param null $post * @param bool $login * @return array - * @throws \Exception + * @throws Exception */ protected function request($endpoint, $post = null, $login = false) { if (!$this->isLoggedIn && !$login) { - throw new \Exception("Not logged in\n"); + //phpcs:ignore Magento2.Exceptions.DirectThrow + throw new Exception("Not logged in"); } $headers = [ @@ -308,7 +323,7 @@ protected function request($endpoint, $post = null, $login = false) if ($this->debug) { $this->logger->critical("REQUEST: $endpoint\n"); - if (!is_null($post)) { + if (!empty($post)) { if (!is_array($post)) { $this->logger->critical('DATA: ' . urldecode($post) . "\n"); @@ -333,7 +348,7 @@ public function generateSignature($data) /** * @return mixed - * @throws \Exception + * @throws Exception */ public function syncFeatures() { @@ -348,11 +363,19 @@ public function syncFeatures() return $this->request('qe/sync/', $this->generateSignature($data))[1]; } + /** + * @return mixed + * @throws Exception + */ protected function autoCompleteUserList() { return $this->request('friendships/autocomplete_user_list/')[1]; } + /** + * @return mixed + * @throws Exception + */ protected function timelineFeed() { return $this->request('feed/timeline/')[1]; @@ -362,322 +385,105 @@ protected function timelineFeed() * Login to Instagram. * * @return bool - * Returns true if logged out correctly + * @throws Exception */ public function logout() { $logout = $this->request('accounts/logout/'); - - if ($logout === 'ok') { - return true; - } else { - return false; - } - } - - /** - * Upload Video - * - * @param $video - * @param null $caption - * @return mixed|void - * @throws \Exception - */ - public function uploadVideo($video, $caption = null) - { - $videoData = file_get_contents($video); - - $endpoint = self::API_URL . 'upload/video/'; - $boundary = $this->uuid; - $upload_id = round(microtime(true) * 1000); - $bodies = [ - [ - 'type' => 'form-data', - 'name' => 'upload_id', - 'data' => $upload_id, - ], - [ - 'type' => 'form-data', - 'name' => '_csrftoken', - 'data' => $this->token, - ], - [ - 'type' => 'form-data', - 'name' => 'media_type', - 'data' => '2', - ], - [ - 'type' => 'form-data', - 'name' => '_uuid', - 'data' => $this->uuid, - ], - ]; - - $data = $this->buildBody($bodies, $boundary); - $headers = [ - 'Connection: keep-alive', - 'Accept: */*', - 'Host: i.instagram.com', - 'Content-type: multipart/form-data; boundary=' . $boundary, - 'Accept-Language: en-en', - ]; - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $endpoint); - curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_VERBOSE, false); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - curl_setopt($ch, CURLOPT_COOKIEFILE, $this->IGDataPath . "$this->username-cookies.dat"); - curl_setopt($ch, CURLOPT_COOKIEJAR, $this->IGDataPath . "$this->username-cookies.dat"); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); - - $resp = curl_exec($ch); - $header_len = curl_getinfo($ch, CURLINFO_HEADER_SIZE); - - $body = json_decode(substr($resp, $header_len), true); - - $uploadUrl = $body['video_upload_urls'][3]['url']; - $job = $body['video_upload_urls'][3]['job']; - - $request_size = floor(strlen($videoData) / 4); - - $lastRequestExtra = (strlen($videoData) - ($request_size * 4)); - - for ($a = 0; $a <= 3; $a++) { - $start = ($a * $request_size); - $end = ($a + 1) * $request_size + ($a == 3 ? $lastRequestExtra : 0); - - $headers = [ - 'Connection: keep-alive', - 'Accept: */*', - 'Host: upload.instagram.com', - 'Cookie2: $Version=1', - 'Accept-Encoding: gzip, deflate', - 'Content-Type: application/octet-stream', - 'Session-ID: ' . $upload_id, - 'Accept-Language: en-en', - 'Content-Disposition: attachment; filename="video.mov"', - 'Content-Length: ' . ($end - $start), - 'Content-Range: ' . 'bytes ' . $start . '-' . ($end - 1) . '/' . strlen($videoData), - 'job: ' . $job, - ]; - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $uploadUrl); - curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_VERBOSE, false); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - curl_setopt($ch, CURLOPT_COOKIEFILE, $this->IGDataPath . "$this->username-cookies.dat"); - curl_setopt($ch, CURLOPT_COOKIEJAR, $this->IGDataPath . "$this->username-cookies.dat"); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, substr($videoData, $start, $end)); - - $result = curl_exec($ch); - $header_len = curl_getinfo($ch, CURLINFO_HEADER_SIZE); - $body = substr($result, $header_len); - $array[] = [$body]; - } - $resp = curl_exec($ch); - $header_len = curl_getinfo($ch, CURLINFO_HEADER_SIZE); - $header = substr($resp, 0, $header_len); - $upload = json_decode(substr($resp, $header_len), true); - - curl_close($ch); - - if ($upload['status'] === 'fail') { - throw new \Exception($upload['message']); - } - - if ($this->debug) { - $this->logger->critical('RESPONSE: ' . substr($resp, $header_len) . "\n\n"); - - } - - $configure = $this->configureVideo($upload_id, $video, $caption); - $this->expose(); - - return $configure; - } - - /** - * @param $bodies - * @param $boundary - * @return string - */ - protected function buildBody($bodies, $boundary) - { - $body = ''; - foreach ($bodies as $b) { - $body .= '--' . $boundary . "\r\n"; - $body .= 'Content-Disposition: ' . $b['type'] . '; name="' . $b['name'] . '"'; - if (isset($b['filename'])) { - $ext = pathinfo($b['filename'], PATHINFO_EXTENSION); - $body .= '; filename="' . 'pending_media_' . number_format(round(microtime(true) * 1000), 0, '', - '') . '.' . $ext . '"'; - } - if (isset($b['headers']) && is_array($b['headers'])) { - foreach ($b['headers'] as $header) { - $body .= "\r\n" . $header; - } - } - - $body .= "\r\n\r\n" . $b['data'] . "\r\n"; - } - $body .= '--' . $boundary . '--'; - - return $body; + return $logout === self::STATUS_OK; } - /** - * @param $upload_id - * @param $video - * @param string $caption - * @return mixed - * @throws \Exception - */ - protected function configureVideo($upload_id, $video, $caption = '') - { - $this->uploadPhoto($video, $caption, $upload_id); - - $size = getimagesize($video)[0]; - - $post = json_encode([ - 'upload_id' => $upload_id, - 'source_type' => '3', - 'poster_frame_index' => 0, - 'length' => 0.00, - 'audio_muted' => false, - 'filter_type' => '0', - 'video_result' => 'deprecated', - 'clips' => [ - 'length' => $this->getSeconds($video), - 'source_type' => '3', - 'camera_position' => 'back', - ], - 'extra' => [ - 'source_width' => 960, - 'source_height' => 1280, - ], - 'device' => [ - 'manufacturer' => 'Xiaomi', - 'model' => 'HM 1SW', - 'android_version' => 18, - 'android_release' => '4.3', - ], - '_csrftoken' => $this->token, - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - 'caption' => $caption, - ]); - - $post = str_replace('"length":0', '"length":0.00', $post); - - return $this->request('media/configure/?video=1', $this->generateSignature($post))[1]; - } /** * Upload photo to Instagram. * * @param string $photo - * Path to your photo * @param string $caption - * Caption to be included in your photo. - * + * @param null $uploadId * @return array - * Upload data - * @throws \Exception + * @throws Exception */ - public function uploadPhoto($photo, $caption = null, $upload_id = null) + public function uploadPhoto($photo, $caption = null, $uploadId = null) { - $endpoint = self::API_URL . 'upload/photo/'; - $boundary = $this->uuid; + $endpoint = self::API_DOMAIN . 'rupload_igphoto/'; - if (!is_null($upload_id)) { + if (!empty($uploadId)) { $fileToUpload = $this->createVideoIcon($photo); } else { - $upload_id = number_format(round(microtime(true) * 1000), 0, '', ''); - $fileToUpload = file_get_contents($photo); - } - - $bodies = [ - [ - 'type' => 'form-data', - 'name' => 'upload_id', - 'data' => $upload_id, - ], - [ - 'type' => 'form-data', - 'name' => '_uuid', - 'data' => $this->uuid, - ], - [ - 'type' => 'form-data', - 'name' => '_csrftoken', - 'data' => $this->token, - ], - [ - 'type' => 'form-data', - 'name' => 'image_compression', - 'data' => '{"lib_name":"jt","lib_version":"1.3.0","quality":"70"}', - ], - [ - 'type' => 'form-data', - 'name' => 'photo', - 'data' => $fileToUpload, - 'filename' => 'pending_media_' . number_format(round(microtime(true) * 1000), 0, '', '') . '.jpg', - 'headers' => [ - 'Content-Transfer-Encoding: binary', - 'Content-type: application/octet-stream', - ], - ], + $uploadId = number_format(round(microtime(true) * 1000), 0, '', ''); + // phpcs:ignore Magento2.Functions.DiscouragedFunction + $fileToUpload = file_get_contents($photo, FILE_BINARY); + } + + $uploadName = "fb_uploader_" . $uploadId; + $endpoint = $endpoint . $uploadName; + + $uploadParams = [ + 'retry_context' => '{"num_step_auto_retry":0,"num_reupload":0,"num_step_manual_retry":0}', + 'media_type' => '1', + 'xsharing_user_ids' => '[]', + 'upload_id' => $uploadId, + 'image_compression' => json_encode( + ['lib_name' => 'moz', 'lib_version' => '3.1.m', 'quality' => '80'] + ) ]; - $data = $this->buildBody($bodies, $boundary); + $headers = [ - 'Connection: close', - 'Accept: */*', - 'Content-type: multipart/form-data; boundary=' . $boundary, - 'Content-Length: ' . strlen($data), - 'Cookie2: $Version=1', - 'Accept-Language: en-US', 'Accept-Encoding: gzip', + 'X-Instagram-Rupload-Params: ' . json_encode($uploadParams), + 'X_FB_PHOTO_WATERFALL_ID: ' . $this->generateUUID(true), + 'X-Entity-Type: image/jpeg', + 'Offset: 0', + 'X-Entity-Name: ' . $uploadName, + 'X-Entity-Length: ' . strlen($fileToUpload), + 'Content-Type: application/octet-stream', ]; + // phpcs:ignore Magento2.Functions.DiscouragedFunction $ch = curl_init(); + // phpcs:ignore Magento2.Functions.DiscouragedFunction curl_setopt($ch, CURLOPT_URL, $endpoint); + // phpcs:ignore Magento2.Functions.DiscouragedFunction curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT); + // phpcs:ignore Magento2.Functions.DiscouragedFunction curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + // phpcs:ignore Magento2.Functions.DiscouragedFunction curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + // phpcs:ignore Magento2.Functions.DiscouragedFunction curl_setopt($ch, CURLOPT_HEADER, true); + // phpcs:ignore Magento2.Functions.DiscouragedFunction curl_setopt($ch, CURLOPT_VERBOSE, $this->debug); + // phpcs:ignore Magento2.Functions.DiscouragedFunction curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + // phpcs:ignore Magento2.Functions.DiscouragedFunction curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + // phpcs:ignore Magento2.Functions.DiscouragedFunction curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + // phpcs:ignore Magento2.Functions.DiscouragedFunction curl_setopt($ch, CURLOPT_COOKIEFILE, $this->IGDataPath . "$this->username-cookies.dat"); + // phpcs:ignore Magento2.Functions.DiscouragedFunction curl_setopt($ch, CURLOPT_COOKIEJAR, $this->IGDataPath . "$this->username-cookies.dat"); + // phpcs:ignore Magento2.Functions.DiscouragedFunction curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + // phpcs:ignore Magento2.Functions.DiscouragedFunction + curl_setopt($ch, CURLOPT_POSTFIELDS, $fileToUpload); + // phpcs:ignore Magento2.Functions.DiscouragedFunction $resp = curl_exec($ch); - $header_len = curl_getinfo($ch, CURLINFO_HEADER_SIZE); - $header = substr($resp, 0, $header_len); - $upload = json_decode(substr($resp, $header_len), true); + // phpcs:ignore Magento2.Functions.DiscouragedFunction + $headerLength = curl_getinfo($ch, CURLINFO_HEADER_SIZE); + $upload = json_decode(substr($resp, $headerLength), true); + // phpcs:ignore Magento2.Functions.DiscouragedFunction curl_close($ch); - if ($upload['status'] === 'fail') { + if ($upload['status'] === self::STATUS_FAIL) { return []; } if ($this->debug) { - $this->logger->critical('RESPONSE: ' . substr($resp, $header_len) . "\n\n"); + $this->logger->critical('RESPONSE: ' . substr($resp, $headerLength) . "\n\n"); } $configure = $this->configure($upload['upload_id'], $photo, $caption); @@ -688,22 +494,21 @@ public function uploadPhoto($photo, $caption = null, $upload_id = null) /** * Creating a video icon/thumbnail - * @param string $file - * path to the video file - * @return image - * icon/thumbnail for the video + * phpcs:disable Generic.PHP.NoSilencedErrors + * phpcs:disable Magento2.Security.InsecureFunction + * + * @param string $file path to the video file + * @return false|string */ - - function createVideoIcon($file) + public function createVideoIcon($file) { - /* should install ffmpeg for the method to work successfully */ $ffmpeg = $this->checkFFMPEG(); if ($ffmpeg) { - //generate thumbnail + //phpcs:ignore Magento2.Security.InsecureFunction $preview = sys_get_temp_dir() . '/' . md5($file) . '.jpg'; + // phpcs:disable Magento2.Functions.DiscouragedFunction.Discouraged @unlink($preview); - //capture video preview $command = $ffmpeg . ' -i "' . $file . '" -f mjpeg -ss 00:00:01 -vframes 1 "' . $preview . '" 2>&1'; @exec($command); @@ -713,11 +518,13 @@ function createVideoIcon($file) /** * Check for ffmpeg/avconv dependencies - * @return string/boolean - * name of the library if present, false otherwise + * + * phpcs:disable Generic.PHP.NoSilencedErrors + * phpcs:disable Magento2.Security.InsecureFunction + * + * @return string|boolean name of the library if present, false otherwise */ - - function checkFFMPEG() + public function checkFFMPEG() { @exec('ffmpeg -version 2>&1', $output, $returnvalue); if ($returnvalue === 0) { @@ -734,13 +541,12 @@ function checkFFMPEG() /** * Implements the actual logic behind creating the icon/thumbnail * - * @param string $file - * path to the file name + * @param string $file path to the file name * - * @return image - * icon/thumbnail for the video + * @param int $size + * @return string|false icon/thumbnail for the video */ - function createIconGD($file, $size = 100, $raw = true) + public function createIconGD($file, $size = 100) { list($width, $height) = getimagesize($file); if ($width > $height) { @@ -768,6 +574,13 @@ function createIconGD($file, $size = 100, $raw = true) return $i; } + /** + * @param $upload_id + * @param $photo + * @param string $caption + * @return mixed + * @throws Exception + */ protected function configure($upload_id, $photo, $caption = '') { $size = getimagesize($photo)[0]; @@ -805,7 +618,7 @@ protected function configure($upload_id, $photo, $caption = '') } /** - * @throws \Exception + * @throws Exception */ protected function expose() { @@ -819,1122 +632,4 @@ protected function expose() $this->request('qe/expose/', $this->generateSignature($data))[1]; } - - /** - * Length of the file in Seconds - * - * @param string $file - * path to the file name - * - * @return integer - * length of the file in seconds - */ - - function getSeconds($file) - { - $ffmpeg = $this->checkFFMPEG(); - if ($ffmpeg) { - $time = exec("$ffmpeg -i " . $file . " 2>&1 | grep 'Duration' | cut -d ' ' -f 4"); - $duration = explode(':', $time); - $seconds = $duration[0] * 3600 + $duration[1] * 60 + round($duration[2]); - - return $seconds; - } - - return mt_rand(15, 300); - } - - /** - * @param $media_id - * @param $recipients - * @param null $text - */ - public function direct_share($media_id, $recipients, $text = null) - { - if (!is_array($recipients)) { - $recipients = [$recipients]; - } - - $string = []; - foreach ($recipients as $recipient) { - $string[] = "\"$recipient\""; - } - - $recipeint_users = implode(',', $string); - - $endpoint = self::API_URL . 'direct_v2/threads/broadcast/media_share/?media_type=photo'; - $boundary = $this->uuid; - $bodies = [ - [ - 'type' => 'form-data', - 'name' => 'media_id', - 'data' => $media_id, - ], - [ - 'type' => 'form-data', - 'name' => 'recipient_users', - 'data' => "[[$recipeint_users]]", - ], - [ - 'type' => 'form-data', - 'name' => 'client_context', - 'data' => $this->uuid, - ], - [ - 'type' => 'form-data', - 'name' => 'thread_ids', - 'data' => '["0"]', - ], - [ - 'type' => 'form-data', - 'name' => 'text', - 'data' => is_null($text) ? '' : $text, - ], - ]; - - $data = $this->buildBody($bodies, $boundary); - $headers = [ - 'Proxy-Connection: keep-alive', - 'Connection: keep-alive', - 'Accept: */*', - 'Content-type: multipart/form-data; boundary=' . $boundary, - 'Accept-Language: en-en', - ]; - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $endpoint); - curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_VERBOSE, $this->debug); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - curl_setopt($ch, CURLOPT_COOKIEFILE, $this->IGDataPath . "$this->username-cookies.dat"); - curl_setopt($ch, CURLOPT_COOKIEJAR, $this->IGDataPath . "$this->username-cookies.dat"); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); - - $resp = curl_exec($ch); - $header_len = curl_getinfo($ch, CURLINFO_HEADER_SIZE); - $header = substr($resp, 0, $header_len); - $upload = json_decode(substr($resp, $header_len), true); - - curl_close($ch); - } - - /** - * Edit media. - * - * @param string $mediaId - * Media id - * @param string $captionText - * Caption text - * - * @return array - * edit media data - */ - public function editMedia($mediaId, $captionText = '') - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - '_csrftoken' => $this->token, - 'caption_text' => $captionText, - ]); - - return $this->request("media/$mediaId/edit_media/", $this->generateSignature($data))[1]; - } - - /** - * Remove yourself from a tagged media. - * - * @param string $mediaId - * Media id - * - * @return array - * edit media data - */ - public function removeSelftag($mediaId) - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - '_csrftoken' => $this->token, - ]); - - return $this->request("usertags/$mediaId/remove/", $this->generateSignature($data))[1]; - } - - /** - * Media info - * - * @param string $mediaId - * Media id - * - * @return array - * delete request data - */ - public function mediaInfo($mediaId) - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - '_csrftoken' => $this->token, - 'media_id' => $mediaId, - ]); - - return $this->request("media/$mediaId/info/", $this->generateSignature($data))[1]; - } - - /** - * Delete photo or video. - * - * @param string $mediaId - * Media id - * - * @return array - * delete request data - */ - public function deleteMedia($mediaId) - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - '_csrftoken' => $this->token, - 'media_id' => $mediaId, - ]); - - return $this->request("media/$mediaId/delete/", $this->generateSignature($data))[1]; - } - - /** - * Comment media. - * - * @param string $mediaId - * Media id - * @param string $commentText - * Comment Text - * - * @return array - * comment media data - */ - public function comment($mediaId, $commentText) - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - '_csrftoken' => $this->token, - 'comment_text' => $commentText, - ]); - - return $this->request("media/$mediaId/comment/", $this->generateSignature($data))[1]; - } - - /** - * Delete Comment. - * - * @param string $mediaId - * Media ID - * @param string $commentId - * Comment ID - * - * @return array - * Delete comment data - */ - public function deleteComment($mediaId, $captionText, $commentId) - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - '_csrftoken' => $this->token, - 'caption_text' => $captionText, - ]); - - return $this->request("media/$mediaId/comment/$commentId/delete/", $this->generateSignature($data))[1]; - } - - /** - * Sets account to public. - * - * @param string $photo - * @return bool - */ - - public function changeProfilePicture($photo) - { - if (is_null($photo)) { - $this->logger->critical("Photo not valid\n\n"); - return false; - } - - $uData = json_encode([ - '_csrftoken' => $this->token, - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - ]); - - $endpoint = self::API_URL . 'accounts/change_profile_picture/'; - $boundary = $this->uuid; - $bodies = [ - [ - 'type' => 'form-data', - 'name' => 'ig_sig_key_version', - 'data' => self::SIG_KEY_VERSION, - ], - [ - 'type' => 'form-data', - 'name' => 'signed_body', - 'data' => hash_hmac('sha256', $uData, self::IG_SIG_KEY) . $uData, - ], - [ - 'type' => 'form-data', - 'name' => 'profile_pic', - 'data' => file_get_contents($photo), - 'filename' => 'profile_pic', - 'headers' => [ - 'Content-type: application/octet-stream', - 'Content-Transfer-Encoding: binary', - ], - ], - ]; - - $data = $this->buildBody($bodies, $boundary); - $headers = [ - 'Proxy-Connection: keep-alive', - 'Connection: keep-alive', - 'Accept: */*', - 'Content-type: multipart/form-data; boundary=' . $boundary, - 'Accept-Language: en-en', - 'Accept-Encoding: gzip, deflate', - ]; - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $endpoint); - curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_VERBOSE, $this->debug); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - curl_setopt($ch, CURLOPT_COOKIEFILE, $this->IGDataPath . "$this->username-cookies.dat"); - curl_setopt($ch, CURLOPT_COOKIEJAR, $this->IGDataPath . "$this->username-cookies.dat"); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); - - $resp = curl_exec($ch); - $header_len = curl_getinfo($ch, CURLINFO_HEADER_SIZE); - $header = substr($resp, 0, $header_len); - $upload = json_decode(substr($resp, $header_len), true); - - curl_close($ch); - } - - /** - * Remove profile picture. - * - * @return array - * status request data - */ - public function removeProfilePicture() - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - '_csrftoken' => $this->token, - ]); - - return $this->request('accounts/remove_profile_picture/', $this->generateSignature($data))[1]; - } - - /** - * Sets account to private. - * - * @return array - * status request data - */ - public function setPrivateAccount() - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - '_csrftoken' => $this->token, - ]); - - return $this->request('accounts/set_private/', $this->generateSignature($data))[1]; - } - - /** - * Sets account to public. - * - * @return array - * status request data - */ - public function setPublicAccount() - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - '_csrftoken' => $this->token, - ]); - - return $this->request('accounts/set_public/', $this->generateSignature($data))[1]; - } - - /** - * Get personal profile data. - * - * @return array - * profile data - */ - public function getProfileData() - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - '_csrftoken' => $this->token, - ]); - - return $this->request('accounts/current_user/?edit=true', $this->generateSignature($data))[1]; - } - - /** - * Edit profile. - * - * @param string $url - * Url - website. "" for nothing - * @param string $phone - * Phone number. "" for nothing - * @param string $first_name - * Name. "" for nothing - * @param string $email - * Email. Required. - * @param int $gender - * Gender. male = 1 , female = 0 - * - * @return array - * edit profile data - */ - public function editProfile($url, $phone, $first_name, $biography, $email, $gender) - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - '_csrftoken' => $this->token, - 'external_url' => $url, - 'phone_number' => $phone, - 'username' => $this->username, - 'full_name' => $first_name, - 'biography' => $biography, - 'email' => $email, - 'gender' => $gender, - ]); - - return $this->request('accounts/edit_profile/', $this->generateSignature($data))[1]; - } - - /** - * Get self username info. - * - * @return array - * Username data - */ - public function getSelfUsernameInfo() - { - return $this->getUsernameInfo($this->usernameId); - } - - /** - * Get username info. - * - * @param string $usernameId - * Username id - * - * @return array - * Username data - */ - public function getUsernameInfo($usernameId) - { - return $this->request("users/$usernameId/info/")[1]; - } - - /** - * Get recent activity. - * - * @return array - * Recent activity data - * @throws \Exception - */ - public function getRecentActivity() - { - $activity = $this->request('news/inbox/?')[1]; - - if ($activity['status'] !== 'ok') { - throw new \Exception($activity['message'] . "\n"); - } - - return $activity; - } - - /** - * Get recent activity from accounts followed. - * - * @return array - * Recent activity data of follows - * @throws \Exception - */ - public function getFollowingRecentActivity() - { - $activity = $this->request('news/?')[1]; - - if ($activity['status'] !== 'ok') { - throw new \Exception($activity['message'] . "\n"); - } - - return $activity; - } - - /** - * I dont know this yet. - * - * @return array - * v2 inbox data - * @throws \Exception - */ - public function getv2Inbox() - { - $inbox = $this->request('direct_v2/inbox/?')[1]; - - if ($inbox['status'] !== 'ok') { - throw new \Exception($inbox['message'] . "\n"); - } - - return $inbox; - } - - /** - * Get self user tags. - * - * @return array - * self user tags data - */ - public function getSelfUserTags() - { - return $this->getUserTags($this->usernameId); - } - - /** - * Get user tags. - * - * @param string $usernameId - * - * @return array - * user tags data - * @throws \Exception - */ - public function getUserTags($usernameId) - { - $tags = $this->request("usertags/$usernameId/feed/?rank_token=$this->rankToken&ranked_content=true&")[1]; - - if ($tags['status'] !== 'ok') { - throw new \Exception($tags['message'] . "\n"); - } - - return $tags; - } - - /** - * Get tagged media. - * - * @param string $tag - * - * @return array - * @throws \Exception - */ - - public function tagFeed($tag) - { - $userFeed = $this->request("feed/tag/$tag/?rank_token=$this->rankToken&ranked_content=true&")[1]; - - if ($userFeed['status'] !== 'ok') { - throw new \Exception($userFeed['message'] . "\n"); - } - - return $userFeed; - } - - /** - * Get media likers. - * - * @param string $mediaId - * - * @return array - * @throws \Exception - */ - public function getMediaLikers($mediaId) - { - $likers = $this->request("media/$mediaId/likers/?")[1]; - if ($likers['status'] !== 'ok') { - throw new \Exception($likers['message'] . "\n"); - } - - return $likers; - } - - /** - * Get self user locations media. - * - * @return array - * Geo Media data - */ - public function getSelfGeoMedia() - { - return $this->getGeoMedia($this->usernameId); - } - - /** - * Get user locations media. - * - * @param string $usernameId - * Username id - * - * @return array - * Geo Media data - * @throws \Exception - */ - public function getGeoMedia($usernameId) - { - $locations = $this->request("maps/user/$usernameId/")[1]; - - if ($locations['status'] !== 'ok') { - throw new \Exception($locations['message'] . "\n"); - } - - return $locations; - } - - /** - * facebook user search. - * - * @param string $query - * - * @return array - * query data - * @throws \Exception - */ - public function fbUserSearch($query) - { - $query = rawurlencode($query); - $query = $this->request("fbsearch/topsearch/?context=blended&query=$query&rank_token=$this->rankToken")[1]; - - if ($query['status'] !== 'ok') { - throw new \Exception($query['message'] . "\n"); - } - - return $query; - } - - /** - * Search users. - * - * @param string $query - * - * @return array - * query data - * @throws \Exception - */ - public function searchUsers($query) - { - $query = $this->request('users/search/?ig_sig_key_version=' . self::SIG_KEY_VERSION . "&is_typeahead=true&query=$query&rank_token=$this->rankToken")[1]; - - if ($query['status'] !== 'ok') { - throw new \Exception($query['message'] . "\n"); - } - - return $query; - } - - /** - * Search exact username - * - * @param string usernameName username as STRING not an id - * - * @return array - * query data - * @throws \Exception - * - */ - public function searchUsername($usernameName) - { - $query = $this->request("users/$usernameName/usernameinfo/")[1]; - - if ($query['status'] !== 'ok') { - throw new \Exception($query['message'] . "\n"); - } - - return $query; - } - - /** - * Search users using addres book. - * - * @param array $contacts - * - * @return array - * query data - */ - public function syncFromAdressBook($contacts) - { - $data = 'contacts=' . json_encode($contacts, true); - - return $this->request('address_book/link/?include=extra_display_name,thumbnails', $data)[1]; - } - - /** - * Search tags. - * - * @param string $query - * - * @return array - * query data - * @throws \Exception - */ - public function searchTags($query) - { - $query = $this->request("tags/search/?is_typeahead=true&q=$query&rank_token=$this->rankToken")[1]; - - if ($query['status'] !== 'ok') { - throw new \Exception($query['message'] . "\n"); - } - - return $query; - } - - /** - * Get timeline data. - * - * @return array - * timeline data - * @throws \Exception - */ - public function getTimeline($maxid = null) - { - $timeline = $this->request( - "feed/timeline/?rank_token=$this->rankToken&ranked_content=true" - . (!is_null($maxid) ? "&max_id=" . $maxid : '') - )[1]; - - if ($timeline['status'] !== 'ok') { - throw new \Exception($timeline['message'] . "\n"); - } - - return $timeline; - } - - /** - * Get hashtag feed. - * - * @param string $hashtagString - * Hashtag string, not including the # - * - * @return array - * Hashtag feed data - * @throws \Exception - */ - public function getHashtagFeed($hashtagString, $maxid = null) - { - if (is_null($maxid)) { - $endpoint = "feed/tag/$hashtagString/?rank_token=$this->rankToken&ranked_content=true&"; - } else { - $endpoint = "feed/tag/$hashtagString/?max_id=" . $maxid . "&rank_token=$this->rankToken&ranked_content=true&"; - } - - $hashtagFeed = $this->request($endpoint)[1]; - - if ($hashtagFeed['status'] !== 'ok') { - throw new \Exception($hashtagFeed['message'] . "\n"); - } - - return $hashtagFeed; - } - - /** - * Get locations. - * - * @param string $query - * search query - * - * @return array - * Location location data - * @throws \Exception - */ - public function searchLocation($query) - { - $query = rawurlencode($query); - $endpoint = "fbsearch/places/?rank_token=$this->rankToken&query=" . $query; - - $locationFeed = $this->request($endpoint)[1]; - - if ($locationFeed['status'] !== 'ok') { - throw new \Exception($locationFeed['message'] . "\n"); - } - - return $locationFeed; - } - - /** - * Get location feed. - * - * @param string $locationId - * location id - * - * @return array - * Location feed data - * @throws \Exception - */ - public function getLocationFeed($locationId, $maxid = null) - { - if (is_null($maxid)) { - $endpoint = "feed/location/$locationId/?rank_token=$this->rankToken&ranked_content=true&"; - } else { - $endpoint = "feed/location/$locationId/?max_id=" . $maxid . "&rank_token=$this->rankToken&ranked_content=true&"; - } - - $locationFeed = $this->request($endpoint)[1]; - - if ($locationFeed['status'] !== 'ok') { - throw new \Exception($locationFeed['message'] . "\n"); - } - - return $locationFeed; - } - - /** - * Get popular feed. - * - * @return array - * popular feed data - * @throws \Exception - */ - public function getPopularFeed() - { - $popularFeed = $this->request("feed/popular/?people_teaser_supported=1&rank_token=$this->rankToken&ranked_content=true&")[1]; - - if ($popularFeed['status'] !== 'ok') { - throw new \Exception($popularFeed['message'] . "\n"); - } - - return $popularFeed; - } - - /** - * Get user followings. - * - * @param string $usernameId - * Username id - * - * @return array - * followers data - */ - public function getUserFollowings($usernameId, $maxid = null) - { - return $this->request("friendships/$usernameId/following/?max_id=$maxid&ig_sig_key_version=" . self::SIG_KEY_VERSION . "&rank_token=$this->rankToken")[1]; - } - - /** - * Get self user followers. - * - * @return array - * followers data - */ - public function getSelfUserFollowers() - { - return $this->getUserFollowers($this->usernameId); - } - - /** - * Get user followers. - * - * @param string $usernameId - * Username id - * - * @return array - * followers data - */ - public function getUserFollowers($usernameId, $maxid = null) - { - return $this->request("friendships/$usernameId/followers/?max_id=$maxid&ig_sig_key_version=" . self::SIG_KEY_VERSION . "&rank_token=$this->rankToken")[1]; - } - - /** - * Get self users we are following. - * - * @return array - * users we are following data - */ - public function getSelfUsersFollowing() - { - return $this->request('friendships/following/?ig_sig_key_version=' . self::SIG_KEY_VERSION . "&rank_token=$this->rankToken")[1]; - } - - /** - * Like photo or video. - * - * @param string $mediaId - * Media id - * - * @return array - * status request - */ - public function like($mediaId) - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - '_csrftoken' => $this->token, - 'media_id' => $mediaId, - ]); - - return $this->request("media/$mediaId/like/", $this->generateSignature($data))[1]; - } - - /** - * Unlike photo or video. - * - * @param string $mediaId - * Media id - * - * @return array - * status request - */ - public function unlike($mediaId) - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - '_csrftoken' => $this->token, - 'media_id' => $mediaId, - ]); - - return $this->request("media/$mediaId/unlike/", $this->generateSignature($data))[1]; - } - - /** - * Get media comments. - * - * @param string $mediaId - * Media id - * - * @return array - * Media comments data - */ - public function getMediaComments($mediaId) - { - return $this->request("media/$mediaId/comments/?")[1]; - } - - /** - * Set name and phone (Optional). - * - * @param string $name - * @param string $phone - * - * @return array - * Set status data - */ - public function setNameAndPhone($name = '', $phone = '') - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - 'first_name' => $name, - 'phone_number' => $phone, - '_csrftoken' => $this->token, - ]); - - return $this->request('accounts/set_phone_and_name/', $this->generateSignature($data))[1]; - } - - /** - * Get direct share. - * - * @return array - * Direct share data - */ - public function getDirectShare() - { - return $this->request('direct_share/inbox/?')[1]; - } - - /** - * Backups all your uploaded photos :). - */ - public function backup() - { - $myUploads = $this->getSelfUserFeed(); - foreach ($myUploads['items'] as $item) { - if (!is_dir($this->IGDataPath . 'backup/' . "$this->username-" . date('Y-m-d'))) { - mkdir($this->IGDataPath . 'backup/' . "$this->username-" . date('Y-m-d')); - } - file_put_contents($this->IGDataPath . 'backup/' . "$this->username-" . date('Y-m-d') . '/' . $item['id'] . '.jpg', - file_get_contents($item['image_versions2']['candidates'][0]['url'])); - } - } - - /** - * Get self user feed. - * - * @return array - * User feed data - */ - public function getSelfUserFeed() - { - return $this->getUserFeed($this->usernameId); - } - - /** - * Get user feed. - * @param string $usernameId - * Username id - * @param null $maxid - * Max Id - * @param null $minTimestamp - * Min timestamp - * @return array User feed data - * User feed data - * @throws \Exception - */ - public function getUserFeed($usernameId, $maxid = null, $minTimestamp = null) - { - $userFeed = $this->request( - "feed/user/$usernameId/?rank_token=$this->rankToken" - . (!is_null($maxid) ? "&max_id=" . $maxid : '') - . (!is_null($minTimestamp) ? "&min_timestamp=" . $minTimestamp : '') - . "&ranked_content=true" - )[1]; - - if ($userFeed['status'] !== 'ok') { - throw new \Exception($userFeed['message'] . "\n"); - } - - return $userFeed; - } - - /** - * Follow. - * - * @param string $userId - * - * @return array - * Friendship status data - */ - public function follow($userId) - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - 'user_id' => $userId, - '_csrftoken' => $this->token, - ]); - - return $this->request("friendships/create/$userId/", $this->generateSignature($data))[1]; - } - - /** - * Unfollow. - * - * @param string $userId - * - * @return array - * Friendship status data - */ - public function unfollow($userId) - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - 'user_id' => $userId, - '_csrftoken' => $this->token, - ]); - - return $this->request("friendships/destroy/$userId/", $this->generateSignature($data))[1]; - } - - /** - * Block. - * - * @param string $userId - * - * @return array - * Friendship status data - */ - public function block($userId) - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - 'user_id' => $userId, - '_csrftoken' => $this->token, - ]); - - return $this->request("friendships/block/$userId/", $this->generateSignature($data))[1]; - } - - /** - * Unblock. - * - * @param string $userId - * - * @return array - * Friendship status data - */ - public function unblock($userId) - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - 'user_id' => $userId, - '_csrftoken' => $this->token, - ]); - - return $this->request("friendships/unblock/$userId/", $this->generateSignature($data))[1]; - } - - /** - * Show User Friendship. - * - * @param string $userId - * - * @return array - * Friendship relationship data - */ - public function userFriendship($userId) - { - $data = json_encode([ - '_uuid' => $this->uuid, - '_uid' => $this->usernameId, - 'user_id' => $userId, - '_csrftoken' => $this->token, - ]); - return $this->request("friendships/show/$userId/", $this->generateSignature($data))[1]; - } - - /** - * Get liked media. - * - * @return array - * Liked media data - */ - public function getLikedMedia() - { - return $this->request('feed/liked/?')[1]; - } - - protected function megaphoneLog() - { - return $this->request('megaphone/log/')[1]; - } } diff --git a/composer.json b/composer.json index 1a2d0c9..3692ea5 100644 --- a/composer.json +++ b/composer.json @@ -8,9 +8,10 @@ "magento 2 auto instagram post" ], "require": { - "php": "7.0.2|7.0.4|~7.0.6|~7.1.0|~7.1.3|~7.2.0", + "php": "~7.0.*|~7.1.0|~7.1.3|~7.2.0|~7.3.0", "magento/module-backend": "100.0.*|100.1.*|100.2.*|100.3.*|101.0.*", - "magento/framework": "100.0.*|100.1.*|100.2.*|100.3.*|101.0.*" + "magento/framework": "100.0.*|100.1.*|100.2.*|100.3.*|101.0.*", + "ext-json": "*" }, "require-dev": { "phpmd/phpmd": "@stable", diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index cfbf0e6..5e21852 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -24,6 +24,7 @@ + required-entry Magento\Config\Model\Config\Backend\Encrypted 1 From c62c6768bdf5fc7ea33c1e05b63b98067339f083 Mon Sep 17 00:00:00 2001 From: tuyennn Date: Mon, 22 Jun 2020 23:21:21 +0700 Subject: [PATCH 5/7] [Issue-35]: Composer json update --- Model/Instagram.php | 2 ++ composer.json | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Model/Instagram.php b/Model/Instagram.php index 46ba684..8715987 100644 --- a/Model/Instagram.php +++ b/Model/Instagram.php @@ -10,7 +10,9 @@ /** * Class Instagram API to communicate with Instagram + * @SuppressWarnings(PHPMD.TooManyFields) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) * @SuppressWarnings(PHPCS.Magento2.Files.LineLength.MaxExceeded) */ class Instagram diff --git a/composer.json b/composer.json index 3692ea5..d18ca2e 100644 --- a/composer.json +++ b/composer.json @@ -8,9 +8,9 @@ "magento 2 auto instagram post" ], "require": { - "php": "~7.0.*|~7.1.0|~7.1.3|~7.2.0|~7.3.0", + "php": "~7.0.0 || ~7.1.0 || ~7.2.0 || ~7.3.0", "magento/module-backend": "100.0.*|100.1.*|100.2.*|100.3.*|101.0.*", - "magento/framework": "100.0.*|100.1.*|100.2.*|100.3.*|101.0.*", + "magento/framework": "*", "ext-json": "*" }, "require-dev": { From 1598ed443d5c2383a6fff2784b7663188c809c74 Mon Sep 17 00:00:00 2001 From: tuyennn Date: Mon, 22 Jun 2020 23:25:11 +0700 Subject: [PATCH 6/7] [Issue-35]: PSR-2 --- Model/Instagram.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Model/Instagram.php b/Model/Instagram.php index 8715987..ad7ce25 100644 --- a/Model/Instagram.php +++ b/Model/Instagram.php @@ -94,7 +94,6 @@ class Instagram */ protected $logger; - /** * Default class constructor. * @@ -133,7 +132,6 @@ public function generateDeviceId() return 'android-' . substr($megaRandomHash, 16); } - /** * Set the user. Manage multiple accounts. * @@ -191,7 +189,6 @@ public function generateUUID($keepDashes = true) */ public function isLoggedIn() { - $account = $this->config->getAccountInformation(); $username = isset($account['username']) ? ($account['username']) : null; @@ -328,7 +325,6 @@ protected function request($endpoint, $post = null, $login = false) if (!empty($post)) { if (!is_array($post)) { $this->logger->critical('DATA: ' . urldecode($post) . "\n"); - } } $this->logger->critical("RESPONSE: $body\n\n"); @@ -395,7 +391,6 @@ public function logout() return $logout === self::STATUS_OK; } - /** * Upload photo to Instagram. * @@ -430,7 +425,6 @@ public function uploadPhoto($photo, $caption = null, $uploadId = null) ) ]; - $headers = [ 'Accept-Encoding: gzip', 'X-Instagram-Rupload-Params: ' . json_encode($uploadParams), @@ -516,6 +510,8 @@ public function createVideoIcon($file) return $this->createIconGD($preview); } + + return false; } /** From 42ab99abb99f56b5b09f40a1b9175d9500e25c50 Mon Sep 17 00:00:00 2001 From: tuyennn Date: Mon, 22 Jun 2020 23:30:45 +0700 Subject: [PATCH 7/7] Bump to v1.1.9 --- README.md | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eb79f18..d726047 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ This Magento 2 extension Auto Instagram Post allows you add your products immedi [![Codacy Badge](https://api.codacy.com/project/badge/Grade/bf0757d0063e489eb3bff2479964fce2)](https://www.codacy.com/app/GhoSterInc/AutoInstagramPost?utm_source=github.com&utm_medium=referral&utm_content=tuyennn/AutoInstagramPost&utm_campaign=Badge_Grade) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/thinghost) [![Build Status](https://travis-ci.org/tuyennn/AutoInstagramPost.svg?branch=master)](https://travis-ci.org/tuyennn/AutoInstagramPost) -![Version 1.1.8](https://img.shields.io/badge/Version-1.1.8-green.svg) +![Version 1.1.9](https://img.shields.io/badge/Version-1.1.9-green.svg) --- ## [![Alt GhoSter](http://thinghost.info/wp-content/uploads/2015/12/ghoster.png "thinghost.info")](http://thinghost.info) Overview diff --git a/composer.json b/composer.json index d18ca2e..f98bf8c 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ } ], "type": "magento2-module", - "version": "1.1.8", + "version": "1.1.9", "license": [ "OSL-3.0", "AFL-3.0"