/[cvs]/nfo/site/htdocs/inc/cms/cms.php.inc
ViewVC logotype

Diff of /nfo/site/htdocs/inc/cms/cms.php.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5 by rabit, Mon Sep 6 00:15:28 2004 UTC revision 1.7 by rabit, Sat Sep 18 19:20:29 2004 UTC
# Line 7  Line 7 
7  --- $Id$  --- $Id$
8  ------------------------------------------------------------------------------*/  ------------------------------------------------------------------------------*/
9    
10    //----------------------------------------------------------
11    //- Declaration of CMS data sources:
12    
13    $i = 0;
14    
15    $cms_sources = array(
16    
17     'SOURCES' => array(
18      'fields' => array(
19       'id' => '',
20       'name' => '',
21       'fieldcount' => '',
22      ),
23      'index' => $i++
24     ),
25    
26     'contents' => array(
27      'fields' => array(
28       'id' => '',
29       'creator_id' => '',
30       'content' => '',
31       'type' => '',
32       'timestamp' => '',
33       'description' => '',
34       'keyname' => '',
35       'language_id' => ''
36      ),
37      'index' => $i++
38     ),
39    
40     'contenttypes' => array(
41      'fields' => array(
42       'id' => '',
43       'name' => ''
44      ),
45      'index' => $i++
46     ),
47    
48     'languages' => array(
49      'fields' => array(
50       'id' => '',
51       'name' => '',
52       'abbreviation' => ''
53      ),
54      'index' => $i++
55     ),
56    
57    );
58    
59    //------------------------------------------------------------------------------
60    
61  function cms_getcontent($type, $keyname, $language_id = 0) {  function cms_getcontent($type, $keyname, $language_id = 0) {
62    
63    $sql = "SELECT contents.content, contents.description, UNIX_TIMESTAMP(contents.timestamp), languages.name, users.name, contents.keyname FROM contents, languages, users WHERE contents.type='$type' AND contents.keyname='$keyname'" . ($language_id ? " AND languages.id='$language_id'" : '') . " AND users.id=contents.creator_id AND languages.id=contents.language_id LIMIT 0,1;";    $sql = "SELECT contents.content, contents.description, UNIX_TIMESTAMP(contents.timestamp), languages.name, users.name, contents.keyname FROM contents, languages, users WHERE contents.type='$type' AND contents.keyname='$keyname'" . ($language_id ? " AND languages.id='$language_id'" : '') . " AND users.id=contents.creator_id AND languages.id=contents.language_id LIMIT 0,1;";
# Line 31  function cms_getcontent($type, $keyname, Line 82  function cms_getcontent($type, $keyname,
82    
83  //----------------------------------------------------------  //----------------------------------------------------------
84    
85  function cms_getlist($types, $keynames, $daterange = '', $languageids = '', $languages = '') {  function cms_getlist(
86     $types, $keynames, $daterange = '', $languageids = '', $languages = '') {
87    
88    $sql = "SELECT contents.id, contents.description, UNIX_TIMESTAMP(contents.timestamp), languages.id, languages.name, users.name FROM contents, languages, users WHERE contents.type='$types' AND contents.keyname='$keynames' AND users.id=contents.creator_id AND languages.id=contents.language_id;";    $sql = "SELECT contents.id, contents.description, UNIX_TIMESTAMP(contents.timestamp), languages.id, languages.name, users.name FROM contents, languages, users WHERE contents.type='$types' AND contents.keyname='$keynames' AND users.id=contents.creator_id AND languages.id=contents.language_id;";
89    
# Line 63  function cms_getindex($type) { Line 115  function cms_getindex($type) {
115    
116  //----------------------------------------------------------  //----------------------------------------------------------
117    
118  function cms_query($querycmd, &$response) {  function cms_query($nqlquery, &$response) {
119    
120    global $cms_sources;
121    
122  //------------------  //------------------
123    
124  $operationindices = array(  $operations = array(
125   'LIST' => 0,   'GET' => 0
  'GET' => 1  
126  );  );
127    
128  //------------------  //------------------
129    
130  $sourceindices = array(    cms_preparsenqlquery($nqlquery, $preparseresponse);
  'contenttypes' => 0,  
  'languages' => 1,  
  'contents' => 2  
 );  
131    
132  //------------------    $subsegcount = count($preparseresponse[1]);//['subsegmentcount'];
133    
134      $querydata = array(
135       'count' => '',
136       'fieldlist' => array(),
137       'first' => '',
138       'get_what' => '',
139       'locked' => '',
140       'operation' => '',
141       'orderby' => '',
142       'query' => $preparseresponse[0]['formattednql'],
143       'source' => '',
144       'with' => array(),
145      );
146    
147    $querycmd = str_replace("\r\n", ' ', $querycmd);    if(!$querydata['query']) {
   $querycmd = str_replace("\n", ' ', $querycmd);  
148    
149    $querycmd = trim($querycmd);      $response = cms_create_response('no_query', null, null, $querydata);
150        return false;
151    
152      }
153    
154      $subseg = 0;
155      $querydata['operation'] = $preparseresponse[1][$subseg][0];
156      $pcount = count($preparseresponse[1][$subseg++]);
157    
158    $d1 = strpos($querycmd, ' ');    if($pcount != 1) {
   $operation = substr($querycmd, 0, $d1);  
159    
160    if(isset($operationindices[$operation])) {      $response = cms_create_response('syntax', null, null, $querydata);
161        return false;
162    
163      }
164    
165      $operationindex = $operationindices[$operation];    if(isset($operations[$querydata['operation']])) {
166    
167        $operationindex = $operations[$querydata['operation']];
168    
169    } else {    } else {
170    
171      $response = cms_create_respose(1, null, null, null, $querycmd, $operation);      $response = cms_create_response('illegal_op', null, null, $querydata);
172        return false;
173    
174      }
175    
176      if($subseg == $subsegcount) {
177    
178        $response = cms_create_response('no_params', null, null, $querydata);
179      return false;      return false;
180    
181    }    }
182    
183    switch($operationindex) {    switch($operationindex) {
184    
185      case 0: // "LIST"      case 0: // "GET"
186    
187        $d2 = strpos($querycmd, ' WITH ', $d1 + 1);        $fieldlist = $preparseresponse[1][$subseg++];
188          $pcount = count($fieldlist);
189    
190        if($d2) {        if($subseg == $subsegcount) {
191    
192          $source = substr($querycmd, $d1 + 1, $d2 - $d1 - 1);          $response = cms_create_response('no_sourcedef', null, null, $querydata);
193          $conditionlist = substr($querycmd, $d2 + 6);          return false;
194    
195        } else {        }
196    
197          $querydata['get_what'] = $fieldlist[0];
198          $querydata['fieldlist'] = array();
199    
200          switch($querydata['get_what']) {
201    
202            case 'COUNT':
203            case 'FIELDNAMES':
204            case '*':
205    
206              if($pcount != 1) {
207    
208                $response = cms_create_response('syntax', null, null, $querydata);
209                return false;
210    
211              }
212    
213              break;
214    
215            case 'FROM':
216    
217              $response = cms_create_response('no_fielddef', null, null, $querydata);
218              return false;
219    
220            default:
221    
222          $source = substr($querycmd, $d1 + 1);            $querydata['fieldlist'] = $fieldlist;
223          $conditionlist = '';            $querydata['get_what'] = 'FIELD';
224    
225        }        }
226    
227        if(isset($sourceindices[$source])) {        $from = $preparseresponse[1][$subseg][0];
228          $pcount = count($preparseresponse[1][$subseg++]);
229    
230          $sourceindex = $sourceindices[$source];        if($from != 'FROM') {
231    
232        } else {          $response = cms_create_response('no_from', null, null, $querydata);
233            return false;
234    
235          }
236    
237          $response = cms_create_respose(2, null, null, null, $querycmd, $operation, $source, $conditionlist);        if($pcount != 1) {
238    
239            $response = cms_create_response('syntax', null, null, $querydata);
240          return false;          return false;
241    
242        }        }
243    
244        $sqlconditions = '';        if($subseg == $subsegcount) {
245    
246            $response = cms_create_response('no_source', null, null, $querydata);
247            return false;
248    
249          }
250    
251        if($conditionlist) {        $querydata['source'] = $preparseresponse[1][$subseg][0];
252          $pcount = count($preparseresponse[1][$subseg++]);
253    
254          $conditions = split(',', $conditionlist);        if($pcount != 1) {
255    
256          $sqlconditions .= ' WHERE';          $response = cms_create_response('syntax', null, null, $querydata);
257            return false;
258    
259          for($c = 0; $c < count($conditions); $c++) {        }
260    
261            list($conditionname, $conditionvalue) = split('=', $conditions[$c]);        if(isset($cms_sources[$querydata['source']])) {
           $condition = trim($conditionname) . '=\'' . trim($conditionvalue) . '\'';  
           $sqlconditions .= ($c ? ' AND' : '') . ' ' . $condition;  
262    
263          }          $sourcedata = $cms_sources[$querydata['source']];
264    //        $sourceindex = $sourcedata['index'];
265            $sourcefields = $sourcedata['fields'];
266    
267          } else {
268    
269            $response = cms_create_response('illegal_source', null, null, $querydata);
270            return false;
271    
272        }        }
273    
274        $sql = 'SELECT id FROM ' . $source . $sqlconditions . ';';        if($querydata['get_what'] == 'FIELDNAMES' || $querydata['get_what'] == '*') {
275    
276        $res = common_dbc_query($sql);          $querydata['fieldlist'] = array_keys($sourcedata['fields']);
277    
278        $columnnames = array('id');        }
       $resultlist = array();  
279    
280        while($row = mysql_fetch_row($res)) array_push($resultlist, $row);  $getparamdata = array(
281     'WITH' => '',
282     'FIRST' => '',
283     'COUNT' => '',
284     'ORDERBY' => '',
285     'LOCKED' => '',
286    );
287    //$getparams = array_keys($getparamdata);
288    
289        $response = cms_create_respose(0, $resultlist, $columnnames, 0, $querycmd, $operation, $source, $conditionlist);        $currentparam = 0;
290          $paramdata = $getparamdata;
291    
292        return true;        while($subseg < $subsegcount) {
293    
294        break;          $segment = $preparseresponse[1][$subseg++];
295            $pcount = count($segment);
296            $param = $segment[0];
297    
298      case 1: // "GET"          if($pcount != 1) {
299    
300        $d2 = strpos($querycmd, 'FROM') + 4;            $response = cms_create_response('syntax', null, null, $querydata);
301        $idlist = trim(substr($querycmd, $d1, $d2 - $d1 - 4));            return false;
       $source = trim(substr($querycmd, $d2));  
302    
303        if(isset($sourceindices[$source])) {          }
304    
305          $sourceindex = $sourceindices[$source];          if(!isset($paramdata[$param])) {
306    
307        } else {            $response = cms_create_response('illegal_param', null, null, $querydata);
308              return false;
309    
310          $response = cms_create_respose(2, null, null, null, $querycmd, $operation, $source);          } else if($paramdata[$param]) {
311    
312          return false;            $response = cms_create_response('double_param', null, null, $querydata);
313              return false;
314    
315            }
316    
317            $paramdata[$param] = true;
318    
319            switch($param) {
320    
321              case 'COUNT':
322    
323                if($subseg == $subsegcount) {
324    
325                  $response = cms_create_response('param_value', null, null, $querydata);
326                  return false;
327    
328                }
329    
330                if($subseg == $subsegcount) {
331    
332                  $response = cms_create_response('param_value', null, null, $querydata);
333                  return false;
334    
335                }
336    
337                $segment = $preparseresponse[1][$subseg++];
338                $pcount = count($segment);
339                $count = $segment[0];
340    
341                if($pcount != 1 || !is_numeric($count)) {
342    
343                  $response = cms_create_response('syntax', null, null, $querydata);
344                  return false;
345    
346                }
347    
348                $querydata['count'] = $count;
349    
350                break;
351    
352              case 'FIRST':
353    
354                if($subseg == $subsegcount) {
355    
356                  $response = cms_create_response('param_value', null, null, $querydata);
357                  return false;
358    
359                }
360    
361                if($subseg == $subsegcount) {
362    
363                  $response = cms_create_response('param_value', null, null, $querydata);
364                  return false;
365    
366                }
367    
368                $segment = $preparseresponse[1][$subseg++];
369                $pcount = count($segment);
370                $first = $segment[0];
371    
372                if($pcount != 1 || !is_numeric($first)) {
373    
374                  $response = cms_create_response('syntax', null, null, $querydata);
375                  return false;
376    
377                }
378    
379                $querydata['first'] = $first;
380    
381                break;
382    
383              case 'LOCKED':
384    
385                $querydata['locked'] = true;
386    
387                if($querydata['get_what'] == 'COUNT' || $querydata['get_what'] == 'FIELDNAMES') {
388    
389                  $response = cms_create_response('locked', null, null, $querydata);
390                  return false;
391    
392                }
393    
394                break;
395    
396              case 'ORDERBY':
397    
398                if($subseg == $subsegcount) {
399    
400                  $response = cms_create_response('param_value', null, null, $querydata);
401                  return false;
402    
403                }
404    
405                $segment = $preparseresponse[1][$subseg++];
406                $pcount = count($segment);
407                $fieldname = $segment[0];
408    
409                if($pcount != 1) {
410    
411                  $response = cms_create_response('syntax', null, null, $querydata);
412                  return false;
413    
414                }
415    
416                if(!isset($sourcefields[$fieldname])) {
417    
418                  $response = cms_create_response('illegal_field', null, null, $querydata);
419                  return false;
420    
421                }
422    
423                $querydata['orderby'] = $fieldname;
424    
425                break;
426    
427              case 'WITH':
428    
429                if($subseg == $subsegcount) {
430    
431                  $response = cms_create_response('param_value', null, null, $querydata);
432                  return false;
433    
434                }
435    
436                $segment = $preparseresponse[1][$subseg++];
437                $pcount = count($segment);
438                $fieldname = $segment[0];
439    
440                if($pcount != 1) {
441    
442                  $response = cms_create_response('syntax', null, null, $querydata);
443                  return false;
444    
445                }
446    
447                if(!isset($sourcefields[$fieldname])) {
448    
449                  $response = cms_create_response('illegal_field', null, null, $querydata);
450                  return false;
451    
452                }
453    
454                if($subseg == $subsegcount) {
455    
456                  $response = cms_create_response('param_value', null, null, $querydata);
457                  return false;
458    
459                }
460    
461                $fieldvalues = $preparseresponse[1][$subseg++];
462                array_push($querydata['with'], array($fieldname, $fieldvalues));
463    
464                break;
465    
466            }
467    
468        }        }
469    
470        switch($sourceindex) {        return cms_perform_get($querydata, $response);
471    
472          case 0: // "contenttypes"    }
473    
474            $fieldlist = 'id, name';  }
           $columnnames = array('id', 'name');  
475    
476            break;  //----------------------------------------------------------
477    
478          case 1: // "languages"  function cms_perform_get($querydata, &$response) {
479    
480            $fieldlist = 'id, name, abbreviation';  global $cms_sources;
           $columnnames = array('id', 'name', 'abbreviation');  
481    
482            break;    $sourcedata = $cms_sources[$querydata['source']];
483      $sourcefields = $sourcedata['fields'];
484    
485          case 2: // "contents"    for($f = 0; $f < count($querydata['fieldlist']); $f++) {
486    
487            $fieldlist = 'id, keyname, type, creator_id, language_id, description, content';      if(!isset($sourcefields[$querydata['fieldlist'][$f]])) {
           $columnnames = array('id', 'keyname', 'contenttype', 'creator_id', 'language_id', 'description', 'content');  
488    
489            break;        $response = cms_create_response('illegal_field', null, null, $querydata);
490          return false;
491    
492        }
493    
494      }
495    
496      $fieldnamelist = $querydata['fieldlist'];
497      $fieldnames = join($fieldnamelist, ', ');
498    
499      $resultlist = array();
500    
501      if($querydata['get_what'] == 'COUNT') {
502    
503        $fieldnamelist = array('COUNT');
504    
505      }
506    
507      if($querydata['get_what'] == 'FIELDNAMES') {
508    
509        $resultlist = null;
510    
511      } else switch($querydata['source']) {
512    
513        case 'SOURCES':
514    
515          if($querydata['get_what'] == 'COUNT') {
516    
517            $resultlist[0][0] = count($cms_sources);
518            break;
519    
520        }        }
521    
522        $sqlconditions = '';        while(list($key, $sourcedata) = each($cms_sources)) {
523    
524        if($idlist) {          $row = array(
525             'id' => $sourcedata['index'] + 1,
526             'name' => $key,
527             'fieldcount' => count($sourcedata['fields']),
528            );
529    
530          $ids = split(',', $idlist);          $resrow = array();
531            reset($fieldnamelist);
532    
533          $sqlconditions .= ' WHERE';          while(list($i, $fieldname) = each($fieldnamelist))
534             array_push($resrow, $row[$fieldname]);
535    
536          for($i = 0; $i < count($ids); $i++) {          array_push($resultlist, $resrow);
537    
538            $condition = ' id=\'' . intval(trim($ids[$i])) . '\'';        }
           $sqlconditions .= ($i ? ' OR' : '') . $condition;  
539    
540          }        break;
541    
542        } else {      // By default the db is used as data source:
543        default:
544    
545          if($querydata['get_what'] == 'COUNT') {
546    
547          // ERROR          $fieldnames = 'COUNT(id)';
548    
549        }        }
550    
551        $sql = "SELECT $fieldlist FROM " . $source . $sqlconditions . ';';        $limit = (
552           (($querydata['count'] > 0) || ($querydata['first'] != '')) ?
553           ' LIMIT ' . ($querydata['first'] > 0 ? $querydata['first'] - 1 : 0) . ',' .
554           ($querydata['count'] > 0 ? $querydata['count'] : -1) : '');
555    
556          $order = (
557           $querydata['orderby'] != '' ? ' ORDER BY ' . $querydata['orderby'] . ' DESC' : '');
558    
559          $where = '';
560    
561          while(list($i, $item) = each($querydata['with'])) {
562    
563            $values = $item[1];
564    
565            while(list($j, $value) = each($values))
566             $where .= ($j ? ' OR' : '') . ' ' . $item[0] . '=\'' . $value . '\'';
567    
568          }
569    
570          if($where) $where = ' WHERE' . $where;
571    
572          $sql = 'SELECT ' . $fieldnames . ' FROM ' . $querydata['source'] .
573           $where . $order . $limit . ';';
574    
575          #echo $sql;
576    
577        $res = common_dbc_query($sql);        $res = common_dbc_query($sql);
       $resultlist = array();  
578    
579        while($row = mysql_fetch_row($res)) array_push($resultlist, $row);        //if(!$res) ...
580    
581        $response = cms_create_respose(0, $resultlist, $columnnames, 0, $querycmd, $operation, $source);        while($resrow = mysql_fetch_row($res)) array_push($resultlist, $resrow);
 //      $response = cms_create_respose(0, $resultlist, $columnnames, 0, $sql, $operation, $source);  
582    
583        return true;    }
584    
585        break;    $response = cms_create_response('no_error', $resultlist, $fieldnamelist, $querydata);
586      return true;
587    
588    }
589    
590    //----------------------------------------------------------
591    
592    function cms_preparsenqlquery($nqlquery, &$response) {
593    
594      $querysubsegs = preg_split('/[\s,]+/', $nqlquery, -1,
595       PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);
596    
597      $segcount = count($querysubsegs);
598    
599      while(list($i, $item) = each($querysubsegs)) {
600    
601        $pos = $item[1];
602        $querysubsegs[$i][2] = strlen($item[0]);
603    
604        if($i < ($segcount - 1)) {
605    
606          $endpos = $pos + $querysubsegs[$i][2];
607          $delim = substr($nqlquery, $endpos, $querysubsegs[$i + 1][1] - $endpos);
608    
609        } else $delim = substr($nqlquery, $pos + $querysubsegs[$i][2]);
610    
611        array_push($querysubsegs[$i], trim($delim));
612    
613    }    }
614    
615      $i = 0;
616    
617      $formattednql = '';
618      $preparseresult = array();
619      $listitems = array();
620      $lastitem = null;
621      $listcount = 0;
622    
623      while($i < $segcount) {
624    
625        $item = $querysubsegs[$i];
626        $formattednql .= ($i ? ' ' : '') . $item[0]. $item[3];
627    
628        if($item[3] == ',') {
629    
630          array_push($listitems, $item[0]);
631    
632        } else {
633    
634          if($listitems) {
635    
636            array_push($listitems, $item[0]);
637            array_push($preparseresult, $listitems);
638            $listitems = array();
639            $listcount++;
640    
641          } else array_push($preparseresult, array($item[0]));
642    
643        }
644    
645    //    $lastitem = $item;
646        $i++;
647    
648      }
649    
650      $response = array(
651    
652       array(
653        'formattednql' => $formattednql,
654    //    'subsegmentcount' => $i,
655        'sublistcount' => $listcount
656       ),
657    
658       $preparseresult
659    
660      );
661    
662  }  }
663    
664  //----------------------------------------------------------  //----------------------------------------------------------
665    
666  function cms_create_respose(  function cms_create_response(
667   $errornumber,   $errorkey, $resultlist, $columnnames, $querydata) {
  $resultlist,  
  $columnnames,  
  $firstrow = 0,  
  $querycmd = '',  
  $operation = '',  
  $source = '',  
  $conditionlist = ''  
 ) {  
668    
669  //------------------  //------------------
670    
671  $errortexts = array(  $i = 0;
672   0 => 'No error',  
673   1 => 'Unknown base operation',  $errors = array(
674   2 => 'Unknown data source name'   'no_error' => array(
675      $i++, 'No error'
676     ),
677     'no_query' => array(
678      $i++, 'Empty query',
679     ),
680     'syntax' => array(
681      $i++, 'Query syntax error',
682     ),
683     'illegal_op' => array(
684      $i++, 'Illegal base operation',
685     ),
686     'no_params' => array(
687      $i++, 'Operation parameters missing',
688     ),
689     'no_fielddef' => array(
690      $i++, 'Field definition missing',
691     ),
692     'no_sourcedef' => array(
693      $i++, 'Source definition missing',
694     ),
695     'no_from' => array(
696      $i++, 'Source declarator missing',
697     ),
698     'no_source' => array(
699      $i++, 'Source name missing',
700     ),
701     'illegal_source' => array(
702      $i++, 'Illegal data source name',
703     ),
704     'illegal_field' => array(
705      $i++, 'Illegal field name for data source',
706     ),
707     'illegal_param' => array(
708      $i++, 'Illegal parameter name',
709     ),
710     'double_param' => array(
711      $i++, 'Illegal double parameter',
712     ),
713     'locked' => array(
714      $i++, 'Illegal use of LOCKED parameter',
715     ),
716     'param_value' => array(
717      $i++, 'Parameter value missing',
718     ),
719  );  );
720    
721  //------------------  //------------------
# Line 273  $errortexts = array( Line 724  $errortexts = array(
724    
725     // Result information:     // Result information:
726     0 => array(     0 => array(
727      'error' => $errornumber,      'columncount' => count($columnnames),
728      'errortext' => $errortexts[$errornumber],      'error' => $errors[$errorkey][0],
729      'columncount' => (isset($columnnames) ? count($columnnames) : null),      'errortext' => $errors[$errorkey][1],
730      'firstrow' => $firstrow,      'firstrow' => $querydata['first'],
731        'get_what' => $querydata['get_what'],
732        'operation' => $querydata['operation'],
733        'query' => $querydata['query'],
734      'rowcount' => count($resultlist),      'rowcount' => count($resultlist),
735      'querycmd' => $querycmd,      'source' => $querydata['source'],
736      'operation' => $operation,      'all' => $querydata
     'source' => $source,  
     'conditionlist' => $conditionlist  
737     ),     ),
738    
739     // Result list:     // Result list:

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.7

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed