public function sendContent($from, $to, $subject, $type, $content)
 {
     if (is_string($to)) {
         $to = [$to];
     }
     $recipients = Mailjet::parse_recipient_type($to);
     // Build the HTTP POST body text
     if ($type == 'html') {
         $body = http_build_query(array('from' => $from, 'to' => implode(', ', $recipients['to']), 'cc' => implode(', ', $recipients['cc']), 'bcc' => implode(', ', $recipients['bcc']), 'subject' => $subject, 'html' => $content));
     } else {
         if ($type == 'text') {
             $body = http_build_query(array('from' => $from, 'to' => implode(', ', $recipients['to']), 'cc' => implode(', ', $recipients['cc']), 'bcc' => implode(', ', $recipients['bcc']), 'subject' => $subject, 'text' => $content));
         } else {
             throw new Exception('Wrong email type');
         }
     }
     utils::log($body);
     $options = array('scheme' => 'http', 'host' => 'api.mailjet.com', 'path' => '/v3/send/');
     $endpoint = Mailjet::unparse_url($options);
     $headers = array('Authorization' => 'Basic ' . $this->_authentificate, 'Content-Type' => 'application/x-www-form-urlencoded', 'Content-Length' => strlen($body));
     // API request
     Unirest\Request::verifyPeer(false);
     $response = Unirest\Request::post($endpoint, $headers, $body);
     utils::log('STATUS: ' . $response->code);
     utils::log('HEADERS: ' . json_encode($response->headers));
     utils::log('BODY: ' . $response->raw_body);
     return $response->code == 200;
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $identifier = $input->getArgument('identifier');
     if (!$identifier) {
         $identifier = '1799-Auobj';
     }
     $path = utils::normalize(__DIR__ . "/../data/{$identifier}.json");
     $content = file_get_contents($path);
     $data = json_decode($content);
     $localFile = utils::normalize(__DIR__ . "/../tmp/{$data->name}");
     $file = fopen($localFile, 'wb');
     $http = new \ADN\Extract\HttpRequest($data->uri, [], null, null);
     $response = $http->get(function ($code, $chunk) use($file, $data, $path) {
         if ($code != 'data') {
             return false;
         }
         fwrite($file, $chunk);
         $data->bytesRead += strlen($chunk);
         file_put_contents($path, json_encode($data));
         return true;
     });
     fclose($file);
     if (!$response || $response->code != Response::HTTP_OK) {
         $output->writeln('oops');
         $fs = new Filesystem();
         $fs->remove($path);
         $fs->remove($localFile);
         return;
     }
     $data->size = utils::findKey($response->headers, 'Content-Length');
     $data->bytesRead = $data->size;
     file_put_contents($path, json_encode($data));
     utils::log('ok');
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $identifier = $input->getArgument('identifier');
     if (!$identifier) {
         $identifier = '1799-Auobj';
     }
     $keys = $input->getOption('keys');
     foreach ($keys as $envkey) {
         putenv($envkey);
     }
     $bucket = lmv::getDefaultBucket();
     $translator = new Translator($identifier, $bucket);
     $bSuccess = $translator->translate();
     utils::log($bSuccess ? 'ok' : 'oops');
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $identifier = $input->getArgument('identifier');
     if (!$identifier) {
         $identifier = '1799-Auobj';
     }
     $keys = $input->getOption('keys');
     foreach ($keys as $envkey) {
         putenv($envkey);
     }
     $bucket = lmv::getDefaultBucket();
     $lmv = new lmv($bucket);
     $urn = $lmv->getURN($identifier);
     //$path =utils::normalize (__DIR__ . '/../data/' . $identifier . '.lock') ;
     //file_put_contents ($path, json_encode ([ '*****@*****.**' ])) ;
     $extractor = new Extractor($identifier, $bucket);
     $bSuccess = $extractor->extract();
     utils::log($bSuccess ? 'ok' : 'oops');
 }
 public function project(Silex\Application $app, Request $request, $identifier)
 {
     $bucket = lmv::getDefaultBucket();
     $lmv = new lmv($bucket);
     $urn = $lmv->getURN($identifier);
     if ($urn == '') {
         return new Response('', Response::HTTP_NOT_FOUND, ['Content-Type' => 'text/plain']);
     }
     $path = $app->extractDir("/{$identifier}.zip", true);
     if ($path) {
         return new Response('', Response::HTTP_OK, ['Content-Type' => 'text/plain']);
     }
     $lock = $app->dataDir("/{$identifier}.lock");
     $email = $request->query->get('email');
     if (realpath($lock)) {
         if (!empty($email)) {
             $data = json_decode(file_get_contents($lock));
             $data->emails[] = $email;
             if (file_put_contents($lock, json_encode($data)) === false) {
                 utils::log("Could not create lock file - {$lock}");
             }
         }
         return new Response('', Response::HTTP_OK, ['Content-Type' => 'text/plain']);
     } else {
         $list = !empty($email) ? [$email] : [];
         $data = (object) array('progress' => 0, 'emails' => $list);
         if (file_put_contents($lock, json_encode($data)) === false) {
             utils::log("Could not create lock file - {$lock}");
         }
     }
     $result = utils::executeScript("/extract.php lmv:extractor {$identifier}", true);
     if ($result === false) {
         return new Response('', Response::HTTP_INTERNAL_SERVER_ERROR, ['Content-Type' => 'text/plain']);
     }
     return new Response('ok', Response::HTTP_OK, ['Content-Type' => 'text/plain']);
 }
 protected function getChunkFilename($chunkNumber, $identifier)
 {
     // Clean up the identifier
     $identifier = $this->cleanIdentifier($identifier);
     // What would the file name be?
     $this->initConfig();
     return utils::normalize($this->config->getTempDir() . "/flow-{$identifier}.{$chunkNumber}");
 }
 public function translate()
 {
     //utils::log ('translate launched!') ;
     try {
         $path = $this->lmv->dataDir("/{$this->identifier}.dependencies.json", true);
         if ($path === false) {
             throw new Exception('No dependency file found');
         }
         $content = file_get_contents($path);
         $connections = json_decode($content);
         if ($content === false || !is_object($connections) || is_null($connections[0])) {
             utils::log('ERROR: project dependencies corrupted :(');
             // Rebuild one master only
             $connections = (object) array('uniqueIdentifier' => $this->identifier, 'children' => []);
         }
         $items = [$this->identifier];
         $items = array_merge($items, $this->traverseConnections($connections->children));
         // This is to help the upload progress bar to be more precise
         if (file_put_contents($path, json_encode($items)) === false) {
             utils::log('ERROR: project dependencies not saved :(');
             throw new Exception('ERROR: project dependencies not saved :(');
         }
         // Bucket
         $policy = 'transient';
         $r1 = $this->lmv->createBucketIfNotExist($policy);
         if ($r1 == null) {
             // No need to continue if the bucket was not created
             utils::log('Failed to create bucket!');
             throw new Exception('Failed to create bucket!');
         }
         utils::log('Bucket created (or did exist already)!');
         // Uploading file(s)
         utils::log('Uploading(s)');
         foreach ($items as $item) {
             utils::log(" uploading {$item}");
             $r2 = $this->lmv->uploadFile($item);
             if ($r2 == null) {
                 utils::log(" Failed to upload {$item}");
                 throw new Exception("Failed to upload {$item}");
             }
             utils::log("{$item} upload completed!");
         }
         utils::log('All files uploaded');
         // Dependencies
         $r3 = $this->lmv->setDependencies(count($items) == 1 ? null : $connections);
         if ($r3 == null) {
             utils::log('Failed to set connections');
             throw new Exception('Failed to set connections');
         }
         utils::log('References set, launching translation');
         // Register
         $r4 = $this->lmv->register($connections);
         if ($r3 == null) {
             utils::log('URN registration for translation failed');
             throw new Exception('URN registration for translation failed');
         }
         utils::log('URN registered for translation');
         // We are done for now!
         // Just remember locally we did submit the project for translation
         $urn = $this->lmv->getURN($this->identifier);
         $urn = base64_encode($urn);
         $data = (object) array('guid' => $urn, 'progress' => '0% complete', 'startedAt' => gmdate(DATE_RFC2822), 'status' => 'requested', 'success' => '0%', 'urn' => $urn);
         $path = $this->lmv->dataDir("/{$this->identifier}.resultdb.json");
         if (file_put_contents($path, json_encode($data)) === false) {
             utils::log("Could not save file to disk - {$path}");
         }
         $filesystem = new Filesystem();
         foreach ($items as $item) {
             $filesystem->remove($this->lmv->dataDir("/{$item}.json"));
         }
         $filesystem->remove($this->lmv->dataDir("/{$this->identifier}.dependencies.json"));
         $filesystem->remove($this->lmv->dataDir("/{$this->identifier}.connections.json"));
         return true;
     } catch (Exception $ex) {
         //- We are done! email me if any error
         utils::log('Something wrong happened during upload');
         $loader = new Twig_Loader_Filesystem(array($this->lmv->viewsDir('', true)));
         $environment = new Twig_Environment($loader, array('debug' => false, 'charset' => 'utf-8', 'strict_variables' => true));
         $content = $environment->render('email-xlt-failed.twig', array('ID' => $this->identifier));
         $config = lmv::config();
         $mjet = new Mailjet($config['MAILJET1'], $config['MAILJET2']);
         $mjet->sendContent('ADN Sparks <*****@*****.**>', '*****@*****.**', 'Autodesk View & Data API Extractor app failed to translate a project', 'html', $content);
         $filesystem = new Filesystem();
         $filesystem->rename($this->lmv->dataDir("/{$this->identifier}.resultdb.json", true), $this->lmv->dataDir("/{$this->identifier}.resultdb.failed"), true);
         return false;
     }
 }
 public function thumbnail($urn, $width = null, $height = null)
 {
     $encodedURN = base64_encode($urn);
     $config = lmv::config();
     $endpoint = sprintf($config['getThumbnailsEndPoint'], $encodedURN);
     $query = [];
     if ($width !== null) {
         $query['width'] = $width;
     }
     if ($height !== null) {
         $query['height'] = $height;
     }
     Unirest\Request::verifyPeer(false);
     $response = Unirest\Request::get($endpoint, array('Authorization' => 'Bearer ' . lmv::bearer()), $query);
     if ($response->code != lmv::HTTP_OK) {
         utils::log('thumbnail fail ' . $response->code);
         return null;
     }
     return $response->raw_body;
 }
 public function __construct($env)
 {
     utils::log('WEB Extract Application running');
     parent::__construct($env);
     $app = $this;
     // Override these values in resources/config/prod.php file
     //$app ['var_dir'] =$this->rootDir . '/var' ;
     //$app ['locale'] ='fr' ;
     //$app ['http_cache.cache_dir'] =$app->share (function (Application $app) {
     //	return ($app ['var_dir'] . '/http') ;
     //}) ;
     //$app ['monolog.options'] =[
     //	'monolog.logfile' => $app['var_dir'].'/logs/app.log',
     //	'monolog.name' => 'app',
     //	'monolog.level' => 300, // = Logger::WARNING
     //] ;
     //$app ['security.users'] =array( 'alice' => array ( 'ROLE_USER', 'password' ) ) ;
     lmv::tokenObject();
     //$app->register (new HttpCacheServiceProvider ()) ;
     $app->register(new SessionServiceProvider());
     //$app->register (new ValidatorServiceProvider ()) ;
     //$app->register (new FormServiceProvider ()) ;
     //$app->register (new UrlGeneratorServiceProvider ()) ;
     //$app->register (new DoctrineServiceProvider ()) ;
     //$app->register (new SecurityServiceProvider (), array (
     //	'security.firewalls' => array (
     //		'admin' => array (
     //			'pattern' => '^/',
     //			'form' => array (
     //				'login_path' => '/login',
     //			),
     //			'logout' => true,
     //			'anonymous' => true,
     //			'users' => $app ['security.users'],
     //		),
     //	),
     //)) ;
     //$app ['security.encoder.digest'] =$app->share (function ($app) {
     //	return (new PlaintextPasswordEncoder ()) ;
     //}) ;
     //$app ['security.utils'] =$app->share (function ($app) {
     //	return (new AuthenticationUtils ($app ['request_stack'])) ;
     //}) ;
     //$app->register (new TranslationServiceProvider ()) ;
     //$app ['translator'] =$app->share($app->extend ('translator', function ($translator, $app) {
     //	$translator->addLoader ('yaml', new YamlFileLoader ()) ;
     //	$translator->addResource ('yaml', $this->rootDir.'/resources/translations/fr.yml', 'fr') ;
     //	return ($translator) ;
     //})) ;
     //$app->register (new MonologServiceProvider (), $app ['monolog.options']) ;
     $app->register(new TwigServiceProvider(), array('twig.path' => array($this->rootDir . '/views')));
     //$app ['twig'] = $app->share ($app->extend ('twig', function ($twig, $app) {
     //	$twig->addFunction (new \Twig_SimpleFunction ('asset', function ($asset) use ($app) {
     //		$base = $app['request_stack']->getCurrentRequest ()->getBasePath () ;
     //		return (sprintf ($base . '/' . $asset, ltrim ($asset, '/'))) ;
     //	})) ;
     //	return ($twig) ;
     //})) ;
     //$app->mount ('', new ControllerProvider ()) ;
     $app->mount('/api', new LmvFile());
     $app->mount('/api/projects', new LmvProjects());
     $app->mount('/api/results', new LmvResults());
     $app->get('/', function () use($app) {
         return $app['twig']->render('index.html.twig', []);
     });
     $app->get('/explore/{identifier}', function ($identifier) use($app) {
         $bucket = lmv::getDefaultBucket();
         $zipExist = $app->extractDir("/{$identifier}.zip", true) !== false;
         $path = $app->dataDir("/{$identifier}.resultdb.json");
         $content = file_get_contents($path);
         if ($content === false) {
             $app->abort(Response::HTTP_NOT_ACCEPTABLE, "DB record not present");
             return;
         }
         $data = json_decode($content);
         return $app['twig']->render('explore.twig', ['bucket' => $bucket, 'extracted' => $zipExist ? 'true' : 'false', 'urn' => $data->urn, 'accessToken' => lmv::bearer(), 'root' => $identifier]);
     });
 }
 public function submitProject(Silex\Application $app, Request $request)
 {
     $bucket = lmv::getDefaultBucket();
     $policy = 'transient';
     $connections = json_decode($request->getContent());
     utils::log("master: {$connections->uniqueIdentifier}");
     // Save parameters for the translate process
     $path = $app->dataDir("/{$connections->uniqueIdentifier}.dependencies.json");
     if (file_put_contents($path, json_encode($connections)) === false) {
         utils::log('ERROR: project dependencies not saved :(');
         return new Response('', Response::HTTP_NOT_FOUND, ['Content-Type' => 'text/plain']);
     }
     $result = utils::executeScript("/translate.php lmv:translator {$connections->uniqueIdentifier}", true);
     if ($result === false) {
         return new Response('', Response::HTTP_INTERNAL_SERVER_ERROR, ['Content-Type' => 'text/plain']);
     }
     // We submitted, no clue if it was successful or if it will fail.
     $response = (object) array('status' => 'submitted');
     return new JsonResponse($response, Response::HTTP_OK);
     //- 202 Accepted
 }
 protected function PackItems($results)
 {
     // We got all d/l
     try {
         // We are done! Create a ZIP file
         $zip = new \ZipArchive();
         $output = $this->lmv->extractDir("/{$this->identifier}.zip");
         $zip->open($output, \ZipArchive::CREATE);
         $zip->addEmptyDir($this->identifier);
         $path = $this->lmv->dataDir("/{$this->identifier}");
         $finder = new Finder();
         $finder->files()->in($path);
         foreach ($finder as $file) {
             $zpath = utils::postStr('/data/', utils::normalize($file->getRealpath()));
             $zip->addFile($file->getRealpath(), $zpath);
         }
         $zip->close();
         utils::log('PackItems ended successfully.');
     } catch (Exception $err) {
         utils::log('PackItems exception');
         throw $err;
     }
 }