Example #1
0
void
ShareCreated::refresh(void)
{
	if (!wApp->internalPathMatches("/share-created"))
		return;

	clear();

	std::string editUUID = wApp->internalPathNextPart("/share-created/");

	Wt::Dbo::Transaction transaction(FsApp->getDboSession());

	Database::Share::pointer share = Database::Share::getByEditUUID(FsApp->getDboSession(), editUUID);
	if (!share)
	{
		FS_LOG(UI, ERROR) << "Edit UUID '" << editUUID << "' not found";
		Wt::WTemplate *t = addNew<Wt::WTemplate>(tr("template-share-not-found"));
		t->addFunction("tr", &Wt::WTemplate::Functions::tr);
		return;
	}

	Wt::WTemplate *t = addNew<Wt::WTemplate>(tr("template-share-created"));
	t->addFunction("tr", &Wt::WTemplate::Functions::tr);

	t->bindWidget("download-link", createShareDownloadAnchor(share));
	t->bindWidget("edit-link", createShareEditAnchor(share));
}
ShareCreatePassword::ShareCreatePassword()
{
	auto model = std::make_shared<ShareCreatePasswordFormModel>();

	setTemplateText(tr("template-share-create-password"));
	addFunction("id", &WTemplate::Functions::id);
	addFunction("block", &WTemplate::Functions::id);

	// Password
	auto password = std::make_unique<Wt::WLineEdit>();
	password->setEchoMode(Wt::EchoMode::Password);
	setFormWidget(ShareCreatePasswordFormModel::PasswordField, std::move(password));

	// Buttons
	Wt::WPushButton* unlockBtn = bindNew<Wt::WPushButton>("unlock-btn", tr("msg-unlock"));
	unlockBtn->clicked().connect([=]
	{
		updateModel(model.get());

		if (model->validate())
		{
			FS_LOG(UI, DEBUG) << "Create password validation OK";

			success().emit();
			return;
		}

		FS_LOG(UI, DEBUG) << "Create password validation failed";

		// Mitigate brute force attemps
		sleep(1);

		updateView(model.get());
	});

	updateView(model.get());
}
Example #3
0
	void Resources::loadXML(const string &xml_name, LoadResourcesContext *load_context, bool load_completely, bool use_load_counter, const string &prebuilt_folder_)
	{
		_name = xml_name;

		string path;

		for (int i = xml_name.size() - 1; i >= 0; --i)
		{
			char c = xml_name[i];
			if (c == '\\' || c == '/')
			{
				path = xml_name.substr(0, i + 1);
			}
		}
		
		FS_LOG("step0");
		file::buffer fb;
		file::read(xml_name.c_str(), fb);

		FS_LOG("step1");
		

		char destHead[255];
		char destTail[255];
		path::split(xml_name.c_str(), destHead, destTail);

		string xml_only_name = destTail;

		string prebuilt_folder = prebuilt_folder_ + "/" + destTail + ".ox/";
		if (prebuilt_folder[0] == '/')
		{
			prebuilt_folder.erase(prebuilt_folder.begin());
		}

		file::buffer fb_meta;
		pugi::xml_document doc_meta;
		string ox = prebuilt_folder + "meta.xml";
		const char *ox_file = ox.c_str();

			

		FS_LOG("step2");

		if (file::exists(ox_file))
		{
			file::read(ox_file, fb_meta, ep_ignore_error);
			if (fb_meta.getSize())
				doc_meta.load_buffer_inplace(&fb_meta.data[0], fb_meta.data.size());
		}

		if (!fb.data.size())
		{
			OX_ASSERT(fb.data.size()  && "can't find xml file");
			return;
		}
		pugi::xml_document *doc = new pugi::xml_document();
		_docs.push_back(doc);

		bool loaded = doc->load_buffer(&fb.data[0], fb.data.size());
		OX_ASSERT(loaded);

		pugi::xml_node resources = doc->first_child();
		pugi::xml_node resources_meta = doc_meta.first_child();
		pugi::xml_node meta_node = resources_meta.first_child();


		int i = 0;

		string id;
		//string file;
		string rect_str;

		FS_LOG("loading xml resources");

		float scaleFactor = 1.0f;

		pugi::xml_node child = resources.first_child();
		while (!child.empty())
		{
			const char *name = child.name();
			pugi::xml_attribute attr;

			

			if (!strcmp(name, "set"))
			{
				pugi::xml_attribute attr = child.first_attribute();
				while (attr)
				{
					if (!strcmp(attr.name(), "path"))
					{
						path = getPath(path.c_str(), attr.value());
						path += "/";
					}
					if (!strcmp(attr.name(), "load"))
					{
						load_completely = attr.as_bool();
					}
					if (!strcmp(attr.name(), "scale_factor"))
					{
						scaleFactor = attr.as_float(scaleFactor);
					}
					attr = attr.next_attribute();
				}
			}
			else/*
			if (!strcmp(name, "atlas"))
			{
				loadAtlas(path, child, meta_node);
				meta_node = meta_node.next_sibling();
			}
			else*/
			{
				bool load = child.attribute("load").as_bool(load_completely);

				registeredResources::iterator i = lower_bound(_registeredResources.begin(), _registeredResources.end(), name);
				if (i != _registeredResources.end())
				{
					registeredResource &r = *i;
					if (!strcmp(r.id, name))
					{
						CreateResourceContext context;
						context.xml_name = &xml_name;
						context.node = child;
						context.meta = meta_node;
						context.folder = &path;
						context.resources = this;
						context.scale_factor = scaleFactor;

						string prebuilt_xml_folder = prebuilt_folder + "/" + name + "/";
						context.prebuilt_folder = &prebuilt_xml_folder;


						FS_LOG("resource: %s ", name);
						Resource *res = r.cb(context);
						OX_ASSERT(res);
						res->setUseLoadCounter(use_load_counter);

						meta_node = context.meta;

						if (res)
						{	
							//res-> = child;
							if (load)
								res->load(load_context);

							_resources.push_back(res);
						}
					}
					else
					{
						log::error("unknown resource. type: '%s' id: '%s'", name, Resource::extractID(child, "", "").c_str());
						OX_ASSERT(!"unknown resource type");
					}
				}
			}

			child = child.next_sibling();
		}

		sort(_resources.begin(), _resources.end(), comparePred);
		FS_LOG("xml loaded");
		//print();
	}
    void Resources::loadXML(const std::string& xmlFile, const ResourcesLoadOptions& opt)
    {
        _name = xmlFile;
        _loadCounter = opt._loadCompletely ? 1 : 0;


        FS_LOG("step0");
        file::buffer fb;
        file::read(xmlFile, fb);

        FS_LOG("step1");


        updateName(xmlFile);

        char destHead[255];
        char destTail[255];
        path::split(xmlFile.c_str(), destHead, destTail);

        std::string prebuilt_folder = opt._prebuilFolder + "/" + destTail + ".ox/";
        if (prebuilt_folder[0] == '/')
        {
            prebuilt_folder.erase(prebuilt_folder.begin());
        }

        file::buffer fb_meta;
        pugi::xml_document doc_meta;
        std::string ox = prebuilt_folder + "meta.xml";
        const char* ox_file = ox.c_str();


        FS_LOG("step2");

        if (file::exists(ox_file))
        {
            file::read(ox_file, fb_meta, ep_ignore_error);
            if (fb_meta.getSize())
                doc_meta.load_buffer_inplace(&fb_meta.data[0], fb_meta.data.size());
        }

        if (!fb.data.size())
        {
            OX_ASSERT(fb.data.size() && "can't find xml file");
            return;
        }

        pugi::xml_document* doc = new pugi::xml_document();
        _docs.push_back(doc);

        bool loaded = doc->load_buffer(&fb.data[0], fb.data.size());
        OX_ASSERT(loaded);

        pugi::xml_node resources = doc->first_child();
        pugi::xml_node resources_meta = doc_meta.first_child();
        if (!resources_meta.empty())
        {
            int metaVersion = resources_meta.attribute("version").as_int(0);
            OX_ASSERT(metaVersion <= 2 && "Incompatible atlas format. Please rebuild xmls with latest 'oxyresbuild' tool or delete .ox folder from data.");
        }


        std::string id;

        FS_LOG("loading xml resources");

        std::string xmlFolder = destHead;
        XmlWalker walker(&xmlFolder, "", 1.0f, opt._loadCompletely, true, resources, resources_meta);

        while (true)
        {
            CreateResourceContext context;
            context.options = &opt;
            context.walker = walker.next();
            if (context.walker.empty())
                break;


            const char* type = context.walker.getType();

            registeredResources::iterator i = std::lower_bound(_registeredResources.begin(), _registeredResources.end(), type);
            if (i == _registeredResources.end() || strcmp(i->id, type))
            {
                log::error("unknown resource. type: '%s' id: '%s'", type, _Resource::extractID(context.walker.getNode(), "", "").c_str());
                OX_ASSERT(!"unknown resource type");
                continue;
            }

            registeredResource& r = *i;


            context.xml_name = &xmlFile;
            context.resources = this;

            std::string prebuilt_xml_folder = prebuilt_folder + type + "/";
            context.prebuilt_folder = &prebuilt_xml_folder;


            FS_LOG("resource: %s ", name);
            Resource* res = r.cb(context);
            OX_ASSERT(res);
            res->setUseLoadCounter(opt._useLoadCounter);

            if (res)
            {
                if (context.walker.getLoad())
                    res->load(0);
                res->setParent(this);
                _resources.push_back(res);
            }
        }

        FS_LOG("xml loaded");
    }
Example #5
0
void
ShareEdit::refresh(void)
{
	if (!wApp->internalPathMatches("/share-edit"))
		return;

	clear();

	std::string editUUID = wApp->internalPathNextPart("/share-edit/");

	Wt::Dbo::Transaction transaction(FsApp->getDboSession());

	Database::Share::pointer share = Database::Share::getByEditUUID(FsApp->getDboSession(), editUUID);
	if (!share || !boost::filesystem::exists(share->getPath()))
	{
		FS_LOG(UI, ERROR) << "Edit UUID '" << editUUID << "' not found";
		displayNotFound();
		return;
	}

	FS_LOG(UI, INFO) << "[" << share->getDownloadUUID() << "] Editing share from " << wApp->environment().clientAddress();

	Wt::WTemplate *t = addNew<Wt::WTemplate>(tr("template-share-edit"));
	t->addFunction("tr", &Wt::WTemplate::Functions::tr);

	if (!share->getDesc().empty())
	{
		t->setCondition("if-desc", true);
		t->bindString("file-desc", Wt::WString::fromUTF8(share->getDesc()));
	}
	t->bindString("file-name", Wt::WString::fromUTF8(share->getFileName()));
	t->bindString("file-size", sizeToString(share->getFileSize()));
	t->bindString("expiry-date-time", share->getExpiryTime().toString() + " UTC");

	auto hits = std::to_string(share->getHits());
	if (share->getMaxHits() > 0)
		hits += " / " + std::to_string(share->getMaxHits());
	t->bindString("hits", hits);

	t->bindWidget("download-link", createShareDownloadAnchor(share));

	Wt::WPushButton* deleteBtn = t->bindNew<Wt::WPushButton>("delete-btn", tr("msg-delete"));

	deleteBtn->clicked().connect([=] ()
	{
		auto messageBox = deleteBtn->addChild(std::make_unique<Wt::WMessageBox>(tr("msg-share-delete"),
			tr("msg-confirm-action"),
			 Wt::Icon::Question,
			 Wt::StandardButton::Yes | Wt::StandardButton::No));

		messageBox->setModal(true);

		messageBox->buttonClicked().connect([=] (Wt::StandardButton btn)
		{
			if (btn == Wt::StandardButton::Yes)
			{
				Wt::Dbo::Transaction transaction(FsApp->getDboSession());

				Database::Share::pointer share = Database::Share::getByEditUUID(FsApp->getDboSession(), editUUID);

				if (share)
				{
					FS_LOG(UI, INFO) << "[" << share->getDownloadUUID() << "] Deleting share from " << wApp->environment().clientAddress();
					share.modify()->destroy();
					share.remove();
				}

				displayRemoved();
			}
			deleteBtn->removeChild(messageBox);
		});

		messageBox->show();
	});

}
	void Resources::loadXML(const string &xml_name, 
		LoadResourcesContext *load_context, 
		bool load_completely, bool use_load_counter, 
		const string &prebuilt_folder_)
	{


		_name = xml_name;
		_loadCounter = load_completely ? 1 : 0;


		FS_LOG("step0");
		file::buffer fb;
		file::read(xml_name.c_str(), fb);

		FS_LOG("step1");
		

        updateName(xml_name);

        char destHead[255];
        char destTail[255];
        path::split(xml_name.c_str(), destHead, destTail);

        string prebuilt_folder = prebuilt_folder_ + "/" + destTail + ".ox/";
		if (prebuilt_folder[0] == '/')
		{
			prebuilt_folder.erase(prebuilt_folder.begin());
		}

		file::buffer fb_meta;
		pugi::xml_document doc_meta;
		string ox = prebuilt_folder + "meta.xml";
		const char *ox_file = ox.c_str();

		
		FS_LOG("step2");

		if (file::exists(ox_file))
		{
			file::read(ox_file, fb_meta, ep_ignore_error);
			if (fb_meta.getSize())
				doc_meta.load_buffer_inplace(&fb_meta.data[0], fb_meta.data.size());
		}

		if (!fb.data.size())
		{
			OX_ASSERT(fb.data.size()  && "can't find xml file");
			return;
		}

		pugi::xml_document *doc = new pugi::xml_document();
		_docs.push_back(doc);

		bool loaded = doc->load_buffer(&fb.data[0], fb.data.size());
		OX_ASSERT(loaded);

		pugi::xml_node resources = doc->first_child();
		pugi::xml_node resources_meta = doc_meta.first_child();


		int i = 0;

		string id;
		//string file;
		string rect_str;

		FS_LOG("loading xml resources");

		XmlWalker walker(destHead, 1.0f, load_completely, resources, resources_meta);

		while(true)
		{
			CreateResourceContext context;
			context.walker = walker.next();
			if (context.walker.empty())
				break;


			const char *type = context.walker.getType();

			registeredResources::iterator i = lower_bound(_registeredResources.begin(), _registeredResources.end(), type);
			if (i == _registeredResources.end() || strcmp(i->id, type))
			{
				log::error("unknown resource. type: '%s' id: '%s'", type, Resource::extractID(context.walker.getNode(), "", "").c_str());
				OX_ASSERT(!"unknown resource type");
				continue;
			}

			registeredResource &r = *i;

			
			context.xml_name = &xml_name;
			context.resources = this;

			string prebuilt_xml_folder = prebuilt_folder + type + "/";
			context.prebuilt_folder = &prebuilt_xml_folder;


			FS_LOG("resource: %s ", name);
			Resource *res = r.cb(context);
			OX_ASSERT(res);
			res->setUseLoadCounter(use_load_counter);

			if (res)
			{	
				bool load = context.walker.getLoad();

				//res-> = child;
				if (load)
					res->load(load_context);
                res->setParent(this);
				_resources.push_back(res);
				//_owned.push_back(res);
			}
		}

		sort();
		FS_LOG("xml loaded");
	}