PageRenderTime 142ms CodeModel.GetById 54ms RepoModel.GetById 14ms app.codeStats 0ms

/randplace/randplace.php

https://github.com/chiefdome/friendica-addons
PHP | 179 lines | 56 code | 49 blank | 74 comment | 12 complexity | 5b9999728b1d5c36f702f6e629296f34 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0, GPL-2.0
  1. <?php
  2. /**
  3. * Name: Random place
  4. * Description: Sample Friendica plugin/addon. Set a random place when posting.
  5. * Version: 1.0
  6. * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
  7. *
  8. *
  9. *
  10. *
  11. * Addons are registered with the system through the admin
  12. * panel.
  13. *
  14. * When registration is detected, the system calls the plugin
  15. * name_install() function, located in 'addon/name/name.php',
  16. * where 'name' is the name of the addon.
  17. * If the addon is removed from the configuration list, the
  18. * system will call the name_uninstall() function.
  19. *
  20. */
  21. function randplace_install() {
  22. /**
  23. *
  24. * Our demo plugin will attach in three places.
  25. * The first is just prior to storing a local post.
  26. *
  27. */
  28. register_hook('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
  29. /**
  30. *
  31. * Then we'll attach into the plugin settings page, and also the
  32. * settings post hook so that we can create and update
  33. * user preferences.
  34. *
  35. */
  36. register_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
  37. register_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
  38. logger("installed randplace");
  39. }
  40. function randplace_uninstall() {
  41. /**
  42. *
  43. * uninstall unregisters any hooks created with register_hook
  44. * during install. It may also delete configuration settings
  45. * and any other cleanup.
  46. *
  47. */
  48. unregister_hook('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
  49. unregister_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
  50. unregister_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
  51. logger("removed randplace");
  52. }
  53. function randplace_post_hook($a, &$item) {
  54. /**
  55. *
  56. * An item was posted on the local system.
  57. * We are going to look for specific items:
  58. * - A status post by a profile owner
  59. * - The profile owner must have allowed our plugin
  60. *
  61. */
  62. logger('randplace invoked');
  63. if(! local_user()) /* non-zero if this is a logged in user of this system */
  64. return;
  65. if(local_user() != $item['uid']) /* Does this person own the post? */
  66. return;
  67. if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
  68. return;
  69. /* Retrieve our personal config setting */
  70. $active = get_pconfig(local_user(), 'randplace', 'enable');
  71. if(! $active)
  72. return;
  73. /**
  74. *
  75. * OK, we're allowed to do our stuff.
  76. * Here's what we are going to do:
  77. * load the list of timezone names, and use that to generate a list of world cities.
  78. * Then we'll pick one of those at random and put it in the "location" field for the post.
  79. *
  80. */
  81. $cities = array();
  82. $zones = timezone_identifiers_list();
  83. foreach($zones as $zone) {
  84. if((strpos($zone,'/')) && (! stristr($zone,'US/')) && (! stristr($zone,'Etc/')))
  85. $cities[] = str_replace('_', ' ',substr($zone,strpos($zone,'/') + 1));
  86. }
  87. if(! count($cities))
  88. return;
  89. $city = array_rand($cities,1);
  90. $item['location'] = $cities[$city];
  91. return;
  92. }
  93. /**
  94. *
  95. * Callback from the settings post function.
  96. * $post contains the $_POST array.
  97. * We will make sure we've got a valid user account
  98. * and if so set our configuration setting for this person.
  99. *
  100. */
  101. function randplace_settings_post($a,$post) {
  102. if(! local_user())
  103. return;
  104. if($_POST['randplace-submit'])
  105. set_pconfig(local_user(),'randplace','enable',intval($_POST['randplace']));
  106. }
  107. /**
  108. *
  109. * Called from the Plugin Setting form.
  110. * Add our own settings info to the page.
  111. *
  112. */
  113. function randplace_settings(&$a,&$s) {
  114. if(! local_user())
  115. return;
  116. /* Add our stylesheet to the page so we can make our settings look nice */
  117. $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/randplace/randplace.css' . '" media="all" />' . "\r\n";
  118. /* Get the current state of our config variable */
  119. $enabled = get_pconfig(local_user(),'randplace','enable');
  120. $checked = (($enabled) ? ' checked="checked" ' : '');
  121. /* Add some HTML to the existing form */
  122. $s .= '<div class="settings-block">';
  123. $s .= '<h3>' . t('Randplace Settings') . '</h3>';
  124. $s .= '<div id="randplace-enable-wrapper">';
  125. $s .= '<label id="randplace-enable-label" for="randplace-checkbox">' . t('Enable Randplace Plugin') . '</label>';
  126. $s .= '<input id="randplace-checkbox" type="checkbox" name="randplace" value="1" ' . $checked . '/>';
  127. $s .= '</div><div class="clear"></div>';
  128. /* provide a submit button */
  129. $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="randplace-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
  130. }