Connecting To Panic’s Transmit Favorites From The Command Line

February 17, 2012 | Command Line, OSX | 0 Comments

I am a fan of running my apps my own way. Sometimes, I want to be able to write a shell script around an action, or just have an alias to run a command from the command line. Whatever the reason, I like to tinker, and recently I needed a way to connect directly into one of my Transmit favorites from the command line.

The Favorites

My first stop was to find out where and how Transmit stores the favorites. Transmit’s favorites are stored here:

1
/Users/***USERNAME***/Library/Application Support/Transmit/Metadata/

Where ***USERNAME*** is your username. Each .favoriteMetadata file in that directory is a favorite you have stored (or a duplicate or autosave of a favorite). You can edit them and get an idea of what they are used for, or you can just connect to each one individually to figure it out.

The Command

The command to load a favorite into Transmit automatically is as follows:

1
/Applications/Transmit.app/Contents/MacOS/Transmit /Users/***USERNAME***/Library/Application Support/Transmit/Metadata/***FILENAME***.favoriteMetadata

It really is as simple as that. If you have any questions or problems, feel free to leave a comment, or shoot me an email using the contact page.

How To Anonymize Your cURL Requests Using Tor

December 9, 2011 | PHP | 0 Comments

Note: This post assumes that you already have Tor installed and configured on your server. If not, visit the Tor Project Homepage for instructions on how to set it up.

I love writing web crawlers and data aggregators in PHP. They say knowledge is power, and I find it exciting to write applications that allow you to gather up as much knowledge as possible. Sometimes, though, you want to gather your data without revealing your identity to your target. This can be for both good and bad reasons, but let’s assume it is for a good reason that you need to keep hidden. The Tor Project is a great way to accomplish this task. Tor allows you to anonymously browse the web, and you can configure the applications on your computer to run through the Tor network. The script below outlines the process it takes to randomly refresh your IP address on Tor, and then run a cURL request through your local Tor proxy, providing a unique identity and anonymity every time you run the script.

For this particular example, I am sending a request to http://whatismyip.org to demonstrate the changing IP address. If you have a greater understanding of cURL, there are many more steps that can be taken to mask your identity as well, but I won’t go into that here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Connect To Local Tor Proxy And Refresh IP Address
$ip = '127.0.0.1';
$port = '9051';
$auth = 'PASSWORD';
$command = 'signal NEWNYM';

$fp = fsockopen($ip,$port,$error_number,$err_string,10);
if(!$fp) {  echo "ERROR: $error_number : $err_string";
    return false;
} else {
    fwrite($fp,"AUTHENTICATE \"".$auth."\"\n");
    $received = fread($fp,512);
    fwrite($fp,$command."\n");
    $received = fread($fp,512);
}

fclose($fp);

// Submit Curl Request Through Tor Proxy At 127.0.0.1:9050
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://whatismyip.org");
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:9050");
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$response = curl_exec($ch);

Custom CodeIgniter Model Class

December 8, 2011 | PHP | 0 Comments

For the past year, I have been an adamant CodeIgniter developer. The one thing that I have found lacking, however, is CodeIgniter’s model class. I am a big fan of the simplicity of CakePHP’s ORM structure, and wrote this model class to (slightly) mimic it. Some of the functionality in this extended model is inspired by Emran Hasan’s CodeIgniter Extended Model Class.

Note: PHP >=5.3.4 Required

Installation

To install this extended model class, copy the code below into the MY_Model.php in the application/core/ directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php

class MY_Model extends CI_Model{
    /**
     * The table name
     *
     * @var string
     * @access protected
     */

    protected $table = '';

    /**
     * The table's primary key
     *
     * @var string
     * @access protected
     */

    protected $primary_key = 'id';

    /**
     * The current object's id
     *
     * @var int
     * @access protected
     */

    protected $id = null;

    /**
     * The current object's data
     *
     * @var array
     * @access protected
     */

    protected $vals = array();

    /**
     * The table's field names
     *
     * @var array
     * @access protected
     */

    protected $fields = array();

    /**
     * The current object's data
     *
     * @var array
     * @access protected
     */

    protected $extra = array();

    /**
     * Constructor
     *
     * @access public
     */

    public function __construct($vals = NULL){
        parent::__construct();

        if($this->table != ''){
            $this->fields = $this->CI()->db->list_fields($this->table);
        }

        if(is_array($vals)) {
            $this->vals = array();

            foreach($vals as $key => $value) {
                if(in_array($key, $this->fields)){
                    $this->vals[$key] = $value;
                } else {
                    $this->extra[$key] = $value;
                }
            }

            if(array_key_exists($this->primary_key, $this->vals)) {
                $this->id = $this->vals[$this->primary_key];
            }
        }
    }


    /**
     * Function to access the CodeIgniter Instance
     *
     * @access public
     */

    public static function &CI() { return get_instance(); }

    /**
     * Insert entry into the database.
     *
     * @return entry object
     * @access public
     */

    public function insert($parameters){
        if (!$this->CI()->db->table_exists($this->table)) {
            throw new Exception("Table does not exist.");
        } else {
            foreach($parameters as $key=>$param){
                if(!in_array($key, $this->fields)){
                    unset($parameters[$key]);
                }
            }

            if($this->CI()->db->insert($this->table, $parameters)){
                $this->id = $this->CI()->db->insert_id();
                $query = $this->CI()->db->get_where($this->table, array($this->primary_key => $this->id));
                $result = $query->result_array();
                $result = $result[0];

                if($class = get_called_class()) {
                    $obj = new $class($result);
               
                    return($obj);
                } else {
                    throw new Exception("Model class not found.");
                }
            } else {
                throw new Exception("Could not insert into table.");
            }
        }
    }

    /**
     * Save current object to the database.
     *
     * @return entry object
     * @access public
     */

    public function save(){
        if($this->CI()->db->update($this->table, $this->vals, array($this->primary_key => $this->id))){
            if($query = $this->CI()->db->get_where($this->table, array($this->primary_key => $this->id))){
                $result = $query->result_array();
                $result = $result[0];

                if($class = get_called_class()) {
                    $obj = new $class($result);

                    return($obj);
                } else {
                    throw new Exception("Model class not found.");
                }
            } else {
                throw new Exception("Unable to find record.");
            }
        } else {
            throw new Exception("Unable to update record.");
        }
    }

    /**
     * Delete current object from the database.
     *
     * @return object
     * @access public
     */

    public function delete(){
        return $this->CI()->db->delete($this->table, array($this->primary_key => $this->id));
    }

    /**
     * Grab the total results of a find query.
     *
     * @return int
     * @access public
     */

    public function findCount($params = NULL){
        $response = $this->find($params, "COUNT(*) as count");
        return $response->extra['count'];
    }

    /**
     * Returns an array of objects based on given query parameters.
     *
     * @return object array
     * @access public
     */

    public function findAll($params = NULL, $fields = "*", $orderby = NULL, $groupby = NULL, $limit = NULL){
        if(isset($orderby) && $orderby != NULL){
            if(is_string($orderby)) {
                $this->CI()->db->order_by($groupby);
            } else if(is_array($orderby) || is_object($orderby)) {
                foreach((array)$orderby as $key=>$value){
                    $this->CI()->db->order_by($key, $value);
                }
            } else {
                throw new Exception("Invalid order by type.");
            }
        }

        if(isset($groupby) && $groupby != NULL){
            if(is_string($groupby)) {
                $this->CI()->db->group_by($groupby);
            } else if(is_array($groupby) || is_object($groupby)) {
                $this->CI()->db->group_by((array)$groupby);
            } else {
                throw new Exception("Invalid group by type.");
            }
        }

        if(isset($limit) && $limit != NULL){
            if(is_int($limit)) {
                $this->CI()->db->limit($limit);
            } else if(is_array($limit) || is_object($limit)) {
                $limit = (array)$limit;
                $this->CI()->db->limit($limit[0], $limit[1]);
            } else {
                throw new Exception("Invalid limit type.");
            }
        }

        if(!isset($fields) || $fields == NULL || $fields == "*"){
            $fields = "*";
            $this->CI()->db->select($fields);
        } else if(is_string($fields)) {
            $this->CI()->db->select($fields);
        } else if(is_array($fields)) {
            $fields = implode(",", $fields);
            $this->CI()->db->select($fields);
        } else {
            throw new Exception("Invalid fields type.");
        }

        if(!isset($params) || $params == NULL){
            $query = $this->CI()->db->get($this->table);
        } else if(is_scalar($params)) {
            $query = $this->CI()->db->get_where($this->table, array($this->primary_key => $params));
            $this->id = $params;
        } else if(is_array($params) || is_object($params)) {
            $query = $this->CI()->db->get_where($this->table, (array)$params);
        } else {
            throw new Exception("Invalid primary key type.");
        }

        if($query->num_rows > 0){
            $results = $query->result_array();
            $objects = array();

            foreach($results as $result){
                if($class = get_called_class()) {
                    $obj = new $class($result);

                    $objects[] = $obj;
                } else {
                    throw new Exception("Model class not found.");
                }
            }

            return $objects;
        } else {
            return false;
        }
    }

    /**
     * Return an object containing one result based on the given query.
     *
     * @return object
     * @access public
     */

    public function find($params = NULL, $fields = NULL){
        if(!isset($fields) || $fields == NULL || $fields == "*"){
            $fields = "*";
            $this->CI()->db->select($fields);
        } else if(is_string($fields)) {
            $this->CI()->db->select($fields);
        } else if(is_array($fields)) {
            $fields = implode(",", $fields);
            $this->CI()->db->select($fields);
        } else {
            throw new Exception("Invalid fields type.");
        }

        if(!isset($params) || $params == NULL){
            $query = $this->CI()->db->order_by($this->primary_key." DESC")->limit(1)->get($this->table);
        } else if(is_scalar($params)) {
            $query = $this->CI()->db->get_where($this->table, array($this->primary_key => $params));
            $this->id = $params;
        } else if(is_array($params) || is_object($params)) {
            $query = $this->CI()->db->get_where($this->table, (array)$params);
        } else {
            throw new Exception("Invalid primary key type.");
        }
       
        if($query->num_rows > 0){
            $result = $query->result_array();
            $result = $result[0];

            if($class = get_called_class()) {
                $obj = new $class($result);

                return($obj);
            } else {
                throw new Exception("Model class not found.");
            }
        } else {
            return false;
        }
    }

    /**
     * Set data array values.
     * Reference: http://php.net/manual/en/language.oop5.overloading.php
     *
     * @access public
     */

    public function __set($name, $value) {
        if(array_search($name, $this->fields) !== FALSE) {
            $this->vals[$name] = $value;

            if($name == $this->primary_key)
                $this->id = $value;

            return;
        }

        $this->$name = $value;
    }

    /**
     * Get values from the data array.
     * Reference: http://www.php.net/manual/en/language.oop5.overloading.php#97918
     *
     * @return reference value
     * @access public
     */

    public function &__get($name) {
        $f = null;

        if(array_search($name, $this->fields) !== FALSE) {
            return $this->vals[$name];
        } else {
            return $f;
        }
    }
}

Continue Reading…

WHOIS Search Bookmarklet

September 7, 2011 | Code, Javascript | 0 Comments

Lately, I’ve been experimenting with Javascript and bookmarklets. Bookmarklets are bookmarks that you place in your bookmarks bar. They execute a piece of code, rather than load a website, when they are clicked. While I was working at a name.com, I worked on a quick bookmarklet that helped me get WHOIS information on a website. Using it is as simple as navigating to a website, and clicking on the bookmarklet. As soon as you click it, you will be taken to a WHOIS information page for that domain, provided by Who.is. To install the bookmarklet, just drag the button below into your bookmarks bar.

The Code

1
javascript:(function(){var url=location.href; var baseURL=url.substring(0, url.indexOf('/', 14)); document.location.href="http://who.is/whois/"+baseURL.replace(/http:\/\//, '').replace(/https:\/\//, '');})();

How To Compile Envy MUD

September 7, 2011 | C, MUD | 0 Comments

I have always been fascinated with MUDs, also known as Multi-User Dungeons. They are the World of Warcraft before there was a world to craft war in. It’s an interesting mesh between computers and Dungeons and Dragons. Imagination is an important factor, and from my experience, they are a great place for beginning programmers to learn how to code. I first learned how to code in C by programming for a few MUDs.

Compiling your first MUD can be a scary experience, though. Many of the codebases are pre-y2k, thus making some of the processes obsolete. For this demonstration, I will be using the EnvyMUD source code. It is widely used enough that many people can relate to it, and being directly related to DIKUMUD, the processes will be very similar for other DIKU related subservers. The first step will be to download the MUD source code. You can get it from the MUDBytes codebase repository here. The version of Envy I will be using is Envy 2.0 on a current Ubuntu server.

After downloading the Envy source code, you will need to un-tar and un-gzip it:

1
tar -xvzf envy-20.tar.gz

Next, navigate to the “src” directory:
Continue Reading…

Pump Audio Placement Scraper

July 23, 2011 | Code, PHP | 0 Comments

A few weeks ago, I had a meeting with the co-founders of Next Big Sound. Next Big Sound is a service that aggregates data for musicians (including plays and fan counts). As an act of good faith (and an attempt at getting hired at this incredible company), I created several scripts to aggregate data from various online sources that Next Big Sound doesn’t yet support. This particular script is used to aggregate fan counts from artists on Pump Audio.

Output

1
2
3
4
5
// Commercial Placements
object(stdClass)#4 (1) {
  ["total"]=>
  int(15)
}

Continue Reading…

Pandora Fan Count Scraper

July 23, 2011 | Code, PHP | 0 Comments

A few weeks ago, I had a meeting with the co-founders of Next Big Sound. Next Big Sound is a service that aggregates data for musicians (including plays and fan counts). As an act of good faith (and an attempt at getting hired at this incredible company), I created several scripts to aggregate data from various online sources that Next Big Sound doesn’t yet support. This particular script is used to aggregate fan counts from artists on Pandora.

Output

1
2
3
4
5
// Fan Counts
object(stdClass)#4 (1) {
  ["total"]=>
  string(6) "174632"
}

Continue Reading…

Official.fm Stats Scraper

July 23, 2011 | Code, PHP | 0 Comments

A few weeks ago, I had a meeting with the co-founders of Next Big Sound. Next Big Sound is a service that aggregates data for musicians (including plays and fan counts). As an act of good faith (and an attempt at getting hired at this incredible company), I created several scripts to aggregate data from various online sources that Next Big Sound doesn’t yet support. This particular script is used to aggregate fan counts and song stats from artists on Official.fm.

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
// Song Stats
object(stdClass)#4 (2) {
  ["plays"]=>
  string(4) "6083"
  ["downloads"]=>
  string(4) "4655"
}

// Fan Counts
object(stdClass)#5 (1) {
  ["total"]=>
  string(2) "33"
}

Continue Reading…

MixCloud Stats Scraper

July 23, 2011 | Code, PHP | 0 Comments

A few weeks ago, I had a meeting with the co-founders of Next Big Sound. Next Big Sound is a service that aggregates data for musicians (including plays and fan counts). As an act of good faith (and an attempt at getting hired at this incredible company), I created several scripts to aggregate data from various online sources that Next Big Sound doesn’t yet support. This particular script is used to aggregate fan counts and song stats from artists on MixCloud.com.

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Song Stats
array(59) {
  [0]=>
  object(stdClass)#4 (4) {
    ["name"]=>
    string(46) "DCR049 - Drumcode Radio - Live from Awakenings"
    ["plays"]=>
    string(4) "5906"
    ["favorites"]=>
    string(2) "97"
    ["tweets"]=>
    string(2) "53"
  }
  [1]=>
  object(stdClass)#6 (4) {
    ["name"]=>
    string(45) "DCR048 - Drumcode Radio - Gary Beck Guest Mix"
    ["plays"]=>
    string(4) "5445"
    ["favorites"]=>
    string(3) "109"
    ["tweets"]=>
    string(2) "53"
  }
  [2]=>
  object(stdClass)#7 (4) {
    ["name"]=>
    string(42) "DCR047 - Drumcode Radio - Live From Row 14"
    ["plays"]=>
    string(4) "5930"
    ["favorites"]=>
    string(2) "91"
    ["tweets"]=>
    string(2) "40"
  }
}

// Fan Count
object(stdClass)#62 (1) {
  ["total"]=>
  string(4) "5138"
}

Continue Reading…

iLike Fan Count Scraper

July 23, 2011 | Code, PHP | 0 Comments

A few weeks ago, I had a meeting with the co-founders of Next Big Sound. Next Big Sound is a service that aggregates data for musicians (including plays and fan counts). As an act of good faith (and an attempt at getting hired at this incredible company), I created several scripts to aggregate data from various online sources that Next Big Sound doesn’t yet support. This particular script is used to aggregate fan counts from artists on iLike.com.

Output

1
2
3
4
object(stdClass)#4 (1) {
  ["total"]=>
  string(6) "721998"
}

Continue Reading…