Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Friday, August 02, 2013

Copy whole folders from Android with only MTP to the PC (over USB, using PHP and adb)

I've always wanted my mobile phone photos to appear on my PC "magically" when I come home and have a WiFi connection. I just haven't found a decent app for this yet.

In the mean time I used to copy files with USB cable. Back in the days the phone SD card was automatically mounted as a new disk so I could automate the process with "robocopy". But ever since phones switched to MTP protocol I had no way to use batch files to copy new pictures. I guess you know the painful process of using Windows Exprorer. Mouse-oriented, choose the source folder (MTP is slow), select all files, copy, go to the destination, paste. Manual work. Programmers don't like repetitive manual work.

But then I discovered that "adb" allows to "pull" and "push" files to the device over MTP. Unfortunately it doesn't support copying whole folders. It's been several years until I discovered an "adb" command to list files on the Android device. Whow! No I can script it to copy whole folders and automatically skip files which have been already copied.

Here's the result. The script is written in PHP. You run it like this:
> php acf.php /sdcard/DCIM/100ANDRO/ ./100ANDRO/

It will read all the files in the "100ANDRO" folder on the connected device and copy missing files to the specified folder on the PC.

Requirements:
1. PHP installed.
2. Android SDK installed.
3. "USB debugging" enabled on the phone.
4. Download the script below and try.

All links contain the same source code:
http://demo.php-pastebin.com/VsY8J7m6
http://codepad.org/bYC3JLZ8
http://ideone.com/3lljIB

Tuesday, June 05, 2012

International PHP Conference Berlin 2012

Just went for a short walk and ended-up going 5.6km around the center. Beautiful city Berlin.

Monday, April 30, 2012

Zend Optimizer on 1und1 hosting

Amazing but you can upload and enable your own PHP extensions on otherwise very limited 1und1 hosting. This way you can even upload Zend encoded scripts (binary PHP) and they will work. This is how.

Wednesday, February 15, 2012

Progress bar for a lengthy PHP process

If you ask an average web-developer if it's possible to make a running progress bar on the web-page while it's loading(!), most of them will say "no". This is understandable as practically all web-pages do not exhibit such progress bars and are in a sense "static" - the whole web-site is dynamically generated from a database, but each page is static except for AJAX changes due to user actions. Here I will show how a lengthy PHP process can show a running progress bar while the page is loading. Of course, it doesn't make sense for pages loading within a second or three, but those import/export/dump/analyze/recode/generate scripts which takes ages.

Thursday, October 06, 2011

PHP security tips

When programming in PHP please pay attention to the following tips. PHP code examples below try to make it impossible to make a mistake by hiding default (non-secure) variables and securing the data on the fly. When you get used to it, you will be able to smell insecure code.

Validating input

Don’t use values from $_REQUEST ($_GET, $_POST) directly assuming they contain data of the necessary data type. Validation or casting is required. For example intval($_REQUEST['id']) will make sure you have an integer. Using a dedicated class for reading URL/Form parameters will allow you to unset($_REQUEST) completely making sure you (or any other developer) is using casting or validation. For example:

class Request {
protected $data = array();

function __construct($other = NULL) {
     $this->data = $other ?: $_REQUEST;
     if (!$other) unset($_REQUEST);
}

function getInt($name) {
     return intval($this->data[$name]);
}

function getTrim($name) {
return trim($this->data[$name]);
}
}

$r = new Request();
echo $r->getInt('id');  // 15
echo $_REQUEST['id'];   // NULL

Note that once you created an instance of the Request class you can't use $_REQUEST anymore, which is good. Request could be made a singleton in order to be able to access it from multiple controllers.

Escape output

In order to prevent XSS you need to use htmlspecialchars() on every dynamic value which may come from the database, user input or web-service. Similarly to the example above it is recommended to close the possibility of using the values without escaping. For example the following View class (from MVC) doesn't allow accessing query results directly:


class View {
   protected $file;
   protected $caller;

   function __construct($file, $controller) {
        $this->file = $file;
        $this->caller = $controller;
   }

   function render() {
        $file = 'template/'.$this->file;
        ob_start();
        require($file);
        $content = ob_get_clean();
        return $content;
}

function __call($func, array $args) {
     $method = array($this->caller, $func);
     return call_user_func_array($method, $args);
}

function __get($var) {
     return htmlspecialchars($this->caller->$var);
}

}

$c = new Controller();
$c->dataXSS = '';
echo new View('output.phtml', $c);

// ---- output.phtml

Test View Controller


Must be escaped: dataXSS ?>


Note that in the template we can access data from the controller directly like this: dataXSS ?>, but this data will be processed with htmlspecialchars() invisibly for you.

Wednesday, July 06, 2011

PHP: Transparent Self-Caching of Objects

When programming PHP I work with objects. Some objects exist only once in memory. Either just because they are instantiated once or by using a Singleton design pattern. Other objects are multiple. In PHP, it being a stateless language, these objects need to be created with every page load. Having too many objects loading every time will slow down your web-site. What comes as a natural solution is caching.
Caching is often thought as an outside operation in regards to the objects. Thus it looks scary, you have to change the way you instantiate objects in your code (in many different places) so that it uses caching. Here I will present a way to implement an internal caching - objects will cache themselves almost seamlessly.