root/branches/multiuser/ajax.php

Revision 1604, 18.5 kB (checked in by mdodoo, 2 years ago)

Sync with trunk up to (but not including changeset [1585]). I should *definitely* do this more often, because this was really annoying.

  • Property svn:eol-style set to native
  • Property svn:eolstyle set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2###############################################################################
3# Gregarius - A PHP based RSS aggregator.
4# Copyright (C) 2003 - 2006 Marco Bonetti
5#
6###############################################################################
7# This program is free software and open source software; you can redistribute
8# it and/or modify it under the terms of the GNU General Public License as
9# published by the Free Software Foundation; either version 2 of the License,
10# or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful, but WITHOUT
13# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15# more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  or visit
20# http://www.gnu.org/licenses/gpl.html
21#
22###############################################################################
23# E-mail:      mbonetti at gmail dot com
24# Web page:    http://gregarius.net/
25###############################################################################
26
27if (array_key_exists('js',$_GET)) {
28    require_once('core.php');
29    rss_bootstrap(false,'$Revision$',0);
30}
31
32require_once('init.php');
33
34function __exp__setState($id,$state) {
35        if (!hidePrivate()) {
36                rss_query('update '.getTable('item') . " set unread=$state where id=$id");     
37                rss_invalidate_cache();
38        }
39    $rs = rss_query('select unread from '.getTable('item') . " where id=$id");
40    list($unread) = rss_fetch_row($rs);
41    return "$id|$unread";
42}
43
44function __exp__getSideContent($what) {
45        ob_start();
46        switch ($what) {
47                case 'FeedList':
48                        $f = new FeedList(false);
49                        $f -> render();
50                        break;
51
52                case 'CatList':
53                        //rss_require('cls/taglist.php');
54                        //$v = new TagList('channel');
55                        rss_require('cls/categories.php');
56                        $v = new CatList();
57                        $v -> render();
58                        break;
59
60                case 'TagList':
61                        rss_require('cls/taglist.php');
62                        $t = new TagList('item');
63                        $t -> render();
64                        break;
65        }
66        $c = ob_get_contents();
67        ob_end_clean();
68        rss_invalidate_cache();
69        return ($what . "#@#" .$c);
70}
71
72function __exp__getFeedContent($cid) {
73
74       
75        ob_start();
76        rss_require('cls/items.php');
77       
78        $readItems = new ItemList();
79
80        $readItems -> populate(" i2u.flgunread=0 and i.cid= $cid", "", 0, 2, ITEM_SORT_HINT_READ);
81        $readItems -> setTitle(__('Recent items'));
82        $readItems -> setRenderOptions(IL_TITLE_NO_ESCAPE);
83        foreach ($readItems -> feeds[0] -> items as $item) {
84                $item -> render();
85        }
86        $c = ob_get_contents();
87       
88        ob_end_clean();
89        return "$cid|@|$c";
90}
91
92function __exp__rateItem($iid, $rt) {
93        list($rrt) = rss_fetch_row(rss_query("select rating from "
94            .getTable('rating') . " where iid = $iid"));
95
96        rss_query('delete from ' .getTable('rating') . ' where iid = ' . $iid);
97        if ($rt == $rrt) {
98                return ("$iid|0");
99        }
100        rss_query('insert into ' .getTable('rating') . "(iid,rating) values ($iid,$rt)");
101        if (rss_is_sql_error(RSS_SQL_ERROR_NO_ERROR)) {
102                return ("$iid|$rt");
103        }
104}
105
106/**
107 * this exported AJAX method is only here so that the plugin callback
108 * hook is asynchronous
109 */
110function __exp_itemRatedCB($iid,$rt) {
111        rss_plugin_hook("rss.plugins.rating.rated",array($iid,$rt));
112        return null;
113}
114
115
116
117$sajax_request_type = "POST";
118$sajax_debug_mode = 0;
119$sajax_remote_uri = getPath() . basename(__FILE__);
120
121// Non standard! One usually calls sajax_export() ...
122$sajax_export_list = array("__exp__submitTag","__exp__getSideContent","__exp__getFeedContent");
123
124// Plugins shall export ajax functions as well
125$sajax_export_list = rss_plugin_hook("rss.plugins.ajax.exports",$sajax_export_list);
126
127
128if (!hidePrivate()) {
129    $sajax_export_list[] = "__exp__setState";
130    $sajax_export_list[] = "__exp__rateItem";
131    $sajax_export_list[] = "__exp_itemRatedCB";
132}
133
134sajax_init();
135
136//////////////////////////////////////////////////////////////////////////////
137//////////////////////////////////////////////////////////////////////////////
138//////////////////////////////////////////////////////////////////////////////
139
140/* spit out the javascript for this bugger */
141if (array_key_exists('js',$_GET)) {
142
143    if (getConfig('rss.output.compression')) {
144            ob_start('ob_gzhandler');
145    } else {
146            ob_start();
147    }
148    header('Content-Type: text/javascript');
149    // The javascript output shall be cached
150    // The Etag was set at the start of this file.
151   
152    $js = sajax_get_javascript();
153    echo $js;
154
155    // and here is s'more javascript for field editing...
156    ?>
157
158/// End Sajax javscript
159/// From here on: Copyright (C) 2003 - 2006 Marco Bonetti, gregarius.net
160/// Released under GPL
161
162function miniloginform() {
163        span = document.getElementById('loginformcontainer');
164        if (span.style.display == 'none') {
165                span.style.display = 'block';
166        } else {
167                span.style.display = 'none';
168        }
169}
170
171function loginHandler() {
172        pw = document.getElementById('password');
173        username = document.getElementById('username');
174        var re = /[0-9a-f]{32}/ ;
175        if (! re.test(pw.value)) {
176                pw.value = hex_md5(pw.value + username.value);
177        }
178        return true;
179}
180
181function setTags(id,tagss) {
182  tags = tagss.split(' ');
183
184  var fld=document.getElementById("t" + id);
185  var html = "";
186  for (i=0;i<tags.length;i++) {
187     html = html + "<a href=\"<?php echo  getPath()
188     . (getConfig('rss.output.usemodrewrite')?'tag/':'tags.php?tag=')
189     ?>" + tags[i] + "\">" + tags[i] + "</a> ";
190  }
191  fld.innerHTML = html;
192
193  var aspan=document.getElementById("ta" + id);
194  aspan.innerHTML = "<a href=\"#\" onclick=\"_et(" +id +"); return false;\"><?php echo  __('edit') ?></a>";
195}
196
197function submit_tag_cb(ret) {
198    data= ret.split(',');
199    //replace(/[^a-zA-Z0-9\ _\.,]/gi,"")
200    id=data[0];
201    tags=data[1];
202    setTags(id,tags);
203}
204
205
206function submit_tag(id,tags) {
207    x___exp__submitTag(id, tags, submit_tag_cb);
208}
209
210function _et(id) {
211   var actionSpan = document.getElementById("ta" + id);
212    var toggle = actionSpan.firstChild;
213    if (toggle.innerHTML == "<?php echo  __('submit') ?>") {
214        var fld = document.getElementById("tfield" + id);
215      toggle.innerHTML="<?php echo  __('...') ?>";
216        submit_tag(id,fld.value);
217    } else if (toggle.innerHTML == "<?php echo __('edit') ?>") {
218       var isIE=document.all?true:false;
219       // the tag container
220       var tc=document.getElementById("t"+id);
221        var tags = tc.innerHTML.replace(/<\/?a[^>]*>(\ $)?/gi,"");
222        //.replace(<?php echo ALLOWED_TAGS_REGEXP ?>gi,"");
223        // submit link
224        toggle.innerHTML="<?php echo  __('submit') ?>";
225        // cancel link
226        cancel = document.createElement("a");
227        cancel.style.margin="0 0 0 0.5em";
228        cancel.innerHTML = "<?php echo  __('cancel') ?>";
229        cancel.setAttribute("href","#");
230        if (isIE) {
231            // the IE sucky way
232            cancel.onclick = function() { setTags(id,tags); return false;}
233       } else {
234          // the proper DOM way
235            cancel.setAttribute("onclick","setTags("+id+",'"+tags+"'); return false;");
236       }
237        actionSpan.appendChild(cancel);
238
239                  <?php rss_plugin_hook("rss.plugins.ajax.extrajs.edittag",null); ?>
240                 
241        tc.innerHTML = "<input class=\"tagedit\" id=\"tfield"
242         +id+"\" type=\"text\" value=\"" + tags + "\" />";
243
244        // set the caret to the end of the field for bloody IE
245        var control = tc.firstChild;
246        control.focus();
247        if (control.createTextRange) {
248            var range = control.createTextRange();
249        range.collapse(false);
250            range.select();
251        } else if (control.setSelectionRange) {
252            control.focus();
253            var length = control.value.length;
254            control.setSelectionRange(length, length);
255        }
256    }
257    return false;
258}
259
260
261cs = getCookie('side');
262if (!cs) {
263        cs = '1';
264}
265document.currentSide = cs;
266document.currentSideCache = new Array();
267for (i=0;i<3;i++) {
268        document.currentSideCache[i] = null;
269}
270
271
272function _side(what) {
273        if (document.currentSide == what) {
274                return 0;
275        }
276       
277        document.currentSideCache[document.currentSide] = document.getElementById('channels').innerHTML;
278        if ((content = document.currentSideCache[what]) != null) {     
279                _setSideContent_cb( what + "#@#" + content );
280        } else {
281                x___exp__getSideContent(what, _setSideContent_cb);
282        }
283}
284
285function _setSideContent_cb(ret) {
286        data=ret.split('#@#');
287        kind=data[0];
288        content=data[1];
289        c = document.getElementById('channels').innerHTML;
290        ul = document.getElementById('sidemenu');
291        lis= ul.getElementsByTagName('li');
292        for (j=0;j<lis.length;j++) {
293                var li = lis[j];
294                if (li.id == ('sidemenu'+kind)) {
295                        li.className = "active";
296                } else {
297                        li.className = "";
298                }
299        }
300
301        document.currentSide = kind;
302        document.currentSideCache[kind] = content;
303        document.getElementById('channels').innerHTML = content;
304    setRootCookie("side",kind);
305}
306
307
308
309// feed collapsing
310function _ftgl(cid) {
311        cids = getCookie('collapsedfeeds');
312        if (cids) {
313                cidsArr = cids.split(":");
314        } else {
315                cidsArr = new Array();
316        }
317       
318        var ul = document.getElementById('f'+cid);
319        var img = document.getElementById('cli'+cid);
320        var collapsed  = (img.parentNode.className == 'expand');
321       
322        if (collapsed) {
323                img.src = img.src.replace(/plus/g,'minus');
324                img.parentNode.className = "collapse";
325                img.parentNode.parentNode.className="";
326                for(i=0;i<cidsArr.length;i++) {
327                        if (cidsArr[i] == cid) {
328                                cidsArr[i] = -1;
329                        }
330                }
331                if (ul.style.display == "none") {
332                        ul.style.display = "block";
333                } else {
334                        ul.innerHTML = "...";
335                        x___exp__getFeedContent(cid, get_feed_content_cb);
336                }
337        } else {
338                img.src = img.src.replace(/minus/g,'plus');
339                img.parentNode.className = "expand";
340                img.parentNode.parentNode.className="collapsed";
341                ul.style.display = "none";
342                cidsArr[cidsArr.length]=cid;
343        }
344       
345        cidsArr.sort();
346        cidsCookie = "";
347        for (i=0;i<cidsArr.length;i++) {
348                if (cidsArr[i] > 0) {
349                        cidsCookie = cidsCookie + cidsArr[i];
350                        if (i<cidsArr.length -1) {
351                                cidsCookie += ":";
352                        }
353                }
354        }
355        setRootCookie('collapsedfeeds',cidsCookie);
356}
357
358
359function get_feed_content_cb(data) {
360        d=data.split('|@|');
361        cid=d[0];
362        html=d[1];
363        if (cid) {
364                ul = document.getElementById('f'+cid);
365                if (ul) {
366                        ul.innerHTML = html;
367                        ul.style.display = "block";
368                }
369        }
370}
371
372function setRootCookie(cookieName,cookieValue) {
373  setCookie(cookieName,cookieValue,"<?php echo getPath(); ?>");
374}       
375function setRootSessionCookie(cookieName,cookieValue) {
376  setSessionCookie(cookieName,cookieValue,"<?php echo getPath(); ?>"); 
377}       
378// src: http://www.javascripter.net/faq/settinga.htm
379function setCookie(cookieName,cookieValue,path) {
380    //alert(cookieValue);
381    var today = new Date();
382    var expire = new Date();
383    // 1 year
384    expire.setTime(today.getTime() + 31536000000);
385    document.cookie = cookieName+"="+escape(cookieValue)
386        + "; expires="+expire.toGMTString()
387        + "; path="+path;
388}
389
390function setSessionCookie(cookieName,cookieValue,path) {
391    document.cookie = cookieName+"="+escape(cookieValue)
392        + "; path="+path;   
393}
394
395function getCookie(cookieName) {
396    var theCookie=""+document.cookie;
397    var ind=theCookie.indexOf(cookieName);
398    if (ind==-1 || cookieName=="") return "";
399    var ind1=theCookie.indexOf(';',ind);
400    if (ind1==-1) ind1=theCookie.length;
401    return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
402}
403
404<?php rss_plugin_hook("rss.plugins.ajax.extrajs.public",null); ?>
405
406<?php if (! hidePrivate()) { ?>
407
408<?php rss_plugin_hook("rss.plugins.ajax.extrajs.private",null); ?>
409
410document.states = new Array();
411document.prevState = new Array();
412
413
414function setState(id,state) {
415    x___exp__setState(id,state,setState_cb);
416}
417
418function setItemClass(id,cls) {
419    if ((a=document.getElementById('sa'+id)) && (li=a.parentNode)) {
420        li.className=cls;
421    }
422}
423
424function fade(id,amount) {
425    if ((a=document.getElementById('sa'+id)) && (li=a.parentNode)) {
426        li.style.opacity=amount;
427        li.style.height = amount*li.style.height;
428        if (amount <= 0) {
429            li.parentNode.removeChild(li);
430        }
431    }
432}
433
434function setItemHide(id, redirect){
435    if ((a=document.getElementById('sa'+id)) && (li=a.parentNode)) {
436       ul = li.parentNode;
437       
438       if (false) {
439        // do funky tuff
440        for (i=5;i>=0;i--) {
441            window.setTimeout('fade('+id+','+(2*i)/10+')', 100*(5-i));
442        }
443       } else {
444         trash = ul.removeChild(li);
445       }
446       
447       // remove parent elements (heading, ul) if all the children are gone
448       if (!redirect && (ul.getElementsByTagName('li').length == 0)) {
449        pn = ul.parentNode;
450       
451        var ps = ul.previousSibling;
452        while ( ps = ps.previousSibling ) {
453            if ("H3" == ps.nodeName.toUpperCase()) {
454                trash=ps.parentNode.removeChild(ps);
455                break;
456            }
457        }
458        trash = pn.removeChild(ul);
459       }
460       
461       if (redirect) {
462        if (t = document.getElementById("_markReadButton")) {
463            // Maybe we should fix the array of ids also...
464            self.setTimeout('t.click()', 1000);
465        }else{
466             self.setTimeout('setRedirect()', 1000);
467        }
468       }
469    }
470}
471
472function setRedirect() {
473        document.location = '<?php echo  getPath() ?>';
474}
475
476function setState_cb(ret) {
477    data=ret.replace(/[^0-9\|]/gi,"").split('|');
478    id=data[0];
479    s=data[1];
480    document.states[id]=s;
481    _ces(id);
482}
483
484function _es(id, state, cid) {
485        if (document.prevState[id] != null) {
486           // if we click the edit icon while editing cancel the edit
487           _ces(id);
488           document.prevState[id] = null;
489           return;
490        }
491         
492    if (document.states[id]) {
493        tmpState =document.states[id];
494    } else {
495        tmpState =state;
496    }
497    document.prevState[id] = tmpState;
498        if (div = document.getElementById('sad'+id)) {
499
500
501        onOk = '<?php echo  rss_plugin_hook("rss.plugins.ajax.admindlg.onok",""); ?>'.replace(/_ID_/g,id);
502        onCancel = '<?php echo  rss_plugin_hook("rss.plugins.ajax.admindlg.oncancel",""); ?>'.replace(/_ID_/g,id);
503        extraCode = '<?php echo  rss_plugin_hook("rss.plugins.ajax.admindlg",""); ?>'.replace(/_ID_/g,id);
504       
505  onOk += '_ses('+id+','+cid+'); return false;';
506    if (!onCancel) {
507        onCancel = '_ces('+id+'); return false;';
508    }
509        if (!extraCode) {
510           extraCode = '';
511        }
512        div.innerHTML = ''
513                + '<form class="sf" id="sf"'+id+'" action="#" method="post">'
514                + '<p><input type="checkbox" id="sf' + id + 'u" value="1"'
515                + (tmpState & <?php echo  RSS_MODE_UNREAD_STATE ?> ?' checked="checked"':'')
516                + ' />'
517                + '<label for="sf' + id + 'u"><?php echo  __("Unread (Set this item\'s read/unread state)") ?></label></p>'
518                + '<p><input type="checkbox" id="sf' + id + 's" value="1"'
519                + (tmpState & <?php echo  RSS_MODE_STICKY_STATE ?> ?' checked="checked"':'')
520                + ' />'
521                + '<label for="sf' + id + 's"><?php echo  __("Sticky (Won\'t be deleted when you prune items)") ?></label></p>'
522                + '<p><input type="checkbox" id="sf' + id + 'p" value="1"'
523                + (tmpState & <?php echo  RSS_MODE_PRIVATE_STATE ?> ?' checked="checked"':'')
524                + ' />'
525                + '<label for="sf' + id + 'p"><?php echo  __("Private (Only administrators can see private items)") ?></label></p>'
526                + '<p><input type="checkbox" id="sf' + id + 'f" value="1"'
527                + (tmpState & <?php echo  RSS_MODE_FLAG_STATE ?> ?' checked="checked"':'')
528                + ' />'
529    + '<label for="sf' + id + 'f"><?php echo  __("Flag (Flags an item for later reading)") ?></label></p>'
530                + extraCode
531                + '<p class="sbm">'
532                + '<a id="ess'+id+'ok" href="#" onclick="'+onOk+'"><?php echo  __("OK") ?></a>'
533                + '<a href="#" onclick="'+onCancel+'"><?php echo  __("Cancel") ?></a></p>'
534                + '</form>';
535
536    div.className = 'ief';
537    div.style.display = "block";
538   }
539}
540
541function _ces(id) {
542        if (div = document.getElementById('sad'+id)) {
543                div.className = '';
544                div.innerHTML = '';
545                div.style.display='none';
546        }
547       
548        if (sa = document.getElementById('sa' + id)) {
549        sa.focus();
550    }
551
552}
553
554function _ses(id,cid) {
555    s = 0;
556    if ((sfu = document.getElementById('sf'+id+'u')) && sfu.checked) {
557      s += <?php echo  RSS_MODE_UNREAD_STATE ?>;
558    }
559    if ((sfs = document.getElementById('sf'+id+'s')) && sfs.checked) {
560      s += <?php echo  RSS_MODE_STICKY_STATE ?>;
561    }
562    if ((sfp = document.getElementById('sf'+id+'p')) && sfp.checked) {
563      s += <?php echo  RSS_MODE_PRIVATE_STATE ?>;
564    }
565    if ((sff = document.getElementById('sf'+id+'f')) && sff.checked) {
566      s += <?php echo  RSS_MODE_FLAG_STATE ?>;
567    }
568   
569   
570
571    if ((p=document.prevState[id]) != s) {
572        if ((s & <?php echo  RSS_MODE_UNREAD_STATE ?>) != (p & <?php echo  RSS_MODE_UNREAD_STATE ?>)) {
573            if (s & <?php echo  RSS_MODE_UNREAD_STATE ?>) {
574                setItemClass(id,'item unread');
575                c=unreadCnt(1,cid);
576            } else {
577                c = unreadCnt(-1,cid);
578                                if ((sel = document.getElementById('<?php echo  SHOW_WHAT ?>')) &&
579                                    sel.options[sel.selectedIndex].value == <?php echo  SHOW_UNREAD_ONLY ?>) {
580                        setItemHide(id, (c==0));
581                                } else{
582                                        setItemClass(id, 'item even');                               
583                                }
584            }
585        }
586        if (btn=document.getElementById('ess'+id+'ok')) {
587            btn.innerHTML = '...';
588            btn.disabled = true;
589        }
590        document.prevState[id] = null;
591        setState(id,s);
592    } else {
593      // state didn't change!
594      _ces(id);
595    }
596}
597
598function unreadCnt(d,channel) {
599        c = null;
600        channels = document.getElementsByTagName("strong");
601        for(i=0;i<channels.length;i++){
602                if((channels[i].id == 'cid'+channel) || (channels[i].id == 'ucnt') || (channels[i].id == 'fucnt')){
603                        if (e = channels[i].innerHTML.replace(/[^0-9]+/g,"")) {
604                                e = d+eval(e);
605                                channels[i].innerHTML = channels[i].innerHTML.replace(/[0-9]+/g,e);
606                                if(channels[i].id == 'ucnt'){ c = e; }
607                        }
608                }
609        }
610        return c;
611}
612
613function _rt(id,rt) {
614         x___exp__rateItem(id,rt,rateItem_cb);
615}
616
617function rateItem_cb(ret) {
618        data=ret.replace(/[^0-9\|]/gi,"").split('|');
619        id = data[0];
620        rt = data[1];
621        if (id && rt) {
622               
623                ul = document.getElementById("rr" + id);
624                lis = ul.getElementsByTagName('li');
625                for (i=0;i<lis.length;i++) {
626                        var li = lis[i];
627                        if ((i+1) == rt) {
628                                li.className = "current";
629                        } else {
630             li.className = "";
631                        }
632                }
633                x___exp_itemRatedCB(id,rt,itemRatedCB_cb);
634        }
635}
636function itemRatedCB_cb(data) {}
637
638<?php }
639
640flush();
641exit();
642   
643//////////////////////////////////////////////////////////////////////////////
644//////////////////////////////////////////////////////////////////////////////
645
646} elseif(array_key_exists('rs',$_REQUEST)) {
647    // this one handles the xmlhttprequest call from the above javascript
648    sajax_handle_client_request();
649    exit();
650}
651
652//////////////////////////////////////////////////////////////////////////////
653//////////////////////////////////////////////////////////////////////////////
654?>
Note: See TracBrowser for help on using the browser.