Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: sNews 1.8 Reborn discussion  (Read 1406 times)

nukpana

  • Hero Member
  • *****
  • Karma: 74
  • Posts: 751
sNews 1.8 Reborn discussion
« on: April 30, 2016, 12:09:32 PM »

There is a new version of sNews in testing, noted here:
http://snewscms.com/forum/index.php/topic,11088.msg70991.html

For discussion, please continue in this thread. (I cannot, so I am creating this).

If you find bugs, add it to the github tracker:
https://github.com/snewscms/snews1.80/issues

If you want to contribute, please do a pull request:
https://github.com/snewscms/snews1.80/pulls

How to use git?
https://git-scm.com/book/en/v1/Getting-Started (Lots of docs)
http://rogerdudler.github.io/git-guide/ (Simpler)
http://redaxscript.com/develop (Very simple guide)
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 74
  • Posts: 751
Re: sNews 1.8 Reborn discussion
« Reply #1 on: May 01, 2016, 11:08:19 PM »

So a few questions that I had before I continue:

a) What is the benefit of doing this now?  If it's just to maintain functionality by updating the mysql_* functions then...

b) Why wasn't any of the previous work done in these forums included?  There has been a lot of work, that doesn't appear to have been considered.
Logged

Rui Mendes

  • Development,Testing, Support
  • sNews Dude
  • Hero Member
  • *****
  • Karma: 195
  • Posts: 1063
  • sNews1.80 Reborn
    • Comunidade Portuguesa
Re: sNews 1.8 Reborn discussion
« Reply #2 on: May 02, 2016, 12:34:17 AM »

Hello Jason.

This version it is for maintain functionality.

I know you did hard work for snews 2.0 core framework, it's good code to start.

As you know I have my cms and next version will be 1.70 (not OOP). The main idea is to support (bootstrap, jquerymobile, angular,..), keep sequecial programming and few files.  Almost a year I made a OOP version (ebookcms 2.0), some functions are working fine. There are some movements against to OOP. I really don't know if I will finish my ebookcms OOP version.

About snews 2.0 core framework, I like the idea and I'm with you.

I have another version 1.80_bootstrap (Work In Progress)
Logged
Need a Job on Europe. Linkdin - Facebook / Group

nukpana

  • Hero Member
  • *****
  • Karma: 74
  • Posts: 751
Re: sNews 1.8 Reborn discussion
« Reply #3 on: May 02, 2016, 02:50:23 AM »

Right, that was not the work I was referring to.  That was abandoned, due to me realizing I was re-creating work that others have already done ie frameworks.

Since you continued with 1.7, the work I was referring to was specifically:
 - 1.7 PDO Conversion:
http://snewscms.com/forum/index.php/topic,10722.0.html
http://snewscms.com/forum/index.php/topic,10722.msg70807.html#msg70807

 - Updating 1.7 - moving towards a 1.8 version? (which 3 of the dudes ok'd this outline)
http://snewscms.com/forum/index.php/topic,10650.0.html

 - Improve one function a week - In response to the bloat of 1.7
http://snewscms.com/forum/index.php/topic,8391.0.html

 - [WIP] No Inline Javascript - based from the code from the above thread
http://snewscms.com/forum/index.php/topic,10007.0.html

 - [Tutorial] Custom Template Functions - To help separate out the code from the template
http://snewscms.com/forum/index.php/topic,8974.0.html

 - Function to help target content types for template authors
http://snewscms.com/forum/index.php/topic,10579.msg69868.html#msg69868

 - Separating Markup from Logic
http://snewscms.com/forum/index.php/topic,10534.0.html

I state this because the work done above to help try and modernize this script to some extent (a long time ago, I know), written by most of us to help improve the script for everyone.

This is one of the code that bothers me:
Code: [Select]
// CONFIGURE DATABASE VARIABLES (eBookCMS.com)
function db_cfg($field) { static $dbcfg;
if (!$dbcfg) {
$dbcfg = array(
'driver' => 'pdo', // by default is pdo but if your database is mysql (mysqli/pdo)
'dbtype' => 'sqlite', // database type
'server' => 'localhost',
'database' => 'snews18',
'username' => 'root',
'password' => '',
'dbpath' => 'snews.db3', // only sqlite PS: For security reasons rename this database and put your filename
'prefix' => ''
);
} return $dbcfg[$field];
}

// DATABASE CONNECTION
function db() { static $conn;
if (!$conn) { $conn = false;
$driver = db_cfg('driver');
$server = db_cfg('server');
$dbtype = db_cfg('dbtype');
$database = db_cfg('database');
$username = db_cfg('username');
$password = db_cfg('password');
$dbpath = db_cfg('dbpath');
// DATABASE TYPE
switch($dbtype) {
case "mysql"  : $dbconn = "mysql:host=$server;dbname=$database;"; break;
case "sqlite" : $dbconn = "sqlite:$dbpath"; break;
case "postgresql" : $dbconn = "pgsql:host=$server dbname=$database"; break;
case "sqlexpress" : $dbconn = "mssql:host=$server dbname=$database"; break;
case "firebird" : $dbconn = "firebird:dbname=$server:$dbpath"; break;
}
if ($driver == 'pdo' || $driver == 'PDO') {
try {$conn = new PDO($dbconn, $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));}
catch (PDOException $msg) {die ('Connection error, because: '.$msg->getMessage());}
} else if ($driver == 'mysqli' || $driver == 'MYSQLI') {
$conn = new mysqli($server, $username, $password, $database);
if (mysqli_connect_errno()) {printf("Connect failed: %s\n", mysqli_connect_error()); exit();}
} else {die('<h1>Driver Error.</h1><p>Check your db_cfg (<b>DB_DRIVER</b>)</p>');}
} return $conn;
}

// eBookCMS - Mysqli Bind and PDO
function dbbind($result, $args, $binds) {
$driver = db_cfg('driver') != 'mysqli' ? true : false;
$mysqlnd = function_exists('mysqli_fetch_all');
if ($driver !== false) {try {$result -> execute($args);} catch (PDOException $msg) {
die ('Connection error, because: '.$msg->getMessage());} return $result;
} else {
$count = is_array($args) ? count($args) : 0;
$bindParamsMethod = new ReflectionMethod('mysqli_stmt', 'bind_param');
$bindParamsReferences = array();
foreach ($args as $key => $value) {$bindParamsReferences[$key] = &$args[$key];}
array_unshift($bindParamsReferences, $binds);
$bindParamsMethod -> invokeArgs($result, $bindParamsReferences);
$result -> execute();
if ($mysqlnd) {return $result -> get_result();} else {return $result;}
}
}

// eBookCMS - FETCH
function dbfetch($result, $prepared = false) {
$driver = db_cfg('driver');
$mysqlnd = function_exists('mysqli_fetch_all');
if (!isset($result)) {return null;}
if ($driver == 'pdo') {return $result -> fetch(PDO::FETCH_ASSOC);} else
if ($prepared && $driver == 'mysqli' && $mysqlnd) {return $result -> fetch_assoc();} else
if ($prepared && $driver == 'mysqli' && !$mysqlnd) {
echo 'Extension "mysqlnd" is disable or not installed. Please use PDO see db_cfg)'; return;
} else if ($driver == 'mysqli') {return mysqli_fetch_assoc($result);}
}
Since I see ebookcms commented here (and other places), I figure you just pulled it from your code and pasted it here. I am unsure why you did this and not gone with simply PDO.  PDO covers all the databases you note and does prepared statements, which is also alot easier to read ie :value instead of ?.  If you liked mysqli, then why include the other Databases? Just stick with mysqli.

There are more issues I could note (using password_hash/verify instead of md5, using public as the document root, etc).

Quote
Almost a year I made a OOP version (ebookcms 2.0), some functions are working fine. There are some movements against to OOP. I really don't know if I will finish my ebookcms OOP version.
Perhaps this community was against OOP, but most of the PHP developers are using OOP.  Use the tool that best fits the job and use code that helps you solve your problems - ie most of the PHP world uses Composer to pull in dependencies in their work.  So if you wanted to use Laravel's Eloquent in sNews 1.80, you can use composer to pull in that dependency, then use the code and upload the whole thing to your site.  Writing your own code to solve problems already solved doesn't make sense anymore - I don't have time to re-create the wheel.
Logged

Rui Mendes

  • Development,Testing, Support
  • sNews Dude
  • Hero Member
  • *****
  • Karma: 195
  • Posts: 1063
  • sNews1.80 Reborn
    • Comunidade Portuguesa
Re: sNews 1.8 Reborn discussion
« Reply #4 on: May 02, 2016, 11:42:12 AM »

Good morning.

I pick all stuff you did and you have this package. I hope don't miss code.

Keep this version for maintain functionality.
- Separating Markup from Logic (maybe sNews2.0 with OOP as MVC)
- Custom Template Functions - same MVC

In you concern lines:
- Maybe your db_cfg is better because you have another file and keep snews updated without touching cfg
- I don't like mysqli, I love PDO but I decide to put here because if you use mysql database, some tells is faster. And is another class included, why not?
- dbbind (for prepared statements) and dbfetch are both functions for PDO and msqli. I create those to use same syntax in queries:

Code: [Select]
** $sql ="....";
** if ($result = db() -> query($sql)) {
** while ($r = dbfetch($result)) {
**         ....
**     }
** }

For prepared statements (i-integer s-string)
Code: [Select]
** $sql = "SELECT * FROM table WHERE id = ?
** if ($result = db() -> prepare($sql)) {
** $result = dbbind($result, array($id), 'i');
** while ($r = dbfetch($result)) {
**          ....
**      }
** }

I'm not against to OOP, in my cms is another story.

Back again to sNews 1.80. I pick snews1.71 and I start codding from scratch, sNews Reborn it's my solution for next version.
Since you made hard work too, I decide put decision in our community what version should be official and I accept final decision.

Jason you have some issues in your package, maybe is my mistake. I tried to use sQlite in your config
Quote
database.dsn = "sqlite:snews.db3"
and there are some bugs you need to fix when you save articles, and see categories.
Logged
Need a Job on Europe. Linkdin - Facebook / Group

nukpana

  • Hero Member
  • *****
  • Karma: 74
  • Posts: 751
Re: sNews 1.8 Reborn discussion
« Reply #5 on: May 02, 2016, 12:56:14 PM »

You didn't have to implement anything.   I was curious as to why previous work wasn't considered.

MVC can be done without using OOP. 
The ideas behind the below threads were to utilize that idea
[Tutorial] Custom Template Functions http://snewscms.com/forum/index.php/topic,8974.0.html
Separating Markup from Logic http://snewscms.com/forum/index.php/topic,10534.0.html

Before Symfony is added, this is just procedural code.
http://symfony.com/doc/current/book/from_flat_php_to_symfony2.html

I never finished the PDO conversion due to the complexity of the admin section, then a hard drive crash.  The code was that was there was used as a jump start for anyone to move forward with, since I lost interest.  I even provided helper functions as well.. Another option is (haven't tested it in ages...):

Code: [Select]
    $pdo = new PDO('xx', 'xx', 'xx');
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    DB::init($pdo);

    class DB {
        public static $pdo;
       
        public static function init(\PDO $pdo) {
            self::$pdo = $pdo;
        }

        public static function query() {
            $result = self::$pdo->prepare($sql);
            foreach ($array as $k => $params) {
                $placeholder = isset($params[0]) ? $params[0] : '';
                $value       = isset($params[1]) ? $params[1] : '';
                $type        = isset($params[2]) ? $params[2] : PDO::PARAM_STR;
                $result->bindValue($placeholder, $value, $type);
            }
            $result->execute();
            return $result;
        }
    }

// Example
            $query = 'SELECT * FROM articles ORDER BY id DESC';
            return DB::query($query)->fetchAll();

            $query = 'SELECT * FROM articles WHERE id = :id';
            return DB::query($query, [[':id', $id, PDO::PARAM_INT]])->fetchAll();

            DB::query(
               'INSERT INTO articles (title, text, date, status)
                VALUES(:title, :text, :date, :status)',
                [
                    [':title', $array['title'],
                    [':text', $array['text']],
                    [':date', $array['date']]
                    [':status', $array['publish']]
                ]
            );

My concerns were readability and maitanence.  On the specific code I noted was this:
 - db_cfg - driver should be the database driver, not PHP interface.  Pick one and stick with it.  You noted PDO, then most of this code is not needed.
 - dbbind doesn't even use PDO.  i,d,s,b type checks are used for mysqli.  So if one chooses PDO, then the data isn't being checked. Where is the mysqli "i" linked to PDO's "PDO::PARAM_INT"? 
 - dbbind isn't even used throughout the codebase.
 - So you used ? for both PDO & mysqli, but PDO can do that as well.  Why re-engineer something that's already done? As stated, pick one and stick with it.

Quote
I'm not against to OOP, in my cms is another story.
I am not sure why this is brought up, but you use the option best suited to solve your problem and where other developers can help out.  I would used third party code, where possible, as it's been tried and tested and easy to implement (ala composer).

Quote
Since you made hard work too, I decide put decision in our community what version should be official and I accept final decision.
I am not asking for a community decision, I was curious why you didn't review, consider ideas and code already done (and some unfinished) to help this project.  You put this on github, so continue developing and let people do pull requests and note issues there.  That's where your involvement will be.  This thread is for anything more beyond bugs and requests. 
« Last Edit: May 02, 2016, 01:29:11 PM by nukpana »
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 74
  • Posts: 751
Re: sNews 1.8 Reborn discussion
« Reply #6 on: May 02, 2016, 02:53:47 PM »

I also want to note, I really don't program anymore.  I keep up with but dont touch it as much anymore.  I am happy to see new progress on this script and wish everyone good luck.  Its ok if you chose to use my or anyone elses ideas or not.
Logged

Rui Mendes

  • Development,Testing, Support
  • sNews Dude
  • Hero Member
  • *****
  • Karma: 195
  • Posts: 1063
  • sNews1.80 Reborn
    • Comunidade Portuguesa
Re: sNews 1.8 Reborn discussion
« Reply #7 on: May 02, 2016, 05:16:56 PM »

Hello Jason.

You're right about dbbind I removed it. I keep dbfetch with some modifications.
I pick your config and put in this version.

I need check if this modification is working 100% before I put on github
Logged
Need a Job on Europe. Linkdin - Facebook / Group

nukpana

  • Hero Member
  • *****
  • Karma: 74
  • Posts: 751
Re: sNews 1.8 Reborn discussion
« Reply #8 on: May 03, 2016, 12:59:42 AM »

Here's a helpful hint: since you are using github, you should always upload your updates to github - that's what's it's there for.  You can tag them as testing, development, master, etc. and each will give a download link.  You shouldn't need to make a zip and host it.  As long as you posted your updates, github will make the zip for you.  With github you can revert changes as well.

I haven't tested anything, but it looks fine.  I still disagree with dbfetch, but again, that's your call.
Logged