Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
Torrent Trader PDO Update Beta Test
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / M-BiTsy / M-BiTsy Support

2020-01-24 18:22:54
[color=rgb(250, 197, 28)]Please Do NOT Use On Live Site This Is A Work In Progress[/color]

This is a project ive been working on for the last 3 months converting TT to PDO. [color=rgb(250, 197, 28)]I am hoping for testers i have a live site which members may signup to to help look for bugs[/color].

Its like [color=rgb(250, 197, 28)]97%[/color] done, there maybe some bugs so please report them if you try.

There are two reasons for the bugs.

1) Apostrophes  - These are easily fixed its mainly query's there need changing to prepared statements
                            you can search all files in notepad+ for =? to see examples, this will allow the sqlesc function to be removed prepared statements are already escaped
                            Heres one in the backend function.php on line 182 i fixed - all are straight forward its just finding time now
old line
SQL_Query_exec("UPDATE users SET last_access='" . get_date_time() . "', ip=".sqlesc($ip).", page=".sqlesc($where)." WHERE id=" . $row["id"]);

now fix bug
$stmt = DB::run("UPDATE users SET last_access=?,ip=?,page=? WHERE id=?", [get_date_time(),$ip,$where,$id]);


2) PDO::rowCount -   This replaces the old sqli_num_rows this pdo function is not as universal as the sql function some work and some need to be looked at closer
                                 If where counting rows its probably better to pay attention to be kind to the database

* Due to the length of the admincp file i split it into smaller files and placed them in a admin folder to make it easier for myself (i was sick of continuously having to go through it all lol)
* I have added a bootstrap frame but left the original  theme im planning on removing most of the theme images (ive made a start)
* I added a exception handler with the abilitiy to get a stack back trace this will make it easier for debugging (any exception will show general error page and log error to exception_error.txt this file needs to be watched for size)
* The only thing ive changed from v3 is the updated password the plan is to get the core fully working before adding any thing im just looking at improvements
* ive thrown a quick quide together you can search yoursite/pdointro.php ill do a more detailed one when i find the time
* The old server block does work but throw errors so i replaced that with a simple one this means i could remove the 2 serverload file in backend as well this will be improved

Now for the geeky stuff
Currently i use a public static function so the database can be called anywhere by anything like (this is just for show)
function deleteaccount($userid)
{
   DB::run("DELETE FROM users WHERE id = $userid");
}

The plan once fully tested and working is to use dependency injection this should improve performance like this
function deleteaccount($pdo, $userid)
{
   $pdo->run("DELETE FROM users WHERE id = $userid");
}

you can always just call the pdo class as well but i dont like how it looks
function deleteaccount(, $userid)
{
   $pdo = new pdo
   $pdo->run("DELETE FROM users WHERE id = $userid");
}


Long term im looking to make oop so maybe to remove $CURUSER and replace with a class so the pdo connect would be in the curuser class like below
With true oop we would use something like init.php with autoload and the connecion would be added in any class constructor
class CURUSER {
   protected $db;
   protected $data;
   public function __construct(PDO $db)
   {
       $this->pdo = $db;
   }


Heres the github https://github.com/M-jay84/Torrent-Trader-2020-PDO

I uploaded v3 as well so you can see the changes https://github.com/M-jay84/Torrent-Trader-2020-PDO/compare/v3-original...master?expand=1

Members are welcome to test and report any bugs or post your exception file or post the contents please beawre this is a work in progress and i still need to go through the admin part more which i hope to do this weekend
Any help here or on the github is welcome ive got loads on the go at the minute but want to get the core done first before i start implementing oop
Untitled.png
Untitled.png
Untitled.png
Untitled.png
Unknown User
2020-01-24 19:08:05
Some things to consider:

1. Avoid using sqli_num_rows it needs to loop through whole database, lock tables, then return results.  (While select count uses weight and cache to provide faster data without locking table)
Use SELECT COUNT(id) from x where y = ?

2. Autoload should be like:
folder "classes"
folder includes "init.php"
spl_autoload_register(function ($class)
{
   include_once './classes/' . $class . '.php';
});


3. If we go through this, then members, torrents, etc should follow the same OOP way :)

4. Not bad for the beggining
2020-01-24 19:17:26
theres a few alternatives in pdo for num_rows like rowcount, coloumncount, fetchcoloumn etc reading into this if i can get torrents and torrent-search working its just the admincp left
but i agree we need to stay away from looping through the database
Im not too concerned with the errors i can pick this up pretty easy its just performance but DI should help once im finished
I had a few classes with autoload like that but wanted to be certain everythings working 100% its all set to build on now though
removing all globals is a goal as well like cache this doesnt need to be global just use $cache = new TTcache and place in init and in any function
Unknown User
2020-01-24 20:10:22
Btw m-jay why don't we use a normal OOP pdo connection like:

// Connection

<?php

class MyPDO extends PDO
{
   public function __construct($dsn, $username = 'root', $password = NULL, $options = [])
   {
       $default_options = [
           PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
           PDO::ATTR_EMULATE_PREPARES => false,
           PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
       ];
       $options = array_replace($default_options, $options);
       parent::__construct($dsn, $username, $password, $options);
   }
   public function run($sql, $args = NULL)
   {
       if (!$args)
       {
           return $this->query($sql);
       }
       $stmt = $this->prepare($sql);
       $stmt->execute($args);
       return $stmt;
   }
}


Class for User

<?php

class User
{
   /* @var MyPDO */
   protected $db;

   protected $data;

   public function __construct(MyPDO $db)
   {
       $this->db = $db;
   }

   public function find($id)
   {
       $this->data = $this->db->run("SELECT * FROM users WHERE id = ?", [$id])->fetch();
       return $this->data;
   }
}


Index.php

<?php
include_once 'includes/class-autoload.inc.php';
// Call connection
$mypdo = new MyPDO('mysql:host=localhost;dbname=oop;charset=utf8');

// Class call
$user = new User($mypdo);
echo $user->find(1)['username'];

// Non class call
$id = 1;
$test = $mypdo->run('SELECT COUNT(*) FROM users')->fetch();
echo $test[0];
2020-01-24 21:10:18
look at the scrape.php that has a normal pdo connection like you suggest not in a class though  but it was so hard with the spaghetti code a static was quick but we can make that class, check.php is independent as well but i injected the dbclass to make it easier
and then mass edit all db::run any function that uses db will need class declaring or injecting into parameters
for the user class if you look at the user login function thats where global curuser is defined so its just putting that query in place of find in the example you posted
ps i fixed torrent search and torrents now just the admincp but i think ive got most of that updating github now with them two files
Unknown User
2020-01-24 21:34:33
Login is so badly written.

Here is a quick example i have in head right now.



public function login(){

     // Check if POST
     if($_SERVER['REQUEST_METHOD'] == 'POST'){
       // Sanitize POST
       $_POST  = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
     
       // Check for email
       if(empty($_POST['email'])){
         $error = 'Please enter email.';
       }

       // Check for password
       if(empty($_POST['password'])){
          $error = 'Please enter name.';
       }

if($this->db->run("SELECT * FROM users WHERE email = ?", [$email])->fetch())
{
// User found continue.
}
else
{
$error = 'Please enter a valid email.';
}

// Check if errors are empty
if(empty($error))
{
// proceed login
}

}
2020-01-24 21:48:20
ive played around with the login the cookies caused a bit off a headache if i remember correctly i had something roughly like this

$ip = new ip
$cookie = new cookie   // this should be session really
$curuser = new curuser

function login($ip, $cookie, $curuser){
$ip = get ip ();
$ip = iptype ();
something like your code
$cookie = set();
$curuser(id);

but my concern was thinking something was broken when actually it was the conversion which is why i reverted back to just the core, i have a habit of not finishing projects off so wanted to get this fully done i might put off doing the rest of the admincp and concentrate on the prepared statements the admin panel does not interact with anything else the rest seems to be more or less working 100% its just the apostrophe issue with query's i mentioned in first post
Unknown User
2020-01-24 21:57:27
I would do it like this:


// Check if errors are empty
if(empty($error))
{
$stmt = $this->db->run("SELECT * FROM users WHERE email = ?", [$email])->fetch()

$hashed_password = $stmt['password'];
     if(password_verify($_POST['password'], $hashed_password)){
       setcookie('uid',blabla);
       setcookie('password',blabla);
     } else {
       return false;
     }
}


Then for current user you just query select * from users where id = ? AND password = ? $_COOKIE['uid'], $_COOKIE['password']
and you get current user logged :P
2020-01-24 22:05:26
the reason i was leaning towards sessions was you can then add a roles class & permission class based on a session group id which i have also played around with lol but one step at a time ill get those statements finished this weekend theres only about 300 left lol
Unknown User
2020-01-24 22:13:44
I don't like sessions as they are stored inside /var/lib/php/sessions folder and are controlled by php's garabage collector. If site is way too popular you can get hundreds of thousand small files written per second.

If you call session_start() at some other place which is not active at all time, garbage collector will not clean sessions which expire.

And empty session files will be written per every site visit and after every browser close/open empty session file will be created. So 1 user can open/close browser 20 times, 20 new files will be created heh
2020-01-25 09:00:10
Did some more prepared statements this morning but got bored :sleep:, on the plus side I think I got all the errors that I know of and finished the admincp [color=rgb(250, 197, 28)]I've update github.[/color]
It's just the admin sql log this is because pdo prepares then executes or throws exception theres nothing in between
2020-01-27 00:44:55
Nice!!
2020-01-27 16:39:14
Thank you [USER=20382]@STorrents2020[/USER]   i dont suppose you would mind messaging me with your forum.php & the forum admin section not the full admincp. I have two last bugs with viewforum and staff panel minclassread if you use prepared statements might point me in right direction.
[USER=20311]@shadows[/USER]    i had a look at using that pdo class in one of the first posts im going to change to that at the weekend should be straight forward just wanted to mess with dependency injection  heres example you can upload to your site its just the check.php (line 261)
As we move to classes the functions connection parameter will be in class constructor
check.zip
Download
2020-02-06 15:02:50
ill update my github this weekend ive done most of the prepared statements and started removing the sqlesc function (pdo already escapes) just playing with styling at the minute.
I tried adding the new dbclass but blocks caused a issue so ill stick to the static one for now until i move functions to classes DB::instance() can always be added to constructor so functions move away from procedural
here is a example of my torrents-detail.php at the minute its all coded on one page but for oop would it be better to have these broken down into details.php, comments.php, nfo.php etc or does anyone have suggestion on how to do it with classes and views ?

[color=rgb(250, 197, 28)]example[/color]
<button type="button" onclick="show('id1');">DETAILS</button>
<button type="button" onclick="show('id6');">NFO</button>
<button type="button" onclick="show('id7');">COMMENTS</button>

echo "<div id=id1  style=display:block>"; // start id1
[color=rgb(250, 197, 28)]mainstuff[/color]
echo "</div>";    

echo "<div id=id6  style=display:none>";
[color=rgb(250, 197, 28)]nfo[/color]
echo "</div>";

echo "<div id=id7  style=display:none>";
[color=rgb(250, 197, 28)]comments[/color]
echo "</div>";


2020-02-06 15:42:45
"m-jay, post: 67831, member: 20288":

ill update my github this weekend ive done most of the prepared statements and started removing the sqlesc function (pdo already escapes) just playing with styling at the minute.
I tried adding the new dbclass but blocks caused a issue so ill stick to the static one for now until i move functions to classes DB::instance() can always be added to constructor so functions move away from procedural
here is a example of my torrents-detail.php at the minute its all coded on one page but for oop would it be better to have these broken down into details.php, comments.php, nfo.php etc or does anyone have suggestion on how to do it with classes and views ?

[color=rgb(250, 197, 28)]example[/color]
<button type="button" onclick="show('id1');">DETAILS</button>
<button type="button" onclick="show('id6');">NFO</button>
<button type="button" onclick="show('id7');">COMMENTS</button>

echo "<div id=id1  style=display:block>"; // start id1
[color=rgb(250, 197, 28)]mainstuff[/color]
echo "</div>";  

echo "<div id=id6  style=display:none>";
[color=rgb(250, 197, 28)]nfo[/color]
echo "</div>";

echo "<div id=id7  style=display:none>";
[color=rgb(250, 197, 28)]comments[/color]
echo "</div>";




I like this concept, it could become a standard :geek:
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 3
  • Members: 0
  • Guests online: 3
  • Comments: 3
  • Messages: 1,728