22
cze/10
0

[JS] CKEditor problem under Opera fix

Problem:
CKEditor 3.0 is a substitute to well known TinyMCE JavaScript editor. It’s easier to install and manage. It displays correctly under Chrome, Firefox or IE, but under latest Opera 10.xx CKEditor windows is not displayed correct. It doesn’t show at all. Some experienced users know that Opera has it’s own, very advanced, web-debugging mechanism – Dragonfly, which I’ve used to diagnose the „problem”.

Solution:
My CKEditor edit window appeared when I copied config.js file, from ./_source/core to ./ path. Path desribed as ./ is path where ckeditor.js file is placed. That fixed the issue and CKEditor appears normally now under Opera 10.xx

8
maj/10
0

[PHP] Hotlinking solution for CodeIgniter

I was having lately small problem with ImageSnip.com which I have developed few months ago. Site got some serious popularity in Asia and the images transfer was killing me. As we know, Asia i really big and very populated continent, so It would explain that few images fetched 200.000 times a day, where generating 5GB of traffic daily.

Although the traffic rate is high, most of the images were linked directly to server resources (images hotlinking). Hotlinked images ate my all free bandwidth in 4 days (20GB!) and I need to find a solution to that …

Solution to avoid hotlinking images is to place in images folder a .htaccess file. Although there are toms of examples, most of them don’t work for CodeIgniter framework (as ImageSnip is developed in), especially when developer uses „static” folder to host the css,js,images content…

After hour of browsing I’ve found solution(s) at Jeff Starr blog located at: http://perishablepress.com/press/2007/11/21/creating-the-ultimate-htaccess-anti-hotlinking-strategy/ which AT LAST WORKS! Code below:

###############################
# ultimate hotlink protection #
###############################

# disable directory browsing
# uncomment this option to protect access to directories
# Options -Indexes

# enable the following of symlinks
# uncomment this option if hotlink protection fails to work
# Options +FollowSymLinks

# verify presence of mod rewrite
<IfModule mod_rewrite.c>

 # enable the rewrite engine
 RewriteEngine on

 # check that file exists
 RewriteCond %{REQUEST_FILENAME} -f

 # check for requested file types
 # include additional file types here
 RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g?|png)$                [NC]

 # allow blank referrer requests
 RewriteCond %{HTTP_REFERER}     !^$

 # allow all requests from your domain
 # edit the domain to match your own
 RewriteCond %{HTTP_REFERER}     !^https?://([^.]+\.)?domain\.      [NC]

 # allow all requests from your ip address
 # edit the ip address to match your own
 RewriteCond %{HTTP_REFERER}     !^https?://123\.123\.123\.123(.*)$ [NC]

 # additional site access
 # include additional sites here replace domain names and or
 # remove unnecessary lines or add new lines for more sites
 RewriteCond %{HTTP_REFERER}     !^https?://([^.]+\.)?domain_01\.   [NC]
 RewriteCond %{HTTP_REFERER}     !^https?://([^.]+\.)?domain_02\.   [NC]
 RewriteCond %{HTTP_REFERER}     !^https?://([^.]+\.)?domain_03\.   [NC]

 # search engine access
 # include or remove search engines feed readers and other sites
 RewriteCond %{HTTP_REFERER}     !search\?q=cache                   [NC]
 RewriteCond %{HTTP_REFERER}     !google\.                          [NC]
 RewriteCond %{HTTP_REFERER}     !yahoo\.                           [NC]

 # allow access to all requests for your anti hotlink image
 # to serve an image instead of delivering a 403 error
 # uncomment the next line and edit the path and file name
 # RewriteCond %{REQUEST_URI}    !^/hotlink\.jpg$                   [NC]

 # deliver the hotlink image for all requests for the listed files
 # protect additional file types by editing the list below
 # if you are serving a file instead of a 403
 # uncomment the next line and edit the path and file name
 # RewriteRule \.(gif|jpe?g?|png)$ http://domain.tld/hotlink.jpg    [R,NC,L]

 # return a 403 error when any of the following file types is requested
 # you can protect additional file types by editing the list below
 RewriteRule \.(gif|jpe?g?|png)$                                  - [F,NC,L]

# close the module container
</ifModule>

As Iam no expert in .htaccess file configuration, this one takes the cake!

1
kwi/10
0

[C#] Row select on right click (context menu)

When using DataGridView control, activating it’s context menu (by right click on the grid) will not select the row/cell you are clicking at. For most of the operations users right-clicking on the grid want to perform operation on that selected item.

Solution to this problem is to select item at same time as the Context menu is shown by using Opening event of the context menu. Following code should be pasted into the Opening event handler function:


Point p = _myGRID.PointToClient(Cursor.Position);
DataGridView.HitTestInfo hti = _myGRID.HitTest(p.X, p.Y);
if (hti.RowIndex > -1)
{
_grid_Feeds.ClearSelection();
_grid_Feeds.Rows[hti.RowIndex].Selected = true;
}

The right clicked row will be selected and from now on you can get the bound object by _myGRID.SelectedRow .

25
mar/10
0

[PHP] Undefined index: lastBuildDate error fix

While working with lastRSS PHP lib I stumbed upon a following error:

Undefined index: lastBuildDate / lastrss.php / Line Number: 160

As far as I googled around I didn’t find ONE precise fix for it, so I tinker with it and eventually It worked. Probably it’s solution for similar problems in other libs…

Fix:

In lastrss.php find (around line 160)

if ($this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) {
// convert lastBuildDate to specified date format
$result['lastBuildDate'] = date($this->date_format, $timestamp);
}

Trick is that PHP5 does not allow (or does not allow us to ignore that) access indexes in arrays that are not declared (or something…). This is PHP5 specific. Anyway fix for this is pretty simple, all what we need to do is to check if such result is defined whith isset() function. Correct code should look like this:


if (isset($result['lastBuildDate']) && $this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) {

// convert lastBuildDate to specified date format

$result['lastBuildDate'] = date($this->date_format, $timestamp);

}

In lastRSS lib (which is cool RSS parsing library for PHP) this occurs when two things get together:

  1. You define own date_format (and don’t leave it empty as default)
  2. You fetch RSS for the first time (before the cache is written). Any another attempt on fetching feeds from same rss address will not cause this error to appear, as long as the cache will be read. If you clear the cache error will come again…
22
mar/10
0

[Web] Can’t login to Live.com mail

I’am not a big fan of web based mail clients. I use them when I don’t have access to my primary systems (for ex. in cafes/PDA) but on my computers I’ve got installed standard stand-alone app mail client similar to Outlook (The Bat). I’ve recently created a mail account at live.com (Microsoft hosted mail system). Logging thru web mail does not make any problems. Although setting the account in TheBat was a problem. When I configured everything I get error:

-ERR authentication failed

I was sure that provided config (server, ports, user name) was correct. After several attempts I found out that live.com services REQUIRES max 16 symbol passwords and my password was longer …

Shorting password length to 16 symbols does the trick and allows user (user@live.com) to login …

3
sty/10
0

[C#] Easy solution for AutoScroll in GridView

Poking around standard WinForms GridView control I found it have not any „Autoscroll” properties. A proper approach would be create new control inherited from base GridView with altered component drawing and focusing etc, but I didn’t have time and will to do this, especially for my extremally simple FileSorter tool. So I came up with easy solution.

The Problem:
Application have a grid, which is in real time adding new rows. When it’s view area height is reached, more rows are showed hidden „below” the view area of grid, and user is forced to use vertical scroll bar to scroll to them.

Solution:
We can use „FirstDisplayCell” property of DataGridView property

Code:

if(_grid.Rows.Count>0 && _grid.Rows[_grid.Rows.Count-1].Cells.Count>0)
   _grid.FirstDisplayedCell = _grid.Rows[_grid.Rows.Count-1].Cells[0];

Solution in 2 lines of code ;-)

Additional Notes:

Method is thread safe and updates GUI properly using Invoke while invoking on a _grid object

13
gru/09
0

[C#] How to play simple system sound ?

Its pretty simple to play system sounds, for example at the end of some operation:


using System.Media;

//....

SystemSounds.Asterisk.Play(); //Makes standard "warning" ding
Tagged as: ,
5
lis/09
0

[.NET] Showing menu on button click

This extremally easy thing, sometimes makes so much problem for someone who don’t know how to use .NET build in functions. As I saw, similar problem implementation in six lines of code (with pretty much calculations) it’s can be easily achived in one line.

Problem: we want to show menu, on center of a button, which was clicked.
Graphic representation:
scroll_menu

Solution:


_context_menu.Show(_button_myButton, new Point(_button_myButton.Width/2, _button_myButton.Height/2));

4
lis/09
0

[.NET] Relative path

Below is a quick and dirty implementation of solution for how to get relative path to file from a folder

Problem1:

Path1 (base): C:\data\my_path
Path2 (target): C:\some_other\folder\data\file.txt
Result: ..\..\some_other\folder\data\file.txt

Problem2:
Path1 (base): C:\data\path
Path2 (target): C:\data\path\somedata\file.txt
Result: .\somedata\file.txt

Solution:

public static string getRelativePath(String baseDirectory, string destinationFile)
 {
   List<String> _from = new List<String>(baseDirectory.Split('\\'));
   List<String> _dest = new List<String>(destinationFile.Split('\\'));

   if (_from[0].ToLower() != _dest[0].ToLower() || _dest.Count==0 || _from.Count==0)
   {
     return destinationFile; //Not same disk
   }

   int lvl = 0;

   for(int i=0; i<_from.Count; i++)
   {

    if (_from[i].ToLower() != _dest[i].ToLower())
    {
     break;
    }

    lvl++;
   }

   for (int j = 0; j < lvl; j++)
   {
    _from.RemoveAt(0);
    _dest.RemoveAt(0);
   }

   String _resultPath = "";
   if (_from.Count == 0) //the file is in higher folder
   {
    _resultPath += ".\\";
    for (int i = 0; i < _dest.Count; i++)
    {
     _resultPath += _dest[i];
     if (i + 1 < _dest.Count)
       _resultPath += "\\";
    }

   }
   else
   {

     for (int i = 0; i < _from.Count; i++)
     {
      _resultPath += "..\\";
     }

     for (int i = 0; i < _dest.Count; i++)
     {
      _resultPath += _dest[i];
      if (i + 1 < _dest.Count)
        _resultPath += "\\";
     }
   }
   return _resultPath;
 }
Tagged as: ,
17
paź/09
0

[C#] How to associate user component with other component which is null at initialization

Problem:

The idea is to put my own component on form and associate it with already existing TabControl object. Also, I wanted to capture user pressed keys and react on them before they will reach other controls (which require to set KeyPreview to true and add KeyDown/KeyUp event). Willing to encapsulate this process behind developer eyes, I write appropriate code in property of my component. But when I added control in Component Designer I was receiving error while starting application, initialize code of my component was written in InitializeComponent() method before the base Form object was initialized. Like this:


void InitializeComponent()
{
//....
//
// MyComponent
//
this.myComponent.ParentTab = this.tabControl1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(522, 273);
this.Controls.Add(this.tabControl1);
this.KeyPreview = true;
this.Name = "Form1";
this.Text = "Form1";
this.myComponent1.ResumeLayout(false);
this.ResumeLayout(false);
}

And in my code I was calling the uninitialized code:


//...
myComponent1.FindForm().KeyDown += //...

FindForm() was returning null (as the form wasn’t yet initialized)

Solution:

  1. Create KeyDown (or any) event for the TabControl itself
  2. At runtime, check in KeyDown state of the form you want to hook to. If it’s null, you can use the FindForm() because at runtime all controls are already initialized !

Solution was implemented in TabSwitcher component I wrote, for the TabControl control.

It’s that simple!