root/trunk/scripts/general.func.php

Revision 1736, 11.5 KB (checked in by jeena, 4 months ago)

changed PHP_SELF to SCRIPT_NAME

Line 
1<?php
2// get weblog link
3function blog($date, $url, $section = 'weblog') {
4        if($section == 'weblog' OR $section == 'comment') {
5                $y = date("Y", $date);
6                $m = date("m", $date);
7                if(JLOG_CLEAN_URL === true) $permalink = JLOG_PATH."/".$y."/".$m."/".$url;
8                else $permalink = JLOG_PATH."/log.php?y=".$y."&amp;m=".$m."&amp;url=".$url;
9        }
10        else {
11                if(JLOG_CLEAN_URL === true) $permalink = JLOG_PATH."/".$url;
12                else $permalink = JLOG_PATH."/page.php?url=".$url;
13        }
14
15   ### Plugin Hook
16   global $plugins;
17   $permalink = $plugins->callHook('permalink', $permalink, $date, $url, $section);
18
19   return $permalink;
20}
21
22function archive() {
23    if(JLOG_CLEAN_URL === true) return JLOG_PATH."/archive";
24    else return JLOG_PATH."/archive.php";
25}
26
27// get year links
28class Year_Links {
29
30        function Year_Links($get, $start, $page, $l, $cat="") {
31         $date  = getdate();
32         $this->_now = $date['year'];
33         $this->_start = $start;
34         $this->_page = $page;
35         $this->_l = $l;
36         if(JLOG_CLEAN_URL === true) {
37                if($cat != "") {
38                        list($tmp, $cat) = explode("=", $cat);
39                        $this->cat = "/cat/".$cat;
40                 }
41         }
42         elseif($cat !== "") $this->cat = $cat."&amp;";
43         
44         if($get >= $this->_start OR $get <= $this->_now AND preg_match("[0-9]", $get)) $this->year = $get;
45         else $this->year = $this->_now;
46        }
47
48        function get_linklist() {
49
50         for($y = $this->_start; $y <= $this->_now; $y++) {
51          if($y != $this->_start) $years_links .= " | ";
52          if($y == $this->year) $years_links .= " <strong>".$y."</strong>";
53          else {
54           if(JLOG_CLEAN_URL === true) $years_links .= " <a href='".JLOG_PATH.$this->cat."/".$y."/'>".$y."</a>\n";
55           else $years_links .= " <a href='".$this->_page.(strpos($this->_page, '?') === false ? "?" : "&amp;").$this->cat."y=".$y."'>".$y."</a>\n";
56          }
57         }
58         
59         return $this->_l['content_choose_year'].$years_links; 
60        }
61       
62        function get_admin_linklist() {
63       
64         for($y = $this->_start; $y <= $this->_now; $y++) {
65          if($y != $this->_start) $years_links .= " | ";
66          if($y == $this->year) $years_links .= " <strong>".$y."</strong>";
67          else $years_links .= " <a href='".$this->_page.(strpos($this->_page, '?') === false ? "?" : "&amp;")."y=".$y."'>".$y."</a>\n";
68         }
69         
70         return $this->_l['content_choose_year'].$years_links; 
71
72        }
73       
74// get selected year
75        function get_selected_year() {
76         return $this->year;
77        }
78}
79
80// kill Magic Quotes
81function strip($_data) {
82  if (!get_magic_quotes_gpc()) return $_data;
83  else {
84        if (is_array($_data)) foreach($_data as $key => $val) $_data[$key] = strip($val);
85        else $_data = stripslashes($_data);
86        return $_data;
87  }
88}
89// escape input for mysql
90function escape_for_mysql($_data) {
91        if (is_array($_data)) foreach($_data as $key => $val) $_data[$key] = escape_for_mysql($val);
92        else $_data = mysql_escape_string($_data);
93        return $_data;
94}
95// htmlspecialchars a whole array
96function array_htmlspecialchars($_data) {
97        if (is_array($_data)) foreach($_data as $key => $val) $_data[$key] = array_htmlspecialchars($val);
98        else $_data = htmlspecialchars($_data, ENT_QUOTES);
99        return $_data;
100}
101// Fehler ausgeben
102function error_output($errors, $id = "", $headline = false) {
103global $l;
104        $error = "";
105        if($headline === false) $headline = $l["error"];
106        if(isset($errors)) {
107                if(strlen($headline) > 0) $error = "\n<h3 id='".$id."' class='error'>".$headline."</h3>";
108                $error .= "\n <ul class='error'>\n";
109                foreach($errors AS $f) $error .= "  <li>".$f."</li>\n";
110                $error .= " </ul>\n";
111   }
112 return $error;
113}
114
115// Aus der Datenbank löschen (wird beim Kommentarlöschen gebraucht)
116
117function trash($id, $table) {
118        $sql = "DELETE FROM ".$table." WHERE id = '".$id."' LIMIT 1";
119       
120        $trash = new Query($sql);
121   if($trash->error()) {
122        echo "<pre>\n";
123        echo $trash->getError();
124        echo "</pre>\n";
125        die();
126   }
127 return true;
128}
129
130// output a teaser
131function do_teaser($data, $cc, $pre = '<h2>', $post = '</h2>') {
132global $l, $bbcode, $categories, $plugins;
133
134 if(empty($data['date_url'])) $data['date_url'] = $data['date']; # fix for search.php
135
136 $output = "\n <div class='teaser'>\n";
137  if($data['teaserpic'] != "") {
138   list($img_width, $img_height, $img_type, $img_attr) = @getimagesize(JLOG_BASEPATH.'img'.DIRECTORY_SEPARATOR.'t_'.$data['teaserpic']);
139   $output .= "   <a title='".$l['content_permalink']."' href='".blog($data['date_url'], $data['url'], $data['section'])."'><img class='teaserpic' src='".JLOG_PATH."/img/t_".$data['teaserpic']."' style='width: ".$img_width."px; height: ".$img_height."px;' alt='' /></a>\n";
140  }
141  $output .= "  ".$pre."<a title='".$l['content_permalink']."' href='".blog($data['date_url'], $data['url'], $data['section'])."'>".htmlspecialchars($data['topic'], ENT_QUOTES)."</a>".$post."
142   <p class='date meta'>".$l['content_posted']." ".strftime(JLOG_DATE, $data['date']).$categories->output_assigned_links($data['id'])."</p>";
143  $output .= $bbcode->parse($data['teaser']);
144
145  $output .="   <p class='meta'><a title='".$l['content_more_title']."' href='".blog($data['date_url'], $data['url'], $data['section'])."'>".$l['content_more']."</a>";
146 
147  if($data['section'] == 'weblog') {
148          if(isset($cc[$data['id']]) AND $cc[$data['id']] != 0) $tmp_comments = " <a title='".$l['content_comments_title']."' href='".blog($data['date'], $data['url'])."#comments'>".$l['content_comments']." (".$cc[$data['id']].")</a>";
149          elseif($data['comments'] === '0') $tmp_comments = $l['comments_teaser_closed'];
150          else $tmp_comments = " <a href='".blog($data['date'], $data['url'])."#comments'>".$l['content_comment_plz']."</a>";
151          $output .= " | ".$tmp_comments;
152  }
153  $output .= "</p>\n </div>\n";
154
155   ### Plugin Hook
156   $output = $plugins->callHook('doTeaser', $output, $data, $cc, $pre, $post);
157
158 return $output;
159}
160
161function do_entry($data, $cc = NULL, $section = 'weblog', $pre = '<h2>', $post = '</h2>') {
162global $l, $bbcode, $categories, $plugins;
163
164 $output = "
165 <div class='mainitem'>
166  ".$pre."<a title='".$l['content_permalink']."' href='".blog($data['date'], $data['url'], $section)."'>".htmlspecialchars($data['topic'], ENT_QUOTES)."</a>".$post."\n";
167
168  if($data['teaserpic'] != "" AND $data['teaserpiconblog'] == 1) {
169   list($img_width, $img_height, $img_type, $img_attr) = @getimagesize(JLOG_BASEPATH.'img'.DIRECTORY_SEPARATOR.'t_'.$data['teaserpic']);
170   $output .= "<a title='".$l['content_permalink']."' href='".blog($data['date'], $data['url'], $section)."'><img class='teaserpic' src='".JLOG_PATH."/img/t_".$data['teaserpic']."' style='width: ".$img_width."px; height: ".$img_height."px;' alt='' /></a>";
171  }
172
173  if($section == 'weblog' OR ($cat = $categories->output_assigned_links($data['id'])) != "") {
174   $output .= " <p class='date meta'>";
175   if($section == 'weblog') $output .= $l['content_posted']." ".strftime(JLOG_DATE, $data['date']);
176   $output .= $categories->output_assigned_links($data['id'])."</p>";
177  }
178
179  $output .= $bbcode->parse($data['content']);
180  $path_parts = pathinfo($_SERVER['SCRIPT_NAME']);
181
182        if($data['section'] == 'weblog' AND $path_parts['basename'] != 'log.php') {
183         if(isset($cc[$data['id']]) AND $cc[$data['id']] != 0) $tmp_comments = " <a title='".$l['content_comments_title']."' href='".blog($data['date'], $data['url'])."#comments'>".$l['content_comments']." (".$cc[$data['id']].")</a>";
184         elseif($data['comments'] === '0') $tmp_comments = $l['comments_teaser_closed'];
185         else $tmp_comments = "<a href='".blog($data['date'], $data['url'])."#comments'>".$l['content_comment_plz']."</a>";
186    $output .="  <p class='meta'>".$tmp_comments."</p>";
187        }
188
189  if($section == 'weblog') $output .= ' <hr />';
190  $output .= " </div>\n";
191
192   ### Plugin Hook
193   $output = $plugins->callHook('doEntry', $output, $data, $cc, $section);
194
195 return $output;
196}
197
198function count_comments() {
199        // -- Kommentare zÀhlen
200         $sql = "SELECT reference, COUNT(*) as count FROM ".JLOG_DB_COMMENTS." WHERE type <> 'pingback' GROUP BY reference";
201         $comments = new Query($sql);
202             if($comments->error()) {
203                echo "<pre>\n";
204                echo $comments->getError();
205             echo "</pre>\n";
206             die();
207          }
208        // -- Anzahl der jeweiligen Kommentare
209         $com = array();
210         while($c = $comments->fetch()) $com[$c['reference']] = $c['count'];
211                                 
212                                 ### Plugin Hook
213                                 global $plugins;
214                           $com = $plugins->callHook('countComments', $com);
215                       
216         return $com;
217}
218
219if (!function_exists('is_a')) {
220    function is_a($object, $class)
221    {
222        if (!is_object($object)) {
223            return false;
224        }
225
226        if (get_class($object) == strtolower($class)) {
227            return true;
228        } else {
229            return is_subclass_of($object, $class);
230        }
231    }
232}
233
234if (!function_exists("stripos")) {
235  function stripos($str,$needle,$offset=0) {
236      return strpos( strtolower($str), strtolower($needle), $offset );
237  }
238}
239
240if(!function_exists('str_ireplace')){
241    function str_ireplace($search, $replace, $subject){
242        if(is_array($search)){
243            array_walk($search, create_function('&$pat, $key', '"/".preg_quote($pat, "/")."/i"'));
244        }
245        else{
246            $search = '/'.preg_quote($search, '/').'/i';
247        }
248        return preg_replace($search, $replace, $subject);
249    }
250}
251
252if ( !function_exists('file_put_contents') && !defined('FILE_APPEND') ) {
253        define('FILE_APPEND', 1);
254        function file_put_contents($n, $d, $flag = false) {
255            $mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';
256            $f = @fopen($n, $mode);
257            if ($f === false) {
258                return 0;
259            } else {
260                if (is_array($d)) $d = implode($d);
261                $bytes_written = fwrite($f, $d);
262                fclose($f);
263                return $bytes_written;
264            }
265        }
266}
267
268
269function my_serialize_cfg($arg) {   
270    if(is_string($arg)) return "'".preg_replace("/'/","\\'",$arg)."'";
271    elseif(is_integer($arg)) return (string)$arg;
272    elseif(is_float($arg)) return (string)$arg;
273    elseif(is_null($arg)) return 'NULL';
274    elseif(is_bool($arg)) {
275        if($arg) return 'true';
276        else return 'false';
277    }
278    elseif(is_array($arg)) {
279        $retval = 'Array(';
280        foreach($arg as $key => $value) {
281            $retval .= my_serialize_cfg($key).' => '.my_serialize_cfg($value).',';
282        }
283        $retval .= ')';
284        return $retval;
285    }
286    else die("unsupported type! ".gettype($arg));
287}
288
289class JLOG_Tags {
290  var $tree = array();
291
292  function JLOG_Tags($body) {
293      preg_match_all('/<jlog:([a-z]\w+)\s?([^>]*)\/?>(<\/(\1):(\2)>)?/ims', $body, $this->tree);
294  }
295
296      function getTag($tagname) {
297          if(($tagnr = array_search($tagname, $this->tree[1])) !== false) return $this->tree[0][$tagnr];
298          else return false;
299      }
300
301      function getAttributeValue($tagname, $attribute) {
302          $pattern = '/(?:^|\s)([a-z]\w+)(?:=)(?:(?:\'([^\']+)\')|(?:"([^"]*)")|([^\s,]+))/i';
303          if(($tagnr = array_search($tagname, $this->tree[1])) !== false) {
304              preg_match_all($pattern, $this->tree[2][ $tagnr ], $matches, PREG_SET_ORDER);
305              $a = count($matches);
306              for($i=0;$i<$a;$i++) {
307                  if($matches[$i][1] == $attribute) return $matches[$i][3];
308              }
309          }
310          else return;
311      }
312}
313?>
Note: See TracBrowser for help on using the browser.