CWIS Developer Documentation
SPTUser.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # FILE: SPT--SPTUser.php
5 #
6 # METHODS PROVIDED:
7 # SPTUser()
8 # - constructor
9 # SomeMethod($SomeParameter, $AnotherParameter)
10 # - short description of method
11 #
12 # AUTHOR:
13 #
14 # Part of the Scout Portal Toolkit
15 # Copyright 2004 Internet Scout Project
16 # http://scout.wisc.edu
17 #
18 
19 class SPTUser extends User {
20  # ---- PUBLIC INTERFACE --------------------------------------------------
21  # ---- user interface preference mnemonics
22  # color avoidance flags
23  const UIPREF_AVOID_RED = 1;
27  const UIPREF_AVOID_ORANGE = 16;
31 
32  # content display options
36 
37  # content view options
41 
42  # audio description options
46 
47  # caption type options
51 
52  # object constructor
53  function SPTUser($UserInfo = NULL)
54  {
55  # call parent constructor
56  $this->User($UserInfo);
57 
58  # if user is logged in
59  if ($this->IsLoggedIn())
60  {
61  # if user already has a UI preferences record in DB
62  $this->DB->Query("SELECT * FROM UserUIPreferences"
63  ." WHERE UserId = '".$this->Id()."'");
64  if ($this->DB->NumRowsSelected())
65  {
66  # load in UI preferences
67  $this->UserUIPreferencesCache = $this->DB->FetchRow();
68  }
69  else
70  {
71  # add UI preferences record to DB for user
72  $this->DB->Query("INSERT INTO UserUIPreferences"
73  ." (UserId) VALUES (".$this->Id().")");
74  }
75  }
76  }
77 
78  static function EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
79  {
80  # look for "From" address in supplied headers
81  if (strlen($AdditionalHeaders))
82  {
83  $HeaderLines = explode("\n", $AdditionalHeaders);
84  $Headers = array();
85  foreach ($HeaderLines as $Line)
86  {
87  $HeaderLine = trim($Line);
88  if (preg_match("/^from:/i", $Line))
89  {
90  $From = preg_replace("/^from:/i", "", $Line);
91  }
92  else
93  {
94  $Headers[] = $HeaderLine;
95  }
96  }
97  }
98 
99  # use default system admin address if no "From" address found
100  if (!isset($From))
101  {
102  $From = trim($GLOBALS["G_SysConfig"]->PortalName())
103  ." <".trim($GLOBALS["G_SysConfig"]->AdminEmail()).">";
104  }
105 
106  # send message
107  $Msg = new Email();
108  $Msg->From($From);
109  $Msg->To($To);
110  $Msg->Subject($Subject);
111  $Msg->Body($Message);
112  $Result = $Msg->Send();
113 
114  # report success to caller
115  return $Result;
116  }
117 
118  # user interface / accessibility preferences
119  function PrefFontSize($NewValue = DB_NOVALUE)
120  { return $this->UUPUpdateValue("FontSize", $NewValue); }
121  function PrefFontTypeFace($NewValue = DB_NOVALUE)
122  { return $this->UUPUpdateValue("FontTypeFace", $NewValue); }
123  function PrefFontColor($NewValue = DB_NOVALUE)
124  { return $this->UUPUpdateValue("FontColor", $NewValue); }
125  function PrefBackgroundColor($NewValue = DB_NOVALUE)
126  { return $this->UUPUpdateValue("BackgroundColor", $NewValue); }
127  function PrefColorAvoidanceFlags($NewValue = DB_NOVALUE)
128  { return $this->UUPUpdateValue("ColorAvoidanceFlags", $NewValue); }
129  function PrefContentDensity($NewValue = DB_NOVALUE)
130  { return $this->UUPUpdateValue("ContentDensity", $NewValue); }
131  function PrefContentView($NewValue = DB_NOVALUE)
132  { return $this->UUPUpdateValue("ContentView", $NewValue); }
133  function PrefAudioDescriptionLevel($NewValue = DB_NOVALUE)
134  { return $this->UUPUpdateValue("AudioDescriptionLevel", $NewValue); }
136  { return $this->UUPUpdateValue("AudioDescriptionLanguage", $NewValue); }
138  { return $this->UUPUpdateValue("VisualDescriptionLanguage", $NewValue); }
140  { return $this->UUPUpdateValue("ImageDescriptionLanguage", $NewValue); }
142  { return $this->UUPUpdateValue("UseGraphicAlternatives", $NewValue); }
143  function PrefSignLanguage($NewValue = DB_NOVALUE)
144  { return $this->UUPUpdateValue("SignLanguage", $NewValue); }
145  function PrefCaptionType($NewValue = DB_NOVALUE)
146  { return $this->UUPUpdateValue("CaptionType", $NewValue); }
147  function PrefCaptionRate($NewValue = DB_NOVALUE)
148  { return $this->UUPUpdateValue("CaptionRate", $NewValue); }
149 
159  static function GetCryptKey()
160  {
161  $DB = new Database();
162 
163  # Clear all keys more than two days old
164  $DB->Query("DELETE FROM LoginKeys WHERE NOW() - CreationTime > 172800");
165  $DB->Query("DELETE FROM UsedLoginTokens WHERE NOW()-KeyCTime > 172800");
166 
167  # Get the most recently generated key
168  $DB->Query("SELECT NOW()-CreationTime as Age,"
169  ."KeyPair FROM LoginKeys "
170  ."ORDER BY Age ASC LIMIT 1");
171  $Row = $DB->FetchRow();
172 
173  # If there is no key in the database, or the key is too old
174  if ( ($Row===FALSE) || ($Row["Age"]>=86400) )
175  {
176  # Generate a new OpenSSL format keypair
177  $KeyPair = openssl_pkey_new(
178  array(
179  'private_key_bits' => 512, # Make this a Sysadmin pref later?
180  'private_key_type' => OPENSSL_KEYTYPE_RSA
181  ));
182 
183  # Serialize it for storage
184  openssl_pkey_export($KeyPair, $KeyPairDBFormat);
185 
186  # And stick it into the database
187  $DB->Query("INSERT INTO LoginKeys "
188  ."(KeyPair, CreationTime) VALUES ("
189  ."\"".addslashes($KeyPairDBFormat)."\","
190  ."NOW())");
191  }
192  else
193  {
194  # If we do have a current key in the database,
195  # Convert it to openssl format for usage
196  $KeyPair = openssl_pkey_get_private( $Row["KeyPair"] );
197  }
198 
199  return $KeyPair;
200  }
201 
208  static function ExtractPubKeyParameters($KeyPair)
209  {
210  # Export the keypair as an ASCII signing request (which contains the data we want)
211  openssl_csr_export(openssl_csr_new(array(), $KeyPair), $Export, false);
212 
213  $Modulus = "";
214  $Exponent = "";
215 
216  $Patterns = array(
217  '/Modulus \([0-9]+ bit\):(.*)Exponent: [0-9]+ \(0x([0-9a-f]+)\)/ms',
218  '/Public-Key: \([0-9]+ bit\).*Modulus:(.*)Exponent: [0-9]+ \(0x([0-9a-f]+)\)/ms',
219  );
220 
221  foreach ($Patterns as $Pattern)
222  {
223  if (preg_match($Pattern, $Export, $Matches))
224  {
225  $Modulus = $Matches[1];
226  $Exponent = $Matches[2];
227  break;
228  }
229  }
230 
231  # Clean newlines and whitespace out of the modulus
232  $Modulus = preg_replace("/[^0-9a-f]/","",$Modulus);
233 
234  # Return key material
235  return array( "Modulus" => $Modulus, "Exponent" => $Exponent );
236  }
237 
238  # ---- PRIVATE INTERFACE -------------------------------------------------
239 
241 
242  function UUPUpdateValue($FieldName, $NewValue)
243  {
244  return $this->DB->UpdateValue("UserUIPreferences", $FieldName,
245  $NewValue, "UserId = '".$this->Id()."'",
246  $this->UserUIPreferencesCache);
247  }
248 }
249 
250 ?>