Source for file sqspell_functions.php

Documentation is available at sqspell_functions.php

  1. <?php
  2.  
  3. /**
  4.  * sqspell_functions.php
  5.  *
  6.  * All SquirrelSpell-wide functions are in this file.
  7.  *
  8.  * @author Konstantin Riabitsev <icon at duke.edu>
  9.  * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  10.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11.  * @version $Id: sqspell_functions.php,v 1.36 2006/09/30 17:48:14 tokul Exp $
  12.  * @package plugins
  13.  * @subpackage squirrelspell
  14.  */
  15.  
  16. /** globalize configuration vars **/
  17. global $SQSPELL_APP$SQSPELL_APP_DEFAULT$SQSPELL_WORDS_FILE$SQSPELL_CRYPTO;
  18.  
  19. /**
  20.  * load plugin configuration
  21.  * @todo allow storing configuration file in config/ directory
  22.  */
  23. include_once(SM_PATH 'plugins/squirrelspell/sqspell_config.php');
  24.  
  25. /** Hooked functions **/
  26.  
  27. /**
  28.  * Register option page block (internal function)
  29.  * @since 1.5.1 (sqspell 0.5)
  30.  * @return void 
  31.  */
  32.   global $optpage_blocks;
  33.  
  34.   /**
  35.    * Dependency on JavaScript is checked by SquirrelMail scripts
  36.    * Register Squirrelspell with the $optpage_blocks array.
  37.    */
  38.   $optpage_blocks[=
  39.     array(
  40.       'name' => _("SpellChecker Options"),
  41.       'url'  => '../plugins/squirrelspell/sqspell_options.php',
  42.       'desc' => _("Here you may set up how your personal dictionary is stored, edit it, or choose which languages should be available to you when spell-checking."),
  43.       'js'   => TRUE);
  44. }
  45.  
  46. /**
  47.  * This function adds a "Check Spelling" link to the "Compose" row
  48.  * during message composition (internal function).
  49.  * @since 1.5.1 (sqspell 0.5)
  50.  * @return void 
  51.  */
  52.   /**
  53.    * Check if this browser is capable of displaying SquirrelSpell
  54.    * correctly.
  55.    */
  56.   if (checkForJavascript()) {
  57.     /**
  58.      * Some people may choose to disable javascript even though their
  59.      * browser is capable of using it. So these freaks don't complain,
  60.      * use document.write() so the "Check Spelling" button is not
  61.      * displayed if js is off in the browser.
  62.      */
  63.     echo "<script type=\"text/javascript\">\n".
  64.       "<!--\n".
  65.       'document.write("<input type=\"button\" value=\"'.
  66.       _("Check Spelling").
  67.       '\" name=\"check_spelling\" onclick=\"window.open(\'../plugins/squirrelspell/sqspell_'.
  68.       'interface.php\', \'sqspell\', \'status=yes,width=550,height=370,'.
  69.       'resizable=yes\')\" />");' "\n".
  70.       "//-->\n".
  71.       "</script>\n";
  72.   }
  73. }
  74.  
  75. /**
  76.  * Upgrade dictionaries (internal function)
  77.  *
  78.  * Transparently upgrades user's dictionaries when message listing is loaded
  79.  * @since 1.5.1 (sqspell 0.5)
  80.  */
  81.   global $data_dir$username;
  82.  
  83.   if (sqspell_check_version(0,5)) {
  84.     $langs=sqspell_getSettings_old(null);
  85.     $words=sqspell_getWords_old();
  86.     sqspell_saveSettings($langs);
  87.     foreach ($langs as $lang{
  88.       $lang_words=sqspell_getLang_old($words,$lang);
  89.       $aLang_words=explode("\n",$lang_words);
  90.       $new_words=array();
  91.       foreach($aLang_words as $word{
  92.         if (preg_match("/^#/",$word&& trim($word)!=''{
  93.           $new_words[]=$word;
  94.         }
  95.       }
  96.       sqspell_writeWords($new_words,$lang);
  97.     }
  98.     // bump up version number
  99.     setPref($data_dir,$username,'sqspell_version','0.5');
  100.   }
  101. }
  102.  
  103. /** Internal functions **/
  104.  
  105. /**
  106.  * This function is the GUI wrapper for the options page. SquirrelSpell
  107.  * uses it for creating all Options pages.
  108.  *
  109.  * @param  string $title     The title of the page to display
  110.  * @param  string $scriptsrc This is used to link a file.js into the
  111.  *                     <script src="file.js"></script> format. This
  112.  *                     allows to separate javascript from the rest of the
  113.  *                     plugin and place it into the js/ directory.
  114.  * @param  string $body      The body of the message to display.
  115.  * @return            void 
  116.  */
  117. function sqspell_makePage($title$scriptsrc$body){
  118.   global $color$SQSPELL_VERSION;
  119.  
  120.   if (sqgetGlobalVar('MOD'$MODSQ_GET) ) {
  121.       $MOD 'options_main';
  122.   }
  123.  
  124.   displayPageHeader($color'None');
  125.   echo "&nbsp;<br />\n";
  126.   /**
  127.    * Check if we need to link in a script.
  128.    */
  129.   if($scriptsrc{
  130.     echo "<script type=\"text/javascript\" src=\"js/$scriptsrc\"></script>\n";
  131.   }
  132.   echo html_tag'table''''center''''width="95%" border="0" cellpadding="2" cellspacing="0"' "\n"
  133.     . html_tag'tr'"\n" .
  134.           html_tag'td''<strong>' $title .'</strong>''center'$color[9)
  135.       "\n"
  136.     . html_tag'tr'"\n" .
  137.           html_tag'td''<hr />''left' )
  138.       "\n"
  139.     . html_tag'tr'"\n" .
  140.           html_tag'td'$body'left' )
  141.       "\n";
  142.   /**
  143.    * Generate a nice "Return to Options" link, unless this is the
  144.    * starting page.
  145.    */
  146.   if ($MOD != "options_main"){
  147.     echo html_tag'tr'"\n" .
  148.                 html_tag'td''<hr />''left' )
  149.             "\n"
  150.       . html_tag'tr'"\n" .
  151.             html_tag'td''<a href="sqspell_options.php">'
  152.                 . _("Back to &quot;SpellChecker Options&quot; page")
  153.                 . '</a>',
  154.             'center' )
  155.         "\n";
  156.   }
  157.   /**
  158.    * Close the table and display the version.
  159.    */
  160.   echo html_tag'tr'"\n" .
  161.               html_tag'td''<hr />''left' )
  162.           "\n"
  163.     . html_tag'tr',
  164.           html_tag'td''SquirrelSpell ' squirrelspell_version()'center'$color[9)
  165.       "\n</table>\n";
  166.   echo '</body></html>';
  167. }
  168.  
  169. /**
  170.  * Function similar to the one above. This one is a general wrapper
  171.  * for the Squirrelspell pop-up window. It's called form nearly
  172.  * everywhere, except the check_me module, since that one is highly
  173.  * customized.
  174.  *
  175.  * @param  string $onload    Used to indicate and pass the name of a js function
  176.  *                     to call in a <body onload="function()" for automatic
  177.  *                     onload script execution.
  178.  * @param  string $title     Title of the page.
  179.  * @param  string $scriptsrc If defined, link this javascript source page into
  180.  *                     the document using <script src="file.js"> format.
  181.  * @param  string $body      The content to include.
  182.  * @return            void 
  183.  */
  184. function sqspell_makeWindow($onload$title$scriptsrc$body){
  185.   global $color$SQSPELL_VERSION;
  186.  
  187.   displayHtmlHeader($title,
  188.       ($scriptsrc "\n<script type=\"text/javascript\" src=\"js/$scriptsrc\"></script>\n''));
  189.  
  190.   echo "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" "
  191.       . "vlink=\"$color[7]\" alink=\"$color[7]\"";
  192.   /**
  193.    * Provide an onload="jsfunction()" if asked to.
  194.    */
  195.   if ($onload{
  196.       echo " onload=\"$onload\"";
  197.   }
  198.   /**
  199.    * Draw the rest of the page.
  200.    */
  201.   echo ">\n"
  202.     . html_tag'table'"\n" .
  203.           html_tag'tr'"\n" .
  204.               html_tag'td''<strong>' $title '</strong>''center'$color[9)
  205.           "\n" .
  206.           html_tag'tr'"\n" .
  207.               html_tag'td''<hr />''left' )
  208.           "\n" .
  209.           html_tag'tr'"\n" .
  210.               html_tag'td'$body'left' )
  211.           "\n" .
  212.           html_tag'tr'"\n" .
  213.               html_tag'td''<hr />''left' )
  214.           "\n" .
  215.           html_tag'tr'"\n" .
  216.               html_tag'td''SquirrelSpell ' squirrelspell_version()'center'$color[9)
  217.           ,
  218.       '''''width="100%" border="0" cellpadding="2"' );
  219.  
  220.   global $oTemplate;
  221.   $oTemplate->display('footer.tpl');
  222. }
  223.  
  224. /**
  225.  * Encryption function used by plugin (old format)
  226.  *
  227.  * This function does the encryption and decryption of the user
  228.  * dictionary. It is only available when PHP is compiled with
  229.  * mcrypt support (--with-mcrypt). See doc/CRYPTO for more
  230.  * information.
  231.  *
  232.  * @param  $mode  A string with either of the two recognized values:
  233.  *                 "encrypt" or "decrypt".
  234.  * @param  $ckey  The key to use for processing (the user's password
  235.  *                 in our case.
  236.  * @param  $input Content to decrypt or encrypt, according to $mode.
  237.  * @return        encrypted/decrypted content, or "PANIC" if the
  238.  *                 process bails out.
  239.  * @since 1.5.1 (sqspell 0.5)
  240.  * @deprecated
  241.  */
  242. function sqspell_crypto_old($mode$ckey$input){
  243.   /**
  244.    * Double-check if we have the mcrypt_generic function. Bail out if
  245.    * not so.
  246.    */
  247.   if (!function_exists('mcrypt_generic')) {
  248.     return 'PANIC';
  249.   }
  250.   /**
  251.    * Setup mcrypt routines.
  252.    */
  253.   $td mcrypt_module_open(MCRYPT_Blowfish""MCRYPT_MODE_ECB"");
  254.   $iv mcrypt_create_iv(mcrypt_enc_get_iv_size ($td)MCRYPT_RAND);
  255.   mcrypt_generic_init($td$ckey$iv);
  256.   /**
  257.    * See what we have to do depending on $mode.
  258.    * 'encrypt' -- Encrypt the content.
  259.    * 'decrypt' -- Decrypt the content.
  260.    */
  261.   switch ($mode){
  262.   case 'encrypt':
  263.     $crypto mcrypt_generic($td$input);
  264.     break;
  265.   case 'decrypt':
  266.     $crypto mdecrypt_generic($td$input);
  267.     /**
  268.      * See if it decrypted successfully. If so, it should contain
  269.      * the string "# SquirrelSpell". If not, then bail out.
  270.      */
  271.     if (!strstr($crypto"# SquirrelSpell")){
  272.       $crypto='PANIC';
  273.     }
  274.     break;
  275.   }
  276.   /**
  277.    * Finish up the mcrypt routines and return the processed content.
  278.    */
  279.   if (function_exists('mcrypt_generic_deinit')) {
  280.       // php 4.1.1+ syntax
  281.       mcrypt_generic_deinit ($td);
  282.       mcrypt_module_close ($td);
  283.   else {
  284.       // older deprecated function
  285.       mcrypt_generic_end ($td);
  286.   }
  287.   return $crypto;
  288. }
  289.  
  290. /**
  291.  * Encryption function used by plugin
  292.  *
  293.  * This function does the encryption and decryption of the user
  294.  * dictionary. It is only available when PHP is compiled with
  295.  * mcrypt support (--with-mcrypt). See doc/CRYPTO for more
  296.  * information.
  297.  *
  298.  * @param  $mode  A string with either of the two recognized values:
  299.  *                 "encrypt" or "decrypt".
  300.  * @param  $ckey  The key to use for processing (the user's password
  301.  *                 in our case.
  302.  * @param  $input Content to decrypt or encrypt, according to $mode.
  303.  * @return        encrypted/decrypted content, or "PANIC" if the
  304.  *                 process bails out.
  305.  */
  306. function sqspell_crypto($mode$ckey$input){
  307.   /**
  308.    * Double-check if we have the mcrypt_generic function. Bail out if
  309.    * not so.
  310.    */
  311.     if (!function_exists('mcrypt_generic')) {
  312.         return 'PANIC';
  313.     }
  314.     /**
  315.      * Setup mcrypt routines.
  316.      */
  317.     $td mcrypt_module_open(MCRYPT_Blowfish""MCRYPT_MODE_ECB"");
  318.     $iv mcrypt_create_iv(mcrypt_enc_get_iv_size ($td)MCRYPT_RAND);
  319.     mcrypt_generic_init($td$ckey$iv);
  320.     /**
  321.      * See what we have to do depending on $mode.
  322.      * 'encrypt' -- Encrypt the content.
  323.      * 'decrypt' -- Decrypt the content.
  324.      */
  325.     switch ($mode){
  326.     case 'encrypt':
  327.         $crypto mcrypt_generic($td'{sqspell}'.$input);
  328.         break;
  329.     case 'decrypt':
  330.         $crypto mdecrypt_generic($td$input);
  331.         if (preg_match("/^\{sqspell\}(.*)/",$crypto,$match)){
  332.             $crypto trim($match[1]);
  333.         else {
  334.             $crypto='PANIC';
  335.         }
  336.         break;
  337.     }
  338.     /**
  339.      * Finish up the mcrypt routines and return the processed content.
  340.      */
  341.     if (function_exists('mcrypt_generic_deinit')) {
  342.         // php 4.1.1+ syntax
  343.         mcrypt_generic_deinit ($td);
  344.         mcrypt_module_close ($td);
  345.     else {
  346.         // older deprecated function
  347.         mcrypt_generic_end ($td);
  348.     }
  349.     return $crypto;
  350. }
  351.  
  352. /**
  353.  * This function transparently upgrades the 0.2 dictionary format to the
  354.  * 0.3 format, since user-defined languages have been added in 0.3 and
  355.  * the new format keeps user dictionaries selection in the file.
  356.  *
  357.  * This function will be retired soon, as it's been a while since anyone
  358.  * has been using SquirrelSpell-0.2.
  359.  *
  360.  * @param  $words_string Contents of the 0.2-style user dictionary.
  361.  * @return               Contents of the 0.3-style user dictionary.
  362.  * @deprecated
  363.  */
  364. function sqspell_upgradeWordsFile($words_string){
  365.   /**
  366.    * Define just one dictionary for this user -- the default.
  367.    * If the user wants more, s/he can set them up in personal
  368.    * preferences. See doc/UPGRADING for more info.
  369.    */
  370.   $new_words_string =
  371.      substr_replace($words_string,
  372.                     "SquirrelSpell User Dictionary $SQSPELL_VERSION\n# "
  373.                     . "Last Revision: " date("Y-m-d")
  374.                     . "\n# LANG$SQSPELL_APP_DEFAULT\n# $SQSPELL_APP_DEFAULT",
  375.                     0strpos($words_string"\n")) "# End\n";
  376.   sqspell_writeWords($new_words_string);
  377.   return $new_words_string;
  378. }
  379.  
  380. /**
  381.  * gets list of available dictionaries from user's prefs.
  382.  * Function was modified in 1.5.1 (sqspell 0.5).
  383.  * Older function is suffixed with '_old'
  384.  * @return array list of dictionaries used by end user.
  385.  */
  386. function sqspell_getSettings(){
  387.     global $data_dir$username$SQSPELL_APP_DEFAULT$SQSPELL_APP;
  388.  
  389.     $ret=array();
  390.  
  391.     $sLangs=getPref($data_dir,$username,'sqspell_langs','');
  392.     if ($sLangs==''{
  393.         $ret[0]=$SQSPELL_APP_DEFAULT;
  394.     else {
  395.         $aLangs explode(',',$sLangs);
  396.         foreach ($aLangs as $lang{
  397.             if (array_key_exists($lang,$SQSPELL_APP)) {
  398.                 $ret[]=$lang;
  399.             }
  400.         }
  401.     }
  402.     return $ret;
  403. }
  404.  
  405. /**
  406.  * Saves user's language preferences
  407.  * @param array $langs languages array (first key is default language)
  408.  * @since 1.5.1 (sqspell 0.5)
  409.  */
  410. function sqspell_saveSettings($langs{
  411.   global $data_dir$username;
  412.   setPref($data_dir,$username,'sqspell_langs',implode(',',$langs));
  413. }
  414.  
  415. /**
  416.  * Get list of enabled languages.
  417.  *
  418.  * Right now it just returns an array with the dictionaries
  419.  * available to the user for spell-checking. It will probably
  420.  * do more in the future, as features are added.
  421.  *
  422.  * @param string $words The contents of the user's ".words" file.
  423.  * @return array a strings array with dictionaries available
  424.  *                 to this user, e.g. {"English", "Spanish"}, etc.
  425.  * @since 1.5.1 (sqspell 0.5)
  426.  * @deprecated
  427.  */
  428. function sqspell_getSettings_old($words){
  429.   /**
  430.    * Check if there is more than one dictionary configured in the
  431.    * system config.
  432.    */
  433.   if (sizeof($SQSPELL_APP1){
  434.     /**
  435.      * Now load the user prefs. Check if $words was empty -- a bit of
  436.      * a dirty fall-back. TODO: make it so this is not required.
  437.      */
  438.     if(!$words){
  439.       $words=sqspell_getWords_old();
  440.     }
  441.     if ($words){
  442.       /**
  443.        * This user has a ".words" file.
  444.        * Find which dictionaries s/he wants to use and load them into
  445.        * the $langs array.
  446.        */
  447.       preg_match("/# LANG: (.*)/i"$words$matches);
  448.       $langs=explode(", "$matches[1]);
  449.     else {
  450.       /**
  451.        * User doesn't have a personal dictionary. Grab the default
  452.        * system setting.
  453.        */
  454.       $langs[0]=$SQSPELL_APP_DEFAULT;
  455.     }
  456.   else {
  457.     /**
  458.      * There is no need to read the ".words" file as there is only one
  459.      * dictionary defined system-wide.
  460.      */
  461.     $langs[0]=$SQSPELL_APP_DEFAULT;
  462.   }
  463.   return $langs;
  464. }
  465.  
  466. /**
  467.  * Get user dictionary for selected language
  468.  * Function was modified in 1.5.1 (sqspell 0.5).
  469.  * Older function is suffixed with '_old'
  470.  * @param string $lang language
  471.  * @param array words stored in selected language dictionary
  472.  */
  473. function sqspell_getLang($lang{
  474.   global $data_dir$username,$SQSPELL_CRYPTO;
  475.   $sWords=getPref($data_dir,$username,'sqspell_dict_' $lang,'');
  476.   if (preg_match("/^\{crypt\}(.*)/i",$sWords,$match)) {
  477.     /**
  478.      * Dictionary is encrypted or mangled. Try to decrypt it.
  479.      * If fails, complain loudly.
  480.      *
  481.      * $old_key would be a value submitted by one of the modules with
  482.      * the user's old mailbox password. I admin, this is rather dirty,
  483.      * but efficient. ;)
  484.      */
  485.     if (sqgetGlobalVar('old_key'$old_keySQ_POST)) {
  486.       $clear_key=$old_key;
  487.     else {
  488.       sqgetGlobalVar('key'$keySQ_COOKIE);
  489.       sqgetGlobalVar('onetimepad'$onetimepadSQ_SESSION);
  490.       /**
  491.        * Get user's password (the key).
  492.        */
  493.       $clear_key OneTimePadDecrypt($key$onetimepad);
  494.     }
  495.     /**
  496.      * Invoke the decryption routines.
  497.      */
  498.     $sWords=sqspell_crypto("decrypt"$clear_key$match[1]);
  499.     /**
  500.      * See if decryption failed.
  501.      */
  502.     if ($sWords=="PANIC"){
  503.       sqspell_handle_crypt_panic($lang);
  504.       // script execution stops here
  505.     else {
  506.       /**
  507.        * OK! Phew. Set the encryption flag to true so we can later on
  508.        * encrypt it again before saving to HDD.
  509.        */
  510.       $SQSPELL_CRYPTO=true;
  511.     }
  512.   else {
  513.     /**
  514.      * No encryption is/was used. Set $SQSPELL_CRYPTO to false,
  515.      * in case we have to save the dictionary later.
  516.      */
  517.     $SQSPELL_CRYPTO=false;
  518.   }
  519.   // rebuild word list and remove empty entries
  520.   $aWords=array();
  521.   foreach (explode(',',$sWordsas $word{
  522.     if (trim($word!=''{
  523.       $aWords[]=trim($word);
  524.       }
  525.   }
  526.   return $aWords;
  527. }
  528.  
  529. /**
  530.  * Get user's dictionary (old format)
  531.  *
  532.  * This function returns only user-defined dictionary words that correspond
  533.  * to the requested language.
  534.  *
  535.  * @param  $words The contents of the user's ".words" file.
  536.  * @param  $lang  Which language words to return, e.g. requesting
  537.  *                 "English" will return ONLY the words from user's
  538.  *                 English dictionary, disregarding any others.
  539.  * @return        The list of words corresponding to the language
  540.  *                 requested.
  541.  * @since 1.5.1 (sqspell 0.5)
  542.  * @deprecated
  543.  */
  544. function sqspell_getLang_old($words$lang){
  545.   $start=strpos($words"$lang\n");
  546.   /**
  547.    * strpos() will return -1 if no # $lang\n string was found.
  548.    * Use this to return a zero-length value and indicate that no
  549.    * words are present in the requested dictionary.
  550.    */
  551.   if (!$startreturn '';
  552.   /**
  553.    * The words list will end with a new directive, which will start
  554.    * with "#". Locate the next "#" and thus find out where the
  555.    * words end.
  556.    */
  557.   $end=strpos($words"#"$start+1);
  558.   $lang_words substr($words$start$end-$start);
  559.   return $lang_words;
  560. }
  561.  
  562. /**
  563.  * Saves user's dictionary (old format)
  564.  *
  565.  * This function operates the user dictionary. If the format is
  566.  * clear-text, then it just reads the file and returns it. However, if
  567.  * the file is encrypted (well, "garbled"), then it tries to decrypt
  568.  * it, checks whether the decryption was successful, troubleshoots if
  569.  * not, then returns the clear-text dictionary to the app.
  570.  *
  571.  * @return the contents of the user's ".words" file, decrypted if
  572.  *          necessary.
  573.  * @since 1.5.1 (sqspell 0.5)
  574.  * @deprecated
  575.  */
  576.   global $SQSPELL_WORDS_FILE$SQSPELL_CRYPTO;
  577.   $words="";
  578.   if (file_exists($SQSPELL_WORDS_FILE)){
  579.     /**
  580.      * Gobble it up.
  581.      */
  582.     $fp=fopen($SQSPELL_WORDS_FILE'r');
  583.     $words=fread($fpfilesize($SQSPELL_WORDS_FILE));
  584.     fclose($fp);
  585.   }
  586.   /**
  587.    * Check if this is an encrypted file by looking for
  588.    * the string "# SquirrelSpell" in it (the crypto
  589.    * function does that).
  590.    */
  591.   if ($words && !strstr($words"# SquirrelSpell")){
  592.     /**
  593.      * This file is encrypted or mangled. Try to decrypt it.
  594.      * If fails, complain loudly.
  595.      *
  596.      * $old_key would be a value submitted by one of the modules with
  597.      * the user's old mailbox password. I admin, this is rather dirty,
  598.      * but efficient. ;)
  599.      */
  600.     sqgetGlobalVar('key'$keySQ_COOKIE);
  601.     sqgetGlobalVar('onetimepad'$onetimepadSQ_SESSION);
  602.  
  603.     sqgetGlobalVar('old_key'$old_keySQ_POST);
  604.  
  605.     if ($old_key != ''{
  606.         $clear_key=$old_key;
  607.     else {
  608.       /**
  609.        * Get user's password (the key).
  610.        */
  611.       $clear_key OneTimePadDecrypt($key$onetimepad);
  612.     }
  613.     /**
  614.      * Invoke the decryption routines.
  615.      */
  616.     $words=sqspell_crypto_old("decrypt"$clear_key$words);
  617.     /**
  618.      * See if decryption failed.
  619.      */
  620.     if ($words=="PANIC"){
  621.       // script execution stops here.
  622.     else {
  623.       /**
  624.        * OK! Phew. Set the encryption flag to true so we can later on
  625.        * encrypt it again before saving to HDD.
  626.        */
  627.       $SQSPELL_CRYPTO=true;
  628.     }
  629.   else {
  630.     /**
  631.      * No encryption is/was used. Set $SQSPELL_CRYPTO to false,
  632.      * in case we have to save the dictionary later.
  633.      */
  634.     $SQSPELL_CRYPTO=false;
  635.   }
  636.   /**
  637.    * Check if we need to upgrade the dictionary from version 0.2.x
  638.    * This is going away soon.
  639.    */
  640.   if (strstr($words"Dictionary v0.2")){
  641.     $words=sqspell_upgradeWordsFile($words);
  642.   }
  643.   return $words;
  644. }
  645.  
  646. /**
  647.  * Saves user's dictionary
  648.  * Function was replaced in 1.5.1 (sqspell 0.5).
  649.  * Older function is suffixed with '_old'
  650.  * @param array $words words that should be stored in dictionary
  651.  * @param string $lang language
  652.  */
  653. function sqspell_writeWords($words,$lang){
  654.   global $SQSPELL_CRYPTO,$username,$data_dir;
  655.  
  656.   $sWords implode(',',$words);
  657.   if ($SQSPELL_CRYPTO){
  658.     /**
  659.      * User wants to encrypt the file. So be it.
  660.      * Get the user's password to use as a key.
  661.      */
  662.     sqgetGlobalVar('key'$keySQ_COOKIE);
  663.     sqgetGlobalVar('onetimepad'$onetimepadSQ_SESSION);
  664.  
  665.     $clear_key=OneTimePadDecrypt($key$onetimepad);
  666.     /**
  667.      * Try encrypting it. If fails, scream bloody hell.
  668.      */
  669.     $save_words sqspell_crypto("encrypt"$clear_key$sWords);
  670.     if ($save_words == 'PANIC'){
  671.       // FIXME: handle errors here
  672.  
  673.     }
  674.     $save_words='{crypt}'.$save_words;
  675.   else {
  676.     $save_words=$sWords;
  677.   }
  678.   setPref($data_dir,$username,'sqspell_dict_'.$lang,$save_words);
  679. }
  680.  
  681. /**
  682.  * Writes user dictionary into the $username.words file, then changes mask
  683.  * to 0600. If encryption is needed -- does that, too.
  684.  *
  685.  * @param  $words The contents of the ".words" file to write.
  686.  * @return        void 
  687.  * @since 1.5.1 (sqspell 0.5)
  688.  * @deprecated
  689.  */
  690. function sqspell_writeWords_old($words){
  691.   global $SQSPELL_WORDS_FILE$SQSPELL_CRYPTO;
  692.   /**
  693.    * if $words is empty, create a template entry by calling the
  694.    * sqspell_makeDummy() function.
  695.    */
  696.   if (!$words){
  697.     $words=sqspell_makeDummy();
  698.   }
  699.   if ($SQSPELL_CRYPTO){
  700.     /**
  701.      * User wants to encrypt the file. So be it.
  702.      * Get the user's password to use as a key.
  703.      */
  704.     sqgetGlobalVar('key'$keySQ_COOKIE);
  705.     sqgetGlobalVar('onetimepad'$onetimepadSQ_SESSION);
  706.  
  707.     $clear_key=OneTimePadDecrypt($key$onetimepad);
  708.     /**
  709.      * Try encrypting it. If fails, scream bloody hell.
  710.      */
  711.     $save_words sqspell_crypto("encrypt"$clear_key$words);
  712.     if ($save_words == 'PANIC'){
  713.       /**
  714.        * AAAAAAAAH! I'm not handling this yet, since obviously
  715.        * the admin of the site forgot to compile the MCRYPT support in
  716.        * when upgrading an existing PHP installation.
  717.        * I will add a handler for this case later, when I can come up
  718.        * with some work-around... Right now, do nothing. Let the Admin's
  719.        * head hurt.. ;)))
  720.        */
  721.       /** save some hairs on admin's head and store error message in logs */
  722.       error_log('SquirrelSpell: php does not have mcrypt support');
  723.     }
  724.   else {
  725.     $save_words $words;
  726.   }
  727.   /**
  728.    * Do the actual writing.
  729.    */
  730.   $fp=fopen($SQSPELL_WORDS_FILE"w");
  731.   fwrite($fp$save_words);
  732.   fclose($fp);
  733.   chmod($SQSPELL_WORDS_FILE0600);
  734. }
  735.  
  736. /**
  737.  * Deletes user's dictionary
  738.  * Function was modified in 1.5.1 (sqspell 0.5). Older function is suffixed
  739.  * with '_old'
  740.  * @param string $lang dictionary
  741.  */
  742. function sqspell_deleteWords($lang{
  743.   global $data_dir$username;
  744.   removePref($data_dir,$username,'sqspell_dict_'.$lang);
  745. }
  746.  
  747. /**
  748.  * Deletes user's dictionary when it is corrupted.
  749.  * @since 1.5.1 (sqspell 0.5)
  750.  * @deprecated
  751.  */
  752.   /**
  753.    * So I open the door to my enemies,
  754.    * and I ask can we wipe the slate clean,
  755.    * but they tell me to please go...
  756.    * uhm... Well, this just erases the user dictionary file.
  757.    */
  758.   global $SQSPELL_WORDS_FILE;
  759.   if (file_exists($SQSPELL_WORDS_FILE)){
  760.     unlink($SQSPELL_WORDS_FILE);
  761.   }
  762. }
  763. /**
  764.  * Creates an empty user dictionary for the sake of saving prefs or
  765.  * whatever.
  766.  *
  767.  * @return The template to use when storing the user dictionary.
  768.  * @deprecated
  769.  */
  770. function sqspell_makeDummy(){
  771.   $words "SquirrelSpell User Dictionary $SQSPELL_VERSION\n"
  772.      . "# Last Revision: " date('Y-m-d')
  773.      . "\n# LANG$SQSPELL_APP_DEFAULT\n# End\n";
  774.   return $words;
  775. }
  776.  
  777. /**
  778.  * This function checks for security attacks. A $MOD variable is
  779.  * provided in the QUERY_STRING and includes one of the files from the
  780.  * modules directory ($MOD.mod). See if someone is trying to get out
  781.  * of the modules directory by providing dots, unicode strings, or
  782.  * slashes.
  783.  *
  784.  * @param  string $rMOD the name of the module requested to include.
  785.  * @return void, since it bails out with an access error if needed.
  786.  */
  787. function sqspell_ckMOD($rMOD){
  788.   if (strstr($rMOD'.')
  789.       || strstr($rMOD'/')
  790.       || strstr($rMOD'%')
  791.       || strstr($rMOD"\\")){
  792.     echo _("Cute.");
  793.     exit;
  794.   }
  795. }
  796.  
  797. /**
  798.  * Used to check internal version of SquirrelSpell dictionary
  799.  * @param integer $major main version number
  800.  * @param integer $minor second version number
  801.  * @return boolean true if stored dictionary version is $major.$minor or newer
  802.  * @since 1.5.1 (sqspell 0.5)
  803.  */
  804. function sqspell_check_version($major,$minor{
  805.   global $data_dir$username;
  806.   // 0.4 version is internal version number that is used to indicate upgrade from
  807.   // separate files to generic SquirrelMail prefs storage.
  808.   $sqspell_version=getPref($data_dir,$username,'sqspell_version','0.4');
  809.  
  810.   $aVersion=explode('.',$sqspell_version);
  811.  
  812.   if ($aVersion[0$major ||
  813.       $aVersion[0== $major && $aVersion[1$minor)) {
  814.     return false;
  815.   }
  816.   return true;
  817. }
  818.  
  819. /**
  820.  * Displays form that allows to enter different password for dictionary decryption.
  821.  * If language is not set, function provides form to handle older dictionary files.
  822.  * @param string $lang language
  823.  * @since 1.5.1 (sqspell 0.5)
  824.  */
  825. function sqspell_handle_crypt_panic($lang=false{
  826.   if (sqgetGlobalVar('SCRIPT_NAME',$SCRIPT_NAME,SQ_SERVER))
  827.     $SCRIPT_NAME='';
  828.  
  829.   /**
  830.    * AAAAAAAAAAAH!!!!! OK, ok, breathe!
  831.    * Let's hope the decryption failed because the user changed his
  832.    * password. Bring up the option to key in the old password
  833.    * or wipe the file and start over if everything else fails.
  834.    *
  835.    * The _("SquirrelSpell...) line has to be on one line, otherwise
  836.    * gettext will bork. ;(
  837.    */
  838.   $msg html_tag'p'"\n" .
  839.     '<strong>' _("ATTENTION:"'</strong><br />'
  840.     .  _("SquirrelSpell was unable to decrypt your personal dictionary. This is most likely due to the fact that you have changed your mailbox password. In order to proceed, you will have to supply your old password so that SquirrelSpell can decrypt your personal dictionary. It will be re-encrypted with your new password after this. If you haven't encrypted your dictionary, then it got mangled and is no longer valid. You will have to delete it and start anew. This is also true if you don't remember your old password -- without it, the encrypted data is no longer accessible.",
  841.     'left' .  "\n"
  842.     . (($langhtml_tag('p',sprintf(_("Your %s dictionary is encrypted with password that differs from your current password."),
  843.                                       htmlspecialchars($lang)),'left''')
  844.     . '<blockquote>' "\n"
  845.     . '<form method="post" onsubmit="return AYS()">' "\n"
  846.     . '<input type="hidden" name="MOD" value="crypto_badkey" />' "\n"
  847.     . (($lang?
  848.        '<input type="hidden" name="dict_lang" value="'.htmlspecialchars($lang).'" />' :
  849.        '<input type="hidden" name="old_setup" value="yes" />')
  850.     . html_tag'p',  "\n" .
  851.         '<input type="checkbox" name="delete_words" value="ON" />'
  852.         . _("Delete my dictionary and start a new one"'<br />'
  853.         . _("Decrypt my dictionary with my old password:")
  854.         . '<input name="old_key" size="10" />' ,
  855.         'left' "\n"
  856.         . '</blockquote>' "\n"
  857.         . html_tag'p'"\n"
  858.                . '<input type="submit" value="'
  859.                . _("Proceed"' &gt;&gt;" />' ,
  860.            'center' "\n"
  861.          . '</form>' "\n";
  862.   /**
  863.    * Add some string vars so they can be i18n'd.
  864.    */
  865.   $msg .= "<script type=\"text/javascript\"><!--\n"
  866.     . "var ui_choice = \"" _("You must make a choice"."\";\n"
  867.     . "var ui_candel = \"" _("You can either delete your dictionary or type in the old password. Not both.""\";\n"
  868.     . "var ui_willdel = \"" _("This will delete your personal dictionary file. Proceed?""\";\n"
  869.     . "//--></script>\n";
  870.   /**
  871.    * See if this happened in the pop-up window or when accessing
  872.    * the SpellChecker options page.
  873.    * This is a dirty solution, I agree.
  874.    * TODO: make this prettier.
  875.    */
  876.   if (strstr($SCRIPT_NAME"sqspell_options")){
  877.     sqspell_makePage(_("Error Decrypting Dictionary"),
  878.                      "decrypt_error.js"$msg);
  879.   else {
  880.     sqspell_makeWindow(null_("Error Decrypting Dictionary"),
  881.                        "decrypt_error.js"$msg);
  882.   }
  883.   exit;
  884. }
  885.  
  886. /**
  887.  * SquirrelSpell version. Don't modify, since it identifies the format
  888.  * of the user dictionary files and messing with this can do ugly
  889.  * stuff. :)
  890.  * @global string $SQSPELL_VERSION 
  891.  * @deprecated
  892.  */
  893. $SQSPELL_VERSION="v0.3.8";

Documentation generated on Sat, 07 Oct 2006 16:13:41 +0300 by phpDocumentor 1.3.0RC6