Bash vs CMD when using imaplib - php

I am trying to run a php script that will call a python script that calls imaplib to pull some files from my email. When I run the python script standalone in vscode (bash terminal), it works as designed. When I run the PHP script that calls the python script, the following error is produced.
Traceback (most recent call last):
File "C:/xampp/htdocs/report/pythonfile/parse_script_og.py", line 43, in <module>
mail = imaplib.IMAP4_SSL('imap.googlemail.com')
AttributeError: module 'imaplib' has no attribute 'IMAP4_SSL'
As a test, I echoed out the command and pasted it into Git Bash. The files were pulled and added to the folder specified. When I pasted the command into CMD, the error above was produced. What settings am I missing?
I am using XAMPP, VSCode, PHP 7.3.4, Python 3.7.1
From recommendation below I added "import ssl" to my python script, I received the following error:
"Traceback (most recent call last):"
" File "C:/xampp/htdocs/report/pythonfile/parse_script_og.py", line 2, in <module>"
" import ssl"
" File "C:\Users\Garrett\Anaconda3\envs\snowflakes\lib\ssl.py", line 98, in <module>"
" import _ssl # if we can't import it, let the error propagate"
"ImportError: DLL load failed: The specified module could not be found."
so then I went to my python environment, and tried to "pip install ssl" and I got the following error.
(snowflakes) C:\Users\Garrett>pip install ssl
Collecting ssl
Using cached https://files.pythonhosted.org/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz
ERROR: Complete output from command python setup.py egg_info:
ERROR: Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\Garrett\AppData\Local\Temp\pip-install-abzxmk8x\ssl\setup.py", line 33
print 'looking for', f
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('looking for', f)?
----------------------------------------
ERROR: Command "python setup.py egg_info" failed with error code 1 in C:\Users\Garrett\AppData\Local\Temp\pip-install-abzxmk8x\ssl\
(snowflakes) C:\Users\Garrett>
Another Update
The following command is what is executed in PHP (exec()). The input folder will contain files pulled from email. The output folder will contain a singular file created from the files pulled from email. If I paste this command into GitBash, everything works as planned. If I paste the following command into CMD, I get the error shown above.
C:/Users/Garrett/Anaconda3/python.exe C:/xampp/htdocs/report/pythonfile/parse_script_og.py C:/xampp/htdocs/report/input/ C:/xampp/htdocs/report/output/out.csv John Doe 06-Jan-2018 16-Jan-2019

IMAP4_SSL is only present if Python's ssl module is available (you can test with import ssl). Try upgrading your server's python version, and/or installing the ssl module.
https://docs.python.org/3/library/ssl.html
EDIT 1:
There’s also a subclass for secure connections:
class imaplib.IMAP4_SSL(host='', port=IMAP4_SSL_PORT, keyfile=None, certfile=None, ssl_context=None)
This is a subclass derived from IMAP4 that connects over an SSL encrypted socket (to use this class you need a socket module that was compiled with SSL support).
If host is not specified, '' (the local host) is used. If port is omitted, the standard IMAP4-over-SSL port (993) is used. ssl_context is a ssl.SSLContext object which allows bundling SSL configuration options, certificates and private keys into a single (potentially long-lived) structure. Please read Security considerations for best practices.
keyfile and certfile are a legacy alternative to ssl_context - they can point to PEM-formatted private key and certificate chain files for the SSL connection. Note that the keyfile/certfile parameters are mutually exclusive with ssl_context, a ValueError is raised if keyfile/certfile is provided along with ssl_context.
Changed in version 3.3: ssl_context parameter added.
Changed in version 3.4: The class now supports hostname check with ssl.SSLContext.check_hostname and Server Name Indication (see ssl.HAS_SNI).
Deprecated since version 3.6: keyfile and certfile are deprecated in favor of ssl_context. Please use ssl.SSLContext.load_cert_chain() instead, or let ssl.create_default_context() select the system’s trusted CA certificates for you.
https://docs.python.org/3/library/imaplib.html
EDIT 2:
you have to use full path for the python and for your file. you may find the former from the which python command, that most likely outputs '/usr/bin/python' and you should already know the latter. so your command would look like this:
$mystring = exec('/usr/bin/python /home/user/testing.py');
and you should make sure your python script has all appropriate permissions, because your web-server most probably is running as a different user, so permissions should be "-rwxrwxr-x" or something close.
Python Script Failing to Execute from PHP exec()

Related

How to change php -s access log/output format

I have a development PHP server running the php -S command in docker and I would like to change the access log/output format. I would like to include some more details, like the http method etc. I've been trying to find some details about it online, but it seems not possible. I've checked the php.ini file and it contains no such configuration.
Is it even possible?
I am using php 5.6.
Thanks
The log of php -S is produced by the function php_cli_server_log_response() defined in sapi/cli/php_cli_server.c at line 1098.
It uses hard-coded formats to display the remote IP address, response status code and the request URI, followed by a message and an error message that includes the file path and the line number, if the script was terminated because of an unhandled runtime error.
There is currently no way to change the format using php.ini or the command line. You can, however, fork your own copy of PHP, change its code and compile it on your system.

Check if a php script is executed in fpm environment [duplicate]

I use shared hosting.
There is possible to find out whether PHP is running via fastCGI (or maybe CGI) or as Apache module mod_php?
Is it possibly to find out by myself, without asking the hoster?
That's the Server API row on top of phpinfo()'s output:
However, please note that it won't necessarily tell you the exact version of Apache or the exact CGI handler. It just describes the SAPI in use.
You can also call the php_sapi_name() function (or the PHP_SAPI constant, which provides the same info):
Description
string php_sapi_name ( void )
Returns a lowercase string that describes the type of interface (the
Server API, SAPI) that PHP is using. For example, in CLI PHP this
string will be "cli" whereas with Apache it may have several different
values depending on the exact SAPI used
It's still a good idea to check your HSP's documentation because it possible to have several PHP versions available.
Remember you need to run phpinfo() from the same environment you want to check (web server won't tell you about command line and vice-versa):
C:\>php -i | findstr /C:"Server API"
Server API => Command Line Interface
$ php -i | grep 'Server API'
Server API => Command Line Interface
You can use the link below:
How to determine php is running as php cgi or apache module?
or create a file info.php and type
<?php
phpinfo();
?>
now run file with your domain name.
find Server API on file and it show you PHP is running on server with CGI OR Apache
Security consideration: Make sure to delete the file which outputs phpinfo() especially if the website is or is going to be hosted online. The information shown there is a gold mine for hackers.

Unable to run inkscape in PHP shell_exec() command

I have a design tool extension that is used on a website I'm working on. The design tool uses inkscape command line to export images. There is a php interface to work with the command line operations which ultimately calls shell_exec($inkscapeCmd). After noticing the image files weren't being exported, I created a few tests to try and debug. I changed the execute line to shell_exec($inkscapeCmd . ' 2>&1') in order to see the error message:
sh: inkscape: command not found
...which is odd since it's definitely installed and accessible. I added a check for the user on my test page to make sure the commands were being executed by a user with access to inkscape:
$processUser = posix_getpwuid(posix_geteuid());
echo 'user: ' . $processUser['name'];
Then I ssh'd into the server to confirm that I could run the same commands as that user, and was able to run them without any issues (which also confirmed inkscape was in the PATH). I'm able to run other, basic shell commands from PHP without issue, like so:
echo shell_exec('ls');
But now I'm at a loss; I'm not sure what else to check to determine why I'm getting the 'command not found' error. Any direction would be helpful.
The server (rather old, I know):
CentOS 6.7
PHP 5.3.3
Inkscape v0.47
The process does not have the location for inkscape in it's path.
You will have to supply to full path to the executable.
Example
/usr/bin/inkscape
At the command line type 'whereis inkscape' to find is location.

Compile PHP code including cURL commands to exe file

Is it possible to create Windows exe file from PHP source which include cURLcommands?
I want to create a portable program which works even on machines where PHP or cURL are not installed.
Actually I don't expect that it is possible to create just one executable file which don't need additional libraries. Even full cURL tools can be attached if it is needed.
Maybe I just want some way to join it after.
When I use some php to exe compiler like Phalanger etc, the compilation is successful but when I run the file it throws an error:
Error: Call to undefined function: 'curl_init' in C:\Users\Pavel\Desktop\comm\comments.php on line 6, column 1.

Execute C scripts using PHP

I need to run a c program in PHP to establish a socket connection and to retrieve a result back from the server.
I have written a C Program to establish a socket connection and server receives the request.
How to convert a C program into .dll file to use as a PHP extension?
Using socket you can't run any c application directly .You need to call that executable to run .
My idea is you can run another client application on remote and send some data to socket and in remote the client will receive the message based on which it can run he application.
Method 1:
You can receive the file sent from client and save it to disk.
Then, you may use system to compile the file received.
The last step, you run it, use the system again.
But i am not suggest you do that, as it may be dangerous, you never know what the code does. It may deletes all your files in the disk. What's more, the libs the code depend on may not be installed.
Method2:
use the loadable library:
1. all the code sent to server is implement a generic interface
2. pre-compile the code to loadable library
2. server save the code to disk
3. server load the library and call the generic interface
More about loadable library, please check it out.
We can not run a C Program directly with PHP, Because to run a C script there is no compiler with any LAMPP, XAMPP, WAMP. If we need to run any C script, Create a PHP Extension with C code with ZEND Engine standards(Check the syntax to receive the parameters in C from PHP).
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls|b", &intNumber, &strVal, &return_long) == FAILURE) {
RETURN_NULL();
}
WINDOWS:
Refer this link to create a PHP Extension with C Scripts:
http://www.dreamincode.net/forums/topic/244215-introduction-to-creating-a-php-extension-for-windows/
Move the extension file to /ext/ folder and add the extension name in php.ini file
LINUX:
http://linux.ferhatbingol.com/2013/06/06/how-to-create-php-extensions/
In a linux, by default .so file will move to /usr/lib/php5/ XXXXXXXX/extension.so
, Specify this extension path in your PHP.ini file
Then restart the apache server, finally our PHP Extension will work fine.

Categories