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

Documentation generated on Mon, 13 Jan 2020 04:23:35 +0100 by phpDocumentor 1.4.3