Пример #1
0
 public function Test_for_getTimestamp()
 {
     $this->assertEqual(Ak::getTimestamp(), Ak::time());
     $this->assertEqual('17:52:03', Ak::getDate(Ak::getTimestamp('17:52:03'), 'H:i:s'));
     $this->assertEqual(date('Y-m-d') . ' 17:52:03', Ak::getDate(Ak::getTimestamp('17:52:03')));
     $this->assertEqual('2005-12-25 00:00:00', Ak::getDate(Ak::getTimestamp('2005-12-25')));
     $this->assertEqual('1592-10-09 00:00:00', Ak::getDate(Ak::getTimestamp('1592-10-09')));
     $this->assertEqual('2192-10-09 00:00:00', Ak::getDate(Ak::getTimestamp('2192-10-09')));
     $this->assertEqual('2192-10-09 01:02:03', Ak::getDate(Ak::getTimestamp('2192-10-9 01:02:03')));
 }
Пример #2
0
 public function test_signed_up()
 {
     $Expected = $this->new_mail();
     $Expected->setTo($this->recipient);
     $Expected->setSubject("[Signed up] Welcome {$this->recipient}");
     $Expected->setBody("Hello there,\n\nMr. {$this->recipient}");
     $Expected->setFrom("*****@*****.**");
     $Expected->setDate(Ak::getTimestamp("2004-12-12"));
     $TestMailer = new TestMailer();
     $this->assertTrue($Created = $TestMailer->create('signed_up', $this->recipient));
     $this->assertEqual($Expected->getEncoded(), $Created->getEncoded());
     $this->assertTrue($TestMailer->deliver('signed_up', $this->recipient));
     $this->assertTrue(!empty($TestMailer->deliveries[0]));
     $this->assertEqual($Expected->getEncoded(), $TestMailer->deliveries[0]);
 }
Пример #3
0
 function authenticateWithToken($token)
 {
     $options = User::_decodeToken($token);
     if (!empty($options) && !empty($options['hash']) && !empty($options['id'])) {
         $User = new User();
         $User = $User->find($options['id']);
         if (!empty($options['expires']) && $options['expires'] < Ak::getTimestamp()) {
             return false;
         }
         if ($options['hash'] == $User->_getTokenHash($options)) {
             $User->updateAttribute('last_login_at', Ak::getDate());
             return $User;
         }
     }
     return false;
 }
Пример #4
0
 public function authenticateWithToken($token, $update_last_login = true)
 {
     $options = User::decodeToken($token);
     if (!empty($options) && !empty($options['hash']) && !empty($options['id'])) {
         $User = new User();
         $User = $User->find($options['id']);
         if (!empty($options['expires']) && $options['expires'] < Ak::getTimestamp()) {
             return false;
         }
         if ($options['hash'] == $User->getTokenHash($options)) {
             if ($update_last_login) {
                 $User->updateAttribute('last_login_at', Ak::getDate());
             }
             $this->setCurrentUser($User);
             return $User;
         }
     }
     return false;
 }
Пример #5
0
 /**
  * Returns a one time use token for accesing an account.
  * 
  * This might be used for retrieving lost passwords.
  * 
  * Tokens can be validated using the Sentinel::isValidLoginTokenForUser method
  */
 function getToken($options = array())
 {
     $default_options = array('id' => (int) $this->get('id'), 'single_use' => !empty($options['single_use']));
     $options = array_merge($default_options, $options);
     $options['expires'] = empty($options['expires']) ? 0 : Ak::getTimestamp() + (empty($options['expires']) ? '0' : ($options['expires'] == true ? 86400 : $options['expires']));
     $options['single_use'] = $options['single_use'] ? 1 : 0;
     $options['hash'] = $this->_getTokenHash($options);
     return $this->_encodeToken($options);
 }
Пример #6
0
 /**
  * Converts an ISO date to current locale format
  *
  */
 function locale_date($iso_date)
 {
     $timestamp = Ak::getTimestamp($iso_date);
     $format = Ak::locale('date_format');
     return Ak::getDate($timestamp, $format);
 }
Пример #7
0
    public function test_with_relations_json()
    {
        $person = $this->Person->create(array('first_name' => 'Hansi', 'last_name' => 'Müller', 'email' => '*****@*****.**'));
        $person_created_at = gmdate('Y-m-d\\TH:i:s\\Z', Ak::getTimestamp($person->created_at));
        $person->account->create(array('username' => 'hansi', 'password' => 'wilma'));
        $account_created_at = gmdate('Y-m-d\\TH:i:s\\Z', Ak::getTimestamp($person->account->created_at));
        $expected = <<<EOX
{"id":{$person->id},"first_name":"Hansi","last_name":"M\\u00fcller","email":"*****@*****.**","created_at":"{$person_created_at}","account":{"id":{$person->account->id},"person_id":{$person->id},"username":"******","password":"******","is_enabled":0,"credit_limit":null,"firm_id":null,"reset_key":null,"created_at":"{$account_created_at}"}}
EOX;
        $json = $person->toJson(array('include' => 'account'));
        $this->_compareJson($expected, $json);
        $person_reloaded = $person->fromJson($json);
        $this->assertEqual($person->first_name, $person_reloaded->first_name);
        $this->assertEqual($person->last_name, $person_reloaded->last_name);
        $this->assertEqual($person->account->id, $person_reloaded->account->id);
    }
Пример #8
0
 public function test_should_convert_between_timestamp_and_date()
 {
     $iso_date = '2007-10-15 16:30:00';
     $this->assertEqual(Ak::getDate(Ak::getTimestamp($iso_date)), $iso_date);
     $this->assertEqual(Ak::getDate(Ak::getTimestamp('2007-10-15 16:30')), $iso_date);
 }
Пример #9
0
    function castAttributeForDatabase($column_name, $value, $add_quotes = true)
    {
        $result = '';
        switch ($this->getColumnType($column_name)) {
            case 'datetime':
                if(!empty($value)){
                    $date_time = $this->_db->quote_datetime(Ak::getTimestamp($value));
                    $result = $add_quotes ? $date_time : trim($date_time ,"'");
                }else{
                    $result = 'null';
                }
                break;

            case 'date':
                if(!empty($value)){
                    $date = $this->_db->quote_date(Ak::getTimestamp($value));
                    $result = $add_quotes ? $date : trim($date, "'");
                }else{
                    $result = 'null';
                }
                break;

            case 'boolean':
                $result = is_null($value) ? 'null' : (!empty($value) ? "'1'" : "'0'");
                break;

            case 'binary':
                if($this->_getDatabaseType() == 'postgre'){
                    $result =  is_null($value) ? 'null::bytea ' : " '".$this->_db->escape_blob($value)."'::bytea ";
                }else{
                    $result = is_null($value) ? 'null' : ($add_quotes ? $this->_db->quote_string($value) : $value);
                }
                break;

            case 'decimal':
                if(is_null($value)){
                    $result = 'null';
                }else{
                    if($scale = $this->getColumnScale($column_name)){
                        $value = number_format($value, $scale, '.', '');
                    }
                    $result = $add_quotes ? $this->_db->quote_string($value) : $value;
                }
                break;

            case 'serial':
            case 'integer':
                $result = (is_null($value) || $value==='') ? 'null' : (integer)$value;
                break;

            case 'float':
                $result = (empty($value) && $value !== 0) ? 'null' : (is_numeric($value) ? $value : $this->_db->quote_string($value));
                $result = !empty($this->_columns[$column_name]['notNull']) && $result == 'null' && $this->_getDatabaseType() == 'sqlite' ? '0' : $result;
                break;

            default:
                if($this->_shouldSerializeColumn($column_name)){
                    $value = serialize($value);
                }
                $result = is_null($value) ? 'null' : ($add_quotes ? $this->_db->quote_string($value) : $value);
                break;
        }

        //  !! nullable vs. not nullable !!
        return empty($this->_columns[$column_name]['notNull']) ? ($result === '' ? "''" : $result) : ($result === 'null' ? '' : $result);
    }
Пример #10
0
 function _getLogFormatedAsString($type, $error_number, $error_message, $filename, $line_number, $serialized = false)
 {
     $message = Ak::getTimestamp() . "\t[{$error_number}]\t{$error_message}";
     $params = array_merge($this->_log_params, $this->extended_details ? array('file' => $filename, 'line_number' => $line_number, 'remote_address' => $_SERVER['REMOTE_ADDR'], 'browser' => $_SERVER['HTTP_USER_AGENT']) : array());
     if ($serialized) {
         $message .= count($params) ? "\t" . serialize($params) : '';
     } else {
         $details = '';
         foreach ($params as $k => $v) {
             $details .= "\n\t\t- " . AkInflector::humanize($k) . ": {$v}";
         }
         $message .= empty($details) ? "\n" : "\n\t" . 'PARAMS{' . $details . "\t\n}\n";
     }
     return $message;
 }
Пример #11
0
 function _saveLogInDatabase($type, $error_mode, $error_message)
 {
     $db =& Ak::db();
     $message = $this->_getLogFormatedAsRawText($type, $error_mode, $error_message);
     $sql = 'INSERT INTO log (user_id, type, message, severity, location, hostname, created) ' . " VALUES (0, " . $db->quote_string($type) . ", " . $db->quote_string($message) . ', ' . ($this->mode & AK_MODE_DIE ? 100 : 0) . ', ' . $db->quote_string(AK_CURRENT_URL) . ', ' . $db->quote_string($_SERVER['REMOTE_ADDR']) . ', ' . $db->quote_string(Ak::getTimestamp()) . ');';
     if ($db->execute($sql) === false) {
         die($this->internalError($this->t('Error inserting: ') . $db->ErrorMsg(), __FILE__, __LINE__));
     }
 }
Пример #12
0
 /**
  * Compute and return the current time, in the time zone represented by
  * AkTimeZone.
  */
 function now()
 {
     return $this->adjust(isset($this->_timestamp) ? $this->_timestamp : Ak::getTimestamp());
 }
Пример #13
0
 function _getHeaders($to = null)
 {
     return array('From' => trim($this->email_account->sender_name . ' <' . $this->email_account->reply_to . '>'), 'Return-path' => trim($this->email_account->sender_name . ' <' . $this->email_account->reply_to . '>'), 'Subject' => $this->subject, 'To' => $to, 'Message-Id' => '<' . $this->id . '.' . Ak::uuid() . substr('*****@*****.**', strpos('*****@*****.**', '@')) . '>', 'Date' => strftime("%a, %d %b %Y %H:%M:%S %z", Ak::getTimestamp()));
 }
Пример #14
0
 /**
  * Generate a xml representation of the model record.
  *
  * Example result:
  *
  * <?xml version="1.0" encoding="UTF-8"?>
  * <person>
  *    <id>1</id>
  *    <first-name>Hansi</first-name>
  *    <last-name>Müller</last-name>
  *    <email>hans@mueller.com</email>
  *    <created-at type="datetime">2008-01-01 13:01:23</created-at>
  * </person>
  *
  * parameters:
  *
  * @param array $options
  *
  *              option parameters:
  *             array(
  *              'collection' => array($Person1,$Person), // array of ActiveRecords
  *              'include' => array('association1','association2'), // include the associations when exporting
  *              'exclude' => array('id','name'), // exclude the attribtues
  *              'only' => array('email','last_name') // only export these attributes
  *              )
  * @return string in Xml Format
  */
 public function toXml($options = array())
 {
     $options['padding'] = empty($options['padding']) ? 0 : $options['padding'] + 1;
     $current_padding = str_repeat(' ', $options['padding']);
     if (is_array($options) && isset($options[0]) && $options[0] instanceof ArrayAccess) {
         $options = array('collection' => $options);
     }
     if (isset($options['collection']) && (is_array($options['collection']) || $options['collection'] instanceof ArrayAccess) && $options['collection'][0]->_modelName == $this->_Model->getModelName()) {
         $root = AkInflector::underscore(AkInflector::pluralize($this->_Model->getModelName()));
         $root = $this->_convertColumnToXmlElement($root);
         $xml = '';
         if (!(isset($options['skip_instruct']) && $options['skip_instruct'] == true)) {
             $xml .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
         }
         $xml .= $current_padding . '<' . $root . " type=\"array\">\n";
         $collection = $options['collection'];
         unset($options['collection']);
         $options['skip_instruct'] = true;
         $options['padding']++;
         foreach ($collection as $element) {
             $xml .= $element->toXml($options);
         }
         $xml .= $current_padding . '</' . $root . ">\n";
         return $xml;
     }
     /**
      * see if we need to include associations
      */
     $associatedIds = array();
     if (isset($options['include']) && !empty($options['include'])) {
         $options['include'] = is_array($options['include']) ? $options['include'] : preg_split('/,\\s*/', $options['include']);
         foreach ($this->_Model->getAssociations() as $key => $obj) {
             if (in_array($key, $options['include'])) {
                 if ($obj->getType() != 'hasAndBelongsToMany') {
                     $associatedIds[$obj->getAssociationIds() . '_id'] = array('name' => $key, 'type' => $obj->getType());
                 } else {
                     $associatedIds[$key] = array('name' => $key, 'type' => $obj->getType());
                 }
             }
         }
     }
     if (isset($options['only'])) {
         $options['only'] = is_array($options['only']) ? $options['only'] : preg_split('/,\\s*/', $options['only']);
     }
     if (isset($options['except'])) {
         $options['except'] = is_array($options['except']) ? $options['except'] : preg_split('/,\\s*/', $options['except']);
     }
     $xml = '';
     if (!(isset($options['skip_instruct']) && $options['skip_instruct'] == true)) {
         $xml .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
     }
     $root = $this->_convertColumnToXmlElement(AkInflector::underscore($this->_Model->getModelName()));
     $xml .= $current_padding . '<' . $root . ">\n";
     foreach ($this->_Model->getColumns() as $key => $def) {
         if (isset($options['except']) && in_array($key, $options['except'])) {
             continue;
         } else {
             if (isset($options['only']) && !in_array($key, $options['only'])) {
                 continue;
             } else {
                 $columnType = $def['type'];
                 $elementName = $this->_convertColumnToXmlElement($key);
                 $xml .= $current_padding . '  <' . $elementName;
                 $val = $this->_Model->{$key};
                 if (!in_array($columnType, array('string', 'text', 'serial'))) {
                     $xml .= ' type="' . $columnType . '"';
                     if ($columnType == 'boolean') {
                         $val = $val ? 1 : 0;
                     }
                 } elseif ($columnType == 'serial' && is_numeric($val)) {
                     $xml .= ' type="integer"';
                 }
                 if (!empty($val) && in_array($columnType, array('datetime'))) {
                     // UTC (Coordinated Universal Time) http://www.w3.org/TR/NOTE-datetime
                     $val = gmdate('Y-m-d\\TH:i:s\\Z', Ak::getTimestamp($val));
                 }
                 if (is_null($val)) {
                     $xml .= ' nil="true"';
                 }
                 $xml .= '>' . Ak::utf8($val) . '</' . $elementName . ">\n";
             }
         }
     }
     if (isset($options['include'])) {
         foreach ($this->_Model->getAssociations() as $key => $val) {
             if (in_array($key, $options['include']) || in_array($val, $options['include'])) {
                 if (is_array($this->_Model->{$key})) {
                     $associationElement = $key;
                     $associationElement = AkInflector::underscore(AkInflector::pluralize($associationElement));
                     $associationElement = $this->_convertColumnToXmlElement($associationElement);
                     $xml .= $current_padding . '<' . $associationElement . " type=\"array\">\n";
                     $options['padding']++;
                     foreach ($this->_Model->{$key} as $el) {
                         if ($el instanceof AkBaseModel) {
                             $xml .= $el->toXml(array('skip_instruct' => true));
                         }
                     }
                     $xml .= $current_padding . '</' . $associationElement . ">\n";
                 } else {
                     $el = $this->_Model->{$key}->load();
                     if ($el instanceof AkBaseModel) {
                         $xml .= $el->toXml(array('skip_instruct' => true));
                     }
                 }
             }
         }
     }
     $xml .= $current_padding . '</' . $root . ">\n";
     return $xml;
 }
Пример #15
0
 function getValueForDateColumn($column_name, $value)
 {
     if (!$this->isNewRecord()) {
         if (($column_name == 'updated_on' || $column_name == 'updated_at') && empty($value)) {
             return null;
         }
     } elseif (($column_name == 'created_on' || $column_name == 'created_at') && empty($value)) {
         return Ak::time();
     } elseif ($column_name == 'expires_on' || $column_name == 'expires_at') {
         return empty($value) ? Ak::getTimestamp('9999-12-31 23:59:59') : Ak::getTimestamp($value);
     }
     return Ak::getTimestamp($value);
 }