linea21-utils
[ class tree: linea21-utils ] [ index: linea21-utils ] [ all elements ]

Source for file lib_common.php

Documentation is available at lib_common.php

  1. <?php
  2. /**
  3.  * @package linea21.utils
  4.  * @subpackage lib
  5.  * @author linea21 <info@linea21.com>
  6.  * @version $id SVN
  7.  * @access public
  8.  * @license http://opensource.org/licenses/gpl-3.0.html
  9.  */
  10.  
  11.  
  12. /**
  13.  * implode_with_keys()
  14.  * implode avec clefs associées renvoyées
  15.  * sous forme de chaîne de caractères
  16.  * @param string $glue 
  17.  * @param string $array 
  18.  * @return string 
  19.  */
  20. function implode_with_keys($glue, $array) {
  21.   $output = array();
  22.   foreach( $array as $key => $item )
  23.   $output[] = $key . "=" . $item;
  24.  
  25.   return implode($glue, $output);
  26. }
  27.  
  28. /**
  29.  * getHttpParameters()
  30.  * Renvoie les paramètres HTTP
  31.  * sous forme de chaîne de caractères
  32.  * @return string 
  33.  */
  34. function getHttpParameters($prefix = '?') {
  35.  
  36.   return $prefix. (string) implode_with_keys('&', $_REQUEST);
  37. }
  38.  
  39. /**
  40.  * strip_input()
  41.  * Remove PHP and HTML code
  42.  * @param string $str 
  43.  * @param string $exceptions 
  44.  * @return string 
  45.  */
  46. function strip_input($str, $exceptions = false) {
  47.  
  48.   if(defined('RICH_TEXT_EDITOR') && RICH_TEXT_EDITOR != (string) 0) {
  49.     return $str;
  50.   }
  51.   else {
  52.     if($exceptions)  {
  53.       return strip_tags($str, ALLOWABLE_TAGS);
  54.     }
  55.     else {
  56.       return strip_tags($str);
  57.     }
  58.   }
  59.  
  60. }
  61.  
  62. /**
  63.  * IncludeTimeline()
  64.  * Include SIMILE Timeline JS code
  65.  * if needed
  66.  * @return void 
  67.  */
  68. function IncludeTimeline() {
  69.   if(defined('TIMELINE_DISPLAY') && TIMELINE_DISPLAY == 1) {
  70.     if(isset($_REQUEST['rub']) && $_REQUEST['rub']=="project" && (!isset($_REQUEST['todo']) || $_REQUEST['todo'] == 'list')) {
  71.       echo '<script src="' .SITE_ROOT_URL. 'lib/js/timeline/timeline_ajax/simile-ajax-api.js" type="text/javascript"></script>'.END_LINE;
  72.       echo '<script type="text/javascript">
  73.              Timeline_urlPrefix=\'' .SITE_ROOT_URL. 'lib/js/timeline/timeline_js/\';
  74.              Timeline_parameters=\'bundle=true\';
  75.              $(function() {
  76.              $("#content").prepend(\'<div id="my-timeline" style="height: 250px; border-bottom: 1px solid #aaa"></div>\');
  77.              });
  78.              
  79.           </script>';
  80.       echo '<script src="' .SITE_ROOT_URL. 'lib/js/timeline/timeline_js/timeline-api.js" type="text/javascript"></script>'.END_LINE;
  81.       echo '<script type="text/javascript" src="../lib/js/timeline.js"></script>'. END_LINE;
  82.     }
  83.   }
  84. }
  85.  
  86. /**
  87.  * IncludeTextResizerJs()
  88.  * Include jQuery text-resizer plugin
  89.  * if no js wysiwyg are enabled
  90.  * @return void 
  91.  */
  92. function IncludeTextResizerJs() {
  93.  
  94.   if(!defined('RICH_TEXT_EDITOR') || RICH_TEXT_EDITOR == (string) 0 )
  95.   {
  96.     echo '<script type="text/javascript" src="../lib/js/jquery.textarearesizer.compressed.js"></script>';
  97.     echo '<script type="text/javascript">
  98.               $(document).ready(function() {
  99.                 $(\'textarea.largetextfield:not(.processed)\').TextAreaResizer();
  100.             })
  101.               </script>'.END_LINE;
  102.   }
  103. }
  104.  
  105. /**
  106.  * IncludePHPDebug()
  107.  * Include Js & css for PHP_DEBUG
  108.  * @return void 
  109.  */
  110. function IncludePHPDebug() {
  111.   if(defined('MOD_DEBUG') && MOD_DEBUG == true) {
  112.     echo '<script type="text/javascript" src="../lib/vendor/PHP_Debug/js/html_div.js"></script>'.END_LINE;
  113.     echo '<link type="text/css" rel="stylesheet" href="../lib/vendor/PHP_Debug/css/html_div.css" />'.END_LINE;
  114.   }
  115. }
  116. /**
  117.  * IncludeTooltipJs()
  118.  * Include jQuery tooltip plugin
  119.  * @param string $selector 
  120.  * @return void 
  121.  */
  122. function IncludeTooltipJs($selector) {
  123.   $str='';
  124.   if(is_string($selector)) {
  125.     $str .= '$("'.$selector.'").tooltip({ effect: "slide", opacity: 0.8});';
  126.   } else {
  127.     foreach($selector as $sel) {
  128.       $str .= '$("'.$sel.'").tooltip({ effect: "slide", opacity: 0.8});';
  129.     }
  130.   }
  131.   echo '<script type="text/javascript" src="../lib/js/jquery.tools.min.js"></script>'.END_LINE;
  132.   echo '<script type="text/javascript">
  133.           $(document).ready(function() {
  134.                   '.$str.'
  135.               })
  136.               </script>'.END_LINE;
  137. }
  138.  
  139. /**
  140.  * extRegex()
  141.  * Transform a list given in the form "jpg,png,gif"
  142.  * into "(jpg)|(png)|(gif)" format
  143.  * @return string 
  144.  */
  145. function extRegex($list) {
  146.   $a = explode(',', $list);
  147.   $b = array();
  148.   foreach($a as $el) {
  149.     array_push($b, '('.$el.')');
  150.   }
  151.   return implode('|', $b);
  152. }
  153.  
  154. /**
  155.  * IncludeFancyUploadJs()
  156.  * Include jQuery Fancy Upload
  157.  * @return void 
  158.  */
  159. function IncludeFancyUploadJs() {
  160.   $rub = isset($_REQUEST['rub']) ? $_REQUEST['rub'] : '';
  161.   if(ActiveItemKey($rub)) $rub = ActiveItemKey($rub);
  162.  
  163.   $a = $GLOBALS['fancyUpload_includes'];
  164.  
  165.   // security check here because uploadify is not able to handle cookie
  166.   if(in_array($rub, $a) && CookieWorkshopid($_REQUEST['id'])) {
  167.  
  168.     $fileDesc = str_replace(',', ', ', MEDIA_ALLOWED_EXT);
  169.     echo '<script type="text/javascript">
  170.               var rooturl = "' . SITE_ROOT_URL. '";
  171.               var upbutton = "' . _t('divers', 'up').'";
  172.               var delbutton = "'. _t('divers', 'delqueue').'";
  173.               var addfiles = "' . _t('upload', 'addfiles').'";
  174.               var succeed = "' . _t('upload', 'confirmed').'";
  175.               var error = "' . _t('upload', 'error').'";
  176.               var notallowed_msg = "'.sprintf(_t('upload', 'wrong_ext'), $fileDesc).'";
  177.               var size_msg = "'.sprintf(_t('upload','upload_size'), formatBytes(MEDIA_ALLOWED_SIZE)).'";
  178.               var maxsize = "' . MEDIA_ALLOWED_SIZE.'";
  179.               var regexpattern = "' . extRegex(MEDIA_ALLOWED_EXT).'";
  180.               var fileReplace = "' . _t('divers', 'replace_file').'";
  181.               var cbText = "'._t('workshop', 'share_files').'";
  182.           </script>
  183.            <link type="text/css" rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/base/jquery-ui.css" id="theme" />
  184.            <link type="text/css" rel="stylesheet" href="../lib/vendor/jQuery-File-Upload/jquery.fileupload-ui-l21.css" media="screen" />
  185.            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script>
  186.           <script type="text/javascript" src="../lib/vendor/jQuery-File-Upload/jquery.fileupload.js"></script>
  187.           <script type="text/javascript" src="../lib/vendor/jQuery-File-Upload/jquery.fileupload-ui.js"></script>
  188.           <script type="text/javascript" src="../lib/vendor/jQuery-File-Upload/public-settings.js"></script>'.END_LINE;
  189.   }
  190. }
  191.  
  192. /**
  193.  * IncludeTextCornerJs()
  194.  * Include jQuery text-corner plugin
  195.  * @return void 
  196.  */
  197. function IncludeTextCornerJs() {
  198.   echo '<script type="text/javascript" src="../lib/js/jquery.corner.js"></script>'.END_LINE;
  199.   echo '<script type="text/javascript" src="../lib/js/jquery.corner.config.js"></script>'.END_LINE;
  200. }
  201.  
  202. /**
  203.  * IncludeTreeJs()
  204.  * Include jQuery tree plugin
  205.  * @return void 
  206.  */
  207. function IncludeTreeJs() {
  208.   $str = '<ul id="cMenu" class="contextMenu"><li class="edit"><a href="#edit">'. _t('divers','modify').'<\/a><\/li><li class="delete separator"><a href="#delete">'. _t('divers','delete').'<\/a><\/li><\/ul>';
  209.   echo '<script type="text/javascript" src="../lib/js/jquery.contextMenu/jquery.contextMenu.js"></script>'.END_LINE;
  210.   echo '<link type="text/css" rel="stylesheet" href="../lib/js/jquery.contextMenu/jquery.contextMenu-l21.css" media="screen" />'.END_LINE;
  211.   echo '<script type="text/javascript" src="../lib/js/jquery.treeview/jquery.treeview.js"></script>'.END_LINE;
  212.   echo '<link type="text/css" rel="stylesheet" href="../lib/js/jquery.treeview/jquery.treeview.css" media="screen" />'.END_LINE;
  213.   echo '<script type="text/javascript">
  214.           $(document).ready(function(){
  215.               $("#tree").after(\''.$str.'\');
  216.             $("#tree").treeview({
  217.                 animated: "fast",
  218.                 collapsed: false,
  219.                 unique: false
  220.             });
  221.             
  222.             $("#tree ul li").contextMenu({
  223.                     menu: "cMenu"
  224.                 }, function(action, el, pos) {
  225.                     if(action=="edit") {
  226.                         if(typeof $(el).children("a.ico_mod").attr("href") == "undefined") {
  227.                             return 0;
  228.                         } else {
  229.                             document.location.href = $(el).children("a.ico_mod").attr("href");
  230.                         }
  231.                     }
  232.                     if(action=="delete") {
  233.                         if(typeof $(el).children("a.ico_sup").attr("href") == "undefined") {
  234.                             //$("cMenu").disableContextMenuItems("delete");
  235.                             //$("ul.contextMenu li.delete").attr("disabled", true);
  236.                             return 0;
  237.                         } else {
  238.                             document.location.href = $(el).children("a.ico_sup").attr("href");
  239.                         }
  240.                     }
  241.  
  242.                 });
  243.             $("#tree li a.ico_mod, #tree li a.ico_sup").css("display", "none");
  244.                 
  245.         });
  246.           </script>'.END_LINE;
  247. }
  248.  
  249. /**
  250.  * IncludeRichTextEditor()
  251.  * Include RichTextEditor if enabled
  252.  * @return void 
  253.  */
  254. function IncludeRichTextEditor() {
  255.  
  256.   if(defined('RICH_TEXT_EDITOR') && RICH_TEXT_EDITOR != (string) 0 )  {
  257.     if(strtolower(RICH_TEXT_EDITOR) == 'tinymce')
  258.     {
  259.       echo '<script type="text/javascript" src="'.themePath('../lib/js/tinymce/jscripts/tiny_mce/tiny_mce.js', THEME_ADMIN_PATH).'"></script>'.END_LINE;
  260.       echo '<script type="text/javascript" src="'.themePath('../lib/js/tinymce/jscripts/tiny_mce/config.js', THEME_ADMIN_PATH).'"></script>'.END_LINE;
  261.     }
  262.     if(strtolower(RICH_TEXT_EDITOR) == 'cke')
  263.     {
  264.       include('../lib/js/ckeditor/ckeditor.php');
  265.       $CKEditor = new CKEditor();
  266.       $CKEditor->basePath = SITE_ROOT_URL.'lib/js/ckeditor/';
  267.       $CKEditor->config['customConfig'] = 'l21_config.js';
  268.  
  269.  
  270.       if(defined('CKFINDER_ENABLED') && CKFINDER_ENABLED == true) {
  271.         require_once '../lib/vendor/ckfinder/ckfinder.php';
  272.         CKFinder::SetupCKEditor( $CKEditor, '../lib/vendor/ckfinder/' ) ;
  273.       } else {
  274.         $CKEditor->config['filebrowserBrowseUrl'] = '../library/access.php';
  275.         $CKEditor->config['filebrowserImageBrowseUrl'] = '../library/access.php?type=images';
  276.         $CKEditor->config['filebrowserUploadUrl'] = '../library/access.php';
  277.         $CKEditor->config['filebrowserImageUploadUrl'] = '../library/access.php?type=images';
  278.         $CKEditor->config['filebrowserWindowWidth'] = '100%';
  279.         $CKEditor->config['filebrowserWindowHeight'] = '100%';
  280.       }
  281.  
  282.       $CKEditor->replaceAll('largetextfield');
  283.     }
  284.   }
  285.  
  286. }
  287.  
  288.  
  289. /**
  290.  * IncludeLightboxJs()
  291.  * Include Lightbox jQuery plugin
  292.  * @param string £selector
  293.  * @return void 
  294.  */
  295. function IncludeLightboxJs($selector) {
  296.   $str='';
  297.   if(is_string($selector)) {
  298.     $str .= '$("'.$selector.'").lightBox();'.END_LINE;
  299.   } else {
  300.     foreach($selector as $sel) {
  301.       $str .= '$("'.$sel.'").lightBox();'.END_LINE;
  302.     }
  303.   }
  304.   echo '<script type="text/javascript" src="../lib/js/jquery.lightbox-0.5.pack.js"></script>'.END_LINE;
  305.   echo '<link type="text/css" rel="stylesheet" href="'.THEME_ADMIN_PATH.'css/jquery.lightbox-0.5.css" />'.END_LINE;
  306.   echo '
  307.         <script type="text/javascript">
  308.         $(document).ready(function() {
  309.                   '.$str.'
  310.           })
  311.         </script>'. END_LINE;
  312.  
  313. }
  314.  
  315. /**
  316.  * IncludeTextBoxListJs()
  317.  * Include jQuery TextBoxList plugin
  318.  * if needed
  319.  * @return void 
  320.  */
  321. function IncludeTextBoxListJs() {
  322.  
  323.   $rub = isset($_REQUEST['rub']) ? $_REQUEST['rub'] : '';
  324.   $todo = isset($_REQUEST['todo']) ? $_REQUEST['todo'] : '';
  325.  
  326.   if(ActiveItemKey($rub)) $rub = ActiveItemKey($rub);
  327.   $s = $rub . '|' . $todo;
  328.   $a = $GLOBALS['textboxList_includes'];
  329.  
  330.   if(in_array($s, $a)) {
  331.     echo '<link type="text/css" rel="stylesheet" href="'.THEME_ADMIN_PATH.'css/TextboxList.css" media="screen" />'.END_LINE;
  332.     echo '<link type="text/css" rel="stylesheet" href="'.THEME_ADMIN_PATH.'css/TextboxList.Autocomplete.css" media="screen" />'.END_LINE;
  333.     echo '<script type="text/javascript" src="../lib/js/jquery.textboxList/GrowingInput.js"></script>'.END_LINE;
  334.     echo '<script type="text/javascript" src="../lib/js/jquery.textboxList/TextboxList.js"></script>'.END_LINE;
  335.     echo '<script type="text/javascript" src="../lib/js/jquery.textboxList/TextboxList.Autocomplete.js"></script>'.END_LINE;
  336.   }
  337. }
  338.  
  339. /**
  340.  * IncludeMultiSelectJs()
  341.  * Include jQuery multiSelect plugin
  342.  * if needed
  343.  * @return void 
  344.  */
  345. function IncludeMultiSelectJs() {
  346.  
  347.   $rub = isset($_REQUEST['rub']) ? $_REQUEST['rub'] : '';
  348.   $todo = isset($_REQUEST['todo']) ? $_REQUEST['todo'] : '';
  349.  
  350.   if(ActiveItemKey($rub)) $rub = ActiveItemKey($rub);
  351.   $s = $rub . '|' . $todo;
  352.   $a = $GLOBALS['multiSelect_includes'];
  353.  
  354.   if(in_array($s, $a)) {
  355.     echo '<script type="text/javascript" src="../lib/js/jquery.multiSelect/jquery.multiSelect.js"></script>'.END_LINE;
  356.     echo '<script type="text/javascript" src="../lib/js/jquery.multiSelect/jquery.bgiframe.min.js"></script>'.END_LINE;
  357.     echo '<link type="text/css" rel="stylesheet" href="'.THEME_ADMIN_PATH.'css/jquery.multiSelect.css" />'.END_LINE;
  358.   }
  359. }
  360.  
  361. /**
  362.  * IncludedatePickerJs()
  363.  * Include jQuery date Picker
  364.  * if needed
  365.  * @return void 
  366.  */
  367. function IncludedatePickerJs() {
  368.  
  369.   $rub = isset($_REQUEST['rub']) ? $_REQUEST['rub'] : '';
  370.   $todo = isset($_REQUEST['todo']) ? $_REQUEST['todo'] : '';
  371.  
  372.   if(ActiveItemKey($rub)) $rub = ActiveItemKey($rub);
  373.   $s = $rub . '|' . $todo;
  374.   $a = $GLOBALS['datePicker_includes'];
  375.  
  376.   if(in_array($s, $a)) {
  377.     echo '<script type="text/javascript" src="../lib/js/jquery.datePicker.js"></script>'.END_LINE;
  378.     echo '<link type="text/css" rel="stylesheet" href="'.THEME_ADMIN_PATH.'css/datePicker.css" />'.END_LINE;
  379.     echo '<script type="text/javascript" src="../lib/js/date.js"></script>'.END_LINE;
  380.     if(U_L != 'en') {
  381.       echo '<script type="text/javascript" src="'.themePath('../lib/js/date_'. strtolower(U_L).'.js', THEME_ADMIN_PATH).'"></script>'. END_LINE;
  382.     }
  383.   }
  384. }
  385.  
  386. /**
  387.  * SureCreateDir()
  388.  * Créer un dossier s'il n'existe pas.
  389.  * @param string $pathname 
  390.  * @param integer $perms 
  391.  * @return integer $ver_num
  392.  */
  393. function SureCreateDir($pathname, $perms) {
  394.   if(!file_exists($pathname)) {
  395.     return mkdir ($pathname, $perms, true);
  396.   } else {
  397.     return true;
  398.   }
  399. }
  400.  
  401.  
  402. /**
  403.  * SureRemoveDir()
  404.  * Supprime le contenu d'un dossier et le dossier lui-même si précisé.
  405.  *
  406.  * @return integer $ver_num
  407.  */
  408. function SureRemoveDir($dir, $DeleteMe) {
  409.   if(!$dh = @opendir($dir)) return;
  410.   while (($obj = readdir($dh))) {
  411.     if($obj=='.' || $obj=='..') continue;
  412.     if (!@unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true);
  413.   }
  414.   if ($DeleteMe){
  415.     closedir($dh);
  416.     @rmdir($dir);
  417.   }
  418. }
  419.  
  420. /**
  421.  * num_phpversion()
  422.  * Retourne un entier comme numéro de version PHP
  423.  *
  424.  * @return integer $ver_num
  425.  */
  426. function num_phpversion() {
  427.   $ver = explode( '.', phpversion() );
  428.   $ver_num = $ver[0] . $ver[1] . $ver[2];
  429.  
  430.   return $ver_num;
  431. }
  432.  
  433. /**
  434.  * cutText()
  435.  * Découpe un texte à une longeur donnée.
  436.  *
  437.  * @param string $content 
  438.  * @param integer $length 
  439.  * @param integer $abbr 
  440.  * @param string $end 
  441.  * @return 
  442.  */
  443. function cutText($content, $length, $abbr = 0, $end = '...')
  444. {
  445.   // fix bug #16
  446.   if(function_exists('mb_substr')) $substr = mb_substr($content, 0, $length, CHARSET);
  447.   else  $substr = substr($content, 0, $length);
  448.   if (strlen($content) > $length) {
  449.     if ($abbr == 1) $content_light = "<abbr title=\"" . $content . "\">" . $substr . $end . "</abbr>\n";
  450.     else $content_light = $substr . $end;
  451.   } else $content_light = $content;
  452.   return $content_light;
  453. }
  454.  
  455. /**
  456.  * cutBody()
  457.  * Renvoie un texte en 2 parties dans un tableau
  458.  *
  459.  * @param string $text 
  460.  * @return array $body
  461.  */
  462. function cutBody($text)
  463. {
  464.   $middle = (strlen($text) / 2);
  465.   $end = strlen($text);
  466.   $body[0] = substr($text, 0, $middle);
  467.   $body[1] = substr($text, $middle, $end);
  468.   $chaine = preg_split("/(\.(<br \/>| ))/", $body[1], 2);
  469.  
  470.   $body[0] .= $chaine[0] . ".";
  471.   $body[1] = @trim($chaine[1]);
  472.  
  473.   return $body;
  474. }
  475.  
  476. /**
  477.  * EnlargeImage()
  478.  * Miniatures; agrandissement onclick
  479.  *
  480.  * @param string $uri 
  481.  * @param string $style 
  482.  * @param string $alt_desc 
  483.  * @return string $img_link
  484.  */
  485. function EnlargeImage($uri, $style = '', $alt_desc = '')
  486. {
  487.   list($width, $height, $type, $attr) = getimagesize($uri);
  488.  
  489.   $img_link = '<a href="' . $uri . '" title="' . _t('divers','enlarge_photo') . '">' . END_LINE;
  490.   $img_link .= '<img src="' . get_min_name($uri) . '" alt="' . _t('divers','enlarge_photo') . ' ' . $alt_desc . '"' . $style . ' />' . END_LINE;
  491.   $img_link .= '</a>' . END_LINE;
  492.  
  493.   return $img_link;
  494. }
  495.  
  496. /**
  497.  * formatNavTitle()
  498.  * Formatage des titres ( interface admin )
  499.  *
  500.  * @param string $content 
  501.  * @return string $content
  502.  */
  503. function formatNavTitle($content)
  504. {
  505.   $content = formatText($content, '2HTML');
  506.   $content = cutText($content, 70, 1);
  507.  
  508.   return $content;
  509. }
  510.  
  511. /**
  512.  * formatTextli()
  513.  * Formatage des listes ( interface admin )
  514.  *
  515.  * @param string $content 
  516.  * @return string $content
  517.  */
  518. function formatTextli($content)
  519. {
  520.   $content = formatText($content, '2HTML');
  521.   $content = cutText($content, 65, 1);
  522.   return $content;
  523. }
  524.  
  525. /**
  526.  * formatTitleh2()
  527.  * Formatage des titres h2 ( interface admin )
  528.  *
  529.  * @param string $content 
  530.  * @return string $content
  531.  */
  532. function formatTitleh2($content)
  533. {
  534.   $content = formatText($content, '2HTML');
  535.   return $content;
  536. }
  537.  
  538. /**
  539.  * isRawText()
  540.  * check if raw text or html text
  541.  *
  542.  * @param string $t 
  543.  * @return bool 
  544.  */
  545. function isRawText($t) {
  546.   if(preg_match('/<[a-z0-9]>/i', $t) == 0) {
  547.     return true;
  548.   } else {
  549.     return false;
  550.   }
  551. }
  552.  
  553. /**
  554.  * formatText()
  555.  * Formatage de texte pour affichage
  556.  *
  557.  * @param  $content 
  558.  * @param string $format 
  559.  * @return string $content
  560.  */
  561. function formatText($content, $format = -1)
  562. {
  563.   $content = stripslashes(trim($content));
  564.   switch ($format) {
  565.     case '2HTML':
  566.       if(RICH_TEXT_EDITOR === 0 || isRawText($content)) $content = nl2br($content);
  567.       break;
  568.     case '2FIELD':
  569.       $content = htmlentities($content, ENT_QUOTES, 'utf-8');
  570.       break;
  571.     case '2ATT':
  572.       $content = htmlentities($content, ENT_QUOTES, 'utf-8');
  573.       break;
  574.     case '2XML':
  575.       $content = strip_tags($content);
  576.       break;
  577.     case '2FILE':
  578.       //$content = addslashes(trim($content));
  579.       $content = htmlspecialchars($content, ENT_QUOTES, 'utf-8');
  580.       break;
  581.     default:
  582.   }
  583.   return $content;
  584. }
  585.  
  586. /**
  587.  * ReplaceInvalidChars()
  588.  * Remplacement des caractères invalides  par leurs entités HTML
  589.  *
  590.  * @param string $str 
  591.  * @return string $valid_string
  592.  */
  593. function ReplaceInvalidChars($str)
  594. {
  595.   $htmlentities_chars = array('å' => '&#338;', 'ú' => '&#339;',
  596.         'ä' => '&#352;', 'ö' => '&#353;',
  597.         'ü' => '&#376;', 'à' => '&#710;',
  598.         'ò' => '&#732;', 'ñ' => '&#8211;',
  599.         'ó' => '&#8212;', 'ë' => '&#8216;',
  600.         'í' => '&#8217;', 'Ç' => '&#8218;',
  601.         'ì' => '&#8220;', 'î' => '&#8221;',
  602.         'Ñ' => '&#8222;', 'Ü' => '&#8224;',
  603.         'á' => '&#8225;', 'Ö' => '&#8230;',
  604.         'â' => '&#8240;', 'É' => '&#402;',
  605.         'ã' => '&#8249;', 'õ' => '&#8250;',
  606.         'Ä' => '&#8364;', 'ô' => '&#8482;',
  607.         'ï' => '&#8226;', 'ò' => '&#732;',
  608.         'ô' => '&#8482;');
  609.  
  610.   //$valid_string = str_replace(array_keys($htmlentities_chars), array_values($htmlentities_chars), $str);
  611.   //return $valid_string;
  612.   return $str;
  613. }
  614.  
  615. function stripAccents($string) {
  616.  
  617.   $search = explode(",","ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u");
  618.   $replace = explode(",","c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u");
  619.  
  620.   return str_replace($search, $replace, $string);
  621.  
  622.  
  623. }
  624.  
  625. function stripText($text) {
  626.  
  627.   $text = strtolower($text);
  628.  
  629.   // strip all non word chars
  630.   $text = preg_replace('/\W/', ' ', $text);
  631.   // replace all white space sections with a dash
  632.   $text = preg_replace('/\ +/', '-', $text);
  633.   // trim dashes
  634.   $text = preg_replace('/\-$/', '', $text);
  635.   $text = preg_replace('/^\-/', '', $text);
  636.  
  637.   return $text;
  638. }
  639.  
  640. /**
  641.  * toStringSqlDate()
  642.  * Renvoie la date au format SQL
  643.  *
  644.  * @param string $format 
  645.  * @return string $s
  646.  */
  647. function toStringSqlDate($format = 'short')
  648. {
  649.   $date_format =   array(
  650.                    'dd-mm-yyyy' => array(
  651.                           'mysql' => array('short'=> '%d-%m-%Y', 'long'=>
  652.   array('12' => '%d-%m-%Y  %r', '24' => '%d-%m-%Y  %T')),
  653.                         'pgsql' => array('short'=> 'DD-MM-YYYY', 'long'=>
  654.   array('12' => 'DD-MM-YYYY HH12:MI:SS', '24' => 'DD-MM-YYYY HH24:MI:SS'))),
  655.                     'yyyy-mm-dd' => array(
  656.                           'mysql' => array('short'=> '%Y-%m-%d', 'long'=>
  657.   array('12' => '%Y-%m-%d  %r', '24' => '%Y-%m-%d  %T')),
  658.                         'pgsql' => array('short'=> 'YYYY-MM-DD', 'long'=>
  659.   array('12' => 'YYYY-MM-DD HH12:MI:SS', '24' => 'YYYY-MM-DD HH24:MI:SS'))),
  660.  
  661.   );
  662.    
  663.   if($format == 'long') $s = $date_format[DATE_FORMAT][SQL][$format][TIME_FORMAT];
  664.   else $s = $date_format[DATE_FORMAT][SQL][$format];
  665.  
  666.   return $s;
  667. }
  668.  
  669.  
  670. /**
  671.  * date_compare()
  672.  * Compare 2 dates with a given operator.
  673.  * @param  $date1 
  674.  * @param  $date2 
  675.  * @param  $op 
  676.  * @return boolean 
  677.  */
  678. function date_compare($date1, $date2, $op) {
  679.  
  680.   $date1= strtotime(formatDate($date1, true));
  681.   $date2= strtotime(formatDate($date2, true));
  682.  
  683.   switch($op) {
  684.     case '>':
  685.       if($date1 > $date2) return true;
  686.       else return false;
  687.       break;
  688.     case '<':
  689.       if($date1 < $date2) return true;
  690.       else return false;
  691.     case '>=':
  692.       if($date1 >= $date2) return true;
  693.       else return false;
  694.       break;
  695.     case '<=':
  696.       if($date1 <= $date2) return true;
  697.       else return false;
  698.     case '==':
  699.       if($date1 == $date2) return true;
  700.       else return false;
  701.     default:
  702.       return false;
  703.   }
  704. }
  705.  
  706. /**
  707.  * ln10filename()
  708.  * Build a localized filename
  709.  * according to the cuurent language
  710.  *
  711.  * @param string $file 
  712.  * @return string 
  713.  */
  714. function ln10filename($file)
  715. {
  716.  
  717.   $tmp=@explode(".", $file);
  718.   $total = count($tmp) - 1;
  719.   $ext = $tmp[$total];
  720.   unset($tmp[$total]);
  721.  
  722.   return @implode(".", $tmp). '.' . U_L. '.' .$ext;
  723. }
  724.  
  725.  
  726. /**
  727.  * distInclude()
  728.  * Include the required file
  729.  * if no user file is found,
  730.  * includes the dist/ version file.
  731.  * Localized files have the priority
  732.  *
  733.  * @param string $file 
  734.  * @return void 
  735.  */
  736. function distInclude($file, $default_dist)
  737. {
  738.   $l10n_file = ln10filename($file);
  739.   $l10n_file_dist =  dirname($l10n_file).'/dist/'.basename($l10n_file);
  740.  
  741.   $file_dist_default =  $default_dist.'dist/'.basename($file);
  742.   $l10n_file_dist_default = $default_dist.'dist/'.basename($l10n_file);
  743.  
  744.   if(file_exists($l10n_file)) {
  745.     _debug('distInclude() file Inclusion : '.$l10n_file);
  746.     include_once($l10n_file);
  747.  
  748.   } elseif(file_exists($l10n_file_dist))  {
  749.     _debug('distInclude() file Inclusion : '.$l10n_file_dist);
  750.     include_once($l10n_file_dist);
  751.   }
  752.   elseif(file_exists($file)) {
  753.     _debug('distInclude() file Inclusion : '.$file);
  754.     include_once($file);
  755.   }
  756.   elseif(file_exists($l10n_file_dist_default)) {
  757.     _debug('distInclude() file Inclusion : '.$l10n_file_dist_default);
  758.     include_once($l10n_file_dist_default);
  759.   }
  760.   elseif(file_exists($file_dist_default)) {
  761.     _debug('distInclude() file Inclusion : '.$file_dist_default);
  762.     include_once($file_dist_default);
  763.   }
  764.   else {
  765.     _debug('distInclude() file Inclusion : '.dirname($file).'/dist/'.basename($file));
  766.     include_once(dirname($file).'/dist/'.basename($file));
  767.   }
  768.  
  769. }
  770.  
  771. /**
  772.  * themePath()
  773.  * check if a template file version exists or not
  774.  * if no user file is found in theme,
  775.  * return the default module version file.
  776.  *
  777.  * @param string $file 
  778.  * @param string $path 
  779.  * @return string 
  780.  */
  781. function themePath($file, $path= 0)
  782. {
  783.   if($path == 0) $path = THEME_PUBLIC_PATH;
  784.   $theme_file = $path.'override/'.str_replace('../', '', $file);
  785.   if(file_exists($theme_file)) {
  786.     _debug('themePath() returned value : '.$theme_file);
  787.     return $theme_file;
  788.  
  789.   } else {
  790.     _debug('themePath() returned value : '.$file);
  791.     return $file;
  792.   }
  793.  
  794. }
  795.  
  796. /**
  797.  * formatDate()
  798.  * Renvoie la date aux formats yyyy-mm-dd ou dd-mm-yyyy suivant le cas de départ
  799.  * Si $db == true renvoie toujours la date au format yyyy-mm-dd
  800.  *
  801.  * @param string $date 
  802.  * @param boolean $db 
  803.  * @return string $new_date
  804.  */
  805. function formatDate($date, $db = false)
  806. {
  807.   @list($part1, $part2, $part3) = explode('-', $date);
  808.   if(strlen($part1) == 2) {
  809.     $new_date = $part3 . '-' . $part2 . '-' . $part1;
  810.   } else {
  811.     $new_date = $part1 . '-' . $part2 . '-' . $part3;
  812.   }
  813.   if($db == true) {
  814.     // always return yyyy-mm-dd format
  815.     if(strlen($part1) == 2) {
  816.       $new_date = $part3 . '-' . $part2 . '-' . $part1;
  817.     } else {
  818.       $new_date = $part1 . '-' . $part2 . '-' . $part3;
  819.     }
  820.   }
  821.  
  822.   return $new_date;
  823. }
  824.  
  825.  
  826. /**
  827.  * date_rfc2822()
  828.  * Format date to RFC 2822 date format
  829.  * @param string $date 
  830.  * @return string (exemple : Thu, 21 Dec 2000 16:01:07 +0200)
  831.  */
  832. function date_rfc2822($date) {
  833.   if(!isNullDate($date)) {
  834.     $tmp_date = formatDate($date, true);
  835.     @list($y, $m, $d) = explode('-', $tmp_date);
  836.     return date("r", mktime(3, 0, 0, $m, $d, $y));
  837.   } else {
  838.     return false;
  839.   }
  840. }
  841.  
  842.  
  843. function isNullDate($date)
  844. {
  845.   if($date == '0001-01-01' || $date == '01-01-0001') {
  846.     return true;
  847.   } else return false;
  848.  
  849. }
  850.  
  851. /**
  852.  * empty_nc()
  853.  * retourne le contenu ou N.C
  854.  *
  855.  * @param string $content 
  856.  * @return string $content
  857.  */
  858. function empty_nc($content)
  859. {
  860.   $content = trim($content);
  861.   if (empty($content) || isNullDate($content)) $content = _t('divers','nc');
  862.  
  863.   return $content;
  864. }
  865.  
  866. /**
  867.  * empty_none()
  868.  * retourne le contenu ou 'aucun'
  869.  *
  870.  * @param string $content 
  871.  * @return string $content
  872.  */
  873. function empty_none($content)
  874. {
  875.   $content = trim($content);
  876.   if (empty($content)) $content = _t('divers','none');
  877.  
  878.   return $content;
  879. }
  880.  
  881. /**
  882.  * empty_none()
  883.  * retourne le contenu ou 0
  884.  *
  885.  * @param string $content 
  886.  * @return string $content
  887.  */
  888. function empty_numeric($content)
  889. {
  890.   $content = trim($content);
  891.   if (empty($content)) $content = 0;
  892.  
  893.   return $content;
  894. }
  895.  
  896. /**
  897.  * checkdate_validity()
  898.  * Vérifie la validité d'une date
  899.  *
  900.  * @param string $date 
  901.  * @param string $msg (optionnal)
  902.  * @return boolean true or error message (string)
  903.  */
  904. function checkdate_validity($date, $msg= '')
  905. {
  906.  
  907.   $date=formatDate($date, true);
  908.   @list($year, $month, $day) = explode('-', $date);
  909.  
  910.   if(!preg_match('/^\d{4}-\d\d-\d\d$/', $date)) {
  911.     $msg .= _t('date','not_valid');
  912.     return $msg;
  913.   }
  914.  
  915.   if (!@checkdate($month , $day , $year)) return $msg . _t('date','date_do_not_exist');
  916.   return true;
  917. }
  918.  
  919. /**
  920.  * display_errors()
  921.  * Affichage d'un message d'erreur utilisateur
  922.  *
  923.  * @param string $msg 
  924.  * @return void (echo)
  925.  */
  926. function display_errors($msg)
  927. {
  928.   $display_it = "<div class=\"error\">\n";
  929.   $display_it .= $msg;
  930.   $display_it .= "</div>\n";
  931.   echo $display_it;
  932. }
  933.  
  934. /**
  935.  * system_error()
  936.  * Affichage d'un message d'erreur syst�me
  937.  *
  938.  * @param string $msg 
  939.  * @return void (echo)
  940.  */
  941. function system_error($msg = ERROR_SYSTEM)
  942. {
  943.   $display_it = "<div class=\"systemerror\">\n";
  944.   $display_it .= $msg;
  945.   $display_it .= "</div>\n";
  946.   echo $display_it;
  947.   exit;
  948. }
  949.  
  950. /**
  951.  * get_temp_name()
  952.  * obtenir le nom temporaire d'un fichier
  953.  *
  954.  * @param string $path 
  955.  * @return string $temp_path
  956.  */
  957. function get_temp_name($path)
  958. {
  959.   $short_path = dirname($path);
  960.   $filename = basename($path);
  961.   $temp_path = $short_path . "/temp_" . $filename;
  962.   return $temp_path;
  963. }
  964.  
  965. /**
  966.  * get_min_name()
  967.  * obtenir le nom de la miniature d'un fichier
  968.  *
  969.  * @param string $path 
  970.  * @return string $min_path
  971.  */
  972. function get_min_name($path)
  973. {
  974.   $short_path = dirname($path);
  975.   $filename = basename($path);
  976.   $min_path = $short_path . "/min_" . $filename;
  977.   return $min_path;
  978. }
  979.  
  980. /**
  981.  * ExcedMaxSize()
  982.  * Teste si une image dépasse ou non la taille autorisée (en pixels)
  983.  *
  984.  * @param string $path 
  985.  * @param integer $width_max 
  986.  * @return boolean 
  987.  */
  988. function ExcedMaxSize($path, $width_max)
  989. {
  990.   list($width, $height, $type, $attr) = getimagesize($path);
  991.   if ($width > $width_max || $height > $width_max) return true;
  992.   else return false;
  993. }
  994.  
  995. /**
  996.  * GetAllPhotoUri()
  997.  * renvoie les paths des photos dans un tableau
  998.  *
  999.  * @param string $path 
  1000.  * @param string $opt 
  1001.  * @return array $tab
  1002.  */
  1003. function GetAllPhotoUri($path, $opt = -1)
  1004. {
  1005.   if (empty($path)) return false;
  1006.   else {
  1007.     $tab = explode('|', $path);
  1008.     if ($opt == 'min') array_walk($tab, 'get_min_name');
  1009.     return $tab;
  1010.   }
  1011. }
  1012.  
  1013. /**
  1014.  * cancel_button()
  1015.  * génére un bouton de retour
  1016.  *
  1017.  * @param  $back_uri 
  1018.  * @return string 
  1019.  */
  1020. function cancel_button($back_uri)
  1021. {
  1022.   return '<input name="annuler" type="button" value="' . _t('btn','annul') . '" class="button" id="annuler" onclick="window.location=\'' . $back_uri . '\';" />';
  1023. }
  1024.  
  1025. /**
  1026.  * GetDisplayUserRight()
  1027.  * renvoie les droits d'un utilisateur
  1028.  *
  1029.  * @param string $indice 
  1030.  * @param string $module 
  1031.  * @return string 
  1032.  */
  1033. function GetDisplayUserRight($indice, $module = -1)
  1034. {
  1035.   $indice = strtoupper($indice);
  1036.   if ($indice == 'U') return _t('user','norights');
  1037.   if ($indice == 'A') return _t('user','adminrights');
  1038.   if ($indice == 'O' && $module == 'dashboard') return _t('user','managerrights');
  1039.   if ($indice == 'O' && $module == 'workshop') return _t('user','animatorrights');
  1040.   if ($indice == 'O' && ($module != 'workshop' && $module != 'dashboard')) return _t('user','redactorrights');
  1041. }
  1042.  
  1043. /**
  1044.  * Display_linkin_page()
  1045.  *
  1046.  * @param array $table_link 
  1047.  * @param integer $total 
  1048.  * @param integer $debut 
  1049.  * @param integer $pas 
  1050.  * @return void 
  1051.  ***/
  1052. function Display_linkin_page($table_link, $total, $debut, $pas = SELECT_LIMIT)
  1053. {
  1054.   $result = ceil($total / $pas);
  1055.   if ($result <= 1) return '&nbsp;';
  1056.   else {
  1057.     $link = '<div class="lienliste">'.END_LINE;
  1058.     $link .= '« ';
  1059.     $sep='';
  1060.     for($i = 0; $i < $result; $i++) {
  1061.       $current_pos = ($pas * $i);
  1062.       if ($debut == $current_pos) $link .= $sep."<span>" . ($i + 1) . "</span> \n";
  1063.       else {
  1064.         $array_pos = array ('debut' => $current_pos);
  1065.         $new_table_link = array_merge ($table_link, $array_pos);
  1066.         $link .= $sep.'<a href="'. HrefMaker($new_table_link) . '">' . ($i + 1) . '</a>'.END_LINE;
  1067.       }
  1068.       $sep=' | ';
  1069.     }
  1070.     $link .= ' »';
  1071.     $link .= '</div>'.END_LINE;
  1072.     echo $link;
  1073.   }
  1074. }
  1075.  
  1076. /**
  1077.  * linkin_page()
  1078.  * création d'un navigateur de pages numérotées
  1079.  *
  1080.  * @param string $string_uri 
  1081.  * @param integer $total 
  1082.  * @param integer $debut 
  1083.  * @param integer $pas 
  1084.  * @return string $link
  1085.  */
  1086. function linkin_page($string_uri, $total, $debut, $pas = SELECT_LIMIT)
  1087. {
  1088.   $result = ceil($total / $pas);
  1089.   if ($result <= 1) return '&nbsp;';
  1090.   else {
  1091.     if (strpos($string_uri, '?') === false) $string_uri .= '?';
  1092.     else $string_uri .= '&amp;';
  1093.     $link = '<div class="lienliste">';
  1094.     for($i = 0; $i < $result; $i++) {
  1095.       $current_pos = ($pas * $i);
  1096.       if ($debut == $current_pos) $link .= "<span>" . ($i + 1) . "</span> \n";
  1097.       else $link .= "<a href=\"" . $string_uri . "debut=" . $current_pos . "\">" . ($i + 1) . "</a> \n";
  1098.     }
  1099.     $link .= '</div>';
  1100.     return $link;
  1101.   }
  1102. }
  1103.  
  1104. /**
  1105.  * display_statut()
  1106.  * renvoie le statut en pleines lettres
  1107.  *
  1108.  * @param string $statut 
  1109.  * @return string $result
  1110.  */
  1111. function display_statut($statut)
  1112. {
  1113.   switch ($statut) {
  1114.     case 'P':
  1115.       $result = _t('statut','public');
  1116.       break;
  1117.     case 'D':
  1118.       $result = _t('statut','draft');
  1119.       break;
  1120.     case 'E':
  1121.       $result = _t('statut','E');
  1122.       break;
  1123.     case 'AA':
  1124.       $result = _t('statut','AA');
  1125.       break;
  1126.     case 'PA':
  1127.       $result = _t('statut','PA');
  1128.       break;
  1129.     case 'C':
  1130.       $result = _t('statut','C');
  1131.       break;
  1132.     case 'U':
  1133.       $result = _t('statut','U');
  1134.       break;
  1135.     case 'O':
  1136.       $result = _t('statut','O');
  1137.       break;
  1138.     case 'A':
  1139.       $result = _t('statut','A');
  1140.       break;
  1141.     case 'W':
  1142.       $result = _t('statut','W');
  1143.       break;
  1144.     default:
  1145.       $result = _t('statut','public');
  1146.   }
  1147.   return $result;
  1148. }
  1149.  
  1150. /**
  1151.  * linkin_content()
  1152.  * Cherche les liens et emails dans du contenu -> linkage
  1153.  *
  1154.  * @param string $content 
  1155.  * @param string $option 
  1156.  * @return string $content
  1157.  */
  1158. function linkin_content($content, $option = 'ALL')
  1159. {
  1160.   if(defined('AUTO_LINK') && AUTO_LINK == 1)
  1161.   {
  1162.     if(defined('RICH_TEXT_EDITOR') && RICH_TEXT_EDITOR == (string) 0) {
  1163.       if ($option == 'ALL' || $option == 'MAIL') {
  1164.         $content = eregi_replace("( |<br />)+([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+)", '\\1<a href="mailto:\\2">\\2</a>', $content);
  1165.       }
  1166.       if ($option == 'ALL' || $option == 'LINK') {
  1167.         $content = eregi_replace("(http|https|ftp|ftps)://([-a-z0-9#?/&=:,_;@%.{}]*)([a-z0-9=]{2,4})", '<a href="\\1://\\2\\3" class="out">\\1://\\2\\3</a>', $content);
  1168.       }
  1169.     }
  1170.   }
  1171.  
  1172.   return $content;
  1173. }
  1174.  
  1175. /**
  1176.  * QuickBoxNow()
  1177.  * Génération de la quickbox
  1178.  *
  1179.  * @param string $module 
  1180.  * @param string $h1 
  1181.  * @param string $liste 
  1182.  * @param string $suffixclass 
  1183.  * @return string $quickbox
  1184.  */
  1185. function QuickBoxNow($module, $h1, $liste, $suffixclass = '')
  1186. {
  1187.   $quickbox = "<div class=\"entete\">\n<div class=\"qb_ico\" id=\"qbico" . $suffixclass . "\" title=\"" . $module . "\"></div>\n";
  1188.   $quickbox .= "<div class=\"quickbox\" id=\"qbbg" . $suffixclass . "\">\n";
  1189.   $quickbox .= "<h1>" . $h1 . "</h1>\n";
  1190.   $quickbox .= "<ul>";
  1191.   $quickbox .= $liste;
  1192.   $quickbox .= "</ul>";
  1193.   $quickbox .= "</div>";
  1194.   $quickbox .= "</div>";
  1195.  
  1196.   return $quickbox;
  1197. }
  1198.  
  1199. /**
  1200.  * ReloadIndex()
  1201.  * Chargement de l'index après destruction de sessions
  1202.  *
  1203.  * @param string $item 
  1204.  * @return void 
  1205.  */
  1206. function ReloadIndex($item)
  1207. {
  1208.   switch ($item) {
  1209.     case 'public':
  1210.       return header("Location: ../public/index.php");
  1211.       break;
  1212.     case 'admin':
  1213.       return header("Location: ../admin/logout.php");
  1214.       break;
  1215.   }
  1216. }
  1217.  
  1218. /**
  1219.  * getmicrotime()
  1220.  * renvoie le temps en microsecondes
  1221.  *
  1222.  * @return float 
  1223.  */
  1224. function getmicrotime()
  1225. {
  1226.   list($usec, $sec) = explode(" ", microtime());
  1227.   return ((float)$usec + (float)$sec);
  1228. }
  1229.  
  1230.  
  1231. /**
  1232.  * availablePlugins()
  1233.  * Return available plugins
  1234.  * @return array 
  1235.  ***/
  1236. function availablePlugins() {
  1237.   $a = array();
  1238.   if ($handle = opendir('../plugins/')) {
  1239.     while (false !== ($file = readdir($handle))) {
  1240.       if (substr($file, 0, 4) == 'l21_' ) {
  1241.         array_push($a, $file);
  1242.       }
  1243.     }
  1244.     closedir($handle);
  1245.   }
  1246.  
  1247.   return $a;
  1248. }
  1249.  
  1250.  
  1251.  
  1252. /**
  1253.  * sql_dump2array()
  1254.  * @param $url 
  1255.  * @param $a 
  1256.  * @return array 
  1257.  * @link http://fr2.php.net/manual/fr/function.mysql-query.php
  1258.  */
  1259. function sql_dump2array($url, $a = -1) {
  1260.    
  1261.   $handle = @fopen($url, "r");
  1262.   $query = "";
  1263.   if($a == -1) $a = array();
  1264.  
  1265.   while(!feof($handle)) {
  1266.     $sql_line = fgets($handle);
  1267.     if (trim($sql_line) != "" && strpos($sql_line, "--") === false && strpos($sql_line, "#") === false) {
  1268.       array_push($a, $sql_line);
  1269.     }
  1270.   }
  1271.   return $a;
  1272. }
  1273.  
  1274. /**
  1275.  * placeholderReplace()
  1276.  * format '{$key}'
  1277.  * @param $array 
  1278.  * @param $input 
  1279.  * @return array 
  1280.  */
  1281. function placeholderReplace($array, $input) {
  1282.   foreach ($input as $key => $value)
  1283.   {
  1284.     $array = preg_replace("/{{$key}}/i", $value, $array);
  1285.   }
  1286.   return $array;
  1287. }
  1288.  
  1289. /**
  1290.  * GenerateXhtmlPage()
  1291.  *
  1292.  * @param $string 
  1293.  * @param integer $time 
  1294.  * @param unknown $redirect 
  1295.  * @return 
  1296.  ***/
  1297. function GenerateXhtmlPage($string, $time = 2, $redirect = CURRENT_APP_URL)
  1298. {
  1299.  
  1300.  
  1301.   $page  = '<?xml version="1.0" encoding="' . CHARSET . '" xml:lang="' . U_L . '"?>' . END_LINE;
  1302.   $page .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' . END_LINE;
  1303.   $page .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . U_L . '">' . END_LINE;
  1304.   $page .= '<head>' . END_LINE;
  1305.   $page .= '<meta http-equiv="content-type" content="text/html;charset=' . CHARSET . '" />' . END_LINE;
  1306.   $page .= '<meta http-equiv="content-langage" content="' . CHARSET . '" />' . END_LINE;
  1307.   $page .= '<meta http-equiv="refresh" content="' . $time . ';url=' . $redirect . '">' . END_LINE;
  1308.   $page .= '<link rel="icon" type="image/gif" href="' .THEME_ADMIN_PATH. 'images/favicon.gif" />' . END_LINE;
  1309.   $page .= '<title>'.SITE_NAME.'</title>' . END_LINE;
  1310.   $page .= '</head>' . END_LINE;
  1311.   $page .= '<body>' . END_LINE;
  1312.   $page .= '<div class="info" style="margin-top:10em">'.$string.END_LINE;
  1313.   $page .= '<br />';
  1314.   $page .= sprintf( _t('divers','redirect_string'), $redirect, $time);
  1315.   $page .= '</div>'. END_LINE;
  1316.   $page .= '</body>' . END_LINE;
  1317.   $page .= '</html>' . END_LINE;
  1318.  
  1319.   return $page;
  1320. }
  1321.  
  1322. /**
  1323.  * _debug()
  1324.  * Display a debug message
  1325.  * @param string 
  1326.  * @return void 
  1327.  ***/
  1328. function _debug($string)
  1329. {
  1330.   if(isset($GLOBALS['Dbg']))
  1331.   {
  1332.     global $Dbg;
  1333.  
  1334.     $Dbg->add($string);
  1335.     $Dbg->stopTimer();
  1336.   }
  1337. }
  1338.  
  1339. /**
  1340.  * headerAddCSS()
  1341.  * Add CSS into header
  1342.  * @param string 
  1343.  * @param string 
  1344.  * @return boolean 
  1345.  ***/
  1346. function headerAddCSS($path, $pos = 'default')
  1347. {
  1348.   if(!file_exists('../'.$path)) return false;
  1349.  
  1350.   $str = '<link type="text/css" rel="stylesheet" href="../'.$path.'" />'.END_LINE;
  1351.  
  1352.   if(isset($GLOBALS['CSSheader']))
  1353.   {
  1354.     if($pos == 'first') {
  1355.       $GLOBALS['CSSheader'] = $str . $GLOBALS['CSSheader'];
  1356.     } else {
  1357.       $GLOBALS['CSSheader'] .= $str . END_LINE ;
  1358.     }
  1359.   } else {
  1360.     $GLOBALS['CSSheader'] = $str;
  1361.   }
  1362.   _debug('Loading <b>CSS file</b> : '.$path . ' (position: '.$pos.')');
  1363.  
  1364.   return true;
  1365. }
  1366.  
  1367. /**
  1368.  * headerAddJS()
  1369.  * Add JS into header
  1370.  * @param string 
  1371.  * @param string 
  1372.  * @return boolean 
  1373.  ***/
  1374. function headerAddJS($path, $pos = 'default')
  1375. {
  1376.   if(!file_exists('../'.$path)) {
  1377.     _debug('<b>Problem loading JS file</b> : '.$path . ' (position: '.$pos.')');
  1378.     return false;
  1379.   }
  1380.  
  1381.   $str= '<script type="text/javascript" src="../'.$path .'"></script>'.END_LINE;
  1382.  
  1383.   if(isset($GLOBALS['JSheader']))
  1384.   {
  1385.     if($pos == 'first') {
  1386.       $GLOBALS['JSheader'] = $str . $GLOBALS['JSheader'];
  1387.     }
  1388.     else {
  1389.       $GLOBALS['JSheader'] .= $str . END_LINE ;
  1390.     }
  1391.   } else {
  1392.     $GLOBALS['JSheader'] = $str;
  1393.   }
  1394.   _debug('Loading <b>JS file</b> : '.$path . ' (position: '.$pos.')');
  1395.  
  1396.   return true;
  1397. }
  1398.  
  1399. /**
  1400.  * footerAddJS()
  1401.  * Add JS into footer
  1402.  * @param string 
  1403.  * @param string 
  1404.  * @return boolean 
  1405.  ***/
  1406. function footerAddJS($path, $pos = 'default')
  1407. {
  1408.   if(!file_exists('../'.$path)) {
  1409.     _debug('<b>Problem loading JS file</b> : '.$path . ' (position: '.$pos.')');
  1410.     return false;
  1411.   }
  1412.  
  1413.   $str= '<script type="text/javascript" src="../'.$path .'"></script>'.END_LINE;
  1414.  
  1415.   if(isset($GLOBALS['JSfooter']))
  1416.   {
  1417.     if($pos == 'first') {
  1418.       $GLOBALS['JSfooter'] = $str . $GLOBALS['JSheader'];
  1419.     }
  1420.     else {
  1421.       $GLOBALS['JSfooter'] .= $str . END_LINE ;
  1422.     }
  1423.   } else {
  1424.     $GLOBALS['JSfooter'] = $str;
  1425.   }
  1426.   _debug('Loading <b>JS file</b> : '.$path . ' (position: '.$pos.')');
  1427.  
  1428.   return true;
  1429. }
  1430.  
  1431. /**
  1432.  * AddDynamicHeader()
  1433.  * Display JS and CSS header
  1434.  * @return void 
  1435.  ***/
  1436. function AddDynamicHeader() {
  1437.   if(isset($GLOBALS['JSheader'])) echo $GLOBALS['JSheader'];
  1438.   if(isset($GLOBALS['CSSheader']))  echo $GLOBALS['CSSheader'];
  1439. }
  1440.  
  1441. /**
  1442.  * AddDynamicFooter()
  1443.  * Display JS footer
  1444.  * @return void 
  1445.  ***/
  1446. function AddDynamicFooter() {
  1447.   if(isset($GLOBALS['JSfooter'])) echo $GLOBALS['JSfooter'];
  1448. }
  1449.  
  1450. /**
  1451.  * check4newVersion()
  1452.  * Display a link to download new version if available
  1453.  * @return string 
  1454.  ***/
  1455. function check4newVersion() {
  1456.   try{
  1457.     if(!@$rss=simplexml_load_file(SITE_LINEA_URL.'/linea_version.xml')){
  1458.       throw new Exception('Version : xml file was not found');
  1459.     }
  1460.     $current_version = preg_replace("/([a-z])/i",'', LINEA_VERSION); // remove letter (if dev version)
  1461.  
  1462.     $latest_version = str_pad(str_replace('.', '', $rss->num), 4, '0');
  1463.     $current_version = str_pad(str_replace('.', '', $current_version), 4, '0');
  1464.  
  1465.     if((integer)$latest_version > (integer)$current_version) {
  1466.       $update = '<div id="version-check">'._t('check_update','search').' : <a href="'.(string)$rss->link.'">'.sprintf(_t('check_update','dl'), $rss->num).'</a></div>';
  1467.     } else {
  1468.       $update = '<div id="version-check">'._t('check_update','search').' : '._t('check_update','ok').'</div>';
  1469.     }
  1470.   }
  1471.   catch(Exception $e){
  1472.     $update = $e->getMessage();
  1473.   }
  1474.  
  1475.   return $update;
  1476. }
  1477.  
  1478. /**
  1479.  * loadThemeInfo()
  1480.  * Load theme info
  1481.  * @param string 
  1482.  * @param string 
  1483.  * @return string 
  1484.  ***/
  1485. function loadThemeInfo($type, $name) {
  1486.   $a = array();
  1487.   try{
  1488.     if(!@$flow=simplexml_load_file(SITE_PATH.'templates/'.$type.'/'.$name.'/theme.xml')){
  1489.       throw new Exception($name.' plugin : xml file was not found');
  1490.     }
  1491.     $a['name'] = $flow->themename;
  1492.     $a['description'] = $flow->description;
  1493.     $a['version'] = $flow->version;
  1494.     $a['date'] = $flow->date;
  1495.     $a['compatibility'] = $flow->compatibility;
  1496.     $a['author'] = $flow->author;
  1497.     $a['homepage'] = $flow->homepage;
  1498.  
  1499.     return $a;
  1500.   }
  1501.   catch(Exception $e){
  1502.     return $e->getMessage();
  1503.   }
  1504. }
  1505. /**
  1506.  * For compatibility with PHP < 5.2
  1507.  * json_decode
  1508.  */
  1509. if ( !function_exists('json_decode') ){
  1510.   function json_decode($content, $assoc=false){
  1511.     require_once __DIR__ .'/vendor/JSON/JSON.php';
  1512.     if ( $assoc ){
  1513.       $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
  1514.     } else {
  1515.       $json = new Services_JSON;
  1516.     }
  1517.     return $json->decode($content);
  1518.   }
  1519. }
  1520.  
  1521. /**
  1522.  * For compatibility with PHP < 5.2
  1523.  * json_encode
  1524.  */
  1525. if ( !function_exists('json_encode') ){
  1526.   function json_encode($content){
  1527.     require_once __DIR__ .'/vendor/JSON/JSON.php';
  1528.     $json = new Services_JSON;
  1529.      
  1530.     return $json->encode($content);
  1531.   }
  1532. }
  1533.  
  1534. /**
  1535.  Validate an email address.
  1536.  Provide email address (raw input)
  1537.  Returns true if the email address has the email
  1538.  address format and the domain exists.
  1539.  @link http://www.linuxjournal.com/article/9585
  1540.  */
  1541. function validEmail($email)
  1542. {
  1543.   $isValid = true;
  1544.   $atIndex = strrpos($email, "@");
  1545.   if (is_bool($atIndex) && !$atIndex)
  1546.   {
  1547.     $isValid = false;
  1548.   }
  1549.   else
  1550.   {
  1551.     $domain = substr($email, $atIndex+1);
  1552.     $local = substr($email, 0, $atIndex);
  1553.     $localLen = strlen($local);
  1554.     $domainLen = strlen($domain);
  1555.     if ($localLen < 1 || $localLen > 64)
  1556.     {
  1557.       // local part length exceeded
  1558.       $isValid = false;
  1559.     }
  1560.     else if ($domainLen < 1 || $domainLen > 255)
  1561.     {
  1562.       // domain part length exceeded
  1563.       $isValid = false;
  1564.     }
  1565.     else if ($local[0] == '.' || $local[$localLen-1] == '.')
  1566.     {
  1567.       // local part starts or ends with '.'
  1568.       $isValid = false;
  1569.     }
  1570.     else if (preg_match('/\\.\\./', $local))
  1571.     {
  1572.       // local part has two consecutive dots
  1573.       $isValid = false;
  1574.     }
  1575.     else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
  1576.     {
  1577.       // character not valid in domain part
  1578.       $isValid = false;
  1579.     }
  1580.     else if (preg_match('/\\.\\./', $domain))
  1581.     {
  1582.       // domain part has two consecutive dots
  1583.       $isValid = false;
  1584.     }
  1585.     else if
  1586.     (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
  1587.     str_replace("\\\\","",$local)))
  1588.     {
  1589.       // character not valid in local part unless
  1590.       // local part is quoted
  1591.       if (!preg_match('/^"(\\\\"|[^"])+"$/',
  1592.       str_replace("\\\\","",$local)))
  1593.       {
  1594.         $isValid = false;
  1595.       }
  1596.     }
  1597.     if(CHECK_LINK == 1) {
  1598.       if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
  1599.       {
  1600.         // domain not found in DNS
  1601.         $isValid = false;
  1602.       }
  1603.     }
  1604.   }
  1605.   return $isValid;
  1606. }
  1607.  
  1608. /**
  1609.  * setBreadcrumb()
  1610.  * Set breadcrumb content
  1611.  * @param array 
  1612.  * @return void 
  1613.  ***/
  1614. function setBreadcrumb($array)
  1615. {
  1616.   if(!isset($GLOBALS['breadcrumb'])) $GLOBALS['breadcrumb']= array();
  1617.   $GLOBALS['breadcrumb'] = array_merge($GLOBALS['breadcrumb'], $array);
  1618. }
  1619.  
  1620. /**
  1621.  * getBreadcrumb()
  1622.  * get the Breadcrumb for displaying
  1623.  * @param string (optional)
  1624.  * @return string 
  1625.  ***/
  1626. function getBreadcrumb($sep = '»')
  1627. {
  1628.   $str = '<div id="breadcrumb">'.END_LINE;
  1629.   $str.= '<ul>'.END_LINE;
  1630.   $str.= '<li>'.$sep.' <a href="'.SITE_ROOT_URL.'public/">Accueil</a></li>'.END_LINE;
  1631.   foreach($GLOBALS['breadcrumb'] as $key => $value) {
  1632.     // echo "$key => $value";
  1633.     if($value!=false) $str.= '<li>'.$sep.' <a href="'.$value.'">'.$key.'</a></li>'.END_LINE;
  1634.     else $str.= '<li>'.$sep.' '.$key.'</li>'.END_LINE;
  1635.   }
  1636.   $str.= '</ul>'.END_LINE;
  1637.   $str.= '</div>'.END_LINE;
  1638.  
  1639.   return $str;
  1640. }
  1641.  
  1642. /**
  1643.  * ListDir()
  1644.  * Return an HTML list for a given folder
  1645.  * @param int 
  1646.  * @param string 
  1647.  * @param string 
  1648.  * @return string 
  1649.  ***/
  1650. function ListDir($dir_handle,$path, $url) {
  1651.   //global $listing;
  1652.   $i= 0;
  1653.   echo "<ul>";
  1654.   while (false !== ($file = readdir($dir_handle))) {
  1655.     $dir =$path . $file;
  1656.     if(is_dir($dir) && $file != '.' && $file !='..' ) {
  1657.       $handle = @opendir($dir) or die("Unable to open file $file");
  1658.       ListDir($handle, $dir.'/', $url);
  1659.     } elseif($file != '.' && $file !='..' && $file !='.htaccess') {
  1660.       $size= formatBytes(filesize($dir), 2);
  1661.       $class = ($i++ & 1) ? '' : 'class="odd"';
  1662.       $dir = str_replace('../', '', $dir);
  1663.       $dir = str_replace('//', '/', $dir);
  1664.       $dir = dirname($dir).'/'. rawurlencode(basename($dir));
  1665.       echo '<li '.$class.'><a href="'. $url . $dir .'">'.$file.'</a> <em>('.$size.')</em> <a  class="dlfile" href="'. $url .'library/dl.php?file='. $dir .'" title="'._t('divers','dl'). ' ' . $file.'"><span>' ._t('divers','dl'). '</span></a></li>';
  1666.     }
  1667.   }
  1668.   if($i==0) echo '<li>'._t('divers','no_files').'</li>';
  1669.   echo "</ul>";
  1670.   closedir($dir_handle);
  1671. }
  1672.  
  1673. /**
  1674.  * formatBytes()
  1675.  * Make File size readable
  1676.  * @param int 
  1677.  * @param int (optional)
  1678.  * @return string 
  1679.  ***/
  1680. function formatBytes($bytes, $precision = 2) {
  1681.   $units = array('o', 'Ko', 'Mo', 'Go', 'To');
  1682.  
  1683.   $bytes = max($bytes, 0);
  1684.   $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  1685.   $pow = min($pow, count($units) - 1);
  1686.  
  1687.   $bytes /= pow(1024, $pow);
  1688.  
  1689.   return round($bytes, $precision) . ' ' . $units[$pow];
  1690. }
  1691.  
  1692. /**
  1693.  * logfile()
  1694.  * Log into file
  1695.  * @param string 
  1696.  * @param array 
  1697.  * @return void 
  1698.  ***/
  1699. function logfile($src, $a) {
  1700.  
  1701.   $sep = '##';
  1702.   $fp = @fopen($src, 'a');
  1703.  
  1704.   foreach($a as $value) {
  1705.     @fwrite($fp, $value . $sep);
  1706.   }
  1707.  
  1708.   @fwrite($fp, date('[d-m-y H:i:s]' . $sep. END_LINE));
  1709.   @fclose($fp);
  1710.  
  1711.   return true;
  1712. }
  1713.  
  1714. /**
  1715.  * getThemes()
  1716.  * Return installed themes
  1717.  * into an array
  1718.  * @param string 
  1719.  * @return array 
  1720.  ***/
  1721. function getThemes($f) {
  1722.  
  1723.   $a = array();
  1724.  
  1725.   if ($handle = opendir('../templates/'.$f)) {
  1726.     $sep='';
  1727.     while (false !== ($file = readdir($handle))) {
  1728.       if ($file != "." && $file != ".." && $file != ".svn") {
  1729.         array_push($a, $file);
  1730.       }
  1731.     }
  1732.     closedir($handle);
  1733.   }
  1734.  
  1735.   return $a;
  1736. }
  1737.  
  1738. /**
  1739.  * cleanString()
  1740.  * Remove exotic characters form string
  1741.  * same as used in filemanager
  1742.  * @param string 
  1743.  * @param array 
  1744.  * @return string 
  1745.  ***/
  1746. function cleanString($string, $allowed = array()) {
  1747.   $allow = null;
  1748.  
  1749.   if (!empty($allowed)) {
  1750.     foreach ($allowed as $value) {
  1751.       $allow .= "\\$value";
  1752.     }
  1753.   }
  1754.  
  1755.   $mapping = array(
  1756.         'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
  1757.         'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
  1758.         'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
  1759.         'Õ'=>'O', 'Ö'=>'O', 'Ő'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ű'=>'U', 'Ý'=>'Y',
  1760.         'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
  1761.         'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n',
  1762.         'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ő'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'ű'=>'u',
  1763.         'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r', ' '=>'_', "'"=>'_', '/'=>''
  1764.         );
  1765.  
  1766.         if (is_array($string)) {
  1767.  
  1768.           $cleaned = array();
  1769.  
  1770.           foreach ($string as $key => $clean) {
  1771.             $clean = strtr($clean, $mapping);
  1772.             $clean = preg_replace("/[^{$allow}_a-zA-Z0-9]/", '', $clean);
  1773.             $cleaned[$key] = preg_replace('/[_]+/', '_', $clean); // remove double underscore
  1774.           }
  1775.         } else {
  1776.           $string = strtr($string, $mapping);
  1777.           $string = preg_replace("/[^{$allow}_a-zA-Z0-9]/", '', $string);
  1778.           $cleaned = preg_replace('/[_]+/', '_', $string); // remove double underscore
  1779.         }
  1780.         return $cleaned;
  1781. }
  1782.  
  1783. /**
  1784.  * securityCheck()
  1785.  * Test and sanitize user input
  1786.  * from request
  1787.  * @return boolean 
  1788.  ***/
  1789. function securityCheck() {
  1790.  
  1791.   $passed = true;
  1792.   
  1793.   // we first sanitize vars
  1794.   if(isset($_REQUEST['rub']))
  1795.     $_REQUEST['rub']= strip_tags($_REQUEST['rub']);
  1796.   if(isset($_REQUEST['name']))
  1797.     $_REQUEST['name']= strip_tags($_REQUEST['name']);
  1798.   // then do tests
  1799.   if(isset($_REQUEST['id']) && preg_match('/[^0-9A-Za-z]/',$_REQUEST['id']))
  1800.     $passed= false;
  1801.   if(isset($_REQUEST['parentid']) && !is_numeric($_REQUEST['parentid']))
  1802.     $passed= false;
  1803.   if(isset($_REQUEST['parentparentid']) && !is_numeric($_REQUEST['parentparentid']))
  1804.     $passed= false;
  1805.   if(isset($_REQUEST['debut']) && !is_numeric($_REQUEST['debut']))
  1806.     $passed= false;
  1807.  
  1808.   if($passed == false) die('no way!');
  1809.   else return true;
  1810. }
  1811. ?>

Documentation generated on Fri, 01 Apr 2011 09:32:25 +0200 by phpDocumentor 1.4.1