PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/concreteOLD/models/marketplace_remote_item.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 309 lines | 249 code | 48 blank | 12 comment | 44 complexity | 297ee80e1f1a3711638c6c9a4d7eebc6 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * For loading an external block from the marketplace
  5. * @author Tony Trupp <tony@concrete5.org>
  6. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  7. * @license http://www.concrete5.org/license/ MIT License
  8. * @package Blocks
  9. * @category Concrete
  10. */
  11. class MarketplaceRemoteItem extends Object {
  12. protected $price=0.00;
  13. protected $remoteCID=0;
  14. protected $remoteURL='';
  15. protected $remoteFileURL='';
  16. protected $remoteIconURL='';
  17. protected $isLicensedToSite = false;
  18. public function setPropertiesFromJSONObject($obj) {
  19. foreach($obj as $prop => $value) {
  20. $this->{$prop} = $value;
  21. }
  22. }
  23. public function getMarketplaceItemID() {return $this->mpID;}
  24. public function getMarketplaceItemType() {return $this->mpType;}
  25. public function getHandle() { return $this->handle; }
  26. public function getName(){ return $this->name; }
  27. public function getDescription() {return $this->description;}
  28. public function getBody() {return $this->bodyContent;}
  29. public function getPrice(){ return sprintf("%.2f",floatval($this->price)); }
  30. public function getScreenshots() {
  31. if (is_array($this->screenshots)) {
  32. return $this->screenshots;
  33. } else {
  34. return array();
  35. }
  36. }
  37. public function getMarketplaceItemVersionForThisSite() {return $this->siteLatestAvailableVersion;}
  38. public function getAverageRating() {return $this->rating;}
  39. public function getVersionHistory() {return $this->versionHistory;}
  40. public function getTotalRatings() {
  41. if ($this->totalRatings) {
  42. return $this->totalRatings;
  43. } else {
  44. return 0;
  45. }
  46. }
  47. public function getRemoteReviewsURL() {return $this->reviewsURL;}
  48. public function getRemoteCollectionID(){ return $this->cID; }
  49. public function getReviewBody() {
  50. return $this->reviewBody;
  51. }
  52. public function getLargeThumbnail() {
  53. if ($this->largethumbnail) {
  54. return $this->largethumbnail;
  55. } else {
  56. $screenshots = $this->getScreenshots();
  57. return $screenshots[0];
  58. }
  59. }
  60. public function getRemoteURL(){ return $this->url; }
  61. public function getProductBlockID() {return $this->productBlockID;}
  62. public function getFivePackProductBlockID() {return $this->fivePackProductBlockID;}
  63. public function getRemoteFileURL(){ return $this->file; }
  64. public function getRemoteIconURL(){ return $this->icon; }
  65. public function getRemoteListIconURL() {return $this->listicon;}
  66. public function isLicensedToSite() {return $this->islicensed;}
  67. public function purchaseRequired() {
  68. if ($this->price == '' || $this->price == '0' || $this->price == '0.00') {
  69. return false;
  70. } else if ($this->isLicensedToSite()) {
  71. return false;
  72. } else {
  73. return true;
  74. }
  75. }
  76. public function getVersion() {return $this->pkgVersion;}
  77. public function downloadUpdate() {
  78. // backup the old package
  79. $pkg = Package::getByHandle($this->getHandle());
  80. $r = $pkg->backup();
  81. if (is_array($r)) {
  82. return $r;
  83. }
  84. $fileURL = $this->getRemoteFileURL();
  85. if (empty($fileURL)) {
  86. return array(Package::E_PACKAGE_NOT_FOUND);
  87. }
  88. $file = Marketplace::downloadRemoteFile($this->getRemoteFileURL());
  89. if (empty($file) || $file == Package::E_PACKAGE_DOWNLOAD) {
  90. return array(Package::E_PACKAGE_DOWNLOAD);
  91. } else if ($file == Package::E_PACKAGE_SAVE) {
  92. return array($file);
  93. }
  94. try {
  95. Loader::model('package_archive');
  96. $am = new PackageArchive($this->getHandle());
  97. $am->install($file, true);
  98. } catch (Exception $e) {
  99. return array($e->getMessage());
  100. }
  101. }
  102. public function download() {
  103. $file = Marketplace::downloadRemoteFile($this->getRemoteFileURL());
  104. if (empty($file) || $file == Package::E_PACKAGE_DOWNLOAD) {
  105. return array(Package::E_PACKAGE_DOWNLOAD);
  106. } else if ($file == Package::E_PACKAGE_SAVE) {
  107. return array($file);
  108. } else if ($file == Package::E_PACKAGE_INVALID_APP_VERSION) {
  109. return array($file);
  110. }
  111. try {
  112. Loader::model('package_archive');
  113. $am = new PackageArchive($this->getHandle());
  114. $am->install($file, true);
  115. } catch (Exception $e) {
  116. return array($e->getMessage());
  117. }
  118. if ($install) {
  119. $tests = Package::testForInstall($this->getHandle());
  120. if (is_array($tests)) {
  121. return $tests;
  122. } else {
  123. $p = Loader::package($this->getHandle());
  124. try {
  125. $p->install();
  126. } catch(Exception $e) {
  127. return array(Package::E_PACKAGE_INSTALL);
  128. }
  129. }
  130. }
  131. }
  132. public function enableFreeLicense() {
  133. $fh = Loader::helper('file');
  134. $csToken = Config::get('MARKETPLACE_SITE_TOKEN');
  135. $csiURL = urlencode(BASE_URL . DIR_REL);
  136. $url = MARKETPLACE_ITEM_FREE_LICENSE_WS."?mpID=" . $this->mpID . "&csToken={$csToken}&csiURL=" . $csiURL . "&csiVersion=" . APP_VERSION;
  137. $fh->getContents($url);
  138. }
  139. protected static function getRemotePackageObject($method, $identifier) {
  140. $fh = Loader::helper('file');
  141. // Retrieve the URL contents
  142. $csToken = Config::get('MARKETPLACE_SITE_TOKEN');
  143. $csiURL = urlencode(BASE_URL . DIR_REL);
  144. $url = MARKETPLACE_ITEM_INFORMATION_WS."?" . $method . "=" . $identifier . "&csToken={$csToken}&csiURL=" . $csiURL . "&csiVersion=" . APP_VERSION;
  145. $json = $fh->getContents($url);
  146. try {
  147. // Parse the returned XML file
  148. $obj = @Loader::helper('json')->decode($json);
  149. if (is_object($obj)) {
  150. $mi = new MarketplaceRemoteItem();
  151. $mi->setPropertiesFromJSONObject($obj);
  152. if ($mi->getMarketplaceItemID() > 0) {
  153. return $mi;
  154. }
  155. }
  156. } catch (Exception $e) {
  157. throw new Exception(t('Unable to connect to marketplace to retrieve item'));
  158. }
  159. }
  160. public static function getByHandle($mpHandle) {
  161. return MarketplaceRemoteItem::getRemotePackageObject('mpHandle', $mpHandle);
  162. }
  163. public static function getByID($mpID) {
  164. return MarketplaceRemoteItem::getRemotePackageObject('mpID', $mpID);
  165. }
  166. }
  167. class MarketplaceRemoteItemList extends ItemList {
  168. protected $includeInstalledItems = true;
  169. protected $params = array();
  170. protected $type = 'themes';
  171. protected $itemsPerPage = 20;
  172. public static function getItemSets($type) {
  173. $url = MARKETPLACE_REMOTE_ITEM_LIST_WS;
  174. $url .= $type . '/-/get_remote_item_sets';
  175. $contents = Loader::helper("file")->getContents($url);
  176. $sets = array();
  177. if ($contents != '') {
  178. $objects = @Loader::helper('json')->decode($contents);
  179. if (is_array($objects)) {
  180. foreach($objects as $obj) {
  181. $mr = new MarketplaceRemoteItemSet();
  182. $mr->id = $obj->marketplaceItemSetID;
  183. $mr->name = $obj->marketplaceItemSetName;
  184. $sets[] = $mr;
  185. }
  186. }
  187. }
  188. return $sets;
  189. }
  190. public function setIncludeInstalledItems($pp) {
  191. $this->includeInstalledItems = $pp;
  192. }
  193. public function setType($type) {
  194. $this->type = $type;
  195. }
  196. public function filterByKeywords($keywords) {
  197. $this->params['keywords'] = $keywords;
  198. }
  199. public function filterByMarketplaceItemID($mpID) {
  200. $this->params['mpID'] = $mpID;
  201. }
  202. public function sortBy($sortBy) {
  203. $this->params['sort'] = $sortBy;
  204. }
  205. public function filterBySet($set) {
  206. $this->params['set'] = $set;
  207. }
  208. public function filterByIsFeaturedRemotely($r) {
  209. $this->params['is_featured_remotely'] = $r;
  210. }
  211. public function filterByCompatibility($r) {
  212. $this->params['is_compatible'] = $r;
  213. }
  214. public function execute() {
  215. $params = $this->params;
  216. $params['version'] = APP_VERSION;
  217. $params['itemsPerPage'] = $this->itemsPerPage;
  218. Loader::library("marketplace");
  219. $mi = Marketplace::getInstance();
  220. $params['csToken'] = $mi->getSiteToken();
  221. if ($this->includeInstalledItems) {
  222. $params['includeInstalledItems'] = 1;
  223. } else {
  224. $params['includeInstalledItems'] = 0;
  225. $list = Package::getInstalledList();
  226. foreach($list as $pkg) {
  227. $params['installedPackages'][] = $pkg->getPackageHandle();
  228. }
  229. }
  230. if (isset($_REQUEST[$this->queryStringPagingVariable])) {
  231. $params[$this->queryStringPagingVariable] = $_REQUEST[$this->queryStringPagingVariable];
  232. }
  233. $uh = Loader::helper('url');
  234. $url = $uh->buildQuery(MARKETPLACE_REMOTE_ITEM_LIST_WS . $this->type . '/-/get_remote_list', $params);
  235. $r = Loader::helper('file')->getContents($url);
  236. $r2 = @Loader::helper('json')->decode($r);
  237. $total = 0;
  238. $items = array();
  239. if (is_object($r2)) {
  240. $items = $r2->items;
  241. $total = $r2->total;
  242. }
  243. $this->total = $total;
  244. $this->setItems($items);
  245. }
  246. public function get($itemsToGet = 0, $offset = 0) {
  247. $this->start = $offset;
  248. $items = $this->items;
  249. $marketplaceItems = array();
  250. foreach($items as $stdObj) {
  251. $mi = new MarketplaceRemoteItem();
  252. $mi->setPropertiesFromJSONObject($stdObj);
  253. $marketplaceItems[] = $mi;
  254. }
  255. return $marketplaceItems;
  256. }
  257. }
  258. class MarketplaceRemoteItemSet extends Object {
  259. public function getMarketplaceRemoteSetName() {return $this->name;}
  260. public function getMarketplaceRemoteSetID() {return $this->id;}
  261. }