Example #1
0
 /**
  * This is a temporary way to add and install new cards via Github.
  *
  * Once CiiMS.org is setup, this functionality will be deprecated in favor of a download from CiiMS.org
  * @return boolean   If the download, extraction, and install was successful
  */
 public function actionAddCard()
 {
     // Only proceed on POST
     if (Cii::get($_POST, 'Card') !== NULL) {
         $repository = Cii::get($_POST['Card'], 'new');
         if ($repository == NULL) {
             return false;
         }
         $repoInfo = explode('/', $repository);
         // Download the Card information from Github via cURL
         $curl = curl_init();
         curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => 'https://raw.github.com/' . $repository . '/master/card.json'));
         $json = CJSON::decode(curl_exec($curl));
         // If we have an invalid repo - abort
         if ($json == NULL) {
             throw new CHttpException(400, Yii::t('Dashboard.main', 'Unable to find valid card at that location.'));
         }
         $config = new Configuration();
         $uuid = $config->generateUniqueId();
         $config->key = 'dashboard_card_' . $uuid;
         $config->value = CJSON::encode(array('name' => Cii::get(Cii::get($json, 'name'), 'displayName'), 'class' => Cii::get(Cii::get($json, 'name'), 'name'), 'path' => 'application.runtime.cards.' . $uuid . '.' . $repoInfo[1] . '-master', 'folderName' => $uuid));
         // Determine the runtime directory
         $runtimeDirectory = Yii::getPathOfAlias('application.runtime');
         $downloadPath = $runtimeDirectory . DIRECTORY_SEPARATOR . 'cards' . DIRECTORY_SEPARATOR . $uuid . '.zip';
         if (!is_writable($runtimeDirectory)) {
             throw new CHttpException(500, Yii::t('Dashboard.main', 'Runtime directory is not writable'));
         }
         $targetFile = fopen($downloadPath, 'w');
         // Initiate the CURL request
         $ch = curl_init('https://github.com/' . $repository . '/archive/master.zip');
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         curl_setopt($ch, CURLOPT_FILE, $targetFile);
         curl_exec($ch);
         // Extract the file
         $zip = new ZipArchive();
         $res = $zip->open($downloadPath);
         // If we can open the file
         if ($res === true) {
             // Extract it to the appropriate location
             $extraction = $zip->extractTo(str_replace('.zip', '', $downloadPath));
             // If we can extract it
             if ($extraction) {
                 // Save the config in the database
                 $config->save();
                 // Delete the zip file
                 unlink($downloadPath);
                 // Flush the cache
                 Yii::app()->cache->delete('dashboard_cards_available');
                 Yii::app()->cache->delete('cards_in_category');
                 header('Content-Type: application/json');
                 // Output the json so we can display pretty stuff in the main view
                 echo $config->value;
                 // And return true
                 return true;
             }
         }
     }
     return false;
 }