On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Parsing arrays (or other complex variables)

    maybe I’m heading for a very hard way to solve a simple issue - please help me.

    I’m using phx through Chunkie class in a snippet (not installing phx as a plugin).
    Everithing seems ok. Very useful the new template-file feature!

    My snippet needs to output simple or complex variables: strings or arrays (of strings and of arrays of...). I’ve got to use a template regardless type of variables (type and composition can’t be foreseen).
    I modified method CreateVars of Chunkie class, because it seems not possible to pass an array to a modifier:
    function CreateVars($value = '', $key = '', $path = '') {
    $keypath = !empty($path) ? $path . "." . $key : $key;
    	if (is_array($value)) {
    	     // added
    	     //		type		name		fields
    	     $info_string =	'array'.',' .	$keypath.',' .	implode(',' , array_keys($value));
                 foreach ($value as $subkey => $subval) {
    	     $this->CreateVars($subval, $subkey, $keypath);
    	// added
    	$this->CreateVars($info_string, '__info', $keypath);			
    	else {
    		$this->phx->setPHxVariable($keypath, $value);

    By this, if I pass an array:
    $aaaa = array( "field1" => 5, "field2" => array( "field21" => 2));

    as phx variable ’aaaa’ I get the placeholders:
    [+aaaa.__info+] = "array,aaaa,field1,field2" (added placeholder containing type, name, fields list)
    [+aaaa.field1+] = "5" (no news)
    [+aaaa.field2.__info+] = "array,aaaa.field2,field21" (added placeholder containing type, name, fields list)
    [+aaaa.field2.field21+] = "2" (no news)

    while I can’t get placeholders for aaaa and aaaa.field2 because they’re not simple variables.

    In this way I can create a template like

    which calls method elements:
    if ($options == '') return $output;		// $output is a simple variable!!
    $var_info = explode(',',$options);
    $var_type = $var_info[0];
    switch ($var_type) { // will add object type?
    	case 'array':
    		$var_name = $var_info[1];
    		$var_fields_array = array_slice($var_info, 2);
    $string .= '<h5>' . $var_name . ' = ' . $var_type . '</h5><br/>
    $string .= '<ul>
    foreach ($var_fields_array as $fieldname) {
    	$field_placeholder = '[+' . $var_name . '.' . $fieldname . '+]';
    	$options_placeholder = '[+' . $var_name . '.' . $fieldname . '.__info' . '+]';
    	$string .= '<li>' . $fieldname . ' = ' . '[+phx:input=`' . $field_placeholder . '`:elements=`' . $options_placeholder . '`+]' . '</li>
    $string .= '</ul>
    return $string;

    which calls itself each time an element is an array.

    the output is
    aaaa = array
    - field1 = 5
    - field2 = aaaa.field2 = array
    - field21 = 2

    This is the only way I found to explore complex variables: I only have to know the variable name - no need to know if it is simple, an array and which fieldnames.
    Of course this is just a test.

    Two questions:
    1 - Is there a different simple way to pass arrays as variables and use them with phx placeholders when the template doesn’t know field names?
    2 - Is there a way to use templates within a modifier code (avoiding html tags into the modifier).
    (3 - any other suggestion is welcome..)

    thanks in advance
    • I made a folder in /assets/plugins called "phx" and dumped all the contents of the latest version in there... so there is a /assets/plugins/phx/phx.parser.class.inc.php and etc....
      I copied all of the contents of phx.plugin.txt in my new plugin called "PHx".. then i set the system event to "OnParseDocument"

      (i am doing everything right)

      This results in the modx error of..
      « MODx Parse Error »
      MODx encountered the following error while attempting to parse the requested resource:
      « PHP Parse Error »
      PHP error debug
        Error: 	include_once(/assets/plugins/phx/phx.parser.class.inc.php) [function.include-once]: failed to open stream: No such file or directory	 
        Error type/ Nr.: 	Warning - 2	 
        File: 	/mnt/target03/358650/www.arezombiesattacking.com/web/content/manager/includes/document.parser.class.inc.php(745) : eval()'d code	 
        Line: 	10	 
      Parser timing
        MySQL: 	0.0121 s	(3 Requests)
        PHP: 	0.1680 s	 
        Total: 	0.1801 s	 

      The file IS there... i am not calling phx even yet.

      What should i do?
        Minneapolis Web Design <-- where i work
        MODx Templates <-- new project im working on
      • Set correct path in system settings tab - tools - configuration - Interface & Features - Resource path. Seems it must be

          DirectResize 0.9 beta. PHPThumb, sets of configurations, configuration, binded to specific path. No backward compatibility. | DirectResize 0.9. PHPThumb, наборы параметров, параметры, привязанные к определенным путям. Без обратной совместимости.

          Unfortunately, DirectResize project is closed. If you want to continue development, PM me for access to project page on Google Code. К сожалению, проект DirectResize закрыт. Если вы желаете продолжить разработки, обращайтесь через ПМ для получения доступа к репозиторию на Google Code.

          [PLUGIN] DirectResize - as Maxigallery but for single images :: Download :: [url=http://modxcms.com/forums/index.php/topic,21490]
        • Thanks... i feel like and idiot now. We have a site that has all the commonly used codes that we use and we just duplicate it for each new site so it had the old info.


          quick question... it seems like phx could be used to escalate user privlege... some sites have a lot of manager users and if you put in phx code you might be able to say this page has this type of security needed etc..

          im sure there are other plugins the let you add shells.. so maybe its not a sizeble threat.
            Minneapolis Web Design <-- where i work
            MODx Templates <-- new project im working on
          • Just a friendly note, please start a new topic in the forum since PHx got a dedicated one. It’ll help to reduce the length of the specific thread, which is a bit difficult to follow... 15 pages... you know tongue

              Zaigham R - MODX Professional | Skype | Email | Twitter

              Digging the interwebs for #MODX gems and bringing it to you. modx.link
            • Good point Zi ... think I’ll lock this one down now. Create new posts here.
                Ryan Thrash, MODX Co-Founder
                Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me

              This discussion is closed to further replies. Keep calm and carry on.