Dotclear

Changeset 3761:849987324197 for inc


Ignore:
Timestamp:
06/15/18 18:31:29 (7 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
sql-statement
Message:

Apply SQL Statement in DC code, work in progress

Location:
inc/core
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • inc/core/class.dc.auth.php

    r3731 r3761  
    9393    { 
    9494        # Check user and password 
    95         $strReq = 'SELECT user_id, user_super, user_pwd, user_change_pwd, ' . 
    96         'user_name, user_firstname, user_displayname, user_email, ' . 
    97         'user_url, user_default_blog, user_options, ' . 
    98         'user_lang, user_tz, user_post_status, user_creadt, user_upddt ' . 
    99         'FROM ' . $this->con->escapeSystem($this->user_table) . ' ' . 
    100         "WHERE user_id = '" . $this->con->escape($user_id) . "' "; 
     95        $sql = new dcSelectStatement($this->core, 'coreAuthCheckUser'); 
     96        $sql 
     97            ->columns(array('user_id', 'user_super', 'user_pwd', 'user_change_pwd', 'user_name', 'user_firstname', 
     98                'user_displayname', 'user_email', 'user_url', 'user_default_blog', 'user_options', 'user_lang', 
     99                'user_tz', 'user_post_status', 'user_creadt', 'user_upddt')) 
     100            ->from($this->user_table) 
     101            ->where('user_id = ' . $sql->quote($user_id)); 
    101102 
    102103        try { 
    103             $rs = $this->con->select($strReq); 
     104            $rs = $this->con->select($sql->statement()); 
    104105        } catch (Exception $e) { 
    105106            $err = $e->getMessage(); 
     
    146147                $cur           = $this->con->openCursor($this->user_table); 
    147148                $cur->user_pwd = (string) $rs->user_pwd; 
    148                 $cur->update("WHERE user_id = '" . $rs->user_id . "'"); 
     149 
     150                $sql = new dcUpdateStatement($this->core, 'coreAuthCheckUser'); 
     151                $sql->where('user_id = ' . $sql->quote($rs->user_id)); 
     152                $cur->update($sql->whereStatement()); 
    149153            } 
    150154        } elseif ($user_key != '') { 
     
    618622    public function recoverUserPassword($recover_key) 
    619623    { 
    620         $strReq = 'SELECT user_id, user_email ' . 
    621         'FROM ' . $this->user_table . ' ' . 
    622         "WHERE user_recover_key = '" . $this->con->escape($recover_key) . "' "; 
     624        $sql = new dcSelectStatement($this->core, 'coreAuthRecoverUserPwd'); 
     625        $sql 
     626            ->columns(array('user_id', 'user_email')) 
     627            ->from($this->user_table) 
     628            ->where('user_recover_key = ' . $sql->quote($recover_key)); 
    623629 
    624630        $rs = $this->con->select($strReq); 
     
    635641        $cur->user_change_pwd  = 1; // User will have to change this temporary password at next login 
    636642 
    637         $cur->update("WHERE user_recover_key = '" . $this->con->escape($recover_key) . "'"); 
     643        $sql = new dcUpdateStatement($this->core, 'coreAuthRecoverUserPwd'); 
     644        $sql->where('user_recover_key = ' . $sql->quote($recover_key)); 
     645        $cur->update($sql->whereStatement()); 
    638646 
    639647        return array('user_email' => $rs->user_email, 'user_id' => $rs->user_id, 'new_pass' => $new_pass); 
  • inc/core/class.dc.blog.php

    r3731 r3761  
    209209        $cur->blog_upddt = date('Y-m-d H:i:s'); 
    210210 
    211         $cur->update("WHERE blog_id = '" . $this->con->escape($this->id) . "' "); 
     211        $sql = new dcUpdateStatement($this->core, 'coreTriggerBlog'); 
     212        $sql 
     213            ->where('blog_id = ' . $sql->quote($this->id)); 
     214 
     215        $cur->update($sql->whereStatement()); 
    212216 
    213217        # --BEHAVIOR-- coreBlogAfterTriggerBlog 
     
    241245        # Get posts affected by comments edition 
    242246        if (empty($affected_posts)) { 
    243             $strReq = 
    244             'SELECT post_id ' . 
    245             'FROM ' . $this->prefix . 'comment ' . 
    246             'WHERE comment_id' . $this->con->in($comments_ids) . 
    247                 'GROUP BY post_id'; 
    248  
    249             $rs = $this->con->select($strReq); 
     247            $sql = new dcSelectStatement($this->core, 'coreTriggerCommentsScope'); 
     248            $sql 
     249                ->columns('post_id') 
     250                ->from($this->prefix . 'comment') 
     251                ->where('comment_id' . $sql->in($comments_ids)) 
     252                ->group('post_id'); 
     253 
     254            $rs = $this->con->select($sql->statement()); 
    250255 
    251256            $affected_posts = array(); 
     
    253258                $affected_posts[] = (integer) $rs->post_id; 
    254259            } 
     260            unset($sql); 
    255261        } 
    256262 
     
    260266 
    261267        # Count number of comments if exists for affected posts 
    262         $strReq = 
    263         'SELECT post_id, COUNT(post_id) AS nb_comment, comment_trackback ' . 
    264         'FROM ' . $this->prefix . 'comment ' . 
    265         'WHERE comment_status = 1 ' . 
    266         'AND post_id' . $this->con->in($affected_posts) . 
    267             'GROUP BY post_id,comment_trackback'; 
    268  
    269         $rs = $this->con->select($strReq); 
     268        $sql = new dcSelectStatement($this->core, 'coreTriggerCommentsCount'); 
     269        $sql 
     270            ->columns(array('post_id', 'COUNT(post_id) AS nb_comment', 'comment_trackback')) 
     271            ->from($this->prefix . 'comment') 
     272            ->where(array( 
     273                'comment_status = 1', 
     274                'post_id' . $sql->in($affected_posts) 
     275            )) 
     276            ->group(array('post_id', 'comment_trackback')); 
     277 
     278        $rs = $this->con->select($sql->statement()); 
    270279 
    271280        $posts = array(); 
     
    461470    private function getCategoriesCounter($params = array()) 
    462471    { 
    463         $strReq = 
    464         'SELECT  C.cat_id, COUNT(P.post_id) AS nb_post ' . 
    465         'FROM ' . $this->prefix . 'category AS C ' . 
    466         'JOIN ' . $this->prefix . "post P ON (C.cat_id = P.cat_id AND P.blog_id = '" . $this->con->escape($this->id) . "' ) " . 
    467         "WHERE C.blog_id = '" . $this->con->escape($this->id) . "' "; 
     472        $sql = new dcSelectStatement($this->core, 'coreGetCategoriesCounter'); 
     473        $sql 
     474            ->columns(array('C.cat_id', 'COUNT(P.post_id) AS nb_post')) 
     475            ->from($this->prefix . 'category AS C') 
     476            ->join('JOIN ' . $this->prefix . 'post P ' . 
     477                'ON (C.cat_id = P.cat_id AND P.blog_id = ' . $sql->quote($this->id) . ')') 
     478            ->where('C.blog_id = ' . $sql->quote($this->id)); 
    468479 
    469480        if (!$this->core->auth->userID()) { 
    470             $strReq .= 'AND P.post_status = 1 '; 
     481            $sql->where('P.post_status = 1'); 
    471482        } 
    472483 
    473484        if (!empty($params['post_type'])) { 
    474             $strReq .= 'AND P.post_type ' . $this->con->in($params['post_type']); 
    475         } 
    476  
    477         $strReq .= 'GROUP BY C.cat_id '; 
    478  
    479         $rs       = $this->con->select($strReq); 
     485            $sql->where('P.post_type ' . $sql->in($params['post_type'])); 
     486        } 
     487 
     488        $sql->group('C.cat_id'); 
     489 
     490        $rs       = $this->con->select($sql->statement()); 
    480491        $counters = array(); 
    481492        while ($rs->fetch()) { 
     
    568579        $this->core->callBehavior('coreBeforeCategoryUpdate', $this, $cur); 
    569580 
    570         $cur->update( 
    571             'WHERE cat_id = ' . (integer) $id . ' ' . 
    572             "AND blog_id = '" . $this->con->escape($this->id) . "' "); 
     581        $sql = new dcUpdateStatement($this->core, 'coreCategoryUpdate'); 
     582        $sql 
     583            ->where(array( 
     584                'cat_id = ' . (integer) $id, 
     585                'blog_id = ' . $sql->quote($this->id) 
     586            )); 
     587        $cur->update($sql->whereStatement()); 
    573588 
    574589        # --BEHAVIOR-- coreAfterCategoryUpdate 
     
    639654        } 
    640655 
    641         $strReq = 'SELECT COUNT(post_id) AS nb_post ' . 
    642         'FROM ' . $this->prefix . 'post ' . 
    643         'WHERE cat_id = ' . (integer) $id . ' ' . 
    644         "AND blog_id = '" . $this->con->escape($this->id) . "' "; 
    645  
    646         $rs = $this->con->select($strReq); 
     656        $sql = new dcSelectStatement($this->core, 'coreCategoryPostCount'); 
     657        $sql 
     658            ->columns('COUNT(post_id) AS nb_post') 
     659            ->from($this->prefix . 'post') 
     660            ->where(array( 
     661                'cat_id = ' . (integer) $id, 
     662                'blog_id = ' . $sql->quote($this->id) 
     663            )); 
     664        $rs = $this->con->select($sql->statement()); 
    647665 
    648666        if ($rs->nb_post > 0) { 
     
    669687    private function checkCategory($title, $url, $id = null) 
    670688    { 
    671         # Let's check if URL is taken... 
    672         $strReq = 
    673         'SELECT cat_url FROM ' . $this->prefix . 'category ' . 
    674         "WHERE cat_url = '" . $this->con->escape($url) . "' " . 
    675         ($id ? 'AND cat_id <> ' . (integer) $id . ' ' : '') . 
    676         "AND blog_id = '" . $this->con->escape($this->id) . "' " . 
    677             'ORDER BY cat_url DESC'; 
    678  
    679         $rs = $this->con->select($strReq); 
     689        // Let's check if URL is taken... 
     690        $sql = new dcSelectStatement($this->core, 'coreCheckCategoryURL'); 
     691        $sql 
     692            ->columns('cat_url') 
     693            ->from($this->prefix . 'category') 
     694            ->where('blog_id = ' . $sql->quote($this->id)) 
     695            ->order('cat_url DESC'); 
     696        if ($id) { 
     697            $sql->where('AND cat_id <> ' . (integer) $i); 
     698        } 
     699        $sql->cond('AND cat_url = ' . $sql->quote($url)); 
     700 
     701        $rs = $this->con->select($sql->statement()); 
    680702 
    681703        if (!$rs->isEmpty()) { 
    682             if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli' || $this->con->driver() == 'mysqlimb4') { 
    683                 $clause = "REGEXP '^" . $this->con->escape($url) . "[0-9]+$'"; 
    684             } elseif ($this->con->driver() == 'pgsql') { 
    685                 $clause = "~ '^" . $this->con->escape($url) . "[0-9]+$'"; 
    686             } else { 
    687                 $clause = "LIKE '" . $this->con->escape($url) . "%'"; 
    688             } 
    689             $strReq = 
    690             'SELECT cat_url FROM ' . $this->prefix . 'category ' . 
    691             "WHERE cat_url " . $clause . ' ' . 
    692             ($id ? 'AND cat_id <> ' . (integer) $id . ' ' : '') . 
    693             "AND blog_id = '" . $this->con->escape($this->id) . "' " . 
    694                 'ORDER BY cat_url DESC '; 
    695  
    696             $rs = $this->con->select($strReq); 
     704            // Replace condition on cat_url 
     705            $sql->cond('AND cat_url ' . $sql->regexp($url), true); 
     706 
     707            $rs = $this->con->select($sql->statement()); 
    697708            $a  = array(); 
    698709            while ($rs->fetch()) { 
     
    791802        $this->core->callBehavior('coreBlogBeforeGetPosts', $params); 
    792803 
     804        $sql = new dcSelectStatement($this->core, 'coreGetPosts'); 
     805 
    793806        if ($count_only) { 
    794             $strReq = 'SELECT count(DISTINCT P.post_id) '; 
     807            $sql->columns('count(DISTINCT P.post_id)'); 
    795808        } elseif (!empty($params['sql_only'])) { 
    796             $strReq = 'SELECT P.post_id '; 
     809            $sql->columns('P.post_id'); 
    797810        } else { 
    798             if (!empty($params['no_content'])) { 
    799                 $content_req = ''; 
    800             } else { 
    801                 $content_req = 
    802                     'post_excerpt, post_excerpt_xhtml, ' . 
    803                     'post_content, post_content_xhtml, post_notes, '; 
     811            if (empty($params['no_content'])) { 
     812                $sql->columns(array('post_excerpt', 'post_excerpt_xhtml', 'post_content', 'post_content_xhtml', 'post_notes')); 
    804813            } 
    805814 
    806815            if (!empty($params['columns']) && is_array($params['columns'])) { 
    807                 $content_req .= implode(', ', $params['columns']) . ', '; 
    808             } 
    809  
    810             $strReq = 
    811                 'SELECT P.post_id, P.blog_id, P.user_id, P.cat_id, post_dt, ' . 
    812                 'post_tz, post_creadt, post_upddt, post_format, post_password, ' . 
    813                 'post_url, post_lang, post_title, ' . $content_req . 
    814                 'post_type, post_meta, ' . 
    815                 'post_status, post_firstpub, post_selected, post_position, ' . 
    816                 'post_open_comment, post_open_tb, nb_comment, nb_trackback, ' . 
    817                 'U.user_name, U.user_firstname, U.user_displayname, U.user_email, ' . 
    818                 'U.user_url, ' . 
    819                 'C.cat_title, C.cat_url, C.cat_desc '; 
    820         } 
    821  
    822         $strReq .= 
    823         'FROM ' . $this->prefix . 'post P ' . 
    824         'INNER JOIN ' . $this->prefix . 'user U ON U.user_id = P.user_id ' . 
    825         'LEFT OUTER JOIN ' . $this->prefix . 'category C ON P.cat_id = C.cat_id '; 
     816                $sql->columns($params['columns']); 
     817            } 
     818 
     819            $sql->columns(array('P.post_id', 'P.blog_id', 'P.user_id', 'P.cat_id', 'post_dt', 'post_tz', 'post_creadt', 
     820                'post_upddt', 'post_format', 'post_password', 'post_url', 'post_lang', 'post_title', 'post_type', 
     821                'post_meta', 'post_status', 'post_firstpub', 'post_selected', 'post_position', 'post_open_comment', 
     822                'post_open_tb', 'nb_comment', 'nb_trackback', 'U.user_name', 'U.user_firstname', 'U.user_displayname', 
     823                'U.user_email', 'U.user_url', 'C.cat_title', 'C.cat_url', 'C.cat_desc')); 
     824        } 
     825 
     826        $sql->from($this->prefix . 'post P') 
     827            ->join('INNER JOIN ' . $this->prefix . 'user U ON U.user_id = P.user_id') 
     828            ->join('LEFT OUTER JOIN ' . $this->prefix . 'category C ON P.cat_id = C.cat_id'); 
     829 
     830        if (!empty($params['join'])) { 
     831            $sql->join($params['join']); 
     832        } 
    826833 
    827834        if (!empty($params['from'])) { 
    828             $strReq .= $params['from'] . ' '; 
    829         } 
    830  
    831         $strReq .= 
    832         "WHERE P.blog_id = '" . $this->con->escape($this->id) . "' "; 
     835            $sql->from($params['from']); 
     836        } 
     837 
     838        $sql->where('P.blog_id = ' . $sql->quote($this->id)); 
    833839 
    834840        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    835             $strReq .= 'AND ((post_status = 1 '; 
    836  
     841            $cond = '((post_status = 1'; 
    837842            if ($this->without_password) { 
    838                 $strReq .= 'AND post_password IS NULL '; 
    839             } 
    840             $strReq .= ') '; 
    841  
     843                $cond .= ' AND post_password IS NULL'; 
     844            } 
     845            $cond .= ')'; 
    842846            if ($this->core->auth->userID()) { 
    843                 $strReq .= "OR P.user_id = '" . $this->con->escape($this->core->auth->userID()) . "')"; 
    844             } else { 
    845                 $strReq .= ') '; 
    846             } 
     847                $cond .= 'OR P.user_id = ' . $sql->quote($this->core->auth->userID()); 
     848            } 
     849            $cond .= ')'; 
     850            $sql->where($cond); 
    847851        } 
    848852 
     
    850854        if (isset($params['post_type'])) { 
    851855            if (is_array($params['post_type']) || $params['post_type'] != '') { 
    852                 $strReq .= 'AND post_type ' . $this->con->in($params['post_type']); 
     856                $sql->where('post_type ' . $sql->in($params['post_type'])); 
    853857            } 
    854858        } else { 
    855             $strReq .= "AND post_type = 'post' "; 
     859            $sql->where("post_type = 'post' "); 
    856860        } 
    857861 
     
    862866                $params['post_id'] = array((integer) $params['post_id']); 
    863867            } 
    864             $strReq .= 'AND P.post_id ' . $this->con->in($params['post_id']); 
     868            $sql->where('P.post_id ' . $sql->in($params['post_id'])); 
    865869        } 
    866870 
     
    871875                $params['exclude_post_id'] = array((integer) $params['exclude_post_id']); 
    872876            } 
    873             $strReq .= 'AND P.post_id NOT ' . $this->con->in($params['exclude_post_id']); 
     877            $sql->where('P.post_id NOT ' . $sql->in($params['exclude_post_id'])); 
    874878        } 
    875879 
    876880        if (isset($params['post_url']) && $params['post_url'] !== '') { 
    877             $strReq .= "AND post_url = '" . $this->con->escape($params['post_url']) . "' "; 
     881            $sql->where('post_url = ' . $sql->quote($params['post_url'])); 
    878882        } 
    879883 
    880884        if (!empty($params['user_id'])) { 
    881             $strReq .= "AND U.user_id = '" . $this->con->escape($params['user_id']) . "' "; 
     885            $sql->where('U.user_id = ' . $sql->quote($params['user_id'])); 
    882886        } 
    883887 
     
    889893                array_walk($params['cat_id'], function (&$v, $k) {$v = $v . " ?not";}); 
    890894            } 
    891             $strReq .= 'AND ' . $this->getPostsCategoryFilter($params['cat_id'], 'cat_id') . ' '; 
     895            $sql->where($this->getPostsCategoryFilter($params['cat_id'], 'cat_id')); 
    892896        } elseif (isset($params['cat_url']) && $params['cat_url'] !== '') { 
    893897            if (!is_array($params['cat_url'])) { 
     
    897901                array_walk($params['cat_url'], function (&$v, $k) {$v = $v . " ?not";}); 
    898902            } 
    899             $strReq .= 'AND ' . $this->getPostsCategoryFilter($params['cat_url'], 'cat_url') . ' '; 
     903            $sql->where($this->getPostsCategoryFilter($params['cat_url'], 'cat_url')); 
    900904        } 
    901905 
    902906        /* Other filters */ 
    903907        if (isset($params['post_status'])) { 
    904             $strReq .= 'AND post_status = ' . (integer) $params['post_status'] . ' '; 
     908            $sql->where('post_status = ' . (integer) $params['post_status']); 
    905909        } 
    906910 
    907911        if (isset($params['post_firstpub'])) { 
    908             $strReq .= 'AND post_firstpub = ' . (integer) $params['post_firstpub'] . ' '; 
     912            $sql->where('post_firstpub = ' . (integer) $params['post_firstpub']); 
    909913        } 
    910914 
    911915        if (isset($params['post_selected'])) { 
    912             $strReq .= 'AND post_selected = ' . (integer) $params['post_selected'] . ' '; 
     916            $sql->where('post_selected = ' . (integer) $params['post_selected']); 
    913917        } 
    914918 
    915919        if (!empty($params['post_year'])) { 
    916             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%Y') . ' = ' . 
    917             "'" . sprintf('%04d', $params['post_year']) . "' "; 
     920            $sql->where($sql->dateFormat('post_dt', '%Y') . ' = ' . "'" . sprintf('%04d', $params['post_year']) . "'"); 
    918921        } 
    919922 
    920923        if (!empty($params['post_month'])) { 
    921             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%m') . ' = ' . 
    922             "'" . sprintf('%02d', $params['post_month']) . "' "; 
     924            $sql->where($sql->dateFormat('post_dt', '%m') . ' = ' . "'" . sprintf('%02d', $params['post_month']) . "'"); 
    923925        } 
    924926 
    925927        if (!empty($params['post_day'])) { 
    926             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%d') . ' = ' . 
    927             "'" . sprintf('%02d', $params['post_day']) . "' "; 
     928            $sql->where($sql->dateFormat('post_dt', '%d') . ' = ' . "'" . sprintf('%02d', $params['post_day']) . "'"); 
    928929        } 
    929930 
    930931        if (!empty($params['post_lang'])) { 
    931             $strReq .= "AND P.post_lang = '" . $this->con->escape($params['post_lang']) . "' "; 
     932            $sql->where('P.post_lang = ' . $sql->quote($params['post_lang'])); 
    932933        } 
    933934 
     
    943944                if ($words) { 
    944945                    foreach ($words as $i => $w) { 
    945                         $words[$i] = "post_words LIKE '%" . $this->con->escape($w) . "%'"; 
     946                        $words[$i] = "post_words LIKE '%" . $sql->escape($w) . "%'"; 
    946947                    } 
    947                     $strReq .= 'AND ' . implode(' AND ', $words) . ' '; 
     948                    $sql->where(implode(' AND ', $words)); 
    948949                } 
    949950            } 
     
    951952 
    952953        if (isset($params['media'])) { 
    953             if ($params['media'] == '0') { 
    954                 $strReq .= 'AND NOT '; 
    955             } else { 
    956                 $strReq .= 'AND '; 
    957             } 
    958             $strReq .= 'EXISTS (SELECT M.post_id FROM ' . $this->prefix . 'post_media M ' . 
    959                 'WHERE M.post_id = P.post_id '; 
     954            $sqlEx = new dcSelectStatement($this->core, 'coreGetPostsMedia'); 
     955            $sqlEx 
     956                ->columns('M.post_id') 
     957                ->from($this->prefix . 'post_media M') 
     958                ->where('M.post_id = P.post_id'); 
    960959            if (isset($params['link_type'])) { 
    961                 $strReq .= " AND M.link_type " . $this->con->in($params['link_type']) . " "; 
    962             } 
    963             $strReq .= ")"; 
     960                $sqlEx->where('M.link_type ' . $sql->in($params['link_type'])); 
     961            } 
     962            $sql->where(($params['media'] == '0' ? 'NOT ' : '') . 'EXISTS (' . $sqlEx->statement() . ')'); 
    964963        } 
    965964 
    966965        if (!empty($params['where'])) { 
    967             $strReq .= $params['where'] . ' '; 
     966            $sql->cond($params['where']); 
    968967        } 
    969968 
    970969        if (!empty($params['sql'])) { 
    971             $strReq .= $params['sql'] . ' '; 
     970            $sql->sql($params['sql']); 
    972971        } 
    973972 
    974973        if (!$count_only) { 
    975974            if (!empty($params['order'])) { 
    976                 $strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' '; 
     975                $sql->order($sql->escape($params['order'])); 
    977976            } else { 
    978                 $strReq .= 'ORDER BY post_dt DESC '; 
    979             } 
    980         } 
    981  
    982         if (!$count_only && !empty($params['limit'])) { 
    983             $strReq .= $this->con->limit($params['limit']); 
    984         } 
     977                $sql->order('post_dt DESC'); 
     978            } 
     979            if (!empty($params['limit'])) { 
     980                $sql->limit($params['limit']); 
     981            } 
     982        } 
     983 
     984        $query = $sql->statement(); 
    985985 
    986986        if (!empty($params['sql_only'])) { 
    987             return $strReq; 
    988         } 
    989  
    990         $rs            = $this->con->select($strReq); 
     987            return $query; 
     988        } 
     989 
     990        $rs            = $this->con->select($query); 
    991991        $rs->core      = $this->core; 
    992992        $rs->_nb_media = array(); 
     
    10701070    public function getLangs($params = array()) 
    10711071    { 
    1072         $strReq = 'SELECT COUNT(post_id) as nb_post, post_lang ' . 
    1073         'FROM ' . $this->prefix . 'post ' . 
    1074         "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1075             "AND post_lang <> '' " . 
    1076             "AND post_lang IS NOT NULL "; 
     1072        $sql = new dcSelectStatement($this->core, 'coreGetLangs'); 
     1073        $sql 
     1074            ->columns(array('COUNT(post_id) as nb_post', 'post_lang')) 
     1075            ->from($this->prefix . 'post') 
     1076            ->where(array( 
     1077                'blog_id = ' . $sql->quote($this->id), 
     1078                "post_lang <> ''", 
     1079                'post_lang IS NOT NULL' 
     1080            )); 
    10771081 
    10781082        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1079             $strReq .= 'AND ((post_status = 1 '; 
     1083            $cond = '((post_status = 1'; 
    10801084 
    10811085            if ($this->without_password) { 
    1082                 $strReq .= 'AND post_password IS NULL '; 
    1083             } 
    1084             $strReq .= ') '; 
     1086                $cond .= ' AND post_password IS NULL'; 
     1087            } 
     1088            $cond .= ')'; 
    10851089 
    10861090            if ($this->core->auth->userID()) { 
    1087                 $strReq .= "OR user_id = '" . $this->con->escape($this->core->auth->userID()) . "')"; 
    1088             } else { 
    1089                 $strReq .= ') '; 
    1090             } 
     1091                $cond .= " OR user_id = " . $sql->quote($this->core->auth->userID()); 
     1092            } 
     1093            $cond .= ')'; 
     1094            $sql->where($cond); 
    10911095        } 
    10921096 
    10931097        if (isset($params['post_type'])) { 
    10941098            if ($params['post_type'] != '') { 
    1095                 $strReq .= "AND post_type = '" . $this->con->escape($params['post_type']) . "' "; 
     1099                $sql->where("post_type = " . $sql->quote($params['post_type'])); 
    10961100            } 
    10971101        } else { 
    1098             $strReq .= "AND post_type = 'post' "; 
     1102            $sql->where("post_type = 'post'"); 
    10991103        } 
    11001104 
    11011105        if (isset($params['lang'])) { 
    1102             $strReq .= "AND post_lang = '" . $this->con->escape($params['lang']) . "' "; 
    1103         } 
    1104  
    1105         $strReq .= 'GROUP BY post_lang '; 
     1106            $sql->where("post_lang = " . $sql->quote($params['lang'])); 
     1107        } 
     1108 
     1109        $sql->group('post_lang'); 
    11061110 
    11071111        $order = 'desc'; 
     
    11091113            $order = $params['order']; 
    11101114        } 
    1111         $strReq .= 'ORDER BY post_lang ' . $order . ' '; 
    1112  
    1113         return $this->con->select($strReq); 
     1115        $sql->order('post_lang ' . $order); 
     1116 
     1117        return $this->con->select($sql->statement()); 
    11141118    } 
    11151119 
     
    11341138    public function getDates($params = array()) 
    11351139    { 
     1140        $sql = new dcSelectStatement($this->core, 'coreGetDates'); 
     1141 
    11361142        $dt_f  = '%Y-%m-%d'; 
    11371143        $dt_fc = '%Y%m%d'; 
     
    11481154        $dt_fc .= '000000'; 
    11491155 
    1150         $cat_field = $catReq = $limit = ''; 
     1156        $sql 
     1157            ->distinct() 
     1158            ->columns(array( 
     1159                $sql->dateFormat('post_dt', $dt_f) . ' AS dt', 
     1160                'COUNT(P.post_id) AS nb_post' 
     1161            )) 
     1162            ->from($this->prefix . 'post P') 
     1163            ->join('LEFT JOIN ' . $this->prefix . 'category C ON P.cat_id = C.cat_id') 
     1164            ->group('dt'); 
    11511165 
    11521166        if (isset($params['cat_id']) && $params['cat_id'] !== '') { 
    1153             $catReq    = 'AND P.cat_id = ' . (integer) $params['cat_id'] . ' '; 
    1154             $cat_field = ', C.cat_url '; 
     1167            $sql 
     1168                ->columns('C.cat_url') 
     1169                ->where('P.cat_id = ' . (integer) $params['cat_id']) 
     1170                ->group('C.car_url'); 
    11551171        } elseif (isset($params['cat_url']) && $params['cat_url'] !== '') { 
    1156             $catReq    = "AND C.cat_url = '" . $this->con->escape($params['cat_url']) . "' "; 
    1157             $cat_field = ', C.cat_url '; 
     1172            $sql 
     1173                ->columns('C.cat_url') 
     1174                ->where('C.cat_url = ' . $sql->quote($params['cat_url'])) 
     1175                ->group('C.car_url'); 
    11581176        } 
    11591177        if (!empty($params['post_lang'])) { 
    1160             $catReq = 'AND P.post_lang = \'' . $params['post_lang'] . '\' '; 
    1161         } 
    1162  
    1163         $strReq = 'SELECT DISTINCT(' . $this->con->dateFormat('post_dt', $dt_f) . ') AS dt ' . 
    1164         $cat_field . 
    1165         ',COUNT(P.post_id) AS nb_post ' . 
    1166         'FROM ' . $this->prefix . 'post P LEFT JOIN ' . $this->prefix . 'category C ' . 
    1167         'ON P.cat_id = C.cat_id ' . 
    1168         "WHERE P.blog_id = '" . $this->con->escape($this->id) . "' " . 
    1169             $catReq; 
     1178            $sql->where('P.post_lang = ' . $sql->quote($params['post_lang'])); 
     1179        } 
    11701180 
    11711181        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1172             $strReq .= 'AND ((post_status = 1 '; 
    1173  
     1182            $cond = '((post_status = 1'; 
    11741183            if ($this->without_password) { 
    1175                 $strReq .= 'AND post_password IS NULL '; 
    1176             } 
    1177             $strReq .= ') '; 
     1184                $cond .= ' AND post_password IS NULL'; 
     1185            } 
     1186            $cond .= ')'; 
    11781187 
    11791188            if ($this->core->auth->userID()) { 
    1180                 $strReq .= "OR P.user_id = '" . $this->con->escape($this->core->auth->userID()) . "')"; 
    1181             } else { 
    1182                 $strReq .= ') '; 
    1183             } 
     1189                $cond .= ' OR P.user_id = ' . $sql->quote($this->core->auth->userID()); 
     1190            } 
     1191            $cond .= ')'; 
     1192            $sql->where($cond); 
    11841193        } 
    11851194 
    11861195        if (!empty($params['post_type'])) { 
    1187             $strReq .= "AND post_type " . $this->con->in($params['post_type']) . " "; 
     1196            $sql->where("post_type " . $sql->in($params['post_type'])); 
    11881197        } else { 
    1189             $strReq .= "AND post_type = 'post' "; 
     1198            $sql->where("post_type = 'post'"); 
    11901199        } 
    11911200 
    11921201        if (!empty($params['year'])) { 
    1193             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%Y') . " = '" . sprintf('%04d', $params['year']) . "' "; 
     1202            $sql->where($sql->dateFormat('post_dt', '%Y') . " = '" . sprintf('%04d', $params['year']) . "'"); 
    11941203        } 
    11951204 
    11961205        if (!empty($params['month'])) { 
    1197             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%m') . " = '" . sprintf('%02d', $params['month']) . "' "; 
     1206            $sql->where($sql->dateFormat('post_dt', '%m') . " = '" . sprintf('%02d', $params['month']) . "'"); 
    11981207        } 
    11991208 
    12001209        if (!empty($params['day'])) { 
    1201             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%d') . " = '" . sprintf('%02d', $params['day']) . "' "; 
     1210            $sql->where($sql->dateFormat('post_dt', '%d') . " = '" . sprintf('%02d', $params['day']) . "'"); 
    12021211        } 
    12031212 
     
    12161225            $dt = date('YmdHis', strtotime($dt)); 
    12171226 
    1218             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', $dt_fc) . $pdir . "'" . $dt . "' "; 
    1219             $limit = $this->con->limit(1); 
    1220         } 
    1221  
    1222         $strReq .= 'GROUP BY dt ' . $cat_field; 
     1227            $sql 
     1228                ->where($sql->dateFormat('post_dt', $dt_fc) . $pdir . "'" . $dt . "'") 
     1229                ->limit(1); 
     1230        } 
    12231231 
    12241232        $order = 'desc'; 
     
    12261234            $order = $params['order']; 
    12271235        } 
    1228  
    1229         $strReq .= 
    1230             'ORDER BY dt ' . $order . ' ' . 
    1231             $limit; 
    1232  
    1233         $rs = $this->con->select($strReq); 
     1236        $sql->order('dt ' . $order); 
     1237 
     1238        $rs = $this->con->select($sql->statement()); 
    12341239        $rs->extend('rsExtDates'); 
    12351240        return $rs; 
     
    12531258        { 
    12541259            # Get ID 
    1255             $rs = $this->con->select( 
    1256                 'SELECT MAX(post_id) ' . 
    1257                 'FROM ' . $this->prefix . 'post ' 
    1258             ); 
     1260            $sql = new dcSelectStatement($this->core, 'corePostCreateGetID'); 
     1261            $sql 
     1262                ->columns('MAX(post_id)') 
     1263                ->from($this->prefix . 'post'); 
     1264 
     1265            $rs = $this->con->select($sql->statement()); 
    12591266 
    12601267            $cur->post_id     = (integer) $rs->f(0) + 1; 
     
    13281335        $cur->post_upddt = date('Y-m-d H:i:s'); 
    13291336 
    1330         #If user is only "usage", we need to check the post's owner 
     1337        // If user is only "usage", we need to check the post's owner 
    13311338        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1332             $strReq = 'SELECT post_id ' . 
    1333             'FROM ' . $this->prefix . 'post ' . 
    1334             'WHERE post_id = ' . $id . ' ' . 
    1335             "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
    1336  
    1337             $rs = $this->con->select($strReq); 
     1339            $sql = new dcSelectStatement($this->core, 'corePostUpdateCheckOwner'); 
     1340            $sql 
     1341                ->column('post_id') 
     1342                ->from($this->prefix . 'post') 
     1343                ->where(array( 
     1344                    'post_id = ' . $id, 
     1345                    'user_id = ' . $sql->quote($this->core->auth->userID()) 
     1346                )); 
     1347 
     1348            $rs = $this->con->select($sql->statement()); 
     1349            unset($sql); 
    13381350 
    13391351            if ($rs->isEmpty()) { 
     
    13451357        $this->core->callBehavior('coreBeforePostUpdate', $this, $cur); 
    13461358 
    1347         $cur->update('WHERE post_id = ' . $id . ' '); 
     1359        $sql = new dcUpdateStatement($this->core, 'corePostUpdate'); 
     1360        $sql->where('post_id = ' . $id); 
     1361 
     1362        $cur->update($sql->whereStatement()); 
    13481363 
    13491364        # --BEHAVIOR-- coreAfterPostUpdate 
     
    13811396        $status    = (integer) $status; 
    13821397 
    1383         $strReq = "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1384         "AND post_id " . $this->con->in($posts_ids); 
     1398        $sql = new dcUpdateStatement($this->core, 'coreUpdPostsStatus'); 
     1399        $sql 
     1400            ->where(array( 
     1401                'blog_id = ' . $sql->quote($this->id), 
     1402                'post_id ' . $sql->in($posts_ids) 
     1403            )); 
    13851404 
    13861405        #If user can only publish, we need to check the post's owner 
    13871406        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1388             $strReq .= "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
     1407            $sql->where('user_id = ' . $sql->quote($this->core->auth->userID())); 
    13891408        } 
    13901409 
     
    13941413        $cur->post_upddt  = date('Y-m-d H:i:s'); 
    13951414 
    1396         $cur->update($strReq); 
     1415        $cur->update($sql->whereStatement()); 
    13971416        $this->triggerBlog(); 
    13981417 
     
    14261445        $selected  = (boolean) $selected; 
    14271446 
    1428         $strReq = "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1429         "AND post_id " . $this->con->in($posts_ids); 
     1447        $sql = new dcUpdateStatement($this->core, 'coreUpdPostsSelected'); 
     1448        $sql 
     1449            ->where(array( 
     1450                'blog_id = ' . $sql->quote($this->id), 
     1451                'post_id ' . $sql->in($posts_ids) 
     1452            )); 
    14301453 
    14311454        # If user is only usage, we need to check the post's owner 
    14321455        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1433             $strReq .= "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
     1456            $sql->where('user_id = ' . $sql->quote($this->core->auth->userID())); 
    14341457        } 
    14351458 
     
    14391462        $cur->post_upddt    = date('Y-m-d H:i:s'); 
    14401463 
    1441         $cur->update($strReq); 
     1464        $cur->update($sql->whereStatement()); 
    14421465        $this->triggerBlog(); 
    14431466    } 
     
    14691492        $cat_id    = (integer) $cat_id; 
    14701493 
    1471         $strReq = "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1472         "AND post_id " . $this->con->in($posts_ids); 
    1473  
    1474         # If user is only usage, we need to check the post's owner 
     1494        $sql = new dcUpdateStatement($this->core, 'coreUpdPostsCategory'); 
     1495        $sql 
     1496            ->where(array( 
     1497                'blog_id = ' . $sql->quote($this->id), 
     1498                'post_id ' . $sql->in($posts_ids) 
     1499            )); 
     1500 
     1501        // If user is only usage, we need to check the post's owner 
    14751502        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1476             $strReq .= "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
     1503            $sql->where('user_id = ' . $sql->quote($this->core->auth->userID())); 
    14771504        } 
    14781505 
     
    14821509        $cur->post_upddt = date('Y-m-d H:i:s'); 
    14831510 
    1484         $cur->update($strReq); 
     1511        $cur->update($sql->whereStatement()); 
    14851512        $this->triggerBlog(); 
    14861513    } 
     
    15061533        $cur->post_upddt = date('Y-m-d H:i:s'); 
    15071534 
    1508         $cur->update( 
    1509             'WHERE cat_id = ' . $old_cat_id . ' ' . 
    1510             "AND blog_id = '" . $this->con->escape($this->id) . "' " 
    1511         ); 
     1535        $sql = new dcUpdateStatement($this->core, 'coreChangePostsCategory'); 
     1536        $sql 
     1537            ->where(array( 
     1538                'cat_id = ' . $old_cat_id, 
     1539                'blog_id = ' . $sql->quote($this->id) 
     1540            )); 
     1541        $cur->update($sql->whereStatement()); 
    15121542        $this->triggerBlog(); 
    15131543    } 
     
    15401570        } 
    15411571 
    1542         $strReq = 'DELETE FROM ' . $this->prefix . 'post ' . 
    1543         "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1544         "AND post_id " . $this->con->in($posts_ids); 
    1545  
    1546         #If user can only delete, we need to check the post's owner 
     1572        $sql = new dcDeleteStatement($this->core, 'coreDelPosts'); 
     1573        $sql 
     1574            ->from($this->prefix . 'post') 
     1575            ->where(array( 
     1576                'blog_id = ' . $sql->quote($this->id), 
     1577                'post_id ' . $sql->in($posts_ids) 
     1578            )); 
     1579 
     1580        // If user can only delete, we need to check the post's owner 
    15471581        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1548             $strReq .= "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
    1549         } 
    1550  
    1551         $this->con->execute($strReq); 
     1582            $sql->where('user_id = ' . $sql->quote($this->core->auth->userID())); 
     1583        } 
     1584 
     1585        $this->con->execute($sql->statement()); 
    15521586        $this->triggerBlog(); 
    15531587    } 
     
    15581592    public function publishScheduledEntries() 
    15591593    { 
    1560         $strReq = 'SELECT post_id, post_dt, post_tz ' . 
    1561         'FROM ' . $this->prefix . 'post ' . 
    1562         'WHERE post_status = -1 ' . 
    1563         "AND blog_id = '" . $this->con->escape($this->id) . "' "; 
    1564  
    1565         $rs = $this->con->select($strReq); 
     1594        $sql = new dcSelectStatement($this->core, 'coreScheduledEntriesPublish'); 
     1595        $sql 
     1596            ->columns(array('post_id', 'post_dt', 'post_tz')) 
     1597            ->from($this->prefix . 'post') 
     1598            ->where(array( 
     1599                'post_status = -1', 
     1600                'blog_id = ' . $sql->quote($this->id) 
     1601            )); 
     1602 
     1603        $rs = $this->con->select($sql->statement()); 
     1604        unset($sql); 
    15661605 
    15671606        $now       = dt::toUTC(time()); 
     
    15881627            $this->core->callBehavior('coreBeforeScheduledEntriesPublish', $this, $to_change); 
    15891628 
    1590             $strReq = 
    1591             'UPDATE ' . $this->prefix . 'post SET ' . 
    1592             'post_status = 1 ' . 
    1593             "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1594             'AND post_id ' . $this->con->in((array) $to_change) . ' '; 
    1595             $this->con->execute($strReq); 
     1629            $sql = new dcUpdateStatement($this->core, 'coreScheduledEntriesPublish'); 
     1630            $sql 
     1631                ->reference($this->prefix . 'post') 
     1632                ->set('post_status = 1') 
     1633                ->where(array( 
     1634                    'blog_id = ' . $sql->quote($this->id), 
     1635                    'post_id ' . $sql->in((array) $to_change) 
     1636                )); 
     1637 
     1638            $this->con->execute($sql->statement()); 
    15961639            $this->triggerBlog(); 
    15971640 
     
    16241667        if (count($to_change)) { 
    16251668 
    1626             $strReq = 
    1627             'UPDATE ' . $this->prefix . 'post ' . 
    1628             'SET post_firstpub = 1 ' . 
    1629             "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1630             'AND post_id ' . $this->con->in((array) $to_change) . ' '; 
    1631             $this->con->execute($strReq); 
     1669            $sql = new dcUpdateStatement($this->core, 'coreFirstPublicationEntries'); 
     1670            $sql 
     1671                ->reference($this->prefix . 'post') 
     1672                ->set('post_firstpub = 1') 
     1673                ->where(array( 
     1674                    'blog_id = ' . $sql->quote($this->id), 
     1675                    'post_id ' . $sql->in((array) $to_change) 
     1676                )); 
     1677 
     1678            $this->con->execute($sql->statement()); 
    16321679 
    16331680            # --BEHAVIOR-- coreFirstPublicationEntries 
     
    16441691    public function getPostsUsers($post_type = 'post') 
    16451692    { 
    1646         $strReq = 'SELECT P.user_id, user_name, user_firstname, ' . 
    1647         'user_displayname, user_email ' . 
    1648         'FROM ' . $this->prefix . 'post P, ' . $this->prefix . 'user U ' . 
    1649         'WHERE P.user_id = U.user_id ' . 
    1650         "AND blog_id = '" . $this->con->escape($this->id) . "' "; 
     1693        $sql = new dcSelectStatement($this->core, 'coreGetPostsUsers'); 
     1694        $sql 
     1695            ->columns(array('P.user_id', 'user_name', 'user_firstname', 'user_displayname', 'user_email')) 
     1696            ->from(array($this->prefix . 'post P', $this->prefix . 'user U')) 
     1697            ->where(array( 
     1698                'P.user_id = U.user_id', 
     1699                'blog_id = ' . $sql->quote($this->id) 
     1700            )); 
    16511701 
    16521702        if ($post_type) { 
    1653             $strReq .= "AND post_type = '" . $this->con->escape($post_type) . "' "; 
    1654         } 
    1655  
    1656         $strReq .= 'GROUP BY P.user_id, user_name, user_firstname, user_displayname, user_email '; 
    1657  
    1658         return $this->con->select($strReq); 
     1703            $sql->where("post_type = '" . $sql->escape($post_type) . "'"); 
     1704        } 
     1705 
     1706        $sql->group(array('P.user_id', 'user_name', 'user_firstname', 'user_displayname', 'user_email')); 
     1707 
     1708        return $this->con->select($sql->statement()); 
    16591709    } 
    16601710 
     
    16871737 
    16881738        if (!empty($sub)) { 
    1689             $rs = $this->con->select( 
    1690                 'SELECT cat_id, cat_url, cat_lft, cat_rgt FROM ' . $this->prefix . 'category ' . 
    1691                 "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1692                 'AND ' . $field . ' ' . $this->con->in(array_keys($sub)) 
    1693             ); 
     1739            $sql = new dcSelectStatement($this->core); 
     1740            $sql 
     1741                ->columns(array('cat_id', 'cat_url', 'cat_lft', 'cat_rgt')) 
     1742                ->from($this->prefix . 'category') 
     1743                ->where(array( 
     1744                    'blog_id = ' . $sql->quote($this->id), 
     1745                    $field . ' ' . $sql->in(array_keys($sub)) 
     1746                )); 
     1747            $rs = $this->con->select($sql->statement()); 
    16941748 
    16951749            while ($rs->fetch()) { 
    16961750                $queries[$rs->f($field)] = '(C.cat_lft BETWEEN ' . $rs->cat_lft . ' AND ' . $rs->cat_rgt . ')'; 
    16971751            } 
     1752            unset($sql); 
    16981753        } 
    16991754 
     
    18811936 
    18821937        # Let's check if URL is taken... 
    1883         $strReq = 'SELECT post_url FROM ' . $this->prefix . 'post ' . 
    1884         "WHERE post_url = '" . $this->con->escape($url) . "' " . 
    1885         'AND post_id <> ' . (integer) $post_id . ' ' . 
    1886         "AND blog_id = '" . $this->con->escape($this->id) . "' " . 
    1887             'ORDER BY post_url DESC'; 
    1888  
    1889         $rs = $this->con->select($strReq); 
     1938        $sql = new dcSelectStatement($this->core, 'coreGetPostURL'); 
     1939        $sql 
     1940            ->columns('post_url') 
     1941            ->from($this->prefix . 'post') 
     1942            ->where(array( 
     1943                'post_id <> ' . (integer) $post_id, 
     1944                'blog_id = ' . $sql->quote($this->id) 
     1945            )) 
     1946            ->cond('AND post_url = ' . $sql->quote($url)) 
     1947            ->order('post_url DESC'); 
     1948 
     1949        $rs = $this->con->select($sql->statement()); 
    18901950 
    18911951        if (!$rs->isEmpty()) { 
    1892             if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli' || $this->con->driver() == 'mysqlimb4') { 
    1893                 $clause = "REGEXP '^" . $this->con->escape(preg_quote($url)) . "[0-9]+$'"; 
    1894             } elseif ($this->con->driver() == 'pgsql') { 
    1895                 $clause = "~ '^" . $this->con->escape(preg_quote($url)) . "[0-9]+$'"; 
    1896             } else { 
    1897                 $clause = "LIKE '" . 
    1898                 $this->con->escape(preg_replace(array('%', '_', '!'), array('!%', '!_', '!!'), $url)) . 
    1899                     "%' ESCAPE '!'"; 
    1900             } 
    1901             $strReq = 'SELECT post_url FROM ' . $this->prefix . 'post ' . 
    1902             "WHERE post_url " . $clause . ' ' . 
    1903             'AND post_id <> ' . (integer) $post_id . ' ' . 
    1904             "AND blog_id = '" . $this->con->escape($this->id) . "' " . 
    1905                 'ORDER BY post_url DESC '; 
    1906  
    1907             $rs = $this->con->select($strReq); 
     1952            // Replace condition on post_url 
     1953            $sql->cond('AND post_url ' . $sql->regexp($url), true); 
     1954 
     1955            $rs = $this->con->select($sql->statement()); 
    19081956            $a  = array(); 
    19091957            while ($rs->fetch()) { 
     
    19632011    public function getComments($params = array(), $count_only = false) 
    19642012    { 
     2013        $sql = new dcSelectStatement($this->core, 'coreGetComments'); 
     2014 
    19652015        if ($count_only) { 
    1966             $strReq = 'SELECT count(comment_id) '; 
     2016            $sql->columns('count(comment_id)'); 
    19672017        } elseif (!empty($params['sql_only'])) { 
    1968             $strReq = 'SELECT P.post_id '; 
     2018            $sql->columns('P.post_id'); 
    19692019        } else { 
    1970             if (!empty($params['no_content'])) { 
    1971                 $content_req = ''; 
    1972             } else { 
    1973                 $content_req = 'comment_content, '; 
     2020            if (empty($params['no_content'])) { 
     2021                $sql->columns('comment_content'); 
    19742022            } 
    19752023 
    19762024            if (!empty($params['columns']) && is_array($params['columns'])) { 
    1977                 $content_req .= implode(', ', $params['columns']) . ', '; 
    1978             } 
    1979  
    1980             $strReq = 
    1981                 'SELECT C.comment_id, comment_dt, comment_tz, comment_upddt, ' . 
    1982                 'comment_author, comment_email, comment_site, ' . 
    1983                 $content_req . ' comment_trackback, comment_status, ' . 
    1984                 'comment_spam_status, comment_spam_filter, comment_ip, ' . 
    1985                 'P.post_title, P.post_url, P.post_id, P.post_password, P.post_type, ' . 
    1986                 'P.post_dt, P.user_id, U.user_email, U.user_url '; 
    1987         } 
    1988  
    1989         $strReq .= 
    1990         'FROM ' . $this->prefix . 'comment C ' . 
    1991         'INNER JOIN ' . $this->prefix . 'post P ON C.post_id = P.post_id ' . 
    1992         'INNER JOIN ' . $this->prefix . 'user U ON P.user_id = U.user_id '; 
     2025                $sql->columns($params['columns']); 
     2026            } 
     2027 
     2028            $sql->columns(array('C.comment_id', 'comment_dt', 'comment_tz', 'comment_upddt', 'comment_author', 'comment_email', 'comment_site', 'comment_trackback', 'comment_status', 'comment_spam_status', 'comment_spam_filter', 'comment_ip', 'P.post_title', 'P.post_url', 'P.post_id', 'P.post_password', 'P.post_type', 'P.post_dt', 'P.user_id', 'U.user_email', 'U.user_url')); 
     2029        } 
     2030 
     2031        $sql 
     2032            ->from($this->prefix . 'comment C') 
     2033            ->join(array( 
     2034                'INNER JOIN ' . $this->prefix . 'post P ON C.post_id = P.post_id', 
     2035                'INNER JOIN ' . $this->prefix . 'user U ON P.user_id = U.user_id ' 
     2036            )); 
    19932037 
    19942038        if (!empty($params['from'])) { 
    1995             $strReq .= $params['from'] . ' '; 
    1996         } 
    1997  
    1998         $strReq .= 
    1999         "WHERE P.blog_id = '" . $this->con->escape($this->id) . "' "; 
     2039            $sql->from($params['from']); 
     2040        } 
     2041 
     2042        $sql->where('P.blog_id = ' . $sql->quote($this->id)); 
    20002043 
    20012044        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    2002             $strReq .= 'AND ((comment_status = 1 AND P.post_status = 1 '; 
     2045            $cond = '((comment_status = 1 AND P.post_status = 1'; 
    20032046 
    20042047            if ($this->without_password) { 
    2005                 $strReq .= 'AND post_password IS NULL '; 
    2006             } 
    2007             $strReq .= ') '; 
     2048                $cond .= ' AND post_password IS NULL'; 
     2049            } 
     2050            $cond .= ')'; 
    20082051 
    20092052            if ($this->core->auth->userID()) { 
    2010                 $strReq .= "OR P.user_id = '" . $this->con->escape($this->core->auth->userID()) . "')"; 
    2011             } else { 
    2012                 $strReq .= ') '; 
    2013             } 
     2053                $cond .= ' OR P.user_id = ' . $sql->quote($this->core->auth->userID()); 
     2054            } 
     2055            $cond .= ')'; 
     2056            $sql->where($cond); 
    20142057        } 
    20152058 
    20162059        if (!empty($params['post_type'])) { 
    2017             $strReq .= 'AND post_type ' . $this->con->in($params['post_type']); 
     2060            $sql->where('post_type ' . $sql->in($params['post_type'])); 
    20182061        } 
    20192062 
    20202063        if (isset($params['post_id']) && $params['post_id'] !== '') { 
    2021             $strReq .= 'AND P.post_id = ' . (integer) $params['post_id'] . ' '; 
     2064            $sql->where('P.post_id = ' . (integer) $params['post_id']); 
    20222065        } 
    20232066 
    20242067        if (isset($params['cat_id']) && $params['cat_id'] !== '') { 
    2025             $strReq .= 'AND P.cat_id = ' . (integer) $params['cat_id'] . ' '; 
     2068            $sql->where('P.cat_id = ' . (integer) $params['cat_id']); 
    20262069        } 
    20272070 
     
    20322075                $params['comment_id'] = array((integer) $params['comment_id']); 
    20332076            } 
    2034             $strReq .= 'AND comment_id ' . $this->con->in($params['comment_id']); 
     2077            $sql->where('comment_id ' . $sql->in($params['comment_id'])); 
    20352078        } 
    20362079 
    20372080        if (isset($params['comment_email'])) { 
    2038             $comment_email = $this->con->escape(str_replace('*', '%', $params['comment_email'])); 
    2039             $strReq .= "AND comment_email LIKE '" . $comment_email . "' "; 
     2081            $comment_email = $sql->quote(str_replace('*', '%', $params['comment_email'])); 
     2082            $sql->where('comment_email LIKE ' . $comment_email); 
    20402083        } 
    20412084 
    20422085        if (isset($params['comment_site'])) { 
    2043             $comment_site = $this->con->escape(str_replace('*', '%', $params['comment_site'])); 
    2044             $strReq .= "AND comment_site LIKE '" . $comment_site . "' "; 
     2086            $comment_site = $sql->quote(str_replace('*', '%', $params['comment_site'])); 
     2087            $sql->where('comment_site LIKE ' . $comment_site); 
    20452088        } 
    20462089 
    20472090        if (isset($params['comment_status'])) { 
    2048             $strReq .= 'AND comment_status = ' . (integer) $params['comment_status'] . ' '; 
     2091            $sql->where('comment_status = ' . (integer) $params['comment_status']); 
    20492092        } 
    20502093 
    20512094        if (!empty($params['comment_status_not'])) { 
    2052             $strReq .= 'AND comment_status <> ' . (integer) $params['comment_status_not'] . ' '; 
     2095            $sql->where('comment_status <> ' . (integer) $params['comment_status_not']); 
    20532096        } 
    20542097 
    20552098        if (isset($params['comment_trackback'])) { 
    2056             $strReq .= 'AND comment_trackback = ' . (integer) (boolean) $params['comment_trackback'] . ' '; 
     2099            $sql->where('comment_trackback = ' . (integer) (boolean) $params['comment_trackback']); 
    20572100        } 
    20582101 
    20592102        if (isset($params['comment_ip'])) { 
    2060             $comment_ip = $this->con->escape(str_replace('*', '%', $params['comment_ip'])); 
    2061             $strReq .= "AND comment_ip LIKE '" . $comment_ip . "' "; 
     2103            $comment_ip = $sql->quote(str_replace('*', '%', $params['comment_ip'])); 
     2104            $sql->where('comment_ip LIKE ' . $comment_ip); 
    20622105        } 
    20632106 
    20642107        if (isset($params['q_author'])) { 
    2065             $q_author = $this->con->escape(str_replace('*', '%', strtolower($params['q_author']))); 
    2066             $strReq .= "AND LOWER(comment_author) LIKE '" . $q_author . "' "; 
     2108            $q_author = $sql->quote(str_replace('*', '%', strtolower($params['q_author']))); 
     2109            $sql->where('LOWER(comment_author) LIKE ' . $q_author); 
    20672110        } 
    20682111 
     
    20782121                if ($words) { 
    20792122                    foreach ($words as $i => $w) { 
    2080                         $words[$i] = "comment_words LIKE '%" . $this->con->escape($w) . "%'"; 
     2123                        $words[$i] = "comment_words LIKE '%" . $sql->escape($w) . "%'"; 
    20812124                    } 
    2082                     $strReq .= 'AND ' . implode(' AND ', $words) . ' '; 
     2125                    $sql->where(implode(' AND ', $words)); 
    20832126                } 
    20842127            } 
     
    20862129 
    20872130        if (!empty($params['sql'])) { 
    2088             $strReq .= $params['sql'] . ' '; 
     2131            $sql->sql($params['sql']); 
    20892132        } 
    20902133 
    20912134        if (!$count_only) { 
    20922135            if (!empty($params['order'])) { 
    2093                 $strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' '; 
     2136                $sql->order($sql->escape($params['order'])); 
    20942137            } else { 
    2095                 $strReq .= 'ORDER BY comment_dt DESC '; 
     2138                $sql->order('comment_dt DESC'); 
    20962139            } 
    20972140        } 
    20982141 
    20992142        if (!$count_only && !empty($params['limit'])) { 
    2100             $strReq .= $this->con->limit($params['limit']); 
    2101         } 
     2143            $sql->limit($params['limit']); 
     2144        } 
     2145 
     2146        $query = $sql->statement(); 
    21022147 
    21032148        if (!empty($params['sql_only'])) { 
    2104             return $strReq; 
    2105         } 
    2106  
    2107         $rs       = $this->con->select($strReq); 
     2149            return $query; 
     2150        } 
     2151 
     2152        $rs       = $this->con->select($query); 
    21082153        $rs->core = $this->core; 
    21092154        $rs->extend('rsExtComment'); 
     
    21282173        { 
    21292174            # Get ID 
    2130             $rs = $this->con->select( 
    2131                 'SELECT MAX(comment_id) ' . 
    2132                 'FROM ' . $this->prefix . 'comment ' 
    2133             ); 
     2175            $sql = new dcSelectStatement($this->core, 'coreCommentCreateGetID'); 
     2176            $sql 
     2177                ->columns('MAX(comment_id)') 
     2178                ->from($this->prefix . 'comment'); 
     2179            $rs = $this->con->select($sql->statement()); 
    21342180 
    21352181            $cur->comment_id    = (integer) $rs->f(0) + 1; 
     
    22082254        $this->core->callBehavior('coreBeforeCommentUpdate', $this, $cur, $rs); 
    22092255 
    2210         $cur->update('WHERE comment_id = ' . $id . ' '); 
     2256        $sql = new dcUpdateStatement($this->core, 'coreCommentUpdate'); 
     2257        $sql->where('comment_id = ' . $id); 
     2258 
     2259        $cur->update($sql->whereStatement()); 
    22112260 
    22122261        # --BEHAVIOR-- coreAfterCommentUpdate 
     
    22432292        $status = (integer) $status; 
    22442293 
    2245         $strReq = 
    2246         'UPDATE ' . $this->prefix . 'comment ' . 
    2247             'SET comment_status = ' . $status . ' '; 
    2248         $strReq .= 
    2249         'WHERE comment_id' . $this->con->in($co_ids) . 
    2250         'AND post_id in (SELECT tp.post_id ' . 
    2251         'FROM ' . $this->prefix . 'post tp ' . 
    2252         "WHERE tp.blog_id = '" . $this->con->escape($this->id) . "' "; 
     2294        $sqlIn = new dcSelectStatement($this->core, 'coreUpdCommentsStatusIn'); 
     2295        $sqlIn 
     2296            ->columns('tp.post_id') 
     2297            ->from($this->prefix . 'post tp') 
     2298            ->where('tp.blog_id = ' . $sqlIn->quote($this->id)); 
    22532299        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    2254             $strReq .= 
    2255             "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
    2256         } 
    2257         $strReq .= ')'; 
    2258         $this->con->execute($strReq); 
     2300            $sqlIn->where('user_id = ' . $sqlIn->quote($this->core->auth->userID())); 
     2301        } 
     2302 
     2303        $sql = new dcUpdateStatement($this->core, 'coreUpdCommentsStatus'); 
     2304        $sql 
     2305            ->reference($this->prefix . 'comment') 
     2306            ->set('comment_status = ' . $status) 
     2307            ->where(array( 
     2308                'comment_id ' . $sql->in($co_ids), 
     2309                'post_id in (' . $sqlIn->statement() . ')' 
     2310            )); 
     2311 
     2312        $this->con->execute($sql->statement()); 
    22592313        $this->triggerComments($co_ids); 
    22602314        $this->triggerBlog(); 
     
    22902344        # Retrieve posts affected by comments edition 
    22912345        $affected_posts = array(); 
    2292         $strReq         = 
    2293         'SELECT post_id ' . 
    2294         'FROM ' . $this->prefix . 'comment ' . 
    2295         'WHERE comment_id' . $this->con->in($co_ids) . 
    2296             'GROUP BY post_id'; 
    2297  
    2298         $rs = $this->con->select($strReq); 
     2346 
     2347        $sql = new dcSelectStatement($this->core, 'coreDelCommentsScope'); 
     2348        $sql 
     2349            ->column('post_id') 
     2350            ->from($this->prefix . 'comment') 
     2351            ->where('comment_id' . $sql->in($co_ids)) 
     2352            ->group('post_id'); 
     2353 
     2354        $rs = $this->con->select($sql->statement()); 
     2355        unset($sql); 
    22992356 
    23002357        while ($rs->fetch()) { 
     
    23022359        } 
    23032360 
    2304         $strReq = 
    2305         'DELETE FROM ' . $this->prefix . 'comment ' . 
    2306         'WHERE comment_id' . $this->con->in($co_ids) . ' ' . 
    2307         'AND post_id in (SELECT tp.post_id ' . 
    2308         'FROM ' . $this->prefix . 'post tp ' . 
    2309         "WHERE tp.blog_id = '" . $this->con->escape($this->id) . "' "; 
    2310         #If user can only delete, we need to check the post's owner 
     2361        $sqlIn = new dcSelectStatement($this->core, 'coreDelCommentsIn'); 
     2362        $sqlIn 
     2363            ->columns('tp.post_id') 
     2364            ->from($this->prefix . 'post tp') 
     2365            ->where(array('tp.blog_id = ' . $sqlIn->quote($this->id))); 
    23112366        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    2312             $strReq .= 
    2313             "AND tp.user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
    2314         } 
    2315         $strReq .= ")"; 
    2316         $this->con->execute($strReq); 
     2367            $sqlIn->where('tp.user_id = ' . $sqlIn->quote($this->core->auth->userID())); 
     2368        } 
     2369 
     2370        $sql = new dcDeleteStatement($this->core, 'coreDelComments'); 
     2371        $sql 
     2372            ->from($this->prefix . 'comment') 
     2373            ->where(array( 
     2374                'comment_id ' . $sql->in($co_ids), 
     2375                'post_id in (' . $sqlIn->statement() . ')' 
     2376            )); 
     2377 
     2378        $this->con->execute($sql->statement()); 
    23172379        $this->triggerComments($co_ids, true, $affected_posts); 
    23182380        $this->triggerBlog(); 
     
    23252387        } 
    23262388 
    2327         $strReq = 
    2328         'DELETE FROM ' . $this->prefix . 'comment ' . 
    2329         'WHERE comment_status = -2 ' . 
    2330         'AND post_id in (SELECT tp.post_id ' . 
    2331         'FROM ' . $this->prefix . 'post tp ' . 
    2332         "WHERE tp.blog_id = '" . $this->con->escape($this->id) . "' "; 
    2333         #If user can only delete, we need to check the post's owner 
     2389        $sqlIn = new dcSelectStatement($this->core, 'coreDelJunkCommentsIn'); 
     2390        $sqlIn 
     2391            ->columns('tp.post_id') 
     2392            ->from($this->prefix . 'post tp') 
     2393            ->where('tp.blog_id = ' . $sqlIn->quote($this->id)); 
    23342394        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    2335             $strReq .= 
    2336             "AND tp.user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
    2337         } 
    2338         $strReq .= ")"; 
    2339         $this->con->execute($strReq); 
     2395            // If user can only delete, we need to check the post's owner 
     2396            $sqlIn->where('tp.user_id = ' . $sqlIn->quote($this->core->auth->userID())); 
     2397        } 
     2398 
     2399        $sql = new dcDeleteStatement($this->core, 'coreDelJunkComments'); 
     2400        $sql 
     2401            ->from($this->prefix . 'comment') 
     2402            ->where(array( 
     2403                'comment_status = -2', 
     2404                'post_id in (' . $sqlIn->statement() . ')' 
     2405            )); 
     2406 
     2407        $this->con->execute($sql->statement()); 
    23402408        $this->triggerBlog(); 
    23412409    } 
  • inc/core/class.dc.core.php

    r3731 r3761  
    442442        # Fetch versions if needed 
    443443        if (!is_array($this->versions)) { 
    444             $strReq = 'SELECT module, version FROM ' . $this->prefix . 'version'; 
    445             $rs     = $this->con->select($strReq); 
     444            $sql = new dcSelectStatement($this, 'coreGetVersion'); 
     445            $sql 
     446                ->columns(array('module', 'version')) 
     447                ->from($this->prefix . 'version'); 
     448 
     449            $rs     = $this->con->select($sql->statement()); 
    446450 
    447451            while ($rs->fetch()) { 
     
    474478            $cur->insert(); 
    475479        } else { 
    476             $cur->update("WHERE module='" . $this->con->escape($module) . "'"); 
     480            $sql = new dcUpdateStatement($this, 'coreSetVersion'); 
     481            $sql->where('module=' . $sql->quote($module)); 
     482 
     483            $cur->update($sql->whereStatement()); 
    477484        } 
    478485 
     
    487494    public function delVersion($module) 
    488495    { 
    489         $strReq = 
    490         'DELETE FROM ' . $this->prefix . 'version ' . 
    491         "WHERE module = '" . $this->con->escape($module) . "' "; 
    492  
    493         $this->con->execute($strReq); 
     496        $sql = new dcDeleteStatement($this, 'coreDelVersion'); 
     497        $sql 
     498            ->from($this->prefix . 'version') 
     499            ->where('module = ' . $sql->quote($module)); 
     500 
     501        $this->con->execute($sql->statement()); 
    494502 
    495503        if (is_array($this->versions)) { 
     
    530538    public function getUsers($params = array(), $count_only = false) 
    531539    { 
     540        $sql = new dcSelectStatement($this, 'coreGetUsers'); 
     541        $sql 
     542            ->from($this->prefix . 'user U') 
     543            ->where('NULL IS NULL'); 
     544 
    532545        if ($count_only) { 
    533             $strReq = 
    534             'SELECT count(U.user_id) ' . 
    535             'FROM ' . $this->prefix . 'user U ' . 
    536                 'WHERE NULL IS NULL '; 
     546            $sql->columns('COUNT(U.user_id)'); 
    537547        } else { 
    538             $strReq = 
    539             'SELECT U.user_id,user_super,user_status,user_pwd,user_change_pwd,' . 
    540             'user_name,user_firstname,user_displayname,user_email,user_url,' . 
    541             'user_desc, user_lang,user_tz, user_post_status,user_options, ' . 
    542             'count(P.post_id) AS nb_post ' . 
    543             'FROM ' . $this->prefix . 'user U ' . 
    544             'LEFT JOIN ' . $this->prefix . 'post P ON U.user_id = P.user_id ' . 
    545                 'WHERE NULL IS NULL '; 
     548            $sql 
     549                ->columns(array('U.user_id', 'user_super', 'user_status', 'user_pwd', 'user_change_pwd', 'user_name', 
     550                    'user_firstname', 'user_displayname', 'user_email', 'user_url', 'user_desc', 'user_lang', 'user_tz', 
     551                    'user_post_status', 'user_options', 'COUNT(P.post_id) AS nb_post')) 
     552                ->join('LEFT JOIN ' . $this->prefix . 'post P ON U.user_id = P.user_id'); 
    546553        } 
    547554 
    548555        if (!empty($params['q'])) { 
    549             $q = $this->con->escape(str_replace('*', '%', strtolower($params['q']))); 
    550             $strReq .= 'AND (' . 
     556            $q = $sql->escape(str_replace('*', '%', strtolower($params['q']))); 
     557            $sql->where( 
     558                '(' . 
    551559                "LOWER(U.user_id) LIKE '" . $q . "' " . 
    552560                "OR LOWER(user_name) LIKE '" . $q . "' " . 
    553561                "OR LOWER(user_firstname) LIKE '" . $q . "' " . 
    554                 ') '; 
     562                ')' 
     563            ); 
    555564        } 
    556565 
    557566        if (!empty($params['user_id'])) { 
    558             $strReq .= "AND U.user_id = '" . $this->con->escape($params['user_id']) . "' "; 
     567            $sql->where('U.user_id = ' . $sql->quote($params['user_id'])); 
    559568        } 
    560569 
    561570        if (!$count_only) { 
    562             $strReq .= 'GROUP BY U.user_id,user_super,user_status,user_pwd,user_change_pwd,' . 
    563                 'user_name,user_firstname,user_displayname,user_email,user_url,' . 
    564                 'user_desc, user_lang,user_tz,user_post_status,user_options '; 
     571            $sql->group(array('U.user_id', 'user_super', 'user_status', 'user_pwd', 'user_change_pwd', 'user_name', 'user_firstname', 'user_displayname', 'user_email', 'user_url', 'user_desc', 'user_lang', 'user_tz', 'user_post_status', 'user_options')); 
    565572 
    566573            if (!empty($params['order']) && !$count_only) { 
     
    571578                        $table_prefix = ''; // order = nb_post (asc|desc) 
    572579                    } 
    573                     $strReq .= 'ORDER BY ' . $table_prefix . $this->con->escape($params['order']) . ' '; 
     580                    $sql->order($table_prefix . $sql->escape($params['order'])); 
    574581                } else { 
    575                     $strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' '; 
     582                    $sql->order($sql->escape($params['order'])); 
    576583                } 
    577584            } else { 
    578                 $strReq .= 'ORDER BY U.user_id ASC '; 
     585                $sql->order('U.user_id ASC'); 
    579586            } 
    580587        } 
    581588 
    582589        if (!$count_only && !empty($params['limit'])) { 
    583             $strReq .= $this->con->limit($params['limit']); 
    584         } 
    585         $rs = $this->con->select($strReq); 
     590            $sql->limit($params['limit']); 
     591        } 
     592 
     593        $rs = $this->con->select($sql->statement()); 
    586594        $rs->extend('rsExtUser'); 
    587595        return $rs; 
     
    637645        } 
    638646 
    639         $cur->update("WHERE user_id = '" . $this->con->escape($id) . "' "); 
     647        $sql = new dcUpdateStatement($this, 'coreUpdUser'); 
     648        $sql 
     649            ->where('user_id = ' . $sql->quote($id)); 
     650 
     651        $cur->update($sql->whereStatement()); 
    640652 
    641653        $this->auth->afterUpdUser($id, $cur); 
     
    646658 
    647659        # Updating all user's blogs 
    648         $rs = $this->con->select( 
    649             'SELECT DISTINCT(blog_id) FROM ' . $this->prefix . 'post ' . 
    650             "WHERE user_id = '" . $this->con->escape($id) . "' " 
    651         ); 
     660        $sql = new dcSelectStatement($this, 'coreUpdUser'); 
     661        $sql 
     662            ->columns('DISTINCT(blog_id)') 
     663            ->from($this->prefix . 'post') 
     664            ->where('user_id = ' . $sql->quote($id)); 
     665 
     666        $rs = $this->con->select($sql->statement()); 
    652667 
    653668        while ($rs->fetch()) { 
     
    681696        } 
    682697 
    683         $strReq = 'DELETE FROM ' . $this->prefix . 'user ' . 
    684         "WHERE user_id = '" . $this->con->escape($id) . "' "; 
    685  
    686         $this->con->execute($strReq); 
     698        $sql = new dcDeleteStatement($this, 'coreDelUser'); 
     699        $sql 
     700            ->from($this->prefix . 'user') 
     701            ->where('user_id = ' . $sql->quote($id)); 
     702 
     703        $this->con->execute($sql->statement()); 
    687704 
    688705        $this->auth->afterDelUser($id); 
     
    697714    public function userExists($id) 
    698715    { 
    699         $strReq = 'SELECT user_id ' . 
    700         'FROM ' . $this->prefix . 'user ' . 
    701         "WHERE user_id = '" . $this->con->escape($id) . "' "; 
    702  
    703         $rs = $this->con->select($strReq); 
     716        $sql = new dcSelectStatement($this, 'coreUserExists'); 
     717        $sql 
     718            ->columns('user_id') 
     719            ->from($this->prefix . 'user') 
     720            ->where('user_id = ' . $sql->quote($id)); 
     721 
     722        $rs = $this->con->select($sql->statement()); 
    704723 
    705724        return !$rs->isEmpty(); 
     
    721740    public function getUserPermissions($id) 
    722741    { 
    723         $strReq = 'SELECT B.blog_id, blog_name, blog_url, permissions ' . 
    724         'FROM ' . $this->prefix . 'permissions P ' . 
    725         'INNER JOIN ' . $this->prefix . 'blog B ON P.blog_id = B.blog_id ' . 
    726         "WHERE user_id = '" . $this->con->escape($id) . "' "; 
    727  
    728         $rs = $this->con->select($strReq); 
     742        $sql = new dcSelectStatement($this, 'coreGetUserPermissions'); 
     743        $sql 
     744            ->columns(array('B.blog_id', 'blog_name', 'blog_url', 'permissions')) 
     745            ->from($this->prefix . 'permissions P') 
     746            ->join('INNER JOIN ' . $this->prefix . 'blog B ON P.blog_id = B.blog_id') 
     747            ->where('user_id = ' . $sql->quote($id)); 
     748 
     749        $rs = $this->con->select($sql->statement()); 
    729750 
    730751        $res = array(); 
     
    756777        } 
    757778 
    758         $strReq = 'DELETE FROM ' . $this->prefix . 'permissions ' . 
    759         "WHERE user_id = '" . $this->con->escape($id) . "' "; 
    760  
    761         $this->con->execute($strReq); 
     779        $sql = new dcDeleteStatement($this, 'coreSetUserPermissions'); 
     780        $sql 
     781            ->from($this->prefix . 'permissions') 
     782            ->where('user_id = ' . $sql->quote($id)); 
     783 
     784        $this->con->execute($sql->statement()); 
    762785 
    763786        foreach ($perms as $blog_id => $p) { 
     
    792815 
    793816        if ($delete_first || $no_perm) { 
    794             $strReq = 'DELETE FROM ' . $this->prefix . 'permissions ' . 
    795             "WHERE blog_id = '" . $this->con->escape($blog_id) . "' " . 
    796             "AND user_id = '" . $this->con->escape($id) . "' "; 
    797  
    798             $this->con->execute($strReq); 
     817            $sql = new dcDeleteStatement($this, 'coreSetUserBlogPermissions'); 
     818            $sql 
     819                ->from($this->prefix . 'permissions') 
     820                ->where(array( 
     821                    'blog_id = ' . $sql->quote($blog_id), 
     822                    'user_id = ' . $sql->quote($id) 
     823                )); 
     824            $this->con->execute($sql->statement()); 
    799825        } 
    800826 
     
    814840        $cur = $this->con->openCursor($this->prefix . 'user'); 
    815841 
     842        $sql = new dcUpdateStatement($this, 'coreSetUserDefaultBlog'); 
     843        $sql->where('user_id = ' . $sql->quote($id)); 
     844 
    816845        $cur->user_default_blog = (string) $blog_id; 
    817846 
    818         $cur->update("WHERE user_id = '" . $this->con->escape($id) . "'"); 
     847        $cur->update($sql->whereStatement()); 
    819848    } 
    820849 
     
    890919    public function getBlogPermissions($id, $with_super = true) 
    891920    { 
    892         $strReq = 
    893         'SELECT U.user_id AS user_id, user_super, user_name, user_firstname, ' . 
    894         'user_displayname, user_email, permissions ' . 
    895         'FROM ' . $this->prefix . 'user U ' . 
    896         'JOIN ' . $this->prefix . 'permissions P ON U.user_id = P.user_id ' . 
    897         "WHERE blog_id = '" . $this->con->escape($id) . "' "; 
     921        $sql = new dcSelectStatement($this, 'coreGetBlogPermissions'); 
     922        $sql 
     923            ->columns(array('U.user_id AS user_id', 'user_super', 'user_name', 'user_firstname', 'user_displayname', 
     924                'user_email', 'permissions')) 
     925            ->from($this->prefix . 'user U') 
     926            ->join('JOIN ' . $this->prefix . 'permissions P ON U.user_id = P.user_id') 
     927            ->where('blog_id = ' . $sql->quote($id)); 
    898928 
    899929        if ($with_super) { 
    900             $strReq .= 
    901             'UNION ' . 
    902             'SELECT U.user_id AS user_id, user_super, user_name, user_firstname, ' . 
    903             "user_displayname, user_email, NULL AS permissions " . 
    904             'FROM ' . $this->prefix . 'user U ' . 
    905                 'WHERE user_super = 1 '; 
    906         } 
    907  
    908         $rs = $this->con->select($strReq); 
     930            $sqlSuper = new dcSelectStatement($this, 'coreGetBlogPermissionsSuper'); 
     931            $sqlSuper 
     932                ->columns(array('U.user_id AS user_id', 'user_super', 'user_name', 'user_firstname', 'user_displayname', 
     933                    'user_email', 'NULL AS permissions')) 
     934                ->from($this->prefix . 'user U') 
     935                ->where('user_super = 1'); 
     936            $sql->sql('UNION ' . $sqlSuper->statement()); 
     937        } 
     938 
     939        $rs = $this->con->select($sql->statement()); 
    909940 
    910941        $res = array(); 
     
    955986    public function getBlogs($params = array(), $count_only = false) 
    956987    { 
    957         $join  = ''; // %1$s 
    958         $where = ''; // %2$s 
     988        $sql = new dcSelectStatement($this, 'coreGetBlogs'); 
     989        $sql->from($this->prefix . 'blog B'); 
    959990 
    960991        if ($count_only) { 
    961             $strReq = 'SELECT count(B.blog_id) ' . 
    962             'FROM ' . $this->prefix . 'blog B ' . 
    963                 '%1$s ' . 
    964                 'WHERE NULL IS NULL ' . 
    965                 '%2$s '; 
     992            $sql->columns('COUNT(B.blog_id)'); 
    966993        } else { 
    967             $strReq = 
    968             'SELECT B.blog_id, blog_uid, blog_url, blog_name, blog_desc, blog_creadt, ' . 
    969             'blog_upddt, blog_status ' . 
    970             'FROM ' . $this->prefix . 'blog B ' . 
    971                 '%1$s ' . 
    972                 'WHERE NULL IS NULL ' . 
    973                 '%2$s '; 
     994            $sql->columns(array('B.blog_id', 'blog_uid', 'blog_url', 'blog_name', 'blog_desc', 'blog_creadt', 'blog_upddt', 'blog_status')); 
    974995 
    975996            if (!empty($params['order'])) { 
    976                 $strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' '; 
     997                $sql->order($sql->escape($params['order'])); 
    977998            } else { 
    978                 $strReq .= 'ORDER BY B.blog_id ASC '; 
     999                $sql->order('B.blog_id ASC'); 
    9791000            } 
    9801001 
    9811002            if (!empty($params['limit'])) { 
    982                 $strReq .= $this->con->limit($params['limit']); 
     1003                $sql->limit($params['limit']); 
    9831004            } 
    9841005        } 
    9851006 
    9861007        if ($this->auth->userID() && !$this->auth->isSuperAdmin()) { 
    987             $join  = 'INNER JOIN ' . $this->prefix . 'permissions PE ON B.blog_id = PE.blog_id '; 
    988             $where = 
    989             "AND PE.user_id = '" . $this->con->escape($this->auth->userID()) . "' " . 
    990                 "AND (permissions LIKE '%|usage|%' OR permissions LIKE '%|admin|%' OR permissions LIKE '%|contentadmin|%') " . 
    991                 "AND blog_status IN (1,0) "; 
     1008            $sql->join('INNER JOIN ' . $this->prefix . 'permissions PE ON B.blog_id = PE.blog_id'); 
     1009            $sql->where("AND PE.user_id = " . $sql->quote($this->auth->userID()) . 
     1010                " AND (permissions LIKE '%|usage|%' OR permissions LIKE '%|admin|%' OR permissions LIKE '%|contentadmin|%')" . 
     1011                " AND blog_status IN (1,0)"); 
    9921012        } elseif (!$this->auth->userID()) { 
    993             $where = 'AND blog_status IN (1,0) '; 
     1013            $sql->where('blog_status IN (1,0)'); 
    9941014        } 
    9951015 
    9961016        if (isset($params['blog_status']) && $params['blog_status'] !== '' && $this->auth->isSuperAdmin()) { 
    997             $where .= 'AND blog_status = ' . (integer) $params['blog_status'] . ' '; 
     1017            $sql->where('blog_status = ' . (integer) $params['blog_status']); 
    9981018        } 
    9991019 
     
    10021022                $params['blog_id'] = array($params['blog_id']); 
    10031023            } 
    1004             $where .= 'AND B.blog_id ' . $this->con->in($params['blog_id']); 
     1024            $sql->where('B.blog_id ' . $sql->in($params['blog_id'])); 
    10051025        } 
    10061026 
    10071027        if (!empty($params['q'])) { 
    10081028            $params['q'] = strtolower(str_replace('*', '%', $params['q'])); 
    1009             $where .= 
    1010             'AND (' . 
    1011             "LOWER(B.blog_id) LIKE '" . $this->con->escape($params['q']) . "' " . 
    1012             "OR LOWER(B.blog_name) LIKE '" . $this->con->escape($params['q']) . "' " . 
    1013             "OR LOWER(B.blog_url) LIKE '" . $this->con->escape($params['q']) . "' " . 
    1014                 ') '; 
    1015         } 
    1016  
    1017         $strReq = sprintf($strReq, $join, $where); 
    1018         return $this->con->select($strReq); 
     1029            $sql->where('(' . 
     1030                "LOWER(B.blog_id) LIKE " . $sql->quote($params['q']) . 
     1031                " OR LOWER(B.blog_name) LIKE " . $sql->quote($params['q']) . 
     1032                " OR LOWER(B.blog_url) LIKE " . $sql->quote($params['q']) . 
     1033                ')'); 
     1034        } 
     1035 
     1036        return $this->con->select($sql->statement()); 
    10191037    } 
    10201038 
     
    10511069        $cur->blog_upddt = date('Y-m-d H:i:s'); 
    10521070 
    1053         $cur->update("WHERE blog_id = '" . $this->con->escape($id) . "'"); 
     1071        $sql = new dcUpdateStatement($this, 'coreUpdBlog'); 
     1072        $sql->where('blog_id = ' . $sql->quote($id)); 
     1073        $cur->update($sql->whereStatement()); 
    10541074    } 
    10551075 
     
    10901110        } 
    10911111 
    1092         $strReq = 'DELETE FROM ' . $this->prefix . 'blog ' . 
    1093         "WHERE blog_id = '" . $this->con->escape($id) . "' "; 
    1094  
    1095         $this->con->execute($strReq); 
     1112        $sql = new dcDeleteStatement($this, 'coreDelBlog'); 
     1113        $sql 
     1114            ->from($this->prefix . 'blog') 
     1115            ->where('blog_id = ' . $sql->quote($id)); 
     1116 
     1117        $this->con->execute($sql->statement()); 
    10961118    } 
    10971119 
     
    11041126    public function blogExists($id) 
    11051127    { 
    1106         $strReq = 'SELECT blog_id ' . 
    1107         'FROM ' . $this->prefix . 'blog ' . 
    1108         "WHERE blog_id = '" . $this->con->escape($id) . "' "; 
    1109  
    1110         $rs = $this->con->select($strReq); 
     1128        $sql = new dcSelectStatement($this, 'coreBlogExists'); 
     1129        $sql 
     1130            ->columns('blog_id') 
     1131            ->from($this->prefix . 'blog') 
     1132            ->where('blog_id = ' . $sql->quote($id)); 
     1133 
     1134        $rs = $this->con->select($sql->statement()); 
    11111135 
    11121136        return !$rs->isEmpty(); 
     
    11221146    public function countBlogPosts($id, $type = null) 
    11231147    { 
    1124         $strReq = 'SELECT COUNT(post_id) ' . 
    1125         'FROM ' . $this->prefix . 'post ' . 
    1126         "WHERE blog_id = '" . $this->con->escape($id) . "' "; 
     1148        $sql = new dcSelectStatement($this, 'coreCountBlogPosts'); 
     1149        $sql 
     1150            ->columns('COUNT(post_id)') 
     1151            ->from($this->prefix . 'post') 
     1152            ->where('blog_id = ' . $sql->quote($id)); 
    11271153 
    11281154        if ($type) { 
    1129             $strReq .= "AND post_type = '" . $this->con->escape($type) . "' "; 
    1130         } 
    1131  
    1132         return $this->con->select($strReq)->f(0); 
     1155            $sql->where('post_type = ' . $sql->quote($type)); 
     1156        } 
     1157 
     1158        return $this->con->select($sql->statement())->f(0); 
    11331159    } 
    11341160    //@} 
     
    14681494    public function indexAllPosts($start = null, $limit = null) 
    14691495    { 
    1470         $strReq = 'SELECT COUNT(post_id) ' . 
    1471         'FROM ' . $this->prefix . 'post'; 
    1472         $rs    = $this->con->select($strReq); 
     1496        $sql = new dcSelectStatement($this, 'coreIndexAllPostsCount'); 
     1497        $sql 
     1498            ->columns('COUNT(post_id)') 
     1499            ->from($this->prefix . 'post'); 
     1500 
     1501        $rs    = $this->con->select($sql->statement()); 
    14731502        $count = $rs->f(0); 
    1474  
    1475         $strReq = 'SELECT post_id, post_title, post_excerpt_xhtml, post_content_xhtml ' . 
    1476         'FROM ' . $this->prefix . 'post '; 
     1503        unset($sql); 
     1504 
     1505        $sql = new dcSelectStatement($this, 'coreIndexAllPosts'); 
     1506        $sql 
     1507            ->columns(array('post_id', 'post_title', 'post_excerpt_xhtml', 'post_content_xhtml')) 
     1508            ->from($this->prefix . 'post'); 
    14771509 
    14781510        if ($start !== null && $limit !== null) { 
    1479             $strReq .= $this->con->limit($start, $limit); 
    1480         } 
    1481  
    1482         $rs = $this->con->select($strReq, true); 
     1511            $sql->limit($start, $limit); 
     1512        } 
     1513 
     1514        $rs = $this->con->select($sql->statement()); 
     1515        unset($sql); 
    14831516 
    14841517        $cur = $this->con->openCursor($this->prefix . 'post'); 
    14851518 
     1519        $sql = new dcUpdateStatement($this, 'coreIndexAllPosts'); 
    14861520        while ($rs->fetch()) { 
    14871521            $words = $rs->post_title . ' ' . $rs->post_excerpt_xhtml . ' ' . 
     
    14891523 
    14901524            $cur->post_words = implode(' ', text::splitWords($words)); 
    1491             $cur->update('WHERE post_id = ' . (integer) $rs->post_id); 
     1525            $sql->where('post_id = ' . (integer) $rs->post_id, true); 
     1526            $cur->update($sql->whereStatement()); 
    14921527            $cur->clean(); 
    14931528        } 
     
    15091544    public function indexAllComments($start = null, $limit = null) 
    15101545    { 
    1511         $strReq = 'SELECT COUNT(comment_id) ' . 
    1512         'FROM ' . $this->prefix . 'comment'; 
    1513         $rs    = $this->con->select($strReq); 
     1546        $sql = new dcSelectStatement($this, 'coreIndexAllCommentsCount'); 
     1547        $sql 
     1548            ->columns('COUNT(comment_id)') 
     1549            ->from($this->prefix . 'comment'); 
     1550 
     1551        $rs    = $this->con->select($sql->statement()); 
    15141552        $count = $rs->f(0); 
    1515  
    1516         $strReq = 'SELECT comment_id, comment_content ' . 
    1517         'FROM ' . $this->prefix . 'comment '; 
     1553        unset($sql); 
     1554 
     1555        $sql = new dcSelectStatement($this, 'coreIndexAllComments'); 
     1556        $sql 
     1557            ->columns(array('comment_id', 'comment_content')) 
     1558            ->from($this->prefix . 'comment'); 
    15181559 
    15191560        if ($start !== null && $limit !== null) { 
    1520             $strReq .= $this->con->limit($start, $limit); 
    1521         } 
    1522  
    1523         $rs = $this->con->select($strReq); 
     1561            $sql->limit($start, $limit); 
     1562        } 
     1563 
     1564        $rs = $this->con->select($sql->statement()); 
     1565        unset($sql); 
    15241566 
    15251567        $cur = $this->con->openCursor($this->prefix . 'comment'); 
    15261568 
     1569        $sql = new dcUpdateStatement($this, 'coreIndexAllComments'); 
    15271570        while ($rs->fetch()) { 
    15281571            $cur->comment_words = implode(' ', text::splitWords($rs->comment_content)); 
    1529             $cur->update('WHERE comment_id = ' . (integer) $rs->comment_id); 
     1572            $sql->where('comment_id = ' . (integer) $rs->comment_id, true); 
     1573            $cur->update($sql->whereStatement()); 
    15301574            $cur->clean(); 
    15311575        } 
     
    15421586    public function countAllComments() 
    15431587    { 
    1544  
    1545         $updCommentReq = 'UPDATE ' . $this->prefix . 'post P ' . 
    1546         'SET nb_comment = (' . 
    1547         'SELECT COUNT(C.comment_id) from ' . $this->prefix . 'comment C ' . 
    1548             'WHERE C.post_id = P.post_id AND C.comment_trackback <> 1 ' . 
    1549             'AND C.comment_status = 1 ' . 
    1550             ')'; 
    1551         $updTrackbackReq = 'UPDATE ' . $this->prefix . 'post P ' . 
    1552         'SET nb_trackback = (' . 
    1553         'SELECT COUNT(C.comment_id) from ' . $this->prefix . 'comment C ' . 
    1554             'WHERE C.post_id = P.post_id AND C.comment_trackback = 1 ' . 
    1555             'AND C.comment_status = 1 ' . 
    1556             ')'; 
    1557         $this->con->execute($updCommentReq); 
    1558         $this->con->execute($updTrackbackReq); 
     1588        $sqlCount = new dcSelectStatement($this, 'coreCountAllComments'); 
     1589        $sqlCount 
     1590            ->columns('COUNT(C.comment_id)') 
     1591            ->from($this->prefix . 'comment C') 
     1592            ->where(array( 
     1593                'C.post_id = P.post_id', 
     1594                'C.comment_status = 1' 
     1595            )); 
     1596 
     1597        $sql = new dcUpdateStatement($this, 'coreCountAllComments'); 
     1598        $sql 
     1599            ->ref($this->prefix . 'post P'); 
     1600 
     1601        $sqlCount->cond('AND C.comment_trackback <> 1', true); 
     1602        $sql->set('nb_comment = (' . $sqlCount->statement() . ')', true); 
     1603        $this->con->execute($sql->statement()); 
     1604 
     1605        $sqlCount->cond('AND C.comment_trackback = 1', true); 
     1606        $sql->set('nb_trackback = (' . $sqlCount->statement() . ')', true); 
     1607        $this->con->execute($sql->statement()); 
    15591608    } 
    15601609 
  • inc/core/class.dc.rs.extensions.php

    r3731 r3761  
    444444            return $rs->_nb_media[$rs->index()]; 
    445445        } else { 
    446             $strReq = 
    447             'SELECT count(media_id) ' . 
    448             'FROM ' . $rs->core->prefix . 'post_media ' . 
    449             'WHERE post_id = ' . (integer) $rs->post_id . ' '; 
     446            $sql = new dcSelectStatement($rs->core, 'coreRsExtCountMedia'); 
     447            $sql 
     448                ->columns(array('count(media_id)')) 
     449                ->from($rs->core->prefix . 'post_media') 
     450                ->where('post_id = ' . $sql->quote((integer) $rs->post_id)); 
    450451            if ($link_type != null) { 
    451                 $strReq .= "AND link_type = '" . $rs->core->con->escape($link_type) . "'"; 
     452                $sql->where('link_type = ' . $sql->quote($link_type)); 
    452453            } 
    453  
    454             $res                         = (integer) $rs->core->con->select($strReq)->f(0); 
     454            $res                         = (integer) $rs->core->con->select($sql->statement())->f(0); 
    455455            $rs->_nb_media[$rs->index()] = $res; 
    456456            return $res; 
  • inc/core/class.dc.sql.statement.php

    r3754 r3761  
    300300     * May be used for debugging purpose as: 
    301301     * 
    302      * if (!$sql->isSame($sql->statement(), $oldRequest)) { 
    303      *     trigger_error('SQL statement error: ' . $sql->statement() . ' / ' . $oldRequest, E_USER_ERROR); 
     302     * if (!$sql->isSame($sql->statement(), $strReq)) { 
     303     *     trigger_error('SQL statement error: ' . $sql->statement() . ' / ' . $strReq, E_USER_ERROR); 
    304304     * } 
    305305     * 
Note: See TracChangeset for help on using the changeset viewer.

Sites map