OpenXPlugin.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin for OpenX ad server
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Ads
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2010 StatusNet Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * Plugin for OpenX Ad Server
  34. *
  35. * This plugin supports the OpenX ad server, http://www.openx.org/
  36. *
  37. * We support the 4 ad sizes for the Universal Ad Platform (UAP):
  38. *
  39. * Medium Rectangle
  40. * (Small) Rectangle
  41. * Leaderboard
  42. * Wide Skyscraper
  43. *
  44. * They fit in different places on the default theme. Some themes
  45. * might interact quite poorly with this plugin.
  46. *
  47. * To enable advertising, you will need an OpenX server. You'll need
  48. * to set up a "zone" for your StatusNet site that identifies a
  49. * kind of ad you want to place (of the above 4 sizes).
  50. *
  51. * Add the plugin to config.php like so:
  52. *
  53. * addPlugin('OpenX', array('adScript' => 'full path to script',
  54. * 'rectangle' => 1));
  55. *
  56. * Here, the 'adScript' parameter is the full path to the OpenX
  57. * ad script, like 'http://example.com/www/delivery/ajs.php'. Note
  58. * that we don't do any magic to swap between HTTP and HTTPS, so
  59. * if you want HTTPS, say so.
  60. *
  61. * The 'rectangle' parameter is the zone ID for that ad space on
  62. * your site. If you've configured another size, try 'mediumRectangle',
  63. * 'leaderboard', or 'wideSkyscraper'.
  64. *
  65. * If for some reason your ad server is different from the default,
  66. * use the 'adScript' parameter to set the full path to the ad script.
  67. *
  68. * @category Ads
  69. * @package StatusNet
  70. * @author Evan Prodromou <evan@status.net>
  71. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  72. * @link http://status.net/
  73. *
  74. * @seeAlso UAPPlugin
  75. */
  76. class OpenXPlugin extends UAPPlugin
  77. {
  78. const PLUGIN_VERSION = '2.0.0';
  79. public $adScript = null;
  80. function initialize()
  81. {
  82. parent::initialize();
  83. // A little bit of chicanery so we avoid overwriting values that
  84. // are passed in with the constructor
  85. foreach (array('mediumRectangle', 'rectangle', 'leaderboard', 'wideSkyscraper', 'adScript') as $setting) {
  86. $value = common_config('openx', $setting);
  87. if (!empty($value)) { // not found
  88. $this->$setting = $value;
  89. }
  90. }
  91. return true;
  92. }
  93. /**
  94. * Show a medium rectangle 'ad'
  95. *
  96. * @param Action $action Action being shown
  97. *
  98. * @return void
  99. */
  100. protected function showMediumRectangle($action)
  101. {
  102. $this->showAd($action, $this->mediumRectangle);
  103. }
  104. /**
  105. * Show a rectangle 'ad'
  106. *
  107. * @param Action $action Action being shown
  108. *
  109. * @return void
  110. */
  111. protected function showRectangle($action)
  112. {
  113. $this->showAd($action, $this->rectangle);
  114. }
  115. /**
  116. * Show a wide skyscraper ad
  117. *
  118. * @param Action $action Action being shown
  119. *
  120. * @return void
  121. */
  122. protected function showWideSkyscraper($action)
  123. {
  124. $this->showAd($action, $this->wideSkyscraper);
  125. }
  126. /**
  127. * Show a leaderboard ad
  128. *
  129. * @param Action $action Action being shown
  130. *
  131. * @return void
  132. */
  133. protected function showLeaderboard($action)
  134. {
  135. $this->showAd($action, $this->leaderboard);
  136. }
  137. /**
  138. * Show an ad using OpenX
  139. *
  140. * @param Action $action Action being shown
  141. * @param integer $zone Zone to show
  142. *
  143. * @return void
  144. */
  145. protected function showAd($action, $zone)
  146. {
  147. $scr = <<<ENDOFSCRIPT
  148. var m3_u = '%s';
  149. var m3_r = Math.floor(Math.random()*99999999999);
  150. if (!document.MAX_used) document.MAX_used = ',';
  151. document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
  152. document.write ("?zoneid=%d");
  153. document.write ('&amp;cb=' + m3_r);
  154. if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
  155. document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
  156. document.write ("&amp;loc=" + escape(window.location));
  157. if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
  158. if (document.context) document.write ("&context=" + escape(document.context));
  159. if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
  160. document.write ("'><\/scr"+"ipt>");
  161. ENDOFSCRIPT;
  162. $action->inlineScript(sprintf($scr, $this->adScript, $zone));
  163. return true;
  164. }
  165. function onRouterInitialized($m)
  166. {
  167. $m->connect('panel/openx',
  168. ['action' => 'openxadminpanel']);
  169. return true;
  170. }
  171. function onEndAdminPanelNav($menu) {
  172. if (AdminPanelAction::canAdmin('openx')) {
  173. // TRANS: Menu item title.
  174. $menu_title = _m('OpenX configuration.');
  175. // TRANS: Menu item for site administration
  176. $menu->out->menuItem(common_local_url('openxadminpanel'), _m('OpenX'),
  177. $menu_title, $action_name == 'openxadminpanel', 'nav_openx_admin_panel');
  178. }
  179. return true;
  180. }
  181. /**
  182. * Add our version information to output
  183. *
  184. * @param array &$versions Array of version-data arrays
  185. *
  186. * @return boolean hook value
  187. */
  188. function onPluginVersion(array &$versions)
  189. {
  190. $versions[] = array('name' => 'OpenX',
  191. 'version' => self::PLUGIN_VERSION,
  192. 'author' => 'Evan Prodromou',
  193. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/OpenX',
  194. 'rawdescription' =>
  195. // TRANS: Plugin description.
  196. _m('Plugin for <a href="http://www.openx.org/">OpenX Ad Server</a>.'));
  197. return true;
  198. }
  199. }