Advertisement:

Author Topic: sleep in plugin?  (Read 356 times)

Offline alex_qrlogin

  • Semi-Newbie
  • *
  • Posts: 42
    • qrlogi on Facebook
    • qrLogin
sleep in plugin?
« on: December 20, 2017, 02:36:39 AM »
I received recommendation  do not use sleep in plugin ((
I do not have too much experience in PHP.
What is the problem with sleep in PHP??
How can I replace it to wait for an event in the script?

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 68,361
    • Arantor on GitHub
Re: sleep in plugin?
« Reply #1 on: December 20, 2017, 03:14:10 AM »
What event do you need to wait for, exactly?
To assume is to hope that those who came before had the presence of mind and capacity to implement the dreams of those who would come after.

You either die a hero or live long enough to see yourself become the villain. It seems you have chosen which, and now I must do the same.

Offline alex_qrlogin

  • Semi-Newbie
  • *
  • Posts: 42
    • qrlogi on Facebook
    • qrLogin
Re: sleep in plugin?
« Reply #2 on: December 20, 2017, 03:24:46 AM »
What event do you need to wait for, exactly?
I use "long polling" ajax requests for waiting login from mobile phone)) And waiting in request from mobile for confirmation of login))
Waiting for the appearance / change of the record in the database table

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 68,361
    • Arantor on GitHub
Re: sleep in plugin?
« Reply #3 on: December 20, 2017, 03:37:08 AM »
Is the sleep in the same thread as the update request itself?
To assume is to hope that those who came before had the presence of mind and capacity to implement the dreams of those who would come after.

You either die a hero or live long enough to see yourself become the villain. It seems you have chosen which, and now I must do the same.

Offline alex_qrlogin

  • Semi-Newbie
  • *
  • Posts: 42
    • qrlogi on Facebook
    • qrLogin
Re: sleep in plugin?
« Reply #4 on: December 20, 2017, 03:43:18 AM »
Is the sleep in the same thread as the update request itself?
no

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 68,361
    • Arantor on GitHub
Re: sleep in plugin?
« Reply #5 on: December 20, 2017, 03:45:56 AM »
So you have an AJAX request that does something, and a second AJAX request to watch to see when the first is done to act on it?
To assume is to hope that those who came before had the presence of mind and capacity to implement the dreams of those who would come after.

You either die a hero or live long enough to see yourself become the villain. It seems you have chosen which, and now I must do the same.

Offline alex_qrlogin

  • Semi-Newbie
  • *
  • Posts: 42
    • qrlogi on Facebook
    • qrLogin
Re: sleep in plugin?
« Reply #6 on: December 20, 2017, 03:54:54 AM »
No)))
I have request from mobile phone with login data and ajax request with waiting it))
Code: [Select]
function qrlogin_ajax()
{
    global $context, $modSettings, $smcFunc, $user_settings;

    // check ajax request
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')
        qrlogin_hack_exit();

// check session_id in 'qrlogin_sid'
    if (!isset($_REQUEST['qrlogin_sid']) || empty($_REQUEST['qrlogin_sid']) )
        qrlogin_hack_exit();
    $asid = preg_split( "/=/", $_REQUEST['qrlogin_sid']);
    if ($asid[0] != $context['session_id'])
        qrlogin_hack_exit();

    $sid = md5('qrlogin' . $_REQUEST['qrlogin_sid']);
    $poll_lifetime = $modSettings['qrlogin_poll_lifetime'];

    // waiting for uid from App qrLogin - max $poll_lifetime s
    while (!$uid = qrlogin_get_field_session('uid', $sid))
    {
        if (--$poll_lifetime < 0) exit;
        sleep(1);
        if (connection_aborted()) exit;
    }
   
  // uid received - register function for answer to App qrLogin
    register_shutdown_function('qrlogin_ajax_shutdown', $sid);

  // Load user data
$request = $smcFunc['db_query']('', '
SELECT *
FROM {db_prefix}members
WHERE id_member = {int:id_member}
LIMIT 1',
array('id_member' => $uid)
);
// user not found !
if ($smcFunc['db_num_rows']($request) == 0) exit;

$user_settings = $smcFunc['db_fetch_assoc']($request);
$smcFunc['db_free_result']($request);

// Login user
    require_once('LogInOut.php');
    DoLogin();
}

function qrlogin_ajax_shutdown($sid)
{
    global $user_info, $smcFunc;

    // write result of login to post
    $smcFunc['db_query']('', '
        UPDATE {db_prefix}qrlogin
        SET result = {int:result}
        WHERE sid = {string:sid}',
        array(
            'result' => $user_info['is_guest'] ? 403 : 200,
            'sid' => $sid,
        )
);
}

function qrlogin_post()
{
    global $smcFunc, $modSettings;

    // check for post from App qrLogin
    if (substr($_SERVER['HTTP_USER_AGENT'], 0, 7) != 'qrLogin')
        qrlogin_hack_exit();
  // if data not correct
    if (!isset($_REQUEST['objectName']) || ($_REQUEST['objectName'] != 'qrLogin') ||
        !isset($_REQUEST['sessionid']) || empty($_REQUEST['sessionid']) ||
        !isset($_REQUEST['login']) || empty($_REQUEST['login']) ||
        !isset($_REQUEST['password']) || empty($_REQUEST['password']))
        qrlogin_hack_exit();

  // Load the user data!
$request = $smcFunc['db_query']('', '
SELECT passwd, id_member, member_name
FROM {db_prefix}members
WHERE member_name = {string:user_name}
LIMIT 1',
array(
            'user_name' => urldecode($_REQUEST['login']),
        )
);
// no user found
if ($smcFunc['db_num_rows']($request) == 0)
{
        http_response_code(403);
        exit;
}

$user_settings = $smcFunc['db_fetch_assoc']($request);
$smcFunc['db_free_result']($request);

// Bad password!
if (!hash_verify_password($user_settings['member_name'], un_htmlspecialchars(urldecode($_REQUEST['password'])), $user_settings['passwd']))
// if ($user_settings['passwd'] != sha1(strtolower($user_settings['member_name']) . un_htmlspecialchars(urldecode($_REQUEST['password']))))
{
        http_response_code(403);
        exit;
}

$sid = md5('qrlogin' . urldecode($_REQUEST['sessionid']));

// remove queue from db
    $smcFunc['db_query']('', '
        DELETE FROM {db_prefix}qrlogin
        WHERE sid = {string:sid}',
        array(
            'sid' => $sid,
        )
    );

// insert queue into db
$smcFunc['db_insert']('insert',
            '{db_prefix}qrlogin',
            array(
                'sid' => 'string',
                'uid' => 'int',
            ),
            array(
                $sid, $user_settings['id_member'],
            ),
            array('sid')
        );

    // waiting for answer - max qrlogin_post_timeout s
    $post_timeout = $modSettings['qrlogin_post_timeout'];
while ((!$ans = qrlogin_get_field_session('result', $sid)) && ($post_timeout-- > 0))
{
sleep(1);
}

// remove queue from db
    $smcFunc['db_query']('', '
        DELETE FROM {db_prefix}qrlogin
        WHERE sid = {string:sid}',
        array(
            'sid' => $sid,
        )
    );

// if not exists answer ! 408 Request Timeout
    http_response_code($ans ? $ans : 408 );
    exit;
}
qrlogin_post() - from mobile
qrlogin_ajax() - from ajax