This is the API documentation for concrete5 version 5.6 and earlier. View Current API Documentation
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 
<?php

defined('C5_EXECUTE') or die("Access Denied.");

class Concrete5_Library_Marketplace {
    
    const E_INVALID_BASE_URL = 20;
    const E_MARKETPLACE_SUPPORT_MANUALLY_DISABLED = 21;
    const E_UNRECOGNIZED_SITE_TOKEN = 22;
    const E_DELETED_SITE_TOKEN = 31;
    const E_GENERAL_CONNECTION_ERROR = 99;

    protected $isConnected = false;
    protected $connectionError = false;
    
    public static function getInstance() {
        static $instance;
        if (!isset($instance)) {
            $m = __CLASS__;
            $instance = new $m;
        }
        return $instance;
    }
    
    public function __construct() {
        if (defined('ENABLE_MARKETPLACE_SUPPORT') && ENABLE_MARKETPLACE_SUPPORT == false) {
            $this->connectionError = Marketplace::E_MARKETPLACE_SUPPORT_MANUALLY_DISABLED;
            return;
        }

        $csToken = Config::get('MARKETPLACE_SITE_TOKEN');
        if ($csToken != '') {

            $fh = Loader::helper('file');
            $csiURL = urlencode(BASE_URL . DIR_REL);
            $url = MARKETPLACE_URL_CONNECT_VALIDATE."?csToken={$csToken}&csiURL=" . $csiURL . "&csiVersion=" . APP_VERSION;
            $vn = Loader::helper('validation/numbers');
            $r = $fh->getContents($url);
            if ($r == false) {
                $this->isConnected = true;
            } else if ($vn->integer($r)) {
                $this->isConnected = false;
                $this->connectionError = $r;
                
                if ($this->connectionError == Marketplace::E_DELETED_SITE_TOKEN) {
                    Config::clear('MARKETPLACE_SITE_TOKEN');
                    Config::clear('MARKETPLACE_SITE_URL_TOKEN');                    
                }
            } else {
                $this->isConnected = false;
                $this->connectionError = self::E_GENERAL_CONNECTION_ERROR;
            }
        }       
    }
    
    public function isConnected() {
        return $this->isConnected;
    }
    
    public function hasConnectionError() {
        return $this->connectionError != false;
    }
    
    public function getConnectionError() {
        return $this->connectionError;
    }
    
    public function generateSiteToken() {
        $fh = Loader::helper('file');
        $token = $fh->getContents(MARKETPLACE_URL_CONNECT_TOKEN_NEW);
        return $token;  
    }

    public function getSiteToken() {
        $token = Config::get('MARKETPLACE_SITE_TOKEN');
        return $token;
    }
    
    public function getSitePageURL() {
        $token = Config::get('MARKETPLACE_SITE_URL_TOKEN');
        return MARKETPLACE_BASE_URL_SITE_PAGE . '/' . $token;
    }

    public static function downloadRemoteFile($file) {
        $fh = Loader::helper('file');
        $file .= '?csiURL=' . urlencode(BASE_URL . DIR_REL);
        $pkg = $fh->getContents($file);
        if (empty($pkg)) {
            return Package::E_PACKAGE_DOWNLOAD;
        }
        if ($pkg == Package::E_PACKAGE_INVALID_APP_VERSION) {
            return Package::E_PACKAGE_INVALID_APP_VERSION;
        }

        $file = time();
        // Use the same method as the Archive library to build a temporary file name.
        $tmpFile = $fh->getTemporaryDirectory() . '/' . $file . '.zip';
        $fp = fopen($tmpFile, "wb");
        if ($fp) {
            fwrite($fp, $pkg);
            fclose($fp);
        } else {
            return Package::E_PACKAGE_SAVE;
        }
        
        return $file;
    }
    
    public function getMarketplaceFrame($width = '100%', $height = '300', $completeURL = false, $connectMethod = 'view') {
        // if $mpID is passed, we are going to either
        // a. go to its purchase page
        // b. pass you through to the page AFTER connecting.
        $tp = new TaskPermission();
        if ($tp->canInstallPackages()) {
            if (!$this->isConnected()) {
                if (!$completeURL) {
                    $completeURL = BASE_URL . View::url('/dashboard/extend/connect', 'connect_complete');
                }
                $csReferrer = urlencode($completeURL);
                $csiURL = urlencode(BASE_URL . DIR_REL);
                $csiBaseURL = urlencode(BASE_URL);
                if ($this->hasConnectionError()) {
                    if ($this->connectionError == E_DELETED_SITE_TOKEN) {
                        $connectMethod = 'view';
                        $csToken = Marketplace::generateSiteToken();
                    } else { 
                        $csToken = $this->getSiteToken();
                    }
                } else {
                    // new connection 
                    $csToken = Marketplace::generateSiteToken();
                }
                $url = MARKETPLACE_URL_CONNECT . '/-/' . $connectMethod;
                $url = $url . '?ts=' . time() . '&csiBaseURL=' . $csiBaseURL . '&csiURL=' . $csiURL . '&csToken=' . $csToken . '&csReferrer=' . $csReferrer . '&csName=' . htmlspecialchars(SITE, ENT_QUOTES, APP_CHARSET);
            } else {
                $csiBaseURL = urlencode(BASE_URL);
                $url = MARKETPLACE_URL_CONNECT_SUCCESS . '?csToken=' . $this->getSiteToken() . '&csiBaseURL=' . $csiBaseURL;
            }
            if ($csToken == false && !$this->isConnected()) {
                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:[email protected]">[email protected]</a>.') . '</div>';
            } else {
                $time = time();
                $ifr = '<script type="text/javascript">$(function() { $.receiveMessage(function(e) { 
                    jQuery.fn.dialog.hideLoader();

                    if (e.data == "loading") {
                        jQuery.fn.dialog.showLoader();
                    } else { 
                        var eh = e.data;
                        eh = parseInt(eh) + 20;
                        $("#ccm-marketplace-frame-' . $time . '").attr("height", eh); 
                    }
                    
                    }, \'' . CONCRETE5_ORG_URL . '\');  
                }); 
                </script>';
                $ifr .= '<iframe class="ccm-marketplace-frame-connect" id="ccm-marketplace-frame-' . $time . '" frameborder="0" width="' . $width . '" height="' . $height . '" src="' . $url . '"></iframe>';
                return $ifr;
            }
        } else {
            return '<div class="ccm-error">' . t('You do not have permission to connect this site to the marketplace.') . '</div>';
        }
    }

    public function getMarketplacePurchaseFrame($mp, $width = '100%', $height = '530') {
        $tp = new TaskPermission();
        if ($tp->canInstallPackages()) {
            if (!is_object($mp)) {
                return '<div class="alert-message block-message error">' . t('Unable to get information about this product.') . '</div>';
            }
            if ($this->isConnected()) {
                $url = MARKETPLACE_URL_CHECKOUT;
                $csiURL = urlencode(BASE_URL . DIR_REL);
                $csiBaseURL = urlencode(BASE_URL);
                $csToken = $this->getSiteToken();
                $url = $url . '/' . intval($mp->getProductBlockID(), 10) . '?ts=' . time() . '&csiBaseURL=' . $csiBaseURL . '&csiURL=' . $csiURL . '&csToken=' . $csToken;
            }
            $time = time();
            $ifr = '<script type="text/javascript">$(function() { $.receiveMessage(function(e) { 
                jQuery.fn.dialog.hideLoader();

                if (e.data == "loading") {
                    jQuery.fn.dialog.showLoader();
                } else { 
                    var eh = e.data;
                    eh = parseInt(eh) + 20;
                    $("#ccm-marketplace-frame-' . $time . '").attr("height", eh); 
                }
                
                }, \'' . CONCRETE5_ORG_URL_SECURE . '\');   
            }); 
            </script>';
            $ifr .= '<iframe class="ccm-marketplace-frame" id="ccm-marketplace-frame-' . $time . '" class="ccm-marketplace-frame" frameborder="0" width="' . $width . '" height="' . $height . '" src="' . $url . '"></iframe>';
            return $ifr;
        } else {
            return '<div class="ccm-error">' . t('You do not have permission to connect this site to the marketplace.') . '</div>';
        }
    }
    
    
    /** 
     * Runs through all packages on the marketplace, sees if they're installed here, and updates the available version number for them
     */
    public static function checkPackageUpdates() {
        Loader::model('system_notification');
        $items = Marketplace::getAvailableMarketplaceItems(false);
        foreach($items as $i) {
            $p = Package::getByHandle($i->getHandle());
            if (is_object($p)) {
                // we only add a notification if it's newer than the last one we know about
                if (version_compare($p->getPackageVersionUpdateAvailable(), $i->getVersion(), '<') && version_compare($p->getPackageVersion(), $i->getVersion(), '<')) {
                    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());
                }
                $p->updateAvailableVersionNumber($i->getVersion());
            }
        }
    }

    public function getAvailableMarketplaceItems($filterInstalled=true) {
        Loader::model('marketplace_remote_item');
        
        $fh = Loader::helper('file'); 
        if (!$fh) return array();

        // Retrieve the URL contents 
        $csToken = Config::get('MARKETPLACE_SITE_TOKEN');
        $csiURL = urlencode(BASE_URL . DIR_REL);
        $url = MARKETPLACE_PURCHASES_LIST_WS."?csToken={$csToken}&csiURL=" . $csiURL . "&csiVersion=" . APP_VERSION;
        $json = $fh->getContents($url);

        $addons=array();
        
        $objects = @Loader::helper('json')->decode($json);
        if (is_array($objects)) {
            try {
                foreach($objects as $addon){
                    $mi = new MarketplaceRemoteItem();
                    $mi->setPropertiesFromJSONObject($addon);
                    $remoteCID = $mi->getRemoteCollectionID();
                    if (!empty($remoteCID)) {
                        $addons[$mi->getHandle()] = $mi;
                    }
                }
            } catch (Exception $e) {}
    
            if ($filterInstalled && is_array($addons)) {
                Loader::model('package');
                $handles = Package::getInstalledHandles();
                if (is_array($handles)) {
                    $adlist = array();
                    foreach($addons as $key=>$ad) {
                        if (!in_array($ad->getHandle(), $handles)) {
                            $adlist[$key] = $ad;
                        }
                    }
                    $addons = $adlist;
                }
            }
        }
        
        return $addons;
    }

}

?>