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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations)
Sat Sep 18 19:20:29 2004 UTC (19 years, 11 months ago) by rabit
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +297 -29 lines
U cms_query(): Added processing of query parameters (and conditions); U cms_create_response(): more error types; U More info in $querydata.

1 <?php
2 /*------------------------------------------------------------------------------
3 --- www.netfrag.org
4 --- Content management functions include file.
5 --------------------------------------------------------------------------------
6 --- rabit, 01:04 27.08.2004
7 --- $Id: cms.php.inc,v 1.6 2004/09/10 21:40:59 rabit Exp $
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) {
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;";
64
65 $res = mysql_query($sql);
66 if(!$res) return false;
67
68 $row = mysql_fetch_row($res);
69
70 $contentdata = array(
71 'content' => $row[0],
72 'description' => $row[1],
73 'unixtime' => $row[2],
74 'language_name' => $row[3],
75 'creator_name' => $row[4],
76 'keyname' => $row[5],
77 );
78
79 return $contentdata;
80
81 }
82
83 //----------------------------------------------------------
84
85 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;";
89
90 $res = common_dbc_query($sql);
91
92 $rowcount = 0;
93
94 while($row = mysql_fetch_row($res)) $rows[$rowcount++] = $row;
95
96 return $rows;
97
98 }
99
100 //----------------------------------------------------------
101
102 function cms_getindex($type) {
103
104 $sql = "SELECT contents.id, contents.keyname, contents.description, UNIX_TIMESTAMP(contents.timestamp) as timestamp, languages.id, languages.name as lang, users.name as creator FROM contents, languages, users WHERE contents.type='$type' AND users.id=contents.creator_id AND languages.id=contents.language_id;";
105
106 $res = common_dbc_query($sql);
107
108 $rowcount = 0;
109
110 while($row = mysql_fetch_assoc($res)) $rows[$rowcount++] = $row;
111
112 return $rows;
113
114 }
115
116 //----------------------------------------------------------
117
118 function cms_query($nqlquery, &$response) {
119
120 global $cms_sources;
121
122 //------------------
123
124 $operations = array(
125 'GET' => 0
126 );
127
128 //------------------
129
130 cms_preparsenqlquery($nqlquery, $preparseresponse);
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 if(!$querydata['query']) {
148
149 $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 if($pcount != 1) {
159
160 $response = cms_create_response('syntax', null, null, $querydata);
161 return false;
162
163 }
164
165 if(isset($operations[$querydata['operation']])) {
166
167 $operationindex = $operations[$querydata['operation']];
168
169 } else {
170
171 $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;
180
181 }
182
183 switch($operationindex) {
184
185 case 0: // "GET"
186
187 $fieldlist = $preparseresponse[1][$subseg++];
188 $pcount = count($fieldlist);
189
190 if($subseg == $subsegcount) {
191
192 $response = cms_create_response('no_sourcedef', null, null, $querydata);
193 return false;
194
195 }
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 $querydata['fieldlist'] = $fieldlist;
223 $querydata['get_what'] = 'FIELD';
224
225 }
226
227 $from = $preparseresponse[1][$subseg][0];
228 $pcount = count($preparseresponse[1][$subseg++]);
229
230 if($from != 'FROM') {
231
232 $response = cms_create_response('no_from', null, null, $querydata);
233 return false;
234
235 }
236
237 if($pcount != 1) {
238
239 $response = cms_create_response('syntax', null, null, $querydata);
240 return false;
241
242 }
243
244 if($subseg == $subsegcount) {
245
246 $response = cms_create_response('no_source', null, null, $querydata);
247 return false;
248
249 }
250
251 $querydata['source'] = $preparseresponse[1][$subseg][0];
252 $pcount = count($preparseresponse[1][$subseg++]);
253
254 if($pcount != 1) {
255
256 $response = cms_create_response('syntax', null, null, $querydata);
257 return false;
258
259 }
260
261 if(isset($cms_sources[$querydata['source']])) {
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 if($querydata['get_what'] == 'FIELDNAMES' || $querydata['get_what'] == '*') {
275
276 $querydata['fieldlist'] = array_keys($sourcedata['fields']);
277
278 }
279
280 $getparamdata = array(
281 'WITH' => '',
282 'FIRST' => '',
283 'COUNT' => '',
284 'ORDERBY' => '',
285 'LOCKED' => '',
286 );
287 //$getparams = array_keys($getparamdata);
288
289 $currentparam = 0;
290 $paramdata = $getparamdata;
291
292 while($subseg < $subsegcount) {
293
294 $segment = $preparseresponse[1][$subseg++];
295 $pcount = count($segment);
296 $param = $segment[0];
297
298 if($pcount != 1) {
299
300 $response = cms_create_response('syntax', null, null, $querydata);
301 return false;
302
303 }
304
305 if(!isset($paramdata[$param])) {
306
307 $response = cms_create_response('illegal_param', null, null, $querydata);
308 return false;
309
310 } else if($paramdata[$param]) {
311
312 $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 return cms_perform_get($querydata, $response);
471
472 }
473
474 }
475
476 //----------------------------------------------------------
477
478 function cms_perform_get($querydata, &$response) {
479
480 global $cms_sources;
481
482 $sourcedata = $cms_sources[$querydata['source']];
483 $sourcefields = $sourcedata['fields'];
484
485 for($f = 0; $f < count($querydata['fieldlist']); $f++) {
486
487 if(!isset($sourcefields[$querydata['fieldlist'][$f]])) {
488
489 $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 while(list($key, $sourcedata) = each($cms_sources)) {
523
524 $row = array(
525 'id' => $sourcedata['index'] + 1,
526 'name' => $key,
527 'fieldcount' => count($sourcedata['fields']),
528 );
529
530 $resrow = array();
531 reset($fieldnamelist);
532
533 while(list($i, $fieldname) = each($fieldnamelist))
534 array_push($resrow, $row[$fieldname]);
535
536 array_push($resultlist, $resrow);
537
538 }
539
540 break;
541
542 // By default the db is used as data source:
543 default:
544
545 if($querydata['get_what'] == 'COUNT') {
546
547 $fieldnames = 'COUNT(id)';
548
549 }
550
551 $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);
578
579 //if(!$res) ...
580
581 while($resrow = mysql_fetch_row($res)) array_push($resultlist, $resrow);
582
583 }
584
585 $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_response(
667 $errorkey, $resultlist, $columnnames, $querydata) {
668
669 //------------------
670
671 $i = 0;
672
673 $errors = array(
674 '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 //------------------
722
723 $response = array(
724
725 // Result information:
726 0 => array(
727 'columncount' => count($columnnames),
728 'error' => $errors[$errorkey][0],
729 'errortext' => $errors[$errorkey][1],
730 'firstrow' => $querydata['first'],
731 'get_what' => $querydata['get_what'],
732 'operation' => $querydata['operation'],
733 'query' => $querydata['query'],
734 'rowcount' => count($resultlist),
735 'source' => $querydata['source'],
736 'all' => $querydata
737 ),
738
739 // Result list:
740 1 => $resultlist,
741
742 // Column names:
743 2 => $columnnames
744
745 );
746
747 return $response;
748
749 }
750
751 //------------------------------------------------------------------------------
752
753 ?>

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