| 
									
										
										
										
											2009-08-21 15:22:02 -04:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Laconica - a distributed open-source microblogging tool | 
					
						
							|  |  |  |  * Copyright (C) 2008, 2009, Control Yourself, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the | 
					
						
							|  |  |  |  * GNU Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.     If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-27 11:50:11 +01:00
										 |  |  | if (!defined('GNUSOCIAL')) { exit(1); } | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-21 15:22:02 -04:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Table Definition for config | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  | class Config extends Managed_DataObject | 
					
						
							| 
									
										
										
										
											2009-08-21 15:22:02 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     ###START_AUTOCODE
 | 
					
						
							|  |  |  |     /* the code below is auto generated do not remove the above tag */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public $__table = 'config';                          // table name
 | 
					
						
							|  |  |  |     public $section;                         // varchar(32)  primary_key not_null
 | 
					
						
							|  |  |  |     public $setting;                         // varchar(32)  primary_key not_null
 | 
					
						
							| 
									
										
										
										
											2015-12-27 11:50:11 +01:00
										 |  |  |     public $value;                           // text
 | 
					
						
							| 
									
										
										
										
											2009-08-21 15:22:02 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* the code above is auto generated do not remove the tag below */ | 
					
						
							|  |  |  |     ###END_AUTOCODE
 | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  |     public static function schemaDef() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return array( | 
					
						
							|  |  |  |             'fields' => array( | 
					
						
							|  |  |  |                 'section' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'configuration section'), | 
					
						
							|  |  |  |                 'setting' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'configuration setting'), | 
					
						
							| 
									
										
										
										
											2015-12-27 11:50:11 +01:00
										 |  |  |                 'value' => array('type' => 'text', 'description' => 'configuration value'), | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  |             ), | 
					
						
							|  |  |  |             'primary key' => array('section', 'setting'), | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  |     const settingsKey = 'config:settings'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static function loadSettings() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-09-08 12:01:06 -04:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $settings = self::_getSettings(); | 
					
						
							|  |  |  |             if (!empty($settings)) { | 
					
						
							|  |  |  |                 self::_applySettings($settings); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static function _getSettings() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $c = self::memcache(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!empty($c)) { | 
					
						
							| 
									
										
										
										
											2010-09-06 10:07:43 -04:00
										 |  |  |             $settings = $c->get(Cache::key(self::settingsKey)); | 
					
						
							| 
									
										
										
										
											2010-01-04 10:02:59 -10:00
										 |  |  |             if ($settings !== false) { | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  |                 return $settings; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $settings = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $config = new Config(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $config->find(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while ($config->fetch()) { | 
					
						
							|  |  |  |             $settings[] = array($config->section, $config->setting, $config->value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $config->free(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!empty($c)) { | 
					
						
							| 
									
										
										
										
											2010-09-06 10:07:43 -04:00
										 |  |  |             $c->set(Cache::key(self::settingsKey), $settings); | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $settings; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static function _applySettings($settings) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         global $config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($settings as $s) { | 
					
						
							|  |  |  |             list($section, $setting, $value) = $s; | 
					
						
							|  |  |  |             $config[$section][$setting] = $value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function insert() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $result = parent::insert(); | 
					
						
							|  |  |  |         if ($result) { | 
					
						
							|  |  |  |             Config::_blowSettingsCache(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-29 10:20:57 +01:00
										 |  |  |     function delete($useWhere=false) | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-10-29 10:20:57 +01:00
										 |  |  |         $result = parent::delete($useWhere); | 
					
						
							|  |  |  |         if ($result !== false) { | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  |             Config::_blowSettingsCache(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-28 19:36:05 +01:00
										 |  |  |     function update($dataObject=false) | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-10-28 19:36:05 +01:00
										 |  |  |         $result = parent::update($dataObject); | 
					
						
							|  |  |  |         if ($result !== false) { | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  |             Config::_blowSettingsCache(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-07 23:16:59 -05:00
										 |  |  |     static function save($section, $setting, $value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $result = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $config = Config::pkeyGet(array('section' => $section, | 
					
						
							|  |  |  |                                         'setting' => $setting)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!empty($config)) { | 
					
						
							|  |  |  |             $orig = clone($config); | 
					
						
							|  |  |  |             $config->value = $value; | 
					
						
							|  |  |  |             $result = $config->update($orig); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $config = new Config(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $config->section = $section; | 
					
						
							|  |  |  |             $config->setting = $setting; | 
					
						
							|  |  |  |             $config->value   = $value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $result = $config->insert(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  |     function _blowSettingsCache() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $c = self::memcache(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!empty($c)) { | 
					
						
							| 
									
										
										
										
											2010-09-06 10:07:43 -04:00
										 |  |  |             $c->delete(Cache::key(self::settingsKey)); | 
					
						
							| 
									
										
										
										
											2009-08-21 16:14:32 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-08-21 15:22:02 -04:00
										 |  |  | } |