Binary String to Array to HEX Color

Trying to import some color pallets from a program which stores them in binary format. I need them to be split and converted to hex. I didn’t think it would be too difficult after seeing this tool: https://www.binaryhexconverter.com/binary-to-hex-converter, but I am not getting the results I expected.

$color = "[00001010,11101111,11000001,00000000,11111111,11111111,11111111,11111111,11111111,11111111,01100000,00000000,10010000,00000000,11111111,11111111,01100000,00000000,10010000,00000000,11111111,11111111,01100000,00000000,10010000,00000000,11111111,11111111,01100000,00000000,11111111,11111111,11111111,11111111,01100000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,10001010,00000000,11000000,00000000,11111111,11111111,10001010,00000000,11000000,00000000,11111111,11111111,10001010,00000000,11000000,00000000,11111111,11111111,11000000,00000000,11111111,11111111,11111111,11111111,10010000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,01110011,01110101,01110010,01100111,01100101,01110010,01111001,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,10101110,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,10101110,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11000000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11000000,00000000,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,11000000,11000000,11000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000]";
$new_arr = array_map('trim', explode(',', trim($color, '[]')));

$arr = array_chunk($new_arr, 3);

foreach($arr as $bingroup){
    print("<div style='position:absolute;height:20px;width:20px;background:#");
    foreach($bingroup as $binitem){

    $hex = bin2hex ($binitem);
    print($hex);
    }
    print("'></div>");
}

So I import the string, trim, explode it to an array, group them into groups of 3 and then run over it with a couple foreach loops using bin2hex() -> simple right? Why then am I getting

<div style="position:absolute;height:20px;width:20px;background:#303030303130313031313130313131313131303030303031"></div>

kevinchappell / form Builder

I am new Comer in JavaScript
I used kevinchappell / form Builder but i am stuck because i am unable to customize it.
I need to add dynamically values in autocomplete text field can you help me

Form Builder Link

My Code is this

var fields = [
        {
            type: 'autocomplete',
            label: 'Custom Autocomplete',
            required: true,
            fields : function (fieldData) {
                return {
                    values: '{label: "SQL"}'

                };
            },
            values:
                [
// here i want to add dynamic values
                {label: 'SQL'},
                {label: 'C#'},
                {label: 'JavaScript'},
                {label: 'Java'},
                {label: 'Python'},
                {label: 'C++'},
                {label: 'PHP'},
                {label: 'Swift'},
                {label: 'Ruby'}
            ]

this is form-builder.js by kevinchappell / form Builder

Duplicate results in mysql query when doing ajax call

SQL Fiddle if you guys wanna mess around with it

I’m unable to reproduce the issue using the fiddle.

Here is the query you can use in the fiddle

SELECT n.*, ns.notification_id AS is_read FROM notifications n
LEFT OUTER JOIN notification_status ns
ON n.id = ns.notification_id
LEFT JOIN notification_user_role nur
ON n.id = nur.notification_id
WHERE
(
  n.esb_consultant_id = 19291
  OR
  n.esb_consultant_id = 'role'
)
AND nur.user_role_id = 'pl_sso_regional_vice_president'
AND n.creation_date <= NOW()
AND n.expiration_date >= NOW()
ORDER BY n.creation_date DESC, (is_read IS NULL) DESC, n.priority ASC
LIMIT 0, 10

I’ve also placed it lower in this post, but here it’s easier to catch the eyes.


I’ll try to keep this as brief as possible.

I’m working on a notification system. I have 3 tables described below.

I’m trying to get notifications with a LIMIT of 10, paginated, 10 per page(so an OFFSET of 10). I’m using ajax to load the next 10.

They are to be ordered by priority(from 1 to 6, 1 being displayed first, while 6th being displayed last).

All unread notifications must be displayed first(priority still applies), while read notifications must be displayed last(priority still applies).

Notifications are per role. A user can have several roles(thus the need for another table).

The notification_status table described below is used to keep track of which notifications are read.

Which ever notification is NOT in the notification_status table is NOT read. This is very important. I didn’t make this decision. I just have to live with it.


To put it into the big picture lets have an example:

Assume we have 14 notifications:

5 of them will be priority 1, unread.

4 of them will be priority > 1, unread.

3 of them will be priority 1, read.

2 of them will be priority > 1, read.


The expected display order is the following.

5 unread priority 1

4 unread priority > 1

1 read priority 1

ajax starts here since we have 10 per page

2 read priority 1

2 read priority > 1


The table structure is as follows.

notifications

+-------------------+------------------+------+-----+---------+----------------+
| Field             | Type             | Null | Key | Default | Extra          |
+-------------------+------------------+------+-----+---------+----------------+
| id                | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| type_id           | int(10) unsigned | NO   |     | NULL    |                |
| sticky            | int(10) unsigned | NO   |     | NULL    |                |
| priority          | int(10) unsigned | NO   |     | NULL    |                |
| esb_consultant_id | varchar(40)      | NO   |     |         |                |
| message_id        | varchar(100)     | NO   |     |         |                |
| esb_params        | varchar(255)     | YES  |     |         |                |
| creation_date     | datetime         | YES  |     | NULL    |                |
| expiration_date   | datetime         | YES  |     | NULL    |                |
+-------------------+------------------+------+-----+---------+----------------+

notification_user_role

+-----------------+------------------+------+-----+---------+-------+
| Field           | Type             | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+-------+
| user_role_id    | varchar(150)     | NO   |     |         |       |
| notification_id | int(10) unsigned | NO   | MUL | NULL    |       |
+-----------------+------------------+------+-----+---------+-------+

notification_status

+-------------------+------------------+------+-----+---------+-------+
| Field             | Type             | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+-------+
| esb_consultant_id | varchar(20)      | NO   |     |         |       |
| notification_id   | int(10) unsigned | NO   | MUL | NULL    |       |
+-------------------+------------------+------+-----+---------+-------+

The query I’m using to retrieve the results:

SELECT n.*, ns.notification_id AS is_read FROM notifications n
LEFT OUTER JOIN notification_status ns
ON n.id = ns.notification_id
LEFT JOIN notification_user_role nur
ON n.id = nur.notification_id
WHERE 
(
  n.esb_consultant_id = :consultant_id 
  OR 
  n.esb_consultant_id = :role_all
)
AND nur.user_role_id = :consultant_role
AND n.creation_date <= NOW()
AND n.expiration_date >= NOW()
ORDER BY n.creation_date DESC, (is_read IS NULL) DESC, n.priority ASC
LIMIT $offset, $limit

$offset is the page times 10 – so if the page is 0(first page) the offset is 0, if the page is 1(first ajax call) the offset is 10 and so on

$limit is the limit, it’s always 10.

:consultant_id is the user id – unique

:role_all is a simple string all. It is used for when certain notifications are for all roles(such as a birthday notification). All users have this notification, regardless of role since all of them have a birthday.


The problem:

Whenever I’m doing an ajax call I get certain notifications that are duplicated. I’ll just post a screenshot of it since it’s easier than drawing it.

Do note that the ajax itself is just part of the way I’m retrieving the results but is not responsible for the duplicates themselvs, I’m absolutely sure. It’s not a display issue either, I’ve double and triple checked that.

Before Ajax

After ajax

What I’ve noticed is that if I were to remove this part

ORDER BY n.creation_date DESC, (is_read IS NULL) DESC, n.priority ASC

from the query. It works fine. No duplicates.

The dump of the query above, with limit removed and order by removed:

Dump

Sorry for the image but it’s easier.


I’m using PHP to query the database.

  public function all($consultant_id, $consultant_role, $offset = 0) {
    $limit = 10;
    $offset = $offset * 10;

    $query = <<<SQL
SELECT n.*, ns.notification_id AS is_read FROM notifications n
LEFT OUTER JOIN notification_status ns
ON n.id = ns.notification_id
LEFT JOIN notification_user_role nur
ON n.id = nur.notification_id
WHERE 
(
  n.esb_consultant_id = :consultant_id 
  OR 
  n.esb_consultant_id = :role_all
)
AND nur.user_role_id = :consultant_role
AND n.creation_date <= NOW()
AND n.expiration_date >= NOW()
ORDER BY n.creation_date DESC, (is_read IS NULL) DESC, n.priority ASC
LIMIT $offset, $limit
SQL;
    $return = $this->connection
      ->query($query
        , [
          ':consultant_role' => $consultant_role,
          ':consultant_id'   => $consultant_id,
          ':role_all'        => NotificationStatus::PL_N_ALL,
        ]
      )->fetchAll(\PDO::FETCH_ASSOC);

    foreach($return as $item) { // this is added simply for display purposes
      echo $item['id'] . '<br>';
    }

    return $return;
  }

The above is a copy+paste of the code used to retrieve the results. The function simply returns the results for displaying, no other magic going around.

The foreach is added to simply display the results in the browser.

Here is the image of the output. Notification 10 is duplicated.

Here is the exact same code only with
ORDER BY n.creation_date DESC, (is_read IS NULL) DESC, n.priority ASC removed. Limit and offset still apply here.


I’m not that good at mysql or sql in general and I’m not sure where the problem itself lies.

Any pointing in the right direction is greatly appreciated. Even workarounds or “hacks” I’m fine with.

PHP remove last empty line in a textfile

I have a problem with a textfile.
I have a textfile with an empty line (always the last line is empty).
I need to remove this line. I tried several ways. Here is my current way to delete empty lines:

function RemoveEmptyLines($filename)
{
    $myfile = fopen($filename, "r") or die("Unable to open file!");
    $Content = fread($myfile,filesize($filename));
    fclose($myfile);
    $NewContent = preg_replace('/^\s+/m', '', $Content);
    $myfile2 = fopen('new.txt', "w") or die("Unable to open file!");
    fwrite($myfile2, $NewContent);
    fclose($myfile2);
    echo "removed";
}

This deletes all the empty lines within the textfile, but not the last empty line.
If the content is:

1 \n 2 \n \n 3

It deletes the empty line.
If it is:

1 \n 2 \n 3 \n \n

It doesn´t..
Any solutions?
The problem is, that the file is dynamic, so I can´t just delete the last line, because it´s not always empty..

php downloading a file from ftp doesn’t work locally using XAMPP

When I run the code below the local file gets deleted, I cannot work out why:

$local_file = 'test.csv';
$server_file = 'test_directory/test2.csv';
$ftp_server="servername";
$ftp_user="test";
$ftp_pass="pass";

$conn_id = ftp_connect($ftp_server);

// login with username and password`
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);

ftp_pasv($conn_id, TRUE);

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    echo "Successfully written to $local_file\n";
} else {
    echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);

So I get an error on the console showing cannot open no file or directory.

Edit:

Renaming the file to something else other than I am getting from ftp does not delete the file but still doesn’t work.

I am running this in XAMMP htdocs location.

cURL not working on server

I’m want to make a call to a public IP adress, and on my localhost it works fine. So I tested it on the server and it is not working. I get this message back

cURL Error #:Failed connect to 95.97.124.124:23; Connection timed out

This is the code i’m using, it is copied from postman

<?php

$curl = curl_init();
set_time_limit(0);
curl_setopt_array($curl, array(
    CURLOPT_PORT => "23", // PORT NEEDED TO CALL SERVER
    CURLOPT_URL => "http://95.97.124.124:23",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
        "Cache-Control: no-cache",
        "Postman-Token: 36b6ae24-1940-7227-5994-a5176f5006f3"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}

I changed some things but it still doesn’t work, please help.
Thanks in advance

Set array value to nothing in php

I try to set an array to nothing but it does not work (which is kind of expected).

$array = [
    'some_key' => my_function()

     /* 30 more rows of values */
];

function my_function() {
    return null;
}

print_r($array);

The above will display:

Array(
    'some_key' =>
)

The result I want is in this case an empty array item, because the function return a null value.

The alternative is a bit more ugly:

$some_key = my_function();

$array = [
     /* 30 more rows of values */
];

if(isset($some_key)) {
    $array['some_key] = $some_key;
}

function my_function() {
    return null;
}

print_r($array);

The reason I don’t like this approach is that I need to step away from my array tree and add the values in later.

It’s easier to see in a large array like this one:

This is my output but the input looks similar.

[component] => Array
    (
        [id] => my-snippet
        [raw] => my-snippet
        [view] => preview
        [template] => tool
        [type] => snippet
        [ctype] => text/html
        [url] => 
    )

To add functions or variables to this array tree looks great. Storing the array in a variable and then add the variables later is not as readale. Is it possible to solve?

AJAX Request Returns 404 with “select from” Anywhere in a Submitted Text Field [on hold]

A user is submitting a form using ajax with some notes that contain “select” and “from” organically in it, with many words in between them, and the server is returning a 404 error before even reaching the PHP script. Anybody seen this before or aware of any server-side SQL injection checks that would be occurring?