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.
 
 
 
 
 
 

413 lines
9.6 KiB

  1. <?php
  2. // install and uninstall plugin
  3. if (! function_exists('uninstall_plugin')){
  4. function uninstall_plugin($plugin){
  5. logger("Addons: uninstalling " . $plugin);
  6. q("DELETE FROM `addon` WHERE `name` = '%s' ",
  7. dbesc($plugin)
  8. );
  9. @include_once('addon/' . $plugin . '/' . $plugin . '.php');
  10. if(function_exists($plugin . '_uninstall')) {
  11. $func = $plugin . '_uninstall';
  12. $func();
  13. }
  14. }}
  15. if (! function_exists('install_plugin')){
  16. function install_plugin($plugin) {
  17. // silently fail if plugin was removed
  18. if(! file_exists('addon/' . $plugin . '/' . $plugin . '.php'))
  19. return false;
  20. logger("Addons: installing " . $plugin);
  21. $t = @filemtime('addon/' . $plugin . '/' . $plugin . '.php');
  22. @include_once('addon/' . $plugin . '/' . $plugin . '.php');
  23. if(function_exists($plugin . '_install')) {
  24. $func = $plugin . '_install';
  25. $func();
  26. $plugin_admin = (function_exists($plugin."_plugin_admin")?1:0);
  27. $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ",
  28. dbesc($plugin),
  29. intval($t),
  30. $plugin_admin
  31. );
  32. // we can add the following with the previous SQL
  33. // once most site tables have been updated.
  34. // This way the system won't fall over dead during the update.
  35. if(file_exists('addon/' . $plugin . '/.hidden')) {
  36. q("update addon set hidden = 1 where name = '%s' limit 1",
  37. dbesc($plugin)
  38. );
  39. }
  40. return true;
  41. }
  42. else {
  43. logger("Addons: FAILED installing " . $plugin);
  44. return false;
  45. }
  46. }}
  47. // reload all updated plugins
  48. if(! function_exists('reload_plugins')) {
  49. function reload_plugins() {
  50. $plugins = get_config('system','addon');
  51. if(strlen($plugins)) {
  52. $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
  53. if(count($r))
  54. $installed = $r;
  55. else
  56. $installed = array();
  57. $parr = explode(',',$plugins);
  58. if(count($parr)) {
  59. foreach($parr as $pl) {
  60. $pl = trim($pl);
  61. $fname = 'addon/' . $pl . '/' . $pl . '.php';
  62. if(file_exists($fname)) {
  63. $t = @filemtime($fname);
  64. foreach($installed as $i) {
  65. if(($i['name'] == $pl) && ($i['timestamp'] != $t)) {
  66. logger('Reloading plugin: ' . $i['name']);
  67. @include_once($fname);
  68. if(function_exists($pl . '_uninstall')) {
  69. $func = $pl . '_uninstall';
  70. $func();
  71. }
  72. if(function_exists($pl . '_install')) {
  73. $func = $pl . '_install';
  74. $func();
  75. }
  76. q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d LIMIT 1",
  77. intval($t),
  78. intval($i['id'])
  79. );
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }}
  87. if(! function_exists('register_hook')) {
  88. function register_hook($hook,$file,$function,$priority=0) {
  89. $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
  90. dbesc($hook),
  91. dbesc($file),
  92. dbesc($function)
  93. );
  94. if(count($r))
  95. return true;
  96. $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`) VALUES ( '%s', '%s', '%s', '%s' ) ",
  97. dbesc($hook),
  98. dbesc($file),
  99. dbesc($function),
  100. dbesc($priority)
  101. );
  102. return $r;
  103. }}
  104. if(! function_exists('unregister_hook')) {
  105. function unregister_hook($hook,$file,$function) {
  106. $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
  107. dbesc($hook),
  108. dbesc($file),
  109. dbesc($function)
  110. );
  111. return $r;
  112. }}
  113. if(! function_exists('load_hooks')) {
  114. function load_hooks() {
  115. $a = get_app();
  116. $a->hooks = array();
  117. $r = q("SELECT * FROM `hook` WHERE 1 ORDER BY `priority` DESC");
  118. if(count($r)) {
  119. foreach($r as $rr) {
  120. if(! array_key_exists($rr['hook'],$a->hooks))
  121. $a->hooks[$rr['hook']] = array();
  122. $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
  123. }
  124. }
  125. }}
  126. if(! function_exists('call_hooks')) {
  127. function call_hooks($name, &$data = null) {
  128. $a = get_app();
  129. if((is_array($a->hooks)) && (array_key_exists($name,$a->hooks))) {
  130. foreach($a->hooks[$name] as $hook) {
  131. @include_once($hook[0]);
  132. if(function_exists($hook[1])) {
  133. $func = $hook[1];
  134. $func($a,$data);
  135. }
  136. else {
  137. // remove orphan hooks
  138. q("delete from hook where hook = '%s' and file = '$s' and function = '%s' limit 1",
  139. dbesc($name),
  140. dbesc($hook[0]),
  141. dbesc($hook[1])
  142. );
  143. }
  144. }
  145. }
  146. }}
  147. /*
  148. * parse plugin comment in search of plugin infos.
  149. * like
  150. *
  151. * * Name: Plugin
  152. * * Description: A plugin which plugs in
  153. * * Version: 1.2.3
  154. * * Author: John <profile url>
  155. * * Author: Jane <email>
  156. * *
  157. */
  158. if (! function_exists('get_plugin_info')){
  159. function get_plugin_info($plugin){
  160. $a = get_app();
  161. $info=Array(
  162. 'name' => $plugin,
  163. 'description' => "",
  164. 'author' => array(),
  165. 'version' => ""
  166. );
  167. if (!is_file("addon/$plugin/$plugin.php")) return $info;
  168. $stamp1 = microtime(true);
  169. $f = file_get_contents("addon/$plugin/$plugin.php");
  170. $a->save_timestamp($stamp1, "file");
  171. $r = preg_match("|/\*.*\*/|msU", $f, $m);
  172. if ($r){
  173. $ll = explode("\n", $m[0]);
  174. foreach( $ll as $l ) {
  175. $l = trim($l,"\t\n\r */");
  176. if ($l!=""){
  177. list($k,$v) = array_map("trim", explode(":",$l,2));
  178. $k= strtolower($k);
  179. if ($k=="author"){
  180. $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
  181. if ($r) {
  182. $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
  183. } else {
  184. $info['author'][] = array('name'=>$v);
  185. }
  186. } else {
  187. if (array_key_exists($k,$info)){
  188. $info[$k]=$v;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. return $info;
  195. }}
  196. /*
  197. * parse theme comment in search of theme infos.
  198. * like
  199. *
  200. * * Name: My Theme
  201. * * Description: My Cool Theme
  202. * * Version: 1.2.3
  203. * * Author: John <profile url>
  204. * * Maintainer: Jane <profile url>
  205. * *
  206. */
  207. if (! function_exists('get_theme_info')){
  208. function get_theme_info($theme){
  209. $info=Array(
  210. 'name' => $theme,
  211. 'description' => "",
  212. 'author' => array(),
  213. 'maintainer' => array(),
  214. 'version' => "",
  215. 'credits' => "",
  216. 'experimental' => false,
  217. 'unsupported' => false
  218. );
  219. if(file_exists("view/theme/$theme/experimental"))
  220. $info['experimental'] = true;
  221. if(file_exists("view/theme/$theme/unsupported"))
  222. $info['unsupported'] = true;
  223. if (!is_file("view/theme/$theme/theme.php")) return $info;
  224. $a = get_app();
  225. $stamp1 = microtime(true);
  226. $f = file_get_contents("view/theme/$theme/theme.php");
  227. $a->save_timestamp($stamp1, "file");
  228. $r = preg_match("|/\*.*\*/|msU", $f, $m);
  229. if ($r){
  230. $ll = explode("\n", $m[0]);
  231. foreach( $ll as $l ) {
  232. $l = trim($l,"\t\n\r */");
  233. if ($l!=""){
  234. list($k,$v) = array_map("trim", explode(":",$l,2));
  235. $k= strtolower($k);
  236. if ($k=="author"){
  237. $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
  238. if ($r) {
  239. $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
  240. } else {
  241. $info['author'][] = array('name'=>$v);
  242. }
  243. }
  244. elseif ($k=="maintainer"){
  245. $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
  246. if ($r) {
  247. $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
  248. } else {
  249. $info['maintainer'][] = array('name'=>$v);
  250. }
  251. } else {
  252. if (array_key_exists($k,$info)){
  253. $info[$k]=$v;
  254. }
  255. }
  256. }
  257. }
  258. }
  259. return $info;
  260. }}
  261. function get_theme_screenshot($theme) {
  262. $a = get_app();
  263. $exts = array('.png','.jpg');
  264. foreach($exts as $ext) {
  265. if(file_exists('view/theme/' . $theme . '/screenshot' . $ext))
  266. return($a->get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
  267. }
  268. return($a->get_baseurl() . '/images/blank.png');
  269. }
  270. // check service_class restrictions. If there are no service_classes defined, everything is allowed.
  271. // if $usage is supplied, we check against a maximum count and return true if the current usage is
  272. // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
  273. // is allowed (or not) in this subscriber plan. An unset property for this service plan means
  274. // the property is allowed, so it is only necessary to provide negative properties for each plan,
  275. // or what the subscriber is not allowed to do.
  276. function service_class_allows($uid,$property,$usage = false) {
  277. if($uid == local_user()) {
  278. $service_class = $a->user['service_class'];
  279. }
  280. else {
  281. $r = q("select service_class from user where uid = %d limit 1",
  282. intval($uid)
  283. );
  284. if($r !== false and count($r)) {
  285. $service_class = $r[0]['service_class'];
  286. }
  287. }
  288. if(! x($service_class))
  289. return true; // everything is allowed
  290. $arr = get_config('service_class',$service_class);
  291. if(! is_array($arr) || (! count($arr)))
  292. return true;
  293. if($usage === false)
  294. return ((x($arr[$property])) ? (bool) $arr['property'] : true);
  295. else {
  296. if(! array_key_exists($property,$arr))
  297. return true;
  298. return (((intval($usage)) < intval($arr[$property])) ? true : false);
  299. }
  300. }
  301. function service_class_fetch($uid,$property) {
  302. if($uid == local_user()) {
  303. $service_class = $a->user['service_class'];
  304. }
  305. else {
  306. $r = q("select service_class from user where uid = %d limit 1",
  307. intval($uid)
  308. );
  309. if($r !== false and count($r)) {
  310. $service_class = $r[0]['service_class'];
  311. }
  312. }
  313. if(! x($service_class))
  314. return false; // everything is allowed
  315. $arr = get_config('service_class',$service_class);
  316. if(! is_array($arr) || (! count($arr)))
  317. return false;
  318. return((array_key_exists($property,$arr)) ? $arr[$property] : false);
  319. }
  320. function upgrade_link($bbcode = false) {
  321. $l = get_config('service_class','upgrade_link');
  322. if(! $l)
  323. return '';
  324. if($bbcode)
  325. $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
  326. else
  327. $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
  328. return $t;
  329. }
  330. function upgrade_message($bbcode = false) {
  331. $x = upgrade_link($bbcode);
  332. return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
  333. }
  334. function upgrade_bool_message($bbcode = false) {
  335. $x = upgrade_link($bbcode);
  336. return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
  337. }