OpenXPlugin.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. public $adScript = null;
  79. function initialize()
  80. {
  81. parent::initialize();
  82. // A little bit of chicanery so we avoid overwriting values that
  83. // are passed in with the constructor
  84. foreach (array('mediumRectangle', 'rectangle', 'leaderboard', 'wideSkyscraper', 'adScript') as $setting) {
  85. $value = common_config('openx', $setting);
  86. if (!empty($value)) { // not found
  87. $this->$setting = $value;
  88. }
  89. }
  90. return true;
  91. }
  92. /**
  93. * Show a medium rectangle 'ad'
  94. *
  95. * @param Action $action Action being shown
  96. *
  97. * @return void
  98. */
  99. protected function showMediumRectangle($action)
  100. {
  101. $this->showAd($action, $this->mediumRectangle);
  102. }
  103. /**
  104. * Show a rectangle 'ad'
  105. *
  106. * @param Action $action Action being shown
  107. *
  108. * @return void
  109. */
  110. protected function showRectangle($action)
  111. {
  112. $this->showAd($action, $this->rectangle);
  113. }
  114. /**
  115. * Show a wide skyscraper ad
  116. *
  117. * @param Action $action Action being shown
  118. *
  119. * @return void
  120. */
  121. protected function showWideSkyscraper($action)
  122. {
  123. $this->showAd($action, $this->wideSkyscraper);
  124. }
  125. /**
  126. * Show a leaderboard ad
  127. *
  128. * @param Action $action Action being shown
  129. *
  130. * @return void
  131. */
  132. protected function showLeaderboard($action)
  133. {
  134. $this->showAd($action, $this->leaderboard);
  135. }
  136. /**
  137. * Show an ad using OpenX
  138. *
  139. * @param Action $action Action being shown
  140. * @param integer $zone Zone to show
  141. *
  142. * @return void
  143. */
  144. protected function showAd($action, $zone)
  145. {
  146. $scr = <<<ENDOFSCRIPT
  147. var m3_u = '%s';
  148. var m3_r = Math.floor(Math.random()*99999999999);
  149. if (!document.MAX_used) document.MAX_used = ',';
  150. document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
  151. document.write ("?zoneid=%d");
  152. document.write ('&amp;cb=' + m3_r);
  153. if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
  154. document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
  155. document.write ("&amp;loc=" + escape(window.location));
  156. if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
  157. if (document.context) document.write ("&context=" + escape(document.context));
  158. if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
  159. document.write ("'><\/scr"+"ipt>");
  160. ENDOFSCRIPT;
  161. $action->inlineScript(sprintf($scr, $this->adScript, $zone));
  162. return true;
  163. }
  164. function onRouterInitialized($m)
  165. {
  166. $m->connect('panel/openx',
  167. array('action' => 'openxadminpanel'));
  168. return true;
  169. }
  170. function onEndAdminPanelNav($menu) {
  171. if (AdminPanelAction::canAdmin('openx')) {
  172. // TRANS: Menu item title.
  173. $menu_title = _m('OpenX configuration.');
  174. // TRANS: Menu item for site administration
  175. $menu->out->menuItem(common_local_url('openxadminpanel'), _m('OpenX'),
  176. $menu_title, $action_name == 'openxadminpanel', 'nav_openx_admin_panel');
  177. }
  178. return true;
  179. }
  180. /**
  181. * Add our version information to output
  182. *
  183. * @param array &$versions Array of version-data arrays
  184. *
  185. * @return boolean hook value
  186. */
  187. function onPluginVersion(array &$versions)
  188. {
  189. $versions[] = array('name' => 'OpenX',
  190. 'version' => GNUSOCIAL_VERSION,
  191. 'author' => 'Evan Prodromou',
  192. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/OpenX',
  193. 'rawdescription' =>
  194. // TRANS: Plugin description.
  195. _m('Plugin for <a href="http://www.openx.org/">OpenX Ad Server</a>.'));
  196. return true;
  197. }
  198. }