I'm dealing with this php script, which when executed on the host gives a 500 error, apparently the line where the preg_match is is the one that contains the error...
this file is going to be executed as a cron to validate.
<?php
$encoded = wordwrap($encoded, 80, "\xa", true);
$license_file = $dir . "/modules/addons/Kayako/license.php";
if ($key != $sellKey) {
die("Invalid "license . php" file!");
}
function getWhmcsDomain() {
if (!empty($_SERVER["SERVER_NAME"])) {
return $_SERVER["SERVER_NAME"];
}
}
$license["checkdate"] = date("Ymd");
$keyName = $modleName . "_licensekey";
$dir = __DIR__;
$encoded = strrev($encoded);
$license["status"] = "Active";
$sellKey = "ModulesGarden_Kayako_54M02934WH301844E_HackbyRicRey";
$license["checktoken"] = $checkToken;
$key_data = WHMCS\Database\Capsule::table("tblconfiguration")->where("setting", "kayako_localkey")->first();
$license = array("licensekey" => $key, "validdomain" => getWhmcsDomain(), "validip" => getIp(), "validdirectory" => $dir . "/modules/addons/Kayako," . $dir . "/modules/addons," . $dir . "/modules/addons/Kayako," . $dir . "/modules/addons/Kayako," . $dir . "/modules/addons," . $dir . "," . $dir . "/modules");
$secret = "659c08a59bbb484f3b40591";
include_once "init.php";
function getIp() {
return isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : $_SERVER["LOCAL_ADDR"];
}
if (!$key_data) {
WHMCS\Database\Capsule::table("tblconfiguration")->insert(array("setting" => "kayako_localkey", "value" => ''));
}
$checkToken = time() . md5(rand(1000000000, 0) . $key);
$modleName = "kayako";
$encoded = $encoded . md5($encoded . $secret);
$encoded = serialize($license);
preg_match("/kayako_licensekey\s?=\s?"([A - Za - z0 - 9_] +) "/", $content, $matches);
$encoded = md5($license["checkdate"] . $secret) . $encoded;
$key = $matches[1];
$encoded = base64_encode($encoded);
if (file_exists($license_file)) {
$content = file_get_contents($license_file);
} else {
echo "Please Upload "license . php" File Inside: " . $dir . "/modules/addons/Kayako/";
}
$content = '';
try {
WHMCS\Database\Capsule::table("tblconfiguration")->where("setting", "kayako_localkey")->update(array("value" => $encoded));
echo "Done!";
}
catch(\Throwable $e) {
echo "There is an issue, contact.";
} ?>
You have extra double quotes in the regular expression. You also have extra spaces inside the [] in the regexp. You can replace that character class with \w, which matches alphanumerics and underscore.
preg_match('/kayako_licensekey\s?=\s?(\w+)/', $content, $matches);
Another problem: You use a number of variables before you assign them:
$modleName
$checkToken
$key
$sellKey
$dir
Did you post the code out of order?
Related
I'm running tesseract on images in images folder and if the OCR result has "Error occurred" text then I want to send all the pictures names with error message concatenated as a single message with lines breaks of course for each message.
With the below code I'm receiving separate messages for every single picture result.
But I want Something like as on this picture all as a single message.
Here is my code tho;
$dir = "images/*.jpg";
$images = glob( $dir );
foreach( $images as $image):
$result = (new TesseractOCR($image))
->run();
$info = pathinfo($image);
$file_name = basename($image,'.'.$info['extension']);
$Name = str_replace("_"," ", $file_name);
$msg = "Error Occurred";
if($result === $msg)
{
$err = $Name . " - Says - ".$msg."\n";
$apiToken = $bot_token;
$data = [
'chat_id' => $chat_id,
'text' => $err
];
$response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data) );
}
else
{
$msg = $Name . " - Good! \n";
echo $msg."</br>";
}
endforeach;
Sending of the message needs to happen outside of the foreach loop
I have changed the code to collect all the errors into an array then after the loop if any errors exist, it implodes the array into a multiline string and sends the message
<?php
$dir = "images/*.jpg";
$images = glob($dir);
$errors = [];
foreach ($images as $image) {
$result = (new TesseractOCR($image))
->run();
$info = pathinfo($image);
$file_name = basename($image, '.' . $info['extension']);
$Name = str_replace("_", " ", $file_name);
$msg = "Error Occurred";
if ($result === $msg) {
$errors[] = $Name . " - Says - " . $msg;
}
else {
$msg = $Name . " - Good! \n";
echo $msg . "</br>";
}
}
if (count($errors) > 0) {
$apiToken = $bot_token;
$data = [
'chat_id' => $chat_id,
'text' => implode("\n", $errors)
];
$response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data));
}
I have an error about my fuction on this line:
$FILE_LIST[$key]['name'] = str_replace('.' . $ext, '', $result);
Warning: Illegal string offset 'name'
I tried different way but i don't find the solution.
I look other post like Warning: Illegal string offset 'name'
but it do not resolve the problem (or I fogot something).
the function
public function getFiles($source_folder, $filename, $ext = 'php') {
if(is_dir( $source_folder)) {
$FILES = glob($source_folder . $filename . '.' . $ext);
$FILE_LIST[] = '';
if (is_array($FILES)) {
foreach($FILES as $key => $file) {
$result = str_replace($source_folder, '', $file);
$name = str_replace('.' . $ext, '', $result);
if (!empty($name)) {
$FILE_LIST[$key]['name'] = $name;
}
}
}
if (is_array($FILE_LIST)) {
return $FILE_LIST;
}
}
}
Change this:
if (!empty($name)) {
$FILE_LIST[$key]['name'] = $name;
}
to this:
if (!empty($name)) {
$FILE_LIST[$key] = Array();
$FILE_LIST[$key]['name'] = $name;
}
I have a simple strange problem but I can not find a function to do this after many search.
I have an URL like http://example.com/folder/folder2/../image/test.jpg and I would like a function which return the correct absolute link:
http://example.com/folder/image/test.jpg
A function with only one param, the url (and not base dir or relative dir like in examples I found)
If you can help me, thanks.
Perhaps a starting point:
<?php
function unrelatify($url)
{
$parts = parse_url($url);
$path = $parts['path'] ?? '';
$hierarchy = explode('/', $path);
while(($key = array_search('..', $hierarchy)) !== false) {
if($key-1 > 0)
unset($hierarchy[$key-1]);
unset($hierarchy[$key]);
$hierarchy = array_values($hierarchy);
}
$new_path = implode('/', $hierarchy);
return str_replace($path, $new_path, $url);
}
echo unrelatify('http://example.com/../folder/../folder2/../image/test.jpg#foo?bar=baz');
Output:
http://example.com/image/test.jpg#foo?bar=baz
You may want to see how browsers and other web clients de-relativify (urls).
thanks to everyone for your answers.
here are a recap and some other ways i've tested
<?php
function unrelatify($url) {
$parts = parse_url($url);
$path = $parts['path'];
$hierarchy = explode('/', $path);
while (($key = array_search('..', $hierarchy)) !== false) {
if ($key - 1 > 0)
unset($hierarchy[$key - 1]);
unset($hierarchy[$key]);
$hierarchy = array_values($hierarchy);
}
$new_path = implode('/', $hierarchy);
return str_replace($path, $new_path, $url);
}
function normalizePath($path) {
do {
$path = preg_replace(
array('#//|/\./#', '#/([^/.]+)/\.\./#'), '/', $path, -1, $count
);
} while ($count > 0);
return str_replace('../', '', $path);
}
function processUrl($url) {
$parsedUrl = parse_url($url);
$path = $parsedUrl['path'];
$pathSegments = explode("/", $path);
$iterator = 0;
$removedElements = 0;
foreach ($pathSegments as $segment) {
if ($segment == "..") {
if ($iterator - $removedElements - 1 < 0) {
return false;
}
unset($pathSegments[$iterator - $removedElements - 1]);
unset($pathSegments[$iterator]);
$removedElements += 2;
}
$iterator++;
}
$parsedUrl['path'] = implode("/", $pathSegments);
$newUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . "/" . $parsedUrl['path'];
return $newUrl;
}
function path_normalize($path) {
$path = str_replace('\\', '/', $path);
$blocks = preg_split('#/#', $path, null, PREG_SPLIT_NO_EMPTY);
$res = array();
while (list($k, $block) = each($blocks)) {
switch ($block) {
case '.':
if ($k == 0)
$res = explode('/', path_normalize(getcwd()));
break;
case '..';
if (!$res)
return false;
array_pop($res);
break;
default:
$res[] = $block;
break;
}
}
$r = implode('/', $res);
return $r;
}
echo 'path_normalize<br />';
$url = 'http://www.example.com/modules/newsletters/../../images/homeslider-images/test-5.jpg';
echo $url . ' === > ' . path_normalize($url);
echo '<hr />';
$url = 'http://www.example.com/../../images/homeslider-images/test-5.jpg';
echo $url . ' === > ' . path_normalize($url);
echo '<hr />normalizePath<br />';
$url = 'http://www.example.com/modules/newsletters/../../images/homeslider-images/test-5.jpg';
echo $url . ' === > ' . normalizePath($url);
echo '<hr />';
$url = 'http://www.example.com/../../images/homeslider-images/test-5.jpg';
echo $url . ' === > ' . normalizePath($url);
echo '<hr />unrelatify<br />';
$url = 'http://www.example.com/modules/newsletters/../../images/homeslider-images/test-5.jpg';
echo $url . ' === > ' . unrelatify($url);
echo '<hr />';
$url = 'http://www.example.com/../../images/homeslider-images/test-5.jpg';
echo $url . ' === > ' . unrelatify($url);
echo '<hr />processUrl<br />';
$url = 'http://www.example.com/modules/newsletters/../../images/homeslider-images/test-5.jpg';
echo $url . ' === > ' . processUrl($url);
echo '<hr />';
$url = 'http://www.example.com/../../images/homeslider-images/test-5.jpg';
echo $url . ' === > ' . processUrl($url);
?>
I have a script that needs to pull data from different cached URLs.
Right now $url = 'http://example.com/search.php?user=abc&part='.$part;
I need the portion of the script below modified to search multiple MD5 encrypted url's
i.e.
$url = 'http://example.com/search.php?user=abc&part='.$part;
$url = 'http://example.com/search.php?user=xyz&part='.$part;
$url = 'http://example.com/search.php?user=123&part='.$part;
If more than 1 value is returned than return the one with the newest date.
$xid needs to be the current setting for $url
Original code.
function get_cache_file($url) {
$xid = md5($url);
$gendir = CACHE_ROOT . substr($xid, 0, 1) . '/'. substr($xid, 1, 2);
if(!is_dir($gendir)) {
mkdir($gendir, 0777, true);
}
return $gendir . '/' . $xid;
}
Found an answer to the problem.
function get_cache_file($part)
{
$users = array('user1', 'user2', 'user3');
$file = '';
$time = 0;
foreach ($users as $user) {
$url = 'http://example.com/search.php?user=' . $user . '&part=' . $part;
$xid = md5($url);
$gendir = CACHE_ROOT . substr($xid, 0, 1) . '/' . substr($xid, 1, 2);
if (is_dir($gendir) && is_file($gendir . '/' . $xid)) {
if ($time < filemtime($file)) {
$time = filemtime($file);
$file = $gendir . '/' . $xid;
}
}
}
Right Now I Have:
$path2 = $file_list1;
$dir_handle2 = #opendir($path2) or die("Unable to open $path2");
while ($file2 = readdir($dir_handle2)) {
if($file2 == "." || $file2 == ".." || $file2 == "index.php" )
continue;
echo ''.$file2.'<br />';
}
closedir($dir_handle2);
echo '<br />';
When $file2 is returned, the last 4 characters in the string will always end in a number plus the file extension .txt, like this:
file_name_here1.txt
some_other-file10.txt
So my question is, how can I separate $file2 so it returns the string in two parts, $file_name and $call_number like this?:
echo 'File: '.$file_name.' Call: '.call_number.'<br />';
Returns:
File: file_name_here Call: 1
File: some_other-file Call: 10
instead of this:
echo ''.$file2.'<br />';
Returns:
file_name_here1.txt
some_other-file10.txt
Thanks....
I'm a big advocate of Regex but I decided to go slightly different here. Check it out:
$file = 'file_name_here19.txt';
$file_parts = pathinfo($file);
$name = $file_parts['filename'];
$call = '';
$char = substr($name, strlen($name) - 1);
while(ord($char) >= 48 && ord($char) <= 57) {
$call = $char . $call;
$name = substr($name, 0, strlen($name) - 1);
$char = substr($name, strlen($name) - 1);
}
echo 'Name: ' . $name . ' Call: ' . $call;
Use regular expressions:
preg_match("/^(.+)(\d+)(\..+)$/", $file2, $matches);
$file_name = $matches[1];
$call_number = $matches[2];
Try this, you need to use Regex to do this effectively
$filename = reset(explode(".", $file2))
preg_match("#(^[a-zA-Z\_\-]*)([\d]*)#", $filename, $matches);
$fullMatch = $matches[0];
$file = $matches[1];
$call = $matches[2];
echo "File: " . $file . " Call: " . $call;
Use pathinfo() function to cut off file extension.
Use preg_match() function to separate name from number.
3.
while (...) {
...
$filename; // some_other-file10.txt
$filename = pathinfo($filename, PATHINFO_FILENAME); // some_other-file10
preg_match('/^(?<name>.*?)(?<number>\d+)$/', $filename, $match);
$match['name']; // some_other-file
$match['number']; // 10
echo "File: {$match['name']} Call: {$match['number']}\n";
}