php POST data to datetime - php

I have an issue with my project. I want to create a .ics file with start and endtime defined by the user of the website. So I get a string for the start and the end year and for the start/end time as well. Now I want to create a DateTime object from this data. My problem is that I just get the timestamp from right now and not from the string made:
$eventStartDay = $_POST["eventStartDay"];
$eventStartMonth = $_POST["eventStartMonth"];
$eventStartYear = $_POST["eventStartYear"];
$eventStartHour = $_POST["eventStartHour"];
$eventStartMinute = $_POST["eventStartMinute"];
$eventStartSecond = $_POST["eventStartSecond"];
$startDateTime = $eventStartYear . "-" . $eventStartMonth . "-"
. $eventStartDay . "T" . $eventStartHour . ":"
. $eventStartMinute . ":" . $eventStartSecond . "+01:00";
$eventEndDay = $_POST["eventEndDay"];
$eventEndMonth = $_POST["eventEndMonth"];
$eventEndYear = $_POST["eventEndYear"];
$eventEndHour = $_POST["eventEndHour"];
$eventEndMinute = $_POST["eventEndMinute"];
$eventEndSecond = $_POST["eventStartSecond"];
$endDateTime = $eventEndYear . "-" . $eventEndMonth . "-"
. $eventEndDay . "T" . $eventEndHour . ":"
. $eventEndMinute . ":" . $eventEndSecond . "+01:00";
$cal = new SimpleICS();
$cal->addEvent(function($e) {
$e->startDate = new DateTime($startDateTime);
$e->endDate = new DateTime($endDateTime);
I checked the strings with var_dump() and I got the right answers. They should look like this: '2016-01-18T19:00:00+01:00'

Quoting the PHP manual on Anonymous functions:
Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.
So you need to close over the DateTime objects, e.g. you need to do
$cal->addEvent(function($e) use ($startDateTime, $endDateTime) {
$e->startDate = new DateTime($startDateTime);
$e->endDate = new DateTime($endDateTime);
}

Related

If not variable condition is not working

I'm trying to check whether the variable value is false or true, here is my code
$print_ready_flag = $_GET['print_ready']; // i'm passing it as either true or false
echo $print_ready_flag ; // I'm getting correct value
What i'm trying to do is,
if ($print_ready_flag == false) {
file_name = "albumpdf_" . $Order['products_name'] . "_" . $OrderId . "_" . $OrderPId . ".pdf";
}
when doing like above expression is evaluating, But when i tried
if (!$print_ready_flag) {,
if (!(bool)$print_ready_flag)
. expression is not evaluating. Is there any way to evaluate the expression with out using comparison operator.
Well your all conditions are looks fine. you must need to add php `error_reporting() in your code, this will help you to find syntax and warnings.
You have a typo here:
// missing $ sign
file_name = "albumpdf_" . $Order['products_name'] . "_" . $OrderId . "_" . $OrderPId . ".pdf";
And its better to use print_ready=0 instead of print_ready=false for $_GET.
Otherwise, you must need to check with === for value+dataType
$print_ready_flag = $_GET['print_ready'];
echo $print_ready_flag ; // I'm getting correct value
$print_ready_flag = filter_var($print_ready_flag, FILTER_VALIDATE_BOOLEAN);
if ($print_ready_flag == false) {
file_name = "albumpdf_" . $Order['products_name'] . "_" . $OrderId . "_" . $OrderPId . ".pdf";
}

WkHtmlToPdf Passing Variables via shell_exec()

Here is a sample of my script
$clientid = $_POST['clientid'];
$from_day = $_POST['stat_from_day'];
$from_month = $_POST['stat_from_month'];
$from_year = $_POST['stat_from_year'];
$to_day = $_POST['stat_to_day'];
$to_month = $_POST['stat_to_month'];
$to_year = $_POST['stat_to_year'];
$from_date_string = $from_day . ' ' . $from_month . ' ' . $from_year ;
$to_date_string = $to_day . ' ' . $to_month . ' ' . $to_year ;
$baseurl = "http://www.test.com/";
$part1 = "?Search=" . $clientid . " from_day=" . $from_day . " from_month=" . $from_month . " from_year=" . $from_year ;
$part2 = " to_day=" . $to_day . " to_month=" . $to_month . " to_year=" . $to_year ;
$time = mktime();
$formatted_time = date("d_M_Y", $time);
$command = "xvfb-run -a /usr/bin/wkhtmltopdf --ignore-load-errors";
$url = $baseurl . $part1 . $part2 ;
$html = file_get_contents($url);
$output_dir = '/var/www/stats/pdf/';
$output = $clientid . '_Search_Export_' . $formatted_time . rand(10000, 99999) . '.pdf';
$generate = shell_exec($command . ' ' . $url . ' ' . $output_dir . $output) ;
The problem i seem to be having is with the $command, basically when it runs wkHTMLtoPDF it runs it via a Command Line, and &variable= bit causes the script to error as via command line & is another command, my question is how do i get the variables to be passed correctly so that the script this then sends to, will be able to use $_GET variables that i require for the script to then work ?
I have done a bit of looking up and found something along the lines of using $argv1;
Replacing $_GET to make it work from command line
However i cannot seem to find a reference that closely matches my needs.
Change this:
$url = $baseurl . $part1 . $part2 ;
To this:
$url = "\" . $baseurl . $part1 . $part2 . "\";
Actually wkhtmltopdf accepts and passes POST data to the server-side page being printed/exported to pdf.
All you need is --post fieldName value.
xvfb-run -a /usr/bin/wkhtmltopdf --ignore-load-errors --post username blablabla --post bla2 answer2
You can have as many as that in the command to pass as many post parameters as you want

Why my PHP defined constants are not evaluated when file is executed using command line?

I am creating a plugin for a WP site and I have to make an initial import of a huge amount of files from another directory. (The plugin is working well, etc etc.) I want to run the import using command line. Everything is OK if executing on local (Windows), but when I execute from Linux bash the defined constants are not evaluated but printed as a string.
define("ASCOR_RECORDINGS_TBL", $wpdb->prefix . "recordings");
Is there any setting I have to make to enable evaluation of PHP defined constants? My website is running on Dreamhost server.
When I execute the folowing line:
[myserver]$ /usr/local/php5/bin/php /home/path/to/import.php
I get:
WordPress database error Table 'db_ro.ASCOR_RECORDINGS_TBL' doesn't exist for query INSERT INTO `ASCOR_RECORDINGS_TBL` ...........
The content of the file I execute:
<?php
require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/wp-load.php";
require_once "class/AscorDbHelper.php";
$origin = realpath("/path/to/files");
$upload_dir = wp_upload_dir();
$subdir = $upload_dir['subdir'];
mkdir(ASCOR_UPLOAD_DIR . "/" . $subdir, 777, true);
require_once $origin . "/classes/PConfFiles.inc.php";
$db = new AscorDbHelper();
$cf = new PConfFiles('/^PC_([0-9\-]+)_(.*)\.([a-z0-9]{2,4})$/i', $origin);
$list = $cf->getFilesList();
$catPC = $db->addCategory("Special");
$catOther = $db->addCategory("Other");
if($list){
$pc = $db->addAuthor("Ciprian V.");
foreach($list as $rec){
$fileUrl = $subdir . "/" . $rec[0];
$desc = str_replace("-", " ", $rec[2]);
copy(realpath($origin . "/" . $rec[0]), ASCOR_UPLOAD_DIR . "/" . $fileUrl );
$db->addRecording($fileUrl, $catPC->id, $pc->id, $desc, null, $rec[1]);
echo "Added: " . $rec[0] . "\n";
}
}
$cf = new PConfFiles('/^([0-9\-]+)\_([^\_]+)_(.*)\.([a-z0-9]{2,4})$/i', $origin);
$list = $cf->getFilesList();
if($list){
foreach($list as $rec){
$authorName = str_replace("-", " ", $rec[2]);
$date = $rec[1];
$desc = str_replace("-", " ", $rec[3]);
$fileUrl = $subdir . "/" . $rec[0];
$authorId = $db->getAuthorIdOrSaveIt($authorName);
copy(realpath($origin . "/" . $rec[0]), ASCOR_UPLOAD_DIR . "/" . $fileUrl );
$db->addRecording($fileUrl, $catOther->id, $authorId, $desc, null, $date);
echo "Added: " . $rec[0] . "\n";
}
}
echo "done";
The constants are defined in the main file of the plugin:
define("ASCOR_RECORDINGS_TBL", $wpdb->prefix . "recordings");
define("ASCOR_RECORDINGS_AUTHORS_TBL", $wpdb->prefix . "recordings_authors");
define("ASCOR_RECORDINGS_CATEGORIES_TBL", $wpdb->prefix . "recordings_categories");
define("ASCOR_RECORDS_PER_PAGE", 50);
define("ASCOR_EXTEND_DIR", dirname(__FILE__));
define("ASCOR_EXTEND_URL", plugins_url("", __FILE__));
define("ASCOR_NOTIFY_UPDATED", "updated");
define("ASCOR_NOTIFY_ERROR", "error");
define("ASCOR_UPLOAD_DIR", ABSPATH . "/wp-content/uploads/recordings");
Is the file loaded? Introduce a parse error into it and see if it fails. And try it on Windows too.

Creating a directory in PHP based on a bunch of variables

I've been trying to create a directory following a specific structure, yet nothing appears to be happening. I've approached this by defining multiple variables as follows:
$rid = '/appicons/';
$sid = '$artistid';
$ssid = '$appid';
$s = '/';
and the function I've been using runs thusly:
$directory = $appid;
if (!is_dir ($directory))
{
mkdir($directory);
}
That works. However, I want to have the following structure in created directories: /appicons/$artistid/$appid/
yet nothing really seems to work. I understand that if I were to add more variables to $directory then I'd have to use quotes around them and concatenate them (which gets confusing).
Does anyone have any solutions?
$directory = "/appicons/$artistid/$appid/";
if (!is_dir ($directory))
{
//file mode
$mode = 0777;
//the third parameter set to true allows the creation of
//nested directories specified in the pathname.
mkdir($directory, $mode, true);
}
This should do what you want:
$rid = '/appicons/';
$sid = $artistid;
$ssid = $appid;
$s = '/';
$directory = $rid . $artistid . '/' . $appid . $s;
if (!is_dir ($directory)) {
mkdir($directory);
}
The reason your current code doesn't work is due to the fact you're trying to use a variable inside a string literal. A string literal in PHP is a string enclosed in single quotes ('). Every character in this string is treated as just a character, so any variables will just be parsed as text. Unquoting the variables so your declarations look like the following fixes your issue:
$rid = '/appicons/';
$sid = $artistid;
$ssid = $appid;
$s = '/';
This next line concatenates (joins) your variables together into a path:
$directory = $rid . $artistid . '/' . $appid . $s;
Concatenation works like this
$directory = $rid.$artistid."/".$appid."/"
When you're assigning one variable to another, you don't need the quotes around it, so the following should be what you're looking for.
$rid = 'appicons';
$sid = $artistid;
$ssid = $appid;
and then...
$dir = '/' . $rid . '/' . $sid . '/' . $ssid . '/';
if (!is_dir($dir)) {
mkdir($dir);
}

saving contents with file put contents

i'm trying to get google steetview tiles and save them. But file_get_contents doesn't like my url:
here's my code:
$img = "http://cbk0.google.com/cbk?output=tile&panoid="+$panorama_id+"&zoom=3&x="+$i+"&y='"+$x+"'";
$url = ""+$panorama_id+"Xval"+$i.+"Yval"+$x+".jpg";
file_put_contents($url, file_get_contents($img));
You're using + for concatenation. That's not PHP. You need the dot . operator instead:
$img = "http://cbk0.google.com/cbk?output=tile&panoid=" . $panorama_id . "&zoom=3&x=" . $i . "&y='" . $x . "'";
$url = $panorama_id . "Xval" . $i . "Yval" . $x . ".jpg";
See string operators in the PHP manual.
Alternatively, with double quoted strings, variables within the string will be parsed. See the manual page for the string type.
$img = "http://cbk0.google.com/cbk?output=tile&panoid=$panorama_id&zoom=3&x=$i&y='$x'";
$url = "${panorama_id}Xval${i}Yval${x}.jpg";

Categories