public function check_email_validity($emailAddr, $giveReasonOnFail = false)
 {
     $retval = false;
     if (strlen($emailAddr)) {
         $cleaned = ToolBox::cleanString($emailAddr, 'email');
         //the length assumes an email with the smallest form being '*****@*****.**'
         $emailRegex = '/^[A-Z0-9\\._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$/i';
         if ($emailAddr == $cleaned && preg_match($emailRegex, $emailAddr)) {
             $retval = true;
         }
         if ($giveReasonOnFail == true) {
             $debug = "";
             if ($emailAddr != $cleaned) {
                 $debug .= "cleaned does NOT match original";
             }
             if (!preg_match($emailRegex, $emailAddr)) {
                 $debug .= " || regex failed";
             }
             $retval = "provided email=(" . $emailAddr . "), cleaned email=(" . $cleaned . "), retval=(" . $retval . "), debug=(" . $debug . ")";
         }
     } else {
         $details = __METHOD__ . ": no valid data provided";
         $this->logger->log_by_class($details, 'code exception');
         throw new exception($details);
     }
     $this->logger->log_by_class(__METHOD__ . ": email=(" . $emailAddr . "), retval=(" . $retval . ")", 'precheck');
     return $retval;
 }
 /**
  * The primary means of building log entries: use log_dberror() to log an 
  * error with a bit more capabilities; throws the details of the error as  
  * an exception.
  */
 public function log_by_class($details, $className = "error", $uid = NULL, array $logAttribs = NULL)
 {
     if (is_null($details) || !strlen($details)) {
         $details = "(" . __METHOD__ . ": no details given)";
     }
     if ($this->suspendLogging === true) {
         $this->pendingLogs[] = func_get_args();
         $retval = count($this->pendingLogs) - 1;
     } else {
         if (count($this->pendingLogs)) {
             $this->handle_suspended_logs();
         }
         //make sure there's a valid class name.
         if (!strlen($className) || is_null($className)) {
             $className = 'error';
         }
         //make sure we've got a uid to log under.
         if (is_null($uid) || !is_numeric($uid)) {
             //set it.
             $uid = $this->defaultUid;
         }
         //determine the log_event_id.
         try {
             $logEventId = $this->get_event_id($className);
         } catch (Exception $e) {
             throw new exception(__METHOD__ . ": while attempting to retrieve logEventId, encountered an " . "exception:::\n" . $e->getMessage() . "\n\nCLASS: {$className}\nDETAILS: {$details}");
         }
         //check to see what uid to use.
         $myUid = $this->get_uid();
         //okay, setup an array of all the data we need.
         $params = array('eventId' => ToolBox::cleanString($logEventId, 'numeric'), 'uid' => $myUid, 'affectedUid' => $uid, 'details' => $details);
         //build, run, error-checking.
         $sql = "INSERT INTO " . self::logTable . " (event_id, uid, affected_uid, details) " . " VALUES (:eventId, :uid, :affectedUid, :details)";
         try {
             $this->db->run_query($sql, $params);
             $newId = $this->db->lastInsertId(self::logTableSeq);
             if (is_numeric($newId) && $newId > 0) {
                 $retval = $newId;
                 if (is_array($logAttribs) && count($logAttribs)) {
                     $this->create_log_attributes($newId, $logAttribs);
                 }
             } else {
                 throw new exception(__METHOD__ . ": failed to insert id or invalid return (" . ToolBox::debug_var_dump($newId, 0) . ")");
             }
         } catch (exception $e) {
             throw new exception(__METHOD__ . ": error while creating log::: " . $e->getMessage());
         }
     }
     return $retval;
 }
Exemplo n.º 3
0
 /**
  * Basically, just a wrapper for create_list(), which returns a list or 
  * an array of lists, depending upon what was requested.
  * 
  * @param $array		<array> list for the array...
  * @param $style		<str,optional> what "style" it should be returned 
  *						 as (select, update, etc).
  * @param $separator	<str,optional> what separattes key from value: see each
  * 							style for more information.
  * @param $cleanString	<mixed,optional> clean the values in $array by sending it
  * 							to cleanString(), with this as the second argument.
  * @param $removeEmptyVals	<bool,optional> If $cleanString is an ARRAY and this
  * 							evaluates as TRUE, indexes of $array whose values have
  *							a length of 0 will be removed.
  */
 public static function string_from_array($array, $style = NULL, $separator = NULL, $cleanString = NULL, $removeEmptyVals = FALSE)
 {
     $retval = NULL;
     //precheck... if it's not an array, kill it.
     if (!is_array($array)) {
         return NULL;
     }
     //make sure $style is valid.
     $style = strtolower($style);
     if (is_array($array)) {
         //if $cleanString is an array, assume it's arrayIndex => cleanStringArg
         if (is_array($cleanString) && (!is_null($style) && strlen($style))) {
             $cleanStringArr = array_intersect_key($cleanString, $array);
             if (count($cleanStringArr) > 0 && is_array($cleanStringArr)) {
                 foreach ($cleanStringArr as $myIndex => $myCleanStringArg) {
                     if ($removeEmptyVals && strlen($array[$myIndex]) == 0) {
                         //remove the index.
                         unset($array[$myIndex]);
                     } else {
                         //now format it properly.
                         $myUseSqlQuotes = null;
                         if (in_array($myCleanStringArg, array('int', 'integer', 'numeric', 'number', 'decimal', 'float'))) {
                             $myUseSqlQuotes = false;
                         }
                         $array[$myIndex] = ToolBox::cleanString($array[$myIndex], $myCleanStringArg, $myUseSqlQuotes);
                         unset($myUseSqlQuotes);
                     }
                 }
             }
         }
         switch ($style) {
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             case "insert":
                 if (!$separator) {
                     $separator = " VALUES ";
                 }
                 //build temporary data...
                 $tmp = array();
                 foreach ($array as $key => $value) {
                     @($tmp[0] = ToolBox::create_list($tmp[0], $key));
                     //clean the string, if required.
                     if (is_null($value)) {
                         $value = "NULL";
                     } elseif ($cleanString) {
                         //make sure it's not full of poo...
                         $value = ToolBox::cleanString($value, "sql");
                         #$value = "'". $value ."'";
                     }
                     @($tmp[1] = ToolBox::create_list($tmp[1], $value, ",", 1));
                 }
                 //make the final product.
                 $retval = "(" . $tmp[0] . ")" . $separator . "(" . $tmp[1] . ")";
                 break;
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             case "update":
                 if (!$separator) {
                     $separator = "=";
                 }
                 //build final product.
                 foreach ($array as $field => $value) {
                     $sqlQuotes = 1;
                     if ($value === "NULL" || $value === NULL) {
                         $sqlQuotes = 0;
                     }
                     if ($cleanString && !(preg_match('/^\'/', $value) && preg_match('/\'$/', $value))) {
                         //make sure it doesn't have crap in it...
                         $value = ToolBox::cleanString($value, "sql", $sqlQuotes);
                     }
                     if ($value == "'") {
                         //Fix possible SQL-injection.
                         $value = "'\\''";
                     } elseif (!strlen($value)) {
                         $value = "''";
                     }
                     $retval = ToolBox::create_list($retval, $field . $separator . $value);
                 }
                 break;
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             case "order":
             case "limit":
                 //for creating the "limit 50 offset 35" part of a query... or at least using that "style".
                 $separator = " ";
                 //build final product.
                 foreach ($array as $field => $value) {
                     if ($cleanString) {
                         //make sure it doesn't have crap in it...
                         $value = ToolBox::cleanString($value, "sql");
                         $value = "'" . $value . "'";
                     }
                     $retval = ToolBox::create_list($retval, $value, ", ");
                 }
                 if ($style == "order" && !preg_match('/order by/', strtolower($retval))) {
                     $retval = "ORDER BY " . $retval;
                 }
                 break;
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             case "select":
                 //build final product.
                 $separator = "=";
                 foreach ($array as $field => $value) {
                     //allow for tricksie things...
                     /*
                      * Example: 
                      * string_from_array(array("y"=>3, "x" => array(1,2,3))); 
                      * 
                      * would yield: "y=3 AND (x=1 OR x=2 OR x=3)"
                      */
                     $delimiter = "AND";
                     if (is_array($value)) {
                         //doing tricksie things!!!
                         $retval = ToolBox::create_list($retval, $field . " IN (" . ToolBox::string_from_array($value) . ")", " {$delimiter} ");
                     } else {
                         //if there's already an operator ($separator), don't specify one.
                         if (preg_match('/^[\\(<=>]/', $value)) {
                             $separator = NULL;
                         }
                         if ($cleanString) {
                             //make sure it doesn't have crap in it...
                             $value = ToolBox::cleanString($value, "sql");
                         }
                         if (isset($separator)) {
                             $value = "'" . $value . "'";
                         }
                         $retval = ToolBox::create_list($retval, $field . $separator . $value, " {$delimiter} ");
                     }
                 }
                 break;
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             case "url":
                 //an array like "array('module'='todo','action'='view','ID'=164)" to "module=todo&action=view&ID=164"
                 if (!$separator) {
                     $separator = "&";
                 }
                 foreach ($array as $field => $value) {
                     if ($cleanString && !is_array($cleanString)) {
                         $value = ToolBox::cleanString($value, $cleanString);
                     }
                     $retval = ToolBox::create_list($retval, "{$field}={$value}", $separator);
                 }
                 break;
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             case "text_list":
                 if (is_null($separator)) {
                     $separator = '=';
                 }
                 foreach ($array as $field => $value) {
                     $retval = ToolBox::create_list($retval, $field . $separator . $value, "\n");
                 }
                 break;
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             case "html_list":
                 if (is_null($separator)) {
                     $separator = '=';
                 }
                 foreach ($array as $field => $value) {
                     $retval = ToolBox::create_list($retval, $field . $separator . $value, "<BR>\n");
                 }
                 break;
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
             default:
                 if (!$separator) {
                     $separator = ", ";
                 }
                 foreach ($array as $field => $value) {
                     if ($cleanString) {
                         $value = ToolBox::cleanString($value, $cleanString);
                     }
                     $retval = ToolBox::create_list($retval, $value, $separator);
                 }
                 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         }
     } else {
         //not an array.
         $retval = NULL;
     }
     return $retval;
 }