GET URL string bad encoding - string

I was trying to get xml code into string. Here is an example:
URL: .../importarxml?enviosXml=<?xml version="1.0" encoding="iso-8859-1"?>+<lista>+<envio>+<REFERENCIA>XXXXXX</REFERENCIA>+<CODIGO>XXXXX</CODIGO>+<CLIENTE>OCAÑA</CLIENTE>+<FCARGA>13/12/2013</FCARGA>+<LCARGA>XXX</LCARGA>+<DESTINATARIO>XXXXX</DESTINATARIO>+<FENTREGA>18/12/2013</FENTREGA>+<LENTREGA>PARIS</LENTREGA>+<MATRICULA>XXXXXX</MATRICULA>+<BULTOS>2</BULTOS>+<PESO>1302</PESO>+<OBSERVACIONES></OBSERVACIONES>+</envio>+</lista>
That's my code to get parameters from URL in my Controller in symfony:
class EnviosController extends Controller{
public function importarxmlAction(){
$request = $this->getRequest();
$em = $this->getDoctrine()->getManager();
$xml = $request->query->get('enviosXml'); // $xml get bad characters
ld($xml); --> (Displays $xml string content. Show bad characters)
//For example 'OCAÑA'-->'OCA�A'
(ldd-->LadybugBundle)
$utf8_1 = utf8_decode($xml);
$utf8_2 = iconv('UTF-8', 'ISO-8859-1', $utf8_1);
$utf8_2 = mb_convert_encoding($utf8_2, 'ISO-8859-1','UTF-8');
ld($utf8_2); // Display $utf8_2 content
...
}
}
I tryed utf8_decode(),iconv() and mb_convert_encoding() but didn't help me. I don't know what else to do, if anyone could help me I'll appreciate it.
Thanks.
P.S: Sorry for my bad english!

It's not a good idea to pass a whole XML in the url, by the GET method. Your url is not even escaped to be used as url string...
You should either give the reference of an existing file by its id/name or upload it by a form (method POST)
Then check the encoding of your file.

Related

Passing JavaScript variable into snippet

I'm working on a search form for my ModX application that is consisted of a chunk and a snippet. What I'm trying to achieve is to pass what was entered into the search box into a javascript variable and then pass it to my snippet, however, the snippet receives the literal text, and not the value that I enter into the parameter when I call it.
I don't know if what I'm attempting is possible in ModX or if I need to take a different approach, but I would be hugely thankful for anyone who can provide any insight.
Chunk:
<script>
$('.search-btn').click(function() {
var search = $('.search-entry').val();
[[showSearchResults? &q=`search`]]
});
</script>
Snippet:
<?php
$search = $modx->getOption('q', $scriptProperties);
echo $search; // this always prints "search"
?>
I doubt that this code makes sense:
<script>
$('.search-btn').click(function() {
var search = $('.search-entry').val();
[[showSearchResults? &q=`search`]]
});
</script>
The snippet call returns the result of snippet's execution with param q always equal to the string 'search' in your case and finally on your page you will have something like this:
<script>
$('.search-btn').click(function() {
var search = $('.search-entry').val();
'search' // assuming your snippet just returns what has been passed to it.
});
</script>
In order to accomplish your task you can use a simple trick. Call your snippet like this:
[[!yourSnippet? &yourVar=`[[!#POST.yourVar]]` ]] // or GET
Lets say this snippet call is located on a page accessible via url /test/ on your server. So, now you just have to send the parameters you collected from your search form using AJAX to the /test/ page where your snippet is:
var yourVar = $('.search-entry').val();
$.ajax({
type: "POST",
url: "/test/",
data: {yourVar: yourVar},
success: success,
dataType: "html"
});
Hope it helps :)
PS If you want to search Resource content and TV content, I can highly recommend an extra called SimpleSearch.

nlobjFile.getValue does not work in Suitelet

I have used the following code in a NetSuite Suitelet to upload and process a file:
function main(request,response){
if (request.getMethod() == 'GET'){
var form = nlapiCreateForm('Item Import Correction', false);
var fileField = form.addField('custpage_file', 'file', 'Select CSV');
form.addSubmitButton();
response.writePage(form);
}else{
try{
var file = request.getFile("custpage_file");
var content = file.getValue();//exception
response.write(content);
}catch(ex){
response.write('Exception:'+ex);
}
}
}
When I select a file and submit it, I get an exception on calling getValue() on nlobjFile. Here is the output of response:
Exception:JavaException: java.lang.NullPointerException: charsetName
However, I replace the getValue() call with some other method of the same object like getSize() or getType(), the code works fine.
I just want to parse a file selected by user in a Suitelet.
getFile() - Returns a file added through the nlobjForm.addField(name, type, label, sourceOrRadio, tab) method. When adding a file field type, you will set the type parameter of 'file'.
Make sure that the field you are referencing to using getFile() do exists in the form.
Calling the setEncoding() method on nlobjFile did the trick for me. I was using Chinese Encoding so this was the code that worked for me
var file = request.getFile("custpage_file");
file.setEncoding('GB18030');// Chinese
var content = file.getValue();//no exception

How do you deal with the fact, that URLs are case sensitive in xPages?

How do you deal with the fact, that URLs are case sensitive in xPages even for parameters? For example URL:
my_page.xsp?folderid=785478 ... is not the same as ...
my_page.xsp?FOLDERID=785478
How to make, for example, a proper check that params contain some key e.g.
param.containsKey("folderid") which desnt work when there is 'FOLDERID' in URL.
I'd suggest defining a couple convenience #Functions:
var #HasParam = function(parameter) {
var result:boolean = false;
for (var eachParam : param.keySet()) {
if (eachParam.toLowerCase() == parameter.toLowerCase()) {
result = true;
break;
}
}
return result;
};
var #GetParam = function(parameter) {
var result = "";
if (#HasParam(parameter)) {
for (var eachParam : param.keySet()) {
if (eachParam.toLowerCase() == parameter.toLowerCase()) {
result = param.get(eachParam);
break;
}
}
}
return result;
};
Then you can safely query the parameters without caring about case. For bonus points, you could add requestScope caching so that you can skip looping through the keySet if you're examining a parameter that you've previously looked at during the same request.
you may use this function:
context.getUrlParameter('param_name')
then test if it's null or not.
make sure to decide for one,so either upper or lowercase
other than that i'd suggest something like
KeyValuePair<string,string> kvp = null;
foreach(KeyValuePair<string,string> p in param)
{
if(UPPERCASE(p.Key) == UPPERCASE("folderid"))
{
kvp = p;
break;
}
}
syntax isn't correct and idk the uppercase method in c# right now,but you get the point
The easiest answer is ofcourse the obvious. Be sure that the parameters you are using througout your application are always the same on every url you are generating and know what to expect. A good approach to accomplish this is to create a ssjs function which generates url's for you according to the objects you submit.
In this function you could check which object you are receiving and with the use of keywords and so forth generate the correct url. This way generating twice a url with the same input parameters should always generate the exact same url.
another option would be just to double check with a bit of code like this
var key = "yourkey";
if(param.contains(#uppercase(key)) || param.contains(#lowercase(key)){
// do stuff
}
But should not be necesarry if the url you are parsing is generated by your own application
Edit after post of topic starter
Another option would be to grap the url directly from from the facescontext and to convert it to a string first. When it is a string you can parse the parameters yourself.
You can combine server side substitution/redirection to get around the issue that David mentioned. So a substitution rule will redirect incoming patern like this:
http://myhost/mypage/param (/mypage/* => which converts to - /dbpath/mypage.xsp?*) - substitution is tricky so please handle with care.
Also I believe I read somewhere that context.getUrlParameter is not case sensitive - can someone please confirm this.
Hope this helps.

PHP. Write an anchor in the Smarty template. (Kohana 3 + KSmarty)

I'm learning Kohana 3.2.0 together with KSmarty for Kohana 3. I'd like to write an anchor on the page like this:
Page list
I can build the url in the controller and pass it to Smarty as a variable but. Is there a way to build the anchor or URL in Smarty template (including "http://www.mysite.cz" part)?
If it is not possible to build the anchor. Is it at least possible to build full URL?
The Reason: I have a main template which includes another template.
The main template will be used by multiple controllers and I would like to avoid building the URL in each controller. Therefore I'll be happy if KSmarty will be able to do it for me.
The only solution I have found is to write custom function. Save following code into function.url.php file in Smarty plugins directory:
function smarty_function_url($params, &$smarty)
{
$type = '';
if(isset($params['type'])) $type = $params['type'];
$protocol = 'http';
if(isset($params['protocol'])) $protocol = $params['protocol'];
$url = '';
if(isset($params['url'])) $url = $params['url'];
$text = '';
if(isset($params['text'])) $text = $params['text'];
switch($params['type'])
{
case 'url':
return Kohana_URL::site($url, $protocol);
case 'anchor':
$url = Kohana_URL::site($url, $protocol);
return "<a href='{$url}'>{$text}</a>";
default:
return Kohana_URL::base('http');
}
}
Examples of use in Smarty template:
{url}
{url type='url' url='admin/categories' protocol='https'}
{url type='anchor' url='admin/articles' text='List of articles'}
The first block in which variables are set I had to write otherwise Smarty was generating notice "Undefined variable...". I'm just PHP student, suggestions for code improvement are welcome.
Hope it will help the others.

How to get the Absolute URL of a file in sharepoint library

I am working on SharePoint 2010.I have an documentlibrary ID and document ID in that library with me.i don't have either web,site in which the document library is present.So now I have to get the Full URL of the document at runtime.How can I get it .
I have tried the following.
string filepath = currentList.DefaultViewUrl + "/" + sListItem.Url;
Please answer this.
Use the field "EncodedAbsUrl" on the SPListItem. Works for SPFile as well:
SPListItem item = ...;
string absUrl = (string) item[SPBuiltInFieldId.EncodedAbsUrl];
or for a SPFile
SPFile file = ...;
string absUrl = (string) file.Item[SPBuiltInFieldId.EncodedAbsUrl];
The built in field id is for sure the best way to go but it returns the Url as encoded which may or may not be what you want.
I think the best way is to add a little extension method to a utilities class somewhere:
public static string AbsoluteUrl(this SPFile File, bool Decode = true)
{
string EncodedUrl = File.Item[SPBuiltInFieldId.EncodedAbsUrl].ToString();
if (Decode)
return SPEncode.UrlDecodeAsUrl(EncodedUrl);
else
return EncodedUrl;
}
And then call as follows
Item.File.AbsoluteUrl();
if you want a decoded Url or
Item.File.AbsoluteUrl(false);
if you want the Url to remain encoded.
Note that the default parameter value for Decode will only be available in .Net4+ and therefore SP2013 only but you can easily create an overload method for SP2010. You'll also need a reference to Microsoft.SharePoint.Utilities namespace to access the SPEncode class.
Try this ,
using (SPSite ospSite = new SPSite("http://abcd:24931"))
{
using (SPWeb web = ospSite.OpenWeb("/subsite")
{
// Get document library collection here and fetch all the document urls
SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["docu"];
//where docu is my document library
SPListItemCollection items = docLib.Items;
foreach (SPListItem item in items)
{
string url = item.Url;
}
}
}
Hope this shall get you going.
public string GetItemURL(SPListItem item)
{
string web = item.Web.Url;
string listID = item.ParentList.ID.ToString();
string contentType = item.ContentTypeId.ToString();
string itemID = item.ID.ToString();
string url = web+"/_layouts/listform.aspx?PageType=4&ListID={"+listID+"}&ID="+itemID+"&ContentTypeID="+contentType;
return url;
}
It's Working for me. Hope I help (List Item url)
If this is for the document library, try this one.
item.Web.Url+"/"+item.File.Url
Use below code to get absolute URL of file:
SPFile file;
string url = file.Item[SPBuiltInFieldId.EncodedAbsUrl] as string;
For what it's worth, accessing the item.Web property means you are actually instantiating the SPWeb object which means that this should be disposed otherwise you'll be causing memory leakage.
It's a lot of overhead when there are better and quicker ways already mentioned.
I would use the BuiltInFieldId.EncodedAbsUrl approach mentioned since this gives you the easiest access to what you want.
The answer is
string url = currentweb.url+"/"+ Listitem.url;

Resources