CWIS Developer Documentation
SPTUser.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # FILE: SPT--SPTUser.php
5 #
6 # Part of the Collection Workflow Integration System (CWIS)
7 # Copyright 2004-2013 Edward Almasy and Internet Scout Research Group
8 # http://scout.wisc.edu/cwis/
9 #
10 
11 class SPTUser extends CWUser {
12  # ---- PUBLIC INTERFACE --------------------------------------------------
13  # ---- user interface preference mnemonics
14  # color avoidance flags
15  const UIPREF_AVOID_RED = 1;
19  const UIPREF_AVOID_ORANGE = 16;
23 
24  # content display options
28 
29  # content view options
33 
34  # audio description options
38 
39  # caption type options
43 
44  # object constructor
45  function SPTUser($UserInfo = NULL)
46  {
47  # call parent constructor
48  $this->User($UserInfo);
49 
50  # if user is logged in
51  if ($this->IsLoggedIn())
52  {
53  # if user already has a UI preferences record in DB
54  $this->DB->Query("SELECT * FROM UserUIPreferences"
55  ." WHERE UserId = '".$this->Id()."'");
56  if ($this->DB->NumRowsSelected())
57  {
58  # load in UI preferences
59  $this->UserUIPreferencesCache = $this->DB->FetchRow();
60  }
61  else
62  {
63  # add UI preferences record to DB for user
64  $this->DB->Query("INSERT INTO UserUIPreferences"
65  ." (UserId) VALUES (".$this->Id().")");
66  }
67  }
68  }
69 
70  # user interface / accessibility preferences
71  function PrefFontSize($NewValue = DB_NOVALUE)
72  { return $this->UUPUpdateValue("FontSize", $NewValue); }
73  function PrefFontTypeFace($NewValue = DB_NOVALUE)
74  { return $this->UUPUpdateValue("FontTypeFace", $NewValue); }
75  function PrefFontColor($NewValue = DB_NOVALUE)
76  { return $this->UUPUpdateValue("FontColor", $NewValue); }
77  function PrefBackgroundColor($NewValue = DB_NOVALUE)
78  { return $this->UUPUpdateValue("BackgroundColor", $NewValue); }
79  function PrefColorAvoidanceFlags($NewValue = DB_NOVALUE)
80  { return $this->UUPUpdateValue("ColorAvoidanceFlags", $NewValue); }
81  function PrefContentDensity($NewValue = DB_NOVALUE)
82  { return $this->UUPUpdateValue("ContentDensity", $NewValue); }
83  function PrefContentView($NewValue = DB_NOVALUE)
84  { return $this->UUPUpdateValue("ContentView", $NewValue); }
85  function PrefAudioDescriptionLevel($NewValue = DB_NOVALUE)
86  { return $this->UUPUpdateValue("AudioDescriptionLevel", $NewValue); }
88  { return $this->UUPUpdateValue("AudioDescriptionLanguage", $NewValue); }
90  { return $this->UUPUpdateValue("VisualDescriptionLanguage", $NewValue); }
92  { return $this->UUPUpdateValue("ImageDescriptionLanguage", $NewValue); }
93  function PrefUseGraphicAlternatives($NewValue = DB_NOVALUE)
94  { return $this->UUPUpdateValue("UseGraphicAlternatives", $NewValue); }
95  function PrefSignLanguage($NewValue = DB_NOVALUE)
96  { return $this->UUPUpdateValue("SignLanguage", $NewValue); }
97  function PrefCaptionType($NewValue = DB_NOVALUE)
98  { return $this->UUPUpdateValue("CaptionType", $NewValue); }
99  function PrefCaptionRate($NewValue = DB_NOVALUE)
100  { return $this->UUPUpdateValue("CaptionRate", $NewValue); }
101 
111  static function GetCryptKey()
112  {
113  $DB = new Database();
114 
115  # Clear all keys more than two days old
116  $DB->Query("DELETE FROM LoginKeys WHERE NOW() - CreationTime > 172800");
117  $DB->Query("DELETE FROM UsedLoginTokens WHERE NOW()-KeyCTime > 172800");
118 
119  # Get the most recently generated key
120  $DB->Query("SELECT NOW()-CreationTime as Age,"
121  ."KeyPair FROM LoginKeys "
122  ."ORDER BY Age ASC LIMIT 1");
123  $Row = $DB->FetchRow();
124 
125  # If there is no key in the database, or the key is too old
126  if ( ($Row===FALSE) || ($Row["Age"]>=86400) )
127  {
128  # Generate a new OpenSSL format keypair
129  $KeyPair = openssl_pkey_new(
130  array(
131  'private_key_bits' => 512, # Make this a Sysadmin pref later?
132  'private_key_type' => OPENSSL_KEYTYPE_RSA
133  ));
134 
135  # Serialize it for storage
136  openssl_pkey_export($KeyPair, $KeyPairDBFormat);
137 
138  # And stick it into the database
139  $DB->Query("INSERT INTO LoginKeys "
140  ."(KeyPair, CreationTime) VALUES ("
141  ."\"".addslashes($KeyPairDBFormat)."\","
142  ."NOW())");
143  }
144  else
145  {
146  # If we do have a current key in the database,
147  # Convert it to openssl format for usage
148  $KeyPair = openssl_pkey_get_private( $Row["KeyPair"] );
149  }
150 
151  return $KeyPair;
152  }
153 
160  static function ExtractPubKeyParameters($KeyPair)
161  {
162  # Export the keypair as an ASCII signing request (which contains the data we want)
163  openssl_csr_export(openssl_csr_new(array(), $KeyPair), $Export, false);
164 
165  $Modulus = "";
166  $Exponent = "";
167 
168  $Patterns = array(
169  '/Modulus \([0-9]+ bit\):(.*)Exponent: [0-9]+ \(0x([0-9a-f]+)\)/ms',
170  '/Public-Key: \([0-9]+ bit\).*Modulus:(.*)Exponent: [0-9]+ \(0x([0-9a-f]+)\)/ms',
171  );
172 
173  foreach ($Patterns as $Pattern)
174  {
175  if (preg_match($Pattern, $Export, $Matches))
176  {
177  $Modulus = $Matches[1];
178  $Exponent = $Matches[2];
179  break;
180  }
181  }
182 
183  # Clean newlines and whitespace out of the modulus
184  $Modulus = preg_replace("/[^0-9a-f]/","",$Modulus);
185 
186  # Return key material
187  return array( "Modulus" => $Modulus, "Exponent" => $Exponent );
188  }
189 
190  # ---- PRIVATE INTERFACE -------------------------------------------------
191 
193 
194  function UUPUpdateValue($FieldName, $NewValue)
195  {
196  return $this->DB->UpdateValue("UserUIPreferences", $FieldName,
197  $NewValue, "UserId = '".$this->Id()."'",
198  $this->UserUIPreferencesCache);
199  }
200 }
201 
202 ?>
PrefUseGraphicAlternatives($NewValue=DB_NOVALUE)
Definition: SPTUser.php:93
IsLoggedIn()
Definition: Axis--User.php:431
const UIPREF_AVOID_RED
Definition: SPTUser.php:15
PrefAudioDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: SPTUser.php:87
const UIPREF_CONTENTDENSITY_DETAILED
Definition: SPTUser.php:26
const UIPREF_CONTENTVIEW_NOPREFERENCE
Definition: SPTUser.php:30
PrefSignLanguage($NewValue=DB_NOVALUE)
Definition: SPTUser.php:95
const UIPREF_CONTENTDENSITY_OVERVIEW
Definition: SPTUser.php:27
PrefContentDensity($NewValue=DB_NOVALUE)
Definition: SPTUser.php:81
SQL database abstraction object with smart query caching.
const DB_NOVALUE
const UIPREF_AVOID_ORANGE
Definition: SPTUser.php:19
const UIPREF_CONTENTDENSITY_NOPREFERENCE
Definition: SPTUser.php:25
const UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL
Definition: SPTUser.php:42
const UIPREF_CAPTIONTYPE_VERBATIM
Definition: SPTUser.php:41
const UIPREF_CONTENTVIEW_IMAGEINTENSIVE
Definition: SPTUser.php:32
static ExtractPubKeyParameters($KeyPair)
Extract the modulus and exponent of the public key from an OpenSSL format keypair to send in login fo...
Definition: SPTUser.php:160
User($UserInfoOne=NULL, $UserInfoTwo=NULL)
Definition: Axis--User.php:47
PrefFontColor($NewValue=DB_NOVALUE)
Definition: SPTUser.php:75
const UIPREF_AUDIODESCRIPTION_EXPANDED
Definition: SPTUser.php:37
const UIPREF_CONTENTVIEW_TEXTINTENSIVE
Definition: SPTUser.php:31
PrefBackgroundColor($NewValue=DB_NOVALUE)
Definition: SPTUser.php:77
PHP
Definition: OAIClient.php:39
PrefVisualDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: SPTUser.php:89
const UIPREF_AUDIODESCRIPTION_NONE
Definition: SPTUser.php:35
PrefFontSize($NewValue=DB_NOVALUE)
Definition: SPTUser.php:71
const UIPREF_AVOID_REDBLACK
Definition: SPTUser.php:20
const UIPREF_AVOID_GREENYELLOW
Definition: SPTUser.php:18
SPTUser($UserInfo=NULL)
Definition: SPTUser.php:45
PrefContentView($NewValue=DB_NOVALUE)
Definition: SPTUser.php:83
const UIPREF_AVOID_REDGREEN
Definition: SPTUser.php:16
const UIPREF_AVOID_USEMAXMONOCHR
Definition: SPTUser.php:22
const UIPREF_AVOID_BLUEYELLOW
Definition: SPTUser.php:17
static GetCryptKey()
Get/generate a cryptographic keypair for user login.
Definition: SPTUser.php:111
UUPUpdateValue($FieldName, $NewValue)
Definition: SPTUser.php:194
const UIPREF_AUDIODESCRIPTION_STANDARD
Definition: SPTUser.php:36
PrefImageDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: SPTUser.php:91
$UserUIPreferencesCache
Definition: SPTUser.php:192
const UIPREF_CAPTIONTYPE_NONE
Definition: SPTUser.php:40
PrefColorAvoidanceFlags($NewValue=DB_NOVALUE)
Definition: SPTUser.php:79
PrefCaptionType($NewValue=DB_NOVALUE)
Definition: SPTUser.php:97
PrefCaptionRate($NewValue=DB_NOVALUE)
Definition: SPTUser.php:99
PrefAudioDescriptionLevel($NewValue=DB_NOVALUE)
Definition: SPTUser.php:85
PrefFontTypeFace($NewValue=DB_NOVALUE)
Definition: SPTUser.php:73
CWIS-specific user class.
Definition: CWUser.php:13
const UIPREF_AVOID_PURPLEGREY
Definition: SPTUser.php:21