<?php

header('Content-Type: application/json');
if (isset($_GET['i']) && isset($_GET['t'])) {
    // track plugin install
    file_put_contents("../data/" . date("Y-W") . ".log", $_SERVER['REMOTE_ADDR'] . "," . lookupGeoLocation($_SERVER['REMOTE_ADDR']) . "," . $_GET['t'] . "," . $_GET['i'] . "," . $_GET['r'] . "\r\n", FILE_APPEND | LOCK_EX);
} else {
    if (isset($_GET['o']) && $_GET['o'] != '') {
        // optout existing plugins
        foreach (split(",", $_GET['o']) as $data) {
            $line = split(":", $data);
            file_put_contents("../data/" . date("Y-W") . ".log", $_SERVER['REMOTE_ADDR'] . "," . lookupGeoLocation($_SERVER['REMOTE_ADDR']) . "," . $line[0] . "," . $line[1] . ",\r\n", FILE_APPEND | LOCK_EX);
        }
    }
    // get current market
    $data = getcwd() . '/../data';
    $ip = array();
    $country = array();
    $type = array();
    $ext = array();
    foreach (scandir($data) as $name) {
        if ($name == '.' || $name == '..' || $name == '.htaccess') {
            continue;
        }
        if (is_file($data . '/' . $name)) {
            $file = fopen($data . '/' . $name, 'r');
            while (!feof($file)) {
                $line = split(",", fgets($file));
                if (!in_array($line[0], $ip)) {
                    array_push($ip, $line[0]);
                    if (!array_key_exists(strtoupper($line[1]), $country)) {
Exemple #2
0
    function createAccount() {

        if (!$this->init) return false;


        if (!$_REQUEST['email']) {
            $return=array('success'       => false,
                          'error'         => 'value_error',
                          'error_message' => 'Missing email address'
                          );
            print (json_encode($return));
            return false;
        }

        if (!$this->checkEmail($_REQUEST['email'])) {
            $return=array('success'       => false,
                          'error'         => 'value_error',
                          'error_message' => 'Invalid email address'
                          );
            print (json_encode($return));
            return false;
        }

        if (!$_REQUEST['password']) {
            $return=array('success'       => false,
                          'error'         => 'value_error',
                          'error_message' => 'Missing password'
                          );
            print (json_encode($return));
            return false;
        }

        if (!$_REQUEST['display_name']) {
            $return=array('success'       => false,
                          'error'         => 'value_error',
                          'error_message' => 'Missing display name'
                          );
            print (json_encode($return));
            return false;
        }

		$username=strtolower(trim($_REQUEST['username']));

        if (!preg_match("/^[1-9a-z][0-9a-z_.-]{2,64}[0-9a-z]$/",$username)) {
            $return=array('success'       => false,
                          'error'         => 'value_error',
                          'error_message' => 'The username must contain at least 4 lowercase alpha-numeric . _ or - characters and must start and end with a positive digit or letter'
                          );
            print (json_encode($return));
            return false;
        }

        $sip_address=$username.'@'.$this->sipDomain;

        if ($this->create_customer && !$_REQUEST['owner']) {
        	// create owner id
            $customerEngine           = 'customers@'.$this->customerEngine;
            $this->CustomerSoapEngine = new SoapEngine($customerEngine,$this->soapEngines,$this->customerLoginCredentials);
            $_customer_class          = $this->CustomerSoapEngine->records_class;
            $this->customerRecords    = new $_customer_class($this->CustomerSoapEngine);
            $this->customerRecords->html=false;

            $properties=$this->customerRecords->setInitialCredits(array('sip_credit'         => 1,
                                                                        'sip_alias_credit'   => 1,
                                                                        'email_credit'       => 1
                                                                        )
                                                                  );
            if (preg_match("/^(\w+)\s+(\w+)$/",$_REQUEST['display_name'],$m)) {
                $firstName = $m[1];
                $lastName  = $m[2];
            } else {
                $firstName = $_REQUEST['display_name'];
                $lastName  = 'Blink';
            }

            $this->log_action("Create owner account ($firstname $lastname) ");

            $timezone=$_REQUEST['tzinfo'];

            if (!in_array($timezone, $this->timezones)) {
            	$timezone=$this->default_timezone;
            }

            $location = lookupGeoLocation($_SERVER['REMOTE_ADDR']);

            $customer=array(
                         'firstName'  => $firstName,
                         'lastName'   => $lastName,
                         'timezone'   => $timezone,
                         'password'   => trim($_REQUEST['password']),
                         'email'      => trim($_REQUEST['email']),
                         'country'    => $location['country_code'],
                         'state'      => $location['region'],
                         'city'       => $location['city'],
                         'properties' => $properties
                        );

            if ($this->customer_belongs_to_reseller) {
                $customer['reseller'] =intval($this->reseller);
            }

            if ($location['country_code'] == 'NL') {
                $customer['tel'] = '+31999999999';
            } else if ($location['country_code'] == 'US') {
                $customer['tel'] = sprintf ("+1%s9999999",$location['area_code']);
            } else {
                $customer['tel'] = '+19999999999';
            }

            $_customer_created=false;

            $j=0;

            while ($j < 3) {

                $username.=RandomString(4);

                $customer['username']=$username;

                if (!$result = $this->customerRecords->addRecord($customer)) {
                    if ($this->customerRecords->SoapEngine->exception->errorcode != "5001") {
                        $return=array('success'       => false,
                        	          'error'         => 'internal_error',
                                      'error_message' => 'failed to create non-duplicate customer entry'
                                      );
                        print (json_encode($return));
                        return false;
                    }
                } else {
                    $_customer_created=true;
                    break;
                }

                $j++;
            }

            if (!$_customer_created) {

                if ($this->sipRecords->soap_error_description) {
                    $_msg=$this->sipRecords->soap_error_description;
                } else {
                    $_msg='failed to create customer account';
                }

                $return=array('success'       => false,
                              'error'         => 'internal_error',
                              'error_message' => $_msg
                              );
                print (json_encode($return));
                return false;
            } else {
                $this->log_action("Owner account created (". $customer['username'].")");
            }

            $owner=$result->id;

            if (!$owner) {
                $return=array('success'       => false,
                              'error'         => 'internal_error',
                              'error_message' => 'failed to obtain a new owner id'
                              );
                print (json_encode($return));
                return false;
            } else {
                $this->log_action("Owner id is $owner (". $customer['username'].")");
            }
        } else if (is_numeric($_REQUEST['owner']) && $_REQUEST['owner'] != 0 ) {
            $owner=intval($_REQUEST['owner']);
        } else {
                $return=array('success'       => false,
                              'error'         => 'internal_error',
                              'error_message' => 'no owner information provided'
                              );
                print (json_encode($return));
                return false;
        }

        // create SIP Account
        $sipEngine           = 'sip_accounts@'.$this->sipEngine;

        $this->SipSoapEngine = new SoapEngine($sipEngine,$this->soapEngines,$this->sipLoginCredentials);
        $_sip_class          = $this->SipSoapEngine->records_class;
        $this->sipRecords    = new $_sip_class($this->SipSoapEngine);
        $this->sipRecords->html=false;


        $sip_properties[]=array('name'=> 'ip',                 'value' => $_SERVER['REMOTE_ADDR']);
        $sip_properties[]=array('name'=> 'registration_email', 'value' => $_REQUEST['email']);

        $languages=array("en","ro","nl","es","de");

        if (isset($_REQUEST['lang'])){
            if (in_array($_REQUEST['lang'],$languages)) {
                $sip_properties[]=array('name'=> 'language',           'value' => $_REQUEST['lang']);
            }
        }

        if (strlen($timezone)) {
            $sip_properties[]=array('name'=> 'timezone', 'value' => $timezone);
        }

        if (strlen($user_agent)) {
            $sip_properties[]=array('name'=> 'user_agent', 'value' => trim(urldecode($user_agent)));
        }

        $sipAccount = array('account'   => $sip_address,
                            'fullname'  => $_REQUEST['display_name'],
                            'email'     => $_REQUEST['email'],
                            'password'  => $_REQUEST['password'],
                            'timezone'  => $timezone,
                            'prepaid'   => $this->prepaid,
                            'pstn'      => $this->allow_pstn,
                            'quota'     => $this->quota,
                            'owner'     => intval($owner),
                            'groups'    => $this->groups,
                            'properties'=> $sip_properties
                            );

        $this->log_action("Create SIP account ($sip_addres)");

        if (!$result = $this->sipRecords->addRecord($sipAccount)) {
            if ($this->sipRecords->SoapEngine->exception->errorstring) {

                if ($this->sipRecords->SoapEngine->exception->errorcode == 1011) {
                    $return=array('success'       => false,
                                  'error'         => 'user_exists',
                                  'error_message' => $this->sipRecords->SoapEngine->exception->errorstring
                                  );
                } else {
                    $return=array('success'       => false,
                                  'error'         => 'internal_error',
                                  'error_message' => $this->sipRecords->SoapEngine->exception->errorstring
                                  );
                }

            } else {
                $_msg='failed to create sip account';
                $return=array('success'       => false,
                              'error'         => 'internal_error',
                              'error_message' => $_msg
                              );
            }


            print (json_encode($return));

            $_dictionary=array('customer'=>intval($owner),
                               'error'   => 'internal_error',
                               'confirm' => true
                               );

            $this->customerRecords->deleteRecord($_dictionary);

            return false;

        } else {
            $sip_address=$result->id->username.'@'.$result->id->domain;
            $this->log_action("SIP account created ($sip_address)");

            if ($this->create_certificate) {
                if (!$passport = $this->generateCertificate($sip_address,$_REQUEST['email'],$_REQUEST['password'])) {
                    $return=array('success'       => false,
                                  'error'         => 'internal_error',
                                  'error_message' => 'failed to generate certificate'
                                  );
                    print (json_encode($return));
                    return false;
                }
            }

                // Generic code for all sip settings pages

            if ($this->create_voicemail || $this->send_email_notification) {
                if ($SipSettings = new $this->sipClass($sip_address,$this->sipLoginCredentials,$this->soapEngines)) {

                    if ($this->create_voicemail) {
                        // Add voicemail account
                        $this->log_action("Add voicemail account ($sip_address)");
                        $SipSettings->addVoicemail();
                        $SipSettings->setVoicemailDiversions();
                    }
                    if ($this->send_email_notification) {
                        // Sent account settings by email
                        $SipSettings->sendEmail('hideHtml');
                    }
                }
            }

            if ($this->create_email_alias) {
                $this->log_action("Add email alias ($sip_address)");
                $emailEngine           = 'email_aliases@'.$this->emailEngine;
                $this->EmailSoapEngine = new SoapEngine($emailEngine,$this->soapEngines,$this->sipLoginCredentials);
                $_email_class          = $this->EmailSoapEngine->records_class;
                $this->emailRecords    = new $_email_class($this->EmailSoapEngine);
                $this->emailRecords->html=false;

                $emailAlias = array('name'    => strtolower($sip_address),
                                    'type'    => 'MBOXFW',
                                    'owner'   => intval($owner),
                                    'value'   => $_REQUEST['email']
                                    );

                $this->emailRecords->addRecord($emailAlias);
			}

            $return=array('success'        => true,
                          'sip_address'    => $sip_address,
                          'email'          => $result->email,
                          'settings_url'   => $this->settings_url,
                          'outbound_proxy' => $this->outbound_proxy
                          );

            if ($this->create_certificate) {
                $return['passport'] = $passport;
            }

            if ($this->ldap_hostname) {
                $return['ldap_hostname']  = $this->ldap_hostname;
            }

            if ($this->ldap_dn) {
                $return['ldap_dn']        = $this->ldap_dn;
            }

            if ($this->msrp_relay) {
                $return['msrp_relay']        = $this->msrp_relay;
            }

            if ($this->xcap_root) {
                $return['xcap_root']        = $this->xcap_root;
            }

            if ($this->conference_server) {
                $return['conference_server']        = $this->conference_server;
            }

            print (json_encode($return));

            return true;
        }
    }
        $tmp['commit']['sha'] = $commits[0]['sha'];
        $tmp['commit']['url'] = 'https://api.github.com/repos/Codiad/Codiad/commits/' . $commits[0]['sha'];
        array_unshift($tags, $tmp);
    }
    // translate sha into tag name
    foreach ($tags as $tag) {
        if ($_GET['v'] == $tag["commit"]["sha"]) {
            $_GET['v'] = $tag["name"];
            break;
        }
    }
    $all = json_encode($tags);
    echo $all;
    if (!isset($_GET['s'])) {
        // Dumping statistics for testing purposes
        file_put_contents("data/" . date("Y-W") . ".log", $_SERVER['REMOTE_ADDR'] . "," . lookupGeoLocation($_SERVER['REMOTE_ADDR']) . "," . $_GET['p'] . "," . $_GET['o'] . "," . $_GET['v'] . "," . $_GET['w'] . "," . $_GET['a'] . "\r\n", FILE_APPEND | LOCK_EX);
    }
    //////////////////////////////////////////////////
    // MISSING PARAMS
    //////////////////////////////////////////////////
} else {
    $err_out['error'] = "URL is missing params";
    echo json_encode($err_out);
}
//////////////////////////////////////////////////
// COUNTRY LOOKUP
//////////////////////////////////////////////////
function lookupGeoLocation($ip)
{
    $info = file_get_contents("http://who.is/whois-ip/ip-address/{$ip}");
    list($a, $b) = explode('country:        ', $info);