public function addonsAction()
 {
     $addons = array();
     $path = MB_ROOT . 'addons/';
     if (is_dir($path)) {
         if ($handle = opendir($path)) {
             while (false !== ($addonpath = readdir($handle))) {
                 if ($addonpath != '.' && $addonpath != '..') {
                     $define = Addon::getAddon($addonpath, true);
                     if (!is_error($define)) {
                         $addons[] = $define;
                     }
                 }
             }
         }
     }
     if (IS_POST) {
         $a = I('post.addon');
         $addons = coll_key($addons, 'name');
         $addon = $addons[$a];
         if (!empty($addon)) {
             $zip = new \ZipArchive();
             $tmpFile = ADDON_CURRENT_PATH . 'Data/package.zip';
             @unlink($tmpFile);
             $zip->open($tmpFile, \ZipArchive::CREATE);
             $root = MB_ROOT . "addons/{$a}";
             $files = File::tree($root);
             foreach ($files as $file) {
                 $local = substr($file, strlen($root));
                 if (substr($local, -4) == '.php') {
                     if (I('post.trim') != '') {
                         $content = $this->trimComments($file, I('post.license'));
                     }
                     $zip->addFromString("{$a}{$local}", $content);
                 } else {
                     $zip->addFile($file, "{$a}{$local}");
                 }
             }
             $zip->close();
             $version = MB_VERSION;
             $filename = "{$a}-v{$addon['version']} (for MB-{$version})";
             header('content-type: application/zip');
             header('content-disposition: attachment; filename="' . $filename . '.zip"');
             readfile($tmpFile);
             @unlink($tmpFile);
         }
     }
     $this->assign('addons', $addons);
     $this->display('addons');
 }
 private function doInstall($addon)
 {
     $define = Addon::getAddon($addon, true);
     if (is_error($define)) {
         $this->error($define['message']);
     } else {
         $rec = coll_elements(array('name', 'type', 'title', 'version', 'description', 'author', 'url'), $define, '');
         $m = new Model();
         $ret = $m->table('__EX_ADDONS__')->data($rec)->add();
         if (!empty($ret)) {
             Addon::autoload();
             $class = "Addon\\{$addon}\\Api\\Application";
             if (class_exists($class)) {
                 $instance = new $class();
                 if (!empty($instance)) {
                     $instance->addon = new Addon($addon);
                     if (method_exists($instance, 'install')) {
                         $ret = $instance->install();
                     }
                 }
             }
             if (is_error($ret)) {
                 $this->error('扩展未能安装成功, 请完全卸载后重试, 或者联系扩展开发商. 扩展提供的详细错误信息为: ' . $ret['message']);
             } else {
                 $this->success('扩展安装成功');
             }
         }
         exit;
     }
 }