public
Description: Git mirror of the CMS Made Simple 2.0 rewrite
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsmadesimple-2-0.git
cmsmadesimple-2-0 / lib / page.functions.php
100644 788 lines (678 sloc) 20.009 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; -*-
#CMS - CMS Made Simple
#(c)2004-2007 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#$Id$
 
/**
* Page related functions. Generally these are functions not necessarily
* related to content, but more to the underlying mechanisms of the system.
*
* @package CMS
*/
/**
* Checks to see if the user is logged in. If not, redirects the browser
* to the admin login.
*
* @since 0.1
* @param string no_redirect - If true, then don't redirect if not logged in
* @return bool If they're logged in, true. If not logged in, false.
*/
function check_login($no_redirect = false)
{
  return CmsLogin::check_login($no_redirect);
}
 
/**
* Gets the userid of the currently logged in user.
*
* @returns If they're logged in, the user id. If not logged in, false.
* @since 0.1
*/
function get_userid($check = true)
{
  return CmsLogin::get_userid($check);
}
 
function check_passhash($userid, $checksum)
{
  return CmsLogin::check_passhash($userid, $checksum);
}
 
/**
* Regenerates the user session information from a userid. This is basically used
* so that if the session expires, but the cookie still remains (site is left along
* for 20+ minutes with no interaction), the user won't have to relogin to regenerate
* the details.
*
* @since 0.5
*/
function generate_user_object($userid)
{
  return CmsLogin::generate_user_object($userid);
}
 
/**
* Loads all permissions for a particular user into a global variable so we don't hit the db for every one.
*
* @since 0.8
* @deprecated Use the new permissions system
*/
function load_all_permissions($userid)
{
  $perms = array();
  
  global $gCms;
  $db = cms_db();
  $variables = &$gCms->variables;
 
  try
  {
    $query = "SELECT DISTINCT permission_name FROM ".cms_db_prefix()."user_groups ug INNER JOIN ".cms_db_prefix()."group_perms gp ON gp.group_id = ug.group_id INNER JOIN ".cms_db_prefix()."permissions p ON p.permission_id = gp.permission_id WHERE ug.user_id = ?";
    $result = &$db->Execute($query, array($userid));
    while ($result && !$result->EOF)
    {
      $perms[] =& $result->fields['permission_name'];
      $result->MoveNext();
    }
  
    if ($result) $result->Close();
  }
  catch (exception $e)
  {
    
  }
 
  $variables['userperms'] = $perms;
}
 
/**
* Checks to see that the given userid has access to
* the given permission.
*
* @returns mixed If they have perimission, true. If they do not, false.
* @since 0.1
*/
function check_permission($userid, $permname)
{
  $check = false;
 
  global $gCms;
 
  if (!isset($gCms->variables['userperms']))
  {
    load_all_permissions($userid);
  }
 
  if (isset($gCms->variables['userperms']))
  {
    if (in_array($permname, $gCms->variables['userperms']) || $userid == 1 )
    {
      $check = true;
    }
  }
 
  return $check;
}
 
/**
* Checks that the given userid is the owner of the given contentid.
*
* @returns mixed If they have ownership, true. If they do not, false.
* @since 0.1
*/
function check_ownership($userid, $contentid = '')
{
  $check = false;
  global $gCms;
 
  if (!isset($gCms->variables['ownerpages']))
  {
    $db =& $gCms->GetDb();
 
    $variables = &$gCms->variables;
    $variables['ownerpages'] = array();
 
    $query = "SELECT id FROM ".cms_db_prefix()."content WHERE owner_id = ?";
    $result = &$db->Execute($query, array($userid));
 
    while ($result && !$result->EOF)
    {
      $variables['ownerpages'][] =& $result->fields['id'];
      $result->MoveNext();
    }
    
    if ($result) $result->Close();
  }
 
  if (isset($gCms->variables['ownerpages']))
  {
    if (in_array($contentid, $gCms->variables['ownerpages']))
    {
      $check = true;
    }
  }
 
  return $check;
}
 
/**
* Checks that the given userid has access to modify the given
* pageid. This would mean that they were set as additional
* authors/editors by the owner.
*
* @returns mixed If they have authorship, true. If they do not, false.
* @since 0.2
*/
function check_authorship($userid, $contentid = '')
{
  $check = false;
  global $gCms;
 
  if (!isset($gCms->variables['authorpages']))
  {
    $db =& $gCms->GetDb();
 
    $variables = &$gCms->variables;
    $variables['authorpages'] = array();
 
    $query = "SELECT content_id FROM ".cms_db_prefix()."additional_users WHERE user_id = ?";
    $result = &$db->Execute($query, array($userid));
 
    while ($result && !$result->EOF)
    {
      $variables['authorpages'][] =& $result->fields['content_id'];
      $result->MoveNext();
    }
    
    if ($result) $result->Close();
  }
 
  if (isset($gCms->variables['authorpages']))
  {
    if (in_array($contentid, $gCms->variables['authorpages']))
    {
      $check = true;
    }
  }
 
  return $check;
}
 
/**
* Prepares an array with the list of the pages $userid is an author of
*
* @returns an array in whose elements are the IDs of the pages
* @since 0.11
*/
function author_pages($userid)
{
  global $gCms;
  $db = cms_db();
    $variables = &$gCms->variables;
  if (!isset($variables['authorpages']))
  {
    $db = cms_db();
    $variables['authorpages'] = array();
    
    $query = "SELECT id FROM ".cms_db_prefix()."content WHERE owner_id = " . $userid;
    $result =& $db->Execute($query);
    
    while ($result && !$result->EOF)
    {
      $variables['authorpages'][] =& $result->fields['id'];
      $result->MoveNext();
    }
    
    if ($result) $result->Close();
 
    $query = "SELECT content_id FROM ".cms_db_prefix()."additional_users WHERE user_id = ?";
    $result = &$db->Execute($query, array($userid));
 
    while ($result && !$result->EOF)
    {
      $variables['authorpages'][] =& $result->fields['content_id'];
      $result->MoveNext();
    }
    
    if ($result) $result->Close();
  }
 
  return $variables['authorpages'];
}
 
/**
* Quickly checks that the given userid has access to modify the given
* pageid. This would mean that they were set as additional
* authors/editors by the owner.
*
* @returns mixed If they have authorship, true. If they do not, false.
* @since 0.11
*/
function quick_check_authorship($contentid, $hispages)
{
  $check = false;
 
  if (in_array($contentid, $hispages))
  {
    $check = true;
  }
 
  return $check;
}
 
/**
* Put an event into the audit (admin) log. This should be
* done on most admin events for consistency.
*
* @since 0.3
*/
function audit($itemid, $itemname, $action)
{
  global $gCms;
  $db = $gCms->GetDb();
 
  $userid = 0;
  $username = '';
 
  if (isset($_SESSION["cmsms_user_id"]))
  {
    $userid = $_SESSION["cmsms_user_id"];
  }
  else
  {
   if (isset($_SESSION['login_user_id']))
   {
    $userid = $_SESSION['login_user_id'];
    $username = $_SESSION['login_user_username'];
   }
  }
 
  if (isset($_SESSION["cms_admin_username"]))
  {
    $username = $_SESSION["cms_admin_username"];
  }
 
  if (!isset($userid) || $userid == "") {
    $userid = 0;
  }
 
  $query = "INSERT INTO ".cms_db_prefix()."adminlog (timestamp, user_id, username, item_id, item_name, action) VALUES (?,?,?,?,?,?)";
  $db->Execute($query,array(time(),$userid,$username,$itemid,$itemname,$action));
}
 
/**
* Gets the given site prefernce
*
* @since 0.6
*/
function get_site_preference($prefname, $defaultvalue = '')
{
  return CmsApplication::get_preference($prefname, $defaultvalue);
}
 
/**
* Removes the given site preference
*
* @param string Preference name to remove
*/
function remove_site_preference($prefname,$regexp=false)
{
  CmsApplication::remove_preference($prefname);
}
 
/**
* Sets the given site perference with the given value.
*
* @since 0.6
*/
function set_site_preference($prefname, $value)
{
  CmsApplication::set_preference($prefname, $value);
}
 
function load_all_preferences($userid)
{
  global $gCms;
  $db = cms_db();
  
  $variables = array();
 
  $query = 'SELECT preference, value FROM '.cms_db_prefix().'userprefs WHERE user_id = ?';
  $result = &$db->Execute($query, array($userid));
 
  while ($result && !$result->EOF)
  {
    $variables[$result->fields['preference']] = $result->fields['value'];
    $result->MoveNext();
  }
 
  if ($result) $result->Close();
  
  return $variables;
}
 
/**
* Gets the given preference for the given userid.
*
* @since 0.3
*/
function get_preference($userid, $prefname, $default='')
{
  global $gCms;
  $db = cms_db();
 
  $userprefs = $gCms->userprefs;
  $result = $default;
 
  if (!isset($userprefs))
  {
    $userprefs = load_all_preferences($userid);
    $gCms->userprefs = $userprefs;
  }
 
  if (isset($userprefs[$prefname]))
  {
    $result = $userprefs[$prefname];
  }
 
  return $result;
}
 
/**
* Sets the given perference for the given userid with the given value.
*
* @since 0.3
*/
function set_preference($userid, $prefname, $value)
{
  $doinsert = true;
 
  global $gCms;
  $db =& $gCms->GetDb();
 
  $userprefs = &$gCms->userprefs;
  $userprefs[$prefname] = $value;
 
  $query = "SELECT value from ".cms_db_prefix()."userprefs WHERE user_id = ? AND preference = ?";
  $result = $db->Execute($query, array($userid, $prefname));
 
  if ($result && $result->RecordCount() > 0)
  {
    $doinsert = false;
  }
  
  if ($result) $result->Close();
 
  if ($doinsert)
  {
    $query = "INSERT INTO ".cms_db_prefix()."userprefs (user_id, preference, value) VALUES (?,?,?)";
    $db->Execute($query, array($userid, $prefname, $value));
  }
  else
  {
    $query = "UPDATE ".cms_db_prefix()."userprefs SET value = ? WHERE user_id = ? AND preference = ?";
    $db->Execute($query, array($value, $userid, $prefname));
  }
}
 
/**
* Returns the stylesheet for the given templateid. Returns a hash with encoding and stylesheet entries.
*
* @since 0.1
*/
function get_stylesheet($template_id, $media_type = '')
{
  $result = array();
  $css = "";
 
  global $gCms;
  $db = cms_db();
  $templateops = $gCms->GetTemplateOperations();
 
  $templateobj = FALSE;
 
  #Grab template id and make sure it's actually "somewhat" valid
  if (isset($template_id) && is_numeric($template_id) && $template_id > -1)
  {
    #Ok, it's valid, let's load the bugger
    $templateobj =& $templateops->LoadTemplateById($template_id);
  }
 
  #If it's valid after loading, then start the process...
  if ($templateobj !== FALSE && ($templateobj->active == '1' || $templateobj->active == TRUE) )
  {
    #Grab the encoding
    if ($templateobj->encoding !== FALSE && $templateobj->encoding != '')
    {
      $result['encoding'] = $templateobj->encoding;
    }
    else
    {
      $result['encoding'] = get_encoding();
    }
 
    #Load in the "standard" template CSS if media type is empty
    if ($media_type == '')
    {
      if (isset($templateobj->stylesheet) && $templateobj->stylesheet != '')
      {
        $css .= $templateobj->stylesheet;
      }
    }
 
    #Handle "advanced" CSS Management
    $cssquery = "SELECT css_text FROM ".cms_db_prefix()."css c, ".cms_db_prefix()."css_assoc ca
      WHERE  id    = assoc_css_id
      AND    assoc_type  = 'template'
      AND    assoc_to_id = ?
      AND    c.media_type = ? ORDER BY ca.create_date";
    $cssresult =& $db->Execute($cssquery, array($template_id, $media_type));
 
    while ($cssresult && $cssline = $cssresult->FetchRow())
    {
      $css .= "\n".$cssline['css_text']."\n";
    }
    
    if ($cssresult) $cssresult->Close();
  }
  else
  {
    $result['nostylesheet'] = true;
    $result['encoding'] = get_encoding();
  }
 
  #$css = preg_replace("/[\r\n]/", "", $css); //hack for tinymce
  $result['stylesheet'] = $css;
 
  return $result;
}
 
function get_stylesheet_media_types($template_id)
{
  $result = array();
 
  global $gCms;
  $db =& $gCms->GetDb();
  $templateops =& $gCms->GetTemplateOperations();
 
  $templateobj = FALSE;
 
  #Grab template id and make sure it's actually "somewhat" valid
  if (isset($template_id) && is_numeric($template_id) && $template_id > -1)
  {
    #Ok, it's valid, let's load the bugger
    $templateobj = $templateops->LoadTemplateById($template_id);
    if (isset($templateobj->stylesheet) && $templateobj->stylesheet != '')
    {
      $result[] = '';
    }
  }
 
  #If it's valid after loading, then start the process...
  if ($templateobj !== FALSE && ($templateobj->active == '1' || $templateobj->active == TRUE) )
  {
    #Handle "advanced" CSS Management
    $cssquery = "SELECT DISTINCT media_type FROM ".cms_db_prefix()."css c, ".cms_db_prefix()."css_assoc
      WHERE  id    = assoc_css_id
      AND    assoc_type  = 'template'
      AND    assoc_to_id = ?";
    $cssresult = &$db->Execute($cssquery, array($template_id));
 
    while ($cssresult && !$cssresult->EOF)
    {
      if (!in_array($cssresult->fields['media_type'], $result))
        $result[] =& $cssresult->fields['media_type'];
      $cssresult->MoveNext();
    }
    
    if ($cssresult) $cssresult->Close();
  }
 
  return $result;
}
 
/**
* Strips slashes from an array of values.
*/
function & stripslashes_deep(&$value)
{
        if (is_array($value))
        {
                $value = array_map('stripslashes_deep', $value);
        }
        elseif (!empty($value) && is_string($value))
        {
                $value = stripslashes($value);
        }
        return $value;
}
  
function create_textarea($enablewysiwyg, $text, $name, $classname='', $id='', $encoding='', $stylesheet='', $width='80', $height='15',$forcewysiwyg='',$wantedsyntax='')
{
  global $gCms;
  $result = '';
 
  if ($enablewysiwyg == true)
  {
    reset($gCms->modules);
    while (list($key) = each($gCms->modules))
    {
      $value =& $gCms->modules[$key];
      if ($gCms->modules[$key]['installed'] == true && //is the module installed?
        $gCms->modules[$key]['active'] == true &&       //us the module active?
        $gCms->modules[$key]['object']->IsWYSIWYG()) //is it a wysiwyg module?
      {
        if ($forcewysiwyg=='') {
          //get_preference(get_userid(), 'wysiwyg')!="" && //not needed as it won't match the wisiwyg anyway
          if ($gCms->modules[$key]['object']->GetName()==get_preference(get_userid(false), 'wysiwyg')) {
            $result=$gCms->modules[$key]['object']->WYSIWYGTextarea($name,$width,$height,$encoding,$text,$stylesheet);
          }
        } else {
          if ($gCms->modules[$key]['object']->GetName()==$forcewysiwyg) {
        &nbs