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
Search Repo:
cmsmadesimple-2-0 / lib / page.functions.php
100644 326 lines (288 sloc) 8.649 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
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; -*-
#CMS - CMS Made Simple
#(c)2004-2008 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);
}
 
/**
* 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);
}
 
function check_permission($userid, $permname)
{
  return CmsAcl::check_core_permission($permname, $userid);
}
 
 
/**
* 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));
}
 
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));
  }
}
  
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']->is_wysiwyg()) //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']->get_name()==get_preference(get_userid(false), 'wysiwyg')) {
            $result=$gCms->modules[$key]['object']->wysiwyg_textarea($name,$width,$height,$encoding,$text,$stylesheet);
          }
        } else {
          if ($gCms->modules[$key]['object']->get_name()==$forcewysiwyg) {
            $result=$gCms->modules[$key]['object']->wysiwyg_textarea($name,$width,$height,$encoding,$text,$stylesheet);
          }
        }
      }
    }
  }
  
  if (($result=="") && ($wantedsyntax!=''))
  {  
    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']->IsSyntaxHighlighter()) //is it a syntaxhighlighter module 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), 'syntaxhighlighter')) {
            $result=$gCms->modules[$key]['object']->SyntaxTextarea($name,$wantedsyntax,$width,$height,$encoding,$text);
          }
        } else {
          if ($gCms->modules[$key]['object']->GetName()==$forcewysiwyg) {
            $result=$gCms->modules[$key]['object']->SyntaxTextarea($name,$wantedsyntax,$width,$height,$encoding,$text);
          }
        }
      }
    }
  }
 
  if ($result == '')
  {
    $result = '<textarea name="'.$name.'" cols="'.$width.'" rows="'.$height.'"';
    if ($classname != '')
    {
      $result .= ' class="'.$classname.'"';
    }
    if ($id != '')
    {
      $result .= ' id="'.$id.'"';
    }
    $result .= '>'.cms_htmlentities($text,ENT_NOQUOTES,CmsResponse::get_encoding()).'</textarea>';
  }
 
  return $result;
}
 
/**
* Creates a string containing links to all the pages.
* @param page - the current page to display
* @param totalrows - the amount of items being listed
* @param limit - the amount of items to list per page
* @return a string containing links to all the pages (ex. next 1,2 prev)
*/
function pagination($page, $totalrows, $limit)
{
  $page_string = "";
  $from = ($page * $limit) - $limit;
  $numofpages = $totalrows / $limit;
  if ($numofpages > 1)
  {
    if($page != 1)
    {
      $pageprev = $page-1;
      $page_string .= "<a href=\"".$_SERVER['PHP_SELF']."?page=$pageprev\">".lang('previous')."</a>&nbsp;";
    }
    else
    {
      $page_string .= lang('previous')." ";
    }
    for($i = 1; $i <= $numofpages; $i++)
    {
      if($i == $page)
      {
        $page_string .= $i."&nbsp;";
      }
      else
      {
        $page_string .= "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a>&nbsp;";
      }
    }
 
    if(($totalrows % $limit) != 0)
    {
      if($i == $page)
      {
        $page_string .= $i."&nbsp;";
      }
      else
      {
        $page_string .= "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a>&nbsp;";
      }
    }
 
    if(($totalrows - ($limit * $page)) > 0)
    {
      $pagenext = $page+1;
      $page_string .= "<a href=\"".$_SERVER['PHP_SELF']."?page=$pagenext\">".lang('next')."</a>";
    }
    else
    {
      $page_string .= lang('next')." ";
    }
  }
  return $page_string;
}
 
# vim:ts=4 sw=4 noet
?>