Archive

Archive for the ‘Programmering’ Category

Add “Open command window here” option to Win10 context menu

June 3rd, 2019 No comments
  1. Open regedit, go to HKEY_CLASSES_ROOT\Directory\shell\cmd
  2. Right-click the cmd (folder) key, and click Permissions.
  3. Click the Advanced button.
  4. On “Advanced Security Settings,” click the Change link next to “Owner”.
  5. Type your account name in the provided field, click Check Names to verify you’re typing the account name correctly, and click OK.
  6. Check the Replace owner on subcontainers and objects option.
  7. Click Apply. Click OK.
  8. On “Permissions,” select the Administrators group.
  9. Under “Permissions for Administrators,” select Allow for the Full Control option.
  10. Click Apply. Click OK.
  11. Inside the cmd (folder) key, right-click the HideBasedOnVelocityId DWORD, and click Rename.
  12. Change the DWORD name from HideBasedOnVelocityId to ShowBasedOnVelocityId, and press Enter.

Once you complete these steps, using the Shift + Right-click combo on a folder will display the “Open command window here” option.

At any time, you can revert the changes following the same instructions, but on step No. 11, make sure to rename the DWORD from from ShowBasedOnVelocityId to HideBasedOnVelocityId.

Categories: Programmering, Windows Tags:

Prepare Linux for source compile

August 11th, 2012 No comments

1. Install packages to build the kernel modules

sudo apt-get install autoconf automake binutils make cpp gcc linux-headers-$(uname -r)

2. Find out where the kernel headers are (you may need this later)

ls -d /usr/src/linux-headers-$(uname -r)*/include

i.e. /usr/src/linux-headers-2.6.31-14-generic-pae/include

i.e. /lib/modules/2.6.31-14-generic-pae/build/include

3. Get the kernel source

sudo apt-get build-dep linux-image-$(uname -r)

sudo apt-get source linux-image-$(uname -r)

Categories: Bash, Linux, Programmering, Ubuntu Tags:

Backup a MySQL database

March 15th, 2012 No comments

– if you have root access you can do mysql dump.

– if you have filesystem access you may stop mysql / flush tables with read lock and then copy data files.

– dump each of the db one by one!! may be a shellscript will do the job! You may directly redirect it to target db if it’s accesible:

mysqldump -uUSER -pPASS –databases DB1 | mysql -uUSER -pPASS -hREMOTEHOST/IP

To dump all the databases you issue:

mysqldump -uUSER -pPASS –all-databases > dumpfile.sql

and then issue:

mysql -uUSER -pPASS < dumpfile.sql

Categories: Bash, Linux, Programmering Tags:

SQL syntax for deleting

November 17th, 2011 No comments

To delete one value:

DELETE FROM [dBName].[dbo].[table_name]
WHERE tag = ‘XXXXxxxxxxxxx01’

To use a *specific* list, you can do this:

DELETE FROM [dBName].[dbo].[table_name]
WHERE tag IN (‘XXXXxxxxxxxxx01’, ‘XXXXxxxxxxxxx05’, ‘XXXXxxxxxxxx11’)

Or, if possible, use a pattern:

DELETE FROM [dBName].[dbo].[table_name]
WHERE tag LIKE ‘XXXXxxxxxxxxx[0-9][0-9]%’

Categories: Programmering Tags:

PHP script to read attachements in mails

June 13th, 2011 No comments
#!/usr/bin/php
<?php

$debug = 0;  // 0 to disable;

//script will fetch an email identified by $msgid, and parse the its parts into an
//array $partsarray
//structure of array:
//$partsarray[<name of part>][<attachment/text>]
//if attachment- subarray is [filename][binary data]
//if text- subarray is [type of text(HTML/PLAIN)][text string]
//i.e.
//$partsarray[3.1][attachment][filename]=filename of attachment in part 3.1
//$partsarray[3.1][attachment][binary]=binary data of attachment in part 3.1
//$partsarray[2][text][type]=type of text in part 2
//$partsarray[2][text][string]=decoded text string in part 2
//$partsarray[not multipart][text][string]=decoded text string in message that isn't multipart
$domain = 'example.com';
$userpart = 'mailboxname';
$password = 'mysecret';
$server = 'mail.example.com';
$username = $userpart.'@'.$domain;
function parsepart($p,$i){
        global $tck,$link,$msgid,$partsarray,$db,$hdr;
        //where to write file attachments to:
        $filestore = '[full/path/to/attachment/store/(chmod777)]';
        //fetch part

        $part=imap_fetchbody($link,$msgid,$i);
        //if type is not text
        if ($p->type!=0){
                //DECODE PART
                //decode if base64
                if ($p->encoding==3)$part=base64_decode($part);
                //decode if quoted printable
                if ($p->encoding==4)$part=quoted_printable_decode($part);
                //no need to decode binary or 8bit!

                //get filename of attachment if present
                $filename='';
                // if there are any dparameters present in this part
                if (count($p->dparameters)>0){
                        foreach ($p->dparameters as $dparam){
                                if ((strtoupper($dparam->attribute)=='NAME') ||(strtoupper($dparam->attribute)=='FILENAME')) $filename=$dparam->value;
                        }
                }
                //if no filename found
                if ($filename==''){
                        // if there are any parameters present in this part
                        if (count($p->parameters)>0){
                                foreach ($p->parameters as $param){
                                        if ((strtoupper($param->attribute)=='NAME') ||(strtoupper($param->attribute)=='FILENAME')) $filename=$param->value;
                                }
                        }
                }
                //write to disk and set partsarray variable
                if ($filename!=''){
                        $partsarray[$i][attachment] = array('filename'=>$filename,'binary'=>$part);
                        $filename=str_replace("'","",$filename);
                        $filename=str_replace('"',"",$filename);
                        if (!file_exists($filestore.$tck)) system("mkdir $filestore".$tck);
                        $fp=fopen($filestore."$tck/".$filename,"w+");
                        fwrite($fp,$part);
                        fclose($fp);
                        $hdr .= "Attachment: $filename<br />\n";
                }
        } //end if type!=0

        //if part is text
        else if($p->type==0){
                //decode text
                //if QUOTED-PRINTABLE
                if ($p->encoding==4) $part=quoted_printable_decode($part);
                //if base 64
                if ($p->encoding==3) $part=base64_decode($part);

                //OPTIONAL PROCESSING e.g. nl2br for plain text
                //if plain text
                if (strtoupper($p->subtype)=='PLAIN') $plain=$part;
                //if HTML
                else if (strtoupper($p->subtype)=='HTML') $html=$part;
                $partsarray[$i][text] = array('type'=>$p->subtype,'string'=>$part);
        }

        //if subparts... recurse into function and parse them too!
        if (count($p->parts)>0){
                foreach ($p->parts as $pno=>$parr){
                        list($pln,$htm)= parsepart($parr,($i.'.'.($pno+1)));
                        $plain .= $pln;
                        $html .= $htm;
                }
        }
        return array($plain,$html);
}

function getUser($fromObj) {
        $user = $fromObj[0]->mailbox . "@" . $fromObj[0]->host;
        return $user;
}

//open resource
if ($debug) echo "Opening mailbox\n";
$link=imap_open("{".$server.":110/pop3/notls}INBOX",$username,$password);

$num = imap_num_msg($link);

if ($debug) echo "Looking through $num messages\n";

for ($msgid=1; $msgid<=$num; $msgid++) {

 if ($debug)   echo ".";
  $header = imap_headerinfo($link,$msgid);
  if ($debug>1) var_dump($header);

  $hdr = "From: ".$header->fromaddress."<br />\n";
  if ($header->fromaddress<>$header->senderaddress)  $hdr .= "Sender: ".$header->senderaddress."<br />\n";
  if ($header->fromaddress<>$header->reply_toaddress)  $hdr .= "ReplyTo: ".$header->reply_toaddress."<br />\n";
  $hdr .= "Subject: ".$header->subject."<br />\n";
  $hdr .= "Date: ".$header->date."<hr />\n";

  $html = $plain = "";

  if ($match=1) { //process

    $head=imap_headerinfo($link,$msgid);

    //fetch structure of message
    $s=imap_fetchstructure($link,$msgid);
    //see if there are any parts
    if (count($s->parts)>0){
        foreach ($s->parts as $partno=>$partarr){
                //parse parts of email
                list($p,$h) = parsepart($partarr,$partno+1);
                $plain .= $p;
                $html .= $h;
        }
    }
    //for not multipart messages
    else{
        //get body of message
        $text=imap_body($link,$msgid);
        //decode if quoted-printable
        if ($s->encoding==4) $text=quoted_printable_decode($text);
        //OPTIONAL PROCESSING
        if (strtoupper($s->subtype)=='PLAIN') $plain=$text;
        if (strtoupper($s->subtype)=='HTML') $html=$text;

/*
        $partsarray['not multipart'][text]=array('type'=>$s->subtype,'string'=>$text);
        $lines = explode("\n",$text);
        foreach ($lines as $line) {
        }
        print $text;
*/
    }
    if ($debug>2)  print_r($partsarray);
    imap_delete($link,$msgid);
    $body = $html ? $html : $plain;
    $cmt = addslashes($hdr.$body);
    $usr = getUser($head->from);
  }
}
imap_expunge($link);
imap_close($link);

?>
Categories: Programmering Tags:

Remove åäö from file names and folders

May 30th, 2011 No comments
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import os

def syntax(item):
# åäö->aao
nn=item.replace('å','a').replace('ä','a').replace('ö','o')
nn=nn.replace('Å','A').replace('Ä','A').replace('Ö','O')
nn=nn.replace('ü','u').replace('Ü','U')
nn=nn.replace('é','e').replace('É','E')

#special chars -> underscore
#    for uscore in ("'",'`',"'",'^','"','¨','(',')','[',']','=',
#                   ',',':',';','*','!','@','£','#','¤','$','%','&','{','}'):
#      nn=nn.replace(uscore,'_')

return nn

for root,dirs,files in os.walk('./',topdown=False):
for name in files: # First change name of the files
newname=os.path.join(root,syntax(name))
name=os.path.join(root,name)
os.rename(name,newname)

for name in dirs: # Then change name of the folders
newname=os.path.join(root,syntax(name))
name=os.path.join(root,name)
os.rename(name,newname)
Categories: Bash, Linux, Programmering Tags:

Read and write to registry in Windows 7 with VBS

May 16th, 2011 No comments

Here’s an example of how you can use VBS script to access the registry in Windows. Tested in Win7.

'_____________________________________________________________________________
' REMOVE POP-UP'S
'
' v1.00 2011-05-16 Lenovo Fingerprint software /Tobias Holm / http://xpro.se
'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
lRet = RegWriteW7("HKEY_CURRENT_USER\Software\Protector Suite\Control Center\1.0\ShowOnStartup", "0", "REG_DWORD")
msgbox "WRITE:" & lRet & " ERR:" & Err
lRet = RegReadW7("HKEY_CURRENT_USER\Software\Protector Suite\Control Center\1.0\ShowOnStartup")
msgbox "READ:" & lRet & " ERR:" & Err
lRet = RegDelW7("HKEY_CURRENT_USER\Software\Protector Suite\Control Center\1.0\ShowOnStartup")
msgbox "DEL:" & lRet & " ERR:" & Err

'______________________________________________________________________________________
' WRITE TO REGISTRY / Tobias Holm 2011-05-16 / http://xpro.se
' Original idea Guy Thomas http://computerperformance.co.uk (Version 1.2 - March 2007)
' Creates the registry keys if they doesn't exists.
' Ex: lRet = RegWriteW7("HKEY_CURRENT_USER\Software\Intel\Count", "35", "REG_DWORD")
' Returns the exit code from Shell.RegWrite
'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Function RegWriteW7(x_sPath, x_sValue, x_sType)
On Error Resume Next
Dim x_objShell
Set x_objShell = CreateObject("WScript.Shell")
Err.Clear
RegWriteW7 = x_objShell.RegWrite(x_sPath, x_sValue, x_sType)
Set x_objShell = Nothing
End Function

'______________________________________________________________________________________
' READ FROM REGISTRY / Tobias Holm 2011-05-16 / http://xpro.se
' Original idea Guy Thomas http://computerperformance.co.uk (Version 1.2 - March 2007)
' Ex: lRet = RegReadW7("HKEY_CURRENT_USER\Software\Intel\Count")
' Returns the value from Shell.RegRead
'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Function RegReadW7(x_sPath)
On Error Resume Next
Dim x_objShell
Set x_objShell = CreateObject("WScript.Shell")
Err.Clear
RegReadW7 = x_objShell.RegRead(x_sPath)
Set x_objShell = Nothing
End Function

'______________________________________________________________________________________
' DELETE FROM REGISTRY / Tobias Holm 2011-05-16 / http://xpro.se
' Original idea Guy Thomas http://computerperformance.co.uk (Version 1.2 - March 2007)
' Deletes a registry value. Doesn't delete the registry keys though.
' Ex: lRet = RegDelW7("HKEY_CURRENT_USER\Software\Intel\Count")
' Returns the exit code from Shell.RegDelete
'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Function RegDelW7(x_sPath)
On Error Resume Next
Dim x_objShell
Set x_objShell = CreateObject("WScript.Shell")
Err.Clear
RegDelW7 = x_objShell.RegDelete(x_sPath)
Set x_objShell = Nothing
End Function
Categories: Programmering, Windows Tags:

Reset MySQL root Password

May 8th, 2011 No comments

Ref: http://www.debian-administration.org/articles/442

Posted by Steve on Thu 28 Sep 2006 at 09:12

Resetting the root password of a MySQL database is trivial if you know the current password if you don’t it is a little tricker. Thankfully it isn’t too difficult to fix, and here we’ll show one possible way of doing so.

If you’ve got access to the root account already, because you know the password, you can change it easily:

steve@steve:~$ mysql --user=root --pass mysql
Enter password:

mysql> update user set Password=PASSWORD('new-password-here') WHERE User='root';
Query OK, 2 rows affected (0.04 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

mysql> exit
Bye

However if you don’t know the current password this approach will not work – you need to login to run any commands and without the password you’ll not be able to login!

Thankfully there is a simple solution to this problem, we just need to start MySQL with a flag to tell it to ignore any username/password restrictions which might be in place. Once that is done you can successfully update the stored details.

First of all you will need to ensure that your database is stopped:

root@steve:~# /etc/init.d/mysql stop

Now you should start up the database in the background, via the mysqld_safe command:

root@steve:~# /usr/bin/mysqld_safe --skip-grant-tables &
[1] 6702
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6763]: started

Here you can see the new job (number “1”) has started and the server is running with the process ID (PID) of 6702.

Now that the server is running with the --skip-grant-tables flag you can connect to it without a password and complete the job:

root@steve:~$ mysql --user=root mysql
Enter password:

mysql> update user set Password=PASSWORD('new-password-here') WHERE User='root';
Query OK, 2 rows affected (0.04 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

mysql> exit
Bye

Now that you’ve done that you just need to stop the server, so that you can go back to running a secure MySQL server with password restrictions in place. First of all bring the server you started into the foreground by typing “fg“, then kill it by pressing “Ctrl+c” afterwards.

This will now allow you to start the server:

root@steve:~# /etc/init.d/mysql start
Starting MySQL database server: mysqld.
Checking for corrupt, not cleanly closed and upgrade needing tables..

Now everything should be done and you should have regained access to your MySQL database(s); you should verify this by connecting with your new password:

root@steve:~# mysql --user=root --pass=new-password-here
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5 to server version: 5.0.24a-Debian_4-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> exit
Bye
Categories: Linux, Programmering Tags:

Update code with GIT or SVN

April 29th, 2011 No comments

To update your source code and recompile SVN:

cd {dir_with_source}
svn up
make clean

http://blog.avirtualhome.com/2009/05/29/how-to-compile-mplayer-with-vdpau-support-on-ubuntu/

Example to compile ffmpeg after source update:

./configure --enable-gpl --enable-version3 --enable-nonfree --enable-postproc \
    --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb \
    --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis \
    --enable-libx264 --enable-libxvid --enable-x11grab
make
sudo checkinstall --pkgname=ffmpeg --pkgversion="5:$(date +%Y%m%d%H%M)-git" --backup=no \
    --deldoc=yes --fstrans=no --default
hash x264 ffmpeg ffplay ffprobe

To compile MPlayer you might need some extra directories from ffmpeg. Example:

cd ~/code/mplayer
mkdir libavutil
cp -r /home/holm/code/ffmpeg/libavutil/* libavutil

To update your source code and recompile GIT:

cd {dir_with_source}
make distclean
git pull
Categories: Programmering Tags:

Redirect all but one IP in PHP

April 28th, 2011 No comments

Example if you’d like to redirect all web visitors except one IP:

<?php
  if ( $_SERVER["REMOTE_ADDR"] != "192.0.0.0" ) {
      header("Location: http://otherurl.example.com");
      exit(0);
  }
?>

Or use mod_rewrite in Apache:

RewriteCond %{REMOTE_ADDR} !^1.2.3.4$
RewriteRule /?(.*)$ http://my.redirect.com/$1
Categories: Programmering Tags: