1<?php
2/**
3 *  BookmarkMe Action component
4 *
5 *  $Id: action.php 111 2008-12-25 19:33:15Z wingedfox $
6 *  $HeadURL: https://svn.debugger.ru/repos/common/DokuWiki/BookmarkMe/tags/BookmarkMe.v0.8/action.php $
7 *
8 *  @lastmodified $Date: 2008-12-25 22:33:15 +0300 (Чтв, 25 Дек 2008) $
9 *  @license      LGPL 2 (http://www.gnu.org/licenses/lgpl.html)
10 *  @author       Ilya Lebedev <ilya@lebedev.net>
11 *  @version      $Rev: 111 $
12 *  @copyright    (c) 2005-2007, Ilya Lebedev
13 */
14
15if(!defined('DOKU_INC')) die();
16
17if(!function_exists('array_combine')) {
18    /**
19     *  array_combine emulation
20     *
21     *  @author Ivo van Sandick
22     *  @date 02-Sep-2005 12:04
23     *  @see http://php.net/manual/ru/function.array-combine.php#56424
24     *  @param array $keys array with keys
25     *  @param array $vals array with values
26     *  @return array resulting array with ($keys => $vals) pairs
27     */
28    function array_combine( $keys, $vals ) {
29        $keys = array_values( (array) $keys );
30        $vals = array_values( (array) $vals );
31        $n = max( count( $keys ), count( $vals ) );
32        $r = array();
33        for( $i=0; $i<$n; $i++ ) {
34           $r[ $keys[ $i ] ] = $vals[ $i ];
35        }
36        return $r;
37    }
38}
39
40if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
41require_once(DOKU_PLUGIN.'action.php');
42
43class action_plugin_bookmarkme extends DokuWiki_Action_Plugin {
44
45    var $bookmarks = array(
46        'del.icio.us' => 'http://del.icio.us/post?title=%title%&amp;url=%permalink%'
47       ,'Digg' => 'http://digg.com/submit?phase=2&amp;title=%title%&amp;url=%permalink%'
48       ,'Furl' => 'http://www.furl.net/storeIt.jsp?t=%title%&amp;u=%permalink%'
49       ,'Reddit' => 'http://reddit.com/submit?title=%title%&amp;url=%permalink%'
50       ,'Ask' => 'http://myjeeves.ask.com/mysearch/BookmarkIt?v=1.2&amp;t=webpages&amp;title=%title%&amp;url=%permalink%'
51       ,'BlinkList' => 'http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Title=%title%&amp;Description=&amp;Url=%permalink%'
52       ,'blogmarks' => 'http://blogmarks.net/my/new.php?mini=1&amp;simple=1&amp;title=%title%&amp;url=%permalink%'
53       ,'Google' => 'http://www.google.com/bookmarks/mark?op=add&amp;title=%title%&amp;bkmk=%permalink%'
54       ,'Ma.gnolia' => 'http://ma.gnolia.com/beta/bookmarklet/add?title=%title%&amp;description=%title%&amp;url=%permalink%'
55       ,'Netscape' => 'http://www.netscape.com/submit/?T=%title%&amp;U=%permalink%'
56       ,'ppnow' => 'http://www.ppnow.com/submit.php?url=%permalink%'
57       ,'Rojo' => 'http://www.rojo.com/submit/?title=%title%&amp;url=%permalink%'
58       ,'Shadows' => 'http://www.shadows.com/features/tcr.htm?title=%title%&amp;url=%permalink%'
59       ,'Simpy' => 'http://www.simpy.com/simpy/LinkAdd.do?title=%title%&amp;href=%permalink%'
60       ,'Socializer' => 'http://ekstreme.com/socializer/?title=%title%&amp;url=%permalink%'
61       ,'Spurl' => 'http://www.spurl.net/spurl.php?title=%title%&amp;url=%permalink%'
62       ,'StumbleUpon' => 'http://www.stumbleupon.com/submit?title=%title%&amp;url=%permalink%'
63       ,'Tailrank' => 'http://tailrank.com/share/?link_href=%permalink%&amp;title=%title%'
64       ,'Technorati' => 'http://www.technorati.com/faves?add=%permalink%'
65       ,'Live Bookmarks' => 'https://favorites.live.com/quickadd.aspx?marklet=1&amp;mkt=en-us&amp;title=%title%&amp;url=%permalink%&amp;top=1'
66       ,'Wists' => 'http://wists.com/r.php?c=&amp;title=%title%&amp;r=%permalink%'
67       ,'Yahoo! Myweb' => 'http://myweb2.search.yahoo.com/myresults/bookmarklet?title=%title%&amp;popup=true&amp;u=%permalink%'
68       ,'BobrDobr' => 'http://bobrdobr.ru/addext.html?url=%permalink%&amp;title=%title%'
69       ,'Memori' => 'http://memori.ru/link/?sm=1&amp;u_data[url]=%permalink%&amp;u_data[name]=%title%'
70       ,'Faves' => 'http://faves.com/Authoring.aspx?u=%title%&amp;t=%title%'
71       ,'Favorites' => "javascript: (function (url,title) {var e; try {if (document.all&&!window.opera) window.external.AddFavorite(url, title); else if (window.opera) {var el=document.createElement(&quot;a&quot;); el.rel=&quot;sidebar&quot;;el.href=url;el.title=title;el.click();} else window.sidebar.addPanel(title, url,&quot;&quot;);}catch(e){}})(&quot;%permalink%&quot;,&quot;%title%&quot;);"
72       ,'Facebook' => 'http://www.facebook.com/sharer.php?u=%permalink%&amp;t=%title%'
73       ,'Newsvine' => 'http://www.newsvine.com/_tools/seed&amp;save?u=%permalink%&amp;h=%title%'
74       ,'Yahoo! Bookmarks' => 'http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;u=%permalink%&t=%title%'
75       ,'Twitter' => 'http://twitter.com/home?status=%title%:%permalink%'
76       ,'myAOL' => 'http://favorites.my.aol.com/ffclient/AddBookmark?url=%permalink%&amp;title=%title%&amp;favelet=true'
77       ,'Slashdot' => 'http://slashdot.org/bookmark.pl?url=%permalink%'
78       ,'Fark' => 'http://cgi.fark.com/cgi/fark/submit.pl?new_url=%permalink%&amp;new_comment=%title%'
79       ,'RawSugar' => 'http://www.rawsugar.com/tagger/?turl=%permalink%&amp;tttl=%title%&amp;editorInitialized=1'
80       ,'LinkaGoGo' => 'http://www.linkagogo.com/go/AddNoPopup?url=%permalink%&amp;title=%title%'
81       ,'Mister Wong' => 'http://www.mister-wong.de/index.php?action=addurl&amp;bm_url=%permalink%&amp;bm_description=%title%'
82       ,'Wink' => 'http://www.wink.com/_/tag?url=%permalink%&amp;doctitle=%title%'
83       ,'BackFlip' => 'http://www.backflip.com/add_page_pop.ihtml?url=%permalink%&amp;title=%title%'
84       ,'Diigo' => 'http://www.diigo.com/post?url=%permalink%&amp;title=%title%'
85       ,'Segnalo' => 'http://segnalo.com/post.html.php?url=%permalink%&amp;title=%title%'
86       ,'Netvouz' => 'http://netvouz.com/action/submitBookmark?url=%permalink%&amp;title=%title%&amp;popup=no'
87       ,'DropJack' => 'http://www.dropjack.com/submit.php?url=%permalink%'
88       ,'Feed Me Links' => 'http://feedmelinks.com/categorize?from=toolbar&amp;op=submit&amp;url=%permalink%&amp;name=%title%'
89       ,'funP' => 'http://funp.com/push/submit/add.php?url=%permalink%&amp;s=%title%'
90       ,'HEMiDEMi' => 'http://www.hemidemi.com/user_bookmark/new?title=%title%&amp;url=%permalink%'
91    );
92
93  /**
94   *  Constants for controlling toolbar show
95   */
96  var $BOOKMARKME_SHOW_OFF    = 0;
97  var $BOOKMARKME_SHOW_ON     = 1;  // equals to bottom, pre-0.7 compatible
98  var $BOOKMARKME_SHOW_BOTTOM = 1;
99  var $BOOKMARKME_SHOW_TOP    = 2;
100  var $BOOKMARKME_SHOW_BOTH   = 3;
101
102  /**
103   * return some info
104   */
105  function getInfo(){
106      preg_match("#^.*?BookmarkMe/([^\\/]+)#"," $HeadURL: https://svn.debugger.ru/repos/common/DokuWiki/BookmarkMe/tags/BookmarkMe.v0.8/action.php $ ", $v);
107      $v = preg_replace("#.*?((trunk|\.v)[\d.]+)#","\\1",$v[1]);
108      $b = preg_replace("/\\D/","", " $Rev: 111 $ ");
109      return array( 'author' => "Ilya Lebedev"
110                   ,'email'  => 'ilya@lebedev.net'
111                   ,'date'   => preg_replace("#.*?(\d{4}-\d{2}-\d{2}).*#","\\1",'$Date: 2008-12-25 22:33:15 +0300 (Чтв, 25 Дек 2008) $')
112                   ,'name'   => "BookmarkMe {$v}.$b Action component."
113                   ,'desc'   => "Insert toolbar with links to social bookmarking services."
114                   ,'url'    => 'http://wiki.splitbrain.org/plugin:bookmarkme'
115                  );
116  }
117  /*
118   * plugin should use this method to register its handlers with the dokuwiki's event controller
119   */
120  function register(&$controller) {
121      $controller->register_hook('TPL_ACT_RENDER','AFTER',$this,'bookmark',array("show" => $this->BOOKMARKME_SHOW_BOTTOM));
122      $controller->register_hook('TPL_ACT_RENDER','BEFORE',$this,'bookmark',array("show" => $this->BOOKMARKME_SHOW_TOP));
123  }
124  /**
125   *  Prints bookmarking icons, if allowed
126   *
127   *  @author Ilya Lebedev <ilya@lebedev.net>
128   */
129  function bookmark(&$event, $param) {
130    global $ID;
131    global $conf;
132
133    if ($event->data != 'show') return; // nothing to do for us
134
135
136    $show = $this->getConf('show_bookmarks');
137
138    //Check if bookmarking is allowed
139    $bm = p_get_metadata($ID,'bookmarkme');
140
141    if (null !== $bm) {
142        $bm = 'BOOKMARKME_SHOW_'.strtoupper($bm);
143        $bm = (int)$this->$bm;
144        if (is_numeric($bm))
145            $show = $bm;
146    }
147
148    if ( !($show & $this->BOOKMARKME_SHOW_BOTTOM & $param['show'])
149      && !($show & $this->BOOKMARKME_SHOW_TOP & $param['show']))
150        return;
151
152    /*
153    *  assume that page does not exists
154    */
155    $exists = false;
156    $id = $ID;
157    resolve_pageid('',$id,$exists);
158
159    /*
160    *  find skip pages
161    */
162    $sp = join("|",explode(",",preg_quote($this->getConf('skip_ids'))));
163
164    if (!$exists || preg_match("/$sp/i",$ID))
165        return;
166
167
168    if (@file_exists(dirname(__FILE__)."/bookmark.ini")) {
169       $bkm = (preg_split("/(\s|[\r\n])+/",@file_get_contents(dirname(__FILE__)."/bookmark.ini")));
170       $bkk = $bkv = array();
171       while (list($bkk[],$bkv[]) = array_splice($bkm,0,2)){}
172       $this->bookmarks = array_merge(array_filter(array_combine($bkk,$bkv)),$this->bookmarks);
173    }
174
175    $be = explode(",",$this->getConf('bookmarks'));
176
177    $ip = dirname(__FILE__)."/img/";
178    $iu = DOKU_URL."lib/plugins/bookmarkme/img/";
179    $title = p_get_first_heading($ID);
180    if (!$title) $title = $ID;
181
182    $pml = wl($ID,'',true);
183
184    $html = array('<ul class="bookmarkme">');
185
186    /*
187    *  show header only if allowed in config
188    */
189    if ($this->getConf('show_header')) $html[] = '<li class="head">'.$this->getLang('bookmark').'</li>';
190
191    $book_lng = htmlspecialchars($this->getLang('bookmark_title'));
192
193//    ksort($this->bookmarks);
194    foreach ($this->bookmarks as $k=>$v) {
195        $i = strtolower( str_replace( array("."," ")
196                                     ,array("" ,"_")
197                        ,$k)).".gif";
198        if (in_array($k,$be) && file_exists($ip.$i)) {
199            list($w,$h) = @getimagesize($ip.$i);
200            $v = str_replace( array('%title%'        ,'%permalink%')
201                             ,array(urlencode($title),urlencode($pml))
202                             ,$v);
203            $html[] = str_replace( "%title%"
204                                  ,str_replace( array("%where%", "%what%")
205                                               ,array($k       , htmlspecialchars($title))
206                                               ,$book_lng
207                                              )
208                                  ,"<li><a href=\"$v\" ".(preg_match("#^javascript:#",$v)?"":"target=\"_blank\"")."><img src=\"$iu$i\" width=\"$w\" height=\"$h\" alt=\"%title%\" title=\"%title%\" /></a></li>"
209                                 );
210        }
211//        if ($_GET['debug']) var_dump($html);
212    }
213    $html[] = '</ul>';
214    if (sizeof($html)>2) echo join("\n",$html);
215  }
216}
217