I am trying to write a simple application with wxPHP that pings IPs from a file using exec('ping') command.
I want the results to show up in the GUI one by one as they get pinged in the background so I'm doing the exec('ping') command in a thread.
But I get very strange errors and behavior. The problem is I can't reproduce the errors. Sometimes the program works totally fine. Sometimes it crashes in the thread. Sometimes it crashes when it is sending the event. etc.
Here are the errors I get when I run the program from command line:
C:\Users\SH\Desktop>wxphp pinger.wxphp
PHP Notice: Undefined property: wxHtmlWindow::$parent in Unknown on line 0
PHP Fatal error: Call to a member function setPingResults() on null in Unknown
on line 0
C:\Users\SH\Desktop>wxphp pinger.wxphp
PHP Notice: Undefined property: wxHtmlWindow::$parent in Unknown on line 0
PHP Fatal error: Call to a member function setPingResults() on null in Unknown
on line 0
C:\Users\SH\Desktop>wxphp pinger.wxphp
PHP Notice: Undefined property: wxHtmlWindow::$parent in Unknown on line 553649674
PHP Fatal error: Call to a member function setPingResults() on null in Unknown
on line 553649674
And this is my code:
<?php
if(!extension_loaded('wxwidgets'))
{
dl('wxwidgets.' . PHP_SHLIB_SUFFIX);
}
define('EVT_PINGDONE',wxNewEventType());
class myPing extends wxThread
{
function __construct($parent)
{
parent::__construct(wxTHREAD_JOINABLE);
$this->parent = $parent;
}
function Entry()
{
$addresses = file($this->parent->btnBrowse->GetPath(),FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
foreach($addresses as $k => $address){
$results = exec("ping $address");
$this->parent->setPingResults($results);
$evt = new wxCommandEvent(EVT_PINGDONE);
$this->parent->QueueEvent($evt);
}
$this->parent->onThreadDone();
return;
}
}
class mythFrame extends wxFrame {
function __construct( $parent=null ){
parent::__construct ( $parent, wxID_ANY, 'Pinger', wxDefaultPosition, new wxSize( 600,400 ), wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
$this->SetSizeHints( wxDefaultSize, wxDefaultSize );
$bSizer2 = new wxBoxSizer( wxHORIZONTAL );
$this->button = new wxButton( $this, wxID_ANY, "Ping", wxDefaultPosition, new wxSize(300, 40), 0 );
$bSizer2->Add($this->button, 0, wxALL|wxEXPAND, 5);
$bSizer1 = new wxBoxSizer( wxVERTICAL );
$this->btnBrowse = new wxFilePickerCtrl( $this, wxID_ANY, 'C:\Users\SH\Desktop\stuff\servers.txt', "Select a file", "*.*", wxDefaultPosition, wxDefaultSize, wxFLP_DEFAULT_STYLE );
$bSizer1->Add( $this->btnBrowse, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL, 5 );
$this->html = new wxHtmlWindow( $this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 );
$bSizer1->Add( $this->html, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxEXPAND, 5 );
$bSizer1->Add( $bSizer2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5 );
$this->SetSizer( $bSizer1 );
$this->Layout();
$this->Centre( wxBOTH );
// Connect Events
$this->button->Connect( wxEVT_COMMAND_BUTTON_CLICKED, array($this, "hndlrButton") );
$this->Connect(wxEVT_TIMER, array($this, "onTimer"));
$this->Connect(EVT_PINGDONE, array($this, 'updateText'));
$this->pingThread = new myPing($this);
$this->m_timer = new wxTimer($this);
$this->pingResults = '';
}
function updateText($event){
$this->html->AppendToPage($this->pingResults.'<br>');
}
function onTimer(){
if($this->threadDone == true){
$this->m_timer->Stop();
$this->button->Enable();
$this->btnBrowse->Enable();
$this->html->AppendToPage('<br>Finished.<hr>');
while($this->pingThread->IsRunning()){}
$this->pingThread->Delete();
$this->pingThread = new myPing($this);
}
}
function onThreadDone(){
$this->threadDone = true;
}
function setPingResults($results){
$this->pingResults = $results;
}
function hndlrButton( $event ){
$this->threadDone = false;
$this->m_timer->start(3000);
$this->button->Disable();
$this->btnBrowse->Disable();
$this->pingThread->Create();
$this->pingThread->Run();
}
}
$myFrame = new mythFrame();
$myFrame->show();
wxEntry();
?>
And this is the errors I get in windows error log:
Faulting application name: wxphp.exe, version: 5.6.9.0, time stamp: 0x55765568
Faulting module name: ntdll.dll, version: 6.1.7601.18247, time stamp: 0x521eaf24
Exception code: 0xc0000005
Fault offset: 0x0000000000052f86
Faulting process id: 0x3c4
Faulting application start time: 0x01d0b6675e88094f
Faulting application path: C:\Program Files\wxPHP\php\wxphp.exe
Faulting module path: C:\Windows\SYSTEM32\ntdll.dll
Report Id: 9cd6fe5e-225a-11e5-b316-f46d04f70903
Faulting module changes depending on whether i call php functions or call exec() in the thread Entry(). But the exception code is always 0xc0000005.
One weird thing that is consistent in errors is errors like
Undefined property wxHtmlWindow::$parent
If you read the code you realize that there is no such call at all. Actually $parent is only accessed in the thread. I have got similar errors with other functions in the thread. It seems like the application doesn't realize it is in the thread and $this in the thread points to the main frame object.
I guess something is terribly wrong with my code or maybe with wxPHP. This program was not working last night, then started working this morning. Then I added an icon and some background colors to it and it suddenly is giving me errors again.
Other info
Doesn't work on:
Windows 7-64bit - wxphp-3.0.2.0-php5.6-x64.exe
Windows 7-32bit - wxphp-3.0.2.0-php5.4-x86
The example thread.wxphp application bundled with the wxPHP
package doesn't work either.
However both my code and the example app work fine on Linux (Linux
Mint-32bit - php5-wxwidgets_3.0.2.0_i386).
When I remove both exec() and QueueEvent() from the thread
Entry() I don't get any errors.
Does anyone have any idea what's going on here?
I'm the maintainer of wxPHP and I would advice to use the PHP pthreads extension for multithreading given that this extension properly handles threading with the PHP zendengine. The wxThread class was automatically wrapped by the wxPHP source generator and it may need further development to properly work.
Related
I have a legacy site (not written by me) that has been on a server with php5 on it for the last several years. I am in the process of creating a new server with php7 on it and testing what works and is broken.
the site uses pear by including the file pear/lib/DB.php. i created a brand new page that only has the code
<?php
require_once( "DB.php" );
?>
this presents the exact same error as the full site.
the error that's being presented is
PHP Parse error: syntax error, unexpected 'new' (T_NEW) in /local/sites/php/pear/lib/DB.php on line 310
the site only requires DB.php because I have added Pear to the php.ini in include_path
checking the version of Pear gives me the following
$ pear version
PEAR Version: 1.10.3
PHP Version: 7.0.15-0ubuntu0.16.04.4
Zend Engine Version: 3.0.0
Running on: Linux cdc-migration-0d 3.13.0-103-generic #150-Ubuntu SMP Thu Nov 24 10:34:17 UTC 2016 x86_64
from my research it shows the latest version of Pear is php7 compatible, so these should work together. any idea why just requiring the DB.php on a test page would immediately generate the parsing error?
edit:
the code in the pear file that is generating the error is as follows
function &factory($type, $options = false)
{
if (!is_array($options)) {
$options = array('persistent' => $options);
}
if (isset($options['debug']) && $options['debug'] >= 2) {
// expose php errors with sufficient debug level
include_once "DB/{$type}.php";
} else {
#include_once "DB/{$type}.php";
}
$classname = "DB_${type}";
if (!class_exists($classname)) {
$tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null,
"Unable to include the DB/{$type}.php file",
'DB_Error', true);
return $tmp;
}
#$obj =& new $classname; // ##### this is line 310 that generates the error #####
foreach ($options as $option => $value) {
$test = $obj->setOption($option, $value);
if (DB::isError($test)) {
return $test;
}
}
return $obj;
}
#$obj =& new $classname;
Assigning the return value of new by reference is deprecated since PHP 5.3. http://php.net/manual/en/migration53.deprecated.php
This is PHP4 style of writing PHP.
Write instead :
$obj = new $classname;
This has been removed as of PHP7.
See: http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.other.new-by-ref
I have a Zend2 project running on my localhost with no problems. The app runs perfect. I Uploaded it to my server and now it gets a fatal error but not every time.
Sometimes it says this,
Fatal error: Class name must be a valid object or a string in /home/public_html/vendor/zendframework/zend-stdlib/src/ArrayObject.php on line 230
public function getIterator()
{
$class = $this->iteratorClass;
return new $class($this->storage); // line 230
}
And sometimes it says this,
File
/vendor/zendframework/zend-stdlib/src/ArrayObject.php:184
Message:
Passed variable is not an array or object, using empty array instead
Never both and sometimes it loads perfectly with no problems. The file it references is in the vendor path this is the link,
public function exchangeArray($data)
{
if (!is_array($data) && !is_object($data)) {
throw new Exception\InvalidArgumentException('Passed variable is not an array or object, using empty array instead');
} // Line 184
if (is_object($data) && ($data instanceof self || $data instanceof \ArrayObject)) {
$data = $data->getArrayCopy();
}
if (!is_array($data)) {
$data = (array) $data;
}
$storage = $this->storage;
$this->storage = $data;
return $storage;
}
Any ideas why this would happen on a live server with a zend site but not on a localhost?
I found this post on github which I think it related to ZFCUser
Git Hub Post
Someone in the comments says this,
This issue is caused by the layout.phtml when there is an error. The layout needs to render but it doesn't have $this->url
I have no clue what he is talking about. Is anyone able to shoot me in the right direction?
I'm making a dashboard that requires the Teamspeak 3 library in CodeIgniter.
I now get an error that the TeamSpeak3_Helper_Uri has not been found in Teamspeak.php . Perhabs it has something to do with naming because it has running fine on a Windows machine (now running on Linux).
The error:
Fatal error: Class 'TeamSpeak3_Helper_Uri' not found in /var/www/html/FlightAcademy/application/libraries/Teamspeak3/Teamspeak3.php on line 313
Teamspeak3.php line 309 till 322:
public static function factory($uri) {
self::init();
$uri = new TeamSpeak3_Helper_Uri($uri); // <-- Line 313
$adapter = self::getAdapterName($uri->getScheme());
$options = array("host" => $uri->getHost(), "port" => $uri->getPort(), "timeout" => intval($uri->getQueryVar("timeout", 10)), "blocking" => intval($uri->getQueryVar("blocking", 1)));
self::loadClass($adapter);
$object = new $adapter($options);
if($object instanceof TeamSpeak3_Adapter_ServerQuery)
My file structure in CodeIgniter:
libraries
Teamspeak3
Adapter
Helper
Uri.php (TeamSpeak3_Helper_Uri)
...
Node
Transport
Viewer
Exeption.php
Teamspeak3.php
The error occurs when I call in my model:
$ts3 = TeamSpeak3::factory
I hope you can help because I have no clue what's wrong.
Ok so, I have figured out this error. It seems that the problem is the naming convention between Windows and Linux in CodeIgniter. I solved the problem by naming all original "TeamSpeak" classes to "Teamspeak". It seems that this was the issue.
I'm writing a CodeIgniter library around PHP's bbcode PECL extension, but I'm having some trouble with callbacks.
I set up the handler in the library constructor:
function __construct() {
$basic = array(
'url' => array(
'type' => BBCODE_TYPE_OPTARG,
'open_tag' => '<a href="{PARAM}" rel="nofollow">',
'close_tag' => '</a>',
'childs'=>'i,b,u,strike,center,img',
'param_handling' => array($this, 'url')
)
);
$this->handler = bbcode_create($basic);
}
public function parse($bbcode_string) {
return bbcode_parse($this->handler, htmlentities($bbcode_string));
}
As you notice, this uses a callback for handling what's allowed to go into the URL. I use this to insert an "exit redirect" page
public static function url($content, $argument) {
if (!$argument) $argument = $content;
$url = parse_url($argument);
if (!isset($url['host'])) {
if (strlen($argument) > 0 && $argument[0] != '/') return false;
$destination = '//'.$_SERVER['HTTP_HOST'].$argument;
} elseif ($url['host'] != $_SERVER['HTTP_HOST']) {
$destination = '//'.$_SERVER['HTTP_HOST'].'/exit?'.urlencode($argument);
} else {
$destination = $argument;
}
return htmlspecialchars($destination);
}
And I also have a little function which helps me test this out as I work:
function test() {
$string = '[url]http://www.google.com[/url]';
echo '<pre>';
die($this->parse($string));
}
This all works fine if the test() method is called from within the library. For example, if I throw $this->test() at the bottom of the constructor, everything works exactly as I would expect. However, calling $this->bbcode->test() from somewhere else (e.g. in a controller), I get the following errors:
**A PHP Error was encountered**
Severity: Warning
Message: Invalid callback , no array or string given
Filename: libraries/bbcode.php
Line Number: 122
**A PHP Error was encountered**
Severity: Warning
Message: bbcode_parse(): function `' is not callable
Filename: libraries/bbcode.php
Line Number: 122
http://www.google.com
The callback does not get executed, and as a result the link's href attribute is empty. Line 122 refers to the single line of code in my parse function:
return bbcode_parse($this->handler, htmlentities($bbcode_string));
How do I address this callback function such that it can be located when $this->bbcode->test() is called from inside a controller?
Now I'm even more confused...
So in the hopes of just putting this all behind me, I put these callback functions in a helper so I can just call them directly. So I now have code like this:
function __construct() {
$basic = array(
'url' => array(
'type' => BBCODE_TYPE_OPTARG,
'open_tag' => '<a href="{PARAM}" rel="nofollow">',
'close_tag' => '</a>',
'childs'=>'i,b,u,strike,center,img',
'param_handling' => 'bbcode_url'
)
);
$this->handler = bbcode_create($basic);
}
With the above, I get the following error:
**A PHP Error was encountered**
Severity: Warning
Message: Invalid callback 6.7949295043945E-5, no array or string given
Filename: libraries/bbcode.php
Line Number: 176
**A PHP Error was encountered**
Severity: Warning
Message: bbcode_parse(): function `6.7949295043945E-5' is not callable
Filename: libraries/bbcode.php
Line Number: 176
(line 176 is the new location of the parse() function)
Um... I don't even know what's going on. The number 6.7949295043945E-5 changes with every attempt.
The only solution I have found to this is quite simply not to set the handler up in a constructor. Instead, the parse() method contains both the bbcode_create() call and the bbcode_parse() call. That is, the bbcode_handler is freshly created for every string of bbcode to be parsed.
This seems needlessly wasteful to me. But, over the course of the lifetime of this project, it is exceedingly unlikely to cost even a tenth of the amount of time that I have spent trying to sort this out "properly", so I'm calling it a day.
I'm posting this "solution" here in case somebody else happens across this question and can thereby save themselves a few hours' pain. That said, I would really like to know what on earth is going on, and how to do this properly.
I can't seem to chain some actions together, specifically this line of code:
$messages = Request::factory('messages/get_messages')->execute()->response;
When I play this in my browser, Kohana fails with the following warning.
ErrorException [ Notice ]: Undefined property: Response::$response
The full line of code for this reads...
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Profile extends Controller_Application
{
public function action_index()
{
$content = View::factory('profile/public')
->set('username', 'Test User')
->bind('messages', $messages);
$messages = Request::factory('messages/get_messages')->execute()->response;
$this->template->content = $content;
}
}
Since I've been going through Beginners Guide by Jason D. Straughan, there have been a few little differences that I've been able to resolve but drawing a blank on this. Any pointers here would be appreciated.
This line of code is on page 81 of the book (jump to page 96 on the scribd viewer).
Try using:
$messages = Request::factory('messages/get_messages')->execute()->body();
The reason you get the Notice error is that the object Response (which is returned by Request::factory(...)->execute()) has no property called $response ( Kohana Docs 3.1 | Response OR Kohana Docs 3.2 | Response ).