Xcode Buttons

This is some code I used to put a reset button on the screen. It’s mostly self-documenting. First, create a rect. I’ve already defined the x and y coordinates—buttonX and distanceFromTop based on the screen width and other buttons on the screen. Likewise, I’ve already defined the width. The rest of the code is just assigning properties to the button. The action is a method in the file that changes the image to a ‘Selected’ image.


// Reset Button
    @synthesize resetButton = resetButton;
    ....

    CGRect resetButtonFrame = CGRectMake(buttonX, distanceFromTop,
                                         button_width, 25.0f);            
    self.resetButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.resetButton.titleLabel.font            = [UIFont systemFontOfSize: 16];
    self.resetButton.titleLabel.textColor       = [UIColor blueColor];
    self.resetButton.titleLabel.shadowOffset    = CGSizeMake (1.0, 0.0);
    [self.resetButton setTitle:@"Reset Scoring" forState:UIControlStateNormal];
    [self.resetButton setFrame:resetButtonFrame];
    [self.resetButton addTarget:self 
                             action:@selector(resetScorekeeper:) 
                   forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.resetButton];
    // This line makes the icons stay in the center of the screen when you rotate
    self.resetButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    
    ....

- (IBAction)resetScorekeeper:(UIButton *)sender {
    
    [self resetResultsFile];
    [sender setImage:[UIImage imageNamed:@"ResetScoringSelected.png"] forState:UIControlStateNormal];
}

How to center an image with a caption.

E6-B

Let’s check that wind correction angle.

Use this code:


<div class="centered"><img class="centered" src="/images/696866-spock.jpg" alt="E6-B" /><p class="caption">Let’s check that wind correction angle.</p>
</div>

Note that the whole thing is wrapped in a div and the caption is in a paragraph.

The CSS for the classes is:

Image centering in a paragraph with padding.


img.centered {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

Caption


.caption {
  text-align: center;
  margin-top: -4px;
  font-style: italic;
  font-size: 80%;
}

MySQL injection attempts

I recently started getting lots of error statements in my error logs for a site I manage. And by lots I mean thousands each week. Since the site works fine and I haven’t changed anything recently I was puzzled as to why the were happening.

So I expanded the MySql error codes to give me more information on what file was the problem and what the MySql statement was that failed. i.e filename, query, and error message.


if (!$result) {
    error_log("product.php");
    error_log($query);
    error_log(mysqli_error($dbLF));
    die();
  }

This is a common error.


[18-Jun-2012 05:34:52 UTC] SELECT * FROM product_table
           WHERE productNum = \\\'1
           ORDER BY display_seq, name
[18-Jun-2012 05:34:52 UTC] You have an error in your SQL syntax; 

And they get more complicated:


SELECT * FROM product_table
           WHERE productNum = 38/product.php?id=381\\\'
           ORDER BY display_seq, name
[19-Jun-2012 07:47:01 UTC] You have an error in your SQL syntax;

I went though all my code and I can’t find anywhere that I could possible have such a malformed query.

What clinched it for me are these queries:


WHERE product_id = 999999.9/**//*!30000union/**/all/**/select/**/(select/**/concat(0x7e,0x27,group_concat(column_name),0x27,0x7e)/**/from/**/`information_schema`.columns/**/where/**/table_schema=0x52656D696E64657273/**/and/**/table_name=0x7573657273),0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536*/--
         WHERE product_id = 999999.9/**//*!30000union/**/all/**/select/**/(select/**/concat(0x7e,0x27,count(column_name),0x27,0x7e)/**/from/**/`information_schema`.columns/**/where/**/table_schema=0x446F776E6C6F616473/**/and/**/table_name=0x507572636861736573),0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536*/--
         WHERE product_id = 999999.9/**//*!30000union/**/all/**/select/**/(select/**/concat(0x7e,0x27,group_concat(column_name),0x27,0x7e)/**/from/**/`information_schema`.columns/**/where/**/table_schema=0x446F776E6C6F616473/**/and/**/table_name=0x507572636861736573),0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536,0x31303235343830303536*/--

There’s absolutely no way I miscoded my query to get that garbage.

Since all my product numbers are integers, I changed the code to only run if the productNum is an integer. Seems to work.


if ( isset($_GET['num']) ) { $productNum  = mysql_real_escape_string($_GET['num']); }  else { $productNum  = '';} 

// Attempts have been made to exploit the database with long strings. 
// This stops it without filling up the error log.
if ( !is_numeric($productNum) ) $productNum = '1';

WordPress Twenty Eleven Theme

The Twenty Eleven Theme is a simple theme that works well for one of the sites I manage. Unfortunately, it is broken on iPads. The navigation on the right-hand side is all the way on the bottom. There is an easy fix.

Open the file /wp=content/themes/twentyeleven/header.php

A few lines down, the header block starts. Comment out (or delete) the line <meta name="viewport" content="width=device-width" /> and you are good to go. The code snippet below shows how I did it.


<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<!-- Comment out this line to have it work the same on iPads
<meta name="viewport" content="width=device-width" />-->

Note: In HTML a comment starts with <!-- and ends with --> so I’ve basically added a text comment and continued it to the next line that contains the offending code.

Concatenating Fields in MySql

I’m working on an iPhone/iPad product that needs to select a word based on the phonemes in it. I have about 40 phonemes that are stored in a MySql table as booleans. I didn’t feel like adding 40 attributes to my datamodel and then I’d have to write 40 conditionals to figure out whether a word contained the phoneme. Fortunately, MySql has a CONCAT function String Operator that I could use to concatenate all the fields into one. Now I can use REGEX expressions to query one field to determine if it is on or off for a specific word.

My first attempt to understand the operator was to just copy the example.


UPDATE `ArticIV_Phonemes` SET `Concatenated_phonemes` = CONCAT(`R`,`VocR`)

This isn’t what I wanted since it treats the stuff between the `s as literals. The result is RVocR. Not the values of the fields. Removing the `s works.


UPDATE `ArticIV_Phonemes` SET `Concatenated_phonemes` = CONCAT(R,VocR)

Now I get, 01, 10, 00, etc. which is what I want.

One minor complication. One of the phonemes is \or\ so naturally I used or as it’s name. My treats this name as a conditional and the CONCAT operation fails. I also used a hyphen in some of the names. My doesn’t like that either. It probably thinks it’s an arithmetic operator.

This is what I ended up using.


UPDATE `ArticIV_Phonemes` SET `Concatenated_phonemes` = CONCAT(R_Words,Vocalic_R_Words,S_Words,Z_Words,L_Words,Voiced_Th_Words,Voiceless_Th_Words,
Initial_R, Initial_R_Clusters, Medial_R, Medial_R_Clusters, 
VocR_aer, VocR_ar, VocR_eer, VocR_er, VocR_ier, VocR_or, VocR_our, VocR_yer, 
Initial_S, Initial_S_Clusters, Medial_S, Medial_S_Clusters, Final_S, Final_S_Clusters, 
Initial_Z, Initial_Z_Clusters, Medial_Z, Medial_Z_Clusters, Final_Z, Final_Z_Clusters, 
Initial_L, Initial_L_Clusters, Medial_L, Medial_L_Clusters, Final_L, 
Final_L_Clusters, Pre_vocalic_L, Post_vocalic_L, Pre_and_Post_V_L, 
Initial_Th, Medial_Th, 
Initial_Voiceless_Th, Initial_Voiceless_Th_Clusters,
Medial_Voiceless_Th, Final_Voiceless_Th, Final_Voiceless_Th_Clusters)

And here are the first few lines.


00001000000000000000000000000000010000010000000
00000000100000000000000000000000000000000000000
00000000000000000100000100000000000000000000000

When I want to find all of the words that have say, VocR_ier, in them I use this REGEX


SELECT *  FROM `ArticIV_Phonemes` WHERE `Concatenated_phonemes` REGEXP '[01]{9}1[01]{34}'

Basically this says, look for 9 occurrences of either a 0 or a 1. Then a 1. Then the rest of the string can be either 0 or 1. Note: you must put the final part in. The first part of the REGEX looks for a string of 9 0’s or 1’s followed by a 1. It doesn’t care where they are in the string. They could be the last 10 digits or the first.

One way to get around that is to tell the REGEX to start at the beginning of the string and look for the pattern.
Note the ^ in this example.


SELECT *  FROM `ArticIV_Phonemes` WHERE `Concatenated_phonemes` REGEXP '^[01]{9}1'

Both examples give the same result,


00000000110000000000000000000000000000000000000
00000000110000000000000000000000100000000000000
00000000100000000000000000000000010000010000000

Here’s what I used in a different database.
UPDATE `OldArtic_Categories` SET `Concatenated_Category` = CONCAT(A, B, M, D, C, E, F, G, H, I, K, L, O, P, Q, R, S, T, U, V, W, Y, Z, N)