web/wp-content/themes/IRI-Theme/functions.php
changeset 136 bde1974c263b
child 142 f6e6b46f8260
equal deleted inserted replaced
135:53cff4b4a802 136:bde1974c263b
       
     1 <?php
       
     2 
       
     3 require( "lib/zip.lib.php" ) ; //indiquez le chemin d'accès à la lib
       
     4 
       
     5 //error_reporting(E_ALL);
       
     6 
       
     7 function getPost($post = NULL) {
       
     8 	include('post.php');
       
     9 }
       
    10 
       
    11 function getSearch($q = NULL) {
       
    12 	include('searchform.php');
       
    13 }
       
    14 
       
    15 if ( function_exists('register_sidebar') )
       
    16 {
       
    17     register_sidebar
       
    18     (   array
       
    19         (
       
    20           'name' => 'Sidebar-Single',
       
    21           'before_widget' => '<div class="bottombar">',
       
    22           'after_widget' => '</div>',
       
    23           'before_title' => '<h2 class="widgettitle">',
       
    24           'after_title' => '</h2>',
       
    25         )
       
    26     );
       
    27 	register_sidebar
       
    28     (   array
       
    29         (
       
    30           'name' => 'Sidebar-Single-home',
       
    31           'before_widget' => '<div class="bottombar">',
       
    32           'after_widget' => '</div>',
       
    33           'before_title' => '<h2 class="widgettitle">',
       
    34           'after_title' => '</h2>',
       
    35         )
       
    36     );
       
    37     register_sidebar
       
    38     (   array
       
    39         (
       
    40           'name' => 'Bottom-Left',
       
    41           'before_widget' => '<div class="bottombar">',
       
    42           'after_widget' => '</div>',
       
    43           'before_title' => '<h2 class="widgettitle">',
       
    44           'after_title' => '</h2>',
       
    45         )
       
    46     );  
       
    47     register_sidebar
       
    48     (   array
       
    49         (
       
    50           'name' => 'Bottom-Middle',
       
    51           'before_widget' => '<div class="bottombar">',
       
    52           'after_widget' => '</div>',
       
    53           'before_title' => '<h2 class="widgettitle">',
       
    54           'after_title' => '</h2>',
       
    55         )
       
    56     );   
       
    57  register_sidebar
       
    58     (   array
       
    59         (
       
    60           'name' => 'Bottom-Right',
       
    61           'before_widget' => '<div class="bottombar">',
       
    62           'after_widget' => '</div>',
       
    63           'before_title' => '<h2 class="widgettitle">',
       
    64           'after_title' => '</h2>',
       
    65         )
       
    66     );   
       
    67 }
       
    68 
       
    69 # Displays a list of categories
       
    70 function dp_list_categories($Eparam='') {
       
    71 
       
    72 	$categories = get_categories('hide_empty=1'.$Eparam);
       
    73 	$first = true;
       
    74 	$count = 0;
       
    75 	
       
    76 	foreach ($categories as $category) {
       
    77 
       
    78 		if ($category->parent<1) {
       
    79 						
       
    80 			echo ('<li class="menu-h-li">');
       
    81 			echo ('<a href="'.get_category_link($category->cat_ID).'">'.$category->name.' </a>');
       
    82 			echo '<ul>';
       
    83 			
       
    84 		
       
    85 			$category_posts = get_posts('numberposts=15&category='.$category->cat_ID);
       
    86 			$count2 = 0;
       
    87 				 //echo("<!-- ".sizeof($category_posts)." -->");
       
    88 					foreach($category_posts as $post) {
       
    89 						
       
    90 						echo '<li class="menu-h-li2" title="'.cache_cutter($post->post_title).'"><a href="'.$post->guid.'">'.text_cutter($post->post_title,200).'</a></li>';
       
    91 						//echo '<li class="menu-h-li2" title="'.$count2.'"><a href="'.$post->guid.'">'.$post->post_title.'</a></li>';
       
    92 						$count2++; //if ($count2>6) break;
       
    93 					};
       
    94 					
       
    95 
       
    96 				echo "</ul></li>";
       
    97 			
       
    98 		}
       
    99 		$count++; //if ($count>6) break; // limit to 5
       
   100 		
       
   101 	}
       
   102 }
       
   103 
       
   104 # Display the list of movies
       
   105 function listeMyLdt(){
       
   106 
       
   107 	$MyLdtDataBase 	= "lignesIRI";
       
   108 	$MyWPDataBase 	= "wordpress";
       
   109 	$MyLdtTable 	= "Lignes";
       
   110 	$MyLdtconnexion = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
       
   111 	$MyLdtListe		= "";
       
   112 	//$WP_db_selected    = mysql_select_db();
       
   113 	
       
   114 	if (!$MyLdtconnexion) {
       
   115 	   echo "Impossible de se connecter à la base de données : " . mysql_error();
       
   116 	   exit;
       
   117 	}
       
   118 
       
   119 	if (!mysql_select_db($MyLdtDataBase)) {
       
   120 	   echo "Impossible de sélectionner la base mydbname : " . mysql_error();
       
   121 	   exit;
       
   122 	}
       
   123 
       
   124 	$MyLdtSql = "SELECT * FROM  ".$MyLdtTable." ORDER BY `Lignes`.`Auteur` ASC";// WHERE `NomFichier` NOT LIKE ''";
       
   125 
       
   126 	$result = mysql_query($MyLdtSql);
       
   127 
       
   128 	if (!$result) {
       
   129 	   echo "Impossible d'exécuter la requête ($MyLdtSql) dans la base : " . mysql_error();
       
   130 	   exit;
       
   131 	}
       
   132 
       
   133 	if (mysql_num_rows($result) == 0) {
       
   134 	   echo "Aucune ligne trouvée, rien à afficher.";
       
   135 	   exit;
       
   136 	}
       
   137 	$MyLdtTable="";
       
   138 	while ($row = mysql_fetch_assoc($result)) {
       
   139 	   $MyLdtLineTemplate = "<tr class='rollover'>
       
   140 							 <td>".$row['Auteur']."</td> <td>".$row['Titre']."</td>  <td>".$row['Contributeur']."</td> ";
       
   141 							 
       
   142 		if ($row['NomFichier']!="") {
       
   143 			$MyLdtLineTemplate .= "<td> <a href='?URL=".$row['NomFichier']."' title='Cliquez ici pour télécharger le projet".$row['NomFichier']."'> <img src='".get_bloginfo('stylesheet_directory')."/images/get.png'/> </a> </td> </tr>";
       
   144 		}else {
       
   145 			$MyLdtLineTemplate .= "<td> <a href='#' title='Fichier non disponible'> <img src='".get_bloginfo('stylesheet_directory')."/images/help.png'/> </a> </td> </tr>";
       
   146 		}
       
   147 		
       
   148 	   $MyLdtTable .=  $MyLdtLineTemplate."\n";
       
   149 	   
       
   150 	}
       
   151 	
       
   152 	//  tableau
       
   153 	$MyLdtOutPut = "
       
   154 	<div class='box'>
       
   155 		<table style='width:100%;'>
       
   156 			<thead>
       
   157 				<tr>
       
   158 					<td class='thead'> <b>Auteur</b></td>
       
   159 					<td class='thead'> <b>Titre</b> </td>
       
   160 					<td class='thead'> <b>Contributeur(s)</b> </td>
       
   161 					<td class='thead'> </td>
       
   162 				</tr> 
       
   163 			</thead>
       
   164 			".$MyLdtTable."
       
   165 		</table></div>";
       
   166 	
       
   167 	echo($MyLdtOutPut);
       
   168 	
       
   169 	mysql_free_result($result);
       
   170 	mysql_select_db($MyWPDataBase);
       
   171 	//mysql_select_db($WP_db_selected);
       
   172 }
       
   173 
       
   174 # Create the  LDT's ZIP to download IT 
       
   175 function createMyLdtProject($fichierIRI){
       
   176 
       
   177 	$MyRoot   	= "/iridata/users/hurons/public_html/DOCUMENT/_LDT";
       
   178 	$MyRootRel  = "non-classe/telecharger/?URL=DOCUMENT/_LDT";
       
   179 	$MyCache	= "/CACHE";
       
   180 	$MyExemple 	= "/EXEMPLE_zip";
       
   181 	$MyPattern  = "X_NOMPROJET_X";
       
   182 	$MyLdtName 	= "projet_".$MyPattern.".ldt";
       
   183 	$MyReadMeName="LISEZ_MOI_READ_ME.txt";
       
   184 	$MySoundExt = "_audio.xml";
       
   185 	$Return 	= "";
       
   186 	
       
   187 	$end 		= strrpos($fichierIRI,"."); 
       
   188 	$FileName	= substr($fichierIRI, 0, $end);
       
   189 	$ext		= substr($fichierIRI, $end, strlen($FileName));
       
   190 	
       
   191 	
       
   192 	// cette fontion n'est utilisé que sur les .IRI
       
   193 	if ($ext==".iri"){
       
   194 	
       
   195 		// Vérifie si le cache éxiste déja
       
   196 		if (file_exists($MyCache."/".$FileName.".zip")){
       
   197 			//echo ($MyCache.$FileName.".zip"." is in cache");
       
   198 			// ensuite envoyer vers la redirection.
       
   199 			redirectionDWL($MyRootRel.$MyCache."/".$FileName.".zip");
       
   200 			
       
   201 		} else {
       
   202 		
       
   203 			echo ("Le fichier ".$FileName.".zip est en cours de compression, veuillez patientez un instant.");
       
   204 					
       
   205 			// Créer le .LDT à partir de l'exemple
       
   206 			$MyLdtExemple = implode(file($MyRoot.$MyExemple."/".$MyLdtName , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
       
   207 			$MyFuturLdt = str_replace($MyPattern, $FileName, $MyLdtExemple);
       
   208 		
       
   209 			// Créer le read me 
       
   210 			$MyReadMeExemple = implode(file($MyRoot.$MyExemple."/".$MyPattern."/".$MyReadMeName , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
       
   211 			$MyFuturReadMe = str_replace($MyPattern, $FileName, $MyReadMeExemple);
       
   212 			
       
   213 			// ZIP l'ensemble est met dans le cache
       
   214 			$Zipfilename = $MyRoot.$MyCache."/".$FileName.".zip";
       
   215 			$zip = new ZipArchive();
       
   216 			if ($zip->open($Zipfilename, ZIPARCHIVE::CREATE)!==TRUE) {
       
   217 				exit("Impossible d'ouvrir <$filename>\n");
       
   218 			}		
       
   219 			// ZIP - ajouter le .LDT 
       
   220 			$MyLdtNewName = str_replace($MyPattern, $FileName, $MyLdtName);
       
   221 			$zip->addFromString($MyLdtNewName, $MyFuturLdt); //$zip->addFile($MyFuturLdtName, $FileName);
       
   222 			// ZIP - ajouter le .IRI 
       
   223 			$zip->addFile($MyRoot."/".$fichierIRI, $FileName."/".$fichierIRI); 
       
   224 			// ZIP - ajouter le ReadMe
       
   225 			$zip->addFromString($FileName."/".$MyReadMeName, $MyFuturReadMe); //$zip->addFile($MyRoot.$MyTemp."/".$FileName."/".$MyReadMeName, $FileName."/".$MyReadMeName); 
       
   226 			// ZIP - ajouter le audio si il y a 
       
   227 			$fileAudio	= $MyRoot."/".$FileName.$MySoundExt ;
       
   228 			if (file_exists($fileAudio)){
       
   229 				$zip->addFile($fileAudio, $FileName."/".$FileName.$MySoundExt); 
       
   230 			}
       
   231 			$zip->close();
       
   232 			
       
   233 			// mettre a jour la bdd ? pour vider le cache quand c'est un nouveau fichier ? 
       
   234 			
       
   235 			// ensuite envoyer vers la redirection.
       
   236 			redirectionDWL($MyRootRel.$MyCache."/".$FileName.".zip");
       
   237 		}
       
   238 	}
       
   239 }
       
   240 
       
   241 # Javascript DWL redirection 
       
   242 function redirectionDWL ($url) {
       
   243 
       
   244      $URL = get_bloginfo('url')."/".$url; 
       
   245 	 
       
   246 	 echo("Le téléchargement est en cours... <br/>\n
       
   247     Si le téléchargement ne démarre pas automatiquement, veuillez cliquez <a href=".$URL.">ici</a>.<br/>\n");
       
   248 	 
       
   249      echo("<script  language=\"javascript\" type=\"text/javascript\"> \n");
       
   250      echo("     <!--\n");
       
   251      echo("    document.location.href = \"".$URL."\";\n ");
       
   252      echo("    window.location = \"".$URL."\";\n ");
       
   253      echo("     //-->\n");
       
   254      echo("</script>\n");
       
   255 	 
       
   256 }
       
   257 
       
   258 # Text cutter
       
   259 function text_cutter($text,$length){
       
   260 	$symbole="<!-- cut -->";
       
   261 	
       
   262 	$lastpos = strrpos($text,$symbole); 
       
   263 	
       
   264 	if($lastpos!=FALSE){
       
   265 	
       
   266 		return (substr($text, 0, $lastpos));
       
   267 	
       
   268 	} else {
       
   269 	
       
   270 		if(strlen($text)>$length){
       
   271 			return (substr($text, 0, $length)."[...]");
       
   272 		} else {
       
   273 			return ($text);
       
   274 		}
       
   275 	}
       
   276 }
       
   277 
       
   278 # Cache cutter
       
   279 function cache_cutter($text){
       
   280 	return (str_replace( "<!-- cut -->", "", $text));
       
   281 }
       
   282 
       
   283 
       
   284 function dp_list_posts($Eparam='') {
       
   285 
       
   286 		$category_posts = get_posts($Eparam);
       
   287 		
       
   288 		foreach($category_posts as $post) {
       
   289 			echo '<li><a href="'.$post->guid.'">'.$post->post_title.'</a></li>';
       
   290 		};
       
   291 }
       
   292 
       
   293 
       
   294 /*
       
   295 Plugin Name: Get The Image
       
   296 Plugin URI: http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
       
   297 Description: This is a highly intuitive script that gets an image either by custom field input or post attachment.
       
   298 Version: 0.1 Beta
       
   299 Author: Justin Tadlock
       
   300 Author URI: http://justintadlock.com
       
   301 License: GPL
       
   302 */
       
   303 
       
   304 /***********************************************************
       
   305 Catchall function for getting images
       
   306 ***********************************************************/
       
   307 function get_the_image($arr = false, $default_size = 'medium', $default_img = false) {
       
   308 	global $post;
       
   309 	$cf_array = load_the_image($arr, $post, $default_size);
       
   310 	$image = display_the_image($cf_array, $post, $default_size, $default_img);
       
   311 	if($image == false) $image = '<!-- No images were added to this post. -->';
       
   312 	return $image;
       
   313 }
       
   314 
       
   315 /***********************************************************
       
   316 Catchall function for getting images with a link
       
   317 ***********************************************************/
       
   318 function get_the_image_link($arr = false, $default_size = 'medium', $default_img = false) {
       
   319 	global $post;
       
   320 	$cf_array = load_the_image($arr, $post, $default_size);
       
   321 	$image = display_the_image($cf_array, $post, $default_size, $default_img);
       
   322 
       
   323 	if($image == false) :
       
   324 		$image_link = '<!-- No images were added to this post. -->';
       
   325 	else :
       
   326 		$post_perm = get_permalink($post->ID);
       
   327 		$image_link = "<a href=\"$post_perm\" title=\"$post->post_title\">$image</a>";
       
   328 	endif;
       
   329 	return $image_link;
       
   330 }
       
   331 
       
   332 /***********************************************************
       
   333 Function for loading an image
       
   334 ***********************************************************/
       
   335 function load_the_image($custom_fields = false, $en_post, $default_size) {
       
   336 
       
   337 // Checks only if there are custom fields to check for
       
   338 	if(isset($custom_fields)) {
       
   339 
       
   340 	// Loop through the custom fields, checking for images or video
       
   341 		$i = 0;
       
   342 		while(strcmp($image[0],'') == 0 && $i <= sizeof($custom_fields)) {
       
   343 
       
   344 		// Check custom field values for image, image alt text, and image class
       
   345 			$image = get_post_custom_values($key = $custom_fields[$i]);
       
   346 			$image_alt = get_post_custom_values($key = $custom_fields[$i] . ' Alt');
       
   347 			$image_class = get_post_custom_values($key = $custom_fields[$i] . ' Class');
       
   348 		// Convert custom field key name to image class
       
   349 			$img_class = $custom_fields[$i];
       
   350 			$img_class = strtolower($img_class);
       
   351 			$img_class = str_replace (" ", "-", $img_class);
       
   352 
       
   353 		// Add space to image class if user inputs an extra class
       
   354 			if($image_class == true) $image_class .= ' ';
       
   355 
       
   356 		// Add user image class to default image classes
       
   357 			if($default_size == 'thumbnail' && $img_class == 'thumbnail') $image_class .= $default_size;
       
   358 			elseif($default_size == 'medium' && $img_class == 'medium') $image_class .= $default_size;
       
   359 			elseif($default_size == 'full' && $img_class == 'full') $image_class .= $default_size;
       
   360 			else $image_class .= $img_class . ' ' . $default_size;
       
   361 
       
   362 		$i++;
       
   363 		} // End while loop
       
   364 	} // End check for custom field image
       
   365 
       
   366 // If there is no image set through custom fields, check post attachments
       
   367 	if($image == false && $default_size == true) {
       
   368 		$img_att_arr = find_attachment_image($custom_fields[0], $en_post, $default_size);
       
   369 		if(strcmp($image_att_arr[0],'') == 0) :
       
   370 			$image = $img_att_arr[0];
       
   371 			$image_class = $img_att_arr[1];
       
   372 			$image_alt = false;
       
   373 		else :
       
   374 			$image = false; $image_alt = false; $image_class = false;
       
   375 		endif;
       
   376 	}
       
   377 
       
   378 // Return array with an image, image alt, and image class
       
   379 	return array($image, $image_alt, $image_class);
       
   380 }
       
   381 
       
   382 /***********************************************************
       
   383 Function for displaying an image
       
   384 ***********************************************************/
       
   385 function display_the_image($cf_array = false, $en_post = false, $default_size = false, $default_img = false) {
       
   386 
       
   387 // Set nice names for image info
       
   388 	if($cf_array[0] == false && $default_img == true) :
       
   389 		$image[0] = $default_img;
       
   390 		$image_class = $default_size;
       
   391 	else :
       
   392 		$image = $cf_array[0];
       
   393 		$image_alt = $cf_array[1];
       
   394 		$image_class = $cf_array[2];
       
   395 	endif;
       
   396 
       
   397 // If there's any kind of image for this post
       
   398 	if(isset($image[0]) && strcmp($image[0],'') != 0) :
       
   399 	// Open img tag
       
   400 		$output = '<img src="'.$image[0].'"';
       
   401 		$output .= ' alt="';
       
   402 	// Image alt text
       
   403 		if(isset($image_alt[0]) && strcmp($image_alt[0],'') != 0) $output .= $image_alt[0];
       
   404 		else $output .= $en_post->post_title;
       
   405 	// Image class
       
   406 		$output .= '" class="';
       
   407 		if(isset($image_class[0])) $output .= $image_class;
       
   408 		else $output .= 'left';
       
   409 	// Close img tag
       
   410 		$output .= '" />';
       
   411 // If there's no image
       
   412 	else :
       
   413 		$output = false;
       
   414 	endif;
       
   415 // Return the image
       
   416 	return $output;
       
   417 }
       
   418 
       
   419 /***********************************************************
       
   420 Function for finding an attachment image.
       
   421 Only called if no custom field images are set.
       
   422 ***********************************************************/
       
   423 function find_attachment_image($custom_fields = false, $en_post = false, $default_size = 'medium') {
       
   424 
       
   425 	$custom = $custom_fields;
       
   426 	$custom = strtolower($custom);
       
   427 	$custom = str_replace (" ", "-", $custom);
       
   428 // Don't repeat the same class name
       
   429 	if($custom == 'thumbnail' || $custom == 'medium' || $custom == 'full') $img_class = $default_size;
       
   430 	else $img_class = $custom . ' ' . $default_size;
       
   431 
       
   432 	$attachments = get_children("post_parent=$en_post->ID&post_type=attachment&post_mime_type=image&orderby=\"menu_order ASC, ID ASC\"&numberposts=1");
       
   433 	if($attachments == true) :
       
   434 		foreach($attachments as $id => $attachment) :
       
   435 			$img = wp_get_attachment_image_src($id, $default_size);
       
   436 			$img_arr[0] = $img;
       
   437 		endforeach;
       
   438 		$img_arr[1] = $img_class;
       
   439 	else :
       
   440 		$img_arr = false;
       
   441 	endif;
       
   442 	return $img_arr;
       
   443 }
       
   444 ?>