Archive for the ‘source-code’ Category

Lecture de fichier “LIT” sous Mac

Vendredi, mai 29th, 2009

Après un coup d’oeil rapide je n’ai pas trouvé de moyen de lire directement des livres électroniques au format “LIT” directement sur MAC.

Par contre, un utilitaire (binaire) en combinaison avec un petit script (applescript) font la conversion des “LIT” en format “HTML” que vous pouvez par la suite lire avec Safari ou votre navigateur préféré.

Pour obtenir plus de détails consulter les URL en commentaire dans le script suivant

(* script source : http://www.macosxhints.com/article.php?story=20060716020829624 *)
(* lit conv src : http://www.convertlit.com/index.php *)

property type_list : {}
property extension_list : {"lit"}
on open these_items
	repeat with i from 1 to the count of these_items
		set this_item to item i of these_items
		set the item_info to info for this_item
		if (folder of the item_info is false) and Â
			(alias of the item_info is false) and Â
			((the file type of the item_info is in the type_list) or Â
				the name extension of the item_info is in the extension_list) then
			process_item(this_item)
		end if
	end repeat
end open

on process_item(this_item)
	set file_path to quoted form of POSIX path of this_item
	set this_item to this_item as alias
	set file_info to info for this_item
	if name of file_info does not end with ".lit" then
		display dialog "This application only works on Microsoft .lit files that adhere to appropriate naming conventions." buttons {"Ok"} default button 1
		error number -128
	end if
	-- REMOVE THE DASHES BELOW THIS LINE
	do shell script "/bin/c-l-i-t " & file_path & " " & file_path & "_Open_eBook/"

end process_item

support Mercurial sous TextMate

Mercredi, mai 13th, 2009

Un “Bundle” existe sous TextMate (le super éditeur Mac) pour gérer les sources sous mercurial.  La façon simple de l’installer est d’exécuter le script ci-dessous qui proviens d’ici où vous retrouvez aussi une liste de Bundle disponibles.   


 

#!/bin/sh

LC_CTYPE=en_US.UTF-8
SVN=`which svn`

echo Changing to Bundles directory...
mkdir -p /Library/Application\ Support/TextMate/Bundles
cd /Library/Application\ Support/TextMate/Bundles

if [ -d /Library/Application\ Support/TextMate/Bundles/Mercurial.tmbundle ]; then
	echo Mercurial bundle already exists - updating...
	$SVN up "Mercurial.tmbundle"
else
	echo Checking out Mercurial bundle...
	$SVN --username anon --password anon co http://macromates.com/svn/Bundles/trunk/Bundles/Mercurial.tmbundle/
fi

echo Reloading bundles in TextMate...
osascript -e 'tell app "TextMate" to reload bundles'

PDF Half page splitting

Jeudi, février 26th, 2009

I’ve found this interresting script digging around.  As I’ve a lot of old book got scanned 2 / 1 pages and I don’t found then easy to read on my portable device I think this could be useful.

#!/bin/bash
#script:    unpnup
#        This script reformats pdf files where two portait pages
#        are joined on one landscape page (like pnup does). After
#        completion each page is on one single fullframe page
#         Copyright (c) 2007 F5 GmbH
# Author: Harald Hackenberg <hackenberggmx.at>
#
#programs required:
#           pdftk, pdftops, poster, epstopdf
#
#
#
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
# the full text of the license.
if [ "$1" == "" ] || [ "$1" == "--help" ]; then
echo
echo “Usage: `basename $0` filename”
echo
else

mkdir tmp
cd tmp
pdftk “$1″ burst
for file in pg*.pdf;
do
pdftops -eps $file
poster -v -pA4 -mA5 -c0% `basename $file .pdf`.eps > `basename $file .pdf`.tps
epstopdf `basename $file .pdf`.tps
done
pdftk pg*.pdf cat output ../`basename $1 .pdf`_unpnuped.pdf
rm *
cd ..
rmdir tmp
fi

Building PNG Images

Jeudi, septembre 18th, 2008

Did you ever try to build a png image (with a programming language, not an image editor)?  A lot of code sample are available on the internet.  But most if not all are using libpng or some high level libraries like gd.

But what happend if you can’t install libraries on the system where you’re working?  Ever thing about building an image from scratch?  Here is what I do in PHP.

This isn’t the most performant code, but at least it generate a valid PNG image without any non standard libraries.   (Some one could say that zlib or crc are php external libs.. but I haven’t see any php distribution whitout them in a while!)

&lt;?php
/*
* Title: PNG Image Generation
* Author: Jean-Luc Cyr
* Date: 2008-09-17
* License: GNU GPL2
* Description: Generate a simple PNG image
* directly in PHP without using external libs
* like GD.
*/
 
header("content-type: image/png");
 
function long2string($long)
{
$string = chr(($long&amp;0xFF000000)&gt;&gt;24).
chr(($long&amp;0xFF0000)&gt;&gt;16).
chr(($long&amp;0xFF00)&gt;&gt;8).
chr(($long&amp;0xFF)&gt;&gt;0);
return $string;
} // long2string()
function build_png($matrix, $w, $h)
{
////////////////////////////////////////////////////////////////
//PNG Header
$image = chr(0x89).chr(0x50).chr(0x4E).chr(0x47);
$image .= chr(0x0D).chr(0x0A).chr(0x1A).chr(0x0A);
 
////////////////////////////////////////////////////////////////
//Header Chunk
 
//Chunk type
$chunk = "IHDR";
//Chunk data
//Width
$data = long2string($w);
//Height
$data .= long2string($h);
//Depth
$data .= chr(8); //Bits per sample
//Color Type
$data .= chr(2); //RGB
//Compression method
$data .= chr(0);
//Filter method
$data .= chr(0);
//Interlaced method
$data .= chr(0);
 
$crc = crc32($chunk.$data);
//Add length at start
$chunk = long2string(strlen($data)).$chunk.$data;
//Add CRC at the end
//Dump chunk
$chunk = $chunk.long2string($crc);
$image .= $chunk;
 
////////////////////////////////////////////////////////////////
//Data Chunk
 
//Chunk type
$chunk = "IDAT";
//Data (2x2 rgb 8bits)
$s = 3; //samples per pixels
#$matrix  ="\0\0\0\0\0\0\0\0\0";
#$matrix .="\0\0\0\0\0\0\0\0\0";
#$matrix .="\0\0\0\0\0\0\0\0\0";
// No filter so add 0 to beginning of each scan line
$datas = '';
for($c=0; $c&lt;$h; $c++)
$datas .= "\0". substr($matrix,$c*($w*$s),($w*$s));
// Compress Data
#$zdata = gzencode($data,9,FORCE_DEFLATE);
#$zdata = gzdeflate($data,0);
$zdata = gzcompress($datas,0);
// Calculate CRC
$crc = crc32($chunk.$zdata);
// Add length at start
$l = strlen($zdata);
$chunk = long2string($l).$chunk.$zdata;
// Add CRC at the end
$chunk = $chunk.long2string($crc);
$image .= $chunk;
 
////////////////////////////////////////////////////////////////
//End Chunk
 
// Chunk type
$chunk = "IEND";
// Data
$data = "";
// Calculate CRC
$crc = crc32($chunk);
// Add length at start
$chunk = long2string(strlen($data)).$chunk.$data;
// Add CRC at the end
$chunk = $chunk.long2string($crc);
$image .= $chunk;
 
// Send out the image
echo $image;die();
} // build_png()
////////////////////////////////////////////////////////////////
// Demo code
 
// Build a simple image matrix
// 256 x 256 x 8rgb
 
$w=256; // width
$h=256; // height
$s=3;   // 3 x 8 bits samples per pixel (8bits rgb)
for($x=0; $x&lt;$h; $x++)
for($y=0; $y&lt;$w; $y++)
//    for($z=0; $z&lt;$s; $z++) //Ordered R G B
$matrix .=
chr( ( (($y/64)&lt;1) &amp;&amp; (($y/64)&gt;0) )?$x:0 ). // R
chr( ( (($y/64)&lt;2) &amp;&amp; (($y/64)&gt;1) )?$x:0 ). // G
chr( ( (($y/64)&lt;3) &amp;&amp; (($y/64)&gt;2) )?$x:0 ); // B
 
build_png($matrix,$w,$h);
die();
 
////////////////////////////////////////////////////////////////
// Hex dump image for debugging
 
for ($x=0; $x&lt;strlen($image); $x++) {
printf("%02X ",ord($image[$x]));
if ((($x+1)%16)==0)
printf("&lt;br/&gt;");
}

PHP Post

Lundi, avril 28th, 2008

Here a quick example of how-to post some data over the web in a simple php script.

<?php
$datastream = array(
'short_msg'=&gt;'this is a simple message',
'address'=&gt;'98887996662',
);
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, <a href="http://psms.canoe-inc.com/send_sms" class="moz-txt-link-rfc2396E">"http://psms.canoe-inc.com/send_sms"</a>);
$o="";
foreach ($datastream as $k=&gt;$v)
{
$o.= "$k=".utf8_encode($v)."&amp;";
}
$post_data=substr($o,0,-1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);

ASN.1/DER/BER PHP

Mercredi, février 13th, 2008

Je vous ai parlé dernièrement de ASN.1. Voici un petit parseur de base en PHP

 
//Title: Simple PHP BER/DER/ASN.1 basic decoder
//Author: Jean-Luc Cyr
//Date: 2008-02-13
//Desc: Simple BER Printable string dumper
//	read BER data from a file
 
// Set input filename
$filename = 'o';
// Set max number of tag to parse (0 = no limit)
$limit = 0;
 
//Open data file
$f = fopen($filename,'rb');
//Set read tag number to 0
$c=0;
//While not end of file
while(!feof($f))
{
    //Read first block data type
    $type = ord(fread($f,1));
    if ($type==0)
      break;
    //Read first len block
    $len = ord(fread($f,1));
    if ($len==0)
      break;
    $le = 0;
    //If first bit of len is set (1)
    //we have a multiple bit len to read
    if (($len&amp;128)==128)
    {
        echo "Big Len ".($len&amp;127)." Bytes\r\n";
        for ($i = 0; $i &lt; ($len&amp;127); $i++)
          $le = $le * 256 + ord(fread($f,1));
        $len = $le;
    }
    //Findout data type (first 2 bits)
    $cl = ($type &amp; (128+64) ) &gt;&gt; 6;
    switch($cl)
    {
     case 0:
        $cla = 'Universal';
        break;
     case 1:
        $cla = 'application';
        break;
     case 2:
        $cla = 'context-specific';
        break;
     case 3:
        $cla = 'private';
        break;
    }
    //Dump some info
    printf("[$cla] Type: %x (%d), len: %d\r\n",$type,$type,$len);
    //Read data chunk
    $data = fread($f,$len);
    //Display data chunk based on type
    switch($type)
    {
     case 2: // integer
        break;
     case 3: // bit string
        break;
     case 4: // octet string
        break;
     case 5: // null
        break;
     case 6: // object identifier
        break;
     case 16: // sequence and sequence of
        break;
     case 17: // set and set of
        break;
     case 19: //string
        printf("Data: $data\r\n");
        break;
     case 20: // t61string
        break;
     case 22: // ia5string
        break;
     case 23: // utctime
        break;
     default:
        #printf("Data: $data\r\n");
        break;
    }
    //increment count
    $c++;
    //check if we reach the number of tag specified
    if (($c&gt;$limit)&amp;&amp;($limit&gt;0)) break;
}
 
//close our input file
fclose($f);

xmlrpc client (2) - php

Mercredi, juin 22nd, 2005
#!/usr/local/php4/bin/php
 
include "xmlrpc-1.0.99.2/xmlrpc.inc";
 
//First attempt to use XML_RPC calls
//assert( $c=new xmlrpc_client("/", "sundev02.grafsoft.com", 8888) );
assert( $c=new xmlrpc_client("/~jlcyr/rpc_server.php", "192.168.2.21", 80) );
$c-&gt;setDebug(1);//never set it to 1 on an agi script..!!
 
////////////////////
// Envoie d'un fichier binaire
/*
$fn = "01.mp3";
$fich = fopen($fn,"rb");
$select = fread($fich,filesize($fn));
echo "filesize:".filesize($fn)."\n";
fclose($fich);
assert( $f=new xmlrpcmsg('doc.putDoc',array(new xmlrpcval($select,"base64"),new xmlrpcval($fn,"string"))) );
assert( $r=$c-&gt;send($f) );
$v=$r-&gt;value();
if ($r-&gt;faultCode())
  {
    //Insert error flag and message into db
    print "Fault: ";
    print "Code: " . $r-&gt;faultCode() . " Reason '" .$r-&gt;faultString()."'
";
  }
else
  {
    //Dump received return value
    print $v-&gt;scalarval();
  }
*/
 
//////////////////
// Demande des infos du RIS
//assert( $f=new xmlrpcmsg('ris.getCMD',array(new xmlrpcval("9999999","string"),new xmlrpcval("2","string"))) );
assert( $f=new xmlrpcmsg('doc.getDocInfo',array(new xmlrpcval("902","string"),
					    new xmlrpcval("fileset200525","string"),
					    new xmlrpcval("filename1.2.3","string"))));
assert( $r=$c-&gt;send($f) );
$v=$r-&gt;value();
if ($r-&gt;faultCode())
  {
    //Insert error flag and message into db
    print "Fault: ";
    print "Code: " . $r-&gt;faultCode() . " Reason '" .$r-&gt;faultString()."'
";
  }
else
  {
    //Dump received return value
    while (list($key,$item)=$v-&gt;structeach()) {
      print $key."=".$item-&gt;scalarval()."\n";
    }
  }
 
?&gt;

Serveur XML RPC (2) - PHP

Mercredi, juin 22nd, 2005

<?php // Title: Example de serveur XML-RPC en php // Version: 0.2 // Author: Jean-Luc Cyr  // Desc: Replacement for python xml-rpc server that handle //       imagem phone dictation system and audio-context calls.  // Exemple avec l'extension disponible // http://phpxmlrpc.sourceforge.net/  include "xmlrpc-1.0.99.2/xmlrpc.inc"; include "xmlrpc-1.0.99.2/xmlrpcs.inc";  // Declare global vars  //Setup parameters //Database user name $duser = "transcription"; //$duser = "DICTWEB"; //Database password //$dpass = "password"; $dpass = "imagemsoft"; //Database tnsname //$dhost = "interweb"; $dhost = "rcorcl"; //Document base path //$base_base = "/export/home/dictedoc/"; $base_path = "/export/home/jlcyr/tmp/";  $db = oci_pconnect($duser,$dpass,$dhost);       // Declare rpc_server possible methods $s=new xmlrpc_server( array("doc.putDoc" => array ("function" => "doc_putDoc"), 			    "doc.getDocInfo" => array ("function" => "doc_getDocInfo"), 			    "doc.createDoc" => array ("function" => "doc_createDoc"), 			    "doc.getUID" => array ("function" => "doc_getUID"), 			    "doc.updateDOC" => array ("function" => "doc_updateDoc"), 			    "doc.help" => array ("function" => "doc_help"), 			    "ris.getBirth" => array("function" => "risCMD_getBirth"), 			    "ris.getCMD" => array("function" => "risCMD_getCMD"), 			    "ris.help" => array("function" => "risCMD_help"), 			    "sql.execSql" => array("function" => "sql_execSql"), 			    "sql.getInst" => array ("function" => "sql_getInst"), 			    "sql.help" => array ("function" => "sql_help") 			    ));  oci_close($db);  // Don't know why, but seen we can't pass class method to xmlrpc_server array of function // so i've declared stand alone functions  ///////////////////////////////////////////////////////////////////////////////////////// // doc_putDoc invoqué via le web sous doc.putDoc // args: 1-Le contenu du document urlencoded //       2-Le path du media file set a concatener avec le root (global $base_path) //       3-Le nom du fichier à créer // retour: 0 function doc_putDoc($params)//($doc, $path, $filename) {   global $base_path;    $doc=$params->getParam(0)->scalarval();   $path=$params->getParam(1)->scalarval();   $filename=$params->getParam(2)->scalarval();    //Est-ce que le répertoir /export/home/dictedoc/$path existe?   if (!file_exists($base_path.$path))     {       //Si non le créer et le mettre chmod 777       mkdir($base_path."/".$path,0777,true);       chmod($base_path."/".$path,0777);     }    //Enregistrer le document $doc sous /export/home/dictedoc/$path/$filename   $f = fopen($base_path."/".$path."/".$filename,"w");   $data = urldecode($doc);   fwrite($f,$data);   fclose($f);   chmod($base_path."/".$path."/".$filename,0777);    return new xmlrpcresp(new xmlrpcval(0,"integer")); }  ///////////////////////////////////////////////////////////////////////////////////////// // doc_getDocInfo invoqué via le web sous doc.getDocInfo // args: le dd_id // retour: toutes les colonnes function doc_getDocInfo($params)//($info) {   global $db;   if ($db==false)       return new xmlrpcresp(new xmlrpcval("Error connection to db ","string"));    $info =$params->getParam(0)->scalarval();   $select = "select * from dictation_document where dd_id='$info'";   $arr = array();   //Execute select, return result values 1,2,3 in uid, id, fileset   $stmt = oci_parse($db,$select);   if (oci_execute($stmt,OCI_DEFAULT))     {       if (oci_fetch($stmt)) 	{ 	  $ncols = OCINumCols($stmt); 	  for ($i = 1; $i <= $ncols; $i++) { 	    $column_name  = OCIColumnName($stmt, $i); 	    $column_type  = OCIColumnType($stmt, $i); 	    $column_size  = OCIColumnSize($stmt, $i); 	    $column_data = OCIResult($stmt, $i);     	    $arr = $arr + array($column_name => new xmlrpcval($column_data,"string")); 	  } 	}       else 	{ 	  return new xmlrpcresp(new xmlrpcval("Error fetching result ","string")); 	}     }   else     {       return new xmlrpcresp(new xmlrpcval("Error executing select ".$select,"string"));     }    return new xmlrpcresp(new xmlrpcval($arr, "struct")); }  ///////////////////////////////////////////////////////////////////////////////////////// // args: le select à exécuter // retour: string "Doc Created" function doc_createDoc($params)//($select) {   $select = $params->getParam(0)->scalarval();    global $db;   if ($db==false)       return new xmlrpcresp(new xmlrpcval("Error connection to db ","string"));    //Execute select, return result values 1,2,3 in uid, id, fileset   $stmt = oci_parse($db,$select);   if (oci_execute($stmt,OCI_DEFAULT))     {       oci_commit($db);     }    //Executer le select dans $select    return new xmlrpcresp(new xmlrpcval("Doc Created","string")); }  ///////////////////////////////////////////////////////////////////////////////////////// // args: un service id // retour: struct: uid, id et fileset pour créer un nouveau document function doc_getUID($params)//($seruid) {   global $db;   if ($db==false)       return new xmlrpcresp(new xmlrpcval("Error connection to db ","string"));    $seruid = $params->getParam(0)->scalarval();    $select = "select '2.16.124.10.2.1.3.100.' ".     "||to_char(sysdate,'YYYYMMDD')||'.'".     "||to_char(to_number(to_char(to_date('010203','HH24MISS'),'HH24MISS')))||'.'".     "||(select CE_VALUE from config_env where CE_ITEM_CONF='INSTITUTION')||'.'".     "||SQ_DICTATION_UID.NEXTVAL,".     "to_char(SQ_DICTATION_UID.NEXTVAL), ".     "(select DS_NAME from DICTATION_SERVICE where DS_ID='$seruid')||'/'||to_char(sysdate,'YYYYIW') from dual";    //Execute select, return result values 1,2,3 in uid, id, fileset   $stmt = oci_parse($db,$select);   if (oci_execute($stmt,OCI_DEFAULT))     {       if (oci_fetch($stmt)) 	{ 	  $uid = oci_result($stmt,1); 	  $id = oci_result($stmt,2); 	  $fileset = oci_result($stmt,3); 	  //oci_commit($this->link); 	}       else 	{ 	  return new xmlrpcresp(new xmlrpcval("Error fetching result ","string")); 	}     }   else     {       return new xmlrpcresp(new xmlrpcval("Error executing select ".$select,"string"));     }    $arr = array( "uid" => new xmlrpcval($uid,"string"), 		"id" => new xmlrpcval($id,"string"), 		"fileset" => new xmlrpcval($fileset,"string") );    return new xmlrpcresp(new xmlrpcval($arr, "struct")); }  ///////////////////////////////////////////////////////////////////////////////////////// // Not done function doc_updateDoc() {   return new xmlrpcresp(new xmlrpcval("Doc Updated","string")); }  ///////////////////////////////////////////////////////////////////////////////////////// // doc_help invoqué via le web sous doc.help // args: aucun // retour: 1-liste des méthodes de l'objet web doc function doc_help() {   return new xmlrpcresp(new xmlrpcval("putDoc, getDocInfo, createDoc, getUID, updateDoc", "string")); }  ///////////////////////////////////////////////////////////////////////////////////////// // args: un patient id // retour: la date de naissance sous forme de string ou 0 si le patient n'existe pas function risCMD_getBirth($params)//($patid) {   $patid =$params->getParam(0)->scalarval();    $select = "select * from WEB.PATIENT where PAT_ID='$patid'";    global $db;   if ($db==false)       return new xmlrpcresp(new xmlrpcval("Error connection to db ","string"));    $stmt = oci_parse($db,$select);   if (oci_execute($stmt,OCI_DEFAULT))     {       if (oci_fetch($stmt)) 	{ 	  $birth = oci_result($stmt,"PAT_BIRTH"); 	}       else 	{ 	  return new xmlrpcresp(new xmlrpcval(0, "integer")); 	  //return new xmlrpcresp(new xmlrpcval("Error fetching result ","string")); 	}     }   else     {       return new xmlrpcresp(new xmlrpcval(0, "integer"));       //return new xmlrpcresp(new xmlrpcval("Error executing select ".$select,"string"));     }    return new xmlrpcresp(new xmlrpcval($birth, "string")); }  ///////////////////////////////////////////////////////////////////////////////////////// // args: le staff id et le délai maximum de validité du click dans le ris // retour: toutes les colonnes de la table RIS_DICTATION_INTERFACE function risCMD_getCMD($params)//($staffid, $gap) {   global $db;   if ($db==false)       return new xmlrpcresp(new xmlrpcval("Error connection to db ","string"));    $staffid = $params->getParam(0)->scalarval();   $gap = $params->getParam(1)->scalarval();    $select = "select * from RIS_DICTATION_INTERFACE where RDI_STAFF_ID='$staffid' and RDI_DT>(sysdate-$gap/(24*60)) order by RDI_DT desc";    //Execute select, Return all values in the array   $arr = array();   $stmt = oci_parse($db,$select);   if (oci_execute($stmt,OCI_DEFAULT))     {       if (oci_fetch($stmt)) 	{ 	  $ncols = OCINumCols($stmt); 	  for ($i = 1; $i <= $ncols; $i++) { 	    $column_name  = OCIColumnName($stmt, $i); 	    $column_type  = OCIColumnType($stmt, $i); 	    $column_size  = OCIColumnSize($stmt, $i); 	    $column_data = OCIResult($stmt, $i);     	    $arr = $arr + array($column_name => new xmlrpcval($column_data,"string")); 	  } 	}       else 	{ 	  return new xmlrpcresp(new xmlrpcval(0, "integer")); 	  //return new xmlrpcresp(new xmlrpcval("Error fetching result ","string")); 	}     }   else     {       return new xmlrpcresp(new xmlrpcval(0, "integer"));       //return new xmlrpcresp(new xmlrpcval("Error executing select ".$select,"string"));     }    return new xmlrpcresp(new xmlrpcval($arr, "struct")); }  ///////////////////////////////////////////////////////////////////////////////////////// function risCMD_help() {   return new xmlrpcresp(new xmlrpcval("getBirth, getCMD", "string")); }  ///////////////////////////////////////////////////////////////////////////////////////// // args: l'affirmation sql a exécuter // retour: la string "Sql Done" function sql_execSql($param)//($select) {   global $db;   if ($db==false)       return new xmlrpcresp(new xmlrpcval("Error connection to db ","string"));    $select = $params->getParam(0)->scalarval();    //Execute select, with commit   $stmt = oci_parse($db,$select);   if (oci_execute($stmt,OCI_DEFAULT))     {       oci_commit($db);     }    return new xmlrpcresp(new xmlrpcval("Sql Done", "string")); }  ///////////////////////////////////////////////////////////////////////////////////////// // args: N/A // retour: le numéro d'institution défini dans la table config_env sous institution //         ou 0 si non défini. function sql_getInst() {   global $db;   if ($db==false)       return new xmlrpcresp(new xmlrpcval("Error connection to db ","string"));    $select = "select CE_VALUE from config_env where CE_ITEM_CONF='INSTITUTION'";    //Execute select, return result value in $inst as a string   $stmt = oci_parse($db,$select);   if (oci_execute($stmt,OCI_DEFAULT))     {       if (oci_fetch($stmt)) 	{ 	  $inst = oci_result($stmt,"CE_VALUE"); 	}       else 	{ 	  return new xmlrpcresp(new xmlrpcval(0, "integer")); 	  //return new xmlrpcresp(new xmlrpcval("Error fetching result ","string")); 	}     }   else     {       return new xmlrpcresp(new xmlrpcval(0, "integer"));       //return new xmlrpcresp(new xmlrpcval("Error executing select ".$select,"string"));     }    return new xmlrpcresp(new xmlrpcval($inst, "string")); }  ///////////////////////////////////////////////////////////////////////////////////////// function sql_help() {   return new xmlrpcresp(new xmlrpcval("execSql, getInst", "string")); }  //Exemple de retour de message d'erreur //return new xmlrpcresp(0, $xmlrpcerruser+3, "Select return an error"); //return new xmlrpcresp($retVal);//OK 


 

Windows tray application

Lundi, mai 30th, 2005

<h3>Voici un exemple de code pour mettre un programme dans le system tray (à coté de l’heure) dans windows.</h3> <br> Il est basé sur un programme en mode dialog.<br> <br> Pour commencer dans le <b>’initinstance’</b> modifier la création de la dialogue de la manière suivante: <pre style=”background-color: #FFF8ED”> //int nResponse = dlg.DoModal(); dlg.Create(IDD_AUDIOCONTEXT_DIALOG); dlg.ShowWindow(SW_HIDE); int nResponse = dlg.RunModalLoop(); dlg.DestroyWindow(); </pre> Par la suite modifier la classe de la dialog des manières suivantes: Ajouter une définition <pre style=”background-color: #FFF8ED”> #define MYWM_NOTIFYICON (WM_USER+2) </pre> A la fin du <b>OnInitDialog(…)</b> <pre style=”background-color: #FFF8ED”> TrayMessage(NIM_ADD); ShowWindow(SW_HIDE); </pre> A la fin du <b>DestroyWindows(…)</b> <pre style=”background-color: #FFF8ED”> TrayMessage(NIM_DELETE); </pre> Ajouter la fonction suivante: <pre style=”background-color: #FFF8ED”> ///////////////////////////////////////////////////////////////////////////// // Supported message // NIM_ADD, NIM_DELETE, NIM_MODIFY BOOL CAudioContextDlg::TrayMessage( DWORD dwMessage, unsigned int icone/*=IDR_TRAYICON*/) { CString sTip(_T(”AudioContext”)); NOTIFYICONDATA tnd; tnd.cbSize = sizeof(NOTIFYICONDATA); tnd.hWnd = m_hWnd; tnd.uID = IDR_TRAYICON; tnd.uFlags = NIF_MESSAGE|NIF_ICON; tnd.uCallbackMessage = MYWM_NOTIFYICON; tnd.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP; VERIFY( tnd.hIcon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE (icone)) ); lstrcpyn(tnd.szTip, (LPCTSTR)sTip, sizeof(tnd.szTip)); return Shell_NotifyIcon(dwMessage, &tnd); } </pre> Créer dans les ressources du projet une icone nommée IDR_TRAYICON vous pouvez aussi créer un menu avec un sous menu qui sera appellé par l’icone avec le bouton de droite, il suffit de créer le menu dans les ressources et d’ajouter ce code <pre style=”background-color: #FFF8ED”> ///////////////////////////////////////////////////////////////////////////// void CAudioContextDlg::TrayMenu(void) { BOOL bResult = FALSE; DWORD SelectionMade; CMenu menu; CString csMessage; int nIndex = 0; long nRecordNumber = 0; long nAbsolutePosition = 0; bResult = menu.LoadMenu(IDR_MENU_POPUP); CMenu* popup = menu.GetSubMenu(0); // menuRtClick.EnableMenuItem(ID_ITEM0, TRUE); // menuRtClick.EnableMenuItem(ID_ITEM1, TRUE); // menuRtClick.EnableMenuItem(ID_ITEM2, TRUE); // call the helper function to setup this as a titled popup menu // AddMenuTitle(popup); POINT pp; GetCursorPos(&pp); SetForegroundWindow(); SelectionMade = popup->TrackPopupMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, pp.x,pp.y,this); popup->DestroyMenu(); // The value of SelectionMade is the id of the command selected or 0 if no // selection was made switch(SelectionMade) { case ID_POPUP_QUIT: OnOK(); break; case ID_POPUP_SHOW: ShowWindow(SW_NORMAL); SetForegroundWindow(); SetFocus(); break; case ID_POPUP_CONTEXT: OnQuery() ; break; case ID_POPUP_ENVOYER: OnSend(); break; } } </pre> Pour gérer tout ça, il faut égallement ajouter un bout de code dans la fonction DefWinProc(…) <pre style=”background-color: #FFF8ED”> case MYWM_NOTIFYICON: { switch (lParam) { case WM_LBUTTONDOWN: { if (::MessageBox(NULL,”Voulez-vous transmettre cette dictée?”,”Transmettre”,MB_YESNO)==IDYES) { OnSend(); } return TRUE; } case WM_LBUTTONDBLCLK: switch (wParam) { case IDR_TRAYICON_PA: ShowWindow(SW_NORMAL); SetForegroundWindow(); SetFocus(); return TRUE; break; } case WM_RBUTTONDOWN: switch (wParam) { case IDR_TRAYICON_PA: //Beep(1000,100); TrayMenu(); break; } break; } } break; </pre> Le code de ce programme est basé sur la fonction shell_notifyicon dont la documentation est disponible à <a href=”http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shell_notifyicon.asp”>Documentation</a> <br> <br> La majorité du code de cet exemple provient de <a href=”http://www.codeguru.com/Cpp/controls/controls/systemtray/article.php/c5309/”>Code Guru</a> et de <a href=”http://www.codeproject.com/shell/mfcstartup.asp”>Code Project</a>

Update PowerDNS

Vendredi, mai 20th, 2005

Ce script reçoit en paramètre le mot de passe du compte Il se connecte à un serveur web sur lequel est installer PowerDNS et met à jour un enregistrement. <pre> #!/bin/bash ################################################################ # # Author: Jean-Luc Cyr # Copyright 2004 / ADN Informatique # Date: 2004-09-27 # Desc: PowerDNS Administration automatic DNS record update # from a GNET router dynamic IP configuration # ################################################################ #GNET ROUTER INTERNAL IP ROUTER_IP=1.2.3.4 #PowerDNS Administration server url DNS_SERVER= mydomain.com/powerdns DNS_USER=username DNS_PASS=$1 DNS_RECORD=33643 if [ $1 ] ; then echo “” >/dev/null ; else echo “must give password”; exit; fi DNS_LOGIN=”username=$DNS_USER&password=$DNS_PASS&submit=Log%20in” cd /tmp #date if [ -f status.htm ] ; then rm status.htm ; fi if [ -f oldip ] ; then rm oldip ; fi if [ -f currentip ] ; then cp currentip oldip ; fi #echo =======Get router status page======================= wget -q http://$ROUTER_IP/status.htm #echo =======Find out the router ip address=============== if [ -f status.htm ] ; then cat status.htm | grep “IP Address” | cut -b 41- | cut -f1 -d”<” > currentip ; else exit ; fi #date >>/tmp/currentip #echo =======Check if router address has changed========== #if diff -q oldip currentip | grep differ >/dev/null ; then echo “ROUTER IP HAS CHANGED” ; else echo “ROUTER IP HAS NOT CHANGED” ; exit ; fi if diff -q oldip currentip | grep differ >/dev/null ; then echo “ROUTER IP HAS CHANGED” ; else echo “” >/dev/null ; exit ; fi #echo =====================LOGGING IN===================== #Call the login page: http://ns1.lcinter.net/dns2/index.php (user/pass/submit) curl -D headers_and_cookies -d $DNS_LOGIN $DNS_SERVER/login.php 2>/dev/null #echo =====================GETTING RECORD================= #Call the update record page: http://ns1.lcinter.net/dns2/edit_record.php?id=33643 (dns data/submit) curl -b headers_and_cookies $DNS_SERVER/edit_record.php?id=$DNS_RECORD 2>/dev/null | grep content | grep name= | cut -b 74- | cut -f1 -d”<” > /tmp/dnsip #if diff -q dnsip currentip | grep differ >/dev/null ; then echo “DNS DIFFER FROM ROUTER IP” ; else echo “DNS IS UP TO DATE” ; curl -b headers_and_cookies $DNS_SERVER/logout.php ; exit ; fi if diff -q dnsip currentip | grep differ >/dev/null ; then echo “DNS DIFFER FROM ROUTER IP” ; else echo “” >/dev/null ; curl -b headers_and_cookies $DNS_SERVER/logout.php 2>/dev/null ; exit ; fi echo =====================UPDATING RECORD================ date #Call the update record page #Here are en example of the original update page from PowerDNS #<form action=”update_record.php?id=33643″ method=”post”> # <input type=”text” name=”record” size=”15″ maxlength=”200″ value=”home”>.adninformatique.net # <select name=”type”> # <option value=”NS”>NS</option> # <option value=”MX”>MX</option> # <option value=”A” selected>A</option> # <option value=”AAAA”>AAAA</option> # <option value=”PTR”>PTR</option> # <option value=”CNAME”>CNAME</option> # <option value=”TXT”>TXT</option> # </select> # <input type=”text” name=”prio” size=”5″ maxlength=”11″ value=”"> # <input type=”text” name=”content” size=”15″ maxlength=”255″ value=”24.201.6.204″> # <input type=”text” name=”ttl” size=”5″ maxlength=”11″ value=”1″> # <input type=”submit” value=”Update”> #</form> echo update adni.adninformatique.net dns DNS_REQ=”record=home&type=A&prio=&ttl=1&submit=Update&content=`cat /tmp/currentip`” #echo request is $DNS_REQ curl -b headers_and_cookies -d $DNS_REQ $DNS_SERVER/update_record.php?id=$DNS_RECORD >/dev/null 2>/dev/null echo update jlcyr.dyn.grafsoft.com dns curl www.grafsoft.com/~jlcyr/dns_update.php?ip=24.201.6.204 >/dev/null 2>/dev/null #grep “The record was updated” #echo =====================LOGGING OUT==================== #Call the logout page: http://ns1.lcinter.net/dns2/logout.php curl -b headers_and_cookies $DNS_SERVER/logout.php 2>/dev/null rm -f headers_and_cookies echo “dynamic ip updated” </pre> <dtml-var standard_html_header> <h2><dtml-var title_or_id></h2> <p> This is the <dtml-var id> Document. </p> <dtml-var standard_html_footer>