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: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607: 608: 609: 610: 611: 612: 613: 614: 615: 616: 617: 618: 619: 620: 621: 622: 623: 624: 625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674: 675: 676: 677: 678: 679: 680: 681: 682: 683: 684: 685: 686: 687: 688: 689: 690: 691: 692: 693: 694: 695: 696: 697: 698: 699: 700: 701: 702: 703: 704: 705: 706: 707: 708: 709: 710: 711: 712: 713: 714: 715: 716: 717: 718: 719: 720: 721: 722: 723: 724: 725: 726: 727: 728: 729: 730: 731: 732: 733: 734: 735: 736: 737: 738: 739: 740: 741: 742: 743: 744: 745: 746: 747: 748: 749: 750: 751: 752: 753: 754: 755: 756: 757: 758: 759: 760: 761: 762: 763: 764: 765: 766: 767: 768: 769: 770: 771: 772: 773: 774: 775: 776: 777: 778: 779: 780: 781: 782: 783: 784: 785: 786: 787: 788: 789: 790: 791: 792: 793: 794: 795: 796: 797: 798: 799: 800: 801: 802: 803: 804: 805: 806: 807: 808: 809: 810: 811: 812: 813: 814: 815: 816: 817: 818: 819: 820: 821: 822: 823: 824: 825: 826: 827: 828: 829: 830: 831: 832: 833: 834: 835: 836: 837: 838: 839: 840: 841: 842: 843: 844: 845: 846: 847: 848: 849: 850: 851: 852: 853: 854: 855: 856: 857: 858: 859: 860: 861: 862: 863: 864: 865: 866: 867: 868: 869: 870: 871: 872: 873: 874: 875: 876: 877: 878: 879: 880: 881: 882: 883: 884: 885: 886: 887: 888: 889: 890: 891: 892: 893: 894: 895: 896: 897: 898: 899: 900: 901: 902: 903: 904: 905: 906: 907: 908: 909: 910: 911: 912: 913: 914: 915: 916: 917: 918: 919: 920: 921: 922: 923: 924: 925: 926: 927: 928: 929: 930: 
<?php
    declare(strict_types=1);
    /**
     *  +------------------------------------------------------------+
     *  | apnscp                                                     |
     *  +------------------------------------------------------------+
     *  | Copyright (c) Apis Networks                                |
     *  +------------------------------------------------------------+
     *  | Licensed under Artistic License 2.0                        |
     *  +------------------------------------------------------------+
     *  | Author: Matt Saladna (msaladna@apisnetworks.com)           |
     *  +------------------------------------------------------------+
     */

    /**
     * Magento management
     *
     * An interface to wp-cli
     *
     * @package core
     */
    class Magento_Module extends \Module\Support\Webapps\Magento
    {
        const APP_NAME = 'Magento';
        // primary domain document root
        const MAGENTO_CLI = '/usr/share/pear/n98-magerun.phar';
        const MAGENTO2_CLI = '/usr/share/pear/n98-magerun2.phar';
        // latest release
        const MAGENTO_CLI_URL = 'https://files.magerun.net/n98-magerun.phar';
        const MAGENTO2_CLI_URL = 'https://files.magerun.net/n98-magerun2.phar';

        const VERSION_CHECK_URL = 'http://mirror.apisnetworks.com/magento';
        const ADMIN_URL = '/admin';

        const DEFAULT_VERSION_LOCK = 'major';

        protected $_aclList = array(
            'min' => array(
                'media',
                'var',
                'downloader',
                'generated',
                'pub/static'
            ),
            'max' => array(
                'media/downloadable',
                'downloader',
                'var/cache',
                'var/session',
                'var/page_cache',
                'var/log',
                'generated/code',
                'generated/metadata',
                'pub/static'
            )
        );

        /**
         * void __construct(void)
         *
         * @ignore
         */
        public function __construct()
        {

            parent::__construct();
        }

        /**
         * Install Magento into a pre-existing location
         *
         * @param string $hostname domain or subdomain to install Magento
         * @param string $path     optional path under hostname
         * @param array  $opts     additional install options
         * @return bool
         */
        public function install(string $hostname, string $path = '', array $opts = array()): bool
        {
            if (!$this->mysql_enabled()) {
                return error("MySQL must be enabled to install %s", ucwords($this->getInternalName()));
            }
            $docroot = $this->getAppRoot($hostname, $path);
            if (!$docroot) {
                return error('failed to install Magento');
            }

            if (!$this->parseInstallOptions($opts, $hostname, $path)) {
                return false;
            }

            if (!is_debug() && !$this->ssl_permitted()) {
                return error('Account requires SSL enabled on account to manage a Magento store');
            }
            if (empty($opts['ssl'])) {
                return error('Magento requires SSL to operate');
            }

            $version = $opts['version'];
            $args = array();
            // cap to 1.x branch
            $vercap = !empty($args['version']) && $args['version'][0] === 2 && $this->mysql_version() < 50600;
            if (!$vercap && !\Opcenter\Php::extensionEnabled('intl')) {
                warn('intl extension missing - Magento capped to 1.x branch');
                $vercap = true;
            }
            if (version_compare(Opcenter\Php::version(), '7.1', '<')) {
                warn('Magento 2.x requires PHP 7.1+. Capping to 1.x');
                $version = $this->getLatestVersion('1');
            } else if (null !== $version) {
                if (!$this->_versionValid($version)) {
                    return error("unknown Magento version `%s'", $version);
                }
                if ($vercap) {
                    warn('capping magento version to 1.x');
                    $version = $this->getLatestVersion('1');
                }
            } else {
                $version = $this->getLatestVersion($vercap ? '1' : null);
            }
            $args['version'] = $version;

            if (version_compare($args['version'], '2', '>=')) {
                if (version_compare($args['version'], '2.2', '>=') && version_compare(Opcenter\Php::version(), '7.1',
                        '<')) {
                    return error('Magento 2.2+ required for PHP 7.1');
                }
                $args['vername'] = 'magento-ce-' . $args['version'];
            } else {
                // 1.x
                $args['vername'] = 'apnscp-mirror-' . $args['version'];
            }
            if (!$this->get_key() && version_compare($args['version'], '2.0', '>=')) {
                return error('Magento Connect key must be setup first, do so via Account > Settings');
            }
            $fqdn = $this->web_normalize_hostname($hostname);
            $args['baseurl'] = 'http://' . $fqdn;
            if ($path) {
                $args['baseurl'] .= '/' . $path;
            }

            $db = $this->_suggestDB($hostname);
            if (!$db) {
                return false;
            }

            $dbuser = $this->_suggestUser($db);
            if (!$dbuser) {
                return false;
            }
            $dbpass = $this->suggestPassword();
            if (!parent::setupDatabase(['db' => $db, 'user' => $dbuser, 'password' => $dbpass])) {
                return false;
            }

            $args['dbuser'] = $dbuser;
            $args['dbpass'] = $dbpass;
            $args['dbname'] = $db;
            $args['dbhost'] = 'localhost';
            $args['docroot'] = $docroot;
            $magerunver = 1;
            if (version_compare($args['version'], '2.0', '>=')) {
                $magerunver = 2;
            }

            // copy custom config to user
            $magerunconf = $this->_copyMagerunConfig($magerunver);

            $cmd = 'install -n --installationFolder=%(docroot)s --dbHost=%(dbhost)s --baseUrl=%(baseurl)s ' .
                '--dbUser=%(dbuser)s --dbPass=%(dbpass)s --installSampleData=no --dbName=%(dbname)s --magentoVersionByName=' .
                '%(vername)s';
            $ret = $this->_exec($docroot, $cmd, $args, $magerunver);
            if ($magerunconf && $this->file_exists($magerunconf)) {
                $this->file_delete($magerunconf);
            }
            if (!$ret['success']) {
                info('removing temporary files');
                $this->file_delete($docroot, true);
                $this->sql_delete_mysql_database($db);
                $this->sql_delete_mysql_user($dbuser, 'localhost');
                if (false !== strpos($ret['stderr'], 'to locate Magento version')) {
                    return error("failed to install magento: unknown Magento version `%s'",
                        $args['version']
                    );
                }

                return error('failed to install magento: %s', coalesce($ret['stderr'], $ret['stdout']));
            }
            /** post install fixup */
            // we install with some stupid defaults, change them
            $ret = $this->_exec($docroot, 'admin:user:delete -f admin');
            if (!$ret['success']) {
                warn('failed to delete placeholder admin');
            }
            $this->_exec($docroot, 'cache:enable');

            $autogenpw = false;
            if (!isset($opts['password'])) {
                $autogenpw = true;
                $opts['password'] = $this->suggestPassword(10);
                info("autogenerated password `%s'", $opts['password']);
            }

            info("setting admin user to `%s'", $opts['username']);

            $args = array(
                'email'    => $opts['email'],
                'user'     => $opts['user'],
                'password' => $opts['password']
            );
            if ($magerunver === 1) {
                $this->_exec($docroot, 'admin:user:create %(user)s %(email)s %(password)s Store Admin', $args);
            } else {
                $this->_exec($docroot, 'admin:user:create --admin-user %(user)s --admin-email %(email)s ' .
                    '--admin-password %(password)s --admin-firstname Store --admin-lastname Admin', $args);
            }
            if (!$ret['success']) {
                info('removing temporary files');
                $this->file_delete($docroot, true);
                $this->sql_delete_mysql_database($db);
                $this->sql_delete_mysql_user($dbuser, 'localhost');

                return error('failed to create admin user: %s', $ret['stdout']);
            }

            // by default, let's only open up ACLs to the bare minimum

            $this->file_touch($docroot . '/.htaccess');
            parent::fixRewriteBase($docroot, $path);
            parent::fixSymlinkTraversal($docroot);
            if ($magerunver === 2) {
                parent::fixRewriteBase($docroot . '/pub', rtrim($path, '/') . '/');
                parent::fixRewriteBase($docroot . '/pub/static', rtrim($path, '/') . '/static');
            }

            // simple confirmation
            $version = $this->get_version($hostname, $path);
            $params = array(
                'version'    => $version,
                'hostname'   => $hostname,
                'path'       => $path,
                'autoupdate' => (bool)$opts['autoupdate'],
            );
            $this->map('add', $docroot, $params);
            $this->fortify($hostname, $path, 'max');
            if ($this->guessMajor($docroot) === 1) {
                $this->file_chmod($docroot . '/app/etc/local.xml', 644);
                $this->_fixModelPHP7($docroot);
            }
            $this->_fixConnectConfig($docroot);
            $url = 'https://' . $fqdn . '/' . $path . '/';
            $args = array(
                'path' => 'web/secure/base_url',
                'url'  => rtrim($url, '/') . '/'
            );
            $this->_exec($docroot, 'config:set %(path)s %(url)s', $args);
            if ($magerunver === 2) {
                $this->_exec($docroot, 'config:set web/secure/use_in_frontend 1');
            }
            $this->_exec($docroot, 'cache:flush');

            /**
             * @todo relink domain/subdomain to pub/
             * setup cron
             * ssl
             */
            if ($magerunver === 1) {
                $this->_fixModelPHP7($docroot);
            }

            if (array_get($opts, 'notify', true)) {
                $msg = 'Hello!' . "\r\n" .
                    'This is a confirmation that Magento has been installed under ' . $docroot .
                    '. You may access Magento via ' . $url . '. Access the administrative ' .
                    'panel at ' . rtrim($url, '/') . self::ADMIN_URL . ' using the following details:' . "\r\n\r\n" .
                    'Username: ' . $opts['user'] . "\r\n" .
                    ($autogenpw ? 'Password: ' . $opts['password'] . "\r\n" : '');
                $msg .= "\r\nWhen installing plugins or themes, you will need to use your " .
                    'control panel password!';
                $hdrs = 'From: ' . Crm_Module::FROM_NAME . ' <' .
                    Crm_Module::FROM_ADDRESS . ">\r\nReply-To: " . Crm_Module::REPLY_ADDRESS;
                Mail::send($opts['email'], 'Magento Installed', $msg, $hdrs);
            }

            if (!$opts['squash']) {
                parent::unsquash($docroot);
            }

            return true;
        }

        protected function getAppRoot(string $hostname, string $path = ''): ?string
        {
            return parent::getAppRoot($hostname, $path);
        }

        /**
         * Requested version is known by magerun
         *
         * @param string $version
         * @return bool
         */
        private function _versionValid($version): bool
        {
            $versions = $this->_getVersions();

            return in_array($version, $versions);
        }

        /**
         * Get all current major versions
         *
         * @return array
         */
        protected function _getVersions(): array
        {
            $key = 'magento.versions';
            $cache = Cache_Super_Global::spawn();
            // @TODO

            if (false !== ($vers = $cache->get($key))) {
                return $vers;
            }

            $req = file_get_contents(self::VERSION_CHECK_URL . '?all');
            if (!$req) {
                return array();
            }
            $req = json_decode($req);
            $cache->set($key, $req, 43200);

            return $req;
        }

        /**
         * Get Magento key for use with Magento Connect
         *
         * @return mixed
         */
        public function get_key()
        {
            $file = $this->_keyAuthFile();
            if (!$this->file_exists($file)) {
                return null;
            }
            $contents = json_decode($this->file_get_file_contents($file), true);
            if (!isset($contents['http-basic']['repo.magento.com'])) {
                return null;
            }
            $tmp = $contents['http-basic']['repo.magento.com'];

            return array($tmp['username'], $tmp['password']);
        }

        private function _keyAuthFile()
        {
            $home = $this->user_get_home();

            return $home . '/.composer/auth.json';
        }

        private function _copyMagerunConfig($version = 1)
        {
            $f = resource_path('storehouse/magento/magento' . $version . '.yaml');
            if (!file_exists($f)) {
                return error('failed to locate magento download YAML');
            }
            $filename = '.n98-magerun' . ($version > 1 ? $version : '') . '.yaml';
            copy($f, $this->domain_fs_path() . '/tmp/' . $filename);
            $dest = $this->user_get_home() . '/' . $filename;
            $this->file_copy('/tmp/' . $filename, $dest);
            unlink($this->domain_fs_path() . '/tmp/' . $filename);

            return $dest;
        }

        private function _exec(string $path = null, $cmd, array $args = array(), $ver = null)
        {

            if ($path && null === $ver) {
                $ver = $this->guessMajor($path);
            }

            $magerun = $ver === 1 ? self::MAGENTO_CLI : self::MAGENTO2_CLI;
            // client may override tz, propagate to bin
            $tz = date_default_timezone_get();
            $cli = 'php -d pdo_mysql.default_socket=' . escapeshellarg(ini_get('mysqli.default_socket')) .
                ' -d date.timezone=' . $tz . ' -d memory_limit=192m ' . $magerun . '';

            if (!is_array($args)) {
                $args = func_get_args();
                array_shift($args);
            }
            $user = $this->username;
            if ($path) {
                $cli = 'cd %(path)s && ' . $cli;
                $args['path'] = $path;
                $stat = $this->file_stat($path);
                $user = !empty($stat['owner']) && $stat['uid'] >= \a23r::get_class_from_module('user')::MIN_UID ?
                    $stat['owner'] : $this->username;
            }
            $cmd = $cli . ' ' . $cmd;
            $ret = $this->pman_run($cmd, $args, null, ['user' => $user]);
            if (!$ret['success'] && $ret['stderr']) {
                $ret['stderr'] = $ret['stdout'];
            }

            return $ret;
        }

        /**
         * Get installed version
         *
         * @param string $hostname
         * @param string $path
         * @return null|string version number
         */
        public function get_version(string $hostname, string $path = ''): ?string
        {
            if (!$this->valid($hostname, $path)) {
                return null;
            }

            $approot = $this->getAppRoot($hostname, $path);

            $ret = $this->_exec($approot, 'sys:info --format=json', []);

            if (!$ret['success']) {
                return null;
            }
            $info = json_decode($ret['stdout'], true);
            foreach ($info as $el) {
                if (strtolower($el['name']) === 'version') {
                    return $el['value'];
                }
            }

            return null;

        }

        /**
         * Location is a valid WP install
         *
         * @param string $hostname or $docroot
         * @param string $path
         * @return bool
         */
        public function valid(string $hostname, string $path = ''): bool
        {
            if ($hostname[0] == '/') {
                $docroot = $hostname;
            } else {
                $docroot = $this->getAppRoot($hostname, $path);
                if (!$docroot) {
                    return false;
                }
            }

            return $this->guessMajor($docroot) !== null;
        }

        private function _fixModelPHP7($docroot)
        {
            // PHP7 fix
            // @see https://www.atwix.com/magento/magento-and-php-7/
            if (version_compare(platform_version(), '6.5', '<')) {
                return true;
            }
            $f = $docroot . '/app/code/core/Mage/Core/Model/Layout.php';
            $contents = $this->file_get_file_contents($f);
            $old = '$out .= $this->getBlock($callback[0])->$callback[1]();';
            $new = '$out .= $this->getBlock($callback[0])->{$callback[1]}();';
            $replacement = str_replace($old, $new, $contents);

            return $this->file_put_file_contents($f, $replacement);
        }

        private function _fixConnectConfig($docroot)
        {
            $file = $docroot . '/downloader/connect.cfg';
            $preamble = '::ConnectConfig::v::1.0::';
            if ($this->file_exists($file)) {
                $raw = $this->file_get_file_contents($file);
                if (!preg_match('/^((?:::[[[:alnum:].]*]*)+?)([sibNaO]:.*)$/mi', $raw, $preamble)) {
                    return error('cannot set Magento Connect FTP login information, ' .
                        'config is malformed: %s', $raw);
                }
                $contents = \Util_PHP::unserialize($preamble[2]);
                $preamble = $preamble[1];
            } else {
                $contents = array();
            }
            $contents['remote_config'] = 'ftp://' . $this->username . '@' . $this->domain . ':debug@localhost';
            $contents['downloader_path'] = $this->domain_fs_path() . $docroot . '/downloader';
            $contents['magento_root'] = $this->domain_fs_path() . $docroot;
            $newdata = $preamble . serialize($contents);

            return $this->file_put_file_contents($file, $newdata);
        }

        /**
         * Install and activate plugin
         *
         * @param string $hostname domain or subdomain of wp install
         * @param string $path     optional path component of wp install
         * @param string $plugin   plugin name
         * @param string $version  optional plugin version
         * @return bool
         */
        public function install_plugin(
            string $hostname,
            string $path = '',
            string $plugin,
            string $version = 'stable'
        ): bool {
            $docroot = $this->getAppRoot($hostname, $path);
            if (!$docroot) {
                return error('invalid WP location');
            }

            return info('not implemented');
        }

        /**
         * Uninstall a plugin
         *
         * @param string      $hostname
         * @param string      $path
         * @param string      $plugin plugin name
         * @param bool|string $force  delete even if plugin activated
         * @return bool
         */
        public function uninstall_plugin(string $hostname, string $path = '', string $plugin, bool $force = false): bool
        {
            $docroot = $this->getAppRoot($hostname, $path);
            if (!$docroot) {
                return error('invalid Magento location');
            }

            return info('not implemented');
        }

        /**
         * Recovery mode to disable all plugins
         *
         * @param string $hostname subdomain or domain of WP
         * @param string $path     optional path
         * @return bool
         */
        public function disable_all_plugins(string $hostname, string $path = ''): bool
        {
            $docroot = $this->getAppRoot($hostname, $path);
            if (!$docroot) {
                return error('failed to determine path');
            }

            return info('not implemented');
        }

        /**
         * Uninstall WP from a location
         *
         * @param        $hostname
         * @param string $path
         * @param string $delete "all", "db", or "files"
         * @return bool
         */
        public function uninstall(string $hostname, string $path = '', string $delete = 'all'): bool
        {
            /**
             * @todo delete app whose docroot is one level below app root
             */
            return parent::uninstall($hostname, $path, $delete);
        }

        /**
         * Get database configuration for a blog
         *
         * @param string $hostname domain or subdomain of wp blog
         * @param string $path     optional path
         * @return array|bool
         */
        public function db_config(string $hostname, string $path = '')
        {
            $docroot = $this->getAppRoot($hostname, $path);
            if (!$docroot) {
                return error('failed to determine Magento');
            }
            if ($this->guessMajor($docroot) === 1) {
                $file = $this->domain_fs_path() . $docroot . '/app/etc/local.xml';
                $code = simplexml_load_string(file_get_contents($file, false), 'SimpleXMLElement', LIBXML_NOCDATA);
                $conn = $code->xpath('//connection');
                if (!$conn) {
                    return error("failed to obtain Magento configuration for `%s'", $docroot);
                }
                $conn = array_pop($conn);

                return array(
                    'user'     => (string)$conn->username,
                    'host'     => (string)$conn->host,
                    'db'       => (string)$conn->dbname,
                    'password' => (string)$conn->password,
                    'prefix'   => (string)$conn->prefix
                );
            }
            $file = \dirname($docroot) . '/app/etc/env.php';
            $fstfile = $this->domain_fs_path() . $file;

            if (!file_exists($fstfile)) {
                return error("failed to obtain Magento configuration for `%s'", $docroot);
            }

            $ret = $this->pman_run('cd  %s && php -r %s', [
                \dirname($docroot),
                'echo json_encode(include "' . $file . '", true);'
            ], [], ['user' => $this->getDocrootUser($docroot)]);

            if (!$ret['success']) {
                warn("Failed to parse `%s'", $file);
                return [];
            }

            $conn = json_decode($ret['stdout'], true);
            return [
                'user'     => array_get($conn, 'connection.default.username'),
                'host'     => array_get($conn, 'connection.default.host'),
                'db'       => array_get($conn, 'connection.default.dbname'),
                'password' => array_get($conn, 'connection.default.password'),
                'prefix'   => array_get($conn, 'table_prefix'),
            ];
        }

        /**
         * Check if version is latest or get latest version
         *
         * @param string|null $version
         * @param string|null $branchcomp
         * @return int|string
         */
        public function is_current(string $version = null, string $branchcomp = null)
        {
            return parent::is_current($version, $branchcomp);
        }

        /**
         * Change Magento admin credentials
         *
         * $fields is a hash whose indices match password
         *
         * @param string $hostname
         * @param string $path
         * @param array  $fields
         * @return bool
         */
        public function change_admin(string $hostname, string $path = '', array $fields): bool
        {
            $approot = $this->getAppRoot($hostname, $path);
            if (!$approot) {
                return warn('failed to change administrator information');
            }
            $admin = $this->get_admin($hostname, $path);

            if (!$admin) {
                return error('cannot determine admin of Magento install');
            }
            $args = array(
                'username' => $admin
            );
            if (isset($fields['password'])) {
                $args['password'] = $fields['password'];
                $ret = $this->_exec($approot, 'admin:user:change-password %(username)s %(password)s', $args);
                if (!$ret['success']) {
                    return error("failed to change admin password, `%s'", $ret['stderr']);
                }
            } else {
                return warn('no other fields besides password implemented');
            }

            return true;

        }

        /**
         * Get the primary admin for a Magento instance
         *
         * @param string      $hostname
         * @param string|null $path
         * @return null|string admin or false on failure
         */
        public function get_admin(string $hostname, string $path = ''): ?string
        {
            $approot = $this->getAppRoot($hostname, $path);
            $ret = $this->_exec($approot, 'admin:user:list --format=json');
            if (!$ret['success']) {
                warn('failed to enumerate administrative users');

                return null;
            }
            $users = json_decode($ret['stdout'], true);
            if (!$users) {
                error('no administrative users found');

                return null;
            }
            foreach ($users as $user) {
                if ($user['status'] === 'active') {
                    break;
                }
            }

            return $user['username'];
        }

        /**
         * Web application supports fortification
         *
         * @param string|null $mode optional mode (min, max)
         * @return bool
         */
        public function has_fortification(string $mode = null): bool
        {
            return parent::has_fortification($mode);
        }

        /**
         * @param string $hostname
         * @param string $path
         * @param string $mode
         * @return bool
         */
        public function fortify(string $hostname, string $path = '', string $mode = 'max'): bool
        {
            return parent::fortify($hostname, $path, $mode);
        }

        /**
         * Relax permissions to allow write-access
         *
         * @param string $hostname
         * @param string $path
         * @return bool
         */
        public function unfortify(string $hostname, string $path = ''): bool
        {
            return parent::unfortify($hostname, $path);
        }

        /**
         * Update core, plugins, and themes atomically
         *
         * @param string $hostname subdomain or domain
         * @param string $path     optional path under hostname
         * @param string $version
         * @return bool
         */
        public function update_all(string $hostname, string $path = '', string $version = null): bool
        {
            $ret = ($this->update($hostname, $path, $version) && $this->update_plugins($hostname, $path) &&
                    $this->update_themes($hostname, $path)) || error('failed to update all components');

            parent::setInfo($this->getAppRoot($hostname, $path), [
                'version' => $this->get_version($hostname, $path),
                'failed'  => !$ret
            ]);

            return $ret;
        }

        /**
         * Update Magento to latest version
         *
         * @param string $hostname domain or subdomain under which WP is installed
         * @param string $path     optional subdirectory
         * @param string $version
         * @return bool
         */
        public function update(string $hostname, string $path = '', string $version = null): bool
        {
            $docroot = $this->getAppRoot($hostname, $path);
            if (!$docroot) {
                return error('update failed');
            }

            return error('Magento updates not supported yet');
        }

        /**
         * Update Magento plugins
         *
         * @param string $hostname domain or subdomain
         * @param string $path     optional path within host
         * @param array  $plugins
         * @return bool
         */
        public function update_plugins(string $hostname, string $path = '', array $plugins = array()): bool
        {
            $docroot = $this->getAppRoot($hostname, $path);
            if (!$docroot) {
                return error('update failed');
            }

            return info('to-do');
        }

        /**
         * Update Magento themes
         *
         * @param string $hostname subdomain or domain
         * @param string $path     optional path under hostname
         * @param array  $themes
         * @return bool
         */
        public function update_themes(string $hostname, string $path = '', array $themes = array()): bool
        {
            $docroot = $this->getAppRoot($hostname, $path);
            if (!$docroot) {
                return error('update failed');
            }

            return info('not implemented');
        }

        public function delete_key()
        {
            $file = $this->_keyAuthFile();
            if (!$this->file_exists($file)) {
                return error("failed to get Magento key file `%s'", $file);
            }
            $contents = json_decode($this->file_get_file_contents($file), true);
            if (!isset($contents['http-basic'])) {
                return true;
            }
            unset($contents['http-basic']['repo.magento.com']);

            return $this->file_put_file_contents($file, json_encode($contents));
        }

        public function set_key($publickey, $privatekey)
        {
            if (!ctype_alnum($publickey) || !ctype_alnum($privatekey)) {
                return error('invalid public and/or private magento key');
            }
            $file = $this->_keyAuthFile();
            $contents = array();
            if ($this->file_exists($file)) {
                $contents = $this->file_get_file_contents($file);
                $contents = json_decode($contents, true);
            } else if (!$this->file_exists(dirname($file))) {
                $this->file_create_directory(dirname($file), 0755, true);
            }
            if (!isset($contents['http-basic'])) {
                $contents['http-basic'] = array();
            }
            $contents['http-basic']['repo.magento.com'] = array(
                'username' => $publickey,
                'password' => $privatekey
            );

            return $this->file_put_file_contents($file, json_encode($contents), true);
        }

        /**
         * Install wp-cli if necessary
         *
         * @return bool
         * @throws \Exception
         */
        public function _housekeeping(): bool
        {
            $clis = array(
                self::MAGENTO_CLI  => self::MAGENTO_CLI_URL,
                self::MAGENTO2_CLI => self::MAGENTO2_CLI_URL
            );
            foreach ($clis as $cli => $url) {
                if (!file_exists($cli)) {
                    $res = Util_HTTP::download($url, $cli);
                    if (!$res) {
                        return error('failed to install magento cli');
                    }
                    info('downloaded %s', basename($cli));
                }

                $local = $this->service_template_path('siteinfo') . '/' . $cli;
                if (!file_exists($local)) {
                    return copy($cli, $local);
                }
            }

            return true;
        }

        /**
         * Get plugin status
         *
         * @param string      $hostname domain or subdomain
         * @param null|string $path     optional path under hostname
         * @param null|string $plugin   specific plugin to query
         * @return array|bool
         */
        public function plugin_status(string $hostname, string $path = '', string $plugin = null)
        {
            return error("not implemented yet");
        }

        /**
         * Get all available versions
         *
         * @return array
         */
        public function get_versions(): array
        {
            return $this->_getVersions();
        }

        public function next_version(string $version, string $maximalbranch = '99999999.99999999.99999999'): ?string
        {
            return parent::next_version($version, $maximalbranch);
        }

        public function theme_status(string $hostname, string $path = '', string $theme = null)
        {
            return parent::theme_status($hostname, $path, $theme); // TODO: Change the autogenerated stub
        }

        public function install_theme(string $hostname, string $path = '', string $theme, string $version = null): bool
        {
            return parent::install_theme($hostname, $path, $theme, $version);
        }
    }