Example #1
0
 public function downloadPlugin($name, $url, $signature)
 {
     if (is_dir(ipFile("Plugin/{$name}/"))) {
         Service::deactivatePlugin($name);
         Helper::removeDir(ipFile("Plugin/{$name}/"));
     }
     //download plugin
     $net = new \Ip\Internal\NetHelper();
     $pluginTempFilename = $net->downloadFile($url, ipFile('file/secure/tmp/'), $name . '.zip');
     if (!$pluginTempFilename) {
         throw new \Ip\Exception('Plugin file download failed.');
     }
     $archivePath = ipFile('file/secure/tmp/' . $pluginTempFilename);
     //check signature
     $fileMd5 = md5_file($archivePath);
     $rsa = new \Crypt_RSA();
     $rsa->loadKey($this->publicKey);
     $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
     $verified = $rsa->verify($fileMd5, base64_decode($signature));
     if (!$verified) {
         throw new \Ip\Exception('Plugin signature verification failed.');
     }
     //extract
     $secureTmpDir = ipFile('file/secure/tmp/');
     $tmpExtractedDir = \Ip\Internal\File\Functions::genUnoccupiedName($name, $secureTmpDir);
     \Ip\Internal\Helper\Zip::extract($secureTmpDir . $pluginTempFilename, $secureTmpDir . $tmpExtractedDir);
     unlink($archivePath);
     //install
     $extractedDir = $this->getFirstDir($secureTmpDir . $tmpExtractedDir);
     $installDir = Model::pluginInstallDir();
     $newPluginDir = \Ip\Internal\File\Functions::genUnoccupiedName($name, $installDir);
     rename($secureTmpDir . $tmpExtractedDir . '/' . $extractedDir, $installDir . $newPluginDir);
     Service::activatePlugin($name);
 }
Example #2
0
 public function downloadTheme($name, $url, $signature)
 {
     $model = Model::instance();
     //download theme
     $net = new \Ip\Internal\NetHelper();
     $themeTempFilename = $net->downloadFile($url, ipFile('file/secure/tmp/'), $name . '.zip');
     if (!$themeTempFilename) {
         throw new \Ip\Exception('Theme file download failed.');
     }
     $archivePath = ipFile('file/secure/tmp/' . $themeTempFilename);
     //check signature
     $fileMd5 = md5_file($archivePath);
     $rsa = new \Crypt_RSA();
     $rsa->loadKey($this->publicKey);
     $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
     $verified = $rsa->verify($fileMd5, base64_decode($signature));
     if (!$verified) {
         throw new \Ip\Exception('Theme signature verification failed.');
     }
     //extract
     $helper = Helper::instance();
     $secureTmpDir = ipFile('file/secure/tmp/');
     $tmpExtractedDir = \Ip\Internal\File\Functions::genUnoccupiedName($name, $secureTmpDir);
     \Ip\Internal\Helper\Zip::extract($secureTmpDir . $themeTempFilename, $secureTmpDir . $tmpExtractedDir);
     unlink($archivePath);
     //install
     $extractedDir = $helper->getFirstDir($secureTmpDir . $tmpExtractedDir);
     $installDir = $model->getThemeInstallDir();
     $newThemeDir = \Ip\Internal\File\Functions::genUnoccupiedName($name, $installDir);
     rename($secureTmpDir . $tmpExtractedDir . '/' . $extractedDir, $installDir . $newThemeDir);
 }
Example #3
0
 /**
  * @param string $url
  * @return string
  */
 protected function downloadFile($url, $title)
 {
     //download image to TMP dir and get $resultFilename
     $net = new \Ip\Internal\NetHelper();
     $tmpFilename = $net->downloadFile($url, ipFile('file/tmp/'), 'bigstock_' . time());
     if (!$tmpFilename) {
         return null;
     }
     //find out file mime type to know required extension
     try {
         $mime = \Ip\Internal\File\Functions::getMimeType(ipFile('file/tmp/' . $tmpFilename));
         switch ($mime) {
             case 'image/png':
                 $ext = '.jpg';
                 break;
             case 'image/gif':
                 $ext = '.gif';
                 break;
             case 'image/bmp':
                 $ext = '.bmp';
                 break;
             case 'image/pjpeg':
             case 'image/jpeg':
             default:
                 $ext = '.jpg';
                 break;
         }
     } catch (\Ip\PhpException $e) {
         $ext = '.jpg';
     }
     //get real nice new file name
     $title = \Ip\Internal\File\Functions::cleanupFileName($title);
     $words = explode(' ', $title);
     $cleanTitle = '';
     foreach ($words as $word) {
         //limit file name to 30 symbols
         if (strlen($cleanTitle . '_' . $word) > 30) {
             break;
         }
         if ($cleanTitle != '') {
             $cleanTitle .= '_';
         }
         $cleanTitle .= $word;
     }
     if ($cleanTitle == '') {
         $cleanTitle = 'file';
     }
     $niceFileName = $cleanTitle . $ext;
     $destinationDir = ipFile('file/repository/');
     $destinationFileName = \Ip\Internal\File\Functions::genUnoccupiedName($niceFileName, $destinationDir);
     copy(ipFile('file/tmp/' . $tmpFilename), $destinationDir . $destinationFileName);
     unlink(ipFile('file/tmp/' . $tmpFilename));
     $browserModel = \Ip\Internal\Repository\BrowserModel::instance();
     $file = $browserModel->getFile($destinationFileName);
     return $file;
 }