/**
  * Encode JSON data and write to the file.
  *
  * @param array $data
  * @param string $filePath
  * @throws \InvalidArgumentException
  */
 public function encodeFile($data, $filePath)
 {
     $file = new File($filePath);
     $dataEncoder = new JsonDataEncoder();
     $fileData = $dataEncoder->encodeData($data);
     $file->setContent($fileData);
 }
 /**
  * Test encodeData function properly encodes data
  * for the JSON format.
  */
 public function testEncodeData()
 {
     $data = self::$dataFileFixturesHelper->loadDecodedData();
     $expectedResult = self::$dataFileFixturesHelper->loadEncodedData();
     $actualResult = $this->jsonDataEncoder->encodeData($data);
     $this->assertEquals($expectedResult, $actualResult);
 }
Ejemplo n.º 3
0
            <tr>
                <td class="info" colspan="4"><?php 
            echo $tentry->getAttachmentsLinks();
            ?>
</td>
            </tr> <?php 
        }
        if ($urls) {
            ?>
                <script type="text/javascript">
                    $('#thread-id-<?php 
            echo $entry['id'];
            ?>
')
                        .data('urls', <?php 
            echo JsonDataEncoder::encode($urls);
            ?>
)
                        .data('id', <?php 
            echo $entry['id'];
            ?>
);
                </script>
<?php 
        }
        ?>
        </table>
        <?php 
        if ($entry['thread_type'] == 'M') {
            $msgId = $entry['id'];
        }
Ejemplo n.º 4
0
    function render($mode = false)
    {
        $config = $this->field->getConfiguration();
        $value = false;
        if ($this->value instanceof DynamicListItem) {
            // Loaded from database
            $value = $this->value->get('id');
            $name = $this->value->get('value');
        } elseif ($this->value) {
            // Loaded from POST
            $value = $this->value;
            $name = $this->getEnteredValue();
        }
        if (!$config['typeahead'] || $mode == 'search') {
            $this->value = $value;
            return parent::render($mode);
        }
        $source = array();
        foreach ($this->field->getList()->getItems() as $i) {
            $source[] = array('value' => $i->get('value'), 'id' => $i->get('id'), 'info' => $i->get('value') . " -- " . $i->get('extra'));
        }
        ?>
        <span style="display:inline-block">
        <input type="text" size="30" name="<?php 
        echo $this->name;
        ?>
"
            id="<?php 
        echo $this->name;
        ?>
" value="<?php 
        echo $name;
        ?>
"
            placeholder="<?php 
        echo $config['prompt'];
        ?>
" autocomplete="off" />
        <input type="hidden" name="<?php 
        echo $this->name;
        ?>
_id" id="<?php 
        echo $this->name;
        ?>
_id" value="<?php 
        echo $value;
        ?>
"/>
        <script type="text/javascript">
        $(function() {
            $('input#<?php 
        echo $this->name;
        ?>
').typeahead({
                source: <?php 
        echo JsonDataEncoder::encode($source);
        ?>
,
                property: 'info',
                onselect: function(item) {
                    $('input#<?php 
        echo $this->name;
        ?>
').val(item['value'])
                    $('input#<?php 
        echo $this->name;
        ?>
_id').val(item['id'])
                }
            });
        });
        </script>
        </span>
        <?php 
    }
Ejemplo n.º 5
0
 static function __create($ht, &$error = false)
 {
     global $ost;
     $ht['properties'] = JsonDataEncoder::encode($ht['properties']);
     if ($status = TicketStatus::create($ht)) {
         $status->save(true);
     }
     return $status;
 }
Ejemplo n.º 6
0
    function _build($lang) {
        list($code, $zip) = $this->_request("download/$lang.zip");

        if ($code !== 200)
            $this->fail('Language is not available'."\n");

        $temp = tempnam('/tmp', 'osticket-cli');
        $f = fopen($temp, 'w');
        fwrite($f, $zip);
        fclose($f);
        $zip = new ZipArchive();
        $zip->open($temp);
        unlink($temp);

        $lang = str_replace('-','_',$lang);
        @unlink(I18N_DIR."$lang.phar");
        $phar = new Phar(I18N_DIR."$lang.phar");
        $phar->startBuffering();

        $po_file = false;

        for ($i=0; $i<$zip->numFiles; $i++) {
            $info = $zip->statIndex($i);
            $contents = $zip->getFromIndex($i);
            if (!$contents)
                continue;
            if (strpos($info['name'], '/messages.po') !== false) {
                $po_file = $contents;
                // Don't add the PO file as-is to the PHAR file
                continue;
            }
            $phar->addFromString($info['name'], $contents);
        }

        // TODO: Add i18n extras (like fonts)
        // Redactor language pack
        //
        $langs = array($lang);
        if (strpos($lang, '_') !== false) {
            @list($short) = explode('_', $lang);
            $langs[] = $short;
        }
        foreach ($langs as $l) {
            list($code, $js) = $this->_http_get(
                'http://imperavi.com/webdownload/redactor/lang/?lang='
                .strtolower($l));
            if ($code == 200 && ($js != 'File not found')) {
                $phar->addFromString('js/redactor.js', $js);
                break;
            }
        }
        if ($code != 200)
            $this->stderr->write($lang . ": Unable to fetch Redactor language file\n");

        // JQuery UI Datepicker
        // http://jquery-ui.googlecode.com/svn/tags/latest/ui/i18n/jquery.ui.datepicker-de.js
        foreach ($langs as $l) {
            list($code, $js) = $this->_http_get(
                'http://jquery-ui.googlecode.com/svn/tags/latest/ui/i18n/jquery.ui.datepicker-'
                    .str_replace('_','-',$l).'.js');
            // If locale-specific version is not available, use the base
            // language version (de if de_CH is not available)
            if ($code == 200)
                break;
        }
        if ($code == 200)
            $phar->addFromString('js/jquery.ui.datepicker.js', $js);
        else
            $this->stderr->write(str_replace('_','-',$lang)
                .": Unable to fetch jQuery UI Datepicker locale file\n");

        // True type fonts for PDF printing
        $langs = (include I18N_DIR . 'langs.php');
        $info = $langs[$lang];
        if (isset($info['fonts'])) {
            foreach ($info['fonts'] as $name => $types) {
                foreach ($types as $code => $fullname) {
                    list($ttf, $url) = $fullname;
                    if (!$url)
                        continue;
                    list($code, $file) = $this->_http_get($url);
                    if ($code == 200)
                        $phar->addFromString('fonts/'.$ttf, $file);
                    else
                        $this->stderr->write(
                            "*** Unable to fetch $url\n");
                }
            }
        }

        // Add in the messages.mo.php file
        if ($po_file) {
            $pipes = array();
            $msgfmt = proc_open('msgfmt -o- -',
                array(0=>array('pipe','r'), 1=>array('pipe','w')),
                $pipes);
            if (is_resource($msgfmt)) {
                fwrite($pipes[0], $po_file);
                fclose($pipes[0]);
                $mo_input = fopen('php://temp', 'r+b');
                fwrite($mo_input, stream_get_contents($pipes[1]));
                rewind($mo_input);
                require_once INCLUDE_DIR . 'class.translation.php';
                $mo = Translation::buildHashFile($mo_input, false, true);
                $phar->addFromString('LC_MESSAGES/messages.mo.php', $mo);
                fclose($mo_input);
            }
        }

        // Add in translation of javascript strings
        $phrases = array();
        if ($mo && ($js = $this->__getAllJsPhrases())) {
            $mo = (eval (substr($mo, 5))); # Chop off <?php
            foreach ($js as $c) {
                foreach ($c['forms'] as $f) {
                    $phrases[$f] = @$mo[$f] ?: $f;
                }
            }
            $phar->addFromString(
                'js/osticket-strings.js',
                sprintf('(function($){$.oststrings=%s;})(jQuery);',
                    JsonDataEncoder::encode($phrases))
            );
        }

        // Include a manifest
        include_once INCLUDE_DIR . 'class.mailfetch.php';

        $po_header = Mail_Parse::splitHeaders($mo['']);
        $info = array(
            'Build-Date' => date(DATE_RFC822),
            'Build-Version' => trim(`git describe`),
            'Language' => $po_header['Language'],
            #'Phrases' =>
            #'Translated' =>
            #'Approved' =>
            'Id' => 'lang:' . $lang,
            'Last-Revision' => $po_header['PO-Revision-Date'],
            'Version' => (int)(strtotime($po_header['PO-Revision-Date']) / 10000),
        );
        $phar->addFromString(
            'MANIFEST.php',
            sprintf('<?php return %s;', var_export($info, true)));

        // TODO: Sign files

        // Use a very small stub
        $phar->setStub('<?php __HALT_COMPILER();');
        $phar->setSignatureAlgorithm(Phar::SHA1);
        $phar->stopBuffering();
    }
Ejemplo n.º 7
0
    function render($how)
    {
        if ($how == 'search') {
            return parent::render($how);
        }
        $name = $this->getEnteredValue();
        $config = $this->field->getConfiguration();
        if (is_array($this->value)) {
            $name = $name ?: current($this->value);
            $value = key($this->value);
        } else {
            // Pull configured default (if configured)
            $def_key = $this->field->get('default');
            if (!$def_key && $config['default']) {
                $def_key = $config['default'];
            }
            if (is_array($def_key)) {
                $name = current($def_key);
            }
        }
        $source = array();
        foreach ($this->field->getList()->getItems() as $i) {
            $source[] = array('value' => $i->getValue(), 'id' => $i->getId(), 'info' => sprintf('%s %s', $i->getValue(), ($extra = $i->getAbbrev()) ? "-- {$extra}" : ''));
        }
        ?>
        <span style="display:inline-block">
        <input type="text" size="30" name="<?php 
        echo $this->name;
        ?>
_name"
            id="<?php 
        echo $this->name;
        ?>
" value="<?php 
        echo Format::htmlchars($name);
        ?>
"
            placeholder="<?php 
        echo $config['prompt'];
        ?>
" autocomplete="off" />
        <input type="hidden" name="<?php 
        echo $this->name;
        ?>
[<?php 
        echo $value;
        ?>
]" id="<?php 
        echo $this->name;
        ?>
_id" value="<?php 
        echo Format::htmlchars($name);
        ?>
"/>
        <script type="text/javascript">
        $(function() {
            $('input#<?php 
        echo $this->name;
        ?>
').typeahead({
                source: <?php 
        echo JsonDataEncoder::encode($source);
        ?>
,
                property: 'info',
                onselect: function(item) {
                    $('input#<?php 
        echo $this->name;
        ?>
_name').val(item['value'])
                    $('input#<?php 
        echo $this->name;
        ?>
_id')
                      .attr('name', '<?php 
        echo $this->name;
        ?>
[' + item['id'] + ']')
                      .val(item['value']);
                }
            });
        });
        </script>
        </span>
        <?php 
    }
Ejemplo n.º 8
0
 /**
  * Json Encoder
  *
  */
 function json_encode($what)
 {
     require_once INCLUDE_DIR . 'class.json.php';
     return JsonDataEncoder::encode($what);
 }
Ejemplo n.º 9
0
 function compileQ($Q, $form)
 {
     $expr = array();
     foreach ($Q->constraints as $c => $value) {
         if ($value instanceof Q) {
             $expr[] = $this->compileQ($value, $form);
         } else {
             list($f, $op) = explode('__', $c, 2);
             $widget = $form->getField($f)->getWidget();
             $id = $widget->id;
             switch ($op) {
                 case 'eq':
                 case null:
                     $expr[] = sprintf('(%s.is(":visible") && %s)', $id, sprintf('%s == %s', sprintf($widget->getJsValueGetter(), $id), JsonDataEncoder::encode($value)));
             }
         }
     }
     $glue = $Q->isOred() ? ' || ' : ' && ';
     $expression = implode($glue, $expr);
     if (count($expr) > 1) {
         $expression = '(' . $expression . ')';
     }
     if ($Q->isNegated) {
         $expression = '!' . $expression;
     }
     return $expression;
 }
Ejemplo n.º 10
0
 function attach()
 {
     $field = new FileUploadField();
     return JsonDataEncoder::encode(array('id' => $field->ajaxUpload(true)));
 }
Ejemplo n.º 11
0
 function write_block($what)
 {
     fwrite($this->stream, JsonDataEncoder::encode($what));
     fwrite($this->stream, "\n");
 }
Ejemplo n.º 12
0
 function to_json()
 {
     $info = array('id' => $this->getId(), 'name' => (string) $this->getName());
     return JsonDataEncoder::encode($info);
 }
Ejemplo n.º 13
0
 function dump()
 {
     require_once INCLUDE_DIR . 'class.json.php';
     $exp = new JsonDataEncoder();
     $rows = array();
     while ($row = $this->nextArray()) {
         $rows[] = $row;
     }
     echo $exp->encode($rows);
 }
Ejemplo n.º 14
0
 function getFileList()
 {
     global $thisstaff;
     if (!$thisstaff) {
         Http::response(403, "Login required for file queries");
     }
     $sql = 'SELECT distinct f.id, COALESCE(a.type, f.ft) FROM ' . FILE_TABLE . ' f LEFT JOIN ' . ATTACHMENT_TABLE . ' a ON (a.file_id = f.id)
         WHERE (a.`type` IN (\'C\', \'F\', \'T\', \'P\') OR f.ft = \'L\')
             AND f.`type` LIKE \'image/%\'';
     if (!($res = db_query($sql))) {
         Http::response(500, 'Unable to lookup files');
     }
     $files = array();
     $folders = array('C' => __('Canned Responses'), 'F' => __('FAQ Articles'), 'T' => __('Email Templates'), 'L' => __('Logos'), 'P' => __('Pages'));
     while (list($id, $type) = db_fetch_row($res)) {
         $f = AttachmentFile::lookup($id);
         $url = $f->getDownloadUrl();
         $files[] = array('thumb' => $url . '&s=128', 'image' => $url, 'title' => $f->getName(), 'folder' => $folders[$type]);
     }
     echo JsonDataEncoder::encode($files);
 }
Ejemplo n.º 15
0
 function to_json()
 {
     $info = array('id' => $this->getId(), 'name' => Format::htmlchars($this->getName()), 'email' => (string) $this->getEmail(), 'phone' => (string) $this->getPhoneNumber());
     return JsonDataEncoder::encode($info);
 }
Ejemplo n.º 16
0
 /**
  * Convert a PHP array into a JSON-encoded string
  */
 function json_encode($what)
 {
     require_once INCLUDE_DIR . 'class.json.php';
     $encoder = new JsonDataEncoder();
     return $encoder->encode($what);
 }