Changeset 477
- Timestamp:
- May 3, 2014 3:13:19 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/App.inc.php
r476 r477 179 179 $this->timer = new ScriptTimer(); 180 180 $this->timer->start('_app'); 181 182 // The codebase now requires a minimum PHP version. 183 $codebase_minimum_php_version = '5.3.0'; 184 if (version_compare(PHP_VERSION, $codebase_minimum_php_version, '<')) { 185 $this->logMsg(sprintf('Codebase minimum PHP version of %s not satisfied (you have %s). ', $codebase_minimum_php_version, phpversion()), LOG_NOTICE, __FILE__, __LINE__); 186 } 181 187 } 182 188 … … 608 614 preg_match_all('/(<[^>\s]{7,})[^>]*>/', $message, $strip_tags_allow); 609 615 $message = strip_tags(preg_replace('/\s+/', ' ', $message), (!empty($strip_tags_allow[1]) ? join('> ', $strip_tags_allow[1]) . '>' : null)); 616 617 // Serialize multi-line messages. 618 $message = preg_replace('/\s+/m', ' ', $message); 610 619 611 620 // Store this event under a unique key, counting each time it occurs so that it only gets reported a limited number of times. … … 1241 1250 } 1242 1251 1243 1244 1252 /** 1245 1253 * to enforce the user to connect via http (port 80) by redirecting them to … … 1252 1260 } 1253 1261 } 1262 1263 /* 1264 * Sets a cookie, with error checking and some sane defaults. 1265 * 1266 * @access public 1267 * @param string $name The name of the cookie. 1268 * @param string $value The value of the cookie. 1269 * @param string $expire The time the cookie expires, as a unix timestamp or string value passed to strtotime. 1270 * @param string $path The path on the server in which the cookie will be available on 1271 * @param string $domain The domain that the cookie is available to 1272 * @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. 1273 * @param bool $httponly When TRUE the cookie will be made accessible only through the HTTP protocol (makes cookies unreadable to javascript). 1274 * @return bool True on success, false on error. 1275 * @author Quinn Comendant <quinn@strangecode.com> 1276 * @version 1.0 1277 * @since 02 May 2014 16:36:34 1278 */ 1279 public function setCookie($name, $value, $expire='+10 years', $path=null, $domain=null, $secure=null, $httponly=null) 1280 { 1281 if (!is_scalar($name)) { 1282 $this->logMsg(sprintf('Cookie name must be scalar, is not: %s', getDump($name)), LOG_NOTICE, __FILE__, __LINE__); 1283 return false; 1284 } 1285 if (!is_scalar($value)) { 1286 $this->logMsg(sprintf('Cookie "%s" value must be scalar, is not: %s', $name, getDump($value)), LOG_NOTICE, __FILE__, __LINE__); 1287 return false; 1288 } 1289 1290 // Defaults. 1291 $expire = (is_numeric($expire) ? $expire : (is_string($expire) ? strtotime($expire) : $expire)); 1292 $secure = $secure ?: ('' != getenv('HTTPS') && $this->getParam('ssl_enabled')); 1293 $httponly = $httponly ?: true; 1294 1295 // Make sure the expiration date is a valid 32bit integer. 1296 if (is_int($expire) && $expire > 2147483647) { 1297 $this->logMsg(sprintf('Cookie "%s" expire time exceeds a 32bit integer (%s)', $key, date('r', $expire)), LOG_NOTICE, __FILE__, __LINE__); 1298 } 1299 1300 // Measure total cookie length and warn if larger than max recommended size of 4093. 1301 // https://stackoverflow.com/questions/640938/what-is-the-maximum-size-of-a-web-browsers-cookies-key 1302 // The date the header name include 51 bytes: Set-Cookie: ; expires=Fri, 03-May-2024 00:04:47 GMT 1303 $cookielen = strlen($name . $value . $path . $domain . ($secure ? '; secure' : '') . ($httponly ? '; httponly' : '')) + 51; 1304 if ($cookielen > 4093) { 1305 $this->logMsg(sprintf('Cookie "%s" has a size greater than 4093 bytes (is %s bytes)', $key, $cookielen), LOG_NOTICE, __FILE__, __LINE__); 1306 } 1307 1308 // Ensure PHP version allow use of httponly. 1309 if (version_compare(PHP_VERSION, '5.2.0', '>=')) { 1310 $ret = setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); 1311 } else { 1312 $ret = setcookie($name, $value, $expire, $path, $domain, $secure); 1313 } 1314 1315 if (false === $ret) { 1316 $this->logMsg(sprintf('Failed to set cookie (%s=%s) probably due to output before headers.', $name, $value), LOG_NOTICE, __FILE__, __LINE__); 1317 } 1318 return $ret; 1319 } 1254 1320 } // End. -
trunk/lib/ImageThumb.inc.php
r468 r477 128 128 if (!is_dir($params['source_dir'])) { 129 129 $app->logMsg(sprintf('Attempting to auto-create source directory: %s', $params['source_dir']), LOG_NOTICE, __FILE__, __LINE__); 130 if ( phpversion() > '5') {130 if (version_compare(PHP_VERSION, '5.0.0', '>=')) { 131 131 // Recursive. 132 132 mkdir($params['source_dir'], isset($params['dest_dir_perms']) ? $params['dest_dir_perms'] : $this->getParam('dest_dir_perms'), true); … … 607 607 608 608 // Sharpen image using a custom filter matrix. 609 if ( phpversion() > '5.1'&& true === $spec['sharpen'] && $spec['sharpen_value'] > 0) {609 if (version_compare(PHP_VERSION, '5.1.0', '>=') && true === $spec['sharpen'] && $spec['sharpen_value'] > 0) { 610 610 $sharpen_value = round((((48 - 10) / (100 - 1)) * (100 - $spec['sharpen_value'])) + 10); // TODO: WTF is this math? 611 611 imageconvolution($dest_image_resource, array(array(-1, -1, -1), array(-1, $sharpen_value, -1),array(-1, -1, -1)), ($sharpen_value - 8), 0); -
trunk/lib/Prefs.inc.php
r468 r477 30 30 * @version 2.1 31 31 * 32 * Example of use :32 * Example of use (database storagetype): 33 33 --------------------------------------------------------------------- 34 34 // Load preferences for the user's session. … … 36 36 $prefs = new Prefs('my-namespace'); 37 37 $prefs->setParam(array( 38 ' persistent' => $auth->isLoggedIn(),38 'storagetype' => ($auth->isLoggedIn() ? 'database' : 'session'), 39 39 'user_id' => $auth->get('user_id'), 40 40 )); … … 49 49 $prefs->set('datalog_num_entries', getFormData('datalog_num_entries')); 50 50 $prefs->save(); 51 52 51 --------------------------------------------------------------------- 53 52 */ … … 60 59 private $_params = array( 61 60 61 // Legacy parameter, superceeded by the 'storagetype' setting. 62 62 // Enable database storage. If this is false, all prefs will live only as long as the session. 63 'persistent' => false, 64 65 // The current user_id for which to load/save persistent preferences. 63 'persistent' => null, 64 65 // Store preferences in one of the available storage mechanisms: session, cookie, database 66 'storagetype' => 'session', 67 68 // ---------------------------------------------------------- 69 // Cookie-type settings. 70 71 // Lifespan of the cookie. If set to an integer, interpreted as a timestamp (0 for 'when user closes browser'), otherwise as a strtotime-compatible value ('tomorrow', etc). 72 'cookie_expire' => '+10 years', 73 74 // The path on the server in which the cookie will be available on. 75 'cookie_path' => null, 76 77 // The domain that the cookie is available to. 78 'cookie_domain' => null, 79 80 // ---------------------------------------------------------- 81 // Database-type settings. 82 83 // The current user_id for which to load/save database-backed preferences. 66 84 'user_id' => null, 67 85 … … 82 100 public function __construct($namespace='') 83 101 { 84 102 $app =& App::getInstance(); 85 103 86 104 $this->_ns = $namespace; … … 106 124 public function initDB($recreate_db=false) 107 125 { 108 109 126 $app =& App::getInstance(); 127 $db =& DB::getInstance(); 110 128 111 129 static $_db_tested = false; … … 144 162 public function setParam($params=null) 145 163 { 164 // CLI scripts can't use prefs stored in HTTP-based protocols. 165 if (defined('_CLI') 166 && isset($params['storagetype']) 167 && in_array($params['storagetype'], array('cookie', 'session'))) { 168 $app->logMsg(sprintf('Storage type %s not available for CLI', $params['storagetype']), LOG_NOTICE, __FILE__, __LINE__); 169 } 170 171 // Convert the legacy param 'persistent' to 'storagetype=database'. 172 if (isset($params['persistent']) && $params['persistent'] && !isset($params['storagetype'])) { 173 $params['storagetype'] = 'database'; 174 } 175 146 176 if (isset($params) && is_array($params)) { 147 177 // Merge new parameters with old overriding only those passed. … … 159 189 public function getParam($param) 160 190 { 161 191 $app =& App::getInstance(); 162 192 163 193 if (isset($this->_params[$param])) { … … 190 220 * @param string $key The name of the preference to modify. 191 221 * @param string $val The new value for this preference. 192 * @param bool $persistent Save this value forever? Set to false and value will exist as long as the session is in use.193 222 */ 194 223 public function set($key, $val) … … 196 225 $app =& App::getInstance(); 197 226 198 if ('' == $key) { 199 $app->logMsg(sprintf('Key is empty (provided with value: %s)', $val), LOG_NOTICE, __FILE__, __LINE__); 227 if (!is_string($key)) { 228 $app->logMsg(sprintf('Key is not a string-compatible type (%s)', getDump($key)), LOG_NOTICE, __FILE__, __LINE__); 229 return false; 230 } 231 if ('' == trim($key)) { 232 $app->logMsg(sprintf('Key is empty (along with value: %s)', $val), LOG_NOTICE, __FILE__, __LINE__); 233 return false; 234 } 235 if (!is_scalar($val) && !is_array($val) && !is_object($val)) { 236 $app->logMsg(sprintf('Value is not a string-compatible type (%s=%s)', $key, getDump($val)), LOG_WARNING, __FILE__, __LINE__); 200 237 return false; 201 238 } … … 205 242 // - or the new value is different than the default 206 243 // - or there is a previously existing saved key. 207 if (!isset($_SESSION['_prefs'][$this->_ns]['defaults'][$key]) 208 || $_SESSION['_prefs'][$this->_ns]['defaults'][$key] != $val 209 || isset($_SESSION['_prefs'][$this->_ns]['saved'][$key])) { 210 $_SESSION['_prefs'][$this->_ns]['saved'][$key] = $val; 211 $app->logMsg(sprintf('Setting preference %s => %s', $key, truncate(getDump($val, true), 128, 'end')), LOG_DEBUG, __FILE__, __LINE__); 212 } else { 213 $app->logMsg(sprintf('Not setting preference %s => %s', $key, truncate(getDump($val, true), 128, 'end')), LOG_DEBUG, __FILE__, __LINE__); 214 } 244 switch ($this->getParam('storagetype')) { 245 case 'session': 246 case 'database': // DB prefs are saved in the session temporarily until they are saved. 247 if (!isset($_SESSION['_prefs'][$this->_ns]['defaults'][$key]) 248 || $_SESSION['_prefs'][$this->_ns]['defaults'][$key] != $val 249 || isset($_SESSION['_prefs'][$this->_ns]['saved'][$key])) { 250 $_SESSION['_prefs'][$this->_ns]['saved'][$key] = $val; 251 $app->logMsg(sprintf('Setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__); 252 } else { 253 $app->logMsg(sprintf('Not setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__); 254 } 255 break; 256 257 case 'cookie': 258 $name = $this->_getCookieName($key); 259 $val = json_encode($val); 260 $app->setCookie($name, $val, $this->getParam('cookie_expire'), $this->getParam('cookie_path'), $this->getParam('cookie_domain')); 261 $app->logMsg(sprintf('Setting cookie preference %s => %s', $key, $val), LOG_DEBUG, __FILE__, __LINE__); 262 break; 263 } 264 215 265 } 216 266 … … 226 276 { 227 277 $app =& App::getInstance(); 228 if (isset($_SESSION['_prefs'][$this->_ns]['saved']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['saved'])) { 229 $app->logMsg(sprintf('Found %s in saved', $key), LOG_DEBUG, __FILE__, __LINE__); 230 return $_SESSION['_prefs'][$this->_ns]['saved'][$key]; 231 } else if (isset($_SESSION['_prefs'][$this->_ns]['defaults']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['defaults'])) { 232 $app->logMsg(sprintf('Found %s in defaults', $key), LOG_DEBUG, __FILE__, __LINE__); 233 return $_SESSION['_prefs'][$this->_ns]['defaults'][$key]; 234 } else { 235 $app->logMsg(sprintf('Key not found in prefs cache: %s', $key), LOG_DEBUG, __FILE__, __LINE__); 236 return null; 278 279 switch ($this->getParam('storagetype')) { 280 case 'session': 281 case 'database': 282 if (isset($_SESSION['_prefs'][$this->_ns]['saved']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['saved'])) { 283 $app->logMsg(sprintf('Found %s in saved', $key), LOG_DEBUG, __FILE__, __LINE__); 284 return $_SESSION['_prefs'][$this->_ns]['saved'][$key]; 285 } else if (isset($_SESSION['_prefs'][$this->_ns]['defaults']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['defaults'])) { 286 $app->logMsg(sprintf('Found %s in defaults', $key), LOG_DEBUG, __FILE__, __LINE__); 287 return $_SESSION['_prefs'][$this->_ns]['defaults'][$key]; 288 } else { 289 $app->logMsg(sprintf('Key not found in prefs cache: %s', $key), LOG_DEBUG, __FILE__, __LINE__); 290 return null; 291 } 292 break; 293 294 case 'cookie': 295 $name = $this->_getCookieName($key); 296 if ($this->exists($key)) { 297 $val = json_decode($_COOKIE[$name]); 298 $app->logMsg(sprintf('Found %s in cookie: %s', $key, getDump($val)), LOG_DEBUG, __FILE__, __LINE__); 299 return $val; 300 } else { 301 $app->logMsg(sprintf('Key not found in cookie: %s', $key), LOG_DEBUG, __FILE__, __LINE__); 302 return null; 303 } 304 break; 237 305 } 238 306 } … … 246 314 public function exists($key) 247 315 { 248 return array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['saved']); 316 switch ($this->getParam('storagetype')) { 317 case 'session': 318 case 'database': 319 return array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['saved']); 320 321 case 'cookie': 322 $name = $this->_getCookieName($key); 323 return isset($_COOKIE[$name]); 324 } 325 249 326 } 250 327 … … 256 333 public function delete($key) 257 334 { 258 unset($_SESSION['_prefs'][$this->_ns]['saved'][$key]); 335 $app =& App::getInstance(); 336 337 switch ($this->getParam('storagetype')) { 338 case 'session': 339 case 'database': 340 unset($_SESSION['_prefs'][$this->_ns]['saved'][$key]); 341 break; 342 343 case 'cookie': 344 if ($this->exists($key)) { 345 // Just set the existing value to an empty string, which expires in the past. 346 $name = $this->_getCookieName($key); 347 $app->setCookie($name, '', time() - 86400); 348 // Also unset the received cookie value, so it is unavailable. 349 unset($_COOKIE[$name]); 350 } 351 break; 352 } 353 259 354 } 260 355 … … 265 360 public function clear($focus='all') 266 361 { 362 267 363 switch ($focus) { 268 364 case 'all' : 269 $_SESSION['_prefs'][$this->_ns] = array( 270 'loaded' => false, 271 'load_datetime' => '1970-01-01', 272 'defaults' => array(), 273 'saved' => array(), 274 ); 275 break; 276 365 switch ($this->getParam('storagetype')) { 366 case 'session': 367 case 'database': 368 $_SESSION['_prefs'][$this->_ns] = array( 369 'loaded' => false, 370 'load_datetime' => '1970-01-01', 371 'defaults' => array(), 372 'saved' => array(), 373 ); 374 break; 375 case 'cookie': 376 foreach ($_COOKIE as $key => $value) { 377 // All cookie keys with our internal prefix. Use only the last part as the key. 378 if (preg_match('/^' . preg_quote(sprintf('strangecode-%s-', $this->_ns)) . '(.+)$/i', $key, $match)) { 379 $this->delete($match[1]); 380 } 381 } 382 break; 383 } 384 break; 277 385 case 'defaults' : 278 386 $_SESSION['_prefs'][$this->_ns]['defaults'] = array(); 279 387 break; 280 281 388 case 'saved' : 282 389 $_SESSION['_prefs'][$this->_ns]['saved'] = array(); … … 298 405 { 299 406 $app =& App::getInstance(); 300 301 302 303 if (true !== $this->getParam('persistent')) {304 305 407 $db =& DB::getInstance(); 408 409 // Skip this method if not using the db. 410 if ('database' != $this->getParam('storagetype')) { 411 return true; 412 } 306 413 307 414 $this->initDB(); 308 415 309 416 // Prefs already loaded for this session. 310 311 312 417 if (!$force && $this->_isLoaded()) { 418 return true; 419 } 313 420 314 421 // User_id must not be empty. … … 322 429 323 430 // Retrieve all prefs for this user and namespace. 324 325 326 327 328 329 330 331 431 $qid = $db->query(" 432 SELECT pref_key, pref_value 433 FROM " . $db->escapeString($this->getParam('db_table')) . " 434 WHERE user_id = '" . $db->escapeString($this->getParam('user_id')) . "' 435 AND pref_namespace = '" . $db->escapeString($this->_ns) . "' 436 LIMIT 10000 437 "); 438 while (list($key, $val) = mysql_fetch_row($qid)) { 332 439 $_SESSION['_prefs'][$this->_ns]['saved'][$key] = unserialize($val); 333 334 335 336 337 338 440 } 441 442 $app->logMsg(sprintf('Loaded %s prefs from database.', mysql_num_rows($qid)), LOG_DEBUG, __FILE__, __LINE__); 443 444 // Data loaded only once per session. 445 $_SESSION['_prefs'][$this->_ns]['loaded'] = true; 339 446 $_SESSION['_prefs'][$this->_ns]['load_datetime'] = date('Y-m-d H:i:s'); 340 447 341 448 return true; 342 449 } 343 450 … … 354 461 private function _isLoaded() 355 462 { 463 if ('database' != $this->getParam('storagetype')) { 464 return true; 465 } 466 356 467 if (isset($_SESSION['_prefs'][$this->_ns]['load_datetime']) 357 468 && strtotime($_SESSION['_prefs'][$this->_ns]['load_datetime']) > time() - $this->getParam('load_timeout') … … 376 487 { 377 488 $app =& App::getInstance(); 378 379 380 381 if (true !== $this->getParam('persistent')) {382 383 489 $db =& DB::getInstance(); 490 491 // Skip this method if not using the db. 492 if ('database' != $this->getParam('storagetype')) { 493 return true; 494 } 384 495 385 496 // User_id must not be empty. … … 395 506 $db->query(" 396 507 DELETE FROM " . $db->escapeString($this->getParam('db_table')) . " 397 398 508 WHERE user_id = '" . $db->escapeString($this->getParam('user_id')) . "' 509 AND pref_namespace = '" . $db->escapeString($this->_ns) . "' 399 510 "); 400 511 … … 422 533 return false; 423 534 } 535 536 /* 537 * 538 * 539 * @access public 540 * @param 541 * @return 542 * @author Quinn Comendant <quinn@strangecode.com> 543 * @version 1.0 544 * @since 02 May 2014 18:17:04 545 */ 546 private function _getCookieName($key) 547 { 548 $app =& App::getInstance(); 549 550 if (mb_strpos($key, sprintf('strangecode-%s', $this->_ns)) === 0) { 551 $app->logMsg(sprintf('Invalid key name (%s). Leave off "strangecode-%s-" and it should work.', $key, $this->_ns), LOG_NOTICE, __FILE__, __LINE__); 552 } 553 return sprintf('strangecode-%s-%s', $this->_ns, $key); 554 } 424 555 } 425 556 -
trunk/lib/Upload.inc.php
r468 r477 86 86 if (!is_dir($params['upload_path'])) { 87 87 $app->logMsg(sprintf('Attempting to auto-create upload directory: %s', $params['upload_path']), LOG_NOTICE, __FILE__, __LINE__); 88 if ( phpversion() > '5') {88 if (version_compare(PHP_VERSION, '5.0.0', '>=')) { 89 89 // Recursive. 90 90 mkdir($params['upload_path'], isset($params['dest_dir_perms']) ? $params['dest_dir_perms'] : $this->getParam('dest_dir_perms'), true); -
trunk/lib/Utilities.inc.php
r474 r477 29 29 * Print variable dump. 30 30 * 31 * @param mixed $var Variable to dump. 32 * @param bool $display Hide the dump in HTML comments? 33 * @param bool $var_dump Use var_dump instead of print_r. 31 * @param mixed $var The variable to dump. 32 * @param bool $display Print the dump in <pre> tags or hide it in html comments (non-CLI only). 33 * @param bool $var_dump Use var_dump instead of print_r. 34 * @param string $file Value of __FILE__. 35 * @param string $line Value of __LINE__ 34 36 */ 35 37 function dump($var, $display=false, $var_dump=false, $file='', $line='') 36 38 { 37 if (empty($var)) {38 $var = '(nothing to dump)';39 }40 39 if (defined('_CLI')) { 41 echo " \nDUMP FROM: $file $line\n";42 } else { 43 echo $display ? "\n<br />DUMP : <strong>$file $line</strong>\n<br /><pre>\n" : "\n\n\n<!--\n$file $line\n";40 echo "DUMP FROM: $file $line\n"; 41 } else { 42 echo $display ? "\n<br />DUMP <strong>$file $line</strong><br /><pre>\n" : "\n<!-- DUMP $file $line\n"; 44 43 } 45 44 if ($var_dump) { 46 45 var_dump($var); 47 46 } else { 48 print_r($var); 47 // Print human-readable descriptions of invisible types. 48 if (null === $var) { 49 echo '(null)'; 50 } else if (true === $var) { 51 echo '(bool: true)'; 52 } else if (false === $var) { 53 echo '(bool: false)'; 54 } else if (is_scalar($var) && '' === $var) { 55 echo '(empty string)'; 56 } else if (is_scalar($var) && preg_match('/^\s+$/', $var)) { 57 echo '(only white space)'; 58 } else { 59 print_r($var); 60 } 49 61 } 50 62 if (defined('_CLI')) { 51 63 echo "\n"; 52 64 } else { 53 echo $display ? "\n</pre><br />\n" : "\n-->\n\n\n";65 echo $display ? "\n</pre><br />\n" : "\n-->\n"; 54 66 } 55 67 } -
trunk/services/login.php
r468 r477 32 32 $app->sslOn(); 33 33 34 // Cookie-based storage preferences. 34 35 require_once 'codebase/lib/Prefs.inc.php'; 35 36 $login_prefs = new Prefs('login'); 36 $login_prefs->set Defaults(array('username' => ''));37 $login_prefs->setParam(array('storagetype' => 'cookie')); 37 38 38 39 $frm['username'] = getFormdata('username', $login_prefs->get('username'));
Note: See TracChangeset
for help on using the changeset viewer.