Jeff PHP framework  0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
functions.php
Go to the documentation of this file.
00001 <?php
00025 function __($id) {
00026         
00027         $language = isset($_SESSION['lng']) ? $_SESSION['lng'] : 'english';
00028 
00029         if(is_readable(ABS_THEMES.DS.'default'.DS.'languages'.DS.$language.'.php'))
00030                 $lng = include(ABS_THEMES.DS.'default'.DS.'languages'.DS.$language.'.php');
00031         else $lng = array();
00032 
00033         // registry singleton
00034         $registry = registry::instance();
00035         if(isset($registry->theme)) {
00036                 $theme = $registry->theme;
00037                 if(get_class($theme)!= 'defaultTheme')
00038                         if(is_readable($theme->path().DS.'languages'.DS.$language.'.php'))
00039                                 $lng = array_merge($lng, include($theme->path().DS.'languages'.DS.$language.'.php'));
00040         }
00041 
00042         return isset($lng[$id]) ? $lng[$id] : $id;
00043 
00044 };
00045 
00060 function anchor($link, $text, $opts=null) {
00061 
00062         $buffer = "<a href=\"$link\"";
00063         $buffer .= gOpt($opts, 'over') ? " onmouseover=\"".gOpt($opts, 'over')."\"":"";
00064         $buffer .= gOpt($opts, 'out') ? " onmouseout=\"".gOpt($opts, 'out')."\"":"";
00065         $buffer .= gOpt($opts, 'class') ? " class=\"".gOpt($opts, 'class')."\"":"";
00066         $buffer .= gOpt($opts, 'title') ? " title=\"".gOpt($opts, 'title')."\"":"";
00067         $buffer .= gOpt($opts, 'target') ? " target=\"".gOpt($opts, 'target')."\"":"";
00068         $buffer .= ">";
00069         $buffer .= $text;
00070         $buffer .= "</a>";
00071 
00072         return $buffer;
00073 }  
00074 
00087 function layerWindow($title, $url, $text, $opts=null) {
00088 
00089         return "<span class=\"link\" onclick=\"".layerWindowCall($title, $url, $opts)."\">$text</span>";
00090 }
00091 
00105 function layerWindowCall($title, $url, $opts=null) {
00106 
00107         $width = gOpt($opts, 'width', 800);
00108         $height = gOpt($opts, 'height', null);
00109         $bodyId = gOpt($opts, 'bodyId', 'bid');
00110         $reloadZindex = gOpt($opts, 'reloadZindex', false) ? "true" : "false";
00111 
00112         $height_opt = $height ? " 'height':$height," : '';
00113 
00114         $onclick = "window.myWin = new layerWindow({'title':'$title', 'url':'$url', 'bodyId':'$bodyId', 'width':$width,$height_opt 'destroyOnClose':true, reloadZindex: $reloadZindex, closeButtonUrl: '".ROOT."/img/icons/ico_close.gif', 'overlay':true});window.myWin.display();";
00115 
00116         return $onclick;
00117 
00118 }
00119 
00131 function tooltip($label, $title, $text, $opts=null) {
00132         $class = gOpt($opts, 'class', '');
00133         return "<span class=\"$class tooltip\" title=\"$title::$text\">$label</span>";
00134 }
00135 
00141 function clearFloat() {
00142         return "<div class=\"clear\"></div>";
00143 }
00144 
00151 function relativePath($abspath) {
00152 
00153         $path = ROOT.preg_replace("#".preg_quote(ABS_ROOT)."#", "", $abspath);
00154 
00155         if(OS=='win') return preg_replace("#".preg_quote("\\")."#", "/", $path);
00156 
00157         return $path;
00158 
00159 }
00160 
00171 function gOpt($opts, $name, $dft=null) {
00172 
00173         return isset($opts[$name]) ? $opts[$name] : $dft;
00174 
00175 }
00176 
00186 function floatcomp($a, $comp, $b, $decimals=2) {
00187         $res = bccomp($a,$b,$decimals); // php function for comparing floating point numbers with a specified level of precision
00188         switch ($comp) {
00189                 case ">":
00190                         return ($res==1);
00191                 case ">=":
00192                         return ($res==1 || $res==0);
00193                 case "<":
00194                         return ($res==-1);
00195                 case "<=":
00196                         return ($res==-1 || $res==0);
00197                 default:
00198                 case "==":
00199                         return ($res==0);
00200         }
00201 }
00202 
00211 function chargeEditor($selector) {
00212 
00213         $registry = registry::instance();
00214 
00215         $stylesheets = '';
00216         foreach($registry->css as $css) 
00217                 $stylesheets .= $css.";";
00218         $stylesheets .= ROOT."/css/dojo_frame.css";
00219 
00220         $registry->addJs("https://ajax.googleapis.com/ajax/libs/dojo/1.6.0/dojo/dojo.xd.js");
00221         $registry->addJs(ROOT."/lib/js/dojo.js");
00222         $registry->addCss(ROOT."/css/dojo.css");
00223 
00224 
00225         $buffer = "<script>";
00226         $buffer .= "var dojo_textareas = [];";
00227         $buffer .= "dojo.ready(function(){
00228                         var textareas = dojo.query(\"$selector\");
00229                         if(textareas && textareas.length){
00230                                 dojo.addClass(dojo.body(), \"claro\");
00231                                 for(var i=0; i<textareas.length; i++) {
00232                                         var textarea = textareas[i];
00233                                         var key = $(textarea).getParents('form')[0].get('name')+'_'+$(textarea).get('id');
00234                                         dojo_textareas[key] = new dijit.Editor({
00235                                         styleSheets: \"$stylesheets\",
00236                                         plugins: [
00237                                                 \"collapsibletoolbar\",
00238                                                 \"fullscreen\", \"viewsource\", \"|\",
00239                                                 \"undo\", \"redo\", \"|\",
00240                                                 \"cut\", \"copy\", \"paste\", \"|\",
00241                                                 \"bold\", \"italic\", \"underline\", \"strikethrough\", \"|\",
00242                                                 \"insertOrderedList\", \"insertUnorderedList\", \"indent\", \"outdent\", \"||\",
00243                                                 \"formatBlock\", \"fontName\", \"fontSize\", \"||\",
00244                                                 \"findreplace\", \"insertEntity\", \"blockquote\", \"|\",
00245                                                 \"createLink\", \"insertImage\", \"insertanchor\", \"|\",
00246                                                 \"foreColor\", \"hiliteColor\", \"|\",
00247                                                 \"showblocknodes\", \"pastefromword\",
00248                                                 // headless plugins
00249                                                 \"normalizeindentoutdent\", \"prettyprint\",
00250                                                 \"autourllink\", \"dijit._editor.plugins.EnterKeyHandling\"
00251                                         ]
00252                                         }, textareas[i]);
00253                                 }
00254                         }
00255                 });";
00256         $buffer .= "</script>";
00257 
00258         return $buffer;
00259 
00260 }
00261 
00277 function share($social, $url, $title=null, $description=null) {
00278 
00279         $registry = registry::instance();
00280 
00281         $ss = new siteSettings($registry);
00282         $source = $ss->app_title;
00283 
00284         if($social==="all") $social = array("facebook", "twitter", "linkedin", "digg", "googleplus");
00285 
00286         $items = array();
00287         foreach($social as $s) {
00288                 if($s=='facebook') {
00289                         $items[] = "<a name=\"fb_share\" type=\"button_count\" share_url=\"$url\" href=\"http://www.facebook.com/sharer.php\">Share</a><script src=\"http://static.ak.fbcdn.net/connect.php/js/FB.Share\" type=\"text/javascript\"></script>";  
00290                 }
00291                 elseif($s=='twitter') {
00292                         $items[] = "<a href=\"http://twitter.com/home?status=Currentlyreading ".urlencode($url)."\" title=\""._("condividi su Twitter")."\"><img src=\"".ROOT."/img/share_twitter.jpg\" alt=\"Share on Twitter\"></a>";
00293                 }
00294                 elseif($s=='linkedin') {
00295                         $items[] = "<a href=\"http://www.linkedin.com/shareArticle?mini=true&url=".urlencode($url)."&title=".urlencode($title)."&source=".urlencode($source)."\"><img src=\"".ROOT."/img/share_linkedin.jpg\" alt=\"Share on LinkedIn\"></a>";
00296                 }
00297                 elseif($s=='googleplus') {
00298                         $items[] = "<g:plusone size=\"small\" width=\"90\"></g:plusone><script type=\"text/javascript\">(function() { var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; po.src = 'https://apis.google.com/js/plusone.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); })();</script>";
00299                 }
00300                 elseif($s=='digg') {
00301                         $items[] = "<a href=\"http://digg.com/submit?phase=2&amp;url=".$url."&amp;title=".$title."\"><img src=\"".ROOT."/img/share_digg.png\" alt=\"Share on LinkedIn\"></a>";
00302                 }
00303         }
00304 
00305         $buffer = implode(" ", $items);
00306 
00307         return "<div class=\"share\">".$buffer."</div>";
00308 }
00309 
00324 function cutHtmlText($html, $length, $ending, $strip_tags, $cut_words, $cut_images, $opts = null) {
00325         
00326         /*
00327                 regular expressions to intercept tags
00328         */
00329         $opened_tag = "<\w+\s*([^>]*[^\/>]){0,1}>";  // i.e. <p> <b> ...
00330         $closed_tag = "<\/\w+\s*[^>]*>";                                // i.e. </p> </b> ...
00331         $openended_tag = "<\w+\s*[^>]*\/>";                     // i.e. <br/> <img /> ...       
00332         $cutten_tag = "<\w+\s*[^>]*$";                          // i.e. <img src="" 
00333         $reg_expr_img = "/<img\s*[^>]*\/>/is";      
00334         /* 
00335                 Check: if text is shorter than length (tags excluded) return $html
00336                 with or without tags
00337         */
00338         $reg_expr = "/$opened_tag|$closed_tag|$openended_tag/is";
00339         $text = preg_replace($reg_expr, '', $html);
00340         if (strlen($text) <= $length) {
00341                 if(!$strip_tags) {
00342                         if($cut_images) {
00343                                 $html = preg_replace($reg_expr_img, "", $html);
00344                         }
00345                         return $html;
00346                 }
00347                 else return $text;
00348         }
00349         
00350         /*
00351                 else if $strip_tags is false...
00352         */
00353         if(!$strip_tags) {
00354         
00355                 // splits all html-tags to scanable lines
00356                 $reg_expr = "/(<\/?\w+\s*[^>]*\/?>)?([^<>]*)/is";
00357                 preg_match_all($reg_expr, $html, $lines, PREG_SET_ORDER);
00358                 /*
00359                         now 
00360                         - in $lines[$i] are listed all the matches with the regular expression:
00361                           $lines[0]: first match
00362                           $lines[1]: second match ...
00363                           
00364                         - $lines[$i][0] contains the wide matching string
00365                         - $lines[$i][1] contains the matching with (<\/?\w+\s*[^>]*\/?>), that is opened or    
00366                           closed ore openclosed tags
00367                         - $lines[$i][2]contains the matching with ([^<>]*) that is the text inside the tag
00368                           or between a tag and another
00369                 */
00370                 $total_length = 0;
00371                 $tags_opened = array();
00372                 $partial_html = '';
00373                 
00374                 foreach ($lines as $line_matchings) {
00375                 /*
00376                         $line_matchings[1] contains tags
00377                         $line_matchings[2] contains text contained in tags
00378                         
00379                         Check: what kind of tag is? open, close, openclose?
00380                 */
00381                         if (!empty($line_matchings[1])) {
00382                                 $strip_this_tag = 0;
00383                                 $reg_expr_oc = "/".$openended_tag."$/is";
00384                                 $reg_expr_o = "/<(\w+)\s*([^>]*[^\/>]){0,1}>$/is";
00385                                 $reg_expr_c = "/<\/(\w+)>$/is";
00386                                 // search img tags
00387                                 if(preg_match($reg_expr_img, $line_matchings[1]) && $cut_images) {
00388                         $strip_this_tag = 1;
00389                 }
00390                 // search openended tags
00391                 elseif (preg_match($reg_expr_oc, $line_matchings[1])) {
00392                         // nothing: doesn't encrease the count of characters
00393                         // and doesn't need a closure
00394                 }
00395                 // search opened tags
00396                 elseif(preg_match($reg_expr_o, $line_matchings[1], $tag_matchings)) {
00397                         // open tag
00398                         // add tag to the beginning of $open_tags list
00399                                         array_unshift($tags_opened, strtolower($tag_matchings[1]));
00400                 }
00401                 // search closed tags
00402                 elseif(preg_match($reg_expr_c, $line_matchings[1], $tag_matchings)) {
00403                         // close tag
00404                         // delete tag from $open_tags list (as it has been already closed)
00405                         $pos = array_search($tag_matchings[1], $tags_opened);
00406                                         if ($pos !== false) {
00407                                                 unset($tags_opened[$pos]);
00408                                         }
00409                 }
00410                 // add html-tag to $truncate'd text
00411                                 if(!$strip_this_tag) $partial_html .= $line_matchings[1];
00412                                 
00413                         }
00414                         /*
00415                                 Calculate the lenght of the text inside tags and replace considering html entities one size characters
00416                         */
00417                         $reg_exp_entities = '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i';
00418                         $content_length = strlen(preg_replace($reg_exp_entities, ' ', $line_matchings[2]));
00419                         
00420                         if ($total_length+$content_length> $length) {
00421                         
00422                                 $left = $length - $total_length;
00423                                 $entities_length = 0;
00424                                 
00425                                 // search for html entities (l'entities conta come un carattere, ma nell'html ne uccupa di più, quindi dobbiamo fare in modo di includere completament l'entities, cioè il suo codice e contarlo interamente come un singolo carattere: scaliamo uno da $left ed aggiungiamo $entities_length all alunghezza della substring)
00426                                 if(preg_match_all($reg_exp_entities, $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
00427                                         // calculate the real length of all entities in the legal range
00428                                         foreach ($entities[0] as $entity) {
00429                                                 if ($entity[1]+1-$entities_length <= $left) {
00430                                                         $left--;
00431                                                         $entities_length += strlen($entity[0]);
00432                                                 }
00433                                                 else {
00434                                                         // no more characters left
00435                                                         break;
00436                                                 }
00437                                         }
00438                                 }
00439                                 
00440                                 $partial_html .= substr($line_matchings[2], 0, $left+$entities_length);
00441                                 // maximum lenght is reached, so get off the loop
00442                                 break;
00443                                                         
00444                         }
00445                         else {
00446                                 $partial_html .= $line_matchings[2];
00447                                 $total_length += $content_length;
00448                         }
00449                         
00450                         // if the maximum length is reached, get off the loop
00451                         if($total_length>= $length) break;
00452 
00453                 }
00454         }
00455         else {
00456                 // onlly pure text
00457                 $partial_html = substr($text, 0, $length);
00458         }
00459         
00460         // if the words shouldn't be cut in the middle...
00461         if (!$cut_words) {
00462                 //search the last occurance of a space or an end tag
00463                 $spacepos = strrpos($partial_html, ' ');
00464                 $endtagpos = strrpos($partial_html, '>');
00465                 if(isset($spacepos) || isset($endtagpos)) {
00466                         //cut the text in this position
00467                         $cutpos = ($spacepos<$endtagpos)? ($endtagpos+1) : $spacepos;
00468                         $partial_html = substr($partial_html, 0, $cutpos);
00469                 }
00470         }
00471         
00472         if(gOpt($opts, 'endingPosition') == 'in')
00473                 $partial_html .= $ending;
00474 
00475         /*
00476                 close all opened tags (if strip tags is false)
00477         */
00478         if(!$strip_tags) 
00479                 // close all unclosed html tags
00480                 foreach ($tags_opened as $tag) 
00481                         $partial_html .= '</' . $tag . '>';
00482         
00483         // add the ending characters to the partial text
00484         if(gOpt($opts, 'endingPosition') != 'in')
00485                 $partial_html .= $ending;
00486    
00487         return $partial_html;   
00488 
00489 }
00490 
00491 ?>