Example #1
0
 public function search()
 {
     $value = $this->txt_search->getSQLNewValue();
     $this->source->setWhere(P4A_DB::singleton()->getCaseInsensitiveLikeSQL('model', "%{$value}%"))->firstRow();
     if (!$this->source->getNumRows()) {
         $this->warning("No results were found");
         $this->source->setWhere(null);
         $this->source->firstRow();
     }
 }
Example #2
0
 /**
  * Event interceptor when user moves an element to another subtree
  */
 public function onMovement()
 {
     $this->redesign();
     $table = $this->source->getTable();
     $pk = $this->source->getPk();
     $current = $this->source->fields->{$pk}->getValue();
     $field = P4A::singleton()->getObject($this->field_to_update_on_movement);
     $new_value = $field->getNormalizedNewValue();
     $receiver_path = $this->getPath($new_value, $table, $pk);
     foreach ($receiver_path as $record) {
         if ($current == $record[$pk]) {
             return;
         }
     }
     if ($this->actionHandler('beforeMovement') == ABORT) {
         return ABORT;
     }
     if ($new_value != $current) {
         if (strlen($new_value)) {
             P4A_DB::singleton($this->source->getDSN())->adapter->query("UPDATE {$table} SET {$this->recursor} = ? WHERE {$pk} = ?", array($new_value, $current));
         } else {
             P4A_DB::singleton($this->source->getDSN())->adapter->query("UPDATE {$table} SET {$this->recursor} = NULL WHERE {$pk} = ?", array($current));
         }
     }
     return $this->actionHandler('afterMovement');
 }
Example #3
0
 /**
  * Connects to the configured database.
  * Database is configured by setting P4A_DSN constant.
  */
 private function connect($DSN = "")
 {
     return P4A_DB::singleton($DSN);
 }
Example #4
0
 /**
  * @param string $fieldname
  * @param string $table
  * @param string $fk
  * @param string $fk_field
  * @return P4A_DB_Source
  */
 public function addMultivalueField($fieldname, $table = null, $fk = null, $fk_field = null)
 {
     $db = P4A_DB::singleton($this->getDSN());
     if ($table === null) {
         $table = $fieldname;
     }
     $this->_multivalue_fields[$fieldname]['table'] = $table;
     if (!$fk) {
         $pk = $this->getPk();
         if (!$pk) {
             trigger_error("Set PK before calling \"addMultivalueField\"", E_USER_ERROR);
         } elseif (is_array($pk)) {
             trigger_error("Multivalue not usable with multiple pk", E_USER_ERROR);
         } else {
             $fk = $pk;
         }
     }
     $this->_multivalue_fields[$fieldname]['fk'] = $fk;
     if (!$fk_field) {
         $p4a_db_table = new P4A_Db_Table(array('name' => $table, 'db' => $db->adapter));
         $info = $p4a_db_table->info();
         foreach ($info['metadata'] as $field_name => $field_info) {
             if ($field_name != $fk) {
                 $fk_field = $field_name;
                 break;
             }
         }
     }
     $this->_multivalue_fields[$fieldname]['fk_field'] = $fk_field;
     $this->fields->build("P4A_Data_Field", $fieldname);
     $this->fields->{$fieldname}->setDSN($this->getDSN());
     return $this;
 }
Example #5
0
 /**
  * @return string
  */
 public function getDefaultValue()
 {
     if ($this->sequence === null) {
         return $this->default_value;
     } else {
         return P4A_DB::singleton($this->getDSN())->nextSequenceId($this->sequence, $this->schema);
     }
 }
Example #6
0
File: p4a.php Project: eliudiaz/p4a
 /**
  * Never call this method if you don't know what you're doing
  */
 public function executeExternalCommands()
 {
     if (isset($_REQUEST['_p4a_session_browser'])) {
         if (!empty($_REQUEST['_p4a_session_browser']) and isset($this->objects[$_REQUEST['_p4a_session_browser']])) {
             $obj =& $this->objects[$_REQUEST['_p4a_session_browser']];
         } else {
             $obj =& $this;
         }
         $vars = get_object_vars($obj);
         ksort($vars);
         $name = $obj->getName();
         if (empty($name)) {
             $name = "P4A main object";
         }
         $name .= ' (' . get_class($obj) . ')';
         echo "<h1>{$name}</h1>";
         echo "<table border='1'>";
         echo "<tr><th>key</th><th>value</th></tr>";
         foreach ($vars as $k => $v) {
             $v = _P4A_Debug_Print_Variable($v);
             echo "<tr><td valign='top'>{$k}</td><td>{$v}</td></tr>";
         }
         echo "</table>";
         die;
     } elseif (isset($_REQUEST['_rte_file_manager']) and isset($_REQUEST['_object_id']) and isset($this->objects[$_REQUEST['_object_id']])) {
         require P4A_THEME_DIR . '/widgets/rich_textarea/filemanager/connectors/php/connector.php';
         die;
     } elseif (isset($_REQUEST['_upload_path'])) {
         $path = P4A_UPLOADS_PATH;
         if (isset($_REQUEST['_object_id']) and isset($this->objects[$_REQUEST['_object_id']])) {
             $object =& $this->objects[$_REQUEST['_object_id']];
             if ($object instanceof P4A_Field) {
                 $path .= '/' . $object->getUploadSubpath();
             }
         }
         echo preg_replace(array("~/+~", "~/\$~"), array('/', ''), $path);
         die;
     } elseif (isset($_REQUEST['_p4a_autocomplete'])) {
         if (isset($_REQUEST['_object']) and isset($_REQUEST['term']) and isset($this->objects[$_REQUEST['_object']])) {
             $object =& $this->objects[$_REQUEST['_object']];
             $db = P4A_DB::singleton($object->data_field->getDSN());
             $data =& $object->data;
             $old_where = $data->getWhere();
             $description_field = $object->getSourceDescriptionField();
             if ($object->isActionTriggered('onautocomplete')) {
                 $this->actionHandler('onautocomplete', $data, $_REQUEST['term']);
             } else {
                 $q = $db->quote($_REQUEST['term'], false);
                 $where = $db->getCaseInsensitiveLikeSQL($description_field, "%{$q}%");
                 if ($old_where) {
                     $where = "({$old_where}) AND ({$where})";
                 }
                 $data->setWhere($where);
             }
             $all = $data->getAll();
             $data->setWhere($old_where);
             $new_data = array();
             $tmp = array();
             foreach ($all as $row) {
                 $tmp[$row[$description_field]] = $row[$description_field];
             }
             ksort($tmp);
             foreach ($tmp as $k => $v) {
                 $new_data[] = array("id" => $k, "label" => htmlspecialchars($k), "value" => $k);
             }
             require_once "Zend/Json.php";
             echo Zend_Json::encode($new_data);
         }
         die;
     } elseif (isset($_REQUEST['_p4a_date_format'])) {
         echo $this->i18n->format($_REQUEST['_p4a_date_format'], 'date', null, false);
         die;
     } elseif (isset($_REQUEST['_p4a_time_format'])) {
         echo $this->i18n->format($_REQUEST['_p4a_time_format'], 'time', null, false);
         die;
     } elseif (isset($_REQUEST['_p4a_datetime_format'])) {
         echo $this->i18n->format($_REQUEST['_p4a_datetime_format'], 'datetime', null, false);
         die;
     } elseif (isset($_REQUEST['_p4a_image_thumbnail'])) {
         $image_data = explode('&', $_REQUEST['_p4a_image_thumbnail']);
         $thumb = new P4A_Thumbnail_Generator();
         $thumb->setCacheDir(P4A_UPLOADS_TMP_DIR)->setFilename(P4A_Strip_Double_Slashes(P4A_UPLOADS_DIR . $image_data[0]))->setMaxWidth($image_data[1])->setMaxHeight($image_data[1])->processFile()->outputThumbnail();
         die;
     } elseif (isset($_REQUEST['_p4a_download_file'])) {
         $file = realpath(P4A_UPLOADS_DIR . '/' . $_REQUEST['_p4a_download_file']);
         if (P4A_OS == "linux") {
             if (strpos($file, realpath(P4A_UPLOADS_DIR)) !== 0) {
                 die;
             }
         } else {
             if (stripos($file, realpath(P4A_UPLOADS_DIR)) !== 0) {
                 die;
             }
         }
         if ($file !== false and file_exists($file)) {
             $name = basename($file);
             $name = preg_replace("/^_p4a_.*?_/", "", $name);
             $gmdate = gmdate("D, d M Y H:i:s");
             header("Content-Type: text/html; charset=UTF-8");
             header("Cache-Control: no-store, no-cache, must-revalidate");
             header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
             header("Pragma: no-cache");
             header("Last-Modified: {$gmdate} GMT");
             header("Content-type: application/octet-stream");
             header("Content-Disposition: attachment; filename=\"{$name}\"");
             header("Content-Length: " . filesize($file));
             $fp = fopen($file, "rb");
             fpassthru($fp);
             fclose($fp);
             if (strpos($file, realpath(P4A_UPLOADS_TMP_DIR) . DIRECTORY_SEPARATOR . '_p4a_') === 0) {
                 unlink($file);
             }
         }
         die;
     } elseif (isset($_REQUEST['_p4a_upload_progress']) and P4A_UPLOAD_PROGRESS) {
         $upload_info = uploadprogress_get_info($_REQUEST['_p4a_upload_progress']);
         if (is_array($upload_info)) {
             $percentage = round($upload_info["bytes_uploaded"] / $upload_info["bytes_total"] * 100) . "%";
             $speed = round($upload_info["speed_average"] / 1024) . "KB/s";
             echo "{$percentage} - {$speed}";
         }
         die;
     }
 }
Example #7
0
function p4a_check_configuration($additionalDir = null)
{
    $correct = true;
    $title = "Configuration checks for \"" . P4A_APPLICATION_NAME . "\"";
    $error = "<center><h2>{$title}</h2></center>\n";
    // OPERATING SYSTEM
    $error .= "<div class='box'>Checking SERVER OPERATING SYSTEM:<br />";
    if (_DS_ == '/') {
        $error .= "P4A is configured as running on <b>Linux</b>, if your server operating system is different, than correct P4A_OS and _DS_ definition.";
    } else {
        $error .= "P4A is configured as running on <b>Windows</b>, if your server operating system is different, than correct P4A_OS and _DS_ definition.";
    }
    $error .= "</div>\n";
    // PHP VERSION
    $error .= "<div class='box'>Checking PHP VERSION: ";
    $phpversion = explode('-', PHP_VERSION);
    $phpversion = explode('.', $phpversion[0]);
    if ($phpversion[0] < 5 or $phpversion[0] == 5 and $phpversion[1] < 2) {
        $error .= "<span class='red'>{$phpversion[0]}.{$phpversion[1]}.{$phpversion[2]}</span><br />PHP 5.2.0 (or higher) is required in order to run P4A";
        $correct = false;
    } else {
        $error .= "<span class='green'>{$phpversion[0]}.{$phpversion[1]}.{$phpversion[2]}</span>";
    }
    $error .= "</div>\n";
    // DOCUMENT ROOT
    $error .= "<div class='box'>Checking DOCUMENT_ROOT: ";
    if (strlen(P4A_SERVER_DIR) == 0) {
        $error .= "<span class='red'>FAILED</span><br />Define P4A_SERVER_DIR as your DOCUMENT_ROOT.";
        $correct = false;
    } else {
        $error .= "<span class='green'>OK</span>";
    }
    $error .= "</div>";
    // UPLOADS DIRECTORY
    $error .= "<div class='box'>Checking UPLOADS DIRECTORY: ";
    if (is_dir(P4A_UPLOADS_DIR) and is_writable(P4A_UPLOADS_DIR)) {
        $ok = true;
    } elseif (!is_dir(P4A_UPLOADS_DIR)) {
        if (P4A_Mkdir_Recursive(P4A_UPLOADS_DIR)) {
            $ok = true;
        } else {
            $ok = false;
        }
    } else {
        $ok = false;
    }
    if ($ok) {
        $error .= "<span class='green'>OK</span>";
    } else {
        $error .= "<span class='red'>FAILED</span><br />Create \"" . P4A_UPLOADS_DIR . "\" and set it writable.";
        $correct = false;
    }
    $error .= "</div>";
    // UPLOADS TEMPORARY DIRECTORY
    $error .= "<div class='box'>Checking UPLOADS TEMPORARY DIRECTORY: ";
    if (is_dir(P4A_UPLOADS_TMP_DIR) and is_writable(P4A_UPLOADS_TMP_DIR)) {
        $ok = true;
    } elseif (!is_dir(P4A_UPLOADS_TMP_DIR)) {
        if (P4A_Mkdir_Recursive(P4A_UPLOADS_TMP_DIR)) {
            $ok = true;
        } else {
            $ok = false;
        }
    } else {
        $ok = false;
    }
    if ($ok) {
        $error .= "<span class='green'>OK</span>";
    } else {
        $error .= "<span class='red'>FAILED</span><br />Create \"" . P4A_UPLOADS_TMP_DIR . "\" and set it writable.";
        $correct = false;
    }
    $error .= "</div>";
    // ADDITIONAL DIRECTORY
    if ($additionalDir) {
        $error .= "<div class='box'>Checking ADDITIONAL DIRECTORY: ";
        if (is_dir($additionalDir) and is_writable($additionalDir)) {
            $ok = true;
        } elseif (!is_dir($additionalDir)) {
            if (P4A_Mkdir_Recursive($additionalDir)) {
                $ok = true;
            } else {
                $ok = false;
            }
        } else {
            $ok = false;
        }
        if ($ok) {
            $error .= "<span class='green'>OK</span>";
        } else {
            $error .= "<span class='red'>FAILED</span><br />Create \"{$additionalDir}\" and set it writable.";
            $correct = false;
        }
        $error .= "</div>";
    }
    // DATABASE CONNECTION
    $error .= "<div class='box'>Checking DATABASE CONNECTION: ";
    if (defined('P4A_DSN')) {
        try {
            P4A_DB::singleton(P4A_DSN)->adapter->getConnection();
            $error .= "<span class='green'>OK</span>";
        } catch (Exception $e) {
            $error .= "<span class='red'>FAILED</span><br />Error: " . $e->getMessage() . "<br />Check P4A_DSN definition.";
            $correct = false;
        }
    } else {
        $error .= "P4A_DSN is not defined, no database connection.";
    }
    $error .= "</div>";
    // FINAL STRINGS
    $style = "<style>body {font-family:sans-serif; font-size:90%; color:#111} h1,h2,h3,h4{text-align:center} .box{padding:10px; border:1px solid #111; background-color:#fafafa; margin-bottom:10px;} .red{color:red;font-weight:bold} .green{color:green;font-weight:bold}</style>";
    $error = "<html><head><title>{$title}</title></head><body>{$style}{$error}</body></html>";
    if ($correct) {
        return true;
    } else {
        return $error;
    }
}