Exemplo n.º 1
0
/**
 * @param $packageName
 *
 * @return array|bool
 */
function installPackage($packageName, $options)
{
    global $modx;
    /** @var modTransportProvider $provider */
    if (isset($options['service_url'])) {
        $providerParams = array('service_url:LIKE' => '%' . $options['service_url'] . '%');
    } else {
        $providerParams = array('service_url:LIKE' => '%store.simpledream.ru%', 'OR:service_url:LIKE' => '%modstore.pro%');
    }
    $provider = $modx->getObject('transport.modTransportProvider', $providerParams);
    if (!$provider) {
        $provider = $modx->getObject('transport.modTransportProvider', 1);
    }
    $provider->getClient();
    $version = $modx->getVersionData();
    $productVersion = $version['code_name'] . '-' . $version['full_version'];
    $response = $provider->request('package', 'GET', array('supports' => $productVersion, 'query' => $packageName));
    if (!empty($response)) {
        $foundPackages = simplexml_load_string($response->response);
        foreach ($foundPackages as $foundPackage) {
            /** @var modTransportPackage $foundPackage */
            if ($foundPackage->name == $packageName) {
                $sig = explode('-', $foundPackage->signature);
                $versionSignature = explode('.', $sig[1]);
                $url = $foundPackage->location;
                if (!downloadPackage($url, MODX_CORE_PATH . 'packages/' . $foundPackage->signature . '.transport.zip')) {
                    return array('success' => 0, 'message' => 'Could not download package <b>' . $packageName . '</b>.');
                }
                // Add in the package as an object so it can be upgraded
                /** @var modTransportPackage $package */
                $package = $modx->newObject('transport.modTransportPackage');
                $package->set('signature', $foundPackage->signature);
                $package->fromArray(array('created' => date('Y-m-d h:i:s'), 'updated' => null, 'state' => 1, 'workspace' => 1, 'provider' => $provider->id, 'source' => $foundPackage->signature . '.transport.zip', 'package_name' => $packageName, 'version_major' => $versionSignature[0], 'version_minor' => !empty($versionSignature[1]) ? $versionSignature[1] : 0, 'version_patch' => !empty($versionSignature[2]) ? $versionSignature[2] : 0));
                if (!empty($sig[2])) {
                    $r = preg_split('/([0-9]+)/', $sig[2], -1, PREG_SPLIT_DELIM_CAPTURE);
                    if (is_array($r) && !empty($r)) {
                        $package->set('release', $r[0]);
                        $package->set('release_index', isset($r[1]) ? $r[1] : '0');
                    } else {
                        $package->set('release', $sig[2]);
                    }
                }
                if ($package->save() && $package->install()) {
                    return array('success' => 1, 'message' => '<b>' . $packageName . '</b> was successfully installed');
                } else {
                    return array('success' => 0, 'message' => 'Could not save package <b>' . $packageName . '</b>');
                }
                break;
            }
        }
    } else {
        return array('success' => 0, 'message' => 'Could not find <b>' . $packageName . '</b> in MODX repository');
    }
    return true;
}
Exemplo n.º 2
0
         $dst_ru = $dicts . 'dict_ru.zip';
         $src_en = 'http://modhost.pro/dists/morphy-0.3.x-en_EN-windows-1250.zip';
         $dst_en = $dicts . 'dict_en.zip';
         $modx->log(modX::LOG_LEVEL_INFO, 'Trying to download <b>russian</b> dictionary. Please wait...');
         if (!file_exists($dst_ru)) {
             downloadPackage($src_ru, $dst_ru);
         }
         $file = new PclZip($dst_ru);
         if ($ru = $file->extract(PCLZIP_OPT_PATH, $dicts)) {
             unlink($dst_ru);
         } else {
             $modx->log(xPDO::LOG_LEVEL_ERROR, 'Could not extract dictionaries from ' . $dst_ru . ' to ' . $dicts . '. Error: ' . $file->errorInfo());
         }
         $modx->log(modX::LOG_LEVEL_INFO, 'Trying to download <b>english</b> dictionary. Please wait...');
         if (!file_exists($dst_en)) {
             downloadPackage($src_en, $dst_en);
         }
         $file = new PclZip($dst_en);
         if ($en = $file->extract(PCLZIP_OPT_PATH, $dicts)) {
             unlink($dst_en);
         } else {
             $modx->log(xPDO::LOG_LEVEL_ERROR, 'Could not extract dictionaries from ' . $dst_en . ' to ' . $dicts . '. Error: ' . $file->errorInfo());
         }
         if ($ru && $en) {
             file_put_contents($path . 'phpmorphy/dicts/.installed', date('Y-m-d H:i:s'));
         }
     }
     $success = true;
     break;
 case xPDOTransport::ACTION_UNINSTALL:
     $success = true;