Log in with username/password: lyceum/lyceum

root/trunk/src/lib/functions.php

Revision 611, 4.2 kB (checked in by jjb, 4 years ago)

Including userid in key to protect against attacks in cases where the $id input is left at default.

Line 
1 <?php
2 function trace($description, $information, $active=true){
3     if ($active && TRACE){
4         $time = date('i:s');
5         $web = ini_get('display_errors');
6         if ($web)
7             echo '<hr/>'.$time.' - <strong>'.$description.':</strong><br/><pre>'.$information.'</pre><hr/>';
8         else {
9             $message = "\n".$time." - [$description]: $information";
10             error_log($message, 3, TRACELOG);
11         }
12     }
13 }
14
15 function assert_handler($file, $line, $code){
16     //TODO log error in log file when assertion fails
17     echo "
18         <hr>Assertion Failed<br/>
19         <u><b>File:</u></b> $file<br />
20         <u><b>Line:</u></b> $line<br />
21         <u><b>Code:</u></b> $code<br /><hr />";
22 }
23
24 function array_print($array, $prefix=''){
25     if (ini_get('display_errors'))
26         $break = '<br/>';
27     else
28         $break = "\n";
29     $out = "array:$break";
30     foreach($array as $key => $value){
31         $text = is_array($value) ? array_print($value, $prefix.'    ') : $value;
32         $out.=$prefix.$key.'->'.$text.$break;
33     }
34     return $out;
35 }
36
37 function hv($thing){//"has value"
38     return ( isset($thing) && (''!=$thing) );
39 }
40
41 function get_metavalues($user_id){
42     global $wpdb, $blog;
43     $blog_metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE blog = '$blog' AND user_id = '$user_id'");
44     $Lyceum_metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE meta_domain = 'system' AND user_id = '$user_id'");
45 //    trace('$blog_metavalues', array_print($blog_metavalues));
46 //    trace('$Lyceum_metavalues', array_print($Lyceum_metavalues));
47     arrayify($blog_metavalues);   
48     arrayify($Lyceum_metavalues);
49     $metavalues = array_merge($blog_metavalues, $Lyceum_metavalues);
50     return $metavalues;
51 }
52
53 function check_admin_ip(){
54     global $wpdb;
55     $ips = $wpdb->get_col("SELECT `option_value` FROM `$wpdb->options` WHERE `option_name` = 'admin_ip'");
56     return array_search($_SERVER['REMOTE_ADDR'], $ips);
57 }
58
59 function comments_post_name(){
60     global $wpdb;
61     return $wpdb->get_var("SELECT `option_value` FROM `$wpdb->options` WHERE `option_name`='wp-comments-post_hash' AND `option_domain`='system'");
62 }
63
64 function delete_dir($dir) {
65     if(!$dh = @opendir($dir))
66         return;
67     while (($obj = readdir($dh))) {
68         if($obj=='.' || $obj=='..')
69             continue;
70         if (!@unlink($dir.'/'.$obj)) {
71             delete_dir($dir.'/'.$obj);
72             } else {
73                 $file_deleted++;
74             }
75         }
76         if (@rmdir($dir))
77             $dir_deleted++;
78 }
79
80 function empty_dir($dir){
81     if(!$dh = @opendir($dir))
82         return;
83     while (($obj = readdir($dh))) {
84         if($obj=='.' || $obj=='..')
85             continue;
86         if (!@unlink($dir.'/'.$obj)) {
87             delete_dir($dir.'/'.$obj);
88             } else {
89                 $file_deleted++;
90             }
91         }
92 }
93
94 function arrayify(&$thing){
95     $thing = $thing ? $thing : array();
96 }
97
98 function bloginput(){
99     global $blogdata;
100     return "<input type=\"hidden\" name=\"b\" value=\"$blogdata->id\" />";
101 }
102
103 function tokeninput($targetscript, $action='default', $id=0){
104     $token = formtoken($targetscript, $action, $id);
105     return "<input type=\"hidden\" name=\"token\" value=\"$token\" />";
106 }
107
108 function formtoken($targetscript, $action='default', $id=0) {
109     global $userdata;
110     $name = "$targetscript-$action-$id";
111     arrayify($_SESSION['formtokens']);                // This could be done better -jjb
112     $token = sha1(uniqid(rand(), TRUE));
113     $key = sha1($targetscript.$action.$id.$userdata->ID);
114     $_SESSION['formtokens'][$key][$token] = time();
115 //trace('formtoken: $token, $targetscript, $action, $id', "$token, $targetscript, $action, $id");
116     return $token;
117 }
118
119 function tokenIsValid ($token, $targetscript, $action='default', $id=0) {
120 //trace('tokenIsValid: $token, $targetscript, $action, $id', "$token, $targetscript, $action, $id");
121     global $userdata;
122     $key = sha1($targetscript.$action.$id.$userdata->ID);
123     if (!array_key_exists($token, $_SESSION['formtokens'][$key]))
124         return false;
125
126     $token_age = time() - $_SESSION['formtokens'][$key][$token];
127
128     if ($token_age >= 3600)//seconds
129         return false;
130
131     unset($_SESSION['formtokens'][$key][$token]);//not sure if this is really achieving what i want -jjb
132     return true;
133 }
134
135 function validateToken( $token, $targetscript, $action='default', $id=0 ){
136     if (!tokenIsValid( $token, $targetscript, $action, $id ))
137         badTokenMessage();
138 }
139
140 function badTokenMessage(){
141     die("Your tokens have timed out. <a href='{$_SERVER['HTTP_REFERER']}'>Reload</a> the page to reset your tokens.");   
142 }
143
144 ?>
145
Note: See TracBrowser for help on using the browser.
Log in with username/password: lyceum/lyceum