Skip to content

Commit

Permalink
[php] add PHP error handler and remove @ error-control operator
Browse files Browse the repository at this point in the history
  • Loading branch information
nao-pon committed May 13, 2016
1 parent 844fd68 commit e88048a
Show file tree
Hide file tree
Showing 15 changed files with 263 additions and 201 deletions.
111 changes: 84 additions & 27 deletions php/elFinder.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ class elFinder {
**/
protected $uploadDebug = '';

/**
* Errors from PHP
*
* @var array
**/
public static $phpErrors = array();

/**
* Errors from not mounted volumes
*
Expand Down Expand Up @@ -307,6 +314,9 @@ class elFinder {
* @author Dmitry (dio) Levashov
*/
public function __construct($opts) {
// set error handler of WARNING, NOTICE
set_error_handler('elFinder::phpErrorHandler', E_WARNING | E_NOTICE | E_USER_WARNING | E_USER_NOTICE);

if (! interface_exists('elFinderSessionInterface')) {
include_once dirname(__FILE__).'/elFinderSessionInterface.php';
}
Expand Down Expand Up @@ -365,7 +375,7 @@ public function __construct($opts) {

// setlocale and global locale regists to elFinder::locale
self::$locale = !empty($opts['locale']) ? $opts['locale'] : 'en_US.UTF-8';
if (false === @setlocale(LC_ALL, self::$locale)) {
if (false === setlocale(LC_ALL, self::$locale)) {
self::$locale = setlocale(LC_ALL, '');
}

Expand Down Expand Up @@ -458,6 +468,9 @@ public function __construct($opts) {

// if at least one readable volume - ii desu >_<
$this->loaded = !empty($this->default);

// restore error handler for now
restore_error_handler();
}

/**
Expand Down Expand Up @@ -608,6 +621,8 @@ private function session_expires() {
* @author Dmitry (dio) Levashov
**/
public function exec($cmd, $args) {
// set error handler of WARNING, NOTICE
set_error_handler('elFinder::phpErrorHandler', E_WARNING | E_NOTICE | E_USER_WARNING | E_USER_NOTICE);

if (!$this->loaded) {
return array('error' => $this->error(self::ERROR_CONF, self::ERROR_CONF_NO_VOL));
Expand Down Expand Up @@ -646,7 +661,7 @@ public function exec($cmd, $args) {
if (substr(PHP_OS,0,3) === 'WIN') {
// set time out
if (($_max_execution_time = ini_get('max_execution_time')) && $_max_execution_time < 300) {
@set_time_limit(300);
set_time_limit(300);
}
}

Expand Down Expand Up @@ -705,8 +720,10 @@ public function exec($cmd, $args) {
'memory' => (function_exists('memory_get_peak_usage') ? ceil(memory_get_peak_usage()/1024).'Kb / ' : '').ceil(memory_get_usage()/1024).'Kb / '.ini_get('memory_limit'),
'upload' => $this->uploadDebug,
'volumes' => array(),
'mountErrors' => $this->mountErrors
);
'mountErrors' => $this->mountErrors,
'phpErrors' => elFinder::$phpErrors
);
elFinder::$phpErrors = array();

foreach ($this->volumes as $id => $volume) {
$result['debug']['volumes'][] = $volume->debug();
Expand Down Expand Up @@ -895,7 +912,7 @@ protected function netmount($args) {
}

if ($volume->mount($options)) {
if (! $key = @ $volume->netMountKey) {
if (! $key = $volume->netMountKey) {
$key = md5($protocol . '-' . join('-', $options));
}
if (isset($netVolumes[$key])) {
Expand Down Expand Up @@ -976,7 +993,7 @@ protected function open($args) {
$limit = max(0, floor($standby / $sleep)) + 1;
$timelimit = ini_get('max_execution_time');
do {
$timelimit && @ set_time_limit($timelimit + $sleep);
$timelimit && set_time_limit($timelimit + $sleep);
$_mtime = 0;
foreach($ls as $_f) {
$_mtime = max($_mtime, $_f['ts']);
Expand Down Expand Up @@ -1125,7 +1142,7 @@ protected function zipdl($args) {
if (($volume = $this->volume($targets[0])) !== false) {
if ($dlres = $volume->zipdl($targets)) {
$path = $dlres['path'];
register_shutdown_function(create_function('$f', 'connection_status() && is_file($f) && @unlink($f);'), $path);
register_shutdown_function(create_function('$f', 'connection_status() && is_file($f) && unlink($f);'), $path);
if (count($targets) === 1) {
$name = basename($volume->path($targets[0]));
} else {
Expand All @@ -1151,7 +1168,7 @@ protected function zipdl($args) {
}
$file = $targets[1];
$path = $volume->getTempPath().DIRECTORY_SEPARATOR.$file;
register_shutdown_function(create_function('$f', 'is_file($f) && @unlink($f);'), $path);
register_shutdown_function(create_function('$f', 'is_file($f) && unlink($f);'), $path);
if (!is_readable($path)) {
return array('error' => 'File not found', 'header' => $h404, 'raw' => true);
}
Expand Down Expand Up @@ -1545,7 +1562,7 @@ protected function fsock_get_contents( &$url, $timeout, $redirect_max, $ua, $out

$errno = 0;
$errstr = "";
$fp = @ fsockopen(
$fp = fsockopen(
$ssl.$arr['host'],
$arr['port'],
$errno,$errstr,$connect_timeout);
Expand Down Expand Up @@ -1677,7 +1694,7 @@ protected function detectFileExtension($path) {
$volume = $this->volumes[$keys[0]];

if (class_exists('finfo', false)) {
$tmpFileInfo = @explode(';', @finfo_file(finfo_open(FILEINFO_MIME), __FILE__));
$tmpFileInfo = explode(';', finfo_file(finfo_open(FILEINFO_MIME), __FILE__));
} else {
$tmpFileInfo = false;
}
Expand All @@ -1697,11 +1714,11 @@ protected function detectFileExtension($path) {

$mime = '';
if ($type === 'finfo') {
$mime = @finfo_file($finfo, $path);
$mime = finfo_file($finfo, $path);
} elseif ($type === 'mime_content_type') {
$mime = mime_content_type($path);
} elseif ($type === 'getimagesize') {
if ($img = @getimagesize($path)) {
if ($img = getimagesize($path)) {
$mime = $img['mime'];
}
}
Expand Down Expand Up @@ -1749,7 +1766,7 @@ private function getTempDir($volumeTempPath = null) {
$gc = time() - 3600;
foreach(glob($tempDir . DIRECTORY_SEPARATOR .'ELF*') as $cf) {
if (filemtime($cf) < $gc) {
@unlink($cf);
unlink($cf);
}
}
break;
Expand Down Expand Up @@ -1825,7 +1842,7 @@ private function checkChunkedFile($tmpname, $chunk, $cid, $tempDir, $volume = nu
sleep(10); // wait 10 sec
// chunked file upload fail
foreach(glob($base . '*') as $cf) {
@unlink($cf);
unlink($cf);
}
ignore_user_abort(false);
return;
Expand Down Expand Up @@ -1899,7 +1916,7 @@ private function checkChunkedFile($tmpname, $chunk, $cid, $tempDir, $volume = nu
// old way
$part = $base . $m[2];
if (move_uploaded_file($tmpname, $part)) {
@chmod($part, 0600);
chmod($part, 0600);
if ($clast < count(glob($base . '*'))) {
$parts = array();
for ($i = 0; $i <= $clast; $i++) {
Expand Down Expand Up @@ -1978,13 +1995,13 @@ protected function upload($args) {
// if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
// $shutdownfunc = function(){ // <- Parse error on PHP < 5.3 ;-(
// foreach(array_keys($GLOBALS['elFinderTempFiles']) as $f){
// @unlink($f);
// unlink($f);
// }
// };
// } else {
$shutdownfunc = create_function('', '
foreach(array_keys($GLOBALS[\'elFinderTempFiles\']) as $f){
@unlink($f);
is_file($f) && unlink($f);
}
');
// }
Expand Down Expand Up @@ -2096,7 +2113,7 @@ protected function upload($args) {
$files['name'][$i] = $_name;
$files['error'][$i] = 0;
} else {
@ unlink($tmpfname);
unlink($tmpfname);
}
}
}
Expand Down Expand Up @@ -2162,7 +2179,7 @@ protected function upload($args) {
$result['warning'] = $this->error(self::ERROR_UPLOAD_FILE, $name, self::ERROR_UPLOAD_TRANSFER);
$this->uploadDebug = 'Upload error: unable open tmp file';
if (! is_uploaded_file($tmpname)) {
if (@ unlink($tmpname)) unset($GLOBALS['elFinderTempFiles'][$tmpfname]);
if ( unlink($tmpname)) unset($GLOBALS['elFinderTempFiles'][$tmpfname]);
continue;
}
break;
Expand Down Expand Up @@ -2200,7 +2217,7 @@ protected function upload($args) {
$result['warning'] = $this->error(self::ERROR_UPLOAD_FILE, $name, $volume->error());
fclose($fp);
if (! is_uploaded_file($tmpname)) {
if (@ unlink($tmpname)) unset($GLOBALS['elFinderTempFiles'][$tmpname]);;
if ( unlink($tmpname)) unset($GLOBALS['elFinderTempFiles'][$tmpname]);;
continue;
}
break;
Expand All @@ -2209,7 +2226,7 @@ protected function upload($args) {
is_resource($fp) && fclose($fp);
if (! is_uploaded_file($tmpname)){
clearstatcache();
if (!is_file($tmpname) || @ unlink($tmpname)) {
if (!is_file($tmpname) || unlink($tmpname)) {
unset($GLOBALS['elFinderTempFiles'][$tmpname]);
}
}
Expand All @@ -2220,7 +2237,7 @@ protected function upload($args) {
}
if ($GLOBALS['elFinderTempFiles']) {
foreach(array_keys($GLOBALS['elFinderTempFiles']) as $_temp) {
@ unlink($_temp);
unlink($_temp);
}
}
$result['removed'] = $volume->removed();
Expand Down Expand Up @@ -2479,7 +2496,7 @@ protected function info($args) {
$limit = max(1, $standby / $sleep) + 1;
$timelimit = ini_get('max_execution_time');
do {
$timelimit && @ set_time_limit($timelimit + $sleep);
$timelimit && set_time_limit($timelimit + $sleep);
$volume->clearstatcache();
if (($info = $volume->file($hash)) != false) {
if ($info['ts'] != $compare) {
Expand Down Expand Up @@ -2586,7 +2603,7 @@ protected function url($args) {
protected function callback($args) {
$checkReg = '/[^a-zA-Z0-9;._-]/';
$node = (isset($args['node']) && !preg_match($checkReg, $args['node']))? $args['node'] : '';
$json = (isset($args['json']) && @json_decode($args['json']))? $args['json'] : '{}';
$json = (isset($args['json']) && json_decode($args['json']))? $args['json'] : '{}';
$bind = (isset($args['bind']) && !preg_match($checkReg, $args['bind']))? $args['bind'] : '';
$done = (!empty($args['done']));

Expand Down Expand Up @@ -2650,6 +2667,46 @@ protected function callback($args) {
exit();
}

/**
* PHP error handler, catch error types only E_WARNING | E_NOTICE | E_USER_WARNING | E_USER_NOTICE
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return void|boolean
*/
public static function phpErrorHandler($errno, $errstr, $errfile, $errline) {
static $base = null;

if (is_null($base)) {
$base = dirname(__FILE__) . DIRECTORY_SEPARATOR;
}

if (! (error_reporting() & $errno)) {
return;
}

$errfile = str_replace($base, '', $errfile);

$proc = false;
switch ($errno) {
case E_WARNING:
case E_USER_WARNING:
elFinder::$phpErrors[] = "WARNING: $errstr in $errfile line $errline.";
$proc = true;
break;

case E_NOTICE:
case E_USER_NOTICE:
elFinder::$phpErrors[] = "NOTICE: $errstr in $errfile line $errline.";
$proc = true;
break;
}

return $proc;
}

/***************************************************************************/
/* utils */
/***************************************************************************/
Expand Down Expand Up @@ -2803,8 +2860,8 @@ public static function isAnimationGif($path) {

$imgcnt = 0;
$fp = fopen($path, 'rb');
@fread($fp, 4);
$c = @fread($fp,1);
fread($fp, 4);
$c = fread($fp,1);
if (ord($c) != 0x39) { // GIF89a
return false;
}
Expand Down Expand Up @@ -2882,7 +2939,7 @@ public static function sessionDataEncode($var) {
*/
public static function sessionDataDecode(&$var, $checkIs = null) {
if (self::$base64encodeSessionData) {
$data = @unserialize(@base64_decode($var));
$data = unserialize(base64_decode($var));
} else {
$data = $var;
}
Expand Down
5 changes: 4 additions & 1 deletion php/elFinderConnector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function run() {
**/
protected function output(array $data) {
// clear output buffer
while(ob_get_level() && @ob_end_clean()){}
while(ob_get_level() && ob_end_clean()){}

$header = isset($data['header']) ? $data['header'] : $this->header;
unset($data['header']);
Expand Down Expand Up @@ -216,6 +216,9 @@ protected function output(array $data) {
if (!empty($data['raw']) && !empty($data['error'])) {
echo $data['error'];
} else {
if (isset($data['debug']) && isset($data['debug']['phpErrors'])) {
$data['debug']['phpErrors'] = array_merge($data['debug']['phpErrors'], elFinder::$phpErrors);
}
echo json_encode($data);
}
flush();
Expand Down
2 changes: 1 addition & 1 deletion php/elFinderFlysystemGoogleDriveNetmount.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function netmountPrepare($options)
$file = $service->files->get($options['path']);
$options['alias'] = sprintf($this->options['gdAlias'], $file->getName());
} catch (Google_Service_Exception $e) {
$err = @json_decode($e->getMessage(), true);
$err = json_decode($e->getMessage(), true);
if (isset($err['error']) && $err['error']['code'] == 404) {
return array('exit' => true, 'error' => [elFinder::ERROR_TRGDIR_NOT_FOUND, $options['path']]);
} else {
Expand Down
2 changes: 1 addition & 1 deletion php/elFinderSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ protected function decodeData($data)
if ($this->base64encode) {
if (is_string($data)) {
if (($data = base64_decode($data)) !== false) {
$data = @unserialize($data);
$data = unserialize($data);
} else {
$data = null;
}
Expand Down
Loading

0 comments on commit e88048a

Please sign in to comment.