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.
 
 
 
 
 
 

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