/** Cypher the publiclet object data and write to disk.
  * @param array $data The publiclet data array to write
  * The data array must have the following keys:
  * - DRIVER      The driver used to get the file's content
  * - OPTIONS     The driver options to be successfully constructed (usually, the user and password)
  * - FILE_PATH   The path to the file's content
  * - PASSWORD    If set, the written publiclet will ask for this password before sending the content
  * - ACTION      If set, action to perform
  * - USER        If set, the AJXP user
  * - EXPIRE_TIME If set, the publiclet will deny downloading after this time, and probably self destruct.
  *               - AUTHOR_WATCH If set, will post notifications for the publiclet author each time the file is loaded
  * @param AbstractAccessDriver $accessDriver
  * @param Repository $repository
  * @param ShareStore $shareStore
  * @param PublicAccessManager $publicAccessManager
  * @return string|array An array containing the hash (0) and the generated url (1)
  */
 public function writePubliclet(&$data, $accessDriver, $repository, $shareStore, $publicAccessManager)
 {
     $downloadFolder = $publicAccessManager->getPublicDownloadFolder();
     $publicAccessManager->initFolder();
     if (!is_dir($downloadFolder)) {
         return "ERROR : Public URL folder does not exist!";
     }
     if (!function_exists("mcrypt_create_iv")) {
         return "ERROR : MCrypt must be installed to use publiclets!";
     }
     $data["PLUGIN_ID"] = $accessDriver->getId();
     $data["BASE_DIR"] = $accessDriver->getBaseDir();
     //$data["REPOSITORY"] = $repository;
     if (AuthService::usersEnabled()) {
         $data["OWNER_ID"] = AuthService::getLoggedUser()->getId();
     }
     $shareStore->storeSafeCredentialsIfNeeded($data, $accessDriver, $repository);
     // Force expanded path in publiclet
     $copy = clone $repository;
     $copy->addOption("PATH", $repository->getOption("PATH"));
     $data["REPOSITORY"] = $copy;
     if ($data["ACTION"] == "") {
         $data["ACTION"] = "download";
     }
     try {
         $hash = $shareStore->storeShare($repository->getId(), $data, "publiclet");
     } catch (Exception $e) {
         return $e->getMessage();
     }
     $shareStore->resetDownloadCounter($hash, AuthService::getLoggedUser()->getId());
     $url = $publicAccessManager->buildPublicLink($hash);
     AJXP_Logger::log2(LOG_LEVEL_INFO, __CLASS__, "New Share", array("file" => "'" . $copy->display . ":/" . $data['FILE_PATH'] . "'", "files" => "'" . $copy->display . ":/" . $data['FILE_PATH'] . "'", "url" => $url, "expiration" => $data['EXPIRE_TIME'], "limit" => $data['DOWNLOAD_LIMIT'], "repo_uuid" => $copy->uuid));
     AJXP_Controller::applyHook("node.share.create", array('type' => 'file', 'repository' => &$copy, 'accessDriver' => &$accessDriver, 'data' => &$data, 'url' => $url));
     return array($hash, $url);
 }
 /**
  * Persist the share to DB using the ShareStore
  * @return string
  * @throws Exception
  */
 public function save()
 {
     $newHash = $this->store->storeShare($this->getParentRepositoryId(), $this->internal, "minisite", $this->getHash(), $this->getNewHash());
     $this->setHash($newHash);
     return $newHash;
 }