За последние 24 часа нас посетили 22802 программиста и 1223 робота. Сейчас ищут 772 программиста ...

Переписать код на php7

Тема в разделе "Сделайте за меня", создана пользователем Negat1ve, 11 авг 2019.

  1. Negat1ve

    Negat1ve Новичок

    С нами с:
    11 авг 2019
    Сообщения:
    2
    Симпатии:
    0
    Доброго времени суток.
    Есть ли у кого нить желание и время переписать данный код на php 7 ?
    PHP:
    1. <?php
    2. //////////////////////////////
    3. // EDIT THESE TWO VARIABLES //
    4. //////////////////////////////
    5. $MySQLUsername = "USERNAME HERE";
    6. $MySQLPassword = "PASSWORD HERE";
    7.  
    8. /////////////////////////////////
    9. // DO NOT EDIT BELOW THIS LINE //
    10. /////////////////////////////////
    11. $MySQLHost = "localhost";
    12. $MySQLDB = "gpio";
    13.  
    14. If (($MySQLUsername == "USERNAME HERE") || ($MySQLPassword == "PASSWORD HERE")){
    15.     print 'ERROR - Please set up the script first';
    16.     exit();
    17. }
    18.  
    19. $dbConnection = mysql_connect($MySQLHost, $MySQLUsername, $MySQLPassword);
    20. mysql_select_db($MySQLDB, $dbConnection);
    21. If (isset($_POST['action'])){
    22.     If ($_POST['action'] == "setPassword"){
    23.         $password1 = $_POST['password1'];
    24.         $password2 = $_POST['password2'];
    25.         If ($password1 != $password2){
    26.             header('Location: control.php');
    27.         }
    28.         $password = mysql_real_escape_string($_POST['password1']);
    29.         If (strlen($password) > 28){
    30.             mysql_close();
    31.             header('location: control.php');
    32.         }
    33.         $resetQuery = "SELECT username, salt FROM users WHERE username = 'admin';";
    34.         $resetResult = mysql_query($resetQuery);
    35.         If (mysql_num_rows($resetResult) < 1){
    36.             mysql_close();
    37.             header('location: control.php');
    38.         }
    39.         $resetData = mysql_fetch_array($resetResult, MYSQL_ASSOC);
    40.         $resetHash = hash('sha256', $salt . hash('sha256', $password));
    41.         $hash = hash('sha256', $password);
    42.         function createSalt(){
    43.             $string = md5(uniqid(rand(), true));
    44.             return substr($string, 0, 8);
    45.         }
    46.         $salt = createSalt();
    47.         $hash = hash('sha256', $salt . $hash);
    48.         mysql_query("UPDATE users SET salt='$salt' WHERE username='admin'");
    49.         mysql_query("UPDATE users SET password='$hash' WHERE username='admin'");
    50.         mysql_close();
    51.         header('location: control.php');
    52.     }
    53. }
    54. If ((isset($_POST['username'])) && (isset($_POST['password']))){
    55.     $username = mysql_real_escape_string($_POST['username']);
    56.     $password = mysql_real_escape_string($_POST['password']);
    57.     $loginQuery = "SELECT UserID, password, salt FROM users WHERE username = '$username';";
    58.     $loginResult = mysql_query($loginQuery);
    59.     If (mysql_num_rows($loginResult) < 1){
    60.         mysql_close();
    61.         header('location: control.php?error=incorrectLogin');
    62.     }
    63.     $loginData = mysql_fetch_array($loginResult, MYSQL_ASSOC);
    64.     $loginHash = hash('sha256', $loginData['salt'] . hash('sha256', $password));
    65.     If ($loginHash != $loginData['password']){
    66.         mysql_close();
    67.         header('location: control.php?error=incorrectLogin');
    68.     } else {
    69.         session_regenerate_id();
    70.         $_SESSION['username'] = "admin";
    71.         $_SESSION['userID'] = "1";
    72.         mysql_close();
    73.         header('location: control.php');
    74.     }
    75. }
    76. If ((!isset($_SESSION['username'])) || (!isset($_SESSION['userID']))){
    77.     print '
    78.    <html>
    79.    <head>
    80.    <title>GPIO Test Page - Login</title>
    81.    </head>
    82.    <body>
    83.    <table border="0" align="center">
    84.    <form name="login" action="control.php" method="post">
    85.    <tr>
    86.    <td>Username: </td><td><input type="text" name="username"></td>
    87.    </tr>
    88.    <tr>
    89.    <td>Password: </td><td><input type="password" name="password"></td>
    90.    </tr>
    91.    <tr>
    92.    <td colspan="2" align="center"><input type="submit" value="Log In"></td>
    93.    </tr>
    94.    </form>
    95.    </table>
    96.    </body>
    97.    </html>
    98.    ';
    99.     die();
    100. }
    101. If (isset($_GET['action'])){
    102.     If ($_GET['action'] == "logout"){
    103.         $_SESSION = array();
    104.         session_destroy();
    105.         header('Location: control.php');
    106.     } else If ($_GET['action'] == "setPassword"){
    107.         print '
    108.        <form name="changePassword" action="control.php" method="post">
    109.        <input type="hidden" name="action" value="setPassword">
    110.        <p>Enter New Password: <input type="password" name="password1">  Confirm: <input type="password" name="password2"><input type="submit" value="submit"></p>
    111.        </form>
    112.        ';
    113.     } else {
    114.         $action = $_GET['action'];
    115.         $pin = mysql_real_escape_string($_GET['pin']);
    116.         if ($action == "turnOn"){
    117.             $setting = "1";
    118.             mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='$pin';");
    119.             mysql_close();
    120.             header('Location: control.php');
    121.         } else If ($action == "turnOff"){
    122.             $setting = "0";
    123.             mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='$pin';");
    124.             mysql_close();
    125.             header('Location: control.php');
    126.         } else IF ($action =="edit"){
    127.             $pin = mysql_real_escape_string($_GET['pin']);
    128.             $query = mysql_query("SELECT pinDescription FROM pinDescription WHERE pinNumber='$pin';");
    129.             $descRow = mysql_fetch_assoc($query);
    130.             $description = $descRow['pinDescription'];
    131.             print '
    132.            <html><head><title>Update Pin ' . $pin . '</title></head><body>
    133.            <table border="0">
    134.            <form name="edit" action="control.php" method="get">
    135.            <input type="hidden" name="action" value="update">
    136.            <input type="hidden" name="pin" value="' . $pin . '">
    137.            <tr>
    138.            <td><p>Description: </p></td><td><input type="text" name="description" value="' . $description . '"></td><td><input type="submit" value="Confirm"></td>
    139.            </tr>
    140.            </form>
    141.            </table>
    142.            </body></html>
    143.            ';
    144.             mysql_close();
    145.         } else IF ($action =="update"){
    146.             $pin = mysql_real_escape_string($_GET['pin']);
    147.             $description = mysql_real_escape_string($_GET['description']);
    148.             mysql_query("UPDATE pinDescription SET pinDescription='$description' WHERE pinNumber='$pin';");
    149.             header('Location: control.php');
    150.         } else {
    151.             header('Location: control.php');
    152.         }
    153.     }
    154. } else {
    155.     print '
    156.        <html>
    157.        <head>
    158.        <title>GPIO Test Page</title>
    159.        </head>
    160.        <font face="verdana">
    161.        <p>GPIO Test Page   <a href="control.php?action=setPassword">Change Password</a></p>
    162.        ';
    163.         $query = mysql_query("SELECT pinNumber, pinStatus FROM pinStatus;");
    164.         $query2 = mysql_query("SELECT pinNumber, pinDescription FROM pinDescription;");
    165.         $totalGPIOCount = mysql_num_rows($query);
    166.         $currentGPIOCount = 0;
    167.         print '<table name="GPIO" border="1" cellpadding="5">';
    168.         print '<tr><th>GPIO #</th><th>GPIO Description</th><th>Status</th><th>Action</th><th>Edit</th></tr>';
    169.         while ($currentGPIOCount < $totalGPIOCount){
    170.             $pinRow = mysql_fetch_assoc($query);
    171.             $descRow = mysql_fetch_assoc($query2);
    172.             $pinNumber = $pinRow['pinNumber'];
    173.             $pinStatus = $pinRow['pinStatus'];
    174.             $pinDescription = $descRow['pinDescription'];
    175.             If ($pinStatus == "0"){
    176.                 $buttonValue = "Turn On";
    177.                 $action = "turnOn";
    178.                 $image = "off.jpg";
    179.             } else {
    180.                 $buttonValue = "Turn Off";
    181.                 $action = "turnOff";
    182.                 $image = "on.jpg";
    183.             }
    184.             print '<tr>';
    185.             print '<td align="center">' . $pinNumber . '</td><td>' . $pinDescription . '</td><td align="center"><img src="' . $image . '" width="50"></td><td align="center" valign="middle"><form name="pin' . $pinNumber . 'edit" action="control.php" method="get"><input type="hidden" name="action" value="' . $action . '"><input type="hidden" name="pin" value="' . $pinNumber . '"><input type="submit" value="' . $buttonValue . '"></form></td><td><form name="pin' . $pinNumber . '" action="control.php" method="get"><input type="hidden" name="action" value="edit"><input type="hidden" name="pin" value="' . $pinNumber . '"><input type="submit" value="Edit"></form></td>';
    186.             print '</tr>';
    187.             $currentGPIOCount ++;
    188.         }
    189.         print '</table>';
    190.         mysql_close();
    191.     print '
    192.    <br><br>
    193.    <a href="control.php?action=logout">Log out</a>
    194.    </font>
    195.    </html>
    196.    ';
    197. }
    198. ?>
     
  2. lex-aizen

    lex-aizen Новичок

    С нами с:
    14 дек 2018
    Сообщения:
    4
    Симпатии:
    0
  3. Negat1ve

    Negat1ve Новичок

    С нами с:
    11 авг 2019
    Сообщения:
    2
    Симпатии:
    0
    Во первых Вы обознались
    Во-вторых я не планировал платить.
     
  4. Prometheus

    Prometheus Новичок

    С нами с:
    17 авг 2019
    Сообщения:
    8
    Симпатии:
    0
    Если приложение работает, трогать не надо. Если хотите использовать несколько версий PHP, используйте php-fpm, так чтоб и старые и новые скрипты работали.