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.

583 lines
14 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. <?php
  2. /**
  3. * @file include/plugin.php
  4. *
  5. * @brief Some functions to handle addons and themes.
  6. */
  7. use Friendica\App;
  8. /**
  9. * @brief uninstalls an addon.
  10. *
  11. * @param string $plugin name of the addon
  12. * @return boolean
  13. */
  14. if (! function_exists('uninstall_plugin')){
  15. function uninstall_plugin($plugin){
  16. logger("Addons: uninstalling " . $plugin);
  17. q("DELETE FROM `addon` WHERE `name` = '%s' ",
  18. dbesc($plugin)
  19. );
  20. @include_once('addon/' . $plugin . '/' . $plugin . '.php');
  21. if(function_exists($plugin . '_uninstall')) {
  22. $func = $plugin . '_uninstall';
  23. $func();
  24. }
  25. }}
  26. /**
  27. * @brief installs an addon.
  28. *
  29. * @param string $plugin name of the addon
  30. * @return bool
  31. */
  32. if (! function_exists('install_plugin')){
  33. function install_plugin($plugin) {
  34. // silently fail if plugin was removed
  35. if(! file_exists('addon/' . $plugin . '/' . $plugin . '.php'))
  36. return false;
  37. logger("Addons: installing " . $plugin);
  38. $t = @filemtime('addon/' . $plugin . '/' . $plugin . '.php');
  39. @include_once('addon/' . $plugin . '/' . $plugin . '.php');
  40. if(function_exists($plugin . '_install')) {
  41. $func = $plugin . '_install';
  42. $func();
  43. $plugin_admin = (function_exists($plugin."_plugin_admin")?1:0);
  44. $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ",
  45. dbesc($plugin),
  46. intval($t),
  47. $plugin_admin
  48. );
  49. // we can add the following with the previous SQL
  50. // once most site tables have been updated.
  51. // This way the system won't fall over dead during the update.
  52. if(file_exists('addon/' . $plugin . '/.hidden')) {
  53. q("UPDATE `addon` SET `hidden` = 1 WHERE `name` = '%s'",
  54. dbesc($plugin)
  55. );
  56. }
  57. return true;
  58. }
  59. else {
  60. logger("Addons: FAILED installing " . $plugin);
  61. return false;
  62. }
  63. }}
  64. // reload all updated plugins
  65. if(! function_exists('reload_plugins')) {
  66. function reload_plugins() {
  67. $plugins = get_config('system','addon');
  68. if(strlen($plugins)) {
  69. $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
  70. if (dbm::is_result($r))
  71. $installed = $r;
  72. else
  73. $installed = array();
  74. $parr = explode(',',$plugins);
  75. if(count($parr)) {
  76. foreach($parr as $pl) {
  77. $pl = trim($pl);
  78. $fname = 'addon/' . $pl . '/' . $pl . '.php';
  79. if(file_exists($fname)) {
  80. $t = @filemtime($fname);
  81. foreach($installed as $i) {
  82. if(($i['name'] == $pl) && ($i['timestamp'] != $t)) {
  83. logger('Reloading plugin: ' . $i['name']);
  84. @include_once($fname);
  85. if(function_exists($pl . '_uninstall')) {
  86. $func = $pl . '_uninstall';
  87. $func();
  88. }
  89. if(function_exists($pl . '_install')) {
  90. $func = $pl . '_install';
  91. $func();
  92. }
  93. q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d",
  94. intval($t),
  95. intval($i['id'])
  96. );
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }}
  104. /**
  105. * @brief check if addon is enabled
  106. *
  107. * @param string $plugin
  108. * @return boolean
  109. */
  110. function plugin_enabled($plugin) {
  111. $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = '%s'", $plugin);
  112. return ((dbm::is_result($r)) && (count($r) > 0));
  113. }
  114. /**
  115. * @brief registers a hook.
  116. *
  117. * @param string $hook the name of the hook
  118. * @param string $file the name of the file that hooks into
  119. * @param string $function the name of the function that the hook will call
  120. * @param int $priority A priority (defaults to 0)
  121. * @return mixed|bool
  122. */
  123. if(! function_exists('register_hook')) {
  124. function register_hook($hook,$file,$function,$priority=0) {
  125. $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
  126. dbesc($hook),
  127. dbesc($file),
  128. dbesc($function)
  129. );
  130. if (dbm::is_result($r))
  131. return true;
  132. $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`) VALUES ( '%s', '%s', '%s', '%s' ) ",
  133. dbesc($hook),
  134. dbesc($file),
  135. dbesc($function),
  136. dbesc($priority)
  137. );
  138. return $r;
  139. }}
  140. /**
  141. * @brief unregisters a hook.
  142. *
  143. * @param string $hook the name of the hook
  144. * @param string $file the name of the file that hooks into
  145. * @param string $function the name of the function that the hook called
  146. * @return array
  147. */
  148. if(! function_exists('unregister_hook')) {
  149. function unregister_hook($hook,$file,$function) {
  150. $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
  151. dbesc($hook),
  152. dbesc($file),
  153. dbesc($function)
  154. );
  155. return $r;
  156. }}
  157. if(! function_exists('load_hooks')) {
  158. function load_hooks() {
  159. $a = get_app();
  160. $a->hooks = array();
  161. $r = q("SELECT * FROM `hook` WHERE 1 ORDER BY `priority` DESC, `file`");
  162. if (dbm::is_result($r)) {
  163. foreach ($r as $rr) {
  164. if(! array_key_exists($rr['hook'],$a->hooks))
  165. $a->hooks[$rr['hook']] = array();
  166. $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
  167. }
  168. }
  169. }}
  170. /**
  171. * @brief Calls a hook.
  172. *
  173. * Use this function when you want to be able to allow a hook to manipulate
  174. * the provided data.
  175. *
  176. * @param string $name of the hook to call
  177. * @param string|array &$data to transmit to the callback handler
  178. */
  179. function call_hooks($name, &$data = null) {
  180. $stamp1 = microtime(true);
  181. $a = get_app();
  182. if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
  183. foreach ($a->hooks[$name] as $hook)
  184. call_single_hook($a, $name, $hook, $data);
  185. }
  186. /**
  187. * @brief Calls a single hook.
  188. *
  189. * @param string $name of the hook to call
  190. * @param array $hook Hook data
  191. * @param string|array &$data to transmit to the callback handler
  192. */
  193. function call_single_hook($a, $name, $hook, &$data = null) {
  194. // Don't run a theme's hook if the user isn't using the theme
  195. if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
  196. return;
  197. @include_once($hook[0]);
  198. if (function_exists($hook[1])) {
  199. $func = $hook[1];
  200. $func($a, $data);
  201. } else {
  202. // remove orphan hooks
  203. q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
  204. dbesc($name),
  205. dbesc($hook[0]),
  206. dbesc($hook[1])
  207. );
  208. }
  209. }
  210. //check if an app_menu hook exist for plugin $name.
  211. //Return true if the plugin is an app
  212. if(! function_exists('plugin_is_app')) {
  213. function plugin_is_app($name) {
  214. $a = get_app();
  215. if(is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
  216. foreach($a->hooks['app_menu'] as $hook) {
  217. if($hook[0] == 'addon/'.$name.'/'.$name.'.php')
  218. return true;
  219. }
  220. }
  221. return false;
  222. }}
  223. /**
  224. * @brief Parse plugin comment in search of plugin infos.
  225. *
  226. * like
  227. * \code
  228. *...* Name: Plugin
  229. * * Description: A plugin which plugs in
  230. * . * Version: 1.2.3
  231. * * Author: John <profile url>
  232. * * Author: Jane <email>
  233. * *
  234. * *\endcode
  235. * @param string $plugin the name of the plugin
  236. * @return array with the plugin information
  237. */
  238. if (! function_exists('get_plugin_info')){
  239. function get_plugin_info($plugin){
  240. $a = get_app();
  241. $info=Array(
  242. 'name' => $plugin,
  243. 'description' => "",
  244. 'author' => array(),
  245. 'version' => "",
  246. 'status' => ""
  247. );
  248. if (!is_file("addon/$plugin/$plugin.php")) return $info;
  249. $stamp1 = microtime(true);
  250. $f = file_get_contents("addon/$plugin/$plugin.php");
  251. $a->save_timestamp($stamp1, "file");
  252. $r = preg_match("|/\*.*\*/|msU", $f, $m);
  253. if ($r){
  254. $ll = explode("\n", $m[0]);
  255. foreach( $ll as $l ) {
  256. $l = trim($l,"\t\n\r */");
  257. if ($l!=""){
  258. list($k,$v) = array_map("trim", explode(":",$l,2));
  259. $k= strtolower($k);
  260. if ($k=="author"){
  261. $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
  262. if ($r) {
  263. $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
  264. } else {
  265. $info['author'][] = array('name'=>$v);
  266. }
  267. } else {
  268. if (array_key_exists($k,$info)){
  269. $info[$k]=$v;
  270. }
  271. }
  272. }
  273. }
  274. }
  275. return $info;
  276. }}
  277. /**
  278. * @brief Parse theme comment in search of theme infos.
  279. *
  280. * like
  281. * \code
  282. * ..* Name: My Theme
  283. * * Description: My Cool Theme
  284. * . * Version: 1.2.3
  285. * * Author: John <profile url>
  286. * * Maintainer: Jane <profile url>
  287. * *
  288. * \endcode
  289. * @param string $theme the name of the theme
  290. * @return array
  291. */
  292. if (! function_exists('get_theme_info')){
  293. function get_theme_info($theme){
  294. $info=Array(
  295. 'name' => $theme,
  296. 'description' => "",
  297. 'author' => array(),
  298. 'maintainer' => array(),
  299. 'version' => "",
  300. 'credits' => "",
  301. 'experimental' => false,
  302. 'unsupported' => false
  303. );
  304. if(file_exists("view/theme/$theme/experimental"))
  305. $info['experimental'] = true;
  306. if(file_exists("view/theme/$theme/unsupported"))
  307. $info['unsupported'] = true;
  308. if (!is_file("view/theme/$theme/theme.php")) return $info;
  309. $a = get_app();
  310. $stamp1 = microtime(true);
  311. $f = file_get_contents("view/theme/$theme/theme.php");
  312. $a->save_timestamp($stamp1, "file");
  313. $r = preg_match("|/\*.*\*/|msU", $f, $m);
  314. if ($r){
  315. $ll = explode("\n", $m[0]);
  316. foreach( $ll as $l ) {
  317. $l = trim($l,"\t\n\r */");
  318. if ($l!=""){
  319. list($k,$v) = array_map("trim", explode(":",$l,2));
  320. $k= strtolower($k);
  321. if ($k=="author"){
  322. $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
  323. if ($r) {
  324. $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
  325. } else {
  326. $info['author'][] = array('name'=>$v);
  327. }
  328. }
  329. elseif ($k=="maintainer"){
  330. $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
  331. if ($r) {
  332. $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
  333. } else {
  334. $info['maintainer'][] = array('name'=>$v);
  335. }
  336. } else {
  337. if (array_key_exists($k,$info)){
  338. $info[$k]=$v;
  339. }
  340. }
  341. }
  342. }
  343. }
  344. return $info;
  345. }}
  346. /**
  347. * @brief Returns the theme's screenshot.
  348. *
  349. * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
  350. *
  351. * @param sring $theme The name of the theme
  352. * @return string
  353. */
  354. function get_theme_screenshot($theme) {
  355. $exts = array('.png','.jpg');
  356. foreach($exts as $ext) {
  357. if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
  358. return(App::get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
  359. }
  360. }
  361. return(App::get_baseurl() . '/images/blank.png');
  362. }
  363. // install and uninstall theme
  364. if (! function_exists('uninstall_theme')){
  365. function uninstall_theme($theme){
  366. logger("Addons: uninstalling theme " . $theme);
  367. include_once("view/theme/$theme/theme.php");
  368. if (function_exists("{$theme}_uninstall")) {
  369. $func = "{$theme}_uninstall";
  370. $func();
  371. }
  372. }}
  373. if (! function_exists('install_theme')){
  374. function install_theme($theme) {
  375. // silently fail if theme was removed
  376. if (! file_exists("view/theme/$theme/theme.php")) {
  377. return false;
  378. }
  379. logger("Addons: installing theme $theme");
  380. include_once("view/theme/$theme/theme.php");
  381. if (function_exists("{$theme}_install")) {
  382. $func = "{$theme}_install";
  383. $func();
  384. return true;
  385. } else {
  386. logger("Addons: FAILED installing theme $theme");
  387. return false;
  388. }
  389. }}
  390. // check service_class restrictions. If there are no service_classes defined, everything is allowed.
  391. // if $usage is supplied, we check against a maximum count and return true if the current usage is
  392. // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
  393. // is allowed (or not) in this subscriber plan. An unset property for this service plan means
  394. // the property is allowed, so it is only necessary to provide negative properties for each plan,
  395. // or what the subscriber is not allowed to do.
  396. function service_class_allows($uid,$property,$usage = false) {
  397. if ($uid == local_user()) {
  398. $service_class = $a->user['service_class'];
  399. } else {
  400. $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
  401. intval($uid)
  402. );
  403. if (dbm::is_result($r)) {
  404. $service_class = $r[0]['service_class'];
  405. }
  406. }
  407. if (! x($service_class)) {
  408. // everything is allowed
  409. return true;
  410. }
  411. $arr = get_config('service_class',$service_class);
  412. if (! is_array($arr) || (! count($arr))) {
  413. return true;
  414. }
  415. if ($usage === false) {
  416. return ((x($arr[$property])) ? (bool) $arr['property'] : true);
  417. } else {
  418. if (! array_key_exists($property,$arr)) {
  419. return true;
  420. }
  421. return (((intval($usage)) < intval($arr[$property])) ? true : false);
  422. }
  423. }
  424. function service_class_fetch($uid,$property) {
  425. if ($uid == local_user()) {
  426. $service_class = $a->user['service_class'];
  427. } else {
  428. $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
  429. intval($uid)
  430. );
  431. if (dbm::is_result($r)) {
  432. $service_class = $r[0]['service_class'];
  433. }
  434. }
  435. if(! x($service_class))
  436. return false; // everything is allowed
  437. $arr = get_config('service_class',$service_class);
  438. if(! is_array($arr) || (! count($arr)))
  439. return false;
  440. return((array_key_exists($property,$arr)) ? $arr[$property] : false);
  441. }
  442. function upgrade_link($bbcode = false) {
  443. $l = get_config('service_class','upgrade_link');
  444. if(! $l)
  445. return '';
  446. if($bbcode)
  447. $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
  448. else
  449. $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
  450. return $t;
  451. }
  452. function upgrade_message($bbcode = false) {
  453. $x = upgrade_link($bbcode);
  454. return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
  455. }
  456. function upgrade_bool_message($bbcode = false) {
  457. $x = upgrade_link($bbcode);
  458. return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
  459. }
  460. /**
  461. * @brief Get the full path to relevant theme files by filename
  462. *
  463. * This function search in the theme directory (and if not present in global theme directory)
  464. * if there is a directory with the file extension and for a file with the given
  465. * filename.
  466. *
  467. * @param string $file Filename
  468. * @param string $root Full root path
  469. * @return string Path to the file or empty string if the file isn't found
  470. */
  471. function theme_include($file, $root = '') {
  472. // Make sure $root ends with a slash / if it's not blank
  473. if($root !== '' && $root[strlen($root)-1] !== '/')
  474. $root = $root . '/';
  475. $theme_info = $a->theme_info;
  476. if(is_array($theme_info) AND array_key_exists('extends',$theme_info))
  477. $parent = $theme_info['extends'];
  478. else
  479. $parent = 'NOPATH';
  480. $theme = current_theme();
  481. $thname = $theme;
  482. $ext = substr($file,strrpos($file,'.')+1);
  483. $paths = array(
  484. "{$root}view/theme/$thname/$ext/$file",
  485. "{$root}view/theme/$parent/$ext/$file",
  486. "{$root}view/$ext/$file",
  487. );
  488. foreach($paths as $p) {
  489. // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
  490. if(strpos($p,'NOPATH') !== false)
  491. continue;
  492. if(file_exists($p))
  493. return $p;
  494. }
  495. return '';
  496. }