1#!/usr/bin/env php 2<?php 3 4/** 5 * Generate a number contacts with random data 6 * 7 * @version 3.1 8 * @author Thomas Bruederli <bruederli@kolabsys.com> 9 * 10 * Copyright (C) 2014, Kolab Systems AG <contact@kolabsys.com> 11 * 12 * This program is free software: you can redistribute it and/or modify 13 * it under the terms of the GNU Affero General Public License as 14 * published by the Free Software Foundation, either version 3 of the 15 * License, or (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU Affero General Public License for more details. 21 * 22 * You should have received a copy of the GNU Affero General Public License 23 * along with this program. If not, see <http://www.gnu.org/licenses/>. 24 */ 25 26define('INSTALL_PATH', __DIR__ . '/../../../'); 27ini_set('display_errors', 1); 28 29require_once INSTALL_PATH . 'program/include/clisetup.php'; 30 31function print_usage() 32{ 33 print "Usage: randomcontacts.sh [OPTIONS] USERNAME FOLDER\n"; 34 print "Create random contact for a given user in a specified folder.\n"; 35 print "-n, --num Number of contacts to be created, defaults to 50\n"; 36 print "-h, --host IMAP host name\n"; 37 print "-p, --password IMAP user password\n"; 38} 39 40// read arguments 41$opts = rcube_utils::get_opt(array( 42 'n' => 'num', 43 'h' => 'host', 44 'u' => 'user', 45 'p' => 'pass', 46 'v' => 'verbose', 47)); 48 49$opts['username'] = !empty($opts[0]) ? $opts[0] : $opts['user']; 50$opts['folder'] = $opts[1]; 51 52$rcmail = rcube::get_instance(rcube::INIT_WITH_DB | rcube::INIT_WITH_PLUGINS); 53$rcmail->plugins->load_plugins(array('libkolab')); 54ini_set('display_errors', 1); 55 56if (empty($opts['host'])) { 57 $opts['host'] = imap_host(); 58} 59 60if (empty($opts['username']) || empty($opts['folder']) || empty($opts['host'])) { 61 print_usage(); 62 exit; 63} 64 65// prompt for password 66if (empty($opts['pass'])) { 67 $opts['pass'] = rcube_utils::prompt_silent("Password: "); 68} 69 70// parse $host URL 71$a_host = parse_url($opts['host']); 72if ($a_host['host']) { 73 $host = $a_host['host']; 74 $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE; 75 $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143); 76} 77else { 78 $host = $opts['host']; 79 $imap_port = 143; 80} 81 82// instantiate IMAP class 83$IMAP = $rcmail->get_storage(); 84 85// try to connect to IMAP server 86if ($IMAP->connect($host, $opts['username'], $opts['pass'], $imap_port, $imap_ssl)) { 87 print "IMAP login successful.\n"; 88 $user = rcube_user::query($opts['username'], $host); 89 $rcmail->user = $user ?: new rcube_user(null, array('username' => $opts['username'], 'host' => $host)); 90} 91else { 92 die("IMAP login failed for user " . $opts['username'] . " @ $host\n"); 93} 94 95// get contacts folder 96$folder = kolab_storage::get_folder($opts['folder']); 97if (!$folder || empty($folder->type)) { 98 die("Invalid Address Book " . $opts['folder'] . "\n"); 99} 100 101$format = new kolab_format_contact; 102 103$num = $opts['num'] ? intval($opts['num']) : 50; 104echo "Creating $num contacts in " . $folder->get_resource_uri() . "\n"; 105 106for ($i=0; $i < $num; $i++) { 107 // generate random names 108 $contact = array( 109 'surname' => random_string(rand(1,2)), 110 'firstname' => random_string(rand(1,2)), 111 'organization' => random_string(rand(0,2)), 112 'profession' => random_string(rand(1,2)), 113 'email' => array(), 114 'phone' => array(), 115 'address' => array(), 116 'notes' => random_string(rand(10,200)), 117 ); 118 119 // randomly add email addresses 120 $em = rand(1,3); 121 for ($e=0; $e < $em; $e++) { 122 $type = array_rand($format->emailtypes); 123 $contact['email'][] = array( 124 'address' => strtolower(random_string(1) . '@' . random_string(1) . '.tld'), 125 'type' => $type, 126 ); 127 } 128 129 // randomly add phone numbers 130 $ph = rand(1,4); 131 for ($p=0; $p < $ph; $p++) { 132 $type = array_rand($format->phonetypes); 133 $contact['phone'][] = array( 134 'number' => '+'.rand(2,8).rand(1,9).rand(1,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9), 135 'type' => $type, 136 ); 137 } 138 139 // randomly add addresses 140 $ad = rand(0,2); 141 for ($a=0; $a < $ad; $a++) { 142 $type = array_rand($format->addresstypes); 143 $contact['address'][] = array( 144 'street' => random_string(rand(1,3)), 145 'locality' => random_string(rand(1,2)), 146 'code' => rand(1000, 89999), 147 'country' => random_string(1), 148 'type' => $type, 149 ); 150 } 151 152 $contact['name'] = $contact['firstname'] . ' ' . $contact['surname']; 153 154 if ($folder->save($contact, 'contact')) { 155 echo "."; 156 } 157 else { 158 echo "x"; 159 break; // abort on error 160 } 161} 162 163echo " done.\n"; 164 165 166 167function random_string($len) 168{ 169 $words = explode(" ", "The Hough transform is named after Paul Hough who patented the method in 1962. It is a technique which can be used to isolate features of a particular shape within an image. Because it requires that the desired features be specified in some parametric form, the classical Hough transform is most commonly used for the de- tection of regular curves such as lines, circles, ellipses, etc. A generalized Hough transform can be employed in applications where a simple analytic description of a features is not possible. Due to the computational complexity of the generalized Hough algorithm, we restrict the main focus of this discussion to the classical Hough transform. Despite its domain restrictions, the classical Hough transform hereafter referred to without the classical prefix retains many applications, as most manufac- tured parts and many anatomical parts investigated in medical imagery contain feature boundaries which can be described by regular curves. The main advantage of the Hough transform technique is that it is tolerant of gaps in feature boundary descriptions and is relatively unaffected by image noise."); 170 for ($i = 0; $i < $len; $i++) { 171 $str .= $words[rand(0,count($words)-1)] . " "; 172 } 173 174 return rtrim($str); 175} 176 177function imap_host() 178{ 179 global $rcmail; 180 181 $default_host = $rcmail->config->get('default_host'); 182 183 if (is_array($default_host)) { 184 $key = key($default_host); 185 $imap_host = is_numeric($key) ? $default_host[$key] : $key; 186 } 187 else { 188 $imap_host = $default_host; 189 } 190 191 // strip protocol prefix 192 $uri = parse_url($imap_host); 193 if (!empty($uri['host'])) { 194 return $uri['host']; 195 } 196} 197