Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2893 lines
78 KiB

11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. <?php
  2. set_time_limit(0);
  3. ini_set('pcre.backtrack_limit', 250000);
  4. define ( 'FRIENDIKA_VERSION', '2.2.1017' );
  5. define ( 'DFRN_PROTOCOL_VERSION', '2.21' );
  6. define ( 'DB_UPDATE_VERSION', 1065 );
  7. define ( 'EOL', "<br />\r\n" );
  8. define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );
  9. define ( 'DOWN_ARROW', '&#x21e9;' );
  10. /**
  11. *
  12. * Image storage quality. Lower numbers save space at cost of image detail.
  13. * For ease of upgrade, please do not change here. Change jpeg quality with
  14. * set_config('system','jpeg_quality',n) in .htconfig.php
  15. * where n is netween 1 and 100, and with very poor results below about 50
  16. *
  17. */
  18. define ( 'JPEG_QUALITY', 100 );
  19. /**
  20. * SSL redirection policies
  21. */
  22. define ( 'SSL_POLICY_NONE', 0 );
  23. define ( 'SSL_POLICY_FULL', 1 );
  24. define ( 'SSL_POLICY_SELFSIGN', 2 );
  25. /**
  26. * log levels
  27. */
  28. define ( 'LOGGER_NORMAL', 0 );
  29. define ( 'LOGGER_TRACE', 1 );
  30. define ( 'LOGGER_DEBUG', 2 );
  31. define ( 'LOGGER_DATA', 3 );
  32. define ( 'LOGGER_ALL', 4 );
  33. /**
  34. * registration policies
  35. */
  36. define ( 'REGISTER_CLOSED', 0 );
  37. define ( 'REGISTER_APPROVE', 1 );
  38. define ( 'REGISTER_OPEN', 2 );
  39. /**
  40. * relationship types
  41. * When used in contact records, this indicates that 'uid' has
  42. * this relationship with contact['name']
  43. */
  44. define ( 'REL_VIP', 1);
  45. define ( 'REL_FAN', 2);
  46. define ( 'REL_BUD', 3);
  47. /**
  48. * Hook array order
  49. */
  50. define ( 'HOOK_HOOK', 0);
  51. define ( 'HOOK_FILE', 1);
  52. define ( 'HOOK_FUNCTION', 2);
  53. /**
  54. *
  55. * page/profile types
  56. *
  57. * PAGE_NORMAL is a typical personal profile account
  58. * PAGE_SOAPBOX automatically approves all friend requests as REL_FAN, (readonly)
  59. * PAGE_COMMUNITY automatically approves all friend requests as REL_FAN, but with
  60. * write access to wall and comments (no email and not included in page owner's ACL lists)
  61. * PAGE_FREELOVE automatically approves all friend requests as full friends (REL_BUD).
  62. *
  63. */
  64. define ( 'PAGE_NORMAL', 0 );
  65. define ( 'PAGE_SOAPBOX', 1 );
  66. define ( 'PAGE_COMMUNITY', 2 );
  67. define ( 'PAGE_FREELOVE', 3 );
  68. /**
  69. * Network and protocol family types
  70. */
  71. define ( 'NETWORK_DFRN', 'dfrn'); // Friendika, Mistpark, other DFRN implementations
  72. define ( 'NETWORK_OSTATUS', 'stat'); // status.net, identi.ca, GNU-social, other OStatus implementations
  73. define ( 'NETWORK_FEED', 'feed'); // RSS/Atom feeds with no known "post/notify" protocol
  74. define ( 'NETWORK_DIASPORA', 'dspr'); // Diaspora
  75. define ( 'NETWORK_MAIL', 'mail'); // IMAP/POP
  76. define ( 'NETWORK_FACEBOOK', 'face'); // Facebook API
  77. /**
  78. * Maximum number of "people who like (or don't like) this" that we will list by name
  79. */
  80. define ( 'MAX_LIKERS', 75);
  81. /**
  82. * email notification options
  83. */
  84. define ( 'NOTIFY_INTRO', 0x0001 );
  85. define ( 'NOTIFY_CONFIRM', 0x0002 );
  86. define ( 'NOTIFY_WALL', 0x0004 );
  87. define ( 'NOTIFY_COMMENT', 0x0008 );
  88. define ( 'NOTIFY_MAIL', 0x0010 );
  89. /**
  90. * various namespaces we may need to parse
  91. */
  92. define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
  93. define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
  94. define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
  95. define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
  96. define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/' );
  97. define ( 'NAMESPACE_MEDIA', 'http://purl.org/syndication/atommedia' );
  98. define ( 'NAMESPACE_SALMON_ME', 'http://salmon-protocol.org/ns/magic-env' );
  99. define ( 'NAMESPACE_OSTATUSSUB', 'http://ostatus.org/schema/1.0/subscribe' );
  100. define ( 'NAMESPACE_GEORSS', 'http://www.georss.org/georss' );
  101. define ( 'NAMESPACE_POCO', 'http://portablecontacts.net/spec/1.0' );
  102. define ( 'NAMESPACE_FEED', 'http://schemas.google.com/g/2010#updates-from' );
  103. define ( 'NAMESPACE_OSTATUS', 'http://ostatus.org/schema/1.0' );
  104. define ( 'NAMESPACE_STATUSNET', 'http://status.net/schema/api/1/' );
  105. /**
  106. * activity stream defines
  107. */
  108. define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
  109. define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
  110. define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
  111. define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
  112. define ( 'ACTIVITY_FOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'follow' );
  113. define ( 'ACTIVITY_UNFOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'stop-following' );
  114. define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
  115. define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
  116. define ( 'ACTIVITY_TAG', NAMESPACE_ACTIVITY_SCHEMA . 'tag' );
  117. define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
  118. define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
  119. define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
  120. define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
  121. define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
  122. define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
  123. define ( 'ACTIVITY_OBJ_EVENT', NAMESPACE_ACTIVITY_SCHEMA . 'event' );
  124. /**
  125. * item weight for query ordering
  126. */
  127. define ( 'GRAVITY_PARENT', 0);
  128. define ( 'GRAVITY_LIKE', 3);
  129. define ( 'GRAVITY_COMMENT', 6);
  130. /**
  131. *
  132. * Reverse the effect of magic_quotes_gpc if it is enabled.
  133. * Please disable magic_quotes_gpc so we don't have to do this.
  134. * See http://php.net/manual/en/security.magicquotes.disabling.php
  135. *
  136. */
  137. if (get_magic_quotes_gpc()) {
  138. $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
  139. while (list($key, $val) = each($process)) {
  140. foreach ($val as $k => $v) {
  141. unset($process[$key][$k]);
  142. if (is_array($v)) {
  143. $process[$key][stripslashes($k)] = $v;
  144. $process[] = &$process[$key][stripslashes($k)];
  145. } else {
  146. $process[$key][stripslashes($k)] = stripslashes($v);
  147. }
  148. }
  149. }
  150. unset($process);
  151. }
  152. /*
  153. * translation system
  154. */
  155. require_once("include/pgettext.php");
  156. /**
  157. *
  158. * class: App
  159. *
  160. * Our main application structure for the life of this page
  161. * Primarily deals with the URL that got us here
  162. * and tries to make some sense of it, and
  163. * stores our page contents and config storage
  164. * and anything else that might need to be passed around
  165. * before we spit the page out.
  166. *
  167. */
  168. if(! class_exists('App')) {
  169. class App {
  170. public $module_loaded = false;
  171. public $query_string;
  172. public $config;
  173. public $page;
  174. public $profile;
  175. public $user;
  176. public $cid;
  177. public $contact;
  178. public $contacts;
  179. public $page_contact;
  180. public $content;
  181. public $data;
  182. public $error = false;
  183. public $cmd;
  184. public $argv;
  185. public $argc;
  186. public $module;
  187. public $pager;
  188. public $strings;
  189. public $path;
  190. public $hooks;
  191. public $timezone;
  192. public $interactive = true;
  193. public $plugins;
  194. public $apps;
  195. public $identities;
  196. private $scheme;
  197. private $hostname;
  198. private $baseurl;
  199. private $db;
  200. private $curl_code;
  201. private $curl_headers;
  202. function __construct() {
  203. $this->config = array();
  204. $this->page = array();
  205. $this->pager= array();
  206. $this->query_string = '';
  207. $this->scheme = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
  208. if(x($_SERVER,'SERVER_NAME')) {
  209. $this->hostname = $_SERVER['SERVER_NAME'];
  210. /**
  211. * Figure out if we are running at the top of a domain
  212. * or in a sub-directory and adjust accordingly
  213. */
  214. $path = trim(dirname($_SERVER['SCRIPT_NAME']),'/\\');
  215. if(isset($path) && strlen($path) && ($path != $this->path))
  216. $this->path = $path;
  217. }
  218. set_include_path("include/$this->hostname" . PATH_SEPARATOR . 'include' . PATH_SEPARATOR . '.' );
  219. if((x($_SERVER,'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'],0,2) === "q=")
  220. $this->query_string = substr($_SERVER['QUERY_STRING'],2);
  221. if(x($_GET,'q'))
  222. $this->cmd = trim($_GET['q'],'/\\');
  223. /**
  224. *
  225. * Break the URL path into C style argc/argv style arguments for our
  226. * modules. Given "http://example.com/module/arg1/arg2", $this->argc
  227. * will be 3 (integer) and $this->argv will contain:
  228. * [0] => 'module'
  229. * [1] => 'arg1'
  230. * [2] => 'arg2'
  231. *
  232. *
  233. * There will always be one argument. If provided a naked domain
  234. * URL, $this->argv[0] is set to "home".
  235. *
  236. */
  237. $this->argv = explode('/',$this->cmd);
  238. $this->argc = count($this->argv);
  239. if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
  240. $this->module = str_replace(".", "_", $this->argv[0]);
  241. }
  242. else {
  243. $this->module = 'home';
  244. }
  245. /**
  246. * Special handling for the webfinger/lrdd host XRD file
  247. * Just spit out the contents and exit.
  248. */
  249. if($this->cmd === '.well-known/host-meta') {
  250. require_once('include/hostxrd.php');
  251. hostxrd($this->get_baseurl());
  252. // NOTREACHED
  253. }
  254. /**
  255. * See if there is any page number information, and initialise
  256. * pagination
  257. */
  258. $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
  259. $this->pager['itemspage'] = 50;
  260. $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
  261. $this->pager['total'] = 0;
  262. }
  263. function get_baseurl($ssl = false) {
  264. $scheme = $this->scheme;
  265. if(x($this->config,'ssl_policy')) {
  266. if(($ssl) || ($this->config['ssl_policy'] == SSL_POLICY_FULL))
  267. $scheme = 'https';
  268. if(($this->config['ssl_policy'] == SSL_POLICY_SELFSIGN) && (local_user() || x($_POST,'auth-params')))
  269. $scheme = 'https';
  270. }
  271. $this->baseurl = $scheme . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
  272. return $this->baseurl;
  273. }
  274. function set_baseurl($url) {
  275. $parsed = @parse_url($url);
  276. $this->baseurl = $url;
  277. if($parsed) {
  278. $this->scheme = $parsed['scheme'];
  279. $this->hostname = $parsed['host'];
  280. if(x($parsed,'port'))
  281. $this->hostname .= ':' . $parsed['port'];
  282. if(x($parsed,'path'))
  283. $this->path = trim($parsed['path'],'\\/');
  284. }
  285. }
  286. function get_hostname() {
  287. return $this->hostname;
  288. }
  289. function set_hostname($h) {
  290. $this->hostname = $h;
  291. }
  292. function set_path($p) {
  293. $this->path = trim(trim($p),'/');
  294. }
  295. function get_path() {
  296. return $this->path;
  297. }
  298. function set_pager_total($n) {
  299. $this->pager['total'] = intval($n);
  300. }
  301. function set_pager_itemspage($n) {
  302. $this->pager['itemspage'] = intval($n);
  303. $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
  304. }
  305. function init_pagehead() {
  306. $this->page['title'] = $this->config['sitename'];
  307. $tpl = file_get_contents('view/head.tpl');
  308. $this->page['htmlhead'] = replace_macros($tpl,array(
  309. '$baseurl' => $this->get_baseurl(),
  310. '$generator' => 'Friendika' . ' ' . FRIENDIKA_VERSION,
  311. '$delitem' => t('Delete this item?'),
  312. '$comment' => t('Comment')
  313. ));
  314. }
  315. function set_curl_code($code) {
  316. $this->curl_code = $code;
  317. }
  318. function get_curl_code() {
  319. return $this->curl_code;
  320. }
  321. function set_curl_headers($headers) {
  322. $this->curl_headers = $headers;
  323. }
  324. function get_curl_headers() {
  325. return $this->curl_headers;
  326. }
  327. }}
  328. // retrieve the App structure
  329. // useful in functions which require it but don't get it passed to them
  330. if(! function_exists('get_app')) {
  331. function get_app() {
  332. global $a;
  333. return $a;
  334. }};
  335. // Multi-purpose function to check variable state.
  336. // Usage: x($var) or $x($array,'key')
  337. // returns false if variable/key is not set
  338. // if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
  339. // e.g. x('') or x(0) returns 0;
  340. if(! function_exists('x')) {
  341. function x($s,$k = NULL) {
  342. if($k != NULL) {
  343. if((is_array($s)) && (array_key_exists($k,$s))) {
  344. if($s[$k])
  345. return (int) 1;
  346. return (int) 0;
  347. }
  348. return false;
  349. }
  350. else {
  351. if(isset($s)) {
  352. if($s) {
  353. return (int) 1;
  354. }
  355. return (int) 0;
  356. }
  357. return false;
  358. }
  359. }}
  360. // called from db initialisation if db is dead.
  361. if(! function_exists('system_unavailable')) {
  362. function system_unavailable() {
  363. include('system_unavailable.php');
  364. system_down();
  365. killme();
  366. }}
  367. // install and uninstall plugin
  368. if (! function_exists('uninstall_plugin')){
  369. function uninstall_plugin($plugin){
  370. logger("Addons: uninstalling " . $plugin);
  371. q("DELETE FROM `addon` WHERE `name` = '%s' LIMIT 1",
  372. dbesc($plugin)
  373. );
  374. @include_once('addon/' . $plugin . '/' . $plugin . '.php');
  375. if(function_exists($plugin . '_uninstall')) {
  376. $func = $plugin . '_uninstall';
  377. $func();
  378. }
  379. }}
  380. if (! function_exists('install_plugin')){
  381. function install_plugin($plugin){
  382. logger("Addons: installing " . $plugin);
  383. $t = filemtime('addon/' . $plugin . '/' . $plugin . '.php');
  384. @include_once('addon/' . $plugin . '/' . $plugin . '.php');
  385. if(function_exists($plugin . '_install')) {
  386. $func = $plugin . '_install';
  387. $func();
  388. $plugin_admin = (function_exists($plugin."_plugin_admin")?1:0);
  389. $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ",
  390. dbesc($plugin),
  391. intval($t),
  392. $plugin_admin
  393. );
  394. }
  395. }}
  396. // Primarily involved with database upgrade, but also sets the
  397. // base url for use in cmdline programs which don't have
  398. // $_SERVER variables, and synchronising the state of installed plugins.
  399. if(! function_exists('check_config')) {
  400. function check_config(&$a) {
  401. load_config('system');
  402. $build = get_config('system','build');
  403. if(! x($build))
  404. $build = set_config('system','build',DB_UPDATE_VERSION);
  405. $url = get_config('system','url');
  406. // if the url isn't set or the stored url is radically different
  407. // than the currently visited url, store the current value accordingly.
  408. // "Radically different" ignores common variations such as http vs https
  409. // and www.example.com vs example.com.
  410. if((! x($url)) || (! link_compare($url,$a->get_baseurl())))
  411. $url = set_config('system','url',$a->get_baseurl());
  412. if($build != DB_UPDATE_VERSION) {
  413. $stored = intval($build);
  414. $current = intval(DB_UPDATE_VERSION);
  415. if(($stored < $current) && file_exists('update.php')) {
  416. // We're reporting a different version than what is currently installed.
  417. // Run any existing update scripts to bring the database up to current.
  418. require_once('update.php');
  419. // make sure that boot.php and update.php are the same release, we might be
  420. // updating right this very second and the correct version of the update.php
  421. // file may not be here yet. This can happen on a very busy site.
  422. if(DB_UPDATE_VERSION == UPDATE_VERSION) {
  423. for($x = $stored; $x < $current; $x ++) {
  424. if(function_exists('update_' . $x)) {
  425. $func = 'update_' . $x;
  426. $func($a);
  427. }
  428. }
  429. set_config('system','build', DB_UPDATE_VERSION);
  430. }
  431. }
  432. }
  433. /**
  434. *
  435. * Synchronise plugins:
  436. *
  437. * $a->config['system']['addon'] contains a comma-separated list of names
  438. * of plugins/addons which are used on this system.
  439. * Go through the database list of already installed addons, and if we have
  440. * an entry, but it isn't in the config list, call the uninstall procedure
  441. * and mark it uninstalled in the database (for now we'll remove it).
  442. * Then go through the config list and if we have a plugin that isn't installed,
  443. * call the install procedure and add it to the database.
  444. *
  445. */
  446. $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
  447. if(count($r))
  448. $installed = $r;
  449. else
  450. $installed = array();
  451. $plugins = get_config('system','addon');
  452. $plugins_arr = array();
  453. if($plugins)
  454. $plugins_arr = explode(',',str_replace(' ', '',$plugins));
  455. $a->plugins = $plugins_arr;
  456. $installed_arr = array();
  457. if(count($installed)) {
  458. foreach($installed as $i) {
  459. if(! in_array($i['name'],$plugins_arr)) {
  460. uninstall_plugin($i['name']);
  461. }
  462. else
  463. $installed_arr[] = $i['name'];
  464. }
  465. }
  466. if(count($plugins_arr)) {
  467. foreach($plugins_arr as $p) {
  468. if(! in_array($p,$installed_arr)) {
  469. install_plugin($p);
  470. }
  471. }
  472. }
  473. load_hooks();
  474. return;
  475. }}
  476. // reload all updated plugins
  477. if(! function_exists('reload_plugins')) {
  478. function reload_plugins() {
  479. $plugins = get_config('system','addon');
  480. if(strlen($plugins)) {
  481. $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
  482. if(count($r))
  483. $installed = $r;
  484. else
  485. $installed = array();
  486. $parr = explode(',',$plugins);
  487. if(count($parr)) {
  488. foreach($parr as $pl) {
  489. $pl = trim($pl);
  490. $t = filemtime('addon/' . $pl . '/' . $pl . '.php');
  491. foreach($installed as $i) {
  492. if(($i['name'] == $pl) && ($i['timestamp'] != $t)) {
  493. logger('Reloading plugin: ' . $i['name']);
  494. @include_once('addon/' . $pl . '/' . $pl . '.php');
  495. if(function_exists($pl . '_uninstall')) {
  496. $func = $pl . '_uninstall';
  497. $func();
  498. }
  499. if(function_exists($pl . '_install')) {
  500. $func = $pl . '_install';
  501. $func();
  502. }
  503. q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d LIMIT 1",
  504. intval($t),
  505. intval($i['id'])
  506. );
  507. }
  508. }
  509. }
  510. }
  511. }
  512. }}
  513. // This is our template processor.
  514. // $s is the string requiring macro substitution.
  515. // $r is an array of key value pairs (search => replace)
  516. // returns substituted string.
  517. // WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
  518. // For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing,
  519. // depending on the order in which they were declared in the array.
  520. require_once("include/template_processor.php");
  521. if(! function_exists('replace_macros')) {
  522. function replace_macros($s,$r) {
  523. global $t;
  524. return $t->replace($s,$r);
  525. }}
  526. // curl wrapper. If binary flag is true, return binary
  527. // results.
  528. if(! function_exists('fetch_url')) {
  529. function fetch_url($url,$binary = false, &$redirects = 0) {
  530. $a = get_app();
  531. $ch = curl_init($url);
  532. if(($redirects > 8) || (! $ch))
  533. return false;
  534. curl_setopt($ch, CURLOPT_HEADER, true);
  535. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  536. $curl_time = intval(get_config('system','curl_timeout'));
  537. curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
  538. // by default we will allow self-signed certs
  539. // but you can override this
  540. $check_cert = get_config('system','verifyssl');
  541. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
  542. $prx = get_config('system','proxy');
  543. if(strlen($prx)) {
  544. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  545. curl_setopt($ch, CURLOPT_PROXY, $prx);
  546. $prxusr = get_config('system','proxyuser');
  547. if(strlen($prxusr))
  548. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
  549. }
  550. if($binary)
  551. curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
  552. $a->set_curl_code(0);
  553. // don't let curl abort the entire application
  554. // if it throws any errors.
  555. $s = @curl_exec($ch);
  556. $http_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  557. $header = substr($s,0,strpos($s,"\r\n\r\n"));
  558. if(stristr($header,'100') && (strlen($header) < 30)) {
  559. // 100 Continue has two headers, get the real one
  560. $s = substr($s,strlen($header)+4);
  561. $header = substr($s,0,strpos($s,"\r\n\r\n"));
  562. }
  563. if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
  564. $matches = array();
  565. preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
  566. $url = trim(array_pop($matches));
  567. $url_parsed = @parse_url($url);
  568. if (isset($url_parsed)) {
  569. $redirects++;
  570. return fetch_url($url,$binary,$redirects);
  571. }
  572. }
  573. $a->set_curl_code($http_code);
  574. $body = substr($s,strlen($header)+4);
  575. /* one more try to make sure there are no more headers */
  576. if(strpos($body,'HTTP/') === 0) {
  577. $header = substr($body,0,strpos($body,"\r\n\r\n"));
  578. $body = substr($body,strlen($header)+4);
  579. }
  580. $a->set_curl_headers($header);
  581. curl_close($ch);
  582. return($body);
  583. }}
  584. // post request to $url. $params is an array of post variables.
  585. if(! function_exists('post_url')) {
  586. function post_url($url,$params, $headers = null, &$redirects = 0) {
  587. $a = get_app();
  588. $ch = curl_init($url);
  589. if(($redirects > 8) || (! $ch))
  590. return false;
  591. curl_setopt($ch, CURLOPT_HEADER, true);
  592. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  593. curl_setopt($ch, CURLOPT_POST,1);
  594. curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
  595. $curl_time = intval(get_config('system','curl_timeout'));
  596. curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
  597. if(is_array($headers))
  598. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  599. $check_cert = get_config('system','verifyssl');
  600. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
  601. $prx = get_config('system','proxy');
  602. if(strlen($prx)) {
  603. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  604. curl_setopt($ch, CURLOPT_PROXY, $prx);
  605. $prxusr = get_config('system','proxyuser');
  606. if(strlen($prxusr))
  607. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
  608. }
  609. $a->set_curl_code(0);
  610. // don't let curl abort the entire application
  611. // if it throws any errors.
  612. $s = @curl_exec($ch);
  613. $http_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  614. $header = substr($s,0,strpos($s,"\r\n\r\n"));
  615. if(stristr($header,'100') && (strlen($header) < 30)) {
  616. // 100 Continue has two headers, get the real one
  617. $s = substr($s,strlen($header)+4);
  618. $header = substr($s,0,strpos($s,"\r\n\r\n"));
  619. }
  620. if($http_code == 301 || $http_code == 302 || $http_code == 303) {
  621. $matches = array();
  622. preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
  623. $url = trim(array_pop($matches));
  624. $url_parsed = @parse_url($url);
  625. if (isset($url_parsed)) {
  626. $redirects++;
  627. return post_url($url,$binary,$headers,$redirects);
  628. }
  629. }
  630. $a->set_curl_code($http_code);
  631. $body = substr($s,strlen($header)+4);
  632. /* one more try to make sure there are no more headers */
  633. if(strpos($body,'HTTP/') === 0) {
  634. $header = substr($body,0,strpos($body,"\r\n\r\n"));
  635. $body = substr($body,strlen($header)+4);
  636. }
  637. $a->set_curl_headers($header);
  638. curl_close($ch);
  639. return($body);
  640. }}
  641. // random hash, 64 chars
  642. if(! function_exists('random_string')) {
  643. function random_string() {
  644. return(hash('sha256',uniqid(rand(),true)));
  645. }}
  646. /**
  647. * This is our primary input filter.
  648. *
  649. * The high bit hack only involved some old IE browser, forget which (IE5/Mac?)
  650. * that had an XSS attack vector due to stripping the high-bit on an 8-bit character
  651. * after cleansing, and angle chars with the high bit set could get through as markup.
  652. *
  653. * This is now disabled because it was interfering with some legitimate unicode sequences
  654. * and hopefully there aren't a lot of those browsers left.
  655. *
  656. * Use this on any text input where angle chars are not valid or permitted
  657. * They will be replaced with safer brackets. This may be filtered further
  658. * if these are not allowed either.
  659. *
  660. */
  661. if(! function_exists('notags')) {
  662. function notags($string) {
  663. return(str_replace(array("<",">"), array('[',']'), $string));
  664. // High-bit filter no longer used
  665. // return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
  666. }}
  667. // use this on "body" or "content" input where angle chars shouldn't be removed,
  668. // and allow them to be safely displayed.
  669. if(! function_exists('escape_tags')) {
  670. function escape_tags($string) {
  671. return(htmlspecialchars($string));
  672. }}
  673. // wrapper for adding a login box. If $register == true provide a registration
  674. // link. This will most always depend on the value of $a->config['register_policy'].
  675. // returns the complete html for inserting into the page
  676. if(! function_exists('login')) {
  677. function login($register = false) {
  678. $o = "";
  679. $register_tpl = (($register) ? get_markup_template("register-link.tpl") : "");
  680. $register_html = replace_macros($register_tpl,array(
  681. '$title' => t('Create a New Account'),
  682. '$desc' => t('Register')
  683. ));
  684. $noid = get_config('system','no_openid');
  685. if($noid) {
  686. $classname = 'no-openid';
  687. $namelabel = t('Nickname or Email address: ');
  688. $passlabel = t('Password: ');
  689. $login = t('Login');
  690. }
  691. else {
  692. $classname = 'openid';
  693. $namelabel = t('Nickname/Email/OpenID: ');
  694. $passlabel = t("Password \x28if not OpenID\x29: ");
  695. $login = t('Login');
  696. }
  697. $lostpass = t('Forgot your password?');
  698. $lostlink = t('Password Reset');
  699. if(local_user()) {
  700. $tpl = get_markup_template("logout.tpl");
  701. }
  702. else {
  703. $tpl = get_markup_template("login.tpl");
  704. }
  705. $o = '<script type="text/javascript"> $(document).ready(function() { $("#login-name").focus();} );</script>';
  706. $o .= replace_macros($tpl,array(
  707. '$logout' => t('Logout'),
  708. '$register_html' => $register_html,
  709. '$classname' => $classname,
  710. '$namelabel' => $namelabel,
  711. '$passlabel' => $passlabel,
  712. '$login' => $login,
  713. '$lostpass' => $lostpass,
  714. '$lostlink' => $lostlink
  715. ));
  716. return $o;
  717. }}
  718. // generate a string that's random, but usually pronounceable.
  719. // used to generate initial passwords
  720. if(! function_exists('autoname')) {
  721. function autoname($len) {
  722. $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
  723. if(mt_rand(0,5) == 4)
  724. $vowels[] = 'y';
  725. $cons = array(
  726. 'b','bl','br',
  727. 'c','ch','cl','cr',
  728. 'd','dr',
  729. 'f','fl','fr',
  730. 'g','gh','gl','gr',
  731. 'h',
  732. 'j',
  733. 'k','kh','kl','kr',
  734. 'l',
  735. 'm',
  736. 'n',
  737. 'p','ph','pl','pr',
  738. 'qu',
  739. 'r','rh',
  740. 's','sc','sh','sm','sp','st',
  741. 't','th','tr',
  742. 'v',
  743. 'w','wh',
  744. 'x',
  745. 'z','zh'
  746. );
  747. $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
  748. 'nd','ng','nk','nt','rn','rp','rt');
  749. $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
  750. 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
  751. $start = mt_rand(0,2);
  752. if($start == 0)
  753. $table = $vowels;
  754. else
  755. $table = $cons;
  756. $word = '';
  757. for ($x = 0; $x < $len; $x ++) {
  758. $r = mt_rand(0,count($table) - 1);
  759. $word .= $table[$r];
  760. if($table == $vowels)
  761. $table = array_merge($cons,$midcons);
  762. else
  763. $table = $vowels;
  764. }
  765. $word = substr($word,0,$len);
  766. foreach($noend as $noe) {
  767. if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
  768. $word = substr($word,0,-1);
  769. break;
  770. }
  771. }
  772. if(substr($word,-1) == 'q')
  773. $word = substr($word,0,-1);
  774. return $word;
  775. }}
  776. // Used to end the current process, after saving session state.
  777. if(! function_exists('killme')) {
  778. function killme() {
  779. session_write_close();
  780. exit;
  781. }}
  782. // redirect to another URL and terminate this process.
  783. if(! function_exists('goaway')) {
  784. function goaway($s) {
  785. header("Location: $s");
  786. killme();
  787. }}
  788. // Generic XML return
  789. // Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
  790. // of $st and an optional text <message> of $message and terminates the current process.
  791. if(! function_exists('xml_status')) {
  792. function xml_status($st, $message = '') {
  793. $xml_message = ((strlen($message)) ? "\t<message>" . xmlify($message) . "</message>\r\n" : '');
  794. if($st)
  795. logger('xml_status returning non_zero: ' . $st . " message=" . $message);
  796. header( "Content-type: text/xml" );
  797. echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
  798. echo "<result>\r\n\t<status>$st</status>\r\n$xml_message</result>\r\n";
  799. killme();
  800. }}
  801. // Returns the uid of locally logged in user or false.
  802. if(! function_exists('local_user')) {
  803. function local_user() {
  804. if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
  805. return intval($_SESSION['uid']);
  806. return false;
  807. }}
  808. // Returns contact id of authenticated site visitor or false
  809. if(! function_exists('remote_user')) {
  810. function remote_user() {
  811. if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
  812. return intval($_SESSION['visitor_id']);
  813. return false;
  814. }}
  815. // contents of $s are displayed prominently on the page the next time
  816. // a page is loaded. Usually used for errors or alerts.
  817. if(! function_exists('notice')) {
  818. function notice($s) {
  819. $a = get_app();
  820. if($a->interactive)
  821. $_SESSION['sysmsg'] .= $s;
  822. }}
  823. if(! function_exists('info')) {
  824. function info($s) {
  825. $a = get_app();
  826. if($a->interactive)
  827. $_SESSION['sysmsg_info'] .= $s;
  828. }}
  829. // wrapper around config to limit the text length of an incoming message
  830. if(! function_exists('get_max_import_size')) {
  831. function get_max_import_size() {
  832. global $a;
  833. return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
  834. }}
  835. // escape text ($str) for XML transport
  836. // returns escaped text.
  837. if(! function_exists('xmlify')) {
  838. function xmlify($str) {
  839. $buffer = '';
  840. for($x = 0; $x < strlen($str); $x ++) {
  841. $char = $str[$x];
  842. switch( $char ) {
  843. case "\r" :
  844. break;
  845. case "&" :
  846. $buffer .= '&amp;';
  847. break;
  848. case "'" :
  849. $buffer .= '&apos;';
  850. break;
  851. case "\"" :
  852. $buffer .= '&quot;';
  853. break;
  854. case '<' :
  855. $buffer .= '&lt;';
  856. break;
  857. case '>' :
  858. $buffer .= '&gt;';
  859. break;
  860. case "\n" :
  861. $buffer .= "\n";
  862. break;
  863. default :
  864. $buffer .= $char;
  865. break;
  866. }
  867. }
  868. $buffer = trim($buffer);
  869. return($buffer);
  870. }}
  871. // undo an xmlify
  872. // pass xml escaped text ($s), returns unescaped text
  873. if(! function_exists('unxmlify')) {
  874. function unxmlify($s) {
  875. $ret = str_replace('&amp;','&', $s);
  876. $ret = str_replace(array('&lt;','&gt;','&quot;','&apos;'),array('<','>','"',"'"),$ret);
  877. return $ret;
  878. }}
  879. // convenience wrapper, reverse the operation "bin2hex"
  880. if(! function_exists('hex2bin')) {
  881. function hex2bin($s) {
  882. if(! ctype_xdigit($s)) {
  883. logger('hex2bin: illegal input: ' . print_r(debug_backtrace(), true));
  884. return($s);
  885. }
  886. return(pack("H*",$s));
  887. }}
  888. // Automatic pagination.
  889. // To use, get the count of total items.
  890. // Then call $a->set_pager_total($number_items);
  891. // Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
  892. // Then call paginate($a) after the end of the display loop to insert the pager block on the page
  893. // (assuming there are enough items to paginate).
  894. // When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
  895. // will limit the results to the correct items for the current page.
  896. // The actual page handling is then accomplished at the application layer.
  897. if(! function_exists('paginate')) {
  898. function paginate(&$a) {
  899. $o = '';
  900. $stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string);
  901. $stripped = str_replace('q=','',$stripped);
  902. $stripped = trim($stripped,'/');
  903. $pagenum = $a->pager['page'];
  904. $url = $a->get_baseurl() . '/' . $stripped;
  905. if($a->pager['total'] > $a->pager['itemspage']) {
  906. $o .= '<div class="pager">';
  907. if($a->pager['page'] != 1)
  908. $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
  909. $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
  910. $numpages = $a->pager['total'] / $a->pager['itemspage'];
  911. $numstart = 1;
  912. $numstop = $numpages;
  913. if($numpages > 14) {
  914. $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
  915. $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
  916. }
  917. for($i = $numstart; $i <= $numstop; $i++){
  918. if($i == $a->pager['page'])
  919. $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
  920. else
  921. $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
  922. $o .= '</span> ';
  923. }
  924. if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
  925. if($i == $a->pager['page'])
  926. $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
  927. else
  928. $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
  929. $o .= '</span> ';
  930. }
  931. $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
  932. $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
  933. if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
  934. $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
  935. $o .= '</div>'."\r\n";
  936. }
  937. return $o;
  938. }}
  939. // Turn user/group ACLs stored as angle bracketed text into arrays
  940. if(! function_exists('expand_acl')) {
  941. function expand_acl($s) {
  942. // turn string array of angle-bracketed elements into numeric array
  943. // e.g. "<1><2><3>" => array(1,2,3);
  944. $ret = array();
  945. if(strlen($s)) {
  946. $t = str_replace('<','',$s);
  947. $a = explode('>',$t);
  948. foreach($a as $aa) {
  949. if(intval($aa))
  950. $ret[] = intval($aa);
  951. }
  952. }
  953. return $ret;
  954. }}
  955. // Used to wrap ACL elements in angle brackets for storage
  956. if(! function_exists('sanitise_acl')) {
  957. function sanitise_acl(&$item) {
  958. if(intval($item))
  959. $item = '<' . intval(notags(trim($item))) . '>';
  960. else
  961. unset($item);
  962. }}
  963. // retrieve a "family" of config variables from database to cached storage
  964. if(! function_exists('load_config')) {
  965. function load_config($family) {
  966. global $a;
  967. $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
  968. dbesc($family)
  969. );
  970. if(count($r)) {
  971. foreach($r as $rr) {
  972. $k = $rr['k'];
  973. $a->config[$family][$k] = $rr['v'];
  974. }
  975. }
  976. }}
  977. // get a particular config variable given the family name
  978. // and key. Returns false if not set.
  979. // $instore is only used by the set_config function
  980. // to determine if the key already exists in the DB
  981. // If a key is found in the DB but doesn't exist in
  982. // local config cache, pull it into the cache so we don't have
  983. // to hit the DB again for this item.
  984. if(! function_exists('get_config')) {
  985. function get_config($family, $key, $instore = false) {
  986. global $a;
  987. if(! $instore) {
  988. if(isset($a->config[$family][$key])) {
  989. if($a->config[$family][$key] === '!<unset>!') {
  990. return false;
  991. }
  992. return $a->config[$family][$key];
  993. }
  994. }
  995. $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
  996. dbesc($family),
  997. dbesc($key)
  998. );
  999. if(count($ret)) {
  1000. // manage array value
  1001. $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
  1002. $a->config[$family][$key] = $val;
  1003. return $val;
  1004. }
  1005. else {
  1006. $a->config[$family][$key] = '!<unset>!';
  1007. }
  1008. return false;
  1009. }}
  1010. // Store a config value ($value) in the category ($family)
  1011. // under the key ($key)
  1012. // Return the value, or false if the database update failed
  1013. if(! function_exists('set_config')) {
  1014. function set_config($family,$key,$value) {
  1015. global $a;
  1016. // manage array value
  1017. $dbvalue = (is_array($value)?serialize($value):$value);
  1018. if(get_config($family,$key,true) === false) {
  1019. $a->config[$family][$key] = $value;
  1020. $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
  1021. dbesc($family),
  1022. dbesc($key),
  1023. dbesc($dbvalue)
  1024. );
  1025. if($ret)
  1026. return $value;
  1027. return $ret;
  1028. }
  1029. $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
  1030. dbesc($dbvalue),
  1031. dbesc($family),
  1032. dbesc($key)
  1033. );
  1034. $a->config[$family][$key] = $value;
  1035. if($ret)
  1036. return $value;
  1037. return $ret;
  1038. }}
  1039. if(! function_exists('load_pconfig')) {
  1040. function load_pconfig($uid,$family) {
  1041. global $a;
  1042. $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
  1043. dbesc($family),
  1044. intval($uid)
  1045. );
  1046. if(count($r)) {
  1047. foreach($r as $rr) {
  1048. $k = $rr['k'];
  1049. $a->config[$uid][$family][$k] = $rr['v'];
  1050. }
  1051. }
  1052. }}
  1053. if(! function_exists('get_pconfig')) {
  1054. function get_pconfig($uid,$family, $key, $instore = false) {
  1055. global $a;
  1056. if(! $instore) {
  1057. if(isset($a->config[$uid][$family][$key])) {
  1058. if($a->config[$uid][$family][$key] === '!<unset>!') {
  1059. return false;
  1060. }
  1061. return $a->config[$uid][$family][$key];
  1062. }
  1063. }
  1064. $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
  1065. intval($uid),
  1066. dbesc($family),
  1067. dbesc($key)
  1068. );
  1069. if(count($ret)) {
  1070. $a->config[$uid][$family][$key] = $ret[0]['v'];
  1071. return $ret[0]['v'];
  1072. }
  1073. else {
  1074. $a->config[$uid][$family][$key] = '!<unset>!';
  1075. }
  1076. return false;
  1077. }}
  1078. if(! function_exists('del_config')) {
  1079. function del_config($family,$key) {
  1080. global $a;
  1081. if(x($a->config[$family],$key))
  1082. unset($a->config[$family][$key]);
  1083. $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
  1084. dbesc($cat),
  1085. dbesc($key)
  1086. );
  1087. return $ret;
  1088. }}
  1089. // Same as above functions except these are for personal config storage and take an
  1090. // additional $uid argument.
  1091. if(! function_exists('set_pconfig')) {
  1092. function set_pconfig($uid,$family,$key,$value) {
  1093. global $a;
  1094. if(get_pconfig($uid,$family,$key,true) === false) {
  1095. $a->config[$uid][$family][$key] = $value;
  1096. $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
  1097. intval($uid),
  1098. dbesc($family),
  1099. dbesc($key),
  1100. dbesc($value)
  1101. );
  1102. if($ret)
  1103. return $value;
  1104. return $ret;
  1105. }
  1106. $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
  1107. dbesc($value),
  1108. intval($uid),
  1109. dbesc($family),
  1110. dbesc($key)
  1111. );
  1112. $a->config[$uid][$family][$key] = $value;
  1113. if($ret)
  1114. return $value;
  1115. return $ret;
  1116. }}
  1117. if(! function_exists('del_pconfig')) {
  1118. function del_pconfig($uid,$family,$key) {
  1119. global $a;
  1120. if(x($a->config[$uid][$family],$key))
  1121. unset($a->config[$uid][$family][$key]);
  1122. $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d A