PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/core/libraries/marketplace.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 266 lines | 225 code | 31 blank | 10 comment | 47 complexity | 707d07270936852f6842f4272f7d1d2e MD5 | raw file
  1. <?php
  2. defined('C5_EXECUTE') or die("Access Denied.");
  3. class Concrete5_Library_Marketplace {
  4. const E_INVALID_BASE_URL = 20;
  5. const E_MARKETPLACE_SUPPORT_MANUALLY_DISABLED = 21;
  6. const E_UNRECOGNIZED_SITE_TOKEN = 22;
  7. const E_DELETED_SITE_TOKEN = 31;
  8. const E_GENERAL_CONNECTION_ERROR = 99;
  9. protected $isConnected = false;
  10. protected $connectionError = false;
  11. public static function getInstance() {
  12. static $instance;
  13. if (!isset($instance)) {
  14. $m = __CLASS__;
  15. $instance = new $m;
  16. }
  17. return $instance;
  18. }
  19. public function __construct() {
  20. if (defined('ENABLE_MARKETPLACE_SUPPORT') && ENABLE_MARKETPLACE_SUPPORT == false) {
  21. $this->connectionError = Marketplace::E_MARKETPLACE_SUPPORT_MANUALLY_DISABLED;
  22. return;
  23. }
  24. $csToken = Config::get('MARKETPLACE_SITE_TOKEN');
  25. if ($csToken != '') {
  26. $fh = Loader::helper('file');
  27. $csiURL = urlencode(BASE_URL . DIR_REL);
  28. $url = MARKETPLACE_URL_CONNECT_VALIDATE."?csToken={$csToken}&csiURL=" . $csiURL . "&csiVersion=" . APP_VERSION;
  29. $vn = Loader::helper('validation/numbers');
  30. $r = $fh->getContents($url);
  31. if ($r == false) {
  32. $this->isConnected = true;
  33. } else if ($vn->integer($r)) {
  34. $this->isConnected = false;
  35. $this->connectionError = $r;
  36. if ($this->connectionError == Marketplace::E_DELETED_SITE_TOKEN) {
  37. Config::clear('MARKETPLACE_SITE_TOKEN');
  38. Config::clear('MARKETPLACE_SITE_URL_TOKEN');
  39. }
  40. } else {
  41. $this->isConnected = false;
  42. $this->connectionError = self::E_GENERAL_CONNECTION_ERROR;
  43. }
  44. }
  45. }
  46. public function isConnected() {
  47. return $this->isConnected;
  48. }
  49. public function hasConnectionError() {
  50. return $this->connectionError != false;
  51. }
  52. public function getConnectionError() {
  53. return $this->connectionError;
  54. }
  55. public function generateSiteToken() {
  56. $fh = Loader::helper('file');
  57. $token = $fh->getContents(MARKETPLACE_URL_CONNECT_TOKEN_NEW);
  58. return $token;
  59. }
  60. public function getSiteToken() {
  61. $token = Config::get('MARKETPLACE_SITE_TOKEN');
  62. return $token;
  63. }
  64. public function getSitePageURL() {
  65. $token = Config::get('MARKETPLACE_SITE_URL_TOKEN');
  66. return MARKETPLACE_BASE_URL_SITE_PAGE . '/' . $token;
  67. }
  68. public static function downloadRemoteFile($file) {
  69. $fh = Loader::helper('file');
  70. $file .= '?csiURL=' . urlencode(BASE_URL . DIR_REL);
  71. $pkg = $fh->getContents($file);
  72. if (empty($pkg)) {
  73. return Package::E_PACKAGE_DOWNLOAD;
  74. }
  75. if ($pkg == Package::E_PACKAGE_INVALID_APP_VERSION) {
  76. return Package::E_PACKAGE_INVALID_APP_VERSION;
  77. }
  78. $file = time();
  79. // Use the same method as the Archive library to build a temporary file name.
  80. $tmpFile = $fh->getTemporaryDirectory() . '/' . $file . '.zip';
  81. $fp = fopen($tmpFile, "wb");
  82. if ($fp) {
  83. fwrite($fp, $pkg);
  84. fclose($fp);
  85. } else {
  86. return Package::E_PACKAGE_SAVE;
  87. }
  88. return $file;
  89. }
  90. public function getMarketplaceFrame($width = '100%', $height = '300', $completeURL = false, $connectMethod = 'view') {
  91. // if $mpID is passed, we are going to either
  92. // a. go to its purchase page
  93. // b. pass you through to the page AFTER connecting.
  94. $tp = new TaskPermission();
  95. if ($tp->canInstallPackages()) {
  96. if (!$this->isConnected()) {
  97. if (!$completeURL) {
  98. $completeURL = BASE_URL . View::url('/dashboard/extend/connect', 'connect_complete');
  99. }
  100. $csReferrer = urlencode($completeURL);
  101. $csiURL = urlencode(BASE_URL . DIR_REL);
  102. $csiBaseURL = urlencode(BASE_URL);
  103. if ($this->hasConnectionError()) {
  104. if ($this->connectionError == E_DELETED_SITE_TOKEN) {
  105. $connectMethod = 'view';
  106. $csToken = Marketplace::generateSiteToken();
  107. } else {
  108. $csToken = $this->getSiteToken();
  109. }
  110. } else {
  111. // new connection
  112. $csToken = Marketplace::generateSiteToken();
  113. }
  114. $url = MARKETPLACE_URL_CONNECT . '/-/' . $connectMethod;
  115. $url = $url . '?ts=' . time() . '&csiBaseURL=' . $csiBaseURL . '&csiURL=' . $csiURL . '&csToken=' . $csToken . '&csReferrer=' . $csReferrer . '&csName=' . htmlspecialchars(SITE, ENT_QUOTES, APP_CHARSET);
  116. } else {
  117. $csiBaseURL = urlencode(BASE_URL);
  118. $url = MARKETPLACE_URL_CONNECT_SUCCESS . '?csToken=' . $this->getSiteToken() . '&csiBaseURL=' . $csiBaseURL;
  119. }
  120. if ($csToken == false && !$this->isConnected()) {
  121. return '<div class="ccm-error">' . t('Unable to generate a marketplace token. Please ensure that allow_url_fopen is turned on, or that cURL is enabled on your server. If these are both true, It\'s possible your site\'s IP address may be blacklisted for some reason on our server. Please ask your webhost what your site\'s outgoing cURL request IP address is, and email it to us at <a href="mailto:help@concrete5.org">help@concrete5.org</a>.') . '</div>';
  122. } else {
  123. $time = time();
  124. $ifr = '<script type="text/javascript">$(function() { $.receiveMessage(function(e) {
  125. jQuery.fn.dialog.hideLoader();
  126. if (e.data == "loading") {
  127. jQuery.fn.dialog.showLoader();
  128. } else {
  129. var eh = e.data;
  130. eh = parseInt(eh) + 20;
  131. $("#ccm-marketplace-frame-' . $time . '").attr("height", eh);
  132. }
  133. }, \'' . CONCRETE5_ORG_URL . '\');
  134. });
  135. </script>';
  136. $ifr .= '<iframe class="ccm-marketplace-frame-connect" id="ccm-marketplace-frame-' . $time . '" frameborder="0" width="' . $width . '" height="' . $height . '" src="' . $url . '"></iframe>';
  137. return $ifr;
  138. }
  139. } else {
  140. return '<div class="ccm-error">' . t('You do not have permission to connect this site to the marketplace.') . '</div>';
  141. }
  142. }
  143. public function getMarketplacePurchaseFrame($mp, $width = '100%', $height = '530') {
  144. $tp = new TaskPermission();
  145. if ($tp->canInstallPackages()) {
  146. if (!is_object($mp)) {
  147. return '<div class="alert-message block-message error">' . t('Unable to get information about this product.') . '</div>';
  148. }
  149. if ($this->isConnected()) {
  150. $url = MARKETPLACE_URL_CHECKOUT;
  151. $csiURL = urlencode(BASE_URL . DIR_REL);
  152. $csiBaseURL = urlencode(BASE_URL);
  153. $csToken = $this->getSiteToken();
  154. $url = $url . '/' . $mp->getProductBlockID() . '?ts=' . time() . '&csiBaseURL=' . $csiBaseURL . '&csiURL=' . $csiURL . '&csToken=' . $csToken;
  155. }
  156. $time = time();
  157. $ifr = '<script type="text/javascript">$(function() { $.receiveMessage(function(e) {
  158. jQuery.fn.dialog.hideLoader();
  159. if (e.data == "loading") {
  160. jQuery.fn.dialog.showLoader();
  161. } else {
  162. var eh = e.data;
  163. eh = parseInt(eh) + 20;
  164. $("#ccm-marketplace-frame-' . $time . '").attr("height", eh);
  165. }
  166. }, \'' . CONCRETE5_ORG_URL_SECURE . '\');
  167. });
  168. </script>';
  169. $ifr .= '<iframe class="ccm-marketplace-frame" id="ccm-marketplace-frame-' . $time . '" class="ccm-marketplace-frame" frameborder="0" width="' . $width . '" height="' . $height . '" src="' . $url . '"></iframe>';
  170. return $ifr;
  171. } else {
  172. return '<div class="ccm-error">' . t('You do not have permission to connect this site to the marketplace.') . '</div>';
  173. }
  174. }
  175. /**
  176. * Runs through all packages on the marketplace, sees if they're installed here, and updates the available version number for them
  177. */
  178. public static function checkPackageUpdates() {
  179. Loader::model('system_notification');
  180. $items = Marketplace::getAvailableMarketplaceItems(false);
  181. foreach($items as $i) {
  182. $p = Package::getByHandle($i->getHandle());
  183. if (is_object($p)) {
  184. // we only add a notification if it's newer than the last one we know about
  185. if (version_compare($p->getPackageVersionUpdateAvailable(), $i->getVersion(), '<') && version_compare($p->getPackageVersion(), $i->getVersion(), '<')) {
  186. SystemNotification::add(SystemNotification::SN_TYPE_ADDON_UPDATE, t('An updated version of %s is available.', $i->getName()), t('New Version: %s.', $i->getVersion()), '', View::url('/dashboard/extend/update'), $i->getRemoteURL());
  187. }
  188. $p->updateAvailableVersionNumber($i->getVersion());
  189. }
  190. }
  191. }
  192. public function getAvailableMarketplaceItems($filterInstalled=true) {
  193. Loader::model('marketplace_remote_item');
  194. $fh = Loader::helper('file');
  195. if (!$fh) return array();
  196. // Retrieve the URL contents
  197. $csToken = Config::get('MARKETPLACE_SITE_TOKEN');
  198. $csiURL = urlencode(BASE_URL . DIR_REL);
  199. $url = MARKETPLACE_PURCHASES_LIST_WS."?csToken={$csToken}&csiURL=" . $csiURL . "&csiVersion=" . APP_VERSION;
  200. $json = $fh->getContents($url);
  201. $addons=array();
  202. $objects = @Loader::helper('json')->decode($json);
  203. if (is_array($objects)) {
  204. try {
  205. foreach($objects as $addon){
  206. $mi = new MarketplaceRemoteItem();
  207. $mi->setPropertiesFromJSONObject($addon);
  208. $remoteCID = $mi->getRemoteCollectionID();
  209. if (!empty($remoteCID)) {
  210. $addons[$mi->getHandle()] = $mi;
  211. }
  212. }
  213. } catch (Exception $e) {}
  214. if ($filterInstalled && is_array($addons)) {
  215. Loader::model('package');
  216. $handles = Package::getInstalledHandles();
  217. if (is_array($handles)) {
  218. $adlist = array();
  219. foreach($addons as $key=>$ad) {
  220. if (!in_array($ad->getHandle(), $handles)) {
  221. $adlist[$key] = $ad;
  222. }
  223. }
  224. $addons = $adlist;
  225. }
  226. }
  227. }
  228. return $addons;
  229. }
  230. }
  231. ?>