Unable to upload web page - perl

I am trying to upload a file from a web page. I use CGI to query for all of the input fields but "my $upload_filehandle = $query->upload("fileInput");" is always empty even when "my $file = $query->param("fileInput");" has properly fetched the file name from the same field. Here is the code:
use CGI;
$CGI::POST_MAX = 1024 * 5000;
my $query = CGI->new;
my $url = $query->param("urlInput");
my $file = $query->param("fileInput");
my $upload_filehandle = $query->upload("fileInput");
my $text = $query->param("textInput");
my $k = $query->param("kInput");
Any advice is appreciated.
Regards.

I will repost my comment, as an answer:
make sure your HTML form contains the enctype attribute, and that it's set to enctype="multipart/form-data"

Related

How to loop the result from findnodes() with HTML::TreeBuilder::XPath

I have my script to monitor some Facebook pages. Since Facebook API banned page public access permission on 4-SEP-2019. I need to parse the content by xpath method.
Each Facebook post is wrap by div[contains(#class,"userContentWrapper")]. I would like to loop posts one by one to find a desired data.
I don't known why $message = $post->findvalue('//div[#data-testid="post_message"]//p'); show all text in <p> of every posts.
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$request = new HTTP::Request;
$request->url('https://www.facebook.com/pg/FIFA/posts/');
$request->method('GET');
$request->header('User-Agent' => 'Mozilla/5.0 Chrome/71.0.3578.98 Safari/537.36');
$response = $ua->request($request);
open(HTM, ">zzz.htm");
print HTM $response->content;
close(HTM);
use HTML::TreeBuilder::XPath;
$tree = HTML::TreeBuilder::XPath->new_from_content($response->content);
$posts = $tree->findnodes('//div[contains(#class,"userContentWrapper")]');
for my $post (#{$posts})
{
$id = $post->findnodes('//div[#data-testid="story-subtitle"]/#id');
$id = $id->[0]->getValue;
print "id = $id\n\n";
$object_id = $post->findnodes('//div[#data-testid="story-subtitle"]//a/#href');
$object_id = 'https://www.facebook.com' . $object_id->[0]->getValue;
print "object_id = $object_id\n\n";
$message = $post->findvalue('//div[#data-testid="post_message"]//p');
# $message = $message->[0]->getValue;
print "$message\n\n";
$ajaxify = $post->findnodes('//div[#class="mtm"]//a/#ajaxify');
$ajaxify = $ajaxify->[0]->getValue;
print "ajaxify = $ajaxify\n\n";
$ploi = $post->findnodes('//div[#class="mtm"]//a/#data-ploi');
$ploi = $ploi->[0]->getValue;
print "ploi = $ploi\n\n";
# $plsi = $post->findnodes('//div[#class="mtm"]//a/#data-plsi');
# $plsi = $plsi->[0]->getValue;
# print "plsi = $plsi\n\n";
$href = $post->findnodes('//div[#class="mtm"]//a/#href');
$href = 'https://www.facebook.com' . $href->[0]->getValue;
print "href = $href\n\n";
print "---------------------------------------------------------\n\n";
}
The post is unclear and it seems to contain multiple questions. This needs to be fixed, but in the mean time, I'll address the following:
I would like to loop posts one by one to find a desired data.
From HTML::TreeBuilder::XPath,
findnodes ($path)
Returns a list of nodes found by $path. In scalar context returns an Tree::XPathEngine::NodeSet object.
From Tree::XPathEngine::NodeSet,
get_nodelist()
Returns a list of nodes. See Tree::XPathEngine::XMLParser for the format of the nodes.
So,
my #posts = $tree->findnodes('...');
for my $post (#posts) { ... }
or
my $posts = $tree->findnodes('...');
for my $post ($posts->get_nodelist()) { ... }
Any other questions should be posted as separate Questions.

OG Tags via php

I'm trying to make a very simple game.
a) I let the user input his name;
b) I put this name in a canvas;
c) I generate an image (base64) whit canvas.toDataRL() and I create an image file sending the base64 URI to a php file. I'm doing it with this code:
JAVASCRIPT:
var canvas = document.getElementById("myCanvas");
var dataURL = canvas.toDataURL("image/jpeg", 0.2);
//console.log(dataURL);
// post the dataUrl to php
$.ajax({
type: "POST",
url: "upload.php",
data: {image: dataURL}
}).done(function( respond ) {
console.log(respond);
});
UPLOAD.PHP
<?php
if ( isset($_POST["image"]) && !empty($_POST["image"]) ) {
$dataURL = $_POST["image"];
$parts = explode(',', $dataURL);
$data = $parts[1];
$data = base64_decode($data);
// create a temporary unique file name
$file = "img/" . UPLOAD_DIR . uniqid() . '.png';
// write the file to the upload directory
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save this image.';
}
?>
d) Now I'd like to use this image that I've created to set the og:image tag (to share it on Facebook)! How can I do? I've honestly no idea.
Thank you!
Maybe in the OG you can put the link with <?php echo $file; ?>
I've found out the solution.
I pass the respond via get to another php page that catch the get parameter and put it in the og:image'content part.
You can debug your OG markup here: https://developers.facebook.com/tools/debug/
General information about open graph markup: https://developers.facebook.com/docs/sharing/webmasters#markup

strip_tags or preg_replace to remove few tags from html?

I am in dilemma between these two.
I want to strip head tags ( and everything inside/before including doctype/html) , body tag and script tags from a page that I am importing via curl.
So first thought was this
$content = strip_tags($content, '<img><p><a><div><table><tbody><th><tr><td><br><span><h1><h2><h3><h4><h5><h6><code><pre><b><strong><ol><ul><li><em>'.$tags);
which as you can see can get even longer with HTML5 tags, video object etc..
Than I saw this here.
https://stackoverflow.com/a/16377509/594423
Can anyone advise the preferred method or show your way of doing this and please explain why and
possibly tell me which one is faster.
Thank you!
You can test something like that:
$dom = new DOMDocument();
#$dom->loadHTML($content);
$result = '';
$bodyNode = $dom->getElementsByTagName('body')->item(0);
$scriptNodes = $bodyNode->getElementsByTagName('script');
$toRemove = array();
foreach ($scriptNodes as $scriptNode) {
$toRemove[] = $scriptNode;
}
foreach($toRemove as $node) {
$node->parentNode->removeChild($node);
}
$bodyChildren = $bodyNode->childNodes;
foreach($bodyChildren as $bodyChild) {
$result .= $dom->saveHTML($bodyChild);
}
The advantage of the DOM approach is a relative reliability against several html traps, especially some cases of malformed tags, or tags inside javascript strings: var str = "<body>";
But what about speed?
If you use a regex approach, for example:
$pattern = <<<'EOD'
~
<script[^>]*> (?>[^<]++|<(?!/script>))* </script>
|
</body>.*$
|
^ (?>[^<]++|<(?!body\b))* <body[^>]*>
~xis
EOD;
$result = preg_replace($pattern, '', $content);
The result is a little faster (from 1x to 2x for an html file with 400 lines). But with this code, the reliability decreases.
If speed is important and if you have a good idea of the html quality, for the same reliability level than the regex version, you can use:
$offset = stripos($content, '<body');
$offset = strpos($content, '>', $offset);
$result = strrev(substr($content,++$offset));
$offset = stripos($result, '>ydob/<');
$result = substr($result, $offset+7);
$offset = 0;
while(false !== $offset = stripos($result, '>tpircs/<', $offset)) {
$soffset = stripos($result, 'tpircs<', $offset);
$result = substr_replace($result, '', $offset, $soffset-$offset+7);
}
$result = strrev($result);
That is between 2x and 5x faster than the DOM version.

Tumblr API get current AVATAR URL

Everyone knows? about avatar url in tumblr api / read / json?
like for example the facebook?
http://graph.facebook.com/[your facebook id]/picture?type=normal
<?php
$tumblog = 'natadec0c0'; // change to your username
// if your Tumblog is self hosted, you need to change the base url to the location of your tumblog
$baseurl = 'http://' . $tumblog . '.tumblr.com';
$request = $baseurl . '/api/read/json';
$ci = curl_init($request);
curl_setopt($ci,CURLOPT_RETURNTRANSFER, TRUE);
$input = curl_exec($ci);
curl_close($ci);
// Tumblr JSON doesn't come in standard form, some str replace needed
$input = str_replace('var tumblr_api_read = ','',$input);
$input = str_replace(';','',$input);
// parameter 'true' is necessary for output as PHP array
$value = json_decode($input,true);
$content = $value['posts'];
$blogInfo = $value['tumblelog'];
// the number of items you want to display
$item = 10;
// Echo the blog info
echo "<h3>" . $blogInfo['title'] . "</h3>\n";
echo "<h4>" . $blogInfo['picture'] . "</h4>\n<hr />\n";
?>
how to append my current avatar?
A better solution for this is to put the Avatar api in an img tag.
api.tumblr.com/v2/blog/{base-hostname}/avatar[/size]
example: <img src='http://api.tumblr.com/v2/blog/myreallycoolblog.tumblr.com/avatar/48'/>
So as long as you have the blogname, you can display the avatar.
I guess you have to use
GET http://www.tumblr.com/api/authenticate?email=user#example.com&password=12345
to get the avatar of the user. The sample response for mine is
<tumblr version="1.0">
<user default-post-format="html" can-upload-audio="1" can-upload-aiff="1" can-ask-question="1" can-upload-video="1" max-video-bytes-uploaded="26214400" liked-post-count="134"/>
<tumblelog title="ABNKKPGPiCTuReNPLaKo?!" is-admin="1" posts="301" twitter-enabled="0" draft-count="0" messages-count="0" queue-count="" name="arvn" url="http://arvn.tumblr.com/" type="public" followers="17" avatar-url="http://28.media.tumblr.com/avatar_b1786ec9e62d_128.png" is-primary="yes" backup-post-limit="30000"/>
<tumblelog title="i kras yu." is-admin="1" posts="1" twitter-enabled="0" draft-count="0" messages-count="0" queue-count="" name="ikrasyu" url="http://ikrasyu.tumblr.com/" type="public" followers="2" avatar-url="http://25.media.tumblr.com/avatar_02a7ef66fce8_128.png" backup-post-limit="30000"/>
</tumblr>
and get the avatar-url field of the corresponding tumblelog. Too bad there is no json format option, maybe use preg_match. You also need the email address and password of the user, or do it via OAuth.
Or you could scrape the tumblelog for the avatar.
$page = file_get_contents("http://{$tumblog}.tumblr.com/");
$avatar = preg_match('/<img src="(http.+)" alt="portrait"/', $page, $matches) ? $matches[1]: 'http://example.com/blank.png';

<fb:comments-count> not working on my WordPress powered blog

I am using the Facebook comments plugin on WordPress and the comments box is working fine but I want to access the number of counts on the index page and on single pages. On the pages, the Facebook Javascript is loaded on the pages.
Here's the code I used:
<fb:comments-count href=<?php echo get_permalink() ?>/></fb:comments-count> comments
But it doesn't count the FB comments.
Is there a simple code that let me retrieve the number of comment counts?
Thanks,
Include this function somewhere in your template file :
function fb_comment_count() {
global $post;
$url = get_permalink($post->ID);
$filecontent = file_get_contents('https://graph.facebook.com/?ids=' . $url);
$json = json_decode($filecontent);
$count = $json->$url->comments;
if ($count == 0 || !isset($count)) {
$count = 0;
}
echo $count;
}
use it like this in your homepage or wherever
<?php fb_comment_count() ?>
Had the same problem, that function worked for me... if you get an error... try reading this.
The comments often don't appear here :
graph.facebook.com/?ids = [your url]
Instead they appear well in
graph.facebook.com/comments/?ids = [your url]
Hence the value of the final solution.
Answer by ifennec seems fine, but actually is not working (facebook maybe changed something and now is only returning the number of shares).
You could try to get all the comments:
$filecontent = file_get_contents(
'https://graph.facebook.com/comments/?ids=' . $url);
And count all:
$json = json_decode($filecontent);
$content = $json->$url;
$count = count($content->data);
if (!isset($count) || $count == 0) {
$count = 0;
}
echo $count;
This is just a fix until facebook decides to read the FAQ about fb:comments-count, and discovers it's not working :) (http://developers.facebook.com/docs/reference/plugins/comments/ yeah, awesome comments).
By the way, I applied the function in Drupal 7 :) Thank you very much ifennec, you showed me the way.
This works for me :
function fb_comment_count() {
global $post;
$url = get_permalink($post->ID);
$filecontent = file_get_contents('https://graph.facebook.com/comments/?ids=' . $url);
$json = json_decode($filecontent);
echo(count($json->$url->comments->data));
}
This is resolved.
<p><span class="cmt"><fb:comments-count href=<?php the_permalink(); ?>></fb:comments-count></span> Comments</p>
The problem was that I was using 'url' than a 'href' attribute in my case.
Just put this function in functions.php and pass the post url to function fb_comment_count wherever you call it on your theme files
function fb_comment_count($url) {
$filecontent = file_get_contents('https://graph.facebook.com/comments/?ids=' . $url);
$json = json_decode($filecontent);
$content = $json->$url;
echo count($content->comments->data);
}