コード例 #1
0
 public function updateApp($app_id, $version = null)
 {
     $app = Mysql::getInstance()->from('apps')->where(array('id' => $app_id))->get()->first();
     if (empty($app)) {
         return false;
     }
     if ($version === null) {
         return $this->installApp($app_id);
     }
     $tmp_file = '/tmp/' . uniqid('app_') . '.zip';
     $repo = new GitHub($app['url']);
     $zip_url = 'https://github.com/' . $repo->getOwner() . '/' . $repo->getRepository() . '/archive/' . $version . '.zip';
     file_put_contents($tmp_file, fopen($zip_url, 'r', false, stream_context_create(array('http' => array('header' => "User-Agent: stalker_portal\r\n")))));
     if (empty($app['alias'])) {
         $app['alias'] = self::safeFilename($app['name']);
     }
     $path = PROJECT_PATH . '/../../' . Config::getSafe('apps_path', 'stalker_apps/') . $app['alias'];
     umask(0);
     if (!is_dir($path)) {
         mkdir($path, 0755, true);
     }
     $archive = new ZipArchive();
     if ($archive->open($tmp_file) === true) {
         $entry = $archive->getNameIndex(0);
         $dir = substr($entry, 0, strpos($entry, '/'));
         $result = $archive->extractTo($path);
         $archive->close();
         rename($path . '/' . $dir, $path . '/' . $version);
     } else {
         return false;
     }
     unlink($tmp_file);
     if ($result) {
         $update_data = array('current_version' => $version);
         $update_data['alias'] = $app['alias'];
         if (!isset($repo)) {
             $repo = new GitHub($app['url']);
         }
         $info = $repo->getFileContent('package.json');
         if (!empty($info['description'])) {
             $update_data['description'] = $info['description'];
         }
         if (!empty($info['config']['icons'])) {
             $update_data['icons'] = $info['config']['icons'];
         } else {
             $update_data['icons'] = 'icons';
         }
         if (!empty($info['config']['backgroundColor'])) {
             $update_data['icon_color'] = $info['config']['backgroundColor'];
         }
         Mysql::getInstance()->update('apps', $update_data, array('id' => $app_id));
     }
     return $result;
 }