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.
 
 
 
 
 
 

470 lines
11 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'",
  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",
  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'",
  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, `file`");
  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. $stamp1 = microtime(true);
  129. $a = get_app();
  130. if((is_array($a->hooks)) && (array_key_exists($name,$a->hooks))) {
  131. foreach($a->hooks[$name] as $hook) {
  132. // Don't run a theme's hook if the user isn't using the theme
  133. if(strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
  134. continue;
  135. @include_once($hook[0]);
  136. if(function_exists($hook[1])) {
  137. $func = $hook[1];
  138. $func($a,$data);
  139. }
  140. else {
  141. // remove orphan hooks
  142. q("delete from hook where hook = '%s' and file = '%s' and function = '%s'",
  143. dbesc($name),
  144. dbesc($hook[0]),
  145. dbesc($hook[1])
  146. );
  147. }
  148. }
  149. }
  150. }}
  151. //check if an app_menu hook exist for plugin $name.
  152. //Return true if the plugin is an app
  153. if(! function_exists('plugin_is_app')) {
  154. function plugin_is_app($name) {
  155. $a = get_app();
  156. if(is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
  157. foreach($a->hooks['app_menu'] as $hook) {
  158. if($hook[0] == 'addon/'.$name.'/'.$name.'.php')
  159. return true;
  160. }
  161. }
  162. return false;
  163. }}
  164. /*
  165. * parse plugin comment in search of plugin infos.
  166. * like
  167. *
  168. * * Name: Plugin
  169. * * Description: A plugin which plugs in
  170. * * Version: 1.2.3
  171. * * Author: John <profile url>
  172. * * Author: Jane <email>
  173. * *
  174. */
  175. if (! function_exists('get_plugin_info')){
  176. function get_plugin_info($plugin){
  177. $a = get_app();
  178. $info=Array(
  179. 'name' => $plugin,
  180. 'description' => "",
  181. 'author' => array(),
  182. 'version' => "",
  183. 'status' => ""
  184. );
  185. if (!is_file("addon/$plugin/$plugin.php")) return $info;
  186. $stamp1 = microtime(true);
  187. $f = file_get_contents("addon/$plugin/$plugin.php");
  188. $a->save_timestamp($stamp1, "file");
  189. $r = preg_match("|/\*.*\*/|msU", $f, $m);
  190. if ($r){
  191. $ll = explode("\n", $m[0]);
  192. foreach( $ll as $l ) {
  193. $l = trim($l,"\t\n\r */");
  194. if ($l!=""){
  195. list($k,$v) = array_map("trim", explode(":",$l,2));
  196. $k= strtolower($k);
  197. if ($k=="author"){
  198. $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
  199. if ($r) {
  200. $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
  201. } else {
  202. $info['author'][] = array('name'=>$v);
  203. }
  204. } else {
  205. if (array_key_exists($k,$info)){
  206. $info[$k]=$v;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. return $info;
  213. }}
  214. /*
  215. * parse theme comment in search of theme infos.
  216. * like
  217. *
  218. * * Name: My Theme
  219. * * Description: My Cool Theme
  220. * * Version: 1.2.3
  221. * * Author: John <profile url>
  222. * * Maintainer: Jane <profile url>
  223. * *
  224. */
  225. if (! function_exists('get_theme_info')){
  226. function get_theme_info($theme){
  227. $info=Array(
  228. 'name' => $theme,
  229. 'description' => "",
  230. 'author' => array(),
  231. 'maintainer' => array(),
  232. 'version' => "",
  233. 'credits' => "",
  234. 'experimental' => false,
  235. 'unsupported' => false
  236. );
  237. if(file_exists("view/theme/$theme/experimental"))
  238. $info['experimental'] = true;
  239. if(file_exists("view/theme/$theme/unsupported"))
  240. $info['unsupported'] = true;
  241. if (!is_file("view/theme/$theme/theme.php")) return $info;
  242. $a = get_app();
  243. $stamp1 = microtime(true);
  244. $f = file_get_contents("view/theme/$theme/theme.php");
  245. $a->save_timestamp($stamp1, "file");
  246. $r = preg_match("|/\*.*\*/|msU", $f, $m);
  247. if ($r){
  248. $ll = explode("\n", $m[0]);
  249. foreach( $ll as $l ) {
  250. $l = trim($l,"\t\n\r */");
  251. if ($l!=""){
  252. list($k,$v) = array_map("trim", explode(":",$l,2));
  253. $k= strtolower($k);
  254. if ($k=="author"){
  255. $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
  256. if ($r) {
  257. $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
  258. } else {
  259. $info['author'][] = array('name'=>$v);
  260. }
  261. }
  262. elseif ($k=="maintainer"){
  263. $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
  264. if ($r) {
  265. $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
  266. } else {
  267. $info['maintainer'][] = array('name'=>$v);
  268. }
  269. } else {
  270. if (array_key_exists($k,$info)){
  271. $info[$k]=$v;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. return $info;
  278. }}
  279. function get_theme_screenshot($theme) {
  280. $a = get_app();
  281. $exts = array('.png','.jpg');
  282. foreach($exts as $ext) {
  283. if(file_exists('view/theme/' . $theme . '/screenshot' . $ext))
  284. return($a->get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
  285. }
  286. return($a->get_baseurl() . '/images/blank.png');
  287. }
  288. // install and uninstall theme
  289. if (! function_exists('uninstall_theme')){
  290. function uninstall_theme($theme){
  291. logger("Addons: uninstalling theme " . $theme);
  292. @include_once("view/theme/$theme/theme.php");
  293. if(function_exists("{$theme}_uninstall")) {
  294. $func = "{$theme}_uninstall";
  295. $func();
  296. }
  297. }}
  298. if (! function_exists('install_theme')){
  299. function install_theme($theme) {
  300. // silently fail if theme was removed
  301. if(! file_exists("view/theme/$theme/theme.php"))
  302. return false;
  303. logger("Addons: installing theme $theme");
  304. @include_once("view/theme/$theme/theme.php");
  305. if(function_exists("{$theme}_install")) {
  306. $func = "{$theme}_install";
  307. $func();
  308. return true;
  309. }
  310. else {
  311. logger("Addons: FAILED installing theme $theme");
  312. return false;
  313. }
  314. }}
  315. // check service_class restrictions. If there are no service_classes defined, everything is allowed.
  316. // if $usage is supplied, we check against a maximum count and return true if the current usage is
  317. // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
  318. // is allowed (or not) in this subscriber plan. An unset property for this service plan means
  319. // the property is allowed, so it is only necessary to provide negative properties for each plan,
  320. // or what the subscriber is not allowed to do.
  321. function service_class_allows($uid,$property,$usage = false) {
  322. if($uid == local_user()) {
  323. $service_class = $a->user['service_class'];
  324. }
  325. else {
  326. $r = q("select service_class from user where uid = %d limit 1",
  327. intval($uid)
  328. );
  329. if($r !== false and count($r)) {
  330. $service_class = $r[0]['service_class'];
  331. }
  332. }
  333. if(! x($service_class))
  334. return true; // everything is allowed
  335. $arr = get_config('service_class',$service_class);
  336. if(! is_array($arr) || (! count($arr)))
  337. return true;
  338. if($usage === false)
  339. return ((x($arr[$property])) ? (bool) $arr['property'] : true);
  340. else {
  341. if(! array_key_exists($property,$arr))
  342. return true;
  343. return (((intval($usage)) < intval($arr[$property])) ? true : false);
  344. }
  345. }
  346. function service_class_fetch($uid,$property) {
  347. if($uid == local_user()) {
  348. $service_class = $a->user['service_class'];
  349. }
  350. else {
  351. $r = q("select service_class from user where uid = %d limit 1",
  352. intval($uid)
  353. );
  354. if($r !== false and count($r)) {
  355. $service_class = $r[0]['service_class'];
  356. }
  357. }
  358. if(! x($service_class))
  359. return false; // everything is allowed
  360. $arr = get_config('service_class',$service_class);
  361. if(! is_array($arr) || (! count($arr)))
  362. return false;
  363. return((array_key_exists($property,$arr)) ? $arr[$property] : false);
  364. }
  365. function upgrade_link($bbcode = false) {
  366. $l = get_config('service_class','upgrade_link');
  367. if(! $l)
  368. return '';
  369. if($bbcode)
  370. $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
  371. else
  372. $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
  373. return $t;
  374. }
  375. function upgrade_message($bbcode = false) {
  376. $x = upgrade_link($bbcode);
  377. return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
  378. }
  379. function upgrade_bool_message($bbcode = false) {
  380. $x = upgrade_link($bbcode);
  381. return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
  382. }