Python script not found on server - wordpress

I am working on a Wordpress plugin embedding a Python script. I have uploaded the Python script to my server, and at first I got an error message on the web page, where my script is to be run, saying "permission denied" (I have redirected stderr to stdout to enable me to see the error messages). When I added execution permissions I instead get a "not found" message (done via Filezilla).
I can see the script is on the server in the correct folder, and it was apparently found earlier when the permissions were wrong. How can I get my plugin to find the Python file?
Here is the relevant PHP function in the plugin:
function embed_python( $attributes )
{
$data = shortcode_atts(
[
'file' => 'hello.py'
],
$attributes
);
$handle = popen( __DIR__ . '/' . $data['file'] . ' 2>&1', 'r' );
$read = '';
while ( ! feof( $handle ) )
{
$read .= fread( $handle, 2096 );
}
pclose( $handle );
return $read;
}

Turns out it was as simple as the server I was using (Loopia) needed the Python script in a certain folder to be able to find/run it, as well as several settings changed, see this Swedish page: https://support.loopia.se/wiki/python/.

Related

Wordpress Cron - Call External API - Save JSON File

i thought i would reach out to get some guidance on a little thing i am working on.
What i would like to do within Wordpress:
Call external API (with token header)
Get the results of the api and save it into a file in wpallimport's upload folder
I would assume i can just make a simple WP plugin and within the 'activate' hook for the plugin:
create a wp-cron (as i would like it to run every day) for the following:
$url = 'the-api-url';
$data = wp_remote_get( $url ,
array('headers' => array( 'Token' => 'tokenkey')
));
$jsonfile = $data['body'];
global $wp_filesystem;
if (empty($wp_filesystem)) {
require_once (ABSPATH . '/wp-admin/includes/file.php');
WP_Filesystem();
}
$file = '/wp-content/uploads/wpallimport/files/JSONFILE.JSON';
$wp_filesystem->put_contents($file, $jsonfile);
However i am not having success with the above (with the correct API url and token etc obviously)
Thanks in advance!

How do I upload PowerPoint slideshow files in Wordpress?

I have some PowerPoint slideshow files, .ppsx, with mime-type application/vnd.openxmlformats-officedocument.presentationml.slideshow, that I want to upload to WordPress. However, when I try to upload it to the media browser, I get the error "Sorry, this file type is not permitted for security reasons.".
This is despite the fact that .ppsx files are in the list of allowed file types and mimetypes.
When you upload a file, WordPress does some security checks on the file in the wp_check_filetype_and_ext function in wp-include/functions.php:2503. Part of these checks is to validate the given mimetype of the file with the mimetype that PHP detects, using the PHP function finfo_file().
However, finfo_file() isn't always accurate, and its results are often OS dependent. In the specific case of .ppsx files, finfo_file() can read the mimetype as application/vnd.openxmlformats-officedocument.presentationml.presentation. WordPress sees this as a potential security risk because it doesn't match the given mimetype for that file extension and shuts down the upload.
wp_check_filetype_and_ext() also has a filter, and we can use this to our advantage:
function my_check_filetype_and_ext( $info, $file, $filename, $mimes, $real_mime )
{
if ( empty( $check['ext'] ) && empty( $check['type'] ) )
{
$secondaryMimetypes = ['ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation'];
// Run another check, but only for our secondary mime and not on core mime types.
remove_filter( 'wp_check_filetype_and_ext', 'my_check_filetype_and_ext', 99, 5 );
$info = wp_check_filetype_and_ext( $file, $filename, $secondaryMimetypes );
add_filter( 'wp_check_filetype_and_ext', 'my_check_filetype_and_ext', 99, 5 );
}
return $info;
}
add_filter( 'wp_check_filetype_and_ext', 'my_check_filetype_and_ext', 99, 5 );
In vanilla WordPress, there is no way to have multiple mimetypes for a single filetype. The above filter runs the mimetype checks again for a secondary set of filetype/mimetype pairs if it fails the first set of pairs. By allowing .ppsx files with the presentation mimetype, you can now upload .ppsx files!
You need to add some code in your configure.php file to upload any type of
define( 'ALLOW_UNFILTERED_UPLOADS', true );
Add this in your configure.php file and you will be able to upload any file format.
or you can follow this link
Following code work fine for me.
function wcAddCustomFileTypeAndExt( $info, $file, $filename, $mimes, $real_mime )
{
if (strpos($filename, '.ppsx') !== false)
{
$info['ext'] = 'ppsx';
$info['type'] = 'application/vnd.openxmlformats-officedocument.presentationml.slideshow';
}
return $info;
}
add_filter('wp_check_filetype_and_ext','wcAddCustomFileTypeAndExt', 99, 5 );

Parse error: syntax error, unexpected '[' in /wp-content/plugins/woocommerce-simply-order-export/main.php

Godaddy migrated my server and I have a site on wordpress / woocommerce that has the woocommerce-simply-order-export plugin. After the migration the site is generating this error:
Parse error: syntax error, unexpected '[' in /fakepath/wp-content/plugins/woocommerce-simply-order-export/main.php
The problem is when I install the site on my local server (with the same code), the website runs without problem.
I have tried renaming the plugin so that the site does not use it, but the server does not generate any response when compiling being empty, without any line of code when compiling the HTML
Any help about what may be happening?
Probably you have different versions of PHP. go to that file, and try replacing
$whatever = [];
with
$whatever = array();
[] notation for arrays isn't supported in older versions of PHP < 5.4
Also, if this is not the case, it could be that you need to deactivate/activate woocommerce as this plugin checks if Woocommerce is enabled.
From woocommerce-simply-order-export/main.php
if (
in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) )
&& ( -1 !== version_compare( get_plugin_data( WP_PLUGIN_DIR.'/woocommerce/woocommerce.php' )['Version'], '3.0.0' ) )
) { ...

Must manually load PHP ActiveRecord models

I'm drawing a blank. I have code working locally (which is MAMP). When moving to a nginx ubuntu box (running php-fpm), for some reason, phpactiverecord is acting up.
I finally traced it down to this - All of my model classes, I have to load manually. If I add a require_once() underneath my code, then it works fine. If I don't, then I get errors like:
PHP Fatal Error: Class not found ... on the models I've created..
Does anyone have ANY idea what direction I could troubleshoot this in? I checked permissions to the models folder (which is not in the public root), echo'd out the path that is sent over to cfg->set_model_directory is correct, etc..
This sound like a nginx or php thing? I'm guessing nginx since this works on my MAMP?
Doesn't work:
ActiveRecord\Config::initialize(
function ($cfg) {
$cfg->set_model_directory(BASE_PATH . '/models');
$cfg->set_connections(
array(
'development' => 'mysql://blah:removed#localhost/com_dbname'
)
);
}
);
Works:
ActiveRecord\Config::initialize(
function ($cfg) {
$cfg->set_model_directory(BASE_PATH . '/models');
$cfg->set_connections(
array(
'development' => 'mysql://blah:removed#localhost/com_dbname'
)
);
}
);
require_once(BASE_PATH . '/models/model1.php');
require_once(BASE_PATH . '/models/model2.php');
Update
Adding in actual code to help identify issue:
require_once ('../lib/php-activerecord/ActiveRecord.php');
ActiveRecord\Config::initialize(
function ($cfg) {
$cfg->set_model_directory('/var/www/uc1/models');
$cfg->set_connections(
array(
'development' => 'mysql://test_usr:test_pwd#localhost/test_db'
)
);
}
);
require_once ('/var/www/uc1/models/ucurls.php'); //Name of model file. Must manually include to get this to work on my nginx server.
$_record = UCUrls::find_by_urlkey('example.com/123');
echo "urlkey=" . $_record->urlkey;
I solved this issue in windows adding a line in the file ActiveRecord.php,
in the function activerecord_autoload($class_name)
at the line 39 or 40
$file = "$root/$class_name.php";
//add this line
$file = strtolower($file);
Set trace in ActiveRecord.php too look where are ActiveRecord is searching for models.
But I think your issue is in filesystem - Mac OS X by default uses Case Insensitive filesystem, while Ubuntu's Case Sensitive filesystem.
So your model UCUrls should be in file /var/www/uc1/models/UCUrls.php, not in /var/www/uc1/models/ucurls.php

Can't add language translation support to Wordpress plugin

I am trying to add language/translation support for my Wordpress plugin using POEdit software to create .po files, but the code isn't working and there are no screen error printing out.
My plugin is located in /plugins/site-status/ and the languages directory is located as /plugins/site-status/languages/. In /languages/ directory all .po files have names such as site-status-en_US.po (the unique identifier is called site-status)
Here is the language support code:
function status_language_init() {
load_plugin_textdomain( 'site-status', false, 'site-status/languages/' );
}
add_action('init', 'status_language_init');
Here is the test code for outputing the default/translated text:
echo _x( 'test', 'site-status' );
I very much look forward for your help!
Thanks in advance and with best wishes,
WHOAMi
To debug this, check the return value of load_plugin_textdomain(). And don’t rely on a directory name of your plugin. The user can change it.
$path = basename( dirname( __FILE__ ) ) . '/languages';
$lang_loaded = load_plugin_textdomain( 'site-status', FALSE, $path );
// die harder!
! $lang_loaded and die( $path . ' not found' );

Resources