public function insertApp(AppLauncher $app)
 {
     $launcherPath = self::APPS_DIR . '/' . $app['name'] . '.xml';
     $xml = new TranslatedDOMDocument('1.0');
     $root = $xml->createElement('shortcut');
     $xml->appendChild($root);
     $appData = $app->toArray();
     foreach ($appData as $name => $value) {
         $attrEl = $xml->createElement('attribute');
         $root->appendChild($attrEl);
         $nameAttr = $xml->createAttribute('name');
         $nameAttr->appendChild($xml->createTextNode($name));
         $attrEl->appendChild($nameAttr);
         $valueAttr = $xml->createAttribute('value');
         $valueAttr->appendChild($xml->createTextNode($value));
         $attrEl->appendChild($valueAttr);
     }
     $this->dao->write($launcherPath, $xml->saveXML());
 }
 public function executeInstall($manifestUrl, $appData)
 {
     $localRepoManager = $this->managers()->getManagerOf('localRepository');
     $fileManager = $this->managers()->getManagerOf('file');
     $launcherManager = $this->managers()->getManagerOf('launcher');
     if ($appData['app_type'] != 'hosted') {
         throw new \RuntimeException('Packaged apps installation is not currently supported', 405);
     }
     $webappDir = '/usr/lib/firefox-marketplace/webapps/' . $appData['slug'];
     //Download icons
     $isIconDownloaded = false;
     $iconName = 'firefox-marketplace-' . $appData['slug'];
     if (function_exists('curl_init')) {
         $ch = curl_init();
     }
     foreach ($appData['icons'] as $iconSize => $iconUrl) {
         $iconUrlPath = parse_url($iconUrl, PHP_URL_PATH);
         if ($fileManager->extension($iconUrlPath) != 'png') {
             continue;
         }
         $iconDestPath = '/usr/share/icons/applications/' . (int) $iconSize . '/' . $iconName . '.png';
         if (isset($ch)) {
             curl_setopt($ch, CURLOPT_HEADER, false);
             curl_setopt($ch, CURLOPT_URL, $iconUrl);
             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
             $output = curl_exec($ch);
         } else {
             $output = file_get_contents($iconUrl);
         }
         if ($output === false) {
             continue;
         }
         $fileManager->createFile($iconDestPath, true);
         $fileManager->write($iconDestPath, $output);
         $isIconDownloaded = true;
     }
     if (isset($ch)) {
         curl_close($ch);
     }
     //Save app data
     $manifestData = $this->executeGetManifest($manifestUrl);
     $manifestContent = $manifestData['manifest'];
     $manifestUrlParts = parse_url($manifestUrl);
     $manifestOrigin = $manifestUrlParts['scheme'] . '://' . $manifestUrlParts['host'];
     $appInstallDataDest = $webappDir . '/webapp.json';
     $appInstallData = array('app' => $appData, 'manifest' => json_decode($manifestContent, true), 'origin' => $manifestOrigin, 'manifestURL' => $manifestUrl, 'manifestHash' => sha1($manifestContent));
     if ($isIconDownloaded) {
         $appInstallData['localIcon'] = 'applications/' . $iconName;
     }
     $fileManager->createFile($appInstallDataDest, true);
     $fileManager->write($appInstallDataDest, json_encode($appInstallData));
     //Create launcher
     $launcher = new AppLauncher(array('name' => 'firefox-marketplace.' . $appData['slug'], 'title' => $appData['title'], 'description' => $appData['description'], 'command' => 'firefox-marketplace run ' . $appData['slug']));
     if ($isIconDownloaded) {
         $launcher->setIcon('applications/' . $iconName);
     }
     $launcherManager->insertApp($launcher);
     //Register new app
     $pkg = new PackageMetadata(array('name' => $appData['slug'], 'title' => $appData['title'], 'version' => $appData['current_version'], 'description' => $appData['description'], 'url' => !empty($appData['support_url']) ? $appData['support_url'] : '', 'maintainer' => $appData['author'], 'updateDate' => strtotime($appData['created']), 'categories' => $appData['categories'], 'icons' => $appData['icons']));
     $localRepoManager->insert($pkg);
     return $pkg;
 }
 public function executeInstall($manifestUrl, $appData)
 {
     $localRepoManager = $this->managers()->getManagerOf('localRepository');
     $fileManager = $this->managers()->getManagerOf('file');
     $launcherManager = $this->managers()->getManagerOf('launcher');
     if ($appData['app_type'] != 'hosted' && $appData['app_type'] != 'packaged') {
         throw new \RuntimeException('App type "' . $appData['app_type'] . '" not supported', 405);
     }
     $webappDir = '/usr/lib/firefox-marketplace/webapps/' . $appData['slug'];
     //Download ZIP if packaged app
     if ($appData['app_type'] == 'packaged') {
         $pkgUrl = $appData['package_path'];
         $tmpPkgPath = $fileManager->tmpfile();
         if (copy($pkgUrl, $fileManager->toInternalPath($tmpPkgPath)) === false) {
             throw new \RuntimeException('Error while copying app package from "' . $pkgUrl . '" to "' . $tmpPkgPath . '"');
         }
         if (!class_exists('\\ZipArchive')) {
             throw new \RuntimeException('Installing packaged apps is not available on this system', 501);
         }
         $zip = new \ZipArchive();
         if ($zip->open($fileManager->toInternalPath($tmpPkgPath)) === false) {
             throw new \RuntimeException('Could not open ZIP package "' . $tmpPkgPath . '"');
         }
         if ($zip->extractTo($fileManager->toInternalPath($webappDir)) === false) {
             throw new \RuntimeException('Could not extract ZIP package "' . $tmpPkgPath . '"');
         }
         $zip->close();
         $fileManager->delete($tmpPkgPath);
     }
     //Download icons
     $isIconDownloaded = false;
     $iconName = 'firefox-marketplace-' . $appData['slug'];
     if (function_exists('curl_init')) {
         $ch = curl_init();
     }
     foreach ($appData['icons'] as $iconSize => $iconUrl) {
         $iconUrlPath = parse_url($iconUrl, PHP_URL_PATH);
         if ($fileManager->extension($iconUrlPath) != 'png') {
             continue;
         }
         if ((int) $iconSize > 512) {
             //Do not download extra large icons
             continue;
         }
         $iconDestPath = '/usr/share/icons/applications/' . (int) $iconSize . '/' . $iconName . '.png';
         if (isset($ch)) {
             curl_setopt($ch, CURLOPT_HEADER, false);
             curl_setopt($ch, CURLOPT_URL, $iconUrl);
             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
             $output = curl_exec($ch);
         } else {
             $output = file_get_contents($iconUrl);
         }
         if ($output === false) {
             //Something went wrong
             continue;
         }
         $fileManager->createFile($iconDestPath, true);
         $fileManager->write($iconDestPath, $output);
         $isIconDownloaded = true;
     }
     if (isset($ch)) {
         curl_close($ch);
     }
     //Save app data
     $manifestData = $this->executeGetManifest($manifestUrl);
     $manifestContent = $manifestData['manifest'];
     $manifestUrlParts = parse_url($manifestUrl);
     $manifestOrigin = $manifestUrlParts['scheme'] . '://' . $manifestUrlParts['host'];
     $appInstallDataDest = $webappDir . '/webapp.json';
     $appInstallData = array('app' => $appData, 'manifest' => json_decode($manifestContent, true), 'origin' => $manifestOrigin, 'manifestURL' => $manifestUrl, 'manifestHash' => sha1($manifestContent));
     if ($isIconDownloaded) {
         $appInstallData['localIcon'] = 'applications/' . $iconName;
     }
     $fileManager->createFile($appInstallDataDest, true);
     $fileManager->write($appInstallDataDest, json_encode($appInstallData));
     //Create launcher
     $launcher = new AppLauncher(array('name' => 'firefox-marketplace.' . $appData['slug'], 'title' => $appData['title'], 'description' => $appData['description'], 'command' => 'firefox-marketplace run ' . $appData['slug']));
     if ($isIconDownloaded) {
         $launcher->setIcon('applications/' . $iconName);
     }
     $launcherManager->insertApp($launcher);
     //Register new app
     $pkg = new PackageMetadata(array('name' => $appData['slug'], 'title' => $appData['title'], 'version' => $appData['current_version'], 'description' => $appData['description'], 'url' => !empty($appData['support_url']) ? reset($appData['support_url']) : '', 'maintainer' => $appData['author'], 'updateDate' => strtotime($appData['created']), 'categories' => $appData['categories'], 'icons' => $appData['icons']));
     $localRepoManager->insert($pkg);
     return $pkg;
 }