laravel save method not saving data completely - php

I am using Laravel to create a web bot that collects data from other websites and stores it in my MySQL database. When I want to save body I use dd($this->render($post)); and it is good. Yet, when I use $post->save() for saving my post in db, it not saving the body of my post completely and some of text is missing.
My body is at least 10000 characters and I always have this problem.
I checked text and longtext for body column type and is not there any difference...
Where is problem?
edit :
this is my index method :
public function getIndex()
{
$temp = App\Temp::where('status' , 0)->orderBy('id' , 'desc')->first();
$temp->status = 1;
$temp->save();
$post = new App\Post;
$post->title = $temp->title;
$post->link = $temp->link;
$post->desc = $temp->desc;
$post->cat_id = $temp->cat_id;
$post->url_id = $temp->url_id;
$post->body = $this->render($post);
$post->save();
echo "+";
}
When I am using dd($this->render($post)); before save, it shows full text without any problem... but after save when I fetch the body, some characters is missing from the end of post...
and this is render() method...
public function render($post)
{
echo "Start : ";
$this->ftp->createFolder('/'.$post->url_id.'/'.$post->id."/");
echo "Dir - ";
$mixed_body = $this->desc($post->title);
echo "Mix - ";
$body ="";
$body = $body . '<h3><a href='.$this->postUrl.'>'.$this->postTitle.'</a></h3>';
echo "Title - ";
while(strlen($mixed_body) > 100)
{
$body = $body . $this->randImage($post);
$body = $body . $this->randTitle();
//insert a random paragraph
$number = rand(100 , strlen($mixed_body));//temporary
$paragraph = substr($mixed_body , 0 , $number);
$mixed_body = substr($mixed_body , $number , strlen($mixed_body)-$number);
$body = $body . '<p>' . $paragraph . '</p>';
echo "P|";
}
echo "\nDone : ".strlen($body);
return $body;
}
others methods in render() are appending some text to $body and those are not important.
and my model :
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
public function tags()
{
return $this->hasMany('App\Tag');
}
}

I find my problem reason...
sometimes I have a character like � in my body that laravel not saves the characters after it...
I find to use this line to delete � character...
$post->body = mb_convert_encoding($this->render($post), 'UTF-8', 'UTF-8');
thanks from all to help me, I have a very bad headache and want to rest...
thanks from all again, good night/morning/afternoon (according to your time-zones) :)

Related

php how to parse variable substitution without printing

how to parse variable inside string?
here is a sample code that works:
$str = "12345";
$str2 = "STR:{$str}";
$str3 = $str2;
echo $str2;
echo $str3;
but these won't work:
$otp = 12345;
$template = MessageTemplate::where('type',1)->first(); //db query
$message = $template->content; //content field, "OTP:{$otp}"
echo $message;
this code prints OTP:{$otp} instead of OTP:12345
these is what we need:
$member = Member::where('id',$id)->with('position')->with('company')->first();
$otp = 12345;
$template = MessageTemplate::where('type',1)->first(); //db query
$message = $template->content; //content field, "OTP:{$otp}"
$sms = Sms::Create(['mobile'=>$member->mobile,'message'=>$message]);
this code prints OTP:{$otp} instead of OTP:12345
Thanks for your responses. Actually what i need is to parse it before saving it to the database. the string "OTP:12345" should be written back to the databse. and also, variables are dynamic. It is a template to allow admin to customize the message, so the admin can add as many variable as he/she want. For ex:
"{$member->title},{$member->firstName}, {$member->lastName}, in order to verify that you are {$mamber->position->name} of {$member->company->name}. enter code {$otp}".
so i can't use str_replace. and also our db supports json so admin can add a custom attribute to $member
You can use mutators in your model like this
// MessageTemplate class
public function getContentAttribute($string)
{
return 'OTP:' . $string;
}
// anywhere in controller
$message = $template->content; // will call content mutator
echo $message;
The value of $template->content is just a string.
You can simply do a replace:
$message = str_replace('{$otp}', $otp, $template->content);

Make PHPBB 3.0.14 and ABBC3 compatible with PHP 7.3

I'm trying to make ABBC3 work with PHP 7.3 and PHPBB 3.0.14 since I can't move to PHPBB 3.3 due lots of issues with MODs not ported to extensions and theme (Absolution).
I have asked help in PHPBB forum without luck because 3.0.x and 3.1.x version are not supported anymore.
So after dozens of hours trying to understand bbcode functions I'm almost ready.
My code works when there's a single bbcode in message. But doesn't works when there's more bbcode or it's mixed with texts.
So I would like to get some help to solve this part to make everything work.
In line 98 in includes/bbcode.php this function:
$message = preg_replace($preg['search'], $preg['replace'], $message);
Is returning something like this:
$message = "some text $this->Text_effect_pass('glow', 'red', 'abc') another text. $this->moderator_pass('"fernando"', 'hello!') more text"
For this message:
some text [glow=red]abc[/glow] another text.
[mod="fernando"]hello![/mod] more text
The input for preg_replace above is like this just for context:
"some text [glow=red:mkpanc3g]abc[/glow:mkpanc3g] another text. [mod="fernando":mkpanc3g]hello![/mod:mkpanc3g]"
So basically I have to split this string in valid expressions to apply eval() then concatenate everything. Like this:
$message = "some text". eval($this->Text_effect_pass('glow', 'red', 'abc');) . "another text " . eval($this->moderator_pass('"fernando"', 'hello!');). "more text"
In this specific case there's also double quotes left in '"fernando"'.
I know is not safe apply eval() to user input so I would like to make some type of preg_match and/or preg_split to get values inside of () to pass as parameter to my functions.
The functions are basically:
Text_effect_pass()
moderator_pass()
anchor_pass()
simpleTabs_pass()
I'm thinking in something like this (Please ignore errors here):
if(preg_match("/$this->Text_effect_pass/", $message)
{
then split the string and get value inside of() and remove extra single or double quotes.
after:
$textEffect = Text_effect_pass($value[0], $value[1], $value[2]);
Finally concatenate everything:
$message = $string[0] .$textEffect. $string[1];
}
if(preg_match("/$this->moderator_pass/", $message)
{
.....
}
P.S.: ABBC3 is not compatible with PHP 7.3 due usage of e modifier. I have edited everything to remove the modifier.
Here you can see it working separately:
bbcode 1
bbcode 2
Can someone give me some help please?
After long time searching for a solution for this problem I found this site that helped me build the regex.
Now I have managed to solve the problem and I have my forum fully working with PHPBB 3.14, PHP 7.3 and ABBC3.
My solution is:
// Start Text_effect_pass
$regex = "/(\\$)(this->Text_effect_pass)(\().*?(\')(,)( )(\').*?(\')(,)( )(\').*?(\'\))/is";
if (preg_match_all($regex, $message, $matches)) {
foreach ($matches[0] as $key => $func) {
$bracket = preg_split("/(\\$)(this->Text_effect_pass)/", $func);
$param = explode("', '", $bracket[1]);
$param[0] = substr($param[0], 2);
$param[2] = substr($param[2], 0, strrpos($param[2], "')"));
$effect = $this->Text_effect_pass($param[0], $param[1], $param[2]);
if ($key == 0) {
$init = $message;
} else {
$init = $mess;
}
$mess = str_replace($matches[0][$key], $effect, $init);
}
$message = $mess;
} // End Text_effect_pass
// Start moderator_pass
$regex = "/(\\$)(this->moderator_pass)(\().*?(\')(,).*?(\').*?(\'\))/is";
if (preg_match_all($regex, $message, $matches)) {
foreach ($matches[0] as $key => $func) {
$bracket = "/(\\$)(this->moderator_pass)/";
$bracket = preg_split($bracket, $func);
$param = explode("', '", $bracket[1]);
$param[0] = substr($param[0], 2);
$param[1] = substr($param[1], 0, strrpos($param[1], "')"));
$effect = $this->moderator_pass($param[0], $param[1]);
if ($key == 0) {
$init = $message;
} else {
$init = $mess;
}
$mess = str_replace($matches[0][$key], $effect, $init);
}
$message = $mess;
} // End moderator_pass
If someone is interested can find patch files and instructions here.
Best regards.

Moodle email templates

I am working on a Learning Management System build upon Moodle. I want to add an email header and footer for each email.
I did some change in Moodle for adding an image in ./lib/moodlelib.php as follows:
function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '', $attachment = '', $attachname = '',
$usetrueaddress = true, $replyto = '', $replytoname = '', $wordwrapwidth = 79) {
global $CFG, $PAGE, $SITE;
// Append image at top
if($messagehtml){
$tmp = $messagehtml;
// Added image here
$messagehtml = '<img src="'.$CFG->wwwroot.'/images/logo.png" alt="LMS" /><br/>';
// $messagehtml = $image;
$messagehtml .= $tmp;
}
if($messagetext){
$tmp = $messagetext;
// Added image here
$messagetext = '<img src="'.$CFG->wwwroot.'/images/logo.png" alt="LMS" /><br/>';
// $messagehtml = $image;
$messagetext .= $tmp;
}
....................
but I want the header and footer as fixed templates. Please help me.
You could create a message header in a language file (in English either directly under /lang/en or in a plugin) and then add the following string in the language file:
$string ['message_header'] = '<p> write here whatever your style and HTML structure you want in your header</p>
adding your image as well <img src="'.$CFG->wwwroot.'/images/logo.png" alt="LMS" />';
Then you could write a string for your footer as well:
$string ['message_footer'] = 'Your HTML footer here';
And finally you could insert your strings in the message:
$message = 'here your body';
// insert message header - if your language string is in /lang/moodle.php:
$message = get_string ( 'message_header') . $message;
// insert message footer - if your language string is in your /local/myplugin:
$message .= get_string ( 'message_footer', 'local_my_plugin' );
This way your header and footer may be customized at will if you change them directly in the language file (or, in the DB. See here).
a little bit too late, but i hope it helps others:
https://docs.moodle.org/dev/Themed_emails just look for e-mail templates in /var/www/html/moodle/lib/templates. The template with a name email_html.mustache should be the right one.

str_replace PHP script can't handle foreign characters such as umlauts robustly

The following script does not always correctly catch and convert foreign characters. Could someone show me what I'm missing to get it to be more robust?
<?php
include("../index_head.inc.php");
$content = implode("",(#file("current.txt")));
$url = "http://XXXXXX.html?no_body=1";
$content = file_get_contents($url,'r');
if (isset($_GET['showcurrent']) && $_GET['showcurrent'] == '')
{
$content = substr($content,1,strpos($content,"<hr ")-1);
}
else
{
$content = str_replace("<br style=\"clear:both\" />\n</p>", "</p>",$content);
$content = str_replace("ck1\"><img", "ck1\" target=_blank><img",$content);
};
$content = str_replace("<h3>current</h3>", "",$content);
echo "<div id=\"service\" style=\"width: 660px;padding-left:5px\">",str_replace("current.html","current.html",$content),"</div>";
include("../index_footer.inc.php");
?>
New information: Pekka, you gave me the idea to check how the page emits without str_replace():
<?php
include("../index_head.inc.php");
$content = implode("",(#file("current.txt")));
$url = "XXXXXX.html?no_body=1";
$content = file_get_contents($url,'r');
echo "<div id=\"service\" style=\"width: 660px;padding-left:5px\">",$content,"</div>";
It seems the problem lies elsewhere because I get the same mangling even without using str_replace()! If you can help me get this sorted out, I would sure appreciate it. I have seen your wish list. ;)
Did you include the charset in php?
try this:
header('Content-Type: text/html; charset=utf-8');
If not working check if your file is already saved in utf8 before str replace:
utf8_encode ( string $data );
In the opposite case use:
utf8_decode( string $data );
Hope it helps!
Thank you SBO - It sure did help! I simply changed the code to:
<?php
include("../index_head.inc.php");
$content = implode("",(#file("current.txt")));
$url = "http://XXXXXX.html?no_body=1";
$content = file_get_contents(utf8_encode($url),'r');
if (isset($_GET['showcurrent']) && $_GET['showcurrent'] == '')
{
$content = substr($content,1,strpos($content,"<hr ")-1);
}
else
{
$content = str_replace("<br style=\"clear:both\" />\n</p>", "</p>",$content);
$content = str_replace("ck1\"><img", "ck1\" target=_blank><img",$content);
};
$content = str_replace("<h3>current</h3>", "",$content);
echo "<div id=\"service\" style=\"width: 660px;padding-left:5px\">",str_replace("current.html","current.html",utf8_decode($content)),"</div>";
include("../index_footer.inc.php");
?>
and everything is working fine.
Thank you very much for your help.

Adding url() breaks hook_mail implementation

I'm writing a module in Drupal-7 that dynamically sends a one-time login link to guests. Everything fires fine until I add the link to the $message array, when it chokes. If I do a dpm($message) the link appears in the $message['body'] array, as I would expect. If I comment out the line with the url() function, everything works as it should. Why is php/Drupal choking on this silly little link?
/*
* Implement hook_mail().
*/
function rsvp_mail($key, &$message, $params) {
switch($key) {
case "send invite" :
$timestamp = REQUEST_TIME;
$account = $params['account'];
$message['subject'] = "And invitation for $account->name";
$message['body'][] = 'Some body text.';
$message['body'][] = 'Some more text!';
//here's the line that's breaking my brain:
$message['body'][] = url( 'http://wedding.juicywatermelon.com/rsvp/' . $account->uid . "/" . $timestamp . "/" . md5($account->pass . $timestamp) . "/" . 'user/' . $account->uid . '/edit/Wedding');
break;
}
}
ps - I had the code to generate the link in a seperate function call and moved it to the hook implementation for brevity. This, however had no effect on the behaviour.
and the code that generates the email:
function rsvp_mail_send($account) {
$module = 'rsvp';
$from = "email#gmail.com";
$key = "send invite";
$params['account'] = $account;
$to = $account->mail;
$language = language_default();
$send = TRUE;
$result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
}
You need to add an extra argument to the url() function which is called options, it's an array and in this array use the key 'absolute' and set it to TRUE to indicate that the URI that you pass as a first argument is an absolute URL.
See the documentation page for more information:
http://api.drupal.org/api/drupal/includes--common.inc/function/url/7

Categories