NitroPack.php
172 lines
| 1 | <?php |
| 2 | namespace NitroPack\WordPress; |
| 3 | |
| 4 | class NitroPack { |
| 5 | private static $instance = NULL; |
| 6 | public static $preUpdatePosts = array(); |
| 7 | public static $preUpdateTaxonomies = array(); |
| 8 | public static $ignoreUpdatePostIDs = array(); |
| 9 | |
| 10 | public static function getInstance() { |
| 11 | if (!self::$instance) { |
| 12 | self::$instance = new NitroPack(); |
| 13 | } |
| 14 | |
| 15 | return self::$instance; |
| 16 | } |
| 17 | |
| 18 | private $sdkObjects; |
| 19 | |
| 20 | public $Config; |
| 21 | public $Notification; |
| 22 | |
| 23 | public function __construct() { |
| 24 | $this->Config = new Config($this); |
| 25 | $this->Notifications = new Notifications($this); |
| 26 | $this->sdkObjects = array(); |
| 27 | } |
| 28 | |
| 29 | public function getSiteConfig() { |
| 30 | $siteConfig = null; |
| 31 | $npConfig = $this->Config->get(); |
| 32 | $host = !empty($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : ""; |
| 33 | $uri = !empty($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : ""; |
| 34 | $currentUrl = $host . $uri; |
| 35 | $matchLength = 0; |
| 36 | |
| 37 | if (stripos($currentUrl, "www.") === 0) { |
| 38 | $currentUrl = substr($currentUrl, 4); |
| 39 | } |
| 40 | |
| 41 | foreach ($npConfig as $siteUrl => $config) { |
| 42 | if (stripos($siteUrl, "www.") === 0) { |
| 43 | $siteUrl = substr($siteUrl, 4); |
| 44 | } |
| 45 | |
| 46 | if (stripos($currentUrl, $siteUrl) === 0 && strlen($siteUrl) > $matchLength) { |
| 47 | $siteConfig = $config; |
| 48 | $matchLength = strlen($siteUrl); |
| 49 | } |
| 50 | } |
| 51 | return $siteConfig; |
| 52 | } |
| 53 | |
| 54 | public function getSiteId() { |
| 55 | $siteConfig = $this->getSiteConfig(); |
| 56 | return $siteConfig ? $siteConfig["siteId"] : NULL; |
| 57 | } |
| 58 | |
| 59 | public function getSiteSecret() { |
| 60 | $siteConfig = $this->getSiteConfig(); |
| 61 | return $siteConfig ? $siteConfig["siteSecret"] : NULL; |
| 62 | } |
| 63 | |
| 64 | public function isConnected() { |
| 65 | return !empty($this->getSiteId()) && !empty($this->getSiteSecret()); |
| 66 | } |
| 67 | |
| 68 | public function updateCurrentBlogConfig($siteId, $siteSecret, $blogId, $enableCompression = null) { |
| 69 | if ($enableCompression === null) { |
| 70 | $enableCompression = (get_option('nitropack-enableCompression') == 1); |
| 71 | } |
| 72 | |
| 73 | $webhookToken = get_option('nitropack-webhookToken'); |
| 74 | $hosting = nitropack_detect_hosting(); |
| 75 | |
| 76 | $home_url = get_home_url(); |
| 77 | $admin_url = admin_url(); |
| 78 | $alwaysBuffer = defined("NITROPACK_ALWAYS_BUFFER") ? NITROPACK_ALWAYS_BUFFER : true; |
| 79 | $configKey = preg_replace("/^https?:\/\/(.*)/", "$1", $home_url); |
| 80 | $staticConfig = $this->Config->get(); |
| 81 | $staticConfig[$configKey] = array( |
| 82 | "siteId" => $siteId, |
| 83 | "siteSecret" => $siteSecret, |
| 84 | "blogId" => $blogId, |
| 85 | "compression" => $enableCompression, |
| 86 | "webhookToken" => $webhookToken, |
| 87 | "home_url" => $home_url, |
| 88 | "admin_url" => $admin_url, |
| 89 | "hosting" => $hosting, |
| 90 | "alwaysBuffer" => $alwaysBuffer, |
| 91 | "isEzoicActive" => \NitroPack\Integration\Plugin\Ezoic::isActive(), |
| 92 | "isApoActive" => \NitroPack\Integration\Plugin\Cloudflare::isApoActive(), |
| 93 | "isLateIntegrationInitRequired" => nitropack_is_late_integration_init_required(), |
| 94 | "isDlmActive" => \NitroPack\Integration\Plugin\DownloadManager::isActive(), |
| 95 | "dlm_downloading_url" => \NitroPack\Integration\Plugin\DownloadManager::isActive() ? \NitroPack\Integration\Plugin\DownloadManager::downloadingUrl() : NULL, |
| 96 | "dlm_download_endpoint" => \NitroPack\Integration\Plugin\DownloadManager::isActive() ? \NitroPack\Integration\Plugin\DownloadManager::downloadEndpoint() : NULL, |
| 97 | "pluginVersion" => NITROPACK_VERSION |
| 98 | ); |
| 99 | return $this->Config->set($staticConfig); |
| 100 | } |
| 101 | |
| 102 | public function unsetCurrentBlogConfig() { |
| 103 | $home_url = get_home_url(); |
| 104 | $configKey = preg_replace("/^https?:\/\/(.*)/", "$1", $home_url); |
| 105 | $staticConfig = $this->Config->get(); |
| 106 | if (!empty($staticConfig[$configKey])) { |
| 107 | unset($staticConfig[$configKey]); |
| 108 | return $this->Config->set($staticConfig); |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | public function getSdk($siteId = null, $siteSecret = null, $urlOverride = NULL, $forwardExceptions = false) { |
| 115 | $siteConfig = $this->getSiteConfig(); |
| 116 | |
| 117 | $siteId = $siteId ? $siteId : ($siteConfig ? $siteConfig['siteId'] : get_option('nitropack-siteId')); |
| 118 | $siteSecret = $siteSecret ? $siteSecret : ($siteConfig ? $siteConfig['siteSecret'] : get_option('nitropack-siteSecret')); |
| 119 | |
| 120 | if ($siteId && $siteSecret) { |
| 121 | try { |
| 122 | $userAgent = NULL; // It will be automatically detected by the SDK |
| 123 | $dataDir = nitropack_trailingslashit(NITROPACK_DATA_DIR) . $siteId; // dir without a trailing slash, because this is how the SDK expects it |
| 124 | $cacheKey = "{$siteId}:{$siteSecret}:{$dataDir}"; |
| 125 | |
| 126 | if ($urlOverride) { |
| 127 | $cacheKey .= ":{$urlOverride}"; |
| 128 | } |
| 129 | |
| 130 | if (!empty($this->sdkObjects[$cacheKey])) { |
| 131 | $nitro = $this->sdkObjects[$cacheKey]; |
| 132 | } else { |
| 133 | if (!defined("NP_COOKIE_FILTER")) { |
| 134 | \NitroPack\SDK\NitroPack::addCookieFilter("nitropack_filter_non_original_cookies"); |
| 135 | define("NP_COOKIE_FILTER", true); |
| 136 | } |
| 137 | if (!defined("NP_STORAGE_CONFIGURED")) { |
| 138 | if (defined("NITROPACK_USE_REDIS") && NITROPACK_USE_REDIS) { |
| 139 | \NitroPack\SDK\Filesystem::setStorageDriver(new \NitroPack\SDK\StorageDriver\Redis( |
| 140 | NITROPACK_REDIS_HOST, |
| 141 | NITROPACK_REDIS_PORT, |
| 142 | NITROPACK_REDIS_PASS, |
| 143 | NITROPACK_REDIS_DB |
| 144 | )); |
| 145 | } |
| 146 | define("NP_STORAGE_CONFIGURED", true); |
| 147 | } |
| 148 | $nitro = new \NitroPack\SDK\NitroPack($siteId, $siteSecret, $userAgent, $urlOverride, $dataDir); |
| 149 | $this->sdkObjects[$cacheKey] = $nitro; |
| 150 | } |
| 151 | } catch (\Exception $e) { |
| 152 | if ($forwardExceptions) { |
| 153 | throw $e; |
| 154 | } |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | return $nitro; |
| 159 | } |
| 160 | |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | public function dataDirExists() { |
| 165 | return defined("NITROPACK_DATA_DIR") && is_dir(NITROPACK_DATA_DIR); // TODO: Convert this to use the Filesystem abstraction for better Redis support |
| 166 | } |
| 167 | |
| 168 | public function initDataDir() { |
| 169 | return $this->dataDirExists() || @mkdir(NITROPACK_DATA_DIR, 0755, true); // TODO: Convert this to use the Filesystem abstraction for better Redis support |
| 170 | } |
| 171 | } |
| 172 |