file_put_contents need to limit number of lines - file-put-contents

New to php but learning. Getting most help from this forum. I want to see how often my web site gets a visitor and the IP address of the visitor. The following code works flawlessly. The only thing I would like to change is a way to limit the number of lines of data to say...200. Anyone willing to help?
$today = date("F j, Y, g:i:s a");
$ip = $_SERVER['REMOTE_ADDR'];
$file_data = "$ip\twas here\t$today\n";
$file_data .= file_get_contents('ip.txt');
file_put_contents('ip.txt', $file_data);

There are a number of ways; here are two and a half.
Search for up to 200 lines in $file_data and output what was found. Replace your last line with:
preg_match("/(.*\R){0,200}/", $file_data, $matches);
file_put_contents("ip.txt", $matches[0]);
Read the file into an array and output up to 200 elements therefrom. Replace your last two lines:
$file_data = array_merge([$file_data], file("ip.txt"));
file_put_contents("ip.txt", array_slice($file_data, 0, 200));
Just a variant of this where your last three lines are to be replaced:
$file_data = file("ip.txt");
array_unshift($file_data, "$ip\twas here\t$today\n");
file_put_contents("ip.txt", array_slice($file_data, 0, 200));

Related

decrypt MCRYPT_RIJNDAEL_128 ECB using openSSL [duplicate]

I have this mcrypt_encrypt call, for a given $key, $message and $iv:
$string = mcrypt_encrypt(MCRYPT_3DES, $key, $message, MCRYPT_MODE_CBC, $iv);
I'd like to change the mcrypt_encrypt call to an openssl_encrypt one, to future-proof this.
By having $mode = 'des-ede3-cbc' or $mode = '3DES'; and $options = true I get the more similar response, but not identical. Is there other way to call it to get a perfect match?
I am getting this (base64_encoded) for a lorem-ipsum $message+$key combinations, so I am starting to believe one function or the other are padding somewhat the message before encrypting...
for mcrypt:
"Y+JgMBdfI7ZYY3M9lJXCtb5Vgu+rWvLBfjug2GLX7uo="
for for openssl:
"Y+JgMBdfI7ZYY3M9lJXCtb5Vgu+rWvLBvte4swdttHY="
Tried using $options to pass OPENSSL_ZERO_PADDING, but passing anything but 1 (OPENSSL_RAW_DATA, or true) results in an empty string ...
Neither using OPENSSL_ZERO_PADDING nor OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING work... :(
I'm using "OpenSSL 1.0.2g 1 Mar 2016".
Already read this q&a, but it does not help me. Not the only one with padding troubles, but no solution in sight so far. Second answer talks about adding padding to mcrypt call, I would really want to remove padding from openssl encryption call...
mcrypt_encrypt zero-pads input data if it's not a multiple of the blocksize. This leads to ambiguous results if the data itself has trailing zeroes. Apparently OpenSSL doesn't allow you to use zero padding in this case, which explains the false return value.
You can circumvent this by adding the padding manually.
$message = "Lorem ipsum";
$key = "123456789012345678901234";
$iv = "12345678";
$message_padded = $message;
if (strlen($message_padded) % 8) {
$message_padded = str_pad($message_padded,
strlen($message_padded) + 8 - strlen($message_padded) % 8, "\0");
}
$encrypted_mcrypt = mcrypt_encrypt(MCRYPT_3DES, $key,
$message, MCRYPT_MODE_CBC, $iv);
$encrypted_openssl = openssl_encrypt($message_padded, "DES-EDE3-CBC",
$key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
printf("%s => %s\n", bin2hex($message), bin2hex($encrypted_mcrypt));
printf("%s => %s\n", bin2hex($message_padded), bin2hex($encrypted_openssl));
This prints both as equal.
4c6f72656d20697073756d => c6fed0af15d494e485af3597ad628cec
4c6f72656d20697073756d0000000000 => c6fed0af15d494e485af3597ad628cec
mcrypt_encrypt uses zeroes to pad message to the block size. So you can add zeroes to the tail of your raw data, and then encrypt the block.
Using OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING should work. If it doesn't, then you can remove padding from the decrypted data by yourself.

How to add an Image to a PDF document?

I am using iTextSharp to join multiple pdfs together that are from microsofts report viewer. page.AddImage(image__1) in the code below is throwing an error Object reference not set to an instance of an object. I am not seeing what is set to nothing. Is there a different way that I can join reports from the report viewer and add a watermark image?
mybytes = MSRptViewer1.LocalReport.Render("PDF", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
reader = New iTextSharp.text.pdf.PdfReader(mybytes)
numberOfPages = reader.NumberOfPages
currentPageNumber = 0
Dim imageFile As String = Server.MapPath("WaterMark.png")
Dim buffer As Byte() = IO.File.ReadAllBytes(imageFile)
Dim image__1 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(buffer)
image__1.SetAbsolutePosition(100, 100)
Do While (currentPageNumber < numberOfPages)
currentPageNumber += 1
doc.SetPageSize(PageSize.LETTER)
doc.NewPage()
page = writer.GetImportedPage(reader, currentPageNumber)
page.AddImage(image__1)
rotation = reader.GetPageRotation(currentPageNumber)
If (rotation = 90) Or (rotation = 270) Then
cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(currentPageNumber).Height)
Else
cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)
End If
Loop
<?php $data = file_get_contents('https://static1.squarespace.com/static/56c775ad27d4bd3fdb24775d/t/5a8b201324a694d7071662ee/1519067160925/dummy+logo.jpg');
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
?>
this method for image convert to base64 format
<?php echo '<div class="company-logo"><img src="'.$base64.'" alt="base" /></div>'; ?>
or
<img src="<?php echo $base64; ?>" alt="base" />
I have asked my Publisher, Manning Publications, to offer chapter 6 of my book for free. I know this chapter only shows you examples in Java, but all the examples were ported to C# for your convenience.
If you read this chapter, you'll find out that you're making the following mistakes:
You are using PdfReader/PdfWriter to add content to an existing document instead of PdfReader/PdfStamper. If you want a nice watermark example, take a look at the ManipulatePdf method in the StampStationery.cs example. Replace background.addTemplate() with background.addImage() to add an image instead of a background page taken from another PDF. You'll also learn more about using an image as a watermark here: How to add a watermark to a PDF file?
You are using PdfReader/PdfWriter to concatenate existing documents instead of PdfCopy. By doing so, you risk all kinds of problems: e.g. parts of pages being cut off, links being removed, annotations getting lost,... Please read my answer to this question: How to merge documents correctly?
You are ignoring the fact that PdfImportedPage is read-only. You can only add content in the context of PdfCopy after creating a PageStamp object. Please take a look at the ConcatenateStamp.cs example.
Summarized: your question is more or less a duplicate of (a combination of) other questions answered before on StackOverflow. It seems that you copy/pasted some code from a source that isn't one of the official sources of documentation.

auto excerpt from WYSIWYG

Is it possible to grab the contents of the WYSIWYG editor and save the first 100 words into the excerpt automatically? I know about excerpt_save_pre which will save the excerpt when you are in the editor, but haven't seen anything that will grab the contents from the WYSIWYG editor.
I've figured this out. The "secret" is &$_POST when the post is saved/published. That builds an array which the content can be extracted and then saved to the excerpt field using excerpt_save_pre.
I went a little further allowing control over the number of characters or the number of words, using $length, and the output is controlled on which $output section you uncomment.
The code below tested on my vanilla site as working.
function auto_insert_excerpt(){
$post_data = &$_POST;
$post_content = $post_data['content'];
$length = 15;
// This will return the first $length number of CHARACTERS
//$output = (strlen($post_content) > 13) ? substr($post_content,0,$length).'...' : $post_content;
// This will return the first $length number of WORDS
$post_content_array = explode(' ',$post_content);
if(count($post_content_array) > $length && $length > 0)
$output = implode(' ',array_slice($post_content_array, 0, $length)).'...';
return $output;
}
add_filter('excerpt_save_pre', 'auto_insert_excerpt');

Drupal filter is not working properly

I'm not sure how to ask it, so if you need anymore additional information, please ask for it!
Situation
I've got a website in three languages. I got a lot of customer cases online each connected to a sector (depending in which sector they belong). Each sector and reference has it's own unique nid.
In my template.php it's stated like this:
if ('sector' == $vars['node']->type) {
$lang = '/'.$vars['language'].'/';
$key_path = $_SERVER['REQUEST_URI'];
$key_path = substr_count($key_path, $lang) ? substr($key_path, strlen($lang)) : $key_path;
if (strpos($key_path, '?')) $key_path = substr_replace($key_path, '', strpos($key_path, '?'));
if (strpos($key_path, 'sectors-references') === 0) {
$view = views_get_view('references');
if (!empty($view)) {
$view->set_arguments((int)$vars['node']->nid);
$vars['content']['suffix'] = $view->render();
}
}
}
And yet, every sector shows me the same references... What do I have to change to get the correct reference under the right sector?
Usually arguments are passed to set_arguments using an array, if you pass a non-array the argument will probably be ignored which is why you're always getting the same result. Try:
$view->set_arguments(array((int)$vars['node']->nid));

Rss feeds: get certain amount of tweets

Is it possible to give a parameter when getting the RSS feeds to determine how many feeds it should get?
I don't want to load all the RSS feeds, but only the first 20. Is this possible?
Thanks!
You can set the limit. By executing a Loop in limit. So it will parse the xml and your program will read items in loop. Once the loop crossed the limit. Just break the loop.
$i=0;
while ($reader->read()) {
if($i>=10)
break;
else{
switch ($reader->nodeType) {
case (XMLREADER::ELEMENT):
if ($reader->localName == "item") {
$node = $reader->expand();
$dom = new DomDocument();
$n = $dom->importNode($node,true);
$dom->appendChild($n);
$sxe = simplexml_import_dom($n);
$url = (String)$sxe->url;
$title=(String)$sxe->title;
}
}
}
In the above code $i is the limiter. Where we can limit number feed to display in the page.

Resources