Changeset 9243
- Timestamp:
- 08/13/08 22:27:02 (4 months ago)
- Files:
-
- trunk/jaws/html/include/Jaws/Registry.php (modified) (27 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/jaws/html/include/Jaws/Registry.php
r9235 r9243 10 10 * @license http://www.gnu.org/copyleft/lesser.html 11 11 */ 12 define('JAWS_REGISTRY_CORE', 0);13 define('JAWS_REGISTRY_GADGET', 1);14 define('JAWS_REGISTRY_PLUGIN', 2);15 16 12 class Jaws_Registry 17 13 { … … 35 31 36 32 /** 37 * Array that has a *registry* of files that have been called33 * Array that has a registry keys of loaded components 38 34 * 39 35 * @var array 40 36 * @access private 41 37 */ 42 var $_Loaded Files = array();38 var $_LoadedComponents = array(); 43 39 44 40 /** … … 50 46 { 51 47 $params = array(); 52 $params['enabled'] = ' version';48 $params['enabled'] = 'enabled'; 53 49 $params['version'] = 'version'; 54 50 … … 56 52 $sql = " 57 53 SELECT 58 [comp_name], [comp_type], [key_name], [key_value] 59 FROM [[registry]] 54 [[components]].[comp_name], [key_name], [key_value] 55 FROM [[registry]] INNER JOIN [[components]] 56 ON [[registry]].[comp_id] = [[components]].[id] 60 57 WHERE 61 58 [key_name] = {enabled} … … 69 66 } 70 67 $this->_Registry = $result; 71 $this->Load File('core');68 $this->LoadComponentKeys('', JAWS_COMPONENT_CORE); 72 69 } 73 70 … … 78 75 * @param string $comp Component's name 79 76 * @param string $name The key 80 * @param string $type The type of the component, (core, plugin or a gadget)81 77 * @return string The value of the key 82 78 */ 83 function GetFromTable($comp, $name , $type = JAWS_REGISTRY_CORE)79 function GetFromTable($comp, $name) 84 80 { 85 81 $params = array(); 86 82 $params['comp_name'] = $comp; 87 $params['comp_type'] = $type;88 83 $params['key_name'] = $name; 89 84 … … 91 86 SELECT 92 87 [key_value] 93 FROM [[registry]] 88 FROM [[registry]] INNER JOIN [[components]] 89 ON [[registry]].[comp_id] = [[components]].[id] 94 90 WHERE 95 [comp_name] = {comp_name} 96 AND 97 [comp_type] = {comp_type} 91 [[components]].[comp_name] = {comp_name} 98 92 AND 99 93 [key_name] = {key_name}"; … … 104 98 } 105 99 100 //FIXME: maybe value really is empty 106 101 if (!empty($value)) { 107 102 // lets update the internal array just in case 108 $this->_Registry[$ type][$comp][$name] = $value;103 $this->_Registry[$comp][$name] = $value; 109 104 return $value; 110 105 } … … 119 114 * @param string $name The key 120 115 * @param string $comp Component's name 121 * @param string $type The type of the component, (core, plugin or a gadget)122 116 * @return bool true when the key was found, else false 123 117 */ 124 function KeyExists($name, $comp = 'Jaws' , $type = JAWS_REGISTRY_CORE)125 { 126 return array_key_exists($name, $this->_Registry[$ type][$comp]);118 function KeyExists($name, $comp = 'Jaws') 119 { 120 return array_key_exists($name, $this->_Registry[$comp]); 127 121 } 128 122 … … 133 127 * @param string $name The key 134 128 * @param string $comp Component's name 135 * @param string $type The type of the component, (core, plugin or a gadget)136 129 * @return string The value of the key 137 130 */ 138 function Get($name, $comp = 'Jaws' , $type = JAWS_REGISTRY_CORE)139 { 140 return $this->KeyExists($name, $comp , $type) ? $this->_Registry[$type][$comp][$name] : null;131 function Get($name, $comp = 'Jaws') 132 { 133 return $this->KeyExists($name, $comp) ? $this->_Registry[$comp][$name] : null; 141 134 } 142 135 … … 159 152 MDB2::loadFile('Date'); 160 153 $params = array(); 161 $params['comp_name'] = 'Registry'; 162 $params['comp_type'] = JAWS_REGISTRY_CORE; 163 $params['key_name'] = 'last_update'; 164 $params['now'] = MDB2_Date::mdbNow(); 154 $params['comp_id'] = $this->_LoadedComponents['Registry']; 155 $params['key_name'] = 'last_update'; 156 $params['now'] = MDB2_Date::mdbNow(); 165 157 166 158 $sql = " … … 168 160 [key_value] = {now} 169 161 WHERE 170 [comp_ name] = {comp_name}162 [comp_id] = {comp_id} 171 163 AND 172 [comp_type] = {comp_type} 173 AND 174 [key_name] = {key_name}"; 164 [[registry]].[key_name] = {key_name}"; 175 165 176 166 $result = $GLOBALS['db']->query($sql, $params); … … 179 169 } 180 170 181 $this->_LastUpdate = $this->_Registry[ JAWS_REGISTRY_CORE]['Registry']['last_updated'] = $params['now'];171 $this->_LastUpdate = $this->_Registry['Registry']['last_updated'] = $params['now']; 182 172 return true; 183 173 } … … 190 180 * @param string $value The value 191 181 * @param string $comp Component's name 192 * @param string $type The type of the component, (core, plugin or a gadget) 193 */ 194 function Set($name, $value, $comp = 'Jaws', $type = JAWS_REGISTRY_CORE) 195 { 196 if (!$this->KeyExists($name, $comp, $type)) { 182 */ 183 function Set($name, $value, $comp = 'Jaws') 184 { 185 if (!array_key_exists($comp, $this->_LoadedComponents) || !$this->KeyExists($name, $comp)) { 197 186 return false; 198 187 } … … 200 189 $xss = $GLOBALS['app']->loadClass('XSS', 'Jaws_XSS'); 201 190 $params = array(); 202 $params['comp_name'] = $comp; 203 $params['comp_type'] = $type; 191 $params['comp_id'] = $this->_LoadedComponents[$comp]; 204 192 $params['key_name'] = $name; 205 193 $params['key_value'] = $xss->parse($value); … … 209 197 [key_value] = {key_value} 210 198 WHERE 211 [comp_ name] = {comp_name}199 [comp_id] = {comp_id} 212 200 AND 213 [comp_type] = {comp_type} 214 AND 215 [key_name] = {key_name}"; 201 [[registry]].[key_name] = {key_name}"; 216 202 217 203 $result = $GLOBALS['db']->query($sql, $params); … … 220 206 } 221 207 222 $this->_Registry[$ type][$comp][$name] = $params['value'];208 $this->_Registry[$comp][$name] = $params['value']; 223 209 return $this->UpdateLastUpdate(); 224 210 } … … 231 217 * @param string $value The value 232 218 * @param string $comp Component's name 233 * @param string $type The type of the component, (core, plugin or a gadget) 234 */ 235 function NewKey($name, $value, $comp = 'Jaws', $type = JAWS_REGISTRY_CORE) 236 { 237 if (!$this->KeyExists($name, $comp, $type)) { 238 return false; //already exists 219 */ 220 function NewKey($name, $value, $comp = 'Jaws') 221 { 222 if (!array_key_exists($comp, $this->_LoadedComponents) || !$this->KeyExists($name, $comp)) { 223 return false; 239 224 } 240 225 … … 243 228 244 229 $params = array(); 245 $params['comp_name'] = $comp; 246 $params['comp_type'] = $type; 230 $params['comp_id'] = $this->_LoadedComponents[$comp]; 247 231 $params['key_name'] = $name; 248 232 $params['key_value'] = $xss->parse($value); … … 251 235 $sql = " 252 236 INSERT INTO [[registry]] 253 ([comp_ name], [comp_type], [key_name], [key_value], [updatetime])237 ([comp_id], [key_name], [key_value], [updatetime]) 254 238 VALUES 255 ({comp_ name}, {comp_type}, {key_name}, {key_value}, {now})";239 ({comp_id}, {key_name}, {key_value}, {now})"; 256 240 257 241 $result = $GLOBALS['db']->query($sql, $params); … … 260 244 } 261 245 262 $this->_Registry[$ type][$comp][$name] = $params['value'];246 $this->_Registry[$comp][$name] = $params['value']; 263 247 return $this->UpdateLastUpdate(); 264 248 } … … 284 268 $xss = $GLOBALS['app']->loadClass('XSS', 'Jaws_XSS'); 285 269 foreach ($reg_keys as $idx => $reg_key) { 286 if ( $this->KeyExists($reg_key[0], $reg_key[2], $reg_key[3])) {270 if (!array_key_exists($reg_key[2], $this->_LoadedComponents) || $this->KeyExists($reg_key[0], $reg_key[2])) { 287 271 unset($reg_keys[$idx]); 288 272 } else { 289 $params["comp_$idx"] = $ reg_key[2];273 $params["comp_$idx"] = $this->_LoadedComponents[$reg_key[2]]; 290 274 $params["name_$idx"] = $reg_key[0]; 291 275 $params["value_$idx"] = $xss->parse($reg_key[1]); 292 $params["type_$idx"] = $reg_key[3];293 276 // Ugly hack to support all databases 294 277 switch ($GLOBALS['db']->_dsn['phptype']) { 295 278 case 'oci8': 296 279 $sqls .= (empty($sqls)? '' : "\n UNION ALL"). 297 "\n SELECT {comp_$idx}, { type_$idx}, {name_$idx}, {value_$idx}, {now} FROM DUAL";280 "\n SELECT {comp_$idx}, {name_$idx}, {value_$idx}, {now} FROM DUAL"; 298 281 break; 299 282 case 'ibase': 300 $sqls[] = " VALUES ({comp_$idx}, { type_$idx}, {name_$idx}, {value_$idx}, {now})";283 $sqls[] = " VALUES ({comp_$idx}, {name_$idx}, {value_$idx}, {now})"; 301 284 break; 302 285 case 'pgsql': 303 286 $sqls .= (empty($sqls)? "\n VALUES" : ","). 304 "\n ({comp_$idx}, { type_$idx}, {name_$idx}, {value_$idx}, {now})";287 "\n ({comp_$idx}, {name_$idx}, {value_$idx}, {now})"; 305 288 break; 306 289 default: 307 290 $sqls .= (empty($sqls)? '' : "\n UNION ALL"). 308 "\n SELECT {comp_$idx}, { type_$idx}, {name_$idx}, {value_$idx}, {now}";291 "\n SELECT {comp_$idx}, {name_$idx}, {value_$idx}, {now}"; 309 292 break; 310 293 } … … 321 304 if (is_array($sqls)) { 322 305 foreach ($sqls as $sql) { 323 $qsql = " INSERT INTO [[registry]]([comp_ name], [comp_type], [key_name], [key_value], [updatetime])" . $sql;306 $qsql = " INSERT INTO [[registry]]([comp_id], [key_name], [key_value], [updatetime])" . $sql; 324 307 $result = $GLOBALS['db']->query($qsql, $params); 325 308 if (Jaws_Error::IsError($result)) { … … 328 311 } 329 312 } else { 330 $qsql = " INSERT INTO [[registry]]([comp_ name], [comp_type], [key_name], [key_value], [updatetime])" . $sqls;313 $qsql = " INSERT INTO [[registry]]([comp_id], [key_name], [key_value], [updatetime])" . $sqls; 331 314 $result = $GLOBALS['db']->query($qsql, $params); 332 315 if (Jaws_Error::IsError($result)) { … … 337 320 foreach ($reg_keys as $idx => $reg_key) { 338 321 if (empty($reg_keys[$idx])) continue; 339 $this->_Registry[$reg_key[ 3]][$reg_key[0]][$reg_key[1]] = $reg_key[2];322 $this->_Registry[$reg_key[0]][$reg_key[1]] = $reg_key[2]; 340 323 } 341 324 … … 349 332 * @param string $name The key 350 333 * @param string $comp Component's name 351 * @param string $type The type of the component, (core, plugin or a gadget) 352 */ 353 function DeleteKey($name, $comp, $type = JAWS_REGISTRY_CORE) 354 { 355 if ($this->KeyExists($name, $comp, $type)) { 356 unset($this->_Registry[$type][$comp][$name]); 357 } 358 334 */ 335 function DeleteKey($name, $comp) 336 { 337 if (!array_key_exists($comp, $this->_LoadedComponents) || !$this->KeyExists($name, $comp)) { 338 unset($this->_Registry[$comp][$name]); 339 } 340 341 $params = array(); 342 $params['comp_id'] = $this->_LoadedComponents[$comp]; 343 $params['key_name'] = $name; 344 345 $sql = " 346 DELETE FROM [[registry]] 347 WHERE 348 [comp_id] = {comp_id} 349 AND 350 [key_name] = {key_name}"; 351 352 $result = $GLOBALS['db']->query($sql, $params); 353 if (Jaws_Error::IsError($result)) { 354 return false; 355 } 356 357 $this->UpdateLastUpdate(); 358 } 359 360 /** 361 * Get the simple array 362 * 363 * @access public 364 * @return array Returns the SimpleArray 365 */ 366 function GetSimpleArray() 367 { 368 return $this->_Registry; 369 } 370 371 /** 372 * Saves the key array file in JAWS_DATA . '/cache/registry/(gadgets|plugins)' . $component 373 * 374 * @access public 375 * @param string $comp Component's name 376 * @param string $regexp Regexp used when we check 'core' 377 */ 378 function Commit($comp) 379 { 380 return true; 381 } 382 383 /** 384 * Loads the keys of a component 385 * 386 * @access public 387 * @param string $comp Component's name 388 * @param string $type The type of the component, (core, gadget, plugin or any components) 389 */ 390 function LoadComponentKeys($comp = '', $type = JAWS_COMPONENT_ANY) 391 { 359 392 $params = array(); 360 393 $params['comp_name'] = $comp; 361 394 $params['comp_type'] = $type; 362 $params['key_name'] = $name; 363 364 $sql = " 365 DELETE FROM [[registry]] 366 WHERE 367 [comp_name] = {comp_name} 368 AND 369 [comp_type] = {comp_type} 370 AND 371 [key_name] = {key_name}"; 372 373 $result = $GLOBALS['db']->query($sql, $params); 374 if (Jaws_Error::IsError($result)) { 375 return false; 376 } 377 378 $this->UpdateLastUpdate(); 379 } 380 381 /** 382 * Get the simple array 383 * 384 * @access public 385 * @return array Returns the SimpleArray 386 */ 387 function GetSimpleArray() 388 { 389 return $this->_Registry; 390 } 391 392 /** 393 * Saves the key array file in JAWS_DATA . '/cache/registry/(gadgets|plugins)' . $component 394 * 395 * @access public 396 * @param string $comp Component's name 397 * @param string $type The type of the component, (plugin or a gadget) only 398 * if the component name is not empty 399 * @param string $regexp Regexp used when we check 'core' 400 */ 401 function Commit($comp, $type = JAWS_REGISTRY_CORE) 402 { 403 $dirs = array(); 404 $dirs['cache'] = JAWS_DATA. 'cache'; 405 $dirs['registry'] = $dirs['cache']. DIRECTORY_SEPARATOR. 'registry'; 406 407 $result = "\$registry = array();\n"; 408 if ($type == JAWS_REGISTRY_CORE) { 409 // Reorder the array so that the output will be easier to read 410 ksort($this->_Registry[$type]); 411 foreach ($this->_Registry[$type] as $core_comp => $comp_items) { 412 foreach ($comp_items as $key => $value) { 413 $result .= "\$registry['".$core_comp."']['".$key."'] = '".addslashes($value)."';\n"; 414 } 415 } 416 $comp_file = $dirs['registry']. DIRECTORY_SEPARATOR. 'core.php'; 417 } else { 418 // Reorder the array so that the output will be easier to read 419 ksort($this->_Registry[$type][$comp]); 420 foreach ($this->_Registry[$type][$comp] as $key => $value) { 421 if ($key != 'enabled' || $key != 'version') { 422 $result .= "\$registry['".$key."'] = '".addslashes($value)."';\n"; 423 } 424 } 425 426 switch ($type) { 427 case JAWS_REGISTRY_GADGET: 428 $dirs['gadgets'] = $dirs['registry']. DIRECTORY_SEPARATOR. 'gadgets'; 429 $comp_file = $dirs['gadgets']. DIRECTORY_SEPARATOR. "$comp.php"; 430 break; 431 case JAWS_REGISTRY_PLUGIN: 432 $dirs['plugins'] = $dirs['registry']. DIRECTORY_SEPARATOR. 'plugins'; 433 $comp_file = $dirs['plugins']. DIRECTORY_SEPARATOR. "$comp.php"; 434 break; 435 } 436 } 437 438 $result = "<?php\n" . $result; 439 foreach ($dirs as $dir) { 440 if (!Jaws_Utils::mkdir($dir)) { 441 return new Jaws_Error(_t('GLOBAL_ERROR_REGISTRY_CACHEDIR_NOT_WRITABLE', $dir), 'Jaws_Registry'); 442 } 443 } 444 445 if (false == file_put_contents($comp_file, $result)) { 446 return new Jaws_Error(_t('GLOBAL_ERROR_REGISTRY_CACHEDIR_NOT_WRITABLE', $comp_file), 'Jaws_Registry'); 447 } 448 449 Jaws_Utils::chmod($comp_file); 395 396 $sql = " 397 SELECT 398 [[components]].[comp_name], [key_name], [key_value] 399 FROM [[registry]] INNER JOIN [[components]] 400 ON [[registry]].[comp_id] = [[components]].[id]"; 401 402 if (!empty($comp)) { 403 $sql .= " WHERE [[components]].[comp_name] = {comp_name}"; 404 } elseif (!empty($type)) { 405 $sql .= " WHERE [[components]].[comp_type] = {comp_type}"; 406 } 407 408 $regKeys = $GLOBALS['db']->queryAll($sql, $params, null, null, true); 409 if (Jaws_Error::isError($regKeys)) { 410 return false; 411 } 412 413 $sql = " 414 SELECT 415 [comp_name], [id] 416 FROM [[components]]" 417 418 if (!empty($comp)) { 419 $sql .= " WHERE [comp_name] = {comp_name}"; 420 } elseif (!empty($type)) { 421 $sql .= " WHERE [comp_type] = {comp_type}"; 422 } 423 $components = $GLOBALS['db']->queryAll($sql, $params, null, null, true); 424 if (Jaws_Error::isError($components)) { 425 return false; 426 } 427 428 $this->_Registry = $regKeys + $this->_Registry; 429 $this->_LoadedComponents = components + $this->_LoadedComponents; 430 450 431 return true; 451 432 } 452 433 453 434 /** 454 * Loads the keys of a component and optionally it returns the keys found in the file455 *456 * @access public457 * @param string $component Component's name458 */459 function LoadFile($component, $type = 'gadgets', $return = false)460 {461 $type = strtolower($type);462 unset($this->_LoadedFiles['core']);463 if ($component === '' || !in_array($type, array('gadgets', 'plugins')) || in_array($component, $this->_LoadedFiles)) {464 return;465 }466 $add = $component !== 'core' ? $type . '/' : '';467 $file = JAWS_DATA . 'cache/registry/' . $add . $component . '.php';468 $exists = file_exists($file);469 if ($exists) {470 require $file;471 $this->_LoadedFiles[$component] = $component;472 // $registry comes from the file loaded473 if (isset($registry) && is_array($registry)) {474 foreach ($registry as $key => $value) {475 if (!$this->KeyExists($key)) {476 $this->_Registry[$key] = stripslashes($value);477 }478 }479 480 if ($return) {481 return $registry;482 }483 }484 485 return true;486 } else {487 // Cache file doesn't exist, lets generate it488 $res = $this->_regenerateInternalRegistry($component, $type);489 if (!$res) {490 return;491 }492 $this->commit($component, $type);493 return $this->loadFile($component, $type, $return);494 }495 }496 497 /**498 * Regenerates/updates the internal registry array ($this->_Registry)499 *500 * @access protected501 * @param string $component Component name502 * @param string $type Type of component (gadget or plugin)503 * @return boolean Success/Failure504 */505 function _regenerateInternalRegistry($component, $type = 'gadgets')506 {507 $type = strtolower($type);508 if (!in_array($type, array('gadgets', 'plugins'))) {509 return false;510 }511 512 if ($component == 'core') {513 // Big ass query since the core isn't under one namespace514 $sql = "515 SELECT [component], [key_name], [key_value]516 FROM [[registry]]517 WHERE518 [key_name] LIKE '/config/%'519 OR520 [key_name] LIKE '/map/%'521 OR522 [key_name] LIKE '/network/%'523 OR524 [key_name] LIKE '/policy/%'525 OR526 [key_name] LIKE '/crypt/%'527 OR528 [key_name] IN('/version', '/last_updated', '/plugins/parse_text/enabled_items',529 '/gadgets/enabled_items', '/gadgets/allowurl_items',530 '/gadgets/autoload_items', '/gadgets/core_items')531 ";532 } else {533 if ($type == 'gadgets') {534 $sql = "SELECT [component], [key_name], [key_value] FROM [[registry]] WHERE [key_name] LIKE '/gadgets/".$component."/%'";535 } else {536 $sql = "537 SELECT [component], [key_name], [key_value] FROM [[registry]]538 WHERE [key_name] LIKE '/plugins/".$component."/%' OR [key_name] LIKE '/plugins/parse_text/".$component."/%'";539 }540 }541 542 $result = $GLOBALS['db']->queryAll($sql, array(), null, null, true);543 if (Jaws_Error::isError($result)) {544 return false;545 }546 547 $this->_Registry = $result + $this->_Registry;548 return true;549 }550 551 /**552 * Loads all the component files553 *554 * @access public555 */556 function LoadAllFiles()557 {558 ///FIXME check for errors559 $gs = explode(',', $this->get('/gadgets/enabled_items'));560 $ci = explode(',', $this->get('/gadgets/core_items'));561 $ps = explode(',', $this->get('/plugins/parse_text/enabled_items'));562 563 $ci = str_replace(' ', '', $ci);564 $ps = str_replace(' ', '', $ps);565 566 // load the core567 $this->LoadFile('core');568 569 foreach ($gs as $gadget) {570 $this->LoadFile($gadget);571 }572 573 foreach ($ci as $gadget) {574 $this->LoadFile($gadget);575 }576 577 foreach ($ps as $plugin) {578 $this->LoadFile($plugin, 'plugins');579 }580 }581 582 /**583 * Deletes the cache file of a certain component584 *585 * @acess public586 * @access protected587 * @param string $name Component name588 * @param string $type Type of component (gadget or plugin)589 * @return boolean Success/Failure590 */591 function deleteCacheFile($name, $type = 'gadgets')592 {593 $type = strtolower($type);594 if (empty($name) || !in_array($type, array('gadgets', 'plugins'))) {595 return false;596 }597 598 $add = $name !== 'core' ? $type . '/' : '';599 600 $file = JAWS_DATA . 'cache/registry/' . $add . $name . '.php';601 if (file_exists($file)) {602 unlink($file);603 return true;604 }605 return false;606 }607 608 /**609 435 * Resets/Cleans the registry 610 436 * … … 614 440 { 615 441 $this->_Registry = array(); 616 $this->_Loaded Files = array();442 $this->_LoadedComponents = array(); 617 443 } 618 444
