Completed
Push — developer ( 81429f...31cfdf )
by Błażej
38:40
created
include/Webservices/Query.php 2 patches
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -10,6 +10,9 @@
 block discarded – undo
10 10
 
11 11
 require_once("include/Webservices/QueryParser.php");
12 12
 
13
+/**
14
+ * @param string $q
15
+ */
13 16
 function vtws_query($q, $user)
14 17
 {
15 18
 
Please login to merge, or discard this patch.
Braces   +3 added lines, -2 removed lines patch added patch discarded remove patch
@@ -21,8 +21,9 @@
 block discarded – undo
21 21
 	// Cache the instance for re-use		
22 22
 	$moduleRegex = "/[fF][rR][Oo][Mm]\s+([^\s;]+)/";
23 23
 	$moduleName = '';
24
-	if (preg_match($moduleRegex, $q, $m))
25
-		$moduleName = trim($m[1]);
24
+	if (preg_match($moduleRegex, $q, $m)) {
25
+			$moduleName = trim($m[1]);
26
+	}
26 27
 
27 28
 	if (!isset($vtws_create_cache[$moduleName]['webserviceobject'])) {
28 29
 		$webserviceObject = VtigerWebserviceObject::fromQuery($adb, $q);
Please login to merge, or discard this patch.
include/Webservices/QueryRelated.php 2 patches
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -12,6 +12,9 @@
 block discarded – undo
12 12
 include_once 'include/Webservices/Query.php';
13 13
 include_once 'include/Webservices/RelatedTypes.php';
14 14
 
15
+/**
16
+ * @param string $query
17
+ */
15 18
 function vtws_query_related($query, $id, $relatedLabel, $user, $filterClause = null)
16 19
 {
17 20
 
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -71,7 +71,7 @@
 block discarded – undo
71 71
 		if (!empty($filterClause)) {
72 72
 			$query .= " " . $filterClause;
73 73
 		}
74
-		$query.=";";
74
+		$query .= ";";
75 75
 		$relatedRecords = vtws_query($query, $user);
76 76
 	}
77 77
 
Please login to merge, or discard this patch.
include/Webservices/RelatedModuleMeta.php 1 patch
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -46,6 +46,9 @@
 block discarded – undo
46 46
 		}
47 47
 	}
48 48
 
49
+	/**
50
+	 * @param integer $relationId
51
+	 */
49 52
 	private function getRelationMetaInfo($relationId)
50 53
 	{
51 54
 		return [
Please login to merge, or discard this patch.
include/Webservices/SessionManager.php 1 patch
Doc Comments   +6 added lines patch added patch discarded remove patch
@@ -112,6 +112,9 @@  discard block
 block discarded – undo
112 112
 		return HTTP_Session::id();
113 113
 	}
114 114
 
115
+	/**
116
+	 * @param string $var_name
117
+	 */
115 118
 	public function set($var_name, $var_value)
116 119
 	{
117 120
 		//TODO test setRef and getRef combination
@@ -119,6 +122,9 @@  discard block
 block discarded – undo
119 122
 		HTTP_Session::set($var_name, $var_value);
120 123
 	}
121 124
 
125
+	/**
126
+	 * @param string $name
127
+	 */
122 128
 	public function get($name)
123 129
 	{
124 130
 		//echo "<br> getting for: ",$name," :value: ",HTTP_Session::get($name);
Please login to merge, or discard this patch.
include/Webservices/VTQL_Lexer.php 1 patch
Doc Comments   +9 added lines patch added patch discarded remove patch
@@ -5,6 +5,9 @@  discard block
 block discarded – undo
5 5
 $in_started = false;
6 6
 $count = false;
7 7
 
8
+/**
9
+ * @param integer $count
10
+ */
8 11
 function incrementN($lexer, $count)
9 12
 {
10 13
 	$i = 0;
@@ -160,6 +163,9 @@  discard block
 block discarded – undo
160 163
 	}
161 164
 }
162 165
 
166
+/**
167
+ * @param VTQL_Lexer $lexer
168
+ */
163 169
 function handleend($lexer, $val)
164 170
 {
165 171
 	return VTQL_Parser::SEMICOLON;
@@ -179,6 +185,9 @@  discard block
 block discarded – undo
179 185
 	public $mandatory;
180 186
 	public $current_state;
181 187
 
188
+	/**
189
+	 * @param string $data
190
+	 */
182 191
 	public function __construct($data)
183 192
 	{
184 193
 		$this->index = 0;
Please login to merge, or discard this patch.
include/Webservices/VTQL_Parser.php 2 patches
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -159,6 +159,9 @@
 block discarded – undo
159 159
 	private $syntax_error;
160 160
 	private $user;
161 161
 
162
+	/**
163
+	 * @param VTQL_Lexer $lex
164
+	 */
162 165
 	public function __construct($user, $lex, $out)
163 166
 	{
164 167
 		if (!is_array($out)) {
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -89,7 +89,7 @@  discard block
 block discarded – undo
89 89
 {
90 90
 
91 91
 	public $stateno; /* The state-number */
92
-	public $major;   /* The major token value.  This is the code
92
+	public $major; /* The major token value.  This is the code
93 93
 	 * * number for the token at this stack level */
94 94
 	public $minor; /* The user-supplied minor token value.  This
95 95
 	 * * is the value of the token  */
@@ -663,17 +663,17 @@  discard block
 block discarded – undo
663 663
 	/**
664 664
 	 * @var int
665 665
 	 */
666
-	public $yyidx;  /* Index of top element in stack */
666
+	public $yyidx; /* Index of top element in stack */
667 667
 
668 668
 	/**
669 669
 	 * @var int
670 670
 	 */
671
-	public $yyerrcnt;  /* Shifts left before out of the error */
671
+	public $yyerrcnt; /* Shifts left before out of the error */
672 672
 
673 673
 	/**
674 674
 	 * @var array
675 675
 	 */
676
-	public $yystack = [];  /* The parser's stack */
676
+	public $yystack = []; /* The parser's stack */
677 677
 
678 678
 	/**
679 679
 	 * For tracing shifts, the names of all terminals and nonterminals
@@ -782,7 +782,7 @@  discard block
 block discarded – undo
782 782
 			 * * which appear on the RHS of the rule, but which are not used
783 783
 			 * * inside the C code.
784 784
 			 */
785
-			default: break;   /* If no destructor action specified: do nothing */
785
+			default: break; /* If no destructor action specified: do nothing */
786 786
 		}
787 787
 	}
788 788
 
@@ -1608,7 +1608,7 @@  discard block
 block discarded – undo
1608 1608
 	{
1609 1609
 //        $yyact;            /* The parser action. */
1610 1610
 //        $yyendofinput;     /* True if we are at the end of input */
1611
-		$yyerrorhit = 0;   /* True if yymajor has invoked an error */
1611
+		$yyerrorhit = 0; /* True if yymajor has invoked an error */
1612 1612
 
1613 1613
 		/* (re)initialize the parser, if necessary */
1614 1614
 		if ($this->yyidx === null || $this->yyidx < 0) {
Please login to merge, or discard this patch.
include/Webservices/WebserviceField.php 3 patches
Doc Comments   +9 added lines patch added patch discarded remove patch
@@ -84,6 +84,9 @@  discard block
 block discarded – undo
84 84
 		}
85 85
 	}
86 86
 
87
+	/**
88
+	 * @param integer $rowNumber
89
+	 */
87 90
 	public static function fromQueryResult($adb, $result, $rowNumber)
88 91
 	{
89 92
 		return new WebserviceField($adb, $adb->query_result_rowdata($result, $rowNumber));
@@ -147,6 +150,9 @@  discard block
 block discarded – undo
147 150
 		return $this->default;
148 151
 	}
149 152
 
153
+	/**
154
+	 * @return string
155
+	 */
150 156
 	public function getColumnName()
151 157
 	{
152 158
 		return $this->columnName;
@@ -203,6 +209,9 @@  discard block
 block discarded – undo
203 209
 		return false;
204 210
 	}
205 211
 
212
+	/**
213
+	 * @param boolean $nullable
214
+	 */
206 215
 	private function setNullable($nullable)
207 216
 	{
208 217
 		$this->nullable = $nullable;
Please login to merge, or discard this patch.
Braces   +13 added lines, -9 removed lines patch added patch discarded remove patch
@@ -198,8 +198,9 @@  discard block
 block discarded – undo
198 198
 
199 199
 	public function isReadOnly()
200 200
 	{
201
-		if ($this->readOnly == 1)
202
-			return true;
201
+		if ($this->readOnly == 1) {
202
+					return true;
203
+		}
203 204
 		return false;
204 205
 	}
205 206
 
@@ -307,8 +308,9 @@  discard block
 block discarded – undo
307 308
 				$numRows = $this->pearDB->num_rows($result);
308 309
 				for ($i = 0; $i < $numRows; ++$i) {
309 310
 					$referenceType = $this->pearDB->query_result($result, $i, "type");
310
-					if (in_array($referenceType, $accessibleTypes))
311
-						array_push($referenceTypes, $referenceType);
311
+					if (in_array($referenceType, $accessibleTypes)) {
312
+											array_push($referenceTypes, $referenceType);
313
+					}
312 314
 				}
313 315
 			} else {
314 316
 				$fieldModel = Vtiger_Field_Model::getInstanceFromFieldId($this->getFieldId());
@@ -425,21 +427,23 @@  discard block
 block discarded – undo
425 427
 				$picklistValue = $this->pearDB->query_result($result, $i, $fieldName);
426 428
 				$picklistValue = decode_html($picklistValue);
427 429
 				$moduleName = getTabModuleName($this->getTabId());
428
-				if ($moduleName == 'Events')
429
-					$moduleName = 'Calendar';
430
+				if ($moduleName == 'Events') {
431
+									$moduleName = 'Calendar';
432
+				}
430 433
 				$elem["label"] = \includes\Language::translate($picklistValue, $moduleName);
431 434
 				$elem["value"] = $picklistValue;
432 435
 				array_push($options, $elem);
433 436
 			}
434
-		}else {
437
+		} else {
435 438
 			$user = VTWS_PreserveGlobal::getGlobal('current_user');
436 439
 			$details = \includes\fields\Picklist::getRoleBasedPicklistValues($fieldName, $user->roleid);
437 440
 			for ($i = 0; $i < sizeof($details); ++$i) {
438 441
 				$elem = [];
439 442
 				$picklistValue = decode_html($details[$i]);
440 443
 				$moduleName = getTabModuleName($this->getTabId());
441
-				if ($moduleName == 'Events')
442
-					$moduleName = 'Calendar';
444
+				if ($moduleName == 'Events') {
445
+									$moduleName = 'Calendar';
446
+				}
443 447
 				$elem["label"] = \includes\Language::translate($picklistValue, $moduleName);
444 448
 				$elem["value"] = $picklistValue;
445 449
 				array_push($options, $elem);
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -41,7 +41,7 @@
 block discarded – undo
41 41
 								module_name=?, summary=?, test=?, execution_condition=?, defaultworkflow=?, filtersavedinnew=?,
42 42
 								schtypeid=?, schtime=?, schdayofmonth=?, schdayofweek=?, schannualdates=?, nexttrigger_time=? where workflow_id=?", array($wf->moduleName, $wf->description, $wf->test, $wf->executionCondition, $wf->defaultworkflow, $wf->filtersavedinnew,
43 43
 				$wf->schtypeid, $wf->schtime, $wf->schdayofmonth, $wf->schdayofweek, $wf->schannualdates, $wf->nexttrigger_time, $wf->id));
44
-		}else {
44
+		} else {
45 45
 			$workflowId = $adb->getUniqueID("com_vtiger_workflows");
46 46
 			$workflow->id = $workflowId;
47 47
 			$wf = $workflow;
Please login to merge, or discard this patch.
install/views/Index.php 2 patches
Doc Comments   +5 added lines patch added patch discarded remove patch
@@ -19,6 +19,11 @@
 block discarded – undo
19 19
 		return false;
20 20
 	}
21 21
 
22
+	/**
23
+	 * @param Vtiger_Request $request
24
+	 *
25
+	 * @return Vtiger_Request
26
+	 */
22 27
 	public function setLanguage($request)
23 28
 	{
24 29
 		if (!$request->get('lang')) {
Please login to merge, or discard this patch.
Braces   +3 added lines, -2 removed lines patch added patch discarded remove patch
@@ -87,8 +87,9 @@
 block discarded – undo
87 87
 	public function process(Vtiger_Request $request)
88 88
 	{
89 89
 		$default_charset = AppConfig::main('default_charset');
90
-		if (empty($default_charset))
91
-			$default_charset = 'UTF-8';
90
+		if (empty($default_charset)) {
91
+					$default_charset = 'UTF-8';
92
+		}
92 93
 		$mode = $request->getMode();
93 94
 		if (!empty($mode) && $this->isMethodExposed($mode)) {
94 95
 			return $this->$mode($request);
Please login to merge, or discard this patch.
libraries/csrf-magic/csrf-magic.php 4 patches
Doc Comments   +4 added lines, -1 removed lines patch added patch discarded remove patch
@@ -211,7 +211,7 @@  discard block
 block discarded – undo
211 211
 /**
212 212
  * Checks if this is a post request, and if it is, checks if the nonce is valid.
213 213
  * @param bool $fatal Whether or not to fatally error out if there is a problem.
214
- * @return True if check passes or is not necessary, false if failure.
214
+ * @return boolean|null if check passes or is not necessary, false if failure.
215 215
  */
216 216
 function csrf_check($fatal = true) {
217 217
     if ($_SERVER['REQUEST_METHOD'] !== 'POST') return true;
@@ -281,6 +281,9 @@  discard block
 block discarded – undo
281 281
     }
282 282
     return $ret;
283 283
 }
284
+/**
285
+ * @param integer $level
286
+ */
284 287
 function csrf_flattenpost2($level, $key, $data) {
285 288
     if(!is_array($data)) return array($key => $data);
286 289
     $ret = array();
Please login to merge, or discard this patch.
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -166,35 +166,35 @@  discard block
 block discarded – undo
166 166
 			if (!$is_html) return $buffer;
167 167
         }
168 168
     }
169
-    $count=1;
169
+    $count = 1;
170 170
     $tokens = csrf_get_tokens();
171 171
     $name = $GLOBALS['csrf']['input-name'];
172 172
     $endslash = $GLOBALS['csrf']['xhtml'] ? ' /' : '';
173 173
     $input = "<input type='hidden' name='$name' value=\"$tokens\"$endslash>";
174 174
     $buffer = preg_replace('#(<form[^>]*method\s*=\s*["\']post["\'][^>]*>)#i', '$1' . $input, $buffer);
175 175
     if ($GLOBALS['csrf']['frame-breaker'] && !$is_partial) {
176
-        $buffer = preg_replace('/<\/head>/', '<script type="text/javascript">if (top != self) {top.location.href = self.location.href;}</script></head>', $buffer,$count);
176
+        $buffer = preg_replace('/<\/head>/', '<script type="text/javascript">if (top != self) {top.location.href = self.location.href;}</script></head>', $buffer, $count);
177 177
     }
178 178
     if (($js = $GLOBALS['csrf']['rewrite-js']) && !$is_partial) {
179 179
 		global $dbconfig;
180 180
         if (empty($dbconfig) || empty($dbconfig['db_name']) || $dbconfig['db_name'] == '_DBC_TYPE_') {
181 181
 		$buffer = preg_replace(
182 182
             '/<\/head>/',
183
-             '<script type="text/javascript">'.
184
-                'var csrfMagicToken = "'.$tokens.'";'.
185
-                'var csrfMagicName = "'.$name.'";</script>'.
186
-            '<script src="../'.$js.'" type="text/javascript"></script></head>',
187
-            $buffer,$count
183
+             '<script type="text/javascript">' .
184
+                'var csrfMagicToken = "' . $tokens . '";' .
185
+                'var csrfMagicName = "' . $name . '";</script>' .
186
+            '<script src="../' . $js . '" type="text/javascript"></script></head>',
187
+            $buffer, $count
188 188
 		);
189 189
 			
190 190
 		} else {
191 191
 		$buffer = preg_replace(
192 192
             '/<\/head>/',
193
-            '<script type="text/javascript">'.
194
-                'var csrfMagicToken = "'.$tokens.'";'.
195
-                'var csrfMagicName = "'.$name.'";</script>'.
196
-            '<script src="'.$js.'" type="text/javascript"></script></head>',
197
-            $buffer,$count
193
+            '<script type="text/javascript">' .
194
+                'var csrfMagicToken = "' . $tokens . '";' .
195
+                'var csrfMagicName = "' . $name . '";</script>' .
196
+            '<script src="' . $js . '" type="text/javascript"></script></head>',
197
+            $buffer, $count
198 198
 		);
199 199
 		}
200 200
 		
@@ -276,17 +276,17 @@  discard block
 block discarded – undo
276 276
 
277 277
 function csrf_flattenpost($data) {
278 278
     $ret = array();
279
-    foreach($data as $n => $v) {
279
+    foreach ($data as $n => $v) {
280 280
         $ret = array_merge($ret, csrf_flattenpost2(1, $n, $v));
281 281
     }
282 282
     return $ret;
283 283
 }
284 284
 function csrf_flattenpost2($level, $key, $data) {
285
-    if(!is_array($data)) return array($key => $data);
285
+    if (!is_array($data)) return array($key => $data);
286 286
     $ret = array();
287
-    foreach($data as $n => $v) {
288
-        $nk = $level >= 1 ? $key."[$n]" : "[$n]";
289
-        $ret = array_merge($ret, csrf_flattenpost2($level+1, $nk, $v));
287
+    foreach ($data as $n => $v) {
288
+        $nk = $level >= 1 ? $key . "[$n]" : "[$n]";
289
+        $ret = array_merge($ret, csrf_flattenpost2($level + 1, $nk, $v));
290 290
     }
291 291
     return $ret;
292 292
 }
@@ -300,7 +300,7 @@  discard block
 block discarded – undo
300 300
     $data = '';
301 301
     foreach (csrf_flattenpost($_POST) as $key => $value) {
302 302
         if ($key == $GLOBALS['csrf']['input-name']) continue;
303
-        $data .= '<input type="hidden" name="'.htmlspecialchars($key).'" value="'.htmlspecialchars($value).'" />';
303
+        $data .= '<input type="hidden" name="' . htmlspecialchars($key) . '" value="' . htmlspecialchars($value) . '" />';
304 304
     }
305 305
     echo "<html><head><title>CSRF check failed</title></head>
306 306
         <body>
@@ -408,7 +408,7 @@  discard block
 block discarded – undo
408 408
     if (is_writable($dir)) {
409 409
         $secret = csrf_generate_secret();
410 410
         $fh = fopen($file, 'w');
411
-        fwrite($fh, '<?php $secret = "'.$secret.'";' . PHP_EOL);
411
+        fwrite($fh, '<?php $secret = "' . $secret . '";' . PHP_EOL);
412 412
         fclose($fh);
413 413
         return $secret;
414 414
     }
Please login to merge, or discard this patch.
Braces   +90 added lines, -31 removed lines patch added patch discarded remove patch
@@ -157,13 +157,16 @@  discard block
 block discarded – undo
157 157
 			// Determine based on content type.
158 158
 			$headers = headers_list();
159 159
 			foreach ($headers as $header) {
160
-				if ($is_html) break;
161
-				else if (stripos('Content-type', $header) !== false && stripos('/html', $header) === false) {
160
+				if ($is_html) {
161
+					break;
162
+				} else if (stripos('Content-type', $header) !== false && stripos('/html', $header) === false) {
162 163
 					$is_html = false;
163 164
 				}
164 165
 			}
165 166
         
166
-			if (!$is_html) return $buffer;
167
+			if (!$is_html) {
168
+				return $buffer;
169
+			}
167 170
         }
168 171
     }
169 172
     $count=1;
@@ -214,22 +217,30 @@  discard block
 block discarded – undo
214 217
  * @return True if check passes or is not necessary, false if failure.
215 218
  */
216 219
 function csrf_check($fatal = true) {
217
-    if ($_SERVER['REQUEST_METHOD'] !== 'POST') return true;
220
+    if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
221
+    	return true;
222
+    }
218 223
     csrf_start();
219 224
     $name = $GLOBALS['csrf']['input-name'];
220 225
     $ok = false;
221 226
     $tokens = '';
222 227
     do {
223
-        if (!isset($_POST[$name])) break;
228
+        if (!isset($_POST[$name])) {
229
+        	break;
230
+        }
224 231
         // we don't regenerate a token and check it because some token creation
225 232
         // schemes are volatile.
226 233
         $tokens = $_POST[$name];
227
-        if (!csrf_check_tokens($tokens)) break;
234
+        if (!csrf_check_tokens($tokens)) {
235
+        	break;
236
+        }
228 237
         $ok = true;
229 238
     } while (false);
230 239
     if ($fatal && !$ok) {
231 240
         $callback = $GLOBALS['csrf']['callback'];
232
-        if (trim($tokens, 'A..Za..z0..9:;,') !== '') $tokens = 'hidden';
241
+        if (trim($tokens, 'A..Za..z0..9:;,') !== '') {
242
+        	$tokens = 'hidden';
243
+        }
233 244
         $callback($tokens);
234 245
         exit;
235 246
     }
@@ -256,15 +267,21 @@  discard block
 block discarded – undo
256 267
     csrf_start();
257 268
 
258 269
     // These are "strong" algorithms that don't require per se a secret
259
-    if (session_id()) return 'sid:' . csrf_hash(session_id()) . $ip;
270
+    if (session_id()) {
271
+    	return 'sid:' . csrf_hash(session_id()) . $ip;
272
+    }
260 273
     if ($GLOBALS['csrf']['cookie']) {
261 274
         $val = csrf_generate_secret();
262 275
         setcookie($GLOBALS['csrf']['cookie'], $val);
263 276
         return 'cookie:' . csrf_hash($val) . $ip;
264 277
     }
265
-    if ($GLOBALS['csrf']['key']) return 'key:' . csrf_hash($GLOBALS['csrf']['key']) . $ip;
278
+    if ($GLOBALS['csrf']['key']) {
279
+    	return 'key:' . csrf_hash($GLOBALS['csrf']['key']) . $ip;
280
+    }
266 281
     // These further algorithms require a server-side secret
267
-    if (!$secret) return 'invalid';
282
+    if (!$secret) {
283
+    	return 'invalid';
284
+    }
268 285
     if ($GLOBALS['csrf']['user'] !== false) {
269 286
         return 'user:' . csrf_hash($GLOBALS['csrf']['user']);
270 287
     }
@@ -282,7 +299,9 @@  discard block
 block discarded – undo
282 299
     return $ret;
283 300
 }
284 301
 function csrf_flattenpost2($level, $key, $data) {
285
-    if(!is_array($data)) return array($key => $data);
302
+    if(!is_array($data)) {
303
+    	return array($key => $data);
304
+    }
286 305
     $ret = array();
287 306
     foreach($data as $n => $v) {
288 307
         $nk = $level >= 1 ? $key."[$n]" : "[$n]";
@@ -299,7 +318,9 @@  discard block
 block discarded – undo
299 318
     header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
300 319
     $data = '';
301 320
     foreach (csrf_flattenpost($_POST) as $key => $value) {
302
-        if ($key == $GLOBALS['csrf']['input-name']) continue;
321
+        if ($key == $GLOBALS['csrf']['input-name']) {
322
+        	continue;
323
+        }
303 324
         $data .= '<input type="hidden" name="'.htmlspecialchars($key).'" value="'.htmlspecialchars($value).'" />';
304 325
     }
305 326
     echo "<html><head><title>CSRF check failed</title></head>
@@ -325,9 +346,13 @@  discard block
 block discarded – undo
325 346
  * instead of csrf_check_token()
326 347
  */
327 348
 function csrf_check_tokens($tokens) {
328
-    if (is_string($tokens)) $tokens = explode(';', $tokens);
349
+    if (is_string($tokens)) {
350
+    	$tokens = explode(';', $tokens);
351
+    }
329 352
     foreach ($tokens as $token) {
330
-        if (csrf_check_token($token)) return true;
353
+        if (csrf_check_token($token)) {
354
+        	return true;
355
+        }
331 356
     }
332 357
     return false;
333 358
 }
@@ -336,38 +361,62 @@  discard block
 block discarded – undo
336 361
  * Checks if a token is valid.
337 362
  */
338 363
 function csrf_check_token($token) {
339
-    if (strpos($token, ':') === false) return false;
364
+    if (strpos($token, ':') === false) {
365
+    	return false;
366
+    }
340 367
     list($type, $value) = explode(':', $token, 2);
341
-    if (strpos($value, ',') === false) return false;
368
+    if (strpos($value, ',') === false) {
369
+    	return false;
370
+    }
342 371
     list($x, $time) = explode(',', $token, 2);
343 372
     if ($GLOBALS['csrf']['expires']) {
344
-        if (time() > $time + $GLOBALS['csrf']['expires']) return false;
373
+        if (time() > $time + $GLOBALS['csrf']['expires']) {
374
+        	return false;
375
+        }
345 376
     }
346 377
     switch ($type) {
347 378
         case 'sid':
348 379
             return $value === csrf_hash(session_id(), $time);
349 380
         case 'cookie':
350 381
             $n = $GLOBALS['csrf']['cookie'];
351
-            if (!$n) return false;
352
-            if (!isset($_COOKIE[$n])) return false;
382
+            if (!$n) {
383
+            	return false;
384
+            }
385
+            if (!isset($_COOKIE[$n])) {
386
+            	return false;
387
+            }
353 388
             return $value === csrf_hash($_COOKIE[$n], $time);
354 389
         case 'key':
355
-            if (!$GLOBALS['csrf']['key']) return false;
390
+            if (!$GLOBALS['csrf']['key']) {
391
+            	return false;
392
+            }
356 393
             return $value === csrf_hash($GLOBALS['csrf']['key'], $time);
357 394
         // We could disable these 'weaker' checks if 'key' was set, but
358 395
         // that doesn't make me feel good then about the cookie-based
359 396
         // implementation.
360 397
         case 'user':
361
-            if (!csrf_get_secret()) return false;
362
-            if ($GLOBALS['csrf']['user'] === false) return false;
398
+            if (!csrf_get_secret()) {
399
+            	return false;
400
+            }
401
+            if ($GLOBALS['csrf']['user'] === false) {
402
+            	return false;
403
+            }
363 404
             return $value === csrf_hash($GLOBALS['csrf']['user'], $time);
364 405
         case 'ip':
365
-            if (!csrf_get_secret()) return false;
406
+            if (!csrf_get_secret()) {
407
+            	return false;
408
+            }
366 409
             // do not allow IP-based checks if the username is set, or if
367 410
             // the browser sent cookies
368
-            if ($GLOBALS['csrf']['user'] !== false) return false;
369
-            if (!empty($_COOKIE)) return false;
370
-            if (!$GLOBALS['csrf']['allow-ip']) return false;
411
+            if ($GLOBALS['csrf']['user'] !== false) {
412
+            	return false;
413
+            }
414
+            if (!empty($_COOKIE)) {
415
+            	return false;
416
+            }
417
+            if (!$GLOBALS['csrf']['allow-ip']) {
418
+            	return false;
419
+            }
371 420
             return $value === csrf_hash($_SERVER['IP_ADDRESS'], $time);
372 421
     }
373 422
     return false;
@@ -397,7 +446,9 @@  discard block
 block discarded – undo
397 446
  * Retrieves the secret, and generates one if necessary.
398 447
  */
399 448
 function csrf_get_secret() {
400
-    if ($GLOBALS['csrf']['secret']) return $GLOBALS['csrf']['secret'];
449
+    if ($GLOBALS['csrf']['secret']) {
450
+    	return $GLOBALS['csrf']['secret'];
451
+    }
401 452
     $dir = dirname(__FILE__);
402 453
     $file = $dir . '/../../config/csrf_secret.php';
403 454
     $secret = '';
@@ -432,13 +483,21 @@  discard block
 block discarded – undo
432 483
  * from the current time.
433 484
  */
434 485
 function csrf_hash($value, $time = null) {
435
-    if (!$time) $time = time();
486
+    if (!$time) {
487
+    	$time = time();
488
+    }
436 489
     return sha1(csrf_get_secret() . $value . $time) . ',' . $time;
437 490
 }
438 491
 
439 492
 // Load user configuration
440
-if (function_exists('csrf_startup')) csrf_startup();
493
+if (function_exists('csrf_startup')) {
494
+	csrf_startup();
495
+}
441 496
 // Initialize our handler
442
-if ($GLOBALS['csrf']['rewrite'])     ob_start('csrf_ob_handler');
497
+if ($GLOBALS['csrf']['rewrite']) {
498
+	ob_start('csrf_ob_handler');
499
+}
443 500
 // Perform check
444
-if (!$GLOBALS['csrf']['defer'])      csrf_check();
501
+if (!$GLOBALS['csrf']['defer']) {
502
+	csrf_check();
503
+}
Please login to merge, or discard this patch.
Indentation   +185 added lines, -185 removed lines patch added patch discarded remove patch
@@ -138,17 +138,17 @@  discard block
 block discarded – undo
138 138
  * inject our JavaScript library.
139 139
  */
140 140
 function csrf_ob_handler($buffer, $flags) {
141
-    // Even though the user told us to rewrite, we should do a quick heuristic
142
-    // to check if the page is *actually* HTML. We don't begin rewriting until
143
-    // we hit the first <html tag.
144
-    static $is_html = false;
145
-    static $is_partial = false;
141
+	// Even though the user told us to rewrite, we should do a quick heuristic
142
+	// to check if the page is *actually* HTML. We don't begin rewriting until
143
+	// we hit the first <html tag.
144
+	static $is_html = false;
145
+	static $is_partial = false;
146 146
     
147
-    if (!$is_html) {
148
-        // not HTML until proven otherwise
149
-        if (stripos($buffer, '<html') !== false) {
150
-            $is_html = true;
151
-        } else {
147
+	if (!$is_html) {
148
+		// not HTML until proven otherwise
149
+		if (stripos($buffer, '<html') !== false) {
150
+			$is_html = true;
151
+		} else {
152 152
 
153 153
 			// Customized to take the partial HTML with form
154 154
 			$is_html = true;
@@ -164,48 +164,48 @@  discard block
 block discarded – undo
164 164
 			}
165 165
         
166 166
 			if (!$is_html) return $buffer;
167
-        }
168
-    }
169
-    $count=1;
170
-    $tokens = csrf_get_tokens();
171
-    $name = $GLOBALS['csrf']['input-name'];
172
-    $endslash = $GLOBALS['csrf']['xhtml'] ? ' /' : '';
173
-    $input = "<input type='hidden' name='$name' value=\"$tokens\"$endslash>";
174
-    $buffer = preg_replace('#(<form[^>]*method\s*=\s*["\']post["\'][^>]*>)#i', '$1' . $input, $buffer);
175
-    if ($GLOBALS['csrf']['frame-breaker'] && !$is_partial) {
176
-        $buffer = preg_replace('/<\/head>/', '<script type="text/javascript">if (top != self) {top.location.href = self.location.href;}</script></head>', $buffer,$count);
177
-    }
178
-    if (($js = $GLOBALS['csrf']['rewrite-js']) && !$is_partial) {
167
+		}
168
+	}
169
+	$count=1;
170
+	$tokens = csrf_get_tokens();
171
+	$name = $GLOBALS['csrf']['input-name'];
172
+	$endslash = $GLOBALS['csrf']['xhtml'] ? ' /' : '';
173
+	$input = "<input type='hidden' name='$name' value=\"$tokens\"$endslash>";
174
+	$buffer = preg_replace('#(<form[^>]*method\s*=\s*["\']post["\'][^>]*>)#i', '$1' . $input, $buffer);
175
+	if ($GLOBALS['csrf']['frame-breaker'] && !$is_partial) {
176
+		$buffer = preg_replace('/<\/head>/', '<script type="text/javascript">if (top != self) {top.location.href = self.location.href;}</script></head>', $buffer,$count);
177
+	}
178
+	if (($js = $GLOBALS['csrf']['rewrite-js']) && !$is_partial) {
179 179
 		global $dbconfig;
180
-        if (empty($dbconfig) || empty($dbconfig['db_name']) || $dbconfig['db_name'] == '_DBC_TYPE_') {
180
+		if (empty($dbconfig) || empty($dbconfig['db_name']) || $dbconfig['db_name'] == '_DBC_TYPE_') {
181 181
 		$buffer = preg_replace(
182
-            '/<\/head>/',
183
-             '<script type="text/javascript">'.
184
-                'var csrfMagicToken = "'.$tokens.'";'.
185
-                'var csrfMagicName = "'.$name.'";</script>'.
186
-            '<script src="../'.$js.'" type="text/javascript"></script></head>',
187
-            $buffer,$count
182
+			'/<\/head>/',
183
+			 '<script type="text/javascript">'.
184
+				'var csrfMagicToken = "'.$tokens.'";'.
185
+				'var csrfMagicName = "'.$name.'";</script>'.
186
+			'<script src="../'.$js.'" type="text/javascript"></script></head>',
187
+			$buffer,$count
188 188
 		);
189 189
 			
190 190
 		} else {
191 191
 		$buffer = preg_replace(
192
-            '/<\/head>/',
193
-            '<script type="text/javascript">'.
194
-                'var csrfMagicToken = "'.$tokens.'";'.
195
-                'var csrfMagicName = "'.$name.'";</script>'.
196
-            '<script src="'.$js.'" type="text/javascript"></script></head>',
197
-            $buffer,$count
192
+			'/<\/head>/',
193
+			'<script type="text/javascript">'.
194
+				'var csrfMagicToken = "'.$tokens.'";'.
195
+				'var csrfMagicName = "'.$name.'";</script>'.
196
+			'<script src="'.$js.'" type="text/javascript"></script></head>',
197
+			$buffer,$count
198 198
 		);
199 199
 		}
200 200
 		
201
-        $script = '<script type="text/javascript">CsrfMagic.end();</script>';
201
+		$script = '<script type="text/javascript">CsrfMagic.end();</script>';
202 202
         
203
-        $buffer = preg_replace('/<\/body>/', $script . '</body>', $buffer, $count);
204
-        if (!$count) {
205
-            $buffer .= $script;
206
-        }
207
-    }
208
-    return $buffer;
203
+		$buffer = preg_replace('/<\/body>/', $script . '</body>', $buffer, $count);
204
+		if (!$count) {
205
+			$buffer .= $script;
206
+		}
207
+	}
208
+	return $buffer;
209 209
 }
210 210
 
211 211
 /**
@@ -214,26 +214,26 @@  discard block
 block discarded – undo
214 214
  * @return True if check passes or is not necessary, false if failure.
215 215
  */
216 216
 function csrf_check($fatal = true) {
217
-    if ($_SERVER['REQUEST_METHOD'] !== 'POST') return true;
218
-    csrf_start();
219
-    $name = $GLOBALS['csrf']['input-name'];
220
-    $ok = false;
221
-    $tokens = '';
222
-    do {
223
-        if (!isset($_POST[$name])) break;
224
-        // we don't regenerate a token and check it because some token creation
225
-        // schemes are volatile.
226
-        $tokens = $_POST[$name];
227
-        if (!csrf_check_tokens($tokens)) break;
228
-        $ok = true;
229
-    } while (false);
230
-    if ($fatal && !$ok) {
231
-        $callback = $GLOBALS['csrf']['callback'];
232
-        if (trim($tokens, 'A..Za..z0..9:;,') !== '') $tokens = 'hidden';
233
-        $callback($tokens);
234
-        exit;
235
-    }
236
-    return $ok;
217
+	if ($_SERVER['REQUEST_METHOD'] !== 'POST') return true;
218
+	csrf_start();
219
+	$name = $GLOBALS['csrf']['input-name'];
220
+	$ok = false;
221
+	$tokens = '';
222
+	do {
223
+		if (!isset($_POST[$name])) break;
224
+		// we don't regenerate a token and check it because some token creation
225
+		// schemes are volatile.
226
+		$tokens = $_POST[$name];
227
+		if (!csrf_check_tokens($tokens)) break;
228
+		$ok = true;
229
+	} while (false);
230
+	if ($fatal && !$ok) {
231
+		$callback = $GLOBALS['csrf']['callback'];
232
+		if (trim($tokens, 'A..Za..z0..9:;,') !== '') $tokens = 'hidden';
233
+		$callback($tokens);
234
+		exit;
235
+	}
236
+	return $ok;
237 237
 }
238 238
 
239 239
 /**
@@ -241,67 +241,67 @@  discard block
 block discarded – undo
241 241
  * by semicolons.
242 242
  */
243 243
 function csrf_get_tokens() {
244
-    $has_cookies = !empty($_COOKIE);
245
-
246
-    // $ip implements a composite key, which is sent if the user hasn't sent
247
-    // any cookies. It may or may not be used, depending on whether or not
248
-    // the cookies "stick"
249
-    $secret = csrf_get_secret();
250
-    if (!$has_cookies && $secret) {
251
-        $ip = ';ip:' . csrf_hash($_SERVER['IP_ADDRESS']);
252
-    } else {
253
-        $ip = '';
254
-    }
255
-    csrf_start();
256
-
257
-    // These are "strong" algorithms that don't require per se a secret
258
-    if (session_id()) return 'sid:' . csrf_hash(session_id()) . $ip;
259
-    if ($GLOBALS['csrf']['cookie']) {
260
-        $val = csrf_generate_secret();
261
-        setcookie($GLOBALS['csrf']['cookie'], $val);
262
-        return 'cookie:' . csrf_hash($val) . $ip;
263
-    }
264
-    if ($GLOBALS['csrf']['key']) return 'key:' . csrf_hash($GLOBALS['csrf']['key']) . $ip;
265
-    // These further algorithms require a server-side secret
266
-    if (!$secret) return 'invalid';
267
-    if ($GLOBALS['csrf']['user'] !== false) {
268
-        return 'user:' . csrf_hash($GLOBALS['csrf']['user']);
269
-    }
270
-    if ($GLOBALS['csrf']['allow-ip']) {
271
-        return ltrim($ip, ';');
272
-    }
273
-    return 'invalid';
244
+	$has_cookies = !empty($_COOKIE);
245
+
246
+	// $ip implements a composite key, which is sent if the user hasn't sent
247
+	// any cookies. It may or may not be used, depending on whether or not
248
+	// the cookies "stick"
249
+	$secret = csrf_get_secret();
250
+	if (!$has_cookies && $secret) {
251
+		$ip = ';ip:' . csrf_hash($_SERVER['IP_ADDRESS']);
252
+	} else {
253
+		$ip = '';
254
+	}
255
+	csrf_start();
256
+
257
+	// These are "strong" algorithms that don't require per se a secret
258
+	if (session_id()) return 'sid:' . csrf_hash(session_id()) . $ip;
259
+	if ($GLOBALS['csrf']['cookie']) {
260
+		$val = csrf_generate_secret();
261
+		setcookie($GLOBALS['csrf']['cookie'], $val);
262
+		return 'cookie:' . csrf_hash($val) . $ip;
263
+	}
264
+	if ($GLOBALS['csrf']['key']) return 'key:' . csrf_hash($GLOBALS['csrf']['key']) . $ip;
265
+	// These further algorithms require a server-side secret
266
+	if (!$secret) return 'invalid';
267
+	if ($GLOBALS['csrf']['user'] !== false) {
268
+		return 'user:' . csrf_hash($GLOBALS['csrf']['user']);
269
+	}
270
+	if ($GLOBALS['csrf']['allow-ip']) {
271
+		return ltrim($ip, ';');
272
+	}
273
+	return 'invalid';
274 274
 }
275 275
 
276 276
 function csrf_flattenpost($data) {
277
-    $ret = array();
278
-    foreach($data as $n => $v) {
279
-        $ret = array_merge($ret, csrf_flattenpost2(1, $n, $v));
280
-    }
281
-    return $ret;
277
+	$ret = array();
278
+	foreach($data as $n => $v) {
279
+		$ret = array_merge($ret, csrf_flattenpost2(1, $n, $v));
280
+	}
281
+	return $ret;
282 282
 }
283 283
 function csrf_flattenpost2($level, $key, $data) {
284
-    if(!is_array($data)) return array($key => $data);
285
-    $ret = array();
286
-    foreach($data as $n => $v) {
287
-        $nk = $level >= 1 ? $key."[$n]" : "[$n]";
288
-        $ret = array_merge($ret, csrf_flattenpost2($level+1, $nk, $v));
289
-    }
290
-    return $ret;
284
+	if(!is_array($data)) return array($key => $data);
285
+	$ret = array();
286
+	foreach($data as $n => $v) {
287
+		$nk = $level >= 1 ? $key."[$n]" : "[$n]";
288
+		$ret = array_merge($ret, csrf_flattenpost2($level+1, $nk, $v));
289
+	}
290
+	return $ret;
291 291
 }
292 292
 
293 293
 /**
294 294
  * @param $tokens is safe for HTML consumption
295 295
  */
296 296
 function csrf_callback($tokens) {
297
-    // (yes, $tokens is safe to echo without escaping)
298
-    header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
299
-    $data = '';
300
-    foreach (csrf_flattenpost($_POST) as $key => $value) {
301
-        if ($key == $GLOBALS['csrf']['input-name']) continue;
302
-        $data .= '<input type="hidden" name="'.htmlspecialchars($key).'" value="'.htmlspecialchars($value).'" />';
303
-    }
304
-    echo "<html><head><title>CSRF check failed</title></head>
297
+	// (yes, $tokens is safe to echo without escaping)
298
+	header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
299
+	$data = '';
300
+	foreach (csrf_flattenpost($_POST) as $key => $value) {
301
+		if ($key == $GLOBALS['csrf']['input-name']) continue;
302
+		$data .= '<input type="hidden" name="'.htmlspecialchars($key).'" value="'.htmlspecialchars($value).'" />';
303
+	}
304
+	echo "<html><head><title>CSRF check failed</title></head>
305 305
         <body>
306 306
         <p>CSRF check failed. Your form session may have expired, or you may not have
307 307
         cookies enabled.</p>
@@ -316,7 +316,7 @@  discard block
 block discarded – undo
316 316
  * It is configurable by setting $GLOBALS['csrf']['callback'] in this file
317 317
  */
318 318
 function vtResponseForIllegalAccess() {
319
-    echo 'Invalid request - Response For Illegal Access';
319
+	echo 'Invalid request - Response For Illegal Access';
320 320
 }
321 321
 
322 322
 /**
@@ -324,106 +324,106 @@  discard block
 block discarded – undo
324 324
  * instead of csrf_check_token()
325 325
  */
326 326
 function csrf_check_tokens($tokens) {
327
-    if (is_string($tokens)) $tokens = explode(';', $tokens);
328
-    foreach ($tokens as $token) {
329
-        if (csrf_check_token($token)) return true;
330
-    }
331
-    return false;
327
+	if (is_string($tokens)) $tokens = explode(';', $tokens);
328
+	foreach ($tokens as $token) {
329
+		if (csrf_check_token($token)) return true;
330
+	}
331
+	return false;
332 332
 }
333 333
 
334 334
 /**
335 335
  * Checks if a token is valid.
336 336
  */
337 337
 function csrf_check_token($token) {
338
-    if (strpos($token, ':') === false) return false;
339
-    list($type, $value) = explode(':', $token, 2);
340
-    if (strpos($value, ',') === false) return false;
341
-    list($x, $time) = explode(',', $token, 2);
342
-    if ($GLOBALS['csrf']['expires']) {
343
-        if (time() > $time + $GLOBALS['csrf']['expires']) return false;
344
-    }
345
-    switch ($type) {
346
-        case 'sid':
347
-            return $value === csrf_hash(session_id(), $time);
348
-        case 'cookie':
349
-            $n = $GLOBALS['csrf']['cookie'];
350
-            if (!$n) return false;
351
-            if (!isset($_COOKIE[$n])) return false;
352
-            return $value === csrf_hash($_COOKIE[$n], $time);
353
-        case 'key':
354
-            if (!$GLOBALS['csrf']['key']) return false;
355
-            return $value === csrf_hash($GLOBALS['csrf']['key'], $time);
356
-        // We could disable these 'weaker' checks if 'key' was set, but
357
-        // that doesn't make me feel good then about the cookie-based
358
-        // implementation.
359
-        case 'user':
360
-            if (!csrf_get_secret()) return false;
361
-            if ($GLOBALS['csrf']['user'] === false) return false;
362
-            return $value === csrf_hash($GLOBALS['csrf']['user'], $time);
363
-        case 'ip':
364
-            if (!csrf_get_secret()) return false;
365
-            // do not allow IP-based checks if the username is set, or if
366
-            // the browser sent cookies
367
-            if ($GLOBALS['csrf']['user'] !== false) return false;
368
-            if (!empty($_COOKIE)) return false;
369
-            if (!$GLOBALS['csrf']['allow-ip']) return false;
370
-            return $value === csrf_hash($_SERVER['IP_ADDRESS'], $time);
371
-    }
372
-    return false;
338
+	if (strpos($token, ':') === false) return false;
339
+	list($type, $value) = explode(':', $token, 2);
340
+	if (strpos($value, ',') === false) return false;
341
+	list($x, $time) = explode(',', $token, 2);
342
+	if ($GLOBALS['csrf']['expires']) {
343
+		if (time() > $time + $GLOBALS['csrf']['expires']) return false;
344
+	}
345
+	switch ($type) {
346
+		case 'sid':
347
+			return $value === csrf_hash(session_id(), $time);
348
+		case 'cookie':
349
+			$n = $GLOBALS['csrf']['cookie'];
350
+			if (!$n) return false;
351
+			if (!isset($_COOKIE[$n])) return false;
352
+			return $value === csrf_hash($_COOKIE[$n], $time);
353
+		case 'key':
354
+			if (!$GLOBALS['csrf']['key']) return false;
355
+			return $value === csrf_hash($GLOBALS['csrf']['key'], $time);
356
+		// We could disable these 'weaker' checks if 'key' was set, but
357
+		// that doesn't make me feel good then about the cookie-based
358
+		// implementation.
359
+		case 'user':
360
+			if (!csrf_get_secret()) return false;
361
+			if ($GLOBALS['csrf']['user'] === false) return false;
362
+			return $value === csrf_hash($GLOBALS['csrf']['user'], $time);
363
+		case 'ip':
364
+			if (!csrf_get_secret()) return false;
365
+			// do not allow IP-based checks if the username is set, or if
366
+			// the browser sent cookies
367
+			if ($GLOBALS['csrf']['user'] !== false) return false;
368
+			if (!empty($_COOKIE)) return false;
369
+			if (!$GLOBALS['csrf']['allow-ip']) return false;
370
+			return $value === csrf_hash($_SERVER['IP_ADDRESS'], $time);
371
+	}
372
+	return false;
373 373
 }
374 374
 
375 375
 /**
376 376
  * Sets a configuration value.
377 377
  */
378 378
 function csrf_conf($key, $val) {
379
-    if (!isset($GLOBALS['csrf'][$key])) {
380
-        trigger_error('No such configuration ' . $key, E_USER_WARNING);
381
-        return;
382
-    }
383
-    $GLOBALS['csrf'][$key] = $val;
379
+	if (!isset($GLOBALS['csrf'][$key])) {
380
+		trigger_error('No such configuration ' . $key, E_USER_WARNING);
381
+		return;
382
+	}
383
+	$GLOBALS['csrf'][$key] = $val;
384 384
 }
385 385
 
386 386
 /**
387 387
  * Starts a session if we're allowed to.
388 388
  */
389 389
 function csrf_start() {
390
-    if ($GLOBALS['csrf']['auto-session'] && !session_id()) {
391
-        session_start();
392
-    }
390
+	if ($GLOBALS['csrf']['auto-session'] && !session_id()) {
391
+		session_start();
392
+	}
393 393
 }
394 394
 
395 395
 /**
396 396
  * Retrieves the secret, and generates one if necessary.
397 397
  */
398 398
 function csrf_get_secret() {
399
-    if ($GLOBALS['csrf']['secret']) return $GLOBALS['csrf']['secret'];
400
-    $dir = dirname(__FILE__);
401
-    $file = $dir . '/../../config/csrf_secret.php';
402
-    $secret = '';
403
-    if (file_exists($file)) {
404
-        include $file;
405
-        return $secret;
406
-    }
407
-    if (is_writable($dir)) {
408
-        $secret = csrf_generate_secret();
409
-        $fh = fopen($file, 'w');
410
-        fwrite($fh, '<?php $secret = "'.$secret.'";' . PHP_EOL);
411
-        fclose($fh);
412
-        return $secret;
413
-    }
414
-    return '';
399
+	if ($GLOBALS['csrf']['secret']) return $GLOBALS['csrf']['secret'];
400
+	$dir = dirname(__FILE__);
401
+	$file = $dir . '/../../config/csrf_secret.php';
402
+	$secret = '';
403
+	if (file_exists($file)) {
404
+		include $file;
405
+		return $secret;
406
+	}
407
+	if (is_writable($dir)) {
408
+		$secret = csrf_generate_secret();
409
+		$fh = fopen($file, 'w');
410
+		fwrite($fh, '<?php $secret = "'.$secret.'";' . PHP_EOL);
411
+		fclose($fh);
412
+		return $secret;
413
+	}
414
+	return '';
415 415
 }
416 416
 
417 417
 /**
418 418
  * Generates a random string as the hash of time, microtime, and mt_rand.
419 419
  */
420 420
 function csrf_generate_secret($len = 32) {
421
-    $r = '';
422
-    for ($i = 0; $i < 32; $i++) {
423
-        $r .= chr(mt_rand(0, 255));
424
-    }
425
-    $r .= time() . microtime();
426
-    return sha1($r);
421
+	$r = '';
422
+	for ($i = 0; $i < 32; $i++) {
423
+		$r .= chr(mt_rand(0, 255));
424
+	}
425
+	$r .= time() . microtime();
426
+	return sha1($r);
427 427
 }
428 428
 
429 429
 /**
@@ -431,8 +431,8 @@  discard block
 block discarded – undo
431 431
  * from the current time.
432 432
  */
433 433
 function csrf_hash($value, $time = null) {
434
-    if (!$time) $time = time();
435
-    return sha1(csrf_get_secret() . $value . $time) . ',' . $time;
434
+	if (!$time) $time = time();
435
+	return sha1(csrf_get_secret() . $value . $time) . ',' . $time;
436 436
 }
437 437
 
438 438
 // Load user configuration
Please login to merge, or discard this patch.